Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
authorAndres Freund <andres@anarazel.de>
Fri, 8 May 2015 03:31:36 +0000 (05:31 +0200)
committerAndres Freund <andres@anarazel.de>
Fri, 8 May 2015 03:43:10 +0000 (05:43 +0200)
commit168d5805e4c08bed7b95d351bf097cff7c07dd65
treecd55bff71bf05324f388d3404c1b3697f3a96e7e
parent2c8f4836db058d0715bc30a30655d646287ba509
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.

The newly added ON CONFLICT clause allows to specify an alternative to
raising a unique or exclusion constraint violation error when inserting.
ON CONFLICT refers to constraints that can either be specified using a
inference clause (by specifying the columns of a unique constraint) or
by naming a unique or exclusion constraint.  DO NOTHING avoids the
constraint violation, without touching the pre-existing row.  DO UPDATE
SET ... [WHERE ...] updates the pre-existing tuple, and has access to
both the tuple proposed for insertion and the existing tuple; the
optional WHERE clause can be used to prevent an update from being
executed.  The UPDATE SET and WHERE clauses have access to the tuple
proposed for insertion using the "magic" EXCLUDED alias, and to the
pre-existing tuple using the table name or its alias.

This feature is often referred to as upsert.

This is implemented using a new infrastructure called "speculative
insertion". It is an optimistic variant of regular insertion that first
does a pre-check for existing tuples and then attempts an insert.  If a
violating tuple was inserted concurrently, the speculatively inserted
tuple is deleted and a new attempt is made.  If the pre-check finds a
matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
If the insertion succeeds without detecting a conflict, the tuple is
deemed inserted.

To handle the possible ambiguity between the excluded alias and a table
named excluded, and for convenience with long relation names, INSERT
INTO now can alias its target table.

Bumps catversion as stored rules change.

Author: Peter Geoghegan, with significant contributions from Heikki
    Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
    Dean Rasheed, Stephen Frost and many others.
122 files changed:
contrib/pg_stat_statements/pg_stat_statements.c
contrib/postgres_fdw/deparse.c
contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/postgres_fdw.c
contrib/postgres_fdw/postgres_fdw.h
contrib/postgres_fdw/sql/postgres_fdw.sql
contrib/test_decoding/expected/ddl.out
contrib/test_decoding/expected/toast.out
contrib/test_decoding/sql/ddl.sql
contrib/test_decoding/sql/toast.sql
doc/src/sgml/fdwhandler.sgml
doc/src/sgml/keywords.sgml
doc/src/sgml/mvcc.sgml
doc/src/sgml/plpgsql.sgml
doc/src/sgml/postgres-fdw.sgml
doc/src/sgml/protocol.sgml
doc/src/sgml/ref/create_policy.sgml
doc/src/sgml/ref/create_rule.sgml
doc/src/sgml/ref/create_table.sgml
doc/src/sgml/ref/create_trigger.sgml
doc/src/sgml/ref/create_view.sgml
doc/src/sgml/ref/insert.sgml
doc/src/sgml/trigger.sgml
src/backend/access/heap/heapam.c
src/backend/access/heap/hio.c
src/backend/access/heap/tuptoaster.c
src/backend/access/nbtree/nbtinsert.c
src/backend/access/rmgrdesc/heapdesc.c
src/backend/catalog/index.c
src/backend/catalog/indexing.c
src/backend/catalog/sql_features.txt
src/backend/commands/constraint.c
src/backend/commands/copy.c
src/backend/commands/explain.c
src/backend/commands/trigger.c
src/backend/executor/execIndexing.c
src/backend/executor/execMain.c
src/backend/executor/nodeLockRows.c
src/backend/executor/nodeModifyTable.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/nodeFuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/plan/planner.c
src/backend/optimizer/plan/setrefs.c
src/backend/optimizer/plan/subselect.c
src/backend/optimizer/prep/prepjointree.c
src/backend/optimizer/prep/preptlist.c
src/backend/optimizer/util/plancat.c
src/backend/parser/analyze.c
src/backend/parser/gram.y
src/backend/parser/parse_clause.c
src/backend/parser/parse_collate.c
src/backend/parser/parse_target.c
src/backend/replication/logical/decode.c
src/backend/replication/logical/reorderbuffer.c
src/backend/rewrite/rewriteHandler.c
src/backend/rewrite/rowsecurity.c
src/backend/storage/lmgr/lmgr.c
src/backend/tcop/pquery.c
src/backend/utils/adt/lockfuncs.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/time/tqual.c
src/bin/psql/common.c
src/include/access/heapam.h
src/include/access/heapam_xlog.h
src/include/access/hio.h
src/include/access/htup_details.h
src/include/catalog/catversion.h
src/include/catalog/index.h
src/include/executor/executor.h
src/include/nodes/execnodes.h
src/include/nodes/nodes.h
src/include/nodes/parsenodes.h
src/include/nodes/plannodes.h
src/include/nodes/primnodes.h
src/include/optimizer/plancat.h
src/include/optimizer/planmain.h
src/include/optimizer/prep.h
src/include/parser/kwlist.h
src/include/parser/parse_clause.h
src/include/replication/reorderbuffer.h
src/include/rewrite/rowsecurity.h
src/include/storage/lmgr.h
src/include/storage/lock.h
src/include/utils/snapshot.h
src/test/isolation/expected/insert-conflict-do-nothing.out [new file with mode: 0644]
src/test/isolation/expected/insert-conflict-do-update-2.out [new file with mode: 0644]
src/test/isolation/expected/insert-conflict-do-update-3.out [new file with mode: 0644]
src/test/isolation/expected/insert-conflict-do-update.out [new file with mode: 0644]
src/test/isolation/isolation_schedule
src/test/isolation/specs/insert-conflict-do-nothing.spec [new file with mode: 0644]
src/test/isolation/specs/insert-conflict-do-update-2.spec [new file with mode: 0644]
src/test/isolation/specs/insert-conflict-do-update-3.spec [new file with mode: 0644]
src/test/isolation/specs/insert-conflict-do-update.spec [new file with mode: 0644]
src/test/regress/expected/errors.out
src/test/regress/expected/insert_conflict.out [new file with mode: 0644]
src/test/regress/expected/privileges.out
src/test/regress/expected/returning.out
src/test/regress/expected/rowsecurity.out
src/test/regress/expected/rules.out
src/test/regress/expected/subselect.out
src/test/regress/expected/triggers.out
src/test/regress/expected/updatable_views.out
src/test/regress/expected/update.out
src/test/regress/expected/with.out
src/test/regress/input/constraints.source
src/test/regress/output/constraints.source
src/test/regress/parallel_schedule
src/test/regress/serial_schedule
src/test/regress/sql/insert_conflict.sql [new file with mode: 0644]
src/test/regress/sql/privileges.sql
src/test/regress/sql/returning.sql
src/test/regress/sql/rowsecurity.sql
src/test/regress/sql/rules.sql
src/test/regress/sql/subselect.sql
src/test/regress/sql/triggers.sql
src/test/regress/sql/updatable_views.sql
src/test/regress/sql/update.sql
src/test/regress/sql/with.sql