Re: Add a hook for handling logical decoding messages on subscribers.

From: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: Ajin Cherian <itsajin(at)gmail(dot)com>, Zhijie Hou <houzhijie22(at)gmail(dot)com>, Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add a hook for handling logical decoding messages on subscribers.
Date: 2026-12-01 07:12:06
Message-ID: CABdArM4yqXR0CmTDp0HVOdcTsWhORsqGCErVnCL7m+BV-fft2g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Nov 21, 2026 at 12:05 AM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>
> On Wed, Nov 18, 2026 at 4:48 PM Ajin Cherian <itsajin(at)gmail(dot)com> wrote:
> >
>
> Given that we’ve agreed on the 0001 patch, I’m going to push it early
> next week, barring any objections. I’d like to hold off on the 0002
> patch to give it more review time.
>

Hi Sawada-san,

I've started reviewing/testing the hook patch (v10-002) and found an
issue with the commit timestamp for non-transactional messages.

worker.c:apply_handle_message():
+ replorigin_xact_state.origin_lsn = msg.lsn;
+ replorigin_xact_state.origin_timestamp = 0;

IIUC, the idea is that 0 makes RecordTransactionCommit() use the local
commit time. That is true for commit_ts, but not for WAL:
- XactLogCommitRecord() copies origin_timestamp (0) into the commit
record first.
- Only after that does the "origin_timestamp == 0" check replace it
with GetCurrentTransactionStopTimestamp(), and only that local time
goes to the commit_ts.
- On replay, xact_redo_commit() takes origin_timestamp from the WAL
record whenever the record has an origin.

If the sub crashes (before a checkpoint covers the commit), on replay,
the commit_ts for the handler's transaction becomes 0, and
pg_xact_commit_timestamp() returns NULL.
If the Sub crashes before a checkpoint covers the commit, replay sets
the commit_ts of the handler's transaction to 0, and then
pg_xact_commit_timestamp() returns NULL. I think a standby (of Sub)
also replays the same record, so it gets the same result.

I reproduced it as:

[SETUP]
A pub-sub setup. On sub , track_commit_timestamp = on and
test_logicalmsg_hooks in shared_preload_libraries, and a subscription
with messages = true:

[PUB]
SELECT pg_logical_emit_message(false, 'ts_test3', 'x');
INSERT INTO tab VALUES (3, 3);

[SUB] before crash:
SELECT xmin, prefix, pg_xact_commit_timestamp(xmin) FROM
public.test_logicalmsg_log WHERE prefix = 'ts_test3';
xmin | prefix | pg_xact_commit_timestamp
------+----------+----------------------------------
688 | ts_test3 | 2026-12-01 10:52:47.308607+05:30

SELECT xmin, pg_xact_commit_timestamp(xmin) FROM tab WHERE a = 3;
xmin | pg_xact_commit_timestamp
------+----------------------------------
689 | 2026-12-01 10:52:47.306854+05:30

pg_waldump on the sub:
rmgr: Transaction len (rec/tot): 65/ 65, tx: 688, lsn:
0/0182FED0, prev 0/0182FD18, desc: COMMIT 2026-12-01 10:52:47.308607
IST; origin: node 1, lsn 0/017CF5B8, at 2000-01-01 05:30:00.000000 IST
rmgr: Transaction len (rec/tot): 65/ 65, tx: 689, lsn:
0/018300A0, prev 0/0182FFE8, desc: COMMIT 2026-12-01 10:52:47.310627
IST; origin: node 1, lsn 0/017CF668, at 2026-12-01 10:52:47.306854 IST

-- WAL has "2000-01-01 05:30:00.000000 IST" becasue of '0'.

pg_ctl stop -m immediate, then start. After recovery:
xmin | prefix | pg_xact_commit_timestamp
------+----------+--------------------------
688 | ts_test3 |
(xid 689 is unchanged)
~~~~

As this only affects non-transactional messages, the real impact
depends on what data the handler writes. For those rows:
- conflict detection info is lost,
- a downstream subscriber of this node gets a wrong commit time,
- pg_xact_commit_timestamp() returns NULL.

Should we set the local time here directly?

- replorigin_xact_state.origin_timestamp = 0;
+ replorigin_xact_state.origin_timestamp = GetCurrentTimestamp();

OTOH, pg_head could have the same problem, because
RecordTransactionCommit() writes the commit record first and only then
replaces a zero origin_timestamp with the local time. So any commit
that has an origin but a zero timestamp is affected, e.g. the
tablesync COPY transaction.
I'm trying to reproduce this on head, and to check the impact of
setting the timestamp before writing the commit record in
RecordTransactionCommit(). If we fix it separately on head, the zero
origin_timestamp above would be fine as is.
Thoughts?

--
Thanks,
Nisha

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ajin Cherian 2026-12-01 07:15:36 Re: Wrong relation in apply worker "not updatable" errors PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>for partitions; relcache leak in check_and_init_gencol()
Previous Message Chao Li 2026-12-01 07:08:50 pg_upgrade: fix use-after-close in cleanup_output_dirs()