pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Krause <krauseto@hu-berlin.de>
To: pgsql-bugs@postgresql.org
Subject: Wrong estimation of unique values of a table when using partitions [PostgreSQL 9.3.1]
Date: Tue, 12 Nov 2013 16:24:37 +0100
Message-ID: <52824835.70300@hu-berlin.de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear PostgreSQL developers,

I'm not sure if this is actually a bug or just a possible enhancement
to PostgreSQL.

In the example I've attached there are two child tables which inherit
from a common parent table. The parent table has an integer column
"v". Both are filled with random values, the first child with values
from 0-500 and the second one with 1000-1499. There is a second column
"p" that defines an exclusive partition of the children, thus each
child table is uniquely defined by the value of the p column.

When you query the distinct values for "v" for the first child table
directly the query planner guesses that there will be 500 rows (which
is already the precise answer). If you execute the same query on the
parent table instead, but restrict the "p" value to "1" (so that
PostgreSQL will use constraint exclusion and only scans the empty
parent table and the first child table) it will assume that there will
be 1000 different values in the result. Since constraint exclusion
already figured out that only one child table will be used and that
the parent table has no rows it could correctly guess the unique
number of values for the child table (and thus the complete query) as
well.

I run into this problem since we are using partitioned tables in our
production database with many different child tables and some queries
did not succeed to execute in a certain time limit in production. They
worked well on the development machines, since only a few child tables
exist there and the planner made better join selection choices on the
development installations.

Best,

Thomas

- -- 
Thomas Krause
Mitarbeiter Korpuslinguistik und Morphologie
Humboldt-Universität zu Berlin

Telefon: +49 (0)30 2093 9720
Raum 3.408, Dorotheenstraße 24, 10117 Berlin - Mitte
http://u.hu-berlin.de/korpling-thomaskrause
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSgkg1AAoJEP4mVMq7eD64OhoH/1QDGoiVDEWLPxFBSoH3ZjhA
yfG5yTSfHoNwXWgDp2JU691UdgEY305cCs84rJ5OundF1sgLMs6+l/MCcdPBxrbZ
E5i49zT+j69Hl/ApSvRnHjBl6UKWLuE795zhbn3b4XfTHNJ51Y4as3FkpTMcTpsP
kmmC9lBYjraGhGxeU2hEYT3DLOuZb9LhbTwSl4T7yVuggPipsKlKcmDQi74uQSRx
mv/XJtHmoanx9rCdSeuHpYOn0MWl1wAdG3cD1D9PvcMtDkYGimKcomvhdN8RX/F6
YMRWSQ2+mF2iTliJdeVuukNqaAjM+wBXisgd0icsjKqvulpQAMG54RoA8rFSYmg=
=bLyO
-----END PGP SIGNATURE-----

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

inheritance_row_estimation.sql (text/x-sql, 846B) download DROP TABLE IF EXISTS cest_child1; DROP TABLE IF EXISTS cest_child2; DROP TABLE IF EXISTS cest; create table cest ( v integer, p integer ); CREATE TABLE cest_child1 ( CHECK(p = 1) ) INHERITS (cest); INSERT INTO cest_child1 ( SELECT round((random()*499)) AS v, 1 AS p FROM generate_series(1,10000) ); CREATE TABLE cest_child2 ( CHECK(p = 2)) INHERITS (cest) ; INSERT INTO cest_child2 ( SELECT round((random()*499)+1000) AS v, 2 AS p FROM generate_series(1,10000) ); ANALYZE cest_child1; ANALYZE cest_child2; ANALYZE cest; -- Correctly guessing 500 different values for the child1 table EXPLAIN ANALYZE SELECT DISTINCT v FROM cest_child1 WHERE p = 1; -- Assuming 1000 different values even if the select effictvly only uses the single child1 table (and the empty child table) EXPLAIN ANALYZE SELECT DISTINCT v FROM cest WHERE p = 1;
inheritance_row_estimation.sql.sig (application/pgp-signature, 287B) download

Message-ID: <52824835.70300@hu-berlin.de>
Permalink:  ../52824835.70300@hu-berlin.de/
Also on:    postgresql.org/message-id/52824835.70300@hu-berlin.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: krauseto@hu-berlin.de
  Subject: Re: Wrong estimation of unique values of a table when using partitions [PostgreSQL 9.3.1]
  In-Reply-To: <52824835.70300@hu-berlin.de>
  References: <52824835.70300@hu-berlin.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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