Re: ResourceOwner refactoring
Heikki Linnakangas <hlinnaka@iki.fi>
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Andres Freund <andres@anarazel.de>
Cc: Julien Rouhaud <rjuju123@gmail.com>,
Aleksander Alekseev <aleksander@timescale.com>,
"pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>,
Robert Haas <robertmhaas@gmail.com>
Date: 2023-02-21T11:40:57Z
Lists: pgsql-hackers
Attachments
- v13-0001-Move-a-few-ResourceOwnerEnlarge-calls-for-safety.patch (text/x-patch) patch v13-0001
- v13-0002-Make-resowners-more-easily-extensible.patch (text/x-patch) patch v13-0002
- v13-0003-Release-resources-in-priority-order.patch (text/x-patch) patch v13-0003
- v13-0004-Use-a-faster-hash-function-in-resource-owners.patch (text/x-patch) patch v13-0004
- v13-0005-Change-pgcrypto-to-use-the-new-ResourceOwner-mec.patch (text/x-patch) patch v13-0005
On 01/11/2022 17:42, Andres Freund wrote: > On 2022-11-01 12:39:39 +0200, Heikki Linnakangas wrote: >> If we do need to worry about release order, perhaps add a "priority" or >> "phase" to each resource kind, and release them in priority order. We >> already have before- and after-locks as phases, but we could generalize >> that. >> >> However, I feel that trying to enforce a particular order moves the >> goalposts. If we need that, let's add it as a separate patch later. > > Like Robert, I think that the patch is moving the goalpost... Ok, I added a release-priority mechanism to this. New patchset attached. I added it as a separate patch on top of the previous patches, as v13-0003-Release-resources-in-priority-order.patch, to make it easier to see what changed after the previous patchset version. But it should be squashed with patch 0002 before committing. In this implementation, when you call ResourceOwnerRelease(), the array/hash table is sorted by phase and priority, and the callbacks are called in that order. To make that feasible, I had to add one limitation: After you call ResourceOwnerRelease(RESOURCE_RELEASE_BEFORE_LOCKS), you cannot continue using the resource owner. You now get an error if you try to call ResourceOwnerRemember() or ResourceOwnerForget() after ResourceOwnerRelease(). Previously, it was allowed, but if you remembered any more before-locks resources after calling ResourceOwnerRelease(RESOURCE_RELEASE_BEFORE_LOCKS), you had to be careful to release them manually, or you hit an assertion failure when deleting the ResourceOwner. We did that sometimes with relcache references in AbortSubTransaction(), in AtEOSubXact_Inval(). Regression tests caught that case. I worked around that in AbortSubTransaction() by switching to the parent subxact's resource owner between the phases. Other end-of-transaction routines also perform some tasks between the phases, but AFAICS they don't try to remember any extra resources. It's a bit hard to tell, though, there's a lot going on in AtEOXact_Inval(). A more general fix would be to have special ResourceOwner for use at end-of-transaction, similar to the TransactionAbortContext memory context. But in general, end-of-transaction activities should be kept simple, especially between the release phases, so I feel that having to remember extra resources there is a bad code smell and we shouldn't encourage that. Perhaps there is a better fix or workaround for the known case in AbortSubTransaction(), too, that would avoid having to remember any resources there. I added a test module in src/test/modules/test_resowner to test those cases. Also, I changed the ReleaseResource callback's contract so that the callback no longer needs to call ResourceOwnerForget. That required some changes in bufmgr.c and some other files, to avoid calling ResourceOwnerForget when the resource is released by the callback. There are no changes to the performance-critical Remember/Forget codepaths, the performance is the same as before. - Heikki
Commits
-
Make RelationFlushRelation() work without ResourceOwner during abort
- e6cd85772647 17.0 landed
-
Fix bug in bulk extending temp relation after failure
- d212957254de 17.0 landed
-
Add missing PGDLLIMPORT markings
- c6b86eaa55ff 17.0 landed
-
Add test_dsa module.
- 325f54033e59 17.0 landed
-
Clear CurrentResourceOwner earlier in CommitTransaction.
- c21e6e2fd48c 17.0 landed
-
Fix dsa.c with different resource owners.
- a8b330ffb6f7 17.0 landed
-
Fix bug in the new ResourceOwner implementation.
- 8f4a1ab471e6 17.0 landed
-
Change pgcrypto to use the new ResourceOwner mechanism.
- cd694f60dc97 17.0 landed
-
Use a faster hash function in resource owners.
- 954e43564d99 17.0 landed
-
Make ResourceOwners more easily extensible.
- b8bff07daa85 17.0 landed
-
Move a few ResourceOwnerEnlarge() calls for safety and clarity.
- b70c2143bbbe 17.0 landed