Re: plpgsql question

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Matthew Peter <survivedsushi(at)yahoo(dot)com>
Cc: Pandurangan R S <pandurangan(dot)r(dot)s(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql question
Date: 2006-01-06 03:03:21
Message-ID: 20060106030321.GB80777@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 05, 2006 at 12:50:34AM -0800, Matthew Peter wrote:
> Is it possible to skip the loop and just return all records in a
> single query and shove all those rows into a table variable?

Not in PL/pgSQL -- you need to return each row with RETURN NEXT,
generally from within a loop. Why do you want to avoid that?

For simple functions you could use SQL instead of PL/pgSQL:

http://www.postgresql.org/docs/8.1/interactive/xfunc-sql.html#AEN31627

> Also, the WHERE part is also important cause I'm not sure i got
> that part right? Would this call for EXECUTE or will it be okay and
> be planned the first time by the query planner?

If each call to the function issues the same query, just with
different values, then you shouldn't need to use EXECUTE. If the
query differs depending on the function parameters then you have
several possibilities:

* You could build the query string and use EXECUTE. Be sure to
read about quote_literal() and quote_ident().

* You could use an IF statement to execute the query you need.

* You could put the queries in separate functions. You can use
the same name for different functions if their call signatures
are different, e.g., getrecord(integer) and getrecord(integer, text).

* You could rewrite the query, possibly using CASE or COALESCE
to handle NULL values.

--
Michael Fuhr

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Kunkel 2006-01-06 04:08:39 Adding another primary key to a populated table
Previous Message Michael Fuhr 2006-01-06 02:22:48 Re: copy from error