mirror of
https://github.com/postgres/postgres.git
synced 2025-10-24 01:29:19 +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:
@@ -28,6 +28,7 @@
|
||||
#define HEAP_INSERT_SKIP_WAL 0x0001
|
||||
#define HEAP_INSERT_SKIP_FSM 0x0002
|
||||
#define HEAP_INSERT_FROZEN 0x0004
|
||||
#define HEAP_INSERT_SPECULATIVE 0x0008
|
||||
|
||||
typedef struct BulkInsertStateData *BulkInsertState;
|
||||
|
||||
@@ -142,6 +143,8 @@ extern void heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
|
||||
extern HTSU_Result heap_delete(Relation relation, ItemPointer tid,
|
||||
CommandId cid, Snapshot crosscheck, bool wait,
|
||||
HeapUpdateFailureData *hufd);
|
||||
extern void heap_finish_speculative(Relation relation, HeapTuple tuple);
|
||||
extern void heap_abort_speculative(Relation relation, HeapTuple tuple);
|
||||
extern HTSU_Result heap_update(Relation relation, ItemPointer otid,
|
||||
HeapTuple newtup,
|
||||
CommandId cid, Snapshot crosscheck, bool wait,
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
#define XLOG_HEAP_UPDATE 0x20
|
||||
/* 0x030 is free, was XLOG_HEAP_MOVE */
|
||||
#define XLOG_HEAP_HOT_UPDATE 0x40
|
||||
/* 0x050 is free, was XLOG_HEAP_NEWPAGE */
|
||||
#define XLOG_HEAP_CONFIRM 0x50
|
||||
#define XLOG_HEAP_LOCK 0x60
|
||||
#define XLOG_HEAP_INPLACE 0x70
|
||||
|
||||
@@ -60,23 +60,43 @@
|
||||
#define XLOG_HEAP2_NEW_CID 0x70
|
||||
|
||||
/*
|
||||
* xl_heap_* ->flag values, 8 bits are available.
|
||||
* xl_heap_insert/xl_heap_multi_insert flag values, 8 bits are available.
|
||||
*/
|
||||
/* PD_ALL_VISIBLE was cleared */
|
||||
#define XLOG_HEAP_ALL_VISIBLE_CLEARED (1<<0)
|
||||
#define XLH_INSERT_ALL_VISIBLE_CLEARED (1<<0)
|
||||
#define XLH_INSERT_LAST_IN_MULTI (1<<1)
|
||||
#define XLH_INSERT_IS_SPECULATIVE (1<<2)
|
||||
#define XLH_INSERT_CONTAINS_NEW_TUPLE (1<<3)
|
||||
|
||||
/*
|
||||
* xl_heap_update flag values, 8 bits are available.
|
||||
*/
|
||||
/* PD_ALL_VISIBLE was cleared */
|
||||
#define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED (1<<0)
|
||||
/* PD_ALL_VISIBLE was cleared in the 2nd page */
|
||||
#define XLOG_HEAP_NEW_ALL_VISIBLE_CLEARED (1<<1)
|
||||
#define XLOG_HEAP_CONTAINS_OLD_TUPLE (1<<2)
|
||||
#define XLOG_HEAP_CONTAINS_OLD_KEY (1<<3)
|
||||
#define XLOG_HEAP_CONTAINS_NEW_TUPLE (1<<4)
|
||||
#define XLOG_HEAP_PREFIX_FROM_OLD (1<<5)
|
||||
#define XLOG_HEAP_SUFFIX_FROM_OLD (1<<6)
|
||||
/* last xl_heap_multi_insert record for one heap_multi_insert() call */
|
||||
#define XLOG_HEAP_LAST_MULTI_INSERT (1<<7)
|
||||
#define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED (1<<1)
|
||||
#define XLH_UPDATE_CONTAINS_OLD_TUPLE (1<<2)
|
||||
#define XLH_UPDATE_CONTAINS_OLD_KEY (1<<3)
|
||||
#define XLH_UPDATE_CONTAINS_NEW_TUPLE (1<<4)
|
||||
#define XLH_UPDATE_PREFIX_FROM_OLD (1<<5)
|
||||
#define XLH_UPDATE_SUFFIX_FROM_OLD (1<<6)
|
||||
|
||||
/* convenience macro for checking whether any form of old tuple was logged */
|
||||
#define XLOG_HEAP_CONTAINS_OLD \
|
||||
(XLOG_HEAP_CONTAINS_OLD_TUPLE | XLOG_HEAP_CONTAINS_OLD_KEY)
|
||||
#define XLH_UPDATE_CONTAINS_OLD \
|
||||
(XLH_UPDATE_CONTAINS_OLD_TUPLE | XLH_UPDATE_CONTAINS_OLD_KEY)
|
||||
|
||||
/*
|
||||
* xl_heap_delete flag values, 8 bits are available.
|
||||
*/
|
||||
/* PD_ALL_VISIBLE was cleared */
|
||||
#define XLH_DELETE_ALL_VISIBLE_CLEARED (1<<0)
|
||||
#define XLH_DELETE_CONTAINS_OLD_TUPLE (1<<1)
|
||||
#define XLH_DELETE_CONTAINS_OLD_KEY (1<<2)
|
||||
#define XLH_DELETE_IS_SUPER (1<<3)
|
||||
|
||||
/* convenience macro for checking whether any form of old tuple was logged */
|
||||
#define XLH_DELETE_CONTAINS_OLD \
|
||||
(XLH_DELETE_CONTAINS_OLD_TUPLE | XLH_DELETE_CONTAINS_OLD_KEY)
|
||||
|
||||
/* This is what we need to know about delete */
|
||||
typedef struct xl_heap_delete
|
||||
@@ -243,6 +263,14 @@ typedef struct xl_heap_lock_updated
|
||||
|
||||
#define SizeOfHeapLockUpdated (offsetof(xl_heap_lock_updated, infobits_set) + sizeof(uint8))
|
||||
|
||||
/* This is what we need to know about confirmation of speculative insertion */
|
||||
typedef struct xl_heap_confirm
|
||||
{
|
||||
OffsetNumber offnum; /* confirmed tuple's offset on page */
|
||||
} xl_heap_confirm;
|
||||
|
||||
#define SizeOfHeapConfirm (offsetof(xl_heap_confirm, offnum) + sizeof(OffsetNumber))
|
||||
|
||||
/* This is what we need to know about in-place update */
|
||||
typedef struct xl_heap_inplace
|
||||
{
|
||||
|
||||
@@ -36,7 +36,7 @@ typedef struct BulkInsertStateData
|
||||
|
||||
|
||||
extern void RelationPutHeapTuple(Relation relation, Buffer buffer,
|
||||
HeapTuple tuple);
|
||||
HeapTuple tuple, bool token);
|
||||
extern Buffer RelationGetBufferForTuple(Relation relation, Size len,
|
||||
Buffer otherBuffer, int options,
|
||||
BulkInsertState bistate,
|
||||
|
||||
@@ -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