PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
snapmgr.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * snapmgr.c
4 * PostgreSQL snapshot manager
5 *
6 * The following functions return an MVCC snapshot that can be used in tuple
7 * visibility checks:
8 *
9 * - GetTransactionSnapshot
10 * - GetLatestSnapshot
11 * - GetCatalogSnapshot
12 * - GetNonHistoricCatalogSnapshot
13 *
14 * Each of these functions returns a reference to a statically allocated
15 * snapshot. The statically allocated snapshot is subject to change on any
16 * snapshot-related function call, and should not be used directly. Instead,
17 * call PushActiveSnapshot() or RegisterSnapshot() to create a longer-lived
18 * copy and use that.
19 *
20 * We keep track of snapshots in two ways: those "registered" by resowner.c,
21 * and the "active snapshot" stack. All snapshots in either of them live in
22 * persistent memory. When a snapshot is no longer in any of these lists
23 * (tracked by separate refcounts on each snapshot), its memory can be freed.
24 *
25 * In addition to the above-mentioned MVCC snapshots, there are some special
26 * snapshots like SnapshotSelf, SnapshotAny, and "dirty" snapshots. They can
27 * only be used in limited contexts and cannot be registered or pushed to the
28 * active stack.
29 *
30 * ActiveSnapshot stack
31 * --------------------
32 *
33 * Most visibility checks use the current "active snapshot" returned by
34 * GetActiveSnapshot(). When running normal queries, the active snapshot is
35 * set when query execution begins based on the transaction isolation level.
36 *
37 * The active snapshot is tracked in a stack so that the currently active one
38 * is at the top of the stack. It mirrors the process call stack: whenever we
39 * recurse or switch context to fetch rows from a different portal for
40 * example, the appropriate snapshot is pushed to become the active snapshot,
41 * and popped on return. Once upon a time, ActiveSnapshot was just a global
42 * variable that was saved and restored similar to CurrentMemoryContext, but
43 * nowadays it's managed as a separate data structure so that we can keep
44 * track of which snapshots are in use and reset MyProc->xmin when there is no
45 * active snapshot.
46 *
47 * However, there are a couple of exceptions where the active snapshot stack
48 * does not strictly mirror the call stack:
49 *
50 * - VACUUM and a few other utility commands manage their own transactions,
51 * which take their own snapshots. They are called with an active snapshot
52 * set, like most utility commands, but they pop the active snapshot that
53 * was pushed by the caller. PortalRunUtility knows about the possibility
54 * that the snapshot it pushed is no longer active on return.
55 *
56 * - When COMMIT or ROLLBACK is executed within a procedure or DO-block, the
57 * active snapshot stack is destroyed, and re-established later when
58 * subsequent statements in the procedure are executed. There are many
59 * limitations on when in-procedure COMMIT/ROLLBACK is allowed; one such
60 * limitation is that all the snapshots on the active snapshot stack are
61 * known to portals that are being executed, which makes it safe to reset
62 * the stack. See EnsurePortalSnapshotExists().
63 *
64 * Registered snapshots
65 * --------------------
66 *
67 * In addition to snapshots pushed to the active snapshot stack, a snapshot
68 * can be registered with a resource owner.
69 *
70 * The FirstXactSnapshot, if any, is treated a bit specially: we increment its
71 * regd_count and list it in RegisteredSnapshots, but this reference is not
72 * tracked by a resource owner. We used to use the TopTransactionResourceOwner
73 * to track this snapshot reference, but that introduces logical circularity
74 * and thus makes it impossible to clean up in a sane fashion. It's better to
75 * handle this reference as an internally-tracked registration, so that this
76 * module is entirely lower-level than ResourceOwners.
77 *
78 * Likewise, any snapshots that have been exported by pg_export_snapshot
79 * have regd_count = 1 and are listed in RegisteredSnapshots, but are not
80 * tracked by any resource owner.
81 *
82 * Likewise, the CatalogSnapshot is listed in RegisteredSnapshots when it
83 * is valid, but is not tracked by any resource owner.
84 *
85 * The same is true for historic snapshots used during logical decoding,
86 * their lifetime is managed separately (as they live longer than one xact.c
87 * transaction).
88 *
89 * These arrangements let us reset MyProc->xmin when there are no snapshots
90 * referenced by this transaction, and advance it when the one with oldest
91 * Xmin is no longer referenced. For simplicity however, only registered
92 * snapshots not active snapshots participate in tracking which one is oldest;
93 * we don't try to change MyProc->xmin except when the active-snapshot
94 * stack is empty.
95 *
96 *
97 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
98 * Portions Copyright (c) 1994, Regents of the University of California
99 *
100 * IDENTIFICATION
101 * src/backend/utils/time/snapmgr.c
102 *
103 *-------------------------------------------------------------------------
104 */
105#include "postgres.h"
106
107#include <sys/stat.h>
108#include <unistd.h>
109
110#include "access/subtrans.h"
111#include "access/transam.h"
112#include "access/xact.h"
113#include "datatype/timestamp.h"
114#include "lib/pairingheap.h"
115#include "miscadmin.h"
116#include "port/pg_lfind.h"
117#include "storage/fd.h"
118#include "storage/predicate.h"
119#include "storage/proc.h"
120#include "storage/procarray.h"
121#include "utils/builtins.h"
122#include "utils/memutils.h"
123#include "utils/resowner.h"
124#include "utils/snapmgr.h"
125#include "utils/syscache.h"
126
127
128/*
129 * CurrentSnapshot points to the only snapshot taken in transaction-snapshot
130 * mode, and to the latest one taken in a read-committed transaction.
131 * SecondarySnapshot is a snapshot that's always up-to-date as of the current
132 * instant, even in transaction-snapshot mode. It should only be used for
133 * special-purpose code (say, RI checking.) CatalogSnapshot points to an
134 * MVCC snapshot intended to be used for catalog scans; we must invalidate it
135 * whenever a system catalog change occurs.
136 *
137 * These SnapshotData structs are static to simplify memory allocation
138 * (see the hack in GetSnapshotData to avoid repeated malloc/free).
139 */
146
147/* Pointers to valid snapshots */
152
153/*
154 * These are updated by GetSnapshotData. We initialize them this way
155 * for the convenience of TransactionIdIsInProgress: even in bootstrap
156 * mode, we don't want it to say that BootstrapTransactionId is in progress.
157 */
160
161/* (table, ctid) => (cmin, cmax) mapping during timetravel */
162static HTAB *tuplecid_data = NULL;
163
164/*
165 * Elements of the active snapshot stack.
166 *
167 * Each element here accounts for exactly one active_count on SnapshotData.
168 *
169 * NB: the code assumes that elements in this list are in non-increasing
170 * order of as_level; also, the list must be NULL-terminated.
171 */
172typedef struct ActiveSnapshotElt
173{
178
179/* Top of the stack of active snapshots */
181
182/*
183 * Currently registered Snapshots. Ordered in a heap by xmin, so that we can
184 * quickly find the one with lowest xmin, to advance our MyProc->xmin.
185 */
186static int xmin_cmp(const pairingheap_node *a, const pairingheap_node *b,
187 void *arg);
188
190
191/* first GetTransactionSnapshot call in a transaction? */
192bool FirstSnapshotSet = false;
193
194/*
195 * Remember the serializable transaction snapshot, if any. We cannot trust
196 * FirstSnapshotSet in combination with IsolationUsesXactSnapshot(), because
197 * GUC may be reset before us, changing the value of IsolationUsesXactSnapshot.
198 */
200
201/* Define pathname of exported-snapshot files */
202#define SNAPSHOT_EXPORT_DIR "pg_snapshots"
203
204/* Structure holding info about exported snapshot. */
205typedef struct ExportedSnapshot
206{
207 char *snapfile;
210
211/* Current xact's exported snapshots (a list of ExportedSnapshot structs) */
213
214/* Prototypes for local functions */
215static Snapshot CopySnapshot(Snapshot snapshot);
216static void UnregisterSnapshotNoOwner(Snapshot snapshot);
217static void FreeSnapshot(Snapshot snapshot);
218static void SnapshotResetXmin(void);
219
220/* ResourceOwner callbacks to track snapshot references */
221static void ResOwnerReleaseSnapshot(Datum res);
222
224{
225 .name = "snapshot reference",
226 .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
227 .release_priority = RELEASE_PRIO_SNAPSHOT_REFS,
228 .ReleaseResource = ResOwnerReleaseSnapshot,
229 .DebugPrint = NULL /* the default message is fine */
230};
231
232/* Convenience wrappers over ResourceOwnerRemember/Forget */
233static inline void
235{
237}
238static inline void
240{
242}
243
244/*
245 * Snapshot fields to be serialized.
246 *
247 * Only these fields need to be sent to the cooperating backend; the
248 * remaining ones can (and must) be set by the receiver upon restore.
249 */
251{
260
261/*
262 * GetTransactionSnapshot
263 * Get the appropriate snapshot for a new query in a transaction.
264 *
265 * Note that the return value points at static storage that will be modified
266 * by future calls and by CommandCounterIncrement(). Callers must call
267 * RegisterSnapshot or PushActiveSnapshot on the returned snap before doing
268 * any other non-trivial work that could invalidate it.
269 */
272{
273 /*
274 * This should not be called while doing logical decoding. Historic
275 * snapshots are only usable for catalog access, not for general-purpose
276 * queries.
277 */
279 elog(ERROR, "cannot take query snapshot during logical decoding");
280
281 /* First call in transaction? */
282 if (!FirstSnapshotSet)
283 {
284 /*
285 * Don't allow catalog snapshot to be older than xact snapshot. Must
286 * do this first to allow the empty-heap Assert to succeed.
287 */
289
291 Assert(FirstXactSnapshot == NULL);
292
293 if (IsInParallelMode())
294 elog(ERROR,
295 "cannot take query snapshot during a parallel operation");
296
297 /*
298 * In transaction-snapshot mode, the first snapshot must live until
299 * end of xact regardless of what the caller does with it, so we must
300 * make a copy of it rather than returning CurrentSnapshotData
301 * directly. Furthermore, if we're running in serializable mode,
302 * predicate.c needs to wrap the snapshot fetch in its own processing.
303 */
305 {
306 /* First, create the snapshot in CurrentSnapshotData */
309 else
311 /* Make a saved copy */
314 /* Mark it as "registered" in FirstXactSnapshot */
317 }
318 else
320
321 FirstSnapshotSet = true;
322 return CurrentSnapshot;
323 }
324
326 return CurrentSnapshot;
327
328 /* Don't allow catalog snapshot to be older than xact snapshot. */
330
332
333 return CurrentSnapshot;
334}
335
336/*
337 * GetLatestSnapshot
338 * Get a snapshot that is up-to-date as of the current instant,
339 * even if we are executing in transaction-snapshot mode.
340 */
343{
344 /*
345 * We might be able to relax this, but nothing that could otherwise work
346 * needs it.
347 */
348 if (IsInParallelMode())
349 elog(ERROR,
350 "cannot update SecondarySnapshot during a parallel operation");
351
352 /*
353 * So far there are no cases requiring support for GetLatestSnapshot()
354 * during logical decoding, but it wouldn't be hard to add if required.
355 */
357
358 /* If first call in transaction, go ahead and set the xact snapshot */
359 if (!FirstSnapshotSet)
360 return GetTransactionSnapshot();
361
363
364 return SecondarySnapshot;
365}
366
367/*
368 * GetCatalogSnapshot
369 * Get a snapshot that is sufficiently up-to-date for scan of the
370 * system catalog with the specified OID.
371 */
374{
375 /*
376 * Return historic snapshot while we're doing logical decoding, so we can
377 * see the appropriate state of the catalog.
378 *
379 * This is the primary reason for needing to reset the system caches after
380 * finishing decoding.
381 */
383 return HistoricSnapshot;
384
385 return GetNonHistoricCatalogSnapshot(relid);
386}
387
388/*
389 * GetNonHistoricCatalogSnapshot
390 * Get a snapshot that is sufficiently up-to-date for scan of the system
391 * catalog with the specified OID, even while historic snapshots are set
392 * up.
393 */
396{
397 /*
398 * If the caller is trying to scan a relation that has no syscache, no
399 * catcache invalidations will be sent when it is updated. For a few key
400 * relations, snapshot invalidations are sent instead. If we're trying to
401 * scan a relation for which neither catcache nor snapshot invalidations
402 * are sent, we must refresh the snapshot every time.
403 */
404 if (CatalogSnapshot &&
406 !RelationHasSysCache(relid))
408
409 if (CatalogSnapshot == NULL)
410 {
411 /* Get new snapshot. */
413
414 /*
415 * Make sure the catalog snapshot will be accounted for in decisions
416 * about advancing PGPROC->xmin. We could apply RegisterSnapshot, but
417 * that would result in making a physical copy, which is overkill; and
418 * it would also create a dependency on some resource owner, which we
419 * do not want for reasons explained at the head of this file. Instead
420 * just shove the CatalogSnapshot into the pairing heap manually. This
421 * has to be reversed in InvalidateCatalogSnapshot, of course.
422 *
423 * NB: it had better be impossible for this to throw error, since the
424 * CatalogSnapshot pointer is already valid.
425 */
427 }
428
429 return CatalogSnapshot;
430}
431
432/*
433 * InvalidateCatalogSnapshot
434 * Mark the current catalog snapshot, if any, as invalid
435 *
436 * We could change this API to allow the caller to provide more fine-grained
437 * invalidation details, so that a change to relation A wouldn't prevent us
438 * from using our cached snapshot to scan relation B, but so far there's no
439 * evidence that the CPU cycles we spent tracking such fine details would be
440 * well-spent.
441 */
442void
444{
445 if (CatalogSnapshot)
446 {
448 CatalogSnapshot = NULL;
450 }
451}
452
453/*
454 * InvalidateCatalogSnapshotConditionally
455 * Drop catalog snapshot if it's the only one we have
456 *
457 * This is called when we are about to wait for client input, so we don't
458 * want to continue holding the catalog snapshot if it might mean that the
459 * global xmin horizon can't advance. However, if there are other snapshots
460 * still active or registered, the catalog snapshot isn't likely to be the
461 * oldest one, so we might as well keep it.
462 */
463void
465{
466 if (CatalogSnapshot &&
467 ActiveSnapshot == NULL &&
470}
471
472/*
473 * SnapshotSetCommandId
474 * Propagate CommandCounterIncrement into the static snapshots, if set
475 */
476void
478{
479 if (!FirstSnapshotSet)
480 return;
481
482 if (CurrentSnapshot)
483 CurrentSnapshot->curcid = curcid;
485 SecondarySnapshot->curcid = curcid;
486 /* Should we do the same with CatalogSnapshot? */
487}
488
489/*
490 * SetTransactionSnapshot
491 * Set the transaction's snapshot from an imported MVCC snapshot.
492 *
493 * Note that this is very closely tied to GetTransactionSnapshot --- it
494 * must take care of all the same considerations as the first-snapshot case
495 * in GetTransactionSnapshot.
496 */
497static void
499 int sourcepid, PGPROC *sourceproc)
500{
501 /* Caller should have checked this already */
503
504 /* Better do this to ensure following Assert succeeds. */
506
508 Assert(FirstXactSnapshot == NULL);
510
511 /*
512 * Even though we are not going to use the snapshot it computes, we must
513 * call GetSnapshotData, for two reasons: (1) to be sure that
514 * CurrentSnapshotData's XID arrays have been allocated, and (2) to update
515 * the state for GlobalVis*.
516 */
518
519 /*
520 * Now copy appropriate fields from the source snapshot.
521 */
522 CurrentSnapshot->xmin = sourcesnap->xmin;
523 CurrentSnapshot->xmax = sourcesnap->xmax;
524 CurrentSnapshot->xcnt = sourcesnap->xcnt;
525 Assert(sourcesnap->xcnt <= GetMaxSnapshotXidCount());
526 if (sourcesnap->xcnt > 0)
527 memcpy(CurrentSnapshot->xip, sourcesnap->xip,
528 sourcesnap->xcnt * sizeof(TransactionId));
529 CurrentSnapshot->subxcnt = sourcesnap->subxcnt;
530 Assert(sourcesnap->subxcnt <= GetMaxSnapshotSubxidCount());
531 if (sourcesnap->subxcnt > 0)
532 memcpy(CurrentSnapshot->subxip, sourcesnap->subxip,
533 sourcesnap->subxcnt * sizeof(TransactionId));
536 /* NB: curcid should NOT be copied, it's a local matter */
537
539
540 /*
541 * Now we have to fix what GetSnapshotData did with MyProc->xmin and
542 * TransactionXmin. There is a race condition: to make sure we are not
543 * causing the global xmin to go backwards, we have to test that the
544 * source transaction is still running, and that has to be done
545 * atomically. So let procarray.c do it.
546 *
547 * Note: in serializable mode, predicate.c will do this a second time. It
548 * doesn't seem worth contorting the logic here to avoid two calls,
549 * especially since it's not clear that predicate.c *must* do this.
550 */
551 if (sourceproc != NULL)
552 {
555 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
556 errmsg("could not import the requested snapshot"),
557 errdetail("The source transaction is not running anymore.")));
558 }
559 else if (!ProcArrayInstallImportedXmin(CurrentSnapshot->xmin, sourcevxid))
561 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
562 errmsg("could not import the requested snapshot"),
563 errdetail("The source process with PID %d is not running anymore.",
564 sourcepid)));
565
566 /*
567 * In transaction-snapshot mode, the first snapshot must live until end of
568 * xact, so we must make a copy of it. Furthermore, if we're running in
569 * serializable mode, predicate.c needs to do its own processing.
570 */
572 {
575 sourcepid);
576 /* Make a saved copy */
579 /* Mark it as "registered" in FirstXactSnapshot */
582 }
583
584 FirstSnapshotSet = true;
585}
586
587/*
588 * CopySnapshot
589 * Copy the given snapshot.
590 *
591 * The copy is palloc'd in TopTransactionContext and has initial refcounts set
592 * to 0. The returned snapshot has the copied flag set.
593 */
594static Snapshot
596{
597 Snapshot newsnap;
598 Size subxipoff;
599 Size size;
600
601 Assert(snapshot != InvalidSnapshot);
602
603 /* We allocate any XID arrays needed in the same palloc block. */
604 size = subxipoff = sizeof(SnapshotData) +
605 snapshot->xcnt * sizeof(TransactionId);
606 if (snapshot->subxcnt > 0)
607 size += snapshot->subxcnt * sizeof(TransactionId);
608
610 memcpy(newsnap, snapshot, sizeof(SnapshotData));
611
612 newsnap->regd_count = 0;
613 newsnap->active_count = 0;
614 newsnap->copied = true;
615 newsnap->snapXactCompletionCount = 0;
616
617 /* setup XID array */
618 if (snapshot->xcnt > 0)
619 {
620 newsnap->xip = (TransactionId *) (newsnap + 1);
621 memcpy(newsnap->xip, snapshot->xip,
622 snapshot->xcnt * sizeof(TransactionId));
623 }
624 else
625 newsnap->xip = NULL;
626
627 /*
628 * Setup subXID array. Don't bother to copy it if it had overflowed,
629 * though, because it's not used anywhere in that case. Except if it's a
630 * snapshot taken during recovery; all the top-level XIDs are in subxip as
631 * well in that case, so we mustn't lose them.
632 */
633 if (snapshot->subxcnt > 0 &&
634 (!snapshot->suboverflowed || snapshot->takenDuringRecovery))
635 {
636 newsnap->subxip = (TransactionId *) ((char *) newsnap + subxipoff);
637 memcpy(newsnap->subxip, snapshot->subxip,
638 snapshot->subxcnt * sizeof(TransactionId));
639 }
640 else
641 newsnap->subxip = NULL;
642
643 return newsnap;
644}
645
646/*
647 * FreeSnapshot
648 * Free the memory associated with a snapshot.
649 */
650static void
652{
653 Assert(snapshot->regd_count == 0);
654 Assert(snapshot->active_count == 0);
655 Assert(snapshot->copied);
656
657 pfree(snapshot);
658}
659
660/*
661 * PushActiveSnapshot
662 * Set the given snapshot as the current active snapshot
663 *
664 * If the passed snapshot is a statically-allocated one, or it is possibly
665 * subject to a future command counter update, create a new long-lived copy
666 * with active refcount=1. Otherwise, only increment the refcount.
667 */
668void
670{
672}
673
674/*
675 * PushActiveSnapshotWithLevel
676 * Set the given snapshot as the current active snapshot
677 *
678 * Same as PushActiveSnapshot except that caller can specify the
679 * transaction nesting level that "owns" the snapshot. This level
680 * must not be deeper than the current top of the snapshot stack.
681 */
682void
683PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
684{
685 ActiveSnapshotElt *newactive;
686
687 Assert(snapshot != InvalidSnapshot);
688 Assert(ActiveSnapshot == NULL || snap_level >= ActiveSnapshot->as_level);
689
691
692 /*
693 * Checking SecondarySnapshot is probably useless here, but it seems
694 * better to be sure.
695 */
696 if (snapshot == CurrentSnapshot || snapshot == SecondarySnapshot ||
697 !snapshot->copied)
698 newactive->as_snap = CopySnapshot(snapshot);
699 else
700 newactive->as_snap = snapshot;
701
702 newactive->as_next = ActiveSnapshot;
703 newactive->as_level = snap_level;
704
705 newactive->as_snap->active_count++;
706
707 ActiveSnapshot = newactive;
708}
709
710/*
711 * PushCopiedSnapshot
712 * As above, except forcibly copy the presented snapshot.
713 *
714 * This should be used when the ActiveSnapshot has to be modifiable, for
715 * example if the caller intends to call UpdateActiveSnapshotCommandId.
716 * The new snapshot will be released when popped from the stack.
717 */
718void
720{
722}
723
724/*
725 * UpdateActiveSnapshotCommandId
726 *
727 * Update the current CID of the active snapshot. This can only be applied
728 * to a snapshot that is not referenced elsewhere.
729 */
730void
732{
733 CommandId save_curcid,
734 curcid;
735
736 Assert(ActiveSnapshot != NULL);
739
740 /*
741 * Don't allow modification of the active snapshot during parallel
742 * operation. We share the snapshot to worker backends at the beginning
743 * of parallel operation, so any change to the snapshot can lead to
744 * inconsistencies. We have other defenses against
745 * CommandCounterIncrement, but there are a few places that call this
746 * directly, so we put an additional guard here.
747 */
748 save_curcid = ActiveSnapshot->as_snap->curcid;
749 curcid = GetCurrentCommandId(false);
750 if (IsInParallelMode() && save_curcid != curcid)
751 elog(ERROR, "cannot modify commandid in active snapshot during a parallel operation");
752 ActiveSnapshot->as_snap->curcid = curcid;
753}
754
755/*
756 * PopActiveSnapshot
757 *
758 * Remove the topmost snapshot from the active snapshot stack, decrementing the
759 * reference count, and free it if this was the last reference.
760 */
761void
763{
764 ActiveSnapshotElt *newstack;
765
766 newstack = ActiveSnapshot->as_next;
767
769
771
775
777 ActiveSnapshot = newstack;
778
780}
781
782/*
783 * GetActiveSnapshot
784 * Return the topmost snapshot in the Active stack.
785 */
788{
789 Assert(ActiveSnapshot != NULL);
790
791 return ActiveSnapshot->as_snap;
792}
793
794/*
795 * ActiveSnapshotSet
796 * Return whether there is at least one snapshot in the Active stack
797 */
798bool
800{
801 return ActiveSnapshot != NULL;
802}
803
804/*
805 * RegisterSnapshot
806 * Register a snapshot as being in use by the current resource owner
807 *
808 * If InvalidSnapshot is passed, it is not registered.
809 */
812{
813 if (snapshot == InvalidSnapshot)
814 return InvalidSnapshot;
815
817}
818
819/*
820 * RegisterSnapshotOnOwner
821 * As above, but use the specified resource owner
822 */
825{
826 Snapshot snap;
827
828 if (snapshot == InvalidSnapshot)
829 return InvalidSnapshot;
830
831 /* Static snapshot? Create a persistent copy */
832 snap = snapshot->copied ? snapshot : CopySnapshot(snapshot);
833
834 /* and tell resowner.c about it */
836 snap->regd_count++;
838
839 if (snap->regd_count == 1)
841
842 return snap;
843}
844
845/*
846 * UnregisterSnapshot
847 *
848 * Decrement the reference count of a snapshot, remove the corresponding
849 * reference from CurrentResourceOwner, and free the snapshot if no more
850 * references remain.
851 */
852void
854{
855 if (snapshot == NULL)
856 return;
857
859}
860
861/*
862 * UnregisterSnapshotFromOwner
863 * As above, but use the specified resource owner
864 */
865void
867{
868 if (snapshot == NULL)
869 return;
870
871 ResourceOwnerForgetSnapshot(owner, snapshot);
873}
874
875static void
877{
878 Assert(snapshot->regd_count > 0);
880
881 snapshot->regd_count--;
882 if (snapshot->regd_count == 0)
884
885 if (snapshot->regd_count == 0 && snapshot->active_count == 0)
886 {
887 FreeSnapshot(snapshot);
889 }
890}
891
892/*
893 * Comparison function for RegisteredSnapshots heap. Snapshots are ordered
894 * by xmin, so that the snapshot with smallest xmin is at the top.
895 */
896static int
898{
899 const SnapshotData *asnap = pairingheap_const_container(SnapshotData, ph_node, a);
900 const SnapshotData *bsnap = pairingheap_const_container(SnapshotData, ph_node, b);
901
902 if (TransactionIdPrecedes(asnap->xmin, bsnap->xmin))
903 return 1;
904 else if (TransactionIdFollows(asnap->xmin, bsnap->xmin))
905 return -1;
906 else
907 return 0;
908}
909
910/*
911 * SnapshotResetXmin
912 *
913 * If there are no more snapshots, we can reset our PGPROC->xmin to
914 * InvalidTransactionId. Note we can do this without locking because we assume
915 * that storing an Xid is atomic.
916 *
917 * Even if there are some remaining snapshots, we may be able to advance our
918 * PGPROC->xmin to some degree. This typically happens when a portal is
919 * dropped. For efficiency, we only consider recomputing PGPROC->xmin when
920 * the active snapshot stack is empty; this allows us not to need to track
921 * which active snapshot is oldest.
922 */
923static void
925{
926 Snapshot minSnapshot;
927
928 if (ActiveSnapshot != NULL)
929 return;
930
932 {
934 return;
935 }
936
937 minSnapshot = pairingheap_container(SnapshotData, ph_node,
939
940 if (TransactionIdPrecedes(MyProc->xmin, minSnapshot->xmin))
941 MyProc->xmin = TransactionXmin = minSnapshot->xmin;
942}
943
944/*
945 * AtSubCommit_Snapshot
946 */
947void
949{
950 ActiveSnapshotElt *active;
951
952 /*
953 * Relabel the active snapshots set in this subtransaction as though they
954 * are owned by the parent subxact.
955 */
956 for (active = ActiveSnapshot; active != NULL; active = active->as_next)
957 {
958 if (active->as_level < level)
959 break;
960 active->as_level = level - 1;
961 }
962}
963
964/*
965 * AtSubAbort_Snapshot
966 * Clean up snapshots after a subtransaction abort
967 */
968void
970{
971 /* Forget the active snapshots set by this subtransaction */
972 while (ActiveSnapshot && ActiveSnapshot->as_level >= level)
973 {
975
977
978 /*
979 * Decrement the snapshot's active count. If it's still registered or
980 * marked as active by an outer subtransaction, we can't free it yet.
981 */
984
988
989 /* and free the stack element */
991
993 }
994
996}
997
998/*
999 * AtEOXact_Snapshot
1000 * Snapshot manager's cleanup function for end of transaction
1001 */
1002void
1003AtEOXact_Snapshot(bool isCommit, bool resetXmin)
1004{
1005 /*
1006 * In transaction-snapshot mode we must release our privately-managed
1007 * reference to the transaction snapshot. We must remove it from
1008 * RegisteredSnapshots to keep the check below happy. But we don't bother
1009 * to do FreeSnapshot, for two reasons: the memory will go away with
1010 * TopTransactionContext anyway, and if someone has left the snapshot
1011 * stacked as active, we don't want the code below to be chasing through a
1012 * dangling pointer.
1013 */
1014 if (FirstXactSnapshot != NULL)
1015 {
1019 }
1020 FirstXactSnapshot = NULL;
1021
1022 /*
1023 * If we exported any snapshots, clean them up.
1024 */
1025 if (exportedSnapshots != NIL)
1026 {
1027 ListCell *lc;
1028
1029 /*
1030 * Get rid of the files. Unlink failure is only a WARNING because (1)
1031 * it's too late to abort the transaction, and (2) leaving a leaked
1032 * file around has little real consequence anyway.
1033 *
1034 * We also need to remove the snapshots from RegisteredSnapshots to
1035 * prevent a warning below.
1036 *
1037 * As with the FirstXactSnapshot, we don't need to free resources of
1038 * the snapshot itself as it will go away with the memory context.
1039 */
1040 foreach(lc, exportedSnapshots)
1041 {
1042 ExportedSnapshot *esnap = (ExportedSnapshot *) lfirst(lc);
1043
1044 if (unlink(esnap->snapfile))
1045 elog(WARNING, "could not unlink file \"%s\": %m",
1046 esnap->snapfile);
1047
1049 &esnap->snapshot->ph_node);
1050 }
1051
1053 }
1054
1055 /* Drop catalog snapshot if any */
1057
1058 /* On commit, complain about leftover snapshots */
1059 if (isCommit)
1060 {
1061 ActiveSnapshotElt *active;
1062
1064 elog(WARNING, "registered snapshots seem to remain after cleanup");
1065
1066 /* complain about unpopped active snapshots */
1067 for (active = ActiveSnapshot; active != NULL; active = active->as_next)
1068 elog(WARNING, "snapshot %p still active", active);
1069 }
1070
1071 /*
1072 * And reset our state. We don't need to free the memory explicitly --
1073 * it'll go away with TopTransactionContext.
1074 */
1075 ActiveSnapshot = NULL;
1077
1078 CurrentSnapshot = NULL;
1079 SecondarySnapshot = NULL;
1080
1081 FirstSnapshotSet = false;
1082
1083 /*
1084 * During normal commit processing, we call ProcArrayEndTransaction() to
1085 * reset the MyProc->xmin. That call happens prior to the call to
1086 * AtEOXact_Snapshot(), so we need not touch xmin here at all.
1087 */
1088 if (resetXmin)
1090
1091 Assert(resetXmin || MyProc->xmin == 0);
1092}
1093
1094
1095/*
1096 * ExportSnapshot
1097 * Export the snapshot to a file so that other backends can import it.
1098 * Returns the token (the file name) that can be used to import this
1099 * snapshot.
1100 */
1101char *
1103{
1104 TransactionId topXid;
1105 TransactionId *children;
1106 ExportedSnapshot *esnap;
1107 int nchildren;
1108 int addTopXid;
1110 FILE *f;
1111 int i;
1112 MemoryContext oldcxt;
1113 char path[MAXPGPATH];
1114 char pathtmp[MAXPGPATH];
1115
1116 /*
1117 * It's tempting to call RequireTransactionBlock here, since it's not very
1118 * useful to export a snapshot that will disappear immediately afterwards.
1119 * However, we haven't got enough information to do that, since we don't
1120 * know if we're at top level or not. For example, we could be inside a
1121 * plpgsql function that is going to fire off other transactions via
1122 * dblink. Rather than disallow perfectly legitimate usages, don't make a
1123 * check.
1124 *
1125 * Also note that we don't make any restriction on the transaction's
1126 * isolation level; however, importers must check the level if they are
1127 * serializable.
1128 */
1129
1130 /*
1131 * Get our transaction ID if there is one, to include in the snapshot.
1132 */
1133 topXid = GetTopTransactionIdIfAny();
1134
1135 /*
1136 * We cannot export a snapshot from a subtransaction because there's no
1137 * easy way for importers to verify that the same subtransaction is still
1138 * running.
1139 */
1140 if (IsSubTransaction())
1141 ereport(ERROR,
1142 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1143 errmsg("cannot export a snapshot from a subtransaction")));
1144
1145 /*
1146 * We do however allow previous committed subtransactions to exist.
1147 * Importers of the snapshot must see them as still running, so get their
1148 * XIDs to add them to the snapshot.
1149 */
1150 nchildren = xactGetCommittedChildren(&children);
1151
1152 /*
1153 * Generate file path for the snapshot. We start numbering of snapshots
1154 * inside the transaction from 1.
1155 */
1156 snprintf(path, sizeof(path), SNAPSHOT_EXPORT_DIR "/%08X-%08X-%d",
1159
1160 /*
1161 * Copy the snapshot into TopTransactionContext, add it to the
1162 * exportedSnapshots list, and mark it pseudo-registered. We do this to
1163 * ensure that the snapshot's xmin is honored for the rest of the
1164 * transaction.
1165 */
1166 snapshot = CopySnapshot(snapshot);
1167
1169 esnap = (ExportedSnapshot *) palloc(sizeof(ExportedSnapshot));
1170 esnap->snapfile = pstrdup(path);
1171 esnap->snapshot = snapshot;
1173 MemoryContextSwitchTo(oldcxt);
1174
1175 snapshot->regd_count++;
1177
1178 /*
1179 * Fill buf with a text serialization of the snapshot, plus identification
1180 * data about this transaction. The format expected by ImportSnapshot is
1181 * pretty rigid: each line must be fieldname:value.
1182 */
1184
1185 appendStringInfo(&buf, "vxid:%d/%u\n", MyProc->vxid.procNumber, MyProc->vxid.lxid);
1186 appendStringInfo(&buf, "pid:%d\n", MyProcPid);
1187 appendStringInfo(&buf, "dbid:%u\n", MyDatabaseId);
1188 appendStringInfo(&buf, "iso:%d\n", XactIsoLevel);
1189 appendStringInfo(&buf, "ro:%d\n", XactReadOnly);
1190
1191 appendStringInfo(&buf, "xmin:%u\n", snapshot->xmin);
1192 appendStringInfo(&buf, "xmax:%u\n", snapshot->xmax);
1193
1194 /*
1195 * We must include our own top transaction ID in the top-xid data, since
1196 * by definition we will still be running when the importing transaction
1197 * adopts the snapshot, but GetSnapshotData never includes our own XID in
1198 * the snapshot. (There must, therefore, be enough room to add it.)
1199 *
1200 * However, it could be that our topXid is after the xmax, in which case
1201 * we shouldn't include it because xip[] members are expected to be before
1202 * xmax. (We need not make the same check for subxip[] members, see
1203 * snapshot.h.)
1204 */
1205 addTopXid = (TransactionIdIsValid(topXid) &&
1206 TransactionIdPrecedes(topXid, snapshot->xmax)) ? 1 : 0;
1207 appendStringInfo(&buf, "xcnt:%d\n", snapshot->xcnt + addTopXid);
1208 for (i = 0; i < snapshot->xcnt; i++)
1209 appendStringInfo(&buf, "xip:%u\n", snapshot->xip[i]);
1210 if (addTopXid)
1211 appendStringInfo(&buf, "xip:%u\n", topXid);
1212
1213 /*
1214 * Similarly, we add our subcommitted child XIDs to the subxid data. Here,
1215 * we have to cope with possible overflow.
1216 */
1217 if (snapshot->suboverflowed ||
1218 snapshot->subxcnt + nchildren > GetMaxSnapshotSubxidCount())
1219 appendStringInfoString(&buf, "sof:1\n");
1220 else
1221 {
1222 appendStringInfoString(&buf, "sof:0\n");
1223 appendStringInfo(&buf, "sxcnt:%d\n", snapshot->subxcnt + nchildren);
1224 for (i = 0; i < snapshot->subxcnt; i++)
1225 appendStringInfo(&buf, "sxp:%u\n", snapshot->subxip[i]);
1226 for (i = 0; i < nchildren; i++)
1227 appendStringInfo(&buf, "sxp:%u\n", children[i]);
1228 }
1229 appendStringInfo(&buf, "rec:%u\n", snapshot->takenDuringRecovery);
1230
1231 /*
1232 * Now write the text representation into a file. We first write to a
1233 * ".tmp" filename, and rename to final filename if no error. This
1234 * ensures that no other backend can read an incomplete file
1235 * (ImportSnapshot won't allow it because of its valid-characters check).
1236 */
1237 snprintf(pathtmp, sizeof(pathtmp), "%s.tmp", path);
1238 if (!(f = AllocateFile(pathtmp, PG_BINARY_W)))
1239 ereport(ERROR,
1241 errmsg("could not create file \"%s\": %m", pathtmp)));
1242
1243 if (fwrite(buf.data, buf.len, 1, f) != 1)
1244 ereport(ERROR,
1246 errmsg("could not write to file \"%s\": %m", pathtmp)));
1247
1248 /* no fsync() since file need not survive a system crash */
1249
1250 if (FreeFile(f))
1251 ereport(ERROR,
1253 errmsg("could not write to file \"%s\": %m", pathtmp)));
1254
1255 /*
1256 * Now that we have written everything into a .tmp file, rename the file
1257 * to remove the .tmp suffix.
1258 */
1259 if (rename(pathtmp, path) < 0)
1260 ereport(ERROR,
1262 errmsg("could not rename file \"%s\" to \"%s\": %m",
1263 pathtmp, path)));
1264
1265 /*
1266 * The basename of the file is what we return from pg_export_snapshot().
1267 * It's already in path in a textual format and we know that the path
1268 * starts with SNAPSHOT_EXPORT_DIR. Skip over the prefix and the slash
1269 * and pstrdup it so as not to return the address of a local variable.
1270 */
1271 return pstrdup(path + strlen(SNAPSHOT_EXPORT_DIR) + 1);
1272}
1273
1274/*
1275 * pg_export_snapshot
1276 * SQL-callable wrapper for ExportSnapshot.
1277 */
1278Datum
1280{
1281 char *snapshotName;
1282
1283 snapshotName = ExportSnapshot(GetActiveSnapshot());
1284 PG_RETURN_TEXT_P(cstring_to_text(snapshotName));
1285}
1286
1287
1288/*
1289 * Parsing subroutines for ImportSnapshot: parse a line with the given
1290 * prefix followed by a value, and advance *s to the next line. The
1291 * filename is provided for use in error messages.
1292 */
1293static int
1294parseIntFromText(const char *prefix, char **s, const char *filename)
1295{
1296 char *ptr = *s;
1297 int prefixlen = strlen(prefix);
1298 int val;
1299
1300 if (strncmp(ptr, prefix, prefixlen) != 0)
1301 ereport(ERROR,
1302 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1303 errmsg("invalid snapshot data in file \"%s\"", filename)));
1304 ptr += prefixlen;
1305 if (sscanf(ptr, "%d", &val) != 1)
1306 ereport(ERROR,
1307 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1308 errmsg("invalid snapshot data in file \"%s\"", filename)));
1309 ptr = strchr(ptr, '\n');
1310 if (!ptr)
1311 ereport(ERROR,
1312 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1313 errmsg("invalid snapshot data in file \"%s\"", filename)));
1314 *s = ptr + 1;
1315 return val;
1316}
1317
1318static TransactionId
1319parseXidFromText(const char *prefix, char **s, const char *filename)
1320{
1321 char *ptr = *s;
1322 int prefixlen = strlen(prefix);
1324
1325 if (strncmp(ptr, prefix, prefixlen) != 0)
1326 ereport(ERROR,
1327 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1328 errmsg("invalid snapshot data in file \"%s\"", filename)));
1329 ptr += prefixlen;
1330 if (sscanf(ptr, "%u", &val) != 1)
1331 ereport(ERROR,
1332 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1333 errmsg("invalid snapshot data in file \"%s\"", filename)));
1334 ptr = strchr(ptr, '\n');
1335 if (!ptr)
1336 ereport(ERROR,
1337 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1338 errmsg("invalid snapshot data in file \"%s\"", filename)));
1339 *s = ptr + 1;
1340 return val;
1341}
1342
1343static void
1344parseVxidFromText(const char *prefix, char **s, const char *filename,
1346{
1347 char *ptr = *s;
1348 int prefixlen = strlen(prefix);
1349
1350 if (strncmp(ptr, prefix, prefixlen) != 0)
1351 ereport(ERROR,
1352 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1353 errmsg("invalid snapshot data in file \"%s\"", filename)));
1354 ptr += prefixlen;
1355 if (sscanf(ptr, "%d/%u", &vxid->procNumber, &vxid->localTransactionId) != 2)
1356 ereport(ERROR,
1357 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1358 errmsg("invalid snapshot data in file \"%s\"", filename)));
1359 ptr = strchr(ptr, '\n');
1360 if (!ptr)
1361 ereport(ERROR,
1362 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1363 errmsg("invalid snapshot data in file \"%s\"", filename)));
1364 *s = ptr + 1;
1365}
1366
1367/*
1368 * ImportSnapshot
1369 * Import a previously exported snapshot. The argument should be a
1370 * filename in SNAPSHOT_EXPORT_DIR. Load the snapshot from that file.
1371 * This is called by "SET TRANSACTION SNAPSHOT 'foo'".
1372 */
1373void
1374ImportSnapshot(const char *idstr)
1375{
1376 char path[MAXPGPATH];
1377 FILE *f;
1378 struct stat stat_buf;
1379 char *filebuf;
1380 int xcnt;
1381 int i;
1382 VirtualTransactionId src_vxid;
1383 int src_pid;
1384 Oid src_dbid;
1385 int src_isolevel;
1386 bool src_readonly;
1387 SnapshotData snapshot;
1388
1389 /*
1390 * Must be at top level of a fresh transaction. Note in particular that
1391 * we check we haven't acquired an XID --- if we have, it's conceivable
1392 * that the snapshot would show it as not running, making for very screwy
1393 * behavior.
1394 */
1395 if (FirstSnapshotSet ||
1398 ereport(ERROR,
1399 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1400 errmsg("SET TRANSACTION SNAPSHOT must be called before any query")));
1401
1402 /*
1403 * If we are in read committed mode then the next query would execute with
1404 * a new snapshot thus making this function call quite useless.
1405 */
1407 ereport(ERROR,
1408 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1409 errmsg("a snapshot-importing transaction must have isolation level SERIALIZABLE or REPEATABLE READ")));
1410
1411 /*
1412 * Verify the identifier: only 0-9, A-F and hyphens are allowed. We do
1413 * this mainly to prevent reading arbitrary files.
1414 */
1415 if (strspn(idstr, "0123456789ABCDEF-") != strlen(idstr))
1416 ereport(ERROR,
1417 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1418 errmsg("invalid snapshot identifier: \"%s\"", idstr)));
1419
1420 /* OK, read the file */
1421 snprintf(path, MAXPGPATH, SNAPSHOT_EXPORT_DIR "/%s", idstr);
1422
1423 f = AllocateFile(path, PG_BINARY_R);
1424 if (!f)
1425 {
1426 /*
1427 * If file is missing while identifier has a correct format, avoid
1428 * system errors.
1429 */
1430 if (errno == ENOENT)
1431 ereport(ERROR,
1432 (errcode(ERRCODE_UNDEFINED_OBJECT),
1433 errmsg("snapshot \"%s\" does not exist", idstr)));
1434 else
1435 ereport(ERROR,
1437 errmsg("could not open file \"%s\" for reading: %m",
1438 path)));
1439 }
1440
1441 /* get the size of the file so that we know how much memory we need */
1442 if (fstat(fileno(f), &stat_buf))
1443 elog(ERROR, "could not stat file \"%s\": %m", path);
1444
1445 /* and read the file into a palloc'd string */
1446 filebuf = (char *) palloc(stat_buf.st_size + 1);
1447 if (fread(filebuf, stat_buf.st_size, 1, f) != 1)
1448 elog(ERROR, "could not read file \"%s\": %m", path);
1449
1450 filebuf[stat_buf.st_size] = '\0';
1451
1452 FreeFile(f);
1453
1454 /*
1455 * Construct a snapshot struct by parsing the file content.
1456 */
1457 memset(&snapshot, 0, sizeof(snapshot));
1458
1459 parseVxidFromText("vxid:", &filebuf, path, &src_vxid);
1460 src_pid = parseIntFromText("pid:", &filebuf, path);
1461 /* we abuse parseXidFromText a bit here ... */
1462 src_dbid = parseXidFromText("dbid:", &filebuf, path);
1463 src_isolevel = parseIntFromText("iso:", &filebuf, path);
1464 src_readonly = parseIntFromText("ro:", &filebuf, path);
1465
1466 snapshot.snapshot_type = SNAPSHOT_MVCC;
1467
1468 snapshot.xmin = parseXidFromText("xmin:", &filebuf, path);
1469 snapshot.xmax = parseXidFromText("xmax:", &filebuf, path);
1470
1471 snapshot.xcnt = xcnt = parseIntFromText("xcnt:", &filebuf, path);
1472
1473 /* sanity-check the xid count before palloc */
1474 if (xcnt < 0 || xcnt > GetMaxSnapshotXidCount())
1475 ereport(ERROR,
1476 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1477 errmsg("invalid snapshot data in file \"%s\"", path)));
1478
1479 snapshot.xip = (TransactionId *) palloc(xcnt * sizeof(TransactionId));
1480 for (i = 0; i < xcnt; i++)
1481 snapshot.xip[i] = parseXidFromText("xip:", &filebuf, path);
1482
1483 snapshot.suboverflowed = parseIntFromText("sof:", &filebuf, path);
1484
1485 if (!snapshot.suboverflowed)
1486 {
1487 snapshot.subxcnt = xcnt = parseIntFromText("sxcnt:", &filebuf, path);
1488
1489 /* sanity-check the xid count before palloc */
1490 if (xcnt < 0 || xcnt > GetMaxSnapshotSubxidCount())
1491 ereport(ERROR,
1492 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1493 errmsg("invalid snapshot data in file \"%s\"", path)));
1494
1495 snapshot.subxip = (TransactionId *) palloc(xcnt * sizeof(TransactionId));
1496 for (i = 0; i < xcnt; i++)
1497 snapshot.subxip[i] = parseXidFromText("sxp:", &filebuf, path);
1498 }
1499 else
1500 {
1501 snapshot.subxcnt = 0;
1502 snapshot.subxip = NULL;
1503 }
1504
1505 snapshot.takenDuringRecovery = parseIntFromText("rec:", &filebuf, path);
1506
1507 /*
1508 * Do some additional sanity checking, just to protect ourselves. We
1509 * don't trouble to check the array elements, just the most critical
1510 * fields.
1511 */
1512 if (!VirtualTransactionIdIsValid(src_vxid) ||
1513 !OidIsValid(src_dbid) ||
1514 !TransactionIdIsNormal(snapshot.xmin) ||
1515 !TransactionIdIsNormal(snapshot.xmax))
1516 ereport(ERROR,
1517 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1518 errmsg("invalid snapshot data in file \"%s\"", path)));
1519
1520 /*
1521 * If we're serializable, the source transaction must be too, otherwise
1522 * predicate.c has problems (SxactGlobalXmin could go backwards). Also, a
1523 * non-read-only transaction can't adopt a snapshot from a read-only
1524 * transaction, as predicate.c handles the cases very differently.
1525 */
1527 {
1528 if (src_isolevel != XACT_SERIALIZABLE)
1529 ereport(ERROR,
1530 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1531 errmsg("a serializable transaction cannot import a snapshot from a non-serializable transaction")));
1532 if (src_readonly && !XactReadOnly)
1533 ereport(ERROR,
1534 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1535 errmsg("a non-read-only serializable transaction cannot import a snapshot from a read-only transaction")));
1536 }
1537
1538 /*
1539 * We cannot import a snapshot that was taken in a different database,
1540 * because vacuum calculates OldestXmin on a per-database basis; so the
1541 * source transaction's xmin doesn't protect us from data loss. This
1542 * restriction could be removed if the source transaction were to mark its
1543 * xmin as being globally applicable. But that would require some
1544 * additional syntax, since that has to be known when the snapshot is
1545 * initially taken. (See pgsql-hackers discussion of 2011-10-21.)
1546 */
1547 if (src_dbid != MyDatabaseId)
1548 ereport(ERROR,
1549 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1550 errmsg("cannot import a snapshot from a different database")));
1551
1552 /* OK, install the snapshot */
1553 SetTransactionSnapshot(&snapshot, &src_vxid, src_pid, NULL);
1554}
1555
1556/*
1557 * XactHasExportedSnapshots
1558 * Test whether current transaction has exported any snapshots.
1559 */
1560bool
1562{
1563 return (exportedSnapshots != NIL);
1564}
1565
1566/*
1567 * DeleteAllExportedSnapshotFiles
1568 * Clean up any files that have been left behind by a crashed backend
1569 * that had exported snapshots before it died.
1570 *
1571 * This should be called during database startup or crash recovery.
1572 */
1573void
1575{
1576 char buf[MAXPGPATH + sizeof(SNAPSHOT_EXPORT_DIR)];
1577 DIR *s_dir;
1578 struct dirent *s_de;
1579
1580 /*
1581 * Problems in reading the directory, or unlinking files, are reported at
1582 * LOG level. Since we're running in the startup process, ERROR level
1583 * would prevent database start, and it's not important enough for that.
1584 */
1586
1587 while ((s_de = ReadDirExtended(s_dir, SNAPSHOT_EXPORT_DIR, LOG)) != NULL)
1588 {
1589 if (strcmp(s_de->d_name, ".") == 0 ||
1590 strcmp(s_de->d_name, "..") == 0)
1591 continue;
1592
1593 snprintf(buf, sizeof(buf), SNAPSHOT_EXPORT_DIR "/%s", s_de->d_name);
1594
1595 if (unlink(buf) != 0)
1596 ereport(LOG,
1598 errmsg("could not remove file \"%s\": %m", buf)));
1599 }
1600
1601 FreeDir(s_dir);
1602}
1603
1604/*
1605 * ThereAreNoPriorRegisteredSnapshots
1606 * Is the registered snapshot count less than or equal to one?
1607 *
1608 * Don't use this to settle important decisions. While zero registrations and
1609 * no ActiveSnapshot would confirm a certain idleness, the system makes no
1610 * guarantees about the significance of one registered snapshot.
1611 */
1612bool
1614{
1617 return true;
1618
1619 return false;
1620}
1621
1622/*
1623 * HaveRegisteredOrActiveSnapshot
1624 * Is there any registered or active snapshot?
1625 *
1626 * NB: Unless pushed or active, the cached catalog snapshot will not cause
1627 * this function to return true. That allows this function to be used in
1628 * checks enforcing a longer-lived snapshot.
1629 */
1630bool
1632{
1633 if (ActiveSnapshot != NULL)
1634 return true;
1635
1636 /*
1637 * The catalog snapshot is in RegisteredSnapshots when valid, but can be
1638 * removed at any time due to invalidation processing. If explicitly
1639 * registered more than one snapshot has to be in RegisteredSnapshots.
1640 */
1641 if (CatalogSnapshot != NULL &&
1643 return false;
1644
1646}
1647
1648
1649/*
1650 * Setup a snapshot that replaces normal catalog snapshots that allows catalog
1651 * access to behave just like it did at a certain point in the past.
1652 *
1653 * Needed for logical decoding.
1654 */
1655void
1656SetupHistoricSnapshot(Snapshot historic_snapshot, HTAB *tuplecids)
1657{
1658 Assert(historic_snapshot != NULL);
1659
1660 /* setup the timetravel snapshot */
1661 HistoricSnapshot = historic_snapshot;
1662
1663 /* setup (cmin, cmax) lookup hash */
1664 tuplecid_data = tuplecids;
1665}
1666
1667
1668/*
1669 * Make catalog snapshots behave normally again.
1670 */
1671void
1673{
1674 HistoricSnapshot = NULL;
1675 tuplecid_data = NULL;
1676}
1677
1678bool
1680{
1681 return HistoricSnapshot != NULL;
1682}
1683
1684HTAB *
1686{
1688 return tuplecid_data;
1689}
1690
1691/*
1692 * EstimateSnapshotSpace
1693 * Returns the size needed to store the given snapshot.
1694 *
1695 * We are exporting only required fields from the Snapshot, stored in
1696 * SerializedSnapshotData.
1697 */
1698Size
1700{
1701 Size size;
1702
1703 Assert(snapshot != InvalidSnapshot);
1704 Assert(snapshot->snapshot_type == SNAPSHOT_MVCC);
1705
1706 /* We allocate any XID arrays needed in the same palloc block. */
1707 size = add_size(sizeof(SerializedSnapshotData),
1708 mul_size(snapshot->xcnt, sizeof(TransactionId)));
1709 if (snapshot->subxcnt > 0 &&
1710 (!snapshot->suboverflowed || snapshot->takenDuringRecovery))
1711 size = add_size(size,
1712 mul_size(snapshot->subxcnt, sizeof(TransactionId)));
1713
1714 return size;
1715}
1716
1717/*
1718 * SerializeSnapshot
1719 * Dumps the serialized snapshot (extracted from given snapshot) onto the
1720 * memory location at start_address.
1721 */
1722void
1723SerializeSnapshot(Snapshot snapshot, char *start_address)
1724{
1725 SerializedSnapshotData serialized_snapshot;
1726
1727 Assert(snapshot->subxcnt >= 0);
1728
1729 /* Copy all required fields */
1730 serialized_snapshot.xmin = snapshot->xmin;
1731 serialized_snapshot.xmax = snapshot->xmax;
1732 serialized_snapshot.xcnt = snapshot->xcnt;
1733 serialized_snapshot.subxcnt = snapshot->subxcnt;
1734 serialized_snapshot.suboverflowed = snapshot->suboverflowed;
1735 serialized_snapshot.takenDuringRecovery = snapshot->takenDuringRecovery;
1736 serialized_snapshot.curcid = snapshot->curcid;
1737
1738 /*
1739 * Ignore the SubXID array if it has overflowed, unless the snapshot was
1740 * taken during recovery - in that case, top-level XIDs are in subxip as
1741 * well, and we mustn't lose them.
1742 */
1743 if (serialized_snapshot.suboverflowed && !snapshot->takenDuringRecovery)
1744 serialized_snapshot.subxcnt = 0;
1745
1746 /* Copy struct to possibly-unaligned buffer */
1747 memcpy(start_address,
1748 &serialized_snapshot, sizeof(SerializedSnapshotData));
1749
1750 /* Copy XID array */
1751 if (snapshot->xcnt > 0)
1752 memcpy((TransactionId *) (start_address +
1753 sizeof(SerializedSnapshotData)),
1754 snapshot->xip, snapshot->xcnt * sizeof(TransactionId));
1755
1756 /*
1757 * Copy SubXID array. Don't bother to copy it if it had overflowed,
1758 * though, because it's not used anywhere in that case. Except if it's a
1759 * snapshot taken during recovery; all the top-level XIDs are in subxip as
1760 * well in that case, so we mustn't lose them.
1761 */
1762 if (serialized_snapshot.subxcnt > 0)
1763 {
1764 Size subxipoff = sizeof(SerializedSnapshotData) +
1765 snapshot->xcnt * sizeof(TransactionId);
1766
1767 memcpy((TransactionId *) (start_address + subxipoff),
1768 snapshot->subxip, snapshot->subxcnt * sizeof(TransactionId));
1769 }
1770}
1771
1772/*
1773 * RestoreSnapshot
1774 * Restore a serialized snapshot from the specified address.
1775 *
1776 * The copy is palloc'd in TopTransactionContext and has initial refcounts set
1777 * to 0. The returned snapshot has the copied flag set.
1778 */
1780RestoreSnapshot(char *start_address)
1781{
1782 SerializedSnapshotData serialized_snapshot;
1783 Size size;
1784 Snapshot snapshot;
1785 TransactionId *serialized_xids;
1786
1787 memcpy(&serialized_snapshot, start_address,
1788 sizeof(SerializedSnapshotData));
1789 serialized_xids = (TransactionId *)
1790 (start_address + sizeof(SerializedSnapshotData));
1791
1792 /* We allocate any XID arrays needed in the same palloc block. */
1793 size = sizeof(SnapshotData)
1794 + serialized_snapshot.xcnt * sizeof(TransactionId)
1795 + serialized_snapshot.subxcnt * sizeof(TransactionId);
1796
1797 /* Copy all required fields */
1799 snapshot->snapshot_type = SNAPSHOT_MVCC;
1800 snapshot->xmin = serialized_snapshot.xmin;
1801 snapshot->xmax = serialized_snapshot.xmax;
1802 snapshot->xip = NULL;
1803 snapshot->xcnt = serialized_snapshot.xcnt;
1804 snapshot->subxip = NULL;
1805 snapshot->subxcnt = serialized_snapshot.subxcnt;
1806 snapshot->suboverflowed = serialized_snapshot.suboverflowed;
1807 snapshot->takenDuringRecovery = serialized_snapshot.takenDuringRecovery;
1808 snapshot->curcid = serialized_snapshot.curcid;
1809 snapshot->snapXactCompletionCount = 0;
1810
1811 /* Copy XIDs, if present. */
1812 if (serialized_snapshot.xcnt > 0)
1813 {
1814 snapshot->xip = (TransactionId *) (snapshot + 1);
1815 memcpy(snapshot->xip, serialized_xids,
1816 serialized_snapshot.xcnt * sizeof(TransactionId));
1817 }
1818
1819 /* Copy SubXIDs, if present. */
1820 if (serialized_snapshot.subxcnt > 0)
1821 {
1822 snapshot->subxip = ((TransactionId *) (snapshot + 1)) +
1823 serialized_snapshot.xcnt;
1824 memcpy(snapshot->subxip, serialized_xids + serialized_snapshot.xcnt,
1825 serialized_snapshot.subxcnt * sizeof(TransactionId));
1826 }
1827
1828 /* Set the copied flag so that the caller will set refcounts correctly. */
1829 snapshot->regd_count = 0;
1830 snapshot->active_count = 0;
1831 snapshot->copied = true;
1832
1833 return snapshot;
1834}
1835
1836/*
1837 * Install a restored snapshot as the transaction snapshot.
1838 *
1839 * The second argument is of type void * so that snapmgr.h need not include
1840 * the declaration for PGPROC.
1841 */
1842void
1843RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
1844{
1845 SetTransactionSnapshot(snapshot, NULL, InvalidPid, source_pgproc);
1846}
1847
1848/*
1849 * XidInMVCCSnapshot
1850 * Is the given XID still-in-progress according to the snapshot?
1851 *
1852 * Note: GetSnapshotData never stores either top xid or subxids of our own
1853 * backend into a snapshot, so these xids will not be reported as "running"
1854 * by this function. This is OK for current uses, because we always check
1855 * TransactionIdIsCurrentTransactionId first, except when it's known the
1856 * XID could not be ours anyway.
1857 */
1858bool
1860{
1861 /*
1862 * Make a quick range check to eliminate most XIDs without looking at the
1863 * xip arrays. Note that this is OK even if we convert a subxact XID to
1864 * its parent below, because a subxact with XID < xmin has surely also got
1865 * a parent with XID < xmin, while one with XID >= xmax must belong to a
1866 * parent that was not yet committed at the time of this snapshot.
1867 */
1868
1869 /* Any xid < xmin is not in-progress */
1870 if (TransactionIdPrecedes(xid, snapshot->xmin))
1871 return false;
1872 /* Any xid >= xmax is in-progress */
1873 if (TransactionIdFollowsOrEquals(xid, snapshot->xmax))
1874 return true;
1875
1876 /*
1877 * Snapshot information is stored slightly differently in snapshots taken
1878 * during recovery.
1879 */
1880 if (!snapshot->takenDuringRecovery)
1881 {
1882 /*
1883 * If the snapshot contains full subxact data, the fastest way to
1884 * check things is just to compare the given XID against both subxact
1885 * XIDs and top-level XIDs. If the snapshot overflowed, we have to
1886 * use pg_subtrans to convert a subxact XID to its parent XID, but
1887 * then we need only look at top-level XIDs not subxacts.
1888 */
1889 if (!snapshot->suboverflowed)
1890 {
1891 /* we have full data, so search subxip */
1892 if (pg_lfind32(xid, snapshot->subxip, snapshot->subxcnt))
1893 return true;
1894
1895 /* not there, fall through to search xip[] */
1896 }
1897 else
1898 {
1899 /*
1900 * Snapshot overflowed, so convert xid to top-level. This is safe
1901 * because we eliminated too-old XIDs above.
1902 */
1904
1905 /*
1906 * If xid was indeed a subxact, we might now have an xid < xmin,
1907 * so recheck to avoid an array scan. No point in rechecking
1908 * xmax.
1909 */
1910 if (TransactionIdPrecedes(xid, snapshot->xmin))
1911 return false;
1912 }
1913
1914 if (pg_lfind32(xid, snapshot->xip, snapshot->xcnt))
1915 return true;
1916 }
1917 else
1918 {
1919 /*
1920 * In recovery we store all xids in the subxip array because it is by
1921 * far the bigger array, and we mostly don't know which xids are
1922 * top-level and which are subxacts. The xip array is empty.
1923 *
1924 * We start by searching subtrans, if we overflowed.
1925 */
1926 if (snapshot->suboverflowed)
1927 {
1928 /*
1929 * Snapshot overflowed, so convert xid to top-level. This is safe
1930 * because we eliminated too-old XIDs above.
1931 */
1933
1934 /*
1935 * If xid was indeed a subxact, we might now have an xid < xmin,
1936 * so recheck to avoid an array scan. No point in rechecking
1937 * xmax.
1938 */
1939 if (TransactionIdPrecedes(xid, snapshot->xmin))
1940 return false;
1941 }
1942
1943 /*
1944 * We now have either a top-level xid higher than xmin or an
1945 * indeterminate xid. We don't know whether it's top level or subxact
1946 * but it doesn't matter. If it's present, the xid is visible.
1947 */
1948 if (pg_lfind32(xid, snapshot->subxip, snapshot->subxcnt))
1949 return true;
1950 }
1951
1952 return false;
1953}
1954
1955/* ResourceOwner callbacks */
1956
1957static void
1959{
1961}
static int32 next
Definition: blutils.c:224
#define PG_BINARY_R
Definition: c.h:1246
int32_t int32
Definition: c.h:498
uint32_t uint32
Definition: c.h:502
#define PG_BINARY_W
Definition: c.h:1247
uint32 CommandId
Definition: c.h:637
uint32 TransactionId
Definition: c.h:623
#define OidIsValid(objectId)
Definition: c.h:746
size_t Size
Definition: c.h:576
int errcode_for_file_access(void)
Definition: elog.c:877
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
int FreeDir(DIR *dir)
Definition: fd.c:3025
int FreeFile(FILE *file)
Definition: fd.c:2843
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2988
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2907
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2644
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
int MyProcPid
Definition: globals.c:48
Oid MyDatabaseId
Definition: globals.c:95
Assert(PointerIsAligned(start, uint64))
long val
Definition: informix.c:689
int b
Definition: isn.c:74
int a
Definition: isn.c:73
int i
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:339
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:68
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1256
MemoryContext TopTransactionContext
Definition: mcxt.c:170
char * pstrdup(const char *in)
Definition: mcxt.c:2322
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc(Size size)
Definition: mcxt.c:1940
#define InvalidPid
Definition: miscadmin.h:32
void pairingheap_remove(pairingheap *heap, pairingheap_node *node)
Definition: pairingheap.c:170
void pairingheap_add(pairingheap *heap, pairingheap_node *node)
Definition: pairingheap.c:112
pairingheap_node * pairingheap_first(pairingheap *heap)
Definition: pairingheap.c:130
#define pairingheap_is_empty(h)
Definition: pairingheap.h:96
#define pairingheap_is_singular(h)
Definition: pairingheap.h:99
#define pairingheap_container(type, membername, ptr)
Definition: pairingheap.h:43
#define pairingheap_const_container(type, membername, ptr)
Definition: pairingheap.h:51
#define pairingheap_reset(h)
Definition: pairingheap.h:93
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:123
static bool pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
Definition: pg_lfind.h:153
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:239
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
unsigned int Oid
Definition: postgres_ext.h:30
void SetSerializableTransactionSnapshot(Snapshot snapshot, VirtualTransactionId *sourcevxid, int sourcepid)
Definition: predicate.c:1722
Snapshot GetSerializableTransactionSnapshot(Snapshot snapshot)
Definition: predicate.c:1682
int GetMaxSnapshotSubxidCount(void)
Definition: procarray.c:2080
Snapshot GetSnapshotData(Snapshot snapshot)
Definition: procarray.c:2175
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:2069
bool ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
Definition: procarray.c:2616
bool ProcArrayInstallImportedXmin(TransactionId xmin, VirtualTransactionId *sourcevxid)
Definition: procarray.c:2532
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:564
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:524
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:452
#define RELEASE_PRIO_SNAPSHOT_REFS
Definition: resowner.h:75
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:56
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static Snapshot HistoricSnapshot
Definition: snapmgr.c:151
static Snapshot FirstXactSnapshot
Definition: snapmgr.c:199
TransactionId RecentXmin
Definition: snapmgr.c:159
static SnapshotData CatalogSnapshotData
Definition: snapmgr.c:142
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:866
static void SetTransactionSnapshot(Snapshot sourcesnap, VirtualTransactionId *sourcevxid, int sourcepid, PGPROC *sourceproc)
Definition: snapmgr.c:498
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:969
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:1723
SnapshotData SnapshotSelfData
Definition: snapmgr.c:143
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1003
struct ActiveSnapshotElt ActiveSnapshotElt
static Snapshot CurrentSnapshot
Definition: snapmgr.c:148
static Snapshot SecondarySnapshot
Definition: snapmgr.c:149
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:1859
static List * exportedSnapshots
Definition: snapmgr.c:212
static pairingheap RegisteredSnapshots
Definition: snapmgr.c:189
bool FirstSnapshotSet
Definition: snapmgr.c:192
static void UnregisterSnapshotNoOwner(Snapshot snapshot)
Definition: snapmgr.c:876
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:271
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:342
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:1672
Snapshot GetCatalogSnapshot(Oid relid)
Definition: snapmgr.c:373
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:853
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:669
static Snapshot CopySnapshot(Snapshot snapshot)
Definition: snapmgr.c:595
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:1780
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:948
static void ResourceOwnerRememberSnapshot(ResourceOwner owner, Snapshot snap)
Definition: snapmgr.c:234
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:731
static void SnapshotResetXmin(void)
Definition: snapmgr.c:924
static int parseIntFromText(const char *prefix, char **s, const char *filename)
Definition: snapmgr.c:1294
static SnapshotData SecondarySnapshotData
Definition: snapmgr.c:141
static int xmin_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
Definition: snapmgr.c:897
TransactionId TransactionXmin
Definition: snapmgr.c:158
SnapshotData SnapshotAnyData
Definition: snapmgr.c:144
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:1679
void ImportSnapshot(const char *idstr)
Definition: snapmgr.c:1374
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:799
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:811
bool XactHasExportedSnapshots(void)
Definition: snapmgr.c:1561
static const ResourceOwnerDesc snapshot_resowner_desc
Definition: snapmgr.c:223
void DeleteAllExportedSnapshotFiles(void)
Definition: snapmgr.c:1574
static void parseVxidFromText(const char *prefix, char **s, const char *filename, VirtualTransactionId *vxid)
Definition: snapmgr.c:1344
static void FreeSnapshot(Snapshot snapshot)
Definition: snapmgr.c:651
#define SNAPSHOT_EXPORT_DIR
Definition: snapmgr.c:202
bool HaveRegisteredOrActiveSnapshot(void)
Definition: snapmgr.c:1631
void InvalidateCatalogSnapshotConditionally(void)
Definition: snapmgr.c:464
static SnapshotData CurrentSnapshotData
Definition: snapmgr.c:140
SnapshotData SnapshotToastData
Definition: snapmgr.c:145
static void ResourceOwnerForgetSnapshot(ResourceOwner owner, Snapshot snap)
Definition: snapmgr.c:239
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1613
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
Definition: snapmgr.c:1843
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:477
struct SerializedSnapshotData SerializedSnapshotData
void PopActiveSnapshot(void)
Definition: snapmgr.c:762
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:719
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1102
Size EstimateSnapshotSpace(Snapshot snapshot)
Definition: snapmgr.c:1699
static ActiveSnapshotElt * ActiveSnapshot
Definition: snapmgr.c:180
void SetupHistoricSnapshot(Snapshot historic_snapshot, HTAB *tuplecids)
Definition: snapmgr.c:1656
static HTAB * tuplecid_data
Definition: snapmgr.c:162
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:824
static TransactionId parseXidFromText(const char *prefix, char **s, const char *filename)
Definition: snapmgr.c:1319
HTAB * HistoricSnapshotGetTupleCids(void)
Definition: snapmgr.c:1685
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:443
void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
Definition: snapmgr.c:683
Snapshot GetNonHistoricCatalogSnapshot(Oid relid)
Definition: snapmgr.c:395
struct ExportedSnapshot ExportedSnapshot
static Snapshot CatalogSnapshot
Definition: snapmgr.c:150
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:787
Datum pg_export_snapshot(PG_FUNCTION_ARGS)
Definition: snapmgr.c:1279
static void ResOwnerReleaseSnapshot(Datum res)
Definition: snapmgr.c:1958
struct SnapshotData * Snapshot
Definition: snapshot.h:117
struct SnapshotData SnapshotData
@ SNAPSHOT_TOAST
Definition: snapshot.h:70
@ SNAPSHOT_SELF
Definition: snapshot.h:60
@ SNAPSHOT_MVCC
Definition: snapshot.h:46
@ SNAPSHOT_ANY
Definition: snapshot.h:65
#define InvalidSnapshot
Definition: snapshot.h:119
PGPROC * MyProc
Definition: proc.c:67
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
struct ActiveSnapshotElt * as_next
Definition: snapmgr.c:176
Snapshot as_snap
Definition: snapmgr.c:174
Definition: dirent.c:26
char * snapfile
Definition: snapmgr.c:207
Snapshot snapshot
Definition: snapmgr.c:208
Definition: dynahash.c:220
Definition: pg_list.h:54
Definition: proc.h:163
TransactionId xmin
Definition: proc.h:178
struct PGPROC::@127 vxid
LocalTransactionId lxid
Definition: proc.h:201
ProcNumber procNumber
Definition: proc.h:196
const char * name
Definition: resowner.h:93
TransactionId xmax
Definition: snapmgr.c:253
TransactionId xmin
Definition: snapmgr.c:252
TransactionId xmin
Definition: snapshot.h:153
int32 subxcnt
Definition: snapshot.h:177
bool copied
Definition: snapshot.h:181
uint32 regd_count
Definition: snapshot.h:201
uint32 active_count
Definition: snapshot.h:200
CommandId curcid
Definition: snapshot.h:183
pairingheap_node ph_node
Definition: snapshot.h:202
uint32 xcnt
Definition: snapshot.h:165
TransactionId * subxip
Definition: snapshot.h:176
uint64 snapXactCompletionCount
Definition: snapshot.h:209
TransactionId xmax
Definition: snapshot.h:154
SnapshotType snapshot_type
Definition: snapshot.h:140
TransactionId * xip
Definition: snapshot.h:164
bool suboverflowed
Definition: snapshot.h:178
bool takenDuringRecovery
Definition: snapshot.h:180
LocalTransactionId localTransactionId
Definition: lock.h:63
ProcNumber procNumber
Definition: lock.h:62
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
__int64 st_size
Definition: win32_port.h:263
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:163
bool RelationHasSysCache(Oid relid)
Definition: syscache.c:745
bool RelationInvalidatesSnapshotsOnly(Oid relid)
Definition: syscache.c:722
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:314
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:329
#define InvalidTransactionId
Definition: transam.h:31
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
text * cstring_to_text(const char *s)
Definition: varlena.c:192
#define fstat
Definition: win32_port.h:273
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:929
bool XactReadOnly
Definition: xact.c:82
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:441
int XactIsoLevel
Definition: xact.c:79
bool IsSubTransaction(void)
Definition: xact.c:5044
bool IsInParallelMode(void)
Definition: xact.c:1089
int xactGetCommittedChildren(TransactionId **ptr)
Definition: xact.c:5790
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:829
#define XACT_SERIALIZABLE
Definition: xact.h:39
#define IsolationUsesXactSnapshot()
Definition: xact.h:51
#define IsolationIsSerializable()
Definition: xact.h:52