Schema name of function

Started by John Hansenalmost 21 years ago6 messages
#1John Hansen
john@geeknet.com.au

Just got reminded...

Is there a way for a C function to determine the name of the schema in which is was created?

... John

#2Michael Fuhr
mike@fuhr.org
In reply to: John Hansen (#1)
Re: Schema name of function

On Mon, Feb 14, 2005 at 07:32:15PM +1100, John Hansen wrote:

Is there a way for a C function to determine the name of the
schema in which is was created?

Dunno if there's anything as simple as whats_my_schema(), but
fcinfo->flinfo->fn_oid should contain the function's oid. If nobody
mentions an easier way, you could use SPI to query pg_proc and
pg_namespace.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Fuhr (#2)
Re: Schema name of function

Michael Fuhr <mike@fuhr.org> writes:

On Mon, Feb 14, 2005 at 07:32:15PM +1100, John Hansen wrote:

Is there a way for a C function to determine the name of the
schema in which is was created?

Dunno if there's anything as simple as whats_my_schema(), but
fcinfo->flinfo->fn_oid should contain the function's oid. If nobody
mentions an easier way, you could use SPI to query pg_proc and
pg_namespace.

In C, it'd be a lot easier (and faster) to do a couple of SearchSysCache
calls than to use SPI to get those rows.

regards, tom lane

#4Michael Fuhr
mike@fuhr.org
In reply to: Tom Lane (#3)
Re: Schema name of function

On Mon, Feb 14, 2005 at 11:11:53AM -0500, Tom Lane wrote:

In C, it'd be a lot easier (and faster) to do a couple of SearchSysCache
calls than to use SPI to get those rows.

The following appears to work -- does it look right, aside from the
missing error checking?

tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
0, 0, 0);
nsoid = SysCacheGetAttr(PROCOID, tuple,
Anum_pg_proc_pronamespace, &isnull);
schemaname = get_namespace_name(nsoid);
ReleaseSysCache(tuple);

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Fuhr (#4)
Re: Schema name of function

Michael Fuhr <mike@fuhr.org> writes:

On Mon, Feb 14, 2005 at 11:11:53AM -0500, Tom Lane wrote:

In C, it'd be a lot easier (and faster) to do a couple of SearchSysCache
calls than to use SPI to get those rows.

The following appears to work -- does it look right, aside from the
missing error checking?

tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
0, 0, 0);
nsoid = SysCacheGetAttr(PROCOID, tuple,
Anum_pg_proc_pronamespace, &isnull);
schemaname = get_namespace_name(nsoid);
ReleaseSysCache(tuple);

That would work. Since pronamespace is one of the fixed non-nullable
columns of pg_proc, you don't really need to use SysCacheGetAttr: you
can just map the C struct onto the tuple and grab the field directly.

nsoid = ((Form_pg_proc) GETSTRUCT(tuple))->pronamespace;

utils/cache/lsyscache.c contains lots of examples of this sort of thing.
(get_namespace_name is one, in fact.)

regards, tom lane

#6John Hansen
john@geeknet.com.au
In reply to: Tom Lane (#5)
Re: Schema name of function

Beautiful, just what I was looking for.

Thnx,

John

Show quoted text

-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Tuesday, February 15, 2005 6:31 AM
To: Michael Fuhr
Cc: John Hansen; pgsql-hackers@postgresql.org
Subject: Re: [HACKERS] Schema name of function

Michael Fuhr <mike@fuhr.org> writes:

On Mon, Feb 14, 2005 at 11:11:53AM -0500, Tom Lane wrote:

In C, it'd be a lot easier (and faster) to do a couple of
SearchSysCache calls than to use SPI to get those rows.

The following appears to work -- does it look right, aside from the
missing error checking?

tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
0, 0, 0);
nsoid = SysCacheGetAttr(PROCOID, tuple,
Anum_pg_proc_pronamespace, &isnull);
schemaname = get_namespace_name(nsoid);
ReleaseSysCache(tuple);

That would work. Since pronamespace is one of the fixed
non-nullable columns of pg_proc, you don't really need to use
SysCacheGetAttr: you can just map the C struct onto the tuple
and grab the field directly.

nsoid = ((Form_pg_proc) GETSTRUCT(tuple))->pronamespace;

utils/cache/lsyscache.c contains lots of examples of this
sort of thing.
(get_namespace_name is one, in fact.)

regards, tom lane