Permalink
4 comments
on commit
sign in to comment.
Browse files
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.- Loading branch information...
Showing
with
6,106 additions
and 435 deletions.
- +25 −0 contrib/pg_stat_statements/pg_stat_statements.c
- +5 −2 contrib/postgres_fdw/deparse.c
- +5 −0 contrib/postgres_fdw/expected/postgres_fdw.out
- +14 −1 contrib/postgres_fdw/postgres_fdw.c
- +1 −1 contrib/postgres_fdw/postgres_fdw.h
- +3 −0 contrib/postgres_fdw/sql/postgres_fdw.sql
- +34 −0 contrib/test_decoding/expected/ddl.out
- +8 −1 contrib/test_decoding/expected/toast.out
- +22 −0 contrib/test_decoding/sql/ddl.sql
- +5 −0 contrib/test_decoding/sql/toast.sql
- +7 −0 doc/src/sgml/fdwhandler.sgml
- +7 −0 doc/src/sgml/keywords.sgml
- +21 −2 doc/src/sgml/mvcc.sgml
- +10 −4 doc/src/sgml/plpgsql.sgml
- +8 −0 doc/src/sgml/postgres-fdw.sgml
- +10 −3 doc/src/sgml/protocol.sgml
- +44 −19 doc/src/sgml/ref/create_policy.sgml
- +5 −1 doc/src/sgml/ref/create_rule.sgml
- +3 −1 doc/src/sgml/ref/create_table.sgml
- +4 −1 doc/src/sgml/ref/create_trigger.sgml
- +6 −3 doc/src/sgml/ref/create_view.sgml
- +385 −18 doc/src/sgml/ref/insert.sgml
- +40 −8 doc/src/sgml/trigger.sgml
- +338 −39 src/backend/access/heap/heapam.c
- +20 −7 src/backend/access/heap/hio.c
- +8 −0 src/backend/access/heap/tuptoaster.c
- +22 −6 src/backend/access/nbtree/nbtinsert.c
- +9 −0 src/backend/access/rmgrdesc/heapdesc.c
- +52 −1 src/backend/catalog/index.c
- +1 −1 src/backend/catalog/indexing.c
- +1 −1 src/backend/catalog/sql_features.txt
- +1 −1 src/backend/commands/constraint.c
- +4 −3 src/backend/commands/copy.c
- +64 −6 src/backend/commands/explain.c
- +6 −13 src/backend/commands/trigger.c
- +364 −53 src/backend/executor/execIndexing.c
- +46 −7 src/backend/executor/execMain.c
- +8 −4 src/backend/executor/nodeLockRows.c
- +441 −18 src/backend/executor/nodeModifyTable.c
- +84 −0 src/backend/nodes/copyfuncs.c
- +62 −0 src/backend/nodes/equalfuncs.c
- +87 −0 src/backend/nodes/nodeFuncs.c
- +40 −1 src/backend/nodes/outfuncs.c
- +40 −0 src/backend/nodes/readfuncs.c
- +25 −1 src/backend/optimizer/plan/createplan.c
- +27 −0 src/backend/optimizer/plan/planner.c
- +43 −9 src/backend/optimizer/plan/setrefs.c
- +4 −0 src/backend/optimizer/plan/subselect.c
- +6 −0 src/backend/optimizer/prep/prepjointree.c
- +13 −0 src/backend/optimizer/prep/preptlist.c
- +352 −0 src/backend/optimizer/util/plancat.c
- +129 −20 src/backend/parser/analyze.c
- +107 −14 src/backend/parser/gram.y
- +203 −0 src/backend/parser/parse_clause.c
- +2 −0 src/backend/parser/parse_collate.c
- +6 −5 src/backend/parser/parse_target.c
- +59 −7 src/backend/replication/logical/decode.c
- +113 −46 src/backend/replication/logical/reorderbuffer.c
- +72 −15 src/backend/rewrite/rewriteHandler.c
- +68 −14 src/backend/rewrite/rowsecurity.c
- +91 −0 src/backend/storage/lmgr/lmgr.c
- +14 −3 src/backend/tcop/pquery.c
- +1 −0 src/backend/utils/adt/lockfuncs.c
- +80 −28 src/backend/utils/adt/ruleutils.c
- +27 −2 src/backend/utils/time/tqual.c
- +4 −1 src/bin/psql/common.c
- +3 −0 src/include/access/heapam.h
- +41 −13 src/include/access/heapam_xlog.h
- +1 −1 src/include/access/hio.h
- +35 −1 src/include/access/htup_details.h
- +1 −1 src/include/catalog/catversion.h
- +2 −0 src/include/catalog/index.h
- +8 −5 src/include/executor/executor.h
- +15 −0 src/include/nodes/execnodes.h
- +17 −0 src/include/nodes/nodes.h
- +40 −5 src/include/nodes/parsenodes.h
- +8 −0 src/include/nodes/plannodes.h
- +42 −0 src/include/nodes/primnodes.h
- +2 −0 src/include/optimizer/plancat.h
- +1 −1 src/include/optimizer/planmain.h
- +3 −0 src/include/optimizer/prep.h
- +1 −0 src/include/parser/kwlist.h
- +4 −0 src/include/parser/parse_clause.h
- +8 −1 src/include/replication/reorderbuffer.h
- +2 −1 src/include/rewrite/rowsecurity.h
- +5 −0 src/include/storage/lmgr.h
- +10 −0 src/include/storage/lock.h
- +16 −6 src/include/utils/snapshot.h
- +23 −0 src/test/isolation/expected/insert-conflict-do-nothing.out
- +23 −0 src/test/isolation/expected/insert-conflict-do-update-2.out
- +26 −0 src/test/isolation/expected/insert-conflict-do-update-3.out
- +23 −0 src/test/isolation/expected/insert-conflict-do-update.out
- +4 −0 src/test/isolation/isolation_schedule
- +41 −0 src/test/isolation/specs/insert-conflict-do-nothing.spec
- +41 −0 src/test/isolation/specs/insert-conflict-do-update-2.spec
- +69 −0 src/test/isolation/specs/insert-conflict-do-update-3.spec
- +40 −0 src/test/isolation/specs/insert-conflict-do-update.spec
- +3 −1 src/test/regress/expected/errors.out
- +476 −0 src/test/regress/expected/insert_conflict.out
- +28 −1 src/test/regress/expected/privileges.out
- +24 −0 src/test/regress/expected/returning.out
- +132 −0 src/test/regress/expected/rowsecurity.out
- +90 −0 src/test/regress/expected/rules.out
- +22 −0 src/test/regress/expected/subselect.out
- +101 −1 src/test/regress/expected/triggers.out
- +61 −0 src/test/regress/expected/updatable_views.out
- +34 −0 src/test/regress/expected/update.out
- +82 −0 src/test/regress/expected/with.out
- +12 −0 src/test/regress/input/constraints.source
- +20 −4 src/test/regress/output/constraints.source
- +1 −0 src/test/regress/parallel_schedule
- +1 −0 src/test/regress/serial_schedule
- +284 −0 src/test/regress/sql/insert_conflict.sql
- +18 −1 src/test/regress/sql/privileges.sql
- +6 −0 src/test/regress/sql/returning.sql
- +112 −0 src/test/regress/sql/rowsecurity.sql
- +59 −0 src/test/regress/sql/rules.sql
- +14 −0 src/test/regress/sql/subselect.sql
- +68 −1 src/test/regress/sql/triggers.sql
- +9 −0 src/test/regress/sql/updatable_views.sql
- +21 −0 src/test/regress/sql/update.sql
- +57 −0 src/test/regress/sql/with.sql
Oops, something went wrong.
This comment has been minimized.
billschallerMay 8, 2015
billschaller repliedMay 8, 2015
This comment has been minimized.
applegaoJun 30, 2015
how can I get the patch??? I want to get this patch,please help me !
applegao repliedJun 30, 2015
how can I get the patch??? I want to get this patch,please help me !
This comment has been minimized.
kotsachinJul 2, 2015
applegao: you can get patch from here:
http://git.postgresql.org/gitweb/?p=postgresql.git;a=patch;h=168d5805e4c08bed7b95d351bf097cff7c07dd65
kotsachin repliedJul 2, 2015
applegao: you can get patch from here:
http://git.postgresql.org/gitweb/?p=postgresql.git;a=patch;h=168d5805e4c08bed7b95d351bf097cff7c07dd65
This comment has been minimized.
TiggsApr 23, 2016
Tiggs repliedApr 23, 2016