Parallel vacuum: workers together with leader could overrun cost budget
Parallel vacuum: workers together with leader could overrun cost budget
Hi, I noticed that the parallel workers can go over the vacuum cost budget, with both parallel vacuum and parallel autovacuum. For example, with the default cost delay of 2ms and cost limit of 200, each of the 4 parallel workers and the autovacuum worker (the leader) sleeps 2ms for every 200 cost units it incurs on its own, so each of the 5 processes runs at the full budget. If the 200 were the budget for the vacuum as a whole, each process would have to sleep 5 times longer, that is 2ms per 40 cost units. I think this is a problem, since the cost budget the user was promised gets crossed. Please find the attached patch fixing this. Although this exists for parallel vacuum too, back-patching it to PG19, where parallel autovacuum was introduced, is fine, since parallel autovacuum runs with the default cost delay and cost limit. Thoughts? -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
Hi, On Tue, Oct 13, 2026 at 8:46 PM Bharath Rupireddy wrote: > > Hi, > > I noticed that the parallel workers can go over the vacuum cost > budget, with both parallel vacuum and parallel autovacuum. For > example, with the default cost delay of 2ms and cost limit of 200, > each of the 4 parallel workers and the autovacuum worker (the leader) > sleeps 2ms for every 200 cost units it incurs on its own, so each of > the 5 processes runs at the full budget. If the 200 were the budget > for the vacuum as a whole, each process would have to sleep 5 times > longer, that is 2ms per 40 cost units. > > I think this is a problem, since the cost budget the user was promised > gets crossed. Please find the attached patch fixing this. Thank you for the report and the patch. I agree with your analysis. Currently each process sleeps in proportion to the cost it incurred by itself, so the leader and its workers together consume N times the budget. I think the cost limit should work like a rate limit of a download manager for example: using 5 connections makes the download after, but if we set the limit to 1MB/s, we expect 1MB/s in total , not 1MB/s * 5 = 5MB/s. > Although this exists for parallel vacuum too, back-patching it to > PG19, where parallel autovacuum was introduced, is fine, since > parallel autovacuum runs with the default cost delay and cost limit. Agree to backpatch it to v19. Autovaucum distributes the cost limit among the running autovacuum workers so that the sum doesn't exceed autovacuum_vacuum_cost_limit, but parallel vacuum workers are not counted there. I've reviewed the patch and have one comment: + /* + * Compute sleep time based on the local cost balance and the number + * of active workers. + */ + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / + vacuum_cost_limit; msec can be up to nworkers * vacuum_cost_delay, but vacuum_delay_point() caps the sleep time at 4 * vacuum_cost_delay. Therefore, when more than 4 parallel workers are active, the sleep time is truncated and we still overrun the budget. IIUC the cap was introduced to avoid uselessly long delays when the cost accumulates far beyond the limit. I think a simple fix is to sleep up to 4 * nworkers * vacuum_cost_delay at once. I think that's acceptable since the sleep time is still proportional to the cost the process actually incurred. Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
Hi, On Fri, Oct 16, 2026 at 10:46 AM Masahiko Sawada wrote: > > Thank you for the report and the patch. I agree with your analysis. > Currently each process sleeps in proportion to the cost it incurred by > itself, so the leader and its workers together consume N times the > budget. I think the cost limit should work like a rate limit of a > download manager for example: using 5 connections makes the download > after, but if we set the limit to 1MB/s, we expect 1MB/s in total , > not 1MB/s * 5 = 5MB/s. Nice analogy! > > Although this exists for parallel vacuum too, back-patching it to > > PG19, where parallel autovacuum was introduced, is fine, since > > parallel autovacuum runs with the default cost delay and cost limit. > > Agree to backpatch it to v19. Autovaucum distributes the cost limit > among the running autovacuum workers so that the sum doesn't exceed > autovacuum_vacuum_cost_limit, but parallel vacuum workers are not > counted there. > > I've reviewed the patch and have one comment: > > + /* > + * Compute sleep time based on the local cost balance and the number > + * of active workers. > + */ > + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / > + vacuum_cost_limit; > > msec can be up to nworkers * vacuum_cost_delay, but > vacuum_delay_point() caps the sleep time at 4 * vacuum_cost_delay. > Therefore, when more than 4 parallel workers are active, the sleep > time is truncated and we still overrun the budget. IIUC the cap was > introduced to avoid uselessly long delays when the cost accumulates > far beyond the limit. Ah, you are right. The long delays are avoided when the cost accumulates far higher than the specified limit. E.g., while holding critical locks or when interrupts are pending, the cost-based delays are not applied, so the cost keeps accumulating until the locks are released or the interrupts are processed. > I think a simple fix is to sleep up to 4 * > nworkers * vacuum_cost_delay at once. I think that's acceptable since > the sleep time is still proportional to the cost the process actually > incurred. Agreed. The non-parallel vacuum caps the sleep time whenever the accumulated cost balance crosses 4 times the cost limit. We should just do the similar capping for each worker while not letting all of them overrun the cost budget. Please find the attached v2 patch with the above changes and some doc adjustments. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Tue, Oct 20, 2026 at 9:38 AM Bharath Rupireddy wrote: > > Hi, > > On Fri, Oct 16, 2026 at 10:46 AM Masahiko Sawada wrote: > > > > Thank you for the report and the patch. I agree with your analysis. > > Currently each process sleeps in proportion to the cost it incurred by > > itself, so the leader and its workers together consume N times the > > budget. I think the cost limit should work like a rate limit of a > > download manager for example: using 5 connections makes the download > > after, but if we set the limit to 1MB/s, we expect 1MB/s in total , > > not 1MB/s * 5 = 5MB/s. > > Nice analogy! > > > > Although this exists for parallel vacuum too, back-patching it to > > > PG19, where parallel autovacuum was introduced, is fine, since > > > parallel autovacuum runs with the default cost delay and cost limit. > > > > Agree to backpatch it to v19. Autovaucum distributes the cost limit > > among the running autovacuum workers so that the sum doesn't exceed > > autovacuum_vacuum_cost_limit, but parallel vacuum workers are not > > counted there. > > > > I've reviewed the patch and have one comment: > > > > + /* > > + * Compute sleep time based on the local cost balance and the number > > + * of active workers. > > + */ > > + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / > > + vacuum_cost_limit; > > > > msec can be up to nworkers * vacuum_cost_delay, but > > vacuum_delay_point() caps the sleep time at 4 * vacuum_cost_delay. > > Therefore, when more than 4 parallel workers are active, the sleep > > time is truncated and we still overrun the budget. IIUC the cap was > > introduced to avoid uselessly long delays when the cost accumulates > > far beyond the limit. > > Ah, you are right. The long delays are avoided when the cost > accumulates far higher than the specified limit. E.g., while holding > critical locks or when interrupts are pending, the cost-based delays > are not applied, so the cost keeps accumulating until the locks are > released or the interrupts are processed. > > > I think a simple fix is to sleep up to 4 * > > nworkers * vacuum_cost_delay at once. I think that's acceptable since > > the sleep time is still proportional to the cost the process actually > > incurred. > > Agreed. The non-parallel vacuum caps the sleep time whenever the > accumulated cost balance crosses 4 times the cost limit. We should > just do the similar capping for each worker while not letting all of > them overrun the cost budget. > > Please find the attached v2 patch with the above changes and some doc > adjustments. Thank you for updating the patch! The patch looks good to me. It's nice to update the documentation part about (vacuum_cost_delay * 4 * nworkers) limit. I think this patch should be backpatched to all supported branches because the VACUUM command with the PARALLEL option should honor the cost-based delay although it's disabled by default. What do you think? Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Tue, Oct 20, 2026 at 2:01 PM Masahiko Sawada wrote: > > > Please find the attached v2 patch with the above changes and some doc > > adjustments. > > Thank you for updating the patch! The patch looks good to me. It's > nice to update the documentation part about (vacuum_cost_delay * 4 * > nworkers) limit. Thanks for reviewing it. > I think this patch should be backpatched to all supported branches > because the VACUUM command with the PARALLEL option should honor the > cost-based delay although it's disabled by default. What do you think? Yes, back-patching makes sense to me. IMHO it's a bug in parallel vacuum (40d964ec99). AFAICS it hasn't been reported anywhere. I have seen the VACUUM command run with a parallel degree when approaching XID wraparound. At that point one wants the vacuum to finish faster, so overrunning the cost is fine. I'm not sure, but it could also have gone unnoticed for those running VACUUM command cron jobs. That said, one can always tune the cost limit to speed things up or down. In any case, I'm fine with fixing it in the back-branches. I can send the patches for the back-branches. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Oct 21, 2026, at 00:38, Bharath Rupireddy wrote: > > Hi, > > On Fri, Oct 16, 2026 at 10:46 AM Masahiko Sawada wrote: >> >> Thank you for the report and the patch. I agree with your analysis. >> Currently each process sleeps in proportion to the cost it incurred by >> itself, so the leader and its workers together consume N times the >> budget. I think the cost limit should work like a rate limit of a >> download manager for example: using 5 connections makes the download >> after, but if we set the limit to 1MB/s, we expect 1MB/s in total , >> not 1MB/s * 5 = 5MB/s. > > Nice analogy! > >>> Although this exists for parallel vacuum too, back-patching it to >>> PG19, where parallel autovacuum was introduced, is fine, since >>> parallel autovacuum runs with the default cost delay and cost limit. >> >> Agree to backpatch it to v19. Autovaucum distributes the cost limit >> among the running autovacuum workers so that the sum doesn't exceed >> autovacuum_vacuum_cost_limit, but parallel vacuum workers are not >> counted there. >> >> I've reviewed the patch and have one comment: >> >> + /* >> + * Compute sleep time based on the local cost balance and the number >> + * of active workers. >> + */ >> + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / >> + vacuum_cost_limit; >> >> msec can be up to nworkers * vacuum_cost_delay, but >> vacuum_delay_point() caps the sleep time at 4 * vacuum_cost_delay. >> Therefore, when more than 4 parallel workers are active, the sleep >> time is truncated and we still overrun the budget. IIUC the cap was >> introduced to avoid uselessly long delays when the cost accumulates >> far beyond the limit. > > Ah, you are right. The long delays are avoided when the cost > accumulates far higher than the specified limit. E.g., while holding > critical locks or when interrupts are pending, the cost-based delays > are not applied, so the cost keeps accumulating until the locks are > released or the interrupts are processed. > >> I think a simple fix is to sleep up to 4 * >> nworkers * vacuum_cost_delay at once. I think that's acceptable since >> the sleep time is still proportional to the cost the process actually >> incurred. > > Agreed. The non-parallel vacuum caps the sleep time whenever the > accumulated cost balance crosses 4 times the cost limit. We should > just do the similar capping for each worker while not letting all of > them overrun the cost budget. > > Please find the attached v2 patch with the above changes and some doc > adjustments. > > -- > Bharath Rupireddy > Amazon Web Services: https://aws.amazon.com > After reading v2, I have a small concern. In the VacuumSharedCostBalance != NULL path, the sleep cap is vacuum_cost_delay * 4 * nworkers. Say vacuum_cost_delay=100ms and nworkers=16; then the sleep time could be 6.4 seconds. Passing such a long delay to pg_usleep() seems concerning because its header comment explicitly says: ``` * CAUTION: It's not a good idea to use long sleeps in the backend. They will * silently return early if a signal is caught, but that doesn't include * latches being set on most OSes, and even signal handlers that set MyLatch * might happen to run before the sleep begins, allowing the full delay. * Better practice is to use WaitLatch() with a timeout, so that backends * respond to latches and signals promptly. ``` Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Wed, Oct 21, 2026 at 7:32 PM Chao Li wrote: > > After reading v2, I have a small concern. > > In the VacuumSharedCostBalance != NULL path, the sleep cap is vacuum_cost_delay * 4 * nworkers. Say vacuum_cost_delay=100ms and nworkers=16; then the sleep time could be 6.4 seconds. Passing such a long delay to pg_usleep() seems concerning because its header comment explicitly says: Nice catch! You are right. With a max cost delay of 100ms today, the existing cap is no more than 400ms. And it looks like other pg_usleep() callers in core will not wait more than 1s at once (except the auth delays, which are developer options anyway). And per the comment in WaitExceedsMaxStandbyDelay(), it seems that waiting more than 1s with pg_usleep() is the "too long" beyond which backends lose prompt response to latches and signals. I think there are a couple of ways to handle this: 1/ Keep the cap at cost_delay * 4 * nworkers, but replace pg_usleep() with WaitLatch() in 400ms steps (or even 1s steps). 2/ Cap the delay per worker at 1s (similar to WaitExceedsMaxStandbyDelay()) i.e. Min(Min(msec, cost_delay * 4 * nworkers), 1000). This still looks fine, and it's better than today's 400ms limit. I prefer option 2, with a 1s cap per worker. Thoughts? -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Oct 23, 2026, at 03:30, Bharath Rupireddy wrote:
>
> Hi,
>
> On Wed, Oct 21, 2026 at 7:32 PM Chao Li wrote:
>>
>> After reading v2, I have a small concern.
>>
>> In the VacuumSharedCostBalance != NULL path, the sleep cap is vacuum_cost_delay * 4 * nworkers. Say vacuum_cost_delay=100ms and nworkers=16; then the sleep time could be 6.4 seconds. Passing such a long delay to pg_usleep() seems concerning because its header comment explicitly says:
>
> Nice catch! You are right. With a max cost delay of 100ms today, the
> existing cap is no more than 400ms. And it looks like other
> pg_usleep() callers in core will not wait more than 1s at once (except
> the auth delays, which are developer options anyway). And per the
> comment in WaitExceedsMaxStandbyDelay(), it seems that waiting more
> than 1s with pg_usleep() is the "too long" beyond which backends lose
> prompt response to latches and signals.
>
> I think there are a couple of ways to handle this:
>
> 1/ Keep the cap at cost_delay * 4 * nworkers, but replace pg_usleep()
> with WaitLatch() in 400ms steps (or even 1s steps).
>
The header comment of pg_usleep() also suggests using WaitLatch() with a timeout for long sleeps. However, one issue is that WaitLatch() takes a timeout in milliseconds, while vacuum needs microsecond precision.
> 2/ Cap the delay per worker at 1s (similar to
> WaitExceedsMaxStandbyDelay()) i.e. Min(Min(msec, cost_delay * 4 *
> nworkers), 1000). This still looks fine, and it's better than today's
> 400ms limit.
>
Wouldn't the 1-second cap still allow the cost budget to be exceeded in some cases, which is exactly what this patch is trying to fix? If so, wouldn't that leave the fix incomplete?
> I prefer option 2, with a 1s cap per worker. Thoughts?
>
> --
> Bharath Rupireddy
> Amazon Web Services: https://aws.amazon.com
I'm thinking that maybe we could combine WaitLatch() and pg_usleep(). Use WaitLatch() to sleep for the whole-millisecond portion, and leave the remaining sub-millisecond portion to pg_usleep(), so we can preserve the precision without sacrificing responsiveness.
The pseudocode would look something like this:
```
while (remaining_us > 0)
{
CHECK_FOR_INTERRUPTS();
if (remaining_us >= 1000)
{
(void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
remaining_us / 1000,
WAIT_EVENT_VACUUM_DELAY);
ResetLatch(MyLatch);
}
else
{
pg_usleep(remaining_us);
}
/* Recalculate remaining_us. */
remaining_us = ...
}
```
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Thu, Oct 22, 2026 at 5:53 PM Chao Li wrote: > > > I think there are a couple of ways to handle this: > > > 2/ Cap the delay per worker at 1s (similar to > > WaitExceedsMaxStandbyDelay()) i.e. Min(Min(msec, cost_delay * 4 * > > nworkers), 1000). This still looks fine, and it's better than today's > > 400ms limit. > > Wouldn't the 1-second cap still allow the cost budget to be exceeded in some cases, which is exactly what this patch is trying to fix? If so, wouldn't that leave the fix incomplete? I think the 1s cap with pg_usleep() is fine here. For any worker or the leader to reach the cap, its cost balance has to be far above its share of the cost limit (VacuumCostBalanceLocal/vacuum_cost_limit >= 4x), which only happens when many delay points are skipped. E.g., with cost delay 100ms, cost limit 200, and 16 processes, each process gets 12.5, so VacuumCostBalanceLocal has to reach 800 to hit 4x, which means skipping about 40 delay points while dirtying pages, or about 400 while reading pages. That looks rare in practice, so I prefer to keep the 1s cap with pg_usleep(). That said, the WaitLatch() approach might be worth discussing for HEAD, though I don't feel strongly about it. One other idea with the 1s cap is to carry the leftover balance over to the next delay point, but I don't think it's worth the complexity. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Bharath,
Thank you for the patches.
I ran v2 on master for 1M rows, 8 indexes, 10% deleted, cost delay 10ms / limit 1000, so a budget of about 100k units/s. Each run
did roughly the same work (~650k units, estimated from VERBOSE buffer counts):
PARALLEL master v2
0 82k/s 7.9s 82k/s 8.0s
2 152k/s 4.3s 90k/s 7.2s
8 (7) 243k/s 2.7s 97k/s 6.7s
So master runs at up to ~2.4x the budget, and v2 stays under it, including with more than 4 processes. The vacuum and vacuum_parallel
regression output matches expected.
Regarding the 1s cap, I think the 800 in your example is where the 6.4s
cap kicks in, not the 1s one. With delay 100ms, limit 200 and 16 processes, the 1s cap applies once the local balance passes 125,
which can probably happen just from uneven work across processes, no skipped delay points needed. It's bounded though, a capped process
gets at most roughly 1.6-1.8x its share there, and only once nworkers * cost_delay gets near 1s. So I'm fine with the 1s cap, but
I'd mention that in the comment instead of saying the budget always holds.
Regarding the WaitLatch() for HEAD, worth remembering it was tried before
(4753ef37e0e) and reverted in 720de00af49 because the millisecond timeout broke fractional cost_delay.
One thing I'm less sure about is back-patching to 14-18. Plain manual VACUUM already goes parallel when there are enough indexes, so anyone
with a cost delay set could see it get noticeably slower after a minor update (with the default 2 workers, 4.3s -> 7.2s above).
Fixing it in 19 and master seems safer to me, but I'm happy to go with the consensus.
Thanks,
Surya Poondla
Thank you for the patches.
I ran v2 on master for 1M rows, 8 indexes, 10% deleted, cost delay 10ms / limit 1000, so a budget of about 100k units/s. Each run
did roughly the same work (~650k units, estimated from VERBOSE buffer counts):
PARALLEL master v2
0 82k/s 7.9s 82k/s 8.0s
2 152k/s 4.3s 90k/s 7.2s
8 (7) 243k/s 2.7s 97k/s 6.7s
So master runs at up to ~2.4x the budget, and v2 stays under it, including with more than 4 processes. The vacuum and vacuum_parallel
regression output matches expected.
Regarding the 1s cap, I think the 800 in your example is where the 6.4s
cap kicks in, not the 1s one. With delay 100ms, limit 200 and 16 processes, the 1s cap applies once the local balance passes 125,
which can probably happen just from uneven work across processes, no skipped delay points needed. It's bounded though, a capped process
gets at most roughly 1.6-1.8x its share there, and only once nworkers * cost_delay gets near 1s. So I'm fine with the 1s cap, but
I'd mention that in the comment instead of saying the budget always holds.
Regarding the WaitLatch() for HEAD, worth remembering it was tried before
(4753ef37e0e) and reverted in 720de00af49 because the millisecond timeout broke fractional cost_delay.
One thing I'm less sure about is back-patching to 14-18. Plain manual VACUUM already goes parallel when there are enough indexes, so anyone
with a cost delay set could see it get noticeably slower after a minor update (with the default 2 workers, 4.3s -> 7.2s above).
Fixing it in 19 and master seems safer to me, but I'm happy to go with the consensus.
Thanks,
Surya Poondla
Re: Parallel vacuum: workers together with leader could overrun cost budget
Hi Surya, On Fri, Oct 23, 2026 at 2:50 PM surya poondla wrote: > It's bounded though, a capped process > gets at most roughly 1.6-1.8x its share there, and only once nworkers * > cost_delay gets near 1s. I measured it on master + v2 with vacuum_cost_delay = 100ms, 20 btree indexes and 10% of the rows deleted, N = 3. Cost spent per second, as a multiple of the budget: limit 200, PARALLEL 4/8/16: 1 s cap 1.08x to 1.18x, v2 1.10x to 1.17x limit 20, PARALLEL 4: 1 s cap 1.05x, v2 1.05x limit 20, PARALLEL 16: 1 s cap 1.54x to 1.55x, v2 1.07x So the cap shows up only with many workers and a small limit, and it stays within your bound. > Regarding the WaitLatch() for HEAD, worth remembering it was tried before > (4753ef37e0e) and reverted in 720de00af49 because the millisecond timeout > broke fractional cost_delay. Chao's loop as posted sleeps the whole milliseconds in WaitLatch() and the rest in pg_usleep(). Fractional delays matched v2: 0.96x to 0.97x for both at 2.5ms, 0.92x to 0.95x for both at 0.5ms. It also held the budget in the runs above: 1.02x to 1.04x at limit 200, 1.03x at limit 20. One thing is independent of the cap: at 100ms, v2 itself runs at 1.10x to 1.19x. On Unix pg_usleep() is nanosleep() (src/port/pgsleep.c:50), which returns early when a signal handler runs, and v2 does not go back to sleep. The leader got 25 SIGUSR1 at PARALLEL 4 and 47 at PARALLEL 16 during one VACUUM (VERBOSE), one per worker message (src/backend/libpq/pqmq.c:187), e.g. the index progress update (vacuumparallel.c:1217). A diagnostic build of v2 that only resumes the interrupted sleep gives 1.02x to 1.03x. At 10ms with a larger table v2 measured 1.00x, in line with your runs. On interrupts, pg_cancel_backend() took 5.0 to 16.1 ms with v2 and 8.5 to 14.9 ms with the loop (N = 10). If the cancel lands right before the sleep (forced with gdb), v2 sleeps the full 6.8 s at PARALLEL 16, the 1 s cap about 1 s, and the loop 0.4 to 3.2 ms. Setup and all values are in the attached measurements.txt. One Linux machine with other jobs running; the ratios repeat within 0.08 across rounds. Windows not measured. regards, Manu
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Fri, Oct 23, 2026 at 2:50 PM surya poondla wrote: > > I ran v2 on master for 1M rows, 8 indexes, 10% deleted, cost delay 10ms / limit 1000, so a budget of about 100k units/s. Each run > did roughly the same work (~650k units, estimated from VERBOSE buffer counts): > > PARALLEL master v2 > 0 82k/s 7.9s 82k/s 8.0s > 2 152k/s 4.3s 90k/s 7.2s > 8 (7) 243k/s 2.7s 97k/s 6.7s > > So master runs at up to ~2.4x the budget, and v2 stays under it, including with more than 4 processes. The vacuum and vacuum_parallel > regression output matches expected. Thanks for reviewing and benchmarking. > Regarding the 1s cap, I think the 800 in your example is where the 6.4s > cap kicks in, not the 1s one. With delay 100ms, limit 200 and 16 processes, the 1s cap applies once the local balance passes 125, > which can probably happen just from uneven work across processes, no skipped delay points needed. It's bounded though, a capped process > gets at most roughly 1.6-1.8x its share there, and only once nworkers * cost_delay gets near 1s. So I'm fine with the 1s cap, but > I'd mention that in the comment instead of saying the budget always holds. The 4x ratio I had in mind is the threshold for the cost_delay * 4 * nworkers cap (and for the non-parallel cost_delay * 4), not for the 1s cap. Either way, overrunning the budget once a cap kicks in seems fine to me, for a good reason that's already mentioned in the docs: https://www.postgresql.org/docs/devel/runtime-config-vacuum.html#GUC-VACUUM-COST-LIMIT. I would just cap it at 1s like this: - /* Compute sleep time based on the local cost balance */ - msec = vacuum_cost_delay * VacuumCostBalanceLocal / vacuum_cost_limit; + /* Compute sleep time based on the local cost balance and nworkers */ + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / + vacuum_cost_limit; + + /* + * Avoid uselessly long delays, as in a non-parallel vacuum. Also + * never sleep more than 1s at once, as sleeping any longer in + * pg_usleep() keeps the process from responding to latches and + * signals promptly. + */ + msec = Min(msec, Min(vacuum_cost_delay * 4 * nworkers, 1000)); > Regarding the WaitLatch() for HEAD, worth remembering it was tried before > (4753ef37e0e) and reverted in 720de00af49 because the millisecond timeout broke fractional cost_delay. Yes, that's a nice find. Any sub-millisecond delay is lost with WaitLatch(). > One thing I'm less sure about is back-patching to 14-18. Plain manual VACUUM already goes parallel when there are enough indexes, so anyone > with a cost delay set could see it get noticeably slower after a minor update (with the default 2 workers, 4.3s -> 7.2s above). > Fixing it in 19 and master seems safer to me, but I'm happy to go with the consensus. You are right, with this fix, anyone using the vacuum comamnd with the parallel degree could get slower without realizing it, and they would need to raise the cost limit and reduce the cost delay to give enough budget for everyone. I tend to agree with your point on not back-patching to <= PG18. As mentioned upthread [1], the fact that it hasn't been reported so far means it may have gone unnoticed, or everyone wanted vacuum to run faster at the cost of foreground path performance. Maybe a doc adjustment for <= PG18 is fine, explaining that the budget can be overrun with parallel degree, so it won't be a surprise when someone hits this in future, and we ship the fix to PG19+. This also works for me. Another option for PG19+ is to leave the vacuum command as-is and apply the fix only to parallel autovacuum, which isn't expected to get in the way of the foreground path. Thoughts? [1] https://postgr.es/m/CALj2ACXsDZR5kpsbOTHHVNNfJuN%2B9_x%3D8XzKh2Yc-0yTCwtu7g%40mail.gmail.com -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Tue, Oct 27, 2026 at 3:18 PM Bharath Rupireddy wrote: > > Hi, > > On Fri, Oct 23, 2026 at 2:50 PM surya poondla wrote: > > > > I ran v2 on master for 1M rows, 8 indexes, 10% deleted, cost delay 10ms / limit 1000, so a budget of about 100k units/s. Each run > > did roughly the same work (~650k units, estimated from VERBOSE buffer counts): > > > > PARALLEL master v2 > > 0 82k/s 7.9s 82k/s 8.0s > > 2 152k/s 4.3s 90k/s 7.2s > > 8 (7) 243k/s 2.7s 97k/s 6.7s > > > > So master runs at up to ~2.4x the budget, and v2 stays under it, including with more than 4 processes. The vacuum and vacuum_parallel > > regression output matches expected. > > Thanks for reviewing and benchmarking. > > > Regarding the 1s cap, I think the 800 in your example is where the 6.4s > > cap kicks in, not the 1s one. With delay 100ms, limit 200 and 16 processes, the 1s cap applies once the local balance passes 125, > > which can probably happen just from uneven work across processes, no skipped delay points needed. It's bounded though, a capped process > > gets at most roughly 1.6-1.8x its share there, and only once nworkers * cost_delay gets near 1s. So I'm fine with the 1s cap, but > > I'd mention that in the comment instead of saying the budget always holds. > > The 4x ratio I had in mind is the threshold for the cost_delay * 4 * > nworkers cap (and for the non-parallel cost_delay * 4), not for the 1s > cap. Either way, overrunning the budget once a cap kicks in seems fine > to me, for a good reason that's already mentioned in the docs: > https://www.postgresql.org/docs/devel/runtime-config-vacuum.html#GUC-VACUUM-COST-LIMIT. > > I would just cap it at 1s like this: > > - /* Compute sleep time based on the local cost balance */ > - msec = vacuum_cost_delay * VacuumCostBalanceLocal / vacuum_cost_limit; > + /* Compute sleep time based on the local cost balance and nworkers */ > + msec = vacuum_cost_delay * VacuumCostBalanceLocal * nworkers / > + vacuum_cost_limit; > + > + /* > + * Avoid uselessly long delays, as in a non-parallel vacuum. Also > + * never sleep more than 1s at once, as sleeping any longer in > + * pg_usleep() keeps the process from responding to latches and > + * signals promptly. > + */ > + msec = Min(msec, Min(vacuum_cost_delay * 4 * nworkers, 1000)); Sound reasonable change. > > One thing I'm less sure about is back-patching to 14-18. Plain manual VACUUM already goes parallel when there are enough indexes, so anyone > > with a cost delay set could see it get noticeably slower after a minor update (with the default 2 workers, 4.3s -> 7.2s above). > > Fixing it in 19 and master seems safer to me, but I'm happy to go with the consensus. > > You are right, with this fix, anyone using the vacuum comamnd with the > parallel degree could get slower without realizing it, and they would > need to raise the cost limit and reduce the cost delay to give enough > budget for everyone. > > I tend to agree with your point on not back-patching to <= PG18. As > mentioned upthread [1], the fact that it hasn't been reported so far > means it may have gone unnoticed, or everyone wanted vacuum to run > faster at the cost of foreground path performance. > > Maybe a doc adjustment for <= PG18 is fine, explaining that the budget > can be overrun with parallel degree, so it won't be a surprise when > someone hits this in future, and we ship the fix to PG19+. This also > works for me. > > Another option for PG19+ is to leave the vacuum command as-is and > apply the fix only to parallel autovacuum, which isn't expected to get > in the way of the foreground path. > > Thoughts? I'd like to withdraw my previous proposal to backpatch it to all supported versions. We haven't received any complaints about this behavior so far, and with the fix VACUUM could get slower after a minor update. So I agree to push it to v19 and master, and I think we should mention this behavior change in the v19 release notes. Also, I think it'd be better to update the paragraph in maintenance.sgml that says parallel workers use the same cost delay parameters as the leader, to mention that they share the cost limit. I'm not sure we need to add a note for v18 or older, as the current behavior of the VACUUM command doesn't violate the documentation. As for applying the fix only to parallel autovacuum, I'm not sure it's a good idea. The cost-based vacuum delay would work differently between VACUUM and autovacuum, and the VACUUM command would keep overrunning the budget in future versions. Regards -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
Hi, On Wed, Oct 28, 2026 at 1:43 PM Masahiko Sawada wrote: > > > I would just cap it at 1s like this: > > > + msec = Min(msec, Min(vacuum_cost_delay * 4 * nworkers, 1000)); > > Sound reasonable change. Thanks. Done that in the attached v3 patch. > > I tend to agree with your point on not back-patching to <= PG18. As > > mentioned upthread [1], the fact that it hasn't been reported so far > > means it may have gone unnoticed, or everyone wanted vacuum to run > > faster at the cost of foreground path performance. > > I'd like to withdraw my previous proposal to backpatch it to all > supported versions. We haven't received any complaints about this > behavior so far, and with the fix VACUUM could get slower after a > minor update. So I agree to push it to v19 and master, and I think we > should mention this behavior change in the v19 release notes. Agreed. > Also, I think it'd be better to update the paragraph in maintenance.sgml > that says parallel workers use the same cost delay parameters as the > leader, to mention that they share the cost limit. Done. Please find the attached v3 patch. -- Bharath Rupireddy Amazon Web Services: https://aws.amazon.com
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Bharath,
Thanks for v3. The doc changes read well, and the maintenance.sgml wording makes the shared limit clear.
v3 looks good to me.
Thanks,
Surya Poondla
Thanks for v3. The doc changes read well, and the maintenance.sgml wording makes the shared limit clear.
v3 looks good to me.
Thanks,
Surya Poondla
Re: Parallel vacuum: workers together with leader could overrun cost budget
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Thu, Oct 29, 2026 at 1:55 PM surya poondla wrote: > > Hi Bharath, > > Thanks for v3. The doc changes read well, and the maintenance.sgml wording makes the shared limit clear. > > v3 looks good to me. The patch looks good to me too. So I've pushed the patch. -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com