68 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
309 #ifdef XIDCACHE_DEBUG
312 static long xc_by_recent_xmin = 0;
313 static long xc_by_known_xact = 0;
314 static long xc_by_my_xact = 0;
315 static long xc_by_latest_xid = 0;
316 static long xc_by_main_xid = 0;
317 static long xc_by_child_xid = 0;
318 static long xc_by_known_assigned = 0;
319 static long xc_no_overflow = 0;
320 static long xc_slow_answer = 0;
322 #define xc_by_recent_xmin_inc() (xc_by_recent_xmin++)
323 #define xc_by_known_xact_inc() (xc_by_known_xact++)
324 #define xc_by_my_xact_inc() (xc_by_my_xact++)
325 #define xc_by_latest_xid_inc() (xc_by_latest_xid++)
326 #define xc_by_main_xid_inc() (xc_by_main_xid++)
327 #define xc_by_child_xid_inc() (xc_by_child_xid++)
328 #define xc_by_known_assigned_inc() (xc_by_known_assigned++)
329 #define xc_no_overflow_inc() (xc_no_overflow++)
330 #define xc_slow_answer_inc() (xc_slow_answer++)
332 static void DisplayXidCache(
void);
335 #define xc_by_recent_xmin_inc() ((void) 0)
336 #define xc_by_known_xact_inc() ((void) 0)
337 #define xc_by_my_xact_inc() ((void) 0)
338 #define xc_by_latest_xid_inc() ((void) 0)
339 #define xc_by_main_xid_inc() ((void) 0)
340 #define xc_by_child_xid_inc() ((void) 0)
341 #define xc_by_known_assigned_inc() ((void) 0)
342 #define xc_no_overflow_inc() ((void) 0)
343 #define xc_slow_answer_inc() ((void) 0)
349 bool exclusive_lock);
381 #define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
399 #define TOTAL_MAX_CACHED_SUBXIDS \
400 ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
487 (
errcode(ERRCODE_TOO_MANY_CONNECTIONS),
488 errmsg(
"sorry, too many clients already")));
508 if (this_procno > pgprocno)
571 #ifdef XIDCACHE_DEBUG
583 Assert(myoff >= 0 && myoff < arrayP->numProcs);
613 movecount = arrayP->
numProcs - myoff - 1;
841 while (extraWaits-- > 0)
1076 advanceNextXid = running->
nextXid;
1125 "recovery snapshots are now enabled");
1129 "recovery snapshot waiting for non-overflowed snapshot or "
1130 "until oldest active xid on standby is at least %u (now %u)",
1184 xids[nxids++] = xid;
1192 elog(
ERROR,
"KnownAssignedXids is not empty");
1211 for (
i = 0;
i < nxids;
i++)
1216 "found duplicated transaction %u for KnownAssignedXids insertion",
1304 elog(
DEBUG1,
"recovery snapshots are now enabled");
1307 "recovery snapshot waiting for non-overflowed snapshot or "
1308 "until oldest active xid on standby is at least %u (now %u)",
1349 for (
i = 0;
i < nsubxids;
i++)
1464 (
errcode(ERRCODE_OUT_OF_MEMORY),
1465 errmsg(
"out of memory")));
1477 latestCompletedXid =
1489 for (
int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
1497 if (pgxactoff == mypgxactoff)
1526 pxids = other_subxidstates[pgxactoff].
count;
1528 pgprocno = arrayP->
pgprocnos[pgxactoff];
1530 for (
j = pxids - 1;
j >= 0;
j--)
1550 if (other_subxidstates[pgxactoff].overflowed)
1551 xids[nxids++] = pxid;
1636 bool result =
false;
1978 rel->
rd_rel->relkind == RELKIND_RELATION ||
1979 rel->
rd_rel->relkind == RELKIND_MATVIEW ||
1980 rel->
rd_rel->relkind == RELKIND_TOASTVALUE);
2097 uint64 curXactCompletionCount;
2137 snapshot->
copied =
false;
2185 bool suboverflowed =
false;
2190 uint64 curXactCompletionCount;
2195 Assert(snapshot != NULL);
2208 if (snapshot->
xip == NULL)
2216 if (snapshot->
xip == NULL)
2218 (
errcode(ERRCODE_OUT_OF_MEMORY),
2219 errmsg(
"out of memory")));
2223 if (snapshot->
subxip == NULL)
2225 (
errcode(ERRCODE_OUT_OF_MEMORY),
2226 errmsg(
"out of memory")));
2243 myxid = other_xids[mypgxactoff];
2275 for (
int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
2295 if (pgxactoff == mypgxactoff)
2318 statusFlags = allStatusFlags[pgxactoff];
2346 if (subxidStates[pgxactoff].overflowed)
2347 suboverflowed =
true;
2350 int nsubxids = subxidStates[pgxactoff].
count;
2354 int pgprocno = pgprocnos[pgxactoff];
2359 memcpy(snapshot->
subxip + subcount,
2362 subcount += nsubxids;
2403 suboverflowed =
true;
2443 def_vis_xid = def_vis_xid_data;
2503 snapshot->
xmin = xmin;
2504 snapshot->
xmax = xmax;
2505 snapshot->
xcnt = count;
2518 snapshot->
copied =
false;
2539 bool result =
false;
2622 bool result =
false;
2721 if (CurrentRunningXacts->
xids == NULL)
2728 if (CurrentRunningXacts->
xids == NULL)
2730 (
errcode(ERRCODE_OUT_OF_MEMORY),
2731 errmsg(
"out of memory")));
2734 xids = CurrentRunningXacts->
xids;
2736 count = subcount = 0;
2737 suboverflowed =
false;
2746 latestCompletedXid =
2748 oldestDatabaseRunningXid = oldestRunningXid =
2774 oldestRunningXid = xid;
2787 oldestDatabaseRunningXid = xid;
2791 suboverflowed =
true;
2801 xids[count++] = xid;
2831 subcount += nsubxids;
2851 CurrentRunningXacts->
xcnt = count - subcount;
2852 CurrentRunningXacts->
subxcnt = subcount;
2865 return CurrentRunningXacts;
2919 oldestRunningXid = xid;
2929 return oldestRunningXid;
2999 if (!recovery_in_progress)
3017 oldestSafeXid = xid;
3023 return oldestSafeXid;
3073 vxids[count++] = vxid;
3095 bool result =
false;
3116 for (
i = 0;
i < nvxids;
i++)
3150 if (result->
pid == 0)
3172 *overflowed =
false;
3236 if (proc->
pid == pid)
3274 if (other_xids[
index] == xid)
3329 bool allDbs,
int excludeVacuum,
3352 if (excludeVacuum & statusFlags)
3374 vxids[count++] = vxid;
3439 (
errcode(ERRCODE_OUT_OF_MEMORY),
3440 errmsg(
"out of memory")));
3475 vxids[count++] = vxid;
3502 bool conflictPending)
3596 return count >= min;
3721 if (proc->
roleId == roleid)
3758 #define MAXAUTOVACPIDS 10
3763 for (tries = 0; tries < 50; tries++)
3771 *nbackends = *nprepared = 0;
3795 autovac_pids[nautovacs++] = proc->
pid;
3811 (
void)
kill(autovac_pids[
index], SIGTERM);
3861 (
errcode(ERRCODE_OBJECT_IN_USE),
3862 errmsg(
"database \"%s\" is being used by prepared transactions",
3865 "There are %d prepared transactions using the database.",
3897 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3898 errmsg(
"permission denied to terminate process"),
3899 errdetail(
"Only roles with the %s attribute may terminate processes of roles with the %s attribute.",
3900 "SUPERUSER",
"SUPERUSER")));
3905 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3906 errmsg(
"permission denied to terminate process"),
3907 errdetail(
"Only roles with privileges of the role whose process is being terminated or with privileges of the \"%s\" role may terminate this process.",
3908 "pg_signal_backend")));
3930 (void)
kill(-pid, SIGTERM);
3932 (void)
kill(pid, SIGTERM);
3948 bool already_locked)
3952 if (!already_locked)
3958 if (!already_locked)
3961 elog(
DEBUG1,
"xmin required by slots: data %u, catalog %u",
3962 xmin, catalog_xmin);
3980 if (catalog_xmin != NULL)
4026 for (
i = nxids - 1;
i >= 0;
i--)
4036 mysubxidstat->
count--;
4049 if (j < 0 && !MyProc->subxidStatus.overflowed)
4050 elog(
WARNING,
"did not find subXID %u in MyProc", anxid);
4059 mysubxidstat->
count--;
4065 if (j < 0 && !MyProc->subxidStatus.overflowed)
4066 elog(
WARNING,
"did not find subXID %u in MyProc", xid);
4077 #ifdef XIDCACHE_DEBUG
4083 DisplayXidCache(
void)
4086 "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
4093 xc_by_known_assigned,
4162 state->definitely_needed))
4336 + (
int32) (xid - rel_xid));
4413 elog(
DEBUG4,
"record known xact %u latestObservedXid %u",
4440 Assert(next_expected_xid == xid);
4655 static unsigned int transactionEndsCounter;
4659 #define KAX_COMPRESS_FREQUENCY 128
4660 #define KAX_COMPRESS_IDLE_INTERVAL 1000
4668 nelements = head - tail;
4699 if (nelements < 2 * pArray->numKnownAssignedXids)
4709 if (lastCompressTs != 0)
4729 for (
i = tail;
i < head;
i++)
4763 bool exclusive_lock)
4779 if (to_xid >= from_xid)
4780 nxids = to_xid - from_xid + 1;
4784 next_xid = from_xid;
4799 Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
4800 Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
4811 elog(
ERROR,
"out-of-order XID insertion in KnownAssignedXids");
4828 elog(
ERROR,
"too many KnownAssignedXids");
4832 next_xid = from_xid;
4833 for (
i = 0;
i < nxids;
i++)
4850 if (!exclusive_lock)
4873 int result_index = -1;
4891 while (first <= last)
4896 mid_index = (first + last) / 2;
4901 result_index = mid_index;
4905 last = mid_index - 1;
4907 first = mid_index + 1;
4910 if (result_index < 0)
4927 if (result_index == tail)
4971 elog(
DEBUG4,
"remove KnownAssignedXid %u", xid);
5001 for (
i = 0;
i < nsubxids;
i++)
5025 elog(
DEBUG4,
"removing all KnownAssignedXids");
5031 elog(
DEBUG4,
"prune KnownAssignedXids to %u", removeXid);
5040 for (
i = tail;
i < head;
i++)
5063 for (
i = tail;
i < head;
i++)
5127 for (
i = tail;
i < head;
i++)
5151 xarray[count++] = knownXid;
5177 for (
i = tail;
i < head;
i++)
5212 for (
i = tail;
i < head;
i++)
5221 elog(trace_level,
"%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
bool has_privs_of_role(Oid member, Oid role)
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
#define pg_read_barrier()
#define pg_write_barrier()
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
TimestampTz GetCurrentTimestamp(void)
#define Assert(condition)
#define FLEXIBLE_ARRAY_MEMBER
#define OidIsValid(objectId)
bool IsCatalogRelation(Relation relation)
char * get_database_name(Oid dbid)
int errdetail(const char *fmt,...)
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
List * lappend_int(List *list, int datum)
#define VirtualTransactionIdIsValid(vxid)
#define GET_VXID_FROM_PGPROC(vxid_dst, proc)
#define InvalidLocalTransactionId
#define VirtualTransactionIdEquals(vxid1, vxid2)
bool LWLockHeldByMe(LWLock *lock)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
void pfree(void *pointer)
#define AmStartupProcess()
#define IsBootstrapProcessingMode()
#define CHECK_FOR_INTERRUPTS()
static bool pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
#define qsort(a, b, c, d)
void PGSemaphoreUnlock(PGSemaphore sema)
void PGSemaphoreLock(PGSemaphore sema)
#define PROC_IN_LOGICAL_DECODING
#define NUM_AUXILIARY_PROCS
#define PROC_AFFECTS_ALL_HORIZONS
#define GetPGProcByNumber(n)
#define GetNumberFromPGProc(proc)
#define PROC_VACUUM_STATE_MASK
#define PROC_IS_AUTOVACUUM
@ KAX_STARTUP_PROCESS_IDLE
static GlobalVisState GlobalVisDataRels
bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid)
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
#define TOTAL_MAX_CACHED_SUBXIDS
static GlobalVisState GlobalVisSharedRels
void ProcArrayGetReplicationSlotXmin(TransactionId *xmin, TransactionId *catalog_xmin)
static GlobalVisState GlobalVisCatalogRels
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid)
bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid)
static void KnownAssignedXidsCompress(KAXCompressReason reason, bool haveLock)
pid_t SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode, bool conflictPending)
Size ProcArrayShmemSize(void)
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
void XidCacheRemoveRunningXids(TransactionId xid, int nxids, const TransactionId *xids, TransactionId latestXid)
bool TransactionIdIsActive(TransactionId xid)
static FullTransactionId FullXidRelativeTo(FullTransactionId rel, TransactionId xid)
bool MinimumActiveBackends(int min)
void TerminateOtherDBBackends(Oid databaseId)
#define xc_no_overflow_inc()
static TransactionId standbySnapshotPendingXmin
void ExpireAllKnownAssignedTransactionIds(void)
#define UINT32_ACCESS_ONCE(var)
VirtualTransactionId * GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
RunningTransactions GetRunningTransactionData(void)
TransactionId GetOldestActiveTransactionId(void)
static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids, TransactionId *subxids)
static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin, TransactionId xmax)
#define xc_by_recent_xmin_inc()
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
void ProcNumberGetTransactionIds(ProcNumber procNumber, TransactionId *xid, TransactionId *xmin, int *nsubxid, bool *overflowed)
void RecordKnownAssignedTransactionIds(TransactionId xid)
static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
TransactionId GetOldestTransactionIdConsideredRunning(void)
static TransactionId latestObservedXid
static ProcArrayStruct * procArray
int GetMaxSnapshotSubxidCount(void)
int CountDBConnections(Oid databaseid)
static GlobalVisState GlobalVisTempRels
#define xc_by_my_xact_inc()
#define xc_by_known_assigned_inc()
struct ProcArrayStruct ProcArrayStruct
void CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
#define PROCARRAY_MAXPROCS
void GetReplicationHorizons(TransactionId *xmin, TransactionId *catalog_xmin)
static bool GlobalVisTestShouldUpdate(GlobalVisState *state)
static void ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
static void KnownAssignedXidsRemovePreceding(TransactionId removeXid)
void ProcArrayAdd(PGPROC *proc)
struct ComputeXidHorizonsResult ComputeXidHorizonsResult
static TransactionId * KnownAssignedXids
#define xc_by_child_xid_inc()
pid_t CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
Snapshot GetSnapshotData(Snapshot snapshot)
static bool * KnownAssignedXidsValid
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
static void KnownAssignedXidsRemove(TransactionId xid)
void KnownAssignedTransactionIdsIdleMaintenance(void)
static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons)
int GetMaxSnapshotXidCount(void)
GlobalVisState * GlobalVisTestFor(Relation rel)
int CountDBBackends(Oid databaseid)
bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid)
bool ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
#define KAX_COMPRESS_FREQUENCY
static TransactionId KnownAssignedXidsGetOldestXmin(void)
void ProcArrayApplyRecoveryInfo(RunningTransactions running)
void ProcArrayClearTransaction(PGPROC *proc)
VirtualTransactionId * GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0, bool allDbs, int excludeVacuum, int *nvxids)
int CountUserBackends(Oid roleid)
PGPROC * BackendPidGetProc(int pid)
static TransactionId ComputeXidHorizonsResultLastXmin
static void GlobalVisUpdate(void)
#define xc_slow_answer_inc()
static void KnownAssignedXidsDisplay(int trace_level)
#define xc_by_main_xid_inc()
PGPROC * BackendPidGetProcWithLock(int pid)
static void MaintainLatestCompletedXidRecovery(TransactionId latestXid)
static void ComputeXidHorizons(ComputeXidHorizonsResult *h)
void ProcArrayApplyXidAssignment(TransactionId topxid, int nsubxids, TransactionId *subxids)
static bool KnownAssignedXidExists(TransactionId xid)
void ProcArrayShmemInit(void)
bool CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
int BackendXidGetPid(TransactionId xid)
#define xc_by_latest_xid_inc()
bool IsBackendPid(int pid)
#define xc_by_known_xact_inc()
static bool KnownAssignedXidsSearch(TransactionId xid, bool remove)
static void KnownAssignedXidsReset(void)
static GlobalVisHorizonKind GlobalVisHorizonKindForRel(Relation rel)
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
void ProcArrayInitRecovery(TransactionId initializedUptoXID)
void ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
#define KAX_COMPRESS_IDLE_INTERVAL
static void MaintainLatestCompletedXid(TransactionId latestXid)
static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
void ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids, TransactionId *subxids, TransactionId max_xid)
static TransactionId cachedXidIsNotInProgress
bool ProcArrayInstallImportedXmin(TransactionId xmin, VirtualTransactionId *sourcevxid)
static bool GetSnapshotDataReuse(Snapshot snapshot)
static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid, bool exclusive_lock)
bool TransactionIdIsInProgress(TransactionId xid)
void ExpireOldKnownAssignedTransactionIds(TransactionId xid)
#define INVALID_PROC_NUMBER
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
#define RELATION_IS_LOCAL(relation)
#define RelationIsAccessibleInLogicalDecoding(relation)
Size add_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Size mul_size(Size s1, Size s2)
void pg_usleep(long microsec)
static pg_noinline void Size size
TransactionId TransactionXmin
void StandbyReleaseOldLocks(TransactionId oldxid)
void appendStringInfo(StringInfo str, const char *fmt,...)
void initStringInfo(StringInfo str)
TransactionId slot_catalog_xmin
TransactionId data_oldest_nonremovable
TransactionId temp_oldest_nonremovable
TransactionId shared_oldest_nonremovable
TransactionId oldest_considered_running
FullTransactionId latest_completed
TransactionId catalog_oldest_nonremovable
TransactionId shared_oldest_nonremovable_raw
FullTransactionId definitely_needed
FullTransactionId maybe_needed
bool procArrayGroupMember
pg_atomic_uint32 procArrayGroupNext
bool recoveryConflictPending
XidCacheStatus subxidStatus
TransactionId procArrayGroupMemberXid
XidCacheStatus * subxidStates
pg_atomic_uint32 procArrayGroupFirst
TransactionId replication_slot_xmin
TransactionId replication_slot_catalog_xmin
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
TransactionId lastOverflowedXid
int tailKnownAssignedXids
int headKnownAssignedXids
TransactionId oldestRunningXid
TransactionId oldestDatabaseRunningXid
TransactionId latestCompletedXid
subxids_array_status subxid_status