Re: Using the extract() function in plpgsql

From: Kristis Makris <kristis(dot)makris(at)datasoft(dot)com>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Using the extract() function in plpgsql
Date: 2001-06-27 19:25:09
Message-ID: 200106271925.f5RJPCa53206@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

Hi Richard,

> 1. drop table not working - I'll check the docs on this
> 2. bad oid reference - even if we did recreate the table, the reference
> to it is compiled in after the first run. Don't see a way around this one.

I suspected that since the function is only compiled once, it somehow
grabs an oid to be used in the future for the temporary table. Still,
though, that makes no sense. I would assume that whenever the CREATE
TEMPORARY TABLE stmt is executed, a new oid would be used. Anyway, I'm
not familiar with the internals of postgres.

> I've gone back and looked at your initial email (tuned in halfway
> through this - sorry). It looks like all you want to do is get the
> number of seconds difference between two times into a variable - is that right?

Yes, that is correct. We got slightly sidetracked here, but the
temporary tables creation issue was also a problem I've been dealing
with for some time now.


> If so, all you need to do is use the following code. I got a bit mislead
> by the whole CREATE TABLE AS business earlier.
>
> DROP FUNCTION sel_in(timestamp, timestamp);
>
> CREATE FUNCTION sel_in(timestamp, timestamp) returns int4 as '
> DECLARE
> fromdt ALIAS FOR $1;
> todt ALIAS FOR $2;
> diff interval;
> idiff int4;
> myrec record;
> BEGIN
> diff := todt - fromdt;
> RAISE NOTICE ''diff = %'',diff;
> idiff:= extract(epoch from diff);
> RAISE NOTICE ''idiff = %'',idiff;
> RETURN idiff;
> END;
> ' language 'plpgsql';
>
> select sel_in(now(), '2001-06-27 19:27:00+01'::timestamp);
>
>
> You don't actually need to use a select at all, just assignment.
>
> Is this more what you were after?

Yap, that's what I was after. After going through the docs I found the
following example for the extract():

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');

Thus, I was so attempting to use in the plpgsql function the syntax:

lSeconds = extract(epoch from interval ''5 days 3 hours''); -- suceeds
lSeconds = extract(epoch from interval ''lDifference''); -- fails
lSeconds = extract(epoch from interval lDifference); -- fails

while the correct syntax is the one you used:

lSeconds = extract(epoch from lDifference);

..without the INTERVAL keyword used. It looks like the reason the
INTERVAL word is used is for casting the interval in the
quotes(otherwise treated as text). This syntax is not acceptable,
though, through the psql client.

Under the psql client this fails:

SELECT EXTRACT(EPOCH FROM '5 days 3 hours');

but this succeeds:

SELECT EXTRACT(EPOCH FROM '5 days 3 hours'::interval);

It would be nice if the differences between the sql and plpgsql syntax
were documented somewhere.

Thanks for spending time on this :)
-Kristis

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2001-06-27 20:04:28 Re: Using the extract() function in plpgsql
Previous Message Richard Huxton 2001-06-27 18:30:54 Re: Using the extract() function in plpgsql

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-06-27 20:04:28 Re: Using the extract() function in plpgsql
Previous Message Jan Wieck 2001-06-27 19:10:57 Re: Storing image contents in TEXT fields