Re: Function for retreiving datatype

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Brendan Jurd <blakjak(at)blakjak(dot)sytes(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Function for retreiving datatype
Date: 2005-01-11 00:56:46
Message-ID: 20050111005646.GA7422@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pierre-Frédéric Caillaud 2005-01-11 01:15:21 Re: Link to development version of docs on website?
Previous Message Tom Lane 2005-01-11 00:29:53 Re: Simulating WAL on separate disks