mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +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:
@@ -76,6 +76,11 @@ extern bool ConditionalXactLockTableWait(TransactionId xid);
|
||||
extern void WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode);
|
||||
extern void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode);
|
||||
|
||||
/* Lock an XID for tuple insertion (used to wait for an insertion to finish) */
|
||||
extern uint32 SpeculativeInsertionLockAcquire(TransactionId xid);
|
||||
extern void SpeculativeInsertionLockRelease(TransactionId xid);
|
||||
extern void SpeculativeInsertionWait(TransactionId xid, uint32 token);
|
||||
|
||||
/* Lock a general object (other than a relation) of the current database */
|
||||
extern void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
|
||||
LOCKMODE lockmode);
|
||||
|
@@ -176,6 +176,8 @@ typedef enum LockTagType
|
||||
/* ID info for a transaction is its TransactionId */
|
||||
LOCKTAG_VIRTUALTRANSACTION, /* virtual transaction (ditto) */
|
||||
/* ID info for a virtual transaction is its VirtualTransactionId */
|
||||
LOCKTAG_SPECULATIVE_TOKEN, /* speculative insertion Xid and token */
|
||||
/* ID info for a transaction is its TransactionId */
|
||||
LOCKTAG_OBJECT, /* non-relation database object */
|
||||
/* ID info for an object is DB OID + CLASS OID + OBJECT OID + SUBID */
|
||||
|
||||
@@ -261,6 +263,14 @@ typedef struct LOCKTAG
|
||||
(locktag).locktag_type = LOCKTAG_VIRTUALTRANSACTION, \
|
||||
(locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
|
||||
|
||||
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag,xid,token) \
|
||||
((locktag).locktag_field1 = (xid), \
|
||||
(locktag).locktag_field2 = (token), \
|
||||
(locktag).locktag_field3 = 0, \
|
||||
(locktag).locktag_field4 = 0, \
|
||||
(locktag).locktag_type = LOCKTAG_SPECULATIVE_TOKEN, \
|
||||
(locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
|
||||
|
||||
#define SET_LOCKTAG_OBJECT(locktag,dboid,classoid,objoid,objsubid) \
|
||||
((locktag).locktag_field1 = (dboid), \
|
||||
(locktag).locktag_field2 = (classoid), \
|
||||
|
Reference in New Issue
Block a user