Re: test datatype for ANY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: NosyMan <nosyman(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: test datatype for ANY
Date: 2005-02-11 20:37:01
Message-ID: 13379.1108154221@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> On Fri, Feb 11, 2005 at 02:32:31PM -0500, Tom Lane wrote:
>> There are some limited cases you could handle in plpgsql using the
>> polymorphic-functions stuff (ie, ANYELEMENT not ANY) but it still has
>> no concept of a run-time type test.

> Eh? What am I misunderstanding then? The following done in 8.0.1:

> CREATE FUNCTION argtype(param anyelement) RETURNS text AS $$
> BEGIN
> IF param IS OF (integer) THEN
> RETURN 'integer';
> ELSIF param IS OF (numeric) THEN
> RETURN 'numeric';
> ELSIF param IS OF (boolean) THEN
> RETURN 'boolean';
> ELSIF param IS OF (text) THEN
> RETURN 'text';
> ELSIF param IS OF (date) THEN
> RETURN 'date';
> END IF;

> RETURN 'something else';
> END;
> $$ LANGUAGE plpgsql IMMUTABLE;

[ thinks about that for awhile... ] Oh, I see. The reason this appears
to work is that plpgsql compiles a separate version of the function for
each actual parameter datatype that is used in a given session. So in
your example, you get a separate version for integer, numeric, etc.
Within each such version IS OF yields constants, but it "works right"
anyway.

I'm not sure if you can actually tell the difference between this
behavior and a true runtime test; except maybe that the backend would
get a bit bloated if you tried it on hundreds of different types in one
session.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message NosyMan 2005-02-11 20:40:53 test datatype for ANY
Previous Message Michael Fuhr 2005-02-11 20:27:38 Re: test datatype for ANY