Re: Function for retreiving datatype

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: Function for retreiving datatype
Дата
Msg-id 20050111005646.GA7422@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: Function for retreiving datatype  (Brendan Jurd <blakjak@blakjak.sytes.net>)
Ответы Re: Function for retreiving datatype  (Joe Conway <mail@joeconway.com>)
Список pgsql-general
On Tue, Jan 11, 2005 at 11:00:15AM +1100, Brendan Jurd wrote:

> Your coltype() function is exactly what I'm looking for.  I'd envisaged
> something that takes an anyelement argument and returns the type as
> text, but returning the OID is even better.
>
> Can you please provide the source for the function?

Here's the C code:

#include "postgres.h"
#include "fmgr.h"

Datum coltype(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(coltype);

Datum
coltype(PG_FUNCTION_ARGS)
{
    PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
}

I lifted the expression to get the argument's type from "Polymorphic
Arguments and Return Types" in the "C-Language Functions" section
of the "Extending SQL" chapter in the documentation.  Read "Compiling
and Linking Dynamically-Loaded Functions" for build instructions.
After you've built and installed the shared object file, create the
function with the following SQL statement:

CREATE OR REPLACE FUNCTION coltype(anyelement) RETURNS oid
AS '$libdir/coltype' LANGUAGE C IMMUTABLE;

Change '$libdir/coltype' if you name the shared object file something
other than coltype.so.

Now watch, somebody will jump in and say, "Why'd you go to all that
trouble?  Here's an easier way...."

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

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Simulating WAL on separate disks
Следующее
От: Pierre-Frédéric Caillaud
Дата:
Сообщение: Re: Link to development version of docs on website?