PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
procarray.h File Reference
#include "storage/lock.h"
#include "storage/standby.h"
#include "utils/relcache.h"
#include "utils/snapshot.h"
Include dependency graph for procarray.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PROCARRAY_VACUUM_FLAG   0x02 /* currently running lazy vacuum */
 
#define PROCARRAY_ANALYZE_FLAG   0x04 /* currently running analyze */
 
#define PROCARRAY_LOGICAL_DECODING_FLAG
 
#define PROCARRAY_SLOTS_XMIN
 
#define PROCARRAY_PROC_FLAGS_MASK
 
#define PROCARRAY_FLAGS_DEFAULT   PROCARRAY_LOGICAL_DECODING_FLAG
 
#define PROCARRAY_FLAGS_VACUUM   PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG
 
#define PROCARRAY_FLAGS_ANALYZE   PROCARRAY_FLAGS_DEFAULT | PROCARRAY_ANALYZE_FLAG
 
#define PROCARRAY_FLAGS_VACUUM_ANALYZE   PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG | PROCARRAY_ANALYZE_FLAG
 

Functions

Size ProcArrayShmemSize (void)
 
void CreateSharedProcArray (void)
 
void ProcArrayAdd (PGPROC *proc)
 
void ProcArrayRemove (PGPROC *proc, TransactionId latestXid)
 
void ProcArrayEndTransaction (PGPROC *proc, TransactionId latestXid)
 
void ProcArrayClearTransaction (PGPROC *proc)
 
void ProcArrayInitRecovery (TransactionId initializedUptoXID)
 
void ProcArrayApplyRecoveryInfo (RunningTransactions running)
 
void ProcArrayApplyXidAssignment (TransactionId topxid, int nsubxids, TransactionId *subxids)
 
void RecordKnownAssignedTransactionIds (TransactionId xid)
 
void ExpireTreeKnownAssignedTransactionIds (TransactionId xid, int nsubxids, TransactionId *subxids, TransactionId max_xid)
 
void ExpireAllKnownAssignedTransactionIds (void)
 
void ExpireOldKnownAssignedTransactionIds (TransactionId xid)
 
int GetMaxSnapshotXidCount (void)
 
int GetMaxSnapshotSubxidCount (void)
 
Snapshot GetSnapshotData (Snapshot snapshot)
 
bool ProcArrayInstallImportedXmin (TransactionId xmin, TransactionId sourcexid)
 
bool ProcArrayInstallRestoredXmin (TransactionId xmin, PGPROC *proc)
 
RunningTransactions GetRunningTransactionData (void)
 
bool TransactionIdIsInProgress (TransactionId xid)
 
bool TransactionIdIsActive (TransactionId xid)
 
TransactionId GetOldestXmin (Relation rel, int flags)
 
TransactionId GetOldestActiveTransactionId (void)
 
TransactionId GetOldestSafeDecodingTransactionId (void)
 
VirtualTransactionIdGetVirtualXIDsDelayingChkpt (int *nvxids)
 
bool HaveVirtualXIDsDelayingChkpt (VirtualTransactionId *vxids, int nvxids)
 
PGPROCBackendPidGetProc (int pid)
 
PGPROCBackendPidGetProcWithLock (int pid)
 
int BackendXidGetPid (TransactionId xid)
 
bool IsBackendPid (int pid)
 
VirtualTransactionIdGetCurrentVirtualXIDs (TransactionId limitXmin, bool excludeXmin0, bool allDbs, int excludeVacuum, int *nvxids)
 
VirtualTransactionIdGetConflictingVirtualXIDs (TransactionId limitXmin, Oid dbOid)
 
pid_t CancelVirtualTransaction (VirtualTransactionId vxid, ProcSignalReason sigmode)
 
bool MinimumActiveBackends (int min)
 
int CountDBBackends (Oid databaseid)
 
int CountDBConnections (Oid databaseid)
 
void CancelDBBackends (Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
 
int CountUserBackends (Oid roleid)
 
bool CountOtherDBBackends (Oid databaseId, int *nbackends, int *nprepared)
 
void XidCacheRemoveRunningXids (TransactionId xid, int nxids, const TransactionId *xids, TransactionId latestXid)
 
void ProcArraySetReplicationSlotXmin (TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
 
void ProcArrayGetReplicationSlotXmin (TransactionId *xmin, TransactionId *catalog_xmin)
 

Macro Definition Documentation

#define PROCARRAY_ANALYZE_FLAG   0x04 /* currently running analyze */

Definition at line 31 of file procarray.h.

#define PROCARRAY_FLAGS_ANALYZE   PROCARRAY_FLAGS_DEFAULT | PROCARRAY_ANALYZE_FLAG

Definition at line 52 of file procarray.h.

#define PROCARRAY_FLAGS_DEFAULT   PROCARRAY_LOGICAL_DECODING_FLAG

Definition at line 48 of file procarray.h.

Referenced by CreateCheckPoint(), CreateRestartPoint(), and XLogWalRcvSendHSFeedback().

#define PROCARRAY_FLAGS_VACUUM_ANALYZE   PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG | PROCARRAY_ANALYZE_FLAG

Definition at line 54 of file procarray.h.

#define PROCARRAY_LOGICAL_DECODING_FLAG
Value:
0x10 /* currently doing logical
* decoding outside xact */

Definition at line 32 of file procarray.h.

#define PROCARRAY_PROC_FLAGS_MASK
Value:
#define PROCARRAY_ANALYZE_FLAG
Definition: procarray.h:31
#define PROCARRAY_VACUUM_FLAG
Definition: procarray.h:30
#define PROCARRAY_LOGICAL_DECODING_FLAG
Definition: procarray.h:32

Definition at line 42 of file procarray.h.

Referenced by GetOldestXmin().

#define PROCARRAY_SLOTS_XMIN
Value:
0x20 /* replication slot xmin,
* catalog_xmin */

Definition at line 35 of file procarray.h.

Referenced by GetOldestXmin(), and XLogWalRcvSendHSFeedback().

#define PROCARRAY_VACUUM_FLAG   0x02 /* currently running lazy vacuum */

Definition at line 30 of file procarray.h.

Function Documentation

PGPROC* BackendPidGetProc ( int  pid)

Definition at line 2334 of file procarray.c.

References BackendPidGetProcWithLock(), LW_SHARED, LWLockAcquire(), LWLockRelease(), NULL, and result.

Referenced by IsBackendPid(), pg_signal_backend(), pg_stat_get_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), ProcSendSignal(), and test_shm_mq_main().

2335 {
2336  PGPROC *result;
2337 
2338  if (pid == 0) /* never match dummy PGPROCs */
2339  return NULL;
2340 
2341  LWLockAcquire(ProcArrayLock, LW_SHARED);
2342 
2343  result = BackendPidGetProcWithLock(pid);
2344 
2345  LWLockRelease(ProcArrayLock);
2346 
2347  return result;
2348 }
return result
Definition: formatting.c:1618
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
PGPROC * BackendPidGetProcWithLock(int pid)
Definition: procarray.c:2357
#define NULL
Definition: c.h:229
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
PGPROC* BackendPidGetProcWithLock ( int  pid)

Definition at line 2357 of file procarray.c.

References NULL, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, and result.

Referenced by BackendPidGetProc(), and GetBlockerStatusData().

2358 {
2359  PGPROC *result = NULL;
2360  ProcArrayStruct *arrayP = procArray;
2361  int index;
2362 
2363  if (pid == 0) /* never match dummy PGPROCs */
2364  return NULL;
2365 
2366  for (index = 0; index < arrayP->numProcs; index++)
2367  {
2368  PGPROC *proc = &allProcs[arrayP->pgprocnos[index]];
2369 
2370  if (proc->pid == pid)
2371  {
2372  result = proc;
2373  break;
2374  }
2375  }
2376 
2377  return result;
2378 }
return result
Definition: formatting.c:1618
Definition: type.h:90
static PGPROC * allProcs
Definition: procarray.c:100
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define NULL
Definition: c.h:229
Definition: proc.h:94
int pid
Definition: proc.h:108
int BackendXidGetPid ( TransactionId  xid)

Definition at line 2394 of file procarray.c.

References InvalidTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, result, and PGXACT::xid.

Referenced by pgrowlocks().

2395 {
2396  int result = 0;
2397  ProcArrayStruct *arrayP = procArray;
2398  int index;
2399 
2400  if (xid == InvalidTransactionId) /* never match invalid xid */
2401  return 0;
2402 
2403  LWLockAcquire(ProcArrayLock, LW_SHARED);
2404 
2405  for (index = 0; index < arrayP->numProcs; index++)
2406  {
2407  int pgprocno = arrayP->pgprocnos[index];
2408  volatile PGPROC *proc = &allProcs[pgprocno];
2409  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2410 
2411  if (pgxact->xid == xid)
2412  {
2413  result = proc->pid;
2414  break;
2415  }
2416  }
2417 
2418  LWLockRelease(ProcArrayLock);
2419 
2420  return result;
2421 }
Definition: proc.h:207
TransactionId xid
Definition: proc.h:209
return result
Definition: formatting.c:1618
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define InvalidTransactionId
Definition: transam.h:31
static PGPROC * allProcs
Definition: procarray.c:100
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
void CancelDBBackends ( Oid  databaseid,
ProcSignalReason  sigmode,
bool  conflictPending 
)

Definition at line 2788 of file procarray.c.

References VirtualTransactionId::backendId, PGPROC::databaseId, GET_VXID_FROM_PGPROC, InvalidOid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, PGPROC::recoveryConflictPending, and SendProcSignal().

Referenced by ResolveRecoveryConflictWithDatabase(), and SendRecoveryConflictWithBufferPin().

2789 {
2790  ProcArrayStruct *arrayP = procArray;
2791  int index;
2792  pid_t pid = 0;
2793 
2794  /* tell all backends to die */
2795  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2796 
2797  for (index = 0; index < arrayP->numProcs; index++)
2798  {
2799  int pgprocno = arrayP->pgprocnos[index];
2800  volatile PGPROC *proc = &allProcs[pgprocno];
2801 
2802  if (databaseid == InvalidOid || proc->databaseId == databaseid)
2803  {
2804  VirtualTransactionId procvxid;
2805 
2806  GET_VXID_FROM_PGPROC(procvxid, *proc);
2807 
2808  proc->recoveryConflictPending = conflictPending;
2809  pid = proc->pid;
2810  if (pid != 0)
2811  {
2812  /*
2813  * Kill the pid if it's still here. If not, that's what we
2814  * wanted so ignore any errors.
2815  */
2816  (void) SendProcSignal(pid, sigmode, procvxid.backendId);
2817  }
2818  }
2819  }
2820 
2821  LWLockRelease(ProcArrayLock);
2822 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:179
bool recoveryConflictPending
Definition: proc.h:123
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define InvalidOid
Definition: postgres_ext.h:36
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
BackendId backendId
Definition: lock.h:65
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
pid_t CancelVirtualTransaction ( VirtualTransactionId  vxid,
ProcSignalReason  sigmode 
)

Definition at line 2625 of file procarray.c.

References VirtualTransactionId::backendId, GET_VXID_FROM_PGPROC, VirtualTransactionId::localTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, PGPROC::recoveryConflictPending, and SendProcSignal().

Referenced by ResolveRecoveryConflictWithVirtualXIDs().

2626 {
2627  ProcArrayStruct *arrayP = procArray;
2628  int index;
2629  pid_t pid = 0;
2630 
2631  LWLockAcquire(ProcArrayLock, LW_SHARED);
2632 
2633  for (index = 0; index < arrayP->numProcs; index++)
2634  {
2635  int pgprocno = arrayP->pgprocnos[index];
2636  volatile PGPROC *proc = &allProcs[pgprocno];
2637  VirtualTransactionId procvxid;
2638 
2639  GET_VXID_FROM_PGPROC(procvxid, *proc);
2640 
2641  if (procvxid.backendId == vxid.backendId &&
2642  procvxid.localTransactionId == vxid.localTransactionId)
2643  {
2644  proc->recoveryConflictPending = true;
2645  pid = proc->pid;
2646  if (pid != 0)
2647  {
2648  /*
2649  * Kill the pid if it's still here. If not, that's what we
2650  * wanted so ignore any errors.
2651  */
2652  (void) SendProcSignal(pid, sigmode, vxid.backendId);
2653  }
2654  break;
2655  }
2656  }
2657 
2658  LWLockRelease(ProcArrayLock);
2659 
2660  return pid;
2661 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
LocalTransactionId localTransactionId
Definition: lock.h:66
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:179
bool recoveryConflictPending
Definition: proc.h:123
static PGPROC * allProcs
Definition: procarray.c:100
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
BackendId backendId
Definition: lock.h:65
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
int CountDBBackends ( Oid  databaseid)

Definition at line 2727 of file procarray.c.

References PGPROC::databaseId, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, OidIsValid, ProcArrayStruct::pgprocnos, PGPROC::pid, and procArray.

Referenced by btree_xlog_delete_get_latestRemovedXid(), hash_xlog_vacuum_get_latestRemovedXid(), and ResolveRecoveryConflictWithDatabase().

2728 {
2729  ProcArrayStruct *arrayP = procArray;
2730  int count = 0;
2731  int index;
2732 
2733  LWLockAcquire(ProcArrayLock, LW_SHARED);
2734 
2735  for (index = 0; index < arrayP->numProcs; index++)
2736  {
2737  int pgprocno = arrayP->pgprocnos[index];
2738  volatile PGPROC *proc = &allProcs[pgprocno];
2739 
2740  if (proc->pid == 0)
2741  continue; /* do not count prepared xacts */
2742  if (!OidIsValid(databaseid) ||
2743  proc->databaseId == databaseid)
2744  count++;
2745  }
2746 
2747  LWLockRelease(ProcArrayLock);
2748 
2749  return count;
2750 }
#define OidIsValid(objectId)
Definition: c.h:538
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
int CountDBConnections ( Oid  databaseid)

Definition at line 2757 of file procarray.c.

References PGPROC::databaseId, PGPROC::isBackgroundWorker, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, OidIsValid, ProcArrayStruct::pgprocnos, PGPROC::pid, and procArray.

Referenced by CheckMyDatabase().

2758 {
2759  ProcArrayStruct *arrayP = procArray;
2760  int count = 0;
2761  int index;
2762 
2763  LWLockAcquire(ProcArrayLock, LW_SHARED);
2764 
2765  for (index = 0; index < arrayP->numProcs; index++)
2766  {
2767  int pgprocno = arrayP->pgprocnos[index];
2768  volatile PGPROC *proc = &allProcs[pgprocno];
2769 
2770  if (proc->pid == 0)
2771  continue; /* do not count prepared xacts */
2772  if (proc->isBackgroundWorker)
2773  continue; /* do not count background workers */
2774  if (!OidIsValid(databaseid) ||
2775  proc->databaseId == databaseid)
2776  count++;
2777  }
2778 
2779  LWLockRelease(ProcArrayLock);
2780 
2781  return count;
2782 }
#define OidIsValid(objectId)
Definition: c.h:538
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
bool isBackgroundWorker
Definition: proc.h:116
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
bool CountOtherDBBackends ( Oid  databaseId,
int *  nbackends,
int *  nprepared 
)

Definition at line 2878 of file procarray.c.

References CHECK_FOR_INTERRUPTS, PGPROC::databaseId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MAXAUTOVACPIDS, MyProc, ProcArrayStruct::numProcs, pg_usleep(), ProcArrayStruct::pgprocnos, PGPROC::pid, PROC_IS_AUTOVACUUM, procArray, and PGXACT::vacuumFlags.

Referenced by createdb(), dropdb(), movedb(), and RenameDatabase().

2879 {
2880  ProcArrayStruct *arrayP = procArray;
2881 
2882 #define MAXAUTOVACPIDS 10 /* max autovacs to SIGTERM per iteration */
2883  int autovac_pids[MAXAUTOVACPIDS];
2884  int tries;
2885 
2886  /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
2887  for (tries = 0; tries < 50; tries++)
2888  {
2889  int nautovacs = 0;
2890  bool found = false;
2891  int index;
2892 
2894 
2895  *nbackends = *nprepared = 0;
2896 
2897  LWLockAcquire(ProcArrayLock, LW_SHARED);
2898 
2899  for (index = 0; index < arrayP->numProcs; index++)
2900  {
2901  int pgprocno = arrayP->pgprocnos[index];
2902  volatile PGPROC *proc = &allProcs[pgprocno];
2903  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2904 
2905  if (proc->databaseId != databaseId)
2906  continue;
2907  if (proc == MyProc)
2908  continue;
2909 
2910  found = true;
2911 
2912  if (proc->pid == 0)
2913  (*nprepared)++;
2914  else
2915  {
2916  (*nbackends)++;
2917  if ((pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
2918  nautovacs < MAXAUTOVACPIDS)
2919  autovac_pids[nautovacs++] = proc->pid;
2920  }
2921  }
2922 
2923  LWLockRelease(ProcArrayLock);
2924 
2925  if (!found)
2926  return false; /* no conflicting backends, so done */
2927 
2928  /*
2929  * Send SIGTERM to any conflicting autovacuums before sleeping. We
2930  * postpone this step until after the loop because we don't want to
2931  * hold ProcArrayLock while issuing kill(). We have no idea what might
2932  * block kill() inside the kernel...
2933  */
2934  for (index = 0; index < nautovacs; index++)
2935  (void) kill(autovac_pids[index], SIGTERM); /* ignore any error */
2936 
2937  /* sleep, then try again */
2938  pg_usleep(100 * 1000L); /* 100ms */
2939  }
2940 
2941  return true; /* timed out, still conflicts */
2942 }
Definition: proc.h:207
PGPROC * MyProc
Definition: proc.c:67
#define MAXAUTOVACPIDS
uint8 vacuumFlags
Definition: proc.h:218
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void pg_usleep(long microsec)
Definition: signal.c:53
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
Definition: proc.h:94
int pid
Definition: proc.h:108
#define PROC_IS_AUTOVACUUM
Definition: proc.h:52
int CountUserBackends ( Oid  roleid)

Definition at line 2828 of file procarray.c.

References PGPROC::isBackgroundWorker, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, and PGPROC::roleId.

Referenced by InitializeSessionUserId().

2829 {
2830  ProcArrayStruct *arrayP = procArray;
2831  int count = 0;
2832  int index;
2833 
2834  LWLockAcquire(ProcArrayLock, LW_SHARED);
2835 
2836  for (index = 0; index < arrayP->numProcs; index++)
2837  {
2838  int pgprocno = arrayP->pgprocnos[index];
2839  volatile PGPROC *proc = &allProcs[pgprocno];
2840 
2841  if (proc->pid == 0)
2842  continue; /* do not count prepared xacts */
2843  if (proc->isBackgroundWorker)
2844  continue; /* do not count background workers */
2845  if (proc->roleId == roleid)
2846  count++;
2847  }
2848 
2849  LWLockRelease(ProcArrayLock);
2850 
2851  return count;
2852 }
Oid roleId
Definition: proc.h:114
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
bool isBackgroundWorker
Definition: proc.h:116
static PGPROC * allProcs
Definition: procarray.c:100
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Definition: proc.h:94
int pid
Definition: proc.h:108
void CreateSharedProcArray ( void  )

Definition at line 221 of file procarray.c.

References add_size(), PROC_HDR::allPgXact, PROC_HDR::allProcs, EnableHotStandby, ProcArrayStruct::headKnownAssignedXids, InvalidTransactionId, ProcArrayStruct::known_assigned_xids_lck, KnownAssignedXids, KnownAssignedXidsValid, ProcArrayStruct::lastOverflowedXid, LWLockRegisterTranche(), LWTRANCHE_PROC, ProcArrayStruct::maxKnownAssignedXids, ProcArrayStruct::maxProcs, mul_size(), ProcArrayStruct::numKnownAssignedXids, ProcArrayStruct::numProcs, offsetof, PROCARRAY_MAXPROCS, ProcGlobal, ProcArrayStruct::replication_slot_xmin, ShmemInitStruct(), SpinLockInit, ProcArrayStruct::tailKnownAssignedXids, and TOTAL_MAX_CACHED_SUBXIDS.

Referenced by CreateSharedMemoryAndSemaphores().

222 {
223  bool found;
224 
225  /* Create or attach to the ProcArray shared structure */
227  ShmemInitStruct("Proc Array",
228  add_size(offsetof(ProcArrayStruct, pgprocnos),
229  mul_size(sizeof(int),
231  &found);
232 
233  if (!found)
234  {
235  /*
236  * We're the first - initialize.
237  */
238  procArray->numProcs = 0;
247  }
248 
251 
252  /* Create or attach to the KnownAssignedXids arrays too, if needed */
253  if (EnableHotStandby)
254  {
256  ShmemInitStruct("KnownAssignedXids",
257  mul_size(sizeof(TransactionId),
259  &found);
260  KnownAssignedXidsValid = (bool *)
261  ShmemInitStruct("KnownAssignedXidsValid",
262  mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
263  &found);
264  }
265 
266  /* Register and initialize fields of ProcLWLockTranche */
268 }
#define PROCARRAY_MAXPROCS
uint32 TransactionId
Definition: c.h:397
PGXACT * allPgXact
Definition: proc.h:234
#define SpinLockInit(lock)
Definition: spin.h:60
slock_t known_assigned_xids_lck
Definition: procarray.c:78
PROC_HDR * ProcGlobal
Definition: proc.c:80
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
int maxKnownAssignedXids
Definition: procarray.c:74
#define InvalidTransactionId
Definition: transam.h:31
static PGPROC * allProcs
Definition: procarray.c:100
int numKnownAssignedXids
Definition: procarray.c:75
static bool * KnownAssignedXidsValid
Definition: procarray.c:107
TransactionId lastOverflowedXid
Definition: procarray.c:87
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId replication_slot_xmin
Definition: procarray.c:90
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
static PGXACT * allPgXact
Definition: procarray.c:101
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define TOTAL_MAX_CACHED_SUBXIDS
static TransactionId * KnownAssignedXids
Definition: procarray.c:106
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:592
bool EnableHotStandby
Definition: xlog.c:96
PGPROC * allProcs
Definition: proc.h:232
int tailKnownAssignedXids
Definition: procarray.c:76
#define offsetof(type, field)
Definition: c.h:555
int headKnownAssignedXids
Definition: procarray.c:77
void ExpireAllKnownAssignedTransactionIds ( void  )

Definition at line 3259 of file procarray.c.

References InvalidTransactionId, KnownAssignedXidsRemovePreceding(), LW_EXCLUSIVE, LWLockAcquire(), and LWLockRelease().

Referenced by ShutdownRecoveryTransactionEnvironment().

3260 {
3261  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3263  LWLockRelease(ProcArrayLock);
3264 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static void KnownAssignedXidsRemovePreceding(TransactionId xid)
Definition: procarray.c:3711
#define InvalidTransactionId
Definition: transam.h:31
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void ExpireOldKnownAssignedTransactionIds ( TransactionId  xid)

Definition at line 3271 of file procarray.c.

References KnownAssignedXidsRemovePreceding(), LW_EXCLUSIVE, LWLockAcquire(), and LWLockRelease().

Referenced by ProcArrayApplyRecoveryInfo().

3272 {
3273  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3275  LWLockRelease(ProcArrayLock);
3276 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static void KnownAssignedXidsRemovePreceding(TransactionId xid)
Definition: procarray.c:3711
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void ExpireTreeKnownAssignedTransactionIds ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
TransactionId  max_xid 
)

Definition at line 3234 of file procarray.c.

References Assert, KnownAssignedXidsRemoveTree(), VariableCacheData::latestCompletedXid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ShmemVariableCache, STANDBY_INITIALIZED, standbyState, and TransactionIdPrecedes().

Referenced by xact_redo_abort(), and xact_redo_commit().

3236 {
3238 
3239  /*
3240  * Uses same locking as transaction commit
3241  */
3242  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3243 
3244  KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
3245 
3246  /* As in ProcArrayEndTransaction, advance latestCompletedXid */
3248  max_xid))
3250 
3251  LWLockRelease(ProcArrayLock);
3252 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids, TransactionId *subxids)
Definition: procarray.c:3689
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
HotStandbyState standbyState
Definition: xlog.c:195
TransactionId latestCompletedXid
Definition: transam.h:135
VirtualTransactionId* GetConflictingVirtualXIDs ( TransactionId  limitXmin,
Oid  dbOid 
)

Definition at line 2550 of file procarray.c.

References VirtualTransactionId::backendId, PGPROC::databaseId, ereport, errcode(), errmsg(), ERROR, GET_VXID_FROM_PGPROC, InvalidBackendId, InvalidLocalTransactionId, VirtualTransactionId::localTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), malloc, ProcArrayStruct::maxProcs, NULL, ProcArrayStruct::numProcs, OidIsValid, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, TransactionIdFollows(), TransactionIdIsValid, VirtualTransactionIdIsValid, and PGXACT::xmin.

Referenced by ResolveRecoveryConflictWithSnapshot(), and ResolveRecoveryConflictWithTablespace().

2551 {
2552  static VirtualTransactionId *vxids;
2553  ProcArrayStruct *arrayP = procArray;
2554  int count = 0;
2555  int index;
2556 
2557  /*
2558  * If first time through, get workspace to remember main XIDs in. We
2559  * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
2560  * result space, remembering room for a terminator.
2561  */
2562  if (vxids == NULL)
2563  {
2564  vxids = (VirtualTransactionId *)
2565  malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
2566  if (vxids == NULL)
2567  ereport(ERROR,
2568  (errcode(ERRCODE_OUT_OF_MEMORY),
2569  errmsg("out of memory")));
2570  }
2571 
2572  LWLockAcquire(ProcArrayLock, LW_SHARED);
2573 
2574  for (index = 0; index < arrayP->numProcs; index++)
2575  {
2576  int pgprocno = arrayP->pgprocnos[index];
2577  volatile PGPROC *proc = &allProcs[pgprocno];
2578  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2579 
2580  /* Exclude prepared transactions */
2581  if (proc->pid == 0)
2582  continue;
2583 
2584  if (!OidIsValid(dbOid) ||
2585  proc->databaseId == dbOid)
2586  {
2587  /* Fetch xmin just once - can't change on us, but good coding */
2588  TransactionId pxmin = pgxact->xmin;
2589 
2590  /*
2591  * We ignore an invalid pxmin because this means that backend has
2592  * no snapshot currently. We hold a Share lock to avoid contention
2593  * with users taking snapshots. That is not a problem because the
2594  * current xmin is always at least one higher than the latest
2595  * removed xid, so any new snapshot would never conflict with the
2596  * test here.
2597  */
2598  if (!TransactionIdIsValid(limitXmin) ||
2599  (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
2600  {
2601  VirtualTransactionId vxid;
2602 
2603  GET_VXID_FROM_PGPROC(vxid, *proc);
2604  if (VirtualTransactionIdIsValid(vxid))
2605  vxids[count++] = vxid;
2606  }
2607  }
2608  }
2609 
2610  LWLockRelease(ProcArrayLock);
2611 
2612  /* add the terminator */
2613  vxids[count].backendId = InvalidBackendId;
2615 
2616  return vxids;
2617 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
int errcode(int sqlerrcode)
Definition: elog.c:575
LocalTransactionId localTransactionId
Definition: lock.h:66
#define OidIsValid(objectId)
Definition: c.h:538
Definition: type.h:90
#define malloc(a)
Definition: header.h:50
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define ERROR
Definition: elog.h:43
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
#define ereport(elevel, rest)
Definition: elog.h:122
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
#define InvalidBackendId
Definition: backendid.h:23
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define NULL
Definition: c.h:229
BackendId backendId
Definition: lock.h:65
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define InvalidLocalTransactionId
Definition: lock.h:70
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Definition: proc.h:94
int pid
Definition: proc.h:108
VirtualTransactionId* GetCurrentVirtualXIDs ( TransactionId  limitXmin,
bool  excludeXmin0,
bool  allDbs,
int  excludeVacuum,
int *  nvxids 
)

Definition at line 2462 of file procarray.c.

References PGPROC::databaseId, GET_VXID_FROM_PGPROC, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::maxProcs, MyDatabaseId, MyProc, ProcArrayStruct::numProcs, palloc(), ProcArrayStruct::pgprocnos, procArray, TransactionIdIsValid, TransactionIdPrecedesOrEquals(), PGXACT::vacuumFlags, VirtualTransactionIdIsValid, and PGXACT::xmin.

Referenced by DefineIndex().

2465 {
2466  VirtualTransactionId *vxids;
2467  ProcArrayStruct *arrayP = procArray;
2468  int count = 0;
2469  int index;
2470 
2471  /* allocate what's certainly enough result space */
2472  vxids = (VirtualTransactionId *)
2473  palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
2474 
2475  LWLockAcquire(ProcArrayLock, LW_SHARED);
2476 
2477  for (index = 0; index < arrayP->numProcs; index++)
2478  {
2479  int pgprocno = arrayP->pgprocnos[index];
2480  volatile PGPROC *proc = &allProcs[pgprocno];
2481  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2482 
2483  if (proc == MyProc)
2484  continue;
2485 
2486  if (excludeVacuum & pgxact->vacuumFlags)
2487  continue;
2488 
2489  if (allDbs || proc->databaseId == MyDatabaseId)
2490  {
2491  /* Fetch xmin just once - might change on us */
2492  TransactionId pxmin = pgxact->xmin;
2493 
2494  if (excludeXmin0 && !TransactionIdIsValid(pxmin))
2495  continue;
2496 
2497  /*
2498  * InvalidTransactionId precedes all other XIDs, so a proc that
2499  * hasn't set xmin yet will not be rejected by this test.
2500  */
2501  if (!TransactionIdIsValid(limitXmin) ||
2502  TransactionIdPrecedesOrEquals(pxmin, limitXmin))
2503  {
2504  VirtualTransactionId vxid;
2505 
2506  GET_VXID_FROM_PGPROC(vxid, *proc);
2507  if (VirtualTransactionIdIsValid(vxid))
2508  vxids[count++] = vxid;
2509  }
2510  }
2511  }
2512 
2513  LWLockRelease(ProcArrayLock);
2514 
2515  *nvxids = count;
2516  return vxids;
2517 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
PGPROC * MyProc
Definition: proc.c:67
uint8 vacuumFlags
Definition: proc.h:218
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
static PGXACT * allPgXact
Definition: procarray.c:101
Oid MyDatabaseId
Definition: globals.c:76
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void * palloc(Size size)
Definition: mcxt.c:849
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Definition: proc.h:94
int GetMaxSnapshotSubxidCount ( void  )

Definition at line 1466 of file procarray.c.

References TOTAL_MAX_CACHED_SUBXIDS.

Referenced by ExportSnapshot(), GetSnapshotData(), ImportSnapshot(), and SetTransactionSnapshot().

1467 {
1468  return TOTAL_MAX_CACHED_SUBXIDS;
1469 }
#define TOTAL_MAX_CACHED_SUBXIDS
int GetMaxSnapshotXidCount ( void  )

Definition at line 1455 of file procarray.c.

References ProcArrayStruct::maxProcs.

Referenced by GetSnapshotData(), ImportSnapshot(), SetTransactionSnapshot(), and SnapBuildInitialSnapshot().

1456 {
1457  return procArray->maxProcs;
1458 }
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId GetOldestActiveTransactionId ( void  )

Definition at line 2088 of file procarray.c.

References Assert, LW_SHARED, LWLockAcquire(), LWLockRelease(), VariableCacheData::nextXid, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, procArray, RecoveryInProgress(), ShmemVariableCache, TransactionIdIsNormal, TransactionIdPrecedes(), and PGXACT::xid.

Referenced by CreateCheckPoint().

2089 {
2090  ProcArrayStruct *arrayP = procArray;
2091  TransactionId oldestRunningXid;
2092  int index;
2093 
2095 
2096  LWLockAcquire(ProcArrayLock, LW_SHARED);
2097 
2098  /*
2099  * It's okay to read nextXid without acquiring XidGenLock because (1) we
2100  * assume TransactionIds can be read atomically and (2) we don't care if
2101  * we get a slightly stale value. It can't be very stale anyway, because
2102  * the LWLockAcquire above will have done any necessary memory
2103  * interlocking.
2104  */
2105  oldestRunningXid = ShmemVariableCache->nextXid;
2106 
2107  /*
2108  * Spin over procArray collecting all xids and subxids.
2109  */
2110  for (index = 0; index < arrayP->numProcs; index++)
2111  {
2112  int pgprocno = arrayP->pgprocnos[index];
2113  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2114  TransactionId xid;
2115 
2116  /* Fetch xid just once - see GetNewTransactionId */
2117  xid = pgxact->xid;
2118 
2119  if (!TransactionIdIsNormal(xid))
2120  continue;
2121 
2122  if (TransactionIdPrecedes(xid, oldestRunningXid))
2123  oldestRunningXid = xid;
2124 
2125  /*
2126  * Top-level XID of a transaction is always less than any of its
2127  * subxids, so we don't need to check if any of the subxids are
2128  * smaller than oldestRunningXid
2129  */
2130  }
2131 
2132  LWLockRelease(ProcArrayLock);
2133 
2134  return oldestRunningXid;
2135 }
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xid
Definition: proc.h:209
bool RecoveryInProgress(void)
Definition: xlog.c:7874
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TransactionId GetOldestSafeDecodingTransactionId ( void  )

Definition at line 2154 of file procarray.c.

References Assert, LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), VariableCacheData::nextXid, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, procArray, RecoveryInProgress(), ProcArrayStruct::replication_slot_catalog_xmin, ShmemVariableCache, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), and PGXACT::xid.

Referenced by CreateInitDecodingContext().

2155 {
2156  ProcArrayStruct *arrayP = procArray;
2157  TransactionId oldestSafeXid;
2158  int index;
2159  bool recovery_in_progress = RecoveryInProgress();
2160 
2161  Assert(LWLockHeldByMe(ProcArrayLock));
2162 
2163  /*
2164  * Acquire XidGenLock, so no transactions can acquire an xid while we're
2165  * running. If no transaction with xid were running concurrently a new xid
2166  * could influence the RecentXmin et al.
2167  *
2168  * We initialize the computation to nextXid since that's guaranteed to be
2169  * a safe, albeit pessimal, value.
2170  */
2171  LWLockAcquire(XidGenLock, LW_SHARED);
2172  oldestSafeXid = ShmemVariableCache->nextXid;
2173 
2174  /*
2175  * If there's already a slot pegging the xmin horizon, we can start with
2176  * that value, it's guaranteed to be safe since it's computed by this
2177  * routine initially and has been enforced since.
2178  */
2181  oldestSafeXid))
2182  oldestSafeXid = procArray->replication_slot_catalog_xmin;
2183 
2184  /*
2185  * If we're not in recovery, we walk over the procarray and collect the
2186  * lowest xid. Since we're called with ProcArrayLock held and have
2187  * acquired XidGenLock, no entries can vanish concurrently, since
2188  * PGXACT->xid is only set with XidGenLock held and only cleared with
2189  * ProcArrayLock held.
2190  *
2191  * In recovery we can't lower the safe value besides what we've computed
2192  * above, so we'll have to wait a bit longer there. We unfortunately can
2193  * *not* use KnownAssignedXidsGetOldestXmin() since the KnownAssignedXids
2194  * machinery can miss values and return an older value than is safe.
2195  */
2196  if (!recovery_in_progress)
2197  {
2198  /*
2199  * Spin over procArray collecting all min(PGXACT->xid)
2200  */
2201  for (index = 0; index < arrayP->numProcs; index++)
2202  {
2203  int pgprocno = arrayP->pgprocnos[index];
2204  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2205  TransactionId xid;
2206 
2207  /* Fetch xid just once - see GetNewTransactionId */
2208  xid = pgxact->xid;
2209 
2210  if (!TransactionIdIsNormal(xid))
2211  continue;
2212 
2213  if (TransactionIdPrecedes(xid, oldestSafeXid))
2214  oldestSafeXid = xid;
2215  }
2216  }
2217 
2218  LWLockRelease(XidGenLock);
2219 
2220  return oldestSafeXid;
2221 }
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
TransactionId xid
Definition: proc.h:209
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:92
bool RecoveryInProgress(void)
Definition: xlog.c:7874
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TransactionId GetOldestXmin ( Relation  rel,
int  flags 
)

Definition at line 1314 of file procarray.c.

References Assert, PGPROC::databaseId, FirstNormalTransactionId, InvalidTransactionId, KnownAssignedXidsGetOldestXmin(), VariableCacheData::latestCompletedXid, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyDatabaseId, NormalTransactionIdPrecedes, NULL, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, procArray, PROCARRAY_PROC_FLAGS_MASK, PROCARRAY_SLOTS_XMIN, RelationData::rd_rel, RecoveryInProgress(), RelationIsAccessibleInLogicalDecoding, ProcArrayStruct::replication_slot_catalog_xmin, ProcArrayStruct::replication_slot_xmin, result, ShmemVariableCache, TransactionIdAdvance, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), vacuum_defer_cleanup_age, PGXACT::vacuumFlags, PGXACT::xid, and PGXACT::xmin.

Referenced by acquire_sample_rows(), collect_corrupt_items(), CreateCheckPoint(), CreateRestartPoint(), IndexBuildHeapRangeScan(), statapprox_heap(), vac_update_datfrozenxid(), vacuum_set_xid_limits(), and XLogWalRcvSendHSFeedback().

1315 {
1316  ProcArrayStruct *arrayP = procArray;
1318  int index;
1319  bool allDbs;
1320 
1321  volatile TransactionId replication_slot_xmin = InvalidTransactionId;
1322  volatile TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
1323 
1324  /*
1325  * If we're not computing a relation specific limit, or if a shared
1326  * relation has been passed in, backends in all databases have to be
1327  * considered.
1328  */
1329  allDbs = rel == NULL || rel->rd_rel->relisshared;
1330 
1331  /* Cannot look for individual databases during recovery */
1332  Assert(allDbs || !RecoveryInProgress());
1333 
1334  LWLockAcquire(ProcArrayLock, LW_SHARED);
1335 
1336  /*
1337  * We initialize the MIN() calculation with latestCompletedXid + 1. This
1338  * is a lower bound for the XIDs that might appear in the ProcArray later,
1339  * and so protects us against overestimating the result due to future
1340  * additions.
1341  */
1343  Assert(TransactionIdIsNormal(result));
1344  TransactionIdAdvance(result);
1345 
1346  for (index = 0; index < arrayP->numProcs; index++)
1347  {
1348  int pgprocno = arrayP->pgprocnos[index];
1349  volatile PGPROC *proc = &allProcs[pgprocno];
1350  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1351 
1352  if (pgxact->vacuumFlags & (flags & PROCARRAY_PROC_FLAGS_MASK))
1353  continue;
1354 
1355  if (allDbs ||
1356  proc->databaseId == MyDatabaseId ||
1357  proc->databaseId == 0) /* always include WalSender */
1358  {
1359  /* Fetch xid just once - see GetNewTransactionId */
1360  TransactionId xid = pgxact->xid;
1361 
1362  /* First consider the transaction's own Xid, if any */
1363  if (TransactionIdIsNormal(xid) &&
1364  TransactionIdPrecedes(xid, result))
1365  result = xid;
1366 
1367  /*
1368  * Also consider the transaction's Xmin, if set.
1369  *
1370  * We must check both Xid and Xmin because a transaction might
1371  * have an Xmin but not (yet) an Xid; conversely, if it has an
1372  * Xid, that could determine some not-yet-set Xmin.
1373  */
1374  xid = pgxact->xmin; /* Fetch just once */
1375  if (TransactionIdIsNormal(xid) &&
1376  TransactionIdPrecedes(xid, result))
1377  result = xid;
1378  }
1379  }
1380 
1381  /* fetch into volatile var while ProcArrayLock is held */
1382  replication_slot_xmin = procArray->replication_slot_xmin;
1383  replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
1384 
1385  if (RecoveryInProgress())
1386  {
1387  /*
1388  * Check to see whether KnownAssignedXids contains an xid value older
1389  * than the main procarray.
1390  */
1392 
1393  LWLockRelease(ProcArrayLock);
1394 
1395  if (TransactionIdIsNormal(kaxmin) &&
1396  TransactionIdPrecedes(kaxmin, result))
1397  result = kaxmin;
1398  }
1399  else
1400  {
1401  /*
1402  * No other information needed, so release the lock immediately.
1403  */
1404  LWLockRelease(ProcArrayLock);
1405 
1406  /*
1407  * Compute the cutoff XID by subtracting vacuum_defer_cleanup_age,
1408  * being careful not to generate a "permanent" XID.
1409  *
1410  * vacuum_defer_cleanup_age provides some additional "slop" for the
1411  * benefit of hot standby queries on slave servers. This is quick and
1412  * dirty, and perhaps not all that useful unless the master has a
1413  * predictable transaction rate, but it offers some protection when
1414  * there's no walsender connection. Note that we are assuming
1415  * vacuum_defer_cleanup_age isn't large enough to cause wraparound ---
1416  * so guc.c should limit it to no more than the xidStopLimit threshold
1417  * in varsup.c. Also note that we intentionally don't apply
1418  * vacuum_defer_cleanup_age on standby servers.
1419  */
1420  result -= vacuum_defer_cleanup_age;
1421  if (!TransactionIdIsNormal(result))
1422  result = FirstNormalTransactionId;
1423  }
1424 
1425  /*
1426  * Check whether there are replication slots requiring an older xmin.
1427  */
1428  if (!(flags & PROCARRAY_SLOTS_XMIN) &&
1429  TransactionIdIsValid(replication_slot_xmin) &&
1430  NormalTransactionIdPrecedes(replication_slot_xmin, result))
1431  result = replication_slot_xmin;
1432 
1433  /*
1434  * After locks have been released and defer_cleanup_age has been applied,
1435  * check whether we need to back up further to make logical decoding
1436  * possible. We need to do so if we're computing the global limit (rel =
1437  * NULL) or if the passed relation is a catalog relation of some kind.
1438  */
1439  if (!(flags & PROCARRAY_SLOTS_XMIN) &&
1440  (rel == NULL ||
1442  TransactionIdIsValid(replication_slot_catalog_xmin) &&
1443  NormalTransactionIdPrecedes(replication_slot_catalog_xmin, result))
1444  result = replication_slot_catalog_xmin;
1445 
1446  return result;
1447 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
TransactionId xid
Definition: proc.h:209
int vacuum_defer_cleanup_age
Definition: standby.c:37
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:92
return result
Definition: formatting.c:1618
Form_pg_class rd_rel
Definition: rel.h:114
bool RecoveryInProgress(void)
Definition: xlog.c:7874
uint8 vacuumFlags
Definition: proc.h:218
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define FirstNormalTransactionId
Definition: transam.h:34
#define PROCARRAY_SLOTS_XMIN
Definition: procarray.h:35
#define PROCARRAY_PROC_FLAGS_MASK
Definition: procarray.h:42
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId replication_slot_xmin
Definition: procarray.c:90
static PGXACT * allPgXact
Definition: procarray.c:101
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:560
Oid MyDatabaseId
Definition: globals.c:76
static TransactionId KnownAssignedXidsGetOldestXmin(void)
Definition: procarray.c:3862
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define NormalTransactionIdPrecedes(id1, id2)
Definition: transam.h:62
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Definition: proc.h:94
TransactionId latestCompletedXid
Definition: transam.h:135
RunningTransactions GetRunningTransactionData ( void  )

Definition at line 1930 of file procarray.c.

References Assert, ereport, errcode(), errmsg(), ERROR, RunningTransactionsData::latestCompletedXid, VariableCacheData::latestCompletedXid, LW_SHARED, LWLockAcquire(), malloc, RunningTransactionsData::nextXid, VariableCacheData::nextXid, NULL, ProcArrayStruct::numProcs, PGXACT::nxids, RunningTransactionsData::oldestRunningXid, PGXACT::overflowed, ProcArrayStruct::pgprocnos, procArray, RecoveryInProgress(), ShmemVariableCache, RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_overflow, PGPROC::subxids, TOTAL_MAX_CACHED_SUBXIDS, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), RunningTransactionsData::xcnt, PGXACT::xid, XidCache::xids, and RunningTransactionsData::xids.

Referenced by LogStandbySnapshot().

1931 {
1932  /* result workspace */
1933  static RunningTransactionsData CurrentRunningXactsData;
1934 
1935  ProcArrayStruct *arrayP = procArray;
1936  RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
1937  TransactionId latestCompletedXid;
1938  TransactionId oldestRunningXid;
1939  TransactionId *xids;
1940  int index;
1941  int count;
1942  int subcount;
1943  bool suboverflowed;
1944 
1946 
1947  /*
1948  * Allocating space for maxProcs xids is usually overkill; numProcs would
1949  * be sufficient. But it seems better to do the malloc while not holding
1950  * the lock, so we can't look at numProcs. Likewise, we allocate much
1951  * more subxip storage than is probably needed.
1952  *
1953  * Should only be allocated in bgwriter, since only ever executed during
1954  * checkpoints.
1955  */
1956  if (CurrentRunningXacts->xids == NULL)
1957  {
1958  /*
1959  * First call
1960  */
1961  CurrentRunningXacts->xids = (TransactionId *)
1963  if (CurrentRunningXacts->xids == NULL)
1964  ereport(ERROR,
1965  (errcode(ERRCODE_OUT_OF_MEMORY),
1966  errmsg("out of memory")));
1967  }
1968 
1969  xids = CurrentRunningXacts->xids;
1970 
1971  count = subcount = 0;
1972  suboverflowed = false;
1973 
1974  /*
1975  * Ensure that no xids enter or leave the procarray while we obtain
1976  * snapshot.
1977  */
1978  LWLockAcquire(ProcArrayLock, LW_SHARED);
1979  LWLockAcquire(XidGenLock, LW_SHARED);
1980 
1981  latestCompletedXid = ShmemVariableCache->latestCompletedXid;
1982 
1983  oldestRunningXid = ShmemVariableCache->nextXid;
1984 
1985  /*
1986  * Spin over procArray collecting all xids
1987  */
1988  for (index = 0; index < arrayP->numProcs; index++)
1989  {
1990  int pgprocno = arrayP->pgprocnos[index];
1991  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1992  TransactionId xid;
1993 
1994  /* Fetch xid just once - see GetNewTransactionId */
1995  xid = pgxact->xid;
1996 
1997  /*
1998  * We don't need to store transactions that don't have a TransactionId
1999  * yet because they will not show as running on a standby server.
2000  */
2001  if (!TransactionIdIsValid(xid))
2002  continue;
2003 
2004  xids[count++] = xid;
2005 
2006  if (TransactionIdPrecedes(xid, oldestRunningXid))
2007  oldestRunningXid = xid;
2008 
2009  if (pgxact->overflowed)
2010  suboverflowed = true;
2011  }
2012 
2013  /*
2014  * Spin over procArray collecting all subxids, but only if there hasn't
2015  * been a suboverflow.
2016  */
2017  if (!suboverflowed)
2018  {
2019  for (index = 0; index < arrayP->numProcs; index++)
2020  {
2021  int pgprocno = arrayP->pgprocnos[index];
2022  volatile PGPROC *proc = &allProcs[pgprocno];
2023  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2024  int nxids;
2025 
2026  /*
2027  * Save subtransaction XIDs. Other backends can't add or remove
2028  * entries while we're holding XidGenLock.
2029  */
2030  nxids = pgxact->nxids;
2031  if (nxids > 0)
2032  {
2033  memcpy(&xids[count], (void *) proc->subxids.xids,
2034  nxids * sizeof(TransactionId));
2035  count += nxids;
2036  subcount += nxids;
2037 
2038  /*
2039  * Top-level XID of a transaction is always less than any of
2040  * its subxids, so we don't need to check if any of the
2041  * subxids are smaller than oldestRunningXid
2042  */
2043  }
2044  }
2045  }
2046 
2047  /*
2048  * It's important *not* to include the limits set by slots here because
2049  * snapbuild.c uses oldestRunningXid to manage its xmin horizon. If those
2050  * were to be included here the initial value could never increase because
2051  * of a circular dependency where slots only increase their limits when
2052  * running xacts increases oldestRunningXid and running xacts only
2053  * increases if slots do.
2054  */
2055 
2056  CurrentRunningXacts->xcnt = count - subcount;
2057  CurrentRunningXacts->subxcnt = subcount;
2058  CurrentRunningXacts->subxid_overflow = suboverflowed;
2059  CurrentRunningXacts->nextXid = ShmemVariableCache->nextXid;
2060  CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
2061  CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
2062 
2063  Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
2064  Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
2065  Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
2066 
2067  /* We don't release the locks here, the caller is responsible for that */
2068 
2069  return CurrentRunningXacts;
2070 }
TransactionId oldestRunningXid
Definition: standby.h:76
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xid
Definition: proc.h:209
int errcode(int sqlerrcode)
Definition: elog.c:575
TransactionId * xids
Definition: standby.h:79
bool RecoveryInProgress(void)
Definition: xlog.c:7874
TransactionId latestCompletedXid
Definition: standby.h:77
Definition: type.h:90
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
TransactionId nextXid
Definition: transam.h:117
uint8 nxids
Definition: proc.h:223
VariableCache ShmemVariableCache
Definition: varsup.c:34
static PGPROC * allProcs
Definition: procarray.c:100
struct XidCache subxids
Definition: proc.h:158
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
bool overflowed
Definition: proc.h:219
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:39
#define TOTAL_MAX_CACHED_SUBXIDS
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
TransactionId nextXid
Definition: standby.h:75
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Definition: proc.h:94
TransactionId latestCompletedXid
Definition: transam.h:135
Snapshot GetSnapshotData ( Snapshot  snapshot)

Definition at line 1508 of file procarray.c.

References SnapshotData::active_count, Assert, SnapshotData::copied, SnapshotData::curcid, ereport, errcode(), errmsg(), ERROR, FirstNormalTransactionId, GetCurrentCommandId(), GetMaxSnapshotSubxidCount(), GetMaxSnapshotXidCount(), GetSnapshotCurrentTimestamp(), GetXLogInsertRecPtr(), InvalidTransactionId, InvalidXLogRecPtr, KnownAssignedXidsGetAndSetXmin(), ProcArrayStruct::lastOverflowedXid, VariableCacheData::latestCompletedXid, SnapshotData::lsn, LW_SHARED, LWLockAcquire(), LWLockRelease(), MaintainOldSnapshotTimeMapping(), malloc, MyPgXact, NormalTransactionIdPrecedes, NULL, ProcArrayStruct::numProcs, PGXACT::nxids, old_snapshot_threshold, PGXACT::overflowed, ProcArrayStruct::pgprocnos, PROC_IN_LOGICAL_DECODING, PROC_IN_VACUUM, procArray, RecentGlobalDataXmin, RecentGlobalXmin, RecentXmin, RecoveryInProgress(), SnapshotData::regd_count, ProcArrayStruct::replication_slot_catalog_xmin, ProcArrayStruct::replication_slot_xmin, ShmemVariableCache, SnapshotData::suboverflowed, SnapshotData::subxcnt, PGPROC::subxids, SnapshotData::subxip, SnapshotData::takenDuringRecovery, TransactionIdAdvance, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), TransactionXmin, vacuum_defer_cleanup_age, PGXACT::vacuumFlags, SnapshotData::whenTaken, SnapshotData::xcnt, PGXACT::xid, XidCache::xids, SnapshotData::xip, SnapshotData::xmax, SnapshotData::xmin, and PGXACT::xmin.

Referenced by GetLatestSnapshot(), GetNonHistoricCatalogSnapshot(), GetSerializableTransactionSnapshotInt(), GetTransactionSnapshot(), and SetTransactionSnapshot().

1509 {
1510  ProcArrayStruct *arrayP = procArray;
1511  TransactionId xmin;
1512  TransactionId xmax;
1513  TransactionId globalxmin;
1514  int index;
1515  int count = 0;
1516  int subcount = 0;
1517  bool suboverflowed = false;
1518  volatile TransactionId replication_slot_xmin = InvalidTransactionId;
1519  volatile TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
1520 
1521  Assert(snapshot != NULL);
1522 
1523  /*
1524  * Allocating space for maxProcs xids is usually overkill; numProcs would
1525  * be sufficient. But it seems better to do the malloc while not holding
1526  * the lock, so we can't look at numProcs. Likewise, we allocate much
1527  * more subxip storage than is probably needed.
1528  *
1529  * This does open a possibility for avoiding repeated malloc/free: since
1530  * maxProcs does not change at runtime, we can simply reuse the previous
1531  * xip arrays if any. (This relies on the fact that all callers pass
1532  * static SnapshotData structs.)
1533  */
1534  if (snapshot->xip == NULL)
1535  {
1536  /*
1537  * First call for this snapshot. Snapshot is same size whether or not
1538  * we are in recovery, see later comments.
1539  */
1540  snapshot->xip = (TransactionId *)
1542  if (snapshot->xip == NULL)
1543  ereport(ERROR,
1544  (errcode(ERRCODE_OUT_OF_MEMORY),
1545  errmsg("out of memory")));
1546  Assert(snapshot->subxip == NULL);
1547  snapshot->subxip = (TransactionId *)
1549  if (snapshot->subxip == NULL)
1550  ereport(ERROR,
1551  (errcode(ERRCODE_OUT_OF_MEMORY),
1552  errmsg("out of memory")));
1553  }
1554 
1555  /*
1556  * It is sufficient to get shared lock on ProcArrayLock, even if we are
1557  * going to set MyPgXact->xmin.
1558  */
1559  LWLockAcquire(ProcArrayLock, LW_SHARED);
1560 
1561  /* xmax is always latestCompletedXid + 1 */
1564  TransactionIdAdvance(xmax);
1565 
1566  /* initialize xmin calculation with xmax */
1567  globalxmin = xmin = xmax;
1568 
1570 
1571  if (!snapshot->takenDuringRecovery)
1572  {
1573  int *pgprocnos = arrayP->pgprocnos;
1574  int numProcs;
1575 
1576  /*
1577  * Spin over procArray checking xid, xmin, and subxids. The goal is
1578  * to gather all active xids, find the lowest xmin, and try to record
1579  * subxids.
1580  */
1581  numProcs = arrayP->numProcs;
1582  for (index = 0; index < numProcs; index++)
1583  {
1584  int pgprocno = pgprocnos[index];
1585  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1586  TransactionId xid;
1587 
1588  /*
1589  * Backend is doing logical decoding which manages xmin
1590  * separately, check below.
1591  */
1592  if (pgxact->vacuumFlags & PROC_IN_LOGICAL_DECODING)
1593  continue;
1594 
1595  /* Ignore procs running LAZY VACUUM */
1596  if (pgxact->vacuumFlags & PROC_IN_VACUUM)
1597  continue;
1598 
1599  /* Update globalxmin to be the smallest valid xmin */
1600  xid = pgxact->xmin; /* fetch just once */
1601  if (TransactionIdIsNormal(xid) &&
1602  NormalTransactionIdPrecedes(xid, globalxmin))
1603  globalxmin = xid;
1604 
1605  /* Fetch xid just once - see GetNewTransactionId */
1606  xid = pgxact->xid;
1607 
1608  /*
1609  * If the transaction has no XID assigned, we can skip it; it
1610  * won't have sub-XIDs either. If the XID is >= xmax, we can also
1611  * skip it; such transactions will be treated as running anyway
1612  * (and any sub-XIDs will also be >= xmax).
1613  */
1614  if (!TransactionIdIsNormal(xid)
1615  || !NormalTransactionIdPrecedes(xid, xmax))
1616  continue;
1617 
1618  /*
1619  * We don't include our own XIDs (if any) in the snapshot, but we
1620  * must include them in xmin.
1621  */
1622  if (NormalTransactionIdPrecedes(xid, xmin))
1623  xmin = xid;
1624  if (pgxact == MyPgXact)
1625  continue;
1626 
1627  /* Add XID to snapshot. */
1628  snapshot->xip[count++] = xid;
1629 
1630  /*
1631  * Save subtransaction XIDs if possible (if we've already
1632  * overflowed, there's no point). Note that the subxact XIDs must
1633  * be later than their parent, so no need to check them against
1634  * xmin. We could filter against xmax, but it seems better not to
1635  * do that much work while holding the ProcArrayLock.
1636  *
1637  * The other backend can add more subxids concurrently, but cannot
1638  * remove any. Hence it's important to fetch nxids just once.
1639  * Should be safe to use memcpy, though. (We needn't worry about
1640  * missing any xids added concurrently, because they must postdate
1641  * xmax.)
1642  *
1643  * Again, our own XIDs are not included in the snapshot.
1644  */
1645  if (!suboverflowed)
1646  {
1647  if (pgxact->overflowed)
1648  suboverflowed = true;
1649  else
1650  {
1651  int nxids = pgxact->nxids;
1652 
1653  if (nxids > 0)
1654  {
1655  volatile PGPROC *proc = &allProcs[pgprocno];
1656 
1657  memcpy(snapshot->subxip + subcount,
1658  (void *) proc->subxids.xids,
1659  nxids * sizeof(TransactionId));
1660  subcount += nxids;
1661  }
1662  }
1663  }
1664  }
1665  }
1666  else
1667  {
1668  /*
1669  * We're in hot standby, so get XIDs from KnownAssignedXids.
1670  *
1671  * We store all xids directly into subxip[]. Here's why:
1672  *
1673  * In recovery we don't know which xids are top-level and which are
1674  * subxacts, a design choice that greatly simplifies xid processing.
1675  *
1676  * It seems like we would want to try to put xids into xip[] only, but
1677  * that is fairly small. We would either need to make that bigger or
1678  * to increase the rate at which we WAL-log xid assignment; neither is
1679  * an appealing choice.
1680  *
1681  * We could try to store xids into xip[] first and then into subxip[]
1682  * if there are too many xids. That only works if the snapshot doesn't
1683  * overflow because we do not search subxip[] in that case. A simpler
1684  * way is to just store all xids in the subxact array because this is
1685  * by far the bigger array. We just leave the xip array empty.
1686  *
1687  * Either way we need to change the way XidInMVCCSnapshot() works
1688  * depending upon when the snapshot was taken, or change normal
1689  * snapshot processing so it matches.
1690  *
1691  * Note: It is possible for recovery to end before we finish taking
1692  * the snapshot, and for newly assigned transaction ids to be added to
1693  * the ProcArray. xmax cannot change while we hold ProcArrayLock, so
1694  * those newly added transaction ids would be filtered away, so we
1695  * need not be concerned about them.
1696  */
1697  subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
1698  xmax);
1699 
1701  suboverflowed = true;
1702  }
1703 
1704 
1705  /* fetch into volatile var while ProcArrayLock is held */
1706  replication_slot_xmin = procArray->replication_slot_xmin;
1707  replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
1708 
1710  MyPgXact->xmin = TransactionXmin = xmin;
1711 
1712  LWLockRelease(ProcArrayLock);
1713 
1714  /*
1715  * Update globalxmin to include actual process xids. This is a slightly
1716  * different way of computing it than GetOldestXmin uses, but should give
1717  * the same result.
1718  */
1719  if (TransactionIdPrecedes(xmin, globalxmin))
1720  globalxmin = xmin;
1721 
1722  /* Update global variables too */
1726 
1727  /* Check whether there's a replication slot requiring an older xmin. */
1728  if (TransactionIdIsValid(replication_slot_xmin) &&
1729  NormalTransactionIdPrecedes(replication_slot_xmin, RecentGlobalXmin))
1730  RecentGlobalXmin = replication_slot_xmin;
1731 
1732  /* Non-catalog tables can be vacuumed if older than this xid */
1734 
1735  /*
1736  * Check whether there's a replication slot requiring an older catalog
1737  * xmin.
1738  */
1739  if (TransactionIdIsNormal(replication_slot_catalog_xmin) &&
1740  NormalTransactionIdPrecedes(replication_slot_catalog_xmin, RecentGlobalXmin))
1741  RecentGlobalXmin = replication_slot_catalog_xmin;
1742 
1743  RecentXmin = xmin;
1744 
1745  snapshot->xmin = xmin;
1746  snapshot->xmax = xmax;
1747  snapshot->xcnt = count;
1748  snapshot->subxcnt = subcount;
1749  snapshot->suboverflowed = suboverflowed;
1750 
1751  snapshot->curcid = GetCurrentCommandId(false);
1752 
1753  /*
1754  * This is a new snapshot, so set both refcounts are zero, and mark it as
1755  * not copied in persistent memory.
1756  */
1757  snapshot->active_count = 0;
1758  snapshot->regd_count = 0;
1759  snapshot->copied = false;
1760 
1761  if (old_snapshot_threshold < 0)
1762  {
1763  /*
1764  * If not using "snapshot too old" feature, fill related fields with
1765  * dummy values that don't require any locking.
1766  */
1767  snapshot->lsn = InvalidXLogRecPtr;
1768  snapshot->whenTaken = 0;
1769  }
1770  else
1771  {
1772  /*
1773  * Capture the current time and WAL stream location in case this
1774  * snapshot becomes old enough to need to fall back on the special
1775  * "old snapshot" logic.
1776  */
1777  snapshot->lsn = GetXLogInsertRecPtr();
1778  snapshot->whenTaken = GetSnapshotCurrentTimestamp();
1779  MaintainOldSnapshotTimeMapping(snapshot->whenTaken, xmin);
1780  }
1781 
1782  return snapshot;
1783 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:56
uint32 TransactionId
Definition: c.h:397
bool copied
Definition: snapshot.h:94
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
TransactionId xid
Definition: proc.h:209
int vacuum_defer_cleanup_age
Definition: standby.c:37
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:92
XLogRecPtr lsn
Definition: snapshot.h:112
int errcode(int sqlerrcode)
Definition: elog.c:575
TransactionId RecentXmin
Definition: snapmgr.c:165
bool suboverflowed
Definition: snapshot.h:91
bool RecoveryInProgress(void)
Definition: xlog.c:7874
uint32 regd_count
Definition: snapshot.h:108
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:218
TransactionId TransactionXmin
Definition: snapmgr.c:164
Definition: type.h:90
#define malloc(a)
Definition: header.h:50
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define PROC_IN_VACUUM
Definition: proc.h:53
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
#define FirstNormalTransactionId
Definition: transam.h:34
#define ERROR
Definition: elog.h:43
TimestampTz GetSnapshotCurrentTimestamp(void)
Definition: snapmgr.c:1643
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11096
TransactionId RecentGlobalXmin
Definition: snapmgr.c:166
uint8 nxids
Definition: proc.h:223
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
static PGPROC * allProcs
Definition: procarray.c:100
TransactionId xmax
Definition: snapshot.h:67
TransactionId xmin
Definition: snapshot.h:66
TransactionId RecentGlobalDataXmin
Definition: snapmgr.c:167
struct XidCache subxids
Definition: proc.h:158
TransactionId lastOverflowedXid
Definition: procarray.c:87
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TransactionId * xip
Definition: snapshot.h:77
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId replication_slot_xmin
Definition: procarray.c:90
static PGXACT * allPgXact
Definition: procarray.c:101
bool overflowed
Definition: proc.h:219
CommandId curcid
Definition: snapshot.h:96
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:1455
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:39
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool takenDuringRecovery
Definition: snapshot.h:93
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin, TransactionId xmax)
Definition: procarray.c:3804
#define NormalTransactionIdPrecedes(id1, id2)
Definition: transam.h:62
uint32 xcnt
Definition: snapshot.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:797
int old_snapshot_threshold
Definition: snapmgr.c:74
int GetMaxSnapshotSubxidCount(void)
Definition: procarray.c:1466
TimestampTz whenTaken
Definition: snapshot.h:111
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:687
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
Definition: snapmgr.c:1805
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Definition: proc.h:94
TransactionId * subxip
Definition: snapshot.h:89
uint32 active_count
Definition: snapshot.h:107
int32 subxcnt
Definition: snapshot.h:90
TransactionId latestCompletedXid
Definition: transam.h:135
VirtualTransactionId* GetVirtualXIDsDelayingChkpt ( int *  nvxids)

Definition at line 2242 of file procarray.c.

References PGXACT::delayChkpt, GET_VXID_FROM_PGPROC, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::maxProcs, ProcArrayStruct::numProcs, palloc(), ProcArrayStruct::pgprocnos, procArray, and VirtualTransactionIdIsValid.

Referenced by CreateCheckPoint().

2243 {
2244  VirtualTransactionId *vxids;
2245  ProcArrayStruct *arrayP = procArray;
2246  int count = 0;
2247  int index;
2248 
2249  /* allocate what's certainly enough result space */
2250  vxids = (VirtualTransactionId *)
2251  palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
2252 
2253  LWLockAcquire(ProcArrayLock, LW_SHARED);
2254 
2255  for (index = 0; index < arrayP->numProcs; index++)
2256  {
2257  int pgprocno = arrayP->pgprocnos[index];
2258  volatile PGPROC *proc = &allProcs[pgprocno];
2259  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2260 
2261  if (pgxact->delayChkpt)
2262  {
2263  VirtualTransactionId vxid;
2264 
2265  GET_VXID_FROM_PGPROC(vxid, *proc);
2266  if (VirtualTransactionIdIsValid(vxid))
2267  vxids[count++] = vxid;
2268  }
2269  }
2270 
2271  LWLockRelease(ProcArrayLock);
2272 
2273  *nvxids = count;
2274  return vxids;
2275 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
Definition: proc.h:207
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static PGPROC * allProcs
Definition: procarray.c:100
bool delayChkpt
Definition: proc.h:220
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void * palloc(Size size)
Definition: mcxt.c:849
Definition: proc.h:94
bool HaveVirtualXIDsDelayingChkpt ( VirtualTransactionId vxids,
int  nvxids 
)

Definition at line 2287 of file procarray.c.

References PGXACT::delayChkpt, GET_VXID_FROM_PGPROC, i, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, procArray, result, VirtualTransactionIdEquals, and VirtualTransactionIdIsValid.

Referenced by CreateCheckPoint().

2288 {
2289  bool result = false;
2290  ProcArrayStruct *arrayP = procArray;
2291  int index;
2292 
2293  LWLockAcquire(ProcArrayLock, LW_SHARED);
2294 
2295  for (index = 0; index < arrayP->numProcs; index++)
2296  {
2297  int pgprocno = arrayP->pgprocnos[index];
2298  volatile PGPROC *proc = &allProcs[pgprocno];
2299  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2300  VirtualTransactionId vxid;
2301 
2302  GET_VXID_FROM_PGPROC(vxid, *proc);
2303 
2304  if (pgxact->delayChkpt && VirtualTransactionIdIsValid(vxid))
2305  {
2306  int i;
2307 
2308  for (i = 0; i < nvxids; i++)
2309  {
2310  if (VirtualTransactionIdEquals(vxid, vxids[i]))
2311  {
2312  result = true;
2313  break;
2314  }
2315  }
2316  if (result)
2317  break;
2318  }
2319  }
2320 
2321  LWLockRelease(ProcArrayLock);
2322 
2323  return result;
2324 }
#define GET_VXID_FROM_PGPROC(vxid, proc)
Definition: lock.h:81
Definition: proc.h:207
return result
Definition: formatting.c:1618
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define VirtualTransactionIdEquals(vxid1, vxid2)
Definition: lock.h:75
static PGPROC * allProcs
Definition: procarray.c:100
bool delayChkpt
Definition: proc.h:220
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int i
Definition: proc.h:94
bool IsBackendPid ( int  pid)

Definition at line 2429 of file procarray.c.

References BackendPidGetProc(), and NULL.

Referenced by logicalrep_worker_find(), and pg_stat_get_subscription().

2430 {
2431  return (BackendPidGetProc(pid) != NULL);
2432 }
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2334
#define NULL
Definition: c.h:229
bool MinimumActiveBackends ( int  min)

Definition at line 2673 of file procarray.c.

References InvalidTransactionId, MyProc, NULL, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, PGPROC::waitLock, and PGXACT::xid.

Referenced by XLogFlush().

2674 {
2675  ProcArrayStruct *arrayP = procArray;
2676  int count = 0;
2677  int index;
2678 
2679  /* Quick short-circuit if no minimum is specified */
2680  if (min == 0)
2681  return true;
2682 
2683  /*
2684  * Note: for speed, we don't acquire ProcArrayLock. This is a little bit
2685  * bogus, but since we are only testing fields for zero or nonzero, it
2686  * should be OK. The result is only used for heuristic purposes anyway...
2687  */
2688  for (index = 0; index < arrayP->numProcs; index++)
2689  {
2690  int pgprocno = arrayP->pgprocnos[index];
2691  volatile PGPROC *proc = &allProcs[pgprocno];
2692  volatile PGXACT *pgxact = &allPgXact[pgprocno];
2693 
2694  /*
2695  * Since we're not holding a lock, need to be prepared to deal with
2696  * garbage, as someone could have incremented numProcs but not yet
2697  * filled the structure.
2698  *
2699  * If someone just decremented numProcs, 'proc' could also point to a
2700  * PGPROC entry that's no longer in the array. It still points to a
2701  * PGPROC struct, though, because freed PGPROC entries just go to the
2702  * free list and are recycled. Its contents are nonsense in that case,
2703  * but that's acceptable for this function.
2704  */
2705  if (pgprocno == -1)
2706  continue; /* do not count deleted entries */
2707  if (proc == MyProc)
2708  continue; /* do not count myself */
2709  if (pgxact->xid == InvalidTransactionId)
2710  continue; /* do not count if no XID assigned */
2711  if (proc->pid == 0)
2712  continue; /* do not count prepared xacts */
2713  if (proc->waitLock != NULL)
2714  continue; /* do not count if blocked on a lock */
2715  count++;
2716  if (count >= min)
2717  break;
2718  }
2719 
2720  return count >= min;
2721 }
Definition: proc.h:207
PGPROC * MyProc
Definition: proc.c:67
TransactionId xid
Definition: proc.h:209
Definition: type.h:90
#define InvalidTransactionId
Definition: transam.h:31
static PGPROC * allProcs
Definition: procarray.c:100
LOCK * waitLock
Definition: proc.h:135
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define NULL
Definition: c.h:229
Definition: proc.h:94
int pid
Definition: proc.h:108
void ProcArrayAdd ( PGPROC proc)

Definition at line 274 of file procarray.c.

References ereport, errcode(), errmsg(), FATAL, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::maxProcs, memmove, ProcArrayStruct::numProcs, PGPROC::pgprocno, ProcArrayStruct::pgprocnos, and procArray.

Referenced by InitProcessPhase2(), and MarkAsPrepared().

275 {
276  ProcArrayStruct *arrayP = procArray;
277  int index;
278 
279  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
280 
281  if (arrayP->numProcs >= arrayP->maxProcs)
282  {
283  /*
284  * Oops, no room. (This really shouldn't happen, since there is a
285  * fixed supply of PGPROC structs too, and so we should have failed
286  * earlier.)
287  */
288  LWLockRelease(ProcArrayLock);
289  ereport(FATAL,
290  (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
291  errmsg("sorry, too many clients already")));
292  }
293 
294  /*
295  * Keep the procs array sorted by (PGPROC *) so that we can utilize
296  * locality of references much better. This is useful while traversing the
297  * ProcArray because there is an increased likelihood of finding the next
298  * PGPROC structure in the cache.
299  *
300  * Since the occurrence of adding/removing a proc is much lower than the
301  * access to the ProcArray itself, the overhead should be marginal
302  */
303  for (index = 0; index < arrayP->numProcs; index++)
304  {
305  /*
306  * If we are the first PGPROC or if we have found our right position
307  * in the array, break
308  */
309  if ((arrayP->pgprocnos[index] == -1) || (arrayP->pgprocnos[index] > proc->pgprocno))
310  break;
311  }
312 
313  memmove(&arrayP->pgprocnos[index + 1], &arrayP->pgprocnos[index],
314  (arrayP->numProcs - index) * sizeof(int));
315  arrayP->pgprocnos[index] = proc->pgprocno;
316  arrayP->numProcs++;
317 
318  LWLockRelease(ProcArrayLock);
319 }
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define FATAL
Definition: elog.h:52
#define memmove(d, s, c)
Definition: c.h:1058
#define ereport(elevel, rest)
Definition: elog.h:122
static ProcArrayStruct * procArray
Definition: procarray.c:98
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int pgprocno
Definition: proc.h:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ProcArrayApplyRecoveryInfo ( RunningTransactions  running)

Definition at line 667 of file procarray.c.

References Assert, DEBUG1, DEBUG3, elog, ERROR, ExpireOldKnownAssignedTransactionIds(), ExtendSUBTRANS(), i, InvalidTransactionId, KnownAssignedXidsAdd(), KnownAssignedXidsDisplay(), KnownAssignedXidsReset(), ProcArrayStruct::lastOverflowedXid, RunningTransactionsData::latestCompletedXid, VariableCacheData::latestCompletedXid, latestObservedXid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), RunningTransactionsData::nextXid, VariableCacheData::nextXid, ProcArrayStruct::numKnownAssignedXids, RunningTransactionsData::oldestRunningXid, palloc(), pfree(), qsort, ShmemVariableCache, STANDBY_INITIALIZED, STANDBY_SNAPSHOT_PENDING, STANDBY_SNAPSHOT_READY, StandbyReleaseOldLocks(), standbySnapshotPendingXmin, standbyState, RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_overflow, trace_recovery(), TransactionIdAdvance, TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdFollows(), TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdRetreat, RunningTransactionsData::xcnt, xidComparator(), and RunningTransactionsData::xids.

Referenced by standby_redo(), StartupXLOG(), and xlog_redo().

668 {
669  TransactionId *xids;
670  int nxids;
671  TransactionId nextXid;
672  int i;
673 
678 
679  /*
680  * Remove stale transactions, if any.
681  */
683 
684  /*
685  * Remove stale locks, if any.
686  *
687  * Locks are always assigned to the toplevel xid so we don't need to care
688  * about subxcnt/subxids (and by extension not about ->suboverflowed).
689  */
690  StandbyReleaseOldLocks(running->xcnt, running->xids);
691 
692  /*
693  * If our snapshot is already valid, nothing else to do...
694  */
696  return;
697 
698  /*
699  * If our initial RunningTransactionsData had an overflowed snapshot then
700  * we knew we were missing some subxids from our snapshot. If we continue
701  * to see overflowed snapshots then we might never be able to start up, so
702  * we make another test to see if our snapshot is now valid. We know that
703  * the missing subxids are equal to or earlier than nextXid. After we
704  * initialise we continue to apply changes during recovery, so once the
705  * oldestRunningXid is later than the nextXid from the initial snapshot we
706  * know that we no longer have missing information and can mark the
707  * snapshot as valid.
708  */
710  {
711  /*
712  * If the snapshot isn't overflowed or if its empty we can reset our
713  * pending state and use this snapshot instead.
714  */
715  if (!running->subxid_overflow || running->xcnt == 0)
716  {
717  /*
718  * If we have already collected known assigned xids, we need to
719  * throw them away before we apply the recovery snapshot.
720  */
723  }
724  else
725  {
727  running->oldestRunningXid))
728  {
731  "recovery snapshots are now enabled");
732  }
733  else
735  "recovery snapshot waiting for non-overflowed snapshot or "
736  "until oldest active xid on standby is at least %u (now %u)",
738  running->oldestRunningXid);
739  return;
740  }
741  }
742 
744 
745  /*
746  * OK, we need to initialise from the RunningTransactionsData record.
747  *
748  * NB: this can be reached at least twice, so make sure new code can deal
749  * with that.
750  */
751 
752  /*
753  * Nobody else is running yet, but take locks anyhow
754  */
755  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
756 
757  /*
758  * KnownAssignedXids is sorted so we cannot just add the xids, we have to
759  * sort them first.
760  *
761  * Some of the new xids are top-level xids and some are subtransactions.
762  * We don't call SubtransSetParent because it doesn't matter yet. If we
763  * aren't overflowed then all xids will fit in snapshot and so we don't
764  * need subtrans. If we later overflow, an xid assignment record will add
765  * xids to subtrans. If RunningXacts is overflowed then we don't have
766  * enough information to correctly update subtrans anyway.
767  */
768 
769  /*
770  * Allocate a temporary array to avoid modifying the array passed as
771  * argument.
772  */
773  xids = palloc(sizeof(TransactionId) * (running->xcnt + running->subxcnt));
774 
775  /*
776  * Add to the temp array any xids which have not already completed.
777  */
778  nxids = 0;
779  for (i = 0; i < running->xcnt + running->subxcnt; i++)
780  {
781  TransactionId xid = running->xids[i];
782 
783  /*
784  * The running-xacts snapshot can contain xids that were still visible
785  * in the procarray when the snapshot was taken, but were already
786  * WAL-logged as completed. They're not running anymore, so ignore
787  * them.
788  */
790  continue;
791 
792  xids[nxids++] = xid;
793  }
794 
795  if (nxids > 0)
796  {
798  {
799  LWLockRelease(ProcArrayLock);
800  elog(ERROR, "KnownAssignedXids is not empty");
801  }
802 
803  /*
804  * Sort the array so that we can add them safely into
805  * KnownAssignedXids.
806  */
807  qsort(xids, nxids, sizeof(TransactionId), xidComparator);
808 
809  /*
810  * Add the sorted snapshot into KnownAssignedXids
811  */
812  for (i = 0; i < nxids; i++)
813  KnownAssignedXidsAdd(xids[i], xids[i], true);
814 
816  }
817 
818  pfree(xids);
819 
820  /*
821  * latestObservedXid is at least set to the point where SUBTRANS was
822  * started up to (c.f. ProcArrayInitRecovery()) or to the biggest xid
823  * RecordKnownAssignedTransactionIds() was called for. Initialize
824  * subtrans from thereon, up to nextXid - 1.
825  *
826  * We need to duplicate parts of RecordKnownAssignedTransactionId() here,
827  * because we've just added xids to the known assigned xids machinery that
828  * haven't gone through RecordKnownAssignedTransactionId().
829  */
833  {
836  }
837  TransactionIdRetreat(latestObservedXid); /* = running->nextXid - 1 */
838 
839  /* ----------
840  * Now we've got the running xids we need to set the global values that
841  * are used to track snapshots as they evolve further.
842  *
843  * - latestCompletedXid which will be the xmax for snapshots
844  * - lastOverflowedXid which shows whether snapshots overflow
845  * - nextXid
846  *
847  * If the snapshot overflowed, then we still initialise with what we know,
848  * but the recovery snapshot isn't fully valid yet because we know there
849  * are some subxids missing. We don't know the specific subxids that are
850  * missing, so conservatively assume the last one is latestObservedXid.
851  * ----------
852  */
853  if (running->subxid_overflow)
854  {
856 
859  }
860  else
861  {
863 
865  }
866 
867  /*
868  * If a transaction wrote a commit record in the gap between taking and
869  * logging the snapshot then latestCompletedXid may already be higher than
870  * the value from the snapshot, so check before we use the incoming value.
871  */
873  running->latestCompletedXid))
875 
877 
878  LWLockRelease(ProcArrayLock);
879 
880  /*
881  * ShmemVariableCache->nextXid must be beyond any observed xid.
882  *
883  * We don't expect anyone else to modify nextXid, hence we don't need to
884  * hold a lock while examining it. We still acquire the lock to modify
885  * it, though.
886  */
887  nextXid = latestObservedXid;
888  TransactionIdAdvance(nextXid);
890  {
891  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
892  ShmemVariableCache->nextXid = nextXid;
893  LWLockRelease(XidGenLock);
894  }
895 
897 
900  elog(trace_recovery(DEBUG1), "recovery snapshots are now enabled");
901  else
903  "recovery snapshot waiting for non-overflowed snapshot or "
904  "until oldest active xid on standby is at least %u (now %u)",
906  running->oldestRunningXid);
907 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
static TransactionId latestObservedXid
Definition: procarray.c:108
TransactionId oldestRunningXid
Definition: standby.h:76
#define DEBUG1
Definition: elog.h:25
static void KnownAssignedXidsDisplay(int trace_level)
Definition: procarray.c:3897
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
uint32 TransactionId
Definition: c.h:397
#define DEBUG3
Definition: elog.h:23
TransactionId * xids
Definition: standby.h:79
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
#define TransactionIdRetreat(dest)
Definition: transam.h:56
int trace_recovery(int trace_level)
Definition: elog.c:3753
TransactionId latestCompletedXid
Definition: standby.h:77
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
void ExtendSUBTRANS(TransactionId newestXact)
Definition: subtrans.c:311
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
static void KnownAssignedXidsReset(void)
Definition: procarray.c:3936
int numKnownAssignedXids
Definition: procarray.c:75
TransactionId lastOverflowedXid
Definition: procarray.c:87
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:181
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
void StandbyReleaseOldLocks(int nxids, TransactionId *xids)
Definition: standby.c:725
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid, bool exclusive_lock)
Definition: procarray.c:3437
TransactionId nextXid
Definition: standby.h:75
void * palloc(Size size)
Definition: mcxt.c:849
int i
void ExpireOldKnownAssignedTransactionIds(TransactionId xid)
Definition: procarray.c:3271
#define elog
Definition: elog.h:219
#define qsort(a, b, c, d)
Definition: port.h:440
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
static TransactionId standbySnapshotPendingXmin
Definition: procarray.c:115
HotStandbyState standbyState
Definition: xlog.c:195
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:138
TransactionId latestCompletedXid
Definition: transam.h:135
void ProcArrayApplyXidAssignment ( TransactionId  topxid,
int  nsubxids,
TransactionId subxids 
)

Definition at line 914 of file procarray.c.

References Assert, i, InvalidTransactionId, KnownAssignedXidsRemoveTree(), ProcArrayStruct::lastOverflowedXid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), RecordKnownAssignedTransactionIds(), STANDBY_INITIALIZED, standbyState, SubTransSetParent(), TransactionIdLatest(), and TransactionIdPrecedes().

Referenced by xact_redo().

916 {
917  TransactionId max_xid;
918  int i;
919 
921 
922  max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
923 
924  /*
925  * Mark all the subtransactions as observed.
926  *
927  * NOTE: This will fail if the subxid contains too many previously
928  * unobserved xids to fit into known-assigned-xids. That shouldn't happen
929  * as the code stands, because xid-assignment records should never contain
930  * more than PGPROC_MAX_CACHED_SUBXIDS entries.
931  */
933 
934  /*
935  * Notice that we update pg_subtrans with the top-level xid, rather than
936  * the parent xid. This is a difference between normal processing and
937  * recovery, yet is still correct in all cases. The reason is that
938  * subtransaction commit is not marked in clog until commit processing, so
939  * all aborted subtransactions have already been clearly marked in clog.
940  * As a result we are able to refer directly to the top-level
941  * transaction's state rather than skipping through all the intermediate
942  * states in the subtransaction tree. This should be the first time we
943  * have attempted to SubTransSetParent().
944  */
945  for (i = 0; i < nsubxids; i++)
946  SubTransSetParent(subxids[i], topxid, false);
947 
948  /* KnownAssignedXids isn't maintained yet, so we're done for now */
950  return;
951 
952  /*
953  * Uses same locking as transaction commit
954  */
955  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
956 
957  /*
958  * Remove subxids from known-assigned-xacts.
959  */
961 
962  /*
963  * Advance lastOverflowedXid to be at least the last of these subxids.
964  */
966  procArray->lastOverflowedXid = max_xid;
967 
968  LWLockRelease(ProcArrayLock);
969 }
uint32 TransactionId
Definition: c.h:397
TransactionId TransactionIdLatest(TransactionId mainxid, int nxids, const TransactionId *xids)
Definition: transam.c:365
void SubTransSetParent(TransactionId xid, TransactionId parent, bool overwriteOK)
Definition: subtrans.c:75
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define InvalidTransactionId
Definition: transam.h:31
TransactionId lastOverflowedXid
Definition: procarray.c:87
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids, TransactionId *subxids)
Definition: procarray.c:3689
static ProcArrayStruct * procArray
Definition: procarray.c:98
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int i
void RecordKnownAssignedTransactionIds(TransactionId xid)
Definition: procarray.c:3161
HotStandbyState standbyState
Definition: xlog.c:195
void ProcArrayClearTransaction ( PGPROC proc)

Definition at line 604 of file procarray.c.

References PGXACT::delayChkpt, InvalidLocalTransactionId, InvalidTransactionId, PGPROC::lxid, PGXACT::nxids, PGXACT::overflowed, PGPROC::pgprocno, PROC_VACUUM_STATE_MASK, PGPROC::recoveryConflictPending, PGXACT::vacuumFlags, PGXACT::xid, and PGXACT::xmin.

Referenced by PrepareTransaction().

605 {
606  PGXACT *pgxact = &allPgXact[proc->pgprocno];
607 
608  /*
609  * We can skip locking ProcArrayLock here, because this action does not
610  * actually change anyone's view of the set of running XIDs: our entry is
611  * duplicate with the gxact that has already been inserted into the
612  * ProcArray.
613  */
614  pgxact->xid = InvalidTransactionId;
616  pgxact->xmin = InvalidTransactionId;
617  proc->recoveryConflictPending = false;
618 
619  /* redundant, but just in case */
621  pgxact->delayChkpt = false;
622 
623  /* Clear the subtransaction-XID cache too */
624  pgxact->nxids = 0;
625  pgxact->overflowed = false;
626 }
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
TransactionId xid
Definition: proc.h:209
uint8 vacuumFlags
Definition: proc.h:218
#define PROC_VACUUM_STATE_MASK
Definition: proc.h:61
bool recoveryConflictPending
Definition: proc.h:123
uint8 nxids
Definition: proc.h:223
#define InvalidTransactionId
Definition: transam.h:31
bool delayChkpt
Definition: proc.h:220
static PGXACT * allPgXact
Definition: procarray.c:101
bool overflowed
Definition: proc.h:219
int pgprocno
Definition: proc.h:109
#define InvalidLocalTransactionId
Definition: lock.h:70
LocalTransactionId lxid
Definition: proc.h:105
void ProcArrayEndTransaction ( PGPROC proc,
TransactionId  latestXid 
)

Definition at line 395 of file procarray.c.

References Assert, PGXACT::delayChkpt, InvalidLocalTransactionId, InvalidTransactionId, LW_EXCLUSIVE, LWLockConditionalAcquire(), LWLockRelease(), PGPROC::lxid, PGXACT::nxids, PGXACT::overflowed, PGPROC::pgprocno, PROC_VACUUM_STATE_MASK, ProcArrayEndTransactionInternal(), ProcArrayGroupClearXid(), PGPROC::recoveryConflictPending, TransactionIdIsValid, PGXACT::vacuumFlags, PGXACT::xid, and PGXACT::xmin.

Referenced by AbortTransaction(), and CommitTransaction().

396 {
397  PGXACT *pgxact = &allPgXact[proc->pgprocno];
398 
399  if (TransactionIdIsValid(latestXid))
400  {
401  /*
402  * We must lock ProcArrayLock while clearing our advertised XID, so
403  * that we do not exit the set of "running" transactions while someone
404  * else is taking a snapshot. See discussion in
405  * src/backend/access/transam/README.
406  */
408 
409  /*
410  * If we can immediately acquire ProcArrayLock, we clear our own XID
411  * and release the lock. If not, use group XID clearing to improve
412  * efficiency.
413  */
414  if (LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE))
415  {
416  ProcArrayEndTransactionInternal(proc, pgxact, latestXid);
417  LWLockRelease(ProcArrayLock);
418  }
419  else
420  ProcArrayGroupClearXid(proc, latestXid);
421  }
422  else
423  {
424  /*
425  * If we have no XID, we don't need to lock, since we won't affect
426  * anyone else's calculation of a snapshot. We might change their
427  * estimate of global xmin, but that's OK.
428  */
430 
432  pgxact->xmin = InvalidTransactionId;
433  /* must be cleared with xid/xmin: */
435  pgxact->delayChkpt = false; /* be sure this is cleared in abort */
436  proc->recoveryConflictPending = false;
437 
438  Assert(pgxact->nxids == 0);
439  Assert(pgxact->overflowed == false);
440  }
441 }
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
TransactionId xid
Definition: proc.h:209
static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:483
uint8 vacuumFlags
Definition: proc.h:218
#define PROC_VACUUM_STATE_MASK
Definition: proc.h:61
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1283
bool recoveryConflictPending
Definition: proc.h:123
uint8 nxids
Definition: proc.h:223
#define InvalidTransactionId
Definition: transam.h:31
bool delayChkpt
Definition: proc.h:220
static PGXACT * allPgXact
Definition: procarray.c:101
bool overflowed
Definition: proc.h:219
static void ProcArrayEndTransactionInternal(PGPROC *proc, PGXACT *pgxact, TransactionId latestXid)
Definition: procarray.c:449
#define Assert(condition)
Definition: c.h:675
int pgprocno
Definition: proc.h:109
#define InvalidLocalTransactionId
Definition: lock.h:70
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:105
void ProcArrayGetReplicationSlotXmin ( TransactionId xmin,
TransactionId catalog_xmin 
)

Definition at line 2974 of file procarray.c.

References LW_SHARED, LWLockAcquire(), LWLockRelease(), NULL, ProcArrayStruct::replication_slot_catalog_xmin, and ProcArrayStruct::replication_slot_xmin.

Referenced by logical_begin_heap_rewrite(), and XLogWalRcvSendHSFeedback().

2976 {
2977  LWLockAcquire(ProcArrayLock, LW_SHARED);
2978 
2979  if (xmin != NULL)
2981 
2982  if (catalog_xmin != NULL)
2983  *catalog_xmin = procArray->replication_slot_catalog_xmin;
2984 
2985  LWLockRelease(ProcArrayLock);
2986 }
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:92
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId replication_slot_xmin
Definition: procarray.c:90
#define NULL
Definition: c.h:229
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void ProcArrayInitRecovery ( TransactionId  initializedUptoXID)

Definition at line 636 of file procarray.c.

References Assert, latestObservedXid, STANDBY_INITIALIZED, standbyState, TransactionIdIsNormal, and TransactionIdRetreat.

Referenced by StartupXLOG().

637 {
639  Assert(TransactionIdIsNormal(initializedUptoXID));
640 
641  /*
642  * we set latestObservedXid to the xid SUBTRANS has been initialized up
643  * to, so we can extend it from that point onwards in
644  * RecordKnownAssignedTransactionIds, and when we get consistent in
645  * ProcArrayApplyRecoveryInfo().
646  */
647  latestObservedXid = initializedUptoXID;
649 }
static TransactionId latestObservedXid
Definition: procarray.c:108
#define TransactionIdRetreat(dest)
Definition: transam.h:56
#define Assert(condition)
Definition: c.h:675
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
HotStandbyState standbyState
Definition: xlog.c:195
bool ProcArrayInstallImportedXmin ( TransactionId  xmin,
TransactionId  sourcexid 
)

Definition at line 1796 of file procarray.c.

References Assert, PGPROC::databaseId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyDatabaseId, MyPgXact, ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PROC_IN_VACUUM, procArray, result, TransactionIdIsNormal, TransactionIdPrecedesOrEquals(), TransactionXmin, PGXACT::vacuumFlags, PGXACT::xid, and PGXACT::xmin.

Referenced by GetSerializableTransactionSnapshotInt(), and SetTransactionSnapshot().

1797 {
1798  bool result = false;
1799  ProcArrayStruct *arrayP = procArray;
1800  int index;
1801 
1803  if (!TransactionIdIsNormal(sourcexid))
1804  return false;
1805 
1806  /* Get lock so source xact can't end while we're doing this */
1807  LWLockAcquire(ProcArrayLock, LW_SHARED);
1808 
1809  for (index = 0; index < arrayP->numProcs; index++)
1810  {
1811  int pgprocno = arrayP->pgprocnos[index];
1812  volatile PGPROC *proc = &allProcs[pgprocno];
1813  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1814  TransactionId xid;
1815 
1816  /* Ignore procs running LAZY VACUUM */
1817  if (pgxact->vacuumFlags & PROC_IN_VACUUM)
1818  continue;
1819 
1820  xid = pgxact->xid; /* fetch just once */
1821  if (xid != sourcexid)
1822  continue;
1823 
1824  /*
1825  * We check the transaction's database ID for paranoia's sake: if it's
1826  * in another DB then its xmin does not cover us. Caller should have
1827  * detected this already, so we just treat any funny cases as
1828  * "transaction not found".
1829  */
1830  if (proc->databaseId != MyDatabaseId)
1831  continue;
1832 
1833  /*
1834  * Likewise, let's just make real sure its xmin does cover us.
1835  */
1836  xid = pgxact->xmin; /* fetch just once */
1837  if (!TransactionIdIsNormal(xid) ||
1838  !TransactionIdPrecedesOrEquals(xid, xmin))
1839  continue;
1840 
1841  /*
1842  * We're good. Install the new xmin. As in GetSnapshotData, set
1843  * TransactionXmin too. (Note that because snapmgr.c called
1844  * GetSnapshotData first, we'll be overwriting a valid xmin here, so
1845  * we don't check that.)
1846  */
1847  MyPgXact->xmin = TransactionXmin = xmin;
1848 
1849  result = true;
1850  break;
1851  }
1852 
1853  LWLockRelease(ProcArrayLock);
1854 
1855  return result;
1856 }
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
TransactionId xid
Definition: proc.h:209
return result
Definition: formatting.c:1618
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:218
TransactionId TransactionXmin
Definition: snapmgr.c:164
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define PROC_IN_VACUUM
Definition: proc.h:53
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
static PGPROC * allProcs
Definition: procarray.c:100
Oid databaseId
Definition: proc.h:113
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
Oid MyDatabaseId
Definition: globals.c:76
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Definition: proc.h:94
bool ProcArrayInstallRestoredXmin ( TransactionId  xmin,
PGPROC proc 
)

Definition at line 1868 of file procarray.c.

References Assert, PGPROC::databaseId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyDatabaseId, MyPgXact, NULL, PGPROC::pgprocno, result, TransactionIdIsNormal, TransactionIdPrecedesOrEquals(), TransactionXmin, and PGXACT::xmin.

Referenced by SetTransactionSnapshot().

1869 {
1870  bool result = false;
1871  TransactionId xid;
1872  volatile PGXACT *pgxact;
1873 
1875  Assert(proc != NULL);
1876 
1877  /* Get lock so source xact can't end while we're doing this */
1878  LWLockAcquire(ProcArrayLock, LW_SHARED);
1879 
1880  pgxact = &allPgXact[proc->pgprocno];
1881 
1882  /*
1883  * Be certain that the referenced PGPROC has an advertised xmin which is
1884  * no later than the one we're installing, so that the system-wide xmin
1885  * can't go backwards. Also, make sure it's running in the same database,
1886  * so that the per-database xmin cannot go backwards.
1887  */
1888  xid = pgxact->xmin; /* fetch just once */
1889  if (proc->databaseId == MyDatabaseId &&
1890  TransactionIdIsNormal(xid) &&
1891  TransactionIdPrecedesOrEquals(xid, xmin))
1892  {
1893  MyPgXact->xmin = TransactionXmin = xmin;
1894  result = true;
1895  }
1896 
1897  LWLockRelease(ProcArrayLock);
1898 
1899  return result;
1900 }
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xmin
Definition: proc.h:213
return result
Definition: formatting.c:1618
PGXACT * MyPgXact
Definition: proc.c:68
TransactionId TransactionXmin
Definition: snapmgr.c:164
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
Oid databaseId
Definition: proc.h:113
static PGXACT * allPgXact
Definition: procarray.c:101
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int pgprocno
Definition: proc.h:109
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void ProcArrayRemove ( PGPROC proc,
TransactionId  latestXid 
)

Definition at line 332 of file procarray.c.

References Assert, elog, VariableCacheData::latestCompletedXid, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), memmove, ProcArrayStruct::numProcs, PGPROC::pgprocno, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, ShmemVariableCache, TransactionIdIsValid, TransactionIdPrecedes(), and PGXACT::xid.

Referenced by FinishPreparedTransaction(), and RemoveProcFromArray().

333 {
334  ProcArrayStruct *arrayP = procArray;
335  int index;
336 
337 #ifdef XIDCACHE_DEBUG
338  /* dump stats at backend shutdown, but not prepared-xact end */
339  if (proc->pid != 0)
340  DisplayXidCache();
341 #endif
342 
343  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
344 
345  if (TransactionIdIsValid(latestXid))
346  {
348 
349  /* Advance global latestCompletedXid while holding the lock */
351  latestXid))
353  }
354  else
355  {
356  /* Shouldn't be trying to remove a live transaction here */
358  }
359 
360  for (index = 0; index < arrayP->numProcs; index++)
361  {
362  if (arrayP->pgprocnos[index] == proc->pgprocno)
363  {
364  /* Keep the PGPROC array sorted. See notes above */
365  memmove(&arrayP->pgprocnos[index], &arrayP->pgprocnos[index + 1],
366  (arrayP->numProcs - index - 1) * sizeof(int));
367  arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
368  arrayP->numProcs--;
369  LWLockRelease(ProcArrayLock);
370  return;
371  }
372  }
373 
374  /* Oops */
375  LWLockRelease(ProcArrayLock);
376 
377  elog(LOG, "failed to find proc %p in ProcArray", proc);
378 }
TransactionId xid
Definition: proc.h:209
#define LOG
Definition: elog.h:26
Definition: type.h:90
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define memmove(d, s, c)
Definition: c.h:1058
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int pgprocno
Definition: proc.h:109
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
int pid
Definition: proc.h:108
TransactionId latestCompletedXid
Definition: transam.h:135
void ProcArraySetReplicationSlotXmin ( TransactionId  xmin,
TransactionId  catalog_xmin,
bool  already_locked 
)

Definition at line 2952 of file procarray.c.

References Assert, LW_EXCLUSIVE, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), ProcArrayStruct::replication_slot_catalog_xmin, and ProcArrayStruct::replication_slot_xmin.

Referenced by ReplicationSlotsComputeRequiredXmin().

2954 {
2955  Assert(!already_locked || LWLockHeldByMe(ProcArrayLock));
2956 
2957  if (!already_locked)
2958  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2959 
2961  procArray->replication_slot_catalog_xmin = catalog_xmin;
2962 
2963  if (!already_locked)
2964  LWLockRelease(ProcArrayLock);
2965 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:92
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static ProcArrayStruct * procArray
Definition: procarray.c:98
TransactionId replication_slot_xmin
Definition: procarray.c:90
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Size ProcArrayShmemSize ( void  )

Definition at line 179 of file procarray.c.

References add_size(), EnableHotStandby, mul_size(), offsetof, PROCARRAY_MAXPROCS, and TOTAL_MAX_CACHED_SUBXIDS.

Referenced by CreateSharedMemoryAndSemaphores().

180 {
181  Size size;
182 
183  /* Size of the ProcArray structure itself */
184 #define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
185 
186  size = offsetof(ProcArrayStruct, pgprocnos);
187  size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
188 
189  /*
190  * During Hot Standby processing we have a data structure called
191  * KnownAssignedXids, created in shared memory. Local data structures are
192  * also created in various backends during GetSnapshotData(),
193  * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
194  * main structures created in those functions must be identically sized,
195  * since we may at times copy the whole of the data structures around. We
196  * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
197  *
198  * Ideally we'd only create this structure if we were actually doing hot
199  * standby in the current run, but we don't know that yet at the time
200  * shared memory is being set up.
201  */
202 #define TOTAL_MAX_CACHED_SUBXIDS \
203  ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
204 
205  if (EnableHotStandby)
206  {
207  size = add_size(size,
208  mul_size(sizeof(TransactionId),
210  size = add_size(size,
211  mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
212  }
213 
214  return size;
215 }
#define PROCARRAY_MAXPROCS
uint32 TransactionId
Definition: c.h:397
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define TOTAL_MAX_CACHED_SUBXIDS
size_t Size
Definition: c.h:356
bool EnableHotStandby
Definition: xlog.c:96
#define offsetof(type, field)
Definition: c.h:555
void RecordKnownAssignedTransactionIds ( TransactionId  xid)

Definition at line 3161 of file procarray.c.

References Assert, DEBUG4, elog, ExtendSUBTRANS(), KnownAssignedXidsAdd(), latestObservedXid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::nextXid, ShmemVariableCache, STANDBY_INITIALIZED, standbyState, trace_recovery(), TransactionIdAdvance, TransactionIdFollows(), TransactionIdIsValid, and TransactionIdPrecedes().

Referenced by ProcArrayApplyXidAssignment(), StartupXLOG(), xact_redo_abort(), and xact_redo_commit().

3162 {
3166 
3167  elog(trace_recovery(DEBUG4), "record known xact %u latestObservedXid %u",
3168  xid, latestObservedXid);
3169 
3170  /*
3171  * When a newly observed xid arrives, it is frequently the case that it is
3172  * *not* the next xid in sequence. When this occurs, we must treat the
3173  * intervening xids as running also.
3174  */
3176  {
3177  TransactionId next_expected_xid;
3178 
3179  /*
3180  * Extend subtrans like we do in GetNewTransactionId() during normal
3181  * operation using individual extend steps. Note that we do not need
3182  * to extend clog since its extensions are WAL logged.
3183  *
3184  * This part has to be done regardless of standbyState since we
3185  * immediately start assigning subtransactions to their toplevel
3186  * transactions.
3187  */
3188  next_expected_xid = latestObservedXid;
3189  while (TransactionIdPrecedes(next_expected_xid, xid))
3190  {
3191  TransactionIdAdvance(next_expected_xid);
3192  ExtendSUBTRANS(next_expected_xid);
3193  }
3194  Assert(next_expected_xid == xid);
3195 
3196  /*
3197  * If the KnownAssignedXids machinery isn't up yet, there's nothing
3198  * more to do since we don't track assigned xids yet.
3199  */
3201  {
3202  latestObservedXid = xid;
3203  return;
3204  }
3205 
3206  /*
3207  * Add (latestObservedXid, xid] onto the KnownAssignedXids array.
3208  */
3209  next_expected_xid = latestObservedXid;
3210  TransactionIdAdvance(next_expected_xid);
3211  KnownAssignedXidsAdd(next_expected_xid, xid, false);
3212 
3213  /*
3214  * Now we can advance latestObservedXid
3215  */
3216  latestObservedXid = xid;
3217 
3218  /* ShmemVariableCache->nextXid must be beyond any observed xid */
3219  next_expected_xid = latestObservedXid;
3220  TransactionIdAdvance(next_expected_xid);
3221  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
3222  ShmemVariableCache->nextXid = next_expected_xid;
3223  LWLockRelease(XidGenLock);
3224  }
3225 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
static TransactionId latestObservedXid
Definition: procarray.c:108
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
uint32 TransactionId
Definition: c.h:397
#define DEBUG4
Definition: elog.h:22
int trace_recovery(int trace_level)
Definition: elog.c:3753
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void ExtendSUBTRANS(TransactionId newestXact)
Definition: subtrans.c:311
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid, bool exclusive_lock)
Definition: procarray.c:3437
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
HotStandbyState standbyState
Definition: xlog.c:195
bool TransactionIdIsActive ( TransactionId  xid)

Definition at line 1215 of file procarray.c.

References i, LW_SHARED, LWLockAcquire(), LWLockRelease(), ProcArrayStruct::numProcs, ProcArrayStruct::pgprocnos, PGPROC::pid, procArray, RecentXmin, result, TransactionIdEquals, TransactionIdIsValid, TransactionIdPrecedes(), and PGXACT::xid.

1216 {
1217  bool result = false;
1218  ProcArrayStruct *arrayP = procArray;
1219  int i;
1220 
1221  /*
1222  * Don't bother checking a transaction older than RecentXmin; it could not
1223  * possibly still be running.
1224  */
1226  return false;
1227 
1228  LWLockAcquire(ProcArrayLock, LW_SHARED);
1229 
1230  for (i = 0; i < arrayP->numProcs; i++)
1231  {
1232  int pgprocno = arrayP->pgprocnos[i];
1233  volatile PGPROC *proc = &allProcs[pgprocno];
1234  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1235  TransactionId pxid;
1236 
1237  /* Fetch xid just once - see GetNewTransactionId */
1238  pxid = pgxact->xid;
1239 
1240  if (!TransactionIdIsValid(pxid))
1241  continue;
1242 
1243  if (proc->pid == 0)
1244  continue; /* ignore prepared transactions */
1245 
1246  if (TransactionIdEquals(pxid, xid))
1247  {
1248  result = true;
1249  break;
1250  }
1251  }
1252 
1253  LWLockRelease(ProcArrayLock);
1254 
1255  return result;
1256 }
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
TransactionId xid
Definition: proc.h:209
TransactionId RecentXmin
Definition: snapmgr.c:165
return result
Definition: formatting.c:1618
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
static PGPROC * allProcs
Definition: procarray.c:100
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int i
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Definition: proc.h:94
int pid
Definition: proc.h:108
bool TransactionIdIsInProgress ( TransactionId  xid)

Definition at line 998 of file procarray.c.

References Assert, ereport, errcode(), errmsg(), ERROR, i, KnownAssignedXidExists(), KnownAssignedXidsGet(), ProcArrayStruct::lastOverflowedXid, VariableCacheData::latestCompletedXid, LW_SHARED, LWLockAcquire(), LWLockRelease(), malloc, ProcArrayStruct::maxProcs, MyProc, NULL, ProcArrayStruct::numProcs, PGXACT::nxids, PGXACT::overflowed, ProcArrayStruct::pgprocnos, procArray, RecentXmin, RecoveryInProgress(), ShmemVariableCache, SubTransGetTopmostTransaction(), PGPROC::subxids, TOTAL_MAX_CACHED_SUBXIDS, TransactionIdDidAbort(), TransactionIdEquals, TransactionIdIsCurrentTransactionId(), TransactionIdIsKnownCompleted(), TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), xc_by_child_xid_inc, xc_by_known_assigned_inc, xc_by_known_xact_inc, xc_by_latest_xid_inc, xc_by_main_xid_inc, xc_by_my_xact_inc, xc_by_recent_xmin_inc, xc_no_overflow_inc, xc_slow_answer_inc, PGXACT::xid, and XidCache::xids.

Referenced by asyncQueueProcessPageEntries(), check_safe_enum_use(), compute_new_xmax_infomask(), ConditionalXactLockTableWait(), Do_MultiXactIdWait(), DoesMultiXactIdConflict(), FreezeMultiXactId(), HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesDirty(), HeapTupleSatisfiesSelf(), HeapTupleSatisfiesToast(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVacuum(), MultiXactIdExpand(), MultiXactIdIsRunning(), test_lockmode_for_conflict(), and XactLockTableWait().

999 {
1000  static TransactionId *xids = NULL;
1001  int nxids = 0;
1002  ProcArrayStruct *arrayP = procArray;
1003  TransactionId topxid;
1004  int i,
1005  j;
1006 
1007  /*
1008  * Don't bother checking a transaction older than RecentXmin; it could not
1009  * possibly still be running. (Note: in particular, this guarantees that
1010  * we reject InvalidTransactionId, FrozenTransactionId, etc as not
1011  * running.)
1012  */
1014  {
1016  return false;
1017  }
1018 
1019  /*
1020  * We may have just checked the status of this transaction, so if it is
1021  * already known to be completed, we can fall out without any access to
1022  * shared memory.
1023  */
1025  {
1027  return false;
1028  }
1029 
1030  /*
1031  * Also, we can handle our own transaction (and subtransactions) without
1032  * any access to shared memory.
1033  */
1035  {
1037  return true;
1038  }
1039 
1040  /*
1041  * If first time through, get workspace to remember main XIDs in. We
1042  * malloc it permanently to avoid repeated palloc/pfree overhead.
1043  */
1044  if (xids == NULL)
1045  {
1046  /*
1047  * In hot standby mode, reserve enough space to hold all xids in the
1048  * known-assigned list. If we later finish recovery, we no longer need
1049  * the bigger array, but we don't bother to shrink it.
1050  */
1051  int maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
1052 
1053  xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
1054  if (xids == NULL)
1055  ereport(ERROR,
1056  (errcode(ERRCODE_OUT_OF_MEMORY),
1057  errmsg("out of memory")));
1058  }
1059 
1060  LWLockAcquire(ProcArrayLock, LW_SHARED);
1061 
1062  /*
1063  * Now that we have the lock, we can check latestCompletedXid; if the
1064  * target Xid is after that, it's surely still running.
1065  */
1067  {
1068  LWLockRelease(ProcArrayLock);
1070  return true;
1071  }
1072 
1073  /* No shortcuts, gotta grovel through the array */
1074  for (i = 0; i < arrayP->numProcs; i++)
1075  {
1076  int pgprocno = arrayP->pgprocnos[i];
1077  volatile PGPROC *proc = &allProcs[pgprocno];
1078  volatile PGXACT *pgxact = &allPgXact[pgprocno];
1079  TransactionId pxid;
1080 
1081  /* Ignore my own proc --- dealt with it above */
1082  if (proc == MyProc)
1083  continue;
1084 
1085  /* Fetch xid just once - see GetNewTransactionId */
1086  pxid = pgxact->xid;
1087 
1088  if (!TransactionIdIsValid(pxid))
1089  continue;
1090 
1091  /*
1092  * Step 1: check the main Xid
1093  */
1094  if (TransactionIdEquals(pxid, xid))
1095  {
1096  LWLockRelease(ProcArrayLock);
1098  return true;
1099  }
1100 
1101  /*
1102  * We can ignore main Xids that are younger than the target Xid, since
1103  * the target could not possibly be their child.
1104  */
1105  if (TransactionIdPrecedes(xid, pxid))
1106  continue;
1107 
1108  /*
1109  * Step 2: check the cached child-Xids arrays
1110  */
1111  for (j = pgxact->nxids - 1; j >= 0; j--)
1112  {
1113  /* Fetch xid just once - see GetNewTransactionId */
1114  TransactionId cxid = proc->subxids.xids[j];
1115 
1116  if (TransactionIdEquals(cxid, xid))
1117  {
1118  LWLockRelease(ProcArrayLock);
1120  return true;
1121  }
1122  }
1123 
1124  /*
1125  * Save the main Xid for step 4. We only need to remember main Xids
1126  * that have uncached children. (Note: there is no race condition
1127  * here because the overflowed flag cannot be cleared, only set, while
1128  * we hold ProcArrayLock. So we can't miss an Xid that we need to
1129  * worry about.)
1130  */
1131  if (pgxact->overflowed)
1132  xids[nxids++] = pxid;
1133  }
1134 
1135  /*
1136  * Step 3: in hot standby mode, check the known-assigned-xids list. XIDs
1137  * in the list must be treated as running.
1138  */
1139  if (RecoveryInProgress())
1140  {
1141  /* none of the PGXACT entries should have XIDs in hot standby mode */
1142  Assert(nxids == 0);
1143 
1144  if (KnownAssignedXidExists(xid))
1145  {
1146  LWLockRelease(ProcArrayLock);
1148  return true;
1149  }
1150 
1151  /*
1152  * If the KnownAssignedXids overflowed, we have to check pg_subtrans
1153  * too. Fetch all xids from KnownAssignedXids that are lower than
1154  * xid, since if xid is a subtransaction its parent will always have a
1155  * lower value. Note we will collect both main and subXIDs here, but
1156  * there's no help for it.
1157  */
1159  nxids = KnownAssignedXidsGet(xids, xid);
1160  }
1161 
1162  LWLockRelease(ProcArrayLock);
1163 
1164  /*
1165  * If none of the relevant caches overflowed, we know the Xid is not
1166  * running without even looking at pg_subtrans.
1167  */
1168  if (nxids == 0)
1169  {
1171  return false;
1172  }
1173 
1174  /*
1175  * Step 4: have to check pg_subtrans.
1176  *
1177  * At this point, we know it's either a subtransaction of one of the Xids
1178  * in xids[], or it's not running. If it's an already-failed
1179  * subtransaction, we want to say "not running" even though its parent may
1180  * still be running. So first, check pg_xact to see if it's been aborted.
1181  */
1183 
1184  if (TransactionIdDidAbort(xid))
1185  return false;
1186 
1187  /*
1188  * It isn't aborted, so check whether the transaction tree it belongs to
1189  * is still running (or, more precisely, whether it was running when we
1190  * held ProcArrayLock).
1191  */
1192  topxid = SubTransGetTopmostTransaction(xid);
1193  Assert(TransactionIdIsValid(topxid));
1194  if (!TransactionIdEquals(topxid, xid))
1195  {
1196  for (i = 0; i < nxids; i++)
1197  {
1198  if (TransactionIdEquals(xids[i], topxid))
1199  return true;
1200  }
1201  }
1202 
1203  return false;
1204 }
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
Definition: proc.h:207
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:773
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:146
PGPROC * MyProc
Definition: proc.c:67
TransactionId xid
Definition: proc.h:209
#define xc_by_my_xact_inc()
Definition: procarray.c:145
int errcode(int sqlerrcode)
Definition: elog.c:575
TransactionId RecentXmin
Definition: snapmgr.c:165
bool RecoveryInProgress(void)
Definition: xlog.c:7874
#define xc_by_child_xid_inc()
Definition: procarray.c:148
bool TransactionIdIsKnownCompleted(TransactionId transactionId)
Definition: transam.c:238
#define malloc(a)
Definition: header.h:50
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define xc_by_recent_xmin_inc()
Definition: procarray.c:143
#define xc_by_known_xact_inc()
Definition: procarray.c:144
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
#define ERROR
Definition: elog.h:43
#define xc_by_main_xid_inc()
Definition: procarray.c:147
#define xc_by_known_assigned_inc()
Definition: procarray.c:149
uint8 nxids
Definition: proc.h:223
VariableCache ShmemVariableCache
Definition: varsup.c:34
static PGPROC * allProcs
Definition: procarray.c:100
struct XidCache subxids
Definition: proc.h:158
TransactionId lastOverflowedXid
Definition: procarray.c:87
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:181
#define xc_by_latest_xid_inc()
Definition: procarray.c:146
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static ProcArrayStruct * procArray
Definition: procarray.c:98
static PGXACT * allPgXact
Definition: procarray.c:101
static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
Definition: procarray.c:3790
bool overflowed
Definition: proc.h:219
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:95
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:39
#define TOTAL_MAX_CACHED_SUBXIDS
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define xc_no_overflow_inc()
Definition: procarray.c:150
static bool KnownAssignedXidExists(TransactionId xid)
Definition: procarray.c:3650
#define xc_slow_answer_inc()
Definition: procarray.c:151
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Definition: proc.h:94
TransactionId latestCompletedXid
Definition: transam.h:135
void XidCacheRemoveRunningXids ( TransactionId  xid,
int  nxids,
const TransactionId xids,
TransactionId  latestXid 
)

Definition at line 3004 of file procarray.c.

References Assert, elog, i, VariableCacheData::latestCompletedXid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyPgXact, MyProc, PGXACT::nxids, ShmemVariableCache, PGPROC::subxids, TransactionIdEquals, TransactionIdIsValid, TransactionIdPrecedes(), WARNING, XidCacheRemove, and XidCache::xids.

Referenced by RecordTransactionAbort().

3007 {
3008  int i,
3009  j;
3010 
3012 
3013  /*
3014  * We must hold ProcArrayLock exclusively in order to remove transactions
3015  * from the PGPROC array. (See src/backend/access/transam/README.) It's
3016  * possible this could be relaxed since we know this routine is only used
3017  * to abort subtransactions, but pending closer analysis we'd best be
3018  * conservative.
3019  */
3020  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3021 
3022  /*
3023  * Under normal circumstances xid and xids[] will be in increasing order,
3024  * as will be the entries in subxids. Scan backwards to avoid O(N^2)
3025  * behavior when removing a lot of xids.
3026  */
3027  for (i = nxids - 1; i >= 0; i--)
3028  {
3029  TransactionId anxid = xids[i];
3030 
3031  for (j = MyPgXact->nxids - 1; j >= 0; j--)
3032  {
3033  if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
3034  {
3035  XidCacheRemove(j);
3036  break;
3037  }
3038  }
3039 
3040  /*
3041  * Ordinarily we should have found it, unless the cache has
3042  * overflowed. However it's also possible for this routine to be
3043  * invoked multiple times for the same subtransaction, in case of an
3044  * error during AbortSubTransaction. So instead of Assert, emit a
3045  * debug warning.
3046  */
3047  if (j < 0 && !MyPgXact->overflowed)
3048  elog(WARNING, "did not find subXID %u in MyProc", anxid);
3049  }
3050 
3051  for (j = MyPgXact->nxids - 1; j >= 0; j--)
3052  {
3053  if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
3054  {
3055  XidCacheRemove(j);
3056  break;
3057  }
3058  }
3059  /* Ordinarily we should have found it, unless the cache has overflowed */
3060  if (j < 0 && !MyPgXact->overflowed)
3061  elog(WARNING, "did not find subXID %u in MyProc", xid);
3062 
3063  /* Also advance global latestCompletedXid while holding the lock */
3065  latestXid))
3067 
3068  LWLockRelease(ProcArrayLock);
3069 }
#define XidCacheRemove(i)
Definition: procarray.c:2989
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
PGPROC * MyProc
Definition: proc.c:67
PGXACT * MyPgXact
Definition: proc.c:68
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
uint8 nxids
Definition: proc.h:223
VariableCache ShmemVariableCache
Definition: varsup.c:34
struct XidCache subxids
Definition: proc.h:158
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define WARNING
Definition: elog.h:40
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:39
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int i
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
TransactionId latestCompletedXid
Definition: transam.h:135