On Fri, Dec 4, 2026 at 1:52 AM Masahiko Sawada <[email protected]> wrote:
>
> On Mon, Nov 30, 2026 at 11:12 PM Nisha Moond <[email protected]> wrote:
> >
> > On Sat, Nov 21, 2026 at 12:05 AM Masahiko Sawada <[email protected]> 
> > wrote:
> > >
> > > On Wed, Nov 18, 2026 at 4:48 PM Ajin Cherian <[email protected]> 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?
>
> Thank you for reviewing the patch! Good catch.
>
> I think your second analysis is right. I think the real problem is
> that the commit record and commit_ts end up with different timestamps
> for the same transaction. I'm not sure it's an intentional behavior.
> As you mentioned, if a server crashes before a checkpoint covers the
> transaction, commit_ts is rebuilt and pg_xact_commit_timestamp() ends
> up returning NULL. I guess it's a bug in RecordTransactionCommit(),
> but not in this patch.
>

Thanks for looking into it. Agreed, it's better fixed in core, so the
zero origin_timestamp in the patch can stay as is.

> I also found that the fallback writes the local time into
> replorigin_xact_state even when there is no origin, and nothing clears
> it at the end of the transaction, so the next commit with an origin
> inherits the previous transaction's timestamp.
>
> So I'd like to fix it separately and start a separate thread for it.
>

Good point about the leftover timestamp. I tested, and the tablesync
COPY transaction indeed inherits the commit time of the earlier
DATASYNC state update (about 2.4s earlier in my test) rather than
getting 0.

I'll start a separate thread on head covering both issues, i.e. the
commit record and commit_ts getting different timestamps, and the
fallback timestamp leaking into the next transaction.

--
Thanks,
Nisha


Reply via email to