Re: REPACK enhancements

From: Antonin Houska <ah(at)cybertec(dot)at>
To: Mihail Nikalayeu <mihailnikalayeu(at)gmail(dot)com>
Cc: Manu <manuelreyesbravo(at)gmail(dot)com>, shihao zhong <zhong950419(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: REPACK enhancements
Date: 2026-11-30 19:48:55
Message-ID: 115922.1796068135@chrudos
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Mihail Nikalayeu <mihailnikalayeu(at)gmail(dot)com> wrote:

> I haven't read the thread, so, some parts may have already been discussed.
>
> > SetHintBits(tuple, buffer, HEAP_XMIN_COMMITTED,
> > HeapTupleHeaderGetRawXmax(tuple));

Sure this is a thinko, I'll fix that in the next version.

> Should it be Xmin?
>
> > PopActiveSnapshot();
> > InvalidateCatalogSnapshot();
>
> > /* See above. */
> > Assert(!TransactionIdIsValid(MyProc->xmin));
>
> I received some helpful feedback from Mathias on related task (CIC
> snapshot rotation) - [0].
> I applied proposed fixes at [1].
>
> Main things we may apply here:
>
> 1) RotateSnapshot (preserves a valid xmin all the time)

Is the concern that, after the xmin (I suppose you mean MyProc->xmin) being
invalid for a while, the next snapshot can have its xmin older than the VACUUM
xmin horizon? I don't think this can happen here because REPACK (CONCURRENTLY)
gets its snapshots from the logical decoding system, and the underlying
replicaiton slot should not allow such situation.

> 2) SnapshotRotation (structure to track rotation frequency)

I don't think exactly this is appropriate, although a new structure is
probably needed for the arguments of finalize_block_range(), which handles the
snapshot switching. (There's a TODO comment about that in front of the
function.)

> 3) Xmin asserts done by injection points with ERROR attachment. It is
> done that way because sometimes it is not possible to avoid getting a
> snapshot due to some expressions/computed columns/etc.

I'm not sure REPACK has such a problem: 1) the heap tuples we copy already
have the expression values computed, 2) indexes are built in separate
transactions and CommitTransaction() should complain about an existing active
snapshot anyway.

> 4) Handling IsolationUsesXactSnapshot in a non-error way (just
> disables rotation)

Is it a problem if, after getting the ERROR, the user adjusts the isolation
level and runs REPACK again? I wonder if it's better to let user run REPACK
w/o ERROR and w/o snapshot rotation. Such an user might think everything is
o.k. until he notices that, due to the xmin horizon not making progress,
VACUUM does not work as efficiently as it should (and especially that too many
tuples remain unfrozen).

> 5) Add stress-tests form [2] and rotate snapshot every page (or some
> other extreme value)

Yes, that makes sense at least for development. (As for merging the stress
tests into the tree, I think it was already discussed with Alvaro. Some work
is probably needed to combine the stress tests for REPACK with those for CIC.

> Also, seems like build_new_index may use new snapshot for each index
> it builds (at least until CIC-related snapshot rotation is not in
> core).

build_new_indexes() actually uses a separate transaction for each index. This
is because by creating a new index the backend inevitably gets XID assigned,
and COMMIT is probably the easiest way to get it cleared.

> Regarding the auxiliary table, have you considered adding some
> additional information to tuplesort (like xmin/lsn/tombstone) and
> later use that during insertion to strip unneeded rows (for all rows
> with the same key, take only "last" one)?

While copying the table contents, a concurrent UPDATE can cause two versions
of the same row appear in two different ranges. If both versions have the same
value of primary/unique key, and if we want to apply the concurrent changes
after having copied all the tuples into the new heap, the problem is that we
won't be able to build an unique index.

Therefore, we always need to apply the concurrent changes before starting the
scan of next range: by replaying the UPDATE we delete the old row from the
earlier range, thus eliminating the duplicity. However, we cannot do this
while the data is still in the tuplestore because this structure has nothing
like an index.

[ Your question indicates that you understand this problem, but I try to
summarize it here for others.]

Yes, it's probably possible to modify the tuplestore / tuplesort as you
suggest, but I think it'd be trickier than using the auxiliary table.

Thanks for your comments!

> [0]: https://www.postgresql.org/message-id/CAEze2WhwwAB_DVAWfunO%3D_1QZu-J%2BThbpVbvdoOyycswQETFfw%40mail.gmail.com
> [1]: https://www.postgresql.org/message-id/CADzfLwV%2Bng11CQPju9HxJvK_rLAZ0_Zozno_%2B4Td26ZL1V_%3DrA%40mail.gmail.com
> [2]: https://www.postgresql.org/message-id/af3oWkvS9K83UqmK@alvherre.pgsql

--
Antonin Houska
Web: https://www.cybertec-postgresql.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Lukas Fittl 2026-11-30 20:03:29 Re: Stack-based tracking of per-node WAL/buffer usage
Previous Message Nathan Bossart 2026-11-30 19:23:29 Re: passwordcheck: Make the user name check case-insensitive