PostgreSQL Source Code  git master
multixact.c File Reference
#include "postgres.h"
#include "access/multixact.h"
#include "access/slru.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/twophase_rmgr.h"
#include "access/xact.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "funcapi.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "postmaster/autovacuum.h"
#include "storage/lmgr.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
Include dependency graph for multixact.c:

Go to the source code of this file.

Data Structures

struct  MultiXactStateData
 
struct  mXactCacheEnt
 
struct  mxtruncinfo
 

Macros

#define MULTIXACT_OFFSETS_PER_PAGE   (BLCKSZ / sizeof(MultiXactOffset))
 
#define MultiXactIdToOffsetPage(xid)   ((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
 
#define MultiXactIdToOffsetEntry(xid)   ((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
 
#define MultiXactIdToOffsetSegment(xid)   (MultiXactIdToOffsetPage(xid) / SLRU_PAGES_PER_SEGMENT)
 
#define MXACT_MEMBER_BITS_PER_XACT   8
 
#define MXACT_MEMBER_FLAGS_PER_BYTE   1
 
#define MXACT_MEMBER_XACT_BITMASK   ((1 << MXACT_MEMBER_BITS_PER_XACT) - 1)
 
#define MULTIXACT_FLAGBYTES_PER_GROUP   4
 
#define MULTIXACT_MEMBERS_PER_MEMBERGROUP   (MULTIXACT_FLAGBYTES_PER_GROUP * MXACT_MEMBER_FLAGS_PER_BYTE)
 
#define MULTIXACT_MEMBERGROUP_SIZE   (sizeof(TransactionId) * MULTIXACT_MEMBERS_PER_MEMBERGROUP + MULTIXACT_FLAGBYTES_PER_GROUP)
 
#define MULTIXACT_MEMBERGROUPS_PER_PAGE   (BLCKSZ / MULTIXACT_MEMBERGROUP_SIZE)
 
#define MULTIXACT_MEMBERS_PER_PAGE   (MULTIXACT_MEMBERGROUPS_PER_PAGE * MULTIXACT_MEMBERS_PER_MEMBERGROUP)
 
#define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE   ((uint32) ((0xFFFFFFFF % MULTIXACT_MEMBERS_PER_PAGE) + 1))
 
#define MXOffsetToMemberPage(xid)   ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
 
#define MXOffsetToMemberSegment(xid)   (MXOffsetToMemberPage(xid) / SLRU_PAGES_PER_SEGMENT)
 
#define MXOffsetToFlagsOffset(xid)
 
#define MXOffsetToFlagsBitShift(xid)
 
#define MXOffsetToMemberOffset(xid)
 
#define MULTIXACT_MEMBER_SAFE_THRESHOLD   (MaxMultiXactOffset / 2)
 
#define MULTIXACT_MEMBER_DANGER_THRESHOLD   (MaxMultiXactOffset - MaxMultiXactOffset / 4)
 
#define PreviousMultiXactId(xid)   ((xid) == FirstMultiXactId ? MaxMultiXactId : (xid) - 1)
 
#define MultiXactOffsetCtl   (&MultiXactOffsetCtlData)
 
#define MultiXactMemberCtl   (&MultiXactMemberCtlData)
 
#define MaxOldestSlot   (MaxBackends + max_prepared_xacts)
 
#define MAX_CACHE_ENTRIES   256
 
#define debug_elog2(a, b)
 
#define debug_elog3(a, b, c)
 
#define debug_elog4(a, b, c, d)
 
#define debug_elog5(a, b, c, d, e)
 
#define debug_elog6(a, b, c, d, e, f)
 
#define OFFSET_WARN_SEGMENTS   20
 
#define SHARED_MULTIXACT_STATE_SIZE
 

Typedefs

typedef struct MultiXactStateData MultiXactStateData
 
typedef struct mXactCacheEnt mXactCacheEnt
 
typedef struct mxtruncinfo mxtruncinfo
 

Functions

static void MultiXactIdSetOldestVisible (void)
 
static void RecordNewMultiXact (MultiXactId multi, MultiXactOffset offset, int nmembers, MultiXactMember *members)
 
static MultiXactId GetNewMultiXactId (int nmembers, MultiXactOffset *offset)
 
static int mxactMemberComparator (const void *arg1, const void *arg2)
 
static MultiXactId mXactCacheGetBySet (int nmembers, MultiXactMember *members)
 
static int mXactCacheGetById (MultiXactId multi, MultiXactMember **members)
 
static void mXactCachePut (MultiXactId multi, int nmembers, MultiXactMember *members)
 
static char * mxstatus_to_string (MultiXactStatus status)
 
static int ZeroMultiXactOffsetPage (int pageno, bool writeXlog)
 
static int ZeroMultiXactMemberPage (int pageno, bool writeXlog)
 
static bool MultiXactOffsetPagePrecedes (int page1, int page2)
 
static bool MultiXactMemberPagePrecedes (int page1, int page2)
 
static bool MultiXactOffsetPrecedes (MultiXactOffset offset1, MultiXactOffset offset2)
 
static void ExtendMultiXactOffset (MultiXactId multi)
 
static void ExtendMultiXactMember (MultiXactOffset offset, int nmembers)
 
static bool MultiXactOffsetWouldWrap (MultiXactOffset boundary, MultiXactOffset start, uint32 distance)
 
static bool SetOffsetVacuumLimit (bool is_startup)
 
static bool find_multixact_start (MultiXactId multi, MultiXactOffset *result)
 
static void WriteMZeroPageXlogRec (int pageno, uint8 info)
 
static void WriteMTruncateXlogRec (Oid oldestMultiDB, MultiXactId startTruncOff, MultiXactId endTruncOff, MultiXactOffset startTruncMemb, MultiXactOffset endTruncMemb)
 
MultiXactId MultiXactIdCreate (TransactionId xid1, MultiXactStatus status1, TransactionId xid2, MultiXactStatus status2)
 
MultiXactId MultiXactIdExpand (MultiXactId multi, TransactionId xid, MultiXactStatus status)
 
bool MultiXactIdIsRunning (MultiXactId multi, bool isLockOnly)
 
void MultiXactIdSetOldestMember (void)
 
MultiXactId ReadNextMultiXactId (void)
 
void ReadMultiXactIdRange (MultiXactId *oldest, MultiXactId *next)
 
MultiXactId MultiXactIdCreateFromMembers (int nmembers, MultiXactMember *members)
 
int GetMultiXactIdMembers (MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
 
char * mxid_to_string (MultiXactId multi, int nmembers, MultiXactMember *members)
 
void AtEOXact_MultiXact (void)
 
void AtPrepare_MultiXact (void)
 
void PostPrepare_MultiXact (TransactionId xid)
 
void multixact_twophase_recover (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
void multixact_twophase_postcommit (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
void multixact_twophase_postabort (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
Size MultiXactShmemSize (void)
 
void MultiXactShmemInit (void)
 
void BootStrapMultiXact (void)
 
static void MaybeExtendOffsetSlru (void)
 
void StartupMultiXact (void)
 
void TrimMultiXact (void)
 
void MultiXactGetCheckptMulti (bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
 
void CheckPointMultiXact (void)
 
void MultiXactSetNextMXact (MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
 
void SetMultiXactIdLimit (MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
 
void MultiXactAdvanceNextMXact (MultiXactId minMulti, MultiXactOffset minMultiOffset)
 
void MultiXactAdvanceOldest (MultiXactId oldestMulti, Oid oldestMultiDB)
 
MultiXactId GetOldestMultiXactId (void)
 
static bool ReadMultiXactCounts (uint32 *multixacts, MultiXactOffset *members)
 
int MultiXactMemberFreezeThreshold (void)
 
static bool SlruScanDirCbFindEarliest (SlruCtl ctl, char *filename, int segpage, void *data)
 
static void PerformMembersTruncation (MultiXactOffset oldestOffset, MultiXactOffset newOldestOffset)
 
static void PerformOffsetsTruncation (MultiXactId oldestMulti, MultiXactId newOldestMulti)
 
void TruncateMultiXact (MultiXactId newOldestMulti, Oid newOldestMultiDB)
 
bool MultiXactIdPrecedes (MultiXactId multi1, MultiXactId multi2)
 
bool MultiXactIdPrecedesOrEquals (MultiXactId multi1, MultiXactId multi2)
 
void multixact_redo (XLogReaderState *record)
 
Datum pg_get_multixact_members (PG_FUNCTION_ARGS)
 
int multixactoffsetssyncfiletag (const FileTag *ftag, char *path)
 
int multixactmemberssyncfiletag (const FileTag *ftag, char *path)
 

Variables

static SlruCtlData MultiXactOffsetCtlData
 
static SlruCtlData MultiXactMemberCtlData
 
static MultiXactStateDataMultiXactState
 
static MultiXactIdOldestMemberMXactId
 
static MultiXactIdOldestVisibleMXactId
 
static dlist_head MXactCache = DLIST_STATIC_INIT(MXactCache)
 
static int MXactCacheMembers = 0
 
static MemoryContext MXactContext = NULL
 

Macro Definition Documentation

◆ debug_elog2

◆ debug_elog3

◆ debug_elog4

#define debug_elog4 (   a,
  b,
  c,
 
)

◆ debug_elog5

#define debug_elog5 (   a,
  b,
  c,
  d,
  e 
)

Definition at line 336 of file multixact.c.

Referenced by MultiXactIdExpand().

◆ debug_elog6

#define debug_elog6 (   a,
  b,
  c,
  d,
  e,
 
)

Definition at line 337 of file multixact.c.

Referenced by MultiXactGetCheckptMulti().

◆ MAX_CACHE_ENTRIES

#define MAX_CACHE_ENTRIES   256

Definition at line 321 of file multixact.c.

Referenced by mXactCachePut().

◆ MAX_MEMBERS_IN_LAST_MEMBERS_PAGE

#define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE   ((uint32) ((0xFFFFFFFF % MULTIXACT_MEMBERS_PER_PAGE) + 1))

Definition at line 155 of file multixact.c.

Referenced by ExtendMultiXactMember().

◆ MaxOldestSlot

#define MaxOldestSlot   (MaxBackends + max_prepared_xacts)

◆ MULTIXACT_FLAGBYTES_PER_GROUP

#define MULTIXACT_FLAGBYTES_PER_GROUP   4

Definition at line 135 of file multixact.c.

◆ MULTIXACT_MEMBER_DANGER_THRESHOLD

#define MULTIXACT_MEMBER_DANGER_THRESHOLD   (MaxMultiXactOffset - MaxMultiXactOffset / 4)

Definition at line 178 of file multixact.c.

Referenced by MultiXactMemberFreezeThreshold().

◆ MULTIXACT_MEMBER_SAFE_THRESHOLD

#define MULTIXACT_MEMBER_SAFE_THRESHOLD   (MaxMultiXactOffset / 2)

◆ MULTIXACT_MEMBERGROUP_SIZE

#define MULTIXACT_MEMBERGROUP_SIZE   (sizeof(TransactionId) * MULTIXACT_MEMBERS_PER_MEMBERGROUP + MULTIXACT_FLAGBYTES_PER_GROUP)

Definition at line 139 of file multixact.c.

◆ MULTIXACT_MEMBERGROUPS_PER_PAGE

#define MULTIXACT_MEMBERGROUPS_PER_PAGE   (BLCKSZ / MULTIXACT_MEMBERGROUP_SIZE)

Definition at line 141 of file multixact.c.

◆ MULTIXACT_MEMBERS_PER_MEMBERGROUP

#define MULTIXACT_MEMBERS_PER_MEMBERGROUP   (MULTIXACT_FLAGBYTES_PER_GROUP * MXACT_MEMBER_FLAGS_PER_BYTE)

Definition at line 136 of file multixact.c.

◆ MULTIXACT_MEMBERS_PER_PAGE

◆ MULTIXACT_OFFSETS_PER_PAGE

#define MULTIXACT_OFFSETS_PER_PAGE   (BLCKSZ / sizeof(MultiXactOffset))

Definition at line 109 of file multixact.c.

Referenced by MultiXactOffsetPagePrecedes(), MultiXactShmemInit(), and TruncateMultiXact().

◆ MultiXactIdToOffsetEntry

#define MultiXactIdToOffsetEntry (   xid)    ((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)

◆ MultiXactIdToOffsetPage

◆ MultiXactIdToOffsetSegment

#define MultiXactIdToOffsetSegment (   xid)    (MultiXactIdToOffsetPage(xid) / SLRU_PAGES_PER_SEGMENT)

Definition at line 115 of file multixact.c.

Referenced by multixact_redo(), and TruncateMultiXact().

◆ MultiXactMemberCtl

◆ MultiXactOffsetCtl

◆ MXACT_MEMBER_BITS_PER_XACT

#define MXACT_MEMBER_BITS_PER_XACT   8

Definition at line 130 of file multixact.c.

Referenced by RecordNewMultiXact().

◆ MXACT_MEMBER_FLAGS_PER_BYTE

#define MXACT_MEMBER_FLAGS_PER_BYTE   1

Definition at line 131 of file multixact.c.

◆ MXACT_MEMBER_XACT_BITMASK

#define MXACT_MEMBER_XACT_BITMASK   ((1 << MXACT_MEMBER_BITS_PER_XACT) - 1)

Definition at line 132 of file multixact.c.

Referenced by GetMultiXactIdMembers().

◆ MXOffsetToFlagsBitShift

#define MXOffsetToFlagsBitShift (   xid)
Value:
uint32 TransactionId
Definition: c.h:587
#define MULTIXACT_MEMBERS_PER_MEMBERGROUP
Definition: multixact.c:136
#define MXACT_MEMBER_BITS_PER_XACT
Definition: multixact.c:130

Definition at line 167 of file multixact.c.

Referenced by ExtendMultiXactMember(), GetMultiXactIdMembers(), and RecordNewMultiXact().

◆ MXOffsetToFlagsOffset

#define MXOffsetToFlagsOffset (   xid)
Value:
uint32 TransactionId
Definition: c.h:587
#define MULTIXACT_MEMBERGROUPS_PER_PAGE
Definition: multixact.c:141
#define MULTIXACT_MEMBERS_PER_MEMBERGROUP
Definition: multixact.c:136
#define MULTIXACT_MEMBERGROUP_SIZE
Definition: multixact.c:139

Definition at line 163 of file multixact.c.

Referenced by ExtendMultiXactMember(), GetMultiXactIdMembers(), RecordNewMultiXact(), and TrimMultiXact().

◆ MXOffsetToMemberOffset

#define MXOffsetToMemberOffset (   xid)
Value:
uint32 TransactionId
Definition: c.h:587
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
#define MULTIXACT_MEMBERS_PER_MEMBERGROUP
Definition: multixact.c:136
#define MULTIXACT_FLAGBYTES_PER_GROUP
Definition: multixact.c:135

Definition at line 172 of file multixact.c.

Referenced by GetMultiXactIdMembers(), RecordNewMultiXact(), and TrimMultiXact().

◆ MXOffsetToMemberPage

#define MXOffsetToMemberPage (   xid)    ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)

◆ MXOffsetToMemberSegment

#define MXOffsetToMemberSegment (   xid)    (MXOffsetToMemberPage(xid) / SLRU_PAGES_PER_SEGMENT)

Definition at line 160 of file multixact.c.

Referenced by multixact_redo(), PerformMembersTruncation(), and TruncateMultiXact().

◆ OFFSET_WARN_SEGMENTS

#define OFFSET_WARN_SEGMENTS   20

Referenced by GetNewMultiXactId().

◆ PreviousMultiXactId

#define PreviousMultiXactId (   xid)    ((xid) == FirstMultiXactId ? MaxMultiXactId : (xid) - 1)

Definition at line 181 of file multixact.c.

Referenced by PerformOffsetsTruncation().

◆ SHARED_MULTIXACT_STATE_SIZE

#define SHARED_MULTIXACT_STATE_SIZE
Value:
add_size(offsetof(MultiXactStateData, perBackendXactIds) + sizeof(MultiXactId), \
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
TransactionId MultiXactId
Definition: c.h:597
#define MaxOldestSlot
Definition: multixact.c:288
#define offsetof(type, field)
Definition: c.h:727

Referenced by MultiXactShmemInit(), and MultiXactShmemSize().

Typedef Documentation

◆ MultiXactStateData

◆ mXactCacheEnt

typedef struct mXactCacheEnt mXactCacheEnt

◆ mxtruncinfo

typedef struct mxtruncinfo mxtruncinfo

Function Documentation

◆ AtEOXact_MultiXact()

void AtEOXact_MultiXact ( void  )

Definition at line 1686 of file multixact.c.

References dlist_init(), InvalidMultiXactId, MXactCacheMembers, MyBackendId, OldestMemberMXactId, and OldestVisibleMXactId.

Referenced by AbortTransaction(), and CommitTransaction().

1687 {
1688  /*
1689  * Reset our OldestMemberMXactId and OldestVisibleMXactId values, both of
1690  * which should only be valid while within a transaction.
1691  *
1692  * We assume that storing a MultiXactId is atomic and so we need not take
1693  * MultiXactGenLock to do this.
1694  */
1697 
1698  /*
1699  * Discard the local MultiXactId cache. Since MXactContext was created as
1700  * a child of TopTransactionContext, we needn't delete it explicitly.
1701  */
1702  MXactContext = NULL;
1704  MXactCacheMembers = 0;
1705 }
BackendId MyBackendId
Definition: globals.c:84
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
static int MXactCacheMembers
Definition: multixact.c:323
#define InvalidMultiXactId
Definition: multixact.h:24
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
static dlist_head MXactCache
Definition: multixact.c:322
static MemoryContext MXactContext
Definition: multixact.c:324

◆ AtPrepare_MultiXact()

void AtPrepare_MultiXact ( void  )

Definition at line 1715 of file multixact.c.

References MultiXactIdIsValid, MyBackendId, OldestMemberMXactId, RegisterTwoPhaseRecord(), and TWOPHASE_RM_MULTIXACT_ID.

Referenced by PrepareTransaction().

1716 {
1717  MultiXactId myOldestMember = OldestMemberMXactId[MyBackendId];
1718 
1719  if (MultiXactIdIsValid(myOldestMember))
1721  &myOldestMember, sizeof(MultiXactId));
1722 }
BackendId MyBackendId
Definition: globals.c:84
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1182
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define TWOPHASE_RM_MULTIXACT_ID
Definition: twophase_rmgr.h:27
TransactionId MultiXactId
Definition: c.h:597

◆ BootStrapMultiXact()

void BootStrapMultiXact ( void  )

Definition at line 1894 of file multixact.c.

References Assert, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactMemberCtl, MultiXactOffsetCtl, SimpleLruWritePage(), ZeroMultiXactMemberPage(), and ZeroMultiXactOffsetPage().

Referenced by BootStrapXLOG().

1895 {
1896  int slotno;
1897 
1898  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1899 
1900  /* Create and zero the first page of the offsets log */
1901  slotno = ZeroMultiXactOffsetPage(0, false);
1902 
1903  /* Make sure it's written out */
1905  Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
1906 
1907  LWLockRelease(MultiXactOffsetSLRULock);
1908 
1909  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
1910 
1911  /* Create and zero the first page of the members log */
1912  slotno = ZeroMultiXactMemberPage(0, false);
1913 
1914  /* Make sure it's written out */
1916  Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
1917 
1918  LWLockRelease(MultiXactMemberSLRULock);
1919 }
static int ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
Definition: multixact.c:1931
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:614
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
static int ZeroMultiXactMemberPage(int pageno, bool writeXlog)
Definition: multixact.c:1947
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ CheckPointMultiXact()

void CheckPointMultiXact ( void  )

Definition at line 2155 of file multixact.c.

References MultiXactMemberCtl, MultiXactOffsetCtl, and SimpleLruWriteAll().

Referenced by CheckPointGuts().

2156 {
2157  TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_START(true);
2158 
2159  /*
2160  * Write dirty MultiXact pages to disk. This may result in sync requests
2161  * queued for later handling by ProcessSyncRequests(), as part of the
2162  * checkpoint.
2163  */
2166 
2167  TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(true);
2168 }
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1156
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ ExtendMultiXactMember()

static void ExtendMultiXactMember ( MultiXactOffset  offset,
int  nmembers 
)
static

Definition at line 2434 of file multixact.c.

References difference(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_MEMBERS_IN_LAST_MEMBERS_PAGE, MaxMultiXactOffset, MULTIXACT_MEMBERS_PER_PAGE, MXOffsetToFlagsBitShift, MXOffsetToFlagsOffset, MXOffsetToMemberPage, and ZeroMultiXactMemberPage().

Referenced by GetNewMultiXactId().

2435 {
2436  /*
2437  * It's possible that the members span more than one page of the members
2438  * file, so we loop to ensure we consider each page. The coding is not
2439  * optimal if the members span several pages, but that seems unusual
2440  * enough to not worry much about.
2441  */
2442  while (nmembers > 0)
2443  {
2444  int flagsoff;
2445  int flagsbit;
2447 
2448  /*
2449  * Only zero when at first entry of a page.
2450  */
2451  flagsoff = MXOffsetToFlagsOffset(offset);
2452  flagsbit = MXOffsetToFlagsBitShift(offset);
2453  if (flagsoff == 0 && flagsbit == 0)
2454  {
2455  int pageno;
2456 
2457  pageno = MXOffsetToMemberPage(offset);
2458 
2459  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
2460 
2461  /* Zero the page and make an XLOG entry about it */
2462  ZeroMultiXactMemberPage(pageno, true);
2463 
2464  LWLockRelease(MultiXactMemberSLRULock);
2465  }
2466 
2467  /*
2468  * Compute the number of items till end of current page. Careful: if
2469  * addition of unsigned ints wraps around, we're at the last page of
2470  * the last segment; since that page holds a different number of items
2471  * than other pages, we need to do it differently.
2472  */
2473  if (offset + MAX_MEMBERS_IN_LAST_MEMBERS_PAGE < offset)
2474  {
2475  /*
2476  * This is the last page of the last segment; we can compute the
2477  * number of items left to allocate in it without modulo
2478  * arithmetic.
2479  */
2480  difference = MaxMultiXactOffset - offset + 1;
2481  }
2482  else
2484 
2485  /*
2486  * Advance to next page, taking care to properly handle the wraparound
2487  * case. OK if nmembers goes negative.
2488  */
2489  nmembers -= difference;
2490  offset += difference;
2491  }
2492 }
#define MaxMultiXactOffset
Definition: multixact.h:30
#define MXOffsetToFlagsBitShift(xid)
Definition: multixact.c:167
#define MULTIXACT_MEMBERS_PER_PAGE
Definition: multixact.c:142
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
unsigned int uint32
Definition: c.h:441
Datum difference(PG_FUNCTION_ARGS)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
static int ZeroMultiXactMemberPage(int pageno, bool writeXlog)
Definition: multixact.c:1947
#define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE
Definition: multixact.c:155

◆ ExtendMultiXactOffset()

static void ExtendMultiXactOffset ( MultiXactId  multi)
static

Definition at line 2404 of file multixact.c.

References FirstMultiXactId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdToOffsetEntry, MultiXactIdToOffsetPage, and ZeroMultiXactOffsetPage().

Referenced by GetNewMultiXactId().

2405 {
2406  int pageno;
2407 
2408  /*
2409  * No work except at first MultiXactId of a page. But beware: just after
2410  * wraparound, the first MultiXactId of page zero is FirstMultiXactId.
2411  */
2412  if (MultiXactIdToOffsetEntry(multi) != 0 &&
2413  multi != FirstMultiXactId)
2414  return;
2415 
2416  pageno = MultiXactIdToOffsetPage(multi);
2417 
2418  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
2419 
2420  /* Zero the page and make an XLOG entry about it */
2421  ZeroMultiXactOffsetPage(pageno, true);
2422 
2423  LWLockRelease(MultiXactOffsetSLRULock);
2424 }
static int ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
Definition: multixact.c:1931
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define FirstMultiXactId
Definition: multixact.h:25
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ find_multixact_start()

static bool find_multixact_start ( MultiXactId  multi,
MultiXactOffset result 
)
static

Definition at line 2734 of file multixact.c.

References Assert, MultiXactStateData::finishedStartup, LWLockRelease(), MultiXactIdToOffsetEntry, MultiXactIdToOffsetPage, MultiXactMemberCtl, MultiXactOffsetCtl, SimpleLruDoesPhysicalPageExist(), SimpleLruReadPage_ReadOnly(), and SimpleLruWriteAll().

Referenced by SetOffsetVacuumLimit(), and TruncateMultiXact().

2735 {
2736  MultiXactOffset offset;
2737  int pageno;
2738  int entryno;
2739  int slotno;
2740  MultiXactOffset *offptr;
2741 
2743 
2744  pageno = MultiXactIdToOffsetPage(multi);
2745  entryno = MultiXactIdToOffsetEntry(multi);
2746 
2747  /*
2748  * Write out dirty data, so PhysicalPageExists can work correctly.
2749  */
2752 
2754  return false;
2755 
2756  /* lock is acquired by SimpleLruReadPage_ReadOnly */
2757  slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, multi);
2758  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
2759  offptr += entryno;
2760  offset = *offptr;
2761  LWLockRelease(MultiXactOffsetSLRULock);
2762 
2763  *result = offset;
2764  return true;
2765 }
uint32 MultiXactOffset
Definition: c.h:599
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1156
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:626
#define MultiXactOffsetCtl
Definition: multixact.c:190
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:495
#define Assert(condition)
Definition: c.h:804
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ GetMultiXactIdMembers()

int GetMultiXactIdMembers ( MultiXactId  multi,
MultiXactMember **  members,
bool  from_pgupgrade,
bool  onlyLock 
)

Definition at line 1223 of file multixact.c.

References Assert, CHECK_FOR_INTERRUPTS, DEBUG2, debug_elog2, debug_elog3, ereport, errcode(), errmsg(), ERROR, FirstMultiXactId, i, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdSetOldestVisible(), MultiXactIdToOffsetEntry, MultiXactIdToOffsetPage, MultiXactMemberCtl, MultiXactOffsetCtl, MXACT_MEMBER_XACT_BITMASK, mXactCacheGetById(), mXactCachePut(), mxid_to_string(), MXOffsetToFlagsBitShift, MXOffsetToFlagsOffset, MXOffsetToMemberOffset, MXOffsetToMemberPage, MyBackendId, MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::oldestMultiXactId, OldestVisibleMXactId, palloc(), pg_usleep(), SimpleLruReadPage(), MultiXactMember::status, TransactionIdIsValid, and MultiXactMember::xid.

Referenced by Do_MultiXactIdWait(), DoesMultiXactIdConflict(), FreezeMultiXactId(), GetMultiXactIdHintBits(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_tuple_needs_freeze(), MultiXactIdExpand(), MultiXactIdGetUpdateXid(), MultiXactIdIsRunning(), pg_get_multixact_members(), and pgrowlocks().

1225 {
1226  int pageno;
1227  int prev_pageno;
1228  int entryno;
1229  int slotno;
1230  MultiXactOffset *offptr;
1231  MultiXactOffset offset;
1232  int length;
1233  int truelength;
1234  int i;
1235  MultiXactId oldestMXact;
1236  MultiXactId nextMXact;
1237  MultiXactId tmpMXact;
1238  MultiXactOffset nextOffset;
1239  MultiXactMember *ptr;
1240 
1241  debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
1242 
1243  if (!MultiXactIdIsValid(multi) || from_pgupgrade)
1244  {
1245  *members = NULL;
1246  return -1;
1247  }
1248 
1249  /* See if the MultiXactId is in the local cache */
1250  length = mXactCacheGetById(multi, members);
1251  if (length >= 0)
1252  {
1253  debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
1254  mxid_to_string(multi, length, *members));
1255  return length;
1256  }
1257 
1258  /* Set our OldestVisibleMXactId[] entry if we didn't already */
1260 
1261  /*
1262  * If we know the multi is used only for locking and not for updates, then
1263  * we can skip checking if the value is older than our oldest visible
1264  * multi. It cannot possibly still be running.
1265  */
1266  if (onlyLock &&
1268  {
1269  debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old");
1270  *members = NULL;
1271  return -1;
1272  }
1273 
1274  /*
1275  * We check known limits on MultiXact before resorting to the SLRU area.
1276  *
1277  * An ID older than MultiXactState->oldestMultiXactId cannot possibly be
1278  * useful; it has already been removed, or will be removed shortly, by
1279  * truncation. If one is passed, an error is raised.
1280  *
1281  * Also, an ID >= nextMXact shouldn't ever be seen here; if it is seen, it
1282  * implies undetected ID wraparound has occurred. This raises a hard
1283  * error.
1284  *
1285  * Shared lock is enough here since we aren't modifying any global state.
1286  * Acquire it just long enough to grab the current counter values. We may
1287  * need both nextMXact and nextOffset; see below.
1288  */
1289  LWLockAcquire(MultiXactGenLock, LW_SHARED);
1290 
1291  oldestMXact = MultiXactState->oldestMultiXactId;
1292  nextMXact = MultiXactState->nextMXact;
1293  nextOffset = MultiXactState->nextOffset;
1294 
1295  LWLockRelease(MultiXactGenLock);
1296 
1297  if (MultiXactIdPrecedes(multi, oldestMXact))
1298  ereport(ERROR,
1299  (errcode(ERRCODE_INTERNAL_ERROR),
1300  errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
1301  multi)));
1302 
1303  if (!MultiXactIdPrecedes(multi, nextMXact))
1304  ereport(ERROR,
1305  (errcode(ERRCODE_INTERNAL_ERROR),
1306  errmsg("MultiXactId %u has not been created yet -- apparent wraparound",
1307  multi)));
1308 
1309  /*
1310  * Find out the offset at which we need to start reading MultiXactMembers
1311  * and the number of members in the multixact. We determine the latter as
1312  * the difference between this multixact's starting offset and the next
1313  * one's. However, there are some corner cases to worry about:
1314  *
1315  * 1. This multixact may be the latest one created, in which case there is
1316  * no next one to look at. In this case the nextOffset value we just
1317  * saved is the correct endpoint.
1318  *
1319  * 2. The next multixact may still be in process of being filled in: that
1320  * is, another process may have done GetNewMultiXactId but not yet written
1321  * the offset entry for that ID. In that scenario, it is guaranteed that
1322  * the offset entry for that multixact exists (because GetNewMultiXactId
1323  * won't release MultiXactGenLock until it does) but contains zero
1324  * (because we are careful to pre-zero offset pages). Because
1325  * GetNewMultiXactId will never return zero as the starting offset for a
1326  * multixact, when we read zero as the next multixact's offset, we know we
1327  * have this case. We sleep for a bit and try again.
1328  *
1329  * 3. Because GetNewMultiXactId increments offset zero to offset one to
1330  * handle case #2, there is an ambiguity near the point of offset
1331  * wraparound. If we see next multixact's offset is one, is that our
1332  * multixact's actual endpoint, or did it end at zero with a subsequent
1333  * increment? We handle this using the knowledge that if the zero'th
1334  * member slot wasn't filled, it'll contain zero, and zero isn't a valid
1335  * transaction ID so it can't be a multixact member. Therefore, if we
1336  * read a zero from the members array, just ignore it.
1337  *
1338  * This is all pretty messy, but the mess occurs only in infrequent corner
1339  * cases, so it seems better than holding the MultiXactGenLock for a long
1340  * time on every multixact creation.
1341  */
1342 retry:
1343  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1344 
1345  pageno = MultiXactIdToOffsetPage(multi);
1346  entryno = MultiXactIdToOffsetEntry(multi);
1347 
1348  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
1349  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1350  offptr += entryno;
1351  offset = *offptr;
1352 
1353  Assert(offset != 0);
1354 
1355  /*
1356  * Use the same increment rule as GetNewMultiXactId(), that is, don't
1357  * handle wraparound explicitly until needed.
1358  */
1359  tmpMXact = multi + 1;
1360 
1361  if (nextMXact == tmpMXact)
1362  {
1363  /* Corner case 1: there is no next multixact */
1364  length = nextOffset - offset;
1365  }
1366  else
1367  {
1368  MultiXactOffset nextMXOffset;
1369 
1370  /* handle wraparound if needed */
1371  if (tmpMXact < FirstMultiXactId)
1372  tmpMXact = FirstMultiXactId;
1373 
1374  prev_pageno = pageno;
1375 
1376  pageno = MultiXactIdToOffsetPage(tmpMXact);
1377  entryno = MultiXactIdToOffsetEntry(tmpMXact);
1378 
1379  if (pageno != prev_pageno)
1380  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, tmpMXact);
1381 
1382  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1383  offptr += entryno;
1384  nextMXOffset = *offptr;
1385 
1386  if (nextMXOffset == 0)
1387  {
1388  /* Corner case 2: next multixact is still being filled in */
1389  LWLockRelease(MultiXactOffsetSLRULock);
1391  pg_usleep(1000L);
1392  goto retry;
1393  }
1394 
1395  length = nextMXOffset - offset;
1396  }
1397 
1398  LWLockRelease(MultiXactOffsetSLRULock);
1399 
1400  ptr = (MultiXactMember *) palloc(length * sizeof(MultiXactMember));
1401 
1402  /* Now get the members themselves. */
1403  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
1404 
1405  truelength = 0;
1406  prev_pageno = -1;
1407  for (i = 0; i < length; i++, offset++)
1408  {
1409  TransactionId *xactptr;
1410  uint32 *flagsptr;
1411  int flagsoff;
1412  int bshift;
1413  int memberoff;
1414 
1415  pageno = MXOffsetToMemberPage(offset);
1416  memberoff = MXOffsetToMemberOffset(offset);
1417 
1418  if (pageno != prev_pageno)
1419  {
1420  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
1421  prev_pageno = pageno;
1422  }
1423 
1424  xactptr = (TransactionId *)
1425  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
1426 
1427  if (!TransactionIdIsValid(*xactptr))
1428  {
1429  /* Corner case 3: we must be looking at unused slot zero */
1430  Assert(offset == 0);
1431  continue;
1432  }
1433 
1434  flagsoff = MXOffsetToFlagsOffset(offset);
1435  bshift = MXOffsetToFlagsBitShift(offset);
1436  flagsptr = (uint32 *) (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
1437 
1438  ptr[truelength].xid = *xactptr;
1439  ptr[truelength].status = (*flagsptr >> bshift) & MXACT_MEMBER_XACT_BITMASK;
1440  truelength++;
1441  }
1442 
1443  LWLockRelease(MultiXactMemberSLRULock);
1444 
1445  /* A multixid with zero members should not happen */
1446  Assert(truelength > 0);
1447 
1448  /*
1449  * Copy the result into the local cache.
1450  */
1451  mXactCachePut(multi, truelength, ptr);
1452 
1453  debug_elog3(DEBUG2, "GetMembers: no cache for %s",
1454  mxid_to_string(multi, truelength, ptr));
1455  *members = ptr;
1456  return truelength;
1457 }
MultiXactId nextMXact
Definition: multixact.c:203
BackendId MyBackendId
Definition: globals.c:84
#define MXOffsetToFlagsBitShift(xid)
Definition: multixact.c:167
uint32 MultiXactOffset
Definition: c.h:599
uint32 TransactionId
Definition: c.h:587
MultiXactOffset nextOffset
Definition: multixact.c:206
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
void pg_usleep(long microsec)
Definition: signal.c:53
#define ERROR
Definition: elog.h:46
TransactionId xid
Definition: multixact.h:62
#define MXOffsetToMemberOffset(xid)
Definition: multixact.c:172
#define MXACT_MEMBER_XACT_BITMASK
Definition: multixact.c:132
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:395
#define DEBUG2
Definition: elog.h:24
MultiXactStatus status
Definition: multixact.h:63
unsigned int uint32
Definition: c.h:441
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define debug_elog2(a, b)
Definition: multixact.c:333
#define FirstMultiXactId
Definition: multixact.h:25
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define debug_elog3(a, b, c)
Definition: multixact.c:334
#define ereport(elevel,...)
Definition: elog.h:157
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804
static void MultiXactIdSetOldestVisible(void)
Definition: multixact.c:682
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
static void mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1585
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
static int mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
Definition: multixact.c:1539
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ GetNewMultiXactId()

static MultiXactId GetNewMultiXactId ( int  nmembers,
MultiXactOffset offset 
)
static

Definition at line 956 of file multixact.c.

References DEBUG2, debug_elog3, debug_elog4, elog, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), errmsg_plural(), ERROR, ExtendMultiXactMember(), ExtendMultiXactOffset(), FirstMultiXactId, get_database_name(), IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactStateData::multiStopLimit, MultiXactStateData::multiVacLimit, MultiXactStateData::multiWarnLimit, MultiXactStateData::multiWrapLimit, MULTIXACT_MEMBER_SAFE_THRESHOLD, MULTIXACT_MEMBERS_PER_PAGE, MultiXactIdPrecedes(), MultiXactOffsetWouldWrap(), MXOffsetToMemberPage, MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, OFFSET_WARN_SEGMENTS, MultiXactStateData::offsetStopLimit, MultiXactStateData::oldestMultiXactDB, MultiXactStateData::oldestOffset, MultiXactStateData::oldestOffsetKnown, PMSIGNAL_START_AUTOVAC_LAUNCHER, RecoveryInProgress(), SendPostmasterSignal(), SLRU_PAGES_PER_SEGMENT, START_CRIT_SECTION, and WARNING.

Referenced by MultiXactIdCreateFromMembers().

957 {
958  MultiXactId result;
959  MultiXactOffset nextOffset;
960 
961  debug_elog3(DEBUG2, "GetNew: for %d xids", nmembers);
962 
963  /* safety check, we should never get this far in a HS standby */
964  if (RecoveryInProgress())
965  elog(ERROR, "cannot assign MultiXactIds during recovery");
966 
967  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
968 
969  /* Handle wraparound of the nextMXact counter */
972 
973  /* Assign the MXID */
974  result = MultiXactState->nextMXact;
975 
976  /*----------
977  * Check to see if it's safe to assign another MultiXactId. This protects
978  * against catastrophic data loss due to multixact wraparound. The basic
979  * rules are:
980  *
981  * If we're past multiVacLimit or the safe threshold for member storage
982  * space, or we don't know what the safe threshold for member storage is,
983  * start trying to force autovacuum cycles.
984  * If we're past multiWarnLimit, start issuing warnings.
985  * If we're past multiStopLimit, refuse to create new MultiXactIds.
986  *
987  * Note these are pretty much the same protections in GetNewTransactionId.
988  *----------
989  */
991  {
992  /*
993  * For safety's sake, we release MultiXactGenLock while sending
994  * signals, warnings, etc. This is not so much because we care about
995  * preserving concurrency in this situation, as to avoid any
996  * possibility of deadlock while doing get_database_name(). First,
997  * copy all the shared values we'll need in this path.
998  */
999  MultiXactId multiWarnLimit = MultiXactState->multiWarnLimit;
1000  MultiXactId multiStopLimit = MultiXactState->multiStopLimit;
1001  MultiXactId multiWrapLimit = MultiXactState->multiWrapLimit;
1002  Oid oldest_datoid = MultiXactState->oldestMultiXactDB;
1003 
1004  LWLockRelease(MultiXactGenLock);
1005 
1006  if (IsUnderPostmaster &&
1007  !MultiXactIdPrecedes(result, multiStopLimit))
1008  {
1009  char *oldest_datname = get_database_name(oldest_datoid);
1010 
1011  /*
1012  * Immediately kick autovacuum into action as we're already in
1013  * ERROR territory.
1014  */
1016 
1017  /* complain even if that DB has disappeared */
1018  if (oldest_datname)
1019  ereport(ERROR,
1020  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1021  errmsg("database is not accepting commands that generate new MultiXactIds to avoid wraparound data loss in database \"%s\"",
1022  oldest_datname),
1023  errhint("Execute a database-wide VACUUM in that database.\n"
1024  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1025  else
1026  ereport(ERROR,
1027  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1028  errmsg("database is not accepting commands that generate new MultiXactIds to avoid wraparound data loss in database with OID %u",
1029  oldest_datoid),
1030  errhint("Execute a database-wide VACUUM in that database.\n"
1031  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1032  }
1033 
1034  /*
1035  * To avoid swamping the postmaster with signals, we issue the autovac
1036  * request only once per 64K multis generated. This still gives
1037  * plenty of chances before we get into real trouble.
1038  */
1039  if (IsUnderPostmaster && (result % 65536) == 0)
1041 
1042  if (!MultiXactIdPrecedes(result, multiWarnLimit))
1043  {
1044  char *oldest_datname = get_database_name(oldest_datoid);
1045 
1046  /* complain even if that DB has disappeared */
1047  if (oldest_datname)
1048  ereport(WARNING,
1049  (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
1050  "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
1051  multiWrapLimit - result,
1052  oldest_datname,
1053  multiWrapLimit - result),
1054  errhint("Execute a database-wide VACUUM in that database.\n"
1055  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1056  else
1057  ereport(WARNING,
1058  (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
1059  "database with OID %u must be vacuumed before %u more MultiXactIds are used",
1060  multiWrapLimit - result,
1061  oldest_datoid,
1062  multiWrapLimit - result),
1063  errhint("Execute a database-wide VACUUM in that database.\n"
1064  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1065  }
1066 
1067  /* Re-acquire lock and start over */
1068  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1069  result = MultiXactState->nextMXact;
1070  if (result < FirstMultiXactId)
1071  result = FirstMultiXactId;
1072  }
1073 
1074  /* Make sure there is room for the MXID in the file. */
1075  ExtendMultiXactOffset(result);
1076 
1077  /*
1078  * Reserve the members space, similarly to above. Also, be careful not to
1079  * return zero as the starting offset for any multixact. See
1080  * GetMultiXactIdMembers() for motivation.
1081  */
1082  nextOffset = MultiXactState->nextOffset;
1083  if (nextOffset == 0)
1084  {
1085  *offset = 1;
1086  nmembers++; /* allocate member slot 0 too */
1087  }
1088  else
1089  *offset = nextOffset;
1090 
1091  /*----------
1092  * Protect against overrun of the members space as well, with the
1093  * following rules:
1094  *
1095  * If we're past offsetStopLimit, refuse to generate more multis.
1096  * If we're close to offsetStopLimit, emit a warning.
1097  *
1098  * Arbitrarily, we start emitting warnings when we're 20 segments or less
1099  * from offsetStopLimit.
1100  *
1101  * Note we haven't updated the shared state yet, so if we fail at this
1102  * point, the multixact ID we grabbed can still be used by the next guy.
1103  *
1104  * Note that there is no point in forcing autovacuum runs here: the
1105  * multixact freeze settings would have to be reduced for that to have any
1106  * effect.
1107  *----------
1108  */
1109 #define OFFSET_WARN_SEGMENTS 20
1112  nmembers))
1113  {
1114  /* see comment in the corresponding offsets wraparound case */
1116 
1117  ereport(ERROR,
1118  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1119  errmsg("multixact \"members\" limit exceeded"),
1120  errdetail_plural("This command would create a multixact with %u members, but the remaining space is only enough for %u member.",
1121  "This command would create a multixact with %u members, but the remaining space is only enough for %u members.",
1122  MultiXactState->offsetStopLimit - nextOffset - 1,
1123  nmembers,
1124  MultiXactState->offsetStopLimit - nextOffset - 1),
1125  errhint("Execute a database-wide VACUUM in database with OID %u with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.",
1127  }
1128 
1129  /*
1130  * Check whether we should kick autovacuum into action, to prevent members
1131  * wraparound. NB we use a much larger window to trigger autovacuum than
1132  * just the warning limit. The warning is just a measure of last resort -
1133  * this is in line with GetNewTransactionId's behaviour.
1134  */
1138  {
1139  /*
1140  * To avoid swamping the postmaster with signals, we issue the autovac
1141  * request only when crossing a segment boundary. With default
1142  * compilation settings that's roughly after 50k members. This still
1143  * gives plenty of chances before we get into real trouble.
1144  */
1145  if ((MXOffsetToMemberPage(nextOffset) / SLRU_PAGES_PER_SEGMENT) !=
1146  (MXOffsetToMemberPage(nextOffset + nmembers) / SLRU_PAGES_PER_SEGMENT))
1148  }
1149 
1152  nextOffset,
1154  ereport(WARNING,
1155  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1156  errmsg_plural("database with OID %u must be vacuumed before %d more multixact member is used",
1157  "database with OID %u must be vacuumed before %d more multixact members are used",
1158  MultiXactState->offsetStopLimit - nextOffset + nmembers,
1160  MultiXactState->offsetStopLimit - nextOffset + nmembers),
1161  errhint("Execute a database-wide VACUUM in that database with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.")));
1162 
1163  ExtendMultiXactMember(nextOffset, nmembers);
1164 
1165  /*
1166  * Critical section from here until caller has written the data into the
1167  * just-reserved SLRU space; we don't want to error out with a partly
1168  * written MultiXact structure. (In particular, failing to write our
1169  * start offset after advancing nextMXact would effectively corrupt the
1170  * previous MultiXact.)
1171  */
1173 
1174  /*
1175  * Advance counters. As in GetNewTransactionId(), this must not happen
1176  * until after file extension has succeeded!
1177  *
1178  * We don't care about MultiXactId wraparound here; it will be handled by
1179  * the next iteration. But note that nextMXact may be InvalidMultiXactId
1180  * or the first value on a segment-beginning page after this routine
1181  * exits, so anyone else looking at the variable must be prepared to deal
1182  * with either case. Similarly, nextOffset may be zero, but we won't use
1183  * that as the actual start offset of the next multixact.
1184  */
1186 
1187  MultiXactState->nextOffset += nmembers;
1188 
1189  LWLockRelease(MultiXactGenLock);
1190 
1191  debug_elog4(DEBUG2, "GetNew: returning %u offset %u", result, *offset);
1192  return result;
1193 }
MultiXactId nextMXact
Definition: multixact.c:203
int errhint(const char *fmt,...)
Definition: elog.c:1156
uint32 MultiXactOffset
Definition: c.h:599
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1019
MultiXactOffset nextOffset
Definition: multixact.c:206
#define MULTIXACT_MEMBERS_PER_PAGE
Definition: multixact.c:142
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:8328
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
static bool MultiXactOffsetWouldWrap(MultiXactOffset boundary, MultiXactOffset start, uint32 distance)
Definition: multixact.c:2686
#define ERROR
Definition: elog.h:46
MultiXactId multiVacLimit
Definition: multixact.c:228
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2113
#define DEBUG2
Definition: elog.h:24
bool IsUnderPostmaster
Definition: globals.c:112
MultiXactId multiStopLimit
Definition: multixact.c:230
#define FirstMultiXactId
Definition: multixact.h:25
#define WARNING
Definition: elog.h:40
MultiXactOffset offsetStopLimit
Definition: multixact.c:234
MultiXactOffset oldestOffset
Definition: multixact.c:224
#define debug_elog3(a, b, c)
Definition: multixact.c:334
#define OFFSET_WARN_SEGMENTS
static void ExtendMultiXactOffset(MultiXactId multi)
Definition: multixact.c:2404
#define ereport(elevel,...)
Definition: elog.h:157
TransactionId MultiXactId
Definition: c.h:597
MultiXactId multiWrapLimit
Definition: multixact.c:231
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1134
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
MultiXactId multiWarnLimit
Definition: multixact.c:229
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:153
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
#define MULTIXACT_MEMBER_SAFE_THRESHOLD
Definition: multixact.c:177
static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers)
Definition: multixact.c:2434

◆ GetOldestMultiXactId()

MultiXactId GetOldestMultiXactId ( void  )

Definition at line 2506 of file multixact.c.

References FirstMultiXactId, i, LW_SHARED, LWLockAcquire(), LWLockRelease(), MaxOldestSlot, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactStateData::nextMXact, OldestMemberMXactId, and OldestVisibleMXactId.

Referenced by heapam_relation_set_new_filenode(), vac_update_datfrozenxid(), and vacuum_set_xid_limits().

2507 {
2508  MultiXactId oldestMXact;
2509  MultiXactId nextMXact;
2510  int i;
2511 
2512  /*
2513  * This is the oldest valid value among all the OldestMemberMXactId[] and
2514  * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
2515  */
2516  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2517 
2518  /*
2519  * We have to beware of the possibility that nextMXact is in the
2520  * wrapped-around state. We don't fix the counter itself here, but we
2521  * must be sure to use a valid value in our calculation.
2522  */
2523  nextMXact = MultiXactState->nextMXact;
2524  if (nextMXact < FirstMultiXactId)
2525  nextMXact = FirstMultiXactId;
2526 
2527  oldestMXact = nextMXact;
2528  for (i = 1; i <= MaxOldestSlot; i++)
2529  {
2530  MultiXactId thisoldest;
2531 
2532  thisoldest = OldestMemberMXactId[i];
2533  if (MultiXactIdIsValid(thisoldest) &&
2534  MultiXactIdPrecedes(thisoldest, oldestMXact))
2535  oldestMXact = thisoldest;
2536  thisoldest = OldestVisibleMXactId[i];
2537  if (MultiXactIdIsValid(thisoldest) &&
2538  MultiXactIdPrecedes(thisoldest, oldestMXact))
2539  oldestMXact = thisoldest;
2540  }
2541 
2542  LWLockRelease(MultiXactGenLock);
2543 
2544  return oldestMXact;
2545 }
MultiXactId nextMXact
Definition: multixact.c:203
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
int i
#define MaxOldestSlot
Definition: multixact.c:288

◆ MaybeExtendOffsetSlru()

static void MaybeExtendOffsetSlru ( void  )
static

Definition at line 1975 of file multixact.c.

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdToOffsetPage, MultiXactOffsetCtl, MultiXactStateData::nextMXact, SimpleLruDoesPhysicalPageExist(), SimpleLruWritePage(), and ZeroMultiXactOffsetPage().

Referenced by MultiXactSetNextMXact().

1976 {
1977  int pageno;
1978 
1980 
1981  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1982 
1984  {
1985  int slotno;
1986 
1987  /*
1988  * Fortunately for us, SimpleLruWritePage is already prepared to deal
1989  * with creating a new segment file even if the page we're writing is
1990  * not the first in it, so this is enough.
1991  */
1992  slotno = ZeroMultiXactOffsetPage(pageno, false);
1994  }
1995 
1996  LWLockRelease(MultiXactOffsetSLRULock);
1997 }
MultiXactId nextMXact
Definition: multixact.c:203
static int ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
Definition: multixact.c:1931
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:626
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:614
#define MultiXactOffsetCtl
Definition: multixact.c:190
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ multixact_redo()

void multixact_redo ( XLogReaderState record)

Definition at line 3236 of file multixact.c.

References AdvanceNextFullTransactionIdPastXid(), Assert, DEBUG1, elog, xl_multixact_truncate::endTruncMemb, xl_multixact_truncate::endTruncOff, i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_multixact_create::members, xl_multixact_create::mid, xl_multixact_create::moff, MultiXactAdvanceNextMXact(), MultiXactIdToOffsetPage, MultiXactIdToOffsetSegment, MultiXactMemberCtl, MultiXactOffsetCtl, MXOffsetToMemberSegment, xl_multixact_create::nmembers, xl_multixact_truncate::oldestMultiDB, PANIC, PerformMembersTruncation(), PerformOffsetsTruncation(), RecordNewMultiXact(), SetMultiXactIdLimit(), SimpleLruWritePage(), SizeOfMultiXactTruncate, xl_multixact_truncate::startTruncMemb, xl_multixact_truncate::startTruncOff, TransactionIdPrecedes(), MultiXactMember::xid, XLOG_MULTIXACT_CREATE_ID, XLOG_MULTIXACT_TRUNCATE_ID, XLOG_MULTIXACT_ZERO_MEM_PAGE, XLOG_MULTIXACT_ZERO_OFF_PAGE, XLogRecGetData, XLogRecGetInfo, XLogRecGetXid, XLogRecHasAnyBlockRefs, XLR_INFO_MASK, ZeroMultiXactMemberPage(), and ZeroMultiXactOffsetPage().

3237 {
3238  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
3239 
3240  /* Backup blocks are not used in multixact records */
3241  Assert(!XLogRecHasAnyBlockRefs(record));
3242 
3243  if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
3244  {
3245  int pageno;
3246  int slotno;
3247 
3248  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
3249 
3250  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
3251 
3252  slotno = ZeroMultiXactOffsetPage(pageno, false);
3254  Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
3255 
3256  LWLockRelease(MultiXactOffsetSLRULock);
3257  }
3258  else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
3259  {
3260  int pageno;
3261  int slotno;
3262 
3263  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
3264 
3265  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
3266 
3267  slotno = ZeroMultiXactMemberPage(pageno, false);
3269  Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
3270 
3271  LWLockRelease(MultiXactMemberSLRULock);
3272  }
3273  else if (info == XLOG_MULTIXACT_CREATE_ID)
3274  {
3275  xl_multixact_create *xlrec =
3276  (xl_multixact_create *) XLogRecGetData(record);
3277  TransactionId max_xid;
3278  int i;
3279 
3280  /* Store the data back into the SLRU files */
3281  RecordNewMultiXact(xlrec->mid, xlrec->moff, xlrec->nmembers,
3282  xlrec->members);
3283 
3284  /* Make sure nextMXact/nextOffset are beyond what this record has */
3285  MultiXactAdvanceNextMXact(xlrec->mid + 1,
3286  xlrec->moff + xlrec->nmembers);
3287 
3288  /*
3289  * Make sure nextXid is beyond any XID mentioned in the record. This
3290  * should be unnecessary, since any XID found here ought to have other
3291  * evidence in the XLOG, but let's be safe.
3292  */
3293  max_xid = XLogRecGetXid(record);
3294  for (i = 0; i < xlrec->nmembers; i++)
3295  {
3296  if (TransactionIdPrecedes(max_xid, xlrec->members[i].xid))
3297  max_xid = xlrec->members[i].xid;
3298  }
3299 
3301  }
3302  else if (info == XLOG_MULTIXACT_TRUNCATE_ID)
3303  {
3304  xl_multixact_truncate xlrec;
3305  int pageno;
3306 
3307  memcpy(&xlrec, XLogRecGetData(record),
3309 
3310  elog(DEBUG1, "replaying multixact truncation: "
3311  "offsets [%u, %u), offsets segments [%x, %x), "
3312  "members [%u, %u), members segments [%x, %x)",
3313  xlrec.startTruncOff, xlrec.endTruncOff,
3316  xlrec.startTruncMemb, xlrec.endTruncMemb,
3319 
3320  /* should not be required, but more than cheap enough */
3321  LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
3322 
3323  /*
3324  * Advance the horizon values, so they're current at the end of
3325  * recovery.
3326  */
3327  SetMultiXactIdLimit(xlrec.endTruncOff, xlrec.oldestMultiDB, false);
3328 
3330 
3331  /*
3332  * During XLOG replay, latest_page_number isn't necessarily set up
3333  * yet; insert a suitable value to bypass the sanity test in
3334  * SimpleLruTruncate.
3335  */
3336  pageno = MultiXactIdToOffsetPage(xlrec.endTruncOff);
3337  MultiXactOffsetCtl->shared->latest_page_number = pageno;
3339 
3340  LWLockRelease(MultiXactTruncationLock);
3341  }
3342  else
3343  elog(PANIC, "multixact_redo: unknown op code %u", info);
3344 }
#define DEBUG1
Definition: elog.h:25
uint32 TransactionId
Definition: c.h:587
void AdvanceNextFullTransactionIdPastXid(TransactionId xid)
Definition: varsup.c:277
static int ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
Definition: multixact.c:1931
unsigned char uint8
Definition: c.h:439
#define XLOG_MULTIXACT_ZERO_MEM_PAGE
Definition: multixact.h:73
void MultiXactAdvanceNextMXact(MultiXactId minMulti, MultiXactOffset minMultiOffset)
Definition: multixact.c:2362
#define PANIC
Definition: elog.h:50
#define SizeOfMultiXactTruncate
Definition: multixact.h:100
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.h:82
#define XLogRecGetData(decoder)
Definition: xlogreader.h:320
#define XLOG_MULTIXACT_CREATE_ID
Definition: multixact.h:74
TransactionId xid
Definition: multixact.h:62
MultiXactOffset moff
Definition: multixact.h:80
#define MultiXactIdToOffsetSegment(xid)
Definition: multixact.c:115
static void PerformMembersTruncation(MultiXactOffset oldestOffset, MultiXactOffset newOldestOffset)
Definition: multixact.c:2891
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:614
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:315
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
MultiXactOffset endTruncMemb
Definition: multixact.h:97
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:317
#define XLOG_MULTIXACT_ZERO_OFF_PAGE
Definition: multixact.h:72
MultiXactOffset startTruncMemb
Definition: multixact.h:96
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2213
MultiXactId mid
Definition: multixact.h:79
#define Assert(condition)
Definition: c.h:804
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_MULTIXACT_TRUNCATE_ID
Definition: multixact.h:75
#define MXOffsetToMemberSegment(xid)
Definition: multixact.c:160
MultiXactId startTruncOff
Definition: multixact.h:92
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
static int ZeroMultiXactMemberPage(int pageno, bool writeXlog)
Definition: multixact.c:1947
#define elog(elevel,...)
Definition: elog.h:232
int i
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:322
static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset, int nmembers, MultiXactMember *members)
Definition: multixact.c:862
MultiXactId endTruncOff
Definition: multixact.h:93
static void PerformOffsetsTruncation(MultiXactId oldestMulti, MultiXactId newOldestMulti)
Definition: multixact.c:2919
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ multixact_twophase_postabort()

void multixact_twophase_postabort ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 1815 of file multixact.c.

References multixact_twophase_postcommit().

1817 {
1818  multixact_twophase_postcommit(xid, info, recdata, len);
1819 }
void multixact_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: multixact.c:1800

◆ multixact_twophase_postcommit()

void multixact_twophase_postcommit ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 1800 of file multixact.c.

References Assert, InvalidMultiXactId, OldestMemberMXactId, and TwoPhaseGetDummyBackendId().

Referenced by multixact_twophase_postabort().

1802 {
1803  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, true);
1804 
1805  Assert(len == sizeof(MultiXactId));
1806 
1807  OldestMemberMXactId[dummyBackendId] = InvalidMultiXactId;
1808 }
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
BackendId TwoPhaseGetDummyBackendId(TransactionId xid, bool lock_held)
Definition: twophase.c:859
int BackendId
Definition: backendid.h:21
#define InvalidMultiXactId
Definition: multixact.h:24
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804

◆ multixact_twophase_recover()

void multixact_twophase_recover ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 1779 of file multixact.c.

References Assert, OldestMemberMXactId, and TwoPhaseGetDummyBackendId().

1781 {
1782  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, false);
1783  MultiXactId oldestMember;
1784 
1785  /*
1786  * Get the oldest member XID from the state file record, and set it in the
1787  * OldestMemberMXactId slot reserved for this prepared transaction.
1788  */
1789  Assert(len == sizeof(MultiXactId));
1790  oldestMember = *((MultiXactId *) recdata);
1791 
1792  OldestMemberMXactId[dummyBackendId] = oldestMember;
1793 }
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
BackendId TwoPhaseGetDummyBackendId(TransactionId xid, bool lock_held)
Definition: twophase.c:859
int BackendId
Definition: backendid.h:21
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804

◆ MultiXactAdvanceNextMXact()

void MultiXactAdvanceNextMXact ( MultiXactId  minMulti,
MultiXactOffset  minMultiOffset 
)

Definition at line 2362 of file multixact.c.

References DEBUG2, debug_elog3, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdPrecedes(), MultiXactOffsetPrecedes(), MultiXactStateData::nextMXact, and MultiXactStateData::nextOffset.

Referenced by multixact_redo(), and xlog_redo().

2364 {
2365  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2367  {
2368  debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
2369  MultiXactState->nextMXact = minMulti;
2370  }
2371  if (MultiXactOffsetPrecedes(MultiXactState->nextOffset, minMultiOffset))
2372  {
2373  debug_elog3(DEBUG2, "MultiXact: setting next offset to %u",
2374  minMultiOffset);
2375  MultiXactState->nextOffset = minMultiOffset;
2376  }
2377  LWLockRelease(MultiXactGenLock);
2378 }
MultiXactId nextMXact
Definition: multixact.c:203
MultiXactOffset nextOffset
Definition: multixact.c:206
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define DEBUG2
Definition: elog.h:24
#define debug_elog3(a, b, c)
Definition: multixact.c:334
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
static bool MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2)
Definition: multixact.c:3185

◆ MultiXactAdvanceOldest()

void MultiXactAdvanceOldest ( MultiXactId  oldestMulti,
Oid  oldestMultiDB 
)

Definition at line 2387 of file multixact.c.

References Assert, InRecovery, MultiXactIdPrecedes(), MultiXactStateData::oldestMultiXactId, and SetMultiXactIdLimit().

Referenced by xlog_redo().

2388 {
2389  Assert(InRecovery);
2390 
2392  SetMultiXactIdLimit(oldestMulti, oldestMultiDB, false);
2393 }
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
MultiXactId oldestMultiXactId
Definition: multixact.c:216
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2213
bool InRecovery
Definition: xlogutils.c:52
#define Assert(condition)
Definition: c.h:804
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159

◆ MultiXactGetCheckptMulti()

void MultiXactGetCheckptMulti ( bool  is_shutdown,
MultiXactId nextMulti,
MultiXactOffset nextMultiOffset,
MultiXactId oldestMulti,
Oid oldestMultiDB 
)

Definition at line 2133 of file multixact.c.

References DEBUG2, debug_elog6, LW_SHARED, LWLockAcquire(), LWLockRelease(), MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::oldestMultiXactDB, and MultiXactStateData::oldestMultiXactId.

Referenced by CreateCheckPoint().

2138 {
2139  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2140  *nextMulti = MultiXactState->nextMXact;
2141  *nextMultiOffset = MultiXactState->nextOffset;
2142  *oldestMulti = MultiXactState->oldestMultiXactId;
2143  *oldestMultiDB = MultiXactState->oldestMultiXactDB;
2144  LWLockRelease(MultiXactGenLock);
2145 
2147  "MultiXact: checkpoint is nextMulti %u, nextOffset %u, oldestMulti %u in DB %u",
2148  *nextMulti, *nextMultiOffset, *oldestMulti, *oldestMultiDB);
2149 }
MultiXactId nextMXact
Definition: multixact.c:203
MultiXactOffset nextOffset
Definition: multixact.c:206
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
#define debug_elog6(a, b, c, d, e, f)
Definition: multixact.c:337
#define DEBUG2
Definition: elog.h:24
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ MultiXactIdCreate()

MultiXactId MultiXactIdCreate ( TransactionId  xid1,
MultiXactStatus  status1,
TransactionId  xid2,
MultiXactStatus  status2 
)

Definition at line 386 of file multixact.c.

References Assert, AssertArg, DEBUG2, debug_elog3, MultiXactIdCreateFromMembers(), MultiXactIdIsValid, mxid_to_string(), MyBackendId, OldestMemberMXactId, MultiXactMember::status, TransactionIdEquals, TransactionIdIsValid, and MultiXactMember::xid.

Referenced by compute_new_xmax_infomask().

388 {
389  MultiXactId newMulti;
390  MultiXactMember members[2];
391 
394 
395  Assert(!TransactionIdEquals(xid1, xid2) || (status1 != status2));
396 
397  /* MultiXactIdSetOldestMember() must have been called already. */
399 
400  /*
401  * Note: unlike MultiXactIdExpand, we don't bother to check that both XIDs
402  * are still running. In typical usage, xid2 will be our own XID and the
403  * caller just did a check on xid1, so it'd be wasted effort.
404  */
405 
406  members[0].xid = xid1;
407  members[0].status = status1;
408  members[1].xid = xid2;
409  members[1].status = status2;
410 
411  newMulti = MultiXactIdCreateFromMembers(2, members);
412 
413  debug_elog3(DEBUG2, "Create: %s",
414  mxid_to_string(newMulti, 2, members));
415 
416  return newMulti;
417 }
BackendId MyBackendId
Definition: globals.c:84
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
MultiXactId MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
Definition: multixact.c:767
TransactionId xid
Definition: multixact.h:62
#define DEBUG2
Definition: elog.h:24
MultiXactStatus status
Definition: multixact.h:63
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
#define AssertArg(condition)
Definition: c.h:806
#define debug_elog3(a, b, c)
Definition: multixact.c:334
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ MultiXactIdCreateFromMembers()

MultiXactId MultiXactIdCreateFromMembers ( int  nmembers,
MultiXactMember members 
)

Definition at line 767 of file multixact.c.

References DEBUG2, debug_elog2, debug_elog3, elog, END_CRIT_SECTION, ERROR, GetNewMultiXactId(), i, InvalidMultiXactId, ISUPDATE_from_mxstatus, xl_multixact_create::mid, xl_multixact_create::moff, MultiXactIdIsValid, mXactCacheGetBySet(), mXactCachePut(), mxid_to_string(), xl_multixact_create::nmembers, RecordNewMultiXact(), SizeOfMultiXactCreate, status(), XLOG_MULTIXACT_CREATE_ID, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by FreezeMultiXactId(), MultiXactIdCreate(), and MultiXactIdExpand().

768 {
769  MultiXactId multi;
770  MultiXactOffset offset;
771  xl_multixact_create xlrec;
772 
773  debug_elog3(DEBUG2, "Create: %s",
774  mxid_to_string(InvalidMultiXactId, nmembers, members));
775 
776  /*
777  * See if the same set of members already exists in our cache; if so, just
778  * re-use that MultiXactId. (Note: it might seem that looking in our
779  * cache is insufficient, and we ought to search disk to see if a
780  * duplicate definition already exists. But since we only ever create
781  * MultiXacts containing our own XID, in most cases any such MultiXacts
782  * were in fact created by us, and so will be in our cache. There are
783  * corner cases where someone else added us to a MultiXact without our
784  * knowledge, but it's not worth checking for.)
785  */
786  multi = mXactCacheGetBySet(nmembers, members);
787  if (MultiXactIdIsValid(multi))
788  {
789  debug_elog2(DEBUG2, "Create: in cache!");
790  return multi;
791  }
792 
793  /* Verify that there is a single update Xid among the given members. */
794  {
795  int i;
796  bool has_update = false;
797 
798  for (i = 0; i < nmembers; i++)
799  {
800  if (ISUPDATE_from_mxstatus(members[i].status))
801  {
802  if (has_update)
803  elog(ERROR, "new multixact has more than one updating member");
804  has_update = true;
805  }
806  }
807  }
808 
809  /*
810  * Assign the MXID and offsets range to use, and make sure there is space
811  * in the OFFSETs and MEMBERs files. NB: this routine does
812  * START_CRIT_SECTION().
813  *
814  * Note: unlike MultiXactIdCreate and MultiXactIdExpand, we do not check
815  * that we've called MultiXactIdSetOldestMember here. This is because
816  * this routine is used in some places to create new MultiXactIds of which
817  * the current backend is not a member, notably during freezing of multis
818  * in vacuum. During vacuum, in particular, it would be unacceptable to
819  * keep OldestMulti set, in case it runs for long.
820  */
821  multi = GetNewMultiXactId(nmembers, &offset);
822 
823  /* Make an XLOG entry describing the new MXID. */
824  xlrec.mid = multi;
825  xlrec.moff = offset;
826  xlrec.nmembers = nmembers;
827 
828  /*
829  * XXX Note: there's a lot of padding space in MultiXactMember. We could
830  * find a more compact representation of this Xlog record -- perhaps all
831  * the status flags in one XLogRecData, then all the xids in another one?
832  * Not clear that it's worth the trouble though.
833  */
834  XLogBeginInsert();
835  XLogRegisterData((char *) (&xlrec), SizeOfMultiXactCreate);
836  XLogRegisterData((char *) members, nmembers * sizeof(MultiXactMember));
837 
838  (void) XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_CREATE_ID);
839 
840  /* Now enter the information into the OFFSETs and MEMBERs logs */
841  RecordNewMultiXact(multi, offset, nmembers, members);
842 
843  /* Done with critical section */
845 
846  /* Store the new MultiXactId in the local cache, too */
847  mXactCachePut(multi, nmembers, members);
848 
849  debug_elog2(DEBUG2, "Create: all done");
850 
851  return multi;
852 }
uint32 MultiXactOffset
Definition: c.h:599
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
Definition: multixact.c:956
#define SizeOfMultiXactCreate
Definition: multixact.h:85
#define ERROR
Definition: elog.h:46
#define XLOG_MULTIXACT_CREATE_ID
Definition: multixact.h:74
MultiXactOffset moff
Definition: multixact.h:80
#define DEBUG2
Definition: elog.h:24
#define ISUPDATE_from_mxstatus(status)
Definition: multixact.h:56
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define debug_elog2(a, b)
Definition: multixact.c:333
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:434
#define debug_elog3(a, b, c)
Definition: multixact.c:334
#define InvalidMultiXactId
Definition: multixact.h:24
TransactionId MultiXactId
Definition: c.h:597
MultiXactId mid
Definition: multixact.h:79
static void mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1585
#define elog(elevel,...)
Definition: elog.h:232
int i
static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset, int nmembers, MultiXactMember *members)
Definition: multixact.c:862
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void XLogBeginInsert(void)
Definition: xloginsert.c:135
static MultiXactId mXactCacheGetBySet(int nmembers, MultiXactMember *members)
Definition: multixact.c:1497

◆ MultiXactIdExpand()

MultiXactId MultiXactIdExpand ( MultiXactId  multi,
TransactionId  xid,
MultiXactStatus  status 
)

Definition at line 439 of file multixact.c.

References Assert, AssertArg, DEBUG2, debug_elog3, debug_elog4, debug_elog5, GetMultiXactIdMembers(), i, ISUPDATE_from_mxstatus, MultiXactIdCreateFromMembers(), MultiXactIdIsValid, mxstatus_to_string(), MyBackendId, OldestMemberMXactId, palloc(), pfree(), MultiXactMember::status, status(), TransactionIdDidCommit(), TransactionIdEquals, TransactionIdIsInProgress(), TransactionIdIsValid, and MultiXactMember::xid.

Referenced by compute_new_xmax_infomask().

440 {
441  MultiXactId newMulti;
442  MultiXactMember *members;
443  MultiXactMember *newMembers;
444  int nmembers;
445  int i;
446  int j;
447 
450 
451  /* MultiXactIdSetOldestMember() must have been called already. */
453 
454  debug_elog5(DEBUG2, "Expand: received multi %u, xid %u status %s",
455  multi, xid, mxstatus_to_string(status));
456 
457  /*
458  * Note: we don't allow for old multis here. The reason is that the only
459  * caller of this function does a check that the multixact is no longer
460  * running.
461  */
462  nmembers = GetMultiXactIdMembers(multi, &members, false, false);
463 
464  if (nmembers < 0)
465  {
466  MultiXactMember member;
467 
468  /*
469  * The MultiXactId is obsolete. This can only happen if all the
470  * MultiXactId members stop running between the caller checking and
471  * passing it to us. It would be better to return that fact to the
472  * caller, but it would complicate the API and it's unlikely to happen
473  * too often, so just deal with it by creating a singleton MultiXact.
474  */
475  member.xid = xid;
476  member.status = status;
477  newMulti = MultiXactIdCreateFromMembers(1, &member);
478 
479  debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
480  multi, newMulti);
481  return newMulti;
482  }
483 
484  /*
485  * If the TransactionId is already a member of the MultiXactId with the
486  * same status, just return the existing MultiXactId.
487  */
488  for (i = 0; i < nmembers; i++)
489  {
490  if (TransactionIdEquals(members[i].xid, xid) &&
491  (members[i].status == status))
492  {
493  debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
494  xid, multi);
495  pfree(members);
496  return multi;
497  }
498  }
499 
500  /*
501  * Determine which of the members of the MultiXactId are still of
502  * interest. This is any running transaction, and also any transaction
503  * that grabbed something stronger than just a lock and was committed. (An
504  * update that aborted is of no interest here; and having more than one
505  * update Xid in a multixact would cause errors elsewhere.)
506  *
507  * Removing dead members is not just an optimization: freezing of tuples
508  * whose Xmax are multis depends on this behavior.
509  *
510  * Note we have the same race condition here as above: j could be 0 at the
511  * end of the loop.
512  */
513  newMembers = (MultiXactMember *)
514  palloc(sizeof(MultiXactMember) * (nmembers + 1));
515 
516  for (i = 0, j = 0; i < nmembers; i++)
517  {
518  if (TransactionIdIsInProgress(members[i].xid) ||
519  (ISUPDATE_from_mxstatus(members[i].status) &&
520  TransactionIdDidCommit(members[i].xid)))
521  {
522  newMembers[j].xid = members[i].xid;
523  newMembers[j++].status = members[i].status;
524  }
525  }
526 
527  newMembers[j].xid = xid;
528  newMembers[j++].status = status;
529  newMulti = MultiXactIdCreateFromMembers(j, newMembers);
530 
531  pfree(members);
532  pfree(newMembers);
533 
534  debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
535 
536  return newMulti;
537 }
BackendId MyBackendId
Definition: globals.c:84
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1359
MultiXactId MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
Definition: multixact.c:767
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
void pfree(void *pointer)
Definition: mcxt.c:1169
TransactionId xid
Definition: multixact.h:62
#define DEBUG2
Definition: elog.h:24
MultiXactStatus status
Definition: multixact.h:63
#define ISUPDATE_from_mxstatus(status)
Definition: multixact.h:56
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define AssertArg(condition)
Definition: c.h:806
#define debug_elog3(a, b, c)
Definition: multixact.c:334
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:1062
int i
static char * mxstatus_to_string(MultiXactStatus status)
Definition: multixact.c:1632
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1223
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define debug_elog5(a, b, c, d, e)
Definition: multixact.c:336

◆ MultiXactIdIsRunning()

bool MultiXactIdIsRunning ( MultiXactId  multi,
bool  isLockOnly 
)

Definition at line 551 of file multixact.c.

References DEBUG2, debug_elog2, debug_elog3, debug_elog4, GetMultiXactIdMembers(), i, pfree(), TransactionIdIsCurrentTransactionId(), and TransactionIdIsInProgress().

Referenced by compute_new_xmax_infomask(), FreezeMultiXactId(), HeapTupleSatisfiesUpdate(), and HeapTupleSatisfiesVacuumHorizon().

552 {
553  MultiXactMember *members;
554  int nmembers;
555  int i;
556 
557  debug_elog3(DEBUG2, "IsRunning %u?", multi);
558 
559  /*
560  * "false" here means we assume our callers have checked that the given
561  * multi cannot possibly come from a pg_upgraded database.
562  */
563  nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly);
564 
565  if (nmembers <= 0)
566  {
567  debug_elog2(DEBUG2, "IsRunning: no members");
568  return false;
569  }
570 
571  /*
572  * Checking for myself is cheap compared to looking in shared memory;
573  * return true if any live subtransaction of the current top-level
574  * transaction is a member.
575  *
576  * This is not needed for correctness, it's just a fast path.
577  */
578  for (i = 0; i < nmembers; i++)
579  {
580  if (TransactionIdIsCurrentTransactionId(members[i].xid))
581  {
582  debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
583  pfree(members);
584  return true;
585  }
586  }
587 
588  /*
589  * This could be made faster by having another entry point in procarray.c,
590  * walking the PGPROC array only once for all the members. But in most
591  * cases nmembers should be small enough that it doesn't much matter.
592  */
593  for (i = 0; i < nmembers; i++)
594  {
595  if (TransactionIdIsInProgress(members[i].xid))
596  {
597  debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
598  i, members[i].xid);
599  pfree(members);
600  return true;
601  }
602  }
603 
604  pfree(members);
605 
606  debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
607 
608  return false;
609 }
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:869
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1359
void pfree(void *pointer)
Definition: mcxt.c:1169
#define DEBUG2
Definition: elog.h:24
#define debug_elog2(a, b)
Definition: multixact.c:333
#define debug_elog3(a, b, c)
Definition: multixact.c:334
int i
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1223

◆ MultiXactIdPrecedes()

◆ MultiXactIdPrecedesOrEquals()

bool MultiXactIdPrecedesOrEquals ( MultiXactId  multi1,
MultiXactId  multi2 
)

Definition at line 3173 of file multixact.c.

Referenced by check_mxid_in_range(), heap_vacuum_rel(), and TruncateMultiXact().

3174 {
3175  int32 diff = (int32) (multi1 - multi2);
3176 
3177  return (diff <= 0);
3178 }
signed int int32
Definition: c.h:429

◆ MultiXactIdSetOldestMember()

void MultiXactIdSetOldestMember ( void  )

Definition at line 625 of file multixact.c.

References DEBUG2, debug_elog4, FirstMultiXactId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MultiXactIdIsValid, MyBackendId, MultiXactStateData::nextMXact, and OldestMemberMXactId.

Referenced by heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple(), and heap_update().

626 {
628  {
629  MultiXactId nextMXact;
630 
631  /*
632  * You might think we don't need to acquire a lock here, since
633  * fetching and storing of TransactionIds is probably atomic, but in
634  * fact we do: suppose we pick up nextMXact and then lose the CPU for
635  * a long time. Someone else could advance nextMXact, and then
636  * another someone else could compute an OldestVisibleMXactId that
637  * would be after the value we are going to store when we get control
638  * back. Which would be wrong.
639  *
640  * Note that a shared lock is sufficient, because it's enough to stop
641  * someone from advancing nextMXact; and nobody else could be trying
642  * to write to our OldestMember entry, only reading (and we assume
643  * storing it is atomic.)
644  */
645  LWLockAcquire(MultiXactGenLock, LW_SHARED);
646 
647  /*
648  * We have to beware of the possibility that nextMXact is in the
649  * wrapped-around state. We don't fix the counter itself here, but we
650  * must be sure to store a valid value in our array entry.
651  */
652  nextMXact = MultiXactState->nextMXact;
653  if (nextMXact < FirstMultiXactId)
654  nextMXact = FirstMultiXactId;
655 
656  OldestMemberMXactId[MyBackendId] = nextMXact;
657 
658  LWLockRelease(MultiXactGenLock);
659 
660  debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
661  MyBackendId, nextMXact);
662  }
663 }
MultiXactId nextMXact
Definition: multixact.c:203
BackendId MyBackendId
Definition: globals.c:84
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define DEBUG2
Definition: elog.h:24
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ MultiXactIdSetOldestVisible()

static void MultiXactIdSetOldestVisible ( void  )
static

Definition at line 682 of file multixact.c.

References DEBUG2, debug_elog4, FirstMultiXactId, i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxOldestSlot, MultiXactIdIsValid, MultiXactIdPrecedes(), MyBackendId, MultiXactStateData::nextMXact, OldestMemberMXactId, and OldestVisibleMXactId.

Referenced by GetMultiXactIdMembers().

683 {
685  {
686  MultiXactId oldestMXact;
687  int i;
688 
689  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
690 
691  /*
692  * We have to beware of the possibility that nextMXact is in the
693  * wrapped-around state. We don't fix the counter itself here, but we
694  * must be sure to store a valid value in our array entry.
695  */
696  oldestMXact = MultiXactState->nextMXact;
697  if (oldestMXact < FirstMultiXactId)
698  oldestMXact = FirstMultiXactId;
699 
700  for (i = 1; i <= MaxOldestSlot; i++)
701  {
702  MultiXactId thisoldest = OldestMemberMXactId[i];
703 
704  if (MultiXactIdIsValid(thisoldest) &&
705  MultiXactIdPrecedes(thisoldest, oldestMXact))
706  oldestMXact = thisoldest;
707  }
708 
709  OldestVisibleMXactId[MyBackendId] = oldestMXact;
710 
711  LWLockRelease(MultiXactGenLock);
712 
713  debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
714  MyBackendId, oldestMXact);
715  }
716 }
MultiXactId nextMXact
Definition: multixact.c:203
BackendId MyBackendId
Definition: globals.c:84
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define DEBUG2
Definition: elog.h:24
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
int i
#define MaxOldestSlot
Definition: multixact.c:288

◆ MultiXactMemberFreezeThreshold()

int MultiXactMemberFreezeThreshold ( void  )

Definition at line 2828 of file multixact.c.

References autovacuum_multixact_freeze_max_age, MULTIXACT_MEMBER_DANGER_THRESHOLD, MULTIXACT_MEMBER_SAFE_THRESHOLD, and ReadMultiXactCounts().

Referenced by do_autovacuum(), do_start_worker(), and vacuum_set_xid_limits().

2829 {
2830  MultiXactOffset members;
2831  uint32 multixacts;
2832  uint32 victim_multixacts;
2833  double fraction;
2834 
2835  /* If we can't determine member space utilization, assume the worst. */
2836  if (!ReadMultiXactCounts(&multixacts, &members))
2837  return 0;
2838 
2839  /* If member space utilization is low, no special action is required. */
2840  if (members <= MULTIXACT_MEMBER_SAFE_THRESHOLD)
2842 
2843  /*
2844  * Compute a target for relminmxid advancement. The number of multixacts
2845  * we try to eliminate from the system is based on how far we are past
2846  * MULTIXACT_MEMBER_SAFE_THRESHOLD.
2847  */
2848  fraction = (double) (members - MULTIXACT_MEMBER_SAFE_THRESHOLD) /
2850  victim_multixacts = multixacts * fraction;
2851 
2852  /* fraction could be > 1.0, but lowest possible freeze age is zero */
2853  if (victim_multixacts > multixacts)
2854  return 0;
2855  return multixacts - victim_multixacts;
2856 }
#define MULTIXACT_MEMBER_DANGER_THRESHOLD
Definition: multixact.c:178
uint32 MultiXactOffset
Definition: c.h:599
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:125
unsigned int uint32
Definition: c.h:441
static bool ReadMultiXactCounts(uint32 *multixacts, MultiXactOffset *members)
Definition: multixact.c:2772
#define MULTIXACT_MEMBER_SAFE_THRESHOLD
Definition: multixact.c:177

◆ MultiXactMemberPagePrecedes()

static bool MultiXactMemberPagePrecedes ( int  page1,
int  page2 
)
static

Definition at line 3139 of file multixact.c.

References MULTIXACT_MEMBERS_PER_PAGE, and MultiXactOffsetPrecedes().

Referenced by MultiXactShmemInit().

3140 {
3141  MultiXactOffset offset1;
3142  MultiXactOffset offset2;
3143 
3144  offset1 = ((MultiXactOffset) page1) * MULTIXACT_MEMBERS_PER_PAGE;
3145  offset2 = ((MultiXactOffset) page2) * MULTIXACT_MEMBERS_PER_PAGE;
3146 
3147  return (MultiXactOffsetPrecedes(offset1, offset2) &&
3148  MultiXactOffsetPrecedes(offset1,
3149  offset2 + MULTIXACT_MEMBERS_PER_PAGE - 1));
3150 }
uint32 MultiXactOffset
Definition: c.h:599
#define MULTIXACT_MEMBERS_PER_PAGE
Definition: multixact.c:142
static bool MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2)
Definition: multixact.c:3185

◆ multixactmemberssyncfiletag()

int multixactmemberssyncfiletag ( const FileTag ftag,
char *  path 
)

Definition at line 3424 of file multixact.c.

References MultiXactMemberCtl, and SlruSyncFileTag().

3425 {
3426  return SlruSyncFileTag(MultiXactMemberCtl, ftag, path);
3427 }
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1593
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ MultiXactOffsetPagePrecedes()

static bool MultiXactOffsetPagePrecedes ( int  page1,
int  page2 
)
static

Definition at line 3119 of file multixact.c.

References FirstMultiXactId, MULTIXACT_OFFSETS_PER_PAGE, and MultiXactIdPrecedes().

Referenced by MultiXactShmemInit().

3120 {
3121  MultiXactId multi1;
3122  MultiXactId multi2;
3123 
3124  multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
3125  multi1 += FirstMultiXactId + 1;
3126  multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
3127  multi2 += FirstMultiXactId + 1;
3128 
3129  return (MultiXactIdPrecedes(multi1, multi2) &&
3130  MultiXactIdPrecedes(multi1,
3131  multi2 + MULTIXACT_OFFSETS_PER_PAGE - 1));
3132 }
#define FirstMultiXactId
Definition: multixact.h:25
#define MULTIXACT_OFFSETS_PER_PAGE
Definition: multixact.c:109
TransactionId MultiXactId
Definition: c.h:597
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159

◆ MultiXactOffsetPrecedes()

static bool MultiXactOffsetPrecedes ( MultiXactOffset  offset1,
MultiXactOffset  offset2 
)
static

Definition at line 3185 of file multixact.c.

Referenced by MultiXactAdvanceNextMXact(), and MultiXactMemberPagePrecedes().

3186 {
3187  int32 diff = (int32) (offset1 - offset2);
3188 
3189  return (diff < 0);
3190 }
signed int int32
Definition: c.h:429

◆ multixactoffsetssyncfiletag()

int multixactoffsetssyncfiletag ( const FileTag ftag,
char *  path 
)

Definition at line 3415 of file multixact.c.

References MultiXactOffsetCtl, and SlruSyncFileTag().

3416 {
3417  return SlruSyncFileTag(MultiXactOffsetCtl, ftag, path);
3418 }
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1593
#define MultiXactOffsetCtl
Definition: multixact.c:190

◆ MultiXactOffsetWouldWrap()

static bool MultiXactOffsetWouldWrap ( MultiXactOffset  boundary,
MultiXactOffset  start,
uint32  distance 
)
static

Definition at line 2686 of file multixact.c.

Referenced by GetNewMultiXactId().

2688 {
2689  MultiXactOffset finish;
2690 
2691  /*
2692  * Note that offset number 0 is not used (see GetMultiXactIdMembers), so
2693  * if the addition wraps around the UINT_MAX boundary, skip that value.
2694  */
2695  finish = start + distance;
2696  if (finish < start)
2697  finish++;
2698 
2699  /*-----------------------------------------------------------------------
2700  * When the boundary is numerically greater than the starting point, any
2701  * value numerically between the two is not wrapped:
2702  *
2703  * <----S----B---->
2704  * [---) = F wrapped past B (and UINT_MAX)
2705  * [---) = F not wrapped
2706  * [----] = F wrapped past B
2707  *
2708  * When the boundary is numerically less than the starting point (i.e. the
2709  * UINT_MAX wraparound occurs somewhere in between) then all values in
2710  * between are wrapped:
2711  *
2712  * <----B----S---->
2713  * [---) = F not wrapped past B (but wrapped past UINT_MAX)
2714  * [---) = F wrapped past B (and UINT_MAX)
2715  * [----] = F not wrapped
2716  *-----------------------------------------------------------------------
2717  */
2718  if (start < boundary)
2719  return finish >= boundary || finish < start;
2720  else
2721  return finish >= boundary && finish < start;
2722 }
uint32 MultiXactOffset
Definition: c.h:599

◆ MultiXactSetNextMXact()

void MultiXactSetNextMXact ( MultiXactId  nextMulti,
MultiXactOffset  nextMultiOffset 
)

Definition at line 2179 of file multixact.c.

References DEBUG2, debug_elog4, IsBinaryUpgrade, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaybeExtendOffsetSlru(), MultiXactStateData::nextMXact, and MultiXactStateData::nextOffset.

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

2181 {
2182  debug_elog4(DEBUG2, "MultiXact: setting next multi to %u offset %u",
2183  nextMulti, nextMultiOffset);
2184  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2185  MultiXactState->nextMXact = nextMulti;
2186  MultiXactState->nextOffset = nextMultiOffset;
2187  LWLockRelease(MultiXactGenLock);
2188 
2189  /*
2190  * During a binary upgrade, make sure that the offsets SLRU is large
2191  * enough to contain the next value that would be created.
2192  *
2193  * We need to do this pretty early during the first startup in binary
2194  * upgrade mode: before StartupMultiXact() in fact, because this routine
2195  * is called even before that by StartupXLOG(). And we can't do it
2196  * earlier than at this point, because during that first call of this
2197  * routine we determine the MultiXactState->nextMXact value that
2198  * MaybeExtendOffsetSlru needs.
2199  */
2200  if (IsBinaryUpgrade)
2202 }
MultiXactId nextMXact
Definition: multixact.c:203
#define debug_elog4(a, b, c, d)
Definition: multixact.c:335
MultiXactOffset nextOffset
Definition: multixact.c:206
static void MaybeExtendOffsetSlru(void)
Definition: multixact.c:1975
bool IsBinaryUpgrade
Definition: globals.c:113
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define DEBUG2
Definition: elog.h:24
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ MultiXactShmemInit()

void MultiXactShmemInit ( void  )

Definition at line 1844 of file multixact.c.

References Assert, DEBUG2, debug_elog2, IsUnderPostmaster, LWTRANCHE_MULTIXACTMEMBER_BUFFER, LWTRANCHE_MULTIXACTOFFSET_BUFFER, MaxOldestSlot, MemSet, MULTIXACT_OFFSETS_PER_PAGE, MultiXactMemberCtl, MultiXactMemberPagePrecedes(), MultiXactOffsetCtl, MultiXactOffsetPagePrecedes(), NUM_MULTIXACTMEMBER_BUFFERS, NUM_MULTIXACTOFFSET_BUFFERS, OldestMemberMXactId, OldestVisibleMXactId, MultiXactStateData::perBackendXactIds, SHARED_MULTIXACT_STATE_SIZE, ShmemInitStruct(), SimpleLruInit(), SlruPagePrecedesUnitTests, SYNC_HANDLER_MULTIXACT_MEMBER, and SYNC_HANDLER_MULTIXACT_OFFSET.

Referenced by CreateSharedMemoryAndSemaphores().

1845 {
1846  bool found;
1847 
1848  debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
1849 
1852 
1854  "MultiXactOffset", NUM_MULTIXACTOFFSET_BUFFERS, 0,
1855  MultiXactOffsetSLRULock, "pg_multixact/offsets",
1860  "MultiXactMember", NUM_MULTIXACTMEMBER_BUFFERS, 0,
1861  MultiXactMemberSLRULock, "pg_multixact/members",
1864  /* doesn't call SimpleLruTruncate() or meet criteria for unit tests */
1865 
1866  /* Initialize our shared state struct */
1867  MultiXactState = ShmemInitStruct("Shared MultiXact State",
1869  &found);
1870  if (!IsUnderPostmaster)
1871  {
1872  Assert(!found);
1873 
1874  /* Make sure we zero out the per-backend state */
1876  }
1877  else
1878  Assert(found);
1879 
1880  /*
1881  * Set up array pointers. Note that perBackendXactIds[0] is wasted space
1882  * since we only use indexes 1..MaxOldestSlot in each array.
1883  */
1886 }
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
MultiXactId perBackendXactIds[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:281
static bool MultiXactOffsetPagePrecedes(int page1, int page2)
Definition: multixact.c:3119
#define MemSet(start, val, len)
Definition: c.h:1008
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
#define NUM_MULTIXACTOFFSET_BUFFERS
Definition: multixact.h:33
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:187
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define DEBUG2
Definition: elog.h:24
bool IsUnderPostmaster
Definition: globals.c:112
#define debug_elog2(a, b)
Definition: multixact.c:333
#define MULTIXACT_OFFSETS_PER_PAGE
Definition: multixact.c:109
static bool MultiXactMemberPagePrecedes(int page1, int page2)
Definition: multixact.c:3139
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define Assert(condition)
Definition: c.h:804
#define MaxOldestSlot
Definition: multixact.c:288
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:156
#define MultiXactMemberCtl
Definition: multixact.c:191
#define SHARED_MULTIXACT_STATE_SIZE
#define NUM_MULTIXACTMEMBER_BUFFERS
Definition: multixact.h:34

◆ MultiXactShmemSize()

Size MultiXactShmemSize ( void  )

Definition at line 1827 of file multixact.c.

References add_size(), NUM_MULTIXACTMEMBER_BUFFERS, NUM_MULTIXACTOFFSET_BUFFERS, SHARED_MULTIXACT_STATE_SIZE, and SimpleLruShmemSize().

Referenced by CalculateShmemSize().

1828 {
1829  Size size;
1830 
1831  /* We need 2*MaxOldestSlot + 1 perBackendXactIds[] entries */
1832 #define SHARED_MULTIXACT_STATE_SIZE \
1833  add_size(offsetof(MultiXactStateData, perBackendXactIds) + sizeof(MultiXactId), \
1834  mul_size(sizeof(MultiXactId) * 2, MaxOldestSlot))
1835 
1839 
1840  return size;
1841 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:156
#define NUM_MULTIXACTOFFSET_BUFFERS
Definition: multixact.h:33
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
#define SHARED_MULTIXACT_STATE_SIZE
#define NUM_MULTIXACTMEMBER_BUFFERS
Definition: multixact.h:34

◆ mXactCacheGetById()

static int mXactCacheGetById ( MultiXactId  multi,
MultiXactMember **  members 
)
static

Definition at line 1539 of file multixact.c.

References dlist_iter::cur, DEBUG2, debug_elog2, debug_elog3, dlist_container, dlist_foreach, dlist_move_head(), mXactCacheEnt::members, mXactCacheEnt::multi, mxid_to_string(), mXactCacheEnt::nmembers, and palloc().

Referenced by GetMultiXactIdMembers().

1540 {
1541  dlist_iter iter;
1542 
1543  debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
1544 
1545  dlist_foreach(iter, &MXactCache)
1546  {
1547  mXactCacheEnt *entry = dlist_container(mXactCacheEnt, node, iter.cur);
1548 
1549  if (entry->multi == multi)
1550  {
1551  MultiXactMember *ptr;
1552  Size size;
1553 
1554  size = sizeof(MultiXactMember) * entry->nmembers;
1555  ptr = (MultiXactMember *) palloc(size);
1556 
1557  memcpy(ptr, entry->members, size);
1558 
1559  debug_elog3(DEBUG2, "CacheGet: found %s",
1560  mxid_to_string(multi,
1561  entry->nmembers,
1562  entry->members));
1563 
1564  /*
1565  * Note we modify the list while not using a modifiable iterator.
1566  * This is acceptable only because we exit the iteration
1567  * immediately afterwards.
1568  */
1569  dlist_move_head(&MXactCache, iter.cur);
1570 
1571  *members = ptr;
1572  return entry->nmembers;
1573  }
1574  }
1575 
1576  debug_elog2(DEBUG2, "CacheGet: not found");
1577  return -1;
1578 }
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:318
#define dlist_foreach(iter, lhead)
Definition: ilist.h:526
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
#define DEBUG2
Definition: elog.h:24
#define debug_elog2(a, b)
Definition: multixact.c:333
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
#define debug_elog3(a, b, c)
Definition: multixact.c:334
dlist_node * cur
Definition: ilist.h:161
static dlist_head MXactCache
Definition: multixact.c:322
struct MultiXactMember MultiXactMember
size_t Size
Definition: c.h:540
static void dlist_move_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:385
void * palloc(Size size)
Definition: mcxt.c:1062
MultiXactId multi
Definition: multixact.c:315

◆ mXactCacheGetBySet()

static MultiXactId mXactCacheGetBySet ( int  nmembers,
MultiXactMember members 
)
static

Definition at line 1497 of file multixact.c.

References dlist_iter::cur, DEBUG2, debug_elog2, debug_elog3, dlist_container, dlist_foreach, dlist_move_head(), InvalidMultiXactId, mXactCacheEnt::members, mXactCacheEnt::multi, mxactMemberComparator(), mxid_to_string(), mXactCacheEnt::nmembers, and qsort.

Referenced by MultiXactIdCreateFromMembers().

1498 {
1499  dlist_iter iter;
1500 
1501  debug_elog3(DEBUG2, "CacheGet: looking for %s",
1502  mxid_to_string(InvalidMultiXactId, nmembers, members));
1503 
1504  /* sort the array so comparison is easy */
1505  qsort(members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1506 
1507  dlist_foreach(iter, &MXactCache)
1508  {
1509  mXactCacheEnt *entry = dlist_container(mXactCacheEnt, node, iter.cur);
1510 
1511  if (entry->nmembers != nmembers)
1512  continue;
1513 
1514  /*
1515  * We assume the cache entries are sorted, and that the unused bits in
1516  * "status" are zeroed.
1517  */
1518  if (memcmp(members, entry->members, nmembers * sizeof(MultiXactMember)) == 0)
1519  {
1520  debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
1521  dlist_move_head(&MXactCache, iter.cur);
1522  return entry->multi;
1523  }
1524  }
1525 
1526  debug_elog2(DEBUG2, "CacheGet: not found :-(");
1527  return InvalidMultiXactId;
1528 }
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:318
#define dlist_foreach(iter, lhead)
Definition: ilist.h:526
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
#define DEBUG2
Definition: elog.h:24
#define debug_elog2(a, b)
Definition: multixact.c:333
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
#define debug_elog3(a, b, c)
Definition: multixact.c:334
#define InvalidMultiXactId
Definition: multixact.h:24
dlist_node * cur
Definition: ilist.h:161
static dlist_head MXactCache
Definition: multixact.c:322
static void dlist_move_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:385
static int mxactMemberComparator(const void *arg1, const void *arg2)
Definition: multixact.c:1467
#define qsort(a, b, c, d)
Definition: port.h:505
MultiXactId multi
Definition: multixact.c:315

◆ mXactCachePut()

static void mXactCachePut ( MultiXactId  multi,
int  nmembers,
MultiXactMember members 
)
static

Definition at line 1585 of file multixact.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, DEBUG2, debug_elog2, debug_elog3, dlist_container, dlist_delete(), dlist_push_head(), dlist_tail_node(), MAX_CACHE_ENTRIES, mXactCacheEnt::members, MemoryContextAlloc(), mXactCacheEnt::multi, MXactCacheMembers, mxactMemberComparator(), mxid_to_string(), mXactCacheEnt::nmembers, mXactCacheEnt::node, offsetof, pfree(), qsort, and TopTransactionContext.

Referenced by GetMultiXactIdMembers(), and MultiXactIdCreateFromMembers().

1586 {
1587  mXactCacheEnt *entry;
1588 
1589  debug_elog3(DEBUG2, "CachePut: storing %s",
1590  mxid_to_string(multi, nmembers, members));
1591 
1592  if (MXactContext == NULL)
1593  {
1594  /* The cache only lives as long as the current transaction */
1595  debug_elog2(DEBUG2, "CachePut: initializing memory context");
1597  "MultiXact cache context",
1599  }
1600 
1601  entry = (mXactCacheEnt *)
1603  offsetof(mXactCacheEnt, members) +
1604  nmembers * sizeof(MultiXactMember));
1605 
1606  entry->multi = multi;
1607  entry->nmembers = nmembers;
1608  memcpy(entry->members, members, nmembers * sizeof(MultiXactMember));
1609 
1610  /* mXactCacheGetBySet assumes the entries are sorted, so sort them */
1611  qsort(entry->members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1612 
1613  dlist_push_head(&MXactCache, &entry->node);
1615  {
1616  dlist_node *node;
1617  mXactCacheEnt *entry;
1618 
1619  node = dlist_tail_node(&MXactCache);
1620  dlist_delete(node);
1622 
1623  entry = dlist_container(mXactCacheEnt, node, node);
1624  debug_elog3(DEBUG2, "CachePut: pruning cached multi %u",
1625  entry->multi);
1626 
1627  pfree(entry);
1628  }
1629 }
#define AllocSetContextCreate
Definition: memutils.h:173
MemoryContext TopTransactionContext
Definition: mcxt.c:53
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:318
static dlist_node * dlist_tail_node(dlist_head *head)
Definition: ilist.h:485
#define MAX_CACHE_ENTRIES
Definition: multixact.c:321
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
dlist_node node
Definition: multixact.c:317
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
void pfree(void *pointer)
Definition: mcxt.c:1169
static int MXactCacheMembers
Definition: multixact.c:323
#define DEBUG2
Definition: elog.h:24
#define debug_elog2(a, b)
Definition: multixact.c:333
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1655
#define debug_elog3(a, b, c)
Definition: multixact.c:334
static dlist_head MXactCache
Definition: multixact.c:322
struct MultiXactMember MultiXactMember
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
static int mxactMemberComparator(const void *arg1, const void *arg2)
Definition: multixact.c:1467
static MemoryContext MXactContext
Definition: multixact.c:324
#define qsort(a, b, c, d)
Definition: port.h:505
MultiXactId multi
Definition: multixact.c:315
#define offsetof(type, field)
Definition: c.h:727

◆ mxactMemberComparator()

static int mxactMemberComparator ( const void *  arg1,
const void *  arg2 
)
static

Definition at line 1467 of file multixact.c.

References MultiXactMember::status, and MultiXactMember::xid.

Referenced by mXactCacheGetBySet(), and mXactCachePut().

1468 {
1469  MultiXactMember member1 = *(const MultiXactMember *) arg1;
1470  MultiXactMember member2 = *(const MultiXactMember *) arg2;
1471 
1472  if (member1.xid > member2.xid)
1473  return 1;
1474  if (member1.xid < member2.xid)
1475  return -1;
1476  if (member1.status > member2.status)
1477  return 1;
1478  if (member1.status < member2.status)
1479  return -1;
1480  return 0;
1481 }
TransactionId xid
Definition: multixact.h:62
MultiXactStatus status
Definition: multixact.h:63

◆ mxid_to_string()

char* mxid_to_string ( MultiXactId  multi,
int  nmembers,
MultiXactMember members 
)

Definition at line 1655 of file multixact.c.

References appendStringInfo(), appendStringInfoChar(), buf, StringInfoData::data, i, initStringInfo(), MemoryContextStrdup(), mxstatus_to_string(), pfree(), status(), generate_unaccent_rules::str, and TopMemoryContext.

Referenced by GetMultiXactIdMembers(), MultiXactIdCreate(), MultiXactIdCreateFromMembers(), mXactCacheGetById(), mXactCacheGetBySet(), and mXactCachePut().

1656 {
1657  static char *str = NULL;
1659  int i;
1660 
1661  if (str != NULL)
1662  pfree(str);
1663 
1664  initStringInfo(&buf);
1665 
1666  appendStringInfo(&buf, "%u %d[%u (%s)", multi, nmembers, members[0].xid,
1667  mxstatus_to_string(members[0].status));
1668 
1669  for (i = 1; i < nmembers; i++)
1670  appendStringInfo(&buf, ", %u (%s)", members[i].xid,
1671  mxstatus_to_string(members[i].status));
1672 
1673  appendStringInfoChar(&buf, ']');
1675  pfree(buf.data);
1676  return str;
1677 }
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static char * buf
Definition: pg_test_fsync.c:68
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
int i
static char * mxstatus_to_string(MultiXactStatus status)
Definition: multixact.c:1632
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ mxstatus_to_string()

static char * mxstatus_to_string ( MultiXactStatus  status)
static

Definition at line 1632 of file multixact.c.

References elog, ERROR, MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, and MultiXactStatusUpdate.

Referenced by MultiXactIdExpand(), mxid_to_string(), and pg_get_multixact_members().

1633 {
1634  switch (status)
1635  {
1637  return "keysh";
1639  return "sh";
1641  return "fornokeyupd";
1643  return "forupd";
1645  return "nokeyupd";
1646  case MultiXactStatusUpdate:
1647  return "upd";
1648  default:
1649  elog(ERROR, "unrecognized multixact status %d", status);
1650  return "";
1651  }
1652 }
#define ERROR
Definition: elog.h:46
#define elog(elevel,...)
Definition: elog.h:232
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ PerformMembersTruncation()

static void PerformMembersTruncation ( MultiXactOffset  oldestOffset,
MultiXactOffset  newOldestOffset 
)
static

Definition at line 2891 of file multixact.c.

References DEBUG2, elog, MaxMultiXactOffset, MultiXactMemberCtl, MXOffsetToMemberSegment, and SlruDeleteSegment().

Referenced by multixact_redo(), and TruncateMultiXact().

2892 {
2893  const int maxsegment = MXOffsetToMemberSegment(MaxMultiXactOffset);
2894  int startsegment = MXOffsetToMemberSegment(oldestOffset);
2895  int endsegment = MXOffsetToMemberSegment(newOldestOffset);
2896  int segment = startsegment;
2897 
2898  /*
2899  * Delete all the segments but the last one. The last segment can still
2900  * contain, possibly partially, valid data.
2901  */
2902  while (segment != endsegment)
2903  {
2904  elog(DEBUG2, "truncating multixact members segment %x", segment);
2906 
2907  /* move to next segment, handling wraparound correctly */
2908  if (segment == maxsegment)
2909  segment = 0;
2910  else
2911  segment += 1;
2912  }
2913 }
#define MaxMultiXactOffset
Definition: multixact.h:30
#define DEBUG2
Definition: elog.h:24
#define MXOffsetToMemberSegment(xid)
Definition: multixact.c:160
#define elog(elevel,...)
Definition: elog.h:232
void SlruDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1327
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ PerformOffsetsTruncation()

static void PerformOffsetsTruncation ( MultiXactId  oldestMulti,
MultiXactId  newOldestMulti 
)
static

Definition at line 2919 of file multixact.c.

References MultiXactIdToOffsetPage, MultiXactOffsetCtl, PreviousMultiXactId, and SimpleLruTruncate().

Referenced by multixact_redo(), and TruncateMultiXact().

2920 {
2921  /*
2922  * We step back one multixact to avoid passing a cutoff page that hasn't
2923  * been created yet in the rare case that oldestMulti would be the first
2924  * item on a page and oldestMulti == nextMulti. In that case, if we
2925  * didn't subtract one, we'd trigger SimpleLruTruncate's wraparound
2926  * detection.
2927  */
2929  MultiXactIdToOffsetPage(PreviousMultiXactId(newOldestMulti)));
2930 }
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1226
#define PreviousMultiXactId(xid)
Definition: multixact.c:181
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
#define MultiXactOffsetCtl
Definition: multixact.c:190

◆ pg_get_multixact_members()

Datum pg_get_multixact_members ( PG_FUNCTION_ARGS  )

Definition at line 3347 of file multixact.c.

References FuncCallContext::attinmeta, BuildTupleFromCStrings(), CreateTemplateTupleDesc(), ereport, errcode(), errmsg(), ERROR, FirstMultiXactId, GetMultiXactIdMembers(), HeapTupleGetDatum, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, mxstatus_to_string(), palloc(), pfree(), PG_GETARG_TRANSACTIONID, psprintf(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, TupleDescGetAttInMetadata(), TupleDescInitEntry(), FuncCallContext::user_fctx, and values.

3348 {
3349  typedef struct
3350  {
3351  MultiXactMember *members;
3352  int nmembers;
3353  int iter;
3354  } mxact;
3356  mxact *multi;
3357  FuncCallContext *funccxt;
3358 
3359  if (mxid < FirstMultiXactId)
3360  ereport(ERROR,
3361  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3362  errmsg("invalid MultiXactId: %u", mxid)));
3363 
3364  if (SRF_IS_FIRSTCALL())
3365  {
3366  MemoryContext oldcxt;
3367  TupleDesc tupdesc;
3368 
3369  funccxt = SRF_FIRSTCALL_INIT();
3370  oldcxt = MemoryContextSwitchTo(funccxt->multi_call_memory_ctx);
3371 
3372  multi = palloc(sizeof(mxact));
3373  /* no need to allow for old values here */
3374  multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false,
3375  false);
3376  multi->iter = 0;
3377 
3378  tupdesc = CreateTemplateTupleDesc(2);
3379  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "xid",
3380  XIDOID, -1, 0);
3381  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "mode",
3382  TEXTOID, -1, 0);
3383 
3384  funccxt->attinmeta = TupleDescGetAttInMetadata(tupdesc);
3385  funccxt->user_fctx = multi;
3386 
3387  MemoryContextSwitchTo(oldcxt);
3388  }
3389 
3390  funccxt = SRF_PERCALL_SETUP();
3391  multi = (mxact *) funccxt->user_fctx;
3392 
3393  while (multi->iter < multi->nmembers)
3394  {
3395  HeapTuple tuple;
3396  char *values[2];
3397 
3398  values[0] = psprintf("%u", multi->members[multi->iter].xid);
3399  values[1] = mxstatus_to_string(multi->members[multi->iter].status);
3400 
3401  tuple = BuildTupleFromCStrings(funccxt->attinmeta, values);
3402 
3403  multi->iter++;
3404  pfree(values[0]);
3405  SRF_RETURN_NEXT(funccxt, HeapTupleGetDatum(tuple));
3406  }
3407 
3408  SRF_RETURN_DONE(funccxt);
3409 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define PG_GETARG_TRANSACTIONID(n)
Definition: fmgr.h:279
AttInMetadata * attinmeta
Definition: funcapi.h:91
#define FirstMultiXactId
Definition: multixact.h:25
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
#define ereport(elevel,...)
Definition: elog.h:157
TransactionId MultiXactId
Definition: c.h:597
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
static char * mxstatus_to_string(MultiXactStatus status)
Definition: multixact.c:1632
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1223
int16 AttrNumber
Definition: attnum.h:21
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ PostPrepare_MultiXact()

void PostPrepare_MultiXact ( TransactionId  xid)

Definition at line 1729 of file multixact.c.

References dlist_init(), InvalidMultiXactId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdIsValid, MXactCacheMembers, MyBackendId, OldestMemberMXactId, OldestVisibleMXactId, and TwoPhaseGetDummyBackendId().

Referenced by PrepareTransaction().

1730 {
1731  MultiXactId myOldestMember;
1732 
1733  /*
1734  * Transfer our OldestMemberMXactId value to the slot reserved for the
1735  * prepared transaction.
1736  */
1737  myOldestMember = OldestMemberMXactId[MyBackendId];
1738  if (MultiXactIdIsValid(myOldestMember))
1739  {
1740  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, false);
1741 
1742  /*
1743  * Even though storing MultiXactId is atomic, acquire lock to make
1744  * sure others see both changes, not just the reset of the slot of the
1745  * current backend. Using a volatile pointer might suffice, but this
1746  * isn't a hot spot.
1747  */
1748  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1749 
1750  OldestMemberMXactId[dummyBackendId] = myOldestMember;
1752 
1753  LWLockRelease(MultiXactGenLock);
1754  }
1755 
1756  /*
1757  * We don't need to transfer OldestVisibleMXactId value, because the
1758  * transaction is not going to be looking at any more multixacts once it's
1759  * prepared.
1760  *
1761  * We assume that storing a MultiXactId is atomic and so we need not take
1762  * MultiXactGenLock to do this.
1763  */
1765 
1766  /*
1767  * Discard the local MultiXactId cache like in AtEOXact_MultiXact.
1768  */
1769  MXactContext = NULL;
1771  MXactCacheMembers = 0;
1772 }
BackendId MyBackendId
Definition: globals.c:84
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:292
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:293
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
static int MXactCacheMembers
Definition: multixact.c:323
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
BackendId TwoPhaseGetDummyBackendId(TransactionId xid, bool lock_held)
Definition: twophase.c:859
int BackendId
Definition: backendid.h:21
#define InvalidMultiXactId
Definition: multixact.h:24
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
TransactionId MultiXactId
Definition: c.h:597
static dlist_head MXactCache
Definition: multixact.c:322
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
static MemoryContext MXactContext
Definition: multixact.c:324

◆ ReadMultiXactCounts()

static bool ReadMultiXactCounts ( uint32 multixacts,
MultiXactOffset members 
)
static

Definition at line 2772 of file multixact.c.

References LW_SHARED, LWLockAcquire(), LWLockRelease(), MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::oldestMultiXactId, MultiXactStateData::oldestOffset, and MultiXactStateData::oldestOffsetKnown.

Referenced by MultiXactMemberFreezeThreshold().

2773 {
2774  MultiXactOffset nextOffset;
2775  MultiXactOffset oldestOffset;
2776  MultiXactId oldestMultiXactId;
2777  MultiXactId nextMultiXactId;
2778  bool oldestOffsetKnown;
2779 
2780  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2781  nextOffset = MultiXactState->nextOffset;
2782  oldestMultiXactId = MultiXactState->oldestMultiXactId;
2783  nextMultiXactId = MultiXactState->nextMXact;
2784  oldestOffset = MultiXactState->oldestOffset;
2785  oldestOffsetKnown = MultiXactState->oldestOffsetKnown;
2786  LWLockRelease(MultiXactGenLock);
2787 
2788  if (!oldestOffsetKnown)
2789  return false;
2790 
2791  *members = nextOffset - oldestOffset;
2792  *multixacts = nextMultiXactId - oldestMultiXactId;
2793  return true;
2794 }
MultiXactId nextMXact
Definition: multixact.c:203
uint32 MultiXactOffset
Definition: c.h:599
MultiXactOffset nextOffset
Definition: multixact.c:206
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
MultiXactOffset oldestOffset
Definition: multixact.c:224
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ ReadMultiXactIdRange()

void ReadMultiXactIdRange ( MultiXactId oldest,
MultiXactId next 
)

Definition at line 743 of file multixact.c.

References FirstMultiXactId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MultiXactStateData::nextMXact, and MultiXactStateData::oldestMultiXactId.

Referenced by update_cached_mxid_range().

744 {
745  LWLockAcquire(MultiXactGenLock, LW_SHARED);
748  LWLockRelease(MultiXactGenLock);
749 
750  if (*oldest < FirstMultiXactId)
751  *oldest = FirstMultiXactId;
752  if (*next < FirstMultiXactId)
754 }
MultiXactId nextMXact
Definition: multixact.c:203
static int32 next
Definition: blutils.c:219
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
#define FirstMultiXactId
Definition: multixact.h:25
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ ReadNextMultiXactId()

MultiXactId ReadNextMultiXactId ( void  )

Definition at line 723 of file multixact.c.

References FirstMultiXactId, LW_SHARED, LWLockAcquire(), LWLockRelease(), and MultiXactStateData::nextMXact.

Referenced by ATRewriteTables(), AutoVacWorkerMain(), do_start_worker(), mxid_age(), refresh_by_heap_swap(), vac_update_datfrozenxid(), vac_update_relstats(), vacuum_set_xid_limits(), and vacuum_xid_failsafe_check().

724 {
725  MultiXactId mxid;
726 
727  /* XXX we could presumably do this without a lock. */
728  LWLockAcquire(MultiXactGenLock, LW_SHARED);
729  mxid = MultiXactState->nextMXact;
730  LWLockRelease(MultiXactGenLock);
731 
732  if (mxid < FirstMultiXactId)
733  mxid = FirstMultiXactId;
734 
735  return mxid;
736 }
MultiXactId nextMXact
Definition: multixact.c:203
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ RecordNewMultiXact()

static void RecordNewMultiXact ( MultiXactId  multi,
MultiXactOffset  offset,
int  nmembers,
MultiXactMember members 
)
static

Definition at line 862 of file multixact.c.

References Assert, i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdToOffsetEntry, MultiXactIdToOffsetPage, MultiXactMemberCtl, MultiXactOffsetCtl, MultiXactStatusUpdate, MXACT_MEMBER_BITS_PER_XACT, MXOffsetToFlagsBitShift, MXOffsetToFlagsOffset, MXOffsetToMemberOffset, MXOffsetToMemberPage, SimpleLruReadPage(), MultiXactMember::status, status(), and MultiXactMember::xid.

Referenced by multixact_redo(), and MultiXactIdCreateFromMembers().

864 {
865  int pageno;
866  int prev_pageno;
867  int entryno;
868  int slotno;
869  MultiXactOffset *offptr;
870  int i;
871 
872  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
873 
874  pageno = MultiXactIdToOffsetPage(multi);
875  entryno = MultiXactIdToOffsetEntry(multi);
876 
877  /*
878  * Note: we pass the MultiXactId to SimpleLruReadPage as the "transaction"
879  * to complain about if there's any I/O error. This is kinda bogus, but
880  * since the errors will always give the full pathname, it should be clear
881  * enough that a MultiXactId is really involved. Perhaps someday we'll
882  * take the trouble to generalize the slru.c error reporting code.
883  */
884  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
885  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
886  offptr += entryno;
887 
888  *offptr = offset;
889 
890  MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
891 
892  /* Exchange our lock */
893  LWLockRelease(MultiXactOffsetSLRULock);
894 
895  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
896 
897  prev_pageno = -1;
898 
899  for (i = 0; i < nmembers; i++, offset++)
900  {
901  TransactionId *memberptr;
902  uint32 *flagsptr;
903  uint32 flagsval;
904  int bshift;
905  int flagsoff;
906  int memberoff;
907 
908  Assert(members[i].status <= MultiXactStatusUpdate);
909 
910  pageno = MXOffsetToMemberPage(offset);
911  memberoff = MXOffsetToMemberOffset(offset);
912  flagsoff = MXOffsetToFlagsOffset(offset);
913  bshift = MXOffsetToFlagsBitShift(offset);
914 
915  if (pageno != prev_pageno)
916  {
917  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
918  prev_pageno = pageno;
919  }
920 
921  memberptr = (TransactionId *)
922  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
923 
924  *memberptr = members[i].xid;
925 
926  flagsptr = (uint32 *)
927  (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
928 
929  flagsval = *flagsptr;
930  flagsval &= ~(((1 << MXACT_MEMBER_BITS_PER_XACT) - 1) << bshift);
931  flagsval |= (members[i].status << bshift);
932  *flagsptr = flagsval;
933 
934  MultiXactMemberCtl->shared->page_dirty[slotno] = true;
935  }
936 
937  LWLockRelease(MultiXactMemberSLRULock);
938 }
#define MXOffsetToFlagsBitShift(xid)
Definition: multixact.c:167
uint32 MultiXactOffset
Definition: c.h:599
uint32 TransactionId
Definition: c.h:587
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
TransactionId xid
Definition: multixact.h:62
#define MXOffsetToMemberOffset(xid)
Definition: multixact.c:172
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:395
MultiXactStatus status
Definition: multixact.h:63
unsigned int uint32
Definition: c.h:441
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define MXACT_MEMBER_BITS_PER_XACT
Definition: multixact.c:130
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ SetMultiXactIdLimit()

void SetMultiXactIdLimit ( MultiXactId  oldest_datminmxid,
Oid  oldest_datoid,
bool  is_startup 
)

Definition at line 2213 of file multixact.c.

References Assert, autovacuum_multixact_freeze_max_age, DEBUG1, ereport, errhint(), errmsg_internal(), errmsg_plural(), MultiXactStateData::finishedStartup, FirstMultiXactId, get_database_name(), InRecovery, IsTransactionState(), IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxMultiXactId, MultiXactStateData::multiStopLimit, MultiXactStateData::multiVacLimit, MultiXactStateData::multiWarnLimit, MultiXactStateData::multiWrapLimit, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactStateData::nextMXact, MultiXactStateData::oldestMultiXactDB, MultiXactStateData::oldestMultiXactId, PMSIGNAL_START_AUTOVAC_LAUNCHER, SendPostmasterSignal(), SetOffsetVacuumLimit(), and WARNING.

Referenced by BootStrapXLOG(), multixact_redo(), MultiXactAdvanceOldest(), StartupXLOG(), TrimMultiXact(), and vac_truncate_clog().

2215 {
2216  MultiXactId multiVacLimit;
2217  MultiXactId multiWarnLimit;
2218  MultiXactId multiStopLimit;
2219  MultiXactId multiWrapLimit;
2220  MultiXactId curMulti;
2221  bool needs_offset_vacuum;
2222 
2223  Assert(MultiXactIdIsValid(oldest_datminmxid));
2224 
2225  /*
2226  * We pretend that a wrap will happen halfway through the multixact ID
2227  * space, but that's not really true, because multixacts wrap differently
2228  * from transaction IDs. Note that, separately from any concern about
2229  * multixact IDs wrapping, we must ensure that multixact members do not
2230  * wrap. Limits for that are set in SetOffsetVacuumLimit, not here.
2231  */
2232  multiWrapLimit = oldest_datminmxid + (MaxMultiXactId >> 1);
2233  if (multiWrapLimit < FirstMultiXactId)
2234  multiWrapLimit += FirstMultiXactId;
2235 
2236  /*
2237  * We'll refuse to continue assigning MultiXactIds once we get within 3M
2238  * multi of data loss. See SetTransactionIdLimit.
2239  */
2240  multiStopLimit = multiWrapLimit - 3000000;
2241  if (multiStopLimit < FirstMultiXactId)
2242  multiStopLimit -= FirstMultiXactId;
2243 
2244  /*
2245  * We'll start complaining loudly when we get within 40M multis of data
2246  * loss. This is kind of arbitrary, but if you let your gas gauge get
2247  * down to 2% of full, would you be looking for the next gas station? We
2248  * need to be fairly liberal about this number because there are lots of
2249  * scenarios where most transactions are done by automatic clients that
2250  * won't pay attention to warnings. (No, we're not gonna make this
2251  * configurable. If you know enough to configure it, you know enough to
2252  * not get in this kind of trouble in the first place.)
2253  */
2254  multiWarnLimit = multiWrapLimit - 40000000;
2255  if (multiWarnLimit < FirstMultiXactId)
2256  multiWarnLimit -= FirstMultiXactId;
2257 
2258  /*
2259  * We'll start trying to force autovacuums when oldest_datminmxid gets to
2260  * be more than autovacuum_multixact_freeze_max_age mxids old.
2261  *
2262  * Note: autovacuum_multixact_freeze_max_age is a PGC_POSTMASTER parameter
2263  * so that we don't have to worry about dealing with on-the-fly changes in
2264  * its value. See SetTransactionIdLimit.
2265  */
2266  multiVacLimit = oldest_datminmxid + autovacuum_multixact_freeze_max_age;
2267  if (multiVacLimit < FirstMultiXactId)
2268  multiVacLimit += FirstMultiXactId;
2269 
2270  /* Grab lock for just long enough to set the new limit values */
2271  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2272  MultiXactState->oldestMultiXactId = oldest_datminmxid;
2273  MultiXactState->oldestMultiXactDB = oldest_datoid;
2274  MultiXactState->multiVacLimit = multiVacLimit;
2275  MultiXactState->multiWarnLimit = multiWarnLimit;
2276  MultiXactState->multiStopLimit = multiStopLimit;
2277  MultiXactState->multiWrapLimit = multiWrapLimit;
2278  curMulti = MultiXactState->nextMXact;
2279  LWLockRelease(MultiXactGenLock);
2280 
2281  /* Log the info */
2282  ereport(DEBUG1,
2283  (errmsg_internal("MultiXactId wrap limit is %u, limited by database with OID %u",
2284  multiWrapLimit, oldest_datoid)));
2285 
2286  /*
2287  * Computing the actual limits is only possible once the data directory is
2288  * in a consistent state. There's no need to compute the limits while
2289  * still replaying WAL - no decisions about new multis are made even
2290  * though multixact creations might be replayed. So we'll only do further
2291  * checks after TrimMultiXact() has been called.
2292  */
2294  return;
2295 
2296  Assert(!InRecovery);
2297 
2298  /* Set limits for offset vacuum. */
2299  needs_offset_vacuum = SetOffsetVacuumLimit(is_startup);
2300 
2301  /*
2302  * If past the autovacuum force point, immediately signal an autovac
2303  * request. The reason for this is that autovac only processes one
2304  * database per invocation. Once it's finished cleaning up the oldest
2305  * database, it'll call here, and we'll signal the postmaster to start
2306  * another iteration immediately if there are still any old databases.
2307  */
2308  if ((MultiXactIdPrecedes(multiVacLimit, curMulti) ||
2309  needs_offset_vacuum) && IsUnderPostmaster)
2311 
2312  /* Give an immediate warning if past the wrap warn point */
2313  if (MultiXactIdPrecedes(multiWarnLimit, curMulti))
2314  {
2315  char *oldest_datname;
2316 
2317  /*
2318  * We can be called when not inside a transaction, for example during
2319  * StartupXLOG(). In such a case we cannot do database access, so we
2320  * must just report the oldest DB's OID.
2321  *
2322  * Note: it's also possible that get_database_name fails and returns
2323  * NULL, for example because the database just got dropped. We'll
2324  * still warn, even though the warning might now be unnecessary.
2325  */
2326  if (IsTransactionState())
2327  oldest_datname = get_database_name(oldest_datoid);
2328  else
2329  oldest_datname = NULL;
2330 
2331  if (oldest_datname)
2332  ereport(WARNING,
2333  (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
2334  "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
2335  multiWrapLimit - curMulti,
2336  oldest_datname,
2337  multiWrapLimit - curMulti),
2338  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
2339  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
2340  else
2341  ereport(WARNING,
2342  (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
2343  "database with OID %u must be vacuumed before %u more MultiXactIds are used",
2344  multiWrapLimit - curMulti,
2345  oldest_datoid,
2346  multiWrapLimit - curMulti),
2347  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
2348  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
2349  }
2350 }
MultiXactId nextMXact
Definition: multixact.c:203
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define MaxMultiXactId
Definition: multixact.h:26
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1019
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:125
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
static bool SetOffsetVacuumLimit(bool is_startup)
Definition: multixact.c:2559
MultiXactId multiVacLimit
Definition: multixact.c:228
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2113
bool IsUnderPostmaster
Definition: globals.c:112
MultiXactId multiStopLimit
Definition: multixact.c:230
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
bool InRecovery
Definition: xlogutils.c:52
TransactionId MultiXactId
Definition: c.h:597
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
MultiXactId multiWrapLimit
Definition: multixact.c:231
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool IsTransactionState(void)
Definition: xact.c:371
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
MultiXactId multiWarnLimit
Definition: multixact.c:229
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:153

◆ SetOffsetVacuumLimit()

static bool SetOffsetVacuumLimit ( bool  is_startup)
static

Definition at line 2559 of file multixact.c.

References Assert, DEBUG1, ereport, errmsg(), errmsg_internal(), find_multixact_start(), MultiXactStateData::finishedStartup, LOG, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MULTIXACT_MEMBER_SAFE_THRESHOLD, MULTIXACT_MEMBERS_PER_PAGE, MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::offsetStopLimit, MultiXactStateData::oldestMultiXactId, MultiXactStateData::oldestOffset, MultiXactStateData::oldestOffsetKnown, and SLRU_PAGES_PER_SEGMENT.

Referenced by SetMultiXactIdLimit().

2560 {
2561  MultiXactId oldestMultiXactId;
2562  MultiXactId nextMXact;
2563  MultiXactOffset oldestOffset = 0; /* placate compiler */
2564  MultiXactOffset prevOldestOffset;
2565  MultiXactOffset nextOffset;
2566  bool oldestOffsetKnown = false;
2567  bool prevOldestOffsetKnown;
2568  MultiXactOffset offsetStopLimit = 0;
2569  MultiXactOffset prevOffsetStopLimit;
2570 
2571  /*
2572  * NB: Have to prevent concurrent truncation, we might otherwise try to
2573  * lookup an oldestMulti that's concurrently getting truncated away.
2574  */
2575  LWLockAcquire(MultiXactTruncationLock, LW_SHARED);
2576 
2577  /* Read relevant fields from shared memory. */
2578  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2579  oldestMultiXactId = MultiXactState->oldestMultiXactId;
2580  nextMXact = MultiXactState->nextMXact;
2581  nextOffset = MultiXactState->nextOffset;
2582  prevOldestOffsetKnown = MultiXactState->oldestOffsetKnown;
2583  prevOldestOffset = MultiXactState->oldestOffset;
2584  prevOffsetStopLimit = MultiXactState->offsetStopLimit;
2586  LWLockRelease(MultiXactGenLock);
2587 
2588  /*
2589  * Determine the offset of the oldest multixact. Normally, we can read
2590  * the offset from the multixact itself, but there's an important special
2591  * case: if there are no multixacts in existence at all, oldestMXact
2592  * obviously can't point to one. It will instead point to the multixact
2593  * ID that will be assigned the next time one is needed.
2594  */
2595  if (oldestMultiXactId == nextMXact)
2596  {
2597  /*
2598  * When the next multixact gets created, it will be stored at the next
2599  * offset.
2600  */
2601  oldestOffset = nextOffset;
2602  oldestOffsetKnown = true;
2603  }
2604  else
2605  {
2606  /*
2607  * Figure out where the oldest existing multixact's offsets are
2608  * stored. Due to bugs in early release of PostgreSQL 9.3.X and 9.4.X,
2609  * the supposedly-earliest multixact might not really exist. We are
2610  * careful not to fail in that case.
2611  */
2612  oldestOffsetKnown =
2613  find_multixact_start(oldestMultiXactId, &oldestOffset);
2614 
2615  if (oldestOffsetKnown)
2616  ereport(DEBUG1,
2617  (errmsg_internal("oldest MultiXactId member is at offset %u",
2618  oldestOffset)));
2619  else
2620  ereport(LOG,
2621  (errmsg("MultiXact member wraparound protections are disabled because oldest checkpointed MultiXact %u does not exist on disk",
2622  oldestMultiXactId)));
2623  }
2624 
2625  LWLockRelease(MultiXactTruncationLock);
2626 
2627  /*
2628  * If we can, compute limits (and install them MultiXactState) to prevent
2629  * overrun of old data in the members SLRU area. We can only do so if the
2630  * oldest offset is known though.
2631  */
2632  if (oldestOffsetKnown)
2633  {
2634  /* move back to start of the corresponding segment */
2635  offsetStopLimit = oldestOffset - (oldestOffset %
2637 
2638  /* always leave one segment before the wraparound point */
2639  offsetStopLimit -= (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT);
2640 
2641  if (!prevOldestOffsetKnown && !is_startup)
2642  ereport(LOG,
2643  (errmsg("MultiXact member wraparound protections are now enabled")));
2644 
2645  ereport(DEBUG1,
2646  (errmsg_internal("MultiXact member stop limit is now %u based on MultiXact %u",
2647  offsetStopLimit, oldestMultiXactId)));
2648  }
2649  else if (prevOldestOffsetKnown)
2650  {
2651  /*
2652  * If we failed to get the oldest offset this time, but we have a
2653  * value from a previous pass through this function, use the old
2654  * values rather than automatically forcing an emergency autovacuum
2655  * cycle again.
2656  */
2657  oldestOffset = prevOldestOffset;
2658  oldestOffsetKnown = true;
2659  offsetStopLimit = prevOffsetStopLimit;
2660  }
2661 
2662  /* Install the computed values */
2663  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2664  MultiXactState->oldestOffset = oldestOffset;
2665  MultiXactState->oldestOffsetKnown = oldestOffsetKnown;
2666  MultiXactState->offsetStopLimit = offsetStopLimit;
2667  LWLockRelease(MultiXactGenLock);
2668 
2669  /*
2670  * Do we need an emergency autovacuum? If we're not sure, assume yes.
2671  */
2672  return !oldestOffsetKnown ||
2673  (nextOffset - oldestOffset > MULTIXACT_MEMBER_SAFE_THRESHOLD);
2674 }
MultiXactId nextMXact
Definition: multixact.c:203
#define DEBUG1
Definition: elog.h:25
uint32 MultiXactOffset
Definition: c.h:599
MultiXactOffset nextOffset
Definition: multixact.c:206
#define MULTIXACT_MEMBERS_PER_PAGE
Definition: multixact.c:142
static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result)
Definition: multixact.c:2734
#define LOG
Definition: elog.h:26
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
MultiXactOffset offsetStopLimit
Definition: multixact.c:234
MultiXactOffset oldestOffset
Definition: multixact.c:224
#define ereport(elevel,...)
Definition: elog.h:157
TransactionId MultiXactId
Definition: c.h:597
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
#define MULTIXACT_MEMBER_SAFE_THRESHOLD
Definition: multixact.c:177

◆ SlruScanDirCbFindEarliest()

static bool SlruScanDirCbFindEarliest ( SlruCtl  ctl,
char *  filename,
int  segpage,
void *  data 
)
static

Definition at line 2868 of file multixact.c.

References mxtruncinfo::earliestExistingPage, and SlruCtlData::PagePrecedes.

Referenced by TruncateMultiXact().

2869 {
2870  mxtruncinfo *trunc = (mxtruncinfo *) data;
2871 
2872  if (trunc->earliestExistingPage == -1 ||
2873  ctl->PagePrecedes(segpage, trunc->earliestExistingPage))
2874  {
2875  trunc->earliestExistingPage = segpage;
2876  }
2877 
2878  return false; /* keep going */
2879 }
int earliestExistingPage
Definition: multixact.c:2860
bool(* PagePrecedes)(int, int)
Definition: slru.h:130

◆ StartupMultiXact()

void StartupMultiXact ( void  )

Definition at line 2008 of file multixact.c.

References MultiXactIdToOffsetPage, MultiXactMemberCtl, MultiXactOffsetCtl, MXOffsetToMemberPage, MultiXactStateData::nextMXact, and MultiXactStateData::nextOffset.

Referenced by StartupXLOG().

2009 {
2012  int pageno;
2013 
2014  /*
2015  * Initialize offset's idea of the latest page number.
2016  */
2017  pageno = MultiXactIdToOffsetPage(multi);
2018  MultiXactOffsetCtl->shared->latest_page_number = pageno;
2019 
2020  /*
2021  * Initialize member's idea of the latest page number.
2022  */
2023  pageno = MXOffsetToMemberPage(offset);
2024  MultiXactMemberCtl->shared->latest_page_number = pageno;
2025 }
MultiXactId nextMXact
Definition: multixact.c:203
uint32 MultiXactOffset
Definition: c.h:599
MultiXactOffset nextOffset
Definition: multixact.c:206
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
#define MultiXactOffsetCtl
Definition: multixact.c:190
TransactionId MultiXactId
Definition: c.h:597
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ TrimMultiXact()

void TrimMultiXact ( void  )

Definition at line 2031 of file multixact.c.

References MultiXactStateData::finishedStartup, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemSet, MultiXactIdToOffsetEntry, MultiXactIdToOffsetPage, MultiXactMemberCtl, MultiXactOffsetCtl, MXOffsetToFlagsOffset, MXOffsetToMemberOffset, MXOffsetToMemberPage, MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::oldestMultiXactDB, MultiXactStateData::oldestMultiXactId, SetMultiXactIdLimit(), and SimpleLruReadPage().

Referenced by StartupXLOG().

2032 {
2033  MultiXactId nextMXact;
2034  MultiXactOffset offset;
2035  MultiXactId oldestMXact;
2036  Oid oldestMXactDB;
2037  int pageno;
2038  int entryno;
2039  int flagsoff;
2040 
2041  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2042  nextMXact = MultiXactState->nextMXact;
2043  offset = MultiXactState->nextOffset;
2044  oldestMXact = MultiXactState->oldestMultiXactId;
2045  oldestMXactDB = MultiXactState->oldestMultiXactDB;
2046  LWLockRelease(MultiXactGenLock);
2047 
2048  /* Clean up offsets state */
2049  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
2050 
2051  /*
2052  * (Re-)Initialize our idea of the latest page number for offsets.
2053  */
2054  pageno = MultiXactIdToOffsetPage(nextMXact);
2055  MultiXactOffsetCtl->shared->latest_page_number = pageno;
2056 
2057  /*
2058  * Zero out the remainder of the current offsets page. See notes in
2059  * TrimCLOG() for background. Unlike CLOG, some WAL record covers every
2060  * pg_multixact SLRU mutation. Since, also unlike CLOG, we ignore the WAL
2061  * rule "write xlog before data," nextMXact successors may carry obsolete,
2062  * nonzero offset values. Zero those so case 2 of GetMultiXactIdMembers()
2063  * operates normally.
2064  */
2065  entryno = MultiXactIdToOffsetEntry(nextMXact);
2066  if (entryno != 0)
2067  {
2068  int slotno;
2069  MultiXactOffset *offptr;
2070 
2071  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, nextMXact);
2072  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
2073  offptr += entryno;
2074 
2075  MemSet(offptr, 0, BLCKSZ - (entryno * sizeof(MultiXactOffset)));
2076 
2077  MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
2078  }
2079 
2080  LWLockRelease(MultiXactOffsetSLRULock);
2081 
2082  /* And the same for members */
2083  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
2084 
2085  /*
2086  * (Re-)Initialize our idea of the latest page number for members.
2087  */
2088  pageno = MXOffsetToMemberPage(offset);
2089  MultiXactMemberCtl->shared->latest_page_number = pageno;
2090 
2091  /*
2092  * Zero out the remainder of the current members page. See notes in
2093  * TrimCLOG() for motivation.
2094  */
2095  flagsoff = MXOffsetToFlagsOffset(offset);
2096  if (flagsoff != 0)
2097  {
2098  int slotno;
2099  TransactionId *xidptr;
2100  int memberoff;
2101 
2102  memberoff = MXOffsetToMemberOffset(offset);
2103  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, offset);
2104  xidptr = (TransactionId *)
2105  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
2106 
2107  MemSet(xidptr, 0, BLCKSZ - memberoff);
2108 
2109  /*
2110  * Note: we don't need to zero out the flag bits in the remaining
2111  * members of the current group, because they are always reset before
2112  * writing.
2113  */
2114 
2115  MultiXactMemberCtl->shared->page_dirty[slotno] = true;
2116  }
2117 
2118  LWLockRelease(MultiXactMemberSLRULock);
2119 
2120  /* signal that we're officially up */
2121  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2123  LWLockRelease(MultiXactGenLock);
2124 
2125  /* Now compute how far away the next members wraparound is. */
2126  SetMultiXactIdLimit(oldestMXact, oldestMXactDB, true);
2127 }
MultiXactId nextMXact
Definition: multixact.c:203
uint32 MultiXactOffset
Definition: c.h:599
uint32 TransactionId
Definition: c.h:587
MultiXactOffset nextOffset
Definition: multixact.c:206
#define MemSet(start, val, len)
Definition: c.h:1008
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
unsigned int Oid
Definition: postgres_ext.h:31
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
#define MXOffsetToMemberOffset(xid)
Definition: multixact.c:172
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:395
#define MultiXactOffsetCtl
Definition: multixact.c:190
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2213
TransactionId MultiXactId
Definition: c.h:597
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define MultiXactMemberCtl
Definition: multixact.c:191

◆ TruncateMultiXact()

void TruncateMultiXact ( MultiXactId  newOldestMulti,
Oid  newOldestMultiDB 
)

Definition at line 2944 of file multixact.c.

References Assert, DEBUG1, PGPROC::delayChkpt, mxtruncinfo::earliestExistingPage, elog, END_CRIT_SECTION, ereport, errmsg(), find_multixact_start(), MultiXactStateData::finishedStartup, FirstMultiXactId, LOG, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MULTIXACT_OFFSETS_PER_PAGE, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), MultiXactIdToOffsetSegment, MultiXactOffsetCtl, MXOffsetToMemberSegment, MyProc, MultiXactStateData::nextMXact, MultiXactStateData::nextOffset, MultiXactStateData::oldestMultiXactDB, MultiXactStateData::oldestMultiXactId, MultiXactStateData::oldestOffset, PerformMembersTruncation(), PerformOffsetsTruncation(), RecoveryInProgress(), SlruScanDirCbFindEarliest(), SlruScanDirectory(), START_CRIT_SECTION, and WriteMTruncateXlogRec().

Referenced by vac_truncate_clog().

2945 {
2946  MultiXactId oldestMulti;
2947  MultiXactId nextMulti;
2948  MultiXactOffset newOldestOffset;
2949  MultiXactOffset oldestOffset;
2950  MultiXactOffset nextOffset;
2951  mxtruncinfo trunc;
2952  MultiXactId earliest;
2953 
2956 
2957  /*
2958  * We can only allow one truncation to happen at once. Otherwise parts of
2959  * members might vanish while we're doing lookups or similar. There's no
2960  * need to have an interlock with creating new multis or such, since those
2961  * are constrained by the limits (which only grow, never shrink).
2962  */
2963  LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
2964 
2965  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2966  nextMulti = MultiXactState->nextMXact;
2967  nextOffset = MultiXactState->nextOffset;
2968  oldestMulti = MultiXactState->oldestMultiXactId;
2969  LWLockRelease(MultiXactGenLock);
2970  Assert(MultiXactIdIsValid(oldestMulti));
2971 
2972  /*
2973  * Make sure to only attempt truncation if there's values to truncate
2974  * away. In normal processing values shouldn't go backwards, but there's
2975  * some corner cases (due to bugs) where that's possible.
2976  */
2977  if (MultiXactIdPrecedesOrEquals(newOldestMulti, oldestMulti))
2978  {
2979  LWLockRelease(MultiXactTruncationLock);
2980  return;
2981  }
2982 
2983  /*
2984  * Note we can't just plow ahead with the truncation; it's possible that
2985  * there are no segments to truncate, which is a problem because we are
2986  * going to attempt to read the offsets page to determine where to
2987  * truncate the members SLRU. So we first scan the directory to determine
2988  * the earliest offsets page number that we can read without error.
2989  *
2990  * When nextMXact is less than one segment away from multiWrapLimit,
2991  * SlruScanDirCbFindEarliest can find some early segment other than the
2992  * actual earliest. (MultiXactOffsetPagePrecedes(EARLIEST, LATEST)
2993  * returns false, because not all pairs of entries have the same answer.)
2994  * That can also arise when an earlier truncation attempt failed unlink()
2995  * or returned early from this function. The only consequence is
2996  * returning early, which wastes space that we could have liberated.
2997  *
2998  * NB: It's also possible that the page that oldestMulti is on has already
2999  * been truncated away, and we crashed before updating oldestMulti.
3000  */
3001  trunc.earliestExistingPage = -1;
3004  if (earliest < FirstMultiXactId)
3005  earliest = FirstMultiXactId;
3006 
3007  /* If there's nothing to remove, we can bail out early. */
3008  if (MultiXactIdPrecedes(oldestMulti, earliest))
3009  {
3010  LWLockRelease(MultiXactTruncationLock);
3011  return;
3012  }
3013 
3014  /*
3015  * First, compute the safe truncation point for MultiXactMember. This is
3016  * the starting offset of the oldest multixact.
3017  *
3018  * Hopefully, find_multixact_start will always work here, because we've
3019  * already checked that it doesn't precede the earliest MultiXact on disk.
3020  * But if it fails, don't truncate anything, and log a message.
3021  */
3022  if (oldestMulti == nextMulti)
3023  {
3024  /* there are NO MultiXacts */
3025  oldestOffset = nextOffset;
3026  }
3027  else if (!find_multixact_start(oldestMulti, &oldestOffset))
3028  {
3029  ereport(LOG,
3030  (errmsg("oldest MultiXact %u not found, earliest MultiXact %u, skipping truncation",
3031  oldestMulti, earliest)));
3032  LWLockRelease(MultiXactTruncationLock);
3033  return;
3034  }
3035 
3036  /*
3037  * Secondly compute up to where to truncate. Lookup the corresponding
3038  * member offset for newOldestMulti for that.
3039  */
3040  if (newOldestMulti == nextMulti)
3041  {
3042  /* there are NO MultiXacts */
3043  newOldestOffset = nextOffset;
3044  }
3045  else if (!find_multixact_start(newOldestMulti, &newOldestOffset))
3046  {
3047  ereport(LOG,
3048  (errmsg("cannot truncate up to MultiXact %u because it does not exist on disk, skipping truncation",
3049  newOldestMulti)));
3050  LWLockRelease(MultiXactTruncationLock);
3051  return;
3052  }
3053 
3054  elog(DEBUG1, "performing multixact truncation: "
3055  "offsets [%u, %u), offsets segments [%x, %x), "
3056  "members [%u, %u), members segments [%x, %x)",
3057  oldestMulti, newOldestMulti,
3058  MultiXactIdToOffsetSegment(oldestMulti),
3059  MultiXactIdToOffsetSegment(newOldestMulti),
3060  oldestOffset, newOldestOffset,
3061  MXOffsetToMemberSegment(oldestOffset),
3062  MXOffsetToMemberSegment(newOldestOffset));
3063 
3064  /*
3065  * Do truncation, and the WAL logging of the truncation, in a critical
3066  * section. That way offsets/members cannot get out of sync anymore, i.e.
3067  * once consistent the newOldestMulti will always exist in members, even
3068  * if we crashed in the wrong moment.
3069  */
3071 
3072  /*
3073  * Prevent checkpoints from being scheduled concurrently. This is critical
3074  * because otherwise a truncation record might not be replayed after a
3075  * crash/basebackup, even though the state of the data directory would
3076  * require it.
3077  */
3079  MyProc->delayChkpt = true;
3080 
3081  /* WAL log truncation */
3082  WriteMTruncateXlogRec(newOldestMultiDB,
3083  oldestMulti, newOldestMulti,
3084  oldestOffset, newOldestOffset);
3085 
3086  /*
3087  * Update in-memory limits before performing the truncation, while inside
3088  * the critical section: Have to do it before truncation, to prevent
3089  * concurrent lookups of those values. Has to be inside the critical
3090  * section as otherwise a future call to this function would error out,
3091  * while looking up the oldest member in offsets, if our caller crashes
3092  * before updating the limits.
3093  */
3094  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
3095  MultiXactState->oldestMultiXactId = newOldestMulti;
3096  MultiXactState->oldestMultiXactDB = newOldestMultiDB;
3097  LWLockRelease(MultiXactGenLock);
3098 
3099  /* First truncate members */
3100  PerformMembersTruncation(oldestOffset, newOldestOffset);
3101 
3102  /* Then offsets */
3103  PerformOffsetsTruncation(oldestMulti, newOldestMulti);
3104 
3105  MyProc->delayChkpt = false;
3106 
3107  END_CRIT_SECTION();
3108  LWLockRelease(MultiXactTruncationLock);
3109 }
MultiXactId nextMXact
Definition: multixact.c:203
#define DEBUG1
Definition: elog.h:25
static bool SlruScanDirCbFindEarliest(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: multixact.c:2868
uint32 MultiXactOffset
Definition: c.h:599
PGPROC * MyProc
Definition: proc.c:68
MultiXactOffset nextOffset
Definition: multixact.c:206
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result)
Definition: multixact.c:2734
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:8328
static MultiXactStateData * MultiXactState
Definition: multixact.c:291
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
MultiXactId oldestMultiXactId
Definition: multixact.c:216
bool delayChkpt
Definition: proc.h:187
#define MultiXactIdToOffsetSegment(xid)
Definition: multixact.c:115
static void PerformMembersTruncation(MultiXactOffset oldestOffset, MultiXactOffset newOldestOffset)
Definition: multixact.c:2891
static void WriteMTruncateXlogRec(Oid oldestMultiDB, MultiXactId startTruncOff, MultiXactId endTruncOff, MultiXactOffset startTruncMemb, MultiXactOffset endTruncMemb)
Definition: multixact.c:3211
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
#define MULTIXACT_OFFSETS_PER_PAGE
Definition: multixact.c:109
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define ereport(elevel,...)
Definition: elog.h:157
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1553
#define MXOffsetToMemberSegment(xid)
Definition: multixact.c:160
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3159
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3173
int earliestExistingPage
Definition: multixact.c:2860
static void PerformOffsetsTruncation(MultiXactId oldestMulti, MultiXactId newOldestMulti)
Definition: multixact.c:2919

◆ WriteMTruncateXlogRec()

static void WriteMTruncateXlogRec ( Oid  oldestMultiDB,
MultiXactId  startTruncOff,
MultiXactId  endTruncOff,
MultiXactOffset  startTruncMemb,
MultiXactOffset  endTruncMemb 
)
static

Definition at line 3211 of file multixact.c.

References xl_multixact_truncate::endTruncMemb, xl_multixact_truncate::endTruncOff, xl_multixact_truncate::oldestMultiDB, SizeOfMultiXactTruncate, xl_multixact_truncate::startTruncMemb, xl_multixact_truncate::startTruncOff, XLOG_MULTIXACT_TRUNCATE_ID, XLogBeginInsert(), XLogFlush(), XLogInsert(), and XLogRegisterData().

Referenced by TruncateMultiXact().

3214 {
3215  XLogRecPtr recptr;
3216  xl_multixact_truncate xlrec;
3217 
3218  xlrec.oldestMultiDB = oldestMultiDB;
3219 
3220  xlrec.startTruncOff = startTruncOff;
3221  xlrec.endTruncOff = endTruncOff;
3222 
3223  xlrec.startTruncMemb = startTruncMemb;
3224  xlrec.endTruncMemb = endTruncMemb;
3225 
3226  XLogBeginInsert();
3227  XLogRegisterData((char *) (&xlrec), SizeOfMultiXactTruncate);
3228  recptr = XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_TRUNCATE_ID);
3229  XLogFlush(recptr);
3230 }
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2887
#define SizeOfMultiXactTruncate
Definition: multixact.h:100
MultiXactOffset endTruncMemb
Definition: multixact.h:97
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:434
MultiXactOffset startTruncMemb
Definition: multixact.h:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLOG_MULTIXACT_TRUNCATE_ID
Definition: multixact.h:75
MultiXactId startTruncOff
Definition: multixact.h:92
MultiXactId endTruncOff
Definition: multixact.h:93
void XLogBeginInsert(void)
Definition: xloginsert.c:135

◆ WriteMZeroPageXlogRec()

static void WriteMZeroPageXlogRec ( int  pageno,
uint8  info 
)
static

Definition at line 3197 of file multixact.c.

References XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by ZeroMultiXactMemberPage(), and ZeroMultiXactOffsetPage().

3198 {
3199  XLogBeginInsert();
3200  XLogRegisterData((char *) (&pageno), sizeof(int));
3201  (void) XLogInsert(RM_MULTIXACT_ID, info);
3202 }
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:434
void XLogBeginInsert(void)
Definition: xloginsert.c:135

◆ ZeroMultiXactMemberPage()

static int ZeroMultiXactMemberPage ( int  pageno,
bool  writeXlog 
)
static

Definition at line 1947 of file multixact.c.

References MultiXactMemberCtl, SimpleLruZeroPage(), WriteMZeroPageXlogRec(), and XLOG_MULTIXACT_ZERO_MEM_PAGE.

Referenced by BootStrapMultiXact(), ExtendMultiXactMember(), and multixact_redo().

1948 {
1949  int slotno;
1950 
1951  slotno = SimpleLruZeroPage(MultiXactMemberCtl, pageno);
1952 
1953  if (writeXlog)
1955 
1956  return slotno;
1957 }
#define XLOG_MULTIXACT_ZERO_MEM_PAGE
Definition: multixact.h:73
static void WriteMZeroPageXlogRec(int pageno, uint8 info)
Definition: multixact.c:3197
#define MultiXactMemberCtl
Definition: multixact.c:191
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:280

◆ ZeroMultiXactOffsetPage()

static int ZeroMultiXactOffsetPage ( int  pageno,
bool  writeXlog 
)
static

Definition at line 1931 of file multixact.c.

References MultiXactOffsetCtl, SimpleLruZeroPage(), WriteMZeroPageXlogRec(), and XLOG_MULTIXACT_ZERO_OFF_PAGE.

Referenced by BootStrapMultiXact(), ExtendMultiXactOffset(), MaybeExtendOffsetSlru(), and multixact_redo().

1932 {
1933  int slotno;
1934 
1935  slotno = SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
1936 
1937  if (writeXlog)
1939 
1940  return slotno;
1941 }
static void WriteMZeroPageXlogRec(int pageno, uint8 info)
Definition: multixact.c:3197
#define MultiXactOffsetCtl
Definition: multixact.c:190
#define XLOG_MULTIXACT_ZERO_OFF_PAGE
Definition: multixact.h:72
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:280

Variable Documentation

◆ MultiXactMemberCtlData

SlruCtlData MultiXactMemberCtlData
static

Definition at line 188 of file multixact.c.

◆ MultiXactOffsetCtlData

SlruCtlData MultiXactOffsetCtlData
static

Definition at line 187 of file multixact.c.

◆ MultiXactState

MultiXactStateData* MultiXactState
static

Definition at line 291 of file multixact.c.

◆ MXactCache

dlist_head MXactCache = DLIST_STATIC_INIT(MXactCache)
static

Definition at line 322 of file multixact.c.

◆ MXactCacheMembers

int MXactCacheMembers = 0
static

Definition at line 323 of file multixact.c.

Referenced by AtEOXact_MultiXact(), mXactCachePut(), and PostPrepare_MultiXact().

◆ MXactContext

MemoryContext MXactContext = NULL
static

Definition at line 324 of file multixact.c.

◆ OldestMemberMXactId

◆ OldestVisibleMXactId