DB2's row_number()

Lists: pgsql-hackers
From: Hans-Jürgen Schönig <postgres(at)cybertec(dot)at>
To: pgsql-hackers(at)postgresql(dot)org
Subject: DB2's row_number()
Date: 2003-07-17 15:03:03
Message-ID: 3F16BAA7.4090503@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I had a brief look at DB2's row_number function which seems to be pretty
useful.
What it does is:

test=# SELECT row_number(), relname FROM pg_class LIMIT 3;
row_number | relname
------------+----------------
1 | pg_description
2 | pg_group
3 | pg_proc
(3 rows)

This makes sense to me and I need this feature from time to time. My
question is: How do I find out when a query starts? Inside a table
function I can call SRF_IS_FIRSTCALL() to see when it is called first.
Is there an easy way to check that inside an "ordinary" C function
returning just one value?
Currently my function counts the number of times it has been called per
connection. I could write a second function for resetting the counter
but this is not too smart ...

Regards,

Hans

--
Cybertec Geschwinde u Schoenig
Ludo-Hartmannplatz 1/14, A-1160 Vienna, Austria
Tel: +43/2952/30706; +43/664/233 90 75
www.cybertec.at, www.postgresql.at, kernel.cybertec.at


From: Darcy Buskermolen <darcy(at)wavefire(dot)com>
To: Hans-Jürgen Schönig <postgres(at)cybertec(dot)at>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: DB2's row_number()
Date: 2003-07-17 18:16:18
Message-ID: 200307171116.18654.darcy@wavefire.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Here is how I doi it when I need to...

BEGIN;
CREATE TEMP SEQUENCE row_num;
SELECT next_val('row_num'), relname FROM pg_class LIMIT 3;
ROLLBACK;

You could also do this with a Set Returning Fucntion so that it returns the
nextval in a simular way

On Thursday 17 July 2003 08:03, Hans-Jürgen Schönig wrote:
> I had a brief look at DB2's row_number function which seems to be pretty
> useful.
> What it does is:
>
> test=# SELECT row_number(), relname FROM pg_class LIMIT 3;
> row_number | relname
> ------------+----------------
> 1 | pg_description
> 2 | pg_group
> 3 | pg_proc
> (3 rows)
>
> This makes sense to me and I need this feature from time to time. My
> question is: How do I find out when a query starts? Inside a table
> function I can call SRF_IS_FIRSTCALL() to see when it is called first.
> Is there an easy way to check that inside an "ordinary" C function
> returning just one value?
> Currently my function counts the number of times it has been called per
> connection. I could write a second function for resetting the counter
> but this is not too smart ...
>
> Regards,
>
> Hans

--
Darcy Buskermolen
Wavefire Technologies Corp.
ph: 250.717.0200
fx: 250.763.1759
http://www.wavefire.com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Hans-Jürgen Schönig <postgres(at)cybertec(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: DB2's row_number()
Date: 2003-07-17 18:30:50
Message-ID: 5771.1058466650@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

=?ISO-8859-1?Q?Hans-J=FCrgen_Sch=F6nig?= <postgres(at)cybertec(dot)at> writes:
> question is: How do I find out when a query starts? Inside a table
> function I can call SRF_IS_FIRSTCALL() to see when it is called first.
> Is there an easy way to check that inside an "ordinary" C function
> returning just one value?

Use fn_extra to store some state data.
pg_stat_get_backend_idset() is a useful example.

regards, tom lane