pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Bryn Llewellyn <bryn@yugabyte.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: alter function/procedure depends on extension
Date: Thu, 17 Feb 2022 20:17:25 -0500
Message-ID: <3781667.1645147045@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwZaqCq89BuHW8-BsuLjWQ=NGSuiXkOCaaxb9FizVw7UVg@mail.gmail.com>
References: <7C2D01B4-AD32-48DB-B5FA-6FFBC312ECB2@yugabyte.com>
	<4c6b69e2-52c6-359d-3ed9-04842f631ffe@aklaver.com>
	<CAKFQuwbq6y6aAcqR0_J_yYY3_8SxfaMMjTxMEDP_NpnBqN7nZA@mail.gmail.com>
	<CAKFQuwbmmLpTDkVPmo8KaQUnXGWKMp6Yo1gouA1gsYKcA5LeDQ@mail.gmail.com>
	<BEFD0F38-FBB4-45DB-94B4-84B7C948D1A0@yugabyte.com>
	<CAKFQuwZaqCq89BuHW8-BsuLjWQ=NGSuiXkOCaaxb9FizVw7UVg@mail.gmail.com>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> Maybe, but the behavior does exist but is bugged in its current state.
> Whether it is advisable for users to use this feature is immaterial, the
> bug needs to be fixed so that is works as documented for those that choose
> to do so.

There is no bug here; it's operating as designed and documented.
Maybe the docs need some refinement.

The way this is intended to work is that if you have a function that
depends on an extension -- say by using an extension-defined data
type -- then the *default* behavior is that you'd have to say CASCADE
in order to drop the extension.  The point of the ALTER DEPENDS ON
functionality is to declare that you don't want that behavior, but
would like the function to go away silently (ie, without CASCADE)
when the extension is dropped.  Thus:

test=# create extension cube;
CREATE EXTENSION
test=# create function myfunc(cube) returns int as 'select cube_dim($1)' language sql;
CREATE FUNCTION
test=# drop extension cube;
ERROR:  cannot drop extension cube because other objects depend on it
DETAIL:  function myfunc(cube) depends on type cube
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
test=# drop extension cube cascade;
NOTICE:  drop cascades to function myfunc(cube)
DROP EXTENSION

versus

test=# create extension cube;
CREATE EXTENSION
test=# create function myfunc(cube) returns int as 'select cube_dim($1)' language sql;
CREATE FUNCTION
test=# alter function myfunc(cube) depends on extension cube;
ALTER FUNCTION
test=# drop extension cube;
DROP EXTENSION
test=# \df myfunc
                       List of functions
 Schema | Name | Result data type | Argument data types | Type 
--------+------+------------------+---------------------+------
(0 rows)

That's acting as designed and we're not going to change it
for fear of breaking existing use-cases.  However, maybe
the use of "automatic" in the documentation isn't sufficiently
clear.

Now, if you have a function that has some internal,
not-visible-to-the-dependency-system extension dependency,
it's true that you don't get any protection against that:

test=# create extension cube;
CREATE EXTENSION
test=# create function myfunc(int) returns int as 'select cube_dim(cube($1,$1))' language sql;
CREATE FUNCTION
test=# drop extension cube;
DROP EXTENSION

However, the function wasn't dropped, so it's hard to argue that the
dependency system didn't do its job.  The function is still there,
even though it will fail at execution:

test=# select myfunc(42);
ERROR:  function cube(integer, integer) does not exist
LINE 1: select cube_dim(cube($1,$1))
                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
QUERY:  select cube_dim(cube($1,$1))
CONTEXT:  SQL function "myfunc" during inlining

You just need to re-install the extension, so it's not like you lost much:

test=# create extension cube;
CREATE EXTENSION
test=# select myfunc(42);
 myfunc 
--------
      1
(1 row)

In any case, ALTER DEPENDS ON EXTENSION is not meant to provide
protection against that scenario.  The best thing we have for
one-off internal dependencies right now is that new-style SQL
functions do expose them:

test=# drop function myfunc(int);
DROP FUNCTION
test=# create function myfunc(int) returns int begin atomic; select cube_dim(cube($1,$1)); end;
CREATE FUNCTION
test=# drop extension cube;
ERROR:  cannot drop extension cube because other objects depend on it
DETAIL:  function myfunc(integer) depends on function cube(double precision,double precision)
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Another possible approach is to group such functions into
their own extension, which you could make dependent on cube.

			regards, tom lane





view thread (18+ messages)  latest in thread

Message-ID: <3781667.1645147045@sss.pgh.pa.us>
Permalink:  ../3781667.1645147045@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3781667.1645147045@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, bryn@yugabyte.com, adrian.klaver@aklaver.com, alvherre@alvh.no-ip.org
  Subject: Re: alter function/procedure depends on extension
  In-Reply-To: <3781667.1645147045@sss.pgh.pa.us>
  References: <7C2D01B4-AD32-48DB-B5FA-6FFBC312ECB2@yugabyte.com> <4c6b69e2-52c6-359d-3ed9-04842f631ffe@aklaver.com> <CAKFQuwbq6y6aAcqR0_J_yYY3_8SxfaMMjTxMEDP_NpnBqN7nZA@mail.gmail.com> <CAKFQuwbmmLpTDkVPmo8KaQUnXGWKMp6Yo1gouA1gsYKcA5LeDQ@mail.gmail.com> <BEFD0F38-FBB4-45DB-94B4-84B7C948D1A0@yugabyte.com> <CAKFQuwZaqCq89BuHW8-BsuLjWQ=NGSuiXkOCaaxb9FizVw7UVg@mail.gmail.com> <3781667.1645147045@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox