From: Tom Lane <tgl@sss.pgh.pa.us>
To: Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Retroactively adding send and recv functions to a type?
Date: Wed, 21 Aug 2019 10:25:36 -0400
Message-ID: <30696.1566397536@sss.pgh.pa.us> (raw)
In-Reply-To: <CAA_SOw_nwn3KCz-rZEL530oQpakUvy_nPvUPHtr-rurYzr+Uyw@mail.gmail.com>
References: <CAA_SOw97LHiS+iqLkXKJ4Sqjc1bagKxQT11R=e=HS460sZ3hYw@mail.gmail.com>
<CAA_SOw954VvMGi8bt3P5C7FN37Jj=SRxnBjxjPcTZGG8KN_KkQ@mail.gmail.com>
<23029.1566235939@sss.pgh.pa.us>
<CAA_SOw8R8Gnx-7cOAum4TC5wkUjEo725k4JevjjnUcM8ghLgug@mail.gmail.com>
<5790.1566240394@sss.pgh.pa.us>
<CAA_SOw_nwn3KCz-rZEL530oQpakUvy_nPvUPHtr-rurYzr+Uyw@mail.gmail.com>
"Johann 'Myrkraverk' Oskarsson" <johann@myrkraverk.com> writes:
> The steps I took are
> create function sha1_send( sha1 ) returns bytea immutable
> language c strict as 'hashtypes', 'sha_send1';
> update pg_type set typsend = 'sha1_send'::regproc
> where typname = 'sha1';
> create function sha1_recv( internal ) returns sha1 immutable
> language c strict as 'hashtypes', 'sha_recv1';
> update pg_type set typreceive = 'sha1_recv'::regproc
> where typname = 'sha1';
Those updates don't look very safe: for instance, what if there's
another type named sha1 in some other schema? I'd do it like
-- create the functions
update pg_type set
typsend = 'sha1_send(sha1)'::regprocedure,
typreceive = 'sha1_recv(internal)'::regprocedure
where oid = 'sha1'::regtype;
This formulation only relies on your schema being frontmost in
the search path, which it should be during CREATE/ALTER EXTENSION.
> Then for completeness sake, I added two rows into pg_depend with
> insert into pg_depend ( classid, objid, objsubid, refclassid,
> refobjid, refobjsubid, deptype )
> values ( 'pg_type'::regclass::oid, 'sha1'::regtype::oid, 0,
> 'pg_proc'::regclass::oid, 'sha1_recv'::regproc::oid, 0, 'n' );
> insert into pg_depend ( classid, objid, objsubid, refclassid,
> refobjid, refobjsubid, deptype )
> values ( 'pg_type'::regclass::oid, 'sha1'::regtype::oid, 0,
> 'pg_proc'::regclass::oid, 'sha1_send'::regproc::oid, 0, 'n' );
You could skip the explicit casts to oid, and again I think use of
regprocedure would be safer than regproc. Seems fine otherwise.
> I did not dare to try before adding to pg_depend, but here's what
> happens when I try to drop function sha1_recv;
> ERROR: cannot drop function sha1_recv(internal) because other
> objects depend on it
> DETAIL: extension hashtypes depends on function sha1_recv(internal)
> column passwd of table pwned depends on type sha1
> function sha1_send(sha1) depends on type sha1
> Does this look correct?
It looks a bit odd, but I think that just indicates that you created the
two functions manually rather than inside an extension update script,
so they're not known to be part of the extension. You could experiment
with ALTER EXTENSION ADD to see if this output changes when they are
part of the extension. (But you don't need ALTER EXTENSION ADD when
you create them in an update script.)
regards, tom lane
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, johann@myrkraverk.com
Subject: Re: Retroactively adding send and recv functions to a type?
In-Reply-To: <30696.1566397536@sss.pgh.pa.us>
References: <CAA_SOw97LHiS+iqLkXKJ4Sqjc1bagKxQT11R=e=HS460sZ3hYw@mail.gmail.com> <CAA_SOw954VvMGi8bt3P5C7FN37Jj=SRxnBjxjPcTZGG8KN_KkQ@mail.gmail.com> <23029.1566235939@sss.pgh.pa.us> <CAA_SOw8R8Gnx-7cOAum4TC5wkUjEo725k4JevjjnUcM8ghLgug@mail.gmail.com> <5790.1566240394@sss.pgh.pa.us> <CAA_SOw_nwn3KCz-rZEL530oQpakUvy_nPvUPHtr-rurYzr+Uyw@mail.gmail.com> <30696.1566397536@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