RE: Potential data loss due to race condition during logical replication slot creation
Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
From: "Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
To: 'Masahiko Sawada' <sawada.mshk@gmail.com>, "Callahan, Drew" <callaan@amazon.com>
Cc: "pgsql-bugs@lists.postgresql.org" <pgsql-bugs@lists.postgresql.org>
Date: 2024-03-13T09:34:22Z
Lists: pgsql-bugs
Attachments
- master_0001-fix-snapbuild-bug-by-approach-a.patch (application/octet-stream) patch 0001
Dear hackers,
While analyzing another failure [1], I found here. I think they occurred by the
same reason.
The reported failure occurred when the replication slot is created in the middle
of the transaction and it reuses the snapshot from other slot. The reproducer is:
```
Session0
SELECT pg_create_logical_replication_slot('slot0', 'test_decoding');
BEGIN;
INSERT INTO foo ...
Session1
SELECT pg_create_logical_replication_slot('slot1', 'test_decoding');
Session2
CHECKPOINT;
SELECT pg_logical_slot_get_changes('slot0', NULL, NULL);
Session0
INSERT INTO var ... // var is defined with (user_catalog_table = true)
COMMIT;
Session1
SELECT pg_logical_slot_get_changes('slot1', NULL, NULL);
-> Assertion failure.
```
> Here is the summary of several proposals we've discussed:
> a) Have CreateInitDecodingContext() always pass need_full_snapshot =
> true to AllocateSnapshotBuilder().
> b) Have snapbuild.c being able to handle multiple SnapBuildOnDisk versions.
> c) Add a global variable, say in_create, to snapbuild.c
Regarding three options raised by Sawada-san, I preferred the approach a).
Since the issue could happen for all supported branches, we should choose the
conservative approach. Also, it is quite painful if there are some codes for
handling the same issue.
Attached patch implemented the approach a) since no one made. I also added
the test which can do assertion failure, but not sure it should be included.
[1]: https://www.postgresql.org/message-id/TYCPR01MB1207717063D701F597EF98A0CF5272%40TYCPR01MB12077.jpnprd01.prod.outlook.com
Best Regards,
Hayato Kuroda
FUJITSU LIMITED
https://www.fujitsu.com/
Commits
-
Fix possibility of logical decoding partial transaction changes.
- bb19b70081e2 18.0 landed
- 068674f4ab44 17.0 landed
- 2f3304ce133f 16.4 landed
- aee8c2b95492 15.8 landed
- f7d3caf9ddbf 14.13 landed
- cf2c69ec5a5b 13.16 landed
- 1b370758770c 12.20 landed