Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUEaa-0000Ka-NX for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2019 18:39:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iUEaY-0000HV-9I for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2019 18:38:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUEaX-0000HN-Sd for pgsql-general@lists.postgresql.org; Mon, 11 Nov 2019 18:38:58 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iUEaR-00047y-76 for pgsql-general@postgresql.org; Mon, 11 Nov 2019 18:38:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xABIcnpp014491; Mon, 11 Nov 2019 13:38:49 -0500 From: Tom Lane To: Paul A Jungwirth cc: pgsql Subject: Re: Rules documentation example In-reply-to: References: Comments: In-reply-to Paul A Jungwirth message dated "Mon, 11 Nov 2019 10:03:13 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <14489.1573497529.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 11 Nov 2019 13:38:49 -0500 Message-ID: <14490.1573497529@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Paul A Jungwirth writes: > I'm reading the docs about the Postgres Rule system here: > https://www.postgresql.org/docs/12/rules-views.html > That page says: >> It turns out that the planner will collapse this tree into a two-level = query tree: the bottommost SELECT commands will be =E2=80=9Cpulled up=E2=80= =9D into the middle SELECT since there's no need to process them separatel= y. But the middle SELECT will remain separate from the top, because it con= tains aggregate functions. If we pulled those up it would change the behav= ior of the topmost SELECT, which we don't want. > But I don't see an aggregate function. Is it referring to MIN? Perhaps. Digging in the git history, that text seems to be mine (commit 1045304a3), but the example that it's talking about was pre-existing. I think I might've just misread it. It's also likely (assuming that I was documenting a behavior that I actually saw at the time) that the real issue is that MIN(), as presented, defaults to being volatile which would also prevent such flattening. But this example is so old that I'm not sure whether that particular optimization behavior existed then. I'm inclined to: (1) get rid of the example's MIN() function in favor of using LEAST(), which is standard and less confusing; (2) change the text to just say that the planner flattens these subqueries, so we don't pay any execution-time penalty from the way the view replacements are handled. regards, tom lane