Skip to content

Commit

Permalink
Fix swap of relfrozenxid, relfrozenxid and relallvisible (#377)
Browse files Browse the repository at this point in the history
This PR is follow up of #157.
- Fixed the typo: #157 (comment)
- Removed `PG_VERSION_NUM >= 90300` check since we don't support such old versions anyway
- Added check `relform1->relkind != RELKIND_INDEX` since `relfrozenxid` and `relminmxid` is non-zero only for tables

---------

Co-authored-by: Alexey Bashtanov <[email protected]>
  • Loading branch information
za-arthur and bashtanov authored May 21, 2024
1 parent b583237 commit cfa429a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1225,20 +1225,28 @@ 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))
/*
* Swap relfrozenxid and relminmxid, as they must be consistent with the data
*/
if (relform1->relkind != RELKIND_INDEX)
{
if (TransactionIdFollows(relform1->relfrozenxid,
relform2->relfrozenxid))
relform1->relfrozenxid = relform2->relfrozenxid;
else
relform2->relfrozenxid = relform1->relfrozenxid;
TransactionId frozenxid;
MultiXactId minmxid;

frozenxid = relform1->relfrozenxid;
relform1->relfrozenxid = relform2->relfrozenxid;
relform2->relfrozenxid = frozenxid;

minmxid = relform1->relminmxid;
relform1->relminmxid = relform2->relminmxid;
relform2->relminmxid = minmxid;
}

/* swap size statistics too, since new rel has freshly-updated stats */
{
int32 swap_pages;
float4 swap_tuples;
int32 swap_allvisible;

swap_pages = relform1->relpages;
relform1->relpages = relform2->relpages;
Expand All @@ -1247,6 +1255,10 @@ swap_heap_or_index_files(Oid r1, Oid r2)
swap_tuples = relform1->reltuples;
relform1->reltuples = relform2->reltuples;
relform2->reltuples = swap_tuples;

swap_allvisible = relform1->relallvisible;
relform1->relallvisible = relform2->relallvisible;
relform2->relallvisible = swap_allvisible;
}

indstate = CatalogOpenIndexes(relRelation);
Expand Down

0 comments on commit cfa429a

Please sign in to comment.