Stale hash entries when reusing an auxiliary resource owner
Hi,
While hacking on the code, I noticed a problem with auxiliary process
resource owners. Sorting and bulk release leave stale entries in the
owner’s hash table, but ReleaseAuxProcessResources() resets the flags to
allow reuse without clearing them.
On reuse, stale entries can be mistaken for resources acquired in the
current cycle and released again. They can also leave the table with no
empty slots, causing insertion to loop indefinitely while searching for
one.
The attached patch clears the entire hash table after resource release,
before allowing the auxiliary owner to be reused. This also removes
stale copies left beyond the sorted prefix.
I think this was introduced by b8bff07daa8. Any comments on this fix?
---
Regards,
Jingtang
On Nov 2, 2026, at 18:13, Jingtang Zhang <mrdrivingduck@gmail.com> wrote:
Hi,
While hacking on the code, I noticed a problem with auxiliary process
resource owners. Sorting and bulk release leave stale entries in the
owner’s hash table, but ReleaseAuxProcessResources() resets the flags to
allow reuse without clearing them.On reuse, stale entries can be mistaken for resources acquired in the
current cycle and released again. They can also leave the table with no
empty slots, causing insertion to loop indefinitely while searching for
one.The attached patch clears the entire hash table after resource release,
before allowing the auxiliary owner to be reused. This also removes
stale copies left beyond the sorted prefix.I think this was introduced by b8bff07daa8. Any comments on this fix?
---
Regards,
Jingtang
<v1-aux-resource-owner-reuse.patch>
Thanks for the patch.
The fix looks good to me. However, when I tried running the test with the fix reverted, the test hung indefinitely, which doesn't seem desirable. Ideally, the test should fail rather than hang when the fix is absent. It’s late here, I haven’t spent time debugging why the test hang.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Hi,
The fix looks good to me. However, when I tried running the test with the fix reverted, the test hung indefinitely, which doesn't seem desirable. Ideally, the test should fail rather than hang when the fix is absent. It’s late here, I haven’t spent time debugging why the test hang.
Thanks for testing. The hang is an expected consequence of the bug. The
resource counts are reset after release, but the hash slots remain
occupied. On reuse, the low counts can prevent a resize even when there
are no free slots, so ResourceOwnerAddToHash() keeps probing
indefinitely.
I have adjusted the test cases in v2 so that they still expose the bug
without hanging when the fix is absent.
I verified that the revised regression passes with the fix and fails
promptly with duplicate-release errors when only the fix is reverted.
Attached is v2.
---
Regards,
Jingtang
Hi,
On 2026-11-03 00:43:00 +0800, Jingtang Zhang wrote:
From 79d65b79449cc85b5588eede60187b5616893361 Mon Sep 17 00:00:00 2001
From: Jingtang Zhang <mrdrivingduck@gmail.com>
Date: Tue, 3 Nov 2026 00:30:29 +0800
Subject: [PATCH v2] Clear auxiliary resource owner hash before reuseBulk release sorts and compacts the resource hash table but only resets
its item count when releasing resources. Entries in the sorted prefix
remain populated, and compaction can leave extra copies outside that
prefix.An auxiliary process reuses its owner after releasing its resources.
Clear the whole hash table before allowing reuse so that subsequent
resource acquisition cannot treat stale entries as live resources or
release them twice.Backpatch-through: 17
---
src/backend/utils/resowner/resowner.c | 9 ++
.../test_resowner/expected/test_resowner.out | 27 ++++++
.../test_resowner/sql/test_resowner.sql | 8 ++
.../test_resowner/test_resowner--1.0.sql | 4 +
.../test_resowner/test_resowner_basic.c | 87 +++++++++++++++++++
5 files changed, 135 insertions(+)diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index ac413d54837..149b5273667 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -1038,6 +1038,15 @@ ReleaseAuxProcessResources(bool isCommit) ResourceOwnerRelease(AuxProcessResourceOwner, RESOURCE_RELEASE_AFTER_LOCKS, isCommit, true); + + /* + * Sorting can leave copies of entries beyond the sorted prefix, and bulk + * release only resets the counts. Clear the entire table before reuse. + */ + if (AuxProcessResourceOwner->hash != NULL) + memset(AuxProcessResourceOwner->hash, 0, + AuxProcessResourceOwner->capacity * sizeof(ResourceElem)); + /* allow it to be reused */ AuxProcessResourceOwner->releasing = false; AuxProcessResourceOwner->sorted = false;
I don't think fixing this locally in ReleaseAuxProcessResources() can be the
right fix. It's just continuing an existing pattern, admittedly. But:
It seems quite wrong that ReleaseAuxProcessResources() does stuff that callers
outside of resowner.c can't do. What if there's a need for a long-lived,
reusable, resowner outside of a context where using AuxProcessResourceOwner is
right? Seems like we should just have a helper that makes a resowner reusable
after some assertions?
As for the concrete bug at hand - why don't we fix this up in
ResourceOwnerSort()? ReleaseAuxProcessResources() shouldn't be that common,
but always memset()ing the full size of the hash can't be great? And even just
for debugging in non-auxprocess context, it doesn't seem awesome to have a
hashtable with obsoleted entries still visible?
The fast-path is the !hash case, I don't think any realistic workload would
notice the overhead of zeroing out hash table entries during the compaction in
ResourceOwnerSort()? I don't know if we'd need to do anything else?
Greetings,
Andres Freund
Hi Andres,
I don't think fixing this locally in ReleaseAuxProcessResources() can be the
right fix. It's just continuing an existing pattern, admittedly. But:
My original patch aimed to keep the fix local, since the auxiliary
resource owner is currently the only owner reused after bulk release.
That was why I avoided changes to the generic ResourceOwner code.
It seems quite wrong that ReleaseAuxProcessResources() does stuff that callers
outside of resowner.c can't do. What if there's a need for a long-lived,
reusable, resowner outside of a context where using AuxProcessResourceOwner is
right? Seems like we should just have a helper that makes a resowner reusable
after some assertions?
Since we have decided to change the API, I think we could add a public
ResourceOwnerReset() interface, by analogy with MemoryContext. After
completing all three release phases, callers could either delete the
owner or reset it for reuse:
Create -> Use -> Release (all three phases) -> Delete
^ |
| v
+----------- Reset
For the implementation, I see two options. One is to clear stale hash
entries during sorting, as you suggested, and clear each entry during
bulk release. Reset would then only restore the internal state. This
also adds some work for owners that are deleted without being reset.
The other is to clear the hash table in Reset, so only owners being
reset for reuse pay that cost.
As for the concrete bug at hand - why don't we fix this up in
ResourceOwnerSort()? ReleaseAuxProcessResources() shouldn't be that common,
but always memset()ing the full size of the hash can't be great? And even just
for debugging in non-auxprocess context, it doesn't seem awesome to have a
hashtable with obsoleted entries still visible?The fast-path is the !hash case, I don't think any realistic workload would
notice the overhead of zeroing out hash table entries during the compaction in
ResourceOwnerSort()? I don't know if we'd need to do anything else?
For now, the latter seems more reasonable to me, since Reset is
specifically responsible for preparing a released owner for reuse.
Clearing the retained hash table fits that responsibility. Also, Reset
would currently only be used for the auxiliary owner, whose cleanup
seems less performance-critical than the general ResourceOwner release
path.
I'm not sure which is better overall, so I have prepared patches
for both approaches.
---
Regards,
Jingtang
Hi,
The fast-path is the !hash case, I don't think any realistic workload would
notice the overhead of zeroing out hash table entries during the compaction in
ResourceOwnerSort()? I don't know if we'd need to do anything else?For now, the latter seems more reasonable to me, since Reset is
specifically responsible for preparing a released owner for reuse.
Clearing the retained hash table fits that responsibility. Also, Reset
would currently only be used for the auxiliary owner, whose cleanup
seems less performance-critical than the general ResourceOwner release
path.I'm not sure which is better overall, so I have prepared patches
for both approaches.
My two cents:
It will be interesting to benchmark the overhead of clearing these entries
during ResourceOwnerSort and ResourceOwnerRelease.
Assuming no significant performance impact, I think it makes more
sense to reset the entries where they are being compacted out or
released instead of introducing a new API
function for the same. It also guarantees that entries are cleared
even if an error occurs before they are reset.
I am planning to review the patches in detail.
FWIW, unrelated but improving the same API, [1]. /messages/by-id/CAH2L28vVo-Q3Jf+P4huRKxjnbF+2t7OdDh3ZGMxL88hiWezd=A@mail.gmail.com makes it easier for
non-transaction resource
owners like the AuxProcessResourceOwner to release resources by
allowing them to release all the resources at
once instead of calling ResourceOwnerRelease three times for each phase.
Views are welcome.
[1]: . /messages/by-id/CAH2L28vVo-Q3Jf+P4huRKxjnbF+2t7OdDh3ZGMxL88hiWezd=A@mail.gmail.com
Thank you,
Rahila Syed
Hi,
Thanks for the comments.
It will be interesting to benchmark the overhead of clearing these entries
during ResourceOwnerSort and ResourceOwnerRelease.Assuming no significant performance impact, I think it makes more
sense to reset the entries where they are being compacted out or
released instead of introducing a new API
function for the same. It also guarantees that entries are cleared
even if an error occurs before they are reset.
I am planning to review the patches in detail.
I used GCC 10.2.1 with CFLAGS=-O2,
--disable-debug --disable-cassert.
All microbenchmark cases below use one resource kind. Columns are:
Timing: Release = all three release phases;
Reset = Reset alone after release has completed.
Live/peak: resource count before release / peak registered count.
For Reset, the peak is established before timing.
Baseline: unpatched.
A: clear hash entries during sorting and bulk release.
B: clear the retained hash table in Reset.
Values are median microseconds per owner. The baseline has no Reset
API, so those measurements are marked with a dash.
| Timing | Live/peak | Baseline | A | B |
+----------+-------------+-----------+-----------+-----------+
| Release | 0/0 | 0.026 | 0.029 | 0.027 |
| Release | 16/16 | 0.108 | 0.116 | 0.107 |
| Release | 32/32 | 0.202 | 0.207 | 0.198 |
| Release | 33/33 | 0.282 | 0.277 | 0.268 |
| Release | 64/64 | 0.463 | 0.466 | 0.456 |
| Release | 128/128 | 0.954 | 0.977 | 0.939 |
| Release | 1024/1024 | 11.617 | 11.341 | 11.825 |
| Release | 16384/16384 | 239.506 | 256.352 | 237.873 |
| Release | 0/1024 | 0.040 | 0.044 | 0.038 |
| Release | 16/1024 | 2.051 | 1.790 | 1.854 |
For Release, the smaller cases tested show small absolute differences.
The difference is more apparent with 16,384 resources.
| Timing | Live/peak | Baseline | A | B |
+----------+-------------+-----------+-----------+-----------+
| Reset | 16/16 | - | 0.005 | 0.006 |
| Reset | 64/64 | - | 0.005 | 0.054 |
| Reset | 1024/1024 | - | 0.008 | 0.570 |
| Reset | 16/1024 | - | 0.005 | 0.706 |
| Reset | 16/16384 | - | 0.013 | 14.698 |
| Reset | 16/65536 | - | 0.023 | 66.911 |
For Reset, B's cost grows with retained hash capacity. An earlier high
peak can therefore make later resets expensive even with few current
resources. The large historical peaks are deliberate stress cases.
To check whether A's extra release work affects SQL throughput,
I also ran pgbench -S -M prepared -c 1 -j 1 at scale 1. TPS values
below are medians; differences compare those medians with baseline.
+----------+------------+------------+--------+
| Run | Baseline | A | A diff |
+----------+------------+------------+--------+
| First | 43736.94 | 43035.63 | -1.60% |
| Repeat | 43340.50 | 43574.31 | +0.54% |
+----------+------------+------------+--------+
The first run used seven trials of 12 seconds with each build's
client. The repeat used five trials of eight seconds with one client
binary for all servers. The TPS differences change direction between
runs, and I did not observe a consistent throughput regression in
this workload.
Given these results, I prefer clearing during sorting and bulk
release. It avoids clearing the full retained table in Reset, and
these pgbench runs did not show a consistent throughput regression.
Attached with v4.
Regards,
Jingtang