Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

Поиск
Список
Период
Сортировка
Искать

Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hello hackers, I found that we currently don't track txns committed in
BUILDING_SNAPSHOT state because of the code in xact_decode():
/*
* If the snapshot isn't yet fully built, we cannot decode anything, so
* bail out.
*/
if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
return;

This can cause a txn to take an incorrect historic snapshot and result in an
interruption of logical replication. Consider the following scenario:
(pub)create table t1 (id int primary key);
(pub)insert into t1 values (1);
(pub)create publication pub for table t1;
(sub)create table t1 (id int primary key);
(pub)begin; insert into t1 values (2); (txn1 in session1)
(sub)create subscription sub connection 'hostaddr=127.0.0.1 port=5432 user=xxx dbname=postgres' publication pub; (pub will switch to BUILDING_SNAPSHOT state soon)
(pub)begin; insert into t1 values (3); (txn2 in session2)
(pub)create table t2 (id int primary key); (session3)
(pub)commit; (commit txn1, and pub will switch to FULL_SNAPSHOT state soon)
(pub)begin; insert into t2 values (1); (txn3 in session3)
(pub)commit; (commit txn2, and pub will switch to CONSISTENT state soon)
(pub)commit; (commit txn3, and replay txn3 will failed because its snapshot cannot see table t2)

The output of pub's log:
ERROR: could not map filenumber "base/5/16395" to relation OID

Is this a bug? Should we also track the txns committed in BUILDING_SNAPSHOT state?

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Sun, Jun 09, 2024 at 11:21:52PM +0800, cca5507 wrote:
> Hello hackers, I found that we currently don't track txns committed in
> BUILDING_SNAPSHOT state because of the code in xact_decode():
> 	/*
> 	 * If the snapshot isn't yet fully built, we cannot decode anything, so
> 	 * bail out.
> 	 */
> 	if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
> 		return;
> 
> The output of pub's log:
> ERROR:  could not map filenumber "base/5/16395" to relation OID
> 
> Is this a bug? Should we also track the txns committed in BUILDING_SNAPSHOT state?

Clearly, this is not an error you should be able to see as a user.  So
yes, that's something that needs to be fixed.
--
Michael

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Вложения:
Thank you for reply!
I am trying to fix it. This patch (pass check-world) will track txns
committed in BUILDING_SNAPSHOT state and can fix this bug.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Mon, Jun 10, 2024 at 10:04:31PM +0800, cca5507 wrote:
> Thank you for reply!I am trying to fix it. This patch (pass check-world) will track txns
> committed in BUILDING_SNAPSHOT state and can fix this bug.

Thanks for the report and the patch!

I did not look at the patch in detail but I can see that it does no contain a test
related to this issue.

Would you mind to add a test in say contrib/test_decoding? (see catalog_change_snapshot
for example).

FWIW, to ease the writing of the test, I think you don't need pub/sub to produce
the issue. I think you can reproduce with a single instance and multiple sessions
: replace in your repro the "(sub)create subscription" by a "logical slot creation"
and finish the test by "pg_logical_slot_get_changes" on the slot created above.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Вложения:
Hi,

Thanks for pointing it out!

Here are the new version patches with a test case.

--
Regards,
ChangAo Chen

------------------ Original ------------------
From: "Bertrand Drouvot" <bertranddrouvot.pg@gmail.com>;
Date: Wed, Aug 7, 2024 06:33 PM
To: "cca5507"<cca5507@qq.com>;
Cc: "Michael Paquier"<michael@paquier.xyz>;"pgsql-hackers"<pgsql-hackers@lists.postgresql.org>;
Subject: Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

Hi,

On Mon, Jun 10, 2024 at 10:04:31PM +0800, cca5507 wrote:
> Thank you for reply!I am trying to fix it. This patch (pass check-world) will track txns
> committed in BUILDING_SNAPSHOT state and can fix this bug.

Thanks for the report and the patch!

I did not look at the patch in detail but I can see that it does no contain a test
related to this issue.

Would you mind to add a test in say contrib/test_decoding? (see catalog_change_snapshot
for example).

FWIW, to ease the writing of the test, I think you don't need pub/sub to produce
the issue. I think you can reproduce with a single instance and multiple sessions
: replace in your repro the "(sub)create subscription" by a "logical slot creation"
and finish the test by "pg_logical_slot_get_changes" on the slot created above.

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Thu, Aug 08, 2024 at 03:53:29PM +0800, cca5507 wrote:
> Hi,
> 
> 
> Thanks for pointing it out!
> 
> 
> Here are the new version patches with a test case.

Thanks!

I think the approach that the patch implements makes sense and that we should
track the transactions that have been commmitted while building the snapshot.

A few random comments:

1 ===

+        * that the xlog in BUILDING_SNAPSHOT is only useful for build

s/for build/to build? (same comment for the commit message)

2 ===

+        * snapshot and will not be decoded.

worth to mention DecodeTXNNeedSkip() here?

3 ===

+        * point in decoding changes. Note that we only handle XLOG_HEAP2_NEW_CID
+        * which mark a transaction as catalog modifying in BUILDING_SNAPSHOT

do you mean? Note that during BUILDING_SNAPSHOT, we only handle XLOG_HEAP2_NEW_CID
as it marks a transaction as catalog modifying. If so, what about making the 

-       if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
+       if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||

more clear about that? (to avoid any work when it's not needed)

4 ===

+        * useful for build the snapshot.

s/for/to/?

5 ===

+        * point in decoding changes. Note that we only handle XLOG_HEAP_INPLACE
+        * which mark a transaction as catalog modifying in BUILDING_SNAPSHOT, it's

s/which mark/which might mark/?

do you mean? Note that during BUILDING_SNAPSHOT, we only handle XLOG_HEAP_INPLACE
as it might mark a transaction as catalog modifying. If so, what about making the 

-       if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
+       if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||

more clear about that? (to avoid any work when it's not needed)

Idea of 3 === and 5 === is to proceed further in the SNAPBUILD_BUILDING_SNAPSHOT
case only if we know that the transaction is a catalog changing one (or might
be one).

6 ===

+        * useful for build the snapshot.

s/for/to/?

7 ===

+# Test snapshot build correctly
+

what about? Test tracking of committed transactions during BUILDING_SNAPSHOT

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Вложения:
Hi,

Thanks for the comments!

Here are the new version patches, I think it will be more clear.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Sat, Aug 10, 2024 at 06:07:30PM +0800, cca5507 wrote:
> Hi,
> 
> 
> Thanks for the comments!
> 
> 
> Here are the new version patches, I think it will be more clear.

Thanks!

1 ===

When applying I get:

Applying: Track transactions committed in BUILDING_SNAPSHOT.
.git/rebase-apply/patch:71: space before tab in indent.
                 */
.git/rebase-apply/patch:94: space before tab in indent.
                 */
warning: 2 lines add whitespace errors.

2 ===

+        * have snapshot and the transaction will not be tracked by snapshot

s/have snapshot/have a snapshot/?

3 ===

+        * snapshot and will not be decoded

s/snapshot/a snapshot/?


4 ===

        if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
                ctx->fast_forward)
+       {
+               /*
+                * Note that during or after BUILDING_SNAPSHOT, we need handle the xlog
+                * that might mark a transaction as catalog modifying because the snapshot
+                * only tracks catalog modifying transactions. The transaction before
+                * BUILDING_SNAPSHOT will not be tracked anyway(see SnapBuildCommitTxn()
+                * for details), so just return.
+                */
+               if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+               {
+                       /* Currently only XLOG_HEAP2_NEW_CID means a catalog modifying */
+                       if (info == XLOG_HEAP2_NEW_CID && TransactionIdIsValid(xid))

What about?

if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
    (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP2_NEW_CID) ||
    ctx->fast_forward)
    return;

That way we'd still rely on what's being done in the XLOG_HEAP2_NEW_CID case (
should it change in the future).

5 ===

        if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
                ctx->fast_forward)
+       {
+               /*
+                * Note that during or after BUILDING_SNAPSHOT, we need handle the xlog
+                * that might mark a transaction as catalog modifying because the snapshot
+                * only tracks catalog modifying transactions. The transaction before
+                * BUILDING_SNAPSHOT will not be tracked anyway(see SnapBuildCommitTxn()
+                * for details), so just return.
+                */
+               if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+               {

What about?

if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
    (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP_INPLACE) ||
    ctx->fast_forward)
    return;

That way we'd still rely on what's being done in the XLOG_HEAP_INPLACE case (
should it change in the future).

6 ===

v3-0002 LGTM.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

4, 5 ===

> if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
>     (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP_INPLACE) ||
>     ctx->fast_forward)
>     return;

I think during fast forward, we also need handle the xlog that marks a transaction
as catalog modifying, or the snapshot might lose some transactions?

> That way we'd still rely on what's being done in the XLOG_HEAP_INPLACE case

+ if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+ {
+ /* Currently only XLOG_HEAP_INPLACE means a catalog modifying */
+ if (info == XLOG_HEAP_INPLACE && TransactionIdIsValid(xid))
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+ }

We only call ReorderBufferXidSetCatalogChanges() for the xlog that marks a transaction as catalog
modifying, and we don't care about the other steps being done in the xlog, so I think the current
approach is ok.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Mon, Aug 12, 2024 at 04:34:25PM +0800, cca5507 wrote:
> Hi,
> 
> 
> 4, 5 ===
> 
> 
> > if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
> >     (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP_INPLACE) ||
> >     ctx->fast_forward)
> >     return;
> 
> 
> 
> I think during fast forward, we also need handle the xlog that marks a transaction
> as catalog modifying, or the snapshot might lose some transactions?

I think it's fine to skip during fast forward as we are not generating logical
changes. It's done that way in master, in your proposal and in my "if" proposals.
Note that my proposals related to the if conditions are for heap2_decode and
heap_decode (not xact_decode).

> > That way we'd still rely on what's being done in the XLOG_HEAP_INPLACE case
> 
> 
> +		if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
> +		{
> +			/* Currently only XLOG_HEAP_INPLACE means a catalog modifying */
> +			if (info == XLOG_HEAP_INPLACE && TransactionIdIsValid(xid))
> +				ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
> +		}
> 
> 
> 
> We only call ReorderBufferXidSetCatalogChanges() for the xlog that marks a transaction as catalog
> modifying, and we don't care about the other steps being done in the xlog, so I think the current
> approach is ok.

Yeah, I think your proposal does not do anything wrong. I just prefer to put
everything in a single if condition (as per my proposal) so that we can jump
directly in the appropriate case. I think that way the code is easier to maintain
instead of having to deal with the same info values in distinct places.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

Thanks for the comments!

- I think it's fine to skip during fast forward as we are not generating logical
- changes. It's done that way in master, in your proposal and in my "if" proposals.
- Note that my proposals related to the if conditions are for heap2_decode and
- heap_decode (not xact_decode).

But note that in xact_decode(), case XLOG_XACT_INVALIDATIONS, we call
ReorderBufferXidSetCatalogChanges() even if we are fast-forwarding, it might
be better to be consistent.

In addition, we don't decode anything during fast forward, but the snapshot might
serialize to disk. If we skip calling ReorderBufferXidSetCatalogChanges(), the snapshot
may be wrong on disk.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Вложения:
Hi,

I refactor the code and fix the git apply warning according to [1].

Here are the new version patches.

--
Regards,
ChangAo Chen

[1] https://www.postgresql.org/message-id/Zrmh7X8jYCbFYXjH%40ip-10-97-1-34.eu-west-3.compute.internal

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Tue, Aug 13, 2024 at 12:23:04PM +0800, cca5507 wrote:
> Hi,
> 
> I refactor the code and fix the git apply warning according to [1].
> 
> 
> Here are the new version patches.

Thanks!

1 ===

+       /* True if the xlog marks the transaction as containing catalog changes */
+       bool    set_catalog_changes = (info == XLOG_HEAP2_NEW_CID);

        if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
                ctx->fast_forward)
+       {
+               /*
+                * If the transaction contains catalog changes, we need mark it in
+                * reorder buffer before return as the snapshot only tracks catalog
+                * modifying transactions. The transaction before BUILDING_SNAPSHOT
+                * won't be tracked anyway(see SnapBuildCommitTxn), so skip it.
+                */
+               if (set_catalog_changes && TransactionIdIsValid(xid) &&
+                       SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+                       ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+
                return;
+       }

I still prefer to replace the above with:

if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
    (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP2_NEW_CID) ||
    ctx->fast_forward)
    return;

Let's see what others think.

2 ===

+       /* True if the xlog marks the transaction as containing catalog changes */
+       bool    set_catalog_changes = (info == XLOG_HEAP_INPLACE);

        if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
                ctx->fast_forward)
+       {
+               /*
+                * If the transaction contains catalog changes, we need mark it in
+                * reorder buffer before return as the snapshot only tracks catalog
+                * modifying transactions. The transaction before BUILDING_SNAPSHOT
+                * won't be tracked anyway(see SnapBuildCommitTxn), so skip it.
+                */
+               if (set_catalog_changes && TransactionIdIsValid(xid) &&
+                       SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+                       ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+
                return;
+       }

I still prefer to replace the above with:

if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT ||
    (SnapBuildCurrentState(builder) == SNAPBUILD_BUILDING_SNAPSHOT && info != XLOG_HEAP_INPLACE) ||
    ctx->fast_forward)
    return;

Let's see what others think.

3 ===

I re-read your comments in [0] and it looks like you've concern about
the 2 "if" I'm proposing above and the fast forward handling. Is that the case
or is your fast forward concern unrelated to my proposals?



Not sure what happened but it looks like your reply in [0] is not part of the
initial thread [1], but created a new thread instead, making the whole
conversation difficult to follow.

[0]: https://www.postgresql.org/message-id/tencent_8DEC9842690A9B6AFD52D4659EF0700E9409%40qq.com
[1]: https://www.postgresql.org/message-id/flat/tencent_6AAF072A7623A11A85C0B5FD290232467808%40qq.com

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

- I re-read your comments in [0] and it looks like you've concern about
- the 2 "if" I'm proposing above and the fast forward handling. Is that the case
- or is your fast forward concern unrelated to my proposals?

In your proposals, we will just return when fast forward. But I think we need
handle XLOG_HEAP2_NEW_CID or XLOG_HEAP_INPLACE even if we are fast
forwarding as it decides whether the snapshot will track the transaction or not.

During fast forward, if there is a transaction that generates XLOG_HEAP2_NEW_CID
but no XLOG_XACT_INVALIDATIONS(I'm not sure), the snapshot won't track this
transaction in your proposals, I think it's wrong from a build snapshot perspective.

Although we don't decode anything during fast forward, the snapshot might be
serialized to disk when CONSISTENT, it would be better to keep the snapshot correct.

- Not sure what happened but it looks like your reply in [0] is not part of the
- initial thread [1], but created a new thread instead, making the whole
- conversation difficult to follow.

I'm not sure what happened but I attach the new thread to the CF:

https://commitfest.postgresql.org/49/5029

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Tue, Aug 13, 2024 at 03:32:42PM +0800, cca5507 wrote:
> Hi,
> 
> - I re-read your comments in [0] and it looks like you've concern about
> - the 2 "if" I'm proposing above and the fast forward handling. Is that the case
> - or is your fast forward concern unrelated to my proposals?
> 
> 
> 
> In your proposals, we will just return when fast forward. But I think we need
> handle XLOG_HEAP2_NEW_CID or XLOG_HEAP_INPLACE even if we are fast
> forwarding as it decides whether the snapshot will track the transaction or not.
> 
> 
> During fast forward, if there is a transaction that generates XLOG_HEAP2_NEW_CID
> but no XLOG_XACT_INVALIDATIONS(I'm not sure), the snapshot won't track this
> transaction in your proposals, I think it's wrong from a build snapshot perspective.
> 
> 
> Although we don't decode anything during fast forward, the snapshot might be
> serialized to disk when CONSISTENT, it would be better to keep the snapshot correct.

IIUC your "fast forward" concern is not related to this particular thread but you
think it's already an issue on the master branch (outside of the BUILDING_SNAPSHOT
handling we are discussing here), is that correct? (that's also what your coding
changes makes me think of). If so, I'd suggest to open a dedicated thread for that
particular "fast forward" point and do the coding in the current thread as if the
fast forward is not an issue.

Does that make sense?

> 
> - Not sure what happened but it looks like your reply in [0] is not part of the
> - initial thread [1], but created a new thread instead, making the whole
> - conversation difficult to follow.
> 
> I'm not sure what happened but I attach the new thread to the CF:

Unfortunately your last reply did start a new email thread again.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

- IIUC your "fast forward" concern is not related to this particular thread but you
- think it's already an issue on the master branch (outside of the BUILDING_SNAPSHOT
- handling we are discussing here), is that correct? (that's also what your coding
- changes makes me think of). If so, I'd suggest to open a dedicated thread for that
- particular "fast forward" point and do the coding in the current thread as if the
- fast forward is not an issue.

- Does that make sense?

Yes.

But I think the v4-0001 in [1] is fine.

Let's see what others think.

--
Regards,
ChangAo Chen

[1]: https://www.postgresql.org/message-id/tencent_925A991463194F3C97830C3BB7D0A2C2BD07%40qq.com

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Wed, Mar 26, 2025 at 9:11 PM cca5507  wrote:
>
> Hi,
>
> Thanks for the comments!
>
> Here are the new version patches, I think it will be more clear.
>
> --

+    {
+        /*
+         * Note that during or after BUILDING_SNAPSHOT, we need handle the xlog
+         * that might mark a transaction as catalog modifying because
the snapshot
+         * only tracks catalog modifying transactions. The transaction before
+         * BUILDING_SNAPSHOT will not be tracked anyway(see
SnapBuildCommitTxn()
+         * for details), so just return.
+          */
+        if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT)
+        {
+            /* Currently only XLOG_HEAP2_NEW_CID means a catalog modifying */
+            if (info == XLOG_HEAP2_NEW_CID && TransactionIdIsValid(xid))
+                ReorderBufferXidSetCatalogChanges(ctx->reorder, xid,
buf->origptr);
+        }

Any reason why you avoided calling SnapBuildProcessNewCid here and
only called ReorderBufferXidSetCatalogChanges? If any, please mention
in the comments the reason.

regards,
Ajin Cherian
Fujitsu Australia


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

There are 3 threads currently.

The latest patches are v4-0001 and v3-0002 in [2].

[1]https://www.postgresql.org/message-id/flat/tencent_6AAF072A7623A11A85C0B5FD290232467808@qq.com
[2]https://www.postgresql.org/message-id/flat/tencent_8DEC9842690A9B6AFD52D4659EF0700E9409@qq.com
[3]https://www.postgresql.org/message-id/flat/tencent_FA60D4EE3E14ACF0B936396551260A4FFD05@qq.com

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Thu, Mar 27, 2025 at 1:13 PM cca5507  wrote:
>
> Hi,
>
> - IIUC your "fast forward" concern is not related to this particular thread but you
> - think it's already an issue on the master branch (outside of the BUILDING_SNAPSHOT
> - handling we are discussing here), is that correct? (that's also what your coding
> - changes makes me think of). If so, I'd suggest to open a dedicated thread for that
> - particular "fast forward" point and do the coding in the current thread as if the
> - fast forward is not an issue.
>
> - Does that make sense?
>
> Yes.
>
> But I think the v4-0001 in [1] is fine.
>

@@ -1301,6 +1335,7 @@ DecodeTXNNeedSkip(LogicalDecodingContext *ctx,
XLogRecordBuffer *buf,
                   Oid txn_dbid, RepOriginId origin_id)
 {
     if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
+        SnapBuildCurrentState(ctx->snapshot_builder) < SNAPBUILD_CONSISTENT ||
         (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
         FilterByOrigin(ctx, origin_id))
         return true;

I don't see an explanation as to why this change was added? Maybe I
missed it in the multiple threads. With this, we are no longer
decoding changes which were in the state SNAPBUILD_FULL_SNAPSHOT. Why
did that change?

regards,
Ajin Cherian
Fujitsu Australia


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

According to the comment above DecodeTXNNeedSkip(), transactions committed before SNAPBUILD_CONSISTENT state won't be decoded. It's important for initial table data synchronization because the table sync workers use the consistent snapshot to copy data so transactions before SNAPBUILD_CONSISTENT are already included in the initial data.

This change may be redundant with SnapBuildXactNeedsSkip(), I add just for safe.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Thu, Mar 27, 2025 at 2:47 PM cca5507  wrote:
>
> Hi,
>
> According to the comment above DecodeTXNNeedSkip(), transactions committed before SNAPBUILD_CONSISTENT state won't be decoded. It's important for initial table data synchronization because the table sync workers use the consistent snapshot to copy data so transactions before SNAPBUILD_CONSISTENT are already included in the initial data.
>
> This change may be redundant with SnapBuildXactNeedsSkip(), I add just for safe.
>
> --

I understand your explanation but was there a bug on HEAD that this is
solving? If so, can you write a test case to show this? This will make
it more convincing that this change as well is required. Your other
change has a test case to confirm, but this doesn't.

regards,
Ajin Cherian
Fujitsu Australia


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

This change doesn't solve a bug. But I think it makes the code and comments more consistent. I currently have no idea about how to test it.

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Mon, Aug 12, 2024 at 9:25 PM cca5507  wrote:
>
> Hi,
>
> I refactor the code and fix the git apply warning according to [1].
>
> Here are the new version patches.
>

I've investigated the reported issue and reviewed the patch. IIUC to
fix this issue, what we need to do is to track catalog-change
transactions even in BUILDING_SNAPSHOT phase so that we can decode
transactions correctly that started in FULL_SNAPSHOT. My question is;
in order to track just catalog-change transactions, whether it's
sufficient to check if XLOG_XACT_COMMIT[_PREPARED] has the
XACT_XINFO_HAS_INVALS flag. If yes, we probably should change only
xact_decode() to check the commit records even in BUILDING_SNAPSHOT.
Otherwise, we would need to change mostly all paths where we mark the
transaction as catalog-change as the patch does.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Fri, Jun 27, 2025 at 3:48 PM cca5507  wrote:
>
> Hi,
>
> I refactor the code and fix the git apply warning according to [1].
>
> Here are the new version patches.
>
> --
> Regards,
> ChangAo Chen
>
> [1] https://www.postgresql.org/message-id/Zrmh7X8jYCbFYXjH%40ip-10-97-1-34.eu-west-3.compute.internal

I see this problem is similar to the bug reported in [1], and your fix
also addresses the issue reported there. Although I like your approach
of tracking changes starting from the BUILDING_SNAPSHOT state, I’d
like to suggest an alternative.

While debugging that issue, my plan was not to track catalog changes
prior to SNAPBUILD_CONSISTENT, but instead to ensure we don’t use
snapshots built before SNAPBUILD_CONSISTENT, since we don’t track
catalog changes in those states. We should discard previously built
snapshots and rebuild them once we reach the SNAPBUILD_CONSISTENT
state. At that point, all necessary transactions would have been
committed, and builder->xmin would have advanced enough to decode all
transactions from then on.

The problem is that previously built snapshots hang around without the
latest xmin and xmax, and we tend to reuse them. We should ensure that
all txn->base_snapshot and builder->snapshot snapshots built in the
SNAPBUILD_FULL_SNAPSHOT state are rebuilt once we reach
SNAPBUILD_CONSISTENT. For this, we need to track when the snapshot was
built. There is already a field in ReorderBufferTXN -
'base_snapshot_lsn' which we can use. If base_snapshot_lsn <
builder->start_decoding_at, then we should rebuild the snapshot. Just
a thought.

regards,
Ajin Cherian
Fujitsu Australia

[1] - https://www.postgresql.org/message-id/18509-983f064d174ea880%40postgresql.org


Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

If I understand correctly, this may break the basic principle:

The aim is to build a snapshot that behaves the same as a freshly taken MVCC snapshot would have at the time the XLogRecord was generated.

--
Regards,
ChangAo Chen

------------------ Original ------------------
From: "Ajin Cherian" <itsajin@gmail.com>;
Date: Fri, Jun 27, 2025 06:29 PM
To: "cca5507"<cca5507@qq.com>;
Cc: "Bertrand Drouvot"<bertranddrouvot.pg@gmail.com>;"Michael Paquier"<michael@paquier.xyz>;"pgsql-hackers"<pgsql-hackers@lists.postgresql.org>;
Subject: Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

On Fri, Jun 27, 2025 at 3:48 PM cca5507 <cca5507@qq.com> wrote:
>
> Hi,
>
> I refactor the code and fix the git apply warning according to [1].
>
> Here are the new version patches.
>
> --
> Regards,
> ChangAo Chen
>
> [1] https://www.postgresql.org/message-id/Zrmh7X8jYCbFYXjH%40ip-10-97-1-34.eu-west-3.compute.internal

I see this problem is similar to the bug reported in [1], and your fix
also addresses the issue reported there. Although I like your approach
of tracking changes starting from the BUILDING_SNAPSHOT state, I’d
like to suggest an alternative.

While debugging that issue, my plan was not to track catalog changes
prior to SNAPBUILD_CONSISTENT, but instead to ensure we don’t use
snapshots built before SNAPBUILD_CONSISTENT, since we don’t track
catalog changes in those states. We should discard previously built
snapshots and rebuild them once we reach the SNAPBUILD_CONSISTENT
state. At that point, all necessary transactions would have been
committed, and builder->xmin would have advanced enough to decode all
transactions from then on.

The problem is that previously built snapshots hang around without the
latest xmin and xmax, and we tend to reuse them. We should ensure that
all txn->base_snapshot and builder->snapshot snapshots built in the
SNAPBUILD_FULL_SNAPSHOT state are rebuilt once we reach
SNAPBUILD_CONSISTENT. For this, we need to track when the snapshot was
built. There is already a field in ReorderBufferTXN -
'base_snapshot_lsn' which we can use. If base_snapshot_lsn <
builder->start_decoding_at, then we should rebuild the snapshot. Just
a thought.

regards,
Ajin Cherian
Fujitsu Australia

[1] - https://www.postgresql.org/message-id/18509-983f064d174ea880%40postgresql.org

Re: Historic snapshot doesn't track txns committed inBUILDING_SNAPSHOT state

От:
"cca5507" <cca5507@qq.com>
Дата:
Вложения:
Hi all,

Attach the rebased patches.

Some useful link:

Discussion:
https://www.postgresql.org/message-id/flat/tencent_6AAF072A7623A11A85C0B5FD290232467808@qq.com
https://www.postgresql.org/message-id/flat/tencent_8DEC9842690A9B6AFD52D4659EF0700E9409@qq.com
https://www.postgresql.org/message-id/flat/tencent_FA60D4EE3E14ACF0B936396551260A4FFD05@qq.com

Bug Report:
https://www.postgresql.org/message-id/flat/18509-983f064d174ea880%40postgresql.org
https://www.postgresql.org/message-id/flat/19109-4a9ce36715d69632%40postgresql.org

CF Entry:
https://commitfest.postgresql.org/patch/5029/

--
Regards,
ChangAo Chen

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Вложения:
Hi,

Masahiko Sawada  wrote (in August 2024):
> My question is;
> in order to track just catalog-change transactions, whether it's
> sufficient to check if XLOG_XACT_COMMIT[_PREPARED] has the
> XACT_XINFO_HAS_INVALS flag. If yes, we probably should change only
> xact_decode() to check the commit records even in BUILDING_SNAPSHOT.
> Otherwise, we would need to change mostly all paths where we mark the
> transaction as catalog-change as the patch does.

I tried to answer that with data, and it looks like the answer is no.

I added two LOG lines to master (the attached instrumentation diff, not
meant to be applied): SnapBuildCommitTxn() logs, for the xid and each
subxid, whether the reorder buffer has it as catalog-changing and
whether the commit has XACT_XINFO_HAS_INVALS; SnapBuildProcessNewCid()
logs which relation the NEW_CID is for.  Then I ran installcheck-parallel
with a logical slot created beforehand, and decoded all of it.  The
attached catchk_run.sh does all of this on a build with that diff,
including the user catalog table check below; three runs gave the same
counts.

    catalog changes   invals in commit   top-level xacts
          no                 no                5087
          yes                no                  37
          yes                yes              15540

The 37 are two kinds.  33 changed only pg_largeobject and
pg_largeobject_metadata.  In the other 4 the catalog changes were made
only in subtransactions that were rolled back (TRUNCATE and DROP TABLE
in stats.sql, and one pg_statistic write), so the top transaction was
marked through its child but had nothing left to invalidate.  In that
run, every committed change to a catalog with a syscache or relcache
had invalidations.

But the regression tests don't write to user catalog tables, and those
behave like large objects:

    CREATE TABLE uc (k int PRIMARY KEY, v text)
        WITH (user_catalog_table = true);

    INSERT INTO uc ...        catalog changes: yes   invals: no
    UPDATE uc ...             catalog changes: yes   invals: no
    DELETE FROM uc ...        catalog changes: yes   invals: no
    ALTER TABLE plain ...     catalog changes: yes   invals: yes

Output plugins may read user catalog tables with the historic snapshot,
so a transaction that writes one during BUILDING_SNAPSHOT has to be
tracked, and XACT_XINFO_HAS_INVALS would miss it.  So I think the
patch's approach, marking the transaction from the NEW_CID records as
well, is the one needed.

The same data says something about master that I can't turn into a
failure: SnapBuildXidHasCatalogChanges() relies on "The transactions
that have changed catalogs must have invalidation info" to skip the
catchange array lookup, which doesn't hold for user catalog tables or
large objects.  Is that a problem when a snapshot is restored, for a
plugin that reads user catalog tables?

On the patch itself, v6 applied on master e8a3ee5b197, against that
master, with --enable-cassert:

- snapshot_build fails on master with "could not map filenumber
  "base/16384/16772" to relation OID" and passes with v6.
- The same test with the catalog change in a subtransaction, and with
  it committed by COMMIT PREPARED, fails the same way on master and
  passes with v6.
- With ALTER TABLE ... ADD COLUMN on an existing table instead of
  CREATE TABLE, master raises no error: the insert that follows is
  decoded with the old tuple descriptor, and the new columns are
  silently dropped:

      master:  table public.tbl3: INSERT: val1[integer]:1
      v6:      table public.tbl3: INSERT: val1[integer]:1 val2[text]:'two' val3[bigint]:3

  That seems worse than the error, since nothing tells the user.

The attached diff, on top of v6-0002, adds these three as permutations
of snapshot_build.  They fail on master and pass with v6.

About Ajin's question in March 2025 on the DecodeTXNNeedSkip() change
(the "SnapBuildCurrentState(...) < SNAPBUILD_CONSISTENT" test), which
ChangAo said might be redundant: on v6 I logged every call made before
CONSISTENT, and whether SnapBuildXactNeedsSkip() already skipped it.
Over test_decoding, src/test/subscription and the recovery tests below
plus 040, it was called 41 times before CONSISTENT (28 in
BUILDING_SNAPSHOT, 13 in FULL_SNAPSHOT), and SnapBuildXactNeedsSkip()
had already skipped every one of them.  So it never changes the result
there.  Maybe an Assert would say the same thing and catch a case where
it does.

With v6: test_decoding (20 and 15, plus the new permutations),
src/test/subscription (605 tests), the logical decoding tests in
src/test/recovery (006, 035 and 038, 119 tests) and make check (239)
pass.

v6-0001 applies cleanly to master and REL_18_STABLE; REL_19_STABLE
conflicts in DecodeTXNNeedSkip(), and REL_17_STABLE and older in the
XLOG_XACT_INVALIDATIONS case of xact_decode().  As Haiyang reported in
bug #19109, it reproduces back to 11.

I haven't looked at Ajin's alternative beyond your question about it.

Regards,
Manu

Re: Historic snapshot doesn't track txns committed inBUILDING_SNAPSHOT state

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
Вложения:
On 2025-Nov-21, cca5507 wrote:

> Attach the rebased patches.

I rebased this again, for CFbot.  I haven't reviewed it.

> Discussion:
> https://www.postgresql.org/message-id/flat/tencent_6AAF072A7623A11A85C0B5FD290232467808@qq.com
> https://www.postgresql.org/message-id/flat/tencent_8DEC9842690A9B6AFD52D4659EF0700E9409@qq.com
> https://www.postgresql.org/message-id/flat/tencent_FA60D4EE3E14ACF0B936396551260A4FFD05@qq.com
> 
> Bug Report:
> https://www.postgresql.org/message-id/flat/18509-983f064d174ea880%40postgresql.org
> https://www.postgresql.org/message-id/flat/19109-4a9ce36715d69632%40postgresql.org
> 
> CF Entry:
> https://commitfest.postgresql.org/patch/5029/

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"Selbst das größte Genie würde nicht weit kommen, wenn es
alles seinem eigenen Innern verdanken wollte." (Johann Wolfgang von Goethe)
               Ni aún el genio más grande llegaría muy lejos si
                    quisiera sacarlo todo de su propio interior.

Re: Historic snapshot doesn't track txns committed inBUILDING_SNAPSHOT state

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Sat, Oct 17, 2026 at 12:40 AM Álvaro Herrera  wrote:
>
> On 2025-Nov-21, cca5507 wrote:
>
> > Attach the rebased patches.
>
> I rebased this again, for CFbot.  I haven't reviewed it.
>
> > Discussion:
> > https://www.postgresql.org/message-id/flat/tencent_6AAF072A7623A11A85C0B5FD290232467808@qq.com
> > https://www.postgresql.org/message-id/flat/tencent_8DEC9842690A9B6AFD52D4659EF0700E9409@qq.com
> > https://www.postgresql.org/message-id/flat/tencent_FA60D4EE3E14ACF0B936396551260A4FFD05@qq.com
> >
> > Bug Report:
> > https://www.postgresql.org/message-id/flat/18509-983f064d174ea880%40postgresql.org
> > https://www.postgresql.org/message-id/flat/19109-4a9ce36715d69632%40postgresql.org
> >
> > CF Entry:
> > https://commitfest.postgresql.org/patch/5029/

Thanks for updating the patches.

I reviewed the patches recently and have a few comments.

1.

+ * Note that we only need to do this when we are not fast-forwarding
+ * and there is a snapshot.
*/
if (TransactionIdIsValid(xid))
{
- if (!ctx->fast_forward)
+ if (!ctx->fast_forward && has_snapshot)
ReorderBufferAddInvalidations(reorder, xid,

The patch skips accumulating the invalidations, but that doesn't seem correct,
since we may now need to distribute the invalidations to concurrent transactions
(see ReorderBufferAddDistributedInvalidations()). So I think we should not add
this check.

2.

If we address point 1, then calling ReorderBufferForget() for transactions
collected during the BUILD state would trigger an Assert in
ReorderBufferForget(), as we don't have a snapshot for them yet. I think we
could simply remove this Assert and add some comments to explain this case.

if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
txn->invalidations);
else
Assert(txn->ninvalidations == 0);

3.

Since we now handle transactions during the BUILDING state, the following code
becomes reachable:

(builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
TransactionIdPrecedes(xid, builder->next_phase_at)))
{
/* ensure that only commits after this are getting replayed */
if (builder->start_decoding_at <= lsn)
builder->start_decoding_at = lsn + 1;
return;
}

But I realized it fails to check the subtransaction IDs. If the main transaction
ID precedes next_phase_at while a subtransaction ID is greater than it, I think
we'd also need to collect it into the snapshot in that case.

4.

@@ -436,7 +446,19 @@ heap2_decode(LogicalDecodingContext *ctx,
XLogRecordBuffer *buf)
* SnapBuildProcessRunningXacts().
*/
if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
+ {
+ /*
+ * If we are building snapshot and the xlog means a catalog
+ * change, we need to mark it in the reorder buffer.
+ *
+ * Now only XLOG_HEAP2_NEW_CID means a catalog change.
+ */
+ if (SnapBuildCurrentState(builder) >= SNAPBUILD_BUILDING_SNAPSHOT &&
+ TransactionIdIsValid(xid) && info == XLOG_HEAP2_NEW_CID)
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+

I feel adding this check inside the switch-case branch looks better
than duplicating some if conditions in a new branch. Also, the above code will
mark the transaction as a catalog change even in fast_forward mode, while the
code in the latter switch branch does not. I believe marking it as a catalog
change in fast_forward mode is correct, because the snapshot built in that mode
could also be shared with other decoding sessions.

I've made a simple follow-up patch that addresses the above, see 0001.

BTW, the current test in the patch doesn't reproduce the issue: it passes even
without the fix. So I tried writing some tests covering the original report and
the corner cases I mentioned above, see 0002 (atop of v7-0002).

Best Regards,
Zhijie Hou

Re: Historic snapshot doesn't track txns committed in BUILDING_SNAPSHOT state

От:
Colin McDonnell <colinmcd94@gmail.com>
Дата:
Вложения:
Hi,

I hit this from the other direction, making logical slots while other
sessions ran DDL, and only found this thread afterwards. So here is some
review by testing, of v7 plus Zhijie's two top-up patches on master at
72b9498df9.

The attached slot_under_ddl.sh needs only pgbench, psql and
test_decoding. Six pgbench clients create a table, add a column, insert
into it in a transaction held open for 2ms, and drop it. One session
loops over creating a slot, decoding twice and dropping it. 300 rounds
took about 20 seconds here.

It also counts decoded inserts that lack the added column, since nothing
else reports those. That is the silent case Manu described in September,
and it turns out about as common as the errors. 1000 rounds,
assert-enabled builds:

                                errors   inserts missing the column
  REL_14_STABLE f72e4686e2        156      129
  REL_18_STABLE 813a74bc5a        188      143
  REL_19_STABLE 0b1eb8b917        180      128
  master        72b9498df9        168      150

The errors are "could not map filenumber" (filenode on 14) and
"pg_attribute catalog is missing N attribute(s)". One failed round can
count twice because it decodes twice. This was a Mac with load average
around 60 from unrelated work and slots being created in a tight loop,
so I wouldn't read anything into the rate.

master with v7 and the top-ups gave 0 errors and 0 missing columns in
5000 rounds.

The wrong output also isn't limited to transactions in flight at the
consistent point, or to missing columns. The attached
snapshot_build_stale.spec needs no load. ALTER TABLE t ADD COLUMN b and
TRUNCATE nk commit in BUILDING_SNAPSHOT, then inserts into t run across
and after CONSISTENT, one of them after a CHECKPOINT, followed by a
DELETE from nk, which has no replica identity, then an ANALYZE and one
more insert. On master with autovacuum off, every run:

  table public.t: INSERT: id[integer]:1 a[text]:'straddles'
  table public.t: INSERT: id[integer]:2 a[text]:'after consistent'
  table public.t: INSERT: id[integer]:3 a[text]:'after checkpoint'
  table public.t: INSERT: id[integer]:4 a[text]:'after analyze' b[integer]:5

With the patches all four inserts have b, and this line appears before
the last one:

  table public.nk: DELETE: (no-tuple-data)

So the DELETE is lost on master with no error. From reading the code,
SnapBuildProcessRunningXacts moves builder->xmin but keeps the cached
builder->snapshot, and SnapBuildProcessChange hands that to new
transactions until a catalog-changing commit is decoded, which here is
the ANALYZE. With no later DDL or ANALYZE I don't see what would end it.
The DELETE goes because the truncated table's new file maps to no
relation under that snapshot, and a change with no tuple data takes the
goto change_done branch in ReorderBufferProcessTXN that exists for
mapped catalogs, instead of raising the error. I didn't try this through
pgoutput, but that skip comes before any plugin is called.

The attached ddl_mix.sql is harsher: DDL in a subtransaction after the
top-level xid is assigned, COMMIT PREPARED, ROLLBACK TO, a table rewrite
and a rolled back CREATE. It is the script's fourth argument. 3000
rounds:

                                errors   inserts missing the column
  master                          677      250
  master + v7 only                 97      158
  master + v7 + top-ups             0        0

v7 alone was clean on the simple workload, so this agrees with Zhijie's
review that it isn't enough. I didn't work out which of the three top-up
changes accounts for what. With two_phase => true on the slots (a small
edit to the script), master gave 879 and 338, and the patched build 0
and 0.

I had also written Sawada-san's April 2025 idea as a quick fix before
finding the thread: call SnapBuildCommitTxn from xact_decode in
BUILDING_SNAPSHOT and treat XACT_XINFO_HAS_INVALS as a catalog change
before CONSISTENT, with no NEW_CID tracking. Clean on the simple
workload, 80 errors and 144 missing columns on the mix, and it fails the
second and third permutations of Zhijie's spec. Mine had neither the
subxact check nor the accumulated invalidations from the top-ups, so
that may say more about my version than the idea, but with Manu's
numbers on large objects and user catalog tables I'd stop looking in
that direction.

Zhijie's snapshot_build spec was stable for me: 30 of 30 runs pass with
the top-ups, and on master all three permutations fail in 30 of 30. It
doesn't have Manu's ALTER TABLE ADD COLUMN and COMMIT PREPARED
permutations, which seem worth merging in.

With the patches, make check passes in contrib/test_decoding,
src/test/subscription and src/test/recovery. I did not run check-world.

On back branches, the patches don't apply to REL_18_STABLE with git
apply and one hunk needed doing by hand, because the NEW_CID case has a
different shape there. That diff is attached with the code changes only.
On it, 5000 rounds of the simple workload were clean, the spec passed 5
of 5 and test_decoding passes. I haven't run the subscription or
recovery suites on it or tried 14 through 17. I see no change to the
on-disk snapshot format or to a function signature, so conflicts look
like the main work of a backpatch, but someone who knows this code
should say.

Not covered by any of this:

- pgoutput and subscribers, I only looked at test_decoding output
- user catalog tables, which test_decoding never reads
- fast-forward mode, so top-up item 3 isn't exercised
- the cost, which I didn't measure. From reading it, a catalog-changing
  transaction in BUILDING_SNAPSHOT now gets a ReorderBufferTXN, its
  invalidations and a slot in committed.xip, as it already does in
  FULL_SNAPSHOT.

Regards,
Colin
FAQ