Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A5399B5DC5B for ; Wed, 2 Nov 2011 11:38:45 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 71923-04 for ; Wed, 2 Nov 2011 14:38:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 98B21B5DCD6 for ; Wed, 2 Nov 2011 11:38:39 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id pA2EcdTL029919; Wed, 2 Nov 2011 10:38:39 -0400 (EDT) To: Jay Levitt cc: pgsql-performance@postgresql.org Subject: Re: Guide to PG's capabilities for inlining, predicate hoisting, flattening, etc? In-reply-to: References: Comments: In-reply-to Jay Levitt message dated "Wed, 02 Nov 2011 10:22:21 -0400" Date: Wed, 02 Nov 2011 10:38:39 -0400 Message-ID: <29918.1320244719@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/16 X-Sequence-Number: 45527 Jay Levitt writes: > So you can see where I'm going. I know if I break everything into > elegant, composable functions, it'll continue to perform poorly. If I > write one big hairy, it'll perform great but it will be difficult to > maintain, and it will be inelegant and a kitten will die. My tools > are CTEs, subqueries, aliases, SQL functions, PL/pgSQL functions, and > views (and other tools?) What optimizations do each of those prevent? plpgsql functions are black boxes to the optimizer. If you can express your functions as single SQL commands, using SQL-language functions is usually a better bet than plpgsql. CTEs are also treated as optimization fences; this is not so much an optimizer limitation as to keep the semantics sane when the CTE contains a writable query. regards, tom lane