Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8333B633991 for ; Wed, 21 Apr 2010 22:21:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 16891-09 for ; Thu, 22 Apr 2010 01:21:24 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gw0-f46.google.com (mail-gw0-f46.google.com [74.125.83.46]) by mail.postgresql.org (Postfix) with ESMTP id DD3B36333E3 for ; Wed, 21 Apr 2010 22:21:32 -0300 (ADT) Received: by gwj19 with SMTP id 19so1733796gwj.19 for ; Wed, 21 Apr 2010 18:21:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:cc:content-type; bh=S9jYwjvvF8f9CXPLtGXub2oY+bWxhFPpFaoZ3V/MYts=; b=kqnckS9jfUNMfsfxx4TyRVYVGUPsGk+h7j9bAvh59Lb/iX3KT5FwsBc85D3EpGGYDe tMA2P5cA878YvhMxpJPBiZUWCsILNGmqImihY95Oj2aBT4RWVCB1Cb7ryYwYlSQcWLfp 8nqtxEjou79KZ9Rr9TagtJsXDss/X8Q83qrnU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=GfZY/K7qup8A3B/WEvqVHrYSLfAJEx1ZpQkbDP59OlrZDOwcPdDPIg5WlcYNlIeFcb XwyZu1xpFO5L4PIWgx2livQVnFFsNnRuLqjCi7OF1KDh0oyXET4tt9eTgizOzb8egnCr zGcLGMGPMwvS0LMwy1ZydiNlkEHfuexi7BQ5g= MIME-Version: 1.0 Received: by 10.231.38.194 with HTTP; Wed, 21 Apr 2010 18:21:28 -0700 (PDT) In-Reply-To: References: Date: Wed, 21 Apr 2010 21:21:28 -0400 Received: by 10.101.207.34 with SMTP id j34mr21014869anq.74.1271899288156; Wed, 21 Apr 2010 18:21:28 -0700 (PDT) Message-ID: Subject: Re: Replacing Cursors with Temporary Tables From: Robert Haas To: Eliot Gable Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.927 tagged_above=-10 required=5 tests=AWL=-0.926, BAYES_40=-0.001 X-Spam-Level: X-Archive-Number: 201004/281 X-Sequence-Number: 38531 I think it's really tough to say how this is going to perform. I'd recommend constructing a couple of simplified test cases and benchmarking the heck out of it. One of the problems with temporary tables is that every time you create a temporary table, it creates a (temporary) record in pg_class; that can get to be a problem if you do it a lot. Another is that for non-trivial queries you may need to do a manual ANALYZE on the table to get good stats for the rest of the query to perform well. But on the flip side, as you say, nesting and unnesting of arrays and function calls are not free either. I am going to hazard a SWAG that the array implementation is faster UNLESS the lack of good stats on the contents of the arrays is hosing the performance somewhere down the road. But that is really just a total shot in the dark. Another possible implementation might be to have a couple of permanent tables where you store the results. Give each such table a "batch id" column, and return the batch id from your stored procedure. This would probably avoid a lot of the overhead associated with temp tables while retaining many of the benefits. ...Robert