Re: Function for retreiving datatype - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Function for retreiving datatype
Date
Msg-id 20050111005646.GA7422@winnie.fuhr.org
Whole thread Raw
In response to Re: Function for retreiving datatype  (Brendan Jurd <blakjak@blakjak.sytes.net>)
Responses Re: Function for retreiving datatype
List 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 by date:

Previous
From: Tom Lane
Date:
Subject: Re: Simulating WAL on separate disks
Next
From: Pierre-Frédéric Caillaud
Date:
Subject: Re: Link to development version of docs on website?