#!/bin/bash

PGHOME="/home/postgres/pgsql/master_20201002_1"
CLUSTER_PUB="/home/postgres/pgsql/master_20201002_1/data_pub"

${PGHOME}/bin/pg_ctl -D  ${CLUSTER_PUB} stop
rm -rf ${CLUSTER_PUB}
${PGHOME}/bin/initdb -D ${CLUSTER_PUB} --no-locale -E UTF8 -k

## Configuration
echo "shared_buffers = 1GB"  >> ${CLUSTER_PUB}/postgresql.conf
echo "max_locks_per_transaction = 10000" >> ${CLUSTER_PUB}/postgresql.conf
echo "synchronous_commit = off"  >> ${CLUSTER_PUB}/postgresql.conf
echo "archive_mode = off"  >> ${CLUSTER_PUB}/postgresql.conf
echo "fsync = off" >> ${CLUSTER_PUB}/postgresql.conf
echo "full_page_writes = off"  >> ${CLUSTER_PUB}/postgresql.conf
echo "autovacuum = off" >> ${CLUSTER_PUB}/postgresql.conf
echo "checkpoint_timeout = 120min" >> ${CLUSTER_PUB}/postgresql.conf
echo "max_wal_size = 20GB" >> ${CLUSTER_PUB}/postgresql.conf

echo "wal_level = logical" >> ${CLUSTER_PUB}/postgresql.conf
echo "max_wal_senders = 10" >> ${CLUSTER_PUB}/postgresql.conf
echo "wal_sender_timeout = 0" >> ${CLUSTER_PUB}/postgresql.conf
echo "wal_receiver_timeout = 0" >> ${CLUSTER_PUB}/postgresql.conf

echo "port = 5432" >> ${CLUSTER_PUB}/postgresql.conf

${PGHOME}/bin/pg_ctl -D ${CLUSTER_PUB} start

## create partition
${PGHOME}/bin/psql -p 5432 << "EOF"
DO $$
DECLARE
    i INT;
    j INT;
    schema TEXT;
    tablename TEXT;
    partname TEXT;
BEGIN
    FOR i IN 1 .. 2 LOOP
        schema := 'nsp_' || to_char(i, 'FM000');
        tablename := 'tbl_' || to_char(i, 'FM000');
        EXECUTE 'CREATE SCHEMA ' || schema;
        EXECUTE 'CREATE TABLE ' || schema || '.' || tablename || ' (
                big01 BIGINT,
                big02 BIGINT,
                big03 BIGINT,
                big04 BIGINT,
                big05 BIGINT,
                big06 BIGINT,
                big07 BIGINT,
                big08 BIGINT,
                big09 BIGINT,
                big10 BIGINT,
                big11 BIGINT,
                big12 BIGINT,
                byt01 BYTEA,
                byt02 BYTEA
        ) PARTITION BY RANGE (big02)';
        FOR j IN 1 .. 1000 LOOP
            partname := 'part_' || to_char(j, 'FM0000');
            EXECUTE 'CREATE TABLE ' || schema || '.' || partname ||
                    ' PARTITION OF ' || schema || '.' || tablename ||
                    ' FOR VALUES FROM (' || (j-1)*10 || ') TO (' || (j)*10 || ')';
            EXECUTE 'ALTER TABLE ' || schema || '.' || partname || ' ADD PRIMARY KEY (big01)';
        END LOOP;
        EXECUTE 'CREATE INDEX ON ' || schema || '.' || tablename || ' (big03)';
    END LOOP;
END;
$$ LANGUAGE plpgsql;
EOF

${PGHOME}/bin/pg_recvlogical --no-loop --create-slot --start -f - --if-not-exists \
--plugin=test_decoding --slot=test1 --dbname=postgres --username=postgres \
--option=include-timestamp | gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' > pg_recvlogical.log &

sleep 1

echo "---walsender process info---"
ps -aux | grep walsender | grep -v grep

# insert data(create relfilenodemaphash)
${PGHOME}/bin/psql -p 5432 << "EOF"
DO $$
DECLARE
    i INT;
    j INT;
    schema TEXT;
    tablename TEXT;
BEGIN
    FOR i IN 1 .. 2 LOOP
        schema := 'nsp_' || to_char(i, 'FM000');
        tablename := 'tbl_' || to_char(i, 'FM000');
        EXECUTE 'INSERT INTO ' || schema || '.' || tablename || '
         SELECT i,i,i,0,0,0,0,0,0,0,0,0,''a''::bytea,''a''::bytea FROM generate_series(0,9999,1) AS i';
    END LOOP;
END;
$$ LANGUAGE plpgsql;
EOF

echo "sleep 10 before truncate"
sleep 10

${PGHOME}/bin/psql -p 5432 << "EOF"
TRUNCATE TABLE nsp_001.tbl_001;
EOF

