Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1wHq-0002Ox-A4 for pgsql-general@arkaria.postgresql.org; Sat, 01 Aug 2020 18:31:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k1wHp-0004Tl-9O for pgsql-general@arkaria.postgresql.org; Sat, 01 Aug 2020 18:31:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1wHp-0004Te-1p for pgsql-general@lists.postgresql.org; Sat, 01 Aug 2020 18:31:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1wHn-0006Zi-7T for pgsql-general@lists.postgresql.org; Sat, 01 Aug 2020 18:31:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 071IV9GF1934925; Sat, 1 Aug 2020 14:31:09 -0400 From: Tom Lane To: Zack Weinberg cc: pgsql-general@lists.postgresql.org Subject: Re: Apparent missed query optimization with self-join and inner grouping In-reply-to: References: Comments: In-reply-to Zack Weinberg message dated "Fri, 31 Jul 2020 11:07:39 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1934923.1596306669.1@sss.pgh.pa.us> Date: Sat, 01 Aug 2020 14:31:09 -0400 Message-ID: <1934924.1596306669@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Zack Weinberg writes: > For each (experiment_id, url_id) pair for some small subset of the > experiment_ids, I need to query the full_url_id corresponding to the > *largest* value of redirect_num. The query planner does something > reasonable with this SELECT: > => explain (analyze, verbose) > select b.experiment_id, b.url_id, b.full_url_id > from blockpage b, > (select experiment_id, url_id, max(redirect_num) as redirect_num > from blockpage group by experiment_id, url_id) bm > where b.experiment_id = bm.experiment_id > and b.url_id = bm.url_id > and b.redirect_num = bm.redirect_num > and bm.experiment_id in (16, 43); With that query, the "bm.experiment_id in (16, 43)" restriction is pushed into the "bm" sub-select, greatly reducing the amount of work the GROUP BY step has to do. > But if I change the final part of the WHERE to reference > b.experiment_id instead of bm.experiment_id, I get this much more > expensive query plan: > => explain (analyze, verbose) > select b.experiment_id, b.url_id, b.full_url_id > from blockpage b, > (select experiment_id, url_id, max(redirect_num) as redirect_num > from blockpage group by experiment_id, url_id) bm > where b.experiment_id = bm.experiment_id > and b.url_id = bm.url_id > and b.redirect_num = bm.redirect_num > and b.experiment_id in (16, 43); With that query, the GROUP BY is evaluated in full, and it costs you. In principle, given the nearby "where b.experiment_id = bm.experiment_id" clause, we could derive "bm.experiment_id in (16, 43)" from the stated clause. But we don't. The existing machinery around derivation of implied equalities only works for simple equalities, not OR clauses. Extending that would be a bit of a research project, and it's far from clear that the benefits would be worth the additional planning costs. > What is the best way to report this to the developers? Should I file > a bug report? I'm using Postgres 12.2. This is not a bug, and you should not hold your breath waiting for it to change. regards, tom lane