I am wondering if there is a way to declare a variable of type table in PL/pgSQL to hold query results? For instance how can I express something like:

q1 = select * from foo;
q2 = select * from bar;
for t1 in q1:
  for t2 in q2:
    -- do something with t1 and t2

I looked into the return next construct but that seems to only able to handle return values.

  • 2
    PL/SQL => Oracle, PgPL/SQL => PostgreSQL. Which one is it you're working with? – Mat Mar 1 '13 at 5:40
  • @Mat: It's PL/pgSQL, btw. – Erwin Brandstetter Aug 5 '13 at 12:09

In PostgreSQL, every table name serves as type name for the row type (a.k.a. composite type) automatically - not a table type, there are no "table types" or "table variables" in Postgres (but there are typed tables).
So you can just declare a variable of that type in PL/pgSQL.

  q1 foo;  -- "foo" ...
  q2 bar;  -- ... and "bar" are existing (visible) table names

FOR q1 IN 
   SELECT * from foo
   FOR q2 IN 
      SELECT * from bar
       -- do something with q1 and q2
       -- since q1 and q2 are well known types, you can access columns
       -- with attribute notation. Like: q1.col1


A FOR loop works with a built-in cursor. There are also explicit cursors in plpgsql.

You could also just declare variables of the generic type record. It can take any row type at assignment automatically. But special rules apply. Be sure to follow the link and read the chapter of the manual!

While it's often convenient to have the function return SETOF <table name>, returning SETOF record is not as convenient. The system does not know what the function returns this way and you have to add a column definition list with every call. Which is a pain. Details about table functions in the manual.

Often there are more efficient solutions with plain SQL, though. Looping is a measure of last resort, when you can do things in one scan where you would need multiple scans in pure SQL.

| improve this answer | |
  • 2
    Also note that if you want to return the table type you can just say something like CREATE FUNCTION footest() RETURNS SETOF foo LANGUAGE PLPGSQL AS $$... – Chris Travers Mar 1 '13 at 6:23
  • Why then we at all have the feature of "composite types" if we can create tables instead, without restrictions applied to composite types (no constraints, etc)? The only meaningful way of using composite types I see is returning a couple (or several) parameters from a function instead of creating a whole table for that. – SaneDeveloper Feb 5 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.