Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 054452E31CF for ; Tue, 22 Jan 2008 21:43:19 -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 37976-08 for ; Tue, 22 Jan 2008 21:43:10 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id BD89E2E2E7D for ; Tue, 22 Jan 2008 21:43:15 -0400 (AST) 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 m0N1hDV8010870; Tue, 22 Jan 2008 20:43:13 -0500 (EST) To: "Guillaume Smet" cc: pgsql-performance Subject: Re: Workaround for cross column stats dependency In-reply-to: <1d4e0c10801221657s6b345fcdqd1c0b3f20e519316@mail.gmail.com> References: <1d4e0c10801221657s6b345fcdqd1c0b3f20e519316@mail.gmail.com> Comments: In-reply-to "Guillaume Smet" message dated "Wed, 23 Jan 2008 01:57:07 +0100" Date: Tue, 22 Jan 2008 20:43:13 -0500 Message-ID: <10869.1201052593@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/185 X-Sequence-Number: 28424 "Guillaume Smet" writes: > So the question is: is there any way to improve the results of the > original query, other than doing a first query in the application to > get the list of types and inject them in a second query (the one just > above)? Well, if you're willing to cheat like mad, you can use a phony immutable function to perform that injection. Here's a really silly example in the regression database: regression=# create or replace function getu2(int) returns int[] as $$ select array(select unique2 from tenk1 where thousand = $1); $$ language sql immutable; CREATE FUNCTION regression=# explain select * from tenk1 where unique1 = any(getu2(42)); QUERY PLAN ------------------------------------------------------------------------------------------------------ Bitmap Heap Scan on tenk1 (cost=38.59..73.80 rows=10 width=244) Recheck Cond: (unique1 = ANY ('{381,932,2369,4476,5530,6342,6842,6961,7817,7973}'::integer[])) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..38.59 rows=10 width=0) Index Cond: (unique1 = ANY ('{381,932,2369,4476,5530,6342,6842,6961,7817,7973}'::integer[])) (4 rows) Since the function is marked immutable, it'll be pre-evaluated during planning and then the constant array result is exposed for statistics purposes. Now this method *only* works for interactive queries, or EXECUTE'd queries in plpgsql, because you don't want the plan containing the folded constants to get cached. At least not if you're worried about responding promptly to changes in the table you're fetching from. But if that table is essentially constant anyway in your application, there's little downside to this trick. regards, tom lane