PostgreSQL Source Code git master
Loading...
Searching...
No Matches
predicate.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * predicate.c
4 * POSTGRES predicate locking
5 * to support full serializable transaction isolation
6 *
7 *
8 * The approach taken is to implement Serializable Snapshot Isolation (SSI)
9 * as initially described in this paper:
10 *
11 * Michael J. Cahill, Uwe Röhm, and Alan D. Fekete. 2008.
12 * Serializable isolation for snapshot databases.
13 * In SIGMOD '08: Proceedings of the 2008 ACM SIGMOD
14 * international conference on Management of data,
15 * pages 729-738, New York, NY, USA. ACM.
16 * http://doi.acm.org/10.1145/1376616.1376690
17 *
18 * and further elaborated in Cahill's doctoral thesis:
19 *
20 * Michael James Cahill. 2009.
21 * Serializable Isolation for Snapshot Databases.
22 * Sydney Digital Theses.
23 * University of Sydney, School of Information Technologies.
24 * http://hdl.handle.net/2123/5353
25 *
26 *
27 * Predicate locks for Serializable Snapshot Isolation (SSI) are SIREAD
28 * locks, which are so different from normal locks that a distinct set of
29 * structures is required to handle them. They are needed to detect
30 * rw-conflicts when the read happens before the write. (When the write
31 * occurs first, the reading transaction can check for a conflict by
32 * examining the MVCC data.)
33 *
34 * (1) Besides tuples actually read, they must cover ranges of tuples
35 * which would have been read based on the predicate. This will
36 * require modelling the predicates through locks against database
37 * objects such as pages, index ranges, or entire tables.
38 *
39 * (2) They must be kept in RAM for quick access. Because of this, it
40 * isn't possible to always maintain tuple-level granularity -- when
41 * the space allocated to store these approaches exhaustion, a
42 * request for a lock may need to scan for situations where a single
43 * transaction holds many fine-grained locks which can be coalesced
44 * into a single coarser-grained lock.
45 *
46 * (3) They never block anything; they are more like flags than locks
47 * in that regard; although they refer to database objects and are
48 * used to identify rw-conflicts with normal write locks.
49 *
50 * (4) While they are associated with a transaction, they must survive
51 * a successful COMMIT of that transaction, and remain until all
52 * overlapping transactions complete. This even means that they
53 * must survive termination of the transaction's process. If a
54 * top level transaction is rolled back, however, it is immediately
55 * flagged so that it can be ignored, and its SIREAD locks can be
56 * released any time after that.
57 *
58 * (5) The only transactions which create SIREAD locks or check for
59 * conflicts with them are serializable transactions.
60 *
61 * (6) When a write lock for a top level transaction is found to cover
62 * an existing SIREAD lock for the same transaction, the SIREAD lock
63 * can be deleted.
64 *
65 * (7) A write from a serializable transaction must ensure that an xact
66 * record exists for the transaction, with the same lifespan (until
67 * all concurrent transaction complete or the transaction is rolled
68 * back) so that rw-dependencies to that transaction can be
69 * detected.
70 *
71 * We use an optimization for read-only transactions. Under certain
72 * circumstances, a read-only transaction's snapshot can be shown to
73 * never have conflicts with other transactions. This is referred to
74 * as a "safe" snapshot (and one known not to be is "unsafe").
75 * However, it can't be determined whether a snapshot is safe until
76 * all concurrent read/write transactions complete.
77 *
78 * Once a read-only transaction is known to have a safe snapshot, it
79 * can release its predicate locks and exempt itself from further
80 * predicate lock tracking. READ ONLY DEFERRABLE transactions run only
81 * on safe snapshots, waiting as necessary for one to be available.
82 *
83 *
84 * Lightweight locks to manage access to the predicate locking shared
85 * memory objects must be taken in this order, and should be released in
86 * reverse order:
87 *
88 * SerializableFinishedListLock
89 * - Protects the list of transactions which have completed but which
90 * may yet matter because they overlap still-active transactions.
91 *
92 * SerializablePredicateListLock
93 * - Protects the linked list of locks held by a transaction. Note
94 * that the locks themselves are also covered by the partition
95 * locks of their respective lock targets; this lock only affects
96 * the linked list connecting the locks related to a transaction.
97 * - All transactions share this single lock (with no partitioning).
98 * - There is never a need for a process other than the one running
99 * an active transaction to walk the list of locks held by that
100 * transaction, except parallel query workers sharing the leader's
101 * transaction. In the parallel case, an extra per-sxact lock is
102 * taken; see below.
103 * - It is relatively infrequent that another process needs to
104 * modify the list for a transaction, but it does happen for such
105 * things as index page splits for pages with predicate locks and
106 * freeing of predicate locked pages by a vacuum process. When
107 * removing a lock in such cases, the lock itself contains the
108 * pointers needed to remove it from the list. When adding a
109 * lock in such cases, the lock can be added using the anchor in
110 * the transaction structure. Neither requires walking the list.
111 * - Cleaning up the list for a terminated transaction is sometimes
112 * not done on a retail basis, in which case no lock is required.
113 * - Due to the above, a process accessing its active transaction's
114 * list always uses a shared lock, regardless of whether it is
115 * walking or maintaining the list. This improves concurrency
116 * for the common access patterns.
117 * - A process which needs to alter the list of a transaction other
118 * than its own active transaction must acquire an exclusive
119 * lock.
120 *
121 * SERIALIZABLEXACT's member 'perXactPredicateListLock'
122 * - Protects the linked list of predicate locks held by a transaction.
123 * Only needed for parallel mode, where multiple backends share the
124 * same SERIALIZABLEXACT object. Not needed if
125 * SerializablePredicateListLock is held exclusively.
126 *
127 * PredicateLockHashPartitionLock(hashcode)
128 * - The same lock protects a target, all locks on that target, and
129 * the linked list of locks on the target.
130 * - When more than one is needed, acquire in ascending address order.
131 * - When all are needed (rare), acquire in ascending index order with
132 * PredicateLockHashPartitionLockByIndex(index).
133 *
134 * SerializableXactHashLock
135 * - Protects both PredXact and SerializableXidHash.
136 *
137 * SerialControlLock
138 * - Protects SerialControlData members
139 *
140 * SLRU per-bank locks
141 * - Protects SerialSlruCtl
142 *
143 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
144 * Portions Copyright (c) 1994, Regents of the University of California
145 *
146 *
147 * IDENTIFICATION
148 * src/backend/storage/lmgr/predicate.c
149 *
150 *-------------------------------------------------------------------------
151 */
152/*
153 * INTERFACE ROUTINES
154 *
155 * housekeeping for setting up shared memory predicate lock structures
156 * PredicateLockShmemInit(void)
157 * PredicateLockShmemSize(void)
158 *
159 * predicate lock reporting
160 * GetPredicateLockStatusData(void)
161 * PageIsPredicateLocked(Relation relation, BlockNumber blkno)
162 *
163 * predicate lock maintenance
164 * GetSerializableTransactionSnapshot(Snapshot snapshot)
165 * SetSerializableTransactionSnapshot(Snapshot snapshot,
166 * VirtualTransactionId *sourcevxid)
167 * RegisterPredicateLockingXid(void)
168 * PredicateLockRelation(Relation relation, Snapshot snapshot)
169 * PredicateLockPage(Relation relation, BlockNumber blkno,
170 * Snapshot snapshot)
171 * PredicateLockTID(Relation relation, const ItemPointerData *tid, Snapshot snapshot,
172 * TransactionId tuple_xid)
173 * PredicateLockPageSplit(Relation relation, BlockNumber oldblkno,
174 * BlockNumber newblkno)
175 * PredicateLockPageCombine(Relation relation, BlockNumber oldblkno,
176 * BlockNumber newblkno)
177 * TransferPredicateLocksToHeapRelation(Relation relation)
178 * ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
179 *
180 * conflict detection (may also trigger rollback)
181 * CheckForSerializableConflictOut(Relation relation, TransactionId xid,
182 * Snapshot snapshot)
183 * CheckForSerializableConflictIn(Relation relation, const ItemPointerData *tid,
184 * BlockNumber blkno)
185 * CheckTableForSerializableConflictIn(Relation relation)
186 *
187 * final rollback checking
188 * PreCommit_CheckForSerializationFailure(void)
189 *
190 * two-phase commit support
191 * AtPrepare_PredicateLocks(void);
192 * PostPrepare_PredicateLocks(TransactionId xid);
193 * PredicateLockTwoPhaseFinish(FullTransactionId fxid, bool isCommit);
194 * predicatelock_twophase_recover(FullTransactionId fxid, uint16 info,
195 * void *recdata, uint32 len);
196 */
197
198#include "postgres.h"
199
200#include "access/parallel.h"
201#include "access/slru.h"
202#include "access/transam.h"
203#include "access/twophase.h"
204#include "access/twophase_rmgr.h"
205#include "access/xact.h"
206#include "access/xlog.h"
207#include "miscadmin.h"
208#include "pgstat.h"
209#include "port/pg_lfind.h"
210#include "storage/predicate.h"
212#include "storage/proc.h"
213#include "storage/procarray.h"
214#include "utils/guc_hooks.h"
215#include "utils/rel.h"
216#include "utils/snapmgr.h"
217#include "utils/wait_event.h"
218
219/* Uncomment the next line to test the graceful degradation code. */
220/* #define TEST_SUMMARIZE_SERIAL */
221
222/*
223 * Test the most selective fields first, for performance.
224 *
225 * a is covered by b if all of the following hold:
226 * 1) a.database = b.database
227 * 2) a.relation = b.relation
228 * 3) b.offset is invalid (b is page-granularity or higher)
229 * 4) either of the following:
230 * 4a) a.offset is valid (a is tuple-granularity) and a.page = b.page
231 * or 4b) a.offset is invalid and b.page is invalid (a is
232 * page-granularity and b is relation-granularity
233 */
234#define TargetTagIsCoveredBy(covered_target, covering_target) \
235 ((GET_PREDICATELOCKTARGETTAG_RELATION(covered_target) == /* (2) */ \
236 GET_PREDICATELOCKTARGETTAG_RELATION(covering_target)) \
237 && (GET_PREDICATELOCKTARGETTAG_OFFSET(covering_target) == \
238 InvalidOffsetNumber) /* (3) */ \
239 && (((GET_PREDICATELOCKTARGETTAG_OFFSET(covered_target) != \
240 InvalidOffsetNumber) /* (4a) */ \
241 && (GET_PREDICATELOCKTARGETTAG_PAGE(covering_target) == \
242 GET_PREDICATELOCKTARGETTAG_PAGE(covered_target))) \
243 || ((GET_PREDICATELOCKTARGETTAG_PAGE(covering_target) == \
244 InvalidBlockNumber) /* (4b) */ \
245 && (GET_PREDICATELOCKTARGETTAG_PAGE(covered_target) \
246 != InvalidBlockNumber))) \
247 && (GET_PREDICATELOCKTARGETTAG_DB(covered_target) == /* (1) */ \
248 GET_PREDICATELOCKTARGETTAG_DB(covering_target)))
249
250/*
251 * The predicate locking target and lock shared hash tables are partitioned to
252 * reduce contention. To determine which partition a given target belongs to,
253 * compute the tag's hash code with PredicateLockTargetTagHashCode(), then
254 * apply one of these macros.
255 * NB: NUM_PREDICATELOCK_PARTITIONS must be a power of 2!
256 */
257#define PredicateLockHashPartition(hashcode) \
258 ((hashcode) % NUM_PREDICATELOCK_PARTITIONS)
259#define PredicateLockHashPartitionLock(hashcode) \
260 (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + \
261 PredicateLockHashPartition(hashcode)].lock)
262#define PredicateLockHashPartitionLockByIndex(i) \
263 (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + (i)].lock)
264
265#define NPREDICATELOCKTARGETENTS() \
266 mul_size(max_predicate_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
267
268#define SxactIsOnFinishedList(sxact) (!dlist_node_is_detached(&(sxact)->finishedLink))
269
270/*
271 * Note that a sxact is marked "prepared" once it has passed
272 * PreCommit_CheckForSerializationFailure, even if it isn't using
273 * 2PC. This is the point at which it can no longer be aborted.
274 *
275 * The PREPARED flag remains set after commit, so SxactIsCommitted
276 * implies SxactIsPrepared.
277 */
278#define SxactIsCommitted(sxact) (((sxact)->flags & SXACT_FLAG_COMMITTED) != 0)
279#define SxactIsPrepared(sxact) (((sxact)->flags & SXACT_FLAG_PREPARED) != 0)
280#define SxactIsRolledBack(sxact) (((sxact)->flags & SXACT_FLAG_ROLLED_BACK) != 0)
281#define SxactIsDoomed(sxact) (((sxact)->flags & SXACT_FLAG_DOOMED) != 0)
282#define SxactIsReadOnly(sxact) (((sxact)->flags & SXACT_FLAG_READ_ONLY) != 0)
283#define SxactHasSummaryConflictIn(sxact) (((sxact)->flags & SXACT_FLAG_SUMMARY_CONFLICT_IN) != 0)
284#define SxactHasSummaryConflictOut(sxact) (((sxact)->flags & SXACT_FLAG_SUMMARY_CONFLICT_OUT) != 0)
285/*
286 * The following macro actually means that the specified transaction has a
287 * conflict out *to a transaction which committed ahead of it*. It's hard
288 * to get that into a name of a reasonable length.
289 */
290#define SxactHasConflictOut(sxact) (((sxact)->flags & SXACT_FLAG_CONFLICT_OUT) != 0)
291#define SxactIsDeferrableWaiting(sxact) (((sxact)->flags & SXACT_FLAG_DEFERRABLE_WAITING) != 0)
292#define SxactIsROSafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_SAFE) != 0)
293#define SxactIsROUnsafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_UNSAFE) != 0)
294#define SxactIsPartiallyReleased(sxact) (((sxact)->flags & SXACT_FLAG_PARTIALLY_RELEASED) != 0)
295
296/*
297 * Compute the hash code associated with a PREDICATELOCKTARGETTAG.
298 *
299 * To avoid unnecessary recomputations of the hash code, we try to do this
300 * just once per function, and then pass it around as needed. Aside from
301 * passing the hashcode to hash_search_with_hash_value(), we can extract
302 * the lock partition number from the hashcode.
303 */
304#define PredicateLockTargetTagHashCode(predicatelocktargettag) \
305 get_hash_value(PredicateLockTargetHash, predicatelocktargettag)
306
307/*
308 * Given a predicate lock tag, and the hash for its target,
309 * compute the lock hash.
310 *
311 * To make the hash code also depend on the transaction, we xor the sxid
312 * struct's address into the hash code, left-shifted so that the
313 * partition-number bits don't change. Since this is only a hash, we
314 * don't care if we lose high-order bits of the address; use an
315 * intermediate variable to suppress cast-pointer-to-int warnings.
316 */
317#define PredicateLockHashCodeFromTargetHashCode(predicatelocktag, targethash) \
318 ((targethash) ^ ((uint32) PointerGetDatum((predicatelocktag)->myXact)) \
319 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
320
321
322/*
323 * The SLRU buffer area through which we access the old xids.
324 */
326
327#define SerialSlruCtl (&SerialSlruCtlData)
328
329#define SERIAL_PAGESIZE BLCKSZ
330#define SERIAL_ENTRYSIZE sizeof(SerCommitSeqNo)
331#define SERIAL_ENTRIESPERPAGE (SERIAL_PAGESIZE / SERIAL_ENTRYSIZE)
332
333/*
334 * Set maximum pages based on the number needed to track all transactions.
335 */
336#define SERIAL_MAX_PAGE (MaxTransactionId / SERIAL_ENTRIESPERPAGE)
337
338#define SerialNextPage(page) (((page) >= SERIAL_MAX_PAGE) ? 0 : (page) + 1)
339
340#define SerialValue(slotno, xid) (*((SerCommitSeqNo *) \
341 (SerialSlruCtl->shared->page_buffer[slotno] + \
342 ((((uint32) (xid)) % SERIAL_ENTRIESPERPAGE) * SERIAL_ENTRYSIZE))))
343
344#define SerialPage(xid) (((uint32) (xid)) / SERIAL_ENTRIESPERPAGE)
345
346typedef struct SerialControlData
347{
348 int64 headPage; /* newest initialized page */
349 TransactionId headXid; /* newest valid Xid in the SLRU */
350 TransactionId tailXid; /* oldest xmin we might be interested in */
352
354
356
357/*
358 * When the oldest committed transaction on the "finished" list is moved to
359 * SLRU, its predicate locks will be moved to this "dummy" transaction,
360 * collapsing duplicate targets. When a duplicate is found, the later
361 * commitSeqNo is used.
362 */
364
365
366/*
367 * These configuration variables are used to set the predicate lock table size
368 * and to control promotion of predicate locks to coarser granularity in an
369 * attempt to degrade performance (mostly as false positive serialization
370 * failure) gracefully in the face of memory pressure.
371 */
372int max_predicate_locks_per_xact; /* in guc_tables.c */
373int max_predicate_locks_per_relation; /* in guc_tables.c */
374int max_predicate_locks_per_page; /* in guc_tables.c */
375
376/*
377 * This provides a list of objects in order to track transactions
378 * participating in predicate locking. Entries in the list are fixed size,
379 * and reside in shared memory. The memory address of an entry must remain
380 * fixed during its lifetime. The list will be protected from concurrent
381 * update externally; no provision is made in this code to manage that. The
382 * number of entries in the list, and the size allowed for each entry is
383 * fixed upon creation.
384 */
386
387/*
388 * This provides a pool of RWConflict data elements to use in conflict lists
389 * between transactions.
390 */
392
393/*
394 * The predicate locking hash tables are in shared memory.
395 * Each backend keeps pointers to them.
396 */
401
402/*
403 * Tag for a dummy entry in PredicateLockTargetHash. By temporarily removing
404 * this entry, you can ensure that there's enough scratch space available for
405 * inserting one entry in the hash table. This is an otherwise-invalid tag.
406 */
407static const PREDICATELOCKTARGETTAG ScratchTargetTag = {0, 0, 0, 0};
410
411/*
412 * The local hash table used to determine when to combine multiple fine-
413 * grained locks into a single courser-grained lock.
414 */
416
417/*
418 * Keep a pointer to the currently-running serializable transaction (if any)
419 * for quick reference. Also, remember if we have written anything that could
420 * cause a rw-conflict.
421 */
423static bool MyXactDidWrite = false;
424
425/*
426 * The SXACT_FLAG_RO_UNSAFE optimization might lead us to release
427 * MySerializableXact early. If that happens in a parallel query, the leader
428 * needs to defer the destruction of the SERIALIZABLEXACT until end of
429 * transaction, because the workers still have a reference to it. In that
430 * case, the leader stores it here.
431 */
433
434/* local functions */
435
436static SERIALIZABLEXACT *CreatePredXact(void);
438
439static bool RWConflictExists(const SERIALIZABLEXACT *reader, const SERIALIZABLEXACT *writer);
444
446static int serial_errdetail_for_io_error(const void *opaque_data);
447static void SerialInit(void);
451
452static uint32 predicatelock_hash(const void *key, Size keysize);
453static void SummarizeOldestCommittedSxact(void);
457 int sourcepid);
460 PREDICATELOCKTARGETTAG *parent);
462static void RemoveScratchTarget(bool lockheld);
463static void RestoreScratchTarget(bool lockheld);
476 bool removeOld);
478static void DropAllPredicateLocksFromTable(Relation relation,
479 bool transfer);
480static void SetNewSxactGlobalXmin(void);
481static void ClearOldPredicateLocks(void);
482static void ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
483 bool summarize);
484static bool XidIsConcurrent(TransactionId xid);
489static void CreateLocalPredicateLockHash(void);
490static void ReleasePredicateLocksLocal(void);
491
492
493/*------------------------------------------------------------------------*/
494
495/*
496 * Does this relation participate in predicate locking? Temporary and system
497 * relations are exempt.
498 */
499static inline bool
501{
502 return !(relation->rd_id < FirstUnpinnedObjectId ||
503 RelationUsesLocalBuffers(relation));
504}
505
506/*
507 * When a public interface method is called for a read, this is the test to
508 * see if we should do a quick return.
509 *
510 * Note: this function has side-effects! If this transaction has been flagged
511 * as RO-safe since the last call, we release all predicate locks and reset
512 * MySerializableXact. That makes subsequent calls to return quickly.
513 *
514 * This is marked as 'inline' to eliminate the function call overhead in the
515 * common case that serialization is not needed.
516 */
517static inline bool
519{
520 /* Nothing to do if this is not a serializable transaction */
522 return false;
523
524 /*
525 * Don't acquire locks or conflict when scanning with a special snapshot.
526 * This excludes things like CLUSTER and REINDEX. They use the wholesale
527 * functions TransferPredicateLocksToHeapRelation() and
528 * CheckTableForSerializableConflictIn() to participate in serialization,
529 * but the scans involved don't need serialization.
530 */
531 if (!IsMVCCSnapshot(snapshot))
532 return false;
533
534 /*
535 * Check if we have just become "RO-safe". If we have, immediately release
536 * all locks as they're not needed anymore. This also resets
537 * MySerializableXact, so that subsequent calls to this function can exit
538 * quickly.
539 *
540 * A transaction is flagged as RO_SAFE if all concurrent R/W transactions
541 * commit without having conflicts out to an earlier snapshot, thus
542 * ensuring that no conflicts are possible for this transaction.
543 */
545 {
546 ReleasePredicateLocks(false, true);
547 return false;
548 }
549
550 /* Check if the relation doesn't participate in predicate locking */
552 return false;
553
554 return true; /* no excuse to skip predicate locking */
555}
556
557/*
558 * Like SerializationNeededForRead(), but called on writes.
559 * The logic is the same, but there is no snapshot and we can't be RO-safe.
560 */
561static inline bool
563{
564 /* Nothing to do if this is not a serializable transaction */
566 return false;
567
568 /* Check if the relation doesn't participate in predicate locking */
570 return false;
571
572 return true; /* no excuse to skip predicate locking */
573}
574
575
576/*------------------------------------------------------------------------*/
577
578/*
579 * These functions are a simple implementation of a list for this specific
580 * type of struct. If there is ever a generalized shared memory list, we
581 * should probably switch to that.
582 */
583static SERIALIZABLEXACT *
596
597static void
605
606/*------------------------------------------------------------------------*/
607
608/*
609 * These functions manage primitive access to the RWConflict pool and lists.
610 */
611static bool
613{
614 dlist_iter iter;
615
616 Assert(reader != writer);
617
618 /* Check the ends of the purported conflict first. */
619 if (SxactIsDoomed(reader)
621 || dlist_is_empty(&reader->outConflicts)
622 || dlist_is_empty(&writer->inConflicts))
623 return false;
624
625 /*
626 * A conflict is possible; walk the list to find out.
627 *
628 * The unconstify is needed as we have no const version of
629 * dlist_foreach().
630 */
631 dlist_foreach(iter, &unconstify(SERIALIZABLEXACT *, reader)->outConflicts)
632 {
634 dlist_container(RWConflictData, outLink, iter.cur);
635
636 if (conflict->sxactIn == writer)
637 return true;
638 }
639
640 /* No conflict found. */
641 return false;
642}
643
644static void
646{
648
649 Assert(reader != writer);
650 Assert(!RWConflictExists(reader, writer));
651
655 errmsg("not enough elements in RWConflictPool to record a read/write conflict"),
656 errhint("You might need to run fewer transactions at a time or increase \"max_connections\".")));
657
659 dlist_delete(&conflict->outLink);
660
661 conflict->sxactOut = reader;
662 conflict->sxactIn = writer;
663 dlist_push_tail(&reader->outConflicts, &conflict->outLink);
664 dlist_push_tail(&writer->inConflicts, &conflict->inLink);
665}
666
667static void
670{
672
676
680 errmsg("not enough elements in RWConflictPool to record a potential read/write conflict"),
681 errhint("You might need to run fewer transactions at a time or increase \"max_connections\".")));
682
684 dlist_delete(&conflict->outLink);
685
686 conflict->sxactOut = activeXact;
687 conflict->sxactIn = roXact;
688 dlist_push_tail(&activeXact->possibleUnsafeConflicts, &conflict->outLink);
689 dlist_push_tail(&roXact->possibleUnsafeConflicts, &conflict->inLink);
690}
691
692static void
699
700static void
702{
704
707
708 sxact->flags |= SXACT_FLAG_RO_UNSAFE;
709
710 /*
711 * We know this isn't a safe snapshot, so we can stop looking for other
712 * potential conflicts.
713 */
714 dlist_foreach_modify(iter, &sxact->possibleUnsafeConflicts)
715 {
717 dlist_container(RWConflictData, inLink, iter.cur);
718
719 Assert(!SxactIsReadOnly(conflict->sxactOut));
720 Assert(sxact == conflict->sxactIn);
721
723 }
724}
725
726/*------------------------------------------------------------------------*/
727
728/*
729 * Decide whether a Serial page number is "older" for truncation purposes.
730 * Analogous to CLOGPagePrecedes().
731 */
732static bool
746
747static int
749{
750 TransactionId xid = *(const TransactionId *) opaque_data;
751
752 return errdetail("Could not access serializable CSN of transaction %u.", xid);
753}
754
755#ifdef USE_ASSERT_CHECKING
756static void
758{
760 offset = per_page / 2;
763 headPage,
766 oldestXact;
767
768 /* GetNewTransactionId() has assigned the last XID it can safely use. */
769 newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1; /* nothing special */
770 newestXact = newestPage * per_page + offset;
772 oldestXact = newestXact + 1;
773 oldestXact -= 1U << 31;
774 oldestPage = oldestXact / per_page;
775
776 /*
777 * In this scenario, the SLRU headPage pertains to the last ~1000 XIDs
778 * assigned. oldestXact finishes, ~2B XIDs having elapsed since it
779 * started. Further transactions cause us to summarize oldestXact to
780 * tailPage. Function must return false so SerialAdd() doesn't zero
781 * tailPage (which may contain entries for other old, recently-finished
782 * XIDs) and half the SLRU. Reaching this requires burning ~2B XIDs in
783 * single-user mode, a negligible possibility.
784 */
788
789 /*
790 * In this scenario, the SLRU headPage pertains to oldestXact. We're
791 * summarizing an XID near newestXact. (Assume few other XIDs used
792 * SERIALIZABLE, hence the minimal headPage advancement. Assume
793 * oldestXact was long-running and only recently reached the SLRU.)
794 * Function must return true to make SerialAdd() create targetPage.
795 *
796 * Today's implementation mishandles this case, but it doesn't matter
797 * enough to fix. Verify that the defect affects just one page by
798 * asserting correct treatment of its prior page. Reaching this case
799 * requires burning ~2B XIDs in single-user mode, a negligible
800 * possibility. Moreover, if it does happen, the consequence would be
801 * mild, namely a new transaction failing in SimpleLruReadPage().
802 */
806#if 0
808#endif
809}
810#endif
811
812/*
813 * Initialize for the tracking of old serializable committed xids.
814 */
815static void
817{
818 bool found;
819
820 /*
821 * Set up SLRU management of the pg_serial data.
822 */
824 SerialSlruCtl->errdetail_for_io_error = serial_errdetail_for_io_error;
825 SimpleLruInit(SerialSlruCtl, "serializable",
826 serializable_buffers, 0, "pg_serial",
828 SYNC_HANDLER_NONE, false);
829#ifdef USE_ASSERT_CHECKING
831#endif
833
834 /*
835 * Create or attach to the SerialControl structure.
836 */
838 ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
839
840 Assert(found == IsUnderPostmaster);
841 if (!found)
842 {
843 /*
844 * Set control information to reflect empty SLRU.
845 */
851 }
852}
853
854/*
855 * GUC check_hook for serializable_buffers
856 */
857bool
859{
860 return check_slru_buffers("serializable_buffers", newval);
861}
862
863/*
864 * Record a committed read write serializable xid and the minimum
865 * commitSeqNo of any transactions to which this xid had a rw-conflict out.
866 * An invalid commitSeqNo means that there were no conflicts out from xid.
867 */
868static void
870{
873 int slotno;
875 bool isNewPage;
876 LWLock *lock;
877
879
880 targetPage = SerialPage(xid);
882
883 /*
884 * In this routine, we must hold both SerialControlLock and the SLRU bank
885 * lock simultaneously while making the SLRU data catch up with the new
886 * state that we determine.
887 */
889
890 /*
891 * If 'xid' is older than the global xmin (== tailXid), there's no need to
892 * store it, after all. This can happen if the oldest transaction holding
893 * back the global xmin just finished, making 'xid' uninteresting, but
894 * ClearOldPredicateLocks() has not yet run.
895 */
898 {
900 return;
901 }
902
903 /*
904 * If the SLRU is currently unused, zero out the whole active region from
905 * tailXid to headXid before taking it into use. Otherwise zero out only
906 * any new pages that enter the tailXid-headXid range as we advance
907 * headXid.
908 */
909 if (serialControl->headPage < 0)
910 {
912 isNewPage = true;
913 }
914 else
915 {
918 targetPage);
919 }
920
923 serialControl->headXid = xid;
924 if (isNewPage)
926
927 if (isNewPage)
928 {
929 /* Initialize intervening pages; might involve trading locks */
930 for (;;)
931 {
936 break;
938 LWLockRelease(lock);
939 }
940 }
941 else
942 {
945 }
946
948 SerialSlruCtl->shared->page_dirty[slotno] = true;
949
950 LWLockRelease(lock);
952}
953
954/*
955 * Get the minimum commitSeqNo for any conflict out for the given xid. For
956 * a transaction which exists but has no conflict out, InvalidSerCommitSeqNo
957 * will be returned.
958 */
959static SerCommitSeqNo
961{
965 int slotno;
966
968
973
975 return 0;
976
978
981 return 0;
982
983 /*
984 * The following function must be called without holding SLRU bank lock,
985 * but will return with that lock held, which must then be released.
986 */
988 SerialPage(xid), &xid);
989 val = SerialValue(slotno, xid);
991 return val;
992}
993
994/*
995 * Call this whenever there is a new xmin for active serializable
996 * transactions. We don't need to keep information on transactions which
997 * precede that. InvalidTransactionId means none active, so everything in
998 * the SLRU can be discarded.
999 */
1000static void
1002{
1004
1005 /*
1006 * When no sxacts are active, nothing overlaps, set the xid values to
1007 * invalid to show that there are no valid entries. Don't clear headPage,
1008 * though. A new xmin might still land on that page, and we don't want to
1009 * repeatedly zero out the same page.
1010 */
1011 if (!TransactionIdIsValid(xid))
1012 {
1016 return;
1017 }
1018
1019 /*
1020 * When we're recovering prepared transactions, the global xmin might move
1021 * backwards depending on the order they're recovered. Normally that's not
1022 * OK, but during recovery no serializable transactions will commit, so
1023 * the SLRU is empty and we can get away with it.
1024 */
1025 if (RecoveryInProgress())
1026 {
1030 {
1031 serialControl->tailXid = xid;
1032 }
1034 return;
1035 }
1036
1039
1040 serialControl->tailXid = xid;
1041
1043}
1044
1045/*
1046 * Perform a checkpoint --- either during shutdown, or on-the-fly
1047 *
1048 * We don't have any data that needs to survive a restart, but this is a
1049 * convenient place to truncate the SLRU.
1050 */
1051void
1053{
1055
1057
1058 /* Exit quickly if the SLRU is currently not in use. */
1059 if (serialControl->headPage < 0)
1060 {
1062 return;
1063 }
1064
1066 {
1068
1070
1071 /*
1072 * It is possible for the tailXid to be ahead of the headXid. This
1073 * occurs if we checkpoint while there are in-progress serializable
1074 * transaction(s) advancing the tail but we are yet to summarize the
1075 * transactions. In this case, we cutoff up to the headPage and the
1076 * next summary will advance the headXid.
1077 */
1079 {
1080 /* We can truncate the SLRU up to the page containing tailXid */
1082 }
1083 else
1085 }
1086 else
1087 {
1088 /*----------
1089 * The SLRU is no longer needed. Truncate to head before we set head
1090 * invalid.
1091 *
1092 * XXX: It's possible that the SLRU is not needed again until XID
1093 * wrap-around has happened, so that the segment containing headPage
1094 * that we leave behind will appear to be new again. In that case it
1095 * won't be removed until XID horizon advances enough to make it
1096 * current again.
1097 *
1098 * XXX: This should happen in vac_truncate_clog(), not in checkpoints.
1099 * Consider this scenario, starting from a system with no in-progress
1100 * transactions and VACUUM FREEZE having maximized oldestXact:
1101 * - Start a SERIALIZABLE transaction.
1102 * - Start, finish, and summarize a SERIALIZABLE transaction, creating
1103 * one SLRU page.
1104 * - Consume XIDs to reach xidStopLimit.
1105 * - Finish all transactions. Due to the long-running SERIALIZABLE
1106 * transaction, earlier checkpoints did not touch headPage. The
1107 * next checkpoint will change it, but that checkpoint happens after
1108 * the end of the scenario.
1109 * - VACUUM to advance XID limits.
1110 * - Consume ~2M XIDs, crossing the former xidWrapLimit.
1111 * - Start, finish, and summarize a SERIALIZABLE transaction.
1112 * SerialAdd() declines to create the targetPage, because headPage
1113 * is not regarded as in the past relative to that targetPage. The
1114 * transaction instigating the summarize fails in
1115 * SimpleLruReadPage().
1116 */
1118 serialControl->headPage = -1;
1119 }
1120
1122
1123 /*
1124 * Truncate away pages that are no longer required. Note that no
1125 * additional locking is required, because this is only called as part of
1126 * a checkpoint, and the validity limits have already been determined.
1127 */
1129
1130 /*
1131 * Write dirty SLRU pages to disk
1132 *
1133 * This is not actually necessary from a correctness point of view. We do
1134 * it merely as a debugging aid.
1135 *
1136 * We're doing this after the truncation to avoid writing pages right
1137 * before deleting the file in which they sit, which would be completely
1138 * pointless.
1139 */
1141}
1142
1143/*------------------------------------------------------------------------*/
1144
1145/*
1146 * PredicateLockShmemInit -- Initialize the predicate locking data structures.
1147 *
1148 * This is called from CreateSharedMemoryAndSemaphores(), which see for
1149 * more comments. In the normal postmaster case, the shared hash tables
1150 * are created here. Backends inherit the pointers
1151 * to the shared tables via fork(). In the EXEC_BACKEND case, each
1152 * backend re-executes this code to obtain pointers to the already existing
1153 * shared hash tables.
1154 */
1155void
1157{
1158 HASHCTL info;
1161 bool found;
1162
1163#ifndef EXEC_BACKEND
1165#endif
1166
1167 /*
1168 * Compute size of predicate lock target hashtable. Note these
1169 * calculations must agree with PredicateLockShmemSize!
1170 */
1172
1173 /*
1174 * Allocate hash table for PREDICATELOCKTARGET structs. This stores
1175 * per-predicate-lock-target information.
1176 */
1177 info.keysize = sizeof(PREDICATELOCKTARGETTAG);
1178 info.entrysize = sizeof(PREDICATELOCKTARGET);
1180
1181 PredicateLockTargetHash = ShmemInitHash("PREDICATELOCKTARGET hash",
1184 &info,
1187
1188 /*
1189 * Reserve a dummy entry in the hash table; we use it to make sure there's
1190 * always one entry available when we need to split or combine a page,
1191 * because running out of space there could mean aborting a
1192 * non-serializable transaction.
1193 */
1194 if (!IsUnderPostmaster)
1195 {
1197 HASH_ENTER, &found);
1198 Assert(!found);
1199 }
1200
1201 /* Pre-calculate the hash and partition lock of the scratch entry */
1204
1205 /*
1206 * Allocate hash table for PREDICATELOCK structs. This stores per
1207 * xact-lock-of-a-target information.
1208 */
1209 info.keysize = sizeof(PREDICATELOCKTAG);
1210 info.entrysize = sizeof(PREDICATELOCK);
1211 info.hash = predicatelock_hash;
1213
1214 /* Assume an average of 2 xacts per target */
1215 max_table_size *= 2;
1216
1217 PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
1220 &info,
1223
1224 /*
1225 * Compute size for serializable transaction hashtable. Note these
1226 * calculations must agree with PredicateLockShmemSize!
1227 */
1229
1230 /*
1231 * Allocate a list to hold information on transactions participating in
1232 * predicate locking.
1233 *
1234 * Assume an average of 10 predicate locking transactions per backend.
1235 * This allows aggressive cleanup while detail is present before data must
1236 * be summarized for storage in SLRU and the "dummy" transaction.
1237 */
1238 max_table_size *= 10;
1239
1242 sizeof(SERIALIZABLEXACT))));
1243
1244 PredXact = ShmemInitStruct("PredXactList",
1246 &found);
1247 Assert(found == IsUnderPostmaster);
1248 if (!found)
1249 {
1250 int i;
1251
1252 /* clean everything, both the header and the element */
1254
1265 /* Add all elements to available list, clean. */
1266 for (i = 0; i < max_table_size; i++)
1267 {
1271 }
1288 }
1289 /* This never changes, so let's keep a local copy. */
1291
1292 /*
1293 * Allocate hash table for SERIALIZABLEXID structs. This stores per-xid
1294 * information for serializable transactions which have accessed data.
1295 */
1296 info.keysize = sizeof(SERIALIZABLEXIDTAG);
1297 info.entrysize = sizeof(SERIALIZABLEXID);
1298
1299 SerializableXidHash = ShmemInitHash("SERIALIZABLEXID hash",
1302 &info,
1305
1306 /*
1307 * Allocate space for tracking rw-conflicts in lists attached to the
1308 * transactions.
1309 *
1310 * Assume an average of 5 conflicts per transaction. Calculations suggest
1311 * that this will prevent resource exhaustion in even the most pessimal
1312 * loads up to max_connections = 200 with all 200 connections pounding the
1313 * database with serializable transactions. Beyond that, there may be
1314 * occasional transactions canceled when trying to flag conflicts. That's
1315 * probably OK.
1316 */
1317 max_table_size *= 5;
1318
1322
1323 RWConflictPool = ShmemInitStruct("RWConflictPool",
1325 &found);
1326 Assert(found == IsUnderPostmaster);
1327 if (!found)
1328 {
1329 int i;
1330
1331 /* clean everything, including the elements */
1333
1337 /* Add all elements to available list, clean. */
1338 for (i = 0; i < max_table_size; i++)
1339 {
1342 }
1343 }
1344
1345 /*
1346 * Create or attach to the header for the list of finished serializable
1347 * transactions.
1348 */
1350 ShmemInitStruct("FinishedSerializableTransactions",
1351 sizeof(dlist_head),
1352 &found);
1353 Assert(found == IsUnderPostmaster);
1354 if (!found)
1356
1357 /*
1358 * Initialize the SLRU storage for old committed serializable
1359 * transactions.
1360 */
1361 SerialInit();
1362}
1363
1364/*
1365 * Estimate shared-memory space used for predicate lock table
1366 */
1367Size
1369{
1370 Size size = 0;
1371 long max_table_size;
1372
1373 /* predicate lock target hash table */
1376 sizeof(PREDICATELOCKTARGET)));
1377
1378 /* predicate lock hash table */
1379 max_table_size *= 2;
1381 sizeof(PREDICATELOCK)));
1382
1383 /*
1384 * Since NPREDICATELOCKTARGETENTS is only an estimate, add 10% safety
1385 * margin.
1386 */
1387 size = add_size(size, size / 10);
1388
1389 /* transaction list */
1391 max_table_size *= 10;
1392 size = add_size(size, PredXactListDataSize);
1393 size = add_size(size, mul_size((Size) max_table_size,
1394 sizeof(SERIALIZABLEXACT)));
1395
1396 /* transaction xid table */
1398 sizeof(SERIALIZABLEXID)));
1399
1400 /* rw-conflict pool */
1401 max_table_size *= 5;
1403 size = add_size(size, mul_size((Size) max_table_size,
1405
1406 /* Head for list of finished serializable transactions. */
1407 size = add_size(size, sizeof(dlist_head));
1408
1409 /* Shared memory structures for SLRU tracking of old committed xids. */
1410 size = add_size(size, sizeof(SerialControlData));
1412
1413 return size;
1414}
1415
1416
1417/*
1418 * Compute the hash code associated with a PREDICATELOCKTAG.
1419 *
1420 * Because we want to use just one set of partition locks for both the
1421 * PREDICATELOCKTARGET and PREDICATELOCK hash tables, we have to make sure
1422 * that PREDICATELOCKs fall into the same partition number as their
1423 * associated PREDICATELOCKTARGETs. dynahash.c expects the partition number
1424 * to be the low-order bits of the hash code, and therefore a
1425 * PREDICATELOCKTAG's hash code must have the same low-order bits as the
1426 * associated PREDICATELOCKTARGETTAG's hash code. We achieve this with this
1427 * specialized hash function.
1428 */
1429static uint32
1430predicatelock_hash(const void *key, Size keysize)
1431{
1432 const PREDICATELOCKTAG *predicatelocktag = (const PREDICATELOCKTAG *) key;
1434
1435 Assert(keysize == sizeof(PREDICATELOCKTAG));
1436
1437 /* Look into the associated target object, and compute its hash code */
1439
1441}
1442
1443
1444/*
1445 * GetPredicateLockStatusData
1446 * Return a table containing the internal state of the predicate
1447 * lock manager for use in pg_lock_status.
1448 *
1449 * Like GetLockStatusData, this function tries to hold the partition LWLocks
1450 * for as short a time as possible by returning two arrays that simply
1451 * contain the PREDICATELOCKTARGETTAG and SERIALIZABLEXACT for each lock
1452 * table entry. Multiple copies of the same PREDICATELOCKTARGETTAG and
1453 * SERIALIZABLEXACT will likely appear.
1454 */
1457{
1459 int i;
1460 int els,
1461 el;
1464
1466
1467 /*
1468 * To ensure consistency, take simultaneous locks on all partition locks
1469 * in ascending order, then SerializableXactHashLock.
1470 */
1471 for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
1474
1475 /* Get number of locks and allocate appropriately-sized arrays. */
1477 data->nelements = els;
1480
1481
1482 /* Scan through PredicateLockHash and copy contents */
1484
1485 el = 0;
1486
1488 {
1489 data->locktags[el] = predlock->tag.myTarget->tag;
1490 data->xacts[el] = *predlock->tag.myXact;
1491 el++;
1492 }
1493
1494 Assert(el == els);
1495
1496 /* Release locks in reverse order */
1498 for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
1500
1501 return data;
1502}
1503
1504/*
1505 * Free up shared memory structures by pushing the oldest sxact (the one at
1506 * the front of the SummarizeOldestCommittedSxact queue) into summary form.
1507 * Each call will free exactly one SERIALIZABLEXACT structure and may also
1508 * free one or more of these structures: SERIALIZABLEXID, PREDICATELOCK,
1509 * PREDICATELOCKTARGET, RWConflictData.
1510 */
1511static void
1513{
1515
1517
1518 /*
1519 * This function is only called if there are no sxact slots available.
1520 * Some of them must belong to old, already-finished transactions, so
1521 * there should be something in FinishedSerializableTransactions list that
1522 * we can summarize. However, there's a race condition: while we were not
1523 * holding any locks, a transaction might have ended and cleaned up all
1524 * the finished sxact entries already, freeing up their sxact slots. In
1525 * that case, we have nothing to do here. The caller will find one of the
1526 * slots released by the other backend when it retries.
1527 */
1529 {
1531 return;
1532 }
1533
1534 /*
1535 * Grab the first sxact off the finished list -- this will be the earliest
1536 * commit. Remove it from the list.
1537 */
1540 dlist_delete_thoroughly(&sxact->finishedLink);
1541
1542 /* Add to SLRU summary information. */
1545 ? sxact->SeqNo.earliestOutConflictCommit : InvalidSerCommitSeqNo);
1546
1547 /* Summarize and release the detail. */
1548 ReleaseOneSerializableXact(sxact, false, true);
1549
1551}
1552
1553/*
1554 * GetSafeSnapshot
1555 * Obtain and register a snapshot for a READ ONLY DEFERRABLE
1556 * transaction. Ensures that the snapshot is "safe", i.e. a
1557 * read-only transaction running on it can execute serializably
1558 * without further checks. This requires waiting for concurrent
1559 * transactions to complete, and retrying with a new snapshot if
1560 * one of them could possibly create a conflict.
1561 *
1562 * As with GetSerializableTransactionSnapshot (which this is a subroutine
1563 * for), the passed-in Snapshot pointer should reference a static data
1564 * area that can safely be passed to GetSnapshotData.
1565 */
1566static Snapshot
1568{
1569 Snapshot snapshot;
1570
1572
1573 while (true)
1574 {
1575 /*
1576 * GetSerializableTransactionSnapshotInt is going to call
1577 * GetSnapshotData, so we need to provide it the static snapshot area
1578 * our caller passed to us. The pointer returned is actually the same
1579 * one passed to it, but we avoid assuming that here.
1580 */
1582 NULL, InvalidPid);
1583
1585 return snapshot; /* no concurrent r/w xacts; it's safe */
1586
1588
1589 /*
1590 * Wait for concurrent transactions to finish. Stop early if one of
1591 * them marked us as conflicted.
1592 */
1596 {
1600 }
1602
1604 {
1606 break; /* success */
1607 }
1608
1610
1611 /* else, need to retry... */
1614 errmsg_internal("deferrable snapshot was unsafe; trying a new one")));
1615 ReleasePredicateLocks(false, false);
1616 }
1617
1618 /*
1619 * Now we have a safe snapshot, so we don't need to do any further checks.
1620 */
1622 ReleasePredicateLocks(false, true);
1623
1624 return snapshot;
1625}
1626
1627/*
1628 * GetSafeSnapshotBlockingPids
1629 * If the specified process is currently blocked in GetSafeSnapshot,
1630 * write the process IDs of all processes that it is blocked by
1631 * into the caller-supplied buffer output[]. The list is truncated at
1632 * output_size, and the number of PIDs written into the buffer is
1633 * returned. Returns zero if the given PID is not currently blocked
1634 * in GetSafeSnapshot.
1635 */
1636int
1638{
1639 int num_written = 0;
1640 dlist_iter iter;
1642
1644
1645 /* Find blocked_pid's SERIALIZABLEXACT by linear search. */
1647 {
1649 dlist_container(SERIALIZABLEXACT, xactLink, iter.cur);
1650
1651 if (sxact->pid == blocked_pid)
1652 {
1654 break;
1655 }
1656 }
1657
1658 /* Did we find it, and is it currently waiting in GetSafeSnapshot? */
1660 {
1661 /* Traverse the list of possible unsafe conflicts collecting PIDs. */
1662 dlist_foreach(iter, &blocking_sxact->possibleUnsafeConflicts)
1663 {
1665 dlist_container(RWConflictData, inLink, iter.cur);
1666
1667 output[num_written++] = possibleUnsafeConflict->sxactOut->pid;
1668
1669 if (num_written >= output_size)
1670 break;
1671 }
1672 }
1673
1675
1676 return num_written;
1677}
1678
1679/*
1680 * Acquire a snapshot that can be used for the current transaction.
1681 *
1682 * Make sure we have a SERIALIZABLEXACT reference in MySerializableXact.
1683 * It should be current for this process and be contained in PredXact.
1684 *
1685 * The passed-in Snapshot pointer should reference a static data area that
1686 * can safely be passed to GetSnapshotData. The return value is actually
1687 * always this same pointer; no new snapshot data structure is allocated
1688 * within this function.
1689 */
1692{
1694
1695 /*
1696 * Can't use serializable mode while recovery is still active, as it is,
1697 * for example, on a hot standby. We could get here despite the check in
1698 * check_transaction_isolation() if default_transaction_isolation is set
1699 * to serializable, so phrase the hint accordingly.
1700 */
1701 if (RecoveryInProgress())
1702 ereport(ERROR,
1704 errmsg("cannot use serializable mode in a hot standby"),
1705 errdetail("\"default_transaction_isolation\" is set to \"serializable\"."),
1706 errhint("You can use \"SET default_transaction_isolation = 'repeatable read'\" to change the default.")));
1707
1708 /*
1709 * A special optimization is available for SERIALIZABLE READ ONLY
1710 * DEFERRABLE transactions -- we can wait for a suitable snapshot and
1711 * thereby avoid all SSI overhead once it's running.
1712 */
1714 return GetSafeSnapshot(snapshot);
1715
1717 NULL, InvalidPid);
1718}
1719
1720/*
1721 * Import a snapshot to be used for the current transaction.
1722 *
1723 * This is nearly the same as GetSerializableTransactionSnapshot, except that
1724 * we don't take a new snapshot, but rather use the data we're handed.
1725 *
1726 * The caller must have verified that the snapshot came from a serializable
1727 * transaction; and if we're read-write, the source transaction must not be
1728 * read-only.
1729 */
1730void
1733 int sourcepid)
1734{
1736
1737 /*
1738 * If this is called by parallel.c in a parallel worker, we don't want to
1739 * create a SERIALIZABLEXACT just yet because the leader's
1740 * SERIALIZABLEXACT will be installed with AttachSerializableXact(). We
1741 * also don't want to reject SERIALIZABLE READ ONLY DEFERRABLE in this
1742 * case, because the leader has already determined that the snapshot it
1743 * has passed us is safe. So there is nothing for us to do.
1744 */
1745 if (IsParallelWorker())
1746 return;
1747
1748 /*
1749 * We do not allow SERIALIZABLE READ ONLY DEFERRABLE transactions to
1750 * import snapshots, since there's no way to wait for a safe snapshot when
1751 * we're using the snap we're told to. (XXX instead of throwing an error,
1752 * we could just ignore the XactDeferrable flag?)
1753 */
1755 ereport(ERROR,
1757 errmsg("a snapshot-importing transaction must not be READ ONLY DEFERRABLE")));
1758
1760 sourcepid);
1761}
1762
1763/*
1764 * Guts of GetSerializableTransactionSnapshot
1765 *
1766 * If sourcevxid is valid, this is actually an import operation and we should
1767 * skip calling GetSnapshotData, because the snapshot contents are already
1768 * loaded up. HOWEVER: to avoid race conditions, we must check that the
1769 * source xact is still running after we acquire SerializableXactHashLock.
1770 * We do that by calling ProcArrayInstallImportedXmin.
1771 */
1772static Snapshot
1775 int sourcepid)
1776{
1777 PGPROC *proc;
1780 *othersxact;
1781
1782 /* We only do this for serializable transactions. Once. */
1784
1786
1787 /*
1788 * Since all parts of a serializable transaction must use the same
1789 * snapshot, it is too late to establish one after a parallel operation
1790 * has begun.
1791 */
1792 if (IsInParallelMode())
1793 elog(ERROR, "cannot establish serializable snapshot during a parallel operation");
1794
1795 proc = MyProc;
1796 Assert(proc != NULL);
1797 GET_VXID_FROM_PGPROC(vxid, *proc);
1798
1799 /*
1800 * First we get the sxact structure, which may involve looping and access
1801 * to the "finished" list to free a structure for use.
1802 *
1803 * We must hold SerializableXactHashLock when taking/checking the snapshot
1804 * to avoid race conditions, for much the same reasons that
1805 * GetSnapshotData takes the ProcArrayLock. Since we might have to
1806 * release SerializableXactHashLock to call SummarizeOldestCommittedSxact,
1807 * this means we have to create the sxact first, which is a bit annoying
1808 * (in particular, an elog(ERROR) in procarray.c would cause us to leak
1809 * the sxact). Consider refactoring to avoid this.
1810 */
1811#ifdef TEST_SUMMARIZE_SERIAL
1813#endif
1815 do
1816 {
1818 /* If null, push out committed sxact to SLRU summary & retry. */
1819 if (!sxact)
1820 {
1824 }
1825 } while (!sxact);
1826
1827 /* Get the snapshot, or check that it's safe to use */
1828 if (!sourcevxid)
1829 snapshot = GetSnapshotData(snapshot);
1830 else if (!ProcArrayInstallImportedXmin(snapshot->xmin, sourcevxid))
1831 {
1834 ereport(ERROR,
1836 errmsg("could not import the requested snapshot"),
1837 errdetail("The source process with PID %d is not running anymore.",
1838 sourcepid)));
1839 }
1840
1841 /*
1842 * If there are no serializable transactions which are not read-only, we
1843 * can "opt out" of predicate locking and conflict checking for a
1844 * read-only transaction.
1845 *
1846 * The reason this is safe is that a read-only transaction can only become
1847 * part of a dangerous structure if it overlaps a writable transaction
1848 * which in turn overlaps a writable transaction which committed before
1849 * the read-only transaction started. A new writable transaction can
1850 * overlap this one, but it can't meet the other condition of overlapping
1851 * a transaction which committed before this one started.
1852 */
1854 {
1857 return snapshot;
1858 }
1859
1860 /* Initialize the structure. */
1861 sxact->vxid = vxid;
1862 sxact->SeqNo.lastCommitBeforeSnapshot = PredXact->LastSxactCommitSeqNo;
1863 sxact->prepareSeqNo = InvalidSerCommitSeqNo;
1864 sxact->commitSeqNo = InvalidSerCommitSeqNo;
1865 dlist_init(&(sxact->outConflicts));
1866 dlist_init(&(sxact->inConflicts));
1867 dlist_init(&(sxact->possibleUnsafeConflicts));
1868 sxact->topXid = GetTopTransactionIdIfAny();
1869 sxact->finishedBefore = InvalidTransactionId;
1870 sxact->xmin = snapshot->xmin;
1871 sxact->pid = MyProcPid;
1872 sxact->pgprocno = MyProcNumber;
1873 dlist_init(&sxact->predicateLocks);
1874 dlist_node_init(&sxact->finishedLink);
1875 sxact->flags = 0;
1876 if (XactReadOnly)
1877 {
1878 dlist_iter iter;
1879
1880 sxact->flags |= SXACT_FLAG_READ_ONLY;
1881
1882 /*
1883 * Register all concurrent r/w transactions as possible conflicts; if
1884 * all of them commit without any outgoing conflicts to earlier
1885 * transactions then this snapshot can be deemed safe (and we can run
1886 * without tracking predicate locks).
1887 */
1889 {
1891
1895 {
1897 }
1898 }
1899
1900 /*
1901 * If we didn't find any possibly unsafe conflicts because every
1902 * uncommitted writable transaction turned out to be doomed, then we
1903 * can "opt out" immediately. See comments above the earlier check
1904 * for PredXact->WritableSxactCount == 0.
1905 */
1906 if (dlist_is_empty(&sxact->possibleUnsafeConflicts))
1907 {
1910 return snapshot;
1911 }
1912 }
1913 else
1914 {
1918 }
1919
1920 /* Maintain serializable global xmin info. */
1922 {
1924 PredXact->SxactGlobalXmin = snapshot->xmin;
1926 SerialSetActiveSerXmin(snapshot->xmin);
1927 }
1928 else if (TransactionIdEquals(snapshot->xmin, PredXact->SxactGlobalXmin))
1929 {
1932 }
1933 else
1934 {
1936 }
1937
1939 MyXactDidWrite = false; /* haven't written anything yet */
1940
1942
1944
1945 return snapshot;
1946}
1947
1948static void
1950{
1952
1953 /* Initialize the backend-local hash table of parent locks */
1955 hash_ctl.keysize = sizeof(PREDICATELOCKTARGETTAG);
1956 hash_ctl.entrysize = sizeof(LOCALPREDICATELOCK);
1957 LocalPredicateLockHash = hash_create("Local predicate lock",
1959 &hash_ctl,
1961}
1962
1963/*
1964 * Register the top level XID in SerializableXidHash.
1965 * Also store it for easy reference in MySerializableXact.
1966 */
1967void
1969{
1972 bool found;
1973
1974 /*
1975 * If we're not tracking predicate lock data for this transaction, we
1976 * should ignore the request and return quickly.
1977 */
1979 return;
1980
1981 /* We should have a valid XID and be at the top level. */
1983
1985
1986 /* This should only be done once per transaction. */
1988
1990
1991 sxidtag.xid = xid;
1993 &sxidtag,
1994 HASH_ENTER, &found);
1995 Assert(!found);
1996
1997 /* Initialize the structure. */
1998 sxid->myXact = MySerializableXact;
2000}
2001
2002
2003/*
2004 * Check whether there are any predicate locks held by any transaction
2005 * for the page at the given block number.
2006 *
2007 * Note that the transaction may be completed but not yet subject to
2008 * cleanup due to overlapping serializable transactions. This must
2009 * return valid information regardless of transaction isolation level.
2010 *
2011 * Also note that this doesn't check for a conflicting relation lock,
2012 * just a lock specifically on the given page.
2013 *
2014 * One use is to support proper behavior during GiST index vacuum.
2015 */
2016bool
2040
2041
2042/*
2043 * Check whether a particular lock is held by this transaction.
2044 *
2045 * Important note: this function may return false even if the lock is
2046 * being held, because it uses the local lock table which is not
2047 * updated if another transaction modifies our lock list (e.g. to
2048 * split an index page). It can also return true when a coarser
2049 * granularity lock that covers this target is being held. Be careful
2050 * to only use this function in circumstances where such errors are
2051 * acceptable!
2052 */
2053static bool
2055{
2056 LOCALPREDICATELOCK *lock;
2057
2058 /* check local hash table */
2060 targettag,
2061 HASH_FIND, NULL);
2062
2063 if (!lock)
2064 return false;
2065
2066 /*
2067 * Found entry in the table, but still need to check whether it's actually
2068 * held -- it could just be a parent of some held lock.
2069 */
2070 return lock->held;
2071}
2072
2073/*
2074 * Return the parent lock tag in the lock hierarchy: the next coarser
2075 * lock that covers the provided tag.
2076 *
2077 * Returns true and sets *parent to the parent tag if one exists,
2078 * returns false if none exists.
2079 */
2080static bool
2082 PREDICATELOCKTARGETTAG *parent)
2083{
2084 switch (GET_PREDICATELOCKTARGETTAG_TYPE(*tag))
2085 {
2087 /* relation locks have no parent lock */
2088 return false;
2089
2090 case PREDLOCKTAG_PAGE:
2091 /* parent lock is relation lock */
2095
2096 return true;
2097
2098 case PREDLOCKTAG_TUPLE:
2099 /* parent lock is page lock */
2104 return true;
2105 }
2106
2107 /* not reachable */
2108 Assert(false);
2109 return false;
2110}
2111
2112/*
2113 * Check whether the lock we are considering is already covered by a
2114 * coarser lock for our transaction.
2115 *
2116 * Like PredicateLockExists, this function might return a false
2117 * negative, but it will never return a false positive.
2118 */
2119static bool
2121{
2123 parenttag;
2124
2126
2127 /* check parents iteratively until no more */
2129 {
2132 return true;
2133 }
2134
2135 /* no more parents to check; lock is not covered */
2136 return false;
2137}
2138
2139/*
2140 * Remove the dummy entry from the predicate lock target hash, to free up some
2141 * scratch space. The caller must be holding SerializablePredicateListLock,
2142 * and must restore the entry with RestoreScratchTarget() before releasing the
2143 * lock.
2144 *
2145 * If lockheld is true, the caller is already holding the partition lock
2146 * of the partition containing the scratch entry.
2147 */
2148static void
2165
2166/*
2167 * Re-insert the dummy entry in predicate lock target hash.
2168 */
2169static void
2186
2187/*
2188 * Check whether the list of related predicate locks is empty for a
2189 * predicate lock target, and remove the target if it is.
2190 */
2191static void
2193{
2195
2197
2198 /* Can't remove it until no locks at this target. */
2199 if (!dlist_is_empty(&target->predicateLocks))
2200 return;
2201
2202 /* Actually remove the target. */
2204 &target->tag,
2206 HASH_REMOVE, NULL);
2207 Assert(rmtarget == target);
2208}
2209
2210/*
2211 * Delete child target locks owned by this process.
2212 * This implementation is assuming that the usage of each target tag field
2213 * is uniform. No need to make this hard if we don't have to.
2214 *
2215 * We acquire an LWLock in the case of parallel mode, because worker
2216 * backends have access to the leader's SERIALIZABLEXACT. Otherwise,
2217 * we aren't acquiring LWLocks for the predicate lock or lock
2218 * target structures associated with this transaction unless we're going
2219 * to modify them, because no other process is permitted to modify our
2220 * locks.
2221 */
2222static void
2224{
2227 dlist_mutable_iter iter;
2228
2231 if (IsInParallelMode())
2232 LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
2233
2234 dlist_foreach_modify(iter, &sxact->predicateLocks)
2235 {
2239
2240 predlock = dlist_container(PREDICATELOCK, xactLink, iter.cur);
2241
2242 oldlocktag = predlock->tag;
2243 Assert(oldlocktag.myXact == sxact);
2244 oldtarget = oldlocktag.myTarget;
2245 oldtargettag = oldtarget->tag;
2246
2248 {
2252
2255
2257
2258 dlist_delete(&predlock->xactLink);
2259 dlist_delete(&predlock->targetLink);
2262 &oldlocktag,
2265 HASH_REMOVE, NULL);
2267
2269
2271
2273 }
2274 }
2275 if (IsInParallelMode())
2276 LWLockRelease(&sxact->perXactPredicateListLock);
2278}
2279
2280/*
2281 * Returns the promotion limit for a given predicate lock target. This is the
2282 * max number of descendant locks allowed before promoting to the specified
2283 * tag. Note that the limit includes non-direct descendants (e.g., both tuples
2284 * and pages for a relation lock).
2285 *
2286 * Currently the default limit is 2 for a page lock, and half of the value of
2287 * max_pred_locks_per_transaction - 1 for a relation lock, to match behavior
2288 * of earlier releases when upgrading.
2289 *
2290 * TODO SSI: We should probably add additional GUCs to allow a maximum ratio
2291 * of page and tuple locks based on the pages in a relation, and the maximum
2292 * ratio of tuple locks to tuples in a page. This would provide more
2293 * generally "balanced" allocation of locks to where they are most useful,
2294 * while still allowing the absolute numbers to prevent one relation from
2295 * tying up all predicate lock resources.
2296 */
2297static int
2299{
2300 switch (GET_PREDICATELOCKTARGETTAG_TYPE(*tag))
2301 {
2307
2308 case PREDLOCKTAG_PAGE:
2310
2311 case PREDLOCKTAG_TUPLE:
2312
2313 /*
2314 * not reachable: nothing is finer-granularity than a tuple, so we
2315 * should never try to promote to it.
2316 */
2317 Assert(false);
2318 return 0;
2319 }
2320
2321 /* not reachable */
2322 Assert(false);
2323 return 0;
2324}
2325
2326/*
2327 * For all ancestors of a newly-acquired predicate lock, increment
2328 * their child count in the parent hash table. If any of them have
2329 * more descendants than their promotion threshold, acquire the
2330 * coarsest such lock.
2331 *
2332 * Returns true if a parent lock was acquired and false otherwise.
2333 */
2334static bool
2336{
2338 nexttag,
2341 bool found,
2342 promote;
2343
2344 promote = false;
2345
2346 targettag = *reqtag;
2347
2348 /* check parents iteratively */
2350 {
2353 &targettag,
2354 HASH_ENTER,
2355 &found);
2356 if (!found)
2357 {
2358 parentlock->held = false;
2359 parentlock->childLocks = 1;
2360 }
2361 else
2362 parentlock->childLocks++;
2363
2364 if (parentlock->childLocks >
2366 {
2367 /*
2368 * We should promote to this parent lock. Continue to check its
2369 * ancestors, however, both to get their child counts right and to
2370 * check whether we should just go ahead and promote to one of
2371 * them.
2372 */
2374 promote = true;
2375 }
2376 }
2377
2378 if (promote)
2379 {
2380 /* acquire coarsest ancestor eligible for promotion */
2382 return true;
2383 }
2384 else
2385 return false;
2386}
2387
2388/*
2389 * When releasing a lock, decrement the child count on all ancestor
2390 * locks.
2391 *
2392 * This is called only when releasing a lock via
2393 * DeleteChildTargetLocks (i.e. when a lock becomes redundant because
2394 * we've acquired its parent, possibly due to promotion) or when a new
2395 * MVCC write lock makes the predicate lock unnecessary. There's no
2396 * point in calling it when locks are released at transaction end, as
2397 * this information is no longer needed.
2398 */
2399static void
2401{
2403 nexttag;
2404
2406
2408 {
2412
2418 HASH_FIND, NULL);
2419
2420 /*
2421 * There's a small chance the parent lock doesn't exist in the lock
2422 * table. This can happen if we prematurely removed it because an
2423 * index split caused the child refcount to be off.
2424 */
2425 if (parentlock == NULL)
2426 continue;
2427
2428 parentlock->childLocks--;
2429
2430 /*
2431 * Under similar circumstances the parent lock's refcount might be
2432 * zero. This only happens if we're holding that lock (otherwise we
2433 * would have removed the entry).
2434 */
2435 if (parentlock->childLocks < 0)
2436 {
2437 Assert(parentlock->held);
2438 parentlock->childLocks = 0;
2439 }
2440
2441 if ((parentlock->childLocks == 0) && (!parentlock->held))
2442 {
2446 HASH_REMOVE, NULL);
2448 }
2449 }
2450}
2451
2452/*
2453 * Indicate that a predicate lock on the given target is held by the
2454 * specified transaction. Has no effect if the lock is already held.
2455 *
2456 * This updates the lock table and the sxact's lock list, and creates
2457 * the lock target if necessary, but does *not* do anything related to
2458 * granularity promotion or the local lock table. See
2459 * PredicateLockAcquire for that.
2460 */
2461static void
2465{
2466 PREDICATELOCKTARGET *target;
2467 PREDICATELOCKTAG locktag;
2468 PREDICATELOCK *lock;
2470 bool found;
2471
2473
2475 if (IsInParallelMode())
2476 LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
2478
2479 /* Make sure that the target is represented. */
2480 target = (PREDICATELOCKTARGET *)
2483 HASH_ENTER_NULL, &found);
2484 if (!target)
2485 ereport(ERROR,
2487 errmsg("out of shared memory"),
2488 errhint("You might need to increase \"%s\".", "max_pred_locks_per_transaction")));
2489 if (!found)
2490 dlist_init(&target->predicateLocks);
2491
2492 /* We've got the sxact and target, make sure they're joined. */
2493 locktag.myTarget = target;
2494 locktag.myXact = sxact;
2495 lock = (PREDICATELOCK *)
2498 HASH_ENTER_NULL, &found);
2499 if (!lock)
2500 ereport(ERROR,
2502 errmsg("out of shared memory"),
2503 errhint("You might need to increase \"%s\".", "max_pred_locks_per_transaction")));
2504
2505 if (!found)
2506 {
2507 dlist_push_tail(&target->predicateLocks, &lock->targetLink);
2508 dlist_push_tail(&sxact->predicateLocks, &lock->xactLink);
2510 }
2511
2513 if (IsInParallelMode())
2514 LWLockRelease(&sxact->perXactPredicateListLock);
2516}
2517
2518/*
2519 * Acquire a predicate lock on the specified target for the current
2520 * connection if not already held. This updates the local lock table
2521 * and uses it to implement granularity promotion. It will consolidate
2522 * multiple locks into a coarser lock if warranted, and will release
2523 * any finer-grained locks covered by the new one.
2524 */
2525static void
2527{
2529 bool found;
2531
2532 /* Do we have the lock already, or a covering lock? */
2534 return;
2535
2537 return;
2538
2539 /* the same hash and LW lock apply to the lock target and the local lock. */
2541
2542 /* Acquire lock in local table */
2546 HASH_ENTER, &found);
2547 locallock->held = true;
2548 if (!found)
2549 locallock->childLocks = 0;
2550
2551 /* Actually create the lock */
2553
2554 /*
2555 * Lock has been acquired. Check whether it should be promoted to a
2556 * coarser granularity, or whether there are finer-granularity locks to
2557 * clean up.
2558 */
2560 {
2561 /*
2562 * Lock request was promoted to a coarser-granularity lock, and that
2563 * lock was acquired. It will delete this lock and any of its
2564 * children, so we're done.
2565 */
2566 }
2567 else
2568 {
2569 /* Clean up any finer-granularity locks */
2572 }
2573}
2574
2575
2576/*
2577 * PredicateLockRelation
2578 *
2579 * Gets a predicate lock at the relation level.
2580 * Skip if not in full serializable transaction isolation level.
2581 * Skip if this is a temporary table.
2582 * Clear any finer-grained predicate locks this session has on the relation.
2583 */
2584void
2586{
2588
2589 if (!SerializationNeededForRead(relation, snapshot))
2590 return;
2591
2593 relation->rd_locator.dbOid,
2594 relation->rd_id);
2596}
2597
2598/*
2599 * PredicateLockPage
2600 *
2601 * Gets a predicate lock at the page level.
2602 * Skip if not in full serializable transaction isolation level.
2603 * Skip if this is a temporary table.
2604 * Skip if a coarser predicate lock already covers this page.
2605 * Clear any finer-grained predicate locks this session has on the relation.
2606 */
2607void
2609{
2611
2612 if (!SerializationNeededForRead(relation, snapshot))
2613 return;
2614
2616 relation->rd_locator.dbOid,
2617 relation->rd_id,
2618 blkno);
2620}
2621
2622/*
2623 * PredicateLockTID
2624 *
2625 * Gets a predicate lock at the tuple level.
2626 * Skip if not in full serializable transaction isolation level.
2627 * Skip if this is a temporary table.
2628 */
2629void
2630PredicateLockTID(Relation relation, const ItemPointerData *tid, Snapshot snapshot,
2632{
2634
2635 if (!SerializationNeededForRead(relation, snapshot))
2636 return;
2637
2638 /*
2639 * Return if this xact wrote it.
2640 */
2641 if (relation->rd_index == NULL)
2642 {
2643 /* If we wrote it; we already have a write lock. */
2645 return;
2646 }
2647
2648 /*
2649 * Do quick-but-not-definitive test for a relation lock first. This will
2650 * never cause a return when the relation is *not* locked, but will
2651 * occasionally let the check continue when there really *is* a relation
2652 * level lock.
2653 */
2655 relation->rd_locator.dbOid,
2656 relation->rd_id);
2657 if (PredicateLockExists(&tag))
2658 return;
2659
2661 relation->rd_locator.dbOid,
2662 relation->rd_id,
2666}
2667
2668
2669/*
2670 * DeleteLockTarget
2671 *
2672 * Remove a predicate lock target along with any locks held for it.
2673 *
2674 * Caller must hold SerializablePredicateListLock and the
2675 * appropriate hash partition lock for the target.
2676 */
2677static void
2679{
2680 dlist_mutable_iter iter;
2681
2683 LW_EXCLUSIVE));
2685
2687
2688 dlist_foreach_modify(iter, &target->predicateLocks)
2689 {
2691 dlist_container(PREDICATELOCK, targetLink, iter.cur);
2692 bool found;
2693
2694 dlist_delete(&(predlock->xactLink));
2695 dlist_delete(&(predlock->targetLink));
2696
2699 &predlock->tag,
2702 HASH_REMOVE, &found);
2703 Assert(found);
2704 }
2706
2707 /* Remove the target itself, if possible. */
2709}
2710
2711
2712/*
2713 * TransferPredicateLocksToNewTarget
2714 *
2715 * Move or copy all the predicate locks for a lock target, for use by
2716 * index page splits/combines and other things that create or replace
2717 * lock targets. If 'removeOld' is true, the old locks and the target
2718 * will be removed.
2719 *
2720 * Returns true on success, or false if we ran out of shared memory to
2721 * allocate the new target or locks. Guaranteed to always succeed if
2722 * removeOld is set (by using the scratch entry in PredicateLockTargetHash
2723 * for scratch space).
2724 *
2725 * Warning: the "removeOld" option should be used only with care,
2726 * because this function does not (indeed, can not) update other
2727 * backends' LocalPredicateLockHash. If we are only adding new
2728 * entries, this is not a problem: the local lock table is used only
2729 * as a hint, so missing entries for locks that are held are
2730 * OK. Having entries for locks that are no longer held, as can happen
2731 * when using "removeOld", is not in general OK. We can only use it
2732 * safely when replacing a lock with a coarser-granularity lock that
2733 * covers it, or if we are absolutely certain that no one will need to
2734 * refer to that lock in the future.
2735 *
2736 * Caller must hold SerializablePredicateListLock exclusively.
2737 */
2738static bool
2741 bool removeOld)
2742{
2748 bool found;
2749 bool outOfShmem = false;
2750
2752 LW_EXCLUSIVE));
2753
2758
2759 if (removeOld)
2760 {
2761 /*
2762 * Remove the dummy entry to give us scratch space, so we know we'll
2763 * be able to create the new lock target.
2764 */
2765 RemoveScratchTarget(false);
2766 }
2767
2768 /*
2769 * We must get the partition locks in ascending sequence to avoid
2770 * deadlocks. If old and new partitions are the same, we must request the
2771 * lock only once.
2772 */
2774 {
2778 }
2780 {
2784 }
2785 else
2787
2788 /*
2789 * Look for the old target. If not found, that's OK; no predicate locks
2790 * are affected, so we can just clean up and return. If it does exist,
2791 * walk its list of predicate locks and move or copy them to the new
2792 * target.
2793 */
2795 &oldtargettag,
2797 HASH_FIND, NULL);
2798
2799 if (oldtarget)
2800 {
2803 dlist_mutable_iter iter;
2804
2806 &newtargettag,
2808 HASH_ENTER_NULL, &found);
2809
2810 if (!newtarget)
2811 {
2812 /* Failed to allocate due to insufficient shmem */
2813 outOfShmem = true;
2814 goto exit;
2815 }
2816
2817 /* If we created a new entry, initialize it */
2818 if (!found)
2819 dlist_init(&newtarget->predicateLocks);
2820
2821 newpredlocktag.myTarget = newtarget;
2822
2823 /*
2824 * Loop through all the locks on the old target, replacing them with
2825 * locks on the new target.
2826 */
2828
2829 dlist_foreach_modify(iter, &oldtarget->predicateLocks)
2830 {
2832 dlist_container(PREDICATELOCK, targetLink, iter.cur);
2835
2836 newpredlocktag.myXact = oldpredlock->tag.myXact;
2837
2838 if (removeOld)
2839 {
2840 dlist_delete(&(oldpredlock->xactLink));
2841 dlist_delete(&(oldpredlock->targetLink));
2842
2845 &oldpredlock->tag,
2848 HASH_REMOVE, &found);
2849 Assert(found);
2850 }
2851
2858 &found);
2859 if (!newpredlock)
2860 {
2861 /* Out of shared memory. Undo what we've done so far. */
2864 outOfShmem = true;
2865 goto exit;
2866 }
2867 if (!found)
2868 {
2869 dlist_push_tail(&(newtarget->predicateLocks),
2870 &(newpredlock->targetLink));
2871 dlist_push_tail(&(newpredlocktag.myXact->predicateLocks),
2872 &(newpredlock->xactLink));
2873 newpredlock->commitSeqNo = oldCommitSeqNo;
2874 }
2875 else
2876 {
2877 if (newpredlock->commitSeqNo < oldCommitSeqNo)
2878 newpredlock->commitSeqNo = oldCommitSeqNo;
2879 }
2880
2881 Assert(newpredlock->commitSeqNo != 0);
2882 Assert((newpredlock->commitSeqNo == InvalidSerCommitSeqNo)
2883 || (newpredlock->tag.myXact == OldCommittedSxact));
2884 }
2886
2887 if (removeOld)
2888 {
2889 Assert(dlist_is_empty(&oldtarget->predicateLocks));
2891 }
2892 }
2893
2894
2895exit:
2896 /* Release partition locks in reverse order of acquisition. */
2898 {
2901 }
2903 {
2906 }
2907 else
2909
2910 if (removeOld)
2911 {
2912 /* We shouldn't run out of memory if we're moving locks */
2914
2915 /* Put the scratch entry back */
2916 RestoreScratchTarget(false);
2917 }
2918
2919 return !outOfShmem;
2920}
2921
2922/*
2923 * Drop all predicate locks of any granularity from the specified relation,
2924 * which can be a heap relation or an index relation. If 'transfer' is true,
2925 * acquire a relation lock on the heap for any transactions with any lock(s)
2926 * on the specified relation.
2927 *
2928 * This requires grabbing a lot of LW locks and scanning the entire lock
2929 * target table for matches. That makes this more expensive than most
2930 * predicate lock management functions, but it will only be called for DDL
2931 * type commands that are expensive anyway, and there are fast returns when
2932 * no serializable transactions are active or the relation is temporary.
2933 *
2934 * We don't use the TransferPredicateLocksToNewTarget function because it
2935 * acquires its own locks on the partitions of the two targets involved,
2936 * and we'll already be holding all partition locks.
2937 *
2938 * We can't throw an error from here, because the call could be from a
2939 * transaction which is not serializable.
2940 *
2941 * NOTE: This is currently only called with transfer set to true, but that may
2942 * change. If we decide to clean up the locks from a table on commit of a
2943 * transaction which executed DROP TABLE, the false condition will be useful.
2944 */
2945static void
2947{
2951 Oid dbId;
2952 Oid relId;
2953 Oid heapId;
2954 int i;
2955 bool isIndex;
2956 bool found;
2958
2959 /*
2960 * Bail out quickly if there are no serializable transactions running.
2961 * It's safe to check this without taking locks because the caller is
2962 * holding an ACCESS EXCLUSIVE lock on the relation. No new locks which
2963 * would matter here can be acquired while that is held.
2964 */
2966 return;
2967
2968 if (!PredicateLockingNeededForRelation(relation))
2969 return;
2970
2971 dbId = relation->rd_locator.dbOid;
2972 relId = relation->rd_id;
2973 if (relation->rd_index == NULL)
2974 {
2975 isIndex = false;
2976 heapId = relId;
2977 }
2978 else
2979 {
2980 isIndex = true;
2981 heapId = relation->rd_index->indrelid;
2982 }
2984 Assert(transfer || !isIndex); /* index OID only makes sense with
2985 * transfer */
2986
2987 /* Retrieve first time needed, then keep. */
2989 heaptarget = NULL;
2990
2991 /* Acquire locks on all lock partitions */
2993 for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
2996
2997 /*
2998 * Remove the dummy entry to give us scratch space, so we know we'll be
2999 * able to create the new lock target.
3000 */
3001 if (transfer)
3002 RemoveScratchTarget(true);
3003
3004 /* Scan through target map */
3006
3008 {
3009 dlist_mutable_iter iter;
3010
3011 /*
3012 * Check whether this is a target which needs attention.
3013 */
3015 continue; /* wrong relation id */
3016 if (GET_PREDICATELOCKTARGETTAG_DB(oldtarget->tag) != dbId)
3017 continue; /* wrong database id */
3018 if (transfer && !isIndex
3020 continue; /* already the right lock */
3021
3022 /*
3023 * If we made it here, we have work to do. We make sure the heap
3024 * relation lock exists, then we walk the list of predicate locks for
3025 * the old target we found, moving all locks to the heap relation lock
3026 * -- unless they already hold that.
3027 */
3028
3029 /*
3030 * First make sure we have the heap relation target. We only need to
3031 * do this once.
3032 */
3033 if (transfer && heaptarget == NULL)
3034 {
3036
3042 HASH_ENTER, &found);
3043 if (!found)
3044 dlist_init(&heaptarget->predicateLocks);
3045 }
3046
3047 /*
3048 * Loop through all the locks on the old target, replacing them with
3049 * locks on the new target.
3050 */
3051 dlist_foreach_modify(iter, &oldtarget->predicateLocks)
3052 {
3054 dlist_container(PREDICATELOCK, targetLink, iter.cur);
3058
3059 /*
3060 * Remove the old lock first. This avoids the chance of running
3061 * out of lock structure entries for the hash table.
3062 */
3064 oldXact = oldpredlock->tag.myXact;
3065
3066 dlist_delete(&(oldpredlock->xactLink));
3067
3068 /*
3069 * No need for retail delete from oldtarget list, we're removing
3070 * the whole target anyway.
3071 */
3073 &oldpredlock->tag,
3074 HASH_REMOVE, &found);
3075 Assert(found);
3076
3077 if (transfer)
3078 {
3080
3082 newpredlocktag.myXact = oldXact;
3088 HASH_ENTER,
3089 &found);
3090 if (!found)
3091 {
3092 dlist_push_tail(&(heaptarget->predicateLocks),
3093 &(newpredlock->targetLink));
3094 dlist_push_tail(&(newpredlocktag.myXact->predicateLocks),
3095 &(newpredlock->xactLink));
3096 newpredlock->commitSeqNo = oldCommitSeqNo;
3097 }
3098 else
3099 {
3100 if (newpredlock->commitSeqNo < oldCommitSeqNo)
3101 newpredlock->commitSeqNo = oldCommitSeqNo;
3102 }
3103
3104 Assert(newpredlock->commitSeqNo != 0);
3105 Assert((newpredlock->commitSeqNo == InvalidSerCommitSeqNo)
3106 || (newpredlock->tag.myXact == OldCommittedSxact));
3107 }
3108 }
3109
3111 &found);
3112 Assert(found);
3113 }
3114
3115 /* Put the scratch entry back */
3116 if (transfer)
3118
3119 /* Release locks in reverse order */
3121 for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
3124}
3125
3126/*
3127 * TransferPredicateLocksToHeapRelation
3128 * For all transactions, transfer all predicate locks for the given
3129 * relation to a single relation lock on the heap.
3130 */
3131void
3136
3137
3138/*
3139 * PredicateLockPageSplit
3140 *
3141 * Copies any predicate locks for the old page to the new page.
3142 * Skip if this is a temporary table or toast table.
3143 *
3144 * NOTE: A page split (or overflow) affects all serializable transactions,
3145 * even if it occurs in the context of another transaction isolation level.
3146 *
3147 * NOTE: This currently leaves the local copy of the locks without
3148 * information on the new lock which is in shared memory. This could cause
3149 * problems if enough page splits occur on locked pages without the processes
3150 * which hold the locks getting in and noticing.
3151 */
3152void
3155{
3158 bool success;
3159
3160 /*
3161 * Bail out quickly if there are no serializable transactions running.
3162 *
3163 * It's safe to do this check without taking any additional locks. Even if
3164 * a serializable transaction starts concurrently, we know it can't take
3165 * any SIREAD locks on the page being split because the caller is holding
3166 * the associated buffer page lock. Memory reordering isn't an issue; the
3167 * memory barrier in the LWLock acquisition guarantees that this read
3168 * occurs while the buffer page lock is held.
3169 */
3171 return;
3172
3173 if (!PredicateLockingNeededForRelation(relation))
3174 return;
3175
3179
3181 relation->rd_locator.dbOid,
3182 relation->rd_id,
3183 oldblkno);
3185 relation->rd_locator.dbOid,
3186 relation->rd_id,
3187 newblkno);
3188
3190
3191 /*
3192 * Try copying the locks over to the new page's tag, creating it if
3193 * necessary.
3194 */
3197 false);
3198
3199 if (!success)
3200 {
3201 /*
3202 * No more predicate lock entries are available. Failure isn't an
3203 * option here, so promote the page lock to a relation lock.
3204 */
3205
3206 /* Get the parent relation lock's lock tag */
3208 &newtargettag);
3209 Assert(success);
3210
3211 /*
3212 * Move the locks to the parent. This shouldn't fail.
3213 *
3214 * Note that here we are removing locks held by other backends,
3215 * leading to a possible inconsistency in their local lock hash table.
3216 * This is OK because we're replacing it with a lock that covers the
3217 * old one.
3218 */
3221 true);
3222 Assert(success);
3223 }
3224
3226}
3227
3228/*
3229 * PredicateLockPageCombine
3230 *
3231 * Combines predicate locks for two existing pages.
3232 * Skip if this is a temporary table or toast table.
3233 *
3234 * NOTE: A page combine affects all serializable transactions, even if it
3235 * occurs in the context of another transaction isolation level.
3236 */
3237void
3240{
3241 /*
3242 * Page combines differ from page splits in that we ought to be able to
3243 * remove the locks on the old page after transferring them to the new
3244 * page, instead of duplicating them. However, because we can't edit other
3245 * backends' local lock tables, removing the old lock would leave them
3246 * with an entry in their LocalPredicateLockHash for a lock they're not
3247 * holding, which isn't acceptable. So we wind up having to do the same
3248 * work as a page split, acquiring a lock on the new page and keeping the
3249 * old page locked too. That can lead to some false positives, but should
3250 * be rare in practice.
3251 */
3253}
3254
3255/*
3256 * Walk the list of in-progress serializable transactions and find the new
3257 * xmin.
3258 */
3259static void
3294
3295/*
3296 * ReleasePredicateLocks
3297 *
3298 * Releases predicate locks based on completion of the current transaction,
3299 * whether committed or rolled back. It can also be called for a read only
3300 * transaction when it becomes impossible for the transaction to become
3301 * part of a dangerous structure.
3302 *
3303 * We do nothing unless this is a serializable transaction.
3304 *
3305 * This method must ensure that shared memory hash tables are cleaned
3306 * up in some relatively timely fashion.
3307 *
3308 * If this transaction is committing and is holding any predicate locks,
3309 * it must be added to a list of completed serializable transactions still
3310 * holding locks.
3311 *
3312 * If isReadOnlySafe is true, then predicate locks are being released before
3313 * the end of the transaction because MySerializableXact has been determined
3314 * to be RO_SAFE. In non-parallel mode we can release it completely, but it
3315 * in parallel mode we partially release the SERIALIZABLEXACT and keep it
3316 * around until the end of the transaction, allowing each backend to clear its
3317 * MySerializableXact variable and benefit from the optimization in its own
3318 * time.
3319 */
3320void
3322{
3323 bool partiallyReleasing = false;
3324 bool needToClear;
3326 dlist_mutable_iter iter;
3327
3328 /*
3329 * We can't trust XactReadOnly here, because a transaction which started
3330 * as READ WRITE can show as READ ONLY later, e.g., within
3331 * subtransactions. We want to flag a transaction as READ ONLY if it
3332 * commits without writing so that de facto READ ONLY transactions get the
3333 * benefit of some RO optimizations, so we will use this local variable to
3334 * get some cleanup logic right which is based on whether the transaction
3335 * was declared READ ONLY at the top level.
3336 */
3338
3339 /* We can't be both committing and releasing early due to RO_SAFE. */
3341
3342 /* Are we at the end of a transaction, that is, a commit or abort? */
3343 if (!isReadOnlySafe)
3344 {
3345 /*
3346 * Parallel workers mustn't release predicate locks at the end of
3347 * their transaction. The leader will do that at the end of its
3348 * transaction.
3349 */
3350 if (IsParallelWorker())
3351 {
3353 return;
3354 }
3355
3356 /*
3357 * By the time the leader in a parallel query reaches end of
3358 * transaction, it has waited for all workers to exit.
3359 */
3361
3362 /*
3363 * If the leader in a parallel query earlier stashed a partially
3364 * released SERIALIZABLEXACT for final clean-up at end of transaction
3365 * (because workers might still have been accessing it), then it's
3366 * time to restore it.
3367 */
3369 {
3374 }
3375 }
3376
3378 {
3380 return;
3381 }
3382
3384
3385 /*
3386 * If the transaction is committing, but it has been partially released
3387 * already, then treat this as a roll back. It was marked as rolled back.
3388 */
3390 isCommit = false;
3391
3392 /*
3393 * If we're called in the middle of a transaction because we discovered
3394 * that the SXACT_FLAG_RO_SAFE flag was set, then we'll partially release
3395 * it (that is, release the predicate locks and conflicts, but not the
3396 * SERIALIZABLEXACT itself) if we're the first backend to have noticed.
3397 */
3399 {
3400 /*
3401 * The leader needs to stash a pointer to it, so that it can
3402 * completely release it at end-of-transaction.
3403 */
3404 if (!IsParallelWorker())
3406
3407 /*
3408 * The first backend to reach this condition will partially release
3409 * the SERIALIZABLEXACT. All others will just clear their
3410 * backend-local state so that they stop doing SSI checks for the rest
3411 * of the transaction.
3412 */
3414 {
3417 return;
3418 }
3419 else
3420 {
3422 partiallyReleasing = true;
3423 /* ... and proceed to perform the partial release below. */
3424 }
3425 }
3431
3432 /* may not be serializable during COMMIT/ROLLBACK PREPARED */
3434
3435 /* We'd better not already be on the cleanup list. */
3437
3439
3440 /*
3441 * We don't hold XidGenLock lock here, assuming that TransactionId is
3442 * atomic!
3443 *
3444 * If this value is changing, we don't care that much whether we get the
3445 * old or new value -- it is just used to determine how far
3446 * SxactGlobalXmin must advance before this transaction can be fully
3447 * cleaned up. The worst that could happen is we wait for one more
3448 * transaction to complete before freeing some RAM; correctness of visible
3449 * behavior is not affected.
3450 */
3452
3453 /*
3454 * If it's not a commit it's either a rollback or a read-only transaction
3455 * flagged SXACT_FLAG_RO_SAFE, and we can clear our locks immediately.
3456 */
3457 if (isCommit)
3458 {
3461 /* Recognize implicit read-only transaction (commit without write). */
3462 if (!MyXactDidWrite)
3464 }
3465 else
3466 {
3467 /*
3468 * The DOOMED flag indicates that we intend to roll back this
3469 * transaction and so it should not cause serialization failures for
3470 * other transactions that conflict with it. Note that this flag might
3471 * already be set, if another backend marked this transaction for
3472 * abort.
3473 *
3474 * The ROLLED_BACK flag further indicates that ReleasePredicateLocks
3475 * has been called, and so the SerializableXact is eligible for
3476 * cleanup. This means it should not be considered when calculating
3477 * SxactGlobalXmin.
3478 */
3481
3482 /*
3483 * If the transaction was previously prepared, but is now failing due
3484 * to a ROLLBACK PREPARED or (hopefully very rare) error after the
3485 * prepare, clear the prepared flag. This simplifies conflict
3486 * checking.
3487 */
3489 }
3490
3492 {
3494 if (--(PredXact->WritableSxactCount) == 0)
3495 {
3496 /*
3497 * Release predicate locks and rw-conflicts in for all committed
3498 * transactions. There are no longer any transactions which might
3499 * conflict with the locks and no chance for new transactions to
3500 * overlap. Similarly, existing conflicts in can't cause pivots,
3501 * and any conflicts in which could have completed a dangerous
3502 * structure would already have caused a rollback, so any
3503 * remaining ones must be benign.
3504 */
3506 }
3507 }
3508 else
3509 {
3510 /*
3511 * Read-only transactions: clear the list of transactions that might
3512 * make us unsafe. Note that we use 'inLink' for the iteration as
3513 * opposed to 'outLink' for the r/w xacts.
3514 */
3516 {
3518 dlist_container(RWConflictData, inLink, iter.cur);
3519
3522
3524 }
3525 }
3526
3527 /* Check for conflict out to old committed transactions. */
3528 if (isCommit
3531 {
3532 /*
3533 * we don't know which old committed transaction we conflicted with,
3534 * so be conservative and use FirstNormalSerCommitSeqNo here
3535 */
3539 }
3540
3541 /*
3542 * Release all outConflicts to committed transactions. If we're rolling
3543 * back clear them all. Set SXACT_FLAG_CONFLICT_OUT if any point to
3544 * previously committed transactions.
3545 */
3547 {
3549 dlist_container(RWConflictData, outLink, iter.cur);
3550
3551 if (isCommit
3553 && SxactIsCommitted(conflict->sxactIn))
3554 {
3556 || conflict->sxactIn->prepareSeqNo < MySerializableXact->SeqNo.earliestOutConflictCommit)
3559 }
3560
3561 if (!isCommit
3562 || SxactIsCommitted(conflict->sxactIn)
3563 || (conflict->sxactIn->SeqNo.lastCommitBeforeSnapshot >= PredXact->LastSxactCommitSeqNo))
3565 }
3566
3567 /*
3568 * Release all inConflicts from committed and read-only transactions. If
3569 * we're rolling back, clear them all.
3570 */
3572 {
3574 dlist_container(RWConflictData, inLink, iter.cur);
3575
3576 if (!isCommit
3577 || SxactIsCommitted(conflict->sxactOut)
3578 || SxactIsReadOnly(conflict->sxactOut))
3580 }
3581
3583 {
3584 /*
3585 * Remove ourselves from the list of possible conflicts for concurrent
3586 * READ ONLY transactions, flagging them as unsafe if we have a
3587 * conflict out. If any are waiting DEFERRABLE transactions, wake them
3588 * up if they are known safe or known unsafe.
3589 */
3591 {
3593 dlist_container(RWConflictData, outLink, iter.cur);
3594
3595 roXact = possibleUnsafeConflict->sxactIn;
3598
3599 /* Mark conflicted if necessary. */
3600 if (isCommit
3604 <= roXact->SeqNo.lastCommitBeforeSnapshot))
3605 {
3606 /*
3607 * This releases possibleUnsafeConflict (as well as all other
3608 * possible conflicts for roXact)
3609 */
3611 }
3612 else
3613 {
3615
3616 /*
3617 * If we were the last possible conflict, flag it safe. The
3618 * transaction can now safely release its predicate locks (but
3619 * that transaction's backend has to do that itself).
3620 */
3621 if (dlist_is_empty(&roXact->possibleUnsafeConflicts))
3622 roXact->flags |= SXACT_FLAG_RO_SAFE;
3623 }
3624
3625 /*
3626 * Wake up the process for a waiting DEFERRABLE transaction if we
3627 * now know it's either safe or conflicted.
3628 */
3631 ProcSendSignal(roXact->pgprocno);
3632 }
3633 }
3634
3635 /*
3636 * Check whether it's time to clean up old transactions. This can only be
3637 * done when the last serializable transaction with the oldest xmin among
3638 * serializable transactions completes. We then find the "new oldest"
3639 * xmin and purge any transactions which finished before this transaction
3640 * was launched.
3641 *
3642 * For parallel queries in read-only transactions, it might run twice. We
3643 * only release the reference on the first call.
3644 */
3645 needToClear = false;
3646 if ((partiallyReleasing ||
3650 {
3652 if (--(PredXact->SxactGlobalXminCount) == 0)
3653 {
3655 needToClear = true;
3656 }
3657 }
3658
3660
3662
3663 /* Add this to the list of transactions to check for later cleanup. */
3664 if (isCommit)
3667
3668 /*
3669 * If we're releasing a RO_SAFE transaction in parallel mode, we'll only
3670 * partially release it. That's necessary because other backends may have
3671 * a reference to it. The leader will release the SERIALIZABLEXACT itself
3672 * at the end of the transaction after workers have stopped running.
3673 */
3674 if (!isCommit)
3677 false);
3678
3680
3681 if (needToClear)
3683
3685}
3686
3687static void
3689{
3691 MyXactDidWrite = false;
3692
3693 /* Delete per-transaction lock table */
3695 {
3698 }
3699}
3700
3701/*
3702 * Clear old predicate locks, belonging to committed transactions that are no
3703 * longer interesting to any in-progress transaction.
3704 */
3705static void
3707{
3708 dlist_mutable_iter iter;
3709
3710 /*
3711 * Loop through finished transactions. They are in commit order, so we can
3712 * stop as soon as we find one that's still interesting.
3713 */
3717 {
3719 dlist_container(SERIALIZABLEXACT, finishedLink, iter.cur);
3720
3724 {
3725 /*
3726 * This transaction committed before any in-progress transaction
3727 * took its snapshot. It's no longer interesting.
3728 */
3730 dlist_delete_thoroughly(&finishedSxact->finishedLink);
3733 }
3734 else if (finishedSxact->commitSeqNo > PredXact->HavePartialClearedThrough
3735 && finishedSxact->commitSeqNo <= PredXact->CanPartialClearThrough)
3736 {
3737 /*
3738 * Any active transactions that took their snapshot before this
3739 * transaction committed are read-only, so we can clear part of
3740 * its state.
3741 */
3743
3745 {
3746 /* A read-only transaction can be removed entirely */
3747 dlist_delete_thoroughly(&(finishedSxact->finishedLink));
3749 }
3750 else
3751 {
3752 /*
3753 * A read-write transaction can only be partially cleared. We
3754 * need to keep the SERIALIZABLEXACT but can release the
3755 * SIREAD locks and conflicts in.
3756 */
3758 }
3759
3762 }
3763 else
3764 {
3765 /* Still interesting. */
3766 break;
3767 }
3768 }
3770
3771 /*
3772 * Loop through predicate locks on dummy transaction for summarized data.
3773 */
3776 {
3778 dlist_container(PREDICATELOCK, xactLink, iter.cur);
3780
3782 Assert(predlock->commitSeqNo != 0);
3783 Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
3786
3787 /*
3788 * If this lock originally belonged to an old enough transaction, we
3789 * can release it.
3790 */
3792 {
3793 PREDICATELOCKTAG tag;
3794 PREDICATELOCKTARGET *target;
3798
3799 tag = predlock->tag;
3800 target = tag.myTarget;
3801 targettag = target->tag;
3804
3806
3807 dlist_delete(&(predlock->targetLink));
3808 dlist_delete(&(predlock->xactLink));
3809
3813 HASH_REMOVE, NULL);
3815
3817 }
3818 }
3819
3822}
3823
3824/*
3825 * This is the normal way to delete anything from any of the predicate
3826 * locking hash tables. Given a transaction which we know can be deleted:
3827 * delete all predicate locks held by that transaction and any predicate
3828 * lock targets which are now unreferenced by a lock; delete all conflicts
3829 * for the transaction; delete all xid values for the transaction; then
3830 * delete the transaction.
3831 *
3832 * When the partial flag is set, we can release all predicate locks and
3833 * in-conflict information -- we've established that there are no longer
3834 * any overlapping read write transactions for which this transaction could
3835 * matter -- but keep the transaction entry itself and any outConflicts.
3836 *
3837 * When the summarize flag is set, we've run short of room for sxact data
3838 * and must summarize to the SLRU. Predicate locks are transferred to a
3839 * dummy "old" transaction, with duplicate locks on a single target
3840 * collapsing to a single lock with the "latest" commitSeqNo from among
3841 * the conflicting locks..
3842 */
3843static void
3845 bool summarize)
3846{
3848 dlist_mutable_iter iter;
3849
3850 Assert(sxact != NULL);
3852 Assert(partial || !SxactIsOnFinishedList(sxact));
3854
3855 /*
3856 * First release all the predicate locks held by this xact (or transfer
3857 * them to OldCommittedSxact if summarize is true)
3858 */
3860 if (IsInParallelMode())
3861 LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
3862 dlist_foreach_modify(iter, &sxact->predicateLocks)
3863 {
3865 dlist_container(PREDICATELOCK, xactLink, iter.cur);
3866 PREDICATELOCKTAG tag;
3867 PREDICATELOCKTARGET *target;
3871
3872 tag = predlock->tag;
3873 target = tag.myTarget;
3874 targettag = target->tag;
3877
3879
3880 dlist_delete(&predlock->targetLink);
3881
3885 HASH_REMOVE, NULL);
3886 if (summarize)
3887 {
3888 bool found;
3889
3890 /* Fold into dummy transaction list. */
3895 HASH_ENTER_NULL, &found);
3896 if (!predlock)
3897 ereport(ERROR,
3899 errmsg("out of shared memory"),
3900 errhint("You might need to increase \"%s\".", "max_pred_locks_per_transaction")));
3901 if (found)
3902 {
3903 Assert(predlock->commitSeqNo != 0);
3904 Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
3905 if (predlock->commitSeqNo < sxact->commitSeqNo)
3906 predlock->commitSeqNo = sxact->commitSeqNo;
3907 }
3908 else
3909 {
3911 &predlock->targetLink);
3913 &predlock->xactLink);
3914 predlock->commitSeqNo = sxact->commitSeqNo;
3915 }
3916 }
3917 else
3919
3921 }
3922
3923 /*
3924 * Rather than retail removal, just re-init the head after we've run
3925 * through the list.
3926 */
3927 dlist_init(&sxact->predicateLocks);
3928
3929 if (IsInParallelMode())
3930 LWLockRelease(&sxact->perXactPredicateListLock);
3932
3933 sxidtag.xid = sxact->topXid;
3935
3936 /* Release all outConflicts (unless 'partial' is true) */
3937 if (!partial)
3938 {
3939 dlist_foreach_modify(iter, &sxact->outConflicts)
3940 {
3942 dlist_container(RWConflictData, outLink, iter.cur);
3943
3944 if (summarize)
3945 conflict->sxactIn->flags |= SXACT_FLAG_SUMMARY_CONFLICT_IN;
3947 }
3948 }
3949
3950 /* Release all inConflicts. */
3951 dlist_foreach_modify(iter, &sxact->inConflicts)
3952 {
3954 dlist_container(RWConflictData, inLink, iter.cur);
3955
3956 if (summarize)
3957 conflict->sxactOut->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
3959 }
3960
3961 /* Finally, get rid of the xid and the record of the transaction itself. */
3962 if (!partial)
3963 {
3964 if (sxidtag.xid != InvalidTransactionId)
3967 }
3968
3970}
3971
3972/*
3973 * Tests whether the given top level transaction is concurrent with
3974 * (overlaps) our current transaction.
3975 *
3976 * We need to identify the top level transaction for SSI, anyway, so pass
3977 * that to this function to save the overhead of checking the snapshot's
3978 * subxip array.
3979 */
3980static bool
3982{
3983 Snapshot snap;
3984
3987
3989
3990 if (TransactionIdPrecedes(xid, snap->xmin))
3991 return false;
3992
3993 if (TransactionIdFollowsOrEquals(xid, snap->xmax))
3994 return true;
3995
3996 return pg_lfind32(xid, snap->xip, snap->xcnt);
3997}
3998
3999bool
4001{
4002 if (!SerializationNeededForRead(relation, snapshot))
4003 return false;
4004
4005 /* Check if someone else has already decided that we need to die */
4007 {
4008 ereport(ERROR,
4010 errmsg("could not serialize access due to read/write dependencies among transactions"),
4011 errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict out checking."),
4012 errhint("The transaction might succeed if retried.")));
4013 }
4014
4015 return true;
4016}
4017
4018/*
4019 * CheckForSerializableConflictOut
4020 * A table AM is reading a tuple that has been modified. If it determines
4021 * that the tuple version it is reading is not visible to us, it should
4022 * pass in the top level xid of the transaction that created it.
4023 * Otherwise, if it determines that it is visible to us but it has been
4024 * deleted or there is a newer version available due to an update, it
4025 * should pass in the top level xid of the modifying transaction.
4026 *
4027 * This function will check for overlap with our own transaction. If the given
4028 * xid is also serializable and the transactions overlap (i.e., they cannot see
4029 * each other's writes), then we have a conflict out.
4030 */
4031void
4033{
4037
4038 if (!SerializationNeededForRead(relation, snapshot))
4039 return;
4040
4041 /* Check if someone else has already decided that we need to die */
4043 {
4044 ereport(ERROR,
4046 errmsg("could not serialize access due to read/write dependencies among transactions"),
4047 errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict out checking."),
4048 errhint("The transaction might succeed if retried.")));
4049 }
4051
4053 return;
4054
4055 /*
4056 * Find sxact or summarized info for the top level xid.
4057 */
4058 sxidtag.xid = xid;
4060 sxid = (SERIALIZABLEXID *)
4062 if (!sxid)
4063 {
4064 /*
4065 * Transaction not found in "normal" SSI structures. Check whether it
4066 * got pushed out to SLRU storage for "old committed" transactions.
4067 */
4069
4071 if (conflictCommitSeqNo != 0)
4072 {
4077 ereport(ERROR,
4079 errmsg("could not serialize access due to read/write dependencies among transactions"),
4080 errdetail_internal("Reason code: Canceled on conflict out to old pivot %u.", xid),
4081 errhint("The transaction might succeed if retried.")));
4082
4085 ereport(ERROR,
4087 errmsg("could not serialize access due to read/write dependencies among transactions"),
4088 errdetail_internal("Reason code: Canceled on identification as a pivot, with conflict out to old committed transaction %u.", xid),
4089 errhint("The transaction might succeed if retried.")));
4090
4092 }
4093
4094 /* It's not serializable or otherwise not important. */
4096 return;
4097 }
4098 sxact = sxid->myXact;
4099 Assert(TransactionIdEquals(sxact->topXid, xid));
4101 {
4102 /* Can't conflict with ourself or a transaction that will roll back. */
4104 return;
4105 }
4106
4107 /*
4108 * We have a conflict out to a transaction which has a conflict out to a
4109 * summarized transaction. That summarized transaction must have
4110 * committed first, and we can't tell when it committed in relation to our
4111 * snapshot acquisition, so something needs to be canceled.
4112 */
4114 {
4115 if (!SxactIsPrepared(sxact))
4116 {
4117 sxact->flags |= SXACT_FLAG_DOOMED;
4119 return;
4120 }
4121 else
4122 {
4124 ereport(ERROR,
4126 errmsg("could not serialize access due to read/write dependencies among transactions"),
4127 errdetail_internal("Reason code: Canceled on conflict out to old pivot."),
4128 errhint("The transaction might succeed if retried.")));
4129 }
4130 }
4131
4132 /*
4133 * If this is a read-only transaction and the writing transaction has
4134 * committed, and it doesn't have a rw-conflict to a transaction which
4135 * committed before it, no conflict.
4136 */
4141 || MySerializableXact->SeqNo.lastCommitBeforeSnapshot < sxact->SeqNo.earliestOutConflictCommit))
4142 {
4143 /* Read-only transaction will appear to run first. No conflict. */
4145 return;
4146 }
4147
4148 if (!XidIsConcurrent(xid))
4149 {
4150 /* This write was already in our snapshot; no conflict. */
4152 return;
4153 }
4154
4156 {
4157 /* We don't want duplicate conflict records in the list. */
4159 return;
4160 }
4161
4162 /*
4163 * Flag the conflict. But first, if this conflict creates a dangerous
4164 * structure, ereport an error.
4165 */
4168}
4169
4170/*
4171 * Check a particular target for rw-dependency conflict in. A subroutine of
4172 * CheckForSerializableConflictIn().
4173 */
4174static void
4176{
4179 PREDICATELOCKTARGET *target;
4182 dlist_mutable_iter iter;
4183
4185
4186 /*
4187 * The same hash and LW lock apply to the lock target and the lock itself.
4188 */
4192 target = (PREDICATELOCKTARGET *)
4195 HASH_FIND, NULL);
4196 if (!target)
4197 {
4198 /* Nothing has this target locked; we're done here. */
4200 return;
4201 }
4202
4203 /*
4204 * Each lock for an overlapping transaction represents a conflict: a
4205 * rw-dependency in to this transaction.
4206 */
4208
4209 dlist_foreach_modify(iter, &target->predicateLocks)
4210 {
4212 dlist_container(PREDICATELOCK, targetLink, iter.cur);
4213 SERIALIZABLEXACT *sxact = predlock->tag.myXact;
4214
4216 {
4217 /*
4218 * If we're getting a write lock on a tuple, we don't need a
4219 * predicate (SIREAD) lock on the same tuple. We can safely remove
4220 * our SIREAD lock, but we'll defer doing so until after the loop
4221 * because that requires upgrading to an exclusive partition lock.
4222 *
4223 * We can't use this optimization within a subtransaction because
4224 * the subtransaction could roll back, and we would be left
4225 * without any lock at the top level.
4226 */
4227 if (!IsSubTransaction()
4229 {
4231 mypredlocktag = predlock->tag;
4232 }
4233 }
4234 else if (!SxactIsDoomed(sxact)
4237 sxact->finishedBefore))
4239 {
4242
4243 /*
4244 * Re-check after getting exclusive lock because the other
4245 * transaction may have flagged a conflict.
4246 */
4247 if (!SxactIsDoomed(sxact)
4250 sxact->finishedBefore))
4252 {
4254 }
4255
4258 }
4259 }
4262
4263 /*
4264 * If we found one of our own SIREAD locks to remove, remove it now.
4265 *
4266 * At this point our transaction already has a RowExclusiveLock on the
4267 * relation, so we are OK to drop the predicate lock on the tuple, if
4268 * found, without fearing that another write against the tuple will occur
4269 * before the MVCC information makes it to the buffer.
4270 */
4271 if (mypredlock != NULL)
4272 {
4275
4277 if (IsInParallelMode())
4281
4282 /*
4283 * Remove the predicate lock from shared memory, if it wasn't removed
4284 * while the locks were released. One way that could happen is from
4285 * autovacuum cleaning up an index.
4286 */
4293 HASH_FIND, NULL);
4294 if (rmpredlock != NULL)
4295 {
4297
4298 dlist_delete(&(mypredlock->targetLink));
4299 dlist_delete(&(mypredlock->xactLink));
4300
4305 HASH_REMOVE, NULL);
4307
4309 }
4310
4313 if (IsInParallelMode())
4316
4317 if (rmpredlock != NULL)
4318 {
4319 /*
4320 * Remove entry in local lock table if it exists. It's OK if it
4321 * doesn't exist; that means the lock was transferred to a new
4322 * target by a different backend.
4323 */
4326 HASH_REMOVE, NULL);
4327
4329 }
4330 }
4331}
4332
4333/*
4334 * CheckForSerializableConflictIn
4335 * We are writing the given tuple. If that indicates a rw-conflict
4336 * in from another serializable transaction, take appropriate action.
4337 *
4338 * Skip checking for any granularity for which a parameter is missing.
4339 *
4340 * A tuple update or delete is in conflict if we have a predicate lock
4341 * against the relation or page in which the tuple exists, or against the
4342 * tuple itself.
4343 */
4344void
4346{
4348
4349 if (!SerializationNeededForWrite(relation))
4350 return;
4351
4352 /* Check if someone else has already decided that we need to die */
4354 ereport(ERROR,
4356 errmsg("could not serialize access due to read/write dependencies among transactions"),
4357 errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict in checking."),
4358 errhint("The transaction might succeed if retried.")));
4359
4360 /*
4361 * We're doing a write which might cause rw-conflicts now or later.
4362 * Memorize that fact.
4363 */
4364 MyXactDidWrite = true;
4365
4366 /*
4367 * It is important that we check for locks from the finest granularity to
4368 * the coarsest granularity, so that granularity promotion doesn't cause
4369 * us to miss a lock. The new (coarser) lock will be acquired before the
4370 * old (finer) locks are released.
4371 *
4372 * It is not possible to take and hold a lock across the checks for all
4373 * granularities because each target could be in a separate partition.
4374 */
4375 if (tid != NULL)
4376 {
4378 relation->rd_locator.dbOid,
4379 relation->rd_id,
4383 }
4384
4385 if (blkno != InvalidBlockNumber)
4386 {
4388 relation->rd_locator.dbOid,
4389 relation->rd_id,
4390 blkno);
4392 }
4393
4395 relation->rd_locator.dbOid,
4396 relation->rd_id);
4398}
4399
4400/*
4401 * CheckTableForSerializableConflictIn
4402 * The entire table is going through a DDL-style logical mass delete
4403 * like TRUNCATE or DROP TABLE. If that causes a rw-conflict in from
4404 * another serializable transaction, take appropriate action.
4405 *
4406 * While these operations do not operate entirely within the bounds of
4407 * snapshot isolation, they can occur inside a serializable transaction, and
4408 * will logically occur after any reads which saw rows which were destroyed
4409 * by these operations, so we do what we can to serialize properly under
4410 * SSI.
4411 *
4412 * The relation passed in must be a heap relation. Any predicate lock of any
4413 * granularity on the heap will cause a rw-conflict in to this transaction.
4414 * Predicate locks on indexes do not matter because they only exist to guard
4415 * against conflicting inserts into the index, and this is a mass *delete*.
4416 * When a table is truncated or dropped, the index will also be truncated
4417 * or dropped, and we'll deal with locks on the index when that happens.
4418 *
4419 * Dropping or truncating a table also needs to drop any existing predicate
4420 * locks on heap tuples or pages, because they're about to go away. This
4421 * should be done before altering the predicate locks because the transaction
4422 * could be rolled back because of a conflict, in which case the lock changes
4423 * are not needed. (At the moment, we don't actually bother to drop the
4424 * existing locks on a dropped or truncated table at the moment. That might
4425 * lead to some false positives, but it doesn't seem worth the trouble.)
4426 */
4427void
4429{
4431 PREDICATELOCKTARGET *target;
4432 Oid dbId;
4433 Oid heapId;
4434 int i;
4435
4436 /*
4437 * Bail out quickly if there are no serializable transactions running.
4438 * It's safe to check this without taking locks because the caller is
4439 * holding an ACCESS EXCLUSIVE lock on the relation. No new locks which
4440 * would matter here can be acquired while that is held.
4441 */
4443 return;
4444
4445 if (!SerializationNeededForWrite(relation))
4446 return;
4447
4448 /*
4449 * We're doing a write which might cause rw-conflicts now or later.
4450 * Memorize that fact.
4451 */
4452 MyXactDidWrite = true;
4453
4454 Assert(relation->rd_index == NULL); /* not an index relation */
4455
4456 dbId = relation->rd_locator.dbOid;
4457 heapId = relation->rd_id;
4458
4460 for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
4463
4464 /* Scan through target list */
4466
4467 while ((target = (PREDICATELOCKTARGET *) hash_seq_search(&seqstat)))
4468 {
4469 dlist_mutable_iter iter;
4470
4471 /*
4472 * Check whether this is a target which needs attention.
4473 */
4475 continue; /* wrong relation id */
4476 if (GET_PREDICATELOCKTARGETTAG_DB(target->tag) != dbId)
4477 continue; /* wrong database id */
4478
4479 /*
4480 * Loop through locks for this target and flag conflicts.
4481 */
4482 dlist_foreach_modify(iter, &target->predicateLocks)
4483 {
4485 dlist_container(PREDICATELOCK, targetLink, iter.cur);
4486
4487 if (predlock->tag.myXact != MySerializableXact
4489 {
4491 }
4492 }
4493 }
4494
4495 /* Release locks in reverse order */
4497 for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
4500}
4501
4502
4503/*
4504 * Flag a rw-dependency between two serializable transactions.
4505 *
4506 * The caller is responsible for ensuring that we have a LW lock on
4507 * the transaction hash table.
4508 */
4509static void
4511{
4512 Assert(reader != writer);
4513
4514 /* First, see if this conflict causes failure. */
4516
4517 /* Actually do the conflict flagging. */
4518 if (reader == OldCommittedSxact)
4520 else if (writer == OldCommittedSxact)
4522 else
4523 SetRWConflict(reader, writer);
4524}
4525
4526/*----------------------------------------------------------------------------
4527 * We are about to add a RW-edge to the dependency graph - check that we don't
4528 * introduce a dangerous structure by doing so, and abort one of the
4529 * transactions if so.
4530 *
4531 * A serialization failure can only occur if there is a dangerous structure
4532 * in the dependency graph:
4533 *
4534 * Tin ------> Tpivot ------> Tout
4535 * rw rw
4536 *
4537 * Furthermore, Tout must commit first.
4538 *
4539 * One more optimization is that if Tin is declared READ ONLY (or commits
4540 * without writing), we can only have a problem if Tout committed before Tin
4541 * acquired its snapshot.
4542 *----------------------------------------------------------------------------
4543 */
4544static void
4547{
4548 bool failure;
4549
4551
4552 failure = false;
4553
4554 /*------------------------------------------------------------------------
4555 * Check for already-committed writer with rw-conflict out flagged
4556 * (conflict-flag on W means that T2 committed before W):
4557 *
4558 * R ------> W ------> T2
4559 * rw rw
4560 *
4561 * That is a dangerous structure, so we must abort. (Since the writer
4562 * has already committed, we must be the reader)
4563 *------------------------------------------------------------------------
4564 */
4567 failure = true;
4568
4569 /*------------------------------------------------------------------------
4570 * Check whether the writer has become a pivot with an out-conflict
4571 * committed transaction (T2), and T2 committed first:
4572 *
4573 * R ------> W ------> T2
4574 * rw rw
4575 *
4576 * Because T2 must've committed first, there is no anomaly if:
4577 * - the reader committed before T2
4578 * - the writer committed before T2
4579 * - the reader is a READ ONLY transaction and the reader was concurrent
4580 * with T2 (= reader acquired its snapshot before T2 committed)
4581 *
4582 * We also handle the case that T2 is prepared but not yet committed
4583 * here. In that case T2 has already checked for conflicts, so if it
4584 * commits first, making the above conflict real, it's too late for it
4585 * to abort.
4586 *------------------------------------------------------------------------
4587 */
4589 failure = true;
4590 else if (!failure)
4591 {
4592 dlist_iter iter;
4593
4594 dlist_foreach(iter, &writer->outConflicts)
4595 {
4597 dlist_container(RWConflictData, outLink, iter.cur);
4598 SERIALIZABLEXACT *t2 = conflict->sxactIn;
4599
4600 if (SxactIsPrepared(t2)
4601 && (!SxactIsCommitted(reader)
4602 || t2->prepareSeqNo <= reader->commitSeqNo)
4604 || t2->prepareSeqNo <= writer->commitSeqNo)
4605 && (!SxactIsReadOnly(reader)
4606 || t2->prepareSeqNo <= reader->SeqNo.lastCommitBeforeSnapshot))
4607 {
4608 failure = true;
4609 break;
4610 }
4611 }
4612 }
4613
4614 /*------------------------------------------------------------------------
4615 * Check whether the reader has become a pivot with a writer
4616 * that's committed (or prepared):
4617 *
4618 * T0 ------> R ------> W
4619 * rw rw
4620 *
4621 * Because W must've committed first for an anomaly to occur, there is no
4622 * anomaly if:
4623 * - T0 committed before the writer
4624 * - T0 is READ ONLY, and overlaps the writer
4625 *------------------------------------------------------------------------
4626 */
4627 if (!failure && SxactIsPrepared(writer) && !SxactIsReadOnly(reader))
4628 {
4629 if (SxactHasSummaryConflictIn(reader))
4630 {
4631 failure = true;
4632 }
4633 else
4634 {
4635 dlist_iter iter;
4636
4637 /*
4638 * The unconstify is needed as we have no const version of
4639 * dlist_foreach().
4640 */
4641 dlist_foreach(iter, &unconstify(SERIALIZABLEXACT *, reader)->inConflicts)
4642 {
4643 const RWConflict conflict =
4644 dlist_container(RWConflictData, inLink, iter.cur);
4645 const SERIALIZABLEXACT *t0 = conflict->sxactOut;
4646
4647 if (!SxactIsDoomed(t0)
4648 && (!SxactIsCommitted(t0)
4649 || t0->commitSeqNo >= writer->prepareSeqNo)
4650 && (!SxactIsReadOnly(t0)
4651 || t0->SeqNo.lastCommitBeforeSnapshot >= writer->prepareSeqNo))
4652 {
4653 failure = true;
4654 break;
4655 }
4656 }
4657 }
4658 }
4659
4660 if (failure)
4661 {
4662 /*
4663 * We have to kill a transaction to avoid a possible anomaly from
4664 * occurring. If the writer is us, we can just ereport() to cause a
4665 * transaction abort. Otherwise we flag the writer for termination,
4666 * causing it to abort when it tries to commit. However, if the writer
4667 * is a prepared transaction, already prepared, we can't abort it
4668 * anymore, so we have to kill the reader instead.
4669 */
4671 {
4673 ereport(ERROR,
4675 errmsg("could not serialize access due to read/write dependencies among transactions"),
4676 errdetail_internal("Reason code: Canceled on identification as a pivot, during write."),
4677 errhint("The transaction might succeed if retried.")));
4678 }
4679 else if (SxactIsPrepared(writer))
4680 {
4682
4683 /* if we're not the writer, we have to be the reader */
4684 Assert(MySerializableXact == reader);
4685 ereport(ERROR,
4687 errmsg("could not serialize access due to read/write dependencies among transactions"),
4688 errdetail_internal("Reason code: Canceled on conflict out to pivot %u, during read.", writer->topXid),
4689 errhint("The transaction might succeed if retried.")));
4690 }
4691 writer->flags |= SXACT_FLAG_DOOMED;
4692 }
4693}
4694
4695/*
4696 * PreCommit_CheckForSerializationFailure
4697 * Check for dangerous structures in a serializable transaction
4698 * at commit.
4699 *
4700 * We're checking for a dangerous structure as each conflict is recorded.
4701 * The only way we could have a problem at commit is if this is the "out"
4702 * side of a pivot, and neither the "in" side nor the pivot has yet
4703 * committed.
4704 *
4705 * If a dangerous structure is found, the pivot (the near conflict) is
4706 * marked for death, because rolling back another transaction might mean
4707 * that we fail without ever making progress. This transaction is
4708 * committing writes, so letting it commit ensures progress. If we
4709 * canceled the far conflict, it might immediately fail again on retry.
4710 */
4711void
4713{
4715
4717 return;
4718
4720
4722
4723 /*
4724 * Check if someone else has already decided that we need to die. Since
4725 * we set our own DOOMED flag when partially releasing, ignore in that
4726 * case.
4727 */
4730 {
4732 ereport(ERROR,
4734 errmsg("could not serialize access due to read/write dependencies among transactions"),
4735 errdetail_internal("Reason code: Canceled on identification as a pivot, during commit attempt."),
4736 errhint("The transaction might succeed if retried.")));
4737 }
4738
4740 {
4743
4744 if (!SxactIsCommitted(nearConflict->sxactOut)
4745 && !SxactIsDoomed(nearConflict->sxactOut))
4746 {
4748
4749 dlist_foreach(far_iter, &nearConflict->sxactOut->inConflicts)
4750 {
4753
4754 if (farConflict->sxactOut == MySerializableXact
4755 || (!SxactIsCommitted(farConflict->sxactOut)
4756 && !SxactIsReadOnly(farConflict->sxactOut)
4757 && !SxactIsDoomed(farConflict->sxactOut)))
4758 {
4759 /*
4760 * Normally, we kill the pivot transaction to make sure we
4761 * make progress if the failing transaction is retried.
4762 * However, we can't kill it if it's already prepared, so
4763 * in that case we commit suicide instead.
4764 */
4765 if (SxactIsPrepared(nearConflict->sxactOut))
4766 {
4768 ereport(ERROR,
4770 errmsg("could not serialize access due to read/write dependencies among transactions"),
4771 errdetail_internal("Reason code: Canceled on commit attempt with conflict in from prepared pivot."),
4772 errhint("The transaction might succeed if retried.")));
4773 }
4774 nearConflict->sxactOut->flags |= SXACT_FLAG_DOOMED;
4775 break;
4776 }
4777 }
4778 }
4779 }
4780
4783
4785}
4786
4787/*------------------------------------------------------------------------*/
4788
4789/*
4790 * Two-phase commit support
4791 */
4792
4793/*
4794 * AtPrepare_Locks
4795 * Do the preparatory work for a PREPARE: make 2PC state file
4796 * records for all predicate locks currently held.
4797 */
4798void
4800{
4803 TwoPhasePredicateXactRecord *xactRecord;
4804 TwoPhasePredicateLockRecord *lockRecord;
4805 dlist_iter iter;
4806
4808 xactRecord = &(record.data.xactRecord);
4809 lockRecord = &(record.data.lockRecord);
4810
4812 return;
4813
4814 /* Generate an xact record for our SERIALIZABLEXACT */
4816 xactRecord->xmin = MySerializableXact->xmin;
4817 xactRecord->flags = MySerializableXact->flags;
4818
4819 /*
4820 * Note that we don't include the list of conflicts in our out in the
4821 * statefile, because new conflicts can be added even after the
4822 * transaction prepares. We'll just make a conservative assumption during
4823 * recovery instead.
4824 */
4825
4827 &record, sizeof(record));
4828
4829 /*
4830 * Generate a lock record for each lock.
4831 *
4832 * To do this, we need to walk the predicate lock list in our sxact rather
4833 * than using the local predicate lock table because the latter is not
4834 * guaranteed to be accurate.
4835 */
4837
4838 /*
4839 * No need to take sxact->perXactPredicateListLock in parallel mode
4840 * because there cannot be any parallel workers running while we are
4841 * preparing a transaction.
4842 */
4844
4845 dlist_foreach(iter, &sxact->predicateLocks)
4846 {
4848 dlist_container(PREDICATELOCK, xactLink, iter.cur);
4849
4851 lockRecord->target = predlock->tag.myTarget->tag;
4852
4854 &record, sizeof(record));
4855 }
4856
4858}
4859
4860/*
4861 * PostPrepare_Locks
4862 * Clean up after successful PREPARE. Unlike the non-predicate
4863 * lock manager, we do not need to transfer locks to a dummy
4864 * PGPROC because our SERIALIZABLEXACT will stay around
4865 * anyway. We only need to clean up our local state.
4866 */
4867void
4884
4885/*
4886 * PredicateLockTwoPhaseFinish
4887 * Release a prepared transaction's predicate locks once it
4888 * commits or aborts.
4889 */
4890void
4892{
4895
4897
4899 sxid = (SERIALIZABLEXID *)
4902
4903 /* xid will not be found if it wasn't a serializable transaction */
4904 if (sxid == NULL)
4905 return;
4906
4907 /* Release its locks */
4908 MySerializableXact = sxid->myXact;
4909 MyXactDidWrite = true; /* conservatively assume that we wrote
4910 * something */
4912}
4913
4914/*
4915 * Re-acquire a predicate lock belonging to a transaction that was prepared.
4916 */
4917void
4919 void *recdata, uint32 len)
4920{
4923
4925
4926 record = (TwoPhasePredicateRecord *) recdata;
4927
4929 (record->type == TWOPHASEPREDICATERECORD_LOCK));
4930
4931 if (record->type == TWOPHASEPREDICATERECORD_XACT)
4932 {
4933 /* Per-transaction record. Set up a SERIALIZABLEXACT. */
4934 TwoPhasePredicateXactRecord *xactRecord;
4938 bool found;
4939
4940 xactRecord = (TwoPhasePredicateXactRecord *) &record->data.xactRecord;
4941
4944 if (!sxact)
4945 ereport(ERROR,
4947 errmsg("out of shared memory")));
4948
4949 /* vxid for a prepared xact is INVALID_PROC_NUMBER/xid; no pid */
4950 sxact->vxid.procNumber = INVALID_PROC_NUMBER;
4951 sxact->vxid.localTransactionId = (LocalTransactionId) xid;
4952 sxact->pid = 0;
4953 sxact->pgprocno = INVALID_PROC_NUMBER;
4954
4955 /* a prepared xact hasn't committed yet */
4956 sxact->prepareSeqNo = RecoverySerCommitSeqNo;
4957 sxact->commitSeqNo = InvalidSerCommitSeqNo;
4958 sxact->finishedBefore = InvalidTransactionId;
4959
4960 sxact->SeqNo.lastCommitBeforeSnapshot = RecoverySerCommitSeqNo;
4961
4962 /*
4963 * Don't need to track this; no transactions running at the time the
4964 * recovered xact started are still active, except possibly other
4965 * prepared xacts and we don't care whether those are RO_SAFE or not.
4966 */
4967 dlist_init(&(sxact->possibleUnsafeConflicts));
4968
4969 dlist_init(&(sxact->predicateLocks));
4970 dlist_node_init(&sxact->finishedLink);
4971
4972 sxact->topXid = xid;
4973 sxact->xmin = xactRecord->xmin;
4974 sxact->flags = xactRecord->flags;
4976 if (!SxactIsReadOnly(sxact))
4977 {
4981 }
4982
4983 /*
4984 * We don't know whether the transaction had any conflicts or not, so
4985 * we'll conservatively assume that it had both a conflict in and a
4986 * conflict out, and represent that with the summary conflict flags.
4987 */
4988 dlist_init(&(sxact->outConflicts));
4989 dlist_init(&(sxact->inConflicts));
4992
4993 /* Register the transaction's xid */
4994 sxidtag.xid = xid;
4996 &sxidtag,
4997 HASH_ENTER, &found);
4998 Assert(sxid != NULL);
4999 Assert(!found);
5000 sxid->myXact = sxact;
5001
5002 /*
5003 * Update global xmin. Note that this is a special case compared to
5004 * registering a normal transaction, because the global xmin might go
5005 * backwards. That's OK, because until recovery is over we're not
5006 * going to complete any transactions or create any non-prepared
5007 * transactions, so there's no danger of throwing away.
5008 */
5011 {
5015 }
5017 {
5020 }
5021
5023 }
5024 else if (record->type == TWOPHASEPREDICATERECORD_LOCK)
5025 {
5026 /* Lock record. Recreate the PREDICATELOCK */
5027 TwoPhasePredicateLockRecord *lockRecord;
5032
5033 lockRecord = (TwoPhasePredicateLockRecord *) &record->data.lockRecord;
5035
5037 sxidtag.xid = xid;
5038 sxid = (SERIALIZABLEXID *)
5041
5042 Assert(sxid != NULL);
5043 sxact = sxid->myXact;
5045
5047 }
5048}
5049
5050/*
5051 * Prepare to share the current SERIALIZABLEXACT with parallel workers.
5052 * Return a handle object that can be used by AttachSerializableXact() in a
5053 * parallel worker.
5054 */
5057{
5058 return MySerializableXact;
5059}
5060
5061/*
5062 * Allow parallel workers to import the leader's SERIALIZABLEXACT.
5063 */
5064void
bool ParallelContextActive(void)
Definition parallel.c:1033
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition block.h:71
#define unconstify(underlying_type, expr)
Definition c.h:1327
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:243
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
uint32 LocalTransactionId
Definition c.h:740
uint32 TransactionId
Definition c.h:738
size_t Size
Definition c.h:691
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
Size hash_estimate_size(int64 num_entries, Size entrysize)
Definition dynahash.c:783
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
void hash_destroy(HTAB *hashp)
Definition dynahash.c:865
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition dynahash.c:965
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
int64 hash_get_num_entries(HTAB *hashp)
Definition dynahash.c:1336
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
int errcode(int sqlerrcode)
Definition elog.c:874
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:29
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
int MyProcPid
Definition globals.c:47
ProcNumber MyProcNumber
Definition globals.c:90
bool IsUnderPostmaster
Definition globals.c:120
int MaxBackends
Definition globals.c:146
int serializable_buffers
Definition globals.c:165
#define newval
GucSource
Definition guc.h:112
@ HASH_FIND
Definition hsearch.h:113
@ HASH_REMOVE
Definition hsearch.h:115
@ HASH_ENTER
Definition hsearch.h:114
@ HASH_ENTER_NULL
Definition hsearch.h:116
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_FUNCTION
Definition hsearch.h:98
#define HASH_BLOBS
Definition hsearch.h:97
#define HASH_FIXED_SIZE
Definition hsearch.h:105
#define HASH_PARTITION
Definition hsearch.h:92
static dlist_node * dlist_pop_head_node(dlist_head *head)
Definition ilist.h:450
#define dlist_foreach(iter, lhead)
Definition ilist.h:623
static void dlist_init(dlist_head *head)
Definition ilist.h:314
#define dlist_head_element(type, membername, lhead)
Definition ilist.h:603
static void dlist_delete_thoroughly(dlist_node *node)
Definition ilist.h:416
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
#define dlist_foreach_modify(iter, lhead)
Definition ilist.h:640
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition ilist.h:364
static void dlist_node_init(dlist_node *node)
Definition ilist.h:325
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
#define IsParallelWorker()
Definition parallel.h:62
FILE * output
long val
Definition informix.c:689
static bool success
Definition initdb.c:188
int i
Definition isn.c:77
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
#define GET_VXID_FROM_PGPROC(vxid_dst, proc)
Definition lock.h:80
#define SetInvalidVirtualTransactionId(vxid)
Definition lock.h:77
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1912
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1956
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:699
@ LW_SHARED
Definition lwlock.h:113
@ LW_EXCLUSIVE
Definition lwlock.h:112
#define NUM_PREDICATELOCK_PARTITIONS
Definition lwlock.h:99
#define InvalidPid
Definition miscadmin.h:32
static char * errmsg
#define SLRU_PAGES_PER_SEGMENT
const void size_t len
const void * data
static bool pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
Definition pg_lfind.h:153
static rewind_source * source
Definition pg_rewind.c:89
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition pgbench.c:77
#define InvalidOid
unsigned int Oid
PredicateLockData * GetPredicateLockStatusData(void)
Definition predicate.c:1456
void CheckPointPredicate(void)
Definition predicate.c:1052
void PredicateLockPageSplit(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition predicate.c:3153
static void DecrementParentLocks(const PREDICATELOCKTARGETTAG *targettag)
Definition predicate.c:2400
static HTAB * PredicateLockHash
Definition predicate.c:399
static void SetPossibleUnsafeConflict(SERIALIZABLEXACT *roXact, SERIALIZABLEXACT *activeXact)
Definition predicate.c:668
#define PredicateLockTargetTagHashCode(predicatelocktargettag)
Definition predicate.c:304
static void SetNewSxactGlobalXmin(void)
Definition predicate.c:3260
void CheckForSerializableConflictIn(Relation relation, const ItemPointerData *tid, BlockNumber blkno)
Definition predicate.c:4345
#define SerialPage(xid)
Definition predicate.c:344
static void ReleasePredXact(SERIALIZABLEXACT *sxact)
Definition predicate.c:598
void SetSerializableTransactionSnapshot(Snapshot snapshot, VirtualTransactionId *sourcevxid, int sourcepid)
Definition predicate.c:1731
static bool RWConflictExists(const SERIALIZABLEXACT *reader, const SERIALIZABLEXACT *writer)
Definition predicate.c:612
static bool PredicateLockingNeededForRelation(Relation relation)
Definition predicate.c:500
static bool SerializationNeededForRead(Relation relation, Snapshot snapshot)
Definition predicate.c:518
static Snapshot GetSafeSnapshot(Snapshot origSnapshot)
Definition predicate.c:1567
#define SxactIsCommitted(sxact)
Definition predicate.c:278
static SerialControl serialControl
Definition predicate.c:355
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition predicate.c:2608
#define SxactIsROUnsafe(sxact)
Definition predicate.c:293
static Snapshot GetSerializableTransactionSnapshotInt(Snapshot snapshot, VirtualTransactionId *sourcevxid, int sourcepid)
Definition predicate.c:1773
static LWLock * ScratchPartitionLock
Definition predicate.c:409
static void PredicateLockAcquire(const PREDICATELOCKTARGETTAG *targettag)
Definition predicate.c:2526
#define SxactIsDeferrableWaiting(sxact)
Definition predicate.c:291
static void ReleasePredicateLocksLocal(void)
Definition predicate.c:3688
static HTAB * LocalPredicateLockHash
Definition predicate.c:415
int max_predicate_locks_per_page
Definition predicate.c:374
struct SerialControlData * SerialControl
Definition predicate.c:353
static PredXactList PredXact
Definition predicate.c:385
static void SetRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer)
Definition predicate.c:645
int GetSafeSnapshotBlockingPids(int blocked_pid, int *output, int output_size)
Definition predicate.c:1637
static uint32 ScratchTargetTagHash
Definition predicate.c:408
static void RemoveTargetIfNoLongerUsed(PREDICATELOCKTARGET *target, uint32 targettaghash)
Definition predicate.c:2192
static uint32 predicatelock_hash(const void *key, Size keysize)
Definition predicate.c:1430
void CheckForSerializableConflictOut(Relation relation, TransactionId xid, Snapshot snapshot)
Definition predicate.c:4032
#define SxactIsReadOnly(sxact)
Definition predicate.c:282
#define SerialNextPage(page)
Definition predicate.c:338
static void DropAllPredicateLocksFromTable(Relation relation, bool transfer)
Definition predicate.c:2946
bool PageIsPredicateLocked(Relation relation, BlockNumber blkno)
Definition predicate.c:2017
static SlruCtlData SerialSlruCtlData
Definition predicate.c:325
static int serial_errdetail_for_io_error(const void *opaque_data)
Definition predicate.c:748
static void CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag, uint32 targettaghash, SERIALIZABLEXACT *sxact)
Definition predicate.c:2462
static void SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo)
Definition predicate.c:869
static void ClearOldPredicateLocks(void)
Definition predicate.c:3706
#define SxactHasSummaryConflictIn(sxact)
Definition predicate.c:283
static SERIALIZABLEXACT * CreatePredXact(void)
Definition predicate.c:584
static bool GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag, PREDICATELOCKTARGETTAG *parent)
Definition predicate.c:2081
#define PredicateLockHashCodeFromTargetHashCode(predicatelocktag, targethash)
Definition predicate.c:317
static void RestoreScratchTarget(bool lockheld)
Definition predicate.c:2170
#define SerialValue(slotno, xid)
Definition predicate.c:340
static void DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag)
Definition predicate.c:2223
static void DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash)
Definition predicate.c:2678
void PredicateLockTwoPhaseFinish(FullTransactionId fxid, bool isCommit)
Definition predicate.c:4891
void predicatelock_twophase_recover(FullTransactionId fxid, uint16 info, void *recdata, uint32 len)
Definition predicate.c:4918
static SERIALIZABLEXACT * OldCommittedSxact
Definition predicate.c:363
#define SxactHasConflictOut(sxact)
Definition predicate.c:290
static bool MyXactDidWrite
Definition predicate.c:423
static int MaxPredicateChildLocks(const PREDICATELOCKTARGETTAG *tag)
Definition predicate.c:2298
static void FlagSxactUnsafe(SERIALIZABLEXACT *sxact)
Definition predicate.c:701
static void SerialInit(void)
Definition predicate.c:816
void CheckTableForSerializableConflictIn(Relation relation)
Definition predicate.c:4428
#define SxactIsPrepared(sxact)
Definition predicate.c:279
void AttachSerializableXact(SerializableXactHandle handle)
Definition predicate.c:5065
SerializableXactHandle ShareSerializableXact(void)
Definition predicate.c:5056
static bool PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag)
Definition predicate.c:2054
static void RemoveScratchTarget(bool lockheld)
Definition predicate.c:2149
#define SxactIsOnFinishedList(sxact)
Definition predicate.c:268
#define SxactIsPartiallyReleased(sxact)
Definition predicate.c:294
static void SerialSetActiveSerXmin(TransactionId xid)
Definition predicate.c:1001
static dlist_head * FinishedSerializableTransactions
Definition predicate.c:400
static bool SerializationNeededForWrite(Relation relation)
Definition predicate.c:562
static HTAB * SerializableXidHash
Definition predicate.c:397
static bool CheckAndPromotePredicateLockRequest(const PREDICATELOCKTARGETTAG *reqtag)
Definition predicate.c:2335
void PredicateLockPageCombine(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition predicate.c:3238
static bool SerialPagePrecedesLogically(int64 page1, int64 page2)
Definition predicate.c:733
static void CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
Definition predicate.c:4175
int max_predicate_locks_per_relation
Definition predicate.c:373
#define SxactIsROSafe(sxact)
Definition predicate.c:292
void PreCommit_CheckForSerializationFailure(void)
Definition predicate.c:4712
void ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
Definition predicate.c:3321
static void FlagRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer)
Definition predicate.c:4510
static const PREDICATELOCKTARGETTAG ScratchTargetTag
Definition predicate.c:407
#define PredicateLockHashPartitionLockByIndex(i)
Definition predicate.c:262
static void OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer)
Definition predicate.c:4545
static bool CoarserLockCovers(const PREDICATELOCKTARGETTAG *newtargettag)
Definition predicate.c:2120
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition predicate.c:2585
static SERIALIZABLEXACT * MySerializableXact
Definition predicate.c:422
void PredicateLockShmemInit(void)
Definition predicate.c:1156
void PredicateLockTID(Relation relation, const ItemPointerData *tid, Snapshot snapshot, TransactionId tuple_xid)
Definition predicate.c:2630
Size PredicateLockShmemSize(void)
Definition predicate.c:1368
#define SxactIsDoomed(sxact)
Definition predicate.c:281
#define NPREDICATELOCKTARGETENTS()
Definition predicate.c:265
static SerCommitSeqNo SerialGetMinConflictCommitSeqNo(TransactionId xid)
Definition predicate.c:960
static void SummarizeOldestCommittedSxact(void)
Definition predicate.c:1512
bool check_serial_buffers(int *newval, void **extra, GucSource source)
Definition predicate.c:858
void PostPrepare_PredicateLocks(FullTransactionId fxid)
Definition predicate.c:4868
#define TargetTagIsCoveredBy(covered_target, covering_target)
Definition predicate.c:234
static RWConflictPoolHeader RWConflictPool
Definition predicate.c:391
static void ReleaseRWConflict(RWConflict conflict)
Definition predicate.c:693
static bool TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag, PREDICATELOCKTARGETTAG newtargettag, bool removeOld)
Definition predicate.c:2739
void AtPrepare_PredicateLocks(void)
Definition predicate.c:4799
void RegisterPredicateLockingXid(TransactionId xid)
Definition predicate.c:1968
#define PredicateLockHashPartitionLock(hashcode)
Definition predicate.c:259
#define SERIAL_ENTRIESPERPAGE
Definition predicate.c:331
static bool XidIsConcurrent(TransactionId xid)
Definition predicate.c:3981
static void ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial, bool summarize)
Definition predicate.c:3844
static HTAB * PredicateLockTargetHash
Definition predicate.c:398
bool CheckForSerializableConflictOutNeeded(Relation relation, Snapshot snapshot)
Definition predicate.c:4000
#define SxactIsRolledBack(sxact)
Definition predicate.c:280
static SERIALIZABLEXACT * SavedSerializableXact
Definition predicate.c:432
#define SxactHasSummaryConflictOut(sxact)
Definition predicate.c:284
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition predicate.c:3132
static void CreateLocalPredicateLockHash(void)
Definition predicate.c:1949
#define SerialSlruCtl
Definition predicate.c:327
int max_predicate_locks_per_xact
Definition predicate.c:372
Snapshot GetSerializableTransactionSnapshot(Snapshot snapshot)
Definition predicate.c:1691
void * SerializableXactHandle
Definition predicate.h:34
#define RWConflictDataSize
#define SXACT_FLAG_DEFERRABLE_WAITING
#define SXACT_FLAG_SUMMARY_CONFLICT_IN
@ TWOPHASEPREDICATERECORD_XACT
@ TWOPHASEPREDICATERECORD_LOCK
#define FirstNormalSerCommitSeqNo
#define InvalidSerCommitSeqNo
@ PREDLOCKTAG_RELATION
@ PREDLOCKTAG_PAGE
@ PREDLOCKTAG_TUPLE
#define SXACT_FLAG_CONFLICT_OUT
#define PredXactListDataSize
#define SXACT_FLAG_READ_ONLY
#define SXACT_FLAG_DOOMED
#define GET_PREDICATELOCKTARGETTAG_DB(locktag)
#define GET_PREDICATELOCKTARGETTAG_RELATION(locktag)
#define RWConflictPoolHeaderDataSize
#define InvalidSerializableXact
#define SET_PREDICATELOCKTARGETTAG_PAGE(locktag, dboid, reloid, blocknum)
#define RecoverySerCommitSeqNo
struct RWConflictData * RWConflict
#define GET_PREDICATELOCKTARGETTAG_TYPE(locktag)
#define SET_PREDICATELOCKTARGETTAG_RELATION(locktag, dboid, reloid)
uint64 SerCommitSeqNo
#define SXACT_FLAG_ROLLED_BACK
#define SXACT_FLAG_COMMITTED
#define SXACT_FLAG_RO_UNSAFE
#define SXACT_FLAG_PREPARED
#define SET_PREDICATELOCKTARGETTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
#define SXACT_FLAG_PARTIALLY_RELEASED
#define GET_PREDICATELOCKTARGETTAG_PAGE(locktag)
#define SXACT_FLAG_RO_SAFE
#define SXACT_FLAG_SUMMARY_CONFLICT_OUT
#define GET_PREDICATELOCKTARGETTAG_OFFSET(locktag)
static int fb(int x)
Snapshot GetSnapshotData(Snapshot snapshot)
Definition procarray.c:2122
bool ProcArrayInstallImportedXmin(TransactionId xmin, VirtualTransactionId *sourcevxid)
Definition procarray.c:2479
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
#define RelationUsesLocalBuffers(relation)
Definition rel.h:646
bool ShmemAddrIsValid(const void *addr)
Definition shmem.c:268
Size add_size(Size s1, Size s2)
Definition shmem.c:485
Size mul_size(Size s1, Size s2)
Definition shmem.c:500
HTAB * ShmemInitHash(const char *name, int64 init_size, int64 max_size, HASHCTL *infoP, int hash_flags)
Definition shmem.c:326
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:381
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition slru.c:254
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition slru.c:1355
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:533
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition slru.c:380
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition slru.c:1441
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
Definition slru.c:637
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition slru.c:200
bool check_slru_buffers(const char *name, int *newval)
Definition slru.c:360
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition slru.h:171
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition slru.h:196
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
#define IsMVCCSnapshot(snapshot)
Definition snapmgr.h:59
void ProcSendSignal(ProcNumber procNumber)
Definition proc.c:2003
PGPROC * MyProc
Definition proc.c:68
void ProcWaitForSignal(uint32 wait_event_info)
Definition proc.c:1991
Size keysize
Definition hsearch.h:75
HashValueFunc hash
Definition hsearch.h:78
Size entrysize
Definition hsearch.h:76
int64 num_partitions
Definition hsearch.h:68
Definition proc.h:176
SERIALIZABLEXACT * myXact
PREDICATELOCKTARGET * myTarget
PREDICATELOCKTARGETTAG tag
SerCommitSeqNo commitSeqNo
SERIALIZABLEXACT * element
SerCommitSeqNo LastSxactCommitSeqNo
SerCommitSeqNo CanPartialClearThrough
SERIALIZABLEXACT * OldCommittedSxact
SerCommitSeqNo HavePartialClearedThrough
TransactionId SxactGlobalXmin
Form_pg_index rd_index
Definition rel.h:192
Oid rd_id
Definition rel.h:113
RelFileLocator rd_locator
Definition rel.h:57
VirtualTransactionId vxid
SerCommitSeqNo lastCommitBeforeSnapshot
dlist_head possibleUnsafeConflicts
SerCommitSeqNo prepareSeqNo
SerCommitSeqNo commitSeqNo
union SERIALIZABLEXACT::@131 SeqNo
TransactionId finishedBefore
SerCommitSeqNo earliestOutConflictCommit
TransactionId headXid
Definition predicate.c:349
TransactionId tailXid
Definition predicate.c:350
TransactionId xmin
Definition snapshot.h:153
FullTransactionId nextXid
Definition transam.h:220
PREDICATELOCKTARGETTAG target
TwoPhasePredicateRecordType type
TwoPhasePredicateLockRecord lockRecord
union TwoPhasePredicateRecord::@132 data
TwoPhasePredicateXactRecord xactRecord
dlist_node * cur
Definition ilist.h:179
dlist_node * cur
Definition ilist.h:200
@ SYNC_HANDLER_NONE
Definition sync.h:42
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition transam.h:297
#define FirstUnpinnedObjectId
Definition transam.h:196
#define InvalidTransactionId
Definition transam.h:31
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282
static bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:312
#define TransactionIdEquals(id1, id2)
Definition transam.h:43
#define XidFromFullTransactionId(x)
Definition transam.h:48
#define FirstNormalTransactionId
Definition transam.h:34
#define TransactionIdIsValid(xid)
Definition transam.h:41
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition twophase.c:1274
int max_prepared_xacts
Definition twophase.c:117
#define TWOPHASE_RM_PREDICATELOCK_ID
TransamVariablesData * TransamVariables
Definition varsup.c:34
bool XactDeferrable
Definition xact.c:87
bool XactReadOnly
Definition xact.c:84
TransactionId GetTopTransactionIdIfAny(void)
Definition xact.c:443
bool IsSubTransaction(void)
Definition xact.c:5067
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition xact.c:943
bool IsInParallelMode(void)
Definition xact.c:1091
#define IsolationIsSerializable()
Definition xact.h:53
bool RecoveryInProgress(void)
Definition xlog.c:6444