Skip to content

Commit 9eecea7

Browse files
committed
Don't use Asserts to check for violations of replication protocol.
Using an Assert to check the validity of incoming messages is an extremely poor decision. In a debug build, it should not be that easy for a broken or malicious remote client to crash the logrep worker. The consequences could be even worse in non-debug builds, which will fail to make such checks at all, leading to who-knows-what misbehavior. Hence, promote every Assert that could possibly be triggered by wrong or out-of-order replication messages to a full test-and-ereport. To avoid bloating the set of messages the translation team has to cope with, establish a policy that replication protocol violation error reports don't need to be translated. Hence, all the new messages here use errmsg_internal(). A couple of old messages are changed likewise for consistency. Along the way, fix some non-idiomatic or outright wrong uses of hash_search(). Most of these mistakes are new with the "streaming replication" patch (commit 4648243), but a couple go back a long way. Back-patch as appropriate. Discussion: https://postgr.es/m/1719083.1623351052@sss.pgh.pa.us
1 parent 8b9e127 commit 9eecea7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/backend/replication/logical/reorderbuffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
13231323
ent = (ReorderBufferTupleCidEnt *)
13241324
hash_search(txn->tuplecid_hash,
13251325
(void *) &key,
1326-
HASH_ENTER | HASH_FIND,
1326+
HASH_ENTER,
13271327
&found);
13281328
if (!found)
13291329
{

src/backend/replication/logical/worker.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,14 @@ apply_handle_commit(StringInfo s)
520520

521521
logicalrep_read_commit(s, &commit_data);
522522

523-
Assert(commit_data.commit_lsn == remote_final_lsn);
523+
if (commit_data.commit_lsn != remote_final_lsn)
524+
ereport(ERROR,
525+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
526+
errmsg_internal("incorrect commit LSN %X/%X in commit message (expected %X/%X)",
527+
(uint32) (commit_data.commit_lsn >> 32),
528+
(uint32) commit_data.commit_lsn,
529+
(uint32) (remote_final_lsn >> 32),
530+
(uint32) remote_final_lsn)));
524531

525532
/* The synchronization worker runs in single transaction. */
526533
if (IsTransactionState() && !am_tablesync_worker())

0 commit comments

Comments
 (0)