Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61a553a

Browse files
committed
Add comments explaining how unique and exclusion constraints are enforced.
1 parentd64a9c8 commit61a553a

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

‎src/backend/executor/execIndexing.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
/*-------------------------------------------------------------------------
22
*
33
* execIndexing.c
4-
* executor support for maintaining indexes
4+
* routines for inserting index tuples and enforcing unique and
5+
* exclusive constraints.
6+
*
7+
* ExecInsertIndexTuples() is the main entry point. It's called after
8+
* inserting a tuple to the heap, and it inserts corresponding index tuples
9+
* into all indexes. At the same time, it enforces any unique and
10+
* exclusion constraints:
11+
*
12+
* Unique Indexes
13+
* --------------
14+
*
15+
* Enforcing a unique constraint is straightforward. When the index AM
16+
* inserts the tuple to the index, it also checks that there are no
17+
* conflicting tuples in the index already. It does so atomically, so that
18+
* even if two backends try to insert the same key concurrently, only one
19+
* of them will succeed. All the logic to ensure atomicity, and to wait
20+
* for in-progress transactions to finish, is handled by the index AM.
21+
*
22+
* If a unique constraint is deferred, we request the index AM to not
23+
* throw an error if a conflict is found. Instead, we make note that there
24+
* was a conflict and return the list of indexes with conflicts to the
25+
* caller. The caller must re-check them later, by calling index_insert()
26+
* with the UNIQUE_CHECK_EXISTING option.
27+
*
28+
* Exclusion Constraints
29+
* ---------------------
30+
*
31+
* Exclusion constraints are different from unique indexes in that when the
32+
* tuple is inserted to the index, the index AM does not check for
33+
* duplicate keys at the same time. After the insertion, we perform a
34+
* separate scan on the index to check for conflicting tuples, and if one
35+
* is found, we throw an error and the transaction is aborted. If the
36+
* conflicting tuple's inserter or deleter is in-progress, we wait for it
37+
* to finish first.
38+
*
39+
* There is a chance of deadlock, if two backends insert a tuple at the
40+
* same time, and then perform the scan to check for conflicts. They will
41+
* find each other's tuple, and both try to wait for each other. The
42+
* deadlock detector will detect that, and abort one of the transactions.
43+
* That's fairly harmless, as one of them was bound to abort with a
44+
* "duplicate key error" anyway, although you get a different error
45+
* message.
46+
*
47+
* If an exclusion constraint is deferred, we still perform the conflict
48+
* checking scan immediately after inserting the index tuple. But instead
49+
* of throwing an error if a conflict is found, we return that information
50+
* to the caller. The caller must re-check them later by calling
51+
* check_exclusion_constraint().
52+
*
553
*
654
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
755
* Portions Copyright (c) 1994, Regents of the University of California
@@ -134,13 +182,10 @@ ExecCloseIndices(ResultRelInfo *resultRelInfo)
134182
*This routine takes care of inserting index tuples
135183
*into all the relations indexing the result relation
136184
*when a heap tuple is inserted into the result relation.
137-
*Much of this code should be moved into the genam
138-
*stuff as it only exists here because the genam stuff
139-
*doesn't provide the functionality needed by the
140-
*executor.. -cim 9/27/89
141185
*
142-
*This returns a list of index OIDs for any unique or exclusion
143-
*constraints that are deferred and that had
186+
*Unique and exclusion constraints are enforced at the same
187+
*time. This returns a list of index OIDs for any unique or
188+
*exclusion constraints that are deferred and that had
144189
*potential (unconfirmed) conflicts.
145190
*
146191
*CAUTION: this must not be called for a HOT update.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp