src/backend/catalog/namespace.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 740570c566..a7f38baf6e 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -2989,6 +2989,11 @@ CheckSetNamespace(Oid oldNspOid, Oid nspOid) * * Note: calling this may result in a CommandCounterIncrement operation, * if we have to create or clean out the temp namespace. + * + * This function acquires AccessShareLock on the target + * namespace. Without this, the namespace could be dropped before our + * transaction commits, leaving behind objects with relnamespace pointing + * to a no-longer-existent namespace. */ Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p) @@ -3029,6 +3034,9 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p) errmsg("no schema has been selected to create in"))); } + /* Lock namespace. */ + LockDatabaseObject(NamespaceRelationId, namespaceId, 0, AccessShareLock); + return namespaceId; }