REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Started by shihao zhong3 months ago17 messageshackers
Jump to latest
#1shihao zhong
zhong950419@gmail.com

HI hackers,

Running two REPACK (CONCURRENTLY) on one database with
ALTER DATABASE ... SET lock_timeout = '5s', the second one fails:

ERROR: canceling statement due to lock timeout
CONTEXT: waiting for ShareLock on transaction 2076
REPACK decoding worker

The worker waits for the first REPACK's XID in the snapshot builder.
It runs in its own session as the table owner, so the database-level
lock_timeout applies to it, and SET lock_timeout = 0 in the REPACK
session does not reach it.

The attached patch turns the settable timeouts off in the worker, as
autovacuum does. statement_timeout and cancel on the REPACK session
still stop the whole command.

Separately, the wait itself means REPACK (CONCURRENTLY) runs are
serialized. A PROC_IN_SAFE_IC-like flag could let the worker skip
other REPACK transactions; I can look into that if there is interest.

Thanks,
Shihao

Attachments:

t253694_1
0001-Force-timeouts-off-in-the-REPACK-CONCURRENTLY-decodi.patchapplication/octet-stream; name=0001-Force-timeouts-off-in-the-REPACK-CONCURRENTLY-decodi.patchDownload+15-1
N1-repro.logapplication/octet-stream; name=N1-repro.logDownload
#2Chao Li
li.evan.chao@gmail.com
In reply to: shihao zhong (#1)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On Sep 7, 2026, at 06:05, shihao zhong <zhong950419@gmail.com> wrote:

HI hackers,

Running two REPACK (CONCURRENTLY) on one database with
ALTER DATABASE ... SET lock_timeout = '5s', the second one fails:

ERROR: canceling statement due to lock timeout
CONTEXT: waiting for ShareLock on transaction 2076
REPACK decoding worker

The worker waits for the first REPACK's XID in the snapshot builder.
It runs in its own session as the table owner, so the database-level
lock_timeout applies to it, and SET lock_timeout = 0 in the REPACK
session does not reach it.

The attached patch turns the settable timeouts off in the worker, as
autovacuum does. statement_timeout and cancel on the REPACK session
still stop the whole command.

Seems reasonable to me. As you also mentioned in the code comment, auto-vacuum does the same thing.

Separately, the wait itself means REPACK (CONCURRENTLY) runs are
serialized. A PROC_IN_SAFE_IC-like flag could let the worker skip
other REPACK transactions; I can look into that if there is interest.

Yeah, I think the PROC_IN_SAFE_IC-like flag is the key point here. Since multiple VACUUM FULL commands on different tables can run in parallel, having REPACK (CONCURRENTLY) operations serialized could make the feature less competitive. In practice, it is quite common to need to repack more than one table, so allowing independent REPACK operations to proceed in parallel seems worthwhile.

Thanks,
Shihao
<0001-Force-timeouts-off-in-the-REPACK-CONCURRENTLY-decodi.patch><N1-repro.log>

The patch itself looks good to me. The PROC_IN_SAFE_IC-like flag enhancement could probably be discussed separately. I imagine that would be a larger feature?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Alvaro Herrera
alvherre@kurilemu.de
In reply to: shihao zhong (#1)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On 2026-Sep-06, shihao zhong wrote:

The worker waits for the first REPACK's XID in the snapshot builder.
It runs in its own session as the table owner, so the database-level
lock_timeout applies to it, and SET lock_timeout = 0 in the REPACK
session does not reach it.

The attached patch turns the settable timeouts off in the worker, as
autovacuum does. statement_timeout and cancel on the REPACK session
still stop the whole command.

Hmm, but there's no practical effect here, right? If it doesn't die
because of this particular timeout, it will fail due to some other
timeout. We just don't have an implementation that allows to run two
concurrent REPACK CONCURRENTLY. Will you later suggest to turn off
statement_timeout during REPACK?

Separately, the wait itself means REPACK (CONCURRENTLY) runs are
serialized. A PROC_IN_SAFE_IC-like flag could let the worker skip
other REPACK transactions; I can look into that if there is interest.

Antonin Houska has a patch which would probably benefit from your
review. /messages/by-id/108776.1784105248@localhost

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Hay dos momentos en la vida de un hombre en los que no debería
especular: cuando puede permitírselo y cuando no puede" (Mark Twain)

#4shihao zhong
zhong950419@gmail.com
In reply to: Alvaro Herrera (#3)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

but there's no practical effect here, right? If it doesn't die because
of this particular timeout, it will fail due to some other timeout.

It's not really about two concurrent REPACKs. The worker waits for
every running XID, so a single ordinary write transaction does it:

ALTER DATABASE postgres SET lock_timeout = '2s';
-- session 1: BEGIN; INSERT INTO other VALUES (1); (left open)
-- session 2: SET lock_timeout = 0; REPACK (CONCURRENTLY) t;

ERROR: canceling statement due to lock timeout
CONTEXT: waiting for ShareLock on transaction 2096
REPACK decoding worker

would statement_timeout also need to be turned off?

It's in the patch already. My first email was sloppy about this. What I
meant was that a timeout or a cancel on the caller's own session still
kills the command, and the patch leaves that alone. statement_timeout
and idle_in_transaction_session_timeout aren't fire in a bgworker
anyway. I only kept them so the block reads the same as autovacuum.

What bothers me is that the caller can't work around it. A session
level SET doesn't reach the worker. ALTER SYSTEM ranks below ALTER
DATABASE, so that doesn't work either. That leaves two options. You can
reset it on the database, but then every session in that database loses
lock_timeout, and it only takes effect for new connections. Or you can
set it on the owner role, but that needs CREATEROLE plus ADMIN on that
role. A DBA who only has MAINTAIN can't do either one. And the first
option means weakening a protection that the running workload depends
on, just to get a maintenance command to start. That seems backwards
for a command whose whole point is to do the job of VACUUM FULL without
disrupting the workload.

Antonin Houska has a patch which would probably benefit from your
review. /messages/by-id/108776.1784105248@localhost

Thanks for pointing me to Antonin's patch. I don't think it helps here
though. It keeps REPACK from holding an XID, so REPACK stops blocking
other people. But the worker is on the receiving end. It still has to
wait for whatever regular transactions are running, and that is where
the inherited lock_timeout hits it. The transaction in my repro above
is just a plain INSERT.

I went through the set to be sure. The wait itself is in
SnapBuildWaitSnapshot(), and that function is unchanged across all eight
patches. I do want to look at his work properly, but I'll do that on his
own thread rather than here.

Thanks,
Shihao

#5Alvaro Herrera
alvherre@kurilemu.de
In reply to: shihao zhong (#4)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On 2026-Sep-08, shihao zhong wrote:

It's in the patch already. My first email was sloppy about this. What I
meant was that a timeout or a cancel on the caller's own session still
kills the command, and the patch leaves that alone. statement_timeout
and idle_in_transaction_session_timeout aren't fire in a bgworker
anyway. I only kept them so the block reads the same as autovacuum.

What bothers me is that the caller can't work around it. A session
level SET doesn't reach the worker.

Ahh, that changes the problem framing completely, and I understand your
whole point now. I'm not sure what the best solution is though, but I'm
not convinced that resetting the timeouts completely is it. I would
prefer to have a way to transmit the effective values from the leader
backend to the worker: that way, you use a session-level SET and that is
also valid in the worker. I guess the question is how to effect such a
transmission ...

Antonin Houska has a patch which would probably benefit from your
review. /messages/by-id/108776.1784105248@localhost

Thanks for pointing me to Antonin's patch. I don't think it helps here
though.

Oh, I didn't intend to suggest that Antonin's patch helps this case, but
rather that if you have development gray cells to spare in connection
with REPACK, that's a very good place to put them to work.

I do want to look at his work properly, but I'll do that on his
own thread rather than here.

Sounds good!

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Investigación es lo que hago cuando no sé lo que estoy haciendo"
(Wernher von Braun)

#6shihao zhong
zhong950419@gmail.com
In reply to: shihao zhong (#4)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro,

I would prefer to have a way to transmit the effective values from the
leader backend to the worker

Done in v2, through the DSM segment the worker already attaches to.

Only lock_timeout and transaction_timeout are passed. A bgworker never
arms statement_timeout or idle_in_transaction_session_timeout. v1 have
because it aligns with the autovacuum worker.

One behavior change from v1. v1 turned the timeouts off, so REPACK always
got through. v2 uses the caller's values instead, so if the caller has a
lock_timeout of its own, the command can still be cancelled.

The difference is that it is now cancelled by a value the
caller sees in SHOW and can override with SET, rather than by one coming
from the owner role that the caller cannot reach at all.

I did consider SerializeGUCState, but that seems too much.

Applies cleanly to master and REL_19_STABLE.

Regards,
Shihao

Attachments:

t253694_6
v2-0001-Pass-the-backend-s-timeout-settings-to-the-REPACK.patchapplication/octet-stream; name=v2-0001-Pass-the-backend-s-timeout-settings-to-the-REPACK.patchDownload+21-0
#7Chao Li
li.evan.chao@gmail.com
In reply to: shihao zhong (#6)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On Sep 10, 2026, at 11:21, shihao zhong <zhong950419@gmail.com> wrote:

Hi Alvaro,

I would prefer to have a way to transmit the effective values from the
leader backend to the worker

Done in v2, through the DSM segment the worker already attaches to.

Only lock_timeout and transaction_timeout are passed. A bgworker never
arms statement_timeout or idle_in_transaction_session_timeout. v1 have
because it aligns with the autovacuum worker.

One behavior change from v1. v1 turned the timeouts off, so REPACK always
got through. v2 uses the caller's values instead, so if the caller has a
lock_timeout of its own, the command can still be cancelled.

The difference is that it is now cancelled by a value the
caller sees in SHOW and can override with SET, rather than by one coming
from the owner role that the caller cannot reach at all.

I did consider SerializeGUCState, but that seems too much.

Applies cleanly to master and REL_19_STABLE.

Regards,
Shihao
<v2-0001-Pass-the-backend-s-timeout-settings-to-the-REPACK.patch>

Auto-vacuum explicitly overrides all four settable session timeouts (statement_timeout, transaction_timeout, lock_timeout, and idle_in_transaction_session_timeout) to zero, while this worker only handles the latter two. I understand that statement_timeout and idle_in_transaction_session_timeout are probably never armed by this worker, so functionally they may not need special handling.

My concern is that the inconsistency might lead to confusion to future readers. Does it make sense to either remove those two from auto-vacuum worker or set them to repack worker as well?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#8Alvaro Herrera
alvherre@kurilemu.de
In reply to: Chao Li (#7)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On 2026-Sep-10, Chao Li wrote:

On Sep 10, 2026, at 11:21, shihao zhong <zhong950419@gmail.com> wrote:

Done in v2, through the DSM segment the worker already attaches to.

I think this is pretty reasonable.

Auto-vacuum explicitly overrides all four settable session timeouts
(statement_timeout, transaction_timeout, lock_timeout, and
idle_in_transaction_session_timeout) to zero, while this worker only
handles the latter two. I understand that statement_timeout and
idle_in_transaction_session_timeout are probably never armed by this
worker, so functionally they may not need special handling.

Hmm, but REPACK is not autovacuum; it's quite different in fact, in that
REPACK is intended to always be invoked manually, while autovacuum runs
on its own. On the other hand, because REPACK refuses to run in a
transaction block, transaction_timeout and
idle_in_transaction_session_timeout don't really apply, so I'm not
seeing the potential for problems.

My concern is that the inconsistency might lead to confusion to future
readers. Does it make sense to either remove those two from
auto-vacuum worker or set them to repack worker as well?

I decidedly don't want to touch autovacuum. Although I'm not sure I see
the reason why the transaction-based timeouts are relevant for
autovacuum.

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/

#9Chao Li
li.evan.chao@gmail.com
In reply to: Alvaro Herrera (#8)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On Sep 12, 2026, at 00:33, Álvaro Herrera <alvherre@kurilemu.de> wrote:

On 2026-Sep-10, Chao Li wrote:

On Sep 10, 2026, at 11:21, shihao zhong <zhong950419@gmail.com> wrote:

Done in v2, through the DSM segment the worker already attaches to.

I think this is pretty reasonable.

Auto-vacuum explicitly overrides all four settable session timeouts
(statement_timeout, transaction_timeout, lock_timeout, and
idle_in_transaction_session_timeout) to zero, while this worker only
handles the latter two. I understand that statement_timeout and
idle_in_transaction_session_timeout are probably never armed by this
worker, so functionally they may not need special handling.

Hmm, but REPACK is not autovacuum; it's quite different in fact, in that
REPACK is intended to always be invoked manually, while autovacuum runs
on its own. On the other hand, because REPACK refuses to run in a
transaction block, transaction_timeout and
idle_in_transaction_session_timeout don't really apply, so I'm not
seeing the potential for problems.

Yeah, I fully understood the difference. My concern was only about the inconsistency.

My concern is that the inconsistency might lead to confusion to future
readers. Does it make sense to either remove those two from
auto-vacuum worker or set them to repack worker as well?

I decidedly don't want to touch autovacuum. Although I'm not sure I see
the reason why the transaction-based timeouts are relevant for
autovacuum.

That was actually my concern. The fact that this raised the question of why autovacuum resets those timeouts suggests that the inconsistency can be confusing to readers.

I agree we don't need to touch autovacuum in this patch. Does it make sense to remove those unnecessary timeout resets from autovacuum by a separate patch?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#10Chao Li
li.evan.chao@gmail.com
In reply to: Chao Li (#9)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On Sep 12, 2026, at 10:57, Chao Li <li.evan.chao@gmail.com> wrote:

On Sep 12, 2026, at 00:33, Álvaro Herrera <alvherre@kurilemu.de> wrote:

On 2026-Sep-10, Chao Li wrote:

On Sep 10, 2026, at 11:21, shihao zhong <zhong950419@gmail.com> wrote:

Done in v2, through the DSM segment the worker already attaches to.

I think this is pretty reasonable.

Auto-vacuum explicitly overrides all four settable session timeouts
(statement_timeout, transaction_timeout, lock_timeout, and
idle_in_transaction_session_timeout) to zero, while this worker only
handles the latter two. I understand that statement_timeout and
idle_in_transaction_session_timeout are probably never armed by this
worker, so functionally they may not need special handling.

Hmm, but REPACK is not autovacuum; it's quite different in fact, in that
REPACK is intended to always be invoked manually, while autovacuum runs
on its own. On the other hand, because REPACK refuses to run in a
transaction block, transaction_timeout and
idle_in_transaction_session_timeout don't really apply, so I'm not
seeing the potential for problems.

Yeah, I fully understood the difference. My concern was only about the inconsistency.

My concern is that the inconsistency might lead to confusion to future
readers. Does it make sense to either remove those two from
auto-vacuum worker or set them to repack worker as well?

I decidedly don't want to touch autovacuum. Although I'm not sure I see
the reason why the transaction-based timeouts are relevant for
autovacuum.

That was actually my concern. The fact that this raised the question of why autovacuum resets those timeouts suggests that the inconsistency can be confusing to readers.

I agree we don't need to touch autovacuum in this patch. Does it make sense to remove those unnecessary timeout resets from autovacuum by a separate patch?

Say, if another worker is added in the future, the author may look at both the autovacuum and repack workers as references, notice that they reset different sets of timeouts, and then have to spend time figuring out which behavior to follow and why.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#11shihao zhong
zhong950419@gmail.com
In reply to: Chao Li (#10)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro, Chao,

Attached is v3. The code is the same as v2, with two additions.

1. The comment in RepackWorkerMain() now says why only lock_timeout and
transaction_timeout are passed. lock_timeout is armed by any lock wait,
and transaction_timeout by StartTransaction(). Both can fire in the
worker: the wait for older transactions in the snapshot builder is a
lock wait, and the worker's transaction spans the whole command.
statement_timeout and idle_in_transaction_session_timeout are only
armed by the command loop in PostgresMain(), which a background worker
never runs. I hope that answers Chao's question without touching
autovacuum.

2. A lock_timeout on the table owner role
does not reach the worker, and one set in the REPACK session does.
It fails on unpatched master and passes with 0001.

I expect this to be common in production, since a database- or
role-level lock_timeout is a normal setting. As REPACK (CONCURRENTLY)
is new in PG19, I suggest adding this to the open items and fixing it
before release.

Thanks,
Shihao

Attachments:

t253770_3
v3-0001-Pass-the-backend-s-timeout-settings-to-the-REPACK.patchapplication/octet-stream; name=v3-0001-Pass-the-backend-s-timeout-settings-to-the-REPACK.patchDownload+27-1
v3-0002-Add-isolation-test-for-timeouts-in-the-REPACK-dec.patchapplication/octet-stream; name=v3-0002-Add-isolation-test-for-timeouts-in-the-REPACK-dec.patchDownload+62-1
#12Alvaro Herrera
alvherre@kurilemu.de
In reply to: shihao zhong (#11)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

On 2026-Sep-12, shihao zhong wrote:

1. The comment in RepackWorkerMain() now says why only lock_timeout and
transaction_timeout are passed. lock_timeout is armed by any lock wait,
and transaction_timeout by StartTransaction(). Both can fire in the
worker: the wait for older transactions in the snapshot builder is a
lock wait, and the worker's transaction spans the whole command.
statement_timeout and idle_in_transaction_session_timeout are only
armed by the command loop in PostgresMain(), which a background worker
never runs. I hope that answers Chao's question without touching
autovacuum.

Thanks. I pushed the patch, but didn't keep the detailed comment. I
don't think we need to explain the details of how these timeouts work in
this comment, and it may be easy for the comment to become outdated.

I added the word "relevant" to the comment, to try and satisfy Chao's
concern: a reader of this code trying to add a new worker will have to
figure out what does "relevant" mean in whatever case they're trying to
implement :-)

2. A lock_timeout on the table owner role does not reach the worker,
and one set in the REPACK session does. It fails on unpatched master
and passes with 0001.

Did you mean to reference the proposed new test in this paragraph? I
think so, but the text says something else. (I don't really know what
the text tries to convey). Anyway, I didn't push this test either,
because I think it's likely to fail randomly on slow buildfarm machines,
so we would have to adjust the timeouts, and that becomes a whack-a-mole
game that I'm not very interested in playing at this point.

Thanks for the report and fix,

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Before you were born your parents weren't as boring as they are now. They
got that way paying your bills, cleaning up your room and listening to you
tell them how idealistic you are." -- Charles J. Sykes' advice to teenagers

#13shihao zhong
zhong950419@gmail.com
In reply to: Alvaro Herrera (#12)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro,

Thanks. I pushed the patch, but didn't keep the detailed comment.

Thanks for pushing it, and for the backpatch to 19.

Did you mean to reference the proposed new test in this paragraph?

Yes, sorry, that paragraph was about the test in 0002. You are right
that it is timing sensitive. It only caught the bug after I added a
pg_sleep before the COMMIT, so it would be fragile on slow machine.
Leaving it out is fine.

Thanks,
Shihao

#14shihao zhong
zhong950419@gmail.com
In reply to: shihao zhong (#13)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro,

I looked at the other settings to see if any of them has the same
problem. I found temp_file_limit, the worker uses it for its decoding
files. 003 fixes it the same way as the timeouts.

Thanks,
Shihao

Attachments:

t253770_6
0003-Have-the-REPACK-decoding-worker-use-temp_file_lim.patchapplication/octet-stream; name=0003-Have-the-REPACK-decoding-worker-use-temp_file_lim.patchDownload+8-4
#15shihao zhong
zhong950419@gmail.com
In reply to: shihao zhong (#14)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro,

A gentle ping on 003. I should have shown the symptom last time. If the
table owner has a role level temp_file_limit, REPACK (CONCURRENTLY) fails
under concurrent updates, even when a superuser runs it:

ERROR: temporary file size exceeds "temp_file_limit" (64kB)
CONTEXT: slot "pg_repack_11897", output plugin "pgrepack", in the change
callback, associated LSN 0/20A7BF00

That was ALTER ROLE owner SET temp_file_limit = '64kB' and 20k updates
during the REPACK. The caller can't override it, same as the timeouts
before a62ff0829d6. The patch still applies cleanly on master.

Should we also add that to the open item list?

Thanks,
Shihao

#16Alvaro Herrera
alvherre@kurilemu.de
In reply to: shihao zhong (#14)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi,

On 2026-Sep-17, shihao zhong wrote:

Hi Alvaro,

I looked at the other settings to see if any of them has the same
problem. I found temp_file_limit, the worker uses it for its decoding
files. 003 fixes it the same way as the timeouts.

I have pushed this, thanks.

I am concerned about the GUC variables changing type and the repack code
not noticing. Suppose temp_files_limit were changed to be int64; we
don't receive a compiler warning for the mismatching type in
repack_internal.h. I thought about adding a
StaticAssertVariableIsOfType, but I have no idea how to make it compare
not to a type, but to the type of some other variable. These _Generic()
things are unknown to me.

I don't understand why you were adding #include "fd.h" to repack.c.

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Finally, the phrase, 'No one was ever fired for buying an IBM' I don't believe
has ever been translated into German." (Leonard Tramiel)
https://www.atarimagazines.com/startv3n1/threeyearsofst.php

#17shihao zhong
zhong950419@gmail.com
In reply to: Alvaro Herrera (#16)
Re: REPACK (CONCURRENTLY) decoding worker is canceled by lock_timeout

Hi Alvaro,

I have pushed this, thanks.

Thanks for pushing that.

I am concerned about the GUC variables changing type and the repack code
not noticing....

I think we can just use type of + StaticAssertVariableIsOfType here, a new
patch
is attached about that.

I don't understand why you were adding #include "fd.h" to repack.c.

ehh, that is auto imported by ide, my mistake. Will recheck all the
#include for
future patches.

Thanks,
Shihao

Attachments:

t253694_17
v1-0001-Check-the-types-of-the-settings-passed-to-the-REP.patchapplication/octet-stream; name=v1-0001-Check-the-types-of-the-settings-passed-to-the-REP.patchDownload+8-1