Hi hackers,
here is a scenario that produces an orphaned function (means it does not belong to any namespace):
Session 1:
postgres=# create schema tobeorph;
CREATE SCHEMA
postgres=# BEGIN;
BEGIN
postgres=*# CREATE OR REPLACE FUNCTION tobeorph.bdttime() RETURNS TIMESTAMP AS $$
DECLARE outTS TIMESTAMP;
BEGIN perform pg_sleep(10); RETURN CURRENT_TIMESTAMP;
END;
$$ LANGUAGE plpgsql volatile;
CREATE FUNCTION
=> Don't commit
Session 2:
postgres=# drop schema tobeorph;
DROP SCHEMA
Session 1:
postgres=*# END;
COMMIT
Function is orphaned:
postgres=# \df *.bdttime List of functions Schema | Name | Result data type | Argument data types | Type
--------+---------+-----------------------------+---------------------+------ | bdttime | timestamp without time zone | | func
(1 row)
It appears that an AccessShareLock on the target namespace is not acquired when the function is being created (while, for example, it is acquired when creating a relation (through RangeVarGetAndCheckCreationNamespace())).
While CreateFunction() could lock the object the same way RangeVarGetAndCheckCreationNamespace() does, that would leave other objects that belong to namespaces unprotected. Locking the schema in QualifiedNameGetCreationNamespace() will protect those objects.
Please find attached a patch that adds a LockDatabaseObject() call in QualifiedNameGetCreationNamespace() to prevent such orphaned situations.
I will add this patch to the next commitfest. I look forward to your feedback.
Bertrand