mirror of
https://github.com/postgres/postgres.git
synced 2025-11-22 12:22:45 +03:00
In REASSIGN OWNED of a database, lock the tuple as mandated.
Commit aac2c9b4fd mandated such locking
and attempted to fulfill that mandate, but it missed REASSIGN OWNED.
Hence, it remained possible to lose VACUUM's inplace update of
datfrozenxid if a REASSIGN OWNED processed that database at the same
time. This didn't affect the other inplace-updated catalog, pg_class.
For pg_class, REASSIGN OWNED calls ATExecChangeOwner() instead of the
generic AlterObjectOwner_internal(), and ATExecChangeOwner() fulfills
the locking mandate.
Like in GRANT, implement this by following the locking protocol for any
catalog subject to the generic AlterObjectOwner_internal(). It would
suffice to do this for IsInplaceUpdateOid() catalogs only. Back-patch
to v13 (all supported versions).
Kirill Reshke. Reported by Alexander Kukushkin.
Discussion: https://postgr.es/m/CAFh8B=mpKjAy4Cuun-HP-f_vRzh2HSvYFG3rhVfYbfEBUhBAGg@mail.gmail.com
This commit is contained in:
@@ -2784,6 +2784,22 @@ get_object_property_data(Oid class_id)
|
||||
*/
|
||||
HeapTuple
|
||||
get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
|
||||
{
|
||||
return
|
||||
get_catalog_object_by_oid_extended(catalog, oidcol, objectId, false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Same as get_catalog_object_by_oid(), but with an additional "locktup"
|
||||
* argument controlling whether to acquire a LOCKTAG_TUPLE at mode
|
||||
* InplaceUpdateTupleLock. See README.tuplock section "Locking to write
|
||||
* inplace-updated tables".
|
||||
*/
|
||||
HeapTuple
|
||||
get_catalog_object_by_oid_extended(Relation catalog,
|
||||
AttrNumber oidcol,
|
||||
Oid objectId,
|
||||
bool locktup)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
Oid classId = RelationGetRelid(catalog);
|
||||
@@ -2791,7 +2807,12 @@ get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
|
||||
|
||||
if (oidCacheId > 0)
|
||||
{
|
||||
tuple = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objectId));
|
||||
if (locktup)
|
||||
tuple = SearchSysCacheLockedCopy1(oidCacheId,
|
||||
ObjectIdGetDatum(objectId));
|
||||
else
|
||||
tuple = SearchSysCacheCopy1(oidCacheId,
|
||||
ObjectIdGetDatum(objectId));
|
||||
if (!HeapTupleIsValid(tuple)) /* should not happen */
|
||||
return NULL;
|
||||
}
|
||||
@@ -2816,6 +2837,10 @@ get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
|
||||
systable_endscan(scan);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (locktup)
|
||||
LockTuple(catalog, &tuple->t_self, InplaceUpdateTupleLock);
|
||||
|
||||
tuple = heap_copytuple(tuple);
|
||||
|
||||
systable_endscan(scan);
|
||||
|
||||
@@ -59,6 +59,7 @@
|
||||
#include "miscadmin.h"
|
||||
#include "replication/logicalworker.h"
|
||||
#include "rewrite/rewriteDefine.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "utils/acl.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/lsyscache.h"
|
||||
@@ -924,7 +925,9 @@ AlterObjectOwner_internal(Oid classId, Oid objectId, Oid new_ownerId)
|
||||
|
||||
rel = table_open(catalogId, RowExclusiveLock);
|
||||
|
||||
oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
|
||||
/* Search tuple and lock it. */
|
||||
oldtup =
|
||||
get_catalog_object_by_oid_extended(rel, Anum_oid, objectId, true);
|
||||
if (oldtup == NULL)
|
||||
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
|
||||
objectId, RelationGetRelationName(rel));
|
||||
@@ -1024,6 +1027,8 @@ AlterObjectOwner_internal(Oid classId, Oid objectId, Oid new_ownerId)
|
||||
/* Perform actual update */
|
||||
CatalogTupleUpdate(rel, &newtup->t_self, newtup);
|
||||
|
||||
UnlockTuple(rel, &oldtup->t_self, InplaceUpdateTupleLock);
|
||||
|
||||
/* Update owner dependency reference */
|
||||
changeDependencyOnOwner(classId, objectId, new_ownerId);
|
||||
|
||||
@@ -1032,6 +1037,8 @@ AlterObjectOwner_internal(Oid classId, Oid objectId, Oid new_ownerId)
|
||||
pfree(nulls);
|
||||
pfree(replaces);
|
||||
}
|
||||
else
|
||||
UnlockTuple(rel, &oldtup->t_self, InplaceUpdateTupleLock);
|
||||
|
||||
/* Note the post-alter hook gets classId not catalogId */
|
||||
InvokeObjectPostAlterHook(classId, objectId, 0);
|
||||
|
||||
Reference in New Issue
Block a user