Skip to content

Commit a49927f

Browse files
pg_logicalinspect: Stabilize isolation tests.
The previous isolation tests did not account for the possibility that the background writer or the checkpointer could write a RUNNING_XACTS record, which could cause logical decoding to produce more logical snapshots than expected. This commit modifies the isolation tests to verify that at least one logical snapshot contains the expected number of committed or ongoing catalog-change transactions. Per buildfarm member skink. Reported-by: Andres Freund <[email protected]> Author: Bertrand Drouvot <[email protected]> Reviewed-by: Amit Kapila <[email protected]> Reviewed-by: Masahiko Sawada <[email protected]> Discussion: https://postgr.es/m/5qbxud4pvnvmtuoi7weiizm5hmumxaeohx4vztfhrwlfhyz6rj@buh4435mllwo
1 parent 8b1b342 commit a49927f

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed
Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Parsed test spec with 2 sessions
22

3-
starting permutation: s0_init s0_begin s0_savepoint s0_truncate s1_checkpoint s1_get_changes s0_commit s0_begin s0_insert s1_checkpoint s1_get_changes s0_commit s1_get_changes s1_get_logical_snapshot_info s1_get_logical_snapshot_meta
3+
starting permutation: s0_init s0_begin s0_savepoint s0_truncate s1_create_table s1_checkpoint s1_get_changes s1_check_snapshot_info s1_check_snapshot_meta s0_commit
44
step s0_init: SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding');
55
?column?
66
--------
@@ -10,43 +10,23 @@ init
1010
step s0_begin: BEGIN;
1111
step s0_savepoint: SAVEPOINT sp1;
1212
step s0_truncate: TRUNCATE tbl1;
13+
step s1_create_table: CREATE TABLE tbl2 (val1 integer, val2 integer);
1314
step s1_checkpoint: CHECKPOINT;
1415
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
1516
data
1617
----
1718
(0 rows)
1819

19-
step s0_commit: COMMIT;
20-
step s0_begin: BEGIN;
21-
step s0_insert: INSERT INTO tbl1 VALUES (1);
22-
step s1_checkpoint: CHECKPOINT;
23-
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
24-
data
25-
---------------------------------------
26-
BEGIN
27-
table public.tbl1: TRUNCATE: (no-flags)
28-
COMMIT
29-
(3 rows)
30-
31-
step s0_commit: COMMIT;
32-
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
33-
data
34-
-------------------------------------------------------------
35-
BEGIN
36-
table public.tbl1: INSERT: val1[integer]:1 val2[integer]:null
37-
COMMIT
38-
(3 rows)
39-
40-
step s1_get_logical_snapshot_info: SELECT info.state, info.catchange_count, array_length(info.catchange_xip,1) AS catchange_array_length, info.committed_count, array_length(info.committed_xip,1) AS committed_array_length FROM pg_ls_logicalsnapdir(), pg_get_logical_snapshot_info(name) AS info ORDER BY 2;
41-
state |catchange_count|catchange_array_length|committed_count|committed_array_length
42-
----------+---------------+----------------------+---------------+----------------------
43-
consistent| 0| | 2| 2
44-
consistent| 2| 2| 0|
45-
(2 rows)
20+
step s1_check_snapshot_info: SELECT count(*) > 0 as has_info FROM pg_ls_logicalsnapdir(), pg_get_logical_snapshot_info(name) AS info where info.catchange_count >= 2 and array_length(info.catchange_xip,1) >= 2 and info.committed_count >= 1 and array_length(info.committed_xip,1) >= 1;
21+
has_info
22+
--------
23+
t
24+
(1 row)
4625

47-
step s1_get_logical_snapshot_meta: SELECT COUNT(meta.*) from pg_ls_logicalsnapdir(), pg_get_logical_snapshot_meta(name) as meta;
48-
count
49-
-----
50-
2
26+
step s1_check_snapshot_meta: SELECT count(meta.*) > 0 AS has_meta from pg_ls_logicalsnapdir(), pg_get_logical_snapshot_meta(name) as meta;
27+
has_meta
28+
--------
29+
t
5130
(1 row)
5231

32+
step s0_commit: COMMIT;
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Test the pg_logicalinspect functions: that needs some permutation to
2-
# ensure that we are creating multiple logical snapshots and that one of them
3-
# contains ongoing catalogs changes.
2+
# ensure that we are creating at least one snapshot that contains ongoing and
3+
# committed catalogs changes.
44
setup
55
{
66
DROP TABLE IF EXISTS tbl1;
7+
DROP TABLE IF EXISTS tbl2;
78
CREATE TABLE tbl1 (val1 integer, val2 integer);
89
CREATE EXTENSION pg_logicalinspect;
910
}
1011

1112
teardown
1213
{
1314
DROP TABLE tbl1;
15+
DROP TABLE tbl2;
1416
SELECT 'stop' FROM pg_drop_replication_slot('isolation_slot');
1517
DROP EXTENSION pg_logicalinspect;
1618
}
@@ -21,14 +23,24 @@ step "s0_init" { SELECT 'init' FROM pg_create_logical_replication_slot('isolatio
2123
step "s0_begin" { BEGIN; }
2224
step "s0_savepoint" { SAVEPOINT sp1; }
2325
step "s0_truncate" { TRUNCATE tbl1; }
24-
step "s0_insert" { INSERT INTO tbl1 VALUES (1); }
2526
step "s0_commit" { COMMIT; }
2627

2728
session "s1"
2829
setup { SET synchronous_commit=on; }
2930
step "s1_checkpoint" { CHECKPOINT; }
31+
step "s1_create_table" { CREATE TABLE tbl2 (val1 integer, val2 integer); }
3032
step "s1_get_changes" { SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0'); }
31-
step "s1_get_logical_snapshot_meta" { SELECT COUNT(meta.*) from pg_ls_logicalsnapdir(), pg_get_logical_snapshot_meta(name) as meta;}
32-
step "s1_get_logical_snapshot_info" { SELECT info.state, info.catchange_count, array_length(info.catchange_xip,1) AS catchange_array_length, info.committed_count, array_length(info.committed_xip,1) AS committed_array_length FROM pg_ls_logicalsnapdir(), pg_get_logical_snapshot_info(name) AS info ORDER BY 2; }
33+
step "s1_check_snapshot_meta" { SELECT count(meta.*) > 0 AS has_meta from pg_ls_logicalsnapdir(), pg_get_logical_snapshot_meta(name) as meta; }
34+
step "s1_check_snapshot_info" { SELECT count(*) > 0 as has_info FROM pg_ls_logicalsnapdir(), pg_get_logical_snapshot_info(name) AS info where info.catchange_count >= 2 and array_length(info.catchange_xip,1) >= 2 and info.committed_count >= 1 and array_length(info.committed_xip,1) >= 1; }
3335

34-
permutation "s0_init" "s0_begin" "s0_savepoint" "s0_truncate" "s1_checkpoint" "s1_get_changes" "s0_commit" "s0_begin" "s0_insert" "s1_checkpoint" "s1_get_changes" "s0_commit" "s1_get_changes" "s1_get_logical_snapshot_info" "s1_get_logical_snapshot_meta"
36+
37+
# Both s0 and s1 execute catalog-change transactions. When "s1_get_changes" is
38+
# executed, s0's transaction is still in progress, while s1's transaction has
39+
# already completed. Consequently, the logical decoding produces a snapshot at
40+
# the point where a RUNNING_XACTS record is generated by "s1_checkpoint".
41+
# This snapshot contains both two ongoing catalog-change transactions (from s0's
42+
# top-level and sub transactions) and one completed transaction (from s1).
43+
# "s1_check_snapshot_info" verifies whether the logical snapshot contains at
44+
# least the expected number of transactions, accounting for potential
45+
# additional catalog changes that may occur due to concurrent autoanalyze.
46+
permutation "s0_init" "s0_begin" "s0_savepoint" "s0_truncate" "s1_create_table" "s1_checkpoint" "s1_get_changes" "s1_check_snapshot_info" "s1_check_snapshot_meta" "s0_commit"

0 commit comments

Comments
 (0)