forked from binakot/Multi-Node-TimescaleDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-access-node.sh
executable file
·54 lines (43 loc) · 2.09 KB
/
init-access-node.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
set -e
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "SHOW config_file"
# To achieve good query performance you need to enable partitionwise aggregation on the access node. This pushes down aggregation queries to the data nodes.
# https://docs.timescale.com/v2.0/using-timescaledb/distributed-hypertables#select
# https://www.postgresql.org/docs/12/runtime-config-query.html#enable_partitionwise_aggregate
sed -ri "s!^#?(enable_partitionwise_aggregate)\s*=.*!\1 = on!" /var/lib/postgresql/data/postgresql.conf
grep "enable_partitionwise_aggregate" /var/lib/postgresql/data/postgresql.conf
# Enable PostGIS extension
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
CREATE EXTENSION IF NOT EXISTS postgis CASCADE;
CREATE EXTENSION IF NOT EXISTS postgis_topology CASCADE;
EOSQL
echo "Waiting for data nodes..."
until PGPASSWORD=$POSTGRES_PASSWORD psql -h pg_data_node_1 -U "$POSTGRES_USER" -c '\q'; do
sleep 5s
done
until PGPASSWORD=$POSTGRES_PASSWORD psql -h pg_data_node_2 -U "$POSTGRES_USER" -c '\q'; do
sleep 5s
done
echo "Connect data nodes to cluster and create distributed hypertable..."
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
CREATE TABLE telemetries
(
imei TEXT NOT NULL,
time TIMESTAMPTZ NOT NULL,
latitude DOUBLE PRECISION NOT NULL,
longitude DOUBLE PRECISION NOT NULL,
geography GEOGRAPHY(POINT, 4326) NOT NULL,
speed SMALLINT NOT NULL,
course SMALLINT NOT NULL,
CONSTRAINT telemetries_pkey PRIMARY KEY (imei, time)
);
SELECT * FROM add_data_node('data_node_1', host => 'pg_data_node_1');
SELECT * FROM add_data_node('data_node_2', host => 'pg_data_node_2');
SELECT * FROM create_distributed_hypertable(
'telemetries', 'time', 'imei',
number_partitions => 2, chunk_time_interval => INTERVAL '7 days', replication_factor => 1
);
--SELECT * FROM set_number_partitions('telemetries', 2, 'imei');
--SELECT * FROM set_chunk_time_interval('telemetries', INTERVAL '7 days');
--SELECT * FROM set_replication_factor('telemetries', 1);
EOSQL