Sub topic 9
Filtering by Operation, Row, and Column
Assume you know basic SQL, primary keys, psql, roles, and the general purpose of PostgreSQL's write-ahead log (WAL). This chapter focuses on PostgreSQL 15 and later for row and column filters. Operation filtering is available in earlier versions.
A publication controls which changes leave the publisher. You can limit changes in three ways:
- Operation filtering chooses
INSERT,UPDATE,DELETE, orTRUNCATE. - Row filtering publishes only rows that match a
WHEREcondition. - Column filtering publishes only selected columns for a table.
These filters reduce network traffic and limit the data copied to a subscriber. They can also make the subscriber an incomplete view of the publisher, so choose them deliberately.
Filter by operation
The publish option controls which change types a publication sends. For example, this publication sends new rows but ignores updates and deletes:
CREATE PUBLICATION orders_inserts_pub
FOR TABLE orders
WITH (publish = 'insert');You can list several operations:
CREATE PUBLICATION orders_changes_pub
FOR TABLE orders
WITH (publish = 'insert, update, delete');If truncate is included, a TRUNCATE on the publisher is sent as a whole-table operation. Row and column filters do not divide a truncate into individual rows.
Filter by row
A row filter is a Boolean expression evaluated for each row. PostgreSQL 15 added row filters to CREATE PUBLICATION:
CREATE PUBLICATION active_orders_pub
FOR TABLE orders
WHERE (status = 'active');This publication sends changes for rows whose status is active. The filter does not stop other rows from being changed on the publisher. It only controls what the subscriber receives.
Keep row filters simple and based on columns in the published table. A filter can cause the subscriber to contain only a partial and changing view. For example, when an order changes from active to closed, the subscriber might not receive a normal update that makes the row stop matching. Plan how the subscriber should handle such transitions.
Filter by column
A column list names the columns sent for a table. This is also available in PostgreSQL 15 and later:
CREATE PUBLICATION active_orders_pub
FOR TABLE orders (id, customer_id, status)
WHERE (status = 'active');The subscriber still needs a compatible table. Columns omitted from the publication need suitable defaults or must allow NULL, because the subscriber will not receive values for them during inserts. Keep the replica identity columns available when updates or deletes are published.
| Filter | Controls | Main limitation |
|---|---|---|
| Operation | Which change types are sent | `TRUNCATE` applies to the whole table |
| Row | Which rows are sent | The subscriber may hold a partial view |
| Column | Which values are sent | Omitted subscriber columns need compatible defaults or nullability |
Common errors include using row or column syntax on PostgreSQL versions older than 15, forgetting that filters can create incomplete data, and omitting the key columns needed to identify updated or deleted rows.
Exercise
You need a subscriber containing only active orders and only the order ID, customer ID, and status. Which publication definition should you use on PostgreSQL 15 or later?
Show answer
Use CREATE PUBLICATION active_orders_pub FOR TABLE orders (id, customer_id, status) WHERE (status = 'active');. Ensure the subscriber table can supply compatible values for omitted columns.