LCOV - differential code coverage report
Current view: top level - src/backend/access/transam - multixact.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 72.4 % 860 623 6 17 125 89 10 333 15 265 134 330 4 11
Current Date: 2023-04-08 17:13:01 Functions: 83.3 % 60 50 10 43 1 6 10 41 2
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 0.0 % 3 0 3
Legend: Lines: hit not hit (120,180] days: 77.8 % 18 14 3 1 14 1
(180,240] days: 100.0 % 1 1 1
(240..) days: 72.6 % 838 608 16 125 89 10 333 265 133 330
Function coverage date bins:
(240..) days: 45.0 % 111 50 10 43 1 6 10 41

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * multixact.c
                                  4                 :  *      PostgreSQL multi-transaction-log manager
                                  5                 :  *
                                  6                 :  * The pg_multixact manager is a pg_xact-like manager that stores an array of
                                  7                 :  * MultiXactMember for each MultiXactId.  It is a fundamental part of the
                                  8                 :  * shared-row-lock implementation.  Each MultiXactMember is comprised of a
                                  9                 :  * TransactionId and a set of flag bits.  The name is a bit historical:
                                 10                 :  * originally, a MultiXactId consisted of more than one TransactionId (except
                                 11                 :  * in rare corner cases), hence "multi".  Nowadays, however, it's perfectly
                                 12                 :  * legitimate to have MultiXactIds that only include a single Xid.
                                 13                 :  *
                                 14                 :  * The meaning of the flag bits is opaque to this module, but they are mostly
                                 15                 :  * used in heapam.c to identify lock modes that each of the member transactions
                                 16                 :  * is holding on any given tuple.  This module just contains support to store
                                 17                 :  * and retrieve the arrays.
                                 18                 :  *
                                 19                 :  * We use two SLRU areas, one for storing the offsets at which the data
                                 20                 :  * starts for each MultiXactId in the other one.  This trick allows us to
                                 21                 :  * store variable length arrays of TransactionIds.  (We could alternatively
                                 22                 :  * use one area containing counts and TransactionIds, with valid MultiXactId
                                 23                 :  * values pointing at slots containing counts; but that way seems less robust
                                 24                 :  * since it would get completely confused if someone inquired about a bogus
                                 25                 :  * MultiXactId that pointed to an intermediate slot containing an XID.)
                                 26                 :  *
                                 27                 :  * XLOG interactions: this module generates a record whenever a new OFFSETs or
                                 28                 :  * MEMBERs page is initialized to zeroes, as well as an
                                 29                 :  * XLOG_MULTIXACT_CREATE_ID record whenever a new MultiXactId is defined.
                                 30                 :  * This module ignores the WAL rule "write xlog before data," because it
                                 31                 :  * suffices that actions recording a MultiXactId in a heap xmax do follow that
                                 32                 :  * rule.  The only way for the MXID to be referenced from any data page is for
                                 33                 :  * heap_lock_tuple() or heap_update() to have put it there, and each generates
                                 34                 :  * an XLOG record that must follow ours.  The normal LSN interlock between the
                                 35                 :  * data page and that XLOG record will ensure that our XLOG record reaches
                                 36                 :  * disk first.  If the SLRU members/offsets data reaches disk sooner than the
                                 37                 :  * XLOG records, we do not care; after recovery, no xmax will refer to it.  On
                                 38                 :  * the flip side, to ensure that all referenced entries _do_ reach disk, this
                                 39                 :  * module's XLOG records completely rebuild the data entered since the last
                                 40                 :  * checkpoint.  We flush and sync all dirty OFFSETs and MEMBERs pages to disk
                                 41                 :  * before each checkpoint is considered complete.
                                 42                 :  *
                                 43                 :  * Like clog.c, and unlike subtrans.c, we have to preserve state across
                                 44                 :  * crashes and ensure that MXID and offset numbering increases monotonically
                                 45                 :  * across a crash.  We do this in the same way as it's done for transaction
                                 46                 :  * IDs: the WAL record is guaranteed to contain evidence of every MXID we
                                 47                 :  * could need to worry about, and we just make sure that at the end of
                                 48                 :  * replay, the next-MXID and next-offset counters are at least as large as
                                 49                 :  * anything we saw during replay.
                                 50                 :  *
                                 51                 :  * We are able to remove segments no longer necessary by carefully tracking
                                 52                 :  * each table's used values: during vacuum, any multixact older than a certain
                                 53                 :  * value is removed; the cutoff value is stored in pg_class.  The minimum value
                                 54                 :  * across all tables in each database is stored in pg_database, and the global
                                 55                 :  * minimum across all databases is part of pg_control and is kept in shared
                                 56                 :  * memory.  Whenever that minimum is advanced, the SLRUs are truncated.
                                 57                 :  *
                                 58                 :  * When new multixactid values are to be created, care is taken that the
                                 59                 :  * counter does not fall within the wraparound horizon considering the global
                                 60                 :  * minimum value.
                                 61                 :  *
                                 62                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
                                 63                 :  * Portions Copyright (c) 1994, Regents of the University of California
                                 64                 :  *
                                 65                 :  * src/backend/access/transam/multixact.c
                                 66                 :  *
                                 67                 :  *-------------------------------------------------------------------------
                                 68                 :  */
                                 69                 : #include "postgres.h"
                                 70                 : 
                                 71                 : #include "access/multixact.h"
                                 72                 : #include "access/slru.h"
                                 73                 : #include "access/transam.h"
                                 74                 : #include "access/twophase.h"
                                 75                 : #include "access/twophase_rmgr.h"
                                 76                 : #include "access/xact.h"
                                 77                 : #include "access/xloginsert.h"
                                 78                 : #include "access/xlogutils.h"
                                 79                 : #include "catalog/pg_type.h"
                                 80                 : #include "commands/dbcommands.h"
                                 81                 : #include "funcapi.h"
                                 82                 : #include "lib/ilist.h"
                                 83                 : #include "miscadmin.h"
                                 84                 : #include "pg_trace.h"
                                 85                 : #include "postmaster/autovacuum.h"
                                 86                 : #include "storage/lmgr.h"
                                 87                 : #include "storage/pmsignal.h"
                                 88                 : #include "storage/proc.h"
                                 89                 : #include "storage/procarray.h"
                                 90                 : #include "utils/builtins.h"
                                 91                 : #include "utils/memutils.h"
                                 92                 : #include "utils/snapmgr.h"
                                 93                 : 
                                 94                 : 
                                 95                 : /*
                                 96                 :  * Defines for MultiXactOffset page sizes.  A page is the same BLCKSZ as is
                                 97                 :  * used everywhere else in Postgres.
                                 98                 :  *
                                 99                 :  * Note: because MultiXactOffsets are 32 bits and wrap around at 0xFFFFFFFF,
                                100                 :  * MultiXact page numbering also wraps around at
                                101                 :  * 0xFFFFFFFF/MULTIXACT_OFFSETS_PER_PAGE, and segment numbering at
                                102                 :  * 0xFFFFFFFF/MULTIXACT_OFFSETS_PER_PAGE/SLRU_PAGES_PER_SEGMENT.  We need
                                103                 :  * take no explicit notice of that fact in this module, except when comparing
                                104                 :  * segment and page numbers in TruncateMultiXact (see
                                105                 :  * MultiXactOffsetPagePrecedes).
                                106                 :  */
                                107                 : 
                                108                 : /* We need four bytes per offset */
                                109                 : #define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(MultiXactOffset))
                                110                 : 
                                111                 : #define MultiXactIdToOffsetPage(xid) \
                                112                 :     ((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
                                113                 : #define MultiXactIdToOffsetEntry(xid) \
                                114                 :     ((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
                                115                 : #define MultiXactIdToOffsetSegment(xid) (MultiXactIdToOffsetPage(xid) / SLRU_PAGES_PER_SEGMENT)
                                116                 : 
                                117                 : /*
                                118                 :  * The situation for members is a bit more complex: we store one byte of
                                119                 :  * additional flag bits for each TransactionId.  To do this without getting
                                120                 :  * into alignment issues, we store four bytes of flags, and then the
                                121                 :  * corresponding 4 Xids.  Each such 5-word (20-byte) set we call a "group", and
                                122                 :  * are stored as a whole in pages.  Thus, with 8kB BLCKSZ, we keep 409 groups
                                123                 :  * per page.  This wastes 12 bytes per page, but that's OK -- simplicity (and
                                124                 :  * performance) trumps space efficiency here.
                                125                 :  *
                                126                 :  * Note that the "offset" macros work with byte offset, not array indexes, so
                                127                 :  * arithmetic must be done using "char *" pointers.
                                128                 :  */
                                129                 : /* We need eight bits per xact, so one xact fits in a byte */
                                130                 : #define MXACT_MEMBER_BITS_PER_XACT          8
                                131                 : #define MXACT_MEMBER_FLAGS_PER_BYTE         1
                                132                 : #define MXACT_MEMBER_XACT_BITMASK   ((1 << MXACT_MEMBER_BITS_PER_XACT) - 1)
                                133                 : 
                                134                 : /* how many full bytes of flags are there in a group? */
                                135                 : #define MULTIXACT_FLAGBYTES_PER_GROUP       4
                                136                 : #define MULTIXACT_MEMBERS_PER_MEMBERGROUP   \
                                137                 :     (MULTIXACT_FLAGBYTES_PER_GROUP * MXACT_MEMBER_FLAGS_PER_BYTE)
                                138                 : /* size in bytes of a complete group */
                                139                 : #define MULTIXACT_MEMBERGROUP_SIZE \
                                140                 :     (sizeof(TransactionId) * MULTIXACT_MEMBERS_PER_MEMBERGROUP + MULTIXACT_FLAGBYTES_PER_GROUP)
                                141                 : #define MULTIXACT_MEMBERGROUPS_PER_PAGE (BLCKSZ / MULTIXACT_MEMBERGROUP_SIZE)
                                142                 : #define MULTIXACT_MEMBERS_PER_PAGE  \
                                143                 :     (MULTIXACT_MEMBERGROUPS_PER_PAGE * MULTIXACT_MEMBERS_PER_MEMBERGROUP)
                                144                 : 
                                145                 : /*
                                146                 :  * Because the number of items per page is not a divisor of the last item
                                147                 :  * number (member 0xFFFFFFFF), the last segment does not use the maximum number
                                148                 :  * of pages, and moreover the last used page therein does not use the same
                                149                 :  * number of items as previous pages.  (Another way to say it is that the
                                150                 :  * 0xFFFFFFFF member is somewhere in the middle of the last page, so the page
                                151                 :  * has some empty space after that item.)
                                152                 :  *
                                153                 :  * This constant is the number of members in the last page of the last segment.
                                154                 :  */
                                155                 : #define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE \
                                156                 :         ((uint32) ((0xFFFFFFFF % MULTIXACT_MEMBERS_PER_PAGE) + 1))
                                157                 : 
                                158                 : /* page in which a member is to be found */
                                159                 : #define MXOffsetToMemberPage(xid) ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
                                160                 : #define MXOffsetToMemberSegment(xid) (MXOffsetToMemberPage(xid) / SLRU_PAGES_PER_SEGMENT)
                                161                 : 
                                162                 : /* Location (byte offset within page) of flag word for a given member */
                                163                 : #define MXOffsetToFlagsOffset(xid) \
                                164                 :     ((((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) % \
                                165                 :       (TransactionId) MULTIXACT_MEMBERGROUPS_PER_PAGE) * \
                                166                 :      (TransactionId) MULTIXACT_MEMBERGROUP_SIZE)
                                167                 : #define MXOffsetToFlagsBitShift(xid) \
                                168                 :     (((xid) % (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) * \
                                169                 :      MXACT_MEMBER_BITS_PER_XACT)
                                170                 : 
                                171                 : /* Location (byte offset within page) of TransactionId of given member */
                                172                 : #define MXOffsetToMemberOffset(xid) \
                                173                 :     (MXOffsetToFlagsOffset(xid) + MULTIXACT_FLAGBYTES_PER_GROUP + \
                                174                 :      ((xid) % MULTIXACT_MEMBERS_PER_MEMBERGROUP) * sizeof(TransactionId))
                                175                 : 
                                176                 : /* Multixact members wraparound thresholds. */
                                177                 : #define MULTIXACT_MEMBER_SAFE_THRESHOLD     (MaxMultiXactOffset / 2)
                                178                 : #define MULTIXACT_MEMBER_DANGER_THRESHOLD   \
                                179                 :     (MaxMultiXactOffset - MaxMultiXactOffset / 4)
                                180                 : 
                                181                 : #define PreviousMultiXactId(xid) \
                                182                 :     ((xid) == FirstMultiXactId ? MaxMultiXactId : (xid) - 1)
                                183                 : 
                                184                 : /*
                                185                 :  * Links to shared-memory data structures for MultiXact control
                                186                 :  */
                                187                 : static SlruCtlData MultiXactOffsetCtlData;
                                188                 : static SlruCtlData MultiXactMemberCtlData;
                                189                 : 
                                190                 : #define MultiXactOffsetCtl  (&MultiXactOffsetCtlData)
                                191                 : #define MultiXactMemberCtl  (&MultiXactMemberCtlData)
                                192                 : 
                                193                 : /*
                                194                 :  * MultiXact state shared across all backends.  All this state is protected
                                195                 :  * by MultiXactGenLock.  (We also use MultiXactOffsetSLRULock and
                                196                 :  * MultiXactMemberSLRULock to guard accesses to the two sets of SLRU
                                197                 :  * buffers.  For concurrency's sake, we avoid holding more than one of these
                                198                 :  * locks at a time.)
                                199                 :  */
                                200                 : typedef struct MultiXactStateData
                                201                 : {
                                202                 :     /* next-to-be-assigned MultiXactId */
                                203                 :     MultiXactId nextMXact;
                                204                 : 
                                205                 :     /* next-to-be-assigned offset */
                                206                 :     MultiXactOffset nextOffset;
                                207                 : 
                                208                 :     /* Have we completed multixact startup? */
                                209                 :     bool        finishedStartup;
                                210                 : 
                                211                 :     /*
                                212                 :      * Oldest multixact that is still potentially referenced by a relation.
                                213                 :      * Anything older than this should not be consulted.  These values are
                                214                 :      * updated by vacuum.
                                215                 :      */
                                216                 :     MultiXactId oldestMultiXactId;
                                217                 :     Oid         oldestMultiXactDB;
                                218                 : 
                                219                 :     /*
                                220                 :      * Oldest multixact offset that is potentially referenced by a multixact
                                221                 :      * referenced by a relation.  We don't always know this value, so there's
                                222                 :      * a flag here to indicate whether or not we currently do.
                                223                 :      */
                                224                 :     MultiXactOffset oldestOffset;
                                225                 :     bool        oldestOffsetKnown;
                                226                 : 
                                227                 :     /* support for anti-wraparound measures */
                                228                 :     MultiXactId multiVacLimit;
                                229                 :     MultiXactId multiWarnLimit;
                                230                 :     MultiXactId multiStopLimit;
                                231                 :     MultiXactId multiWrapLimit;
                                232                 : 
                                233                 :     /* support for members anti-wraparound measures */
                                234                 :     MultiXactOffset offsetStopLimit;    /* known if oldestOffsetKnown */
                                235                 : 
                                236                 :     /*
                                237                 :      * Per-backend data starts here.  We have two arrays stored in the area
                                238                 :      * immediately following the MultiXactStateData struct. Each is indexed by
                                239                 :      * BackendId.
                                240                 :      *
                                241                 :      * In both arrays, there's a slot for all normal backends (1..MaxBackends)
                                242                 :      * followed by a slot for max_prepared_xacts prepared transactions. Valid
                                243                 :      * BackendIds start from 1; element zero of each array is never used.
                                244                 :      *
                                245                 :      * OldestMemberMXactId[k] is the oldest MultiXactId each backend's current
                                246                 :      * transaction(s) could possibly be a member of, or InvalidMultiXactId
                                247                 :      * when the backend has no live transaction that could possibly be a
                                248                 :      * member of a MultiXact.  Each backend sets its entry to the current
                                249                 :      * nextMXact counter just before first acquiring a shared lock in a given
                                250                 :      * transaction, and clears it at transaction end. (This works because only
                                251                 :      * during or after acquiring a shared lock could an XID possibly become a
                                252                 :      * member of a MultiXact, and that MultiXact would have to be created
                                253                 :      * during or after the lock acquisition.)
                                254                 :      *
                                255                 :      * OldestVisibleMXactId[k] is the oldest MultiXactId each backend's
                                256                 :      * current transaction(s) think is potentially live, or InvalidMultiXactId
                                257                 :      * when not in a transaction or not in a transaction that's paid any
                                258                 :      * attention to MultiXacts yet.  This is computed when first needed in a
                                259                 :      * given transaction, and cleared at transaction end.  We can compute it
                                260                 :      * as the minimum of the valid OldestMemberMXactId[] entries at the time
                                261                 :      * we compute it (using nextMXact if none are valid).  Each backend is
                                262                 :      * required not to attempt to access any SLRU data for MultiXactIds older
                                263                 :      * than its own OldestVisibleMXactId[] setting; this is necessary because
                                264                 :      * the relevant SLRU data can be concurrently truncated away.
                                265                 :      *
                                266                 :      * The oldest valid value among all of the OldestMemberMXactId[] and
                                267                 :      * OldestVisibleMXactId[] entries is considered by vacuum as the earliest
                                268                 :      * possible value still having any live member transaction -- OldestMxact.
                                269                 :      * Any value older than that is typically removed from tuple headers, or
                                270                 :      * "frozen" via being replaced with a new xmax.  VACUUM can sometimes even
                                271                 :      * remove an individual MultiXact xmax whose value is >= its OldestMxact
                                272                 :      * cutoff, though typically only when no individual member XID is still
                                273                 :      * running.  See FreezeMultiXactId for full details.
                                274                 :      *
                                275                 :      * Whenever VACUUM advances relminmxid, then either its OldestMxact cutoff
                                276                 :      * or the oldest extant Multi remaining in the table is used as the new
                                277                 :      * pg_class.relminmxid value (whichever is earlier).  The minimum of all
                                278                 :      * relminmxid values in each database is stored in pg_database.datminmxid.
                                279                 :      * In turn, the minimum of all of those values is stored in pg_control.
                                280                 :      * This is used as the truncation point for pg_multixact when unneeded
                                281                 :      * segments get removed by vac_truncate_clog() during vacuuming.
                                282                 :      */
                                283                 :     MultiXactId perBackendXactIds[FLEXIBLE_ARRAY_MEMBER];
                                284                 : } MultiXactStateData;
                                285                 : 
                                286                 : /*
                                287                 :  * Last element of OldestMemberMXactId and OldestVisibleMXactId arrays.
                                288                 :  * Valid elements are (1..MaxOldestSlot); element 0 is never used.
                                289                 :  */
                                290                 : #define MaxOldestSlot   (MaxBackends + max_prepared_xacts)
                                291                 : 
                                292                 : /* Pointers to the state data in shared memory */
                                293                 : static MultiXactStateData *MultiXactState;
                                294                 : static MultiXactId *OldestMemberMXactId;
                                295                 : static MultiXactId *OldestVisibleMXactId;
                                296                 : 
                                297                 : 
                                298                 : /*
                                299                 :  * Definitions for the backend-local MultiXactId cache.
                                300                 :  *
                                301                 :  * We use this cache to store known MultiXacts, so we don't need to go to
                                302                 :  * SLRU areas every time.
                                303                 :  *
                                304                 :  * The cache lasts for the duration of a single transaction, the rationale
                                305                 :  * for this being that most entries will contain our own TransactionId and
                                306                 :  * so they will be uninteresting by the time our next transaction starts.
                                307                 :  * (XXX not clear that this is correct --- other members of the MultiXact
                                308                 :  * could hang around longer than we did.  However, it's not clear what a
                                309                 :  * better policy for flushing old cache entries would be.)  FIXME actually
                                310                 :  * this is plain wrong now that multixact's may contain update Xids.
                                311                 :  *
                                312                 :  * We allocate the cache entries in a memory context that is deleted at
                                313                 :  * transaction end, so we don't need to do retail freeing of entries.
                                314                 :  */
                                315                 : typedef struct mXactCacheEnt
                                316                 : {
                                317                 :     MultiXactId multi;
                                318                 :     int         nmembers;
                                319                 :     dlist_node  node;
                                320                 :     MultiXactMember members[FLEXIBLE_ARRAY_MEMBER];
                                321                 : } mXactCacheEnt;
                                322                 : 
                                323                 : #define MAX_CACHE_ENTRIES   256
                                324                 : static dclist_head MXactCache = DCLIST_STATIC_INIT(MXactCache);
                                325                 : static MemoryContext MXactContext = NULL;
                                326                 : 
                                327                 : #ifdef MULTIXACT_DEBUG
                                328                 : #define debug_elog2(a,b) elog(a,b)
                                329                 : #define debug_elog3(a,b,c) elog(a,b,c)
                                330                 : #define debug_elog4(a,b,c,d) elog(a,b,c,d)
                                331                 : #define debug_elog5(a,b,c,d,e) elog(a,b,c,d,e)
                                332                 : #define debug_elog6(a,b,c,d,e,f) elog(a,b,c,d,e,f)
                                333                 : #else
                                334                 : #define debug_elog2(a,b)
                                335                 : #define debug_elog3(a,b,c)
                                336                 : #define debug_elog4(a,b,c,d)
                                337                 : #define debug_elog5(a,b,c,d,e)
                                338                 : #define debug_elog6(a,b,c,d,e,f)
                                339                 : #endif
                                340                 : 
                                341                 : /* internal MultiXactId management */
                                342                 : static void MultiXactIdSetOldestVisible(void);
                                343                 : static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
                                344                 :                                int nmembers, MultiXactMember *members);
                                345                 : static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset);
                                346                 : 
                                347                 : /* MultiXact cache management */
                                348                 : static int  mxactMemberComparator(const void *arg1, const void *arg2);
                                349                 : static MultiXactId mXactCacheGetBySet(int nmembers, MultiXactMember *members);
                                350                 : static int  mXactCacheGetById(MultiXactId multi, MultiXactMember **members);
                                351                 : static void mXactCachePut(MultiXactId multi, int nmembers,
                                352                 :                           MultiXactMember *members);
                                353                 : 
                                354                 : static char *mxstatus_to_string(MultiXactStatus status);
                                355                 : 
                                356                 : /* management of SLRU infrastructure */
                                357                 : static int  ZeroMultiXactOffsetPage(int pageno, bool writeXlog);
                                358                 : static int  ZeroMultiXactMemberPage(int pageno, bool writeXlog);
                                359                 : static bool MultiXactOffsetPagePrecedes(int page1, int page2);
                                360                 : static bool MultiXactMemberPagePrecedes(int page1, int page2);
                                361                 : static bool MultiXactOffsetPrecedes(MultiXactOffset offset1,
                                362                 :                                     MultiXactOffset offset2);
                                363                 : static void ExtendMultiXactOffset(MultiXactId multi);
                                364                 : static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
                                365                 : static bool MultiXactOffsetWouldWrap(MultiXactOffset boundary,
                                366                 :                                      MultiXactOffset start, uint32 distance);
                                367                 : static bool SetOffsetVacuumLimit(bool is_startup);
                                368                 : static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result);
                                369                 : static void WriteMZeroPageXlogRec(int pageno, uint8 info);
                                370                 : static void WriteMTruncateXlogRec(Oid oldestMultiDB,
                                371                 :                                   MultiXactId startTruncOff,
                                372                 :                                   MultiXactId endTruncOff,
                                373                 :                                   MultiXactOffset startTruncMemb,
                                374                 :                                   MultiXactOffset endTruncMemb);
                                375                 : 
                                376                 : 
                                377                 : /*
                                378                 :  * MultiXactIdCreate
                                379                 :  *      Construct a MultiXactId representing two TransactionIds.
                                380                 :  *
                                381                 :  * The two XIDs must be different, or be requesting different statuses.
                                382                 :  *
                                383                 :  * NB - we don't worry about our local MultiXactId cache here, because that
                                384                 :  * is handled by the lower-level routines.
                                385                 :  */
                                386                 : MultiXactId
 3728 alvherre                  387 GIC        1019 : MultiXactIdCreate(TransactionId xid1, MultiXactStatus status1,
 3728 alvherre                  388 ECB             :                   TransactionId xid2, MultiXactStatus status2)
                                389                 : {
                                390                 :     MultiXactId newMulti;
                                391                 :     MultiXactMember members[2];
                                392                 : 
  163 peter                     393 GNC        1019 :     Assert(TransactionIdIsValid(xid1));
                                394            1019 :     Assert(TransactionIdIsValid(xid2));
 6550 tgl                       395 ECB             : 
 3728 alvherre                  396 GIC        1019 :     Assert(!TransactionIdEquals(xid1, xid2) || (status1 != status2));
 6550 tgl                       397 ECB             : 
                                398                 :     /* MultiXactIdSetOldestMember() must have been called already. */
 3401 alvherre                  399 GIC        1019 :     Assert(MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]));
 3401 alvherre                  400 ECB             : 
                                401                 :     /*
                                402                 :      * Note: unlike MultiXactIdExpand, we don't bother to check that both XIDs
                                403                 :      * are still running.  In typical usage, xid2 will be our own XID and the
                                404                 :      * caller just did a check on xid1, so it'd be wasted effort.
                                405                 :      */
                                406                 : 
 3728 alvherre                  407 GIC        1019 :     members[0].xid = xid1;
 3728 alvherre                  408 CBC        1019 :     members[0].status = status1;
                                409            1019 :     members[1].xid = xid2;
                                410            1019 :     members[1].status = status2;
 6550 tgl                       411 ECB             : 
 3401 alvherre                  412 GIC        1019 :     newMulti = MultiXactIdCreateFromMembers(2, members);
 6550 tgl                       413 ECB             : 
                                414                 :     debug_elog3(DEBUG2, "Create: %s",
                                415                 :                 mxid_to_string(newMulti, 2, members));
                                416                 : 
 6550 tgl                       417 GIC        1019 :     return newMulti;
 6550 tgl                       418 ECB             : }
                                419                 : 
                                420                 : /*
                                421                 :  * MultiXactIdExpand
                                422                 :  *      Add a TransactionId to a pre-existing MultiXactId.
                                423                 :  *
                                424                 :  * If the TransactionId is already a member of the passed MultiXactId with the
                                425                 :  * same status, just return it as-is.
                                426                 :  *
                                427                 :  * Note that we do NOT actually modify the membership of a pre-existing
                                428                 :  * MultiXactId; instead we create a new one.  This is necessary to avoid
                                429                 :  * a race condition against code trying to wait for one MultiXactId to finish;
                                430                 :  * see notes in heapam.c.
                                431                 :  *
                                432                 :  * NB - we don't worry about our local MultiXactId cache here, because that
                                433                 :  * is handled by the lower-level routines.
                                434                 :  *
                                435                 :  * Note: It is critical that MultiXactIds that come from an old cluster (i.e.
                                436                 :  * one upgraded by pg_upgrade from a cluster older than this feature) are not
                                437                 :  * passed in.
                                438                 :  */
                                439                 : MultiXactId
 3728 alvherre                  440 GIC          97 : MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
 6555 tgl                       441 ECB             : {
                                442                 :     MultiXactId newMulti;
                                443                 :     MultiXactMember *members;
                                444                 :     MultiXactMember *newMembers;
                                445                 :     int         nmembers;
                                446                 :     int         i;
                                447                 :     int         j;
                                448                 : 
  163 peter                     449 GNC          97 :     Assert(MultiXactIdIsValid(multi));
                                450              97 :     Assert(TransactionIdIsValid(xid));
 6555 tgl                       451 ECB             : 
                                452                 :     /* MultiXactIdSetOldestMember() must have been called already. */
 3401 alvherre                  453 GIC          97 :     Assert(MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]));
 3401 alvherre                  454 ECB             : 
                                455                 :     debug_elog5(DEBUG2, "Expand: received multi %u, xid %u status %s",
                                456                 :                 multi, xid, mxstatus_to_string(status));
                                457                 : 
                                458                 :     /*
                                459                 :      * Note: we don't allow for old multis here.  The reason is that the only
                                460                 :      * caller of this function does a check that the multixact is no longer
                                461                 :      * running.
                                462                 :      */
 3176 alvherre                  463 GIC          97 :     nmembers = GetMultiXactIdMembers(multi, &members, false, false);
 6555 tgl                       464 ECB             : 
 6555 tgl                       465 GIC          97 :     if (nmembers < 0)
 6555 tgl                       466 ECB             :     {
                                467                 :         MultiXactMember member;
                                468                 : 
                                469                 :         /*
                                470                 :          * The MultiXactId is obsolete.  This can only happen if all the
                                471                 :          * MultiXactId members stop running between the caller checking and
                                472                 :          * passing it to us.  It would be better to return that fact to the
                                473                 :          * caller, but it would complicate the API and it's unlikely to happen
                                474                 :          * too often, so just deal with it by creating a singleton MultiXact.
                                475                 :          */
 3728 alvherre                  476 UIC           0 :         member.xid = xid;
 3728 alvherre                  477 UBC           0 :         member.status = status;
 3401                           478               0 :         newMulti = MultiXactIdCreateFromMembers(1, &member);
 6555 tgl                       479 EUB             : 
                                480                 :         debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
                                481                 :                     multi, newMulti);
 6555 tgl                       482 UIC           0 :         return newMulti;
 6555 tgl                       483 EUB             :     }
                                484                 : 
                                485                 :     /*
                                486                 :      * If the TransactionId is already a member of the MultiXactId with the
                                487                 :      * same status, just return the existing MultiXactId.
                                488                 :      */
 6555 tgl                       489 GIC         301 :     for (i = 0; i < nmembers; i++)
 6555 tgl                       490 ECB             :     {
 3728 alvherre                  491 GIC         204 :         if (TransactionIdEquals(members[i].xid, xid) &&
 3728 alvherre                  492 CBC          54 :             (members[i].status == status))
 6555 tgl                       493 ECB             :         {
                                494                 :             debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
                                495                 :                         xid, multi);
 6546 tgl                       496 UIC           0 :             pfree(members);
 6555 tgl                       497 UBC           0 :             return multi;
 6555 tgl                       498 EUB             :         }
                                499                 :     }
                                500                 : 
                                501                 :     /*
                                502                 :      * Determine which of the members of the MultiXactId are still of
                                503                 :      * interest. This is any running transaction, and also any transaction
                                504                 :      * that grabbed something stronger than just a lock and was committed. (An
                                505                 :      * update that aborted is of no interest here; and having more than one
                                506                 :      * update Xid in a multixact would cause errors elsewhere.)
                                507                 :      *
                                508                 :      * Removing dead members is not just an optimization: freezing of tuples
                                509                 :      * whose Xmax are multis depends on this behavior.
                                510                 :      *
                                511                 :      * Note we have the same race condition here as above: j could be 0 at the
                                512                 :      * end of the loop.
                                513                 :      */
                                514                 :     newMembers = (MultiXactMember *)
 3728 alvherre                  515 GIC          97 :         palloc(sizeof(MultiXactMember) * (nmembers + 1));
 6555 tgl                       516 ECB             : 
 6555 tgl                       517 GIC         301 :     for (i = 0, j = 0; i < nmembers; i++)
 6555 tgl                       518 ECB             :     {
 3728 alvherre                  519 GIC         204 :         if (TransactionIdIsInProgress(members[i].xid) ||
 3272 alvherre                  520 CBC          44 :             (ISUPDATE_from_mxstatus(members[i].status) &&
 3728                           521               6 :              TransactionIdDidCommit(members[i].xid)))
 3728 alvherre                  522 ECB             :         {
 3728 alvherre                  523 GIC         166 :             newMembers[j].xid = members[i].xid;
 3728 alvherre                  524 CBC         166 :             newMembers[j++].status = members[i].status;
 3728 alvherre                  525 ECB             :         }
                                526                 :     }
                                527                 : 
 3728 alvherre                  528 GIC          97 :     newMembers[j].xid = xid;
 3728 alvherre                  529 CBC          97 :     newMembers[j++].status = status;
 3401                           530              97 :     newMulti = MultiXactIdCreateFromMembers(j, newMembers);
 6555 tgl                       531 ECB             : 
 6555 tgl                       532 GIC          97 :     pfree(members);
 6555 tgl                       533 CBC          97 :     pfree(newMembers);
 6555 tgl                       534 ECB             : 
                                535                 :     debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
                                536                 : 
 6555 tgl                       537 GIC          97 :     return newMulti;
 6555 tgl                       538 ECB             : }
                                539                 : 
                                540                 : /*
                                541                 :  * MultiXactIdIsRunning
                                542                 :  *      Returns whether a MultiXactId is "running".
                                543                 :  *
                                544                 :  * We return true if at least one member of the given MultiXactId is still
                                545                 :  * running.  Note that a "false" result is certain not to change,
                                546                 :  * because it is not legal to add members to an existing MultiXactId.
                                547                 :  *
                                548                 :  * Caller is expected to have verified that the multixact does not come from
                                549                 :  * a pg_upgraded share-locked tuple.
                                550                 :  */
                                551                 : bool
 3176 alvherre                  552 GIC         961 : MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
 6555 tgl                       553 ECB             : {
                                554                 :     MultiXactMember *members;
                                555                 :     int         nmembers;
                                556                 :     int         i;
                                557                 : 
                                558                 :     debug_elog3(DEBUG2, "IsRunning %u?", multi);
                                559                 : 
                                560                 :     /*
                                561                 :      * "false" here means we assume our callers have checked that the given
                                562                 :      * multi cannot possibly come from a pg_upgraded database.
                                563                 :      */
 3176 alvherre                  564 GIC         961 :     nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly);
 6555 tgl                       565 ECB             : 
 2921 alvherre                  566 GIC         961 :     if (nmembers <= 0)
 6555 tgl                       567 ECB             :     {
                                568                 :         debug_elog2(DEBUG2, "IsRunning: no members");
 6555 tgl                       569 GIC         643 :         return false;
 6555 tgl                       570 ECB             :     }
                                571                 : 
                                572                 :     /*
                                573                 :      * Checking for myself is cheap compared to looking in shared memory;
                                574                 :      * return true if any live subtransaction of the current top-level
                                575                 :      * transaction is a member.
                                576                 :      *
                                577                 :      * This is not needed for correctness, it's just a fast path.
                                578                 :      */
 6555 tgl                       579 GIC         736 :     for (i = 0; i < nmembers; i++)
 6555 tgl                       580 ECB             :     {
 3728 alvherre                  581 GIC         573 :         if (TransactionIdIsCurrentTransactionId(members[i].xid))
 6555 tgl                       582 ECB             :         {
                                583                 :             debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
 6546 tgl                       584 GIC         155 :             pfree(members);
 6555 tgl                       585 CBC         155 :             return true;
 6555 tgl                       586 ECB             :         }
                                587                 :     }
                                588                 : 
                                589                 :     /*
                                590                 :      * This could be made faster by having another entry point in procarray.c,
                                591                 :      * walking the PGPROC array only once for all the members.  But in most
                                592                 :      * cases nmembers should be small enough that it doesn't much matter.
                                593                 :      */
 6555 tgl                       594 GIC         296 :     for (i = 0; i < nmembers; i++)
 6555 tgl                       595 ECB             :     {
 3728 alvherre                  596 GIC         248 :         if (TransactionIdIsInProgress(members[i].xid))
 6555 tgl                       597 ECB             :         {
                                598                 :             debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
                                599                 :                         i, members[i].xid);
 6546 tgl                       600 GIC         115 :             pfree(members);
 6555 tgl                       601 CBC         115 :             return true;
 6555 tgl                       602 ECB             :         }
                                603                 :     }
                                604                 : 
 6555 tgl                       605 GIC          48 :     pfree(members);
 6546 tgl                       606 ECB             : 
                                607                 :     debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
                                608                 : 
 6555 tgl                       609 GIC          48 :     return false;
 6555 tgl                       610 ECB             : }
                                611                 : 
                                612                 : /*
                                613                 :  * MultiXactIdSetOldestMember
                                614                 :  *      Save the oldest MultiXactId this transaction could be a member of.
                                615                 :  *
                                616                 :  * We set the OldestMemberMXactId for a given transaction the first time it's
                                617                 :  * going to do some operation that might require a MultiXactId (tuple lock,
                                618                 :  * update or delete).  We need to do this even if we end up using a
                                619                 :  * TransactionId instead of a MultiXactId, because there is a chance that
                                620                 :  * another transaction would add our XID to a MultiXactId.
                                621                 :  *
                                622                 :  * The value to set is the next-to-be-assigned MultiXactId, so this is meant to
                                623                 :  * be called just before doing any such possibly-MultiXactId-able operation.
                                624                 :  */
                                625                 : void
 6555 tgl                       626 GIC     1917382 : MultiXactIdSetOldestMember(void)
 6555 tgl                       627 ECB             : {
 6555 tgl                       628 GIC     1917382 :     if (!MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]))
 6555 tgl                       629 ECB             :     {
                                630                 :         MultiXactId nextMXact;
                                631                 : 
                                632                 :         /*
                                633                 :          * You might think we don't need to acquire a lock here, since
                                634                 :          * fetching and storing of TransactionIds is probably atomic, but in
                                635                 :          * fact we do: suppose we pick up nextMXact and then lose the CPU for
                                636                 :          * a long time.  Someone else could advance nextMXact, and then
                                637                 :          * another someone else could compute an OldestVisibleMXactId that
                                638                 :          * would be after the value we are going to store when we get control
                                639                 :          * back.  Which would be wrong.
                                640                 :          *
                                641                 :          * Note that a shared lock is sufficient, because it's enough to stop
                                642                 :          * someone from advancing nextMXact; and nobody else could be trying
                                643                 :          * to write to our OldestMember entry, only reading (and we assume
                                644                 :          * storing it is atomic.)
                                645                 :          */
 3384 alvherre                  646 GIC      179372 :         LWLockAcquire(MultiXactGenLock, LW_SHARED);
 6555 tgl                       647 ECB             : 
                                648                 :         /*
                                649                 :          * We have to beware of the possibility that nextMXact is in the
                                650                 :          * wrapped-around state.  We don't fix the counter itself here, but we
                                651                 :          * must be sure to store a valid value in our array entry.
                                652                 :          */
 6555 tgl                       653 GIC      179372 :         nextMXact = MultiXactState->nextMXact;
 6555 tgl                       654 CBC      179372 :         if (nextMXact < FirstMultiXactId)
 6555 tgl                       655 LBC           0 :             nextMXact = FirstMultiXactId;
 6555 tgl                       656 EUB             : 
 6555 tgl                       657 GIC      179372 :         OldestMemberMXactId[MyBackendId] = nextMXact;
 6555 tgl                       658 ECB             : 
 6555 tgl                       659 GIC      179372 :         LWLockRelease(MultiXactGenLock);
 6555 tgl                       660 ECB             : 
                                661                 :         debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
                                662                 :                     MyBackendId, nextMXact);
                                663                 :     }
 6555 tgl                       664 GIC     1917382 : }
 6555 tgl                       665 ECB             : 
                                666                 : /*
                                667                 :  * MultiXactIdSetOldestVisible
                                668                 :  *      Save the oldest MultiXactId this transaction considers possibly live.
                                669                 :  *
                                670                 :  * We set the OldestVisibleMXactId for a given transaction the first time
                                671                 :  * it's going to inspect any MultiXactId.  Once we have set this, we are
                                672                 :  * guaranteed that SLRU data for MultiXactIds >= our own OldestVisibleMXactId
                                673                 :  * won't be truncated away.
                                674                 :  *
                                675                 :  * The value to set is the oldest of nextMXact and all the valid per-backend
                                676                 :  * OldestMemberMXactId[] entries.  Because of the locking we do, we can be
                                677                 :  * certain that no subsequent call to MultiXactIdSetOldestMember can set
                                678                 :  * an OldestMemberMXactId[] entry older than what we compute here.  Therefore
                                679                 :  * there is no live transaction, now or later, that can be a member of any
                                680                 :  * MultiXactId older than the OldestVisibleMXactId we compute here.
                                681                 :  */
                                682                 : static void
 6555 tgl                       683 GIC         877 : MultiXactIdSetOldestVisible(void)
 6555 tgl                       684 ECB             : {
 6555 tgl                       685 GIC         877 :     if (!MultiXactIdIsValid(OldestVisibleMXactId[MyBackendId]))
 6555 tgl                       686 ECB             :     {
                                687                 :         MultiXactId oldestMXact;
                                688                 :         int         i;
                                689                 : 
 6555 tgl                       690 GIC         222 :         LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
 6555 tgl                       691 ECB             : 
                                692                 :         /*
                                693                 :          * We have to beware of the possibility that nextMXact is in the
                                694                 :          * wrapped-around state.  We don't fix the counter itself here, but we
                                695                 :          * must be sure to store a valid value in our array entry.
                                696                 :          */
 6555 tgl                       697 GIC         222 :         oldestMXact = MultiXactState->nextMXact;
 6555 tgl                       698 CBC         222 :         if (oldestMXact < FirstMultiXactId)
 6555 tgl                       699 LBC           0 :             oldestMXact = FirstMultiXactId;
 6555 tgl                       700 EUB             : 
  362 rhaas                     701 GIC       26738 :         for (i = 1; i <= MaxOldestSlot; i++)
 6555 tgl                       702 ECB             :         {
 6555 tgl                       703 GIC       26516 :             MultiXactId thisoldest = OldestMemberMXactId[i];
 6555 tgl                       704 ECB             : 
 6555 tgl                       705 GIC       26740 :             if (MultiXactIdIsValid(thisoldest) &&
 6555 tgl                       706 CBC         224 :                 MultiXactIdPrecedes(thisoldest, oldestMXact))
                                707             114 :                 oldestMXact = thisoldest;
 6555 tgl                       708 ECB             :         }
                                709                 : 
 6555 tgl                       710 GIC         222 :         OldestVisibleMXactId[MyBackendId] = oldestMXact;
 6555 tgl                       711 ECB             : 
 6555 tgl                       712 GIC         222 :         LWLockRelease(MultiXactGenLock);
 6555 tgl                       713 ECB             : 
                                714                 :         debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
                                715                 :                     MyBackendId, oldestMXact);
                                716                 :     }
 6555 tgl                       717 GIC         877 : }
 6555 tgl                       718 ECB             : 
                                719                 : /*
                                720                 :  * ReadNextMultiXactId
                                721                 :  *      Return the next MultiXactId to be assigned, but don't allocate it
                                722                 :  */
                                723                 : MultiXactId
 3728 alvherre                  724 GIC       81007 : ReadNextMultiXactId(void)
 6460 tgl                       725 ECB             : {
                                726                 :     MultiXactId mxid;
                                727                 : 
                                728                 :     /* XXX we could presumably do this without a lock. */
 3728 alvherre                  729 GIC       81007 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
 3728 alvherre                  730 CBC       81007 :     mxid = MultiXactState->nextMXact;
                                731           81007 :     LWLockRelease(MultiXactGenLock);
 6460 tgl                       732 ECB             : 
 3728 alvherre                  733 GIC       81007 :     if (mxid < FirstMultiXactId)
 3728 alvherre                  734 LBC           0 :         mxid = FirstMultiXactId;
 6460 tgl                       735 EUB             : 
 3728 alvherre                  736 GIC       81007 :     return mxid;
 6460 tgl                       737 ECB             : }
                                738                 : 
                                739                 : /*
                                740                 :  * ReadMultiXactIdRange
                                741                 :  *      Get the range of IDs that may still be referenced by a relation.
                                742                 :  */
                                743                 : void
  899 rhaas                     744 GIC        1202 : ReadMultiXactIdRange(MultiXactId *oldest, MultiXactId *next)
  899 rhaas                     745 ECB             : {
  899 rhaas                     746 GIC        1202 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
  899 rhaas                     747 CBC        1202 :     *oldest = MultiXactState->oldestMultiXactId;
                                748            1202 :     *next = MultiXactState->nextMXact;
                                749            1202 :     LWLockRelease(MultiXactGenLock);
  899 rhaas                     750 ECB             : 
  899 rhaas                     751 GIC        1202 :     if (*oldest < FirstMultiXactId)
  899 rhaas                     752 LBC           0 :         *oldest = FirstMultiXactId;
  899 rhaas                     753 GBC        1202 :     if (*next < FirstMultiXactId)
  899 rhaas                     754 LBC           0 :         *next = FirstMultiXactId;
  899 rhaas                     755 GBC        1202 : }
  899 rhaas                     756 ECB             : 
                                757                 : 
                                758                 : /*
                                759                 :  * MultiXactIdCreateFromMembers
                                760                 :  *      Make a new MultiXactId from the specified set of members
                                761                 :  *
                                762                 :  * Make XLOG, SLRU and cache entries for a new MultiXactId, recording the
                                763                 :  * given TransactionIds as members.  Returns the newly created MultiXactId.
                                764                 :  *
                                765                 :  * NB: the passed members[] array will be sorted in-place.
                                766                 :  */
                                767                 : MultiXactId
 3401 alvherre                  768 GIC        1117 : MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
 6555 tgl                       769 ECB             : {
                                770                 :     MultiXactId multi;
                                771                 :     MultiXactOffset offset;
                                772                 :     xl_multixact_create xlrec;
                                773                 : 
                                774                 :     debug_elog3(DEBUG2, "Create: %s",
                                775                 :                 mxid_to_string(InvalidMultiXactId, nmembers, members));
                                776                 : 
                                777                 :     /*
                                778                 :      * See if the same set of members already exists in our cache; if so, just
                                779                 :      * re-use that MultiXactId.  (Note: it might seem that looking in our
                                780                 :      * cache is insufficient, and we ought to search disk to see if a
                                781                 :      * duplicate definition already exists.  But since we only ever create
                                782                 :      * MultiXacts containing our own XID, in most cases any such MultiXacts
                                783                 :      * were in fact created by us, and so will be in our cache.  There are
                                784                 :      * corner cases where someone else added us to a MultiXact without our
                                785                 :      * knowledge, but it's not worth checking for.)
                                786                 :      */
 3728 alvherre                  787 GIC        1117 :     multi = mXactCacheGetBySet(nmembers, members);
 6555 tgl                       788 CBC        1117 :     if (MultiXactIdIsValid(multi))
 6460 tgl                       789 ECB             :     {
                                790                 :         debug_elog2(DEBUG2, "Create: in cache!");
 6555 tgl                       791 GIC         845 :         return multi;
 6555 tgl                       792 ECB             :     }
                                793                 : 
                                794                 :     /* Verify that there is a single update Xid among the given members. */
                                795                 :     {
                                796                 :         int         i;
 3272 alvherre                  797 GIC         272 :         bool        has_update = false;
 3272 alvherre                  798 ECB             : 
 3272 alvherre                  799 GIC         879 :         for (i = 0; i < nmembers; i++)
 3272 alvherre                  800 ECB             :         {
 3272 alvherre                  801 GIC         607 :             if (ISUPDATE_from_mxstatus(members[i].status))
 3272 alvherre                  802 ECB             :             {
 3272 alvherre                  803 GIC         130 :                 if (has_update)
  136 alvherre                  804 LBC           0 :                     elog(ERROR, "new multixact has more than one updating member: %s",
  136 alvherre                  805 EUB             :                          mxid_to_string(InvalidMultiXactId, nmembers, members));
 3272 alvherre                  806 GIC         130 :                 has_update = true;
 3272 alvherre                  807 ECB             :             }
                                808                 :         }
                                809                 :     }
                                810                 : 
                                811                 :     /*
                                812                 :      * Assign the MXID and offsets range to use, and make sure there is space
                                813                 :      * in the OFFSETs and MEMBERs files.  NB: this routine does
                                814                 :      * START_CRIT_SECTION().
                                815                 :      *
                                816                 :      * Note: unlike MultiXactIdCreate and MultiXactIdExpand, we do not check
                                817                 :      * that we've called MultiXactIdSetOldestMember here.  This is because
                                818                 :      * this routine is used in some places to create new MultiXactIds of which
                                819                 :      * the current backend is not a member, notably during freezing of multis
                                820                 :      * in vacuum.  During vacuum, in particular, it would be unacceptable to
                                821                 :      * keep OldestMulti set, in case it runs for long.
                                822                 :      */
 3728 alvherre                  823 GIC         272 :     multi = GetNewMultiXactId(nmembers, &offset);
 6555 tgl                       824 ECB             : 
                                825                 :     /* Make an XLOG entry describing the new MXID. */
 6514 tgl                       826 GIC         272 :     xlrec.mid = multi;
 6514 tgl                       827 CBC         272 :     xlrec.moff = offset;
 3728 alvherre                  828             272 :     xlrec.nmembers = nmembers;
 6514 tgl                       829 ECB             : 
                                830                 :     /*
                                831                 :      * XXX Note: there's a lot of padding space in MultiXactMember.  We could
                                832                 :      * find a more compact representation of this Xlog record -- perhaps all
                                833                 :      * the status flags in one XLogRecData, then all the xids in another one?
                                834                 :      * Not clear that it's worth the trouble though.
                                835                 :      */
 3062 heikki.linnakangas        836 GIC         272 :     XLogBeginInsert();
 3062 heikki.linnakangas        837 CBC         272 :     XLogRegisterData((char *) (&xlrec), SizeOfMultiXactCreate);
                                838             272 :     XLogRegisterData((char *) members, nmembers * sizeof(MultiXactMember));
 3728 alvherre                  839 ECB             : 
 3062 heikki.linnakangas        840 GIC         272 :     (void) XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_CREATE_ID);
 6514 tgl                       841 ECB             : 
                                842                 :     /* Now enter the information into the OFFSETs and MEMBERs logs */
 3728 alvherre                  843 GIC         272 :     RecordNewMultiXact(multi, offset, nmembers, members);
 6514 tgl                       844 ECB             : 
                                845                 :     /* Done with critical section */
 6372 tgl                       846 GIC         272 :     END_CRIT_SECTION();
 6372 tgl                       847 ECB             : 
                                848                 :     /* Store the new MultiXactId in the local cache, too */
 3728 alvherre                  849 GIC         272 :     mXactCachePut(multi, nmembers, members);
 6514 tgl                       850 ECB             : 
                                851                 :     debug_elog2(DEBUG2, "Create: all done");
                                852                 : 
 6514 tgl                       853 GIC         272 :     return multi;
 6514 tgl                       854 ECB             : }
                                855                 : 
                                856                 : /*
                                857                 :  * RecordNewMultiXact
                                858                 :  *      Write info about a new multixact into the offsets and members files
                                859                 :  *
                                860                 :  * This is broken out of MultiXactIdCreateFromMembers so that xlog replay can
                                861                 :  * use it.
                                862                 :  */
                                863                 : static void
 6514 tgl                       864 GIC         274 : RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
 3728 alvherre                  865 ECB             :                    int nmembers, MultiXactMember *members)
                                866                 : {
                                867                 :     int         pageno;
                                868                 :     int         prev_pageno;
                                869                 :     int         entryno;
                                870                 :     int         slotno;
                                871                 :     MultiXactOffset *offptr;
                                872                 :     int         i;
                                873                 : 
 1059 tgl                       874 GIC         274 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
 6555 tgl                       875 ECB             : 
 6555 tgl                       876 GIC         274 :     pageno = MultiXactIdToOffsetPage(multi);
 6555 tgl                       877 CBC         274 :     entryno = MultiXactIdToOffsetEntry(multi);
 6555 tgl                       878 ECB             : 
                                879                 :     /*
                                880                 :      * Note: we pass the MultiXactId to SimpleLruReadPage as the "transaction"
                                881                 :      * to complain about if there's any I/O error.  This is kinda bogus, but
                                882                 :      * since the errors will always give the full pathname, it should be clear
                                883                 :      * enough that a MultiXactId is really involved.  Perhaps someday we'll
                                884                 :      * take the trouble to generalize the slru.c error reporting code.
                                885                 :      */
 5730 tgl                       886 GIC         274 :     slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
 6514 tgl                       887 CBC         274 :     offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
 6555                           888             274 :     offptr += entryno;
 6514 tgl                       889 ECB             : 
 6555 tgl                       890 GIC         274 :     *offptr = offset;
 6555 tgl                       891 ECB             : 
 6364 tgl                       892 GIC         274 :     MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
 6555 tgl                       893 ECB             : 
                                894                 :     /* Exchange our lock */
 1059 tgl                       895 GIC         274 :     LWLockRelease(MultiXactOffsetSLRULock);
 6555 tgl                       896 ECB             : 
 1059 tgl                       897 GIC         274 :     LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
 6555 tgl                       898 ECB             : 
 6555 tgl                       899 GIC         274 :     prev_pageno = -1;
 6555 tgl                       900 ECB             : 
 3728 alvherre                  901 GIC         885 :     for (i = 0; i < nmembers; i++, offset++)
 6555 tgl                       902 ECB             :     {
                                903                 :         TransactionId *memberptr;
                                904                 :         uint32     *flagsptr;
                                905                 :         uint32      flagsval;
                                906                 :         int         bshift;
                                907                 :         int         flagsoff;
                                908                 :         int         memberoff;
                                909                 : 
 3728 alvherre                  910 GIC         611 :         Assert(members[i].status <= MultiXactStatusUpdate);
 6555 tgl                       911 ECB             : 
 6555 tgl                       912 GIC         611 :         pageno = MXOffsetToMemberPage(offset);
 3728 alvherre                  913 CBC         611 :         memberoff = MXOffsetToMemberOffset(offset);
                                914             611 :         flagsoff = MXOffsetToFlagsOffset(offset);
                                915             611 :         bshift = MXOffsetToFlagsBitShift(offset);
 6555 tgl                       916 ECB             : 
 6555 tgl                       917 GIC         611 :         if (pageno != prev_pageno)
 6555 tgl                       918 ECB             :         {
 5730 tgl                       919 GIC         274 :             slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
 6555 tgl                       920 CBC         274 :             prev_pageno = pageno;
 6555 tgl                       921 ECB             :         }
                                922                 : 
 6555 tgl                       923 GIC         611 :         memberptr = (TransactionId *)
 3728 alvherre                  924 CBC         611 :             (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
 3728 alvherre                  925 ECB             : 
 3728 alvherre                  926 GIC         611 :         *memberptr = members[i].xid;
 3728 alvherre                  927 ECB             : 
 3728 alvherre                  928 GIC         611 :         flagsptr = (uint32 *)
 3728 alvherre                  929 CBC         611 :             (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
 6555 tgl                       930 ECB             : 
 3728 alvherre                  931 GIC         611 :         flagsval = *flagsptr;
 3728 alvherre                  932 CBC         611 :         flagsval &= ~(((1 << MXACT_MEMBER_BITS_PER_XACT) - 1) << bshift);
                                933             611 :         flagsval |= (members[i].status << bshift);
                                934             611 :         *flagsptr = flagsval;
 6514 tgl                       935 ECB             : 
 6364 tgl                       936 GIC         611 :         MultiXactMemberCtl->shared->page_dirty[slotno] = true;
 6555 tgl                       937 ECB             :     }
                                938                 : 
 1059 tgl                       939 GIC         274 :     LWLockRelease(MultiXactMemberSLRULock);
 6555 tgl                       940 CBC         274 : }
 6555 tgl                       941 ECB             : 
                                942                 : /*
                                943                 :  * GetNewMultiXactId
                                944                 :  *      Get the next MultiXactId.
                                945                 :  *
                                946                 :  * Also, reserve the needed amount of space in the "members" area.  The
                                947                 :  * starting offset of the reserved space is returned in *offset.
                                948                 :  *
                                949                 :  * This may generate XLOG records for expansion of the offsets and/or members
                                950                 :  * files.  Unfortunately, we have to do that while holding MultiXactGenLock
                                951                 :  * to avoid race conditions --- the XLOG record for zeroing a page must appear
                                952                 :  * before any backend can possibly try to store data in that page!
                                953                 :  *
                                954                 :  * We start a critical section before advancing the shared counters.  The
                                955                 :  * caller must end the critical section after writing SLRU data.
                                956                 :  */
                                957                 : static MultiXactId
 3728 alvherre                  958 GIC         272 : GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
 6555 tgl                       959 ECB             : {
                                960                 :     MultiXactId result;
                                961                 :     MultiXactOffset nextOffset;
                                962                 : 
                                963                 :     debug_elog3(DEBUG2, "GetNew: for %d xids", nmembers);
                                964                 : 
                                965                 :     /* safety check, we should never get this far in a HS standby */
 3728 alvherre                  966 GIC         272 :     if (RecoveryInProgress())
 3728 alvherre                  967 LBC           0 :         elog(ERROR, "cannot assign MultiXactIds during recovery");
 3728 alvherre                  968 EUB             : 
 6555 tgl                       969 GIC         272 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
 6555 tgl                       970 ECB             : 
                                971                 :     /* Handle wraparound of the nextMXact counter */
 6555 tgl                       972 GIC         272 :     if (MultiXactState->nextMXact < FirstMultiXactId)
 6555 tgl                       973 LBC           0 :         MultiXactState->nextMXact = FirstMultiXactId;
 6555 tgl                       974 EUB             : 
                                975                 :     /* Assign the MXID */
 6555 tgl                       976 GIC         272 :     result = MultiXactState->nextMXact;
 6555 tgl                       977 ECB             : 
                                978                 :     /*----------
                                979                 :      * Check to see if it's safe to assign another MultiXactId.  This protects
                                980                 :      * against catastrophic data loss due to multixact wraparound.  The basic
                                981                 :      * rules are:
                                982                 :      *
                                983                 :      * If we're past multiVacLimit or the safe threshold for member storage
                                984                 :      * space, or we don't know what the safe threshold for member storage is,
                                985                 :      * start trying to force autovacuum cycles.
                                986                 :      * If we're past multiWarnLimit, start issuing warnings.
                                987                 :      * If we're past multiStopLimit, refuse to create new MultiXactIds.
                                988                 :      *
                                989                 :      * Note these are pretty much the same protections in GetNewTransactionId.
                                990                 :      *----------
                                991                 :      */
 2849 andres                    992 GIC         272 :     if (!MultiXactIdPrecedes(result, MultiXactState->multiVacLimit))
 3728 alvherre                  993 ECB             :     {
                                994                 :         /*
                                995                 :          * For safety's sake, we release MultiXactGenLock while sending
                                996                 :          * signals, warnings, etc.  This is not so much because we care about
                                997                 :          * preserving concurrency in this situation, as to avoid any
                                998                 :          * possibility of deadlock while doing get_database_name(). First,
                                999                 :          * copy all the shared values we'll need in this path.
                               1000                 :          */
 3728 alvherre                 1001 UIC           0 :         MultiXactId multiWarnLimit = MultiXactState->multiWarnLimit;
 3728 alvherre                 1002 UBC           0 :         MultiXactId multiStopLimit = MultiXactState->multiStopLimit;
                               1003               0 :         MultiXactId multiWrapLimit = MultiXactState->multiWrapLimit;
                               1004               0 :         Oid         oldest_datoid = MultiXactState->oldestMultiXactDB;
 3728 alvherre                 1005 EUB             : 
 3728 alvherre                 1006 UIC           0 :         LWLockRelease(MultiXactGenLock);
 3728 alvherre                 1007 EUB             : 
 3728 alvherre                 1008 UIC           0 :         if (IsUnderPostmaster &&
 3728 alvherre                 1009 UBC           0 :             !MultiXactIdPrecedes(result, multiStopLimit))
 3728 alvherre                 1010 EUB             :         {
 3728 alvherre                 1011 UIC           0 :             char       *oldest_datname = get_database_name(oldest_datoid);
 3728 alvherre                 1012 EUB             : 
                               1013                 :             /*
                               1014                 :              * Immediately kick autovacuum into action as we're already in
                               1015                 :              * ERROR territory.
                               1016                 :              */
 2849 andres                   1017 UIC           0 :             SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
 2849 andres                   1018 EUB             : 
                               1019                 :             /* complain even if that DB has disappeared */
 3728 alvherre                 1020 UIC           0 :             if (oldest_datname)
 3728 alvherre                 1021 UBC           0 :                 ereport(ERROR,
 3728 alvherre                 1022 EUB             :                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1023                 :                          errmsg("database is not accepting commands that generate new MultiXactIds to avoid wraparound data loss in database \"%s\"",
                               1024                 :                                 oldest_datname),
                               1025                 :                          errhint("Execute a database-wide VACUUM in that database.\n"
                               1026                 :                                  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
                               1027                 :             else
 3728 alvherre                 1028 UIC           0 :                 ereport(ERROR,
 3728 alvherre                 1029 EUB             :                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1030                 :                          errmsg("database is not accepting commands that generate new MultiXactIds to avoid wraparound data loss in database with OID %u",
                               1031                 :                                 oldest_datoid),
                               1032                 :                          errhint("Execute a database-wide VACUUM in that database.\n"
                               1033                 :                                  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
                               1034                 :         }
                               1035                 : 
                               1036                 :         /*
                               1037                 :          * To avoid swamping the postmaster with signals, we issue the autovac
                               1038                 :          * request only once per 64K multis generated.  This still gives
                               1039                 :          * plenty of chances before we get into real trouble.
                               1040                 :          */
 2849 andres                   1041 UIC           0 :         if (IsUnderPostmaster && (result % 65536) == 0)
 2849 andres                   1042 UBC           0 :             SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
 2849 andres                   1043 EUB             : 
 2849 andres                   1044 UIC           0 :         if (!MultiXactIdPrecedes(result, multiWarnLimit))
 3728 alvherre                 1045 EUB             :         {
 3728 alvherre                 1046 UIC           0 :             char       *oldest_datname = get_database_name(oldest_datoid);
 3728 alvherre                 1047 EUB             : 
                               1048                 :             /* complain even if that DB has disappeared */
 3728 alvherre                 1049 UIC           0 :             if (oldest_datname)
 3728 alvherre                 1050 UBC           0 :                 ereport(WARNING,
 3530 peter_e                  1051 EUB             :                         (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
                               1052                 :                                        "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
                               1053                 :                                        multiWrapLimit - result,
                               1054                 :                                        oldest_datname,
                               1055                 :                                        multiWrapLimit - result),
                               1056                 :                          errhint("Execute a database-wide VACUUM in that database.\n"
                               1057                 :                                  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
                               1058                 :             else
 3728 alvherre                 1059 UIC           0 :                 ereport(WARNING,
 3530 peter_e                  1060 EUB             :                         (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
                               1061                 :                                        "database with OID %u must be vacuumed before %u more MultiXactIds are used",
                               1062                 :                                        multiWrapLimit - result,
                               1063                 :                                        oldest_datoid,
                               1064                 :                                        multiWrapLimit - result),
                               1065                 :                          errhint("Execute a database-wide VACUUM in that database.\n"
                               1066                 :                                  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
                               1067                 :         }
                               1068                 : 
                               1069                 :         /* Re-acquire lock and start over */
 3728 alvherre                 1070 UIC           0 :         LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
 3728 alvherre                 1071 UBC           0 :         result = MultiXactState->nextMXact;
                               1072               0 :         if (result < FirstMultiXactId)
                               1073               0 :             result = FirstMultiXactId;
 3728 alvherre                 1074 EUB             :     }
                               1075                 : 
                               1076                 :     /* Make sure there is room for the MXID in the file.  */
 6514 tgl                      1077 GIC         272 :     ExtendMultiXactOffset(result);
 6514 tgl                      1078 ECB             : 
                               1079                 :     /*
                               1080                 :      * Reserve the members space, similarly to above.  Also, be careful not to
                               1081                 :      * return zero as the starting offset for any multixact. See
                               1082                 :      * GetMultiXactIdMembers() for motivation.
                               1083                 :      */
 6372 tgl                      1084 GIC         272 :     nextOffset = MultiXactState->nextOffset;
 6372 tgl                      1085 CBC         272 :     if (nextOffset == 0)
 6372 tgl                      1086 ECB             :     {
 6372 tgl                      1087 GIC          10 :         *offset = 1;
 3728 alvherre                 1088 CBC          10 :         nmembers++;             /* allocate member slot 0 too */
 6372 tgl                      1089 ECB             :     }
                               1090                 :     else
 6372 tgl                      1091 GIC         262 :         *offset = nextOffset;
 6514 tgl                      1092 ECB             : 
                               1093                 :     /*----------
                               1094                 :      * Protect against overrun of the members space as well, with the
                               1095                 :      * following rules:
                               1096                 :      *
                               1097                 :      * If we're past offsetStopLimit, refuse to generate more multis.
                               1098                 :      * If we're close to offsetStopLimit, emit a warning.
                               1099                 :      *
                               1100                 :      * Arbitrarily, we start emitting warnings when we're 20 segments or less
                               1101                 :      * from offsetStopLimit.
                               1102                 :      *
                               1103                 :      * Note we haven't updated the shared state yet, so if we fail at this
                               1104                 :      * point, the multixact ID we grabbed can still be used by the next guy.
                               1105                 :      *
                               1106                 :      * Note that there is no point in forcing autovacuum runs here: the
                               1107                 :      * multixact freeze settings would have to be reduced for that to have any
                               1108                 :      * effect.
                               1109                 :      *----------
                               1110                 :      */
                               1111                 : #define OFFSET_WARN_SEGMENTS    20
 2752 andres                   1112 GIC         544 :     if (MultiXactState->oldestOffsetKnown &&
 2865 rhaas                    1113 CBC         272 :         MultiXactOffsetWouldWrap(MultiXactState->offsetStopLimit, nextOffset,
 2903 alvherre                 1114 ECB             :                                  nmembers))
                               1115                 :     {
                               1116                 :         /* see comment in the corresponding offsets wraparound case */
 2849 andres                   1117 UIC           0 :         SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
 2849 andres                   1118 EUB             : 
 2903 alvherre                 1119 UIC           0 :         ereport(ERROR,
 2903 alvherre                 1120 EUB             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1121                 :                  errmsg("multixact \"members\" limit exceeded"),
                               1122                 :                  errdetail_plural("This command would create a multixact with %u members, but the remaining space is only enough for %u member.",
                               1123                 :                                   "This command would create a multixact with %u members, but the remaining space is only enough for %u members.",
                               1124                 :                                   MultiXactState->offsetStopLimit - nextOffset - 1,
                               1125                 :                                   nmembers,
                               1126                 :                                   MultiXactState->offsetStopLimit - nextOffset - 1),
                               1127                 :                  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.",
                               1128                 :                          MultiXactState->oldestMultiXactDB)));
                               1129                 :     }
                               1130                 : 
                               1131                 :     /*
                               1132                 :      * Check whether we should kick autovacuum into action, to prevent members
                               1133                 :      * wraparound. NB we use a much larger window to trigger autovacuum than
                               1134                 :      * just the warning limit. The warning is just a measure of last resort -
                               1135                 :      * this is in line with GetNewTransactionId's behaviour.
                               1136                 :      */
 2849 andres                   1137 GIC         272 :     if (!MultiXactState->oldestOffsetKnown ||
 2849 andres                   1138 CBC         272 :         (MultiXactState->nextOffset - MultiXactState->oldestOffset
                               1139             272 :          > MULTIXACT_MEMBER_SAFE_THRESHOLD))
 2849 andres                   1140 ECB             :     {
                               1141                 :         /*
                               1142                 :          * To avoid swamping the postmaster with signals, we issue the autovac
                               1143                 :          * request only when crossing a segment boundary. With default
                               1144                 :          * compilation settings that's roughly after 50k members.  This still
                               1145                 :          * gives plenty of chances before we get into real trouble.
                               1146                 :          */
 2849 andres                   1147 UIC           0 :         if ((MXOffsetToMemberPage(nextOffset) / SLRU_PAGES_PER_SEGMENT) !=
 2849 andres                   1148 UBC           0 :             (MXOffsetToMemberPage(nextOffset + nmembers) / SLRU_PAGES_PER_SEGMENT))
                               1149               0 :             SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
 2849 andres                   1150 EUB             :     }
                               1151                 : 
 2752 andres                   1152 GIC         544 :     if (MultiXactState->oldestOffsetKnown &&
 2849 andres                   1153 CBC         272 :         MultiXactOffsetWouldWrap(MultiXactState->offsetStopLimit,
 2849 andres                   1154 ECB             :                                  nextOffset,
                               1155                 :                                  nmembers + MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT * OFFSET_WARN_SEGMENTS))
 2903 alvherre                 1156 UIC           0 :         ereport(WARNING,
 2903 alvherre                 1157 EUB             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1158                 :                  errmsg_plural("database with OID %u must be vacuumed before %d more multixact member is used",
                               1159                 :                                "database with OID %u must be vacuumed before %d more multixact members are used",
                               1160                 :                                MultiXactState->offsetStopLimit - nextOffset + nmembers,
                               1161                 :                                MultiXactState->oldestMultiXactDB,
                               1162                 :                                MultiXactState->offsetStopLimit - nextOffset + nmembers),
                               1163                 :                  errhint("Execute a database-wide VACUUM in that database with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.")));
                               1164                 : 
 3728 alvherre                 1165 GIC         272 :     ExtendMultiXactMember(nextOffset, nmembers);
 6514 tgl                      1166 ECB             : 
                               1167                 :     /*
                               1168                 :      * Critical section from here until caller has written the data into the
                               1169                 :      * just-reserved SLRU space; we don't want to error out with a partly
                               1170                 :      * written MultiXact structure.  (In particular, failing to write our
                               1171                 :      * start offset after advancing nextMXact would effectively corrupt the
                               1172                 :      * previous MultiXact.)
                               1173                 :      */
 6372 tgl                      1174 GIC         272 :     START_CRIT_SECTION();
 6372 tgl                      1175 ECB             : 
                               1176                 :     /*
                               1177                 :      * Advance counters.  As in GetNewTransactionId(), this must not happen
                               1178                 :      * until after file extension has succeeded!
                               1179                 :      *
                               1180                 :      * We don't care about MultiXactId wraparound here; it will be handled by
                               1181                 :      * the next iteration.  But note that nextMXact may be InvalidMultiXactId
                               1182                 :      * or the first value on a segment-beginning page after this routine
                               1183                 :      * exits, so anyone else looking at the variable must be prepared to deal
                               1184                 :      * with either case.  Similarly, nextOffset may be zero, but we won't use
                               1185                 :      * that as the actual start offset of the next multixact.
                               1186                 :      */
 6372 tgl                      1187 GIC         272 :     (MultiXactState->nextMXact)++;
 6372 tgl                      1188 ECB             : 
 3728 alvherre                 1189 GIC         272 :     MultiXactState->nextOffset += nmembers;
 6555 tgl                      1190 ECB             : 
 6555 tgl                      1191 GIC         272 :     LWLockRelease(MultiXactGenLock);
 6555 tgl                      1192 ECB             : 
                               1193                 :     debug_elog4(DEBUG2, "GetNew: returning %u offset %u", result, *offset);
 6555 tgl                      1194 GIC         272 :     return result;
 6555 tgl                      1195 ECB             : }
                               1196                 : 
                               1197                 : /*
                               1198                 :  * GetMultiXactIdMembers
                               1199                 :  *      Return the set of MultiXactMembers that make up a MultiXactId
                               1200                 :  *
                               1201                 :  * Return value is the number of members found, or -1 if there are none,
                               1202                 :  * and *members is set to a newly palloc'ed array of members.  It's the
                               1203                 :  * caller's responsibility to free it when done with it.
                               1204                 :  *
                               1205                 :  * from_pgupgrade must be passed as true if and only if only the multixact
                               1206                 :  * corresponds to a value from a tuple that was locked in a 9.2-or-older
                               1207                 :  * installation and later pg_upgrade'd (that is, the infomask is
                               1208                 :  * HEAP_LOCKED_UPGRADED).  In this case, we know for certain that no members
                               1209                 :  * can still be running, so we return -1 just like for an empty multixact
                               1210                 :  * without any further checking.  It would be wrong to try to resolve such a
                               1211                 :  * multixact: either the multixact is within the current valid multixact
                               1212                 :  * range, in which case the returned result would be bogus, or outside that
                               1213                 :  * range, in which case an error would be raised.
                               1214                 :  *
                               1215                 :  * In all other cases, the passed multixact must be within the known valid
                               1216                 :  * range, that is, greater to or equal than oldestMultiXactId, and less than
                               1217                 :  * nextMXact.  Otherwise, an error is raised.
                               1218                 :  *
                               1219                 :  * isLockOnly must be set to true if caller is certain that the given multi
                               1220                 :  * is used only to lock tuples; can be false without loss of correctness,
                               1221                 :  * but passing a true means we can return quickly without checking for
                               1222                 :  * old updates.
                               1223                 :  */
                               1224                 : int
 3728 alvherre                 1225 GIC        3001 : GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
                               1226                 :                       bool from_pgupgrade, bool isLockOnly)
                               1227                 : {
                               1228                 :     int         pageno;
                               1229                 :     int         prev_pageno;
                               1230                 :     int         entryno;
                               1231                 :     int         slotno;
                               1232                 :     MultiXactOffset *offptr;
                               1233                 :     MultiXactOffset offset;
                               1234                 :     int         length;
                               1235                 :     int         truelength;
                               1236                 :     int         i;
                               1237                 :     MultiXactId oldestMXact;
                               1238                 :     MultiXactId nextMXact;
                               1239                 :     MultiXactId tmpMXact;
                               1240                 :     MultiXactOffset nextOffset;
                               1241                 :     MultiXactMember *ptr;
                               1242                 : 
                               1243                 :     debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
                               1244                 : 
 2480                          1245            3001 :     if (!MultiXactIdIsValid(multi) || from_pgupgrade)
  661 heikki.linnakangas       1246 ECB             :     {
  661 heikki.linnakangas       1247 UIC           0 :         *members = NULL;
 3419 alvherre                 1248 UBC           0 :         return -1;
  661 heikki.linnakangas       1249 EUB             :     }
                               1250                 : 
                               1251                 :     /* See if the MultiXactId is in the local cache */
 3728 alvherre                 1252 GIC        3001 :     length = mXactCacheGetById(multi, members);
 6555 tgl                      1253 CBC        3001 :     if (length >= 0)
 6555 tgl                      1254 ECB             :     {
                               1255                 :         debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
                               1256                 :                     mxid_to_string(multi, length, *members));
 6555 tgl                      1257 GIC        2124 :         return length;
 6555 tgl                      1258 ECB             :     }
                               1259                 : 
                               1260                 :     /* Set our OldestVisibleMXactId[] entry if we didn't already */
 6555 tgl                      1261 GIC         877 :     MultiXactIdSetOldestVisible();
 6555 tgl                      1262 ECB             : 
                               1263                 :     /*
                               1264                 :      * If we know the multi is used only for locking and not for updates, then
                               1265                 :      * we can skip checking if the value is older than our oldest visible
                               1266                 :      * multi.  It cannot possibly still be running.
                               1267                 :      */
  202 pg                       1268 GNC        1601 :     if (isLockOnly &&
 3176 alvherre                 1269 CBC         724 :         MultiXactIdPrecedes(multi, OldestVisibleMXactId[MyBackendId]))
 3176 alvherre                 1270 ECB             :     {
                               1271                 :         debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old");
 3176 alvherre                 1272 GIC         644 :         *members = NULL;
 3176 alvherre                 1273 CBC         644 :         return -1;
 3176 alvherre                 1274 ECB             :     }
                               1275                 : 
                               1276                 :     /*
                               1277                 :      * We check known limits on MultiXact before resorting to the SLRU area.
                               1278                 :      *
                               1279                 :      * An ID older than MultiXactState->oldestMultiXactId cannot possibly be
                               1280                 :      * useful; it has already been removed, or will be removed shortly, by
                               1281                 :      * truncation.  If one is passed, an error is raised.
                               1282                 :      *
                               1283                 :      * Also, an ID >= nextMXact shouldn't ever be seen here; if it is seen, it
                               1284                 :      * implies undetected ID wraparound has occurred.  This raises a hard
                               1285                 :      * error.
                               1286                 :      *
                               1287                 :      * Shared lock is enough here since we aren't modifying any global state.
                               1288                 :      * Acquire it just long enough to grab the current counter values.  We may
                               1289                 :      * need both nextMXact and nextOffset; see below.
                               1290                 :      */
 6555 tgl                      1291 GIC         233 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
 6555 tgl                      1292 ECB             : 
 3728 alvherre                 1293 GIC         233 :     oldestMXact = MultiXactState->oldestMultiXactId;
 6372 tgl                      1294 CBC         233 :     nextMXact = MultiXactState->nextMXact;
                               1295             233 :     nextOffset = MultiXactState->nextOffset;
 6372 tgl                      1296 ECB             : 
 6372 tgl                      1297 GIC         233 :     LWLockRelease(MultiXactGenLock);
 6372 tgl                      1298 ECB             : 
 3728 alvherre                 1299 GIC         233 :     if (MultiXactIdPrecedes(multi, oldestMXact))
 2480 alvherre                 1300 LBC           0 :         ereport(ERROR,
 3728 alvherre                 1301 EUB             :                 (errcode(ERRCODE_INTERNAL_ERROR),
                               1302                 :                  errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
                               1303                 :                         multi)));
                               1304                 : 
 3728 alvherre                 1305 GIC         233 :     if (!MultiXactIdPrecedes(multi, nextMXact))
 3728 alvherre                 1306 LBC           0 :         ereport(ERROR,
 3728 alvherre                 1307 EUB             :                 (errcode(ERRCODE_INTERNAL_ERROR),
                               1308                 :                  errmsg("MultiXactId %u has not been created yet -- apparent wraparound",
                               1309                 :                         multi)));
                               1310                 : 
                               1311                 :     /*
                               1312                 :      * Find out the offset at which we need to start reading MultiXactMembers
                               1313                 :      * and the number of members in the multixact.  We determine the latter as
                               1314                 :      * the difference between this multixact's starting offset and the next
                               1315                 :      * one's.  However, there are some corner cases to worry about:
                               1316                 :      *
                               1317                 :      * 1. This multixact may be the latest one created, in which case there is
                               1318                 :      * no next one to look at.  In this case the nextOffset value we just
                               1319                 :      * saved is the correct endpoint.
                               1320                 :      *
                               1321                 :      * 2. The next multixact may still be in process of being filled in: that
                               1322                 :      * is, another process may have done GetNewMultiXactId but not yet written
                               1323                 :      * the offset entry for that ID.  In that scenario, it is guaranteed that
                               1324                 :      * the offset entry for that multixact exists (because GetNewMultiXactId
                               1325                 :      * won't release MultiXactGenLock until it does) but contains zero
                               1326                 :      * (because we are careful to pre-zero offset pages). Because
                               1327                 :      * GetNewMultiXactId will never return zero as the starting offset for a
                               1328                 :      * multixact, when we read zero as the next multixact's offset, we know we
                               1329                 :      * have this case.  We sleep for a bit and try again.
                               1330                 :      *
                               1331                 :      * 3. Because GetNewMultiXactId increments offset zero to offset one to
                               1332                 :      * handle case #2, there is an ambiguity near the point of offset
                               1333                 :      * wraparound.  If we see next multixact's offset is one, is that our
                               1334                 :      * multixact's actual endpoint, or did it end at zero with a subsequent
                               1335                 :      * increment?  We handle this using the knowledge that if the zero'th
                               1336                 :      * member slot wasn't filled, it'll contain zero, and zero isn't a valid
                               1337                 :      * transaction ID so it can't be a multixact member.  Therefore, if we
                               1338                 :      * read a zero from the members array, just ignore it.
                               1339                 :      *
                               1340                 :      * This is all pretty messy, but the mess occurs only in infrequent corner
                               1341                 :      * cases, so it seems better than holding the MultiXactGenLock for a long
                               1342                 :      * time on every multixact creation.
                               1343                 :      */
 6372 tgl                      1344 GIC         233 : retry:
 1059 tgl                      1345 CBC         233 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
 6555 tgl                      1346 ECB             : 
 6555 tgl                      1347 GIC         233 :     pageno = MultiXactIdToOffsetPage(multi);
 6555 tgl                      1348 CBC         233 :     entryno = MultiXactIdToOffsetEntry(multi);
 6555 tgl                      1349 ECB             : 
 5730 tgl                      1350 GIC         233 :     slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
 6514 tgl                      1351 CBC         233 :     offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
 6555                          1352             233 :     offptr += entryno;
                               1353             233 :     offset = *offptr;
 6555 tgl                      1354 ECB             : 
 6372 tgl                      1355 GIC         233 :     Assert(offset != 0);
 6372 tgl                      1356 ECB             : 
                               1357                 :     /*
                               1358                 :      * Use the same increment rule as GetNewMultiXactId(), that is, don't
                               1359                 :      * handle wraparound explicitly until needed.
                               1360                 :      */
 6555 tgl                      1361 GIC         233 :     tmpMXact = multi + 1;
 6555 tgl                      1362 ECB             : 
 6555 tgl                      1363 GIC         233 :     if (nextMXact == tmpMXact)
 6372 tgl                      1364 ECB             :     {
                               1365                 :         /* Corner case 1: there is no next multixact */
 6555 tgl                      1366 GIC         206 :         length = nextOffset - offset;
 6372 tgl                      1367 ECB             :     }
                               1368                 :     else
                               1369                 :     {
                               1370                 :         MultiXactOffset nextMXOffset;
                               1371                 : 
                               1372                 :         /* handle wraparound if needed */
 6555 tgl                      1373 GIC          27 :         if (tmpMXact < FirstMultiXactId)
 6555 tgl                      1374 LBC           0 :             tmpMXact = FirstMultiXactId;
 6555 tgl                      1375 EUB             : 
 6555 tgl                      1376 GIC          27 :         prev_pageno = pageno;
 6555 tgl                      1377 ECB             : 
 6555 tgl                      1378 GIC          27 :         pageno = MultiXactIdToOffsetPage(tmpMXact);
 6555 tgl                      1379 CBC          27 :         entryno = MultiXactIdToOffsetEntry(tmpMXact);
 6555 tgl                      1380 ECB             : 
 6555 tgl                      1381 GIC          27 :         if (pageno != prev_pageno)
 5730 tgl                      1382 LBC           0 :             slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, tmpMXact);
 6555 tgl                      1383 EUB             : 
 6514 tgl                      1384 GIC          27 :         offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
 6555 tgl                      1385 CBC          27 :         offptr += entryno;
 6372                          1386              27 :         nextMXOffset = *offptr;
 6372 tgl                      1387 ECB             : 
 6372 tgl                      1388 GIC          27 :         if (nextMXOffset == 0)
 6372 tgl                      1389 ECB             :         {
                               1390                 :             /* Corner case 2: next multixact is still being filled in */
 1059 tgl                      1391 UIC           0 :             LWLockRelease(MultiXactOffsetSLRULock);
 3068 alvherre                 1392 UBC           0 :             CHECK_FOR_INTERRUPTS();
 6372 tgl                      1393               0 :             pg_usleep(1000L);
                               1394               0 :             goto retry;
 6372 tgl                      1395 EUB             :         }
                               1396                 : 
 6372 tgl                      1397 GIC          27 :         length = nextMXOffset - offset;
 6555 tgl                      1398 ECB             :     }
                               1399                 : 
 1059 tgl                      1400 GIC         233 :     LWLockRelease(MultiXactOffsetSLRULock);
 6555 tgl                      1401 ECB             : 
 3728 alvherre                 1402 GIC         233 :     ptr = (MultiXactMember *) palloc(length * sizeof(MultiXactMember));
 6555 tgl                      1403 ECB             : 
                               1404                 :     /* Now get the members themselves. */
 1059 tgl                      1405 GIC         233 :     LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
 6555 tgl                      1406 ECB             : 
 6372 tgl                      1407 GIC         233 :     truelength = 0;
 6555 tgl                      1408 CBC         233 :     prev_pageno = -1;
                               1409             835 :     for (i = 0; i < length; i++, offset++)
 6555 tgl                      1410 ECB             :     {
                               1411                 :         TransactionId *xactptr;
                               1412                 :         uint32     *flagsptr;
                               1413                 :         int         flagsoff;
                               1414                 :         int         bshift;
                               1415                 :         int         memberoff;
                               1416                 : 
 6555 tgl                      1417 GIC         602 :         pageno = MXOffsetToMemberPage(offset);
 3728 alvherre                 1418 CBC         602 :         memberoff = MXOffsetToMemberOffset(offset);
 6555 tgl                      1419 ECB             : 
 6555 tgl                      1420 GIC         602 :         if (pageno != prev_pageno)
 6555 tgl                      1421 ECB             :         {
 5730 tgl                      1422 GIC         233 :             slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
 6555 tgl                      1423 CBC         233 :             prev_pageno = pageno;
 6555 tgl                      1424 ECB             :         }
                               1425                 : 
 6555 tgl                      1426 GIC         602 :         xactptr = (TransactionId *)
 3728 alvherre                 1427 CBC         602 :             (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
 6555 tgl                      1428 ECB             : 
 6372 tgl                      1429 GIC         602 :         if (!TransactionIdIsValid(*xactptr))
 6372 tgl                      1430 ECB             :         {
                               1431                 :             /* Corner case 3: we must be looking at unused slot zero */
 6372 tgl                      1432 UIC           0 :             Assert(offset == 0);
 6372 tgl                      1433 UBC           0 :             continue;
 6372 tgl                      1434 EUB             :         }
                               1435                 : 
 3728 alvherre                 1436 GIC         602 :         flagsoff = MXOffsetToFlagsOffset(offset);
 3728 alvherre                 1437 CBC         602 :         bshift = MXOffsetToFlagsBitShift(offset);
                               1438             602 :         flagsptr = (uint32 *) (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
 3728 alvherre                 1439 ECB             : 
 3728 alvherre                 1440 GIC         602 :         ptr[truelength].xid = *xactptr;
 3728 alvherre                 1441 CBC         602 :         ptr[truelength].status = (*flagsptr >> bshift) & MXACT_MEMBER_XACT_BITMASK;
                               1442             602 :         truelength++;
 6555 tgl                      1443 ECB             :     }
                               1444                 : 
 1059 tgl                      1445 GIC         233 :     LWLockRelease(MultiXactMemberSLRULock);
 6555 tgl                      1446 ECB             : 
                               1447                 :     /* A multixid with zero members should not happen */
  661 heikki.linnakangas       1448 GIC         233 :     Assert(truelength > 0);
  661 heikki.linnakangas       1449 ECB             : 
                               1450                 :     /*
                               1451                 :      * Copy the result into the local cache.
                               1452                 :      */
 6372 tgl                      1453 GIC         233 :     mXactCachePut(multi, truelength, ptr);
 6555 tgl                      1454 ECB             : 
                               1455                 :     debug_elog3(DEBUG2, "GetMembers: no cache for %s",
                               1456                 :                 mxid_to_string(multi, truelength, ptr));
  661 heikki.linnakangas       1457 GIC         233 :     *members = ptr;
 6372 tgl                      1458 CBC         233 :     return truelength;
 6555 tgl                      1459 ECB             : }
                               1460                 : 
                               1461                 : /*
                               1462                 :  * mxactMemberComparator
                               1463                 :  *      qsort comparison function for MultiXactMember
                               1464                 :  *
                               1465                 :  * We can't use wraparound comparison for XIDs because that does not respect
                               1466                 :  * the triangle inequality!  Any old sort order will do.
                               1467                 :  */
                               1468                 : static int
 3728 alvherre                 1469 GIC        1901 : mxactMemberComparator(const void *arg1, const void *arg2)
 3728 alvherre                 1470 ECB             : {
 3728 alvherre                 1471 GIC        1901 :     MultiXactMember member1 = *(const MultiXactMember *) arg1;
 3728 alvherre                 1472 CBC        1901 :     MultiXactMember member2 = *(const MultiXactMember *) arg2;
 3728 alvherre                 1473 ECB             : 
 3728 alvherre                 1474 GIC        1901 :     if (member1.xid > member2.xid)
 3728 alvherre                 1475 CBC          22 :         return 1;
                               1476            1879 :     if (member1.xid < member2.xid)
                               1477            1711 :         return -1;
                               1478             168 :     if (member1.status > member2.status)
 3728 alvherre                 1479 LBC           0 :         return 1;
 3728 alvherre                 1480 GBC         168 :     if (member1.status < member2.status)
 3728 alvherre                 1481 CBC         168 :         return -1;
 3728 alvherre                 1482 LBC           0 :     return 0;
 3728 alvherre                 1483 EUB             : }
                               1484                 : 
                               1485                 : /*
                               1486                 :  * mXactCacheGetBySet
                               1487                 :  *      returns a MultiXactId from the cache based on the set of
                               1488                 :  *      TransactionIds that compose it, or InvalidMultiXactId if
                               1489                 :  *      none matches.
                               1490                 :  *
                               1491                 :  * This is helpful, for example, if two transactions want to lock a huge
                               1492                 :  * table.  By using the cache, the second will use the same MultiXactId
                               1493                 :  * for the majority of tuples, thus keeping MultiXactId usage low (saving
                               1494                 :  * both I/O and wraparound issues).
                               1495                 :  *
                               1496                 :  * NB: the passed members array will be sorted in-place.
                               1497                 :  */
                               1498                 : static MultiXactId
 3728 alvherre                 1499 GIC        1117 : mXactCacheGetBySet(int nmembers, MultiXactMember *members)
 6555 tgl                      1500 ECB             : {
                               1501                 :     dlist_iter  iter;
                               1502                 : 
                               1503                 :     debug_elog3(DEBUG2, "CacheGet: looking for %s",
                               1504                 :                 mxid_to_string(InvalidMultiXactId, nmembers, members));
                               1505                 : 
                               1506                 :     /* sort the array so comparison is easy */
 3728 alvherre                 1507 GIC        1117 :     qsort(members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
 6555 tgl                      1508 ECB             : 
  158 drowley                  1509 GNC        1356 :     dclist_foreach(iter, &MXactCache)
 6555 tgl                      1510 ECB             :     {
  158 drowley                  1511 GNC        1084 :         mXactCacheEnt *entry = dclist_container(mXactCacheEnt, node,
                               1512                 :                                                 iter.cur);
 3404 alvherre                 1513 ECB             : 
 3728 alvherre                 1514 GIC        1084 :         if (entry->nmembers != nmembers)
 6555 tgl                      1515             123 :             continue;
 6555 tgl                      1516 ECB             : 
 3728 alvherre                 1517                 :         /*
                               1518                 :          * We assume the cache entries are sorted, and that the unused bits in
                               1519                 :          * "status" are zeroed.
                               1520                 :          */
 3728 alvherre                 1521 GIC         961 :         if (memcmp(members, entry->members, nmembers * sizeof(MultiXactMember)) == 0)
                               1522                 :         {
 6555 tgl                      1523 ECB             :             debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
  158 drowley                  1524 GNC         845 :             dclist_move_head(&MXactCache, iter.cur);
 6555 tgl                      1525 GIC         845 :             return entry->multi;
 6555 tgl                      1526 ECB             :         }
                               1527                 :     }
                               1528                 : 
                               1529                 :     debug_elog2(DEBUG2, "CacheGet: not found :-(");
 6555 tgl                      1530 GIC         272 :     return InvalidMultiXactId;
                               1531                 : }
 6555 tgl                      1532 ECB             : 
                               1533                 : /*
                               1534                 :  * mXactCacheGetById
                               1535                 :  *      returns the composing MultiXactMember set from the cache for a
                               1536                 :  *      given MultiXactId, if present.
                               1537                 :  *
                               1538                 :  * If successful, *xids is set to the address of a palloc'd copy of the
                               1539                 :  * MultiXactMember set.  Return value is number of members, or -1 on failure.
                               1540                 :  */
                               1541                 : static int
 3728 alvherre                 1542 GIC        3001 : mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
                               1543                 : {
 3404 alvherre                 1544 ECB             :     dlist_iter  iter;
                               1545                 : 
                               1546                 :     debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
                               1547                 : 
  158 drowley                  1548 GNC        3446 :     dclist_foreach(iter, &MXactCache)
                               1549                 :     {
                               1550            2569 :         mXactCacheEnt *entry = dclist_container(mXactCacheEnt, node,
                               1551                 :                                                 iter.cur);
                               1552                 : 
 6555 tgl                      1553 CBC        2569 :         if (entry->multi == multi)
                               1554                 :         {
                               1555                 :             MultiXactMember *ptr;
 6555 tgl                      1556 ECB             :             Size        size;
                               1557                 : 
 3728 alvherre                 1558 GIC        2124 :             size = sizeof(MultiXactMember) * entry->nmembers;
                               1559            2124 :             ptr = (MultiXactMember *) palloc(size);
                               1560                 : 
 3728 alvherre                 1561 CBC        2124 :             memcpy(ptr, entry->members, size);
 6555 tgl                      1562 ECB             : 
                               1563                 :             debug_elog3(DEBUG2, "CacheGet: found %s",
 3599 sfrost                   1564                 :                         mxid_to_string(multi,
                               1565                 :                                        entry->nmembers,
                               1566                 :                                        entry->members));
                               1567                 : 
                               1568                 :             /*
                               1569                 :              * Note we modify the list while not using a modifiable iterator.
                               1570                 :              * This is acceptable only because we exit the iteration
                               1571                 :              * immediately afterwards.
                               1572                 :              */
  158 drowley                  1573 GNC        2124 :             dclist_move_head(&MXactCache, iter.cur);
                               1574                 : 
  661 heikki.linnakangas       1575 GIC        2124 :             *members = ptr;
 3728 alvherre                 1576 CBC        2124 :             return entry->nmembers;
                               1577                 :         }
 6555 tgl                      1578 ECB             :     }
                               1579                 : 
                               1580                 :     debug_elog2(DEBUG2, "CacheGet: not found");
 6555 tgl                      1581 GIC         877 :     return -1;
                               1582                 : }
                               1583                 : 
 6555 tgl                      1584 ECB             : /*
                               1585                 :  * mXactCachePut
                               1586                 :  *      Add a new MultiXactId and its composing set into the local cache.
                               1587                 :  */
                               1588                 : static void
 3728 alvherre                 1589 GIC         505 : mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
                               1590                 : {
                               1591                 :     mXactCacheEnt *entry;
 6555 tgl                      1592 ECB             : 
                               1593                 :     debug_elog3(DEBUG2, "CachePut: storing %s",
                               1594                 :                 mxid_to_string(multi, nmembers, members));
                               1595                 : 
 6555 tgl                      1596 GIC         505 :     if (MXactContext == NULL)
                               1597                 :     {
                               1598                 :         /* The cache only lives as long as the current transaction */
 6555 tgl                      1599 ECB             :         debug_elog2(DEBUG2, "CachePut: initializing memory context");
 6555 tgl                      1600 GIC         358 :         MXactContext = AllocSetContextCreate(TopTransactionContext,
                               1601                 :                                              "MultiXact cache context",
                               1602                 :                                              ALLOCSET_SMALL_SIZES);
 6555 tgl                      1603 ECB             :     }
                               1604                 : 
                               1605                 :     entry = (mXactCacheEnt *)
 6555 tgl                      1606 GIC         505 :         MemoryContextAlloc(MXactContext,
 3728 alvherre                 1607             505 :                            offsetof(mXactCacheEnt, members) +
                               1608                 :                            nmembers * sizeof(MultiXactMember));
 6555 tgl                      1609 ECB             : 
 6555 tgl                      1610 CBC         505 :     entry->multi = multi;
 3728 alvherre                 1611 GIC         505 :     entry->nmembers = nmembers;
                               1612             505 :     memcpy(entry->members, members, nmembers * sizeof(MultiXactMember));
 6555 tgl                      1613 ECB             : 
                               1614                 :     /* mXactCacheGetBySet assumes the entries are sorted, so sort them */
 3728 alvherre                 1615 CBC         505 :     qsort(entry->members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
                               1616                 : 
  158 drowley                  1617 GNC         505 :     dclist_push_head(&MXactCache, &entry->node);
                               1618             505 :     if (dclist_count(&MXactCache) > MAX_CACHE_ENTRIES)
                               1619                 :     {
 3404 alvherre                 1620 ECB             :         dlist_node *node;
                               1621                 : 
  158 drowley                  1622 UNC           0 :         node = dclist_tail_node(&MXactCache);
                               1623               0 :         dclist_delete_from(&MXactCache, node);
 3404 alvherre                 1624 EUB             : 
  158 drowley                  1625 UNC           0 :         entry = dclist_container(mXactCacheEnt, node, node);
 3404 alvherre                 1626 EUB             :         debug_elog3(DEBUG2, "CachePut: pruning cached multi %u",
                               1627                 :                     entry->multi);
                               1628                 : 
 3404 alvherre                 1629 UIC           0 :         pfree(entry);
 3404 alvherre                 1630 EUB             :     }
 6555 tgl                      1631 GIC         505 : }
 6555 tgl                      1632 ECB             : 
                               1633                 : static char *
 3728 alvherre                 1634 UIC           0 : mxstatus_to_string(MultiXactStatus status)
 3728 alvherre                 1635 EUB             : {
 3728 alvherre                 1636 UIC           0 :     switch (status)
 3728 alvherre                 1637 EUB             :     {
 3728 alvherre                 1638 UIC           0 :         case MultiXactStatusForKeyShare:
 3728 alvherre                 1639 UBC           0 :             return "keysh";
                               1640               0 :         case MultiXactStatusForShare:
                               1641               0 :             return "sh";
                               1642               0 :         case MultiXactStatusForNoKeyUpdate:
                               1643               0 :             return "fornokeyupd";
                               1644               0 :         case MultiXactStatusForUpdate:
                               1645               0 :             return "forupd";
                               1646               0 :         case MultiXactStatusNoKeyUpdate:
                               1647               0 :             return "nokeyupd";
                               1648               0 :         case MultiXactStatusUpdate:
                               1649               0 :             return "upd";
                               1650               0 :         default:
                               1651               0 :             elog(ERROR, "unrecognized multixact status %d", status);
 3728 alvherre                 1652 EUB             :             return "";
                               1653                 :     }
                               1654                 : }
                               1655                 : 
                               1656                 : char *
 3728 alvherre                 1657 UIC           0 : mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
 6555 tgl                      1658 EUB             : {
                               1659                 :     static char *str = NULL;
                               1660                 :     StringInfoData buf;
                               1661                 :     int         i;
                               1662                 : 
 3728 alvherre                 1663 UIC           0 :     if (str != NULL)
 3728 alvherre                 1664 UBC           0 :         pfree(str);
 6555 tgl                      1665 EUB             : 
 3728 alvherre                 1666 UIC           0 :     initStringInfo(&buf);
 6555 tgl                      1667 EUB             : 
 3728 alvherre                 1668 UIC           0 :     appendStringInfo(&buf, "%u %d[%u (%s)", multi, nmembers, members[0].xid,
 3728 alvherre                 1669 EUB             :                      mxstatus_to_string(members[0].status));
                               1670                 : 
 3728 alvherre                 1671 UIC           0 :     for (i = 1; i < nmembers; i++)
 3728 alvherre                 1672 UBC           0 :         appendStringInfo(&buf, ", %u (%s)", members[i].xid,
                               1673               0 :                          mxstatus_to_string(members[i].status));
 3728 alvherre                 1674 EUB             : 
 3728 alvherre                 1675 UIC           0 :     appendStringInfoChar(&buf, ']');
 3728 alvherre                 1676 UBC           0 :     str = MemoryContextStrdup(TopMemoryContext, buf.data);
                               1677               0 :     pfree(buf.data);
 6555 tgl                      1678               0 :     return str;
 6555 tgl                      1679 EUB             : }
                               1680                 : 
                               1681                 : /*
                               1682                 :  * AtEOXact_MultiXact
                               1683                 :  *      Handle transaction end for MultiXact
                               1684                 :  *
                               1685                 :  * This is called at top transaction commit or abort (we don't care which).
                               1686                 :  */
                               1687                 : void
 6555 tgl                      1688 GIC      485037 : AtEOXact_MultiXact(void)
 6555 tgl                      1689 ECB             : {
                               1690                 :     /*
                               1691                 :      * Reset our OldestMemberMXactId and OldestVisibleMXactId values, both of
                               1692                 :      * which should only be valid while within a transaction.
                               1693                 :      *
                               1694                 :      * We assume that storing a MultiXactId is atomic and so we need not take
                               1695                 :      * MultiXactGenLock to do this.
                               1696                 :      */
 6555 tgl                      1697 GIC      485037 :     OldestMemberMXactId[MyBackendId] = InvalidMultiXactId;
 6555 tgl                      1698 CBC      485037 :     OldestVisibleMXactId[MyBackendId] = InvalidMultiXactId;
 6555 tgl                      1699 ECB             : 
                               1700                 :     /*
                               1701                 :      * Discard the local MultiXactId cache.  Since MXactContext was created as
                               1702                 :      * a child of TopTransactionContext, we needn't delete it explicitly.
                               1703                 :      */
 6555 tgl                      1704 GIC      485037 :     MXactContext = NULL;
  158 drowley                  1705 GNC      485037 :     dclist_init(&MXactCache);
 6555 tgl                      1706 CBC      485037 : }
                               1707                 : 
                               1708                 : /*
                               1709                 :  * AtPrepare_MultiXact
                               1710                 :  *      Save multixact state at 2PC transaction prepare
                               1711                 :  *
                               1712                 :  * In this phase, we only store our OldestMemberMXactId value in the two-phase
                               1713                 :  * state file.
                               1714                 :  */
                               1715                 : void
 4885 heikki.linnakangas       1716             354 : AtPrepare_MultiXact(void)
                               1717                 : {
                               1718             354 :     MultiXactId myOldestMember = OldestMemberMXactId[MyBackendId];
                               1719                 : 
                               1720             354 :     if (MultiXactIdIsValid(myOldestMember))
                               1721              47 :         RegisterTwoPhaseRecord(TWOPHASE_RM_MULTIXACT_ID, 0,
                               1722                 :                                &myOldestMember, sizeof(MultiXactId));
                               1723             354 : }
                               1724                 : 
                               1725                 : /*
                               1726                 :  * PostPrepare_MultiXact
                               1727                 :  *      Clean up after successful PREPARE TRANSACTION
                               1728                 :  */
                               1729                 : void
                               1730             354 : PostPrepare_MultiXact(TransactionId xid)
                               1731                 : {
                               1732                 :     MultiXactId myOldestMember;
                               1733                 : 
                               1734                 :     /*
                               1735                 :      * Transfer our OldestMemberMXactId value to the slot reserved for the
                               1736                 :      * prepared transaction.
                               1737                 :      */
                               1738             354 :     myOldestMember = OldestMemberMXactId[MyBackendId];
                               1739             354 :     if (MultiXactIdIsValid(myOldestMember))
                               1740                 :     {
 1504 michael                  1741              47 :         BackendId   dummyBackendId = TwoPhaseGetDummyBackendId(xid, false);
                               1742                 : 
                               1743                 :         /*
                               1744                 :          * Even though storing MultiXactId is atomic, acquire lock to make
                               1745                 :          * sure others see both changes, not just the reset of the slot of the
                               1746                 :          * current backend. Using a volatile pointer might suffice, but this
                               1747                 :          * isn't a hot spot.
                               1748                 :          */
 4885 heikki.linnakangas       1749              47 :         LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
                               1750                 : 
                               1751              47 :         OldestMemberMXactId[dummyBackendId] = myOldestMember;
                               1752              47 :         OldestMemberMXactId[MyBackendId] = InvalidMultiXactId;
                               1753                 : 
                               1754              47 :         LWLockRelease(MultiXactGenLock);
                               1755                 :     }
                               1756                 : 
                               1757                 :     /*
                               1758                 :      * We don't need to transfer OldestVisibleMXactId value, because the
                               1759                 :      * transaction is not going to be looking at any more multixacts once it's
                               1760                 :      * prepared.
                               1761                 :      *
                               1762                 :      * We assume that storing a MultiXactId is atomic and so we need not take
                               1763                 :      * MultiXactGenLock to do this.
                               1764                 :      */
                               1765             354 :     OldestVisibleMXactId[MyBackendId] = InvalidMultiXactId;
                               1766                 : 
                               1767                 :     /*
                               1768                 :      * Discard the local MultiXactId cache like in AtEOXact_MultiXact.
                               1769                 :      */
                               1770             354 :     MXactContext = NULL;
  158 drowley                  1771 GNC         354 :     dclist_init(&MXactCache);
 4885 heikki.linnakangas       1772 GIC         354 : }
                               1773                 : 
                               1774                 : /*
                               1775                 :  * multixact_twophase_recover
                               1776                 :  *      Recover the state of a prepared transaction at startup
                               1777                 :  */
 4885 heikki.linnakangas       1778 ECB             : void
 4885 heikki.linnakangas       1779 GIC           8 : multixact_twophase_recover(TransactionId xid, uint16 info,
                               1780                 :                            void *recdata, uint32 len)
 4885 heikki.linnakangas       1781 ECB             : {
 1504 michael                  1782 GIC           8 :     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.
 4885 heikki.linnakangas       1788 ECB             :      */
 4885 heikki.linnakangas       1789 CBC           8 :     Assert(len == sizeof(MultiXactId));
 4790 bruce                    1790 GIC           8 :     oldestMember = *((MultiXactId *) recdata);
 4885 heikki.linnakangas       1791 ECB             : 
 4885 heikki.linnakangas       1792 CBC           8 :     OldestMemberMXactId[dummyBackendId] = oldestMember;
 4885 heikki.linnakangas       1793 GIC           8 : }
                               1794                 : 
                               1795                 : /*
                               1796                 :  * multixact_twophase_postcommit
                               1797                 :  *      Similar to AtEOXact_MultiXact but for COMMIT PREPARED
                               1798                 :  */
 4885 heikki.linnakangas       1799 ECB             : void
 4885 heikki.linnakangas       1800 GIC          52 : multixact_twophase_postcommit(TransactionId xid, uint16 info,
                               1801                 :                               void *recdata, uint32 len)
 4885 heikki.linnakangas       1802 ECB             : {
 1504 michael                  1803 GIC          52 :     BackendId   dummyBackendId = TwoPhaseGetDummyBackendId(xid, true);
 4885 heikki.linnakangas       1804 ECB             : 
 4885 heikki.linnakangas       1805 GIC          52 :     Assert(len == sizeof(MultiXactId));
 4885 heikki.linnakangas       1806 ECB             : 
 4885 heikki.linnakangas       1807 CBC          52 :     OldestMemberMXactId[dummyBackendId] = InvalidMultiXactId;
 4885 heikki.linnakangas       1808 GIC          52 : }
                               1809                 : 
                               1810                 : /*
                               1811                 :  * multixact_twophase_postabort
                               1812                 :  *      This is actually just the same as the COMMIT case.
                               1813                 :  */
 4885 heikki.linnakangas       1814 ECB             : void
 4885 heikki.linnakangas       1815 GIC          22 : multixact_twophase_postabort(TransactionId xid, uint16 info,
                               1816                 :                              void *recdata, uint32 len)
 4885 heikki.linnakangas       1817 ECB             : {
 4885 heikki.linnakangas       1818 CBC          22 :     multixact_twophase_postcommit(xid, info, recdata, len);
 4885 heikki.linnakangas       1819 GIC          22 : }
                               1820                 : 
                               1821                 : /*
                               1822                 :  * Initialization of shared memory for MultiXact.  We use two SLRU areas,
                               1823                 :  * thus double memory.  Also, reserve space for the shared MultiXactState
                               1824                 :  * struct and the per-backend MultiXactId arrays (two of those, too).
                               1825                 :  */
 6441 tgl                      1826 ECB             : Size
 6555 tgl                      1827 GIC        2738 : MultiXactShmemSize(void)
                               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))
 6441 tgl                      1835 ECB             : 
 6441 tgl                      1836 CBC        2738 :     size = SHARED_MULTIXACT_STATE_SIZE;
 1059                          1837            2738 :     size = add_size(size, SimpleLruShmemSize(NUM_MULTIXACTOFFSET_BUFFERS, 0));
 1059 tgl                      1838 GIC        2738 :     size = add_size(size, SimpleLruShmemSize(NUM_MULTIXACTMEMBER_BUFFERS, 0));
 6555 tgl                      1839 ECB             : 
 6441 tgl                      1840 GIC        2738 :     return size;
                               1841                 : }
                               1842                 : 
 6555 tgl                      1843 ECB             : void
 6555 tgl                      1844 GIC        1826 : MultiXactShmemInit(void)
                               1845                 : {
                               1846                 :     bool        found;
                               1847                 : 
                               1848                 :     debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
 6555 tgl                      1849 ECB             : 
 6555 tgl                      1850 CBC        1826 :     MultiXactOffsetCtl->PagePrecedes = MultiXactOffsetPagePrecedes;
 6555 tgl                      1851 GIC        1826 :     MultiXactMemberCtl->PagePrecedes = MultiXactMemberPagePrecedes;
 6555 tgl                      1852 ECB             : 
 6333 tgl                      1853 GIC        1826 :     SimpleLruInit(MultiXactOffsetCtl,
 1059 tgl                      1854 ECB             :                   "MultiXactOffset", NUM_MULTIXACTOFFSET_BUFFERS, 0,
 1059 tgl                      1855 GIC        1826 :                   MultiXactOffsetSLRULock, "pg_multixact/offsets",
                               1856                 :                   LWTRANCHE_MULTIXACTOFFSET_BUFFER,
  926 tmunro                   1857 ECB             :                   SYNC_HANDLER_MULTIXACT_OFFSET);
  813 noah                     1858 CBC        1826 :     SlruPagePrecedesUnitTests(MultiXactOffsetCtl, MULTIXACT_OFFSETS_PER_PAGE);
 6333 tgl                      1859 GIC        1826 :     SimpleLruInit(MultiXactMemberCtl,
 1059 tgl                      1860 ECB             :                   "MultiXactMember", NUM_MULTIXACTMEMBER_BUFFERS, 0,
 1059 tgl                      1861 GIC        1826 :                   MultiXactMemberSLRULock, "pg_multixact/members",
                               1862                 :                   LWTRANCHE_MULTIXACTMEMBER_BUFFER,
                               1863                 :                   SYNC_HANDLER_MULTIXACT_MEMBER);
                               1864                 :     /* doesn't call SimpleLruTruncate() or meet criteria for unit tests */
                               1865                 : 
 6555 tgl                      1866 ECB             :     /* Initialize our shared state struct */
 6555 tgl                      1867 CBC        1826 :     MultiXactState = ShmemInitStruct("Shared MultiXact State",
 6555 tgl                      1868 GIC        1826 :                                      SHARED_MULTIXACT_STATE_SIZE,
 6555 tgl                      1869 ECB             :                                      &found);
 6555 tgl                      1870 GIC        1826 :     if (!IsUnderPostmaster)
 6555 tgl                      1871 ECB             :     {
 6555 tgl                      1872 GIC        1826 :         Assert(!found);
                               1873                 : 
 6555 tgl                      1874 ECB             :         /* Make sure we zero out the per-backend state */
 6555 tgl                      1875 GIC        1826 :         MemSet(MultiXactState, 0, SHARED_MULTIXACT_STATE_SIZE);
                               1876                 :     }
 6555 tgl                      1877 EUB             :     else
 6555 tgl                      1878 UIC           0 :         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.
 6555 tgl                      1883 ECB             :      */
 6555 tgl                      1884 CBC        1826 :     OldestMemberMXactId = MultiXactState->perBackendXactIds;
  362 rhaas                    1885            1826 :     OldestVisibleMXactId = OldestMemberMXactId + MaxOldestSlot;
 6555 tgl                      1886 GIC        1826 : }
                               1887                 : 
                               1888                 : /*
                               1889                 :  * This func must be called ONCE on system install.  It creates the initial
                               1890                 :  * MultiXact segments.  (The MultiXacts directories are assumed to have been
                               1891                 :  * created by initdb, and MultiXactShmemInit must have been called already.)
                               1892                 :  */
 6555 tgl                      1893 ECB             : void
 6555 tgl                      1894 GIC         305 : BootStrapMultiXact(void)
                               1895                 : {
                               1896                 :     int         slotno;
 6555 tgl                      1897 ECB             : 
 1059 tgl                      1898 GIC         305 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
                               1899                 : 
 6514 tgl                      1900 ECB             :     /* Create and zero the first page of the offsets log */
 6514 tgl                      1901 GIC         305 :     slotno = ZeroMultiXactOffsetPage(0, false);
                               1902                 : 
 6514 tgl                      1903 ECB             :     /* Make sure it's written out */
 4483 alvherre                 1904 CBC         305 :     SimpleLruWritePage(MultiXactOffsetCtl, slotno);
 6364 tgl                      1905 GIC         305 :     Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
 6555 tgl                      1906 ECB             : 
 1059 tgl                      1907 GIC         305 :     LWLockRelease(MultiXactOffsetSLRULock);
 6555 tgl                      1908 ECB             : 
 1059 tgl                      1909 GIC         305 :     LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
                               1910                 : 
 6514 tgl                      1911 ECB             :     /* Create and zero the first page of the members log */
 6514 tgl                      1912 GIC         305 :     slotno = ZeroMultiXactMemberPage(0, false);
                               1913                 : 
 6514 tgl                      1914 ECB             :     /* Make sure it's written out */
 4483 alvherre                 1915 CBC         305 :     SimpleLruWritePage(MultiXactMemberCtl, slotno);
 6364 tgl                      1916 GIC         305 :     Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
 6555 tgl                      1917 ECB             : 
 1059 tgl                      1918 CBC         305 :     LWLockRelease(MultiXactMemberSLRULock);
 6555 tgl                      1919 GIC         305 : }
                               1920                 : 
                               1921                 : /*
                               1922                 :  * Initialize (or reinitialize) a page of MultiXactOffset to zeroes.
                               1923                 :  * If writeXlog is true, also emit an XLOG record saying we did this.
                               1924                 :  *
                               1925                 :  * The page is not actually written, just set up in shared memory.
                               1926                 :  * The slot number of the new page is returned.
                               1927                 :  *
                               1928                 :  * Control lock must be held at entry, and will be held at exit.
                               1929                 :  */
 6555 tgl                      1930 ECB             : static int
 6514 tgl                      1931 GIC         316 : ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
                               1932                 : {
                               1933                 :     int         slotno;
 6514 tgl                      1934 ECB             : 
 6514 tgl                      1935 GIC         316 :     slotno = SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
 6514 tgl                      1936 ECB             : 
 6514 tgl                      1937 CBC         316 :     if (writeXlog)
 6514 tgl                      1938 GIC          10 :         WriteMZeroPageXlogRec(pageno, XLOG_MULTIXACT_ZERO_OFF_PAGE);
 6514 tgl                      1939 ECB             : 
 6514 tgl                      1940 GIC         316 :     return slotno;
                               1941                 : }
                               1942                 : 
                               1943                 : /*
                               1944                 :  * Ditto, for MultiXactMember
                               1945                 :  */
 6555 tgl                      1946 ECB             : static int
 6514 tgl                      1947 GIC         316 : ZeroMultiXactMemberPage(int pageno, bool writeXlog)
                               1948                 : {
                               1949                 :     int         slotno;
 6514 tgl                      1950 ECB             : 
 6514 tgl                      1951 GIC         316 :     slotno = SimpleLruZeroPage(MultiXactMemberCtl, pageno);
 6514 tgl                      1952 ECB             : 
 6514 tgl                      1953 CBC         316 :     if (writeXlog)
 6514 tgl                      1954 GIC          10 :         WriteMZeroPageXlogRec(pageno, XLOG_MULTIXACT_ZERO_MEM_PAGE);
 6514 tgl                      1955 ECB             : 
 6514 tgl                      1956 GIC         316 :     return slotno;
                               1957                 : }
                               1958                 : 
                               1959                 : /*
                               1960                 :  * MaybeExtendOffsetSlru
                               1961                 :  *      Extend the offsets SLRU area, if necessary
                               1962                 :  *
                               1963                 :  * After a binary upgrade from <= 9.2, the pg_multixact/offsets SLRU area might
                               1964                 :  * contain files that are shorter than necessary; this would occur if the old
                               1965                 :  * installation had used multixacts beyond the first page (files cannot be
                               1966                 :  * copied, because the on-disk representation is different).  pg_upgrade would
                               1967                 :  * update pg_control to set the next offset value to be at that position, so
                               1968                 :  * that tuples marked as locked by such MultiXacts would be seen as visible
                               1969                 :  * without having to consult multixact.  However, trying to create and use a
                               1970                 :  * new MultiXactId would result in an error because the page on which the new
                               1971                 :  * value would reside does not exist.  This routine is in charge of creating
                               1972                 :  * such pages.
                               1973                 :  */
 3520 alvherre                 1974 ECB             : static void
 3520 alvherre                 1975 GIC           6 : MaybeExtendOffsetSlru(void)
                               1976                 : {
                               1977                 :     int         pageno;
 3520 alvherre                 1978 ECB             : 
 3520 alvherre                 1979 GIC           6 :     pageno = MultiXactIdToOffsetPage(MultiXactState->nextMXact);
 3520 alvherre                 1980 ECB             : 
 1059 tgl                      1981 GIC           6 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
 3520 alvherre                 1982 ECB             : 
 3520 alvherre                 1983 GIC           6 :     if (!SimpleLruDoesPhysicalPageExist(MultiXactOffsetCtl, pageno))
                               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.
 3520 alvherre                 1991 EUB             :          */
 3520 alvherre                 1992 UBC           0 :         slotno = ZeroMultiXactOffsetPage(pageno, false);
 3520 alvherre                 1993 UIC           0 :         SimpleLruWritePage(MultiXactOffsetCtl, slotno);
                               1994                 :     }
 3520 alvherre                 1995 ECB             : 
 1059 tgl                      1996 CBC           6 :     LWLockRelease(MultiXactOffsetSLRULock);
 3520 alvherre                 1997 GIC           6 : }
                               1998                 : 
                               1999                 : /*
                               2000                 :  * This must be called ONCE during postmaster or standalone-backend startup.
                               2001                 :  *
                               2002                 :  * StartupXLOG has already established nextMXact/nextOffset by calling
                               2003                 :  * MultiXactSetNextMXact and/or MultiXactAdvanceNextMXact, and the oldestMulti
                               2004                 :  * info from pg_control and/or MultiXactAdvanceOldest, but we haven't yet
                               2005                 :  * replayed WAL.
                               2006                 :  */
 6555 tgl                      2007 ECB             : void
 6555 tgl                      2008 GIC        1176 : StartupMultiXact(void)
 3418 alvherre                 2009 ECB             : {
 3418 alvherre                 2010 CBC        1176 :     MultiXactId multi = MultiXactState->nextMXact;
 3418 alvherre                 2011 GIC        1176 :     MultiXactOffset offset = MultiXactState->nextOffset;
                               2012                 :     int         pageno;
                               2013                 : 
                               2014                 :     /*
                               2015                 :      * Initialize offset's idea of the latest page number.
 3418 alvherre                 2016 ECB             :      */
 3418 alvherre                 2017 CBC        1176 :     pageno = MultiXactIdToOffsetPage(multi);
 3418 alvherre                 2018 GIC        1176 :     MultiXactOffsetCtl->shared->latest_page_number = pageno;
                               2019                 : 
                               2020                 :     /*
                               2021                 :      * Initialize member's idea of the latest page number.
 3418 alvherre                 2022 ECB             :      */
 3418 alvherre                 2023 CBC        1176 :     pageno = MXOffsetToMemberPage(offset);
                               2024            1176 :     MultiXactMemberCtl->shared->latest_page_number = pageno;
 3418 alvherre                 2025 GIC        1176 : }
                               2026                 : 
                               2027                 : /*
                               2028                 :  * This must be called ONCE at the end of startup/recovery.
                               2029                 :  */
 3418 alvherre                 2030 ECB             : void
 3418 alvherre                 2031 GIC        1142 : TrimMultiXact(void)
                               2032                 : {
                               2033                 :     MultiXactId nextMXact;
                               2034                 :     MultiXactOffset offset;
                               2035                 :     MultiXactId oldestMXact;
                               2036                 :     Oid         oldestMXactDB;
                               2037                 :     int         pageno;
                               2038                 :     int         entryno;
                               2039                 :     int         flagsoff;
 6514 tgl                      2040 ECB             : 
 2752 andres                   2041 CBC        1142 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
                               2042            1142 :     nextMXact = MultiXactState->nextMXact;
                               2043            1142 :     offset = MultiXactState->nextOffset;
                               2044            1142 :     oldestMXact = MultiXactState->oldestMultiXactId;
                               2045            1142 :     oldestMXactDB = MultiXactState->oldestMultiXactDB;
 2752 andres                   2046 GIC        1142 :     LWLockRelease(MultiXactGenLock);
                               2047                 : 
 6514 tgl                      2048 ECB             :     /* Clean up offsets state */
 1059 tgl                      2049 GIC        1142 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
                               2050                 : 
                               2051                 :     /*
                               2052                 :      * (Re-)Initialize our idea of the latest page number for offsets.
 6555 tgl                      2053 ECB             :      */
 2752 andres                   2054 CBC        1142 :     pageno = MultiXactIdToOffsetPage(nextMXact);
 6514 tgl                      2055 GIC        1142 :     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.
 6555 tgl                      2064 ECB             :      */
 2752 andres                   2065 CBC        1142 :     entryno = MultiXactIdToOffsetEntry(nextMXact);
 6514 tgl                      2066 GIC        1142 :     if (entryno != 0)
                               2067                 :     {
                               2068                 :         int         slotno;
                               2069                 :         MultiXactOffset *offptr;
 6555 tgl                      2070 ECB             : 
 2752 andres                   2071 CBC        1142 :         slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, nextMXact);
 6514 tgl                      2072            1142 :         offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
 6514 tgl                      2073 GIC        1142 :         offptr += entryno;
 6555 tgl                      2074 ECB             : 
 6514 tgl                      2075 GIC        1142 :         MemSet(offptr, 0, BLCKSZ - (entryno * sizeof(MultiXactOffset)));
 6555 tgl                      2076 ECB             : 
 6364 tgl                      2077 GIC        1142 :         MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
                               2078                 :     }
 6514 tgl                      2079 ECB             : 
 1059 tgl                      2080 GIC        1142 :     LWLockRelease(MultiXactOffsetSLRULock);
                               2081                 : 
 6514 tgl                      2082 ECB             :     /* And the same for members */
 1059 tgl                      2083 GIC        1142 :     LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
                               2084                 : 
                               2085                 :     /*
                               2086                 :      * (Re-)Initialize our idea of the latest page number for members.
 6555 tgl                      2087 ECB             :      */
 6514 tgl                      2088 CBC        1142 :     pageno = MXOffsetToMemberPage(offset);
 6514 tgl                      2089 GIC        1142 :     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.
 6555 tgl                      2094 ECB             :      */
 3728 alvherre                 2095 CBC        1142 :     flagsoff = MXOffsetToFlagsOffset(offset);
 3728 alvherre                 2096 GIC        1142 :     if (flagsoff != 0)
                               2097                 :     {
                               2098                 :         int         slotno;
                               2099                 :         TransactionId *xidptr;
                               2100                 :         int         memberoff;
 6555 tgl                      2101 ECB             : 
 3728 alvherre                 2102 CBC           7 :         memberoff = MXOffsetToMemberOffset(offset);
 5730 tgl                      2103               7 :         slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, offset);
 3728 alvherre                 2104               7 :         xidptr = (TransactionId *)
 3728 alvherre                 2105 GIC           7 :             (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
 6555 tgl                      2106 ECB             : 
 3728 alvherre                 2107 GIC           7 :         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                 :          */
 6555 tgl                      2114 ECB             : 
 6364 tgl                      2115 GIC           7 :         MultiXactMemberCtl->shared->page_dirty[slotno] = true;
                               2116                 :     }
 6555 tgl                      2117 ECB             : 
 1059 tgl                      2118 GIC        1142 :     LWLockRelease(MultiXactMemberSLRULock);
                               2119                 : 
 2752 andres                   2120 ECB             :     /* signal that we're officially up */
 2752 andres                   2121 CBC        1142 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
                               2122            1142 :     MultiXactState->finishedStartup = true;
 2865 rhaas                    2123 GIC        1142 :     LWLockRelease(MultiXactGenLock);
                               2124                 : 
 2752 andres                   2125 ECB             :     /* Now compute how far away the next members wraparound is. */
 2217 tgl                      2126 CBC        1142 :     SetMultiXactIdLimit(oldestMXact, oldestMXactDB, true);
 6555 tgl                      2127 GIC        1142 : }
                               2128                 : 
                               2129                 : /*
                               2130                 :  * Get the MultiXact data to save in a checkpoint record
                               2131                 :  */
 6514 tgl                      2132 ECB             : void
 6514 tgl                      2133 GIC        2335 : MultiXactGetCheckptMulti(bool is_shutdown,
                               2134                 :                          MultiXactId *nextMulti,
                               2135                 :                          MultiXactOffset *nextMultiOffset,
                               2136                 :                          MultiXactId *oldestMulti,
                               2137                 :                          Oid *oldestMultiDB)
 6555 tgl                      2138 ECB             : {
 6555 tgl                      2139 CBC        2335 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
 6514                          2140            2335 :     *nextMulti = MultiXactState->nextMXact;
                               2141            2335 :     *nextMultiOffset = MultiXactState->nextOffset;
 3728 alvherre                 2142            2335 :     *oldestMulti = MultiXactState->oldestMultiXactId;
                               2143            2335 :     *oldestMultiDB = MultiXactState->oldestMultiXactDB;
 6555 tgl                      2144 GIC        2335 :     LWLockRelease(MultiXactGenLock);
                               2145                 : 
                               2146                 :     debug_elog6(DEBUG2,
                               2147                 :                 "MultiXact: checkpoint is nextMulti %u, nextOffset %u, oldestMulti %u in DB %u",
 3728 alvherre                 2148 ECB             :                 *nextMulti, *nextMultiOffset, *oldestMulti, *oldestMultiDB);
 6555 tgl                      2149 GIC        2335 : }
                               2150                 : 
                               2151                 : /*
                               2152                 :  * Perform a checkpoint --- either during shutdown, or on-the-fly
                               2153                 :  */
 6555 tgl                      2154 ECB             : void
 6555 tgl                      2155 GIC        2363 : CheckPointMultiXact(void)
                               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.
  926 tmunro                   2163 ECB             :      */
  926 tmunro                   2164 CBC        2363 :     SimpleLruWriteAll(MultiXactOffsetCtl, true);
  926 tmunro                   2165 GIC        2363 :     SimpleLruWriteAll(MultiXactMemberCtl, true);
                               2166                 : 
 5364 alvherre                 2167 ECB             :     TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(true);
 6555 tgl                      2168 GIC        2363 : }
                               2169                 : 
                               2170                 : /*
                               2171                 :  * Set the next-to-be-assigned MultiXactId and offset
                               2172                 :  *
                               2173                 :  * This is used when we can determine the correct next ID/offset exactly
                               2174                 :  * from a checkpoint record.  Although this is only called during bootstrap
                               2175                 :  * and XLog replay, we take the lock in case any hot-standby backends are
                               2176                 :  * examining the values.
                               2177                 :  */
 6555 tgl                      2178 ECB             : void
 6514 tgl                      2179 GIC        1506 : MultiXactSetNextMXact(MultiXactId nextMulti,
                               2180                 :                       MultiXactOffset nextMultiOffset)
                               2181                 : {
                               2182                 :     debug_elog4(DEBUG2, "MultiXact: setting next multi to %u offset %u",
 6514 tgl                      2183 ECB             :                 nextMulti, nextMultiOffset);
 4080 tgl                      2184 CBC        1506 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
 6555                          2185            1506 :     MultiXactState->nextMXact = nextMulti;
 6514                          2186            1506 :     MultiXactState->nextOffset = nextMultiOffset;
 4080 tgl                      2187 GIC        1506 :     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.
 2901 alvherre                 2199 ECB             :      */
 2901 alvherre                 2200 CBC        1506 :     if (IsBinaryUpgrade)
                               2201               6 :         MaybeExtendOffsetSlru();
 6555 tgl                      2202 GIC        1506 : }
                               2203                 : 
                               2204                 : /*
                               2205                 :  * Determine the last safe MultiXactId to allocate given the currently oldest
                               2206                 :  * datminmxid (ie, the oldest MultiXactId that might exist in any database
                               2207                 :  * of our cluster), and the OID of the (or a) database with that value.
                               2208                 :  *
                               2209                 :  * is_startup is true when we are just starting the cluster, false when we
                               2210                 :  * are updating state in a running cluster.  This only affects log messages.
                               2211                 :  */
 3728 alvherre                 2212 ECB             : void
 2217 tgl                      2213 GIC        2940 : SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid,
                               2214                 :                     bool is_startup)
                               2215                 : {
                               2216                 :     MultiXactId multiVacLimit;
                               2217                 :     MultiXactId multiWarnLimit;
                               2218                 :     MultiXactId multiStopLimit;
                               2219                 :     MultiXactId multiWrapLimit;
                               2220                 :     MultiXactId curMulti;
                               2221                 :     bool        needs_offset_vacuum;
 3728 alvherre                 2222 ECB             : 
 3728 alvherre                 2223 GIC        2940 :     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.
 3728 alvherre                 2231 ECB             :      */
 3728 alvherre                 2232 CBC        2940 :     multiWrapLimit = oldest_datminmxid + (MaxMultiXactId >> 1);
 3728 alvherre                 2233 GBC        2940 :     if (multiWrapLimit < FirstMultiXactId)
 3728 alvherre                 2234 UIC           0 :         multiWrapLimit += FirstMultiXactId;
                               2235                 : 
                               2236                 :     /*
                               2237                 :      * We'll refuse to continue assigning MultiXactIds once we get within 3M
                               2238                 :      * multi of data loss.  See SetTransactionIdLimit.
 3728 alvherre                 2239 ECB             :      */
  981 noah                     2240 CBC        2940 :     multiStopLimit = multiWrapLimit - 3000000;
 3728 alvherre                 2241 GBC        2940 :     if (multiStopLimit < FirstMultiXactId)
 3728 alvherre                 2242 UIC           0 :         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.)
 3728 alvherre                 2253 ECB             :      */
  981 noah                     2254 CBC        2940 :     multiWarnLimit = multiWrapLimit - 40000000;
 3728 alvherre                 2255 GBC        2940 :     if (multiWarnLimit < FirstMultiXactId)
 3728 alvherre                 2256 UIC           0 :         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.
 3728 alvherre                 2265 ECB             :      */
 3342 alvherre                 2266 CBC        2940 :     multiVacLimit = oldest_datminmxid + autovacuum_multixact_freeze_max_age;
 3728 alvherre                 2267 GBC        2940 :     if (multiVacLimit < FirstMultiXactId)
 3728 alvherre                 2268 UIC           0 :         multiVacLimit += FirstMultiXactId;
                               2269                 : 
 3728 alvherre                 2270 ECB             :     /* Grab lock for just long enough to set the new limit values */
 3728 alvherre                 2271 CBC        2940 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
                               2272            2940 :     MultiXactState->oldestMultiXactId = oldest_datminmxid;
                               2273            2940 :     MultiXactState->oldestMultiXactDB = oldest_datoid;
                               2274            2940 :     MultiXactState->multiVacLimit = multiVacLimit;
                               2275            2940 :     MultiXactState->multiWarnLimit = multiWarnLimit;
                               2276            2940 :     MultiXactState->multiStopLimit = multiStopLimit;
                               2277            2940 :     MultiXactState->multiWrapLimit = multiWrapLimit;
                               2278            2940 :     curMulti = MultiXactState->nextMXact;
 3728 alvherre                 2279 GIC        2940 :     LWLockRelease(MultiXactGenLock);
                               2280                 : 
 3728 alvherre                 2281 ECB             :     /* Log the info */
 3728 alvherre                 2282 GIC        2940 :     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.
 2752 andres                   2292 ECB             :      */
 2752 andres                   2293 CBC        2940 :     if (!MultiXactState->finishedStartup)
 2752 andres                   2294 GIC        1481 :         return;
 2752 andres                   2295 ECB             : 
 2752 andres                   2296 GIC        1459 :     Assert(!InRecovery);
                               2297                 : 
 2865 rhaas                    2298 ECB             :     /* Set limits for offset vacuum. */
 2217 tgl                      2299 GIC        1459 :     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.
 3728 alvherre                 2307 ECB             :      */
 2890 rhaas                    2308 GBC        1459 :     if ((MultiXactIdPrecedes(multiVacLimit, curMulti) ||
 2752 andres                   2309 UBC           0 :          needs_offset_vacuum) && IsUnderPostmaster)
 3728 alvherre                 2310 UIC           0 :         SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
                               2311                 : 
 3728 alvherre                 2312 ECB             :     /* Give an immediate warning if past the wrap warn point */
 2752 andres                   2313 GIC        1459 :     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.
 3728 alvherre                 2325 EUB             :          */
 3728 alvherre                 2326 UBC           0 :         if (IsTransactionState())
 3728 alvherre                 2327 UIC           0 :             oldest_datname = get_database_name(oldest_datoid);
 3728 alvherre                 2328 EUB             :         else
 3728 alvherre                 2329 UIC           0 :             oldest_datname = NULL;
 3728 alvherre                 2330 EUB             : 
 3728 alvherre                 2331 UBC           0 :         if (oldest_datname)
 3728 alvherre                 2332 UIC           0 :             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.")));
 3728 alvherre                 2340 EUB             :         else
 3728 alvherre                 2341 UIC           0 :             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                 : }
                               2351                 : 
                               2352                 : /*
                               2353                 :  * Ensure the next-to-be-assigned MultiXactId is at least minMulti,
                               2354                 :  * and similarly nextOffset is at least minMultiOffset.
                               2355                 :  *
                               2356                 :  * This is used when we can determine minimum safe values from an XLog
                               2357                 :  * record (either an on-line checkpoint or an mxact creation log entry).
                               2358                 :  * Although this is only called during XLog replay, we take the lock in case
                               2359                 :  * any hot-standby backends are examining the values.
                               2360                 :  */
 6555 tgl                      2361 ECB             : void
 6514 tgl                      2362 GIC         143 : MultiXactAdvanceNextMXact(MultiXactId minMulti,
                               2363                 :                           MultiXactOffset minMultiOffset)
 6555 tgl                      2364 ECB             : {
 4080 tgl                      2365 CBC         143 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
 6555 tgl                      2366 GIC         143 :     if (MultiXactIdPrecedes(MultiXactState->nextMXact, minMulti))
                               2367                 :     {
 6555 tgl                      2368 ECB             :         debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
 6555 tgl                      2369 GIC           2 :         MultiXactState->nextMXact = minMulti;
 6514 tgl                      2370 ECB             :     }
 6514 tgl                      2371 GIC         143 :     if (MultiXactOffsetPrecedes(MultiXactState->nextOffset, minMultiOffset))
                               2372                 :     {
                               2373                 :         debug_elog3(DEBUG2, "MultiXact: setting next offset to %u",
 6514 tgl                      2374 ECB             :                     minMultiOffset);
 6514 tgl                      2375 GIC           2 :         MultiXactState->nextOffset = minMultiOffset;
 6555 tgl                      2376 ECB             :     }
 4080 tgl                      2377 CBC         143 :     LWLockRelease(MultiXactGenLock);
 6555 tgl                      2378 GIC         143 : }
                               2379                 : 
                               2380                 : /*
                               2381                 :  * Update our oldestMultiXactId value, but only if it's more recent than what
                               2382                 :  * we had.
                               2383                 :  *
                               2384                 :  * This may only be called during WAL replay.
                               2385                 :  */
 3728 alvherre                 2386 ECB             : void
 3728 alvherre                 2387 GIC         166 : MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
 3728 alvherre                 2388 ECB             : {
 2752 andres                   2389 GIC         166 :     Assert(InRecovery);
 2752 andres                   2390 ECB             : 
 3728 alvherre                 2391 GBC         166 :     if (MultiXactIdPrecedes(MultiXactState->oldestMultiXactId, oldestMulti))
 2217 tgl                      2392 LBC           0 :         SetMultiXactIdLimit(oldestMulti, oldestMultiDB, false);
 3208 alvherre                 2393 GIC         166 : }
                               2394                 : 
                               2395                 : /*
                               2396                 :  * Make sure that MultiXactOffset has room for a newly-allocated MultiXactId.
                               2397                 :  *
                               2398                 :  * NB: this is called while holding MultiXactGenLock.  We want it to be very
                               2399                 :  * fast most of the time; even when it's not so fast, no actual I/O need
                               2400                 :  * happen unless we're forced to write out a dirty log or xlog page to make
                               2401                 :  * room in shared memory.
                               2402                 :  */
 6514 tgl                      2403 ECB             : static void
 6555 tgl                      2404 GIC         272 : ExtendMultiXactOffset(MultiXactId multi)
                               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.
 6555 tgl                      2411 ECB             :      */
 6555 tgl                      2412 GIC         272 :     if (MultiXactIdToOffsetEntry(multi) != 0 &&
 6555 tgl                      2413 ECB             :         multi != FirstMultiXactId)
 6555 tgl                      2414 GIC         262 :         return;
 6555 tgl                      2415 ECB             : 
 6555 tgl                      2416 GIC          10 :     pageno = MultiXactIdToOffsetPage(multi);
 6555 tgl                      2417 ECB             : 
 1059 tgl                      2418 GIC          10 :     LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
                               2419                 : 
 6514 tgl                      2420 ECB             :     /* Zero the page and make an XLOG entry about it */
 6514 tgl                      2421 GIC          10 :     ZeroMultiXactOffsetPage(pageno, true);
 6514 tgl                      2422 ECB             : 
 1059 tgl                      2423 GIC          10 :     LWLockRelease(MultiXactOffsetSLRULock);
                               2424                 : }
                               2425                 : 
                               2426                 : /*
                               2427                 :  * Make sure that MultiXactMember has room for the members of a newly-
                               2428                 :  * allocated MultiXactId.
                               2429                 :  *
                               2430                 :  * Like the above routine, this is called while holding MultiXactGenLock;
                               2431                 :  * same comments apply.
                               2432                 :  */
 6514 tgl                      2433 ECB             : static void
 6514 tgl                      2434 GIC         272 : ExtendMultiXactMember(MultiXactOffset offset, int nmembers)
                               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.
 6555 tgl                      2441 ECB             :      */
 6514 tgl                      2442 GIC         544 :     while (nmembers > 0)
                               2443                 :     {
                               2444                 :         int         flagsoff;
                               2445                 :         int         flagsbit;
                               2446                 :         uint32      difference;
                               2447                 : 
                               2448                 :         /*
                               2449                 :          * Only zero when at first entry of a page.
 6514 tgl                      2450 ECB             :          */
 3728 alvherre                 2451 CBC         272 :         flagsoff = MXOffsetToFlagsOffset(offset);
                               2452             272 :         flagsbit = MXOffsetToFlagsBitShift(offset);
 3728 alvherre                 2453 GIC         272 :         if (flagsoff == 0 && flagsbit == 0)
                               2454                 :         {
                               2455                 :             int         pageno;
 6555 tgl                      2456 ECB             : 
 6514 tgl                      2457 GIC          10 :             pageno = MXOffsetToMemberPage(offset);
 6514 tgl                      2458 ECB             : 
 1059 tgl                      2459 GIC          10 :             LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
                               2460                 : 
 6514 tgl                      2461 ECB             :             /* Zero the page and make an XLOG entry about it */
 6514 tgl                      2462 GIC          10 :             ZeroMultiXactMemberPage(pageno, true);
 6514 tgl                      2463 ECB             : 
 1059 tgl                      2464 GIC          10 :             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.
 3384 alvherre                 2472 ECB             :          */
 3226 alvherre                 2473 GIC         272 :         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.
 3226 alvherre                 2479 EUB             :              */
 3226 alvherre                 2480 UIC           0 :             difference = MaxMultiXactOffset - offset + 1;
                               2481                 :         }
 3384 alvherre                 2482 ECB             :         else
 3384 alvherre                 2483 GIC         272 :             difference = MULTIXACT_MEMBERS_PER_PAGE - offset % MULTIXACT_MEMBERS_PER_PAGE;
                               2484                 : 
                               2485                 :         /*
                               2486                 :          * Advance to next page, taking care to properly handle the wraparound
                               2487                 :          * case.  OK if nmembers goes negative.
 3226 alvherre                 2488 ECB             :          */
 3226 alvherre                 2489 CBC         272 :         nmembers -= difference;
 3226 alvherre                 2490 GIC         272 :         offset += difference;
 6514 tgl                      2491 ECB             :     }
 6555 tgl                      2492 GIC         272 : }
                               2493                 : 
                               2494                 : /*
                               2495                 :  * GetOldestMultiXactId
                               2496                 :  *
                               2497                 :  * Return the oldest MultiXactId that's still possibly still seen as live by
                               2498                 :  * any running transaction.  Older ones might still exist on disk, but they no
                               2499                 :  * longer have any running member transaction.
                               2500                 :  *
                               2501                 :  * It's not safe to truncate MultiXact SLRU segments on the value returned by
                               2502                 :  * this function; however, it can be set as the new relminmxid for any table
                               2503                 :  * that VACUUM knows has no remaining MXIDs < the same value.  It is only safe
                               2504                 :  * to truncate SLRUs when no table can possibly still have a referencing MXID.
                               2505                 :  */
                               2506                 : MultiXactId
 3728 alvherre                 2507 CBC       93785 : GetOldestMultiXactId(void)
                               2508                 : {
                               2509                 :     MultiXactId oldestMXact;
                               2510                 :     MultiXactId nextMXact;
                               2511                 :     int         i;
                               2512                 : 
                               2513                 :     /*
                               2514                 :      * This is the oldest valid value among all the OldestMemberMXactId[] and
                               2515                 :      * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
                               2516                 :      */
 6555 tgl                      2517           93785 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
                               2518                 : 
                               2519                 :     /*
                               2520                 :      * We have to beware of the possibility that nextMXact is in the
                               2521                 :      * wrapped-around state.  We don't fix the counter itself here, but we
                               2522                 :      * must be sure to use a valid value in our calculation.
                               2523                 :      */
                               2524           93785 :     nextMXact = MultiXactState->nextMXact;
                               2525           93785 :     if (nextMXact < FirstMultiXactId)
 6555 tgl                      2526 UBC           0 :         nextMXact = FirstMultiXactId;
                               2527                 : 
 6555 tgl                      2528 CBC       93785 :     oldestMXact = nextMXact;
  362 rhaas                    2529        11033905 :     for (i = 1; i <= MaxOldestSlot; i++)
                               2530                 :     {
                               2531                 :         MultiXactId thisoldest;
                               2532                 : 
 6555 tgl                      2533        10940120 :         thisoldest = OldestMemberMXactId[i];
                               2534        10956348 :         if (MultiXactIdIsValid(thisoldest) &&
                               2535           16228 :             MultiXactIdPrecedes(thisoldest, oldestMXact))
                               2536               8 :             oldestMXact = thisoldest;
                               2537        10940120 :         thisoldest = OldestVisibleMXactId[i];
                               2538        10940127 :         if (MultiXactIdIsValid(thisoldest) &&
                               2539               7 :             MultiXactIdPrecedes(thisoldest, oldestMXact))
                               2540               2 :             oldestMXact = thisoldest;
                               2541                 :     }
                               2542                 : 
                               2543           93785 :     LWLockRelease(MultiXactGenLock);
                               2544                 : 
 3728 alvherre                 2545           93785 :     return oldestMXact;
                               2546                 : }
                               2547                 : 
                               2548                 : /*
                               2549                 :  * Determine how aggressively we need to vacuum in order to prevent member
                               2550                 :  * wraparound.
                               2551                 :  *
                               2552                 :  * To do so determine what's the oldest member offset and install the limit
                               2553                 :  * info in MultiXactState, where it can be used to prevent overrun of old data
                               2554                 :  * in the members SLRU area.
                               2555                 :  *
                               2556                 :  * The return value is true if emergency autovacuum is required and false
                               2557                 :  * otherwise.
                               2558                 :  */
                               2559                 : static bool
 2217 tgl                      2560            1459 : SetOffsetVacuumLimit(bool is_startup)
                               2561                 : {
                               2562                 :     MultiXactId oldestMultiXactId;
                               2563                 :     MultiXactId nextMXact;
 2752 andres                   2564            1459 :     MultiXactOffset oldestOffset = 0;   /* placate compiler */
                               2565                 :     MultiXactOffset prevOldestOffset;
                               2566                 :     MultiXactOffset nextOffset;
 2865 rhaas                    2567            1459 :     bool        oldestOffsetKnown = false;
                               2568                 :     bool        prevOldestOffsetKnown;
 2752 andres                   2569            1459 :     MultiXactOffset offsetStopLimit = 0;
                               2570                 :     MultiXactOffset prevOffsetStopLimit;
                               2571                 : 
                               2572                 :     /*
                               2573                 :      * NB: Have to prevent concurrent truncation, we might otherwise try to
                               2574                 :      * lookup an oldestMulti that's concurrently getting truncated away.
                               2575                 :      */
                               2576            1459 :     LWLockAcquire(MultiXactTruncationLock, LW_SHARED);
                               2577                 : 
                               2578                 :     /* Read relevant fields from shared memory. */
 2865 rhaas                    2579            1459 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
                               2580            1459 :     oldestMultiXactId = MultiXactState->oldestMultiXactId;
                               2581            1459 :     nextMXact = MultiXactState->nextMXact;
                               2582            1459 :     nextOffset = MultiXactState->nextOffset;
                               2583            1459 :     prevOldestOffsetKnown = MultiXactState->oldestOffsetKnown;
 2752 andres                   2584            1459 :     prevOldestOffset = MultiXactState->oldestOffset;
 2673                          2585            1459 :     prevOffsetStopLimit = MultiXactState->offsetStopLimit;
 2752                          2586            1459 :     Assert(MultiXactState->finishedStartup);
 2903 alvherre                 2587            1459 :     LWLockRelease(MultiXactGenLock);
                               2588                 : 
                               2589                 :     /*
                               2590                 :      * Determine the offset of the oldest multixact.  Normally, we can read
                               2591                 :      * the offset from the multixact itself, but there's an important special
                               2592                 :      * case: if there are no multixacts in existence at all, oldestMXact
                               2593                 :      * obviously can't point to one.  It will instead point to the multixact
                               2594                 :      * ID that will be assigned the next time one is needed.
                               2595                 :      */
 2865 rhaas                    2596            1459 :     if (oldestMultiXactId == nextMXact)
                               2597                 :     {
                               2598                 :         /*
                               2599                 :          * When the next multixact gets created, it will be stored at the next
                               2600                 :          * offset.
                               2601                 :          */
                               2602            1452 :         oldestOffset = nextOffset;
                               2603            1452 :         oldestOffsetKnown = true;
                               2604                 :     }
                               2605                 :     else
                               2606                 :     {
                               2607                 :         /*
                               2608                 :          * Figure out where the oldest existing multixact's offsets are
                               2609                 :          * stored. Due to bugs in early release of PostgreSQL 9.3.X and 9.4.X,
                               2610                 :          * the supposedly-earliest multixact might not really exist.  We are
                               2611                 :          * careful not to fail in that case.
                               2612                 :          */
                               2613                 :         oldestOffsetKnown =
                               2614               7 :             find_multixact_start(oldestMultiXactId, &oldestOffset);
                               2615                 : 
                               2616               7 :         if (oldestOffsetKnown)
                               2617               7 :             ereport(DEBUG1,
                               2618                 :                     (errmsg_internal("oldest MultiXactId member is at offset %u",
                               2619                 :                                      oldestOffset)));
                               2620                 :         else
 2752 andres                   2621 UBC           0 :             ereport(LOG,
                               2622                 :                     (errmsg("MultiXact member wraparound protections are disabled because oldest checkpointed MultiXact %u does not exist on disk",
                               2623                 :                             oldestMultiXactId)));
                               2624                 :     }
                               2625                 : 
 2752 andres                   2626 CBC        1459 :     LWLockRelease(MultiXactTruncationLock);
                               2627                 : 
                               2628                 :     /*
                               2629                 :      * If we can, compute limits (and install them MultiXactState) to prevent
                               2630                 :      * overrun of old data in the members SLRU area. We can only do so if the
                               2631                 :      * oldest offset is known though.
                               2632                 :      */
                               2633            1459 :     if (oldestOffsetKnown)
                               2634                 :     {
                               2635                 :         /* move back to start of the corresponding segment */
                               2636            1459 :         offsetStopLimit = oldestOffset - (oldestOffset %
                               2637                 :                                           (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT));
                               2638                 : 
                               2639                 :         /* always leave one segment before the wraparound point */
                               2640            1459 :         offsetStopLimit -= (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT);
                               2641                 : 
 2217 tgl                      2642            1459 :         if (!prevOldestOffsetKnown && !is_startup)
 2752 andres                   2643 UBC           0 :             ereport(LOG,
                               2644                 :                     (errmsg("MultiXact member wraparound protections are now enabled")));
                               2645                 : 
 2752 andres                   2646 CBC        1459 :         ereport(DEBUG1,
                               2647                 :                 (errmsg_internal("MultiXact member stop limit is now %u based on MultiXact %u",
                               2648                 :                                  offsetStopLimit, oldestMultiXactId)));
                               2649                 :     }
 2752 andres                   2650 UBC           0 :     else if (prevOldestOffsetKnown)
                               2651                 :     {
                               2652                 :         /*
                               2653                 :          * If we failed to get the oldest offset this time, but we have a
                               2654                 :          * value from a previous pass through this function, use the old
                               2655                 :          * values rather than automatically forcing an emergency autovacuum
                               2656                 :          * cycle again.
                               2657                 :          */
 2851 rhaas                    2658               0 :         oldestOffset = prevOldestOffset;
                               2659               0 :         oldestOffsetKnown = true;
 2673 andres                   2660               0 :         offsetStopLimit = prevOffsetStopLimit;
                               2661                 :     }
                               2662                 : 
                               2663                 :     /* Install the computed values */
 2752 andres                   2664 CBC        1459 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
                               2665            1459 :     MultiXactState->oldestOffset = oldestOffset;
                               2666            1459 :     MultiXactState->oldestOffsetKnown = oldestOffsetKnown;
                               2667            1459 :     MultiXactState->offsetStopLimit = offsetStopLimit;
                               2668            1459 :     LWLockRelease(MultiXactGenLock);
                               2669                 : 
                               2670                 :     /*
                               2671                 :      * Do we need an emergency autovacuum?  If we're not sure, assume yes.
                               2672                 :      */
 2865 rhaas                    2673            2918 :     return !oldestOffsetKnown ||
                               2674            1459 :         (nextOffset - oldestOffset > MULTIXACT_MEMBER_SAFE_THRESHOLD);
                               2675                 : }
                               2676                 : 
                               2677                 : /*
                               2678                 :  * Return whether adding "distance" to "start" would move past "boundary".
                               2679                 :  *
                               2680                 :  * We use this to determine whether the addition is "wrapping around" the
                               2681                 :  * boundary point, hence the name.  The reason we don't want to use the regular
                               2682                 :  * 2^31-modulo arithmetic here is that we want to be able to use the whole of
                               2683                 :  * the 2^32-1 space here, allowing for more multixacts than would fit
                               2684                 :  * otherwise.
                               2685                 :  */
                               2686                 : static bool
 2903 alvherre                 2687             544 : MultiXactOffsetWouldWrap(MultiXactOffset boundary, MultiXactOffset start,
                               2688                 :                          uint32 distance)
                               2689                 : {
                               2690                 :     MultiXactOffset finish;
                               2691                 : 
                               2692                 :     /*
                               2693                 :      * Note that offset number 0 is not used (see GetMultiXactIdMembers), so
                               2694                 :      * if the addition wraps around the UINT_MAX boundary, skip that value.
                               2695                 :      */
                               2696             544 :     finish = start + distance;
                               2697             544 :     if (finish < start)
 2903 alvherre                 2698 UBC           0 :         finish++;
                               2699                 : 
                               2700                 :     /*-----------------------------------------------------------------------
                               2701                 :      * When the boundary is numerically greater than the starting point, any
                               2702                 :      * value numerically between the two is not wrapped:
                               2703                 :      *
                               2704                 :      *  <----S----B---->
                               2705                 :      *  [---)            = F wrapped past B (and UINT_MAX)
                               2706                 :      *       [---)       = F not wrapped
                               2707                 :      *            [----] = F wrapped past B
                               2708                 :      *
                               2709                 :      * When the boundary is numerically less than the starting point (i.e. the
                               2710                 :      * UINT_MAX wraparound occurs somewhere in between) then all values in
                               2711                 :      * between are wrapped:
                               2712                 :      *
                               2713                 :      *  <----B----S---->
                               2714                 :      *  [---)            = F not wrapped past B (but wrapped past UINT_MAX)
                               2715                 :      *       [---)       = F wrapped past B (and UINT_MAX)
                               2716                 :      *            [----] = F not wrapped
                               2717                 :      *-----------------------------------------------------------------------
                               2718                 :      */
 2903 alvherre                 2719 CBC         544 :     if (start < boundary)
                               2720             544 :         return finish >= boundary || finish < start;
                               2721                 :     else
 2903 alvherre                 2722 UBC           0 :         return finish >= boundary && finish < start;
                               2723                 : }
                               2724                 : 
                               2725                 : /*
                               2726                 :  * Find the starting offset of the given MultiXactId.
                               2727                 :  *
                               2728                 :  * Returns false if the file containing the multi does not exist on disk.
                               2729                 :  * Otherwise, returns true and sets *result to the starting member offset.
                               2730                 :  *
                               2731                 :  * This function does not prevent concurrent truncation, so if that's
                               2732                 :  * required, the caller has to protect against that.
                               2733                 :  */
                               2734                 : static bool
 2865 rhaas                    2735 CBC           7 : find_multixact_start(MultiXactId multi, MultiXactOffset *result)
                               2736                 : {
                               2737                 :     MultiXactOffset offset;
                               2738                 :     int         pageno;
                               2739                 :     int         entryno;
                               2740                 :     int         slotno;
                               2741                 :     MultiXactOffset *offptr;
                               2742                 : 
 2752 andres                   2743               7 :     Assert(MultiXactState->finishedStartup);
                               2744                 : 
 2903 alvherre                 2745               7 :     pageno = MultiXactIdToOffsetPage(multi);
                               2746               7 :     entryno = MultiXactIdToOffsetEntry(multi);
                               2747                 : 
                               2748                 :     /*
                               2749                 :      * Write out dirty data, so PhysicalPageExists can work correctly.
                               2750                 :      */
  926 tmunro                   2751               7 :     SimpleLruWriteAll(MultiXactOffsetCtl, true);
                               2752               7 :     SimpleLruWriteAll(MultiXactMemberCtl, true);
                               2753                 : 
 2865 rhaas                    2754               7 :     if (!SimpleLruDoesPhysicalPageExist(MultiXactOffsetCtl, pageno))
 2865 rhaas                    2755 UBC           0 :         return false;
                               2756                 : 
                               2757                 :     /* lock is acquired by SimpleLruReadPage_ReadOnly */
 2903 alvherre                 2758 CBC           7 :     slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, multi);
                               2759               7 :     offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
                               2760               7 :     offptr += entryno;
                               2761               7 :     offset = *offptr;
 1059 tgl                      2762               7 :     LWLockRelease(MultiXactOffsetSLRULock);
                               2763                 : 
 2865 rhaas                    2764               7 :     *result = offset;
                               2765               7 :     return true;
                               2766                 : }
                               2767                 : 
                               2768                 : /*
                               2769                 :  * Determine how many multixacts, and how many multixact members, currently
                               2770                 :  * exist.  Return false if unable to determine.
                               2771                 :  */
                               2772                 : static bool
 2893                          2773           37057 : ReadMultiXactCounts(uint32 *multixacts, MultiXactOffset *members)
                               2774                 : {
                               2775                 :     MultiXactOffset nextOffset;
                               2776                 :     MultiXactOffset oldestOffset;
                               2777                 :     MultiXactId oldestMultiXactId;
                               2778                 :     MultiXactId nextMultiXactId;
                               2779                 :     bool        oldestOffsetKnown;
                               2780                 : 
                               2781           37057 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
                               2782           37057 :     nextOffset = MultiXactState->nextOffset;
                               2783           37057 :     oldestMultiXactId = MultiXactState->oldestMultiXactId;
                               2784           37057 :     nextMultiXactId = MultiXactState->nextMXact;
 2890                          2785           37057 :     oldestOffset = MultiXactState->oldestOffset;
 2865                          2786           37057 :     oldestOffsetKnown = MultiXactState->oldestOffsetKnown;
 2893                          2787           37057 :     LWLockRelease(MultiXactGenLock);
                               2788                 : 
 2865                          2789           37057 :     if (!oldestOffsetKnown)
 2865 rhaas                    2790 UBC           0 :         return false;
                               2791                 : 
 2893 rhaas                    2792 CBC       37057 :     *members = nextOffset - oldestOffset;
                               2793           37057 :     *multixacts = nextMultiXactId - oldestMultiXactId;
 2865                          2794           37057 :     return true;
                               2795                 : }
                               2796                 : 
                               2797                 : /*
                               2798                 :  * Multixact members can be removed once the multixacts that refer to them
                               2799                 :  * are older than every datminmxid.  autovacuum_multixact_freeze_max_age and
                               2800                 :  * vacuum_multixact_freeze_table_age work together to make sure we never have
                               2801                 :  * too many multixacts; we hope that, at least under normal circumstances,
                               2802                 :  * this will also be sufficient to keep us from using too many offsets.
                               2803                 :  * However, if the average multixact has many members, we might exhaust the
                               2804                 :  * members space while still using few enough members that these limits fail
                               2805                 :  * to trigger relminmxid advancement by VACUUM.  At that point, we'd have no
                               2806                 :  * choice but to start failing multixact-creating operations with an error.
                               2807                 :  *
                               2808                 :  * To prevent that, if more than a threshold portion of the members space is
                               2809                 :  * used, we effectively reduce autovacuum_multixact_freeze_max_age and
                               2810                 :  * to a value just less than the number of multixacts in use.  We hope that
                               2811                 :  * this will quickly trigger autovacuuming on the table or tables with the
                               2812                 :  * oldest relminmxid, thus allowing datminmxid values to advance and removing
                               2813                 :  * some members.
                               2814                 :  *
                               2815                 :  * As the fraction of the member space currently in use grows, we become
                               2816                 :  * more aggressive in clamping this value.  That not only causes autovacuum
                               2817                 :  * to ramp up, but also makes any manual vacuums the user issues more
                               2818                 :  * aggressive.  This happens because vacuum_get_cutoffs() will clamp the
                               2819                 :  * freeze table and the minimum freeze age cutoffs based on the effective
                               2820                 :  * autovacuum_multixact_freeze_max_age this function returns.  In the worst
                               2821                 :  * case, we'll claim the freeze_max_age to zero, and every vacuum of any
                               2822                 :  * table will freeze every multixact.
                               2823                 :  */
                               2824                 : int
 2893 rhaas                    2825 GIC       37057 : MultiXactMemberFreezeThreshold(void)
                               2826                 : {
                               2827                 :     MultiXactOffset members;
                               2828                 :     uint32      multixacts;
 2878 bruce                    2829 ECB             :     uint32      victim_multixacts;
 2878 bruce                    2830 EUB             :     double      fraction;
                               2831                 : 
                               2832                 :     /* If we can't determine member space utilization, assume the worst. */
 2865 rhaas                    2833 CBC       37057 :     if (!ReadMultiXactCounts(&multixacts, &members))
 2865 rhaas                    2834 LBC           0 :         return 0;
                               2835                 : 
                               2836                 :     /* If member space utilization is low, no special action is required. */
 2893 rhaas                    2837 GIC       37057 :     if (members <= MULTIXACT_MEMBER_SAFE_THRESHOLD)
                               2838           37057 :         return autovacuum_multixact_freeze_max_age;
                               2839                 : 
                               2840                 :     /*
 2893 rhaas                    2841 EUB             :      * Compute a target for relminmxid advancement.  The number of multixacts
                               2842                 :      * we try to eliminate from the system is based on how far we are past
                               2843                 :      * MULTIXACT_MEMBER_SAFE_THRESHOLD.
                               2844                 :      */
 2893 rhaas                    2845 UIC           0 :     fraction = (double) (members - MULTIXACT_MEMBER_SAFE_THRESHOLD) /
 2893 rhaas                    2846 EUB             :         (MULTIXACT_MEMBER_DANGER_THRESHOLD - MULTIXACT_MEMBER_SAFE_THRESHOLD);
 2893 rhaas                    2847 UBC           0 :     victim_multixacts = multixacts * fraction;
 2893 rhaas                    2848 EUB             : 
                               2849                 :     /* fraction could be > 1.0, but lowest possible freeze age is zero */
 2893 rhaas                    2850 UIC           0 :     if (victim_multixacts > multixacts)
                               2851               0 :         return 0;
                               2852               0 :     return multixacts - victim_multixacts;
                               2853                 : }
                               2854                 : 
                               2855                 : typedef struct mxtruncinfo
                               2856                 : {
                               2857                 :     int         earliestExistingPage;
                               2858                 : } mxtruncinfo;
                               2859                 : 
                               2860                 : /*
 3728 alvherre                 2861 EUB             :  * SlruScanDirectory callback
                               2862                 :  *      This callback determines the earliest existing page number.
                               2863                 :  */
                               2864                 : static bool
 3728 alvherre                 2865 UBC           0 : SlruScanDirCbFindEarliest(SlruCtl ctl, char *filename, int segpage, void *data)
 3728 alvherre                 2866 EUB             : {
 3602 bruce                    2867 UIC           0 :     mxtruncinfo *trunc = (mxtruncinfo *) data;
 3728 alvherre                 2868 EUB             : 
 3728 alvherre                 2869 UIC           0 :     if (trunc->earliestExistingPage == -1 ||
                               2870               0 :         ctl->PagePrecedes(segpage, trunc->earliestExistingPage))
 3728 alvherre                 2871 EUB             :     {
 3728 alvherre                 2872 UIC           0 :         trunc->earliestExistingPage = segpage;
                               2873                 :     }
                               2874                 : 
 3602 bruce                    2875               0 :     return false;               /* keep going */
                               2876                 : }
                               2877                 : 
                               2878                 : 
                               2879                 : /*
                               2880                 :  * Delete members segments [oldest, newOldest)
                               2881                 :  *
                               2882                 :  * The members SLRU can, in contrast to the offsets one, be filled to almost
                               2883                 :  * the full range at once. This means SimpleLruTruncate() can't trivially be
 2752 andres                   2884 EUB             :  * used - instead the to-be-deleted range is computed using the offsets
                               2885                 :  * SLRU. C.f. TruncateMultiXact().
                               2886                 :  */
                               2887                 : static void
 2752 andres                   2888 UBC           0 : PerformMembersTruncation(MultiXactOffset oldestOffset, MultiXactOffset newOldestOffset)
 2752 andres                   2889 EUB             : {
 2752 andres                   2890 UIC           0 :     const int   maxsegment = MXOffsetToMemberSegment(MaxMultiXactOffset);
                               2891               0 :     int         startsegment = MXOffsetToMemberSegment(oldestOffset);
                               2892               0 :     int         endsegment = MXOffsetToMemberSegment(newOldestOffset);
                               2893               0 :     int         segment = startsegment;
                               2894                 : 
 2752 andres                   2895 EUB             :     /*
                               2896                 :      * Delete all the segments but the last one. The last segment can still
                               2897                 :      * contain, possibly partially, valid data.
                               2898                 :      */
 2752 andres                   2899 UIC           0 :     while (segment != endsegment)
                               2900                 :     {
 2752 andres                   2901 UBC           0 :         elog(DEBUG2, "truncating multixact members segment %x", segment);
                               2902               0 :         SlruDeleteSegment(MultiXactMemberCtl, segment);
                               2903                 : 
 2752 andres                   2904 EUB             :         /* move to next segment, handling wraparound correctly */
 2752 andres                   2905 UIC           0 :         if (segment == maxsegment)
 2752 andres                   2906 UBC           0 :             segment = 0;
                               2907                 :         else
 2752 andres                   2908 UIC           0 :             segment += 1;
                               2909                 :     }
                               2910               0 : }
                               2911                 : 
 2752 andres                   2912 EUB             : /*
                               2913                 :  * Delete offsets segments [oldest, newOldest)
                               2914                 :  */
                               2915                 : static void
 2752 andres                   2916 UIC           0 : PerformOffsetsTruncation(MultiXactId oldestMulti, MultiXactId newOldestMulti)
                               2917                 : {
                               2918                 :     /*
                               2919                 :      * We step back one multixact to avoid passing a cutoff page that hasn't
                               2920                 :      * been created yet in the rare case that oldestMulti would be the first
 2752 andres                   2921 EUB             :      * item on a page and oldestMulti == nextMulti.  In that case, if we
                               2922                 :      * didn't subtract one, we'd trigger SimpleLruTruncate's wraparound
                               2923                 :      * detection.
                               2924                 :      */
 2752 andres                   2925 UIC           0 :     SimpleLruTruncate(MultiXactOffsetCtl,
 2118 tgl                      2926               0 :                       MultiXactIdToOffsetPage(PreviousMultiXactId(newOldestMulti)));
 2752 andres                   2927               0 : }
                               2928                 : 
                               2929                 : /*
                               2930                 :  * Remove all MultiXactOffset and MultiXactMember segments before the oldest
                               2931                 :  * ones still of interest.
                               2932                 :  *
                               2933                 :  * This is only called on a primary as part of vacuum (via
                               2934                 :  * vac_truncate_clog()). During recovery truncation is done by replaying
                               2935                 :  * truncation WAL records logged here.
                               2936                 :  *
 2752 andres                   2937 ECB             :  * newOldestMulti is the oldest currently required multixact, newOldestMultiDB
                               2938                 :  * is one of the databases preventing newOldestMulti from increasing.
                               2939                 :  */
                               2940                 : void
 2752 andres                   2941 GIC         317 : TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
                               2942                 : {
                               2943                 :     MultiXactId oldestMulti;
                               2944                 :     MultiXactId nextMulti;
                               2945                 :     MultiXactOffset newOldestOffset;
                               2946                 :     MultiXactOffset oldestOffset;
 2752 andres                   2947 ECB             :     MultiXactOffset nextOffset;
 3602 bruce                    2948                 :     mxtruncinfo trunc;
                               2949                 :     MultiXactId earliest;
                               2950                 : 
 2752 andres                   2951 GIC         317 :     Assert(!RecoveryInProgress());
                               2952             317 :     Assert(MultiXactState->finishedStartup);
                               2953                 : 
                               2954                 :     /*
                               2955                 :      * We can only allow one truncation to happen at once. Otherwise parts of
 2752 andres                   2956 ECB             :      * members might vanish while we're doing lookups or similar. There's no
                               2957                 :      * need to have an interlock with creating new multis or such, since those
                               2958                 :      * are constrained by the limits (which only grow, never shrink).
                               2959                 :      */
 2752 andres                   2960 CBC         317 :     LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
 3208 alvherre                 2961 ECB             : 
 3208 alvherre                 2962 CBC         317 :     LWLockAcquire(MultiXactGenLock, LW_SHARED);
 2752 andres                   2963             317 :     nextMulti = MultiXactState->nextMXact;
 2865 rhaas                    2964 GIC         317 :     nextOffset = MultiXactState->nextOffset;
 2752 andres                   2965             317 :     oldestMulti = MultiXactState->oldestMultiXactId;
 3208 alvherre                 2966             317 :     LWLockRelease(MultiXactGenLock);
 2752 andres                   2967             317 :     Assert(MultiXactIdIsValid(oldestMulti));
                               2968                 : 
                               2969                 :     /*
 2752 andres                   2970 ECB             :      * Make sure to only attempt truncation if there's values to truncate
                               2971                 :      * away. In normal processing values shouldn't go backwards, but there's
                               2972                 :      * some corner cases (due to bugs) where that's possible.
                               2973                 :      */
 2752 andres                   2974 GIC         317 :     if (MultiXactIdPrecedesOrEquals(newOldestMulti, oldestMulti))
                               2975                 :     {
                               2976             317 :         LWLockRelease(MultiXactTruncationLock);
                               2977             317 :         return;
                               2978                 :     }
                               2979                 : 
                               2980                 :     /*
                               2981                 :      * Note we can't just plow ahead with the truncation; it's possible that
                               2982                 :      * there are no segments to truncate, which is a problem because we are
                               2983                 :      * going to attempt to read the offsets page to determine where to
                               2984                 :      * truncate the members SLRU.  So we first scan the directory to determine
                               2985                 :      * the earliest offsets page number that we can read without error.
                               2986                 :      *
                               2987                 :      * When nextMXact is less than one segment away from multiWrapLimit,
                               2988                 :      * SlruScanDirCbFindEarliest can find some early segment other than the
                               2989                 :      * actual earliest.  (MultiXactOffsetPagePrecedes(EARLIEST, LATEST)
                               2990                 :      * returns false, because not all pairs of entries have the same answer.)
                               2991                 :      * That can also arise when an earlier truncation attempt failed unlink()
                               2992                 :      * or returned early from this function.  The only consequence is
                               2993                 :      * returning early, which wastes space that we could have liberated.
  813 noah                     2994 EUB             :      *
 2752 andres                   2995                 :      * NB: It's also possible that the page that oldestMulti is on has already
                               2996                 :      * been truncated away, and we crashed before updating oldestMulti.
 6555 tgl                      2997                 :      */
 3728 alvherre                 2998 UBC           0 :     trunc.earliestExistingPage = -1;
 3728 alvherre                 2999 UIC           0 :     SlruScanDirectory(MultiXactOffsetCtl, SlruScanDirCbFindEarliest, &trunc);
                               3000               0 :     earliest = trunc.earliestExistingPage * MULTIXACT_OFFSETS_PER_PAGE;
 3208 alvherre                 3001 UBC           0 :     if (earliest < FirstMultiXactId)
 3208 alvherre                 3002 UIC           0 :         earliest = FirstMultiXactId;
 3728 alvherre                 3003 EUB             : 
 2752 andres                   3004                 :     /* If there's nothing to remove, we can bail out early. */
 2752 andres                   3005 UIC           0 :     if (MultiXactIdPrecedes(oldestMulti, earliest))
                               3006                 :     {
                               3007               0 :         LWLockRelease(MultiXactTruncationLock);
 6555 tgl                      3008               0 :         return;
                               3009                 :     }
                               3010                 : 
                               3011                 :     /*
                               3012                 :      * First, compute the safe truncation point for MultiXactMember. This is
                               3013                 :      * the starting offset of the oldest multixact.
                               3014                 :      *
 2865 rhaas                    3015 EUB             :      * Hopefully, find_multixact_start will always work here, because we've
                               3016                 :      * already checked that it doesn't precede the earliest MultiXact on disk.
                               3017                 :      * But if it fails, don't truncate anything, and log a message.
 6555 tgl                      3018                 :      */
 2752 andres                   3019 UIC           0 :     if (oldestMulti == nextMulti)
 2752 andres                   3020 EUB             :     {
                               3021                 :         /* there are NO MultiXacts */
 2752 andres                   3022 UBC           0 :         oldestOffset = nextOffset;
                               3023                 :     }
 2752 andres                   3024 UIC           0 :     else if (!find_multixact_start(oldestMulti, &oldestOffset))
 2865 rhaas                    3025 EUB             :     {
 2865 rhaas                    3026 UBC           0 :         ereport(LOG,
                               3027                 :                 (errmsg("oldest MultiXact %u not found, earliest MultiXact %u, skipping truncation",
                               3028                 :                         oldestMulti, earliest)));
 2752 andres                   3029 UIC           0 :         LWLockRelease(MultiXactTruncationLock);
 2865 rhaas                    3030               0 :         return;
                               3031                 :     }
                               3032                 : 
 3384 alvherre                 3033 EUB             :     /*
                               3034                 :      * Secondly compute up to where to truncate. Lookup the corresponding
                               3035                 :      * member offset for newOldestMulti for that.
                               3036                 :      */
 2752 andres                   3037 UIC           0 :     if (newOldestMulti == nextMulti)
 2752 andres                   3038 EUB             :     {
                               3039                 :         /* there are NO MultiXacts */
 2752 andres                   3040 UBC           0 :         newOldestOffset = nextOffset;
                               3041                 :     }
 2752 andres                   3042 UIC           0 :     else if (!find_multixact_start(newOldestMulti, &newOldestOffset))
 2752 andres                   3043 EUB             :     {
 2752 andres                   3044 UBC           0 :         ereport(LOG,
                               3045                 :                 (errmsg("cannot truncate up to MultiXact %u because it does not exist on disk, skipping truncation",
                               3046                 :                         newOldestMulti)));
                               3047               0 :         LWLockRelease(MultiXactTruncationLock);
 2752 andres                   3048 UIC           0 :         return;
                               3049                 :     }
                               3050                 : 
                               3051               0 :     elog(DEBUG1, "performing multixact truncation: "
                               3052                 :          "offsets [%u, %u), offsets segments [%x, %x), "
                               3053                 :          "members [%u, %u), members segments [%x, %x)",
                               3054                 :          oldestMulti, newOldestMulti,
                               3055                 :          MultiXactIdToOffsetSegment(oldestMulti),
                               3056                 :          MultiXactIdToOffsetSegment(newOldestMulti),
                               3057                 :          oldestOffset, newOldestOffset,
                               3058                 :          MXOffsetToMemberSegment(oldestOffset),
                               3059                 :          MXOffsetToMemberSegment(newOldestOffset));
                               3060                 : 
                               3061                 :     /*
                               3062                 :      * Do truncation, and the WAL logging of the truncation, in a critical
 2752 andres                   3063 EUB             :      * section. That way offsets/members cannot get out of sync anymore, i.e.
                               3064                 :      * once consistent the newOldestMulti will always exist in members, even
                               3065                 :      * if we crashed in the wrong moment.
                               3066                 :      */
 2752 andres                   3067 UIC           0 :     START_CRIT_SECTION();
                               3068                 : 
                               3069                 :     /*
                               3070                 :      * Prevent checkpoints from being scheduled concurrently. This is critical
 2752 andres                   3071 EUB             :      * because otherwise a truncation record might not be replayed after a
                               3072                 :      * crash/basebackup, even though the state of the data directory would
                               3073                 :      * require it.
                               3074                 :      */
  366 rhaas                    3075 UBC           0 :     Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
  366 rhaas                    3076 UIC           0 :     MyProc->delayChkptFlags |= DELAY_CHKPT_START;
                               3077                 : 
                               3078                 :     /* WAL log truncation */
 2752 andres                   3079               0 :     WriteMTruncateXlogRec(newOldestMultiDB,
                               3080                 :                           oldestMulti, newOldestMulti,
                               3081                 :                           oldestOffset, newOldestOffset);
                               3082                 : 
                               3083                 :     /*
                               3084                 :      * Update in-memory limits before performing the truncation, while inside
                               3085                 :      * the critical section: Have to do it before truncation, to prevent
                               3086                 :      * concurrent lookups of those values. Has to be inside the critical
 2752 andres                   3087 EUB             :      * section as otherwise a future call to this function would error out,
                               3088                 :      * while looking up the oldest member in offsets, if our caller crashes
                               3089                 :      * before updating the limits.
 2891 rhaas                    3090                 :      */
 2752 andres                   3091 UIC           0 :     LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
                               3092               0 :     MultiXactState->oldestMultiXactId = newOldestMulti;
 2752 andres                   3093 UBC           0 :     MultiXactState->oldestMultiXactDB = newOldestMultiDB;
 2752 andres                   3094 UIC           0 :     LWLockRelease(MultiXactGenLock);
                               3095                 : 
 2752 andres                   3096 EUB             :     /* First truncate members */
 2752 andres                   3097 UIC           0 :     PerformMembersTruncation(oldestOffset, newOldestOffset);
 2752 andres                   3098 EUB             : 
                               3099                 :     /* Then offsets */
 2752 andres                   3100 UBC           0 :     PerformOffsetsTruncation(oldestMulti, newOldestMulti);
 2752 andres                   3101 EUB             : 
  366 rhaas                    3102 UIC           0 :     MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
                               3103                 : 
 2752 andres                   3104               0 :     END_CRIT_SECTION();
                               3105               0 :     LWLockRelease(MultiXactTruncationLock);
                               3106                 : }
                               3107                 : 
                               3108                 : /*
                               3109                 :  * Decide whether a MultiXactOffset page number is "older" for truncation
                               3110                 :  * purposes.  Analogous to CLOGPagePrecedes().
                               3111                 :  *
  813 noah                     3112 ECB             :  * Offsetting the values is optional, because MultiXactIdPrecedes() has
                               3113                 :  * translational symmetry.
                               3114                 :  */
                               3115                 : static bool
 6555 tgl                      3116 GIC       71214 : MultiXactOffsetPagePrecedes(int page1, int page2)
 6555 tgl                      3117 ECB             : {
                               3118                 :     MultiXactId multi1;
                               3119                 :     MultiXactId multi2;
                               3120                 : 
 6555 tgl                      3121 GIC       71214 :     multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
  813 noah                     3122 CBC       71214 :     multi1 += FirstMultiXactId + 1;
 6555 tgl                      3123           71214 :     multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
  813 noah                     3124 GIC       71214 :     multi2 += FirstMultiXactId + 1;
                               3125                 : 
                               3126          118690 :     return (MultiXactIdPrecedes(multi1, multi2) &&
                               3127           47476 :             MultiXactIdPrecedes(multi1,
                               3128                 :                                 multi2 + MULTIXACT_OFFSETS_PER_PAGE - 1));
                               3129                 : }
                               3130                 : 
                               3131                 : /*
  813 noah                     3132 EUB             :  * Decide whether a MultiXactMember page number is "older" for truncation
                               3133                 :  * purposes.  There is no "invalid offset number" so use the numbers verbatim.
                               3134                 :  */
                               3135                 : static bool
 6555 tgl                      3136 UIC           0 : MultiXactMemberPagePrecedes(int page1, int page2)
 6555 tgl                      3137 EUB             : {
 6385 bruce                    3138                 :     MultiXactOffset offset1;
                               3139                 :     MultiXactOffset offset2;
 6555 tgl                      3140                 : 
 6514 tgl                      3141 UBC           0 :     offset1 = ((MultiXactOffset) page1) * MULTIXACT_MEMBERS_PER_PAGE;
 6514 tgl                      3142 UIC           0 :     offset2 = ((MultiXactOffset) page2) * MULTIXACT_MEMBERS_PER_PAGE;
                               3143                 : 
  813 noah                     3144               0 :     return (MultiXactOffsetPrecedes(offset1, offset2) &&
                               3145               0 :             MultiXactOffsetPrecedes(offset1,
                               3146                 :                                     offset2 + MULTIXACT_MEMBERS_PER_PAGE - 1));
                               3147                 : }
                               3148                 : 
                               3149                 : /*
                               3150                 :  * Decide which of two MultiXactIds is earlier.
                               3151                 :  *
 6555 tgl                      3152 ECB             :  * XXX do we need to do something special for InvalidMultiXactId?
                               3153                 :  * (Doesn't look like it.)
                               3154                 :  */
                               3155                 : bool
 6555 tgl                      3156 CBC      714000 : MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
                               3157                 : {
 6385 bruce                    3158 GIC      714000 :     int32       diff = (int32) (multi1 - multi2);
                               3159                 : 
 6555 tgl                      3160          714000 :     return (diff < 0);
                               3161                 : }
                               3162                 : 
                               3163                 : /*
                               3164                 :  * MultiXactIdPrecedesOrEquals -- is multi1 logically <= multi2?
                               3165                 :  *
 3419 alvherre                 3166 ECB             :  * XXX do we need to do something special for InvalidMultiXactId?
                               3167                 :  * (Doesn't look like it.)
                               3168                 :  */
                               3169                 : bool
 3419 alvherre                 3170 CBC        3880 : MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
                               3171                 : {
 3419 alvherre                 3172 GIC        3880 :     int32       diff = (int32) (multi1 - multi2);
                               3173                 : 
                               3174            3880 :     return (diff <= 0);
                               3175                 : }
                               3176                 : 
                               3177                 : 
 6555 tgl                      3178 ECB             : /*
                               3179                 :  * Decide which of two offsets is earlier.
                               3180                 :  */
                               3181                 : static bool
 6514 tgl                      3182 CBC         143 : MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2)
                               3183                 : {
 6385 bruce                    3184 GIC         143 :     int32       diff = (int32) (offset1 - offset2);
                               3185                 : 
 6555 tgl                      3186             143 :     return (diff < 0);
                               3187                 : }
                               3188                 : 
                               3189                 : /*
 6514 tgl                      3190 ECB             :  * Write an xlog record reflecting the zeroing of either a MEMBERs or
                               3191                 :  * OFFSETs page (info shows which)
                               3192                 :  */
                               3193                 : static void
 6514 tgl                      3194 CBC          20 : WriteMZeroPageXlogRec(int pageno, uint8 info)
 6514 tgl                      3195 ECB             : {
 3062 heikki.linnakangas       3196 GIC          20 :     XLogBeginInsert();
                               3197              20 :     XLogRegisterData((char *) (&pageno), sizeof(int));
                               3198              20 :     (void) XLogInsert(RM_MULTIXACT_ID, info);
 6514 tgl                      3199              20 : }
                               3200                 : 
                               3201                 : /*
                               3202                 :  * Write a TRUNCATE xlog record
                               3203                 :  *
 2752 andres                   3204 EUB             :  * We must flush the xlog record to disk before returning --- see notes in
                               3205                 :  * TruncateCLOG().
                               3206                 :  */
                               3207                 : static void
 2752 andres                   3208 UIC           0 : WriteMTruncateXlogRec(Oid oldestMultiDB,
                               3209                 :                       MultiXactId startTruncOff, MultiXactId endTruncOff,
                               3210                 :                       MultiXactOffset startTruncMemb, MultiXactOffset endTruncMemb)
 2752 andres                   3211 EUB             : {
                               3212                 :     XLogRecPtr  recptr;
                               3213                 :     xl_multixact_truncate xlrec;
                               3214                 : 
 2752 andres                   3215 UIC           0 :     xlrec.oldestMultiDB = oldestMultiDB;
 2752 andres                   3216 EUB             : 
 2752 andres                   3217 UBC           0 :     xlrec.startTruncOff = startTruncOff;
 2752 andres                   3218 UIC           0 :     xlrec.endTruncOff = endTruncOff;
 2752 andres                   3219 EUB             : 
 2752 andres                   3220 UBC           0 :     xlrec.startTruncMemb = startTruncMemb;
                               3221               0 :     xlrec.endTruncMemb = endTruncMemb;
 2752 andres                   3222 EUB             : 
 2752 andres                   3223 UBC           0 :     XLogBeginInsert();
 2752 andres                   3224 UIC           0 :     XLogRegisterData((char *) (&xlrec), SizeOfMultiXactTruncate);
                               3225               0 :     recptr = XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_TRUNCATE_ID);
                               3226               0 :     XLogFlush(recptr);
                               3227               0 : }
                               3228                 : 
 6514 tgl                      3229 ECB             : /*
                               3230                 :  * MULTIXACT resource manager's routines
                               3231                 :  */
                               3232                 : void
 3062 heikki.linnakangas       3233 GIC           4 : multixact_redo(XLogReaderState *record)
 6514 tgl                      3234 ECB             : {
 3062 heikki.linnakangas       3235 GIC           4 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
 6514 tgl                      3236 ECB             : 
                               3237                 :     /* Backup blocks are not used in multixact records */
 3062 heikki.linnakangas       3238 GIC           4 :     Assert(!XLogRecHasAnyBlockRefs(record));
                               3239                 : 
 6514 tgl                      3240               4 :     if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
 6514 tgl                      3241 ECB             :     {
                               3242                 :         int         pageno;
                               3243                 :         int         slotno;
                               3244                 : 
 6514 tgl                      3245 CBC           1 :         memcpy(&pageno, XLogRecGetData(record), sizeof(int));
 6514 tgl                      3246 ECB             : 
 1059 tgl                      3247 CBC           1 :         LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
                               3248                 : 
 6514                          3249               1 :         slotno = ZeroMultiXactOffsetPage(pageno, false);
 4483 alvherre                 3250 GIC           1 :         SimpleLruWritePage(MultiXactOffsetCtl, slotno);
 6364 tgl                      3251 CBC           1 :         Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
                               3252                 : 
 1059 tgl                      3253 GIC           1 :         LWLockRelease(MultiXactOffsetSLRULock);
                               3254                 :     }
 6514                          3255               3 :     else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
 6514 tgl                      3256 ECB             :     {
                               3257                 :         int         pageno;
                               3258                 :         int         slotno;
                               3259                 : 
 6514 tgl                      3260 CBC           1 :         memcpy(&pageno, XLogRecGetData(record), sizeof(int));
 6514 tgl                      3261 ECB             : 
 1059 tgl                      3262 CBC           1 :         LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
                               3263                 : 
 6514                          3264               1 :         slotno = ZeroMultiXactMemberPage(pageno, false);
 4483 alvherre                 3265 GIC           1 :         SimpleLruWritePage(MultiXactMemberCtl, slotno);
 6364 tgl                      3266 CBC           1 :         Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
                               3267                 : 
 1059                          3268               1 :         LWLockRelease(MultiXactMemberSLRULock);
 6514 tgl                      3269 ECB             :     }
 6514 tgl                      3270 GIC           2 :     else if (info == XLOG_MULTIXACT_CREATE_ID)
                               3271                 :     {
 3728 alvherre                 3272               2 :         xl_multixact_create *xlrec =
 3602 bruce                    3273               2 :         (xl_multixact_create *) XLogRecGetData(record);
 6514 tgl                      3274 ECB             :         TransactionId max_xid;
                               3275                 :         int         i;
                               3276                 : 
                               3277                 :         /* Store the data back into the SLRU files */
 3728 alvherre                 3278 CBC           2 :         RecordNewMultiXact(xlrec->mid, xlrec->moff, xlrec->nmembers,
                               3279               2 :                            xlrec->members);
                               3280                 : 
                               3281                 :         /* Make sure nextMXact/nextOffset are beyond what this record has */
 3728 alvherre                 3282 GIC           2 :         MultiXactAdvanceNextMXact(xlrec->mid + 1,
                               3283               2 :                                   xlrec->moff + xlrec->nmembers);
                               3284                 : 
                               3285                 :         /*
  697 tgl                      3286 ECB             :          * Make sure nextXid is beyond any XID mentioned in the record. This
                               3287                 :          * should be unnecessary, since any XID found here ought to have other
                               3288                 :          * evidence in the XLOG, but let's be safe.
 6514                          3289                 :          */
 3062 heikki.linnakangas       3290 GBC           2 :         max_xid = XLogRecGetXid(record);
 3728 alvherre                 3291 GIC           6 :         for (i = 0; i < xlrec->nmembers; i++)
                               3292                 :         {
 3728 alvherre                 3293 CBC           4 :             if (TransactionIdPrecedes(max_xid, xlrec->members[i].xid))
 3728 alvherre                 3294 UIC           0 :                 max_xid = xlrec->members[i].xid;
 6514 tgl                      3295 EUB             :         }
                               3296                 : 
 1473 tmunro                   3297 GIC           2 :         AdvanceNextFullTransactionIdPastXid(max_xid);
                               3298                 :     }
 2752 andres                   3299 UIC           0 :     else if (info == XLOG_MULTIXACT_TRUNCATE_ID)
 2752 andres                   3300 EUB             :     {
                               3301                 :         xl_multixact_truncate xlrec;
                               3302                 :         int         pageno;
                               3303                 : 
 2752 andres                   3304 UIC           0 :         memcpy(&xlrec, XLogRecGetData(record),
                               3305                 :                SizeOfMultiXactTruncate);
                               3306                 : 
                               3307               0 :         elog(DEBUG1, "replaying multixact truncation: "
                               3308                 :              "offsets [%u, %u), offsets segments [%x, %x), "
                               3309                 :              "members [%u, %u), members segments [%x, %x)",
                               3310                 :              xlrec.startTruncOff, xlrec.endTruncOff,
                               3311                 :              MultiXactIdToOffsetSegment(xlrec.startTruncOff),
                               3312                 :              MultiXactIdToOffsetSegment(xlrec.endTruncOff),
                               3313                 :              xlrec.startTruncMemb, xlrec.endTruncMemb,
 2752 andres                   3314 EUB             :              MXOffsetToMemberSegment(xlrec.startTruncMemb),
                               3315                 :              MXOffsetToMemberSegment(xlrec.endTruncMemb));
                               3316                 : 
                               3317                 :         /* should not be required, but more than cheap enough */
 2752 andres                   3318 UIC           0 :         LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
                               3319                 : 
 2752 andres                   3320 EUB             :         /*
                               3321                 :          * Advance the horizon values, so they're current at the end of
                               3322                 :          * recovery.
                               3323                 :          */
 2217 tgl                      3324 UIC           0 :         SetMultiXactIdLimit(xlrec.endTruncOff, xlrec.oldestMultiDB, false);
                               3325                 : 
 2752 andres                   3326               0 :         PerformMembersTruncation(xlrec.startTruncMemb, xlrec.endTruncMemb);
                               3327                 : 
                               3328                 :         /*
 2752 andres                   3329 EUB             :          * During XLOG replay, latest_page_number isn't necessarily set up
                               3330                 :          * yet; insert a suitable value to bypass the sanity test in
                               3331                 :          * SimpleLruTruncate.
                               3332                 :          */
 2752 andres                   3333 UBC           0 :         pageno = MultiXactIdToOffsetPage(xlrec.endTruncOff);
 2752 andres                   3334 UIC           0 :         MultiXactOffsetCtl->shared->latest_page_number = pageno;
                               3335               0 :         PerformOffsetsTruncation(xlrec.startTruncOff, xlrec.endTruncOff);
 2752 andres                   3336 EUB             : 
 2752 andres                   3337 LBC           0 :         LWLockRelease(MultiXactTruncationLock);
                               3338                 :     }
                               3339                 :     else
 6514 tgl                      3340 UBC           0 :         elog(PANIC, "multixact_redo: unknown op code %u", info);
 6514 tgl                      3341 GIC           4 : }
                               3342                 : 
                               3343                 : Datum
 3728 alvherre                 3344 UIC           0 : pg_get_multixact_members(PG_FUNCTION_ARGS)
                               3345                 : {
                               3346                 :     typedef struct
                               3347                 :     {
 3602 bruce                    3348 EUB             :         MultiXactMember *members;
                               3349                 :         int         nmembers;
                               3350                 :         int         iter;
                               3351                 :     } mxact;
  888 peter                    3352 UBC           0 :     MultiXactId mxid = PG_GETARG_TRANSACTIONID(0);
 3602 bruce                    3353 EUB             :     mxact      *multi;
                               3354                 :     FuncCallContext *funccxt;
                               3355                 : 
 3728 alvherre                 3356 UIC           0 :     if (mxid < FirstMultiXactId)
 3728 alvherre                 3357 UBC           0 :         ereport(ERROR,
                               3358                 :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               3359                 :                  errmsg("invalid MultiXactId: %u", mxid)));
                               3360                 : 
 3728 alvherre                 3361 UIC           0 :     if (SRF_IS_FIRSTCALL())
 3728 alvherre                 3362 EUB             :     {
                               3363                 :         MemoryContext oldcxt;
                               3364                 :         TupleDesc   tupdesc;
                               3365                 : 
 3728 alvherre                 3366 UIC           0 :         funccxt = SRF_FIRSTCALL_INIT();
 3728 alvherre                 3367 UBC           0 :         oldcxt = MemoryContextSwitchTo(funccxt->multi_call_memory_ctx);
                               3368                 : 
                               3369               0 :         multi = palloc(sizeof(mxact));
                               3370                 :         /* no need to allow for old values here */
 3176                          3371               0 :         multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false,
 3176 alvherre                 3372 EUB             :                                                 false);
 3728 alvherre                 3373 UBC           0 :         multi->iter = 0;
 3728 alvherre                 3374 EUB             : 
  109 michael                  3375 UNC           0 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
                               3376               0 :             elog(ERROR, "return type must be a row type");
                               3377               0 :         funccxt->tuple_desc = tupdesc;
 3728 alvherre                 3378 UBC           0 :         funccxt->attinmeta = TupleDescGetAttInMetadata(tupdesc);
 3728 alvherre                 3379 UIC           0 :         funccxt->user_fctx = multi;
 3728 alvherre                 3380 EUB             : 
 3728 alvherre                 3381 UIC           0 :         MemoryContextSwitchTo(oldcxt);
                               3382                 :     }
                               3383                 : 
                               3384               0 :     funccxt = SRF_PERCALL_SETUP();
 3728 alvherre                 3385 UBC           0 :     multi = (mxact *) funccxt->user_fctx;
 3728 alvherre                 3386 EUB             : 
 3728 alvherre                 3387 UIC           0 :     while (multi->iter < multi->nmembers)
 3728 alvherre                 3388 EUB             :     {
                               3389                 :         HeapTuple   tuple;
                               3390                 :         char       *values[2];
                               3391                 : 
 3380 peter_e                  3392 UBC           0 :         values[0] = psprintf("%u", multi->members[multi->iter].xid);
 3728 alvherre                 3393 UIC           0 :         values[1] = mxstatus_to_string(multi->members[multi->iter].status);
                               3394                 : 
 3728 alvherre                 3395 UBC           0 :         tuple = BuildTupleFromCStrings(funccxt->attinmeta, values);
                               3396                 : 
 3728 alvherre                 3397 UIC           0 :         multi->iter++;
                               3398               0 :         pfree(values[0]);
                               3399               0 :         SRF_RETURN_NEXT(funccxt, HeapTupleGetDatum(tuple));
                               3400                 :     }
                               3401                 : 
 3728 alvherre                 3402 UBC           0 :     SRF_RETURN_DONE(funccxt);
                               3403                 : }
  926 tmunro                   3404 EUB             : 
                               3405                 : /*
                               3406                 :  * Entrypoint for sync.c to sync offsets files.
                               3407                 :  */
                               3408                 : int
  926 tmunro                   3409 UIC           0 : multixactoffsetssyncfiletag(const FileTag *ftag, char *path)
                               3410                 : {
  926 tmunro                   3411 UBC           0 :     return SlruSyncFileTag(MultiXactOffsetCtl, ftag, path);
                               3412                 : }
  926 tmunro                   3413 EUB             : 
                               3414                 : /*
                               3415                 :  * Entrypoint for sync.c to sync members files.
                               3416                 :  */
                               3417                 : int
  926 tmunro                   3418 UIC           0 : multixactmemberssyncfiletag(const FileTag *ftag, char *path)
                               3419                 : {
                               3420               0 :     return SlruSyncFileTag(MultiXactMemberCtl, ftag, path);
                               3421                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a