Re: Passing a list of values to a function

From: Matthew Peter <survivedsushi(at)yahoo(dot)com>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: Jaime Casanova <systemguards(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a list of values to a function
Date: 2006-01-10 04:10:51
Message-ID: 20060110041051.26971.qmail@web35215.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

You could write the function to take an array of integers instead
of a text string:

CREATE OR REPLACE FUNCTION getlist(integer[]) RETURNS SETOF my_tbl as $$
SELECT * FROM my_tbl
WHERE u_id = 0 OR u_id = ANY($1)
$$ LANGUAGE SQL STABLE STRICT;

SELECT * FROM getlist('{1,2,3}');

Another way would be to build a query string in a PL/pgSQL function
and use EXECUTE, but beware of embedding function arguments in query
strings without quoting.

--
Michael Fuhr
Hmmmmmm .... I suspected it was using it as a text string, seeing how that's how it way defined. I guess querying it as an array would work okay.

Is there any way to blow out how the query was executed? Such as seeing all the WHERE joins and what not along with the explain output on the console? I think I read somewhere about it showing in the logs or something?


---------------------------------
Yahoo! Photos – Showcase holiday pictures in hardcover
Photo Books. You design it and we’ll bind it!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2006-01-10 04:29:47 Re: Vacuum all tables unders under one schema (not under one database)
Previous Message Matthew Peter 2006-01-10 03:42:01 Re: plpgsql question