Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 57B916333DE for ; Wed, 3 Feb 2010 06:27:29 -0400 (AST) 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 54125-04 for ; Wed, 3 Feb 2010 10:27:13 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp1.hi-media-techno.com (smtp1.hi-media-techno.com [195.158.241.85]) by mail.postgresql.org (Postfix) with ESMTP id 9C29C632F01 for ; Wed, 3 Feb 2010 06:27:18 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by smtp1.hi-media-techno.com (Postfix) with ESMTP id 4BD6B93B90; Wed, 3 Feb 2010 11:27:16 +0100 (CET) Received: from smtp1.hi-media-techno.com ([127.0.0.1]) by localhost (smtp1.backbone.hi-media-techno.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yr-VsCWOgQZr; Wed, 3 Feb 2010 11:27:16 +0100 (CET) Received: from mail1.hi-media-techno.com (unknown [192.168.27.17]) by smtp1.hi-media-techno.com (Postfix) with ESMTP id 0116293B76; Wed, 3 Feb 2010 11:27:16 +0100 (CET) Received: from localhost (localhost.localdomain [127.0.0.1]) by mail1.hi-media-techno.com (Postfix) with ESMTP id 08F7268330; Wed, 3 Feb 2010 11:27:16 +0100 (CET) X-Virus-Scanned: amavisd-new at mail1.hi-media-techno.com Received: from mail1.hi-media-techno.com ([127.0.0.1]) by localhost (mail1.hi-media-techno.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LKgHsYTNnCW1; Wed, 3 Feb 2010 11:27:15 +0100 (CET) Received: from dim (unknown [83.167.62.196]) by mail1.hi-media-techno.com (Postfix) with ESMTPSA id D883C68222; Wed, 3 Feb 2010 11:27:15 +0100 (CET) Received: by dim (Postfix, from userid 1000) id E224413F43; Wed, 3 Feb 2010 11:23:58 +0100 (CET) From: Dimitri Fontaine To: Tom Lane Cc: "Davor J." , pgsql-performance@postgresql.org Subject: Re: queries with subquery constraints on partitioned tables not optimized? Organization: Hi-Media References: <5002.1265156054@sss.pgh.pa.us> Date: Wed, 03 Feb 2010 11:23:58 +0100 In-Reply-To: <5002.1265156054@sss.pgh.pa.us> (Tom Lane's message of "Tue, 02 Feb 2010 19:14:14 -0500") Message-ID: <87k4uuty69.fsf@hi-media-techno.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.412 tagged_above=-10 required=5 tests=AWL=0.031, BAYES_00=-2.599, SUBJECT_FUZZY_TION=0.156 X-Spam-Level: X-Archive-Number: 201002/50 X-Sequence-Number: 37471 Tom Lane writes: > "Davor J." writes: >> Now, if one takes a subquery for "1", the optimizer evaluates it first >> (let's say to "1"), but then searches for it (sequentially) in every >> partition, which, for large partitions, can be very time-consuming and goes >> beyond the point of partitioning. > > No, the optimizer doesn't "evaluate it first". Subqueries aren't ever > assumed to reduce to constants. (If you actually do have a constant > expression, why don't you just leave out the word SELECT?) It's easy to experience the same problem with a JOIN you'd want to happen at the partition level that the planner will apply on the Append Node. I'm yet to figure out if 8.4 is smarter about this, meanwhile I'm using array tricks to force the push-down. WHERE ... AND service = ANY ((SELECT array_accum(id) FROM services WHERE x=281) || (SELECT array_accum(id) FROM services WHERE y=281)) It happens that I need the array concatenation more than the = ANY operator (as compared to IN), so I also have queries using = ANY ('{}':int[] || (SELECT array_accum(x) ...)) to really force the planner into doing the join in the partitions rather than after the Append has taken place. Regards, -- dim PS: If you're interrested into complete examples, I'll be able to provide for them in private.