Sub topic 9
Filtering by operation, row, and column
This chapter assumes you can create a publication and subscription (earlier chapters) and know what replica identity is. Row filters and column lists need PostgreSQL 15 or later on the publisher. The publish option works in older versions.
Three levels of filtering
A publication sends every change on its tables by default. You can narrow that on three axes.
| Filter | Syntax | Minimum version | What it limits |
|---|---|---|---|
| Operation | `WITH (publish = '...')` | Before 15 | Which of INSERT, UPDATE, DELETE, TRUNCATE are sent |
| Row | `WHERE (condition)` | 15 | Which rows are sent |
| Column | `(col1, col2)` after the table name | 15 | Which columns of each row are sent |
All three are set on the publisher. The subscriber needs no special setup, but its table must contain every published column.
Filtering by operation
The publish option takes a comma-separated list. The default is insert, update, delete, truncate.
-- On the publisher
CREATE PUBLICATION orders_feed_pub
FOR TABLE orders
WITH (publish = 'insert, update');
-- Change it later
ALTER PUBLICATION orders_feed_pub SET (publish = 'insert');Operations you leave out are not sent at all. If you omit delete, deleting a row on the publisher leaves it on the subscriber. This suits archive or audit copies, where the subscriber should keep history.
A publication that publishes only inserts has no replica identity requirement, because inserts never need to locate an existing row.
Filtering by row
A row filter is a WHERE clause attached to a table in the publication.
-- Requires PostgreSQL 15 or later
CREATE PUBLICATION big_orders_pub
FOR TABLE orders WHERE (total >= 1000);The filter is evaluated on the publisher for each change. During initial synchronization, only rows that satisfy the filter are copied (when copy_data = true).
How UPDATE is treated
An UPDATE has an old row and a new row, and the filter is checked against both. The result depends on which versions match.
This keeps the subscriber consistent with the filter. A row that moves into the filtered set appears on the subscriber as an INSERT. A row that leaves it is deleted there.
Restrictions on the expression
- It may use only columns of the table it is attached to.
- It may call only built-in immutable functions and operators. User-defined functions, types, operators, and collations are rejected.
- It does not apply to TRUNCATE. A truncate empties the whole table on the subscriber.
- For UPDATE and DELETE, every column in the filter must be part of the replica identity (next section).
Filtering by column
A column list names the columns to send.
-- Requires PostgreSQL 15 or later
CREATE PUBLICATION orders_slim_pub
FOR TABLE orders (id, customer_id, status);On the subscriber, the table must have at least these columns. Extra subscriber columns get their default value, or NULL when there is no default.
-- On the subscriber
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
note text -- local column, not published
);If the subscriber table has a NOT NULL column with no default that the publication does not supply, the apply worker fails on the first insert.
Column lists are useful for leaving out sensitive or bulky columns. They are not an access control mechanism. A subscriber that connects to the publisher can still read the table with the privileges of its role through other means.
You can combine a column list and a row filter:
CREATE PUBLICATION active_orders_pub
FOR TABLE orders (id, customer_id, status) WHERE (status = 'active');The replica identity rule
The most common error with these features comes from replica identity. If a publication publishes UPDATE or DELETE, PostgreSQL requires two things:
- Every column used in the row filter must be in the table's replica identity.
- The column list, if present, must include all replica identity columns.
The active_orders_pub example above publishes updates and deletes, and status is not in the primary key. The error appears on the publisher, when someone updates the table, not when you create the publication:
-- On the publisher, after creating active_orders_pub
UPDATE orders SET total = 120.00 WHERE id = 7;
-- ERROR: cannot update table "orders"
-- DETAIL: Column used in the publication WHERE expression is not part of the replica identity.This means an application write fails because of a replication setting. Test filters on a staging system before you apply them to a busy table.
You have three ways to fix it:
-- Option 1: filter on the key column only
CREATE PUBLICATION low_id_orders_pub
FOR TABLE orders WHERE (id < 100000);
-- Option 2: publish only inserts, which have no identity requirement
CREATE PUBLICATION active_orders_pub
FOR TABLE orders (id, customer_id, status) WHERE (status = 'active')
WITH (publish = 'insert');
-- Option 3: use all columns as the identity (more WAL per update and delete)
ALTER TABLE orders REPLICA IDENTITY FULL;Option 3 has a catch. With REPLICA IDENTITY FULL, the identity is every column, so a column list that omits any column is rejected for updates and deletes. For a non-key filter column and a partial column list, you must publish inserts only.
Changing filters on an existing publication
ALTER PUBLICATION ... SET TABLE replaces the table's definition, including its filter and column list.
ALTER PUBLICATION big_orders_pub
SET TABLE orders WHERE (total >= 500);The new filter applies to changes made after the command. Rows already copied to the subscriber are not re-evaluated, so rows with total between 500 and 999 do not appear until they change. To backfill, copy those rows yourself, or drop the table from the subscription's tracking and resynchronize it. Do this in a maintenance window, because it needs care to avoid duplicate-key errors.
Changing a column list has the same limit. If you add a column to the list, the subscriber needs that column first, and old rows have NULL there until they are updated.
Inspecting what a publication sends
Use pg_publication_tables, which exposes both the column list and the filter in PostgreSQL 15 and later.
SELECT pubname, tablename, attnames, rowfilter
FROM pg_publication_tables
WHERE pubname IN ('orders_slim_pub', 'active_orders_pub');In psql, \dRp+ active_orders_pub shows the same information, plus the published operations.
Edge cases
- Multiple publications for one table. If a subscription uses several publications that include the same table, row filters are combined with OR. A row is sent if any publication's filter accepts it. If any of those publications has no filter, all rows are sent.
- Different column lists. PostgreSQL 15 does not support a subscription that receives the same table through publications with different column lists. Keep one column list per table per subscription.
- Partitioned tables. With
publish_via_partition_root = false(the default), each partition's own filter and column list are used. Withtrue, the root table's are used. Settings on a partition are ignored in that case. - Schema publications. You cannot use a column list on a table when the same publication also uses
FOR TABLES IN SCHEMA.FOR ALL TABLESalso cannot carry filters. - Filters are not security. A filtered publication reduces what is sent, but a subscription role with enough rights on the publisher can read the table directly.
Exercise
A table tickets(id bigint primary key, state text, body text) has the default replica identity. You create CREATE PUBLICATION open_pub FOR TABLE tickets WHERE (state = 'open');. Creation succeeds, but an application's UPDATE tickets SET body = 'x' WHERE id = 1 then fails. Why, and what are two ways to fix it?
Show answer
The publication publishes updates, and the filter uses state, which is not part of the replica identity (the primary key id). PostgreSQL rejects the UPDATE on the publisher. Fixes: set ALTER TABLE tickets REPLICA IDENTITY FULL (no column list may then omit columns), or recreate the publication with WITH (publish = 'insert'), or filter on id instead of state.
Exercise
A publication has the filter WHERE (status = 'active') with replica identity FULL. A row is updated on the publisher from status = 'pending' to status = 'active'. What operation does the subscriber receive, and what does it receive if the row changes from 'active' to 'closed'?
Show answer
The first change reaches the subscriber as an INSERT, because only the new row matches the filter. The second reaches it as a DELETE, because only the old row matches.