Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 07DB02E00C7 for ; Tue, 4 Dec 2007 16:27:26 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 72081-03 for ; Tue, 4 Dec 2007 16:27:22 -0400 (AST) Received: from io.link-m.de (io.link-m.de [89.250.128.34]) by postgresql.org (Postfix) with ESMTP id 783C12E0035 for ; Tue, 4 Dec 2007 16:27:22 -0400 (AST) Received: from cl-40.muc-02.de.sixxs.net (cl-40.muc-02.de.sixxs.net [2001:a60:f000:27::2]) (AUTH: CRAM-MD5 julian@mehnle.net, TLS: TLSv1/SSLv3, 256bits, AES256-SHA) by io.link-m.de with esmtp; Tue, 04 Dec 2007 20:27:04 +0000 id 0008C029.4755B818.0000655B From: Julian Mehnle To: pgsql-performance@postgresql.org Subject: Re: Bad query plans for queries on partitioned table Date: Tue, 4 Dec 2007 20:27:01 +0000 User-Agent: KMail/1.9.7 References: <200712041944.13543.julian@mehnle.net> <87ir3e6wti.fsf@oxford.xeocode.com> In-Reply-To: <87ir3e6wti.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart3585935.L4lJuYgS1L"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200712042027.01921.julian@mehnle.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/58 X-Sequence-Number: 27874 --nextPart3585935.L4lJuYgS1L Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Gregory Stark wrote: > There are a few things going on here. > > 1) The optimizer can't build a plan which ignores those partitions > because the statistics are just approximations. You could insert into > one of them at any time and the statistics won't update immediately. If > you have a partition which is empty of some type of data you can put a > constraint on it to promise the optimizer that that condition will stay > true. I actually do have constraints on all the partitions, e.g. for week 34: Check constraints [for email_2007_week34]: "email_2007_week34_ts_check" CHECK (ts >= '2007-08-20 00:00:00'::timestamp without time zone AND ts < '2007-08-27 00:00:00'::timestamp without time zone) Check constraints [for email_2007_week34_extras]: "email_2007_week34_extras_ts_check" CHECK (ts >= '2007-08-20 00:00:00'::timestamp without time zone AND ts < '2007-08-27 00:00:00'::timestamp without time zone) Shouldn't this be enough to give the query planner a clue that it only has to join the "email" and "email_extras" tables' partitions pair-wise, as opposed to cross-joining them? For the record, I also have indexes: Indexes [for email_2007_week34]: "email_2007_week34_pkey" PRIMARY KEY, btree (id) "index_email_2007_week34_on_helo" btree (helo) "index_email_2007_week34_on_ip" btree (ip) "index_email_2007_week34_on_load_id" btree (load_id) "index_email_2007_week34_on_mfrom" btree (mfrom) "index_email_2007_week34_on_ts" btree (ts) Indexes [for for email_2007_week34_extras]: "email_2007_week34_extras_pkey" PRIMARY KEY, btree (id) > 2) The optimizer is assuming that empty tables have a default 1,000 > records in them with no idea about their statistics. Otherwise you get > terrible plans on tables which have just been created or never > analyzed. In this case that's causing it to think there will be tons of > matches on what is apparently a very selective criterion. I see. But this shouldn't matter under the assumption that constraint exclusion works correctly, right? > 3) The optimizer is a bit dumb about partitioned tables. But I'm not > sure if that's actually the fault here. > > Try adding one record of data to each of those partitions or putting a > constraint on them which will allow constraint_exclusion (I assume you > have that enabled?) to kick in. You'll still be bitten by the parent > table but hopefully that's not enough to cause a problem. The parent table is empty. How will adding one record to each of the partitions make a difference given the above constraints? Julian. --nextPart3585935.L4lJuYgS1L Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQBHVbgVwL7PKlBZWjsRAq1cAKDyZBznf5cf+4WB3lEMu34L2Vux4QCgxAC8 K/Ogo4blabHIYwufUnYOUO4= =a2Rx -----END PGP SIGNATURE----- --nextPart3585935.L4lJuYgS1L--