mirror of
https://github.com/postgres/postgres.git
synced 2025-11-04 20:11:56 +03:00
Give SMgrRelation pointers a well-defined lifetime.
After calling smgropen(), it was not clear how long you could continue to use the result, because various code paths including cache invalidation could call smgrclose(), which freed the memory. Guarantee that the object won't be destroyed until the end of the current transaction, or in recovery, the commit/abort record that destroys the underlying storage. smgrclose() is now just an alias for smgrrelease(). It closes files and forgets all state except the rlocator, but keeps the SMgrRelation object valid. A new smgrdestroy() function is used by rare places that know there should be no other references to the SMgrRelation. The short version: * smgrclose() is now just an alias for smgrrelease(). It releases resources, but doesn't destroy until EOX * smgrdestroy() now frees memory, and should rarely be used. Existing code should be unaffected, but it is now possible for code that has an SMgrRelation object to use it repeatedly during a transaction as long as the storage hasn't been physically dropped. Such code would normally hold a lock on the relation. This also replaces the "ownership" mechanism of SMgrRelations with a pin counter. An SMgrRelation can now be "pinned", which prevents it from being destroyed at end of transaction. There can be multiple pins on the same SMgrRelation. In practice, the pin mechanism is only used by the relcache, so there cannot be more than one pin on the same SMgrRelation. Except with swap_relation_files XXX Author: Thomas Munro, Heikki Linnakangas Reviewed-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://www.postgresql.org/message-id/CA%2BhUKGJ8NTvqLHz6dqbQnt2c8XCki4r2QvXjBQcXpVwxTY_pvA@mail.gmail.com
This commit is contained in:
2
src/backend/utils/cache/inval.c
vendored
2
src/backend/utils/cache/inval.c
vendored
@@ -756,7 +756,7 @@ LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg)
|
||||
|
||||
rlocator.locator = msg->sm.rlocator;
|
||||
rlocator.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
|
||||
smgrcloserellocator(rlocator);
|
||||
smgrreleaserellocator(rlocator);
|
||||
}
|
||||
else if (msg->id == SHAREDINVALRELMAP_ID)
|
||||
{
|
||||
|
||||
21
src/backend/utils/cache/relcache.c
vendored
21
src/backend/utils/cache/relcache.c
vendored
@@ -3044,7 +3044,7 @@ RelationCacheInvalidate(bool debug_discard)
|
||||
* start to rebuild entries, since that may involve catalog fetches which
|
||||
* will re-open catalog files.
|
||||
*/
|
||||
smgrcloseall();
|
||||
smgrdestroyall();
|
||||
|
||||
/* Phase 2: rebuild the items found to need rebuild in phase 1 */
|
||||
foreach(l, rebuildFirstList)
|
||||
@@ -3066,25 +3066,6 @@ RelationCacheInvalidate(bool debug_discard)
|
||||
in_progress_list[i].invalidated = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* RelationCloseSmgrByOid - close a relcache entry's smgr link
|
||||
*
|
||||
* Needed in some cases where we are changing a relation's physical mapping.
|
||||
* The link will be automatically reopened on next use.
|
||||
*/
|
||||
void
|
||||
RelationCloseSmgrByOid(Oid relationId)
|
||||
{
|
||||
Relation relation;
|
||||
|
||||
RelationIdCacheLookup(relationId, relation);
|
||||
|
||||
if (!PointerIsValid(relation))
|
||||
return; /* not in cache, nothing to do */
|
||||
|
||||
RelationCloseSmgr(relation);
|
||||
}
|
||||
|
||||
static void
|
||||
RememberToFreeTupleDescAtEOX(TupleDesc td)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user