Skip to content

Commit

Permalink
fix relfrozenxid and relminmxid on table data files swap
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtanov authored and za-arthur committed Jan 4, 2024
1 parent 3ecda64 commit bbbd008
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ swap_heap_or_index_files(Oid r1, Oid r2)
Form_pg_class relform1,
relform2;
Oid swaptemp;
TransactionId swaptempxid;
CatalogIndexState indstate;

/* We need writable copies of both pg_class tuples. */
Expand Down Expand Up @@ -1225,15 +1226,18 @@ swap_heap_or_index_files(Oid r1, Oid r2)
relform1->reltoastrelid = relform2->reltoastrelid;
relform2->reltoastrelid = swaptemp;

/* set rel1's frozen Xid to larger one */
if (TransactionIdIsNormal(relform1->relfrozenxid))
{
if (TransactionIdFollows(relform1->relfrozenxid,
relform2->relfrozenxid))
relform1->relfrozenxid = relform2->relfrozenxid;
else
relform2->relfrozenxid = relform1->relfrozenxid;
}
/*
* Swap relfrozenxid and relminmxid, as they must be consistent with the data
*/
swaptemp = relform1->relfrozenxid;
relform1->relfrozenxid = relform2->relfrozenxid;
relform2->relfrozenxid = swaptempxid;

#if PG_VERSION_NUM >= 90300
swaptemp = relform1->relminmxid;
relform1->relminmxid = relform2->relminmxid;
relform2->relminmxid = swaptempxid;
#endif

/* swap size statistics too, since new rel has freshly-updated stats */
{
Expand All @@ -1247,6 +1251,12 @@ swap_heap_or_index_files(Oid r1, Oid r2)
swap_tuples = relform1->reltuples;
relform1->reltuples = relform2->reltuples;
relform2->reltuples = swap_tuples;

#if PG_VERSION_NUM >= 90200
swap_pages = relform1->relallvisible;
relform1->relallvisible = relform2->relallvisible;
relform2->relallvisible = swap_pages;
#endif
}

indstate = CatalogOpenIndexes(relRelation);
Expand Down

0 comments on commit bbbd008

Please sign in to comment.