Create stored functions after creating distributed hypertables

This commit is contained in:
Prathamesh Musale 2022-05-02 10:03:25 +05:30
parent 06d9ef96e7
commit 70cf01ff27
10 changed files with 95 additions and 57 deletions

View File

@ -12,13 +12,13 @@ Schemas and utils for IPLD ETH Postgres database
docker-compose down -v --remove-orphans
```
* Spin up an access node and two data nodes using [docker-compose.test.yml](./docker-compose.test.yml):
* Spin up an access node and three data nodes using [docker-compose.test.yml](./docker-compose.test.yml):
```bash
docker-compose -f docker-compose.test.yml up timescale-test-db pg_data_node_1 pg_data_node_2
docker-compose -f docker-compose.test.yml up timescale-test-db pg_data_node_1 pg_data_node_2 pg_data_node_3
```
Following final output should be seen on each node:
Following final output should be seen on all the nodes:
```
LOG: TimescaleDB background worker launcher connected to shared catalogs
@ -27,7 +27,7 @@ Schemas and utils for IPLD ETH Postgres database
* Edit [startup_script.sh](./scripts/startup_script.sh) to change the number of migrations to be run:
```bash
./goose -dir migrations/vulcanizedb postgres "$VDB_PG_CONNECT" up-to 21
./goose -dir migrations/vulcanizedb postgres "$VDB_PG_CONNECT" up-to 23
```
* In another `ipld-eth-db` terminal window, build an image `migrations-test` using [Dockerfile](./db/Dockerfile):

View File

@ -55,13 +55,13 @@ DROP TABLE eth.header_cids;
DROP TABLE public.blocks;
-- rename new tables
ALTER TABLE eth.log_cids_i RENAME TO eth.log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO eth.access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO eth.state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO eth.storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO eth.state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO eth.receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO eth.transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO eth.uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO eth.header_cids;
ALTER TABLE public.blocks_i RENAME TO public.blocks;
ALTER TABLE eth.log_cids_i RENAME TO log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO header_cids;
ALTER TABLE public.blocks_i RENAME TO blocks;

View File

@ -1,34 +1,16 @@
-- +goose NO TRANSACTION
-- +goose Up
-- this is generated by a script
SELECT add_data_node('dn3', host => 'pg_data_node_3', port => 5432, database => 'vulcanize_testing_v4', password => 'password');
SELECT add_data_node('dn2', host => 'pg_data_node_2', port => 5432, database => 'vulcanize_testing_v4', password => 'password');
-- SELECT attach_data_node('dn2', 'eth.log_cids', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.access_list_elements', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.state_accounts', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.storage_cids', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.state_cid', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.receipt_cids', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.transaction_cids', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.uncle_cids', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'eth.header_cid', if_not_attached => true);
-- SELECT attach_data_node('dn2', 'public.blocks', if_not_attached => true);
SELECT add_data_node('dn1', host => 'pg_data_node_1', port => 5432, database => 'vulcanize_testing_v4', password => 'password');
-- SELECT attach_data_node('dn1', 'eth.log_cids', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.access_list_elements', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.state_accounts', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.storage_cids', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.state_cid', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.receipt_cids', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.transaction_cids', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.uncle_cids', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'eth.header_cid', if_not_attached => true);
-- SELECT attach_data_node('dn1', 'public.blocks', if_not_attached => true);
CALL distributed_exec($$ CREATE SCHEMA eth $$);
-- +goose Down
-- this is generated by a script
CALL distributed_exec($$ DROP SCHEMA eth $$);
-- SELECT detach_data_node('dn1', force => true, if_attached = true);
SELECT delete_data_node('dn1', force => true, if_attached => true);
-- SELECT detach_data_node('dn2', force => true, if_attached = true);
SELECT delete_data_node('dn2', force => true, if_attached => true);
SELECT delete_data_node('dn3', force => true, if_attached => true);

View File

@ -48,16 +48,26 @@ DROP TABLE eth.header_cids;
DROP TABLE public.blocks;
-- rename distributed hypertables
ALTER TABLE eth.log_cids_i RENAME TO eth.log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO eth.access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO eth.state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO eth.storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO eth.state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO eth.receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO eth.transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO eth.uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO eth.header_cids;
ALTER TABLE public.blocks_i RENAME TO public.blocks;
ALTER TABLE eth.log_cids_i RENAME TO log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO header_cids;
ALTER TABLE public.blocks_i RENAME TO blocks;
CALL distributed_exec('ALTER TABLE eth.log_cids_i RENAME TO log_cids');
CALL distributed_exec('ALTER TABLE eth.access_list_elements_i RENAME TO access_list_elements');
CALL distributed_exec('ALTER TABLE eth.state_accounts_i RENAME TO state_accounts');
CALL distributed_exec('ALTER TABLE eth.storage_cids_i RENAME TO storage_cids');
CALL distributed_exec('ALTER TABLE eth.state_cids_i RENAME TO state_cids');
CALL distributed_exec('ALTER TABLE eth.receipt_cids_i RENAME TO receipt_cids');
CALL distributed_exec('ALTER TABLE eth.transaction_cids_i RENAME TO transaction_cids');
CALL distributed_exec('ALTER TABLE eth.uncle_cids_i RENAME TO uncle_cids');
CALL distributed_exec('ALTER TABLE eth.header_cids_i RENAME TO header_cids');
CALL distributed_exec('ALTER TABLE public.blocks_i RENAME TO blocks');
-- update version
INSERT INTO public.db_version (singleton, version) VALUES (true, 'v4.0.00-dh')
@ -116,13 +126,13 @@ DROP TABLE eth.header_cids;
DROP TABLE public.blocks;
-- rename hypertable tables
ALTER TABLE eth.log_cids_i RENAME TO eth.log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO eth.access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO eth.state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO eth.storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO eth.state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO eth.receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO eth.transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO eth.uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO eth.header_cids;
ALTER TABLE public.blocks_i RENAME TO public.blocks;
ALTER TABLE eth.log_cids_i RENAME TO log_cids;
ALTER TABLE eth.access_list_elements_i RENAME TO access_list_elements;
ALTER TABLE eth.state_accounts_i RENAME TO state_accounts;
ALTER TABLE eth.storage_cids_i RENAME TO storage_cids;
ALTER TABLE eth.state_cids_i RENAME TO state_cids;
ALTER TABLE eth.receipt_cids_i RENAME TO receipt_cids;
ALTER TABLE eth.transaction_cids_i RENAME TO transaction_cids;
ALTER TABLE eth.uncle_cids_i RENAME TO uncle_cids;
ALTER TABLE eth.header_cids_i RENAME TO header_cids;
ALTER TABLE public.blocks_i RENAME TO blocks;

View File

@ -0,0 +1,27 @@
-- +goose Up
-- to be generated by a script
SELECT set_replication_factor('public.blocks', 3);
SELECT set_replication_factor('eth.header_cids', 3);
SELECT set_replication_factor('eth.uncle_cids', 3);
SELECT set_replication_factor('eth.transaction_cids', 3);
SELECT set_replication_factor('eth.receipt_cids', 3);
SELECT set_replication_factor('eth.state_cids', 3);
SELECT set_replication_factor('eth.storage_cids', 3);
SELECT set_replication_factor('eth.state_accounts', 3);
SELECT set_replication_factor('eth.access_list_elements', 3);
SELECT set_replication_factor('eth.log_cids', 3);
-- +goose Down
-- to be generated by a script
SELECT set_replication_factor('public.blocks', 1);
SELECT set_replication_factor('eth.header_cids', 1);
SELECT set_replication_factor('eth.uncle_cids', 1);
SELECT set_replication_factor('eth.transaction_cids', 1);
SELECT set_replication_factor('eth.receipt_cids', 1);
SELECT set_replication_factor('eth.state_cids', 1);
SELECT set_replication_factor('eth.storage_cids', 1);
SELECT set_replication_factor('eth.state_accounts', 1);
SELECT set_replication_factor('eth.access_list_elements', 1);
SELECT set_replication_factor('eth.log_cids', 1);

View File

@ -58,3 +58,17 @@ services:
- "127.0.0.1:8068:5432"
volumes:
- ./scripts/init-data-node.sh:/docker-entrypoint-initdb.d/init-data-node.sh
pg_data_node_3:
image: timescale/timescaledb:latest-pg14
container_name: pg_data_node_3
restart: unless-stopped
command: ["postgres", "-c", "log_statement=all"]
environment:
POSTGRES_USER: "postgres"
POSTGRES_DB: "vulcanize_testing_v4"
POSTGRES_PASSWORD: "password"
ports:
- "127.0.0.1:8069:5432"
volumes:
- ./scripts/init-data-node.sh:/docker-entrypoint-initdb.d/init-data-node.sh

0
scripts/init-access-node.sh Normal file → Executable file
View File

View File

@ -14,6 +14,11 @@ grep "max_prepared_transactions" /var/lib/postgresql/data/postgresql.conf
sed -ri "s!^#?(statement_timeout)\s*=.*!\1 = 0!" /var/lib/postgresql/data/postgresql.conf
grep "statement_timeout" /var/lib/postgresql/data/postgresql.conf
# On the data nodes, set the wal_level to logical or higher to move or copy chunks between data nodes.
# https://www.postgresql.org/docs/14/runtime-config-wal.html
sed -ri "s!^#?(wal_level)\s*=.*!\1 = 'logical'!" /var/lib/postgresql/data/postgresql.conf
grep "wal_level" /var/lib/postgresql/data/postgresql.conf
# https://docs.timescale.com/timescaledb/latest/how-to-guides/multinode-timescaledb/multinode-auth/
# https://docs.timescale.com/timescaledb/latest/how-to-guides/multinode-timescaledb/multinode-auth/#password-authentication

View File

@ -8,7 +8,7 @@ VDB_PG_CONNECT=postgresql://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOSTNAME
# Run the DB migrations
echo "Connecting with: $VDB_PG_CONNECT"
echo "Running database migrations"
./goose -dir migrations/vulcanizedb postgres "$VDB_PG_CONNECT" up-to 21
./goose -dir migrations/vulcanizedb postgres "$VDB_PG_CONNECT" up-to 23
# If the db migrations ran without err
if [[ $? -eq 0 ]]; then