[BUG] orphaned function

From: "Drouvot, Bertrand" <bdrouvot(at)amazon(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: [BUG] orphaned function
Date: 2020-11-30 21:07:29
Message-ID: 5a9daaae-5538-b209-6279-e903c3ea2157@amazon.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v1-001-orphaned_function.patch text/plain 1.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2020-11-30 21:15:32 Re: proposal: unescape_text function
Previous Message Andres Freund 2020-11-30 21:03:26 Re: Printing LSN made easy