Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created July 25, 2024 13:47
Show Gist options
  • Save CHERTS/f9d3b626e7f3b14a89d855afbeb6c8b1 to your computer and use it in GitHub Desktop.
Save CHERTS/f9d3b626e7f3b14a89d855afbeb6c8b1 to your computer and use it in GitHub Desktop.
Create logical replication and test
-- Create publication
CREATE PUBLICATION cdc;
-- Create slot
SELECT pg_create_logical_replication_slot('test_slot_v1', 'pgoutput');
-- Create example table
CREATE TABLE replication_test_v1
(
id integer NOT NULL PRIMARY KEY,
name text
);
-- Add table to publication
ALTER PUBLICATION cdc ADD TABLE replication_test_v1;
-- Insert example data
INSERT INTO replication_test_v1(id, name) VALUES
(1, 'Number 1')
;
-- Peak changes (does not consume changes)
SELECT pg_logical_slot_peek_binary_changes('test_slot_v1', NULL, NULL, 'publication_names', 'cdc', 'proto_version', '1');
-- Get changes (consumes changes)
SELECT pg_logical_slot_get_binary_changes('test_slot_v1', NULL, NULL, 'publication_names', 'cdc', 'proto_version', '1');
-- Drop slot
-- SELECT pg_drop_replication_slot ('test_slot_v1');
CREATE SUBSCRIPTION mysub
CONNECTION <conn stuff>
PUBLICATION cdc
WITH (slot_name=test_slot_v1, create_slot=false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment