pgsql: Rework planning and execution of UPDATE and DELETE.

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Rework planning and execution of UPDATE and DELETE.
Дата
Msg-id E1lRd9p-00026k-VC@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Rework planning and execution of UPDATE and DELETE.

This patch makes two closely related sets of changes:

1. For UPDATE, the subplan of the ModifyTable node now only delivers
the new values of the changed columns (i.e., the expressions computed
in the query's SET clause) plus row identity information such as CTID.
ModifyTable must re-fetch the original tuple to merge in the old
values of any unchanged columns.  The core advantage of this is that
the changed columns are uniform across all tables of an inherited or
partitioned target relation, whereas the other columns might not be.
A secondary advantage, when the UPDATE involves joins, is that less
data needs to pass through the plan tree.  The disadvantage of course
is an extra fetch of each tuple to be updated.  However, that seems to
be very nearly free in context; even worst-case tests don't show it to
add more than a couple percent to the total query cost.  At some point
it might be interesting to combine the re-fetch with the tuple access
that ModifyTable must do anyway to mark the old tuple dead; but that
would require a good deal of refactoring and it seems it wouldn't buy
all that much, so this patch doesn't attempt it.

2. For inherited UPDATE/DELETE, instead of generating a separate
subplan for each target relation, we now generate a single subplan
that is just exactly like a SELECT's plan, then stick ModifyTable
on top of that.  To let ModifyTable know which target relation a
given incoming row refers to, a tableoid junk column is added to
the row identity information.  This gets rid of the horrid hack
that was inheritance_planner(), eliminating O(N^2) planning cost
and memory consumption in cases where there were many unprunable
target relations.

Point 2 of course requires point 1, so that there is a uniform
definition of the non-junk columns to be returned by the subplan.
We can't insist on uniform definition of the row identity junk
columns however, if we want to keep the ability to have both
plain and foreign tables in a partitioning hierarchy.  Since
it wouldn't scale very far to have every child table have its
own row identity column, this patch includes provisions to merge
similar row identity columns into one column of the subplan result.
In particular, we can merge the whole-row Vars typically used as
row identity by FDWs into one column by pretending they are type
RECORD.  (It's still okay for the actual composite Datums to be
labeled with the table's rowtype OID, though.)

There is more that can be done to file down residual inefficiencies
in this patch, but it seems to be committable now.

FDW authors should note several API changes:

* The argument list for AddForeignUpdateTargets() has changed, and so
has the method it must use for adding junk columns to the query.  Call
add_row_identity_var() instead of manipulating the parse tree directly.
You might want to reconsider exactly what you're adding, too.

* PlanDirectModify() must now work a little harder to find the
ForeignScan plan node; if the foreign table is part of a partitioning
hierarchy then the ForeignScan might not be the direct child of
ModifyTable.  See postgres_fdw for sample code.

* To check whether a relation is a target relation, it's no
longer sufficient to compare its relid to root->parse->resultRelation.
Instead, check it against all_result_relids or leaf_result_relids,
as appropriate.

Amit Langote and Tom Lane

Discussion: https://postgr.es/m/CA+HiwqHpHdqdDn48yCEhynnniahH78rwcrv1rEX65-fsZGBOLQ@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/86dc90056dfdbd9d1b891718d2e5614e3e432f35

Modified Files
--------------
contrib/postgres_fdw/deparse.c                 |  17 +-
contrib/postgres_fdw/expected/postgres_fdw.out | 423 ++++++-------
contrib/postgres_fdw/postgres_fdw.c            | 129 ++--
doc/src/sgml/ddl.sgml                          |   3 +-
doc/src/sgml/fdwhandler.sgml                   |  65 +-
doc/src/sgml/perform.sgml                      |  47 +-
doc/src/sgml/postgres-fdw.sgml                 |   2 +-
src/backend/commands/copyfrom.c                |   1 +
src/backend/commands/explain.c                 |  18 +-
src/backend/commands/trigger.c                 |  18 +-
src/backend/executor/README                    |  12 +-
src/backend/executor/execExpr.c                | 200 +++++++
src/backend/executor/execJunk.c                |  52 +-
src/backend/executor/execMain.c                |   8 +-
src/backend/executor/execPartition.c           |  14 +-
src/backend/executor/nodeModifyTable.c         | 728 +++++++++++++++--------
src/backend/nodes/copyfuncs.c                  |   2 +-
src/backend/nodes/nodeFuncs.c                  |   6 -
src/backend/nodes/outfuncs.c                   |  25 +-
src/backend/nodes/readfuncs.c                  |   2 +-
src/backend/optimizer/path/allpaths.c          |   2 +-
src/backend/optimizer/path/indxpath.c          |   2 +-
src/backend/optimizer/plan/createplan.c        |  87 +--
src/backend/optimizer/plan/planmain.c          |   7 +
src/backend/optimizer/plan/planner.c           | 790 +++++--------------------
src/backend/optimizer/plan/setrefs.c           |  55 +-
src/backend/optimizer/plan/subselect.c         |  11 -
src/backend/optimizer/prep/prepjointree.c      |   5 +-
src/backend/optimizer/prep/preptlist.c         | 134 +++--
src/backend/optimizer/util/appendinfo.c        | 546 ++++++++++++-----
src/backend/optimizer/util/inherit.c           |  44 ++
src/backend/optimizer/util/pathnode.c          |  63 +-
src/backend/optimizer/util/plancat.c           |  19 +-
src/backend/optimizer/util/relnode.c           |  38 +-
src/backend/rewrite/rewriteHandler.c           |  93 +--
src/backend/utils/adt/ruleutils.c              |   6 +-
src/include/executor/executor.h                |  31 +-
src/include/foreign/fdwapi.h                   |   3 +-
src/include/nodes/execnodes.h                  |  55 +-
src/include/nodes/nodes.h                      |   1 +
src/include/nodes/pathnodes.h                  |  84 ++-
src/include/nodes/plannodes.h                  |   8 +-
src/include/nodes/primnodes.h                  |  20 +-
src/include/optimizer/appendinfo.h             |  17 +-
src/include/optimizer/pathnode.h               |   5 +-
src/include/optimizer/prep.h                   |   2 +-
src/include/rewrite/rewriteHandler.h           |   2 -
src/test/regress/expected/inherit.out          |  22 +-
src/test/regress/expected/insert_conflict.out  |   2 +-
src/test/regress/expected/partition_join.out   |  42 +-
src/test/regress/expected/partition_prune.out  | 199 +++----
src/test/regress/expected/rowsecurity.out      | 135 ++---
src/test/regress/expected/updatable_views.out  | 149 ++---
src/test/regress/expected/update.out           |  43 +-
src/test/regress/expected/with.out             |  56 +-
55 files changed, 2352 insertions(+), 2198 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Peter Eisentraut
Дата:
Сообщение: pgsql: Allow an alias to be attached to a JOIN ... USING
Следующее
От: Tomas Vondra
Дата:
Сообщение: Re: pgsql: Extended statistics on expressions