This is an automated email from the ASF dual-hosted git repository.

oppenheimer01 pushed a commit to branch cbdb-postgres-merge
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 93b11456bc9522168ebe0e660fef4367a754e979
Author: Jianghua Yang <[email protected]>
AuthorDate: Wed Apr 8 22:47:38 2026 +0800

    Fix aoco_relation_size() using wrong snapshot to read pg_aocsseg
    
    aoco_relation_size() used GetLatestSnapshot() to read pg_aocsseg
    catalog metadata. During ALTER TABLE SET DISTRIBUTED BY on AOCO
    tables, the reader gang's GetLatestSnapshot() cannot see pg_aocsseg
    rows written by the writer gang within the same distributed
    transaction (uncommitted local xid), causing the function to return
    0 bytes. This led to relpages=0 being passed to vac_update_relstats()
    alongside a non-zero totalrows from sampling (which correctly uses
    GetCatalogSnapshot()), triggering an assertion failure:
    
      FailedAssertion: "Gp_role == GP_ROLE_UTILITY", vacuum.c:1738
    
    Fix by passing NULL to GetAllAOCSFileSegInfo() so that
    systable_beginscan() uses GetCatalogSnapshot() internally, consistent
    with appendonly_relation_size() for AO row tables.
---
 src/backend/access/aocs/aocsam_handler.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/backend/access/aocs/aocsam_handler.c 
b/src/backend/access/aocs/aocsam_handler.c
index a87bd517aed..5cbf2015c64 100644
--- a/src/backend/access/aocs/aocsam_handler.c
+++ b/src/backend/access/aocs/aocsam_handler.c
@@ -2043,15 +2043,25 @@ static uint64
 aoco_relation_size(Relation rel, ForkNumber forkNumber)
 {
        AOCSFileSegInfo   **allseg;
-       Snapshot                        snapshot;
        uint64                          totalbytes      = 0;
        int                                     totalseg;
 
        if (forkNumber != MAIN_FORKNUM)
                return totalbytes;
 
-       snapshot = RegisterSnapshot(GetLatestSnapshot());
-       allseg = GetAllAOCSFileSegInfo(rel, snapshot, &totalseg, NULL);
+       /*
+        * Pass NULL as snapshot so that GetAllAOCSFileSegInfo -> 
systable_beginscan
+        * uses GetCatalogSnapshot() internally.  This is consistent with
+        * appendonly_relation_size() for AO row tables and ensures pg_aocsseg
+        * entries are visible even when called within the same transaction that
+        * populated them (e.g. ALTER TABLE SET DISTRIBUTED BY).
+        *
+        * Using GetLatestSnapshot() here previously caused the metadata to be
+        * invisible on QE segments during in-transaction redistribution, 
leading
+        * to a zero return value and a subsequent assertion failure in
+        * vac_update_relstats().
+        */
+       allseg = GetAllAOCSFileSegInfo(rel, NULL, &totalseg, NULL);
        for (int seg = 0; seg < totalseg; seg++)
        {
                for (int attr = 0; attr < RelationGetNumberOfAttributes(rel); 
attr++)
@@ -2078,7 +2088,6 @@ aoco_relation_size(Relation rel, ForkNumber forkNumber)
                FreeAllAOCSSegFileInfo(allseg, totalseg);
                pfree(allseg);
        }
-       UnregisterSnapshot(snapshot);
 
        return totalbytes;
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to