Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 10f1f93

Browse files
committed
Don't skip SQL backends in logical decoding for visibility computation.
The logical decoding patchset introduced PROC_IN_LOGICAL_DECODING flag PGXACT flag, that allows such backends to be skipped when computing the xmin horizon/snapshots. That's fine and sensible for walsenders streaming out logical changes, but not at all fine for SQL backends doing logical decoding. If the latter set that flag any change they have performed outside of logical decoding will not be regarded as visible - which e.g. can lead to that change being vacuumed away. Note that not setting the flag for SQL backends isn't particularly bothersome - the SQL backend doesn't do streaming, so it only runs for a limited amount of time. Per buildfarm member 'tick' and Alvaro. Backpatch to 9.4, where logical decoding was introduced.
1 parent f0eeba6 commit 10f1f93

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

contrib/test_decoding/expected/decoding_into_rel.out

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-- test that we can insert the result of a get_changes call into a
22
-- logged relation. That's really not a good idea in practical terms,
33
-- but provides a nice test.
4+
-- predictability
5+
SET synchronous_commit = on;
46
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
57
?column?
68
----------

contrib/test_decoding/sql/decoding_into_rel.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-- test that we can insert the result of a get_changes call into a
22
-- logged relation. That's really not a good idea in practical terms,
33
-- but provides a nice test.
4+
5+
-- predictability
6+
SET synchronous_commit = on;
7+
48
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
59

610
-- slot works

src/backend/replication/logical/logical.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,19 @@ StartupDecodingContext(List *output_plugin_options,
145145
* logical decoding backend which doesn't need to be checked individually
146146
* when computing the xmin horizon because the xmin is enforced via
147147
* replication slots.
148+
*
149+
* We can only do so if we're outside of a transaction (i.e. the case when
150+
* streaming changes via walsender), otherwise a already setup
151+
* snapshot/xid would end up being ignored. That's not a particularly
152+
* bothersome restriction since the SQL interface can't be used for
153+
* streaming anyway.
148154
*/
149-
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
150-
MyPgXact->vacuumFlags |= PROC_IN_LOGICAL_DECODING;
151-
LWLockRelease(ProcArrayLock);
155+
if (!IsTransactionOrTransactionBlock())
156+
{
157+
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
158+
MyPgXact->vacuumFlags |= PROC_IN_LOGICAL_DECODING;
159+
LWLockRelease(ProcArrayLock);
160+
}
152161

153162
ctx->slot = slot;
154163

src/include/storage/proc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct XidCache
4343
#define PROC_IN_ANALYZE 0x04 /* currently running analyze */
4444
#define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */
4545
#define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical
46-
* decoding */
46+
* decoding outside xact */
4747

4848
/* flags reset at EOXact */
4949
#define PROC_VACUUM_STATE_MASK \

0 commit comments

Comments
 (0)