pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Question about partitioned query behavior
6+ messages / 4 participants
[nested] [flat]

* Question about partitioned query behavior
@ 2010-07-02 15:28 Ranga Gopalan <ranga_gopalan@hotmail.com>
  2010-07-02 16:40 ` Re: Question about partitioned query behavior Benjamin Krajmalnik <kraj@servoyant.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Ranga Gopalan @ 2010-07-02 15:28 UTC (permalink / raw)
  To: pgsql-performance


Hi,

My question is regarding ORDER BY / LIMIT query behavior when using partitioning.

I have a large table (about 100 columns, several million rows) partitioned by a column called day (which is the date stored as yyyymmdd - say 20100502 for May 2nd 2010 etc.). Say the main table  is called FACT_TABLE and each child table is called FACT_TABLE_yyyymmdd (e.g. FACT_TABLE_20100502, FACT_TABLE_20100503 etc.) and has an appropriate CHECK constraint created on it to CHECK (day = yyyymmdd).

Postgres Version:  PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10), 64-bit

The query pattern I am looking at is (I have tried to simplify the column names for readability):

SELECT F1 from FACT_TABLE 
where day >= 20100502 and day <= 20100507  # selecting for a week
ORDER BY F2 desc
LIMIT 100


This is what is happening:

When I query from the specific day's (child) table, I get what I expect - a descending Index scan and good performance.

# explain  select F1 from FACT_TABLE_20100502 where day = 20100502 order by F2 desc limit 100;
                                                                    QUERY PLAN                                                                  
  
------------------------------------------------------------------------------------------------------------------------------------------------
--
 Limit  (cost=0.00..4.81 rows=100 width=41)
   ->  Index Scan Backward using F2_20100502 on FACT_TABLE_20100502  (cost=0.00..90355.89 rows=1876985 width=41
)
         Filter: (day = 20100502)



BUT:

When I do the same query against the parent table it is much slower - two things seem to happen - one is that the descending scan of the index is not done and secondly there seems to be a separate sort/limit at the end - i.e. all data from all partitions is retrieved and then sorted and limited - This seems to be much less efficient than doing a descending scan on each partition and limiting the results and then combining and reapplying the limit at the end.

explain  select F1 from FACT_TABLE where day = 20100502 order by F2 desc limit 100;
                                                                    QUERY PLAN                                                                  
   
------------------------------------------------------------------------------------------------------------------------------------------------
---
 Limit  (cost=20000084948.01..20000084948.01 rows=100 width=41)
   ->  Sort  (cost=20000084948.01..20000084994.93 rows=1876986 width=41)
         Sort Key: public.FACT_TABLE.F2
         ->  Result  (cost=10000000000.00..20000084230.64 rows=1876986 width=41)
               ->  Append  (cost=10000000000.00..20000084230.64 rows=1876986 width=41)
                     ->  Seq Scan on FACT_TABLE  (cost=10000000000.00..10000000010.02 rows=1 width=186)
                           Filter: (day = 20100502)
                     ->  Seq Scan on FACT_TABLE_20100502 FACT_TABLE  (cost=10000000000.00..10000084220.62 rows=1876985 width=4
1)
                           Filter: (day = 20100502)
(9 rows)


Could anyone please explain why this is happening and what I can do to get the query to perform well even when querying from the parent table?

Thanks,

Ranga




 		 	   		  
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2=

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Question about partitioned query behavior
  2010-07-02 15:28 Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
@ 2010-07-02 16:40 ` Benjamin Krajmalnik <kraj@servoyant.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Benjamin Krajmalnik @ 2010-07-02 16:40 UTC (permalink / raw)
  To: Ranga Gopalan <ranga_gopalan@hotmail.com>; pgsql-performance

In postgresql.conf, what are your settings for constraint_exclusion?

There are 3 settings - on, off, or partition.

Mine are set as follows:

 

constraint_exclusion = on            # on, off, or partition

 

Under 8.4.4 I had it set to partition, but the behavior was not what I
expected so I set it back to "on" and only the applicable partitions get
processed.

 

From: pgsql-performance-owner@postgresql.org
[mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Ranga
Gopalan
Sent: Friday, July 02, 2010 9:29 AM
To: pgsql-performance@postgresql.org
Subject: [PERFORM] Question about partitioned query behavior

 

Hi,

My question is regarding ORDER BY / LIMIT query behavior when using
partitioning.

I have a large table (about 100 columns, several million rows)
partitioned by a column called day (which is the date stored as yyyymmdd
- say 20100502 for May 2nd 2010 etc.). Say the main table  is called
FACT_TABLE and each child table is called FACT_TABLE_yyyymmdd (e.g.
FACT_TABLE_20100502, FACT_TABLE_20100503 etc.) and has an appropriate
CHECK constraint created on it to CHECK (day = yyyymmdd).

Postgres Version:  PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu,
compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10), 64-bit

The query pattern I am looking at is (I have tried to simplify the
column names for readability):

SELECT F1 from FACT_TABLE 
where day >= 20100502 and day <= 20100507  # selecting for a week
ORDER BY F2 desc
LIMIT 100


This is what is happening:

When I query from the specific day's (child) table, I get what I expect
- a descending Index scan and good performance.

# explain  select F1 from FACT_TABLE_20100502 where day = 20100502 order
by F2 desc limit 100;
 
QUERY PLAN

  
------------------------------------------------------------------------
------------------------------------------------------------------------
--
 Limit  (cost=0.00..4.81 rows=100 width=41)
   ->  Index Scan Backward using F2_20100502 on FACT_TABLE_20100502
(cost=0.00..90355.89 rows=1876985 width=41
)
         Filter: (day = 20100502)



BUT:

When I do the same query against the parent table it is much slower -
two things seem to happen - one is that the descending scan of the index
is not done and secondly there seems to be a separate sort/limit at the
end - i.e. all data from all partitions is retrieved and then sorted and
limited - This seems to be much less efficient than doing a descending
scan on each partition and limiting the results and then combining and
reapplying the limit at the end.

explain  select F1 from FACT_TABLE where day = 20100502 order by F2 desc
limit 100;
 
QUERY PLAN

   
------------------------------------------------------------------------
------------------------------------------------------------------------
---
 Limit  (cost=20000084948.01..20000084948.01 rows=100 width=41)
   ->  Sort  (cost=20000084948.01..20000084994.93 rows=1876986 width=41)
         Sort Key: public.FACT_TABLE.F2
         ->  Result  (cost=10000000000.00..20000084230.64 rows=1876986
width=41)
               ->  Append  (cost=10000000000.00..20000084230.64
rows=1876986 width=41)
                     ->  Seq Scan on FACT_TABLE
(cost=10000000000.00..10000000010.02 rows=1 width=186)
                           Filter: (day = 20100502)
                     ->  Seq Scan on FACT_TABLE_20100502 FACT_TABLE
(cost=10000000000.00..10000084220.62 rows=1876985 width=4
1)
                           Filter: (day = 20100502)
(9 rows)


Could anyone please explain why this is happening and what I can do to
get the query to perform well even when querying from the parent table?

Thanks,

Ranga






________________________________

Hotmail is redefining busy with tools for the New Busy. Get more from
your inbox. See how.
<http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL
:ON:WL:en-US:WM_HMP:042010_2> 

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Question about partitioned query behavior
@ 2010-07-06 16:30 Ranga Gopalan <ranga_gopalan@hotmail.com>
  2010-07-06 19:30 ` Re: Question about partitioned query behavior Robert Haas <robertmhaas@gmail.com>
  2010-07-06 20:26 ` Re: Question about partitioned query behavior Stephen Frost <sfrost@snowman.net>
  0 siblings, 2 replies; 6+ messages in thread

From: Ranga Gopalan @ 2010-07-06 16:30 UTC (permalink / raw)
  To: pgsql-performance


Hi,

It seems that this is an issue faced by others as well - Please see this link: http://stackoverflow.com/questions/2236776/efficient-querying-of-multi-partition-postgres-table

Is this a known bug? Is this something that someone is working on or is there a known work around?

Thanks,

Ranga


From: ranga_gopalan@hotmail.com
To: pgsql-performance@postgresql.org
Subject: Question about partitioned query behavior
Date: Fri, 2 Jul 2010 15:28:45 +0000








Hi,

My question is regarding ORDER BY / LIMIT query behavior when using partitioning.

I have a large table (about 100 columns, several million rows) partitioned by a column called day (which is the date stored as yyyymmdd - say 20100502 for May 2nd 2010 etc.). Say the main table  is called FACT_TABLE and each child table is called FACT_TABLE_yyyymmdd (e.g. FACT_TABLE_20100502, FACT_TABLE_20100503 etc.) and has an appropriate CHECK constraint created on it to CHECK (day = yyyymmdd).

Postgres Version:  PostgreSQL 8.4.2 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10), 64-bit

The query pattern I am looking at is (I have tried to simplify the column names for readability):

SELECT F1 from FACT_TABLE 
where day >= 20100502 and day <= 20100507  # selecting for a week
ORDER BY F2 desc
LIMIT 100


This is what is happening:

When I query from the specific day's (child) table, I get what I expect - a descending Index scan and good performance.

# explain  select F1 from FACT_TABLE_20100502 where day = 20100502 order by F2 desc limit 100;
                                                                    QUERY PLAN                                                                  
  
------------------------------------------------------------------------------------------------------------------------------------------------
--
 Limit  (cost=0.00..4.81 rows=100 width=41)
   ->  Index Scan Backward using F2_20100502 on FACT_TABLE_20100502  (cost=0.00..90355.89 rows=1876985 width=41
)
         Filter: (day = 20100502)



BUT:

When I do the same query against the parent table it is much slower - two things seem to happen - one is that the descending scan of the index is not done and secondly there seems to be a separate sort/limit at the end - i.e. all data from all partitions is retrieved and then sorted and limited - This seems to be much less efficient than doing a descending scan on each partition and limiting the results and then combining and reapplying the limit at the end.

explain  select F1 from FACT_TABLE where day = 20100502 order by F2 desc limit 100;
                                                                    QUERY PLAN                                                                  
   
------------------------------------------------------------------------------------------------------------------------------------------------
---
 Limit  (cost=20000084948.01..20000084948.01 rows=100 width=41)
   ->  Sort  (cost=20000084948.01..20000084994.93 rows=1876986 width=41)
         Sort Key: public.FACT_TABLE.F2
         ->  Result  (cost=10000000000.00..20000084230.64 rows=1876986 width=41)
               ->  Append  (cost=10000000000.00..20000084230.64 rows=1876986 width=41)
                     ->  Seq Scan on FACT_TABLE  (cost=10000000000.00..10000000010.02 rows=1 width=186)
                           Filter: (day = 20100502)
                     ->  Seq Scan on FACT_TABLE_20100502 FACT_TABLE  (cost=10000000000.00..10000084220.62 rows=1876985 width=4
1)
                           Filter: (day = 20100502)
(9 rows)


Could anyone please explain why this is happening and what I can do to get the query to perform well even when querying from the parent table?

Thanks,

Ranga




 		 	   		  
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. See how. 		 	   		  
_________________________________________________________________
Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_1=

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Question about partitioned query behavior
  2010-07-06 16:30 Re: Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
@ 2010-07-06 19:30 ` Robert Haas <robertmhaas@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Robert Haas @ 2010-07-06 19:30 UTC (permalink / raw)
  To: Ranga Gopalan <ranga_gopalan@hotmail.com>; +Cc: pgsql-performance

On Tue, Jul 6, 2010 at 12:30 PM, Ranga Gopalan
<ranga_gopalan@hotmail.com> wrote:
> It seems that this is an issue faced by others as well - Please see this
> link:
> http://stackoverflow.com/questions/2236776/efficient-querying-of-multi-partition-postgres-table
>
> Is this a known bug? Is this something that someone is working on or is
> there a known work around?

I think that we know this problem exists, but I'm not aware anyone is
working on fixing it.  There is a "Merge Append" patch floating around
out there that I think might help with this, but AFAICS it was last
updated on July 5, 2009, and still needed some more work at that time.

Since this is an all-volunteer effort, complicated problems like this
don't always get fixed as fast as we'd like; most of us have to spend
most of our time on whatever it is that our employer pays us to do.
Of course if you're in a position to sponsor a developer there are a
number of companies that will be happy to work with you.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Question about partitioned query behavior
  2010-07-06 16:30 Re: Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
@ 2010-07-06 20:26 ` Stephen Frost <sfrost@snowman.net>
  2010-07-06 21:02   ` Re: Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Stephen Frost @ 2010-07-06 20:26 UTC (permalink / raw)
  To: Ranga Gopalan <ranga_gopalan@hotmail.com>; +Cc: pgsql-performance

Ranga,

* Ranga Gopalan (ranga_gopalan@hotmail.com) wrote:
> It seems that this is an issue faced by others as well - Please see this link: http://stackoverflow.com/questions/2236776/efficient-querying-of-multi-partition-postgres-table
> 
> Is this a known bug? Is this something that someone is working on or is there a known work around?

Actually, if you look at that, the problem the original poster had was
that they didn't have constraint_exclusion turned on.  Then they were
complaining about having the (empty) master table and the needed
partition included (which, really, shouldn't be that big a deal).

Did you look at what the other reply suggested?  Do you have
constraint_exclusion = 'on' in your postgresql.conf?

	Thanks,

		Stephen

Attachments:

  signature.asc (application/pgp-signature, 196B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Question about partitioned query behavior
  2010-07-06 16:30 Re: Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
  2010-07-06 20:26 ` Re: Question about partitioned query behavior Stephen Frost <sfrost@snowman.net>
@ 2010-07-06 21:02   ` Ranga Gopalan <ranga_gopalan@hotmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Ranga Gopalan @ 2010-07-06 21:02 UTC (permalink / raw)
  To: sfrost@snowman.net; +Cc: pgsql-performance


Hi Stephen,



Constraint exclusion was initially partition and I set it to "on" as suggested and 
tried that - the query planner in both cases was correctly identifying the specific partitions being queried - the problem seems to be a generic issue related to the way queries on partition tables are handled and how the order by / limit is applied in this scenario.


Thanks,



Ranga

> Date: Tue, 6 Jul 2010 16:26:23 -0400
> From: sfrost@snowman.net
> To: ranga_gopalan@hotmail.com
> CC: pgsql-performance@postgresql.org
> Subject: Re: [PERFORM] Question about partitioned query behavior
> 
> Ranga,
> 
> * Ranga Gopalan (ranga_gopalan@hotmail.com) wrote:
> > It seems that this is an issue faced by others as well - Please see this link: http://stackoverflow.com/questions/2236776/efficient-querying-of-multi-partition-postgres-table
> > 
> > Is this a known bug? Is this something that someone is working on or is there a known work around?
> 
> Actually, if you look at that, the problem the original poster had was
> that they didn't have constraint_exclusion turned on.  Then they were
> complaining about having the (empty) master table and the needed
> partition included (which, really, shouldn't be that big a deal).
> 
> Did you look at what the other reply suggested?  Do you have
> constraint_exclusion = 'on' in your postgresql.conf?
> 
> 	Thanks,
> 
> 		Stephen
 		 	   		  
_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:...

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2010-07-06 21:02 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-07-02 15:28 Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
2010-07-02 16:40 ` Benjamin Krajmalnik <kraj@servoyant.com>
2010-07-06 16:30 Re: Question about partitioned query behavior Ranga Gopalan <ranga_gopalan@hotmail.com>
2010-07-06 19:30 ` Robert Haas <robertmhaas@gmail.com>
2010-07-06 20:26 ` Stephen Frost <sfrost@snowman.net>
2010-07-06 21:02   ` Ranga Gopalan <ranga_gopalan@hotmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox