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