Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 29F0F9FB375 for ; Sun, 15 Oct 2006 13:54:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 69443-08 for ; Sun, 15 Oct 2006 13:54:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 214DA9FA4A7 for ; Sun, 15 Oct 2006 13:54:09 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k9FGs8ed023116; Sun, 15 Oct 2006 12:54:08 -0400 (EDT) To: ow cc: pgsql-sql@postgresql.org Subject: Re: Temp tables, reports in Postgresql (and other RDBMS) In-reply-to: <20061015160847.83246.qmail@web53909.mail.yahoo.com> References: <20061015160847.83246.qmail@web53909.mail.yahoo.com> Comments: In-reply-to ow message dated "Sun, 15 Oct 2006 09:08:47 -0700" Date: Sun, 15 Oct 2006 12:54:08 -0400 Message-ID: <23115.1160931248@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.089 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/145 X-Sequence-Number: 26534 ow writes: > We are considering moving some reports from *** to Postgres. Our reports are > written as stored procs in Transact-SQL and usually have the following > structure: > ... > How would one rewrite the above logic in Postgresql? If that's all you have to do, then SQL-language functions ought to be enough. If you need some procedural logic (if-then-else stuff) then you'd need to go over to plpgsql, which would be a bit of a pain because its habit of caching query plans doesn't play nice with temp tables. Possibly you could work some trick with ON COMMIT DELETE ROWS temp tables that are created once at the start of a session and are auto-emptied after each function by the ON COMMIT rule. Since the tables themselves don't get dropped, there's no problem with plan invalidation. There's also the possibility of using one of the other PLs such as plperl, if you're familiar with any of the common scripting languages they're based on. The other PLs don't do implicit plan caching so they won't have problems with temp tables; but it does mean knowing still another language and putting up with some notational inconvenience. regards, tom lane