Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 0474A2E0227 for ; Tue, 29 Jan 2008 11:30:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 17784-04-2 for ; Tue, 29 Jan 2008 11:29:53 -0400 (AST) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.154]) by postgresql.org (Postfix) with ESMTP id AF2B12E020F for ; Tue, 29 Jan 2008 11:29:52 -0400 (AST) Received: by fg-out-1718.google.com with SMTP id 22so2162142fge.23 for ; Tue, 29 Jan 2008 07:29:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; bh=t9WNQGmS9oc/a5g1wIg5gLaOeqlVQS+6PGnGl7EKc1Y=; b=T1ebL3descIbeoZkKUyRv5FdCLG0TWeV02r9fZJz98rBzXqVA4QbMqf6LPSe1je1etBDnMImlfaXl2F8T+s2DTgZo6PuHnPd56n5wp5MZuUKjCHReXyrd2AUighSG4QGExNnHdcYQMRbW9oc281NN7xBDxo1RdPH0XMb4nQ1YV4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=TrkNmGqtvinqiAHtlLiSjHH2lm8qSY3ZvY9dxpxBcL4p9T48hb7cF74E0eCZYfVkq1ShfLgKmw8AoHmUjWlkid7CTwkapamDiw58+I1tqGMu1w2Osx9u8e8jgorTS2LqbQV0THnhecr1Qb4d6im8Y8GYcGDckD1UtzeRgNv0/uk= Received: by 10.82.138.6 with SMTP id l6mr12407427bud.13.1201620590360; Tue, 29 Jan 2008 07:29:50 -0800 (PST) Received: by 10.82.186.11 with HTTP; Tue, 29 Jan 2008 07:29:50 -0800 (PST) Message-ID: <758d5e7f0801290729t64bd9ad8oaeb05a521baa6ca5@mail.gmail.com> Date: Tue, 29 Jan 2008 16:29:50 +0100 From: "Dawid Kuroczko" To: pgsql-sql Subject: JOINing SET returning function. MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/222 X-Sequence-Number: 30075 Hi, I have a set returning function returning variable number of rows (RETURNS SETOF RECORD). I have a table which contains a list of input values for this SRF. I want to write SQL which will return all the data found there. A simple test case: a tble CREATE TEMP TABLE list (n int); INSERT INTO list VALUES (1),(3),(5),(6); CREATE OR REPLACE FUNCTION test_set (IN OUT n int, OUT bar text) RETURNS SETOF RECORD AS $$ my $n = shift; for my $x (1..$n) { return_next { n=> $x, bar => '#' x $x } }; return undef; $$ LANGUAGE PLperl; Now, this works fine: SELECT select * from test_set(4); n | bar ---+------ 1 | # 2 | ## 3 | ### 4 | #### And this works fine too: SELECT generate_series(1,n) FROM list; generate_series ----------------- 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 However I would like to write a code which will work with test_set() as it does with generate_series: SELECT test_set(n) FROM list; ERROR: set-valued function called in context that cannot accept a set ...for which I've tried subselects and JOINs but somehow I fail to make it work, for instance: SELECT * FROM list, test_set(n); ERROR: function expression in FROM may not refer to other relations of same query level Is there an obvious solution (except wrapping it in some PL function)? If it is not clear unitl that point, I want to do equivalent of: SELECT * FROM test_set(1) UNION ALL SELECT * FROM test_set(3) UNION ALL SELECT * FROM test_set(5) UNION ALL SELECT * FROM test_set(6); ...except dynamically using "n" column from "list" table. Of course this is a test case, not the original problem. :-) Regards, Dawid