mirror of
https://github.com/postgres/postgres.git
synced 2025-09-03 15:22:11 +03:00
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.
This commit is contained in:
@@ -96,6 +96,15 @@
|
||||
* unrelated tuple stored into a slot recently freed by VACUUM. If either
|
||||
* check fails, one may assume that there is no live descendant version.
|
||||
*
|
||||
* t_ctid is sometimes used to store a speculative insertion token, instead
|
||||
* of a real TID. A speculative token is set on a tuple that's being
|
||||
* inserted, until the inserter is sure that it wants to go ahead with the
|
||||
* insertion. Hence a token should only be seen on a tuple with an XMAX
|
||||
* that's still in-progress, or invalid/aborted. The token is replaced with
|
||||
* the tuple's real TID when the insertion is confirmed. One should never
|
||||
* see a speculative insertion token while following a chain of t_ctid links,
|
||||
* because they are not used on updates, only insertions.
|
||||
*
|
||||
* Following the fixed header fields, the nulls bitmap is stored (beginning
|
||||
* at t_bits). The bitmap is *not* stored if t_infomask shows that there
|
||||
* are no nulls in the tuple. If an OID field is present (as indicated by
|
||||
@@ -138,7 +147,8 @@ struct HeapTupleHeaderData
|
||||
DatumTupleFields t_datum;
|
||||
} t_choice;
|
||||
|
||||
ItemPointerData t_ctid; /* current TID of this or newer tuple */
|
||||
ItemPointerData t_ctid; /* current TID of this or newer tuple (or a
|
||||
* speculative insertion token) */
|
||||
|
||||
/* Fields below here must match MinimalTupleData! */
|
||||
|
||||
@@ -241,6 +251,14 @@ struct HeapTupleHeaderData
|
||||
*/
|
||||
#define HEAP_TUPLE_HAS_MATCH HEAP_ONLY_TUPLE /* tuple has a join match */
|
||||
|
||||
/*
|
||||
* Special value used in t_ctid.ip_posid, to indicate that it holds a
|
||||
* speculative insertion token rather than a real TID. This must be higher
|
||||
* than MaxOffsetNumber, so that it can be distinguished from a valid
|
||||
* offset number in a regular item pointer.
|
||||
*/
|
||||
#define SpecTokenOffsetNumber 0xfffe
|
||||
|
||||
/*
|
||||
* HeapTupleHeader accessor macros
|
||||
*
|
||||
@@ -377,6 +395,22 @@ do { \
|
||||
(tup)->t_choice.t_heap.t_field3.t_xvac = (xid); \
|
||||
} while (0)
|
||||
|
||||
#define HeapTupleHeaderIsSpeculative(tup) \
|
||||
( \
|
||||
(tup)->t_ctid.ip_posid == SpecTokenOffsetNumber \
|
||||
)
|
||||
|
||||
#define HeapTupleHeaderGetSpeculativeToken(tup) \
|
||||
( \
|
||||
AssertMacro(HeapTupleHeaderIsSpeculative(tup)), \
|
||||
ItemPointerGetBlockNumber(&(tup)->t_ctid) \
|
||||
)
|
||||
|
||||
#define HeapTupleHeaderSetSpeculativeToken(tup, token) \
|
||||
( \
|
||||
ItemPointerSet(&(tup)->t_ctid, token, SpecTokenOffsetNumber) \
|
||||
)
|
||||
|
||||
#define HeapTupleHeaderGetDatumLength(tup) \
|
||||
VARSIZE(tup)
|
||||
|
||||
|
Reference in New Issue
Block a user