Re: row filtering for logical replication
Euler Taveira <euler@eulerto.com>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Release cache tuple when no longer needed
- ed0fbc8e5ac9 15.0 landed
-
Add some additional tests for row filters in logical replication.
- ceb57afd3ce1 15.0 landed
-
Fix one of the tests introduced in commit 52e4f0cd47.
- cfb4e209ec15 15.0 landed
-
Allow specifying row filters for logical replication of tables.
- 52e4f0cd472d 15.0 landed
-
Move scanint8() to numutils.c
- cfc7191dfea3 15.0 cited
-
Replace Test::More plans with done_testing
- 549ec201d613 15.0 cited
-
Reduce relcache access in WAL sender streaming logical changes
- 6ce16088bfed 15.0 cited
-
Small cleanups related to PUBLICATION framework code
- c9105dd3660f 15.0 cited
-
Add a view to show the stats of subscription workers.
- 8d74fc96db5f 15.0 cited
-
Allow publishing the tables of schema.
- 5a2832465fd8 15.0 cited
-
Doc: improve documentation of CREATE/ALTER SUBSCRIPTION.
- 1882d6cca161 15.0 cited
-
Add PublicationTable and PublicationRelInfo structs
- 0c6828fa987b 15.0 cited
-
Remove unused argument "txn" in maybe_send_schema().
- 93d573d86571 15.0 cited
-
Add prepare API support for streaming transactions in logical replication.
- 63cf61cdeb7b 15.0 cited
-
Unify PostgresNode's new() and get_new_node() methods
- 201a76183e20 15.0 cited
-
Use l*_node() family of functions where appropriate
- 2b00db4fb0c7 15.0 cited
-
Add support for prepared transactions to built-in logical replication.
- a8fd13cab0ba 15.0 cited
-
Restore the portal-level snapshot after procedure COMMIT/ROLLBACK.
- ef9480509622 11.13 cited
-
Rename a parse node to be more general
- 91d1f2d30210 14.0 landed
-
Remove unused column atttypmod from initial tablesync query
- 4ad31bb2ef25 14.0 landed
-
SEARCH and CYCLE clauses
- 3696a600e229 14.0 cited
Attachments
- 0001-Row-filter-for-logical-replication.patch (text/x-patch) patch 0001
On Fri, Dec 3, 2021, at 8:12 PM, Euler Taveira wrote: > PS> I will update the commit message in the next version. I barely changed the > documentation to reflect the current behavior. I probably missed some changes > but I will fix in the next version. I realized that I forgot to mention a few things about the UPDATE behavior. Regardless of 0003, we need to define which tuple will be used to evaluate the row filter for UPDATEs. We already discussed it circa [1]. This current version chooses *new* tuple. Is it the best choice? Let's check all cases. There are 2 rows on the provider. One row satisfies the row filter and the other one doesn't. For each case, I expect the initial rows to be there (no modifications). The DDLs are: CREATE TABLE foo (a integer, b text, PRIMARY KEY(a)); INSERT INTO foo (a, b) VALUES(10, 'abc'),(30, 'abc'); CREATE PUBLICATION bar FOR TABLE foo WHERE (a > 20); The table describes what happen on the subscriber. BEFORE is the current row on subscriber. OLD, NEW and OLD & NEW are action/row if we consider different ways to evaluate the row filter. -- case 1: old tuple (10, abc) ; new tuple (10, def) UPDATE foo SET b = 'def' WHERE a = 10; +-----------+--------------------+------------------+------------------+ | BEFORE | OLD | NEW | OLD & NEW | +-----------+--------------------+------------------+------------------+ | NA | NA | NA | NA | +-----------+--------------------+------------------+------------------+ If the old and new tuple don't satisfy the row filter, there is no issue. -- case 2: old tuple (30, abc) ; new tuple (30, def) UPDATE foo SET b = 'def' WHERE a = 30; +-----------+--------------------+------------------+------------------+ | BEFORE | OLD | NEW | OLD & NEW | +-----------+--------------------+------------------+------------------+ | (30, abc) | UPDATE (30, def) | UPDATE (30, def) | UPDATE (30, def) | +-----------+--------------------+------------------+------------------+ If the old and new tuple satisfy the row filter, there is no issue. -- case 3: old tuple (30, abc) ; new tuple (10, def) UPDATE foo SET a = 10, b = 'def' WHERE a = 30; +-----------+--------------------+------------------+------------------+ | BEFORE | OLD | NEW | OLD & NEW | +-----------+--------------------+------------------+------------------+ | (30, abc) | UPDATE (10, def) * | KEEP (30, abc) * | KEEP (30, abc) * | +-----------+--------------------+------------------+------------------+ If the old tuple satisfies the row filter but the new tuple doesn't, we have a data consistency issue. Since the old tuple satisfies the row filter, the initial table synchronization copies this row. However, after the UPDATE the new tuple doesn't satisfy the row filter then, from the data consistency perspective, that row should be removed on the subscriber. The OLD sends the UPDATE because it satisfies the row filter (if it is a sharding solution this new row should be moved to another node). The new row would likely not be modified by replication again. That's a data inconsistency according to the row filter. The NEW and OLD & NEW don't send the UPDATE because it doesn't satisfy the row filter. Keep the old row is undesirable because it doesn't reflect what we have on the source. This row on the subscriber would likely not be modified by replication again. If someone inserted a new row with a = 30, replication will stop because there is already a row with that value. -- case 4: old tuple (10, abc) ; new tuple (30, def) UPDATE foo SET a = 30, b = 'def' WHERE a = 10; +-----------+--------------------+------------------+------------------+ | BEFORE | OLD | NEW | OLD & NEW | +-----------+--------------------+------------------+------------------+ | NA | NA ! | NA ! | NA | +-----------+--------------------+------------------+------------------+ The OLD and OLD & NEW don't send the UPDATE because it doesn't satisfy the row filter. The NEW sends the UPDATE because it satisfies the row filter but there is no row to modify. The current behavior does nothing. However, it should INSERT the new tuple. Subsequent UPDATE or DELETE have no effect. It could be a surprise for an application that expects the same data set from the provider. If we have to choose the default behavior I would say use the old tuple for evaluates row filter. Why? The validation already restricts the columns to replica identity so there isn't an issues with missing (NULL) columns. The case 3 updates the row with a value that is not consistent but keeping the old row is worse because it could stop the replication if someone inserted the old key in a new row on the provider. The case 4 ignores the UPDATE if it cannot find the tuple but it could provide an error if there was an strict mode. Since this change is very simple to revert, this new version contains this modification. I also improve the documentation, remove extra parenthesis from psql/pg_dump. As I said in the previous email, I merged the validation patch too. FWIW in the previous version, I removed a code that compares nodes to decide if it is necessary to remove the publication-relation entry. I had a similar code in a ancient version of this patch but decided that the additional code is not worth. There is at least one issue in the current code that should be addressed: PK or REPLICA IDENTITY modification could break the publication check for UPDATEs and DELETEs. [1] https://postgr.es/m/202107162135.m5ehijgcasjk@alvherre.pgsql -- Euler Taveira EDB https://www.enterprisedb.com/