Sub topic 4
Publishing Tables with `CREATE PUBLICATION`
This chapter assumes you know basic SQL, primary keys, PostgreSQL roles, and the general purpose of the write-ahead log (WAL). You should also know how to connect to a database with psql. A publication is a named set of tables and row changes that a PostgreSQL publisher makes available to subscribers.
How a publication fits into logical replication
Logical replication sends row-level changes. A publication defines which changes are eligible to leave the publisher. A subscription on another database reads that stream and applies the changes there.
A publication does not copy existing rows by itself. When a subscription is created, the subscriber usually performs an initial table copy and then receives later changes from the publication.
Creating a publication
Create the table on the publisher, then add it to a publication:
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total numeric(10,2) NOT NULL
);
CREATE PUBLICATION orders_pub
FOR TABLE orders;This publication includes inserts, updates, deletes, and truncates by default. The table's primary key gives PostgreSQL a normal identity for locating rows during updates and deletes.
A publication can contain several tables:
CREATE PUBLICATION shop_pub
FOR TABLE orders, order_items, customers;You can also publish every table in a schema:
CREATE PUBLICATION reporting_pub
FOR TABLES IN SCHEMA reporting;The publisher's database administrator must grant suitable privileges. The role creating a publication generally needs CREATE privilege on the database. Adding a table normally requires ownership of that table, although superusers and roles with suitable membership can perform the operation.
Choosing the operations to publish
Use the publish option when a subscriber needs only particular operation types:
CREATE PUBLICATION orders_inserts_pub
FOR TABLE orders
WITH (publish = 'insert');Valid operation names include insert, update, delete, and truncate. Separate names with commas:
CREATE PUBLICATION order_changes_pub
FOR TABLE orders
WITH (publish = 'insert, update, delete');Excluding operations reduces traffic, but it can leave the subscriber incomplete. For example, publishing inserts without updates means a changed order remains stale on the subscriber.
Filtering columns and rows
PostgreSQL 15 and later support column lists and row filters. This publication sends only selected columns and active rows:
CREATE PUBLICATION active_orders_pub
FOR TABLE orders (id, customer_id, status)
WHERE (status = 'active');Column filtering can help protect sensitive values such as payment details. Row filtering can reduce the data sent to a regional or reporting subscriber. The subscriber table must still have a compatible structure, and filtering rules have restrictions. For example, a row filter cannot use volatile functions.
Changing a publication
You can add or remove tables later:
ALTER PUBLICATION orders_pub ADD TABLE order_items;
ALTER PUBLICATION orders_pub DROP TABLE orders;These commands change what future changes are published. They do not automatically create or alter tables on the subscriber. The subscriber must have a compatible table, and its subscription must be refreshed when a newly added table should be synchronized.
You can inspect publications with:
SELECT pubname, puballtables, pubinsert, pubupdate, pubdelete, pubtruncate
FROM pg_publication;Exercise
Does CREATE PUBLICATION orders_pub FOR TABLE orders copy the existing rows in orders to another database?
Show answer
No. It defines the change stream. A subscription performs the initial copy and then applies later changes.