postgresql/src/backend
Tom Lane 608b167f9f Allow user control of CTE materialization, and change the default behavior.
Historically we've always materialized the full output of a CTE query,
treating WITH as an optimization fence (so that, for example, restrictions
from the outer query cannot be pushed into it).  This is appropriate when
the CTE query is INSERT/UPDATE/DELETE, or is recursive; but when the CTE
query is non-recursive and side-effect-free, there's no hazard of changing
the query results by pushing restrictions down.

Another argument for materialization is that it can avoid duplicate
computation of an expensive WITH query --- but that only applies if
the WITH query is called more than once in the outer query.  Even then
it could still be a net loss, if each call has restrictions that
would allow just a small part of the WITH query to be computed.

Hence, let's change the behavior for WITH queries that are non-recursive
and side-effect-free.  By default, we will inline them into the outer
query (removing the optimization fence) if they are called just once.
If they are called more than once, we will keep the old behavior by
default, but the user can override this and force inlining by specifying
NOT MATERIALIZED.  Lastly, the user can force the old behavior by
specifying MATERIALIZED; this would mainly be useful when the query had
deliberately been employing WITH as an optimization fence to prevent a
poor choice of plan.

Andreas Karlsson, Andrew Gierth, David Fetter

Discussion: https://postgr.es/m/87sh48ffhb.fsf@news-spur.riddles.org.uk
2019-02-16 16:11:12 -05:00
..
access Make use of compiler builtins and/or assembly for CLZ, CTZ, POPCNT. 2019-02-15 23:22:33 -05:00
bootstrap Remove argument isprimary from index_build() 2019-01-24 07:57:09 +09:00
catalog Remove useless casts 2019-02-13 11:50:09 +01:00
commands Redesign the partition dependency mechanism. 2019-02-11 14:41:17 -05:00
executor More unconstify use 2019-02-13 11:50:16 +01:00
foreign Update copyright for 2019 2019-01-02 12:44:25 -05:00
jit Renaming for new subscripting mechanism 2019-02-01 12:50:32 -03:00
lib Make use of compiler builtins and/or assembly for CLZ, CTZ, POPCNT. 2019-02-15 23:22:33 -05:00
libpq Get rid of another unconstify through API changes 2019-02-14 20:44:47 +01:00
main Replace @postgresql.org with @lists.postgresql.org for mailinglists 2019-01-19 19:06:35 +01:00
nodes Allow user control of CTE materialization, and change the default behavior. 2019-02-16 16:11:12 -05:00
optimizer Allow user control of CTE materialization, and change the default behavior. 2019-02-16 16:11:12 -05:00
parser Allow user control of CTE materialization, and change the default behavior. 2019-02-16 16:11:12 -05:00
partitioning Refactor planner's header files. 2019-01-29 15:48:51 -05:00
po Translation updates 2018-06-25 12:37:18 +02:00
port Add shared_memory_type GUC. 2019-02-03 12:47:26 +01:00
postmaster Move max_wal_senders out of max_connections for connection slot handling 2019-02-12 10:07:56 +09:00
regex Update copyright for 2019 2019-01-02 12:44:25 -05:00
replication More unconstify use 2019-02-13 11:50:16 +01:00
rewrite Renaming for new subscripting mechanism 2019-02-01 12:50:32 -03:00
snowball Update copyright for 2019 2019-01-02 12:44:25 -05:00
statistics Rename nodes/relation.h to nodes/pathnodes.h. 2019-01-29 16:49:25 -05:00
storage Fix race in dsm_attach() when handles are reused. 2019-02-15 14:05:09 +13:00
tcop More unconstify use 2019-02-13 11:50:16 +01:00
tsearch Update copyright for 2019 2019-01-02 12:44:25 -05:00
utils Allow user control of CTE materialization, and change the default behavior. 2019-02-16 16:11:12 -05:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
Makefile Ensure link commands list *.o files before LDFLAGS. 2019-01-02 13:57:54 -05:00
nls.mk Translation updates 2018-06-25 12:37:18 +02:00