Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c40489e

Browse files
committed
Fix logical replication slot initialization
This was broken in commit 9c7d06d, which inadvertently gave the wrong value to fast_forward in one StartupDecodingContext call. Fix by flipping the value. Add a test for the obvious error, namely trying to initialize a replication slot with an nonexistent output plugin. While at it, move the CreateDecodingContext call earlier, so that any errors are reported before sending the CopyBoth message. Author: Dave Cramer <[email protected]> Reviewed-by: Andres Freund <[email protected]> Discussion: https://postgr.es/m/CADK3HHLVkeRe1v4P02-5hj55H3_yJg3AEtpXyEY5T3wuzO2jSg@mail.gmail.com
1 parent 91bc213 commit c40489e

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

contrib/test_decoding/expected/slot.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t2', 'tes
3030
init
3131
(1 row)
3232

33+
SELECT pg_create_logical_replication_slot('foo', 'nonexistent');
34+
ERROR: could not access file "nonexistent": No such file or directory
3335
-- here we want to start a new session and wait till old one is gone
3436
select pg_backend_pid() as oldpid \gset
3537
\c -

contrib/test_decoding/sql/slot.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_p', 'test
99

1010
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t2', 'test_decoding', true);
1111

12+
SELECT pg_create_logical_replication_slot('foo', 'nonexistent');
13+
1214
-- here we want to start a new session and wait till old one is gone
1315
select pg_backend_pid() as oldpid \gset
1416
\c -

src/backend/replication/logical/logical.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ CreateInitDecodingContext(char *plugin,
312312
ReplicationSlotSave();
313313

314314
ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
315-
need_full_snapshot, true,
315+
need_full_snapshot, false,
316316
read_page, prepare_write, do_write,
317317
update_progress);
318318

src/backend/replication/walsender.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,19 @@ StartLogicalReplication(StartReplicationCmd *cmd)
10681068
got_STOPPING = true;
10691069
}
10701070

1071+
/*
1072+
* Create our decoding context, making it start at the previously ack'ed
1073+
* position.
1074+
*
1075+
* Do this before sending CopyBoth, so that any errors are reported early.
1076+
*/
1077+
logical_decoding_ctx =
1078+
CreateDecodingContext(cmd->startpoint, cmd->options, false,
1079+
logical_read_xlog_page,
1080+
WalSndPrepareWrite, WalSndWriteData,
1081+
WalSndUpdateProgress);
1082+
1083+
10711084
WalSndSetState(WALSNDSTATE_CATCHUP);
10721085

10731086
/* Send a CopyBothResponse message, and start streaming */
@@ -1077,16 +1090,6 @@ StartLogicalReplication(StartReplicationCmd *cmd)
10771090
pq_endmessage(&buf);
10781091
pq_flush();
10791092

1080-
/*
1081-
* Initialize position to the last ack'ed one, then the xlog records begin
1082-
* to be shipped from that position.
1083-
*/
1084-
logical_decoding_ctx = CreateDecodingContext(cmd->startpoint, cmd->options,
1085-
false,
1086-
logical_read_xlog_page,
1087-
WalSndPrepareWrite,
1088-
WalSndWriteData,
1089-
WalSndUpdateProgress);
10901093

10911094
/* Start reading WAL from the oldest required WAL. */
10921095
logical_startptr = MyReplicationSlot->data.restart_lsn;

0 commit comments

Comments
 (0)