Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 123D2B5DD56 for ; Sun, 4 Sep 2011 12:18:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 11207-06 for ; Sun, 4 Sep 2011 15:18:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C44F5B5DD52 for ; Sun, 4 Sep 2011 12:18:27 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p84FIJg8012018; Sun, 4 Sep 2011 11:18:19 -0400 (EDT) To: "Kevin Grittner" cc: heikki.linnakangas@enterprisedb.com, Jayadevan.Maymala@ibsplc.com, pgsql-performance@postgresql.org, pgsql-performance-owner@postgresql.org Subject: Re: Query performance issue In-reply-to: <4E6345520200002500040BD6@gw.wicourts.gov> References: <4E6345520200002500040BD6@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Sun, 04 Sep 2011 09:30:58 -0500" Date: Sun, 04 Sep 2011 11:18:19 -0400 Message-ID: <12017.1315149499@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/19 X-Sequence-Number: 44795 "Kevin Grittner" writes: > Thanks for posting the query and related schema. I tried working > through it, but I keep coming back to this sort, and wondering how a > sort can have 1121 rows as input and 2673340321 rows as output. Does > anyone have any ideas on what could cause that? Mergejoin rescan. There really are only 1121 rows in the data, but the parent merge join is pulling them over and over again --- evidently there are a lot of equal keys in the data. The EXPLAIN ANALYZE machinery counts each fetch as a new row, even after a mark/restore. The planner does know about that effect and will penalize merge joins when it realizes there are a lot of duplicate keys in the input. In this case I'm thinking that the drastic underestimate of the size of the other side of the join results in not penalizing the merge enough. (On the other hand, hash joins don't like equal keys that much either...) regards, tom lane