Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VgGes-0003Gf-Ov for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Nov 2013 16:17:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VgGes-0004l8-8H for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Nov 2013 16:17:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VgFpa-0003cA-FE for pgsql-bugs@postgresql.org; Tue, 12 Nov 2013 15:24:42 +0000 Received: from ir1.cms.hu-berlin.de ([141.20.1.146]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VgFpW-0004dV-Qd for pgsql-bugs@postgresql.org; Tue, 12 Nov 2013 15:24:41 +0000 X-IronPort-AV: E=Sophos;i="4.93,685,1378850400"; d="sql'?sig'?scan'208";a="30312497" Received: from unknown (HELO [141.20.144.94]) ([141.20.144.94]) by ir1-relay.cms.hu-berlin.de with ESMTP/TLS/DHE-RSA-CAMELLIA256-SHA; 12 Nov 2013 16:24:37 +0100 Message-ID: <52824835.70300@hu-berlin.de> Date: Tue, 12 Nov 2013 16:24:37 +0100 From: Thomas Krause User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Wrong estimation of unique values of a table when using partitions [PostgreSQL 9.3.1] X-Enigmail-Version: 1.6 Content-Type: multipart/mixed; boundary="------------030902080103090500060201" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------030902080103090500060201 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 8bit -----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----- --------------030902080103090500060201 Content-Type: text/x-sql; name="inheritance_row_estimation.sql" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="inheritance_row_estimation.sql" 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; --------------030902080103090500060201 Content-Type: application/pgp-signature; name="inheritance_row_estimation.sql.sig" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="inheritance_row_estimation.sql.sig" iQEcBAABAgAGBQJSgkg1AAoJEP4mVMq7eD64XlYH/38K4K/OelDDpLiHUQg7 hAgm3VfDoM3hFLh6T8Oz7j+YH7N7FKGfYZo563c24JO6FjvcCwUD4wkthjrq IAmEwQqv+wkIOTulMtfjdL1Jp4P3DRPJ7mdPF4Yf04Kn1+Z6FA2XXw8i21ix sPUgqHSuWw96K6/aSjyRBkSgP5HJ5GFdU0gAxoIu3OhDiiJ7R7b5I903RTpR Hrf74waE+7ms3Ps3C5GhW+mN8BBm4ktZComV66Uet00FKovsMaoUaT7Skxg6 P6t7Cj8dX7bqVEvb4BbLki9tH6v6kmc/u+5/y9X7+VSZH0FCMQeYVu9sSjnF Q2MPZS64IkeJca82mPIp8OI= --------------030902080103090500060201 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------030902080103090500060201--