v1
This commit is contained in:
commit
ba69e7c774
7 files changed
+361
No files matched your search
@@ -0,0 +1,238 @@
|
||||
Let's make a logger.
|
||||
|
||||
CREATE TABLE ddl_history (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
event_time TIMESTAMPTZ DEFAULT now(),
|
||||
command TEXT,
|
||||
object_type TEXT,
|
||||
object_name TEXT
|
||||
);
|
||||
|
||||
Create the function:
|
||||
|
||||
CREATE OR REPLACE FUNCTION capture_ddl()
|
||||
RETURNS event_trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
INSERT INTO ddl_history (
|
||||
command,
|
||||
object_type,
|
||||
object_name
|
||||
)
|
||||
SELECT
|
||||
TG_TAG,
|
||||
object_type,
|
||||
object_identity
|
||||
FROM pg_event_trigger_ddl_commands();
|
||||
END;
|
||||
$$;
|
||||
|
||||
DROP TABLE
|
||||
|
||||
CREATE OR REPLACE FUNCTION capture_drop()
|
||||
RETURNS event_trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
INSERT INTO ddl_log (
|
||||
event_time,
|
||||
command,
|
||||
object_type,
|
||||
object_name
|
||||
)
|
||||
SELECT
|
||||
now(),
|
||||
TG_TAG,
|
||||
object_type,
|
||||
object_identity
|
||||
FROM pg_event_trigger_dropped_objects();
|
||||
END;
|
||||
$$;
|
||||
|
||||
|
||||
Create the trigger:
|
||||
|
||||
CREATE EVENT TRIGGER capture_ddl_trigger
|
||||
ON ddl_command_end
|
||||
EXECUTE FUNCTION capture_ddl();
|
||||
|
||||
Now test:
|
||||
|
||||
CREATE TABLE products (
|
||||
id INT PRIMARY KEY,
|
||||
name TEXT
|
||||
);
|
||||
|
||||
Check:
|
||||
|
||||
SELECT * FROM ddl_history;
|
||||
|
||||
|
||||
|
||||
docker-compose.yml for LOGICAL REPLICATION MODE
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15
|
||||
container_name: cluster1Node1
|
||||
|
||||
restart: unless-stopped
|
||||
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: postgres
|
||||
|
||||
ports:
|
||||
- "5444:5432"
|
||||
|
||||
volumes:
|
||||
- ./postgres_data:/var/lib/postgresql/data
|
||||
|
||||
command:
|
||||
- postgres
|
||||
- -c
|
||||
- wal_level=logical
|
||||
- -c
|
||||
- max_replication_slots=10
|
||||
- -c
|
||||
- max_wal_senders=10
|
||||
|
||||
|
||||
sudo docker compose down
|
||||
sudo docker compose up -d
|
||||
|
||||
|
||||
SHOW wal_level;
|
||||
|
||||
psql -h 172.16.10.123 -p 5444 -U postgres
|
||||
|
||||
SHOW max_replication_slots;
|
||||
SHOW max_wal_senders;
|
||||
|
||||
|
||||
CREATE TABLE users (
|
||||
id INT PRIMARY KEY,
|
||||
name TEXT
|
||||
);
|
||||
|
||||
|
||||
SELECT *
|
||||
FROM pg_create_logical_replication_slot(
|
||||
'myslot',
|
||||
'test_decoding'
|
||||
);
|
||||
|
||||
INSERT INTO users VALUES (1, 'Alice');
|
||||
|
||||
SELECT *
|
||||
FROM pg_logical_slot_peek_changes(
|
||||
'myslot',
|
||||
NULL,
|
||||
NULL
|
||||
);
|
||||
|
||||
|
||||
|===================================================|
|
||||
| psql -h 127.0.0.1 -p 5444 -U postgres -d postgres |
|
||||
|===================================================|
|
||||
command:
|
||||
- postgres
|
||||
- -c
|
||||
- wal_level=logical
|
||||
- -c
|
||||
- shared_preload_libraries=spock
|
||||
- -c
|
||||
- max_replication_slots=10
|
||||
- -c
|
||||
- max_wal_senders=10
|
||||
- -c
|
||||
- track_commit_timestamp=on
|
||||
|
||||
psql -h 127.0.0.1 -p 5444 -U postgres -d postgres
|
||||
|
||||
sudo nano postgres_data/pg_hba.conf
|
||||
|
||||
host all all 172.19.0.0/16 scram-sha-256
|
||||
host all all 172.16.10.0/24 scram-sha-256
|
||||
|
||||
sudo docker exec -it cluster1Node1 psql -U postgres -d postgres
|
||||
ALTER USER postgres WITH PASSWORD 'postgres';
|
||||
|
||||
sudo docker exec -it cluster1Node3 bash
|
||||
psql -U postgres -c "SHOW hba_file;"
|
||||
psql -U postgres -c "SELECT pg_reload_conf();"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|==========|
|
||||
|LINK Nodes|
|
||||
|==========|
|
||||
SELECT spock.sub_create(
|
||||
subscription_name := 'node2_from_node1',
|
||||
provider_dsn := 'host=172.16.10.123 port=5444 dbname=postgres user=postgres password=postgres'
|
||||
);
|
||||
|
||||
|=============|
|
||||
|Register Node|
|
||||
|=============|
|
||||
psql -h localhost -p 5444 -U postgres -d postgres
|
||||
CREATE EXTENSION IF NOT EXISTS spock;
|
||||
SELECT extname, extversion
|
||||
FROM pg_extension
|
||||
WHERE extname = 'spock';
|
||||
SELECT spock.node_create(
|
||||
node_name := 'node1',
|
||||
dsn := 'host=172.16.10.123 port=5444 dbname=postgres user=postgres password=postgres'
|
||||
);
|
||||
SELECT * FROM spock.node;
|
||||
|
||||
|================|
|
||||
| CREATE A table |
|
||||
|================|
|
||||
CREATE TABLE test_replication (
|
||||
id bigint PRIMARY KEY,
|
||||
source_node text,
|
||||
message text
|
||||
);
|
||||
|
||||
SELECT spock.repset_add_table(
|
||||
'default',
|
||||
'test_replication'
|
||||
);
|
||||
|
||||
|===========|
|
||||
|Turn On DDL|
|
||||
|===========|
|
||||
|
||||
CREATE TABLE customers (
|
||||
id bigint PRIMARY KEY,
|
||||
name text
|
||||
);
|
||||
ALTER SYSTEM SET spock.enable_ddl_replication = on;
|
||||
ALTER SYSTEM SET spock.include_ddl_repset = on;
|
||||
|
||||
SELECT pg_reload_conf();
|
||||
|
||||
SHOW spock.enable_ddl_replication;
|
||||
SHOW spock.include_ddl_repset;
|
||||
|
||||
|======================================================|
|
||||
| HOW TO INSERT OR PUT AUTO GEN OF ID IN EXISTING TABLE|
|
||||
|======================================================|
|
||||
ALTER TABLE test_replication
|
||||
ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;
|
||||
|
||||
|
||||
HOW TO UPADE A NODE CONFIG
|
||||
|
||||
UPDATE spock.local_node
|
||||
SET node_local_interface = (
|
||||
SELECT if_id
|
||||
FROM spock.node_interface
|
||||
WHERE if_name = 'node2_local'
|
||||
AND if_nodeid = 13534
|
||||
);
|
||||
Reference in new issue
Block a user