pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Rules documentation example
2+ messages / 2 participants
[nested] [flat]

Commits from this thread:
  2019-11-11 b6423e92ab b75667d84f 02f7b7ab75 b3c762a45a 750d72fde6 18831346d4 84aad60b9c  Doc: fix ancient mistake, or at least obsolete info, in rules example.
* Rules documentation example
@ 2019-11-11 18:03  Paul A Jungwirth <pj@illuminatedcomputing.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Paul A Jungwirth @ 2019-11-11 18:03 UTC (permalink / raw)
  To: pgsql-general

Hello,

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 “pulled up” into the middle SELECT since there's no need to process them separately. But the middle SELECT will remain separate from the top, because it contains aggregate functions. If we pulled those up it would change the behavior of the topmost SELECT, which we don't want.

But I don't see an aggregate function. Is it referring to MIN? But
that is this two-param version defined on the same page. It isn't an
aggregate:

CREATE FUNCTION min(integer, integer) RETURNS integer AS $$
    SELECT CASE WHEN $1 < $2 THEN $1 ELSE $2 END
$$ LANGUAGE SQL STRICT;

Is that an error in the docs, or am I missing something?

Does a non-aggregate function also prevent the subqueries from being
pulled up? Will all levels of that query actually get combined, or
does something else prevent it?

Thanks,
Paul





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Rules documentation example
@ 2019-11-11 18:38  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Paul A Jungwirth <pj@illuminatedcomputing.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2019-11-11 18:38 UTC (permalink / raw)
  To: Paul A Jungwirth <pj@illuminatedcomputing.com>; +Cc: pgsql-general

Paul A Jungwirth <pj@illuminatedcomputing.com> 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 “pulled up” into the middle SELECT since there's no need to process them separately. But the middle SELECT will remain separate from the top, because it contains aggregate functions. If we pulled those up it would change the behavior 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





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2019-11-11 18:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-11-11 18:03 Rules documentation example Paul A Jungwirth <pj@illuminatedcomputing.com>
2019-11-11 18:38 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox