X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 9FB0D9FA42F for ; Fri, 21 Jul 2006 17:52:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 42565-01 for ; Fri, 21 Jul 2006 20:52:38 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.borderware.com (mail.borderware.com [207.236.65.231]) by postgresql.org (Postfix) with ESMTP id 47E2C9FA6B0 for ; Fri, 21 Jul 2006 17:28:59 -0300 (ADT) Message-ID: <44C13909.9050900@borderware.com> Date: Fri, 21 Jul 2006 16:28:57 -0400 From: Kevin Keith User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Partitioned tables in queries References: <44C12852.1040909@borderware.com> In-Reply-To: <44C12852.1040909@borderware.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200607/215 X-Sequence-Number: 20075 My post might have been a little premature - and I apologize for that. I have figured out what was causing the problem: 1. Constraint exclusion was disabled. I re-enabled. 2. I found that using the now() function - and arbitrary interval will produce a different execution plan that using a specific date. For example: assuming the current time is 16:00: a) where start_date > now() - interval '4 hours' scans all child tables. b) where start_date > '2006-07-21 12:00:00' only scans the child table with today's data. So am I to assume that the value in the query must be a constant, and cannot be a result of a built-in function in order for constraint_exclusion to work correctly? Thanks, Kevin Kevin Keith wrote: > I have a case where I am partitioning tables based on a date range in > version 8.1.4. For example: > > table_with_millions_of_records > interaction_id char(16) primary key > start_date timestamp (without timezone) - indexed > .. other columns > > child_1 start_date >= 2006-07-21 00:00:00 > child_2 start_date >= 2006-07-20 00:00:00 and start_date < > 2006-07-21 00:00:00 > ... > child_5 start_date >= 2006-07-17 00:00:00 and start_date < > 2006-07-18 00:00:00 > > with rules on the parent and child tables that redirect the data to > the appropriate child table based on the start_date. > > Because this table is going to grow very large (very quickly), and > will need to be purged daily, I created partitions, or child tables to > hold data for each day. I have done the same thing in Oracle in the > past, and the PostgreSQL solution works great. The archival process is > very simple - drop the expired child table. I am having one problem. > > If I run a query on the full table (there are 5 child tables with data > for the last 5 days), and my where clause contains data for the > current day only: > where start_date > date_trunc('day', now()) > all 5 child tables are scanned when I look at the output from explain > analyze. > > My question is - can I force the planner to only scan the relevant > child table - when the key related to the partitioned data it part of > the where clause? > > Thanks, > > Kevin > > > ... >