diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2024-06-06 18:56:28 +0300 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2024-06-06 18:56:28 +0300 |
commit | e6cd85772647099563da5ada53bd6ab7d096ce00 (patch) | |
tree | b0f9135f14ac914e5d2621b37ce052896f4e6413 /src/backend/utils/cache/relcache.c | |
parent | 505c008ca37c4f6f2fffcde370b5d8354c4d4dc3 (diff) | |
download | postgresql-e6cd85772647099563da5ada53bd6ab7d096ce00.tar.gz postgresql-e6cd85772647099563da5ada53bd6ab7d096ce00.zip |
Make RelationFlushRelation() work without ResourceOwner during abort
ReorderBufferImmediateInvalidation() executes invalidation messages in
an aborted transaction. However, RelationFlushRelation sometimes
required a valid resource owner, to temporarily increment the refcount
of the relache entry. Commit b8bff07daa worked around that in the main
subtransaction abort function, AbortSubTransaction(), but missed this
similar case in ReorderBufferImmediateInvalidation().
To fix, introduce a separate function to invalidate a relcache
entry. It does the same thing as RelationClearRelation(rebuild==true)
does when outside a transaction, but can be called without
incrementing the refcount.
Add regression test. Before this fix, it failed with:
ERROR: ResourceOwnerEnlarge called after release started
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://www.postgresql.org/message-id/e56be7d9-14b1-664d-0bfc-00ce9772721c@gmail.com
Diffstat (limited to 'src/backend/utils/cache/relcache.c')
-rw-r--r-- | src/backend/utils/cache/relcache.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index cc9b0c6524f..35dbb87ae3d 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -275,6 +275,7 @@ static HTAB *OpClassCache = NULL; static void RelationCloseCleanup(Relation relation); static void RelationDestroyRelation(Relation relation, bool remember_tupdesc); +static void RelationInvalidateRelation(Relation relation); static void RelationClearRelation(Relation relation, bool rebuild); static void RelationReloadIndexInfo(Relation relation); @@ -2513,6 +2514,31 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc) } /* + * RelationInvalidateRelation - mark a relation cache entry as invalid + * + * An entry that's marked as invalid will be reloaded on next access. + */ +static void +RelationInvalidateRelation(Relation relation) +{ + /* + * Make sure smgr and lower levels close the relation's files, if they + * weren't closed already. If the relation is not getting deleted, the + * next smgr access should reopen the files automatically. This ensures + * that the low-level file access state is updated after, say, a vacuum + * truncation. + */ + RelationCloseSmgr(relation); + + /* Free AM cached data, if any */ + if (relation->rd_amcache) + pfree(relation->rd_amcache); + relation->rd_amcache = NULL; + + relation->rd_isvalid = false; +} + +/* * RelationClearRelation * * Physically blow away a relation cache entry, or reset it and rebuild @@ -2846,14 +2872,28 @@ RelationFlushRelation(Relation relation) * New relcache entries are always rebuilt, not flushed; else we'd * forget the "new" status of the relation. Ditto for the * new-relfilenumber status. - * - * The rel could have zero refcnt here, so temporarily increment the - * refcnt to ensure it's safe to rebuild it. We can assume that the - * current transaction has some lock on the rel already. */ - RelationIncrementReferenceCount(relation); - RelationClearRelation(relation, true); - RelationDecrementReferenceCount(relation); + if (IsTransactionState() && relation->rd_droppedSubid == InvalidSubTransactionId) + { + /* + * The rel could have zero refcnt here, so temporarily increment + * the refcnt to ensure it's safe to rebuild it. We can assume + * that the current transaction has some lock on the rel already. + */ + RelationIncrementReferenceCount(relation); + RelationClearRelation(relation, true); + RelationDecrementReferenceCount(relation); + } + else + { + /* + * During abort processing, the current resource owner is not + * valid and we cannot hold a refcnt. Without a valid + * transaction, RelationClearRelation() would just mark the rel as + * invalid anyway, so we can do the same directly. + */ + RelationInvalidateRelation(relation); + } } else { |