Unix Technical Forum

Query_time SQL as a function w/o creating a new type

This is a discussion on Query_time SQL as a function w/o creating a new type within the Pgsql General forums, part of the PostgreSQL category; --> Hi, After Erik Jones gave me the idea for this, I started to become lazy to have to type ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:08 PM
Ow Mun Heng
 
Posts: n/a
Default Query_time SQL as a function w/o creating a new type

Hi,

After Erik Jones gave me the idea for this, I started to become lazy to
have to type this into the sql everytime I want to see how long a query
is taking.. so, I thought that I'll create a function to do just that..
I ended up with..

CREATE OR REPLACE FUNCTION query_time()
RETURNS SETOF query_time AS
$BODY$
DECLARE
rec RECORD;

BEGIN
FOR rec IN
SELECT procpid, client_addr, now() - query_start as query_time,
current_query
FROM pg_stat_activity
ORDER BY query_time DESC
LOOP
RETURN NEXT rec;
END LOOP;
RETURN;
END;

$BODY$
LANGUAGE 'plpgsql' VOLATILE;


But the issue with the above is that I need to create a type.

CREATE TYPE query_time AS
(procpid integer,
client_addr inet,
query_time interval,
current_query text);

Is there a method which I'm able to return a result set w/o needing to declare/create a new type.

I tried to use language 'sql' but it only returned me 1 column, with all the fields concatenated together with
comma separating the fields.





---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 11:08 PM
Reg Me Please
 
Posts: n/a
Default Re: Query_time SQL as a function w/o creating a new type

You could try this:


CREATE OR REPLACE FUNCTION foo( out procpid integer, out client_addr inet, out
query_time interval, out current_query text )
RETURNS SETOF RECORD AS $BODY$
....
$BODY$ LANGUAGE PLPGSQL VOLATILE;

(Thanks to Joen Conway for showing this in tablefunc!)


Il Friday 26 October 2007 08:24:46 Ow Mun Heng ha scritto:
> Hi,
>
> After Erik Jones gave me the idea for this, I started to become lazy to
> have to type this into the sql everytime I want to see how long a query
> is taking.. so, I thought that I'll create a function to do just that..
> I ended up with..
>
> CREATE OR REPLACE FUNCTION query_time()
> RETURNS SETOF query_time AS
> $BODY$
> DECLARE
> rec RECORD;
>
> BEGIN
> FOR rec IN
> SELECT procpid, client_addr, now() - query_start as query_time,
> current_query
> FROM pg_stat_activity
> ORDER BY query_time DESC
> LOOP
> RETURN NEXT rec;
> END LOOP;
> RETURN;
> END;
>
> $BODY$
> LANGUAGE 'plpgsql' VOLATILE;
>
>
> But the issue with the above is that I need to create a type.
>
> CREATE TYPE query_time AS
> (procpid integer,
> client_addr inet,
> query_time interval,
> current_query text);
>
> Is there a method which I'm able to return a result set w/o needing to
> declare/create a new type.
>
> I tried to use language 'sql' but it only returned me 1 column, with all
> the fields concatenated together with comma separating the fields.
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 11:08 PM
Ow Mun Heng
 
Posts: n/a
Default Re: Query_time SQL as a function w/o creating a new type


On Fri, 2007-10-26 at 08:35 +0200, Reg Me Please wrote:
> You could try this:
>
>
> CREATE OR REPLACE FUNCTION foo( out procpid integer, out client_addr inet, out
> query_time interval, out current_query text )
> RETURNS SETOF RECORD AS $BODY$
> ...
> $BODY$ LANGUAGE PLPGSQL VOLATILE;



Somehow it doesn't work..

CREATE OR REPLACE FUNCTION query_time2( out procpid integer, out
client_addr inet, out
query_time interval, out current_query text ) AS
--RETURNS SETOF RECORD AS
$BODY$

BEGIN
SELECT procpid, client_addr, (now() - query_start),
current_query
FROM pg_stat_activity
ORDER BY (now() - query_start) DESC;
RETURN;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;


ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM
instead.
CONTEXT: PL/pgSQL function "query_time2" line 3 at SQL statement


>
> Il Friday 26 October 2007 08:24:46 Ow Mun Heng ha scritto:
> > Hi,
> >
> > After Erik Jones gave me the idea for this, I started to become lazy to
> > have to type this into the sql everytime I want to see how long a query
> > is taking.. so, I thought that I'll create a function to do just that..
> > I ended up with..
> >
> > CREATE OR REPLACE FUNCTION query_time()
> > RETURNS SETOF query_time AS
> > $BODY$
> > DECLARE
> > rec RECORD;
> >
> > BEGIN
> > FOR rec IN
> > SELECT procpid, client_addr, now() - query_start as query_time,
> > current_query
> > FROM pg_stat_activity
> > ORDER BY query_time DESC
> > LOOP
> > RETURN NEXT rec;
> > END LOOP;
> > RETURN;
> > END;
> >
> > $BODY$
> > LANGUAGE 'plpgsql' VOLATILE;
> >
> >
> > But the issue with the above is that I need to create a type.
> >
> > CREATE TYPE query_time AS
> > (procpid integer,
> > client_addr inet,
> > query_time interval,
> > current_query text);
> >
> > Is there a method which I'm able to return a result set w/o needing to
> > declare/create a new type.
> >
> > I tried to use language 'sql' but it only returned me 1 column, with all
> > the fields concatenated together with comma separating the fields.
> >
> >
> >
> >
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 1: if posting/reading through Usenet, please send an appropriate
> > subscribe-nomail command to majordomo@postgresql.org so that your
> > message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 11:08 PM
A. Kretschmer
 
Posts: n/a
Default Re: Query_time SQL as a function w/o creating a new type

am Fri, dem 26.10.2007, um 14:51:52 +0800 mailte Ow Mun Heng folgendes:
>
> On Fri, 2007-10-26 at 08:35 +0200, Reg Me Please wrote:
> > You could try this:
> >
> >
> > CREATE OR REPLACE FUNCTION foo( out procpid integer, out client_addr inet, out
> > query_time interval, out current_query text )
> > RETURNS SETOF RECORD AS $BODY$
> > ...
> > $BODY$ LANGUAGE PLPGSQL VOLATILE;

>
>
> Somehow it doesn't work..
>
> CREATE OR REPLACE FUNCTION query_time2( out procpid integer, out
> client_addr inet, out
> query_time interval, out current_query text ) AS
> --RETURNS SETOF RECORD AS
> $BODY$
>
> BEGIN
> SELECT procpid, client_addr, (now() - query_start),
> current_query
> FROM pg_stat_activity
> ORDER BY (now() - query_start) DESC;
> RETURN;
> END;
> $BODY$
> LANGUAGE 'plpgsql' VOLATILE;
>
>
> ERROR: query has no destination for result data
> HINT: If you want to discard the results of a SELECT, use PERFORM
> instead.
> CONTEXT: PL/pgSQL function "query_time2" line 3 at SQL statement


Change the SELECT procpid, ... to
SELECT into procpid, ...


Thats all (i hope)...


Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 11:08 PM
Tom Lane
 
Posts: n/a
Default Re: Query_time SQL as a function w/o creating a new type

"A. Kretschmer" <andreas.kretschmer@schollglas.com> writes:
> Change the SELECT procpid, ... to
> SELECT into procpid, ...


For something like this, you shouldn't use plpgsql at all: a simple
SQL function gets the job done with a lot less notational overhead
(and likely less runtime overhead too).

postgres=# CREATE OR REPLACE FUNCTION query_time2( out procpid integer, out
postgres(# client_addr inet, out
postgres(# query_time interval, out current_query text )
postgres-# returns setof record as $$
postgres$# SELECT procpid, client_addr, (now() - query_start),
postgres$# current_query
postgres$# FROM pg_stat_activity
postgres$# ORDER BY (now() - query_start) DESC;
postgres$# $$ language sql;
CREATE FUNCTION
postgres=# select * from query_time2();
procpid | client_addr | query_time | current_query
---------+-------------+------------+------------------------------
9874 | | 00:00:00 | select * from query_time2();
(1 row)

postgres=#

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:33 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com