| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* heapam.c |
| 4 |
* heap access method code |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/access/heap/heapam.c |
| 12 |
* |
| 13 |
* |
| 14 |
* INTERFACE ROUTINES |
| 15 |
* heap_beginscan - begin relation scan |
| 16 |
* heap_rescan - restart a relation scan |
| 17 |
* heap_endscan - end relation scan |
| 18 |
* heap_getnext - retrieve next tuple in scan |
| 19 |
* heap_fetch - retrieve tuple with given tid |
| 20 |
* heap_insert - insert tuple into a relation |
| 21 |
* heap_multi_insert - insert multiple tuples into a relation |
| 22 |
* heap_delete - delete a tuple from a relation |
| 23 |
* heap_update - replace a tuple in a relation with another tuple |
| 24 |
* |
| 25 |
* NOTES |
| 26 |
* This file contains the heap_ routines which implement |
| 27 |
* the POSTGRES heap access method used for all POSTGRES |
| 28 |
* relations. |
| 29 |
* |
| 30 |
*------------------------------------------------------------------------- |
| 31 |
*/ |
| 32 |
#include "postgres.h" |
| 33 |
|
| 34 |
#include "access/heapam.h" |
| 35 |
#include "access/heaptoast.h" |
| 36 |
#include "access/hio.h" |
| 37 |
#include "access/multixact.h" |
| 38 |
#include "access/subtrans.h" |
| 39 |
#include "access/syncscan.h" |
| 40 |
#include "access/valid.h" |
| 41 |
#include "access/visibilitymap.h" |
| 42 |
#include "access/xloginsert.h" |
| 43 |
#include "catalog/pg_database.h" |
| 44 |
#include "catalog/pg_database_d.h" |
| 45 |
#include "commands/vacuum.h" |
| 46 |
#include "pgstat.h" |
| 47 |
#include "port/pg_bitutils.h" |
| 48 |
#include "storage/lmgr.h" |
| 49 |
#include "storage/predicate.h" |
| 50 |
#include "storage/procarray.h" |
| 51 |
#include "utils/datum.h" |
| 52 |
#include "utils/injection_point.h" |
| 53 |
#include "utils/inval.h" |
| 54 |
#include "utils/spccache.h" |
| 55 |
#include "utils/syscache.h" |
| 56 |
|
| 57 |
|
| 58 |
static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup, |
| 59 |
TransactionId xid, CommandId cid, int options); |
| 60 |
static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf, |
| 61 |
Buffer vmbuffer_old, Buffer newbuf, |
| 62 |
Buffer vmbuffer_new, HeapTuple oldtup, |
| 63 |
HeapTuple newtup, HeapTuple old_key_tuple, |
| 64 |
bool all_visible_cleared, bool new_all_visible_cleared); |
| 65 |
#ifdef USE_ASSERT_CHECKING |
| 66 |
static void check_lock_if_inplace_updateable_rel(Relation relation, |
| 67 |
ItemPointer otid, |
| 68 |
HeapTuple newtup); |
| 69 |
static void check_inplace_rel_lock(HeapTuple oldtup); |
| 70 |
#endif |
| 71 |
static Bitmapset *HeapDetermineColumnsInfo(Relation relation, |
| 72 |
Bitmapset *interesting_cols, |
| 73 |
Bitmapset *external_cols, |
| 74 |
HeapTuple oldtup, HeapTuple newtup, |
| 75 |
bool *has_external); |
| 76 |
static bool heap_acquire_tuplock(Relation relation, ItemPointer tid, |
| 77 |
LockTupleMode mode, LockWaitPolicy wait_policy, |
| 78 |
bool *have_tuple_lock); |
| 79 |
static inline BlockNumber heapgettup_advance_block(HeapScanDesc scan, |
| 80 |
BlockNumber block, |
| 81 |
ScanDirection dir); |
| 82 |
static pg_noinline BlockNumber heapgettup_initial_block(HeapScanDesc scan, |
| 83 |
ScanDirection dir); |
| 84 |
static void compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask, |
| 85 |
uint16 old_infomask2, TransactionId add_to_xmax, |
| 86 |
LockTupleMode mode, bool is_update, |
| 87 |
TransactionId *result_xmax, uint16 *result_infomask, |
| 88 |
uint16 *result_infomask2); |
| 89 |
static TM_Result heap_lock_updated_tuple(Relation rel, |
| 90 |
uint16 prior_infomask, |
| 91 |
TransactionId prior_rawxmax, |
| 92 |
const ItemPointerData *prior_ctid, |
| 93 |
TransactionId xid, |
| 94 |
LockTupleMode mode); |
| 95 |
static void GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask, |
| 96 |
uint16 *new_infomask2); |
| 97 |
static TransactionId MultiXactIdGetUpdateXid(TransactionId xmax, |
| 98 |
uint16 t_infomask); |
| 99 |
static bool DoesMultiXactIdConflict(MultiXactId multi, uint16 infomask, |
| 100 |
LockTupleMode lockmode, bool *current_is_member); |
| 101 |
static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, |
| 102 |
Relation rel, ItemPointer ctid, XLTW_Oper oper, |
| 103 |
int *remaining); |
| 104 |
static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status, |
| 105 |
uint16 infomask, Relation rel, int *remaining, |
| 106 |
bool logLockFailure); |
| 107 |
static void index_delete_sort(TM_IndexDeleteOp *delstate); |
| 108 |
static int bottomup_sort_and_shrink(TM_IndexDeleteOp *delstate); |
| 109 |
static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup); |
| 110 |
static HeapTuple ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required, |
| 111 |
bool *copy); |
| 112 |
|
| 113 |
|
| 114 |
/* |
| 115 |
* Each tuple lock mode has a corresponding heavyweight lock, and one or two |
| 116 |
* corresponding MultiXactStatuses (one to merely lock tuples, another one to |
| 117 |
* update them). This table (and the macros below) helps us determine the |
| 118 |
* heavyweight lock mode and MultiXactStatus values to use for any particular |
| 119 |
* tuple lock strength. |
| 120 |
* |
| 121 |
* These interact with InplaceUpdateTupleLock, an alias for ExclusiveLock. |
| 122 |
* |
| 123 |
* Don't look at lockstatus/updstatus directly! Use get_mxact_status_for_lock |
| 124 |
* instead. |
| 125 |
*/ |
| 126 |
static const struct |
| 127 |
{ |
| 128 |
LOCKMODE hwlock; |
| 129 |
int lockstatus; |
| 130 |
int updstatus; |
| 131 |
} |
| 132 |
|
| 133 |
tupleLockExtraInfo[MaxLockTupleMode + 1] = |
| 134 |
{ |
| 135 |
{ /* LockTupleKeyShare */ |
| 136 |
AccessShareLock, |
| 137 |
MultiXactStatusForKeyShare, |
| 138 |
-1 /* KeyShare does not allow updating tuples */ |
| 139 |
}, |
| 140 |
{ /* LockTupleShare */ |
| 141 |
RowShareLock, |
| 142 |
MultiXactStatusForShare, |
| 143 |
-1 /* Share does not allow updating tuples */ |
| 144 |
}, |
| 145 |
{ /* LockTupleNoKeyExclusive */ |
| 146 |
ExclusiveLock, |
| 147 |
MultiXactStatusForNoKeyUpdate, |
| 148 |
MultiXactStatusNoKeyUpdate |
| 149 |
}, |
| 150 |
{ /* LockTupleExclusive */ |
| 151 |
AccessExclusiveLock, |
| 152 |
MultiXactStatusForUpdate, |
| 153 |
MultiXactStatusUpdate |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
/* Get the LOCKMODE for a given MultiXactStatus */ |
| 158 |
#define LOCKMODE_from_mxstatus(status) \ |
| 159 |
(tupleLockExtraInfo[TUPLOCK_from_mxstatus((status))].hwlock) |
| 160 |
|
| 161 |
/* |
| 162 |
* Acquire heavyweight locks on tuples, using a LockTupleMode strength value. |
| 163 |
* This is more readable than having every caller translate it to lock.h's |
| 164 |
* LOCKMODE. |
| 165 |
*/ |
| 166 |
#define LockTupleTuplock(rel, tup, mode) \ |
| 167 |
LockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock) |
| 168 |
#define UnlockTupleTuplock(rel, tup, mode) \ |
| 169 |
UnlockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock) |
| 170 |
#define ConditionalLockTupleTuplock(rel, tup, mode, log) \ |
| 171 |
ConditionalLockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock, (log)) |
| 172 |
|
| 173 |
#ifdef USE_PREFETCH |
| 174 |
/* |
| 175 |
* heap_index_delete_tuples and index_delete_prefetch_buffer use this |
| 176 |
* structure to coordinate prefetching activity |
| 177 |
*/ |
| 178 |
typedef struct |
| 179 |
{ |
| 180 |
BlockNumber cur_hblkno; |
| 181 |
int next_item; |
| 182 |
int ndeltids; |
| 183 |
TM_IndexDelete *deltids; |
| 184 |
} IndexDeletePrefetchState; |
| 185 |
#endif |
| 186 |
|
| 187 |
/* heap_index_delete_tuples bottom-up index deletion costing constants */ |
| 188 |
#define BOTTOMUP_MAX_NBLOCKS 6 |
| 189 |
#define BOTTOMUP_TOLERANCE_NBLOCKS 3 |
| 190 |
|
| 191 |
/* |
| 192 |
* heap_index_delete_tuples uses this when determining which heap blocks it |
| 193 |
* must visit to help its bottom-up index deletion caller |
| 194 |
*/ |
| 195 |
typedef struct IndexDeleteCounts |
| 196 |
{ |
| 197 |
int16 npromisingtids; /* Number of "promising" TIDs in group */ |
| 198 |
int16 ntids; /* Number of TIDs in group */ |
| 199 |
int16 ifirsttid; /* Offset to group's first deltid */ |
| 200 |
} IndexDeleteCounts; |
| 201 |
|
| 202 |
/* |
| 203 |
* This table maps tuple lock strength values for each particular |
| 204 |
* MultiXactStatus value. |
| 205 |
*/ |
| 206 |
static const int MultiXactStatusLock[MaxMultiXactStatus + 1] = |
| 207 |
{ |
| 208 |
LockTupleKeyShare, /* ForKeyShare */ |
| 209 |
LockTupleShare, /* ForShare */ |
| 210 |
LockTupleNoKeyExclusive, /* ForNoKeyUpdate */ |
| 211 |
LockTupleExclusive, /* ForUpdate */ |
| 212 |
LockTupleNoKeyExclusive, /* NoKeyUpdate */ |
| 213 |
LockTupleExclusive /* Update */ |
| 214 |
}; |
| 215 |
|
| 216 |
/* Get the LockTupleMode for a given MultiXactStatus */ |
| 217 |
#define TUPLOCK_from_mxstatus(status) \ |
| 218 |
(MultiXactStatusLock[(status)]) |
| 219 |
|
| 220 |
/* |
| 221 |
* Check that we have a valid snapshot if we might need TOAST access. |
| 222 |
*/ |
| 223 |
static inline void |
| 224 |
AssertHasSnapshotForToast(Relation rel) |
| 225 |
{ |
| 226 |
#ifdef USE_ASSERT_CHECKING |
| 227 |
|
| 228 |
/* bootstrap mode in particular breaks this rule */ |
| 229 |
if (!IsNormalProcessingMode()) |
| 230 |
return; |
| 231 |
|
| 232 |
/* if the relation doesn't have a TOAST table, we are good */ |
| 233 |
if (!OidIsValid(rel->rd_rel->reltoastrelid)) |
| 234 |
return; |
| 235 |
|
| 236 |
Assert(HaveRegisteredOrActiveSnapshot()); |
| 237 |
|
| 238 |
#endif /* USE_ASSERT_CHECKING */ |
| 239 |
} |
| 240 |
|
| 241 |
/* ---------------------------------------------------------------- |
| 242 |
* heap support routines |
| 243 |
* ---------------------------------------------------------------- |
| 244 |
*/ |
| 245 |
|
| 246 |
/* |
| 247 |
* Streaming read API callback for parallel sequential scans. Returns the next |
| 248 |
* block the caller wants from the read stream or InvalidBlockNumber when done. |
| 249 |
*/ |
| 250 |
static BlockNumber |
| 251 |
heap_scan_stream_read_next_parallel(ReadStream *stream, |
| 252 |
void *callback_private_data, |
| 253 |
void *per_buffer_data) |
| 254 |
{ |
| 255 |
HeapScanDesc scan = (HeapScanDesc) callback_private_data; |
| 256 |
|
| 257 |
Assert(ScanDirectionIsForward(scan->rs_dir)); |
| 258 |
Assert(scan->rs_base.rs_parallel); |
| 259 |
|
| 260 |
if (unlikely(!scan->rs_inited)) |
| 261 |
{ |
| 262 |
/* parallel scan */ |
| 263 |
table_block_parallelscan_startblock_init(scan->rs_base.rs_rd, |
| 264 |
scan->rs_parallelworkerdata, |
| 265 |
(ParallelBlockTableScanDesc) scan->rs_base.rs_parallel); |
| 266 |
|
| 267 |
/* may return InvalidBlockNumber if there are no more blocks */ |
| 268 |
scan->rs_prefetch_block = table_block_parallelscan_nextpage(scan->rs_base.rs_rd, |
| 269 |
scan->rs_parallelworkerdata, |
| 270 |
(ParallelBlockTableScanDesc) scan->rs_base.rs_parallel); |
| 271 |
scan->rs_inited = true; |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
scan->rs_prefetch_block = table_block_parallelscan_nextpage(scan->rs_base.rs_rd, |
| 276 |
scan->rs_parallelworkerdata, (ParallelBlockTableScanDesc) |
| 277 |
scan->rs_base.rs_parallel); |
| 278 |
} |
| 279 |
|
| 280 |
return scan->rs_prefetch_block; |
| 281 |
} |
| 282 |
|
| 283 |
/* |
| 284 |
* Streaming read API callback for serial sequential and TID range scans. |
| 285 |
* Returns the next block the caller wants from the read stream or |
| 286 |
* InvalidBlockNumber when done. |
| 287 |
*/ |
| 288 |
static BlockNumber |
| 289 |
heap_scan_stream_read_next_serial(ReadStream *stream, |
| 290 |
void *callback_private_data, |
| 291 |
void *per_buffer_data) |
| 292 |
{ |
| 293 |
HeapScanDesc scan = (HeapScanDesc) callback_private_data; |
| 294 |
|
| 295 |
if (unlikely(!scan->rs_inited)) |
| 296 |
{ |
| 297 |
scan->rs_prefetch_block = heapgettup_initial_block(scan, scan->rs_dir); |
| 298 |
scan->rs_inited = true; |
| 299 |
} |
| 300 |
else |
| 301 |
scan->rs_prefetch_block = heapgettup_advance_block(scan, |
| 302 |
scan->rs_prefetch_block, |
| 303 |
scan->rs_dir); |
| 304 |
|
| 305 |
return scan->rs_prefetch_block; |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
* Read stream API callback for bitmap heap scans. |
| 310 |
* Returns the next block the caller wants from the read stream or |
| 311 |
* InvalidBlockNumber when done. |
| 312 |
*/ |
| 313 |
static BlockNumber |
| 314 |
bitmapheap_stream_read_next(ReadStream *pgsr, void *private_data, |
| 315 |
void *per_buffer_data) |
| 316 |
{ |
| 317 |
TBMIterateResult *tbmres = per_buffer_data; |
| 318 |
BitmapHeapScanDesc bscan = (BitmapHeapScanDesc) private_data; |
| 319 |
HeapScanDesc hscan = (HeapScanDesc) bscan; |
| 320 |
TableScanDesc sscan = &hscan->rs_base; |
| 321 |
|
| 322 |
for (;;) |
| 323 |
{ |
| 324 |
CHECK_FOR_INTERRUPTS(); |
| 325 |
|
| 326 |
/* no more entries in the bitmap */ |
| 327 |
if (!tbm_iterate(&sscan->st.rs_tbmiterator, tbmres)) |
| 328 |
return InvalidBlockNumber; |
| 329 |
|
| 330 |
/* |
| 331 |
* Ignore any claimed entries past what we think is the end of the |
| 332 |
* relation. It may have been extended after the start of our scan (we |
| 333 |
* only hold an AccessShareLock, and it could be inserts from this |
| 334 |
* backend). We don't take this optimization in SERIALIZABLE |
| 335 |
* isolation though, as we need to examine all invisible tuples |
| 336 |
* reachable by the index. |
| 337 |
*/ |
| 338 |
if (!IsolationIsSerializable() && |
| 339 |
tbmres->blockno >= hscan->rs_nblocks) |
| 340 |
continue; |
| 341 |
|
| 342 |
return tbmres->blockno; |
| 343 |
} |
| 344 |
|
| 345 |
/* not reachable */ |
| 346 |
Assert(false); |
| 347 |
} |
| 348 |
|
| 349 |
/* ---------------- |
| 350 |
* initscan - scan code common to heap_beginscan and heap_rescan |
| 351 |
* ---------------- |
| 352 |
*/ |
| 353 |
static void |
| 354 |
initscan(HeapScanDesc scan, ScanKey key, bool keep_startblock) |
| 355 |
{ |
| 356 |
ParallelBlockTableScanDesc bpscan = NULL; |
| 357 |
bool allow_strat; |
| 358 |
bool allow_sync; |
| 359 |
|
| 360 |
/* |
| 361 |
* Determine the number of blocks we have to scan. |
| 362 |
* |
| 363 |
* It is sufficient to do this once at scan start, since any tuples added |
| 364 |
* while the scan is in progress will be invisible to my snapshot anyway. |
| 365 |
* (That is not true when using a non-MVCC snapshot. However, we couldn't |
| 366 |
* guarantee to return tuples added after scan start anyway, since they |
| 367 |
* might go into pages we already scanned. To guarantee consistent |
| 368 |
* results for a non-MVCC snapshot, the caller must hold some higher-level |
| 369 |
* lock that ensures the interesting tuple(s) won't change.) |
| 370 |
*/ |
| 371 |
if (scan->rs_base.rs_parallel != NULL) |
| 372 |
{ |
| 373 |
bpscan = (ParallelBlockTableScanDesc) scan->rs_base.rs_parallel; |
| 374 |
scan->rs_nblocks = bpscan->phs_nblocks; |
| 375 |
} |
| 376 |
else |
| 377 |
scan->rs_nblocks = RelationGetNumberOfBlocks(scan->rs_base.rs_rd); |
| 378 |
|
| 379 |
/* |
| 380 |
* If the table is large relative to NBuffers, use a bulk-read access |
| 381 |
* strategy and enable synchronized scanning (see syncscan.c). Although |
| 382 |
* the thresholds for these features could be different, we make them the |
| 383 |
* same so that there are only two behaviors to tune rather than four. |
| 384 |
* (However, some callers need to be able to disable one or both of these |
| 385 |
* behaviors, independently of the size of the table; also there is a GUC |
| 386 |
* variable that can disable synchronized scanning.) |
| 387 |
* |
| 388 |
* Note that table_block_parallelscan_initialize has a very similar test; |
| 389 |
* if you change this, consider changing that one, too. |
| 390 |
*/ |
| 391 |
if (!RelationUsesLocalBuffers(scan->rs_base.rs_rd) && |
| 392 |
scan->rs_nblocks > NBuffers / 4) |
| 393 |
{ |
| 394 |
allow_strat = (scan->rs_base.rs_flags & SO_ALLOW_STRAT) != 0; |
| 395 |
allow_sync = (scan->rs_base.rs_flags & SO_ALLOW_SYNC) != 0; |
| 396 |
} |
| 397 |
else |
| 398 |
allow_strat = allow_sync = false; |
| 399 |
|
| 400 |
if (allow_strat) |
| 401 |
{ |
| 402 |
/* During a rescan, keep the previous strategy object. */ |
| 403 |
if (scan->rs_strategy == NULL) |
| 404 |
scan->rs_strategy = GetAccessStrategy(BAS_BULKREAD); |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
if (scan->rs_strategy != NULL) |
| 409 |
FreeAccessStrategy(scan->rs_strategy); |
| 410 |
scan->rs_strategy = NULL; |
| 411 |
} |
| 412 |
|
| 413 |
if (scan->rs_base.rs_parallel != NULL) |
| 414 |
{ |
| 415 |
/* For parallel scan, believe whatever ParallelTableScanDesc says. */ |
| 416 |
if (scan->rs_base.rs_parallel->phs_syncscan) |
| 417 |
scan->rs_base.rs_flags |= SO_ALLOW_SYNC; |
| 418 |
else |
| 419 |
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC; |
| 420 |
} |
| 421 |
else if (keep_startblock) |
| 422 |
{ |
| 423 |
/* |
| 424 |
* When rescanning, we want to keep the previous startblock setting, |
| 425 |
* so that rewinding a cursor doesn't generate surprising results. |
| 426 |
* Reset the active syncscan setting, though. |
| 427 |
*/ |
| 428 |
if (allow_sync && synchronize_seqscans) |
| 429 |
scan->rs_base.rs_flags |= SO_ALLOW_SYNC; |
| 430 |
else |
| 431 |
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC; |
| 432 |
} |
| 433 |
else if (allow_sync && synchronize_seqscans) |
| 434 |
{ |
| 435 |
scan->rs_base.rs_flags |= SO_ALLOW_SYNC; |
| 436 |
scan->rs_startblock = ss_get_location(scan->rs_base.rs_rd, scan->rs_nblocks); |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC; |
| 441 |
scan->rs_startblock = 0; |
| 442 |
} |
| 443 |
|
| 444 |
scan->rs_numblocks = InvalidBlockNumber; |
| 445 |
scan->rs_inited = false; |
| 446 |
scan->rs_ctup.t_data = NULL; |
| 447 |
ItemPointerSetInvalid(&scan->rs_ctup.t_self); |
| 448 |
scan->rs_cbuf = InvalidBuffer; |
| 449 |
scan->rs_cblock = InvalidBlockNumber; |
| 450 |
scan->rs_ntuples = 0; |
| 451 |
scan->rs_cindex = 0; |
| 452 |
|
| 453 |
/* |
| 454 |
* Initialize to ForwardScanDirection because it is most common and |
| 455 |
* because heap scans go forward before going backward (e.g. CURSORs). |
| 456 |
*/ |
| 457 |
scan->rs_dir = ForwardScanDirection; |
| 458 |
scan->rs_prefetch_block = InvalidBlockNumber; |
| 459 |
|
| 460 |
/* page-at-a-time fields are always invalid when not rs_inited */ |
| 461 |
|
| 462 |
/* |
| 463 |
* copy the scan key, if appropriate |
| 464 |
*/ |
| 465 |
if (key != NULL && scan->rs_base.rs_nkeys > 0) |
| 466 |
memcpy(scan->rs_base.rs_key, key, scan->rs_base.rs_nkeys * sizeof(ScanKeyData)); |
| 467 |
|
| 468 |
/* |
| 469 |
* Currently, we only have a stats counter for sequential heap scans (but |
| 470 |
* e.g for bitmap scans the underlying bitmap index scans will be counted, |
| 471 |
* and for sample scans we update stats for tuple fetches). |
| 472 |
*/ |
| 473 |
if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN) |
| 474 |
pgstat_count_heap_scan(scan->rs_base.rs_rd); |
| 475 |
} |
| 476 |
|
| 477 |
/* |
| 478 |
* heap_setscanlimits - restrict range of a heapscan |
| 479 |
* |
| 480 |
* startBlk is the page to start at |
| 481 |
* numBlks is number of pages to scan (InvalidBlockNumber means "all") |
| 482 |
*/ |
| 483 |
void |
| 484 |
heap_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks) |
| 485 |
{ |
| 486 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 487 |
|
| 488 |
Assert(!scan->rs_inited); /* else too late to change */ |
| 489 |
/* else rs_startblock is significant */ |
| 490 |
Assert(!(scan->rs_base.rs_flags & SO_ALLOW_SYNC)); |
| 491 |
|
| 492 |
/* Check startBlk is valid (but allow case of zero blocks...) */ |
| 493 |
Assert(startBlk == 0 || startBlk < scan->rs_nblocks); |
| 494 |
|
| 495 |
scan->rs_startblock = startBlk; |
| 496 |
scan->rs_numblocks = numBlks; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Per-tuple loop for heap_prepare_pagescan(). Pulled out so it can be called |
| 501 |
* multiple times, with constant arguments for all_visible, |
| 502 |
* check_serializable. |
| 503 |
*/ |
| 504 |
pg_always_inline |
| 505 |
static int |
| 506 |
page_collect_tuples(HeapScanDesc scan, Snapshot snapshot, |
| 507 |
Page page, Buffer buffer, |
| 508 |
BlockNumber block, int lines, |
| 509 |
bool all_visible, bool check_serializable) |
| 510 |
{ |
| 511 |
int ntup = 0; |
| 512 |
OffsetNumber lineoff; |
| 513 |
|
| 514 |
for (lineoff = FirstOffsetNumber; lineoff <= lines; lineoff++) |
| 515 |
{ |
| 516 |
ItemId lpp = PageGetItemId(page, lineoff); |
| 517 |
HeapTupleData loctup; |
| 518 |
bool valid; |
| 519 |
|
| 520 |
if (!ItemIdIsNormal(lpp)) |
| 521 |
continue; |
| 522 |
|
| 523 |
loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp); |
| 524 |
loctup.t_len = ItemIdGetLength(lpp); |
| 525 |
loctup.t_tableOid = RelationGetRelid(scan->rs_base.rs_rd); |
| 526 |
ItemPointerSet(&(loctup.t_self), block, lineoff); |
| 527 |
|
| 528 |
if (all_visible) |
| 529 |
valid = true; |
| 530 |
else |
| 531 |
valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer); |
| 532 |
|
| 533 |
if (check_serializable) |
| 534 |
HeapCheckForSerializableConflictOut(valid, scan->rs_base.rs_rd, |
| 535 |
&loctup, buffer, snapshot); |
| 536 |
|
| 537 |
if (valid) |
| 538 |
{ |
| 539 |
scan->rs_vistuples[ntup] = lineoff; |
| 540 |
ntup++; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
Assert(ntup <= MaxHeapTuplesPerPage); |
| 545 |
|
| 546 |
return ntup; |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
* heap_prepare_pagescan - Prepare current scan page to be scanned in pagemode |
| 551 |
* |
| 552 |
* Preparation currently consists of 1. prune the scan's rs_cbuf page, and 2. |
| 553 |
* fill the rs_vistuples[] array with the OffsetNumbers of visible tuples. |
| 554 |
*/ |
| 555 |
void |
| 556 |
heap_prepare_pagescan(TableScanDesc sscan) |
| 557 |
{ |
| 558 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 559 |
Buffer buffer = scan->rs_cbuf; |
| 560 |
BlockNumber block = scan->rs_cblock; |
| 561 |
Snapshot snapshot; |
| 562 |
Page page; |
| 563 |
int lines; |
| 564 |
bool all_visible; |
| 565 |
bool check_serializable; |
| 566 |
|
| 567 |
Assert(BufferGetBlockNumber(buffer) == block); |
| 568 |
|
| 569 |
/* ensure we're not accidentally being used when not in pagemode */ |
| 570 |
Assert(scan->rs_base.rs_flags & SO_ALLOW_PAGEMODE); |
| 571 |
snapshot = scan->rs_base.rs_snapshot; |
| 572 |
|
| 573 |
/* |
| 574 |
* Prune and repair fragmentation for the whole page, if possible. |
| 575 |
*/ |
| 576 |
heap_page_prune_opt(scan->rs_base.rs_rd, buffer); |
| 577 |
|
| 578 |
/* |
| 579 |
* We must hold share lock on the buffer content while examining tuple |
| 580 |
* visibility. Afterwards, however, the tuples we have found to be |
| 581 |
* visible are guaranteed good as long as we hold the buffer pin. |
| 582 |
*/ |
| 583 |
LockBuffer(buffer, BUFFER_LOCK_SHARE); |
| 584 |
|
| 585 |
page = BufferGetPage(buffer); |
| 586 |
lines = PageGetMaxOffsetNumber(page); |
| 587 |
|
| 588 |
/* |
| 589 |
* If the all-visible flag indicates that all tuples on the page are |
| 590 |
* visible to everyone, we can skip the per-tuple visibility tests. |
| 591 |
* |
| 592 |
* Note: In hot standby, a tuple that's already visible to all |
| 593 |
* transactions on the primary might still be invisible to a read-only |
| 594 |
* transaction in the standby. We partly handle this problem by tracking |
| 595 |
* the minimum xmin of visible tuples as the cut-off XID while marking a |
| 596 |
* page all-visible on the primary and WAL log that along with the |
| 597 |
* visibility map SET operation. In hot standby, we wait for (or abort) |
| 598 |
* all transactions that can potentially may not see one or more tuples on |
| 599 |
* the page. That's how index-only scans work fine in hot standby. A |
| 600 |
* crucial difference between index-only scans and heap scans is that the |
| 601 |
* index-only scan completely relies on the visibility map where as heap |
| 602 |
* scan looks at the page-level PD_ALL_VISIBLE flag. We are not sure if |
| 603 |
* the page-level flag can be trusted in the same way, because it might |
| 604 |
* get propagated somehow without being explicitly WAL-logged, e.g. via a |
| 605 |
* full page write. Until we can prove that beyond doubt, let's check each |
| 606 |
* tuple for visibility the hard way. |
| 607 |
*/ |
| 608 |
all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery; |
| 609 |
check_serializable = |
| 610 |
CheckForSerializableConflictOutNeeded(scan->rs_base.rs_rd, snapshot); |
| 611 |
|
| 612 |
/* |
| 613 |
* We call page_collect_tuples() with constant arguments, to get the |
| 614 |
* compiler to constant fold the constant arguments. Separate calls with |
| 615 |
* constant arguments, rather than variables, are needed on several |
| 616 |
* compilers to actually perform constant folding. |
| 617 |
*/ |
| 618 |
if (likely(all_visible)) |
| 619 |
{ |
| 620 |
if (likely(!check_serializable)) |
| 621 |
scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer, |
| 622 |
block, lines, true, false); |
| 623 |
else |
| 624 |
scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer, |
| 625 |
block, lines, true, true); |
| 626 |
} |
| 627 |
else |
| 628 |
{ |
| 629 |
if (likely(!check_serializable)) |
| 630 |
scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer, |
| 631 |
block, lines, false, false); |
| 632 |
else |
| 633 |
scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer, |
| 634 |
block, lines, false, true); |
| 635 |
} |
| 636 |
|
| 637 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 638 |
} |
| 639 |
|
| 640 |
/* |
| 641 |
* heap_fetch_next_buffer - read and pin the next block from MAIN_FORKNUM. |
| 642 |
* |
| 643 |
* Read the next block of the scan relation from the read stream and save it |
| 644 |
* in the scan descriptor. It is already pinned. |
| 645 |
*/ |
| 646 |
static inline void |
| 647 |
heap_fetch_next_buffer(HeapScanDesc scan, ScanDirection dir) |
| 648 |
{ |
| 649 |
Assert(scan->rs_read_stream); |
| 650 |
|
| 651 |
/* release previous scan buffer, if any */ |
| 652 |
if (BufferIsValid(scan->rs_cbuf)) |
| 653 |
{ |
| 654 |
ReleaseBuffer(scan->rs_cbuf); |
| 655 |
scan->rs_cbuf = InvalidBuffer; |
| 656 |
} |
| 657 |
|
| 658 |
/* |
| 659 |
* Be sure to check for interrupts at least once per page. Checks at |
| 660 |
* higher code levels won't be able to stop a seqscan that encounters many |
| 661 |
* pages' worth of consecutive dead tuples. |
| 662 |
*/ |
| 663 |
CHECK_FOR_INTERRUPTS(); |
| 664 |
|
| 665 |
/* |
| 666 |
* If the scan direction is changing, reset the prefetch block to the |
| 667 |
* current block. Otherwise, we will incorrectly prefetch the blocks |
| 668 |
* between the prefetch block and the current block again before |
| 669 |
* prefetching blocks in the new, correct scan direction. |
| 670 |
*/ |
| 671 |
if (unlikely(scan->rs_dir != dir)) |
| 672 |
{ |
| 673 |
scan->rs_prefetch_block = scan->rs_cblock; |
| 674 |
read_stream_reset(scan->rs_read_stream); |
| 675 |
} |
| 676 |
|
| 677 |
scan->rs_dir = dir; |
| 678 |
|
| 679 |
scan->rs_cbuf = read_stream_next_buffer(scan->rs_read_stream, NULL); |
| 680 |
if (BufferIsValid(scan->rs_cbuf)) |
| 681 |
scan->rs_cblock = BufferGetBlockNumber(scan->rs_cbuf); |
| 682 |
} |
| 683 |
|
| 684 |
/* |
| 685 |
* heapgettup_initial_block - return the first BlockNumber to scan |
| 686 |
* |
| 687 |
* Returns InvalidBlockNumber when there are no blocks to scan. This can |
| 688 |
* occur with empty tables and in parallel scans when parallel workers get all |
| 689 |
* of the pages before we can get a chance to get our first page. |
| 690 |
*/ |
| 691 |
static pg_noinline BlockNumber |
| 692 |
heapgettup_initial_block(HeapScanDesc scan, ScanDirection dir) |
| 693 |
{ |
| 694 |
Assert(!scan->rs_inited); |
| 695 |
Assert(scan->rs_base.rs_parallel == NULL); |
| 696 |
|
| 697 |
/* When there are no pages to scan, return InvalidBlockNumber */ |
| 698 |
if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0) |
| 699 |
return InvalidBlockNumber; |
| 700 |
|
| 701 |
if (ScanDirectionIsForward(dir)) |
| 702 |
{ |
| 703 |
return scan->rs_startblock; |
| 704 |
} |
| 705 |
else |
| 706 |
{ |
| 707 |
/* |
| 708 |
* Disable reporting to syncscan logic in a backwards scan; it's not |
| 709 |
* very likely anyone else is doing the same thing at the same time, |
| 710 |
* and much more likely that we'll just bollix things for forward |
| 711 |
* scanners. |
| 712 |
*/ |
| 713 |
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC; |
| 714 |
|
| 715 |
/* |
| 716 |
* Start from last page of the scan. Ensure we take into account |
| 717 |
* rs_numblocks if it's been adjusted by heap_setscanlimits(). |
| 718 |
*/ |
| 719 |
if (scan->rs_numblocks != InvalidBlockNumber) |
| 720 |
return (scan->rs_startblock + scan->rs_numblocks - 1) % scan->rs_nblocks; |
| 721 |
|
| 722 |
if (scan->rs_startblock > 0) |
| 723 |
return scan->rs_startblock - 1; |
| 724 |
|
| 725 |
return scan->rs_nblocks - 1; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
/* |
| 731 |
* heapgettup_start_page - helper function for heapgettup() |
| 732 |
* |
| 733 |
* Return the next page to scan based on the scan->rs_cbuf and set *linesleft |
| 734 |
* to the number of tuples on this page. Also set *lineoff to the first |
| 735 |
* offset to scan with forward scans getting the first offset and backward |
| 736 |
* getting the final offset on the page. |
| 737 |
*/ |
| 738 |
static Page |
| 739 |
heapgettup_start_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, |
| 740 |
OffsetNumber *lineoff) |
| 741 |
{ |
| 742 |
Page page; |
| 743 |
|
| 744 |
Assert(scan->rs_inited); |
| 745 |
Assert(BufferIsValid(scan->rs_cbuf)); |
| 746 |
|
| 747 |
/* Caller is responsible for ensuring buffer is locked if needed */ |
| 748 |
page = BufferGetPage(scan->rs_cbuf); |
| 749 |
|
| 750 |
*linesleft = PageGetMaxOffsetNumber(page) - FirstOffsetNumber + 1; |
| 751 |
|
| 752 |
if (ScanDirectionIsForward(dir)) |
| 753 |
*lineoff = FirstOffsetNumber; |
| 754 |
else |
| 755 |
*lineoff = (OffsetNumber) (*linesleft); |
| 756 |
|
| 757 |
/* lineoff now references the physically previous or next tid */ |
| 758 |
return page; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
/* |
| 763 |
* heapgettup_continue_page - helper function for heapgettup() |
| 764 |
* |
| 765 |
* Return the next page to scan based on the scan->rs_cbuf and set *linesleft |
| 766 |
* to the number of tuples left to scan on this page. Also set *lineoff to |
| 767 |
* the next offset to scan according to the ScanDirection in 'dir'. |
| 768 |
*/ |
| 769 |
static inline Page |
| 770 |
heapgettup_continue_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, |
| 771 |
OffsetNumber *lineoff) |
| 772 |
{ |
| 773 |
Page page; |
| 774 |
|
| 775 |
Assert(scan->rs_inited); |
| 776 |
Assert(BufferIsValid(scan->rs_cbuf)); |
| 777 |
|
| 778 |
/* Caller is responsible for ensuring buffer is locked if needed */ |
| 779 |
page = BufferGetPage(scan->rs_cbuf); |
| 780 |
|
| 781 |
if (ScanDirectionIsForward(dir)) |
| 782 |
{ |
| 783 |
*lineoff = OffsetNumberNext(scan->rs_coffset); |
| 784 |
*linesleft = PageGetMaxOffsetNumber(page) - (*lineoff) + 1; |
| 785 |
} |
| 786 |
else |
| 787 |
{ |
| 788 |
/* |
| 789 |
* The previous returned tuple may have been vacuumed since the |
| 790 |
* previous scan when we use a non-MVCC snapshot, so we must |
| 791 |
* re-establish the lineoff <= PageGetMaxOffsetNumber(page) invariant |
| 792 |
*/ |
| 793 |
*lineoff = Min(PageGetMaxOffsetNumber(page), OffsetNumberPrev(scan->rs_coffset)); |
| 794 |
*linesleft = *lineoff; |
| 795 |
} |
| 796 |
|
| 797 |
/* lineoff now references the physically previous or next tid */ |
| 798 |
return page; |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
* heapgettup_advance_block - helper for heap_fetch_next_buffer() |
| 803 |
* |
| 804 |
* Given the current block number, the scan direction, and various information |
| 805 |
* contained in the scan descriptor, calculate the BlockNumber to scan next |
| 806 |
* and return it. If there are no further blocks to scan, return |
| 807 |
* InvalidBlockNumber to indicate this fact to the caller. |
| 808 |
* |
| 809 |
* This should not be called to determine the initial block number -- only for |
| 810 |
* subsequent blocks. |
| 811 |
* |
| 812 |
* This also adjusts rs_numblocks when a limit has been imposed by |
| 813 |
* heap_setscanlimits(). |
| 814 |
*/ |
| 815 |
static inline BlockNumber |
| 816 |
heapgettup_advance_block(HeapScanDesc scan, BlockNumber block, ScanDirection dir) |
| 817 |
{ |
| 818 |
Assert(scan->rs_base.rs_parallel == NULL); |
| 819 |
|
| 820 |
if (likely(ScanDirectionIsForward(dir))) |
| 821 |
{ |
| 822 |
block++; |
| 823 |
|
| 824 |
/* wrap back to the start of the heap */ |
| 825 |
if (block >= scan->rs_nblocks) |
| 826 |
block = 0; |
| 827 |
|
| 828 |
/* |
| 829 |
* Report our new scan position for synchronization purposes. We don't |
| 830 |
* do that when moving backwards, however. That would just mess up any |
| 831 |
* other forward-moving scanners. |
| 832 |
* |
| 833 |
* Note: we do this before checking for end of scan so that the final |
| 834 |
* state of the position hint is back at the start of the rel. That's |
| 835 |
* not strictly necessary, but otherwise when you run the same query |
| 836 |
* multiple times the starting position would shift a little bit |
| 837 |
* backwards on every invocation, which is confusing. We don't |
| 838 |
* guarantee any specific ordering in general, though. |
| 839 |
*/ |
| 840 |
if (scan->rs_base.rs_flags & SO_ALLOW_SYNC) |
| 841 |
ss_report_location(scan->rs_base.rs_rd, block); |
| 842 |
|
| 843 |
/* we're done if we're back at where we started */ |
| 844 |
if (block == scan->rs_startblock) |
| 845 |
return InvalidBlockNumber; |
| 846 |
|
| 847 |
/* check if the limit imposed by heap_setscanlimits() is met */ |
| 848 |
if (scan->rs_numblocks != InvalidBlockNumber) |
| 849 |
{ |
| 850 |
if (--scan->rs_numblocks == 0) |
| 851 |
return InvalidBlockNumber; |
| 852 |
} |
| 853 |
|
| 854 |
return block; |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
/* we're done if the last block is the start position */ |
| 859 |
if (block == scan->rs_startblock) |
| 860 |
return InvalidBlockNumber; |
| 861 |
|
| 862 |
/* check if the limit imposed by heap_setscanlimits() is met */ |
| 863 |
if (scan->rs_numblocks != InvalidBlockNumber) |
| 864 |
{ |
| 865 |
if (--scan->rs_numblocks == 0) |
| 866 |
return InvalidBlockNumber; |
| 867 |
} |
| 868 |
|
| 869 |
/* wrap to the end of the heap when the last page was page 0 */ |
| 870 |
if (block == 0) |
| 871 |
block = scan->rs_nblocks; |
| 872 |
|
| 873 |
block--; |
| 874 |
|
| 875 |
return block; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
/* ---------------- |
| 880 |
* heapgettup - fetch next heap tuple |
| 881 |
* |
| 882 |
* Initialize the scan if not already done; then advance to the next |
| 883 |
* tuple as indicated by "dir"; return the next tuple in scan->rs_ctup, |
| 884 |
* or set scan->rs_ctup.t_data = NULL if no more tuples. |
| 885 |
* |
| 886 |
* Note: the reason nkeys/key are passed separately, even though they are |
| 887 |
* kept in the scan descriptor, is that the caller may not want us to check |
| 888 |
* the scankeys. |
| 889 |
* |
| 890 |
* Note: when we fall off the end of the scan in either direction, we |
| 891 |
* reset rs_inited. This means that a further request with the same |
| 892 |
* scan direction will restart the scan, which is a bit odd, but a |
| 893 |
* request with the opposite scan direction will start a fresh scan |
| 894 |
* in the proper direction. The latter is required behavior for cursors, |
| 895 |
* while the former case is generally undefined behavior in Postgres |
| 896 |
* so we don't care too much. |
| 897 |
* ---------------- |
| 898 |
*/ |
| 899 |
static void |
| 900 |
heapgettup(HeapScanDesc scan, |
| 901 |
ScanDirection dir, |
| 902 |
int nkeys, |
| 903 |
ScanKey key) |
| 904 |
{ |
| 905 |
HeapTuple tuple = &(scan->rs_ctup); |
| 906 |
Page page; |
| 907 |
OffsetNumber lineoff; |
| 908 |
int linesleft; |
| 909 |
|
| 910 |
if (likely(scan->rs_inited)) |
| 911 |
{ |
| 912 |
/* continue from previously returned page/tuple */ |
| 913 |
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); |
| 914 |
page = heapgettup_continue_page(scan, dir, &linesleft, &lineoff); |
| 915 |
goto continue_page; |
| 916 |
} |
| 917 |
|
| 918 |
/* |
| 919 |
* advance the scan until we find a qualifying tuple or run out of stuff |
| 920 |
* to scan |
| 921 |
*/ |
| 922 |
while (true) |
| 923 |
{ |
| 924 |
heap_fetch_next_buffer(scan, dir); |
| 925 |
|
| 926 |
/* did we run out of blocks to scan? */ |
| 927 |
if (!BufferIsValid(scan->rs_cbuf)) |
| 928 |
break; |
| 929 |
|
| 930 |
Assert(BufferGetBlockNumber(scan->rs_cbuf) == scan->rs_cblock); |
| 931 |
|
| 932 |
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); |
| 933 |
page = heapgettup_start_page(scan, dir, &linesleft, &lineoff); |
| 934 |
continue_page: |
| 935 |
|
| 936 |
/* |
| 937 |
* Only continue scanning the page while we have lines left. |
| 938 |
* |
| 939 |
* Note that this protects us from accessing line pointers past |
| 940 |
* PageGetMaxOffsetNumber(); both for forward scans when we resume the |
| 941 |
* table scan, and for when we start scanning a new page. |
| 942 |
*/ |
| 943 |
for (; linesleft > 0; linesleft--, lineoff += dir) |
| 944 |
{ |
| 945 |
bool visible; |
| 946 |
ItemId lpp = PageGetItemId(page, lineoff); |
| 947 |
|
| 948 |
if (!ItemIdIsNormal(lpp)) |
| 949 |
continue; |
| 950 |
|
| 951 |
tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp); |
| 952 |
tuple->t_len = ItemIdGetLength(lpp); |
| 953 |
ItemPointerSet(&(tuple->t_self), scan->rs_cblock, lineoff); |
| 954 |
|
| 955 |
visible = HeapTupleSatisfiesVisibility(tuple, |
| 956 |
scan->rs_base.rs_snapshot, |
| 957 |
scan->rs_cbuf); |
| 958 |
|
| 959 |
HeapCheckForSerializableConflictOut(visible, scan->rs_base.rs_rd, |
| 960 |
tuple, scan->rs_cbuf, |
| 961 |
scan->rs_base.rs_snapshot); |
| 962 |
|
| 963 |
/* skip tuples not visible to this snapshot */ |
| 964 |
if (!visible) |
| 965 |
continue; |
| 966 |
|
| 967 |
/* skip any tuples that don't match the scan key */ |
| 968 |
if (key != NULL && |
| 969 |
!HeapKeyTest(tuple, RelationGetDescr(scan->rs_base.rs_rd), |
| 970 |
nkeys, key)) |
| 971 |
continue; |
| 972 |
|
| 973 |
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); |
| 974 |
scan->rs_coffset = lineoff; |
| 975 |
return; |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* if we get here, it means we've exhausted the items on this page and |
| 980 |
* it's time to move to the next. |
| 981 |
*/ |
| 982 |
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); |
| 983 |
} |
| 984 |
|
| 985 |
/* end of scan */ |
| 986 |
if (BufferIsValid(scan->rs_cbuf)) |
| 987 |
ReleaseBuffer(scan->rs_cbuf); |
| 988 |
|
| 989 |
scan->rs_cbuf = InvalidBuffer; |
| 990 |
scan->rs_cblock = InvalidBlockNumber; |
| 991 |
scan->rs_prefetch_block = InvalidBlockNumber; |
| 992 |
tuple->t_data = NULL; |
| 993 |
scan->rs_inited = false; |
| 994 |
} |
| 995 |
|
| 996 |
/* ---------------- |
| 997 |
* heapgettup_pagemode - fetch next heap tuple in page-at-a-time mode |
| 998 |
* |
| 999 |
* Same API as heapgettup, but used in page-at-a-time mode |
| 1000 |
* |
| 1001 |
* The internal logic is much the same as heapgettup's too, but there are some |
| 1002 |
* differences: we do not take the buffer content lock (that only needs to |
| 1003 |
* happen inside heap_prepare_pagescan), and we iterate through just the |
| 1004 |
* tuples listed in rs_vistuples[] rather than all tuples on the page. Notice |
| 1005 |
* that lineindex is 0-based, where the corresponding loop variable lineoff in |
| 1006 |
* heapgettup is 1-based. |
| 1007 |
* ---------------- |
| 1008 |
*/ |
| 1009 |
static void |
| 1010 |
heapgettup_pagemode(HeapScanDesc scan, |
| 1011 |
ScanDirection dir, |
| 1012 |
int nkeys, |
| 1013 |
ScanKey key) |
| 1014 |
{ |
| 1015 |
HeapTuple tuple = &(scan->rs_ctup); |
| 1016 |
Page page; |
| 1017 |
uint32 lineindex; |
| 1018 |
uint32 linesleft; |
| 1019 |
|
| 1020 |
if (likely(scan->rs_inited)) |
| 1021 |
{ |
| 1022 |
/* continue from previously returned page/tuple */ |
| 1023 |
page = BufferGetPage(scan->rs_cbuf); |
| 1024 |
|
| 1025 |
lineindex = scan->rs_cindex + dir; |
| 1026 |
if (ScanDirectionIsForward(dir)) |
| 1027 |
linesleft = scan->rs_ntuples - lineindex; |
| 1028 |
else |
| 1029 |
linesleft = scan->rs_cindex; |
| 1030 |
/* lineindex now references the next or previous visible tid */ |
| 1031 |
|
| 1032 |
goto continue_page; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* |
| 1036 |
* advance the scan until we find a qualifying tuple or run out of stuff |
| 1037 |
* to scan |
| 1038 |
*/ |
| 1039 |
while (true) |
| 1040 |
{ |
| 1041 |
heap_fetch_next_buffer(scan, dir); |
| 1042 |
|
| 1043 |
/* did we run out of blocks to scan? */ |
| 1044 |
if (!BufferIsValid(scan->rs_cbuf)) |
| 1045 |
break; |
| 1046 |
|
| 1047 |
Assert(BufferGetBlockNumber(scan->rs_cbuf) == scan->rs_cblock); |
| 1048 |
|
| 1049 |
/* prune the page and determine visible tuple offsets */ |
| 1050 |
heap_prepare_pagescan((TableScanDesc) scan); |
| 1051 |
page = BufferGetPage(scan->rs_cbuf); |
| 1052 |
linesleft = scan->rs_ntuples; |
| 1053 |
lineindex = ScanDirectionIsForward(dir) ? 0 : linesleft - 1; |
| 1054 |
|
| 1055 |
/* block is the same for all tuples, set it once outside the loop */ |
| 1056 |
ItemPointerSetBlockNumber(&tuple->t_self, scan->rs_cblock); |
| 1057 |
|
| 1058 |
/* lineindex now references the next or previous visible tid */ |
| 1059 |
continue_page: |
| 1060 |
|
| 1061 |
for (; linesleft > 0; linesleft--, lineindex += dir) |
| 1062 |
{ |
| 1063 |
ItemId lpp; |
| 1064 |
OffsetNumber lineoff; |
| 1065 |
|
| 1066 |
Assert(lineindex < scan->rs_ntuples); |
| 1067 |
lineoff = scan->rs_vistuples[lineindex]; |
| 1068 |
lpp = PageGetItemId(page, lineoff); |
| 1069 |
Assert(ItemIdIsNormal(lpp)); |
| 1070 |
|
| 1071 |
tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp); |
| 1072 |
tuple->t_len = ItemIdGetLength(lpp); |
| 1073 |
ItemPointerSetOffsetNumber(&tuple->t_self, lineoff); |
| 1074 |
|
| 1075 |
/* skip any tuples that don't match the scan key */ |
| 1076 |
if (key != NULL && |
| 1077 |
!HeapKeyTest(tuple, RelationGetDescr(scan->rs_base.rs_rd), |
| 1078 |
nkeys, key)) |
| 1079 |
continue; |
| 1080 |
|
| 1081 |
scan->rs_cindex = lineindex; |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* end of scan */ |
| 1087 |
if (BufferIsValid(scan->rs_cbuf)) |
| 1088 |
ReleaseBuffer(scan->rs_cbuf); |
| 1089 |
scan->rs_cbuf = InvalidBuffer; |
| 1090 |
scan->rs_cblock = InvalidBlockNumber; |
| 1091 |
scan->rs_prefetch_block = InvalidBlockNumber; |
| 1092 |
tuple->t_data = NULL; |
| 1093 |
scan->rs_inited = false; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
/* ---------------------------------------------------------------- |
| 1098 |
* heap access method interface |
| 1099 |
* ---------------------------------------------------------------- |
| 1100 |
*/ |
| 1101 |
|
| 1102 |
|
| 1103 |
TableScanDesc |
| 1104 |
heap_beginscan(Relation relation, Snapshot snapshot, |
| 1105 |
int nkeys, ScanKey key, |
| 1106 |
ParallelTableScanDesc parallel_scan, |
| 1107 |
uint32 flags) |
| 1108 |
{ |
| 1109 |
HeapScanDesc scan; |
| 1110 |
|
| 1111 |
/* |
| 1112 |
* increment relation ref count while scanning relation |
| 1113 |
* |
| 1114 |
* This is just to make really sure the relcache entry won't go away while |
| 1115 |
* the scan has a pointer to it. Caller should be holding the rel open |
| 1116 |
* anyway, so this is redundant in all normal scenarios... |
| 1117 |
*/ |
| 1118 |
RelationIncrementReferenceCount(relation); |
| 1119 |
|
| 1120 |
/* |
| 1121 |
* allocate and initialize scan descriptor |
| 1122 |
*/ |
| 1123 |
if (flags & SO_TYPE_BITMAPSCAN) |
| 1124 |
{ |
| 1125 |
BitmapHeapScanDesc bscan = palloc(sizeof(BitmapHeapScanDescData)); |
| 1126 |
|
| 1127 |
/* |
| 1128 |
* Bitmap Heap scans do not have any fields that a normal Heap Scan |
| 1129 |
* does not have, so no special initializations required here. |
| 1130 |
*/ |
| 1131 |
scan = (HeapScanDesc) bscan; |
| 1132 |
} |
| 1133 |
else |
| 1134 |
scan = (HeapScanDesc) palloc(sizeof(HeapScanDescData)); |
| 1135 |
|
| 1136 |
scan->rs_base.rs_rd = relation; |
| 1137 |
scan->rs_base.rs_snapshot = snapshot; |
| 1138 |
scan->rs_base.rs_nkeys = nkeys; |
| 1139 |
scan->rs_base.rs_flags = flags; |
| 1140 |
scan->rs_base.rs_parallel = parallel_scan; |
| 1141 |
scan->rs_strategy = NULL; /* set in initscan */ |
| 1142 |
scan->rs_cbuf = InvalidBuffer; |
| 1143 |
|
| 1144 |
/* |
| 1145 |
* Disable page-at-a-time mode if it's not a MVCC-safe snapshot. |
| 1146 |
*/ |
| 1147 |
if (!(snapshot && IsMVCCSnapshot(snapshot))) |
| 1148 |
scan->rs_base.rs_flags &= ~SO_ALLOW_PAGEMODE; |
| 1149 |
|
| 1150 |
/* |
| 1151 |
* For seqscan and sample scans in a serializable transaction, acquire a |
| 1152 |
* predicate lock on the entire relation. This is required not only to |
| 1153 |
* lock all the matching tuples, but also to conflict with new insertions |
| 1154 |
* into the table. In an indexscan, we take page locks on the index pages |
| 1155 |
* covering the range specified in the scan qual, but in a heap scan there |
| 1156 |
* is nothing more fine-grained to lock. A bitmap scan is a different |
| 1157 |
* story, there we have already scanned the index and locked the index |
| 1158 |
* pages covering the predicate. But in that case we still have to lock |
| 1159 |
* any matching heap tuples. For sample scan we could optimize the locking |
| 1160 |
* to be at least page-level granularity, but we'd need to add per-tuple |
| 1161 |
* locking for that. |
| 1162 |
*/ |
| 1163 |
if (scan->rs_base.rs_flags & (SO_TYPE_SEQSCAN | SO_TYPE_SAMPLESCAN)) |
| 1164 |
{ |
| 1165 |
/* |
| 1166 |
* Ensure a missing snapshot is noticed reliably, even if the |
| 1167 |
* isolation mode means predicate locking isn't performed (and |
| 1168 |
* therefore the snapshot isn't used here). |
| 1169 |
*/ |
| 1170 |
Assert(snapshot); |
| 1171 |
PredicateLockRelation(relation, snapshot); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* we only need to set this up once */ |
| 1175 |
scan->rs_ctup.t_tableOid = RelationGetRelid(relation); |
| 1176 |
|
| 1177 |
/* |
| 1178 |
* Allocate memory to keep track of page allocation for parallel workers |
| 1179 |
* when doing a parallel scan. |
| 1180 |
*/ |
| 1181 |
if (parallel_scan != NULL) |
| 1182 |
scan->rs_parallelworkerdata = palloc(sizeof(ParallelBlockTableScanWorkerData)); |
| 1183 |
else |
| 1184 |
scan->rs_parallelworkerdata = NULL; |
| 1185 |
|
| 1186 |
/* |
| 1187 |
* we do this here instead of in initscan() because heap_rescan also calls |
| 1188 |
* initscan() and we don't want to allocate memory again |
| 1189 |
*/ |
| 1190 |
if (nkeys > 0) |
| 1191 |
scan->rs_base.rs_key = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys); |
| 1192 |
else |
| 1193 |
scan->rs_base.rs_key = NULL; |
| 1194 |
|
| 1195 |
initscan(scan, key, false); |
| 1196 |
|
| 1197 |
scan->rs_read_stream = NULL; |
| 1198 |
|
| 1199 |
/* |
| 1200 |
* Set up a read stream for sequential scans and TID range scans. This |
| 1201 |
* should be done after initscan() because initscan() allocates the |
| 1202 |
* BufferAccessStrategy object passed to the read stream API. |
| 1203 |
*/ |
| 1204 |
if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN || |
| 1205 |
scan->rs_base.rs_flags & SO_TYPE_TIDRANGESCAN) |
| 1206 |
{ |
| 1207 |
ReadStreamBlockNumberCB cb; |
| 1208 |
|
| 1209 |
if (scan->rs_base.rs_parallel) |
| 1210 |
cb = heap_scan_stream_read_next_parallel; |
| 1211 |
else |
| 1212 |
cb = heap_scan_stream_read_next_serial; |
| 1213 |
|
| 1214 |
/* --- |
| 1215 |
* It is safe to use batchmode as the only locks taken by `cb` |
| 1216 |
* are never taken while waiting for IO: |
| 1217 |
* - SyncScanLock is used in the non-parallel case |
| 1218 |
* - in the parallel case, only spinlocks and atomics are used |
| 1219 |
* --- |
| 1220 |
*/ |
| 1221 |
scan->rs_read_stream = read_stream_begin_relation(READ_STREAM_SEQUENTIAL | |
| 1222 |
READ_STREAM_USE_BATCHING, |
| 1223 |
scan->rs_strategy, |
| 1224 |
scan->rs_base.rs_rd, |
| 1225 |
MAIN_FORKNUM, |
| 1226 |
cb, |
| 1227 |
scan, |
| 1228 |
0); |
| 1229 |
} |
| 1230 |
else if (scan->rs_base.rs_flags & SO_TYPE_BITMAPSCAN) |
| 1231 |
{ |
| 1232 |
scan->rs_read_stream = read_stream_begin_relation(READ_STREAM_DEFAULT | |
| 1233 |
READ_STREAM_USE_BATCHING, |
| 1234 |
scan->rs_strategy, |
| 1235 |
scan->rs_base.rs_rd, |
| 1236 |
MAIN_FORKNUM, |
| 1237 |
bitmapheap_stream_read_next, |
| 1238 |
scan, |
| 1239 |
sizeof(TBMIterateResult)); |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
return (TableScanDesc) scan; |
| 1244 |
} |
| 1245 |
|
| 1246 |
void |
| 1247 |
heap_rescan(TableScanDesc sscan, ScanKey key, bool set_params, |
| 1248 |
bool allow_strat, bool allow_sync, bool allow_pagemode) |
| 1249 |
{ |
| 1250 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1251 |
|
| 1252 |
if (set_params) |
| 1253 |
{ |
| 1254 |
if (allow_strat) |
| 1255 |
scan->rs_base.rs_flags |= SO_ALLOW_STRAT; |
| 1256 |
else |
| 1257 |
scan->rs_base.rs_flags &= ~SO_ALLOW_STRAT; |
| 1258 |
|
| 1259 |
if (allow_sync) |
| 1260 |
scan->rs_base.rs_flags |= SO_ALLOW_SYNC; |
| 1261 |
else |
| 1262 |
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC; |
| 1263 |
|
| 1264 |
if (allow_pagemode && scan->rs_base.rs_snapshot && |
| 1265 |
IsMVCCSnapshot(scan->rs_base.rs_snapshot)) |
| 1266 |
scan->rs_base.rs_flags |= SO_ALLOW_PAGEMODE; |
| 1267 |
else |
| 1268 |
scan->rs_base.rs_flags &= ~SO_ALLOW_PAGEMODE; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* |
| 1272 |
* unpin scan buffers |
| 1273 |
*/ |
| 1274 |
if (BufferIsValid(scan->rs_cbuf)) |
| 1275 |
{ |
| 1276 |
ReleaseBuffer(scan->rs_cbuf); |
| 1277 |
scan->rs_cbuf = InvalidBuffer; |
| 1278 |
} |
| 1279 |
|
| 1280 |
/* |
| 1281 |
* SO_TYPE_BITMAPSCAN would be cleaned up here, but it does not hold any |
| 1282 |
* additional data vs a normal HeapScan |
| 1283 |
*/ |
| 1284 |
|
| 1285 |
/* |
| 1286 |
* The read stream is reset on rescan. This must be done before |
| 1287 |
* initscan(), as some state referred to by read_stream_reset() is reset |
| 1288 |
* in initscan(). |
| 1289 |
*/ |
| 1290 |
if (scan->rs_read_stream) |
| 1291 |
read_stream_reset(scan->rs_read_stream); |
| 1292 |
|
| 1293 |
/* |
| 1294 |
* reinitialize scan descriptor |
| 1295 |
*/ |
| 1296 |
initscan(scan, key, true); |
| 1297 |
} |
| 1298 |
|
| 1299 |
void |
| 1300 |
heap_endscan(TableScanDesc sscan) |
| 1301 |
{ |
| 1302 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1303 |
|
| 1304 |
/* Note: no locking manipulations needed */ |
| 1305 |
|
| 1306 |
/* |
| 1307 |
* unpin scan buffers |
| 1308 |
*/ |
| 1309 |
if (BufferIsValid(scan->rs_cbuf)) |
| 1310 |
ReleaseBuffer(scan->rs_cbuf); |
| 1311 |
|
| 1312 |
/* |
| 1313 |
* Must free the read stream before freeing the BufferAccessStrategy. |
| 1314 |
*/ |
| 1315 |
if (scan->rs_read_stream) |
| 1316 |
read_stream_end(scan->rs_read_stream); |
| 1317 |
|
| 1318 |
/* |
| 1319 |
* decrement relation reference count and free scan descriptor storage |
| 1320 |
*/ |
| 1321 |
RelationDecrementReferenceCount(scan->rs_base.rs_rd); |
| 1322 |
|
| 1323 |
if (scan->rs_base.rs_key) |
| 1324 |
pfree(scan->rs_base.rs_key); |
| 1325 |
|
| 1326 |
if (scan->rs_strategy != NULL) |
| 1327 |
FreeAccessStrategy(scan->rs_strategy); |
| 1328 |
|
| 1329 |
if (scan->rs_parallelworkerdata != NULL) |
| 1330 |
pfree(scan->rs_parallelworkerdata); |
| 1331 |
|
| 1332 |
if (scan->rs_base.rs_flags & SO_TEMP_SNAPSHOT) |
| 1333 |
UnregisterSnapshot(scan->rs_base.rs_snapshot); |
| 1334 |
|
| 1335 |
pfree(scan); |
| 1336 |
} |
| 1337 |
|
| 1338 |
HeapTuple |
| 1339 |
heap_getnext(TableScanDesc sscan, ScanDirection direction) |
| 1340 |
{ |
| 1341 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1342 |
|
| 1343 |
/* |
| 1344 |
* This is still widely used directly, without going through table AM, so |
| 1345 |
* add a safety check. It's possible we should, at a later point, |
| 1346 |
* downgrade this to an assert. The reason for checking the AM routine, |
| 1347 |
* rather than the AM oid, is that this allows to write regression tests |
| 1348 |
* that create another AM reusing the heap handler. |
| 1349 |
*/ |
| 1350 |
if (unlikely(sscan->rs_rd->rd_tableam != GetHeapamTableAmRoutine())) |
| 1351 |
ereport(ERROR, |
| 1352 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1353 |
errmsg_internal("only heap AM is supported"))); |
| 1354 |
|
| 1355 |
/* |
| 1356 |
* We don't expect direct calls to heap_getnext with valid CheckXidAlive |
| 1357 |
* for catalog or regular tables. See detailed comments in xact.c where |
| 1358 |
* these variables are declared. Normally we have such a check at tableam |
| 1359 |
* level API but this is called from many places so we need to ensure it |
| 1360 |
* here. |
| 1361 |
*/ |
| 1362 |
if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan)) |
| 1363 |
elog(ERROR, "unexpected heap_getnext call during logical decoding"); |
| 1364 |
|
| 1365 |
/* Note: no locking manipulations needed */ |
| 1366 |
|
| 1367 |
if (scan->rs_base.rs_flags & SO_ALLOW_PAGEMODE) |
| 1368 |
heapgettup_pagemode(scan, direction, |
| 1369 |
scan->rs_base.rs_nkeys, scan->rs_base.rs_key); |
| 1370 |
else |
| 1371 |
heapgettup(scan, direction, |
| 1372 |
scan->rs_base.rs_nkeys, scan->rs_base.rs_key); |
| 1373 |
|
| 1374 |
if (scan->rs_ctup.t_data == NULL) |
| 1375 |
return NULL; |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* if we get here it means we have a new current scan tuple, so point to |
| 1379 |
* the proper return buffer and return the tuple. |
| 1380 |
*/ |
| 1381 |
|
| 1382 |
pgstat_count_heap_getnext(scan->rs_base.rs_rd); |
| 1383 |
|
| 1384 |
return &scan->rs_ctup; |
| 1385 |
} |
| 1386 |
|
| 1387 |
bool |
| 1388 |
heap_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot) |
| 1389 |
{ |
| 1390 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1391 |
|
| 1392 |
/* Note: no locking manipulations needed */ |
| 1393 |
|
| 1394 |
if (sscan->rs_flags & SO_ALLOW_PAGEMODE) |
| 1395 |
heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key); |
| 1396 |
else |
| 1397 |
heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key); |
| 1398 |
|
| 1399 |
if (scan->rs_ctup.t_data == NULL) |
| 1400 |
{ |
| 1401 |
ExecClearTuple(slot); |
| 1402 |
return false; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* if we get here it means we have a new current scan tuple, so point to |
| 1407 |
* the proper return buffer and return the tuple. |
| 1408 |
*/ |
| 1409 |
|
| 1410 |
pgstat_count_heap_getnext(scan->rs_base.rs_rd); |
| 1411 |
|
| 1412 |
ExecStoreBufferHeapTuple(&scan->rs_ctup, slot, |
| 1413 |
scan->rs_cbuf); |
| 1414 |
return true; |
| 1415 |
} |
| 1416 |
|
| 1417 |
void |
| 1418 |
heap_set_tidrange(TableScanDesc sscan, ItemPointer mintid, |
| 1419 |
ItemPointer maxtid) |
| 1420 |
{ |
| 1421 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1422 |
BlockNumber startBlk; |
| 1423 |
BlockNumber numBlks; |
| 1424 |
ItemPointerData highestItem; |
| 1425 |
ItemPointerData lowestItem; |
| 1426 |
|
| 1427 |
/* |
| 1428 |
* For relations without any pages, we can simply leave the TID range |
| 1429 |
* unset. There will be no tuples to scan, therefore no tuples outside |
| 1430 |
* the given TID range. |
| 1431 |
*/ |
| 1432 |
if (scan->rs_nblocks == 0) |
| 1433 |
return; |
| 1434 |
|
| 1435 |
/* |
| 1436 |
* Set up some ItemPointers which point to the first and last possible |
| 1437 |
* tuples in the heap. |
| 1438 |
*/ |
| 1439 |
ItemPointerSet(&highestItem, scan->rs_nblocks - 1, MaxOffsetNumber); |
| 1440 |
ItemPointerSet(&lowestItem, 0, FirstOffsetNumber); |
| 1441 |
|
| 1442 |
/* |
| 1443 |
* If the given maximum TID is below the highest possible TID in the |
| 1444 |
* relation, then restrict the range to that, otherwise we scan to the end |
| 1445 |
* of the relation. |
| 1446 |
*/ |
| 1447 |
if (ItemPointerCompare(maxtid, &highestItem) < 0) |
| 1448 |
ItemPointerCopy(maxtid, &highestItem); |
| 1449 |
|
| 1450 |
/* |
| 1451 |
* If the given minimum TID is above the lowest possible TID in the |
| 1452 |
* relation, then restrict the range to only scan for TIDs above that. |
| 1453 |
*/ |
| 1454 |
if (ItemPointerCompare(mintid, &lowestItem) > 0) |
| 1455 |
ItemPointerCopy(mintid, &lowestItem); |
| 1456 |
|
| 1457 |
/* |
| 1458 |
* Check for an empty range and protect from would be negative results |
| 1459 |
* from the numBlks calculation below. |
| 1460 |
*/ |
| 1461 |
if (ItemPointerCompare(&highestItem, &lowestItem) < 0) |
| 1462 |
{ |
| 1463 |
/* Set an empty range of blocks to scan */ |
| 1464 |
heap_setscanlimits(sscan, 0, 0); |
| 1465 |
return; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* |
| 1469 |
* Calculate the first block and the number of blocks we must scan. We |
| 1470 |
* could be more aggressive here and perform some more validation to try |
| 1471 |
* and further narrow the scope of blocks to scan by checking if the |
| 1472 |
* lowestItem has an offset above MaxOffsetNumber. In this case, we could |
| 1473 |
* advance startBlk by one. Likewise, if highestItem has an offset of 0 |
| 1474 |
* we could scan one fewer blocks. However, such an optimization does not |
| 1475 |
* seem worth troubling over, currently. |
| 1476 |
*/ |
| 1477 |
startBlk = ItemPointerGetBlockNumberNoCheck(&lowestItem); |
| 1478 |
|
| 1479 |
numBlks = ItemPointerGetBlockNumberNoCheck(&highestItem) - |
| 1480 |
ItemPointerGetBlockNumberNoCheck(&lowestItem) + 1; |
| 1481 |
|
| 1482 |
/* Set the start block and number of blocks to scan */ |
| 1483 |
heap_setscanlimits(sscan, startBlk, numBlks); |
| 1484 |
|
| 1485 |
/* Finally, set the TID range in sscan */ |
| 1486 |
ItemPointerCopy(&lowestItem, &sscan->st.tidrange.rs_mintid); |
| 1487 |
ItemPointerCopy(&highestItem, &sscan->st.tidrange.rs_maxtid); |
| 1488 |
} |
| 1489 |
|
| 1490 |
bool |
| 1491 |
heap_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction, |
| 1492 |
TupleTableSlot *slot) |
| 1493 |
{ |
| 1494 |
HeapScanDesc scan = (HeapScanDesc) sscan; |
| 1495 |
ItemPointer mintid = &sscan->st.tidrange.rs_mintid; |
| 1496 |
ItemPointer maxtid = &sscan->st.tidrange.rs_maxtid; |
| 1497 |
|
| 1498 |
/* Note: no locking manipulations needed */ |
| 1499 |
for (;;) |
| 1500 |
{ |
| 1501 |
if (sscan->rs_flags & SO_ALLOW_PAGEMODE) |
| 1502 |
heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key); |
| 1503 |
else |
| 1504 |
heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key); |
| 1505 |
|
| 1506 |
if (scan->rs_ctup.t_data == NULL) |
| 1507 |
{ |
| 1508 |
ExecClearTuple(slot); |
| 1509 |
return false; |
| 1510 |
} |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* heap_set_tidrange will have used heap_setscanlimits to limit the |
| 1514 |
* range of pages we scan to only ones that can contain the TID range |
| 1515 |
* we're scanning for. Here we must filter out any tuples from these |
| 1516 |
* pages that are outside of that range. |
| 1517 |
*/ |
| 1518 |
if (ItemPointerCompare(&scan->rs_ctup.t_self, mintid) < 0) |
| 1519 |
{ |
| 1520 |
ExecClearTuple(slot); |
| 1521 |
|
| 1522 |
/* |
| 1523 |
* When scanning backwards, the TIDs will be in descending order. |
| 1524 |
* Future tuples in this direction will be lower still, so we can |
| 1525 |
* just return false to indicate there will be no more tuples. |
| 1526 |
*/ |
| 1527 |
if (ScanDirectionIsBackward(direction)) |
| 1528 |
return false; |
| 1529 |
|
| 1530 |
continue; |
| 1531 |
} |
| 1532 |
|
| 1533 |
/* |
| 1534 |
* Likewise for the final page, we must filter out TIDs greater than |
| 1535 |
* maxtid. |
| 1536 |
*/ |
| 1537 |
if (ItemPointerCompare(&scan->rs_ctup.t_self, maxtid) > 0) |
| 1538 |
{ |
| 1539 |
ExecClearTuple(slot); |
| 1540 |
|
| 1541 |
/* |
| 1542 |
* When scanning forward, the TIDs will be in ascending order. |
| 1543 |
* Future tuples in this direction will be higher still, so we can |
| 1544 |
* just return false to indicate there will be no more tuples. |
| 1545 |
*/ |
| 1546 |
if (ScanDirectionIsForward(direction)) |
| 1547 |
return false; |
| 1548 |
continue; |
| 1549 |
} |
| 1550 |
|
| 1551 |
break; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* |
| 1555 |
* if we get here it means we have a new current scan tuple, so point to |
| 1556 |
* the proper return buffer and return the tuple. |
| 1557 |
*/ |
| 1558 |
pgstat_count_heap_getnext(scan->rs_base.rs_rd); |
| 1559 |
|
| 1560 |
ExecStoreBufferHeapTuple(&scan->rs_ctup, slot, scan->rs_cbuf); |
| 1561 |
return true; |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* |
| 1565 |
* heap_fetch - retrieve tuple with given tid |
| 1566 |
* |
| 1567 |
* On entry, tuple->t_self is the TID to fetch. We pin the buffer holding |
| 1568 |
* the tuple, fill in the remaining fields of *tuple, and check the tuple |
| 1569 |
* against the specified snapshot. |
| 1570 |
* |
| 1571 |
* If successful (tuple found and passes snapshot time qual), then *userbuf |
| 1572 |
* is set to the buffer holding the tuple and true is returned. The caller |
| 1573 |
* must unpin the buffer when done with the tuple. |
| 1574 |
* |
| 1575 |
* If the tuple is not found (ie, item number references a deleted slot), |
| 1576 |
* then tuple->t_data is set to NULL, *userbuf is set to InvalidBuffer, |
| 1577 |
* and false is returned. |
| 1578 |
* |
| 1579 |
* If the tuple is found but fails the time qual check, then the behavior |
| 1580 |
* depends on the keep_buf parameter. If keep_buf is false, the results |
| 1581 |
* are the same as for the tuple-not-found case. If keep_buf is true, |
| 1582 |
* then tuple->t_data and *userbuf are returned as for the success case, |
| 1583 |
* and again the caller must unpin the buffer; but false is returned. |
| 1584 |
* |
| 1585 |
* heap_fetch does not follow HOT chains: only the exact TID requested will |
| 1586 |
* be fetched. |
| 1587 |
* |
| 1588 |
* It is somewhat inconsistent that we ereport() on invalid block number but |
| 1589 |
* return false on invalid item number. There are a couple of reasons though. |
| 1590 |
* One is that the caller can relatively easily check the block number for |
| 1591 |
* validity, but cannot check the item number without reading the page |
| 1592 |
* himself. Another is that when we are following a t_ctid link, we can be |
| 1593 |
* reasonably confident that the page number is valid (since VACUUM shouldn't |
| 1594 |
* truncate off the destination page without having killed the referencing |
| 1595 |
* tuple first), but the item number might well not be good. |
| 1596 |
*/ |
| 1597 |
bool |
| 1598 |
heap_fetch(Relation relation, |
| 1599 |
Snapshot snapshot, |
| 1600 |
HeapTuple tuple, |
| 1601 |
Buffer *userbuf, |
| 1602 |
bool keep_buf) |
| 1603 |
{ |
| 1604 |
ItemPointer tid = &(tuple->t_self); |
| 1605 |
ItemId lp; |
| 1606 |
Buffer buffer; |
| 1607 |
Page page; |
| 1608 |
OffsetNumber offnum; |
| 1609 |
bool valid; |
| 1610 |
|
| 1611 |
/* |
| 1612 |
* Fetch and pin the appropriate page of the relation. |
| 1613 |
*/ |
| 1614 |
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); |
| 1615 |
|
| 1616 |
/* |
| 1617 |
* Need share lock on buffer to examine tuple commit status. |
| 1618 |
*/ |
| 1619 |
LockBuffer(buffer, BUFFER_LOCK_SHARE); |
| 1620 |
page = BufferGetPage(buffer); |
| 1621 |
|
| 1622 |
/* |
| 1623 |
* We'd better check for out-of-range offnum in case of VACUUM since the |
| 1624 |
* TID was obtained. |
| 1625 |
*/ |
| 1626 |
offnum = ItemPointerGetOffsetNumber(tid); |
| 1627 |
if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page)) |
| 1628 |
{ |
| 1629 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 1630 |
ReleaseBuffer(buffer); |
| 1631 |
*userbuf = InvalidBuffer; |
| 1632 |
tuple->t_data = NULL; |
| 1633 |
return false; |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* |
| 1637 |
* get the item line pointer corresponding to the requested tid |
| 1638 |
*/ |
| 1639 |
lp = PageGetItemId(page, offnum); |
| 1640 |
|
| 1641 |
/* |
| 1642 |
* Must check for deleted tuple. |
| 1643 |
*/ |
| 1644 |
if (!ItemIdIsNormal(lp)) |
| 1645 |
{ |
| 1646 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 1647 |
ReleaseBuffer(buffer); |
| 1648 |
*userbuf = InvalidBuffer; |
| 1649 |
tuple->t_data = NULL; |
| 1650 |
return false; |
| 1651 |
} |
| 1652 |
|
| 1653 |
/* |
| 1654 |
* fill in *tuple fields |
| 1655 |
*/ |
| 1656 |
tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 1657 |
tuple->t_len = ItemIdGetLength(lp); |
| 1658 |
tuple->t_tableOid = RelationGetRelid(relation); |
| 1659 |
|
| 1660 |
/* |
| 1661 |
* check tuple visibility, then release lock |
| 1662 |
*/ |
| 1663 |
valid = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer); |
| 1664 |
|
| 1665 |
if (valid) |
| 1666 |
PredicateLockTID(relation, &(tuple->t_self), snapshot, |
| 1667 |
HeapTupleHeaderGetXmin(tuple->t_data)); |
| 1668 |
|
| 1669 |
HeapCheckForSerializableConflictOut(valid, relation, tuple, buffer, snapshot); |
| 1670 |
|
| 1671 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 1672 |
|
| 1673 |
if (valid) |
| 1674 |
{ |
| 1675 |
/* |
| 1676 |
* All checks passed, so return the tuple as valid. Caller is now |
| 1677 |
* responsible for releasing the buffer. |
| 1678 |
*/ |
| 1679 |
*userbuf = buffer; |
| 1680 |
|
| 1681 |
return true; |
| 1682 |
} |
| 1683 |
|
| 1684 |
/* Tuple failed time qual, but maybe caller wants to see it anyway. */ |
| 1685 |
if (keep_buf) |
| 1686 |
*userbuf = buffer; |
| 1687 |
else |
| 1688 |
{ |
| 1689 |
ReleaseBuffer(buffer); |
| 1690 |
*userbuf = InvalidBuffer; |
| 1691 |
tuple->t_data = NULL; |
| 1692 |
} |
| 1693 |
|
| 1694 |
return false; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* |
| 1698 |
* heap_hot_search_buffer - search HOT chain for tuple satisfying snapshot |
| 1699 |
* |
| 1700 |
* On entry, *tid is the TID of a tuple (either a simple tuple, or the root |
| 1701 |
* of a HOT chain), and buffer is the buffer holding this tuple. We search |
| 1702 |
* for the first chain member satisfying the given snapshot. If one is |
| 1703 |
* found, we update *tid to reference that tuple's offset number, and |
| 1704 |
* return true. If no match, return false without modifying *tid. |
| 1705 |
* |
| 1706 |
* heapTuple is a caller-supplied buffer. When a match is found, we return |
| 1707 |
* the tuple here, in addition to updating *tid. If no match is found, the |
| 1708 |
* contents of this buffer on return are undefined. |
| 1709 |
* |
| 1710 |
* If all_dead is not NULL, we check non-visible tuples to see if they are |
| 1711 |
* globally dead; *all_dead is set true if all members of the HOT chain |
| 1712 |
* are vacuumable, false if not. |
| 1713 |
* |
| 1714 |
* Unlike heap_fetch, the caller must already have pin and (at least) share |
| 1715 |
* lock on the buffer; it is still pinned/locked at exit. |
| 1716 |
*/ |
| 1717 |
bool |
| 1718 |
heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, |
| 1719 |
Snapshot snapshot, HeapTuple heapTuple, |
| 1720 |
bool *all_dead, bool first_call) |
| 1721 |
{ |
| 1722 |
Page page = BufferGetPage(buffer); |
| 1723 |
TransactionId prev_xmax = InvalidTransactionId; |
| 1724 |
BlockNumber blkno; |
| 1725 |
OffsetNumber offnum; |
| 1726 |
bool at_chain_start; |
| 1727 |
bool valid; |
| 1728 |
bool skip; |
| 1729 |
GlobalVisState *vistest = NULL; |
| 1730 |
|
| 1731 |
/* If this is not the first call, previous call returned a (live!) tuple */ |
| 1732 |
if (all_dead) |
| 1733 |
*all_dead = first_call; |
| 1734 |
|
| 1735 |
blkno = ItemPointerGetBlockNumber(tid); |
| 1736 |
offnum = ItemPointerGetOffsetNumber(tid); |
| 1737 |
at_chain_start = first_call; |
| 1738 |
skip = !first_call; |
| 1739 |
|
| 1740 |
/* XXX: we should assert that a snapshot is pushed or registered */ |
| 1741 |
Assert(TransactionIdIsValid(RecentXmin)); |
| 1742 |
Assert(BufferGetBlockNumber(buffer) == blkno); |
| 1743 |
|
| 1744 |
/* Scan through possible multiple members of HOT-chain */ |
| 1745 |
for (;;) |
| 1746 |
{ |
| 1747 |
ItemId lp; |
| 1748 |
|
| 1749 |
/* check for bogus TID */ |
| 1750 |
if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page)) |
| 1751 |
break; |
| 1752 |
|
| 1753 |
lp = PageGetItemId(page, offnum); |
| 1754 |
|
| 1755 |
/* check for unused, dead, or redirected items */ |
| 1756 |
if (!ItemIdIsNormal(lp)) |
| 1757 |
{ |
| 1758 |
/* We should only see a redirect at start of chain */ |
| 1759 |
if (ItemIdIsRedirected(lp) && at_chain_start) |
| 1760 |
{ |
| 1761 |
/* Follow the redirect */ |
| 1762 |
offnum = ItemIdGetRedirect(lp); |
| 1763 |
at_chain_start = false; |
| 1764 |
continue; |
| 1765 |
} |
| 1766 |
/* else must be end of chain */ |
| 1767 |
break; |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* |
| 1771 |
* Update heapTuple to point to the element of the HOT chain we're |
| 1772 |
* currently investigating. Having t_self set correctly is important |
| 1773 |
* because the SSI checks and the *Satisfies routine for historical |
| 1774 |
* MVCC snapshots need the correct tid to decide about the visibility. |
| 1775 |
*/ |
| 1776 |
heapTuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 1777 |
heapTuple->t_len = ItemIdGetLength(lp); |
| 1778 |
heapTuple->t_tableOid = RelationGetRelid(relation); |
| 1779 |
ItemPointerSet(&heapTuple->t_self, blkno, offnum); |
| 1780 |
|
| 1781 |
/* |
| 1782 |
* Shouldn't see a HEAP_ONLY tuple at chain start. |
| 1783 |
*/ |
| 1784 |
if (at_chain_start && HeapTupleIsHeapOnly(heapTuple)) |
| 1785 |
break; |
| 1786 |
|
| 1787 |
/* |
| 1788 |
* The xmin should match the previous xmax value, else chain is |
| 1789 |
* broken. |
| 1790 |
*/ |
| 1791 |
if (TransactionIdIsValid(prev_xmax) && |
| 1792 |
!TransactionIdEquals(prev_xmax, |
| 1793 |
HeapTupleHeaderGetXmin(heapTuple->t_data))) |
| 1794 |
break; |
| 1795 |
|
| 1796 |
/* |
| 1797 |
* When first_call is true (and thus, skip is initially false) we'll |
| 1798 |
* return the first tuple we find. But on later passes, heapTuple |
| 1799 |
* will initially be pointing to the tuple we returned last time. |
| 1800 |
* Returning it again would be incorrect (and would loop forever), so |
| 1801 |
* we skip it and return the next match we find. |
| 1802 |
*/ |
| 1803 |
if (!skip) |
| 1804 |
{ |
| 1805 |
/* If it's visible per the snapshot, we must return it */ |
| 1806 |
valid = HeapTupleSatisfiesVisibility(heapTuple, snapshot, buffer); |
| 1807 |
HeapCheckForSerializableConflictOut(valid, relation, heapTuple, |
| 1808 |
buffer, snapshot); |
| 1809 |
|
| 1810 |
if (valid) |
| 1811 |
{ |
| 1812 |
ItemPointerSetOffsetNumber(tid, offnum); |
| 1813 |
PredicateLockTID(relation, &heapTuple->t_self, snapshot, |
| 1814 |
HeapTupleHeaderGetXmin(heapTuple->t_data)); |
| 1815 |
if (all_dead) |
| 1816 |
*all_dead = false; |
| 1817 |
return true; |
| 1818 |
} |
| 1819 |
} |
| 1820 |
skip = false; |
| 1821 |
|
| 1822 |
/* |
| 1823 |
* If we can't see it, maybe no one else can either. At caller |
| 1824 |
* request, check whether all chain members are dead to all |
| 1825 |
* transactions. |
| 1826 |
* |
| 1827 |
* Note: if you change the criterion here for what is "dead", fix the |
| 1828 |
* planner's get_actual_variable_range() function to match. |
| 1829 |
*/ |
| 1830 |
if (all_dead && *all_dead) |
| 1831 |
{ |
| 1832 |
if (!vistest) |
| 1833 |
vistest = GlobalVisTestFor(relation); |
| 1834 |
|
| 1835 |
if (!HeapTupleIsSurelyDead(heapTuple, vistest)) |
| 1836 |
*all_dead = false; |
| 1837 |
} |
| 1838 |
|
| 1839 |
/* |
| 1840 |
* Check to see if HOT chain continues past this tuple; if so fetch |
| 1841 |
* the next offnum and loop around. |
| 1842 |
*/ |
| 1843 |
if (HeapTupleIsHotUpdated(heapTuple)) |
| 1844 |
{ |
| 1845 |
Assert(ItemPointerGetBlockNumber(&heapTuple->t_data->t_ctid) == |
| 1846 |
blkno); |
| 1847 |
offnum = ItemPointerGetOffsetNumber(&heapTuple->t_data->t_ctid); |
| 1848 |
at_chain_start = false; |
| 1849 |
prev_xmax = HeapTupleHeaderGetUpdateXid(heapTuple->t_data); |
| 1850 |
} |
| 1851 |
else |
| 1852 |
break; /* end of chain */ |
| 1853 |
} |
| 1854 |
|
| 1855 |
return false; |
| 1856 |
} |
| 1857 |
|
| 1858 |
/* |
| 1859 |
* heap_get_latest_tid - get the latest tid of a specified tuple |
| 1860 |
* |
| 1861 |
* Actually, this gets the latest version that is visible according to the |
| 1862 |
* scan's snapshot. Create a scan using SnapshotDirty to get the very latest, |
| 1863 |
* possibly uncommitted version. |
| 1864 |
* |
| 1865 |
* *tid is both an input and an output parameter: it is updated to |
| 1866 |
* show the latest version of the row. Note that it will not be changed |
| 1867 |
* if no version of the row passes the snapshot test. |
| 1868 |
*/ |
| 1869 |
void |
| 1870 |
heap_get_latest_tid(TableScanDesc sscan, |
| 1871 |
ItemPointer tid) |
| 1872 |
{ |
| 1873 |
Relation relation = sscan->rs_rd; |
| 1874 |
Snapshot snapshot = sscan->rs_snapshot; |
| 1875 |
ItemPointerData ctid; |
| 1876 |
TransactionId priorXmax; |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* table_tuple_get_latest_tid() verified that the passed in tid is valid. |
| 1880 |
* Assume that t_ctid links are valid however - there shouldn't be invalid |
| 1881 |
* ones in the table. |
| 1882 |
*/ |
| 1883 |
Assert(ItemPointerIsValid(tid)); |
| 1884 |
|
| 1885 |
/* |
| 1886 |
* Loop to chase down t_ctid links. At top of loop, ctid is the tuple we |
| 1887 |
* need to examine, and *tid is the TID we will return if ctid turns out |
| 1888 |
* to be bogus. |
| 1889 |
* |
| 1890 |
* Note that we will loop until we reach the end of the t_ctid chain. |
| 1891 |
* Depending on the snapshot passed, there might be at most one visible |
| 1892 |
* version of the row, but we don't try to optimize for that. |
| 1893 |
*/ |
| 1894 |
ctid = *tid; |
| 1895 |
priorXmax = InvalidTransactionId; /* cannot check first XMIN */ |
| 1896 |
for (;;) |
| 1897 |
{ |
| 1898 |
Buffer buffer; |
| 1899 |
Page page; |
| 1900 |
OffsetNumber offnum; |
| 1901 |
ItemId lp; |
| 1902 |
HeapTupleData tp; |
| 1903 |
bool valid; |
| 1904 |
|
| 1905 |
/* |
| 1906 |
* Read, pin, and lock the page. |
| 1907 |
*/ |
| 1908 |
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&ctid)); |
| 1909 |
LockBuffer(buffer, BUFFER_LOCK_SHARE); |
| 1910 |
page = BufferGetPage(buffer); |
| 1911 |
|
| 1912 |
/* |
| 1913 |
* Check for bogus item number. This is not treated as an error |
| 1914 |
* condition because it can happen while following a t_ctid link. We |
| 1915 |
* just assume that the prior tid is OK and return it unchanged. |
| 1916 |
*/ |
| 1917 |
offnum = ItemPointerGetOffsetNumber(&ctid); |
| 1918 |
if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page)) |
| 1919 |
{ |
| 1920 |
UnlockReleaseBuffer(buffer); |
| 1921 |
break; |
| 1922 |
} |
| 1923 |
lp = PageGetItemId(page, offnum); |
| 1924 |
if (!ItemIdIsNormal(lp)) |
| 1925 |
{ |
| 1926 |
UnlockReleaseBuffer(buffer); |
| 1927 |
break; |
| 1928 |
} |
| 1929 |
|
| 1930 |
/* OK to access the tuple */ |
| 1931 |
tp.t_self = ctid; |
| 1932 |
tp.t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 1933 |
tp.t_len = ItemIdGetLength(lp); |
| 1934 |
tp.t_tableOid = RelationGetRelid(relation); |
| 1935 |
|
| 1936 |
/* |
| 1937 |
* After following a t_ctid link, we might arrive at an unrelated |
| 1938 |
* tuple. Check for XMIN match. |
| 1939 |
*/ |
| 1940 |
if (TransactionIdIsValid(priorXmax) && |
| 1941 |
!TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(tp.t_data))) |
| 1942 |
{ |
| 1943 |
UnlockReleaseBuffer(buffer); |
| 1944 |
break; |
| 1945 |
} |
| 1946 |
|
| 1947 |
/* |
| 1948 |
* Check tuple visibility; if visible, set it as the new result |
| 1949 |
* candidate. |
| 1950 |
*/ |
| 1951 |
valid = HeapTupleSatisfiesVisibility(&tp, snapshot, buffer); |
| 1952 |
HeapCheckForSerializableConflictOut(valid, relation, &tp, buffer, snapshot); |
| 1953 |
if (valid) |
| 1954 |
*tid = ctid; |
| 1955 |
|
| 1956 |
/* |
| 1957 |
* If there's a valid t_ctid link, follow it, else we're done. |
| 1958 |
*/ |
| 1959 |
if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) || |
| 1960 |
HeapTupleHeaderIsOnlyLocked(tp.t_data) || |
| 1961 |
HeapTupleHeaderIndicatesMovedPartitions(tp.t_data) || |
| 1962 |
ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid)) |
| 1963 |
{ |
| 1964 |
UnlockReleaseBuffer(buffer); |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
|
| 1968 |
ctid = tp.t_data->t_ctid; |
| 1969 |
priorXmax = HeapTupleHeaderGetUpdateXid(tp.t_data); |
| 1970 |
UnlockReleaseBuffer(buffer); |
| 1971 |
} /* end of loop */ |
| 1972 |
} |
| 1973 |
|
| 1974 |
|
| 1975 |
/* |
| 1976 |
* UpdateXmaxHintBits - update tuple hint bits after xmax transaction ends |
| 1977 |
* |
| 1978 |
* This is called after we have waited for the XMAX transaction to terminate. |
| 1979 |
* If the transaction aborted, we guarantee the XMAX_INVALID hint bit will |
| 1980 |
* be set on exit. If the transaction committed, we set the XMAX_COMMITTED |
| 1981 |
* hint bit if possible --- but beware that that may not yet be possible, |
| 1982 |
* if the transaction committed asynchronously. |
| 1983 |
* |
| 1984 |
* Note that if the transaction was a locker only, we set HEAP_XMAX_INVALID |
| 1985 |
* even if it commits. |
| 1986 |
* |
| 1987 |
* Hence callers should look only at XMAX_INVALID. |
| 1988 |
* |
| 1989 |
* Note this is not allowed for tuples whose xmax is a multixact. |
| 1990 |
*/ |
| 1991 |
static void |
| 1992 |
UpdateXmaxHintBits(HeapTupleHeader tuple, Buffer buffer, TransactionId xid) |
| 1993 |
{ |
| 1994 |
Assert(TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple), xid)); |
| 1995 |
Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI)); |
| 1996 |
|
| 1997 |
if (!(tuple->t_infomask & (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID))) |
| 1998 |
{ |
| 1999 |
if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask) && |
| 2000 |
TransactionIdDidCommit(xid)) |
| 2001 |
HeapTupleSetHintBits(tuple, buffer, HEAP_XMAX_COMMITTED, |
| 2002 |
xid); |
| 2003 |
else |
| 2004 |
HeapTupleSetHintBits(tuple, buffer, HEAP_XMAX_INVALID, |
| 2005 |
InvalidTransactionId); |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
/* |
| 2011 |
* GetBulkInsertState - prepare status object for a bulk insert |
| 2012 |
*/ |
| 2013 |
BulkInsertState |
| 2014 |
GetBulkInsertState(void) |
| 2015 |
{ |
| 2016 |
BulkInsertState bistate; |
| 2017 |
|
| 2018 |
bistate = (BulkInsertState) palloc(sizeof(BulkInsertStateData)); |
| 2019 |
bistate->strategy = GetAccessStrategy(BAS_BULKWRITE); |
| 2020 |
bistate->current_buf = InvalidBuffer; |
| 2021 |
bistate->next_free = InvalidBlockNumber; |
| 2022 |
bistate->last_free = InvalidBlockNumber; |
| 2023 |
bistate->already_extended_by = 0; |
| 2024 |
return bistate; |
| 2025 |
} |
| 2026 |
|
| 2027 |
/* |
| 2028 |
* FreeBulkInsertState - clean up after finishing a bulk insert |
| 2029 |
*/ |
| 2030 |
void |
| 2031 |
FreeBulkInsertState(BulkInsertState bistate) |
| 2032 |
{ |
| 2033 |
if (bistate->current_buf != InvalidBuffer) |
| 2034 |
ReleaseBuffer(bistate->current_buf); |
| 2035 |
FreeAccessStrategy(bistate->strategy); |
| 2036 |
pfree(bistate); |
| 2037 |
} |
| 2038 |
|
| 2039 |
/* |
| 2040 |
* ReleaseBulkInsertStatePin - release a buffer currently held in bistate |
| 2041 |
*/ |
| 2042 |
void |
| 2043 |
ReleaseBulkInsertStatePin(BulkInsertState bistate) |
| 2044 |
{ |
| 2045 |
if (bistate->current_buf != InvalidBuffer) |
| 2046 |
ReleaseBuffer(bistate->current_buf); |
| 2047 |
bistate->current_buf = InvalidBuffer; |
| 2048 |
|
| 2049 |
/* |
| 2050 |
* Despite the name, we also reset bulk relation extension state. |
| 2051 |
* Otherwise we can end up erroring out due to looking for free space in |
| 2052 |
* ->next_free of one partition, even though ->next_free was set when |
| 2053 |
* extending another partition. It could obviously also be bad for |
| 2054 |
* efficiency to look at existing blocks at offsets from another |
| 2055 |
* partition, even if we don't error out. |
| 2056 |
*/ |
| 2057 |
bistate->next_free = InvalidBlockNumber; |
| 2058 |
bistate->last_free = InvalidBlockNumber; |
| 2059 |
} |
| 2060 |
|
| 2061 |
|
| 2062 |
/* |
| 2063 |
* heap_insert - insert tuple into a heap |
| 2064 |
* |
| 2065 |
* The new tuple is stamped with current transaction ID and the specified |
| 2066 |
* command ID. |
| 2067 |
* |
| 2068 |
* See table_tuple_insert for comments about most of the input flags, except |
| 2069 |
* that this routine directly takes a tuple rather than a slot. |
| 2070 |
* |
| 2071 |
* There's corresponding HEAP_INSERT_ options to all the TABLE_INSERT_ |
| 2072 |
* options, and there additionally is HEAP_INSERT_SPECULATIVE which is used to |
| 2073 |
* implement table_tuple_insert_speculative(). |
| 2074 |
* |
| 2075 |
* On return the header fields of *tup are updated to match the stored tuple; |
| 2076 |
* in particular tup->t_self receives the actual TID where the tuple was |
| 2077 |
* stored. But note that any toasting of fields within the tuple data is NOT |
| 2078 |
* reflected into *tup. |
| 2079 |
*/ |
| 2080 |
void |
| 2081 |
heap_insert(Relation relation, HeapTuple tup, CommandId cid, |
| 2082 |
int options, BulkInsertState bistate) |
| 2083 |
{ |
| 2084 |
TransactionId xid = GetCurrentTransactionId(); |
| 2085 |
HeapTuple heaptup; |
| 2086 |
Buffer buffer; |
| 2087 |
Page page; |
| 2088 |
Buffer vmbuffer = InvalidBuffer; |
| 2089 |
bool clear_all_visible = false; |
| 2090 |
bool vmbuffer_modified = false; |
| 2091 |
|
| 2092 |
/* Cheap, simplistic check that the tuple matches the rel's rowtype. */ |
| 2093 |
Assert(HeapTupleHeaderGetNatts(tup->t_data) <= |
| 2094 |
RelationGetNumberOfAttributes(relation)); |
| 2095 |
|
| 2096 |
AssertHasSnapshotForToast(relation); |
| 2097 |
|
| 2098 |
/* |
| 2099 |
* Fill in tuple header fields and toast the tuple if necessary. |
| 2100 |
* |
| 2101 |
* Note: below this point, heaptup is the data we actually intend to store |
| 2102 |
* into the relation; tup is the caller's original untoasted data. |
| 2103 |
*/ |
| 2104 |
heaptup = heap_prepare_insert(relation, tup, xid, cid, options); |
| 2105 |
|
| 2106 |
/* |
| 2107 |
* Find buffer to insert this tuple into. If the page is all visible, |
| 2108 |
* this will also pin the requisite visibility map page. |
| 2109 |
*/ |
| 2110 |
buffer = RelationGetBufferForTuple(relation, heaptup->t_len, |
| 2111 |
InvalidBuffer, options, bistate, |
| 2112 |
&vmbuffer, NULL, |
| 2113 |
0); |
| 2114 |
page = BufferGetPage(buffer); |
| 2115 |
|
| 2116 |
/* |
| 2117 |
* We're about to do the actual insert -- but check for conflict first, to |
| 2118 |
* avoid possibly having to roll back work we've just done. |
| 2119 |
* |
| 2120 |
* This is safe without a recheck as long as there is no possibility of |
| 2121 |
* another process scanning the page between this check and the insert |
| 2122 |
* being visible to the scan (i.e., an exclusive buffer content lock is |
| 2123 |
* continuously held from this point until the tuple insert is visible). |
| 2124 |
* |
| 2125 |
* For a heap insert, we only need to check for table-level SSI locks. Our |
| 2126 |
* new tuple can't possibly conflict with existing tuple locks, and heap |
| 2127 |
* page locks are only consolidated versions of tuple locks; they do not |
| 2128 |
* lock "gaps" as index page locks do. So we don't need to specify a |
| 2129 |
* buffer when making the call, which makes for a faster check. |
| 2130 |
*/ |
| 2131 |
CheckForSerializableConflictIn(relation, NULL, InvalidBlockNumber); |
| 2132 |
|
| 2133 |
/* Lock the vmbuffer before the critical section */ |
| 2134 |
if (PageIsAllVisible(page)) |
| 2135 |
{ |
| 2136 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 2137 |
clear_all_visible = true; |
| 2138 |
} |
| 2139 |
|
| 2140 |
/* NO EREPORT(ERROR) from here till changes are logged */ |
| 2141 |
START_CRIT_SECTION(); |
| 2142 |
|
| 2143 |
RelationPutHeapTuple(relation, buffer, heaptup, |
| 2144 |
(options & HEAP_INSERT_SPECULATIVE) != 0); |
| 2145 |
|
| 2146 |
if (clear_all_visible) |
| 2147 |
{ |
| 2148 |
/* It's possible the VM bits were already clear */ |
| 2149 |
if (visibilitymap_clear_locked(relation, |
| 2150 |
ItemPointerGetBlockNumber(&(heaptup->t_self)), |
| 2151 |
vmbuffer, VISIBILITYMAP_VALID_BITS)) |
| 2152 |
vmbuffer_modified = true; |
| 2153 |
|
| 2154 |
PageClearAllVisible(page); |
| 2155 |
} |
| 2156 |
|
| 2157 |
/* |
| 2158 |
* XXX Should we set PageSetPrunable on this page ? |
| 2159 |
* |
| 2160 |
* The inserting transaction may eventually abort thus making this tuple |
| 2161 |
* DEAD and hence available for pruning. Though we don't want to optimize |
| 2162 |
* for aborts, if no other tuple in this page is UPDATEd/DELETEd, the |
| 2163 |
* aborted tuple will never be pruned until next vacuum is triggered. |
| 2164 |
* |
| 2165 |
* If you do add PageSetPrunable here, add it in heap_xlog_insert too. |
| 2166 |
*/ |
| 2167 |
|
| 2168 |
MarkBufferDirty(buffer); |
| 2169 |
|
| 2170 |
/* XLOG stuff */ |
| 2171 |
if (RelationNeedsWAL(relation)) |
| 2172 |
{ |
| 2173 |
xl_heap_insert xlrec; |
| 2174 |
xl_heap_header xlhdr; |
| 2175 |
XLogRecPtr recptr; |
| 2176 |
uint8 info = XLOG_HEAP_INSERT; |
| 2177 |
int bufflags = 0; |
| 2178 |
|
| 2179 |
/* |
| 2180 |
* If this is a catalog, we need to transmit combo CIDs to properly |
| 2181 |
* decode, so log that as well. |
| 2182 |
*/ |
| 2183 |
if (RelationIsAccessibleInLogicalDecoding(relation)) |
| 2184 |
log_heap_new_cid(relation, heaptup); |
| 2185 |
|
| 2186 |
/* |
| 2187 |
* If this is the single and first tuple on page, we can reinit the |
| 2188 |
* page instead of restoring the whole thing. Set flag, and hide |
| 2189 |
* buffer references from XLogInsert. |
| 2190 |
*/ |
| 2191 |
if (ItemPointerGetOffsetNumber(&(heaptup->t_self)) == FirstOffsetNumber && |
| 2192 |
PageGetMaxOffsetNumber(page) == FirstOffsetNumber) |
| 2193 |
{ |
| 2194 |
info |= XLOG_HEAP_INIT_PAGE; |
| 2195 |
bufflags |= REGBUF_WILL_INIT; |
| 2196 |
} |
| 2197 |
|
| 2198 |
xlrec.offnum = ItemPointerGetOffsetNumber(&heaptup->t_self); |
| 2199 |
xlrec.flags = 0; |
| 2200 |
if (clear_all_visible) |
| 2201 |
xlrec.flags |= XLH_INSERT_ALL_VISIBLE_CLEARED; |
| 2202 |
if (options & HEAP_INSERT_SPECULATIVE) |
| 2203 |
xlrec.flags |= XLH_INSERT_IS_SPECULATIVE; |
| 2204 |
Assert(ItemPointerGetBlockNumber(&heaptup->t_self) == BufferGetBlockNumber(buffer)); |
| 2205 |
|
| 2206 |
/* |
| 2207 |
* For logical decoding, we need the tuple even if we're doing a full |
| 2208 |
* page write, so make sure it's included even if we take a full-page |
| 2209 |
* image. (XXX We could alternatively store a pointer into the FPW). |
| 2210 |
*/ |
| 2211 |
if (RelationIsLogicallyLogged(relation) && |
| 2212 |
!(options & HEAP_INSERT_NO_LOGICAL)) |
| 2213 |
{ |
| 2214 |
xlrec.flags |= XLH_INSERT_CONTAINS_NEW_TUPLE; |
| 2215 |
bufflags |= REGBUF_KEEP_DATA; |
| 2216 |
|
| 2217 |
if (IsToastRelation(relation)) |
| 2218 |
xlrec.flags |= XLH_INSERT_ON_TOAST_RELATION; |
| 2219 |
} |
| 2220 |
|
| 2221 |
XLogBeginInsert(); |
| 2222 |
XLogRegisterData(&xlrec, SizeOfHeapInsert); |
| 2223 |
|
| 2224 |
xlhdr.t_infomask2 = heaptup->t_data->t_infomask2; |
| 2225 |
xlhdr.t_infomask = heaptup->t_data->t_infomask; |
| 2226 |
xlhdr.t_hoff = heaptup->t_data->t_hoff; |
| 2227 |
|
| 2228 |
/* |
| 2229 |
* note we mark xlhdr as belonging to buffer; if XLogInsert decides to |
| 2230 |
* write the whole page to the xlog, we don't need to store |
| 2231 |
* xl_heap_header in the xlog. |
| 2232 |
*/ |
| 2233 |
XLogRegisterBuffer(HEAP_INSERT_BLKREF_HEAP, buffer, |
| 2234 |
REGBUF_STANDARD | bufflags); |
| 2235 |
XLogRegisterBufData(HEAP_INSERT_BLKREF_HEAP, &xlhdr, |
| 2236 |
SizeOfHeapHeader); |
| 2237 |
/* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */ |
| 2238 |
XLogRegisterBufData(HEAP_INSERT_BLKREF_HEAP, |
| 2239 |
(char *) heaptup->t_data + SizeofHeapTupleHeader, |
| 2240 |
heaptup->t_len - SizeofHeapTupleHeader); |
| 2241 |
|
| 2242 |
/* filtering by origin on a row level is much more efficient */ |
| 2243 |
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); |
| 2244 |
|
| 2245 |
if (vmbuffer_modified) |
| 2246 |
XLogRegisterBuffer(HEAP_INSERT_BLKREF_VM, vmbuffer, 0); |
| 2247 |
|
| 2248 |
recptr = XLogInsert(RM_HEAP_ID, info); |
| 2249 |
|
| 2250 |
PageSetLSN(page, recptr); |
| 2251 |
|
| 2252 |
if (vmbuffer_modified) |
| 2253 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 2254 |
} |
| 2255 |
|
| 2256 |
END_CRIT_SECTION(); |
| 2257 |
|
| 2258 |
UnlockReleaseBuffer(buffer); |
| 2259 |
|
| 2260 |
/* |
| 2261 |
* We locked vmbuffer if clear_all_visible was true regardless of whether |
| 2262 |
* or not we ended up modifying the vmbuffer. |
| 2263 |
*/ |
| 2264 |
if (clear_all_visible) |
| 2265 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 2266 |
if (BufferIsValid(vmbuffer)) |
| 2267 |
ReleaseBuffer(vmbuffer); |
| 2268 |
|
| 2269 |
/* |
| 2270 |
* If tuple is cachable, mark it for invalidation from the caches in case |
| 2271 |
* we abort. Note it is OK to do this after releasing the buffer, because |
| 2272 |
* the heaptup data structure is all in local memory, not in the shared |
| 2273 |
* buffer. |
| 2274 |
*/ |
| 2275 |
CacheInvalidateHeapTuple(relation, heaptup, NULL); |
| 2276 |
|
| 2277 |
/* Note: speculative insertions are counted too, even if aborted later */ |
| 2278 |
pgstat_count_heap_insert(relation, 1); |
| 2279 |
|
| 2280 |
/* |
| 2281 |
* If heaptup is a private copy, release it. Don't forget to copy t_self |
| 2282 |
* back to the caller's image, too. |
| 2283 |
*/ |
| 2284 |
if (heaptup != tup) |
| 2285 |
{ |
| 2286 |
tup->t_self = heaptup->t_self; |
| 2287 |
heap_freetuple(heaptup); |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* |
| 2292 |
* Subroutine for heap_insert(). Prepares a tuple for insertion. This sets the |
| 2293 |
* tuple header fields and toasts the tuple if necessary. Returns a toasted |
| 2294 |
* version of the tuple if it was toasted, or the original tuple if not. Note |
| 2295 |
* that in any case, the header fields are also set in the original tuple. |
| 2296 |
*/ |
| 2297 |
static HeapTuple |
| 2298 |
heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid, |
| 2299 |
CommandId cid, int options) |
| 2300 |
{ |
| 2301 |
/* |
| 2302 |
* To allow parallel inserts, we need to ensure that they are safe to be |
| 2303 |
* performed in workers. We have the infrastructure to allow parallel |
| 2304 |
* inserts in general except for the cases where inserts generate a new |
| 2305 |
* CommandId (eg. inserts into a table having a foreign key column). |
| 2306 |
*/ |
| 2307 |
if (IsParallelWorker()) |
| 2308 |
ereport(ERROR, |
| 2309 |
(errcode(ERRCODE_INVALID_TRANSACTION_STATE), |
| 2310 |
errmsg("cannot insert tuples in a parallel worker"))); |
| 2311 |
|
| 2312 |
tup->t_data->t_infomask &= ~(HEAP_XACT_MASK); |
| 2313 |
tup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK); |
| 2314 |
tup->t_data->t_infomask |= HEAP_XMAX_INVALID; |
| 2315 |
HeapTupleHeaderSetXmin(tup->t_data, xid); |
| 2316 |
if (options & HEAP_INSERT_FROZEN) |
| 2317 |
HeapTupleHeaderSetXminFrozen(tup->t_data); |
| 2318 |
|
| 2319 |
HeapTupleHeaderSetCmin(tup->t_data, cid); |
| 2320 |
HeapTupleHeaderSetXmax(tup->t_data, 0); /* for cleanliness */ |
| 2321 |
tup->t_tableOid = RelationGetRelid(relation); |
| 2322 |
|
| 2323 |
/* |
| 2324 |
* If the new tuple is too big for storage or contains already toasted |
| 2325 |
* out-of-line attributes from some other relation, invoke the toaster. |
| 2326 |
*/ |
| 2327 |
if (relation->rd_rel->relkind != RELKIND_RELATION && |
| 2328 |
relation->rd_rel->relkind != RELKIND_MATVIEW) |
| 2329 |
{ |
| 2330 |
/* toast table entries should never be recursively toasted */ |
| 2331 |
Assert(!HeapTupleHasExternal(tup)); |
| 2332 |
return tup; |
| 2333 |
} |
| 2334 |
else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD) |
| 2335 |
return heap_toast_insert_or_update(relation, tup, NULL, options); |
| 2336 |
else |
| 2337 |
return tup; |
| 2338 |
} |
| 2339 |
|
| 2340 |
/* |
| 2341 |
* Helper for heap_multi_insert() that computes the number of entire pages |
| 2342 |
* that inserting the remaining heaptuples requires. Used to determine how |
| 2343 |
* much the relation needs to be extended by. |
| 2344 |
*/ |
| 2345 |
static int |
| 2346 |
heap_multi_insert_pages(HeapTuple *heaptuples, int done, int ntuples, Size saveFreeSpace) |
| 2347 |
{ |
| 2348 |
size_t page_avail = BLCKSZ - SizeOfPageHeaderData - saveFreeSpace; |
| 2349 |
int npages = 1; |
| 2350 |
|
| 2351 |
for (int i = done; i < ntuples; i++) |
| 2352 |
{ |
| 2353 |
size_t tup_sz = sizeof(ItemIdData) + MAXALIGN(heaptuples[i]->t_len); |
| 2354 |
|
| 2355 |
if (page_avail < tup_sz) |
| 2356 |
{ |
| 2357 |
npages++; |
| 2358 |
page_avail = BLCKSZ - SizeOfPageHeaderData - saveFreeSpace; |
| 2359 |
} |
| 2360 |
page_avail -= tup_sz; |
| 2361 |
} |
| 2362 |
|
| 2363 |
return npages; |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* |
| 2367 |
* heap_multi_insert - insert multiple tuples into a heap |
| 2368 |
* |
| 2369 |
* This is like heap_insert(), but inserts multiple tuples in one operation. |
| 2370 |
* That's faster than calling heap_insert() in a loop, because when multiple |
| 2371 |
* tuples can be inserted on a single page, we can write just a single WAL |
| 2372 |
* record covering all of them, and only need to lock/unlock the page once. |
| 2373 |
* |
| 2374 |
* Note: this leaks memory into the current memory context. You can create a |
| 2375 |
* temporary context before calling this, if that's a problem. |
| 2376 |
*/ |
| 2377 |
void |
| 2378 |
heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples, |
| 2379 |
CommandId cid, int options, BulkInsertState bistate) |
| 2380 |
{ |
| 2381 |
TransactionId xid = GetCurrentTransactionId(); |
| 2382 |
HeapTuple *heaptuples; |
| 2383 |
int i; |
| 2384 |
int ndone; |
| 2385 |
PGAlignedBlock scratch; |
| 2386 |
Page page; |
| 2387 |
Buffer vmbuffer = InvalidBuffer; |
| 2388 |
bool needwal; |
| 2389 |
Size saveFreeSpace; |
| 2390 |
bool need_tuple_data = RelationIsLogicallyLogged(relation); |
| 2391 |
bool need_cids = RelationIsAccessibleInLogicalDecoding(relation); |
| 2392 |
bool starting_with_empty_page = false; |
| 2393 |
int npages = 0; |
| 2394 |
int npages_used = 0; |
| 2395 |
|
| 2396 |
/* currently not needed (thus unsupported) for heap_multi_insert() */ |
| 2397 |
Assert(!(options & HEAP_INSERT_NO_LOGICAL)); |
| 2398 |
|
| 2399 |
AssertHasSnapshotForToast(relation); |
| 2400 |
|
| 2401 |
needwal = RelationNeedsWAL(relation); |
| 2402 |
saveFreeSpace = RelationGetTargetPageFreeSpace(relation, |
| 2403 |
HEAP_DEFAULT_FILLFACTOR); |
| 2404 |
|
| 2405 |
/* Toast and set header data in all the slots */ |
| 2406 |
heaptuples = palloc(ntuples * sizeof(HeapTuple)); |
| 2407 |
for (i = 0; i < ntuples; i++) |
| 2408 |
{ |
| 2409 |
HeapTuple tuple; |
| 2410 |
|
| 2411 |
tuple = ExecFetchSlotHeapTuple(slots[i], true, NULL); |
| 2412 |
slots[i]->tts_tableOid = RelationGetRelid(relation); |
| 2413 |
tuple->t_tableOid = slots[i]->tts_tableOid; |
| 2414 |
heaptuples[i] = heap_prepare_insert(relation, tuple, xid, cid, |
| 2415 |
options); |
| 2416 |
} |
| 2417 |
|
| 2418 |
/* |
| 2419 |
* We're about to do the actual inserts -- but check for conflict first, |
| 2420 |
* to minimize the possibility of having to roll back work we've just |
| 2421 |
* done. |
| 2422 |
* |
| 2423 |
* A check here does not definitively prevent a serialization anomaly; |
| 2424 |
* that check MUST be done at least past the point of acquiring an |
| 2425 |
* exclusive buffer content lock on every buffer that will be affected, |
| 2426 |
* and MAY be done after all inserts are reflected in the buffers and |
| 2427 |
* those locks are released; otherwise there is a race condition. Since |
| 2428 |
* multiple buffers can be locked and unlocked in the loop below, and it |
| 2429 |
* would not be feasible to identify and lock all of those buffers before |
| 2430 |
* the loop, we must do a final check at the end. |
| 2431 |
* |
| 2432 |
* The check here could be omitted with no loss of correctness; it is |
| 2433 |
* present strictly as an optimization. |
| 2434 |
* |
| 2435 |
* For heap inserts, we only need to check for table-level SSI locks. Our |
| 2436 |
* new tuples can't possibly conflict with existing tuple locks, and heap |
| 2437 |
* page locks are only consolidated versions of tuple locks; they do not |
| 2438 |
* lock "gaps" as index page locks do. So we don't need to specify a |
| 2439 |
* buffer when making the call, which makes for a faster check. |
| 2440 |
*/ |
| 2441 |
CheckForSerializableConflictIn(relation, NULL, InvalidBlockNumber); |
| 2442 |
|
| 2443 |
ndone = 0; |
| 2444 |
while (ndone < ntuples) |
| 2445 |
{ |
| 2446 |
Buffer buffer; |
| 2447 |
bool clear_all_visible = false; |
| 2448 |
bool all_frozen_set = false; |
| 2449 |
bool vmbuffer_modified = false; |
| 2450 |
int nthispage; |
| 2451 |
|
| 2452 |
CHECK_FOR_INTERRUPTS(); |
| 2453 |
|
| 2454 |
/* |
| 2455 |
* Compute number of pages needed to fit the to-be-inserted tuples in |
| 2456 |
* the worst case. This will be used to determine how much to extend |
| 2457 |
* the relation by in RelationGetBufferForTuple(), if needed. If we |
| 2458 |
* filled a prior page from scratch, we can just update our last |
| 2459 |
* computation, but if we started with a partially filled page, |
| 2460 |
* recompute from scratch, the number of potentially required pages |
| 2461 |
* can vary due to tuples needing to fit onto the page, page headers |
| 2462 |
* etc. |
| 2463 |
*/ |
| 2464 |
if (ndone == 0 || !starting_with_empty_page) |
| 2465 |
{ |
| 2466 |
npages = heap_multi_insert_pages(heaptuples, ndone, ntuples, |
| 2467 |
saveFreeSpace); |
| 2468 |
npages_used = 0; |
| 2469 |
} |
| 2470 |
else |
| 2471 |
npages_used++; |
| 2472 |
|
| 2473 |
/* |
| 2474 |
* Find buffer where at least the next tuple will fit. If the page is |
| 2475 |
* all-visible, this will also pin the requisite visibility map page. |
| 2476 |
* |
| 2477 |
* Also pin visibility map page if COPY FREEZE inserts tuples into an |
| 2478 |
* empty page. See all_frozen_set below. |
| 2479 |
*/ |
| 2480 |
buffer = RelationGetBufferForTuple(relation, heaptuples[ndone]->t_len, |
| 2481 |
InvalidBuffer, options, bistate, |
| 2482 |
&vmbuffer, NULL, |
| 2483 |
npages - npages_used); |
| 2484 |
page = BufferGetPage(buffer); |
| 2485 |
|
| 2486 |
starting_with_empty_page = PageGetMaxOffsetNumber(page) == 0; |
| 2487 |
|
| 2488 |
if (starting_with_empty_page && (options & HEAP_INSERT_FROZEN)) |
| 2489 |
all_frozen_set = true; |
| 2490 |
|
| 2491 |
/* |
| 2492 |
* If clearing all-visible, take the VM buffer lock before entering |
| 2493 |
* the critical section where that action will be WAL-logged. Setting |
| 2494 |
* the VM all-frozen is done and WAL-logged separately. |
| 2495 |
*/ |
| 2496 |
if (PageIsAllVisible(page) && !(options & HEAP_INSERT_FROZEN)) |
| 2497 |
{ |
| 2498 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 2499 |
clear_all_visible = true; |
| 2500 |
} |
| 2501 |
|
| 2502 |
/* NO EREPORT(ERROR) from here till changes are logged */ |
| 2503 |
START_CRIT_SECTION(); |
| 2504 |
|
| 2505 |
/* |
| 2506 |
* RelationGetBufferForTuple has ensured that the first tuple fits. |
| 2507 |
* Put that on the page, and then as many other tuples as fit. |
| 2508 |
*/ |
| 2509 |
RelationPutHeapTuple(relation, buffer, heaptuples[ndone], false); |
| 2510 |
|
| 2511 |
/* |
| 2512 |
* For logical decoding we need combo CIDs to properly decode the |
| 2513 |
* catalog. |
| 2514 |
*/ |
| 2515 |
if (needwal && need_cids) |
| 2516 |
log_heap_new_cid(relation, heaptuples[ndone]); |
| 2517 |
|
| 2518 |
for (nthispage = 1; ndone + nthispage < ntuples; nthispage++) |
| 2519 |
{ |
| 2520 |
HeapTuple heaptup = heaptuples[ndone + nthispage]; |
| 2521 |
|
| 2522 |
if (PageGetHeapFreeSpace(page) < MAXALIGN(heaptup->t_len) + saveFreeSpace) |
| 2523 |
break; |
| 2524 |
|
| 2525 |
RelationPutHeapTuple(relation, buffer, heaptup, false); |
| 2526 |
|
| 2527 |
/* |
| 2528 |
* For logical decoding we need combo CIDs to properly decode the |
| 2529 |
* catalog. |
| 2530 |
*/ |
| 2531 |
if (needwal && need_cids) |
| 2532 |
log_heap_new_cid(relation, heaptup); |
| 2533 |
} |
| 2534 |
|
| 2535 |
/* |
| 2536 |
* If the page is all visible, need to clear that, unless we're only |
| 2537 |
* going to add further frozen rows to it. |
| 2538 |
* |
| 2539 |
* If we're only adding already frozen rows to a previously empty |
| 2540 |
* page, mark it as all-visible. |
| 2541 |
*/ |
| 2542 |
if (clear_all_visible) |
| 2543 |
{ |
| 2544 |
Assert(!(options & HEAP_INSERT_FROZEN)); |
| 2545 |
/* It's possible the VM bits were already clear */ |
| 2546 |
if (visibilitymap_clear_locked(relation, |
| 2547 |
BufferGetBlockNumber(buffer), |
| 2548 |
vmbuffer, VISIBILITYMAP_VALID_BITS)) |
| 2549 |
vmbuffer_modified = true; |
| 2550 |
|
| 2551 |
PageClearAllVisible(page); |
| 2552 |
} |
| 2553 |
else if (all_frozen_set) |
| 2554 |
PageSetAllVisible(page); |
| 2555 |
|
| 2556 |
/* |
| 2557 |
* XXX Should we set PageSetPrunable on this page ? See heap_insert() |
| 2558 |
*/ |
| 2559 |
|
| 2560 |
MarkBufferDirty(buffer); |
| 2561 |
|
| 2562 |
/* XLOG stuff */ |
| 2563 |
if (needwal) |
| 2564 |
{ |
| 2565 |
XLogRecPtr recptr; |
| 2566 |
xl_heap_multi_insert *xlrec; |
| 2567 |
uint8 info = XLOG_HEAP2_MULTI_INSERT; |
| 2568 |
char *tupledata; |
| 2569 |
int totaldatalen; |
| 2570 |
char *scratchptr = scratch.data; |
| 2571 |
bool init; |
| 2572 |
int bufflags = 0; |
| 2573 |
|
| 2574 |
/* |
| 2575 |
* If the page was previously empty, we can reinit the page |
| 2576 |
* instead of restoring the whole thing. |
| 2577 |
*/ |
| 2578 |
init = starting_with_empty_page; |
| 2579 |
|
| 2580 |
/* allocate xl_heap_multi_insert struct from the scratch area */ |
| 2581 |
xlrec = (xl_heap_multi_insert *) scratchptr; |
| 2582 |
scratchptr += SizeOfHeapMultiInsert; |
| 2583 |
|
| 2584 |
/* |
| 2585 |
* Allocate offsets array. Unless we're reinitializing the page, |
| 2586 |
* in that case the tuples are stored in order starting at |
| 2587 |
* FirstOffsetNumber and we don't need to store the offsets |
| 2588 |
* explicitly. |
| 2589 |
*/ |
| 2590 |
if (!init) |
| 2591 |
scratchptr += nthispage * sizeof(OffsetNumber); |
| 2592 |
|
| 2593 |
/* the rest of the scratch space is used for tuple data */ |
| 2594 |
tupledata = scratchptr; |
| 2595 |
|
| 2596 |
/* check that the mutually exclusive flags are not both set */ |
| 2597 |
Assert(!(clear_all_visible && all_frozen_set)); |
| 2598 |
|
| 2599 |
xlrec->flags = 0; |
| 2600 |
if (clear_all_visible) |
| 2601 |
xlrec->flags = XLH_INSERT_ALL_VISIBLE_CLEARED; |
| 2602 |
if (all_frozen_set) |
| 2603 |
xlrec->flags = XLH_INSERT_ALL_FROZEN_SET; |
| 2604 |
|
| 2605 |
xlrec->ntuples = nthispage; |
| 2606 |
|
| 2607 |
/* |
| 2608 |
* Write out an xl_multi_insert_tuple and the tuple data itself |
| 2609 |
* for each tuple. |
| 2610 |
*/ |
| 2611 |
for (i = 0; i < nthispage; i++) |
| 2612 |
{ |
| 2613 |
HeapTuple heaptup = heaptuples[ndone + i]; |
| 2614 |
xl_multi_insert_tuple *tuphdr; |
| 2615 |
int datalen; |
| 2616 |
|
| 2617 |
if (!init) |
| 2618 |
xlrec->offsets[i] = ItemPointerGetOffsetNumber(&heaptup->t_self); |
| 2619 |
/* xl_multi_insert_tuple needs two-byte alignment. */ |
| 2620 |
tuphdr = (xl_multi_insert_tuple *) SHORTALIGN(scratchptr); |
| 2621 |
scratchptr = ((char *) tuphdr) + SizeOfMultiInsertTuple; |
| 2622 |
|
| 2623 |
tuphdr->t_infomask2 = heaptup->t_data->t_infomask2; |
| 2624 |
tuphdr->t_infomask = heaptup->t_data->t_infomask; |
| 2625 |
tuphdr->t_hoff = heaptup->t_data->t_hoff; |
| 2626 |
|
| 2627 |
/* write bitmap [+ padding] [+ oid] + data */ |
| 2628 |
datalen = heaptup->t_len - SizeofHeapTupleHeader; |
| 2629 |
memcpy(scratchptr, |
| 2630 |
(char *) heaptup->t_data + SizeofHeapTupleHeader, |
| 2631 |
datalen); |
| 2632 |
tuphdr->datalen = datalen; |
| 2633 |
scratchptr += datalen; |
| 2634 |
} |
| 2635 |
totaldatalen = scratchptr - tupledata; |
| 2636 |
Assert((scratchptr - scratch.data) < BLCKSZ); |
| 2637 |
|
| 2638 |
if (need_tuple_data) |
| 2639 |
xlrec->flags |= XLH_INSERT_CONTAINS_NEW_TUPLE; |
| 2640 |
|
| 2641 |
/* |
| 2642 |
* Signal that this is the last xl_heap_multi_insert record |
| 2643 |
* emitted by this call to heap_multi_insert(). Needed for logical |
| 2644 |
* decoding so it knows when to cleanup temporary data. |
| 2645 |
*/ |
| 2646 |
if (ndone + nthispage == ntuples) |
| 2647 |
xlrec->flags |= XLH_INSERT_LAST_IN_MULTI; |
| 2648 |
|
| 2649 |
if (init) |
| 2650 |
{ |
| 2651 |
info |= XLOG_HEAP_INIT_PAGE; |
| 2652 |
bufflags |= REGBUF_WILL_INIT; |
| 2653 |
} |
| 2654 |
|
| 2655 |
/* |
| 2656 |
* If we're doing logical decoding, include the new tuple data |
| 2657 |
* even if we take a full-page image of the page. |
| 2658 |
*/ |
| 2659 |
if (need_tuple_data) |
| 2660 |
bufflags |= REGBUF_KEEP_DATA; |
| 2661 |
|
| 2662 |
XLogBeginInsert(); |
| 2663 |
XLogRegisterData(xlrec, tupledata - scratch.data); |
| 2664 |
XLogRegisterBuffer(HEAP_MULTI_INSERT_BLKREF_HEAP, buffer, |
| 2665 |
REGBUF_STANDARD | bufflags); |
| 2666 |
if (vmbuffer_modified) |
| 2667 |
XLogRegisterBuffer(HEAP_MULTI_INSERT_BLKREF_VM, vmbuffer, 0); |
| 2668 |
|
| 2669 |
XLogRegisterBufData(HEAP_MULTI_INSERT_BLKREF_HEAP, tupledata, |
| 2670 |
totaldatalen); |
| 2671 |
|
| 2672 |
/* filtering by origin on a row level is much more efficient */ |
| 2673 |
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); |
| 2674 |
|
| 2675 |
recptr = XLogInsert(RM_HEAP2_ID, info); |
| 2676 |
|
| 2677 |
PageSetLSN(page, recptr); |
| 2678 |
if (vmbuffer_modified) |
| 2679 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 2680 |
} |
| 2681 |
|
| 2682 |
END_CRIT_SECTION(); |
| 2683 |
|
| 2684 |
/* |
| 2685 |
* We locked vmbuffer if clear_all_visible was true regardless of |
| 2686 |
* whether or not we ended up modifying the vmbuffer. |
| 2687 |
*/ |
| 2688 |
if (clear_all_visible) |
| 2689 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 2690 |
|
| 2691 |
/* |
| 2692 |
* If we've frozen everything on the page, update the visibilitymap. |
| 2693 |
* We're already holding pin on the vmbuffer. |
| 2694 |
*/ |
| 2695 |
if (all_frozen_set) |
| 2696 |
{ |
| 2697 |
Assert(PageIsAllVisible(page)); |
| 2698 |
Assert(visibilitymap_pin_ok(BufferGetBlockNumber(buffer), vmbuffer)); |
| 2699 |
|
| 2700 |
/* |
| 2701 |
* It's fine to use InvalidTransactionId here - this is only used |
| 2702 |
* when HEAP_INSERT_FROZEN is specified, which intentionally |
| 2703 |
* violates visibility rules. |
| 2704 |
*/ |
| 2705 |
visibilitymap_set(relation, BufferGetBlockNumber(buffer), buffer, |
| 2706 |
InvalidXLogRecPtr, vmbuffer, |
| 2707 |
InvalidTransactionId, |
| 2708 |
VISIBILITYMAP_ALL_VISIBLE | VISIBILITYMAP_ALL_FROZEN); |
| 2709 |
} |
| 2710 |
|
| 2711 |
UnlockReleaseBuffer(buffer); |
| 2712 |
ndone += nthispage; |
| 2713 |
|
| 2714 |
/* |
| 2715 |
* NB: Only release vmbuffer after inserting all tuples - it's fairly |
| 2716 |
* likely that we'll insert into subsequent heap pages that are likely |
| 2717 |
* to use the same vm page. |
| 2718 |
*/ |
| 2719 |
} |
| 2720 |
|
| 2721 |
/* We're done with inserting all tuples, so release the last vmbuffer. */ |
| 2722 |
if (vmbuffer != InvalidBuffer) |
| 2723 |
ReleaseBuffer(vmbuffer); |
| 2724 |
|
| 2725 |
/* |
| 2726 |
* We're done with the actual inserts. Check for conflicts again, to |
| 2727 |
* ensure that all rw-conflicts in to these inserts are detected. Without |
| 2728 |
* this final check, a sequential scan of the heap may have locked the |
| 2729 |
* table after the "before" check, missing one opportunity to detect the |
| 2730 |
* conflict, and then scanned the table before the new tuples were there, |
| 2731 |
* missing the other chance to detect the conflict. |
| 2732 |
* |
| 2733 |
* For heap inserts, we only need to check for table-level SSI locks. Our |
| 2734 |
* new tuples can't possibly conflict with existing tuple locks, and heap |
| 2735 |
* page locks are only consolidated versions of tuple locks; they do not |
| 2736 |
* lock "gaps" as index page locks do. So we don't need to specify a |
| 2737 |
* buffer when making the call. |
| 2738 |
*/ |
| 2739 |
CheckForSerializableConflictIn(relation, NULL, InvalidBlockNumber); |
| 2740 |
|
| 2741 |
/* |
| 2742 |
* If tuples are cachable, mark them for invalidation from the caches in |
| 2743 |
* case we abort. Note it is OK to do this after releasing the buffer, |
| 2744 |
* because the heaptuples data structure is all in local memory, not in |
| 2745 |
* the shared buffer. |
| 2746 |
*/ |
| 2747 |
if (IsCatalogRelation(relation)) |
| 2748 |
{ |
| 2749 |
for (i = 0; i < ntuples; i++) |
| 2750 |
CacheInvalidateHeapTuple(relation, heaptuples[i], NULL); |
| 2751 |
} |
| 2752 |
|
| 2753 |
/* copy t_self fields back to the caller's slots */ |
| 2754 |
for (i = 0; i < ntuples; i++) |
| 2755 |
slots[i]->tts_tid = heaptuples[i]->t_self; |
| 2756 |
|
| 2757 |
pgstat_count_heap_insert(relation, ntuples); |
| 2758 |
} |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* simple_heap_insert - insert a tuple |
| 2762 |
* |
| 2763 |
* Currently, this routine differs from heap_insert only in supplying |
| 2764 |
* a default command ID and not allowing access to the speedup options. |
| 2765 |
* |
| 2766 |
* This should be used rather than using heap_insert directly in most places |
| 2767 |
* where we are modifying system catalogs. |
| 2768 |
*/ |
| 2769 |
void |
| 2770 |
simple_heap_insert(Relation relation, HeapTuple tup) |
| 2771 |
{ |
| 2772 |
heap_insert(relation, tup, GetCurrentCommandId(true), 0, NULL); |
| 2773 |
} |
| 2774 |
|
| 2775 |
/* |
| 2776 |
* Given infomask/infomask2, compute the bits that must be saved in the |
| 2777 |
* "infobits" field of xl_heap_delete, xl_heap_update, xl_heap_lock, |
| 2778 |
* xl_heap_lock_updated WAL records. |
| 2779 |
* |
| 2780 |
* See fix_infomask_from_infobits. |
| 2781 |
*/ |
| 2782 |
static uint8 |
| 2783 |
compute_infobits(uint16 infomask, uint16 infomask2) |
| 2784 |
{ |
| 2785 |
return |
| 2786 |
((infomask & HEAP_XMAX_IS_MULTI) != 0 ? XLHL_XMAX_IS_MULTI : 0) | |
| 2787 |
((infomask & HEAP_XMAX_LOCK_ONLY) != 0 ? XLHL_XMAX_LOCK_ONLY : 0) | |
| 2788 |
((infomask & HEAP_XMAX_EXCL_LOCK) != 0 ? XLHL_XMAX_EXCL_LOCK : 0) | |
| 2789 |
/* note we ignore HEAP_XMAX_SHR_LOCK here */ |
| 2790 |
((infomask & HEAP_XMAX_KEYSHR_LOCK) != 0 ? XLHL_XMAX_KEYSHR_LOCK : 0) | |
| 2791 |
((infomask2 & HEAP_KEYS_UPDATED) != 0 ? |
| 2792 |
XLHL_KEYS_UPDATED : 0); |
| 2793 |
} |
| 2794 |
|
| 2795 |
/* |
| 2796 |
* Given two versions of the same t_infomask for a tuple, compare them and |
| 2797 |
* return whether the relevant status for a tuple Xmax has changed. This is |
| 2798 |
* used after a buffer lock has been released and reacquired: we want to ensure |
| 2799 |
* that the tuple state continues to be the same it was when we previously |
| 2800 |
* examined it. |
| 2801 |
* |
| 2802 |
* Note the Xmax field itself must be compared separately. |
| 2803 |
*/ |
| 2804 |
static inline bool |
| 2805 |
xmax_infomask_changed(uint16 new_infomask, uint16 old_infomask) |
| 2806 |
{ |
| 2807 |
const uint16 interesting = |
| 2808 |
HEAP_XMAX_IS_MULTI | HEAP_XMAX_LOCK_ONLY | HEAP_LOCK_MASK; |
| 2809 |
|
| 2810 |
if ((new_infomask & interesting) != (old_infomask & interesting)) |
| 2811 |
return true; |
| 2812 |
|
| 2813 |
return false; |
| 2814 |
} |
| 2815 |
|
| 2816 |
/* |
| 2817 |
* heap_delete - delete a tuple |
| 2818 |
* |
| 2819 |
* See table_tuple_delete() for an explanation of the parameters, except that |
| 2820 |
* this routine directly takes a tuple rather than a slot. |
| 2821 |
* |
| 2822 |
* In the failure cases, the routine fills *tmfd with the tuple's t_ctid, |
| 2823 |
* t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last |
| 2824 |
* only for TM_SelfModified, since we cannot obtain cmax from a combo CID |
| 2825 |
* generated by another transaction). |
| 2826 |
*/ |
| 2827 |
TM_Result |
| 2828 |
heap_delete(Relation relation, ItemPointer tid, |
| 2829 |
CommandId cid, Snapshot crosscheck, bool wait, |
| 2830 |
TM_FailureData *tmfd, bool changingPart) |
| 2831 |
{ |
| 2832 |
TM_Result result; |
| 2833 |
TransactionId xid = GetCurrentTransactionId(); |
| 2834 |
ItemId lp; |
| 2835 |
HeapTupleData tp; |
| 2836 |
Page page; |
| 2837 |
BlockNumber block; |
| 2838 |
Buffer buffer; |
| 2839 |
Buffer vmbuffer = InvalidBuffer; |
| 2840 |
bool vmbuffer_modified = false; |
| 2841 |
TransactionId new_xmax; |
| 2842 |
uint16 new_infomask, |
| 2843 |
new_infomask2; |
| 2844 |
bool have_tuple_lock = false; |
| 2845 |
bool iscombo; |
| 2846 |
bool clear_all_visible = false; |
| 2847 |
HeapTuple old_key_tuple = NULL; /* replica identity of the tuple */ |
| 2848 |
bool old_key_copied = false; |
| 2849 |
|
| 2850 |
Assert(ItemPointerIsValid(tid)); |
| 2851 |
|
| 2852 |
AssertHasSnapshotForToast(relation); |
| 2853 |
|
| 2854 |
/* |
| 2855 |
* Forbid this during a parallel operation, lest it allocate a combo CID. |
| 2856 |
* Other workers might need that combo CID for visibility checks, and we |
| 2857 |
* have no provision for broadcasting it to them. |
| 2858 |
*/ |
| 2859 |
if (IsInParallelMode()) |
| 2860 |
ereport(ERROR, |
| 2861 |
(errcode(ERRCODE_INVALID_TRANSACTION_STATE), |
| 2862 |
errmsg("cannot delete tuples during a parallel operation"))); |
| 2863 |
|
| 2864 |
block = ItemPointerGetBlockNumber(tid); |
| 2865 |
buffer = ReadBuffer(relation, block); |
| 2866 |
page = BufferGetPage(buffer); |
| 2867 |
|
| 2868 |
/* |
| 2869 |
* Before locking the buffer, pin the visibility map page if it appears to |
| 2870 |
* be necessary. Since we haven't got the lock yet, someone else might be |
| 2871 |
* in the middle of changing this, so we'll need to recheck after we have |
| 2872 |
* the lock. |
| 2873 |
*/ |
| 2874 |
if (PageIsAllVisible(page)) |
| 2875 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 2876 |
|
| 2877 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 2878 |
|
| 2879 |
lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid)); |
| 2880 |
Assert(ItemIdIsNormal(lp)); |
| 2881 |
|
| 2882 |
tp.t_tableOid = RelationGetRelid(relation); |
| 2883 |
tp.t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 2884 |
tp.t_len = ItemIdGetLength(lp); |
| 2885 |
tp.t_self = *tid; |
| 2886 |
|
| 2887 |
l1: |
| 2888 |
|
| 2889 |
/* |
| 2890 |
* If we didn't pin the visibility map page and the page has become all |
| 2891 |
* visible while we were busy locking the buffer, we'll have to unlock and |
| 2892 |
* re-lock, to avoid holding the buffer lock across an I/O. That's a bit |
| 2893 |
* unfortunate, but hopefully shouldn't happen often. |
| 2894 |
*/ |
| 2895 |
if (vmbuffer == InvalidBuffer && PageIsAllVisible(page)) |
| 2896 |
{ |
| 2897 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 2898 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 2899 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 2900 |
} |
| 2901 |
|
| 2902 |
result = HeapTupleSatisfiesUpdate(&tp, cid, buffer); |
| 2903 |
|
| 2904 |
if (result == TM_Invisible) |
| 2905 |
{ |
| 2906 |
UnlockReleaseBuffer(buffer); |
| 2907 |
ereport(ERROR, |
| 2908 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 2909 |
errmsg("attempted to delete invisible tuple"))); |
| 2910 |
} |
| 2911 |
else if (result == TM_BeingModified && wait) |
| 2912 |
{ |
| 2913 |
TransactionId xwait; |
| 2914 |
uint16 infomask; |
| 2915 |
|
| 2916 |
/* must copy state data before unlocking buffer */ |
| 2917 |
xwait = HeapTupleHeaderGetRawXmax(tp.t_data); |
| 2918 |
infomask = tp.t_data->t_infomask; |
| 2919 |
|
| 2920 |
/* |
| 2921 |
* Sleep until concurrent transaction ends -- except when there's a |
| 2922 |
* single locker and it's our own transaction. Note we don't care |
| 2923 |
* which lock mode the locker has, because we need the strongest one. |
| 2924 |
* |
| 2925 |
* Before sleeping, we need to acquire tuple lock to establish our |
| 2926 |
* priority for the tuple (see heap_lock_tuple). LockTuple will |
| 2927 |
* release us when we are next-in-line for the tuple. |
| 2928 |
* |
| 2929 |
* If we are forced to "start over" below, we keep the tuple lock; |
| 2930 |
* this arranges that we stay at the head of the line while rechecking |
| 2931 |
* tuple state. |
| 2932 |
*/ |
| 2933 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 2934 |
{ |
| 2935 |
bool current_is_member = false; |
| 2936 |
|
| 2937 |
if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, |
| 2938 |
LockTupleExclusive, ¤t_is_member)) |
| 2939 |
{ |
| 2940 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 2941 |
|
| 2942 |
/* |
| 2943 |
* Acquire the lock, if necessary (but skip it when we're |
| 2944 |
* requesting a lock and already have one; avoids deadlock). |
| 2945 |
*/ |
| 2946 |
if (!current_is_member) |
| 2947 |
heap_acquire_tuplock(relation, &(tp.t_self), LockTupleExclusive, |
| 2948 |
LockWaitBlock, &have_tuple_lock); |
| 2949 |
|
| 2950 |
/* wait for multixact */ |
| 2951 |
MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask, |
| 2952 |
relation, &(tp.t_self), XLTW_Delete, |
| 2953 |
NULL); |
| 2954 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 2955 |
|
| 2956 |
/* |
| 2957 |
* If xwait had just locked the tuple then some other xact |
| 2958 |
* could update this tuple before we get to this point. Check |
| 2959 |
* for xmax change, and start over if so. |
| 2960 |
* |
| 2961 |
* We also must start over if we didn't pin the VM page, and |
| 2962 |
* the page has become all visible. |
| 2963 |
*/ |
| 2964 |
if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) || |
| 2965 |
xmax_infomask_changed(tp.t_data->t_infomask, infomask) || |
| 2966 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data), |
| 2967 |
xwait)) |
| 2968 |
goto l1; |
| 2969 |
} |
| 2970 |
|
| 2971 |
/* |
| 2972 |
* You might think the multixact is necessarily done here, but not |
| 2973 |
* so: it could have surviving members, namely our own xact or |
| 2974 |
* other subxacts of this backend. It is legal for us to delete |
| 2975 |
* the tuple in either case, however (the latter case is |
| 2976 |
* essentially a situation of upgrading our former shared lock to |
| 2977 |
* exclusive). We don't bother changing the on-disk hint bits |
| 2978 |
* since we are about to overwrite the xmax altogether. |
| 2979 |
*/ |
| 2980 |
} |
| 2981 |
else if (!TransactionIdIsCurrentTransactionId(xwait)) |
| 2982 |
{ |
| 2983 |
/* |
| 2984 |
* Wait for regular transaction to end; but first, acquire tuple |
| 2985 |
* lock. |
| 2986 |
*/ |
| 2987 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 2988 |
heap_acquire_tuplock(relation, &(tp.t_self), LockTupleExclusive, |
| 2989 |
LockWaitBlock, &have_tuple_lock); |
| 2990 |
XactLockTableWait(xwait, relation, &(tp.t_self), XLTW_Delete); |
| 2991 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 2992 |
|
| 2993 |
/* |
| 2994 |
* xwait is done, but if xwait had just locked the tuple then some |
| 2995 |
* other xact could update this tuple before we get to this point. |
| 2996 |
* Check for xmax change, and start over if so. |
| 2997 |
* |
| 2998 |
* We also must start over if we didn't pin the VM page, and the |
| 2999 |
* page has become all visible. |
| 3000 |
*/ |
| 3001 |
if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) || |
| 3002 |
xmax_infomask_changed(tp.t_data->t_infomask, infomask) || |
| 3003 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data), |
| 3004 |
xwait)) |
| 3005 |
goto l1; |
| 3006 |
|
| 3007 |
/* Otherwise check if it committed or aborted */ |
| 3008 |
UpdateXmaxHintBits(tp.t_data, buffer, xwait); |
| 3009 |
} |
| 3010 |
|
| 3011 |
/* |
| 3012 |
* We may overwrite if previous xmax aborted, or if it committed but |
| 3013 |
* only locked the tuple without updating it. |
| 3014 |
*/ |
| 3015 |
if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) || |
| 3016 |
HEAP_XMAX_IS_LOCKED_ONLY(tp.t_data->t_infomask) || |
| 3017 |
HeapTupleHeaderIsOnlyLocked(tp.t_data)) |
| 3018 |
result = TM_Ok; |
| 3019 |
else if (!ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid)) |
| 3020 |
result = TM_Updated; |
| 3021 |
else |
| 3022 |
result = TM_Deleted; |
| 3023 |
} |
| 3024 |
|
| 3025 |
/* sanity check the result HeapTupleSatisfiesUpdate() and the logic above */ |
| 3026 |
if (result != TM_Ok) |
| 3027 |
{ |
| 3028 |
Assert(result == TM_SelfModified || |
| 3029 |
result == TM_Updated || |
| 3030 |
result == TM_Deleted || |
| 3031 |
result == TM_BeingModified); |
| 3032 |
Assert(!(tp.t_data->t_infomask & HEAP_XMAX_INVALID)); |
| 3033 |
Assert(result != TM_Updated || |
| 3034 |
!ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid)); |
| 3035 |
} |
| 3036 |
|
| 3037 |
if (crosscheck != InvalidSnapshot && result == TM_Ok) |
| 3038 |
{ |
| 3039 |
/* Perform additional check for transaction-snapshot mode RI updates */ |
| 3040 |
if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer)) |
| 3041 |
result = TM_Updated; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if (result != TM_Ok) |
| 3045 |
{ |
| 3046 |
tmfd->ctid = tp.t_data->t_ctid; |
| 3047 |
tmfd->xmax = HeapTupleHeaderGetUpdateXid(tp.t_data); |
| 3048 |
if (result == TM_SelfModified) |
| 3049 |
tmfd->cmax = HeapTupleHeaderGetCmax(tp.t_data); |
| 3050 |
else |
| 3051 |
tmfd->cmax = InvalidCommandId; |
| 3052 |
UnlockReleaseBuffer(buffer); |
| 3053 |
if (have_tuple_lock) |
| 3054 |
UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive); |
| 3055 |
if (vmbuffer != InvalidBuffer) |
| 3056 |
ReleaseBuffer(vmbuffer); |
| 3057 |
return result; |
| 3058 |
} |
| 3059 |
|
| 3060 |
/* |
| 3061 |
* We're about to do the actual delete -- check for conflict first, to |
| 3062 |
* avoid possibly having to roll back work we've just done. |
| 3063 |
* |
| 3064 |
* This is safe without a recheck as long as there is no possibility of |
| 3065 |
* another process scanning the page between this check and the delete |
| 3066 |
* being visible to the scan (i.e., an exclusive buffer content lock is |
| 3067 |
* continuously held from this point until the tuple delete is visible). |
| 3068 |
*/ |
| 3069 |
CheckForSerializableConflictIn(relation, tid, BufferGetBlockNumber(buffer)); |
| 3070 |
|
| 3071 |
/* replace cid with a combo CID if necessary */ |
| 3072 |
HeapTupleHeaderAdjustCmax(tp.t_data, &cid, &iscombo); |
| 3073 |
|
| 3074 |
/* |
| 3075 |
* Compute replica identity tuple before entering the critical section so |
| 3076 |
* we don't PANIC upon a memory allocation failure. |
| 3077 |
*/ |
| 3078 |
old_key_tuple = ExtractReplicaIdentity(relation, &tp, true, &old_key_copied); |
| 3079 |
|
| 3080 |
/* |
| 3081 |
* If this is the first possibly-multixact-able operation in the current |
| 3082 |
* transaction, set my per-backend OldestMemberMXactId setting. We can be |
| 3083 |
* certain that the transaction will never become a member of any older |
| 3084 |
* MultiXactIds than that. (We have to do this even if we end up just |
| 3085 |
* using our own TransactionId below, since some other backend could |
| 3086 |
* incorporate our XID into a MultiXact immediately afterwards.) |
| 3087 |
*/ |
| 3088 |
MultiXactIdSetOldestMember(); |
| 3089 |
|
| 3090 |
compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(tp.t_data), |
| 3091 |
tp.t_data->t_infomask, tp.t_data->t_infomask2, |
| 3092 |
xid, LockTupleExclusive, true, |
| 3093 |
&new_xmax, &new_infomask, &new_infomask2); |
| 3094 |
|
| 3095 |
/* Lock the VM before entering the critical section */ |
| 3096 |
if (PageIsAllVisible(page)) |
| 3097 |
{ |
| 3098 |
clear_all_visible = true; |
| 3099 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 3100 |
} |
| 3101 |
|
| 3102 |
START_CRIT_SECTION(); |
| 3103 |
|
| 3104 |
/* |
| 3105 |
* If this transaction commits, the tuple will become DEAD sooner or |
| 3106 |
* later. Set flag that this page is a candidate for pruning once our xid |
| 3107 |
* falls below the OldestXmin horizon. If the transaction finally aborts, |
| 3108 |
* the subsequent page pruning will be a no-op and the hint will be |
| 3109 |
* cleared. |
| 3110 |
*/ |
| 3111 |
PageSetPrunable(page, xid); |
| 3112 |
|
| 3113 |
if (clear_all_visible) |
| 3114 |
{ |
| 3115 |
/* It's possible the VM bits were already clear */ |
| 3116 |
if (visibilitymap_clear_locked(relation, BufferGetBlockNumber(buffer), |
| 3117 |
vmbuffer, VISIBILITYMAP_VALID_BITS)) |
| 3118 |
vmbuffer_modified = true; |
| 3119 |
|
| 3120 |
PageClearAllVisible(page); |
| 3121 |
} |
| 3122 |
|
| 3123 |
/* store transaction information of xact deleting the tuple */ |
| 3124 |
tp.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED); |
| 3125 |
tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 3126 |
tp.t_data->t_infomask |= new_infomask; |
| 3127 |
tp.t_data->t_infomask2 |= new_infomask2; |
| 3128 |
HeapTupleHeaderClearHotUpdated(tp.t_data); |
| 3129 |
HeapTupleHeaderSetXmax(tp.t_data, new_xmax); |
| 3130 |
HeapTupleHeaderSetCmax(tp.t_data, cid, iscombo); |
| 3131 |
/* Make sure there is no forward chain link in t_ctid */ |
| 3132 |
tp.t_data->t_ctid = tp.t_self; |
| 3133 |
|
| 3134 |
/* Signal that this is actually a move into another partition */ |
| 3135 |
if (changingPart) |
| 3136 |
HeapTupleHeaderSetMovedPartitions(tp.t_data); |
| 3137 |
|
| 3138 |
MarkBufferDirty(buffer); |
| 3139 |
|
| 3140 |
/* |
| 3141 |
* XLOG stuff |
| 3142 |
* |
| 3143 |
* NB: heap_abort_speculative() uses the same xlog record and replay |
| 3144 |
* routines. |
| 3145 |
*/ |
| 3146 |
if (RelationNeedsWAL(relation)) |
| 3147 |
{ |
| 3148 |
xl_heap_delete xlrec; |
| 3149 |
xl_heap_header xlhdr; |
| 3150 |
XLogRecPtr recptr; |
| 3151 |
|
| 3152 |
/* |
| 3153 |
* For logical decode we need combo CIDs to properly decode the |
| 3154 |
* catalog |
| 3155 |
*/ |
| 3156 |
if (RelationIsAccessibleInLogicalDecoding(relation)) |
| 3157 |
log_heap_new_cid(relation, &tp); |
| 3158 |
|
| 3159 |
xlrec.flags = 0; |
| 3160 |
if (clear_all_visible) |
| 3161 |
xlrec.flags |= XLH_DELETE_ALL_VISIBLE_CLEARED; |
| 3162 |
if (changingPart) |
| 3163 |
xlrec.flags |= XLH_DELETE_IS_PARTITION_MOVE; |
| 3164 |
xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask, |
| 3165 |
tp.t_data->t_infomask2); |
| 3166 |
xlrec.offnum = ItemPointerGetOffsetNumber(&tp.t_self); |
| 3167 |
xlrec.xmax = new_xmax; |
| 3168 |
|
| 3169 |
if (old_key_tuple != NULL) |
| 3170 |
{ |
| 3171 |
if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL) |
| 3172 |
xlrec.flags |= XLH_DELETE_CONTAINS_OLD_TUPLE; |
| 3173 |
else |
| 3174 |
xlrec.flags |= XLH_DELETE_CONTAINS_OLD_KEY; |
| 3175 |
} |
| 3176 |
|
| 3177 |
XLogBeginInsert(); |
| 3178 |
XLogRegisterData(&xlrec, SizeOfHeapDelete); |
| 3179 |
|
| 3180 |
XLogRegisterBuffer(HEAP_DELETE_BLKREF_HEAP, buffer, REGBUF_STANDARD); |
| 3181 |
|
| 3182 |
/* |
| 3183 |
* Log replica identity of the deleted tuple if there is one |
| 3184 |
*/ |
| 3185 |
if (old_key_tuple != NULL) |
| 3186 |
{ |
| 3187 |
xlhdr.t_infomask2 = old_key_tuple->t_data->t_infomask2; |
| 3188 |
xlhdr.t_infomask = old_key_tuple->t_data->t_infomask; |
| 3189 |
xlhdr.t_hoff = old_key_tuple->t_data->t_hoff; |
| 3190 |
|
| 3191 |
XLogRegisterData(&xlhdr, SizeOfHeapHeader); |
| 3192 |
XLogRegisterData((char *) old_key_tuple->t_data |
| 3193 |
+ SizeofHeapTupleHeader, |
| 3194 |
old_key_tuple->t_len |
| 3195 |
- SizeofHeapTupleHeader); |
| 3196 |
} |
| 3197 |
|
| 3198 |
/* filtering by origin on a row level is much more efficient */ |
| 3199 |
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); |
| 3200 |
|
| 3201 |
if (vmbuffer_modified) |
| 3202 |
XLogRegisterBuffer(HEAP_DELETE_BLKREF_VM, vmbuffer, 0); |
| 3203 |
|
| 3204 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE); |
| 3205 |
|
| 3206 |
PageSetLSN(page, recptr); |
| 3207 |
|
| 3208 |
if (vmbuffer_modified) |
| 3209 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 3210 |
} |
| 3211 |
|
| 3212 |
END_CRIT_SECTION(); |
| 3213 |
|
| 3214 |
/* |
| 3215 |
* Release VM lock first, since it covers many heap blocks. We locked |
| 3216 |
* vmbuffer if clear_all_visible was true regardless of whether or not we |
| 3217 |
* ended up modifying the vmbuffer. |
| 3218 |
*/ |
| 3219 |
if (clear_all_visible) |
| 3220 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 3221 |
|
| 3222 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 3223 |
|
| 3224 |
if (vmbuffer != InvalidBuffer) |
| 3225 |
ReleaseBuffer(vmbuffer); |
| 3226 |
|
| 3227 |
/* |
| 3228 |
* If the tuple has toasted out-of-line attributes, we need to delete |
| 3229 |
* those items too. We have to do this before releasing the buffer |
| 3230 |
* because we need to look at the contents of the tuple, but it's OK to |
| 3231 |
* release the content lock on the buffer first. |
| 3232 |
*/ |
| 3233 |
if (relation->rd_rel->relkind != RELKIND_RELATION && |
| 3234 |
relation->rd_rel->relkind != RELKIND_MATVIEW) |
| 3235 |
{ |
| 3236 |
/* toast table entries should never be recursively toasted */ |
| 3237 |
Assert(!HeapTupleHasExternal(&tp)); |
| 3238 |
} |
| 3239 |
else if (HeapTupleHasExternal(&tp)) |
| 3240 |
heap_toast_delete(relation, &tp, false); |
| 3241 |
|
| 3242 |
/* |
| 3243 |
* Mark tuple for invalidation from system caches at next command |
| 3244 |
* boundary. We have to do this before releasing the buffer because we |
| 3245 |
* need to look at the contents of the tuple. |
| 3246 |
*/ |
| 3247 |
CacheInvalidateHeapTuple(relation, &tp, NULL); |
| 3248 |
|
| 3249 |
/* Now we can release the buffer */ |
| 3250 |
ReleaseBuffer(buffer); |
| 3251 |
|
| 3252 |
/* |
| 3253 |
* Release the lmgr tuple lock, if we had it. |
| 3254 |
*/ |
| 3255 |
if (have_tuple_lock) |
| 3256 |
UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive); |
| 3257 |
|
| 3258 |
pgstat_count_heap_delete(relation); |
| 3259 |
|
| 3260 |
if (old_key_tuple != NULL && old_key_copied) |
| 3261 |
heap_freetuple(old_key_tuple); |
| 3262 |
|
| 3263 |
return TM_Ok; |
| 3264 |
} |
| 3265 |
|
| 3266 |
/* |
| 3267 |
* simple_heap_delete - delete a tuple |
| 3268 |
* |
| 3269 |
* This routine may be used to delete a tuple when concurrent updates of |
| 3270 |
* the target tuple are not expected (for example, because we have a lock |
| 3271 |
* on the relation associated with the tuple). Any failure is reported |
| 3272 |
* via ereport(). |
| 3273 |
*/ |
| 3274 |
void |
| 3275 |
simple_heap_delete(Relation relation, ItemPointer tid) |
| 3276 |
{ |
| 3277 |
TM_Result result; |
| 3278 |
TM_FailureData tmfd; |
| 3279 |
|
| 3280 |
result = heap_delete(relation, tid, |
| 3281 |
GetCurrentCommandId(true), InvalidSnapshot, |
| 3282 |
true /* wait for commit */ , |
| 3283 |
&tmfd, false /* changingPart */ ); |
| 3284 |
switch (result) |
| 3285 |
{ |
| 3286 |
case TM_SelfModified: |
| 3287 |
/* Tuple was already updated in current command? */ |
| 3288 |
elog(ERROR, "tuple already updated by self"); |
| 3289 |
break; |
| 3290 |
|
| 3291 |
case TM_Ok: |
| 3292 |
/* done successfully */ |
| 3293 |
break; |
| 3294 |
|
| 3295 |
case TM_Updated: |
| 3296 |
elog(ERROR, "tuple concurrently updated"); |
| 3297 |
break; |
| 3298 |
|
| 3299 |
case TM_Deleted: |
| 3300 |
elog(ERROR, "tuple concurrently deleted"); |
| 3301 |
break; |
| 3302 |
|
| 3303 |
default: |
| 3304 |
elog(ERROR, "unrecognized heap_delete status: %u", result); |
| 3305 |
break; |
| 3306 |
} |
| 3307 |
} |
| 3308 |
|
| 3309 |
/* |
| 3310 |
* heap_update - replace a tuple |
| 3311 |
* |
| 3312 |
* See table_tuple_update() for an explanation of the parameters, except that |
| 3313 |
* this routine directly takes a tuple rather than a slot. |
| 3314 |
* |
| 3315 |
* In the failure cases, the routine fills *tmfd with the tuple's t_ctid, |
| 3316 |
* t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last |
| 3317 |
* only for TM_SelfModified, since we cannot obtain cmax from a combo CID |
| 3318 |
* generated by another transaction). |
| 3319 |
*/ |
| 3320 |
TM_Result |
| 3321 |
heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, |
| 3322 |
CommandId cid, Snapshot crosscheck, bool wait, |
| 3323 |
TM_FailureData *tmfd, LockTupleMode *lockmode, |
| 3324 |
TU_UpdateIndexes *update_indexes) |
| 3325 |
{ |
| 3326 |
TM_Result result; |
| 3327 |
TransactionId xid = GetCurrentTransactionId(); |
| 3328 |
Bitmapset *hot_attrs; |
| 3329 |
Bitmapset *sum_attrs; |
| 3330 |
Bitmapset *key_attrs; |
| 3331 |
Bitmapset *id_attrs; |
| 3332 |
Bitmapset *interesting_attrs; |
| 3333 |
Bitmapset *modified_attrs; |
| 3334 |
ItemId lp; |
| 3335 |
HeapTupleData oldtup; |
| 3336 |
HeapTuple heaptup; |
| 3337 |
HeapTuple old_key_tuple = NULL; |
| 3338 |
bool old_key_copied = false; |
| 3339 |
Page page, |
| 3340 |
newpage; |
| 3341 |
BlockNumber block; |
| 3342 |
MultiXactStatus mxact_status; |
| 3343 |
Buffer buffer, |
| 3344 |
newbuf, |
| 3345 |
vmbuffer = InvalidBuffer, |
| 3346 |
vmbuffer_new = InvalidBuffer; |
| 3347 |
bool unlock_vmbuffer = false; |
| 3348 |
bool unlock_vmbuffer_new = false; |
| 3349 |
bool need_toast; |
| 3350 |
Size newtupsize, |
| 3351 |
pagefree; |
| 3352 |
bool have_tuple_lock = false; |
| 3353 |
bool iscombo; |
| 3354 |
bool use_hot_update = false; |
| 3355 |
bool summarized_update = false; |
| 3356 |
bool key_intact; |
| 3357 |
bool clear_all_visible = false; |
| 3358 |
bool clear_all_visible_new = false; |
| 3359 |
bool vmbuffer_modified = false; |
| 3360 |
bool vmbuffer_new_modified = false; |
| 3361 |
bool checked_lockers; |
| 3362 |
bool locker_remains; |
| 3363 |
bool id_has_external = false; |
| 3364 |
TransactionId xmax_new_tuple, |
| 3365 |
xmax_old_tuple; |
| 3366 |
uint16 infomask_old_tuple, |
| 3367 |
infomask2_old_tuple, |
| 3368 |
infomask_new_tuple, |
| 3369 |
infomask2_new_tuple; |
| 3370 |
|
| 3371 |
Assert(ItemPointerIsValid(otid)); |
| 3372 |
|
| 3373 |
/* Cheap, simplistic check that the tuple matches the rel's rowtype. */ |
| 3374 |
Assert(HeapTupleHeaderGetNatts(newtup->t_data) <= |
| 3375 |
RelationGetNumberOfAttributes(relation)); |
| 3376 |
|
| 3377 |
AssertHasSnapshotForToast(relation); |
| 3378 |
|
| 3379 |
/* |
| 3380 |
* Forbid this during a parallel operation, lest it allocate a combo CID. |
| 3381 |
* Other workers might need that combo CID for visibility checks, and we |
| 3382 |
* have no provision for broadcasting it to them. |
| 3383 |
*/ |
| 3384 |
if (IsInParallelMode()) |
| 3385 |
ereport(ERROR, |
| 3386 |
(errcode(ERRCODE_INVALID_TRANSACTION_STATE), |
| 3387 |
errmsg("cannot update tuples during a parallel operation"))); |
| 3388 |
|
| 3389 |
#ifdef USE_ASSERT_CHECKING |
| 3390 |
check_lock_if_inplace_updateable_rel(relation, otid, newtup); |
| 3391 |
#endif |
| 3392 |
|
| 3393 |
/* |
| 3394 |
* Fetch the list of attributes to be checked for various operations. |
| 3395 |
* |
| 3396 |
* For HOT considerations, this is wasted effort if we fail to update or |
| 3397 |
* have to put the new tuple on a different page. But we must compute the |
| 3398 |
* list before obtaining buffer lock --- in the worst case, if we are |
| 3399 |
* doing an update on one of the relevant system catalogs, we could |
| 3400 |
* deadlock if we try to fetch the list later. In any case, the relcache |
| 3401 |
* caches the data so this is usually pretty cheap. |
| 3402 |
* |
| 3403 |
* We also need columns used by the replica identity and columns that are |
| 3404 |
* considered the "key" of rows in the table. |
| 3405 |
* |
| 3406 |
* Note that we get copies of each bitmap, so we need not worry about |
| 3407 |
* relcache flush happening midway through. |
| 3408 |
*/ |
| 3409 |
hot_attrs = RelationGetIndexAttrBitmap(relation, |
| 3410 |
INDEX_ATTR_BITMAP_HOT_BLOCKING); |
| 3411 |
sum_attrs = RelationGetIndexAttrBitmap(relation, |
| 3412 |
INDEX_ATTR_BITMAP_SUMMARIZED); |
| 3413 |
key_attrs = RelationGetIndexAttrBitmap(relation, INDEX_ATTR_BITMAP_KEY); |
| 3414 |
id_attrs = RelationGetIndexAttrBitmap(relation, |
| 3415 |
INDEX_ATTR_BITMAP_IDENTITY_KEY); |
| 3416 |
interesting_attrs = NULL; |
| 3417 |
interesting_attrs = bms_add_members(interesting_attrs, hot_attrs); |
| 3418 |
interesting_attrs = bms_add_members(interesting_attrs, sum_attrs); |
| 3419 |
interesting_attrs = bms_add_members(interesting_attrs, key_attrs); |
| 3420 |
interesting_attrs = bms_add_members(interesting_attrs, id_attrs); |
| 3421 |
|
| 3422 |
block = ItemPointerGetBlockNumber(otid); |
| 3423 |
INJECTION_POINT("heap_update-before-pin", NULL); |
| 3424 |
buffer = ReadBuffer(relation, block); |
| 3425 |
page = BufferGetPage(buffer); |
| 3426 |
|
| 3427 |
/* |
| 3428 |
* Before locking the buffer, pin the visibility map page if it appears to |
| 3429 |
* be necessary. Since we haven't got the lock yet, someone else might be |
| 3430 |
* in the middle of changing this, so we'll need to recheck after we have |
| 3431 |
* the lock. |
| 3432 |
*/ |
| 3433 |
if (PageIsAllVisible(page)) |
| 3434 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 3435 |
|
| 3436 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 3437 |
|
| 3438 |
lp = PageGetItemId(page, ItemPointerGetOffsetNumber(otid)); |
| 3439 |
|
| 3440 |
/* |
| 3441 |
* Usually, a buffer pin and/or snapshot blocks pruning of otid, ensuring |
| 3442 |
* we see LP_NORMAL here. When the otid origin is a syscache, we may have |
| 3443 |
* neither a pin nor a snapshot. Hence, we may see other LP_ states, each |
| 3444 |
* of which indicates concurrent pruning. |
| 3445 |
* |
| 3446 |
* Failing with TM_Updated would be most accurate. However, unlike other |
| 3447 |
* TM_Updated scenarios, we don't know the successor ctid in LP_UNUSED and |
| 3448 |
* LP_DEAD cases. While the distinction between TM_Updated and TM_Deleted |
| 3449 |
* does matter to SQL statements UPDATE and MERGE, those SQL statements |
| 3450 |
* hold a snapshot that ensures LP_NORMAL. Hence, the choice between |
| 3451 |
* TM_Updated and TM_Deleted affects only the wording of error messages. |
| 3452 |
* Settle on TM_Deleted, for two reasons. First, it avoids complicating |
| 3453 |
* the specification of when tmfd->ctid is valid. Second, it creates |
| 3454 |
* error log evidence that we took this branch. |
| 3455 |
* |
| 3456 |
* Since it's possible to see LP_UNUSED at otid, it's also possible to see |
| 3457 |
* LP_NORMAL for a tuple that replaced LP_UNUSED. If it's a tuple for an |
| 3458 |
* unrelated row, we'll fail with "duplicate key value violates unique". |
| 3459 |
* XXX if otid is the live, newer version of the newtup row, we'll discard |
| 3460 |
* changes originating in versions of this catalog row after the version |
| 3461 |
* the caller got from syscache. See syscache-update-pruned.spec. |
| 3462 |
*/ |
| 3463 |
if (!ItemIdIsNormal(lp)) |
| 3464 |
{ |
| 3465 |
Assert(RelationSupportsSysCache(RelationGetRelid(relation))); |
| 3466 |
|
| 3467 |
UnlockReleaseBuffer(buffer); |
| 3468 |
Assert(!have_tuple_lock); |
| 3469 |
if (vmbuffer != InvalidBuffer) |
| 3470 |
ReleaseBuffer(vmbuffer); |
| 3471 |
tmfd->ctid = *otid; |
| 3472 |
tmfd->xmax = InvalidTransactionId; |
| 3473 |
tmfd->cmax = InvalidCommandId; |
| 3474 |
*update_indexes = TU_None; |
| 3475 |
|
| 3476 |
bms_free(hot_attrs); |
| 3477 |
bms_free(sum_attrs); |
| 3478 |
bms_free(key_attrs); |
| 3479 |
bms_free(id_attrs); |
| 3480 |
/* modified_attrs not yet initialized */ |
| 3481 |
bms_free(interesting_attrs); |
| 3482 |
return TM_Deleted; |
| 3483 |
} |
| 3484 |
|
| 3485 |
/* |
| 3486 |
* Fill in enough data in oldtup for HeapDetermineColumnsInfo to work |
| 3487 |
* properly. |
| 3488 |
*/ |
| 3489 |
oldtup.t_tableOid = RelationGetRelid(relation); |
| 3490 |
oldtup.t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 3491 |
oldtup.t_len = ItemIdGetLength(lp); |
| 3492 |
oldtup.t_self = *otid; |
| 3493 |
|
| 3494 |
/* the new tuple is ready, except for this: */ |
| 3495 |
newtup->t_tableOid = RelationGetRelid(relation); |
| 3496 |
|
| 3497 |
/* |
| 3498 |
* Determine columns modified by the update. Additionally, identify |
| 3499 |
* whether any of the unmodified replica identity key attributes in the |
| 3500 |
* old tuple is externally stored or not. This is required because for |
| 3501 |
* such attributes the flattened value won't be WAL logged as part of the |
| 3502 |
* new tuple so we must include it as part of the old_key_tuple. See |
| 3503 |
* ExtractReplicaIdentity. |
| 3504 |
*/ |
| 3505 |
modified_attrs = HeapDetermineColumnsInfo(relation, interesting_attrs, |
| 3506 |
id_attrs, &oldtup, |
| 3507 |
newtup, &id_has_external); |
| 3508 |
|
| 3509 |
/* |
| 3510 |
* If we're not updating any "key" column, we can grab a weaker lock type. |
| 3511 |
* This allows for more concurrency when we are running simultaneously |
| 3512 |
* with foreign key checks. |
| 3513 |
* |
| 3514 |
* Note that if a column gets detoasted while executing the update, but |
| 3515 |
* the value ends up being the same, this test will fail and we will use |
| 3516 |
* the stronger lock. This is acceptable; the important case to optimize |
| 3517 |
* is updates that don't manipulate key columns, not those that |
| 3518 |
* serendipitously arrive at the same key values. |
| 3519 |
*/ |
| 3520 |
if (!bms_overlap(modified_attrs, key_attrs)) |
| 3521 |
{ |
| 3522 |
*lockmode = LockTupleNoKeyExclusive; |
| 3523 |
mxact_status = MultiXactStatusNoKeyUpdate; |
| 3524 |
key_intact = true; |
| 3525 |
|
| 3526 |
/* |
| 3527 |
* If this is the first possibly-multixact-able operation in the |
| 3528 |
* current transaction, set my per-backend OldestMemberMXactId |
| 3529 |
* setting. We can be certain that the transaction will never become a |
| 3530 |
* member of any older MultiXactIds than that. (We have to do this |
| 3531 |
* even if we end up just using our own TransactionId below, since |
| 3532 |
* some other backend could incorporate our XID into a MultiXact |
| 3533 |
* immediately afterwards.) |
| 3534 |
*/ |
| 3535 |
MultiXactIdSetOldestMember(); |
| 3536 |
} |
| 3537 |
else |
| 3538 |
{ |
| 3539 |
*lockmode = LockTupleExclusive; |
| 3540 |
mxact_status = MultiXactStatusUpdate; |
| 3541 |
key_intact = false; |
| 3542 |
} |
| 3543 |
|
| 3544 |
/* |
| 3545 |
* Note: beyond this point, use oldtup not otid to refer to old tuple. |
| 3546 |
* otid may very well point at newtup->t_self, which we will overwrite |
| 3547 |
* with the new tuple's location, so there's great risk of confusion if we |
| 3548 |
* use otid anymore. |
| 3549 |
*/ |
| 3550 |
|
| 3551 |
l2: |
| 3552 |
checked_lockers = false; |
| 3553 |
locker_remains = false; |
| 3554 |
result = HeapTupleSatisfiesUpdate(&oldtup, cid, buffer); |
| 3555 |
|
| 3556 |
/* see below about the "no wait" case */ |
| 3557 |
Assert(result != TM_BeingModified || wait); |
| 3558 |
|
| 3559 |
if (result == TM_Invisible) |
| 3560 |
{ |
| 3561 |
UnlockReleaseBuffer(buffer); |
| 3562 |
ereport(ERROR, |
| 3563 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 3564 |
errmsg("attempted to update invisible tuple"))); |
| 3565 |
} |
| 3566 |
else if (result == TM_BeingModified && wait) |
| 3567 |
{ |
| 3568 |
TransactionId xwait; |
| 3569 |
uint16 infomask; |
| 3570 |
bool can_continue = false; |
| 3571 |
|
| 3572 |
/* |
| 3573 |
* XXX note that we don't consider the "no wait" case here. This |
| 3574 |
* isn't a problem currently because no caller uses that case, but it |
| 3575 |
* should be fixed if such a caller is introduced. It wasn't a |
| 3576 |
* problem previously because this code would always wait, but now |
| 3577 |
* that some tuple locks do not conflict with one of the lock modes we |
| 3578 |
* use, it is possible that this case is interesting to handle |
| 3579 |
* specially. |
| 3580 |
* |
| 3581 |
* This may cause failures with third-party code that calls |
| 3582 |
* heap_update directly. |
| 3583 |
*/ |
| 3584 |
|
| 3585 |
/* must copy state data before unlocking buffer */ |
| 3586 |
xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data); |
| 3587 |
infomask = oldtup.t_data->t_infomask; |
| 3588 |
|
| 3589 |
/* |
| 3590 |
* Now we have to do something about the existing locker. If it's a |
| 3591 |
* multi, sleep on it; we might be awakened before it is completely |
| 3592 |
* gone (or even not sleep at all in some cases); we need to preserve |
| 3593 |
* it as locker, unless it is gone completely. |
| 3594 |
* |
| 3595 |
* If it's not a multi, we need to check for sleeping conditions |
| 3596 |
* before actually going to sleep. If the update doesn't conflict |
| 3597 |
* with the locks, we just continue without sleeping (but making sure |
| 3598 |
* it is preserved). |
| 3599 |
* |
| 3600 |
* Before sleeping, we need to acquire tuple lock to establish our |
| 3601 |
* priority for the tuple (see heap_lock_tuple). LockTuple will |
| 3602 |
* release us when we are next-in-line for the tuple. Note we must |
| 3603 |
* not acquire the tuple lock until we're sure we're going to sleep; |
| 3604 |
* otherwise we're open for race conditions with other transactions |
| 3605 |
* holding the tuple lock which sleep on us. |
| 3606 |
* |
| 3607 |
* If we are forced to "start over" below, we keep the tuple lock; |
| 3608 |
* this arranges that we stay at the head of the line while rechecking |
| 3609 |
* tuple state. |
| 3610 |
*/ |
| 3611 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 3612 |
{ |
| 3613 |
TransactionId update_xact; |
| 3614 |
int remain; |
| 3615 |
bool current_is_member = false; |
| 3616 |
|
| 3617 |
if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, |
| 3618 |
*lockmode, ¤t_is_member)) |
| 3619 |
{ |
| 3620 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 3621 |
|
| 3622 |
/* |
| 3623 |
* Acquire the lock, if necessary (but skip it when we're |
| 3624 |
* requesting a lock and already have one; avoids deadlock). |
| 3625 |
*/ |
| 3626 |
if (!current_is_member) |
| 3627 |
heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, |
| 3628 |
LockWaitBlock, &have_tuple_lock); |
| 3629 |
|
| 3630 |
/* wait for multixact */ |
| 3631 |
MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, |
| 3632 |
relation, &oldtup.t_self, XLTW_Update, |
| 3633 |
&remain); |
| 3634 |
checked_lockers = true; |
| 3635 |
locker_remains = remain != 0; |
| 3636 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 3637 |
|
| 3638 |
/* |
| 3639 |
* If xwait had just locked the tuple then some other xact |
| 3640 |
* could update this tuple before we get to this point. Check |
| 3641 |
* for xmax change, and start over if so. |
| 3642 |
*/ |
| 3643 |
if (xmax_infomask_changed(oldtup.t_data->t_infomask, |
| 3644 |
infomask) || |
| 3645 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data), |
| 3646 |
xwait)) |
| 3647 |
goto l2; |
| 3648 |
} |
| 3649 |
|
| 3650 |
/* |
| 3651 |
* Note that the multixact may not be done by now. It could have |
| 3652 |
* surviving members; our own xact or other subxacts of this |
| 3653 |
* backend, and also any other concurrent transaction that locked |
| 3654 |
* the tuple with LockTupleKeyShare if we only got |
| 3655 |
* LockTupleNoKeyExclusive. If this is the case, we have to be |
| 3656 |
* careful to mark the updated tuple with the surviving members in |
| 3657 |
* Xmax. |
| 3658 |
* |
| 3659 |
* Note that there could have been another update in the |
| 3660 |
* MultiXact. In that case, we need to check whether it committed |
| 3661 |
* or aborted. If it aborted we are safe to update it again; |
| 3662 |
* otherwise there is an update conflict, and we have to return |
| 3663 |
* TableTuple{Deleted, Updated} below. |
| 3664 |
* |
| 3665 |
* In the LockTupleExclusive case, we still need to preserve the |
| 3666 |
* surviving members: those would include the tuple locks we had |
| 3667 |
* before this one, which are important to keep in case this |
| 3668 |
* subxact aborts. |
| 3669 |
*/ |
| 3670 |
if (!HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask)) |
| 3671 |
update_xact = HeapTupleGetUpdateXid(oldtup.t_data); |
| 3672 |
else |
| 3673 |
update_xact = InvalidTransactionId; |
| 3674 |
|
| 3675 |
/* |
| 3676 |
* There was no UPDATE in the MultiXact; or it aborted. No |
| 3677 |
* TransactionIdIsInProgress() call needed here, since we called |
| 3678 |
* MultiXactIdWait() above. |
| 3679 |
*/ |
| 3680 |
if (!TransactionIdIsValid(update_xact) || |
| 3681 |
TransactionIdDidAbort(update_xact)) |
| 3682 |
can_continue = true; |
| 3683 |
} |
| 3684 |
else if (TransactionIdIsCurrentTransactionId(xwait)) |
| 3685 |
{ |
| 3686 |
/* |
| 3687 |
* The only locker is ourselves; we can avoid grabbing the tuple |
| 3688 |
* lock here, but must preserve our locking information. |
| 3689 |
*/ |
| 3690 |
checked_lockers = true; |
| 3691 |
locker_remains = true; |
| 3692 |
can_continue = true; |
| 3693 |
} |
| 3694 |
else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact) |
| 3695 |
{ |
| 3696 |
/* |
| 3697 |
* If it's just a key-share locker, and we're not changing the key |
| 3698 |
* columns, we don't need to wait for it to end; but we need to |
| 3699 |
* preserve it as locker. |
| 3700 |
*/ |
| 3701 |
checked_lockers = true; |
| 3702 |
locker_remains = true; |
| 3703 |
can_continue = true; |
| 3704 |
} |
| 3705 |
else |
| 3706 |
{ |
| 3707 |
/* |
| 3708 |
* Wait for regular transaction to end; but first, acquire tuple |
| 3709 |
* lock. |
| 3710 |
*/ |
| 3711 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 3712 |
heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, |
| 3713 |
LockWaitBlock, &have_tuple_lock); |
| 3714 |
XactLockTableWait(xwait, relation, &oldtup.t_self, |
| 3715 |
XLTW_Update); |
| 3716 |
checked_lockers = true; |
| 3717 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 3718 |
|
| 3719 |
/* |
| 3720 |
* xwait is done, but if xwait had just locked the tuple then some |
| 3721 |
* other xact could update this tuple before we get to this point. |
| 3722 |
* Check for xmax change, and start over if so. |
| 3723 |
*/ |
| 3724 |
if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) || |
| 3725 |
!TransactionIdEquals(xwait, |
| 3726 |
HeapTupleHeaderGetRawXmax(oldtup.t_data))) |
| 3727 |
goto l2; |
| 3728 |
|
| 3729 |
/* Otherwise check if it committed or aborted */ |
| 3730 |
UpdateXmaxHintBits(oldtup.t_data, buffer, xwait); |
| 3731 |
if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) |
| 3732 |
can_continue = true; |
| 3733 |
} |
| 3734 |
|
| 3735 |
if (can_continue) |
| 3736 |
result = TM_Ok; |
| 3737 |
else if (!ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid)) |
| 3738 |
result = TM_Updated; |
| 3739 |
else |
| 3740 |
result = TM_Deleted; |
| 3741 |
} |
| 3742 |
|
| 3743 |
/* Sanity check the result HeapTupleSatisfiesUpdate() and the logic above */ |
| 3744 |
if (result != TM_Ok) |
| 3745 |
{ |
| 3746 |
Assert(result == TM_SelfModified || |
| 3747 |
result == TM_Updated || |
| 3748 |
result == TM_Deleted || |
| 3749 |
result == TM_BeingModified); |
| 3750 |
Assert(!(oldtup.t_data->t_infomask & HEAP_XMAX_INVALID)); |
| 3751 |
Assert(result != TM_Updated || |
| 3752 |
!ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid)); |
| 3753 |
} |
| 3754 |
|
| 3755 |
if (crosscheck != InvalidSnapshot && result == TM_Ok) |
| 3756 |
{ |
| 3757 |
/* Perform additional check for transaction-snapshot mode RI updates */ |
| 3758 |
if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer)) |
| 3759 |
result = TM_Updated; |
| 3760 |
} |
| 3761 |
|
| 3762 |
if (result != TM_Ok) |
| 3763 |
{ |
| 3764 |
tmfd->ctid = oldtup.t_data->t_ctid; |
| 3765 |
tmfd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data); |
| 3766 |
if (result == TM_SelfModified) |
| 3767 |
tmfd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data); |
| 3768 |
else |
| 3769 |
tmfd->cmax = InvalidCommandId; |
| 3770 |
UnlockReleaseBuffer(buffer); |
| 3771 |
if (have_tuple_lock) |
| 3772 |
UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode); |
| 3773 |
if (vmbuffer != InvalidBuffer) |
| 3774 |
ReleaseBuffer(vmbuffer); |
| 3775 |
*update_indexes = TU_None; |
| 3776 |
|
| 3777 |
bms_free(hot_attrs); |
| 3778 |
bms_free(sum_attrs); |
| 3779 |
bms_free(key_attrs); |
| 3780 |
bms_free(id_attrs); |
| 3781 |
bms_free(modified_attrs); |
| 3782 |
bms_free(interesting_attrs); |
| 3783 |
return result; |
| 3784 |
} |
| 3785 |
|
| 3786 |
/* |
| 3787 |
* If we didn't pin the visibility map page and the page has become all |
| 3788 |
* visible while we were busy locking the buffer, or during some |
| 3789 |
* subsequent window during which we had it unlocked, we'll have to unlock |
| 3790 |
* and re-lock, to avoid holding the buffer lock across an I/O. That's a |
| 3791 |
* bit unfortunate, especially since we'll now have to recheck whether the |
| 3792 |
* tuple has been locked or updated under us, but hopefully it won't |
| 3793 |
* happen very often. |
| 3794 |
*/ |
| 3795 |
if (vmbuffer == InvalidBuffer && PageIsAllVisible(page)) |
| 3796 |
{ |
| 3797 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 3798 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 3799 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 3800 |
goto l2; |
| 3801 |
} |
| 3802 |
|
| 3803 |
/* Fill in transaction status data */ |
| 3804 |
|
| 3805 |
/* |
| 3806 |
* If the tuple we're updating is locked, we need to preserve the locking |
| 3807 |
* info in the old tuple's Xmax. Prepare a new Xmax value for this. |
| 3808 |
*/ |
| 3809 |
compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data), |
| 3810 |
oldtup.t_data->t_infomask, |
| 3811 |
oldtup.t_data->t_infomask2, |
| 3812 |
xid, *lockmode, true, |
| 3813 |
&xmax_old_tuple, &infomask_old_tuple, |
| 3814 |
&infomask2_old_tuple); |
| 3815 |
|
| 3816 |
/* |
| 3817 |
* And also prepare an Xmax value for the new copy of the tuple. If there |
| 3818 |
* was no xmax previously, or there was one but all lockers are now gone, |
| 3819 |
* then use InvalidTransactionId; otherwise, get the xmax from the old |
| 3820 |
* tuple. (In rare cases that might also be InvalidTransactionId and yet |
| 3821 |
* not have the HEAP_XMAX_INVALID bit set; that's fine.) |
| 3822 |
*/ |
| 3823 |
if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) || |
| 3824 |
HEAP_LOCKED_UPGRADED(oldtup.t_data->t_infomask) || |
| 3825 |
(checked_lockers && !locker_remains)) |
| 3826 |
xmax_new_tuple = InvalidTransactionId; |
| 3827 |
else |
| 3828 |
xmax_new_tuple = HeapTupleHeaderGetRawXmax(oldtup.t_data); |
| 3829 |
|
| 3830 |
if (!TransactionIdIsValid(xmax_new_tuple)) |
| 3831 |
{ |
| 3832 |
infomask_new_tuple = HEAP_XMAX_INVALID; |
| 3833 |
infomask2_new_tuple = 0; |
| 3834 |
} |
| 3835 |
else |
| 3836 |
{ |
| 3837 |
/* |
| 3838 |
* If we found a valid Xmax for the new tuple, then the infomask bits |
| 3839 |
* to use on the new tuple depend on what was there on the old one. |
| 3840 |
* Note that since we're doing an update, the only possibility is that |
| 3841 |
* the lockers had FOR KEY SHARE lock. |
| 3842 |
*/ |
| 3843 |
if (oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) |
| 3844 |
{ |
| 3845 |
GetMultiXactIdHintBits(xmax_new_tuple, &infomask_new_tuple, |
| 3846 |
&infomask2_new_tuple); |
| 3847 |
} |
| 3848 |
else |
| 3849 |
{ |
| 3850 |
infomask_new_tuple = HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY; |
| 3851 |
infomask2_new_tuple = 0; |
| 3852 |
} |
| 3853 |
} |
| 3854 |
|
| 3855 |
/* |
| 3856 |
* Prepare the new tuple with the appropriate initial values of Xmin and |
| 3857 |
* Xmax, as well as initial infomask bits as computed above. |
| 3858 |
*/ |
| 3859 |
newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK); |
| 3860 |
newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK); |
| 3861 |
HeapTupleHeaderSetXmin(newtup->t_data, xid); |
| 3862 |
HeapTupleHeaderSetCmin(newtup->t_data, cid); |
| 3863 |
newtup->t_data->t_infomask |= HEAP_UPDATED | infomask_new_tuple; |
| 3864 |
newtup->t_data->t_infomask2 |= infomask2_new_tuple; |
| 3865 |
HeapTupleHeaderSetXmax(newtup->t_data, xmax_new_tuple); |
| 3866 |
|
| 3867 |
/* |
| 3868 |
* Replace cid with a combo CID if necessary. Note that we already put |
| 3869 |
* the plain cid into the new tuple. |
| 3870 |
*/ |
| 3871 |
HeapTupleHeaderAdjustCmax(oldtup.t_data, &cid, &iscombo); |
| 3872 |
|
| 3873 |
/* |
| 3874 |
* If the toaster needs to be activated, OR if the new tuple will not fit |
| 3875 |
* on the same page as the old, then we need to release the content lock |
| 3876 |
* (but not the pin!) on the old tuple's buffer while we are off doing |
| 3877 |
* TOAST and/or table-file-extension work. We must mark the old tuple to |
| 3878 |
* show that it's locked, else other processes may try to update it |
| 3879 |
* themselves. |
| 3880 |
* |
| 3881 |
* We need to invoke the toaster if there are already any out-of-line |
| 3882 |
* toasted values present, or if the new tuple is over-threshold. |
| 3883 |
*/ |
| 3884 |
if (relation->rd_rel->relkind != RELKIND_RELATION && |
| 3885 |
relation->rd_rel->relkind != RELKIND_MATVIEW) |
| 3886 |
{ |
| 3887 |
/* toast table entries should never be recursively toasted */ |
| 3888 |
Assert(!HeapTupleHasExternal(&oldtup)); |
| 3889 |
Assert(!HeapTupleHasExternal(newtup)); |
| 3890 |
need_toast = false; |
| 3891 |
} |
| 3892 |
else |
| 3893 |
need_toast = (HeapTupleHasExternal(&oldtup) || |
| 3894 |
HeapTupleHasExternal(newtup) || |
| 3895 |
newtup->t_len > TOAST_TUPLE_THRESHOLD); |
| 3896 |
|
| 3897 |
pagefree = PageGetHeapFreeSpace(page); |
| 3898 |
|
| 3899 |
newtupsize = MAXALIGN(newtup->t_len); |
| 3900 |
|
| 3901 |
if (need_toast || newtupsize > pagefree) |
| 3902 |
{ |
| 3903 |
TransactionId xmax_lock_old_tuple; |
| 3904 |
uint16 infomask_lock_old_tuple, |
| 3905 |
infomask2_lock_old_tuple; |
| 3906 |
bool cleared_all_frozen = false; |
| 3907 |
|
| 3908 |
/* |
| 3909 |
* To prevent concurrent sessions from updating the tuple, we have to |
| 3910 |
* temporarily mark it locked, while we release the page-level lock. |
| 3911 |
* |
| 3912 |
* To satisfy the rule that any xid potentially appearing in a buffer |
| 3913 |
* written out to disk, we unfortunately have to WAL log this |
| 3914 |
* temporary modification. We can reuse xl_heap_lock for this |
| 3915 |
* purpose. If we crash/error before following through with the |
| 3916 |
* actual update, xmax will be of an aborted transaction, allowing |
| 3917 |
* other sessions to proceed. |
| 3918 |
*/ |
| 3919 |
|
| 3920 |
/* |
| 3921 |
* Compute xmax / infomask appropriate for locking the tuple. This has |
| 3922 |
* to be done separately from the combo that's going to be used for |
| 3923 |
* updating, because the potentially created multixact would otherwise |
| 3924 |
* be wrong. |
| 3925 |
*/ |
| 3926 |
compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data), |
| 3927 |
oldtup.t_data->t_infomask, |
| 3928 |
oldtup.t_data->t_infomask2, |
| 3929 |
xid, *lockmode, false, |
| 3930 |
&xmax_lock_old_tuple, &infomask_lock_old_tuple, |
| 3931 |
&infomask2_lock_old_tuple); |
| 3932 |
|
| 3933 |
Assert(HEAP_XMAX_IS_LOCKED_ONLY(infomask_lock_old_tuple)); |
| 3934 |
|
| 3935 |
if (PageIsAllVisible(page)) |
| 3936 |
{ |
| 3937 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 3938 |
unlock_vmbuffer = true; |
| 3939 |
} |
| 3940 |
|
| 3941 |
START_CRIT_SECTION(); |
| 3942 |
|
| 3943 |
/* Clear obsolete visibility flags ... */ |
| 3944 |
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED); |
| 3945 |
oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 3946 |
HeapTupleClearHotUpdated(&oldtup); |
| 3947 |
/* ... and store info about transaction updating this tuple */ |
| 3948 |
Assert(TransactionIdIsValid(xmax_lock_old_tuple)); |
| 3949 |
HeapTupleHeaderSetXmax(oldtup.t_data, xmax_lock_old_tuple); |
| 3950 |
oldtup.t_data->t_infomask |= infomask_lock_old_tuple; |
| 3951 |
oldtup.t_data->t_infomask2 |= infomask2_lock_old_tuple; |
| 3952 |
HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo); |
| 3953 |
|
| 3954 |
/* temporarily make it look not-updated, but locked */ |
| 3955 |
oldtup.t_data->t_ctid = oldtup.t_self; |
| 3956 |
|
| 3957 |
/* |
| 3958 |
* Clear all-frozen bit on visibility map if needed. We could |
| 3959 |
* immediately reset ALL_VISIBLE, but given that the WAL logging |
| 3960 |
* overhead would be unchanged, that doesn't seem necessarily |
| 3961 |
* worthwhile. |
| 3962 |
*/ |
| 3963 |
if (PageIsAllVisible(page)) |
| 3964 |
{ |
| 3965 |
/* It's possible all-frozen was already clear */ |
| 3966 |
if (visibilitymap_clear_locked(relation, block, vmbuffer, |
| 3967 |
VISIBILITYMAP_ALL_FROZEN)) |
| 3968 |
cleared_all_frozen = true; |
| 3969 |
} |
| 3970 |
|
| 3971 |
MarkBufferDirty(buffer); |
| 3972 |
|
| 3973 |
if (RelationNeedsWAL(relation)) |
| 3974 |
{ |
| 3975 |
xl_heap_lock xlrec; |
| 3976 |
XLogRecPtr recptr; |
| 3977 |
|
| 3978 |
XLogBeginInsert(); |
| 3979 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_HEAP, buffer, REGBUF_STANDARD); |
| 3980 |
|
| 3981 |
xlrec.offnum = ItemPointerGetOffsetNumber(&oldtup.t_self); |
| 3982 |
xlrec.xmax = xmax_lock_old_tuple; |
| 3983 |
xlrec.infobits_set = compute_infobits(oldtup.t_data->t_infomask, |
| 3984 |
oldtup.t_data->t_infomask2); |
| 3985 |
xlrec.flags = |
| 3986 |
cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0; |
| 3987 |
XLogRegisterData(&xlrec, SizeOfHeapLock); |
| 3988 |
|
| 3989 |
if (cleared_all_frozen) |
| 3990 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_VM, vmbuffer, 0); |
| 3991 |
|
| 3992 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK); |
| 3993 |
PageSetLSN(page, recptr); |
| 3994 |
|
| 3995 |
if (cleared_all_frozen) |
| 3996 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 3997 |
} |
| 3998 |
|
| 3999 |
END_CRIT_SECTION(); |
| 4000 |
|
| 4001 |
/* release VM lock first, since it covers many heap blocks */ |
| 4002 |
if (unlock_vmbuffer) |
| 4003 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 4004 |
unlock_vmbuffer = false; |
| 4005 |
|
| 4006 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 4007 |
|
| 4008 |
/* |
| 4009 |
* Let the toaster do its thing, if needed. |
| 4010 |
* |
| 4011 |
* Note: below this point, heaptup is the data we actually intend to |
| 4012 |
* store into the relation; newtup is the caller's original untoasted |
| 4013 |
* data. |
| 4014 |
*/ |
| 4015 |
if (need_toast) |
| 4016 |
{ |
| 4017 |
/* Note we always use WAL and FSM during updates */ |
| 4018 |
heaptup = heap_toast_insert_or_update(relation, newtup, &oldtup, 0); |
| 4019 |
newtupsize = MAXALIGN(heaptup->t_len); |
| 4020 |
} |
| 4021 |
else |
| 4022 |
heaptup = newtup; |
| 4023 |
|
| 4024 |
/* |
| 4025 |
* Now, do we need a new page for the tuple, or not? This is a bit |
| 4026 |
* tricky since someone else could have added tuples to the page while |
| 4027 |
* we weren't looking. We have to recheck the available space after |
| 4028 |
* reacquiring the buffer lock. But don't bother to do that if the |
| 4029 |
* former amount of free space is still not enough; it's unlikely |
| 4030 |
* there's more free now than before. |
| 4031 |
* |
| 4032 |
* What's more, if we need to get a new page, we will need to acquire |
| 4033 |
* buffer locks on both old and new pages. To avoid deadlock against |
| 4034 |
* some other backend trying to get the same two locks in the other |
| 4035 |
* order, we must be consistent about the order we get the locks in. |
| 4036 |
* We use the rule "lock the lower-numbered page of the relation |
| 4037 |
* first". To implement this, we must do RelationGetBufferForTuple |
| 4038 |
* while not holding the lock on the old page, and we must rely on it |
| 4039 |
* to get the locks on both pages in the correct order. |
| 4040 |
* |
| 4041 |
* Another consideration is that we need visibility map page pin(s) if |
| 4042 |
* we will have to clear the all-visible flag on either page. If we |
| 4043 |
* call RelationGetBufferForTuple, we rely on it to acquire any such |
| 4044 |
* pins; but if we don't, we have to handle that here. Hence we need |
| 4045 |
* a loop. |
| 4046 |
*/ |
| 4047 |
for (;;) |
| 4048 |
{ |
| 4049 |
if (newtupsize > pagefree) |
| 4050 |
{ |
| 4051 |
/* It doesn't fit, must use RelationGetBufferForTuple. */ |
| 4052 |
newbuf = RelationGetBufferForTuple(relation, heaptup->t_len, |
| 4053 |
buffer, 0, NULL, |
| 4054 |
&vmbuffer_new, &vmbuffer, |
| 4055 |
0); |
| 4056 |
/* We're all done. */ |
| 4057 |
break; |
| 4058 |
} |
| 4059 |
/* Acquire VM page pin if needed and we don't have it. */ |
| 4060 |
if (vmbuffer == InvalidBuffer && PageIsAllVisible(page)) |
| 4061 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 4062 |
/* Re-acquire the lock on the old tuple's page. */ |
| 4063 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 4064 |
/* Re-check using the up-to-date free space */ |
| 4065 |
pagefree = PageGetHeapFreeSpace(page); |
| 4066 |
if (newtupsize > pagefree || |
| 4067 |
(vmbuffer == InvalidBuffer && PageIsAllVisible(page))) |
| 4068 |
{ |
| 4069 |
/* |
| 4070 |
* Rats, it doesn't fit anymore, or somebody just now set the |
| 4071 |
* all-visible flag. We must now unlock and loop to avoid |
| 4072 |
* deadlock. Fortunately, this path should seldom be taken. |
| 4073 |
*/ |
| 4074 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 4075 |
} |
| 4076 |
else |
| 4077 |
{ |
| 4078 |
/* We're all done. */ |
| 4079 |
newbuf = buffer; |
| 4080 |
break; |
| 4081 |
} |
| 4082 |
} |
| 4083 |
} |
| 4084 |
else |
| 4085 |
{ |
| 4086 |
/* No TOAST work needed, and it'll fit on same page */ |
| 4087 |
newbuf = buffer; |
| 4088 |
heaptup = newtup; |
| 4089 |
} |
| 4090 |
|
| 4091 |
newpage = BufferGetPage(newbuf); |
| 4092 |
|
| 4093 |
/* |
| 4094 |
* We're about to do the actual update -- check for conflict first, to |
| 4095 |
* avoid possibly having to roll back work we've just done. |
| 4096 |
* |
| 4097 |
* This is safe without a recheck as long as there is no possibility of |
| 4098 |
* another process scanning the pages between this check and the update |
| 4099 |
* being visible to the scan (i.e., exclusive buffer content lock(s) are |
| 4100 |
* continuously held from this point until the tuple update is visible). |
| 4101 |
* |
| 4102 |
* For the new tuple the only check needed is at the relation level, but |
| 4103 |
* since both tuples are in the same relation and the check for oldtup |
| 4104 |
* will include checking the relation level, there is no benefit to a |
| 4105 |
* separate check for the new tuple. |
| 4106 |
*/ |
| 4107 |
CheckForSerializableConflictIn(relation, &oldtup.t_self, |
| 4108 |
BufferGetBlockNumber(buffer)); |
| 4109 |
|
| 4110 |
/* |
| 4111 |
* At this point newbuf and buffer are both pinned and locked, and newbuf |
| 4112 |
* has enough space for the new tuple. If they are the same buffer, only |
| 4113 |
* one pin is held. |
| 4114 |
*/ |
| 4115 |
|
| 4116 |
if (newbuf == buffer) |
| 4117 |
{ |
| 4118 |
/* |
| 4119 |
* Since the new tuple is going into the same page, we might be able |
| 4120 |
* to do a HOT update. Check if any of the index columns have been |
| 4121 |
* changed. |
| 4122 |
*/ |
| 4123 |
if (!bms_overlap(modified_attrs, hot_attrs)) |
| 4124 |
{ |
| 4125 |
use_hot_update = true; |
| 4126 |
|
| 4127 |
/* |
| 4128 |
* If none of the columns that are used in hot-blocking indexes |
| 4129 |
* were updated, we can apply HOT, but we do still need to check |
| 4130 |
* if we need to update the summarizing indexes, and update those |
| 4131 |
* indexes if the columns were updated, or we may fail to detect |
| 4132 |
* e.g. value bound changes in BRIN minmax indexes. |
| 4133 |
*/ |
| 4134 |
if (bms_overlap(modified_attrs, sum_attrs)) |
| 4135 |
summarized_update = true; |
| 4136 |
} |
| 4137 |
} |
| 4138 |
else |
| 4139 |
{ |
| 4140 |
/* Set a hint that the old page could use prune/defrag */ |
| 4141 |
PageSetFull(page); |
| 4142 |
} |
| 4143 |
|
| 4144 |
/* |
| 4145 |
* Compute replica identity tuple before entering the critical section so |
| 4146 |
* we don't PANIC upon a memory allocation failure. |
| 4147 |
* ExtractReplicaIdentity() will return NULL if nothing needs to be |
| 4148 |
* logged. Pass old key required as true only if the replica identity key |
| 4149 |
* columns are modified or it has external data. |
| 4150 |
*/ |
| 4151 |
old_key_tuple = ExtractReplicaIdentity(relation, &oldtup, |
| 4152 |
bms_overlap(modified_attrs, id_attrs) || |
| 4153 |
id_has_external, |
| 4154 |
&old_key_copied); |
| 4155 |
|
| 4156 |
clear_all_visible = PageIsAllVisible(page); |
| 4157 |
clear_all_visible_new = newbuf != buffer && PageIsAllVisible(newpage); |
| 4158 |
|
| 4159 |
/* |
| 4160 |
* Clear PD_ALL_VISIBLE flags and reset visibility map bits for any heap |
| 4161 |
* pages that were all-visible. If there are two heap pages, we may need |
| 4162 |
* to clear VM bits for both. |
| 4163 |
*/ |
| 4164 |
if (clear_all_visible && clear_all_visible_new && |
| 4165 |
vmbuffer_new == vmbuffer) |
| 4166 |
{ |
| 4167 |
/* |
| 4168 |
* This is the more complicated case: both the new and old heap pages |
| 4169 |
* are all-visible and both their VM bits are on the same page of the |
| 4170 |
* VM, so we register a single VM buffer as HEAP_UPDATE_BLKREF_VM_NEW |
| 4171 |
* in the WAL record. We must be careful to only lock and register one |
| 4172 |
* buffer, even though we modify it twice -- once for each heap |
| 4173 |
* block's VM bits. |
| 4174 |
*/ |
| 4175 |
LockBuffer(vmbuffer_new, BUFFER_LOCK_EXCLUSIVE); |
| 4176 |
unlock_vmbuffer_new = true; |
| 4177 |
|
| 4178 |
/* We will not lock or attempt to modify old VM buffer */ |
| 4179 |
} |
| 4180 |
else |
| 4181 |
{ |
| 4182 |
/* |
| 4183 |
* In all the remaining cases, we will clear at most one heap block's |
| 4184 |
* VM bits per VM page. |
| 4185 |
*/ |
| 4186 |
Buffer vmbuffers[2] = { |
| 4187 |
clear_all_visible ? vmbuffer : InvalidBuffer, |
| 4188 |
clear_all_visible_new ? vmbuffer_new : InvalidBuffer |
| 4189 |
}; |
| 4190 |
|
| 4191 |
/* |
| 4192 |
* When both pages need different VM pages cleared, acquire the VM |
| 4193 |
* buffer locks in VM block order to avoid deadlocks between backends |
| 4194 |
* updating tuples in opposite directions across VM pages. |
| 4195 |
*/ |
| 4196 |
if (clear_all_visible && clear_all_visible_new && |
| 4197 |
BufferGetBlockNumber(vmbuffers[0]) > BufferGetBlockNumber(vmbuffers[1])) |
| 4198 |
{ |
| 4199 |
Buffer swap = vmbuffers[0]; |
| 4200 |
|
| 4201 |
vmbuffers[0] = vmbuffers[1]; |
| 4202 |
vmbuffers[1] = swap; |
| 4203 |
} |
| 4204 |
|
| 4205 |
Assert((!BufferIsValid(vmbuffers[0]) && !BufferIsValid(vmbuffers[1])) || |
| 4206 |
vmbuffers[0] != vmbuffers[1]); |
| 4207 |
|
| 4208 |
if (BufferIsValid(vmbuffers[0])) |
| 4209 |
LockBuffer(vmbuffers[0], BUFFER_LOCK_EXCLUSIVE); |
| 4210 |
if (BufferIsValid(vmbuffers[1])) |
| 4211 |
LockBuffer(vmbuffers[1], BUFFER_LOCK_EXCLUSIVE); |
| 4212 |
|
| 4213 |
if (clear_all_visible) |
| 4214 |
unlock_vmbuffer = true; |
| 4215 |
if (clear_all_visible_new) |
| 4216 |
unlock_vmbuffer_new = true; |
| 4217 |
} |
| 4218 |
|
| 4219 |
/* NO EREPORT(ERROR) from here till changes are logged */ |
| 4220 |
START_CRIT_SECTION(); |
| 4221 |
|
| 4222 |
/* |
| 4223 |
* If this transaction commits, the old tuple will become DEAD sooner or |
| 4224 |
* later. Set flag that this page is a candidate for pruning once our xid |
| 4225 |
* falls below the OldestXmin horizon. If the transaction finally aborts, |
| 4226 |
* the subsequent page pruning will be a no-op and the hint will be |
| 4227 |
* cleared. |
| 4228 |
* |
| 4229 |
* XXX Should we set hint on newbuf as well? If the transaction aborts, |
| 4230 |
* there would be a prunable tuple in the newbuf; but for now we choose |
| 4231 |
* not to optimize for aborts. Note that heap_xlog_update must be kept in |
| 4232 |
* sync if this decision changes. |
| 4233 |
*/ |
| 4234 |
PageSetPrunable(page, xid); |
| 4235 |
|
| 4236 |
if (use_hot_update) |
| 4237 |
{ |
| 4238 |
/* Mark the old tuple as HOT-updated */ |
| 4239 |
HeapTupleSetHotUpdated(&oldtup); |
| 4240 |
/* And mark the new tuple as heap-only */ |
| 4241 |
HeapTupleSetHeapOnly(heaptup); |
| 4242 |
/* Mark the caller's copy too, in case different from heaptup */ |
| 4243 |
HeapTupleSetHeapOnly(newtup); |
| 4244 |
} |
| 4245 |
else |
| 4246 |
{ |
| 4247 |
/* Make sure tuples are correctly marked as not-HOT */ |
| 4248 |
HeapTupleClearHotUpdated(&oldtup); |
| 4249 |
HeapTupleClearHeapOnly(heaptup); |
| 4250 |
HeapTupleClearHeapOnly(newtup); |
| 4251 |
} |
| 4252 |
|
| 4253 |
RelationPutHeapTuple(relation, newbuf, heaptup, false); /* insert new tuple */ |
| 4254 |
|
| 4255 |
/* Clear obsolete visibility flags, possibly set by ourselves above... */ |
| 4256 |
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED); |
| 4257 |
oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 4258 |
/* ... and store info about transaction updating this tuple */ |
| 4259 |
Assert(TransactionIdIsValid(xmax_old_tuple)); |
| 4260 |
HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple); |
| 4261 |
oldtup.t_data->t_infomask |= infomask_old_tuple; |
| 4262 |
oldtup.t_data->t_infomask2 |= infomask2_old_tuple; |
| 4263 |
HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo); |
| 4264 |
|
| 4265 |
/* record address of new tuple in t_ctid of old one */ |
| 4266 |
oldtup.t_data->t_ctid = heaptup->t_self; |
| 4267 |
|
| 4268 |
/* |
| 4269 |
* Clear PD_ALL_VISIBLE flags and reset all visibilitymap bits. In all |
| 4270 |
* cases, it's possible that PD_ALL_VISIBLE was set but the corresponding |
| 4271 |
* visibility map bits were already clear. |
| 4272 |
*/ |
| 4273 |
if (clear_all_visible) |
| 4274 |
{ |
| 4275 |
if (visibilitymap_clear_locked(relation, block, |
| 4276 |
vmbuffer, VISIBILITYMAP_VALID_BITS)) |
| 4277 |
{ |
| 4278 |
/* |
| 4279 |
* When old and new heap blocks' VM bits are on the same VM page, |
| 4280 |
* that page is registered in the WAL record only once. If both |
| 4281 |
* heap pages were PD_ALL_VISIBLE and either VM bit needs |
| 4282 |
* clearing, we register the VM buffer as |
| 4283 |
* HEAP_UPDATE_BLKREF_VM_NEW. |
| 4284 |
*/ |
| 4285 |
if (clear_all_visible_new && vmbuffer == vmbuffer_new) |
| 4286 |
vmbuffer_new_modified = true; |
| 4287 |
else |
| 4288 |
vmbuffer_modified = true; |
| 4289 |
} |
| 4290 |
|
| 4291 |
PageClearAllVisible(page); |
| 4292 |
} |
| 4293 |
if (clear_all_visible_new) |
| 4294 |
{ |
| 4295 |
/* |
| 4296 |
* If both heap blocks' VM bits are on the same VM buffer, this will |
| 4297 |
* clear the new heap block's VM bits from the shared vmbuffer. |
| 4298 |
*/ |
| 4299 |
if (visibilitymap_clear_locked(relation, BufferGetBlockNumber(newbuf), |
| 4300 |
vmbuffer_new, VISIBILITYMAP_VALID_BITS)) |
| 4301 |
vmbuffer_new_modified = true; |
| 4302 |
|
| 4303 |
PageClearAllVisible(newpage); |
| 4304 |
} |
| 4305 |
|
| 4306 |
if (newbuf != buffer) |
| 4307 |
MarkBufferDirty(newbuf); |
| 4308 |
MarkBufferDirty(buffer); |
| 4309 |
|
| 4310 |
/* XLOG stuff */ |
| 4311 |
if (RelationNeedsWAL(relation)) |
| 4312 |
{ |
| 4313 |
XLogRecPtr recptr; |
| 4314 |
|
| 4315 |
/* |
| 4316 |
* For logical decoding we need combo CIDs to properly decode the |
| 4317 |
* catalog. |
| 4318 |
*/ |
| 4319 |
if (RelationIsAccessibleInLogicalDecoding(relation)) |
| 4320 |
{ |
| 4321 |
log_heap_new_cid(relation, &oldtup); |
| 4322 |
log_heap_new_cid(relation, heaptup); |
| 4323 |
} |
| 4324 |
|
| 4325 |
recptr = log_heap_update(relation, buffer, |
| 4326 |
vmbuffer_modified ? vmbuffer : InvalidBuffer, |
| 4327 |
newbuf, |
| 4328 |
vmbuffer_new_modified ? vmbuffer_new : InvalidBuffer, |
| 4329 |
&oldtup, heaptup, |
| 4330 |
old_key_tuple, |
| 4331 |
clear_all_visible, |
| 4332 |
clear_all_visible_new); |
| 4333 |
if (newbuf != buffer) |
| 4334 |
PageSetLSN(newpage, recptr); |
| 4335 |
PageSetLSN(BufferGetPage(buffer), recptr); |
| 4336 |
|
| 4337 |
if (vmbuffer_modified) |
| 4338 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 4339 |
if (vmbuffer_new_modified) |
| 4340 |
PageSetLSN(BufferGetPage(vmbuffer_new), recptr); |
| 4341 |
} |
| 4342 |
|
| 4343 |
END_CRIT_SECTION(); |
| 4344 |
|
| 4345 |
if (unlock_vmbuffer) |
| 4346 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 4347 |
if (unlock_vmbuffer_new) |
| 4348 |
LockBuffer(vmbuffer_new, BUFFER_LOCK_UNLOCK); |
| 4349 |
|
| 4350 |
if (newbuf != buffer) |
| 4351 |
LockBuffer(newbuf, BUFFER_LOCK_UNLOCK); |
| 4352 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 4353 |
|
| 4354 |
/* |
| 4355 |
* Mark old tuple for invalidation from system caches at next command |
| 4356 |
* boundary, and mark the new tuple for invalidation in case we abort. We |
| 4357 |
* have to do this before releasing the buffer because oldtup is in the |
| 4358 |
* buffer. (heaptup is all in local memory, but it's necessary to process |
| 4359 |
* both tuple versions in one call to inval.c so we can avoid redundant |
| 4360 |
* sinval messages.) |
| 4361 |
*/ |
| 4362 |
CacheInvalidateHeapTuple(relation, &oldtup, heaptup); |
| 4363 |
|
| 4364 |
/* Now we can release the buffer(s) */ |
| 4365 |
if (newbuf != buffer) |
| 4366 |
ReleaseBuffer(newbuf); |
| 4367 |
ReleaseBuffer(buffer); |
| 4368 |
if (BufferIsValid(vmbuffer_new)) |
| 4369 |
ReleaseBuffer(vmbuffer_new); |
| 4370 |
if (BufferIsValid(vmbuffer)) |
| 4371 |
ReleaseBuffer(vmbuffer); |
| 4372 |
|
| 4373 |
/* |
| 4374 |
* Release the lmgr tuple lock, if we had it. |
| 4375 |
*/ |
| 4376 |
if (have_tuple_lock) |
| 4377 |
UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode); |
| 4378 |
|
| 4379 |
pgstat_count_heap_update(relation, use_hot_update, newbuf != buffer); |
| 4380 |
|
| 4381 |
/* |
| 4382 |
* If heaptup is a private copy, release it. Don't forget to copy t_self |
| 4383 |
* back to the caller's image, too. |
| 4384 |
*/ |
| 4385 |
if (heaptup != newtup) |
| 4386 |
{ |
| 4387 |
newtup->t_self = heaptup->t_self; |
| 4388 |
heap_freetuple(heaptup); |
| 4389 |
} |
| 4390 |
|
| 4391 |
/* |
| 4392 |
* If it is a HOT update, the update may still need to update summarized |
| 4393 |
* indexes, lest we fail to update those summaries and get incorrect |
| 4394 |
* results (for example, minmax bounds of the block may change with this |
| 4395 |
* update). |
| 4396 |
*/ |
| 4397 |
if (use_hot_update) |
| 4398 |
{ |
| 4399 |
if (summarized_update) |
| 4400 |
*update_indexes = TU_Summarizing; |
| 4401 |
else |
| 4402 |
*update_indexes = TU_None; |
| 4403 |
} |
| 4404 |
else |
| 4405 |
*update_indexes = TU_All; |
| 4406 |
|
| 4407 |
if (old_key_tuple != NULL && old_key_copied) |
| 4408 |
heap_freetuple(old_key_tuple); |
| 4409 |
|
| 4410 |
bms_free(hot_attrs); |
| 4411 |
bms_free(sum_attrs); |
| 4412 |
bms_free(key_attrs); |
| 4413 |
bms_free(id_attrs); |
| 4414 |
bms_free(modified_attrs); |
| 4415 |
bms_free(interesting_attrs); |
| 4416 |
|
| 4417 |
return TM_Ok; |
| 4418 |
} |
| 4419 |
|
| 4420 |
#ifdef USE_ASSERT_CHECKING |
| 4421 |
/* |
| 4422 |
* Confirm adequate lock held during heap_update(), per rules from |
| 4423 |
* README.tuplock section "Locking to write inplace-updated tables". |
| 4424 |
*/ |
| 4425 |
static void |
| 4426 |
check_lock_if_inplace_updateable_rel(Relation relation, |
| 4427 |
ItemPointer otid, |
| 4428 |
HeapTuple newtup) |
| 4429 |
{ |
| 4430 |
/* LOCKTAG_TUPLE acceptable for any catalog */ |
| 4431 |
switch (RelationGetRelid(relation)) |
| 4432 |
{ |
| 4433 |
case RelationRelationId: |
| 4434 |
case DatabaseRelationId: |
| 4435 |
{ |
| 4436 |
LOCKTAG tuptag; |
| 4437 |
|
| 4438 |
SET_LOCKTAG_TUPLE(tuptag, |
| 4439 |
relation->rd_lockInfo.lockRelId.dbId, |
| 4440 |
relation->rd_lockInfo.lockRelId.relId, |
| 4441 |
ItemPointerGetBlockNumber(otid), |
| 4442 |
ItemPointerGetOffsetNumber(otid)); |
| 4443 |
if (LockHeldByMe(&tuptag, InplaceUpdateTupleLock, false)) |
| 4444 |
return; |
| 4445 |
} |
| 4446 |
break; |
| 4447 |
default: |
| 4448 |
Assert(!IsInplaceUpdateRelation(relation)); |
| 4449 |
return; |
| 4450 |
} |
| 4451 |
|
| 4452 |
switch (RelationGetRelid(relation)) |
| 4453 |
{ |
| 4454 |
case RelationRelationId: |
| 4455 |
{ |
| 4456 |
/* LOCKTAG_TUPLE or LOCKTAG_RELATION ok */ |
| 4457 |
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(newtup); |
| 4458 |
Oid relid = classForm->oid; |
| 4459 |
Oid dbid; |
| 4460 |
LOCKTAG tag; |
| 4461 |
|
| 4462 |
if (IsSharedRelation(relid)) |
| 4463 |
dbid = InvalidOid; |
| 4464 |
else |
| 4465 |
dbid = MyDatabaseId; |
| 4466 |
|
| 4467 |
if (classForm->relkind == RELKIND_INDEX) |
| 4468 |
{ |
| 4469 |
Relation irel = index_open(relid, AccessShareLock); |
| 4470 |
|
| 4471 |
SET_LOCKTAG_RELATION(tag, dbid, irel->rd_index->indrelid); |
| 4472 |
index_close(irel, AccessShareLock); |
| 4473 |
} |
| 4474 |
else |
| 4475 |
SET_LOCKTAG_RELATION(tag, dbid, relid); |
| 4476 |
|
| 4477 |
if (!LockHeldByMe(&tag, ShareUpdateExclusiveLock, false) && |
| 4478 |
!LockHeldByMe(&tag, ShareRowExclusiveLock, true)) |
| 4479 |
elog(WARNING, |
| 4480 |
"missing lock for relation \"%s\" (OID %u, relkind %c) @ TID (%u,%u)", |
| 4481 |
NameStr(classForm->relname), |
| 4482 |
relid, |
| 4483 |
classForm->relkind, |
| 4484 |
ItemPointerGetBlockNumber(otid), |
| 4485 |
ItemPointerGetOffsetNumber(otid)); |
| 4486 |
} |
| 4487 |
break; |
| 4488 |
case DatabaseRelationId: |
| 4489 |
{ |
| 4490 |
/* LOCKTAG_TUPLE required */ |
| 4491 |
Form_pg_database dbForm = (Form_pg_database) GETSTRUCT(newtup); |
| 4492 |
|
| 4493 |
elog(WARNING, |
| 4494 |
"missing lock on database \"%s\" (OID %u) @ TID (%u,%u)", |
| 4495 |
NameStr(dbForm->datname), |
| 4496 |
dbForm->oid, |
| 4497 |
ItemPointerGetBlockNumber(otid), |
| 4498 |
ItemPointerGetOffsetNumber(otid)); |
| 4499 |
} |
| 4500 |
break; |
| 4501 |
} |
| 4502 |
} |
| 4503 |
|
| 4504 |
/* |
| 4505 |
* Confirm adequate relation lock held, per rules from README.tuplock section |
| 4506 |
* "Locking to write inplace-updated tables". |
| 4507 |
*/ |
| 4508 |
static void |
| 4509 |
check_inplace_rel_lock(HeapTuple oldtup) |
| 4510 |
{ |
| 4511 |
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(oldtup); |
| 4512 |
Oid relid = classForm->oid; |
| 4513 |
Oid dbid; |
| 4514 |
LOCKTAG tag; |
| 4515 |
|
| 4516 |
if (IsSharedRelation(relid)) |
| 4517 |
dbid = InvalidOid; |
| 4518 |
else |
| 4519 |
dbid = MyDatabaseId; |
| 4520 |
|
| 4521 |
if (classForm->relkind == RELKIND_INDEX) |
| 4522 |
{ |
| 4523 |
Relation irel = index_open(relid, AccessShareLock); |
| 4524 |
|
| 4525 |
SET_LOCKTAG_RELATION(tag, dbid, irel->rd_index->indrelid); |
| 4526 |
index_close(irel, AccessShareLock); |
| 4527 |
} |
| 4528 |
else |
| 4529 |
SET_LOCKTAG_RELATION(tag, dbid, relid); |
| 4530 |
|
| 4531 |
if (!LockHeldByMe(&tag, ShareUpdateExclusiveLock, true)) |
| 4532 |
elog(WARNING, |
| 4533 |
"missing lock for relation \"%s\" (OID %u, relkind %c) @ TID (%u,%u)", |
| 4534 |
NameStr(classForm->relname), |
| 4535 |
relid, |
| 4536 |
classForm->relkind, |
| 4537 |
ItemPointerGetBlockNumber(&oldtup->t_self), |
| 4538 |
ItemPointerGetOffsetNumber(&oldtup->t_self)); |
| 4539 |
} |
| 4540 |
#endif |
| 4541 |
|
| 4542 |
/* |
| 4543 |
* Check if the specified attribute's values are the same. Subroutine for |
| 4544 |
* HeapDetermineColumnsInfo. |
| 4545 |
*/ |
| 4546 |
static bool |
| 4547 |
heap_attr_equals(TupleDesc tupdesc, int attrnum, Datum value1, Datum value2, |
| 4548 |
bool isnull1, bool isnull2) |
| 4549 |
{ |
| 4550 |
/* |
| 4551 |
* If one value is NULL and other is not, then they are certainly not |
| 4552 |
* equal |
| 4553 |
*/ |
| 4554 |
if (isnull1 != isnull2) |
| 4555 |
return false; |
| 4556 |
|
| 4557 |
/* |
| 4558 |
* If both are NULL, they can be considered equal. |
| 4559 |
*/ |
| 4560 |
if (isnull1) |
| 4561 |
return true; |
| 4562 |
|
| 4563 |
/* |
| 4564 |
* We do simple binary comparison of the two datums. This may be overly |
| 4565 |
* strict because there can be multiple binary representations for the |
| 4566 |
* same logical value. But we should be OK as long as there are no false |
| 4567 |
* positives. Using a type-specific equality operator is messy because |
| 4568 |
* there could be multiple notions of equality in different operator |
| 4569 |
* classes; furthermore, we cannot safely invoke user-defined functions |
| 4570 |
* while holding exclusive buffer lock. |
| 4571 |
*/ |
| 4572 |
if (attrnum <= 0) |
| 4573 |
{ |
| 4574 |
/* The only allowed system columns are OIDs, so do this */ |
| 4575 |
return (DatumGetObjectId(value1) == DatumGetObjectId(value2)); |
| 4576 |
} |
| 4577 |
else |
| 4578 |
{ |
| 4579 |
CompactAttribute *att; |
| 4580 |
|
| 4581 |
Assert(attrnum <= tupdesc->natts); |
| 4582 |
att = TupleDescCompactAttr(tupdesc, attrnum - 1); |
| 4583 |
return datumIsEqual(value1, value2, att->attbyval, att->attlen); |
| 4584 |
} |
| 4585 |
} |
| 4586 |
|
| 4587 |
/* |
| 4588 |
* Check which columns are being updated. |
| 4589 |
* |
| 4590 |
* Given an updated tuple, determine (and return into the output bitmapset), |
| 4591 |
* from those listed as interesting, the set of columns that changed. |
| 4592 |
* |
| 4593 |
* has_external indicates if any of the unmodified attributes (from those |
| 4594 |
* listed as interesting) of the old tuple is a member of external_cols and is |
| 4595 |
* stored externally. |
| 4596 |
*/ |
| 4597 |
static Bitmapset * |
| 4598 |
HeapDetermineColumnsInfo(Relation relation, |
| 4599 |
Bitmapset *interesting_cols, |
| 4600 |
Bitmapset *external_cols, |
| 4601 |
HeapTuple oldtup, HeapTuple newtup, |
| 4602 |
bool *has_external) |
| 4603 |
{ |
| 4604 |
int attidx; |
| 4605 |
Bitmapset *modified = NULL; |
| 4606 |
TupleDesc tupdesc = RelationGetDescr(relation); |
| 4607 |
|
| 4608 |
attidx = -1; |
| 4609 |
while ((attidx = bms_next_member(interesting_cols, attidx)) >= 0) |
| 4610 |
{ |
| 4611 |
/* attidx is zero-based, attrnum is the normal attribute number */ |
| 4612 |
AttrNumber attrnum = attidx + FirstLowInvalidHeapAttributeNumber; |
| 4613 |
Datum value1, |
| 4614 |
value2; |
| 4615 |
bool isnull1, |
| 4616 |
isnull2; |
| 4617 |
|
| 4618 |
/* |
| 4619 |
* If it's a whole-tuple reference, say "not equal". It's not really |
| 4620 |
* worth supporting this case, since it could only succeed after a |
| 4621 |
* no-op update, which is hardly a case worth optimizing for. |
| 4622 |
*/ |
| 4623 |
if (attrnum == 0) |
| 4624 |
{ |
| 4625 |
modified = bms_add_member(modified, attidx); |
| 4626 |
continue; |
| 4627 |
} |
| 4628 |
|
| 4629 |
/* |
| 4630 |
* Likewise, automatically say "not equal" for any system attribute |
| 4631 |
* other than tableOID; we cannot expect these to be consistent in a |
| 4632 |
* HOT chain, or even to be set correctly yet in the new tuple. |
| 4633 |
*/ |
| 4634 |
if (attrnum < 0) |
| 4635 |
{ |
| 4636 |
if (attrnum != TableOidAttributeNumber) |
| 4637 |
{ |
| 4638 |
modified = bms_add_member(modified, attidx); |
| 4639 |
continue; |
| 4640 |
} |
| 4641 |
} |
| 4642 |
|
| 4643 |
/* |
| 4644 |
* Extract the corresponding values. XXX this is pretty inefficient |
| 4645 |
* if there are many indexed columns. Should we do a single |
| 4646 |
* heap_deform_tuple call on each tuple, instead? But that doesn't |
| 4647 |
* work for system columns ... |
| 4648 |
*/ |
| 4649 |
value1 = heap_getattr(oldtup, attrnum, tupdesc, &isnull1); |
| 4650 |
value2 = heap_getattr(newtup, attrnum, tupdesc, &isnull2); |
| 4651 |
|
| 4652 |
if (!heap_attr_equals(tupdesc, attrnum, value1, |
| 4653 |
value2, isnull1, isnull2)) |
| 4654 |
{ |
| 4655 |
modified = bms_add_member(modified, attidx); |
| 4656 |
continue; |
| 4657 |
} |
| 4658 |
|
| 4659 |
/* |
| 4660 |
* No need to check attributes that can't be stored externally. Note |
| 4661 |
* that system attributes can't be stored externally. |
| 4662 |
*/ |
| 4663 |
if (attrnum < 0 || isnull1 || |
| 4664 |
TupleDescCompactAttr(tupdesc, attrnum - 1)->attlen != -1) |
| 4665 |
continue; |
| 4666 |
|
| 4667 |
/* |
| 4668 |
* Check if the old tuple's attribute is stored externally and is a |
| 4669 |
* member of external_cols. |
| 4670 |
*/ |
| 4671 |
if (VARATT_IS_EXTERNAL((struct varlena *) DatumGetPointer(value1)) && |
| 4672 |
bms_is_member(attidx, external_cols)) |
| 4673 |
*has_external = true; |
| 4674 |
} |
| 4675 |
|
| 4676 |
return modified; |
| 4677 |
} |
| 4678 |
|
| 4679 |
/* |
| 4680 |
* simple_heap_update - replace a tuple |
| 4681 |
* |
| 4682 |
* This routine may be used to update a tuple when concurrent updates of |
| 4683 |
* the target tuple are not expected (for example, because we have a lock |
| 4684 |
* on the relation associated with the tuple). Any failure is reported |
| 4685 |
* via ereport(). |
| 4686 |
*/ |
| 4687 |
void |
| 4688 |
simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup, |
| 4689 |
TU_UpdateIndexes *update_indexes) |
| 4690 |
{ |
| 4691 |
TM_Result result; |
| 4692 |
TM_FailureData tmfd; |
| 4693 |
LockTupleMode lockmode; |
| 4694 |
|
| 4695 |
result = heap_update(relation, otid, tup, |
| 4696 |
GetCurrentCommandId(true), InvalidSnapshot, |
| 4697 |
true /* wait for commit */ , |
| 4698 |
&tmfd, &lockmode, update_indexes); |
| 4699 |
switch (result) |
| 4700 |
{ |
| 4701 |
case TM_SelfModified: |
| 4702 |
/* Tuple was already updated in current command? */ |
| 4703 |
elog(ERROR, "tuple already updated by self"); |
| 4704 |
break; |
| 4705 |
|
| 4706 |
case TM_Ok: |
| 4707 |
/* done successfully */ |
| 4708 |
break; |
| 4709 |
|
| 4710 |
case TM_Updated: |
| 4711 |
elog(ERROR, "tuple concurrently updated"); |
| 4712 |
break; |
| 4713 |
|
| 4714 |
case TM_Deleted: |
| 4715 |
elog(ERROR, "tuple concurrently deleted"); |
| 4716 |
break; |
| 4717 |
|
| 4718 |
default: |
| 4719 |
elog(ERROR, "unrecognized heap_update status: %u", result); |
| 4720 |
break; |
| 4721 |
} |
| 4722 |
} |
| 4723 |
|
| 4724 |
|
| 4725 |
/* |
| 4726 |
* Return the MultiXactStatus corresponding to the given tuple lock mode. |
| 4727 |
*/ |
| 4728 |
static MultiXactStatus |
| 4729 |
get_mxact_status_for_lock(LockTupleMode mode, bool is_update) |
| 4730 |
{ |
| 4731 |
int retval; |
| 4732 |
|
| 4733 |
if (is_update) |
| 4734 |
retval = tupleLockExtraInfo[mode].updstatus; |
| 4735 |
else |
| 4736 |
retval = tupleLockExtraInfo[mode].lockstatus; |
| 4737 |
|
| 4738 |
if (retval == -1) |
| 4739 |
elog(ERROR, "invalid lock tuple mode %d/%s", mode, |
| 4740 |
is_update ? "true" : "false"); |
| 4741 |
|
| 4742 |
return (MultiXactStatus) retval; |
| 4743 |
} |
| 4744 |
|
| 4745 |
/* |
| 4746 |
* heap_lock_tuple - lock a tuple in shared or exclusive mode |
| 4747 |
* |
| 4748 |
* Note that this acquires a buffer pin, which the caller must release. |
| 4749 |
* |
| 4750 |
* Input parameters: |
| 4751 |
* relation: relation containing tuple (caller must hold suitable lock) |
| 4752 |
* tid: TID of tuple to lock |
| 4753 |
* cid: current command ID (used for visibility test, and stored into |
| 4754 |
* tuple's cmax if lock is successful) |
| 4755 |
* mode: indicates if shared or exclusive tuple lock is desired |
| 4756 |
* wait_policy: what to do if tuple lock is not available |
| 4757 |
* follow_updates: if true, follow the update chain to also lock descendant |
| 4758 |
* tuples. |
| 4759 |
* |
| 4760 |
* Output parameters: |
| 4761 |
* *tuple: all fields filled in |
| 4762 |
* *buffer: set to buffer holding tuple (pinned but not locked at exit) |
| 4763 |
* *tmfd: filled in failure cases (see below) |
| 4764 |
* |
| 4765 |
* Function results are the same as the ones for table_tuple_lock(). |
| 4766 |
* |
| 4767 |
* In the failure cases other than TM_Invisible, the routine fills |
| 4768 |
* *tmfd with the tuple's t_ctid, t_xmax (resolving a possible MultiXact, |
| 4769 |
* if necessary), and t_cmax (the last only for TM_SelfModified, |
| 4770 |
* since we cannot obtain cmax from a combo CID generated by another |
| 4771 |
* transaction). |
| 4772 |
* See comments for struct TM_FailureData for additional info. |
| 4773 |
* |
| 4774 |
* See README.tuplock for a thorough explanation of this mechanism. |
| 4775 |
*/ |
| 4776 |
TM_Result |
| 4777 |
heap_lock_tuple(Relation relation, HeapTuple tuple, |
| 4778 |
CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, |
| 4779 |
bool follow_updates, |
| 4780 |
Buffer *buffer, TM_FailureData *tmfd) |
| 4781 |
{ |
| 4782 |
TM_Result result; |
| 4783 |
ItemPointer tid = &(tuple->t_self); |
| 4784 |
ItemId lp; |
| 4785 |
Page page; |
| 4786 |
Buffer vmbuffer = InvalidBuffer; |
| 4787 |
bool unlock_vmbuffer = false; |
| 4788 |
BlockNumber block; |
| 4789 |
TransactionId xid, |
| 4790 |
xmax; |
| 4791 |
uint16 old_infomask, |
| 4792 |
new_infomask, |
| 4793 |
new_infomask2; |
| 4794 |
bool first_time = true; |
| 4795 |
bool skip_tuple_lock = false; |
| 4796 |
bool have_tuple_lock = false; |
| 4797 |
bool cleared_all_frozen = false; |
| 4798 |
|
| 4799 |
*buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); |
| 4800 |
block = ItemPointerGetBlockNumber(tid); |
| 4801 |
page = BufferGetPage(*buffer); |
| 4802 |
|
| 4803 |
/* |
| 4804 |
* Before locking the buffer, pin the visibility map page if it appears to |
| 4805 |
* be necessary. Since we haven't got the lock yet, someone else might be |
| 4806 |
* in the middle of changing this, so we'll need to recheck after we have |
| 4807 |
* the lock. |
| 4808 |
*/ |
| 4809 |
if (PageIsAllVisible(page)) |
| 4810 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 4811 |
|
| 4812 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 4813 |
|
| 4814 |
lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid)); |
| 4815 |
Assert(ItemIdIsNormal(lp)); |
| 4816 |
|
| 4817 |
tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 4818 |
tuple->t_len = ItemIdGetLength(lp); |
| 4819 |
tuple->t_tableOid = RelationGetRelid(relation); |
| 4820 |
|
| 4821 |
l3: |
| 4822 |
result = HeapTupleSatisfiesUpdate(tuple, cid, *buffer); |
| 4823 |
|
| 4824 |
if (result == TM_Invisible) |
| 4825 |
{ |
| 4826 |
/* |
| 4827 |
* This is possible, but only when locking a tuple for ON CONFLICT |
| 4828 |
* UPDATE. We return this value here rather than throwing an error in |
| 4829 |
* order to give that case the opportunity to throw a more specific |
| 4830 |
* error. |
| 4831 |
*/ |
| 4832 |
result = TM_Invisible; |
| 4833 |
goto out_locked; |
| 4834 |
} |
| 4835 |
else if (result == TM_BeingModified || |
| 4836 |
result == TM_Updated || |
| 4837 |
result == TM_Deleted) |
| 4838 |
{ |
| 4839 |
TransactionId xwait; |
| 4840 |
uint16 infomask; |
| 4841 |
uint16 infomask2; |
| 4842 |
bool require_sleep; |
| 4843 |
ItemPointerData t_ctid; |
| 4844 |
|
| 4845 |
/* must copy state data before unlocking buffer */ |
| 4846 |
xwait = HeapTupleHeaderGetRawXmax(tuple->t_data); |
| 4847 |
infomask = tuple->t_data->t_infomask; |
| 4848 |
infomask2 = tuple->t_data->t_infomask2; |
| 4849 |
ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid); |
| 4850 |
|
| 4851 |
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); |
| 4852 |
|
| 4853 |
/* |
| 4854 |
* If any subtransaction of the current top transaction already holds |
| 4855 |
* a lock as strong as or stronger than what we're requesting, we |
| 4856 |
* effectively hold the desired lock already. We *must* succeed |
| 4857 |
* without trying to take the tuple lock, else we will deadlock |
| 4858 |
* against anyone wanting to acquire a stronger lock. |
| 4859 |
* |
| 4860 |
* Note we only do this the first time we loop on the HTSU result; |
| 4861 |
* there is no point in testing in subsequent passes, because |
| 4862 |
* evidently our own transaction cannot have acquired a new lock after |
| 4863 |
* the first time we checked. |
| 4864 |
*/ |
| 4865 |
if (first_time) |
| 4866 |
{ |
| 4867 |
first_time = false; |
| 4868 |
|
| 4869 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 4870 |
{ |
| 4871 |
int i; |
| 4872 |
int nmembers; |
| 4873 |
MultiXactMember *members; |
| 4874 |
|
| 4875 |
/* |
| 4876 |
* We don't need to allow old multixacts here; if that had |
| 4877 |
* been the case, HeapTupleSatisfiesUpdate would have returned |
| 4878 |
* MayBeUpdated and we wouldn't be here. |
| 4879 |
*/ |
| 4880 |
nmembers = |
| 4881 |
GetMultiXactIdMembers(xwait, &members, false, |
| 4882 |
HEAP_XMAX_IS_LOCKED_ONLY(infomask)); |
| 4883 |
|
| 4884 |
for (i = 0; i < nmembers; i++) |
| 4885 |
{ |
| 4886 |
/* only consider members of our own transaction */ |
| 4887 |
if (!TransactionIdIsCurrentTransactionId(members[i].xid)) |
| 4888 |
continue; |
| 4889 |
|
| 4890 |
if (TUPLOCK_from_mxstatus(members[i].status) >= mode) |
| 4891 |
{ |
| 4892 |
pfree(members); |
| 4893 |
result = TM_Ok; |
| 4894 |
goto out_unlocked; |
| 4895 |
} |
| 4896 |
else |
| 4897 |
{ |
| 4898 |
/* |
| 4899 |
* Disable acquisition of the heavyweight tuple lock. |
| 4900 |
* Otherwise, when promoting a weaker lock, we might |
| 4901 |
* deadlock with another locker that has acquired the |
| 4902 |
* heavyweight tuple lock and is waiting for our |
| 4903 |
* transaction to finish. |
| 4904 |
* |
| 4905 |
* Note that in this case we still need to wait for |
| 4906 |
* the multixact if required, to avoid acquiring |
| 4907 |
* conflicting locks. |
| 4908 |
*/ |
| 4909 |
skip_tuple_lock = true; |
| 4910 |
} |
| 4911 |
} |
| 4912 |
|
| 4913 |
if (members) |
| 4914 |
pfree(members); |
| 4915 |
} |
| 4916 |
else if (TransactionIdIsCurrentTransactionId(xwait)) |
| 4917 |
{ |
| 4918 |
switch (mode) |
| 4919 |
{ |
| 4920 |
case LockTupleKeyShare: |
| 4921 |
Assert(HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) || |
| 4922 |
HEAP_XMAX_IS_SHR_LOCKED(infomask) || |
| 4923 |
HEAP_XMAX_IS_EXCL_LOCKED(infomask)); |
| 4924 |
result = TM_Ok; |
| 4925 |
goto out_unlocked; |
| 4926 |
case LockTupleShare: |
| 4927 |
if (HEAP_XMAX_IS_SHR_LOCKED(infomask) || |
| 4928 |
HEAP_XMAX_IS_EXCL_LOCKED(infomask)) |
| 4929 |
{ |
| 4930 |
result = TM_Ok; |
| 4931 |
goto out_unlocked; |
| 4932 |
} |
| 4933 |
break; |
| 4934 |
case LockTupleNoKeyExclusive: |
| 4935 |
if (HEAP_XMAX_IS_EXCL_LOCKED(infomask)) |
| 4936 |
{ |
| 4937 |
result = TM_Ok; |
| 4938 |
goto out_unlocked; |
| 4939 |
} |
| 4940 |
break; |
| 4941 |
case LockTupleExclusive: |
| 4942 |
if (HEAP_XMAX_IS_EXCL_LOCKED(infomask) && |
| 4943 |
infomask2 & HEAP_KEYS_UPDATED) |
| 4944 |
{ |
| 4945 |
result = TM_Ok; |
| 4946 |
goto out_unlocked; |
| 4947 |
} |
| 4948 |
break; |
| 4949 |
} |
| 4950 |
} |
| 4951 |
} |
| 4952 |
|
| 4953 |
/* |
| 4954 |
* Initially assume that we will have to wait for the locking |
| 4955 |
* transaction(s) to finish. We check various cases below in which |
| 4956 |
* this can be turned off. |
| 4957 |
*/ |
| 4958 |
require_sleep = true; |
| 4959 |
if (mode == LockTupleKeyShare) |
| 4960 |
{ |
| 4961 |
/* |
| 4962 |
* If we're requesting KeyShare, and there's no update present, we |
| 4963 |
* don't need to wait. Even if there is an update, we can still |
| 4964 |
* continue if the key hasn't been modified. |
| 4965 |
* |
| 4966 |
* However, if there are updates, we need to walk the update chain |
| 4967 |
* to mark future versions of the row as locked, too. That way, |
| 4968 |
* if somebody deletes that future version, we're protected |
| 4969 |
* against the key going away. This locking of future versions |
| 4970 |
* could block momentarily, if a concurrent transaction is |
| 4971 |
* deleting a key; or it could return a value to the effect that |
| 4972 |
* the transaction deleting the key has already committed. So we |
| 4973 |
* do this before re-locking the buffer; otherwise this would be |
| 4974 |
* prone to deadlocks. |
| 4975 |
* |
| 4976 |
* Note that the TID we're locking was grabbed before we unlocked |
| 4977 |
* the buffer. For it to change while we're not looking, the |
| 4978 |
* other properties we're testing for below after re-locking the |
| 4979 |
* buffer would also change, in which case we would restart this |
| 4980 |
* loop above. |
| 4981 |
*/ |
| 4982 |
if (!(infomask2 & HEAP_KEYS_UPDATED)) |
| 4983 |
{ |
| 4984 |
bool updated; |
| 4985 |
|
| 4986 |
updated = !HEAP_XMAX_IS_LOCKED_ONLY(infomask); |
| 4987 |
|
| 4988 |
/* |
| 4989 |
* If there are updates, follow the update chain; bail out if |
| 4990 |
* that cannot be done. |
| 4991 |
*/ |
| 4992 |
if (follow_updates && updated && |
| 4993 |
!ItemPointerEquals(&tuple->t_self, &t_ctid)) |
| 4994 |
{ |
| 4995 |
TM_Result res; |
| 4996 |
|
| 4997 |
res = heap_lock_updated_tuple(relation, |
| 4998 |
infomask, xwait, &t_ctid, |
| 4999 |
GetCurrentTransactionId(), |
| 5000 |
mode); |
| 5001 |
if (res != TM_Ok) |
| 5002 |
{ |
| 5003 |
result = res; |
| 5004 |
/* recovery code expects to have buffer lock held */ |
| 5005 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5006 |
goto failed; |
| 5007 |
} |
| 5008 |
} |
| 5009 |
|
| 5010 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5011 |
|
| 5012 |
/* |
| 5013 |
* Make sure it's still an appropriate lock, else start over. |
| 5014 |
* Also, if it wasn't updated before we released the lock, but |
| 5015 |
* is updated now, we start over too; the reason is that we |
| 5016 |
* now need to follow the update chain to lock the new |
| 5017 |
* versions. |
| 5018 |
*/ |
| 5019 |
if (!HeapTupleHeaderIsOnlyLocked(tuple->t_data) && |
| 5020 |
((tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED) || |
| 5021 |
!updated)) |
| 5022 |
goto l3; |
| 5023 |
|
| 5024 |
/* Things look okay, so we can skip sleeping */ |
| 5025 |
require_sleep = false; |
| 5026 |
|
| 5027 |
/* |
| 5028 |
* Note we allow Xmax to change here; other updaters/lockers |
| 5029 |
* could have modified it before we grabbed the buffer lock. |
| 5030 |
* However, this is not a problem, because with the recheck we |
| 5031 |
* just did we ensure that they still don't conflict with the |
| 5032 |
* lock we want. |
| 5033 |
*/ |
| 5034 |
} |
| 5035 |
} |
| 5036 |
else if (mode == LockTupleShare) |
| 5037 |
{ |
| 5038 |
/* |
| 5039 |
* If we're requesting Share, we can similarly avoid sleeping if |
| 5040 |
* there's no update and no exclusive lock present. |
| 5041 |
*/ |
| 5042 |
if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) && |
| 5043 |
!HEAP_XMAX_IS_EXCL_LOCKED(infomask)) |
| 5044 |
{ |
| 5045 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5046 |
|
| 5047 |
/* |
| 5048 |
* Make sure it's still an appropriate lock, else start over. |
| 5049 |
* See above about allowing xmax to change. |
| 5050 |
*/ |
| 5051 |
if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) || |
| 5052 |
HEAP_XMAX_IS_EXCL_LOCKED(tuple->t_data->t_infomask)) |
| 5053 |
goto l3; |
| 5054 |
require_sleep = false; |
| 5055 |
} |
| 5056 |
} |
| 5057 |
else if (mode == LockTupleNoKeyExclusive) |
| 5058 |
{ |
| 5059 |
/* |
| 5060 |
* If we're requesting NoKeyExclusive, we might also be able to |
| 5061 |
* avoid sleeping; just ensure that there no conflicting lock |
| 5062 |
* already acquired. |
| 5063 |
*/ |
| 5064 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 5065 |
{ |
| 5066 |
if (!DoesMultiXactIdConflict((MultiXactId) xwait, infomask, |
| 5067 |
mode, NULL)) |
| 5068 |
{ |
| 5069 |
/* |
| 5070 |
* No conflict, but if the xmax changed under us in the |
| 5071 |
* meantime, start over. |
| 5072 |
*/ |
| 5073 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5074 |
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || |
| 5075 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), |
| 5076 |
xwait)) |
| 5077 |
goto l3; |
| 5078 |
|
| 5079 |
/* otherwise, we're good */ |
| 5080 |
require_sleep = false; |
| 5081 |
} |
| 5082 |
} |
| 5083 |
else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)) |
| 5084 |
{ |
| 5085 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5086 |
|
| 5087 |
/* if the xmax changed in the meantime, start over */ |
| 5088 |
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || |
| 5089 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), |
| 5090 |
xwait)) |
| 5091 |
goto l3; |
| 5092 |
/* otherwise, we're good */ |
| 5093 |
require_sleep = false; |
| 5094 |
} |
| 5095 |
} |
| 5096 |
|
| 5097 |
/* |
| 5098 |
* As a check independent from those above, we can also avoid sleeping |
| 5099 |
* if the current transaction is the sole locker of the tuple. Note |
| 5100 |
* that the strength of the lock already held is irrelevant; this is |
| 5101 |
* not about recording the lock in Xmax (which will be done regardless |
| 5102 |
* of this optimization, below). Also, note that the cases where we |
| 5103 |
* hold a lock stronger than we are requesting are already handled |
| 5104 |
* above by not doing anything. |
| 5105 |
* |
| 5106 |
* Note we only deal with the non-multixact case here; MultiXactIdWait |
| 5107 |
* is well equipped to deal with this situation on its own. |
| 5108 |
*/ |
| 5109 |
if (require_sleep && !(infomask & HEAP_XMAX_IS_MULTI) && |
| 5110 |
TransactionIdIsCurrentTransactionId(xwait)) |
| 5111 |
{ |
| 5112 |
/* ... but if the xmax changed in the meantime, start over */ |
| 5113 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5114 |
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || |
| 5115 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), |
| 5116 |
xwait)) |
| 5117 |
goto l3; |
| 5118 |
Assert(HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask)); |
| 5119 |
require_sleep = false; |
| 5120 |
} |
| 5121 |
|
| 5122 |
/* |
| 5123 |
* Time to sleep on the other transaction/multixact, if necessary. |
| 5124 |
* |
| 5125 |
* If the other transaction is an update/delete that's already |
| 5126 |
* committed, then sleeping cannot possibly do any good: if we're |
| 5127 |
* required to sleep, get out to raise an error instead. |
| 5128 |
* |
| 5129 |
* By here, we either have already acquired the buffer exclusive lock, |
| 5130 |
* or we must wait for the locking transaction or multixact; so below |
| 5131 |
* we ensure that we grab buffer lock after the sleep. |
| 5132 |
*/ |
| 5133 |
if (require_sleep && (result == TM_Updated || result == TM_Deleted)) |
| 5134 |
{ |
| 5135 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5136 |
goto failed; |
| 5137 |
} |
| 5138 |
else if (require_sleep) |
| 5139 |
{ |
| 5140 |
/* |
| 5141 |
* Acquire tuple lock to establish our priority for the tuple, or |
| 5142 |
* die trying. LockTuple will release us when we are next-in-line |
| 5143 |
* for the tuple. We must do this even if we are share-locking, |
| 5144 |
* but not if we already have a weaker lock on the tuple. |
| 5145 |
* |
| 5146 |
* If we are forced to "start over" below, we keep the tuple lock; |
| 5147 |
* this arranges that we stay at the head of the line while |
| 5148 |
* rechecking tuple state. |
| 5149 |
*/ |
| 5150 |
if (!skip_tuple_lock && |
| 5151 |
!heap_acquire_tuplock(relation, tid, mode, wait_policy, |
| 5152 |
&have_tuple_lock)) |
| 5153 |
{ |
| 5154 |
/* |
| 5155 |
* This can only happen if wait_policy is Skip and the lock |
| 5156 |
* couldn't be obtained. |
| 5157 |
*/ |
| 5158 |
result = TM_WouldBlock; |
| 5159 |
/* recovery code expects to have buffer lock held */ |
| 5160 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5161 |
goto failed; |
| 5162 |
} |
| 5163 |
|
| 5164 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 5165 |
{ |
| 5166 |
MultiXactStatus status = get_mxact_status_for_lock(mode, false); |
| 5167 |
|
| 5168 |
/* We only ever lock tuples, never update them */ |
| 5169 |
if (status >= MultiXactStatusNoKeyUpdate) |
| 5170 |
elog(ERROR, "invalid lock mode in heap_lock_tuple"); |
| 5171 |
|
| 5172 |
/* wait for multixact to end, or die trying */ |
| 5173 |
switch (wait_policy) |
| 5174 |
{ |
| 5175 |
case LockWaitBlock: |
| 5176 |
MultiXactIdWait((MultiXactId) xwait, status, infomask, |
| 5177 |
relation, &tuple->t_self, XLTW_Lock, NULL); |
| 5178 |
break; |
| 5179 |
case LockWaitSkip: |
| 5180 |
if (!ConditionalMultiXactIdWait((MultiXactId) xwait, |
| 5181 |
status, infomask, relation, |
| 5182 |
NULL, false)) |
| 5183 |
{ |
| 5184 |
result = TM_WouldBlock; |
| 5185 |
/* recovery code expects to have buffer lock held */ |
| 5186 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5187 |
goto failed; |
| 5188 |
} |
| 5189 |
break; |
| 5190 |
case LockWaitError: |
| 5191 |
if (!ConditionalMultiXactIdWait((MultiXactId) xwait, |
| 5192 |
status, infomask, relation, |
| 5193 |
NULL, log_lock_failures)) |
| 5194 |
ereport(ERROR, |
| 5195 |
(errcode(ERRCODE_LOCK_NOT_AVAILABLE), |
| 5196 |
errmsg("could not obtain lock on row in relation \"%s\"", |
| 5197 |
RelationGetRelationName(relation)))); |
| 5198 |
|
| 5199 |
break; |
| 5200 |
} |
| 5201 |
|
| 5202 |
/* |
| 5203 |
* Of course, the multixact might not be done here: if we're |
| 5204 |
* requesting a light lock mode, other transactions with light |
| 5205 |
* locks could still be alive, as well as locks owned by our |
| 5206 |
* own xact or other subxacts of this backend. We need to |
| 5207 |
* preserve the surviving MultiXact members. Note that it |
| 5208 |
* isn't absolutely necessary in the latter case, but doing so |
| 5209 |
* is simpler. |
| 5210 |
*/ |
| 5211 |
} |
| 5212 |
else |
| 5213 |
{ |
| 5214 |
/* wait for regular transaction to end, or die trying */ |
| 5215 |
switch (wait_policy) |
| 5216 |
{ |
| 5217 |
case LockWaitBlock: |
| 5218 |
XactLockTableWait(xwait, relation, &tuple->t_self, |
| 5219 |
XLTW_Lock); |
| 5220 |
break; |
| 5221 |
case LockWaitSkip: |
| 5222 |
if (!ConditionalXactLockTableWait(xwait, false)) |
| 5223 |
{ |
| 5224 |
result = TM_WouldBlock; |
| 5225 |
/* recovery code expects to have buffer lock held */ |
| 5226 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5227 |
goto failed; |
| 5228 |
} |
| 5229 |
break; |
| 5230 |
case LockWaitError: |
| 5231 |
if (!ConditionalXactLockTableWait(xwait, log_lock_failures)) |
| 5232 |
ereport(ERROR, |
| 5233 |
(errcode(ERRCODE_LOCK_NOT_AVAILABLE), |
| 5234 |
errmsg("could not obtain lock on row in relation \"%s\"", |
| 5235 |
RelationGetRelationName(relation)))); |
| 5236 |
break; |
| 5237 |
} |
| 5238 |
} |
| 5239 |
|
| 5240 |
/* if there are updates, follow the update chain */ |
| 5241 |
if (follow_updates && !HEAP_XMAX_IS_LOCKED_ONLY(infomask) && |
| 5242 |
!ItemPointerEquals(&tuple->t_self, &t_ctid)) |
| 5243 |
{ |
| 5244 |
TM_Result res; |
| 5245 |
|
| 5246 |
res = heap_lock_updated_tuple(relation, |
| 5247 |
infomask, xwait, &t_ctid, |
| 5248 |
GetCurrentTransactionId(), |
| 5249 |
mode); |
| 5250 |
if (res != TM_Ok) |
| 5251 |
{ |
| 5252 |
result = res; |
| 5253 |
/* recovery code expects to have buffer lock held */ |
| 5254 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5255 |
goto failed; |
| 5256 |
} |
| 5257 |
} |
| 5258 |
|
| 5259 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5260 |
|
| 5261 |
/* |
| 5262 |
* xwait is done, but if xwait had just locked the tuple then some |
| 5263 |
* other xact could update this tuple before we get to this point. |
| 5264 |
* Check for xmax change, and start over if so. |
| 5265 |
*/ |
| 5266 |
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || |
| 5267 |
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), |
| 5268 |
xwait)) |
| 5269 |
goto l3; |
| 5270 |
|
| 5271 |
if (!(infomask & HEAP_XMAX_IS_MULTI)) |
| 5272 |
{ |
| 5273 |
/* |
| 5274 |
* Otherwise check if it committed or aborted. Note we cannot |
| 5275 |
* be here if the tuple was only locked by somebody who didn't |
| 5276 |
* conflict with us; that would have been handled above. So |
| 5277 |
* that transaction must necessarily be gone by now. But |
| 5278 |
* don't check for this in the multixact case, because some |
| 5279 |
* locker transactions might still be running. |
| 5280 |
*/ |
| 5281 |
UpdateXmaxHintBits(tuple->t_data, *buffer, xwait); |
| 5282 |
} |
| 5283 |
} |
| 5284 |
|
| 5285 |
/* By here, we're certain that we hold buffer exclusive lock again */ |
| 5286 |
|
| 5287 |
/* |
| 5288 |
* We may lock if previous xmax aborted, or if it committed but only |
| 5289 |
* locked the tuple without updating it; or if we didn't have to wait |
| 5290 |
* at all for whatever reason. |
| 5291 |
*/ |
| 5292 |
if (!require_sleep || |
| 5293 |
(tuple->t_data->t_infomask & HEAP_XMAX_INVALID) || |
| 5294 |
HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) || |
| 5295 |
HeapTupleHeaderIsOnlyLocked(tuple->t_data)) |
| 5296 |
result = TM_Ok; |
| 5297 |
else if (!ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid)) |
| 5298 |
result = TM_Updated; |
| 5299 |
else |
| 5300 |
result = TM_Deleted; |
| 5301 |
} |
| 5302 |
|
| 5303 |
failed: |
| 5304 |
if (result != TM_Ok) |
| 5305 |
{ |
| 5306 |
Assert(result == TM_SelfModified || result == TM_Updated || |
| 5307 |
result == TM_Deleted || result == TM_WouldBlock); |
| 5308 |
|
| 5309 |
/* |
| 5310 |
* When locking a tuple under LockWaitSkip semantics and we fail with |
| 5311 |
* TM_WouldBlock above, it's possible for concurrent transactions to |
| 5312 |
* release the lock and set HEAP_XMAX_INVALID in the meantime. So |
| 5313 |
* this assert is slightly different from the equivalent one in |
| 5314 |
* heap_delete and heap_update. |
| 5315 |
*/ |
| 5316 |
Assert((result == TM_WouldBlock) || |
| 5317 |
!(tuple->t_data->t_infomask & HEAP_XMAX_INVALID)); |
| 5318 |
Assert(result != TM_Updated || |
| 5319 |
!ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid)); |
| 5320 |
tmfd->ctid = tuple->t_data->t_ctid; |
| 5321 |
tmfd->xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data); |
| 5322 |
if (result == TM_SelfModified) |
| 5323 |
tmfd->cmax = HeapTupleHeaderGetCmax(tuple->t_data); |
| 5324 |
else |
| 5325 |
tmfd->cmax = InvalidCommandId; |
| 5326 |
goto out_locked; |
| 5327 |
} |
| 5328 |
|
| 5329 |
/* |
| 5330 |
* If we didn't pin the visibility map page and the page has become all |
| 5331 |
* visible while we were busy locking the buffer, or during some |
| 5332 |
* subsequent window during which we had it unlocked, we'll have to unlock |
| 5333 |
* and re-lock, to avoid holding the buffer lock across I/O. That's a bit |
| 5334 |
* unfortunate, especially since we'll now have to recheck whether the |
| 5335 |
* tuple has been locked or updated under us, but hopefully it won't |
| 5336 |
* happen very often. |
| 5337 |
*/ |
| 5338 |
if (vmbuffer == InvalidBuffer && PageIsAllVisible(page)) |
| 5339 |
{ |
| 5340 |
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); |
| 5341 |
visibilitymap_pin(relation, block, &vmbuffer); |
| 5342 |
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); |
| 5343 |
goto l3; |
| 5344 |
} |
| 5345 |
|
| 5346 |
xmax = HeapTupleHeaderGetRawXmax(tuple->t_data); |
| 5347 |
old_infomask = tuple->t_data->t_infomask; |
| 5348 |
|
| 5349 |
/* |
| 5350 |
* If this is the first possibly-multixact-able operation in the current |
| 5351 |
* transaction, set my per-backend OldestMemberMXactId setting. We can be |
| 5352 |
* certain that the transaction will never become a member of any older |
| 5353 |
* MultiXactIds than that. (We have to do this even if we end up just |
| 5354 |
* using our own TransactionId below, since some other backend could |
| 5355 |
* incorporate our XID into a MultiXact immediately afterwards.) |
| 5356 |
*/ |
| 5357 |
MultiXactIdSetOldestMember(); |
| 5358 |
|
| 5359 |
/* |
| 5360 |
* Compute the new xmax and infomask to store into the tuple. Note we do |
| 5361 |
* not modify the tuple just yet, because that would leave it in the wrong |
| 5362 |
* state if multixact.c elogs. |
| 5363 |
*/ |
| 5364 |
compute_new_xmax_infomask(xmax, old_infomask, tuple->t_data->t_infomask2, |
| 5365 |
GetCurrentTransactionId(), mode, false, |
| 5366 |
&xid, &new_infomask, &new_infomask2); |
| 5367 |
|
| 5368 |
/* Lock VM buffer before entering critical section */ |
| 5369 |
if (PageIsAllVisible(page)) |
| 5370 |
{ |
| 5371 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 5372 |
unlock_vmbuffer = true; |
| 5373 |
} |
| 5374 |
|
| 5375 |
START_CRIT_SECTION(); |
| 5376 |
|
| 5377 |
/* |
| 5378 |
* Store transaction information of xact locking the tuple. |
| 5379 |
* |
| 5380 |
* Note: Cmax is meaningless in this context, so don't set it; this avoids |
| 5381 |
* possibly generating a useless combo CID. Moreover, if we're locking a |
| 5382 |
* previously updated tuple, it's important to preserve the Cmax. |
| 5383 |
* |
| 5384 |
* Also reset the HOT UPDATE bit, but only if there's no update; otherwise |
| 5385 |
* we would break the HOT chain. |
| 5386 |
*/ |
| 5387 |
tuple->t_data->t_infomask &= ~HEAP_XMAX_BITS; |
| 5388 |
tuple->t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 5389 |
tuple->t_data->t_infomask |= new_infomask; |
| 5390 |
tuple->t_data->t_infomask2 |= new_infomask2; |
| 5391 |
if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask)) |
| 5392 |
HeapTupleHeaderClearHotUpdated(tuple->t_data); |
| 5393 |
HeapTupleHeaderSetXmax(tuple->t_data, xid); |
| 5394 |
|
| 5395 |
/* |
| 5396 |
* Make sure there is no forward chain link in t_ctid. Note that in the |
| 5397 |
* cases where the tuple has been updated, we must not overwrite t_ctid, |
| 5398 |
* because it was set by the updater. Moreover, if the tuple has been |
| 5399 |
* updated, we need to follow the update chain to lock the new versions of |
| 5400 |
* the tuple as well. |
| 5401 |
*/ |
| 5402 |
if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask)) |
| 5403 |
tuple->t_data->t_ctid = *tid; |
| 5404 |
|
| 5405 |
/* Clear only the all-frozen bit on visibility map if needed */ |
| 5406 |
if (PageIsAllVisible(page)) |
| 5407 |
{ |
| 5408 |
/* It's possible all-frozen was already clear */ |
| 5409 |
if (visibilitymap_clear_locked(relation, block, vmbuffer, |
| 5410 |
VISIBILITYMAP_ALL_FROZEN)) |
| 5411 |
cleared_all_frozen = true; |
| 5412 |
} |
| 5413 |
|
| 5414 |
MarkBufferDirty(*buffer); |
| 5415 |
|
| 5416 |
/* |
| 5417 |
* XLOG stuff. You might think that we don't need an XLOG record because |
| 5418 |
* there is no state change worth restoring after a crash. You would be |
| 5419 |
* wrong however: we have just written either a TransactionId or a |
| 5420 |
* MultiXactId that may never have been seen on disk before, and we need |
| 5421 |
* to make sure that there are XLOG entries covering those ID numbers. |
| 5422 |
* Else the same IDs might be re-used after a crash, which would be |
| 5423 |
* disastrous if this page made it to disk before the crash. Essentially |
| 5424 |
* we have to enforce the WAL log-before-data rule even in this case. |
| 5425 |
* (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG |
| 5426 |
* entries for everything anyway.) |
| 5427 |
*/ |
| 5428 |
if (RelationNeedsWAL(relation)) |
| 5429 |
{ |
| 5430 |
xl_heap_lock xlrec; |
| 5431 |
XLogRecPtr recptr; |
| 5432 |
|
| 5433 |
XLogBeginInsert(); |
| 5434 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_HEAP, *buffer, REGBUF_STANDARD); |
| 5435 |
|
| 5436 |
xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self); |
| 5437 |
xlrec.xmax = xid; |
| 5438 |
xlrec.infobits_set = compute_infobits(new_infomask, |
| 5439 |
tuple->t_data->t_infomask2); |
| 5440 |
xlrec.flags = cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0; |
| 5441 |
XLogRegisterData(&xlrec, SizeOfHeapLock); |
| 5442 |
|
| 5443 |
if (cleared_all_frozen) |
| 5444 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_VM, vmbuffer, 0); |
| 5445 |
|
| 5446 |
/* we don't decode row locks atm, so no need to log the origin */ |
| 5447 |
|
| 5448 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK); |
| 5449 |
|
| 5450 |
PageSetLSN(page, recptr); |
| 5451 |
|
| 5452 |
if (cleared_all_frozen) |
| 5453 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 5454 |
} |
| 5455 |
|
| 5456 |
END_CRIT_SECTION(); |
| 5457 |
|
| 5458 |
/* release VM lock first, since it covers many heap blocks */ |
| 5459 |
if (unlock_vmbuffer) |
| 5460 |
{ |
| 5461 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 5462 |
unlock_vmbuffer = false; |
| 5463 |
} |
| 5464 |
|
| 5465 |
result = TM_Ok; |
| 5466 |
|
| 5467 |
out_locked: |
| 5468 |
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); |
| 5469 |
Assert(!unlock_vmbuffer); |
| 5470 |
|
| 5471 |
out_unlocked: |
| 5472 |
if (BufferIsValid(vmbuffer)) |
| 5473 |
ReleaseBuffer(vmbuffer); |
| 5474 |
|
| 5475 |
/* |
| 5476 |
* Don't update the visibility map here. Locking a tuple doesn't change |
| 5477 |
* visibility info. |
| 5478 |
*/ |
| 5479 |
|
| 5480 |
/* |
| 5481 |
* Now that we have successfully marked the tuple as locked, we can |
| 5482 |
* release the lmgr tuple lock, if we had it. |
| 5483 |
*/ |
| 5484 |
if (have_tuple_lock) |
| 5485 |
UnlockTupleTuplock(relation, tid, mode); |
| 5486 |
|
| 5487 |
return result; |
| 5488 |
} |
| 5489 |
|
| 5490 |
/* |
| 5491 |
* Acquire heavyweight lock on the given tuple, in preparation for acquiring |
| 5492 |
* its normal, Xmax-based tuple lock. |
| 5493 |
* |
| 5494 |
* have_tuple_lock is an input and output parameter: on input, it indicates |
| 5495 |
* whether the lock has previously been acquired (and this function does |
| 5496 |
* nothing in that case). If this function returns success, have_tuple_lock |
| 5497 |
* has been flipped to true. |
| 5498 |
* |
| 5499 |
* Returns false if it was unable to obtain the lock; this can only happen if |
| 5500 |
* wait_policy is Skip. |
| 5501 |
*/ |
| 5502 |
static bool |
| 5503 |
heap_acquire_tuplock(Relation relation, ItemPointer tid, LockTupleMode mode, |
| 5504 |
LockWaitPolicy wait_policy, bool *have_tuple_lock) |
| 5505 |
{ |
| 5506 |
if (*have_tuple_lock) |
| 5507 |
return true; |
| 5508 |
|
| 5509 |
switch (wait_policy) |
| 5510 |
{ |
| 5511 |
case LockWaitBlock: |
| 5512 |
LockTupleTuplock(relation, tid, mode); |
| 5513 |
break; |
| 5514 |
|
| 5515 |
case LockWaitSkip: |
| 5516 |
if (!ConditionalLockTupleTuplock(relation, tid, mode, false)) |
| 5517 |
return false; |
| 5518 |
break; |
| 5519 |
|
| 5520 |
case LockWaitError: |
| 5521 |
if (!ConditionalLockTupleTuplock(relation, tid, mode, log_lock_failures)) |
| 5522 |
ereport(ERROR, |
| 5523 |
(errcode(ERRCODE_LOCK_NOT_AVAILABLE), |
| 5524 |
errmsg("could not obtain lock on row in relation \"%s\"", |
| 5525 |
RelationGetRelationName(relation)))); |
| 5526 |
break; |
| 5527 |
} |
| 5528 |
*have_tuple_lock = true; |
| 5529 |
|
| 5530 |
return true; |
| 5531 |
} |
| 5532 |
|
| 5533 |
/* |
| 5534 |
* Given an original set of Xmax and infomask, and a transaction (identified by |
| 5535 |
* add_to_xmax) acquiring a new lock of some mode, compute the new Xmax and |
| 5536 |
* corresponding infomasks to use on the tuple. |
| 5537 |
* |
| 5538 |
* Note that this might have side effects such as creating a new MultiXactId. |
| 5539 |
* |
| 5540 |
* Most callers will have called HeapTupleSatisfiesUpdate before this function; |
| 5541 |
* that will have set the HEAP_XMAX_INVALID bit if the xmax was a MultiXactId |
| 5542 |
* but it was not running anymore. There is a race condition, which is that the |
| 5543 |
* MultiXactId may have finished since then, but that uncommon case is handled |
| 5544 |
* either here, or within MultiXactIdExpand. |
| 5545 |
* |
| 5546 |
* There is a similar race condition possible when the old xmax was a regular |
| 5547 |
* TransactionId. We test TransactionIdIsInProgress again just to narrow the |
| 5548 |
* window, but it's still possible to end up creating an unnecessary |
| 5549 |
* MultiXactId. Fortunately this is harmless. |
| 5550 |
*/ |
| 5551 |
static void |
| 5552 |
compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask, |
| 5553 |
uint16 old_infomask2, TransactionId add_to_xmax, |
| 5554 |
LockTupleMode mode, bool is_update, |
| 5555 |
TransactionId *result_xmax, uint16 *result_infomask, |
| 5556 |
uint16 *result_infomask2) |
| 5557 |
{ |
| 5558 |
TransactionId new_xmax; |
| 5559 |
uint16 new_infomask, |
| 5560 |
new_infomask2; |
| 5561 |
|
| 5562 |
Assert(TransactionIdIsCurrentTransactionId(add_to_xmax)); |
| 5563 |
|
| 5564 |
l5: |
| 5565 |
new_infomask = 0; |
| 5566 |
new_infomask2 = 0; |
| 5567 |
if (old_infomask & HEAP_XMAX_INVALID) |
| 5568 |
{ |
| 5569 |
/* |
| 5570 |
* No previous locker; we just insert our own TransactionId. |
| 5571 |
* |
| 5572 |
* Note that it's critical that this case be the first one checked, |
| 5573 |
* because there are several blocks below that come back to this one |
| 5574 |
* to implement certain optimizations; old_infomask might contain |
| 5575 |
* other dirty bits in those cases, but we don't really care. |
| 5576 |
*/ |
| 5577 |
if (is_update) |
| 5578 |
{ |
| 5579 |
new_xmax = add_to_xmax; |
| 5580 |
if (mode == LockTupleExclusive) |
| 5581 |
new_infomask2 |= HEAP_KEYS_UPDATED; |
| 5582 |
} |
| 5583 |
else |
| 5584 |
{ |
| 5585 |
new_infomask |= HEAP_XMAX_LOCK_ONLY; |
| 5586 |
switch (mode) |
| 5587 |
{ |
| 5588 |
case LockTupleKeyShare: |
| 5589 |
new_xmax = add_to_xmax; |
| 5590 |
new_infomask |= HEAP_XMAX_KEYSHR_LOCK; |
| 5591 |
break; |
| 5592 |
case LockTupleShare: |
| 5593 |
new_xmax = add_to_xmax; |
| 5594 |
new_infomask |= HEAP_XMAX_SHR_LOCK; |
| 5595 |
break; |
| 5596 |
case LockTupleNoKeyExclusive: |
| 5597 |
new_xmax = add_to_xmax; |
| 5598 |
new_infomask |= HEAP_XMAX_EXCL_LOCK; |
| 5599 |
break; |
| 5600 |
case LockTupleExclusive: |
| 5601 |
new_xmax = add_to_xmax; |
| 5602 |
new_infomask |= HEAP_XMAX_EXCL_LOCK; |
| 5603 |
new_infomask2 |= HEAP_KEYS_UPDATED; |
| 5604 |
break; |
| 5605 |
default: |
| 5606 |
new_xmax = InvalidTransactionId; /* silence compiler */ |
| 5607 |
elog(ERROR, "invalid lock mode"); |
| 5608 |
} |
| 5609 |
} |
| 5610 |
} |
| 5611 |
else if (old_infomask & HEAP_XMAX_IS_MULTI) |
| 5612 |
{ |
| 5613 |
MultiXactStatus new_status; |
| 5614 |
|
| 5615 |
/* |
| 5616 |
* Currently we don't allow XMAX_COMMITTED to be set for multis, so |
| 5617 |
* cross-check. |
| 5618 |
*/ |
| 5619 |
Assert(!(old_infomask & HEAP_XMAX_COMMITTED)); |
| 5620 |
|
| 5621 |
/* |
| 5622 |
* A multixact together with LOCK_ONLY set but neither lock bit set |
| 5623 |
* (i.e. a pg_upgraded share locked tuple) cannot possibly be running |
| 5624 |
* anymore. This check is critical for databases upgraded by |
| 5625 |
* pg_upgrade; both MultiXactIdIsRunning and MultiXactIdExpand assume |
| 5626 |
* that such multis are never passed. |
| 5627 |
*/ |
| 5628 |
if (HEAP_LOCKED_UPGRADED(old_infomask)) |
| 5629 |
{ |
| 5630 |
old_infomask &= ~HEAP_XMAX_IS_MULTI; |
| 5631 |
old_infomask |= HEAP_XMAX_INVALID; |
| 5632 |
goto l5; |
| 5633 |
} |
| 5634 |
|
| 5635 |
/* |
| 5636 |
* If the XMAX is already a MultiXactId, then we need to expand it to |
| 5637 |
* include add_to_xmax; but if all the members were lockers and are |
| 5638 |
* all gone, we can do away with the IS_MULTI bit and just set |
| 5639 |
* add_to_xmax as the only locker/updater. If all lockers are gone |
| 5640 |
* and we have an updater that aborted, we can also do without a |
| 5641 |
* multi. |
| 5642 |
* |
| 5643 |
* The cost of doing GetMultiXactIdMembers would be paid by |
| 5644 |
* MultiXactIdExpand if we weren't to do this, so this check is not |
| 5645 |
* incurring extra work anyhow. |
| 5646 |
*/ |
| 5647 |
if (!MultiXactIdIsRunning(xmax, HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))) |
| 5648 |
{ |
| 5649 |
if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || |
| 5650 |
!TransactionIdDidCommit(MultiXactIdGetUpdateXid(xmax, |
| 5651 |
old_infomask))) |
| 5652 |
{ |
| 5653 |
/* |
| 5654 |
* Reset these bits and restart; otherwise fall through to |
| 5655 |
* create a new multi below. |
| 5656 |
*/ |
| 5657 |
old_infomask &= ~HEAP_XMAX_IS_MULTI; |
| 5658 |
old_infomask |= HEAP_XMAX_INVALID; |
| 5659 |
goto l5; |
| 5660 |
} |
| 5661 |
} |
| 5662 |
|
| 5663 |
new_status = get_mxact_status_for_lock(mode, is_update); |
| 5664 |
|
| 5665 |
new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax, |
| 5666 |
new_status); |
| 5667 |
GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); |
| 5668 |
} |
| 5669 |
else if (old_infomask & HEAP_XMAX_COMMITTED) |
| 5670 |
{ |
| 5671 |
/* |
| 5672 |
* It's a committed update, so we need to preserve him as updater of |
| 5673 |
* the tuple. |
| 5674 |
*/ |
| 5675 |
MultiXactStatus status; |
| 5676 |
MultiXactStatus new_status; |
| 5677 |
|
| 5678 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 5679 |
status = MultiXactStatusUpdate; |
| 5680 |
else |
| 5681 |
status = MultiXactStatusNoKeyUpdate; |
| 5682 |
|
| 5683 |
new_status = get_mxact_status_for_lock(mode, is_update); |
| 5684 |
|
| 5685 |
/* |
| 5686 |
* since it's not running, it's obviously impossible for the old |
| 5687 |
* updater to be identical to the current one, so we need not check |
| 5688 |
* for that case as we do in the block above. |
| 5689 |
*/ |
| 5690 |
new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); |
| 5691 |
GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); |
| 5692 |
} |
| 5693 |
else if (TransactionIdIsInProgress(xmax)) |
| 5694 |
{ |
| 5695 |
/* |
| 5696 |
* If the XMAX is a valid, in-progress TransactionId, then we need to |
| 5697 |
* create a new MultiXactId that includes both the old locker or |
| 5698 |
* updater and our own TransactionId. |
| 5699 |
*/ |
| 5700 |
MultiXactStatus new_status; |
| 5701 |
MultiXactStatus old_status; |
| 5702 |
LockTupleMode old_mode; |
| 5703 |
|
| 5704 |
if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)) |
| 5705 |
{ |
| 5706 |
if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask)) |
| 5707 |
old_status = MultiXactStatusForKeyShare; |
| 5708 |
else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask)) |
| 5709 |
old_status = MultiXactStatusForShare; |
| 5710 |
else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) |
| 5711 |
{ |
| 5712 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 5713 |
old_status = MultiXactStatusForUpdate; |
| 5714 |
else |
| 5715 |
old_status = MultiXactStatusForNoKeyUpdate; |
| 5716 |
} |
| 5717 |
else |
| 5718 |
{ |
| 5719 |
/* |
| 5720 |
* LOCK_ONLY can be present alone only when a page has been |
| 5721 |
* upgraded by pg_upgrade. But in that case, |
| 5722 |
* TransactionIdIsInProgress() should have returned false. We |
| 5723 |
* assume it's no longer locked in this case. |
| 5724 |
*/ |
| 5725 |
elog(WARNING, "LOCK_ONLY found for Xid in progress %u", xmax); |
| 5726 |
old_infomask |= HEAP_XMAX_INVALID; |
| 5727 |
old_infomask &= ~HEAP_XMAX_LOCK_ONLY; |
| 5728 |
goto l5; |
| 5729 |
} |
| 5730 |
} |
| 5731 |
else |
| 5732 |
{ |
| 5733 |
/* it's an update, but which kind? */ |
| 5734 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 5735 |
old_status = MultiXactStatusUpdate; |
| 5736 |
else |
| 5737 |
old_status = MultiXactStatusNoKeyUpdate; |
| 5738 |
} |
| 5739 |
|
| 5740 |
old_mode = TUPLOCK_from_mxstatus(old_status); |
| 5741 |
|
| 5742 |
/* |
| 5743 |
* If the lock to be acquired is for the same TransactionId as the |
| 5744 |
* existing lock, there's an optimization possible: consider only the |
| 5745 |
* strongest of both locks as the only one present, and restart. |
| 5746 |
*/ |
| 5747 |
if (xmax == add_to_xmax) |
| 5748 |
{ |
| 5749 |
/* |
| 5750 |
* Note that it's not possible for the original tuple to be |
| 5751 |
* updated: we wouldn't be here because the tuple would have been |
| 5752 |
* invisible and we wouldn't try to update it. As a subtlety, |
| 5753 |
* this code can also run when traversing an update chain to lock |
| 5754 |
* future versions of a tuple. But we wouldn't be here either, |
| 5755 |
* because the add_to_xmax would be different from the original |
| 5756 |
* updater. |
| 5757 |
*/ |
| 5758 |
Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); |
| 5759 |
|
| 5760 |
/* acquire the strongest of both */ |
| 5761 |
if (mode < old_mode) |
| 5762 |
mode = old_mode; |
| 5763 |
/* mustn't touch is_update */ |
| 5764 |
|
| 5765 |
old_infomask |= HEAP_XMAX_INVALID; |
| 5766 |
goto l5; |
| 5767 |
} |
| 5768 |
|
| 5769 |
/* otherwise, just fall back to creating a new multixact */ |
| 5770 |
new_status = get_mxact_status_for_lock(mode, is_update); |
| 5771 |
new_xmax = MultiXactIdCreate(xmax, old_status, |
| 5772 |
add_to_xmax, new_status); |
| 5773 |
GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); |
| 5774 |
} |
| 5775 |
else if (!HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) && |
| 5776 |
TransactionIdDidCommit(xmax)) |
| 5777 |
{ |
| 5778 |
/* |
| 5779 |
* It's a committed update, so we gotta preserve him as updater of the |
| 5780 |
* tuple. |
| 5781 |
*/ |
| 5782 |
MultiXactStatus status; |
| 5783 |
MultiXactStatus new_status; |
| 5784 |
|
| 5785 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 5786 |
status = MultiXactStatusUpdate; |
| 5787 |
else |
| 5788 |
status = MultiXactStatusNoKeyUpdate; |
| 5789 |
|
| 5790 |
new_status = get_mxact_status_for_lock(mode, is_update); |
| 5791 |
|
| 5792 |
/* |
| 5793 |
* since it's not running, it's obviously impossible for the old |
| 5794 |
* updater to be identical to the current one, so we need not check |
| 5795 |
* for that case as we do in the block above. |
| 5796 |
*/ |
| 5797 |
new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); |
| 5798 |
GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); |
| 5799 |
} |
| 5800 |
else |
| 5801 |
{ |
| 5802 |
/* |
| 5803 |
* Can get here iff the locking/updating transaction was running when |
| 5804 |
* the infomask was extracted from the tuple, but finished before |
| 5805 |
* TransactionIdIsInProgress got to run. Deal with it as if there was |
| 5806 |
* no locker at all in the first place. |
| 5807 |
*/ |
| 5808 |
old_infomask |= HEAP_XMAX_INVALID; |
| 5809 |
goto l5; |
| 5810 |
} |
| 5811 |
|
| 5812 |
*result_infomask = new_infomask; |
| 5813 |
*result_infomask2 = new_infomask2; |
| 5814 |
*result_xmax = new_xmax; |
| 5815 |
} |
| 5816 |
|
| 5817 |
/* |
| 5818 |
* Subroutine for heap_lock_updated_tuple_rec. |
| 5819 |
* |
| 5820 |
* Given a hypothetical multixact status held by the transaction identified |
| 5821 |
* with the given xid, does the current transaction need to wait, fail, or can |
| 5822 |
* it continue if it wanted to acquire a lock of the given mode? "needwait" |
| 5823 |
* is set to true if waiting is necessary; if it can continue, then TM_Ok is |
| 5824 |
* returned. If the lock is already held by the current transaction, return |
| 5825 |
* TM_SelfModified. In case of a conflict with another transaction, a |
| 5826 |
* different HeapTupleSatisfiesUpdate return code is returned. |
| 5827 |
* |
| 5828 |
* The held status is said to be hypothetical because it might correspond to a |
| 5829 |
* lock held by a single Xid, i.e. not a real MultiXactId; we express it this |
| 5830 |
* way for simplicity of API. |
| 5831 |
*/ |
| 5832 |
static TM_Result |
| 5833 |
test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid, |
| 5834 |
LockTupleMode mode, HeapTuple tup, |
| 5835 |
bool *needwait) |
| 5836 |
{ |
| 5837 |
MultiXactStatus wantedstatus; |
| 5838 |
|
| 5839 |
*needwait = false; |
| 5840 |
wantedstatus = get_mxact_status_for_lock(mode, false); |
| 5841 |
|
| 5842 |
/* |
| 5843 |
* Note: we *must* check TransactionIdIsInProgress before |
| 5844 |
* TransactionIdDidAbort/Commit; see comment at top of heapam_visibility.c |
| 5845 |
* for an explanation. |
| 5846 |
*/ |
| 5847 |
if (TransactionIdIsCurrentTransactionId(xid)) |
| 5848 |
{ |
| 5849 |
/* |
| 5850 |
* The tuple has already been locked by our own transaction. This is |
| 5851 |
* very rare but can happen if multiple transactions are trying to |
| 5852 |
* lock an ancient version of the same tuple. |
| 5853 |
*/ |
| 5854 |
return TM_SelfModified; |
| 5855 |
} |
| 5856 |
else if (TransactionIdIsInProgress(xid)) |
| 5857 |
{ |
| 5858 |
/* |
| 5859 |
* If the locking transaction is running, what we do depends on |
| 5860 |
* whether the lock modes conflict: if they do, then we must wait for |
| 5861 |
* it to finish; otherwise we can fall through to lock this tuple |
| 5862 |
* version without waiting. |
| 5863 |
*/ |
| 5864 |
if (DoLockModesConflict(LOCKMODE_from_mxstatus(status), |
| 5865 |
LOCKMODE_from_mxstatus(wantedstatus))) |
| 5866 |
{ |
| 5867 |
*needwait = true; |
| 5868 |
} |
| 5869 |
|
| 5870 |
/* |
| 5871 |
* If we set needwait above, then this value doesn't matter; |
| 5872 |
* otherwise, this value signals to caller that it's okay to proceed. |
| 5873 |
*/ |
| 5874 |
return TM_Ok; |
| 5875 |
} |
| 5876 |
else if (TransactionIdDidAbort(xid)) |
| 5877 |
return TM_Ok; |
| 5878 |
else if (TransactionIdDidCommit(xid)) |
| 5879 |
{ |
| 5880 |
/* |
| 5881 |
* The other transaction committed. If it was only a locker, then the |
| 5882 |
* lock is completely gone now and we can return success; but if it |
| 5883 |
* was an update, then what we do depends on whether the two lock |
| 5884 |
* modes conflict. If they conflict, then we must report error to |
| 5885 |
* caller. But if they don't, we can fall through to allow the current |
| 5886 |
* transaction to lock the tuple. |
| 5887 |
* |
| 5888 |
* Note: the reason we worry about ISUPDATE here is because as soon as |
| 5889 |
* a transaction ends, all its locks are gone and meaningless, and |
| 5890 |
* thus we can ignore them; whereas its updates persist. In the |
| 5891 |
* TransactionIdIsInProgress case, above, we don't need to check |
| 5892 |
* because we know the lock is still "alive" and thus a conflict needs |
| 5893 |
* always be checked. |
| 5894 |
*/ |
| 5895 |
if (!ISUPDATE_from_mxstatus(status)) |
| 5896 |
return TM_Ok; |
| 5897 |
|
| 5898 |
if (DoLockModesConflict(LOCKMODE_from_mxstatus(status), |
| 5899 |
LOCKMODE_from_mxstatus(wantedstatus))) |
| 5900 |
{ |
| 5901 |
/* bummer */ |
| 5902 |
if (!ItemPointerEquals(&tup->t_self, &tup->t_data->t_ctid)) |
| 5903 |
return TM_Updated; |
| 5904 |
else |
| 5905 |
return TM_Deleted; |
| 5906 |
} |
| 5907 |
|
| 5908 |
return TM_Ok; |
| 5909 |
} |
| 5910 |
|
| 5911 |
/* Not in progress, not aborted, not committed -- must have crashed */ |
| 5912 |
return TM_Ok; |
| 5913 |
} |
| 5914 |
|
| 5915 |
|
| 5916 |
/* |
| 5917 |
* Recursive part of heap_lock_updated_tuple |
| 5918 |
* |
| 5919 |
* Fetch the tuple pointed to by tid in rel, and mark it as locked by the given |
| 5920 |
* xid with the given mode; if this tuple is updated, recurse to lock the new |
| 5921 |
* version as well. |
| 5922 |
*/ |
| 5923 |
static TM_Result |
| 5924 |
heap_lock_updated_tuple_rec(Relation rel, TransactionId priorXmax, |
| 5925 |
const ItemPointerData *tid, TransactionId xid, |
| 5926 |
LockTupleMode mode) |
| 5927 |
{ |
| 5928 |
TM_Result result; |
| 5929 |
ItemPointerData tupid; |
| 5930 |
HeapTupleData mytup; |
| 5931 |
Buffer buf; |
| 5932 |
Page page; |
| 5933 |
uint16 new_infomask, |
| 5934 |
new_infomask2, |
| 5935 |
old_infomask, |
| 5936 |
old_infomask2; |
| 5937 |
TransactionId xmax, |
| 5938 |
new_xmax; |
| 5939 |
bool cleared_all_frozen = false; |
| 5940 |
bool pinned_desired_page; |
| 5941 |
Buffer vmbuffer = InvalidBuffer; |
| 5942 |
bool unlock_vmbuffer = false; |
| 5943 |
BlockNumber block; |
| 5944 |
|
| 5945 |
ItemPointerCopy(tid, &tupid); |
| 5946 |
|
| 5947 |
for (;;) |
| 5948 |
{ |
| 5949 |
new_infomask = 0; |
| 5950 |
new_xmax = InvalidTransactionId; |
| 5951 |
cleared_all_frozen = false; |
| 5952 |
block = ItemPointerGetBlockNumber(&tupid); |
| 5953 |
ItemPointerCopy(&tupid, &(mytup.t_self)); |
| 5954 |
|
| 5955 |
if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false)) |
| 5956 |
{ |
| 5957 |
/* |
| 5958 |
* if we fail to find the updated version of the tuple, it's |
| 5959 |
* because it was vacuumed/pruned away after its creator |
| 5960 |
* transaction aborted. So behave as if we got to the end of the |
| 5961 |
* chain, and there's no further tuple to lock: return success to |
| 5962 |
* caller. |
| 5963 |
*/ |
| 5964 |
result = TM_Ok; |
| 5965 |
goto out_unlocked; |
| 5966 |
} |
| 5967 |
|
| 5968 |
l4: |
| 5969 |
CHECK_FOR_INTERRUPTS(); |
| 5970 |
|
| 5971 |
page = BufferGetPage(buf); |
| 5972 |
|
| 5973 |
/* |
| 5974 |
* Before locking the buffer, pin the visibility map page if it |
| 5975 |
* appears to be necessary. Since we haven't got the lock yet, |
| 5976 |
* someone else might be in the middle of changing this, so we'll need |
| 5977 |
* to recheck after we have the lock. |
| 5978 |
*/ |
| 5979 |
if (PageIsAllVisible(page)) |
| 5980 |
{ |
| 5981 |
visibilitymap_pin(rel, block, &vmbuffer); |
| 5982 |
pinned_desired_page = true; |
| 5983 |
} |
| 5984 |
else |
| 5985 |
pinned_desired_page = false; |
| 5986 |
|
| 5987 |
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); |
| 5988 |
|
| 5989 |
/* |
| 5990 |
* If we didn't pin the visibility map page and the page has become |
| 5991 |
* all visible while we were busy locking the buffer, we'll have to |
| 5992 |
* unlock and re-lock, to avoid holding the buffer lock across I/O. |
| 5993 |
* That's a bit unfortunate, but hopefully shouldn't happen often. |
| 5994 |
* |
| 5995 |
* Note: in some paths through this function, we will reach here |
| 5996 |
* holding a pin on a vm page that may or may not be the one matching |
| 5997 |
* this page. If this page isn't all-visible, we won't use the vm |
| 5998 |
* page, but we hold onto such a pin till the end of the function. |
| 5999 |
*/ |
| 6000 |
if (!pinned_desired_page && PageIsAllVisible(page)) |
| 6001 |
{ |
| 6002 |
LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
| 6003 |
visibilitymap_pin(rel, block, &vmbuffer); |
| 6004 |
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); |
| 6005 |
} |
| 6006 |
|
| 6007 |
/* |
| 6008 |
* Check the tuple XMIN against prior XMAX, if any. If we reached the |
| 6009 |
* end of the chain, we're done, so return success. |
| 6010 |
*/ |
| 6011 |
if (TransactionIdIsValid(priorXmax) && |
| 6012 |
!TransactionIdEquals(HeapTupleHeaderGetXmin(mytup.t_data), |
| 6013 |
priorXmax)) |
| 6014 |
{ |
| 6015 |
result = TM_Ok; |
| 6016 |
goto out_locked; |
| 6017 |
} |
| 6018 |
|
| 6019 |
/* |
| 6020 |
* Also check Xmin: if this tuple was created by an aborted |
| 6021 |
* (sub)transaction, then we already locked the last live one in the |
| 6022 |
* chain, thus we're done, so return success. |
| 6023 |
*/ |
| 6024 |
if (TransactionIdDidAbort(HeapTupleHeaderGetXmin(mytup.t_data))) |
| 6025 |
{ |
| 6026 |
result = TM_Ok; |
| 6027 |
goto out_locked; |
| 6028 |
} |
| 6029 |
|
| 6030 |
old_infomask = mytup.t_data->t_infomask; |
| 6031 |
old_infomask2 = mytup.t_data->t_infomask2; |
| 6032 |
xmax = HeapTupleHeaderGetRawXmax(mytup.t_data); |
| 6033 |
|
| 6034 |
/* |
| 6035 |
* If this tuple version has been updated or locked by some concurrent |
| 6036 |
* transaction(s), what we do depends on whether our lock mode |
| 6037 |
* conflicts with what those other transactions hold, and also on the |
| 6038 |
* status of them. |
| 6039 |
*/ |
| 6040 |
if (!(old_infomask & HEAP_XMAX_INVALID)) |
| 6041 |
{ |
| 6042 |
TransactionId rawxmax; |
| 6043 |
bool needwait; |
| 6044 |
|
| 6045 |
rawxmax = HeapTupleHeaderGetRawXmax(mytup.t_data); |
| 6046 |
if (old_infomask & HEAP_XMAX_IS_MULTI) |
| 6047 |
{ |
| 6048 |
int nmembers; |
| 6049 |
int i; |
| 6050 |
MultiXactMember *members; |
| 6051 |
|
| 6052 |
/* |
| 6053 |
* We don't need a test for pg_upgrade'd tuples: this is only |
| 6054 |
* applied to tuples after the first in an update chain. Said |
| 6055 |
* first tuple in the chain may well be locked-in-9.2-and- |
| 6056 |
* pg_upgraded, but that one was already locked by our caller, |
| 6057 |
* not us; and any subsequent ones cannot be because our |
| 6058 |
* caller must necessarily have obtained a snapshot later than |
| 6059 |
* the pg_upgrade itself. |
| 6060 |
*/ |
| 6061 |
Assert(!HEAP_LOCKED_UPGRADED(mytup.t_data->t_infomask)); |
| 6062 |
|
| 6063 |
nmembers = GetMultiXactIdMembers(rawxmax, &members, false, |
| 6064 |
HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); |
| 6065 |
for (i = 0; i < nmembers; i++) |
| 6066 |
{ |
| 6067 |
result = test_lockmode_for_conflict(members[i].status, |
| 6068 |
members[i].xid, |
| 6069 |
mode, |
| 6070 |
&mytup, |
| 6071 |
&needwait); |
| 6072 |
|
| 6073 |
/* |
| 6074 |
* If the tuple was already locked by ourselves in a |
| 6075 |
* previous iteration of this (say heap_lock_tuple was |
| 6076 |
* forced to restart the locking loop because of a change |
| 6077 |
* in xmax), then we hold the lock already on this tuple |
| 6078 |
* version and we don't need to do anything; and this is |
| 6079 |
* not an error condition either. We just need to skip |
| 6080 |
* this tuple and continue locking the next version in the |
| 6081 |
* update chain. |
| 6082 |
*/ |
| 6083 |
if (result == TM_SelfModified) |
| 6084 |
{ |
| 6085 |
pfree(members); |
| 6086 |
goto next; |
| 6087 |
} |
| 6088 |
|
| 6089 |
if (needwait) |
| 6090 |
{ |
| 6091 |
LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
| 6092 |
XactLockTableWait(members[i].xid, rel, |
| 6093 |
&mytup.t_self, |
| 6094 |
XLTW_LockUpdated); |
| 6095 |
pfree(members); |
| 6096 |
goto l4; |
| 6097 |
} |
| 6098 |
if (result != TM_Ok) |
| 6099 |
{ |
| 6100 |
pfree(members); |
| 6101 |
goto out_locked; |
| 6102 |
} |
| 6103 |
} |
| 6104 |
if (members) |
| 6105 |
pfree(members); |
| 6106 |
} |
| 6107 |
else |
| 6108 |
{ |
| 6109 |
MultiXactStatus status; |
| 6110 |
|
| 6111 |
/* |
| 6112 |
* For a non-multi Xmax, we first need to compute the |
| 6113 |
* corresponding MultiXactStatus by using the infomask bits. |
| 6114 |
*/ |
| 6115 |
if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)) |
| 6116 |
{ |
| 6117 |
if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask)) |
| 6118 |
status = MultiXactStatusForKeyShare; |
| 6119 |
else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask)) |
| 6120 |
status = MultiXactStatusForShare; |
| 6121 |
else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) |
| 6122 |
{ |
| 6123 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 6124 |
status = MultiXactStatusForUpdate; |
| 6125 |
else |
| 6126 |
status = MultiXactStatusForNoKeyUpdate; |
| 6127 |
} |
| 6128 |
else |
| 6129 |
{ |
| 6130 |
/* |
| 6131 |
* LOCK_ONLY present alone (a pg_upgraded tuple marked |
| 6132 |
* as share-locked in the old cluster) shouldn't be |
| 6133 |
* seen in the middle of an update chain. |
| 6134 |
*/ |
| 6135 |
elog(ERROR, "invalid lock status in tuple"); |
| 6136 |
} |
| 6137 |
} |
| 6138 |
else |
| 6139 |
{ |
| 6140 |
/* it's an update, but which kind? */ |
| 6141 |
if (old_infomask2 & HEAP_KEYS_UPDATED) |
| 6142 |
status = MultiXactStatusUpdate; |
| 6143 |
else |
| 6144 |
status = MultiXactStatusNoKeyUpdate; |
| 6145 |
} |
| 6146 |
|
| 6147 |
result = test_lockmode_for_conflict(status, rawxmax, mode, |
| 6148 |
&mytup, &needwait); |
| 6149 |
|
| 6150 |
/* |
| 6151 |
* If the tuple was already locked by ourselves in a previous |
| 6152 |
* iteration of this (say heap_lock_tuple was forced to |
| 6153 |
* restart the locking loop because of a change in xmax), then |
| 6154 |
* we hold the lock already on this tuple version and we don't |
| 6155 |
* need to do anything; and this is not an error condition |
| 6156 |
* either. We just need to skip this tuple and continue |
| 6157 |
* locking the next version in the update chain. |
| 6158 |
*/ |
| 6159 |
if (result == TM_SelfModified) |
| 6160 |
goto next; |
| 6161 |
|
| 6162 |
if (needwait) |
| 6163 |
{ |
| 6164 |
LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
| 6165 |
XactLockTableWait(rawxmax, rel, &mytup.t_self, |
| 6166 |
XLTW_LockUpdated); |
| 6167 |
goto l4; |
| 6168 |
} |
| 6169 |
if (result != TM_Ok) |
| 6170 |
{ |
| 6171 |
goto out_locked; |
| 6172 |
} |
| 6173 |
} |
| 6174 |
} |
| 6175 |
|
| 6176 |
/* compute the new Xmax and infomask values for the tuple ... */ |
| 6177 |
compute_new_xmax_infomask(xmax, old_infomask, mytup.t_data->t_infomask2, |
| 6178 |
xid, mode, false, |
| 6179 |
&new_xmax, &new_infomask, &new_infomask2); |
| 6180 |
|
| 6181 |
if (PageIsAllVisible(page)) |
| 6182 |
{ |
| 6183 |
LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE); |
| 6184 |
unlock_vmbuffer = true; |
| 6185 |
} |
| 6186 |
|
| 6187 |
START_CRIT_SECTION(); |
| 6188 |
|
| 6189 |
/* ... and set them */ |
| 6190 |
HeapTupleHeaderSetXmax(mytup.t_data, new_xmax); |
| 6191 |
mytup.t_data->t_infomask &= ~HEAP_XMAX_BITS; |
| 6192 |
mytup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 6193 |
mytup.t_data->t_infomask |= new_infomask; |
| 6194 |
mytup.t_data->t_infomask2 |= new_infomask2; |
| 6195 |
|
| 6196 |
MarkBufferDirty(buf); |
| 6197 |
|
| 6198 |
if (PageIsAllVisible(page)) |
| 6199 |
{ |
| 6200 |
/* It's possible all-frozen was already clear */ |
| 6201 |
if (visibilitymap_clear_locked(rel, block, vmbuffer, |
| 6202 |
VISIBILITYMAP_ALL_FROZEN)) |
| 6203 |
cleared_all_frozen = true; |
| 6204 |
} |
| 6205 |
|
| 6206 |
/* XLOG stuff */ |
| 6207 |
if (RelationNeedsWAL(rel)) |
| 6208 |
{ |
| 6209 |
xl_heap_lock_updated xlrec; |
| 6210 |
XLogRecPtr recptr; |
| 6211 |
|
| 6212 |
XLogBeginInsert(); |
| 6213 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_HEAP, buf, REGBUF_STANDARD); |
| 6214 |
|
| 6215 |
xlrec.offnum = ItemPointerGetOffsetNumber(&mytup.t_self); |
| 6216 |
xlrec.xmax = new_xmax; |
| 6217 |
xlrec.infobits_set = compute_infobits(new_infomask, new_infomask2); |
| 6218 |
xlrec.flags = |
| 6219 |
cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0; |
| 6220 |
|
| 6221 |
XLogRegisterData(&xlrec, SizeOfHeapLockUpdated); |
| 6222 |
|
| 6223 |
if (cleared_all_frozen) |
| 6224 |
XLogRegisterBuffer(HEAP_LOCK_BLKREF_VM, vmbuffer, 0); |
| 6225 |
|
| 6226 |
recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_LOCK_UPDATED); |
| 6227 |
|
| 6228 |
PageSetLSN(page, recptr); |
| 6229 |
|
| 6230 |
if (cleared_all_frozen) |
| 6231 |
PageSetLSN(BufferGetPage(vmbuffer), recptr); |
| 6232 |
} |
| 6233 |
|
| 6234 |
END_CRIT_SECTION(); |
| 6235 |
|
| 6236 |
/* release VM lock first, since it covers many heap blocks */ |
| 6237 |
if (unlock_vmbuffer) |
| 6238 |
{ |
| 6239 |
LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK); |
| 6240 |
unlock_vmbuffer = false; |
| 6241 |
} |
| 6242 |
|
| 6243 |
next: |
| 6244 |
/* if we find the end of update chain, we're done. */ |
| 6245 |
if (mytup.t_data->t_infomask & HEAP_XMAX_INVALID || |
| 6246 |
HeapTupleHeaderIndicatesMovedPartitions(mytup.t_data) || |
| 6247 |
ItemPointerEquals(&mytup.t_self, &mytup.t_data->t_ctid) || |
| 6248 |
HeapTupleHeaderIsOnlyLocked(mytup.t_data)) |
| 6249 |
{ |
| 6250 |
result = TM_Ok; |
| 6251 |
goto out_locked; |
| 6252 |
} |
| 6253 |
|
| 6254 |
/* tail recursion */ |
| 6255 |
priorXmax = HeapTupleHeaderGetUpdateXid(mytup.t_data); |
| 6256 |
ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid); |
| 6257 |
UnlockReleaseBuffer(buf); |
| 6258 |
} |
| 6259 |
|
| 6260 |
result = TM_Ok; |
| 6261 |
|
| 6262 |
out_locked: |
| 6263 |
UnlockReleaseBuffer(buf); |
| 6264 |
|
| 6265 |
out_unlocked: |
| 6266 |
if (vmbuffer != InvalidBuffer) |
| 6267 |
ReleaseBuffer(vmbuffer); |
| 6268 |
Assert(!unlock_vmbuffer); |
| 6269 |
|
| 6270 |
return result; |
| 6271 |
} |
| 6272 |
|
| 6273 |
/* |
| 6274 |
* heap_lock_updated_tuple |
| 6275 |
* Follow update chain when locking an updated tuple, acquiring locks (row |
| 6276 |
* marks) on the updated versions. |
| 6277 |
* |
| 6278 |
* 'prior_infomask', 'prior_raw_xmax' and 'prior_ctid' are the corresponding |
| 6279 |
* fields from the initial tuple. We will lock the tuples starting from the |
| 6280 |
* one that 'prior_ctid' points to. Note: This function does not lock the |
| 6281 |
* initial tuple itself. |
| 6282 |
* |
| 6283 |
* This function doesn't check visibility, it just unconditionally marks the |
| 6284 |
* tuple(s) as locked. If any tuple in the updated chain is being deleted |
| 6285 |
* concurrently (or updated with the key being modified), sleep until the |
| 6286 |
* transaction doing it is finished. |
| 6287 |
* |
| 6288 |
* Note that we don't acquire heavyweight tuple locks on the tuples we walk |
| 6289 |
* when we have to wait for other transactions to release them, as opposed to |
| 6290 |
* what heap_lock_tuple does. The reason is that having more than one |
| 6291 |
* transaction walking the chain is probably uncommon enough that risk of |
| 6292 |
* starvation is not likely: one of the preconditions for being here is that |
| 6293 |
* the snapshot in use predates the update that created this tuple (because we |
| 6294 |
* started at an earlier version of the tuple), but at the same time such a |
| 6295 |
* transaction cannot be using repeatable read or serializable isolation |
| 6296 |
* levels, because that would lead to a serializability failure. |
| 6297 |
*/ |
| 6298 |
static TM_Result |
| 6299 |
heap_lock_updated_tuple(Relation rel, |
| 6300 |
uint16 prior_infomask, |
| 6301 |
TransactionId prior_raw_xmax, |
| 6302 |
const ItemPointerData *prior_ctid, |
| 6303 |
TransactionId xid, LockTupleMode mode) |
| 6304 |
{ |
| 6305 |
INJECTION_POINT("heap_lock_updated_tuple", NULL); |
| 6306 |
|
| 6307 |
/* |
| 6308 |
* If the tuple has moved into another partition (effectively a delete) |
| 6309 |
* stop here. |
| 6310 |
*/ |
| 6311 |
if (!ItemPointerIndicatesMovedPartitions(prior_ctid)) |
| 6312 |
{ |
| 6313 |
TransactionId prior_xmax; |
| 6314 |
|
| 6315 |
/* |
| 6316 |
* If this is the first possibly-multixact-able operation in the |
| 6317 |
* current transaction, set my per-backend OldestMemberMXactId |
| 6318 |
* setting. We can be certain that the transaction will never become a |
| 6319 |
* member of any older MultiXactIds than that. (We have to do this |
| 6320 |
* even if we end up just using our own TransactionId below, since |
| 6321 |
* some other backend could incorporate our XID into a MultiXact |
| 6322 |
* immediately afterwards.) |
| 6323 |
*/ |
| 6324 |
MultiXactIdSetOldestMember(); |
| 6325 |
|
| 6326 |
prior_xmax = (prior_infomask & HEAP_XMAX_IS_MULTI) ? |
| 6327 |
MultiXactIdGetUpdateXid(prior_raw_xmax, prior_infomask) : prior_raw_xmax; |
| 6328 |
return heap_lock_updated_tuple_rec(rel, prior_xmax, prior_ctid, xid, mode); |
| 6329 |
} |
| 6330 |
|
| 6331 |
/* nothing to lock */ |
| 6332 |
return TM_Ok; |
| 6333 |
} |
| 6334 |
|
| 6335 |
/* |
| 6336 |
* heap_finish_speculative - mark speculative insertion as successful |
| 6337 |
* |
| 6338 |
* To successfully finish a speculative insertion we have to clear speculative |
| 6339 |
* token from tuple. To do so the t_ctid field, which will contain a |
| 6340 |
* speculative token value, is modified in place to point to the tuple itself, |
| 6341 |
* which is characteristic of a newly inserted ordinary tuple. |
| 6342 |
* |
| 6343 |
* NB: It is not ok to commit without either finishing or aborting a |
| 6344 |
* speculative insertion. We could treat speculative tuples of committed |
| 6345 |
* transactions implicitly as completed, but then we would have to be prepared |
| 6346 |
* to deal with speculative tokens on committed tuples. That wouldn't be |
| 6347 |
* difficult - no-one looks at the ctid field of a tuple with invalid xmax - |
| 6348 |
* but clearing the token at completion isn't very expensive either. |
| 6349 |
* An explicit confirmation WAL record also makes logical decoding simpler. |
| 6350 |
*/ |
| 6351 |
void |
| 6352 |
heap_finish_speculative(Relation relation, ItemPointer tid) |
| 6353 |
{ |
| 6354 |
Buffer buffer; |
| 6355 |
Page page; |
| 6356 |
OffsetNumber offnum; |
| 6357 |
ItemId lp = NULL; |
| 6358 |
HeapTupleHeader htup; |
| 6359 |
|
| 6360 |
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); |
| 6361 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 6362 |
page = (Page) BufferGetPage(buffer); |
| 6363 |
|
| 6364 |
offnum = ItemPointerGetOffsetNumber(tid); |
| 6365 |
if (PageGetMaxOffsetNumber(page) >= offnum) |
| 6366 |
lp = PageGetItemId(page, offnum); |
| 6367 |
|
| 6368 |
if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp)) |
| 6369 |
elog(ERROR, "invalid lp"); |
| 6370 |
|
| 6371 |
htup = (HeapTupleHeader) PageGetItem(page, lp); |
| 6372 |
|
| 6373 |
/* NO EREPORT(ERROR) from here till changes are logged */ |
| 6374 |
START_CRIT_SECTION(); |
| 6375 |
|
| 6376 |
Assert(HeapTupleHeaderIsSpeculative(htup)); |
| 6377 |
|
| 6378 |
MarkBufferDirty(buffer); |
| 6379 |
|
| 6380 |
/* |
| 6381 |
* Replace the speculative insertion token with a real t_ctid, pointing to |
| 6382 |
* itself like it does on regular tuples. |
| 6383 |
*/ |
| 6384 |
htup->t_ctid = *tid; |
| 6385 |
|
| 6386 |
/* XLOG stuff */ |
| 6387 |
if (RelationNeedsWAL(relation)) |
| 6388 |
{ |
| 6389 |
xl_heap_confirm xlrec; |
| 6390 |
XLogRecPtr recptr; |
| 6391 |
|
| 6392 |
xlrec.offnum = ItemPointerGetOffsetNumber(tid); |
| 6393 |
|
| 6394 |
XLogBeginInsert(); |
| 6395 |
|
| 6396 |
/* We want the same filtering on this as on a plain insert */ |
| 6397 |
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); |
| 6398 |
|
| 6399 |
XLogRegisterData(&xlrec, SizeOfHeapConfirm); |
| 6400 |
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD); |
| 6401 |
|
| 6402 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_CONFIRM); |
| 6403 |
|
| 6404 |
PageSetLSN(page, recptr); |
| 6405 |
} |
| 6406 |
|
| 6407 |
END_CRIT_SECTION(); |
| 6408 |
|
| 6409 |
UnlockReleaseBuffer(buffer); |
| 6410 |
} |
| 6411 |
|
| 6412 |
/* |
| 6413 |
* heap_abort_speculative - kill a speculatively inserted tuple |
| 6414 |
* |
| 6415 |
* Marks a tuple that was speculatively inserted in the same command as dead, |
| 6416 |
* by setting its xmin as invalid. That makes it immediately appear as dead |
| 6417 |
* to all transactions, including our own. In particular, it makes |
| 6418 |
* HeapTupleSatisfiesDirty() regard the tuple as dead, so that another backend |
| 6419 |
* inserting a duplicate key value won't unnecessarily wait for our whole |
| 6420 |
* transaction to finish (it'll just wait for our speculative insertion to |
| 6421 |
* finish). |
| 6422 |
* |
| 6423 |
* Killing the tuple prevents "unprincipled deadlocks", which are deadlocks |
| 6424 |
* that arise due to a mutual dependency that is not user visible. By |
| 6425 |
* definition, unprincipled deadlocks cannot be prevented by the user |
| 6426 |
* reordering lock acquisition in client code, because the implementation level |
| 6427 |
* lock acquisitions are not under the user's direct control. If speculative |
| 6428 |
* inserters did not take this precaution, then under high concurrency they |
| 6429 |
* could deadlock with each other, which would not be acceptable. |
| 6430 |
* |
| 6431 |
* This is somewhat redundant with heap_delete, but we prefer to have a |
| 6432 |
* dedicated routine with stripped down requirements. Note that this is also |
| 6433 |
* used to delete the TOAST tuples created during speculative insertion. |
| 6434 |
* |
| 6435 |
* This routine does not affect logical decoding as it only looks at |
| 6436 |
* confirmation records. |
| 6437 |
*/ |
| 6438 |
void |
| 6439 |
heap_abort_speculative(Relation relation, ItemPointer tid) |
| 6440 |
{ |
| 6441 |
TransactionId xid = GetCurrentTransactionId(); |
| 6442 |
ItemId lp; |
| 6443 |
HeapTupleData tp; |
| 6444 |
Page page; |
| 6445 |
BlockNumber block; |
| 6446 |
Buffer buffer; |
| 6447 |
|
| 6448 |
Assert(ItemPointerIsValid(tid)); |
| 6449 |
|
| 6450 |
block = ItemPointerGetBlockNumber(tid); |
| 6451 |
buffer = ReadBuffer(relation, block); |
| 6452 |
page = BufferGetPage(buffer); |
| 6453 |
|
| 6454 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 6455 |
|
| 6456 |
/* |
| 6457 |
* Page can't be all visible, we just inserted into it, and are still |
| 6458 |
* running. |
| 6459 |
*/ |
| 6460 |
Assert(!PageIsAllVisible(page)); |
| 6461 |
|
| 6462 |
lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid)); |
| 6463 |
Assert(ItemIdIsNormal(lp)); |
| 6464 |
|
| 6465 |
tp.t_tableOid = RelationGetRelid(relation); |
| 6466 |
tp.t_data = (HeapTupleHeader) PageGetItem(page, lp); |
| 6467 |
tp.t_len = ItemIdGetLength(lp); |
| 6468 |
tp.t_self = *tid; |
| 6469 |
|
| 6470 |
/* |
| 6471 |
* Sanity check that the tuple really is a speculatively inserted tuple, |
| 6472 |
* inserted by us. |
| 6473 |
*/ |
| 6474 |
if (tp.t_data->t_choice.t_heap.t_xmin != xid) |
| 6475 |
elog(ERROR, "attempted to kill a tuple inserted by another transaction"); |
| 6476 |
if (!(IsToastRelation(relation) || HeapTupleHeaderIsSpeculative(tp.t_data))) |
| 6477 |
elog(ERROR, "attempted to kill a non-speculative tuple"); |
| 6478 |
Assert(!HeapTupleHeaderIsHeapOnly(tp.t_data)); |
| 6479 |
|
| 6480 |
/* |
| 6481 |
* No need to check for serializable conflicts here. There is never a |
| 6482 |
* need for a combo CID, either. No need to extract replica identity, or |
| 6483 |
* do anything special with infomask bits. |
| 6484 |
*/ |
| 6485 |
|
| 6486 |
START_CRIT_SECTION(); |
| 6487 |
|
| 6488 |
/* |
| 6489 |
* The tuple will become DEAD immediately. Flag that this page is a |
| 6490 |
* candidate for pruning by setting xmin to TransactionXmin. While not |
| 6491 |
* immediately prunable, it is the oldest xid we can cheaply determine |
| 6492 |
* that's safe against wraparound / being older than the table's |
| 6493 |
* relfrozenxid. To defend against the unlikely case of a new relation |
| 6494 |
* having a newer relfrozenxid than our TransactionXmin, use relfrozenxid |
| 6495 |
* if so (vacuum can't subsequently move relfrozenxid to beyond |
| 6496 |
* TransactionXmin, so there's no race here). |
| 6497 |
*/ |
| 6498 |
Assert(TransactionIdIsValid(TransactionXmin)); |
| 6499 |
{ |
| 6500 |
TransactionId relfrozenxid = relation->rd_rel->relfrozenxid; |
| 6501 |
TransactionId prune_xid; |
| 6502 |
|
| 6503 |
if (TransactionIdPrecedes(TransactionXmin, relfrozenxid)) |
| 6504 |
prune_xid = relfrozenxid; |
| 6505 |
else |
| 6506 |
prune_xid = TransactionXmin; |
| 6507 |
PageSetPrunable(page, prune_xid); |
| 6508 |
} |
| 6509 |
|
| 6510 |
/* store transaction information of xact deleting the tuple */ |
| 6511 |
tp.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED); |
| 6512 |
tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 6513 |
|
| 6514 |
/* |
| 6515 |
* Set the tuple header xmin to InvalidTransactionId. This makes the |
| 6516 |
* tuple immediately invisible everyone. (In particular, to any |
| 6517 |
* transactions waiting on the speculative token, woken up later.) |
| 6518 |
*/ |
| 6519 |
HeapTupleHeaderSetXmin(tp.t_data, InvalidTransactionId); |
| 6520 |
|
| 6521 |
/* Clear the speculative insertion token too */ |
| 6522 |
tp.t_data->t_ctid = tp.t_self; |
| 6523 |
|
| 6524 |
MarkBufferDirty(buffer); |
| 6525 |
|
| 6526 |
/* |
| 6527 |
* XLOG stuff |
| 6528 |
* |
| 6529 |
* The WAL records generated here match heap_delete(). The same recovery |
| 6530 |
* routines are used. |
| 6531 |
*/ |
| 6532 |
if (RelationNeedsWAL(relation)) |
| 6533 |
{ |
| 6534 |
xl_heap_delete xlrec; |
| 6535 |
XLogRecPtr recptr; |
| 6536 |
|
| 6537 |
xlrec.flags = XLH_DELETE_IS_SUPER; |
| 6538 |
xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask, |
| 6539 |
tp.t_data->t_infomask2); |
| 6540 |
xlrec.offnum = ItemPointerGetOffsetNumber(&tp.t_self); |
| 6541 |
xlrec.xmax = xid; |
| 6542 |
|
| 6543 |
XLogBeginInsert(); |
| 6544 |
XLogRegisterData(&xlrec, SizeOfHeapDelete); |
| 6545 |
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD); |
| 6546 |
|
| 6547 |
/* No replica identity & replication origin logged */ |
| 6548 |
|
| 6549 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE); |
| 6550 |
|
| 6551 |
PageSetLSN(page, recptr); |
| 6552 |
} |
| 6553 |
|
| 6554 |
END_CRIT_SECTION(); |
| 6555 |
|
| 6556 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6557 |
|
| 6558 |
if (HeapTupleHasExternal(&tp)) |
| 6559 |
{ |
| 6560 |
Assert(!IsToastRelation(relation)); |
| 6561 |
heap_toast_delete(relation, &tp, true); |
| 6562 |
} |
| 6563 |
|
| 6564 |
/* |
| 6565 |
* Never need to mark tuple for invalidation, since catalogs don't support |
| 6566 |
* speculative insertion |
| 6567 |
*/ |
| 6568 |
|
| 6569 |
/* Now we can release the buffer */ |
| 6570 |
ReleaseBuffer(buffer); |
| 6571 |
|
| 6572 |
/* count deletion, as we counted the insertion too */ |
| 6573 |
pgstat_count_heap_delete(relation); |
| 6574 |
} |
| 6575 |
|
| 6576 |
/* |
| 6577 |
* heap_inplace_lock - protect inplace update from concurrent heap_update() |
| 6578 |
* |
| 6579 |
* Evaluate whether the tuple's state is compatible with a no-key update. |
| 6580 |
* Current transaction rowmarks are fine, as is KEY SHARE from any |
| 6581 |
* transaction. If compatible, return true with the buffer exclusive-locked, |
| 6582 |
* and the caller must release that by calling |
| 6583 |
* heap_inplace_update_and_unlock(), calling heap_inplace_unlock(), or raising |
| 6584 |
* an error. Otherwise, call release_callback(arg), wait for blocking |
| 6585 |
* transactions to end, and return false. |
| 6586 |
* |
| 6587 |
* Since this is intended for system catalogs and SERIALIZABLE doesn't cover |
| 6588 |
* DDL, this doesn't guarantee any particular predicate locking. |
| 6589 |
* |
| 6590 |
* One could modify this to return true for tuples with delete in progress, |
| 6591 |
* All inplace updaters take a lock that conflicts with DROP. If explicit |
| 6592 |
* "DELETE FROM pg_class" is in progress, we'll wait for it like we would an |
| 6593 |
* update. |
| 6594 |
* |
| 6595 |
* Readers of inplace-updated fields expect changes to those fields are |
| 6596 |
* durable. For example, vac_truncate_clog() reads datfrozenxid from |
| 6597 |
* pg_database tuples via catalog snapshots. A future snapshot must not |
| 6598 |
* return a lower datfrozenxid for the same database OID (lower in the |
| 6599 |
* FullTransactionIdPrecedes() sense). We achieve that since no update of a |
| 6600 |
* tuple can start while we hold a lock on its buffer. In cases like |
| 6601 |
* BEGIN;GRANT;CREATE INDEX;COMMIT we're inplace-updating a tuple visible only |
| 6602 |
* to this transaction. ROLLBACK then is one case where it's okay to lose |
| 6603 |
* inplace updates. (Restoring relhasindex=false on ROLLBACK is fine, since |
| 6604 |
* any concurrent CREATE INDEX would have blocked, then inplace-updated the |
| 6605 |
* committed tuple.) |
| 6606 |
* |
| 6607 |
* In principle, we could avoid waiting by overwriting every tuple in the |
| 6608 |
* updated tuple chain. Reader expectations permit updating a tuple only if |
| 6609 |
* it's aborted, is the tail of the chain, or we already updated the tuple |
| 6610 |
* referenced in its t_ctid. Hence, we would need to overwrite the tuples in |
| 6611 |
* order from tail to head. That would imply either (a) mutating all tuples |
| 6612 |
* in one critical section or (b) accepting a chance of partial completion. |
| 6613 |
* Partial completion of a relfrozenxid update would have the weird |
| 6614 |
* consequence that the table's next VACUUM could see the table's relfrozenxid |
| 6615 |
* move forward between vacuum_get_cutoffs() and finishing. |
| 6616 |
*/ |
| 6617 |
bool |
| 6618 |
heap_inplace_lock(Relation relation, |
| 6619 |
HeapTuple oldtup_ptr, Buffer buffer, |
| 6620 |
void (*release_callback) (void *), void *arg) |
| 6621 |
{ |
| 6622 |
HeapTupleData oldtup = *oldtup_ptr; /* minimize diff vs. heap_update() */ |
| 6623 |
TM_Result result; |
| 6624 |
bool ret; |
| 6625 |
|
| 6626 |
#ifdef USE_ASSERT_CHECKING |
| 6627 |
if (RelationGetRelid(relation) == RelationRelationId) |
| 6628 |
check_inplace_rel_lock(oldtup_ptr); |
| 6629 |
#endif |
| 6630 |
|
| 6631 |
Assert(BufferIsValid(buffer)); |
| 6632 |
|
| 6633 |
/* |
| 6634 |
* Register shared cache invals if necessary. Other sessions may finish |
| 6635 |
* inplace updates of this tuple between this step and LockTuple(). Since |
| 6636 |
* inplace updates don't change cache keys, that's harmless. |
| 6637 |
* |
| 6638 |
* While it's tempting to register invals only after confirming we can |
| 6639 |
* return true, the following obstacle precludes reordering steps that |
| 6640 |
* way. Registering invals might reach a CatalogCacheInitializeCache() |
| 6641 |
* that locks "buffer". That would hang indefinitely if running after our |
| 6642 |
* own LockBuffer(). Hence, we must register invals before LockBuffer(). |
| 6643 |
*/ |
| 6644 |
CacheInvalidateHeapTupleInplace(relation, oldtup_ptr); |
| 6645 |
|
| 6646 |
LockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock); |
| 6647 |
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); |
| 6648 |
|
| 6649 |
/*---------- |
| 6650 |
* Interpret HeapTupleSatisfiesUpdate() like heap_update() does, except: |
| 6651 |
* |
| 6652 |
* - wait unconditionally |
| 6653 |
* - already locked tuple above, since inplace needs that unconditionally |
| 6654 |
* - don't recheck header after wait: simpler to defer to next iteration |
| 6655 |
* - don't try to continue even if the updater aborts: likewise |
| 6656 |
* - no crosscheck |
| 6657 |
*/ |
| 6658 |
result = HeapTupleSatisfiesUpdate(&oldtup, GetCurrentCommandId(false), |
| 6659 |
buffer); |
| 6660 |
|
| 6661 |
if (result == TM_Invisible) |
| 6662 |
{ |
| 6663 |
/* no known way this can happen */ |
| 6664 |
ereport(ERROR, |
| 6665 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 6666 |
errmsg_internal("attempted to overwrite invisible tuple"))); |
| 6667 |
} |
| 6668 |
else if (result == TM_SelfModified) |
| 6669 |
{ |
| 6670 |
/* |
| 6671 |
* CREATE INDEX might reach this if an expression is silly enough to |
| 6672 |
* call e.g. SELECT ... FROM pg_class FOR SHARE. C code of other SQL |
| 6673 |
* statements might get here after a heap_update() of the same row, in |
| 6674 |
* the absence of an intervening CommandCounterIncrement(). |
| 6675 |
*/ |
| 6676 |
ereport(ERROR, |
| 6677 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 6678 |
errmsg("tuple to be updated was already modified by an operation triggered by the current command"))); |
| 6679 |
} |
| 6680 |
else if (result == TM_BeingModified) |
| 6681 |
{ |
| 6682 |
TransactionId xwait; |
| 6683 |
uint16 infomask; |
| 6684 |
|
| 6685 |
xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data); |
| 6686 |
infomask = oldtup.t_data->t_infomask; |
| 6687 |
|
| 6688 |
if (infomask & HEAP_XMAX_IS_MULTI) |
| 6689 |
{ |
| 6690 |
LockTupleMode lockmode = LockTupleNoKeyExclusive; |
| 6691 |
MultiXactStatus mxact_status = MultiXactStatusNoKeyUpdate; |
| 6692 |
int remain; |
| 6693 |
|
| 6694 |
if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, |
| 6695 |
lockmode, NULL)) |
| 6696 |
{ |
| 6697 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6698 |
release_callback(arg); |
| 6699 |
ret = false; |
| 6700 |
MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, |
| 6701 |
relation, &oldtup.t_self, XLTW_Update, |
| 6702 |
&remain); |
| 6703 |
} |
| 6704 |
else |
| 6705 |
ret = true; |
| 6706 |
} |
| 6707 |
else if (TransactionIdIsCurrentTransactionId(xwait)) |
| 6708 |
ret = true; |
| 6709 |
else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)) |
| 6710 |
ret = true; |
| 6711 |
else |
| 6712 |
{ |
| 6713 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6714 |
release_callback(arg); |
| 6715 |
ret = false; |
| 6716 |
XactLockTableWait(xwait, relation, &oldtup.t_self, |
| 6717 |
XLTW_Update); |
| 6718 |
} |
| 6719 |
} |
| 6720 |
else |
| 6721 |
{ |
| 6722 |
ret = (result == TM_Ok); |
| 6723 |
if (!ret) |
| 6724 |
{ |
| 6725 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6726 |
release_callback(arg); |
| 6727 |
} |
| 6728 |
} |
| 6729 |
|
| 6730 |
/* |
| 6731 |
* GetCatalogSnapshot() relies on invalidation messages to know when to |
| 6732 |
* take a new snapshot. COMMIT of xwait is responsible for sending the |
| 6733 |
* invalidation. We're not acquiring heavyweight locks sufficient to |
| 6734 |
* block if not yet sent, so we must take a new snapshot to ensure a later |
| 6735 |
* attempt has a fair chance. While we don't need this if xwait aborted, |
| 6736 |
* don't bother optimizing that. |
| 6737 |
*/ |
| 6738 |
if (!ret) |
| 6739 |
{ |
| 6740 |
UnlockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock); |
| 6741 |
ForgetInplace_Inval(); |
| 6742 |
InvalidateCatalogSnapshot(); |
| 6743 |
} |
| 6744 |
return ret; |
| 6745 |
} |
| 6746 |
|
| 6747 |
/* |
| 6748 |
* heap_inplace_update_and_unlock - core of systable_inplace_update_finish |
| 6749 |
* |
| 6750 |
* The tuple cannot change size, and therefore its header fields and null |
| 6751 |
* bitmap (if any) don't change either. |
| 6752 |
* |
| 6753 |
* Since we hold LOCKTAG_TUPLE, no updater has a local copy of this tuple. |
| 6754 |
*/ |
| 6755 |
void |
| 6756 |
heap_inplace_update_and_unlock(Relation relation, |
| 6757 |
HeapTuple oldtup, HeapTuple tuple, |
| 6758 |
Buffer buffer) |
| 6759 |
{ |
| 6760 |
HeapTupleHeader htup = oldtup->t_data; |
| 6761 |
uint32 oldlen; |
| 6762 |
uint32 newlen; |
| 6763 |
char *dst; |
| 6764 |
char *src; |
| 6765 |
int nmsgs = 0; |
| 6766 |
SharedInvalidationMessage *invalMessages = NULL; |
| 6767 |
bool RelcacheInitFileInval = false; |
| 6768 |
|
| 6769 |
Assert(ItemPointerEquals(&oldtup->t_self, &tuple->t_self)); |
| 6770 |
oldlen = oldtup->t_len - htup->t_hoff; |
| 6771 |
newlen = tuple->t_len - tuple->t_data->t_hoff; |
| 6772 |
if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff) |
| 6773 |
elog(ERROR, "wrong tuple length"); |
| 6774 |
|
| 6775 |
dst = (char *) htup + htup->t_hoff; |
| 6776 |
src = (char *) tuple->t_data + tuple->t_data->t_hoff; |
| 6777 |
|
| 6778 |
/* Like RecordTransactionCommit(), log only if needed */ |
| 6779 |
if (XLogStandbyInfoActive()) |
| 6780 |
nmsgs = inplaceGetInvalidationMessages(&invalMessages, |
| 6781 |
&RelcacheInitFileInval); |
| 6782 |
|
| 6783 |
/* |
| 6784 |
* Unlink relcache init files as needed. If unlinking, acquire |
| 6785 |
* RelCacheInitLock until after associated invalidations. By doing this |
| 6786 |
* in advance, if we checkpoint and then crash between inplace |
| 6787 |
* XLogInsert() and inval, we don't rely on StartupXLOG() -> |
| 6788 |
* RelationCacheInitFileRemove(). That uses elevel==LOG, so replay would |
| 6789 |
* neglect to PANIC on EIO. |
| 6790 |
*/ |
| 6791 |
PreInplace_Inval(); |
| 6792 |
|
| 6793 |
/*---------- |
| 6794 |
* NO EREPORT(ERROR) from here till changes are complete |
| 6795 |
* |
| 6796 |
* Our buffer lock won't stop a reader having already pinned and checked |
| 6797 |
* visibility for this tuple. Hence, we write WAL first, then mutate the |
| 6798 |
* buffer. Like in MarkBufferDirtyHint() or RecordTransactionCommit(), |
| 6799 |
* checkpoint delay makes that acceptable. With the usual order of |
| 6800 |
* changes, a crash after memcpy() and before XLogInsert() could allow |
| 6801 |
* datfrozenxid to overtake relfrozenxid: |
| 6802 |
* |
| 6803 |
* ["D" is a VACUUM (ONLY_DATABASE_STATS)] |
| 6804 |
* ["R" is a VACUUM tbl] |
| 6805 |
* D: vac_update_datfrozenxid() -> systable_beginscan(pg_class) |
| 6806 |
* D: systable_getnext() returns pg_class tuple of tbl |
| 6807 |
* R: memcpy() into pg_class tuple of tbl |
| 6808 |
* D: raise pg_database.datfrozenxid, XLogInsert(), finish |
| 6809 |
* [crash] |
| 6810 |
* [recovery restores datfrozenxid w/o relfrozenxid] |
| 6811 |
* |
| 6812 |
* Mimic MarkBufferDirtyHint() subroutine XLogSaveBufferForHint(). |
| 6813 |
* Specifically, use DELAY_CHKPT_START, and copy the buffer to the stack. |
| 6814 |
* The stack copy facilitates a FPI of the post-mutation block before we |
| 6815 |
* accept other sessions seeing it. DELAY_CHKPT_START allows us to |
| 6816 |
* XLogInsert() before MarkBufferDirty(). Since XLogSaveBufferForHint() |
| 6817 |
* can operate under BUFFER_LOCK_SHARED, it can't avoid DELAY_CHKPT_START. |
| 6818 |
* This function, however, likely could avoid it with the following order |
| 6819 |
* of operations: MarkBufferDirty(), XLogInsert(), memcpy(). Opt to use |
| 6820 |
* DELAY_CHKPT_START here, too, as a way to have fewer distinct code |
| 6821 |
* patterns to analyze. Inplace update isn't so frequent that it should |
| 6822 |
* pursue the small optimization of skipping DELAY_CHKPT_START. |
| 6823 |
*/ |
| 6824 |
Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0); |
| 6825 |
START_CRIT_SECTION(); |
| 6826 |
MyProc->delayChkptFlags |= DELAY_CHKPT_START; |
| 6827 |
|
| 6828 |
/* XLOG stuff */ |
| 6829 |
if (RelationNeedsWAL(relation)) |
| 6830 |
{ |
| 6831 |
xl_heap_inplace xlrec; |
| 6832 |
PGAlignedBlock copied_buffer; |
| 6833 |
char *origdata = (char *) BufferGetBlock(buffer); |
| 6834 |
Page page = BufferGetPage(buffer); |
| 6835 |
uint16 lower = ((PageHeader) page)->pd_lower; |
| 6836 |
uint16 upper = ((PageHeader) page)->pd_upper; |
| 6837 |
uintptr_t dst_offset_in_block; |
| 6838 |
RelFileLocator rlocator; |
| 6839 |
ForkNumber forkno; |
| 6840 |
BlockNumber blkno; |
| 6841 |
XLogRecPtr recptr; |
| 6842 |
|
| 6843 |
xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self); |
| 6844 |
xlrec.dbId = MyDatabaseId; |
| 6845 |
xlrec.tsId = MyDatabaseTableSpace; |
| 6846 |
xlrec.relcacheInitFileInval = RelcacheInitFileInval; |
| 6847 |
xlrec.nmsgs = nmsgs; |
| 6848 |
|
| 6849 |
XLogBeginInsert(); |
| 6850 |
XLogRegisterData(&xlrec, MinSizeOfHeapInplace); |
| 6851 |
if (nmsgs != 0) |
| 6852 |
XLogRegisterData(invalMessages, |
| 6853 |
nmsgs * sizeof(SharedInvalidationMessage)); |
| 6854 |
|
| 6855 |
/* register block matching what buffer will look like after changes */ |
| 6856 |
memcpy(copied_buffer.data, origdata, lower); |
| 6857 |
memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper); |
| 6858 |
dst_offset_in_block = dst - origdata; |
| 6859 |
memcpy(copied_buffer.data + dst_offset_in_block, src, newlen); |
| 6860 |
BufferGetTag(buffer, &rlocator, &forkno, &blkno); |
| 6861 |
Assert(forkno == MAIN_FORKNUM); |
| 6862 |
XLogRegisterBlock(0, &rlocator, forkno, blkno, copied_buffer.data, |
| 6863 |
REGBUF_STANDARD); |
| 6864 |
XLogRegisterBufData(0, src, newlen); |
| 6865 |
|
| 6866 |
/* inplace updates aren't decoded atm, don't log the origin */ |
| 6867 |
|
| 6868 |
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE); |
| 6869 |
|
| 6870 |
PageSetLSN(page, recptr); |
| 6871 |
} |
| 6872 |
|
| 6873 |
memcpy(dst, src, newlen); |
| 6874 |
|
| 6875 |
MarkBufferDirty(buffer); |
| 6876 |
|
| 6877 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6878 |
|
| 6879 |
/* |
| 6880 |
* Send invalidations to shared queue. SearchSysCacheLocked1() assumes we |
| 6881 |
* do this before UnlockTuple(). |
| 6882 |
*/ |
| 6883 |
AtInplace_Inval(); |
| 6884 |
|
| 6885 |
MyProc->delayChkptFlags &= ~DELAY_CHKPT_START; |
| 6886 |
END_CRIT_SECTION(); |
| 6887 |
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock); |
| 6888 |
|
| 6889 |
AcceptInvalidationMessages(); /* local processing of just-sent inval */ |
| 6890 |
|
| 6891 |
/* |
| 6892 |
* Queue a transactional inval, for logical decoding and for third-party |
| 6893 |
* code that might have been relying on it since long before inplace |
| 6894 |
* update adopted immediate invalidation. See README.tuplock section |
| 6895 |
* "Reading inplace-updated columns" for logical decoding details. |
| 6896 |
*/ |
| 6897 |
if (!IsBootstrapProcessingMode()) |
| 6898 |
CacheInvalidateHeapTuple(relation, tuple, NULL); |
| 6899 |
} |
| 6900 |
|
| 6901 |
/* |
| 6902 |
* heap_inplace_unlock - reverse of heap_inplace_lock |
| 6903 |
*/ |
| 6904 |
void |
| 6905 |
heap_inplace_unlock(Relation relation, |
| 6906 |
HeapTuple oldtup, Buffer buffer) |
| 6907 |
{ |
| 6908 |
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 6909 |
UnlockTuple(relation, &oldtup->t_self, InplaceUpdateTupleLock); |
| 6910 |
ForgetInplace_Inval(); |
| 6911 |
} |
| 6912 |
|
| 6913 |
#define FRM_NOOP 0x0001 |
| 6914 |
#define FRM_INVALIDATE_XMAX 0x0002 |
| 6915 |
#define FRM_RETURN_IS_XID 0x0004 |
| 6916 |
#define FRM_RETURN_IS_MULTI 0x0008 |
| 6917 |
#define FRM_MARK_COMMITTED 0x0010 |
| 6918 |
|
| 6919 |
/* |
| 6920 |
* FreezeMultiXactId |
| 6921 |
* Determine what to do during freezing when a tuple is marked by a |
| 6922 |
* MultiXactId. |
| 6923 |
* |
| 6924 |
* "flags" is an output value; it's used to tell caller what to do on return. |
| 6925 |
* "pagefrz" is an input/output value, used to manage page level freezing. |
| 6926 |
* |
| 6927 |
* Possible values that we can set in "flags": |
| 6928 |
* FRM_NOOP |
| 6929 |
* don't do anything -- keep existing Xmax |
| 6930 |
* FRM_INVALIDATE_XMAX |
| 6931 |
* mark Xmax as InvalidTransactionId and set XMAX_INVALID flag. |
| 6932 |
* FRM_RETURN_IS_XID |
| 6933 |
* The Xid return value is a single update Xid to set as xmax. |
| 6934 |
* FRM_MARK_COMMITTED |
| 6935 |
* Xmax can be marked as HEAP_XMAX_COMMITTED |
| 6936 |
* FRM_RETURN_IS_MULTI |
| 6937 |
* The return value is a new MultiXactId to set as new Xmax. |
| 6938 |
* (caller must obtain proper infomask bits using GetMultiXactIdHintBits) |
| 6939 |
* |
| 6940 |
* Caller delegates control of page freezing to us. In practice we always |
| 6941 |
* force freezing of caller's page unless FRM_NOOP processing is indicated. |
| 6942 |
* We help caller ensure that XIDs < FreezeLimit and MXIDs < MultiXactCutoff |
| 6943 |
* can never be left behind. We freely choose when and how to process each |
| 6944 |
* Multi, without ever violating the cutoff postconditions for freezing. |
| 6945 |
* |
| 6946 |
* It's useful to remove Multis on a proactive timeline (relative to freezing |
| 6947 |
* XIDs) to keep MultiXact member SLRU buffer misses to a minimum. It can also |
| 6948 |
* be cheaper in the short run, for us, since we too can avoid SLRU buffer |
| 6949 |
* misses through eager processing. |
| 6950 |
* |
| 6951 |
* NB: Creates a _new_ MultiXactId when FRM_RETURN_IS_MULTI is set, though only |
| 6952 |
* when FreezeLimit and/or MultiXactCutoff cutoffs leave us with no choice. |
| 6953 |
* This can usually be put off, which is usually enough to avoid it altogether. |
| 6954 |
* Allocating new multis during VACUUM should be avoided on general principle; |
| 6955 |
* only VACUUM can advance relminmxid, so allocating new Multis here comes with |
| 6956 |
* its own special risks. |
| 6957 |
* |
| 6958 |
* NB: Caller must maintain "no freeze" NewRelfrozenXid/NewRelminMxid trackers |
| 6959 |
* using heap_tuple_should_freeze when we haven't forced page-level freezing. |
| 6960 |
* |
| 6961 |
* NB: Caller should avoid needlessly calling heap_tuple_should_freeze when we |
| 6962 |
* have already forced page-level freezing, since that might incur the same |
| 6963 |
* SLRU buffer misses that we specifically intended to avoid by freezing. |
| 6964 |
*/ |
| 6965 |
static TransactionId |
| 6966 |
FreezeMultiXactId(MultiXactId multi, uint16 t_infomask, |
| 6967 |
const struct VacuumCutoffs *cutoffs, uint16 *flags, |
| 6968 |
HeapPageFreeze *pagefrz) |
| 6969 |
{ |
| 6970 |
TransactionId newxmax; |
| 6971 |
MultiXactMember *members; |
| 6972 |
int nmembers; |
| 6973 |
bool need_replace; |
| 6974 |
int nnewmembers; |
| 6975 |
MultiXactMember *newmembers; |
| 6976 |
bool has_lockers; |
| 6977 |
TransactionId update_xid; |
| 6978 |
bool update_committed; |
| 6979 |
TransactionId FreezePageRelfrozenXid; |
| 6980 |
|
| 6981 |
*flags = 0; |
| 6982 |
|
| 6983 |
/* We should only be called in Multis */ |
| 6984 |
Assert(t_infomask & HEAP_XMAX_IS_MULTI); |
| 6985 |
|
| 6986 |
if (!MultiXactIdIsValid(multi) || |
| 6987 |
HEAP_LOCKED_UPGRADED(t_infomask)) |
| 6988 |
{ |
| 6989 |
*flags |= FRM_INVALIDATE_XMAX; |
| 6990 |
pagefrz->freeze_required = true; |
| 6991 |
return InvalidTransactionId; |
| 6992 |
} |
| 6993 |
else if (MultiXactIdPrecedes(multi, cutoffs->relminmxid)) |
| 6994 |
ereport(ERROR, |
| 6995 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 6996 |
errmsg_internal("found multixact %u from before relminmxid %u", |
| 6997 |
multi, cutoffs->relminmxid))); |
| 6998 |
else if (MultiXactIdPrecedes(multi, cutoffs->OldestMxact)) |
| 6999 |
{ |
| 7000 |
TransactionId update_xact; |
| 7001 |
|
| 7002 |
/* |
| 7003 |
* This old multi cannot possibly have members still running, but |
| 7004 |
* verify just in case. If it was a locker only, it can be removed |
| 7005 |
* without any further consideration; but if it contained an update, |
| 7006 |
* we might need to preserve it. |
| 7007 |
*/ |
| 7008 |
if (MultiXactIdIsRunning(multi, |
| 7009 |
HEAP_XMAX_IS_LOCKED_ONLY(t_infomask))) |
| 7010 |
ereport(ERROR, |
| 7011 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7012 |
errmsg_internal("multixact %u from before multi freeze cutoff %u found to be still running", |
| 7013 |
multi, cutoffs->OldestMxact))); |
| 7014 |
|
| 7015 |
if (HEAP_XMAX_IS_LOCKED_ONLY(t_infomask)) |
| 7016 |
{ |
| 7017 |
*flags |= FRM_INVALIDATE_XMAX; |
| 7018 |
pagefrz->freeze_required = true; |
| 7019 |
return InvalidTransactionId; |
| 7020 |
} |
| 7021 |
|
| 7022 |
/* replace multi with single XID for its updater? */ |
| 7023 |
update_xact = MultiXactIdGetUpdateXid(multi, t_infomask); |
| 7024 |
if (TransactionIdPrecedes(update_xact, cutoffs->relfrozenxid)) |
| 7025 |
ereport(ERROR, |
| 7026 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7027 |
errmsg_internal("multixact %u contains update XID %u from before relfrozenxid %u", |
| 7028 |
multi, update_xact, |
| 7029 |
cutoffs->relfrozenxid))); |
| 7030 |
else if (TransactionIdPrecedes(update_xact, cutoffs->OldestXmin)) |
| 7031 |
{ |
| 7032 |
/* |
| 7033 |
* Updater XID has to have aborted (otherwise the tuple would have |
| 7034 |
* been pruned away instead, since updater XID is < OldestXmin). |
| 7035 |
* Just remove xmax. |
| 7036 |
*/ |
| 7037 |
if (TransactionIdDidCommit(update_xact)) |
| 7038 |
ereport(ERROR, |
| 7039 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7040 |
errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u", |
| 7041 |
multi, update_xact, |
| 7042 |
cutoffs->OldestXmin))); |
| 7043 |
*flags |= FRM_INVALIDATE_XMAX; |
| 7044 |
pagefrz->freeze_required = true; |
| 7045 |
return InvalidTransactionId; |
| 7046 |
} |
| 7047 |
|
| 7048 |
/* Have to keep updater XID as new xmax */ |
| 7049 |
*flags |= FRM_RETURN_IS_XID; |
| 7050 |
pagefrz->freeze_required = true; |
| 7051 |
return update_xact; |
| 7052 |
} |
| 7053 |
|
| 7054 |
/* |
| 7055 |
* Some member(s) of this Multi may be below FreezeLimit xid cutoff, so we |
| 7056 |
* need to walk the whole members array to figure out what to do, if |
| 7057 |
* anything. |
| 7058 |
*/ |
| 7059 |
nmembers = |
| 7060 |
GetMultiXactIdMembers(multi, &members, false, |
| 7061 |
HEAP_XMAX_IS_LOCKED_ONLY(t_infomask)); |
| 7062 |
if (nmembers <= 0) |
| 7063 |
{ |
| 7064 |
/* Nothing worth keeping */ |
| 7065 |
*flags |= FRM_INVALIDATE_XMAX; |
| 7066 |
pagefrz->freeze_required = true; |
| 7067 |
return InvalidTransactionId; |
| 7068 |
} |
| 7069 |
|
| 7070 |
/* |
| 7071 |
* The FRM_NOOP case is the only case where we might need to ratchet back |
| 7072 |
* FreezePageRelfrozenXid or FreezePageRelminMxid. It is also the only |
| 7073 |
* case where our caller might ratchet back its NoFreezePageRelfrozenXid |
| 7074 |
* or NoFreezePageRelminMxid "no freeze" trackers to deal with a multi. |
| 7075 |
* FRM_NOOP handling should result in the NewRelfrozenXid/NewRelminMxid |
| 7076 |
* trackers managed by VACUUM being ratcheting back by xmax to the degree |
| 7077 |
* required to make it safe to leave xmax undisturbed, independent of |
| 7078 |
* whether or not page freezing is triggered somewhere else. |
| 7079 |
* |
| 7080 |
* Our policy is to force freezing in every case other than FRM_NOOP, |
| 7081 |
* which obviates the need to maintain either set of trackers, anywhere. |
| 7082 |
* Every other case will reliably execute a freeze plan for xmax that |
| 7083 |
* either replaces xmax with an XID/MXID >= OldestXmin/OldestMxact, or |
| 7084 |
* sets xmax to an InvalidTransactionId XID, rendering xmax fully frozen. |
| 7085 |
* (VACUUM's NewRelfrozenXid/NewRelminMxid trackers are initialized with |
| 7086 |
* OldestXmin/OldestMxact, so later values never need to be tracked here.) |
| 7087 |
*/ |
| 7088 |
need_replace = false; |
| 7089 |
FreezePageRelfrozenXid = pagefrz->FreezePageRelfrozenXid; |
| 7090 |
for (int i = 0; i < nmembers; i++) |
| 7091 |
{ |
| 7092 |
TransactionId xid = members[i].xid; |
| 7093 |
|
| 7094 |
Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid)); |
| 7095 |
|
| 7096 |
if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit)) |
| 7097 |
{ |
| 7098 |
/* Can't violate the FreezeLimit postcondition */ |
| 7099 |
need_replace = true; |
| 7100 |
break; |
| 7101 |
} |
| 7102 |
if (TransactionIdPrecedes(xid, FreezePageRelfrozenXid)) |
| 7103 |
FreezePageRelfrozenXid = xid; |
| 7104 |
} |
| 7105 |
|
| 7106 |
/* Can't violate the MultiXactCutoff postcondition, either */ |
| 7107 |
if (!need_replace) |
| 7108 |
need_replace = MultiXactIdPrecedes(multi, cutoffs->MultiXactCutoff); |
| 7109 |
|
| 7110 |
if (!need_replace) |
| 7111 |
{ |
| 7112 |
/* |
| 7113 |
* vacuumlazy.c might ratchet back NewRelminMxid, NewRelfrozenXid, or |
| 7114 |
* both together to make it safe to retain this particular multi after |
| 7115 |
* freezing its page |
| 7116 |
*/ |
| 7117 |
*flags |= FRM_NOOP; |
| 7118 |
pagefrz->FreezePageRelfrozenXid = FreezePageRelfrozenXid; |
| 7119 |
if (MultiXactIdPrecedes(multi, pagefrz->FreezePageRelminMxid)) |
| 7120 |
pagefrz->FreezePageRelminMxid = multi; |
| 7121 |
pfree(members); |
| 7122 |
return multi; |
| 7123 |
} |
| 7124 |
|
| 7125 |
/* |
| 7126 |
* Do a more thorough second pass over the multi to figure out which |
| 7127 |
* member XIDs actually need to be kept. Checking the precise status of |
| 7128 |
* individual members might even show that we don't need to keep anything. |
| 7129 |
* That is quite possible even though the Multi must be >= OldestMxact, |
| 7130 |
* since our second pass only keeps member XIDs when it's truly necessary; |
| 7131 |
* even member XIDs >= OldestXmin often won't be kept by second pass. |
| 7132 |
*/ |
| 7133 |
nnewmembers = 0; |
| 7134 |
newmembers = palloc(sizeof(MultiXactMember) * nmembers); |
| 7135 |
has_lockers = false; |
| 7136 |
update_xid = InvalidTransactionId; |
| 7137 |
update_committed = false; |
| 7138 |
|
| 7139 |
/* |
| 7140 |
* Determine whether to keep each member xid, or to ignore it instead |
| 7141 |
*/ |
| 7142 |
for (int i = 0; i < nmembers; i++) |
| 7143 |
{ |
| 7144 |
TransactionId xid = members[i].xid; |
| 7145 |
MultiXactStatus mstatus = members[i].status; |
| 7146 |
|
| 7147 |
Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid)); |
| 7148 |
|
| 7149 |
if (!ISUPDATE_from_mxstatus(mstatus)) |
| 7150 |
{ |
| 7151 |
/* |
| 7152 |
* Locker XID (not updater XID). We only keep lockers that are |
| 7153 |
* still running. |
| 7154 |
*/ |
| 7155 |
if (TransactionIdIsCurrentTransactionId(xid) || |
| 7156 |
TransactionIdIsInProgress(xid)) |
| 7157 |
{ |
| 7158 |
if (TransactionIdPrecedes(xid, cutoffs->OldestXmin)) |
| 7159 |
ereport(ERROR, |
| 7160 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7161 |
errmsg_internal("multixact %u contains running locker XID %u from before removable cutoff %u", |
| 7162 |
multi, xid, |
| 7163 |
cutoffs->OldestXmin))); |
| 7164 |
newmembers[nnewmembers++] = members[i]; |
| 7165 |
has_lockers = true; |
| 7166 |
} |
| 7167 |
|
| 7168 |
continue; |
| 7169 |
} |
| 7170 |
|
| 7171 |
/* |
| 7172 |
* Updater XID (not locker XID). Should we keep it? |
| 7173 |
* |
| 7174 |
* Since the tuple wasn't totally removed when vacuum pruned, the |
| 7175 |
* update Xid cannot possibly be older than OldestXmin cutoff unless |
| 7176 |
* the updater XID aborted. If the updater transaction is known |
| 7177 |
* aborted or crashed then it's okay to ignore it, otherwise not. |
| 7178 |
* |
| 7179 |
* In any case the Multi should never contain two updaters, whatever |
| 7180 |
* their individual commit status. Check for that first, in passing. |
| 7181 |
*/ |
| 7182 |
if (TransactionIdIsValid(update_xid)) |
| 7183 |
ereport(ERROR, |
| 7184 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7185 |
errmsg_internal("multixact %u has two or more updating members", |
| 7186 |
multi), |
| 7187 |
errdetail_internal("First updater XID=%u second updater XID=%u.", |
| 7188 |
update_xid, xid))); |
| 7189 |
|
| 7190 |
/* |
| 7191 |
* As with all tuple visibility routines, it's critical to test |
| 7192 |
* TransactionIdIsInProgress before TransactionIdDidCommit, because of |
| 7193 |
* race conditions explained in detail in heapam_visibility.c. |
| 7194 |
*/ |
| 7195 |
if (TransactionIdIsCurrentTransactionId(xid) || |
| 7196 |
TransactionIdIsInProgress(xid)) |
| 7197 |
update_xid = xid; |
| 7198 |
else if (TransactionIdDidCommit(xid)) |
| 7199 |
{ |
| 7200 |
/* |
| 7201 |
* The transaction committed, so we can tell caller to set |
| 7202 |
* HEAP_XMAX_COMMITTED. (We can only do this because we know the |
| 7203 |
* transaction is not running.) |
| 7204 |
*/ |
| 7205 |
update_committed = true; |
| 7206 |
update_xid = xid; |
| 7207 |
} |
| 7208 |
else |
| 7209 |
{ |
| 7210 |
/* |
| 7211 |
* Not in progress, not committed -- must be aborted or crashed; |
| 7212 |
* we can ignore it. |
| 7213 |
*/ |
| 7214 |
continue; |
| 7215 |
} |
| 7216 |
|
| 7217 |
/* |
| 7218 |
* We determined that updater must be kept -- add it to pending new |
| 7219 |
* members list |
| 7220 |
*/ |
| 7221 |
if (TransactionIdPrecedes(xid, cutoffs->OldestXmin)) |
| 7222 |
ereport(ERROR, |
| 7223 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7224 |
errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u", |
| 7225 |
multi, xid, cutoffs->OldestXmin))); |
| 7226 |
newmembers[nnewmembers++] = members[i]; |
| 7227 |
} |
| 7228 |
|
| 7229 |
pfree(members); |
| 7230 |
|
| 7231 |
/* |
| 7232 |
* Determine what to do with caller's multi based on information gathered |
| 7233 |
* during our second pass |
| 7234 |
*/ |
| 7235 |
if (nnewmembers == 0) |
| 7236 |
{ |
| 7237 |
/* Nothing worth keeping */ |
| 7238 |
*flags |= FRM_INVALIDATE_XMAX; |
| 7239 |
newxmax = InvalidTransactionId; |
| 7240 |
} |
| 7241 |
else if (TransactionIdIsValid(update_xid) && !has_lockers) |
| 7242 |
{ |
| 7243 |
/* |
| 7244 |
* If there's a single member and it's an update, pass it back alone |
| 7245 |
* without creating a new Multi. (XXX we could do this when there's a |
| 7246 |
* single remaining locker, too, but that would complicate the API too |
| 7247 |
* much; moreover, the case with the single updater is more |
| 7248 |
* interesting, because those are longer-lived.) |
| 7249 |
*/ |
| 7250 |
Assert(nnewmembers == 1); |
| 7251 |
*flags |= FRM_RETURN_IS_XID; |
| 7252 |
if (update_committed) |
| 7253 |
*flags |= FRM_MARK_COMMITTED; |
| 7254 |
newxmax = update_xid; |
| 7255 |
} |
| 7256 |
else |
| 7257 |
{ |
| 7258 |
/* |
| 7259 |
* Create a new multixact with the surviving members of the previous |
| 7260 |
* one, to set as new Xmax in the tuple |
| 7261 |
*/ |
| 7262 |
newxmax = MultiXactIdCreateFromMembers(nnewmembers, newmembers); |
| 7263 |
*flags |= FRM_RETURN_IS_MULTI; |
| 7264 |
} |
| 7265 |
|
| 7266 |
pfree(newmembers); |
| 7267 |
|
| 7268 |
pagefrz->freeze_required = true; |
| 7269 |
return newxmax; |
| 7270 |
} |
| 7271 |
|
| 7272 |
/* |
| 7273 |
* heap_prepare_freeze_tuple |
| 7274 |
* |
| 7275 |
* Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac) |
| 7276 |
* are older than the OldestXmin and/or OldestMxact freeze cutoffs. If so, |
| 7277 |
* setup enough state (in the *frz output argument) to enable caller to |
| 7278 |
* process this tuple as part of freezing its page, and return true. Return |
| 7279 |
* false if nothing can be changed about the tuple right now. |
| 7280 |
* |
| 7281 |
* Also sets *totally_frozen to true if the tuple will be totally frozen once |
| 7282 |
* caller executes returned freeze plan (or if the tuple was already totally |
| 7283 |
* frozen by an earlier VACUUM). This indicates that there are no remaining |
| 7284 |
* XIDs or MultiXactIds that will need to be processed by a future VACUUM. |
| 7285 |
* |
| 7286 |
* VACUUM caller must assemble HeapTupleFreeze freeze plan entries for every |
| 7287 |
* tuple that we returned true for, and then execute freezing. Caller must |
| 7288 |
* initialize pagefrz fields for page as a whole before first call here for |
| 7289 |
* each heap page. |
| 7290 |
* |
| 7291 |
* VACUUM caller decides on whether or not to freeze the page as a whole. |
| 7292 |
* We'll often prepare freeze plans for a page that caller just discards. |
| 7293 |
* However, VACUUM doesn't always get to make a choice; it must freeze when |
| 7294 |
* pagefrz.freeze_required is set, to ensure that any XIDs < FreezeLimit (and |
| 7295 |
* MXIDs < MultiXactCutoff) can never be left behind. We help to make sure |
| 7296 |
* that VACUUM always follows that rule. |
| 7297 |
* |
| 7298 |
* We sometimes force freezing of xmax MultiXactId values long before it is |
| 7299 |
* strictly necessary to do so just to ensure the FreezeLimit postcondition. |
| 7300 |
* It's worth processing MultiXactIds proactively when it is cheap to do so, |
| 7301 |
* and it's convenient to make that happen by piggy-backing it on the "force |
| 7302 |
* freezing" mechanism. Conversely, we sometimes delay freezing MultiXactIds |
| 7303 |
* because it is expensive right now (though only when it's still possible to |
| 7304 |
* do so without violating the FreezeLimit/MultiXactCutoff postcondition). |
| 7305 |
* |
| 7306 |
* It is assumed that the caller has checked the tuple with |
| 7307 |
* HeapTupleSatisfiesVacuum() and determined that it is not HEAPTUPLE_DEAD |
| 7308 |
* (else we should be removing the tuple, not freezing it). |
| 7309 |
* |
| 7310 |
* NB: This function has side effects: it might allocate a new MultiXactId. |
| 7311 |
* It will be set as tuple's new xmax when our *frz output is processed within |
| 7312 |
* heap_execute_freeze_tuple later on. If the tuple is in a shared buffer |
| 7313 |
* then caller had better have an exclusive lock on it already. |
| 7314 |
*/ |
| 7315 |
bool |
| 7316 |
heap_prepare_freeze_tuple(HeapTupleHeader tuple, |
| 7317 |
const struct VacuumCutoffs *cutoffs, |
| 7318 |
HeapPageFreeze *pagefrz, |
| 7319 |
HeapTupleFreeze *frz, bool *totally_frozen) |
| 7320 |
{ |
| 7321 |
bool xmin_already_frozen = false, |
| 7322 |
xmax_already_frozen = false; |
| 7323 |
bool freeze_xmin = false, |
| 7324 |
replace_xvac = false, |
| 7325 |
replace_xmax = false, |
| 7326 |
freeze_xmax = false; |
| 7327 |
TransactionId xid; |
| 7328 |
|
| 7329 |
frz->xmax = HeapTupleHeaderGetRawXmax(tuple); |
| 7330 |
frz->t_infomask2 = tuple->t_infomask2; |
| 7331 |
frz->t_infomask = tuple->t_infomask; |
| 7332 |
frz->frzflags = 0; |
| 7333 |
frz->checkflags = 0; |
| 7334 |
|
| 7335 |
/* |
| 7336 |
* Process xmin, while keeping track of whether it's already frozen, or |
| 7337 |
* will become frozen iff our freeze plan is executed by caller (could be |
| 7338 |
* neither). |
| 7339 |
*/ |
| 7340 |
xid = HeapTupleHeaderGetXmin(tuple); |
| 7341 |
if (!TransactionIdIsNormal(xid)) |
| 7342 |
xmin_already_frozen = true; |
| 7343 |
else |
| 7344 |
{ |
| 7345 |
if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid)) |
| 7346 |
ereport(ERROR, |
| 7347 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7348 |
errmsg_internal("found xmin %u from before relfrozenxid %u", |
| 7349 |
xid, cutoffs->relfrozenxid))); |
| 7350 |
|
| 7351 |
/* Will set freeze_xmin flags in freeze plan below */ |
| 7352 |
freeze_xmin = TransactionIdPrecedes(xid, cutoffs->OldestXmin); |
| 7353 |
|
| 7354 |
/* Verify that xmin committed if and when freeze plan is executed */ |
| 7355 |
if (freeze_xmin) |
| 7356 |
frz->checkflags |= HEAP_FREEZE_CHECK_XMIN_COMMITTED; |
| 7357 |
} |
| 7358 |
|
| 7359 |
/* |
| 7360 |
* Old-style VACUUM FULL is gone, but we have to process xvac for as long |
| 7361 |
* as we support having MOVED_OFF/MOVED_IN tuples in the database |
| 7362 |
*/ |
| 7363 |
xid = HeapTupleHeaderGetXvac(tuple); |
| 7364 |
if (TransactionIdIsNormal(xid)) |
| 7365 |
{ |
| 7366 |
Assert(TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid, xid)); |
| 7367 |
Assert(TransactionIdPrecedes(xid, cutoffs->OldestXmin)); |
| 7368 |
|
| 7369 |
/* |
| 7370 |
* For Xvac, we always freeze proactively. This allows totally_frozen |
| 7371 |
* tracking to ignore xvac. |
| 7372 |
*/ |
| 7373 |
replace_xvac = pagefrz->freeze_required = true; |
| 7374 |
|
| 7375 |
/* Will set replace_xvac flags in freeze plan below */ |
| 7376 |
} |
| 7377 |
|
| 7378 |
/* Now process xmax */ |
| 7379 |
xid = frz->xmax; |
| 7380 |
if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) |
| 7381 |
{ |
| 7382 |
/* Raw xmax is a MultiXactId */ |
| 7383 |
TransactionId newxmax; |
| 7384 |
uint16 flags; |
| 7385 |
|
| 7386 |
/* |
| 7387 |
* We will either remove xmax completely (in the "freeze_xmax" path), |
| 7388 |
* process xmax by replacing it (in the "replace_xmax" path), or |
| 7389 |
* perform no-op xmax processing. The only constraint is that the |
| 7390 |
* FreezeLimit/MultiXactCutoff postcondition must never be violated. |
| 7391 |
*/ |
| 7392 |
newxmax = FreezeMultiXactId(xid, tuple->t_infomask, cutoffs, |
| 7393 |
&flags, pagefrz); |
| 7394 |
|
| 7395 |
if (flags & FRM_NOOP) |
| 7396 |
{ |
| 7397 |
/* |
| 7398 |
* xmax is a MultiXactId, and nothing about it changes for now. |
| 7399 |
* This is the only case where 'freeze_required' won't have been |
| 7400 |
* set for us by FreezeMultiXactId, as well as the only case where |
| 7401 |
* neither freeze_xmax nor replace_xmax are set (given a multi). |
| 7402 |
* |
| 7403 |
* This is a no-op, but the call to FreezeMultiXactId might have |
| 7404 |
* ratcheted back NewRelfrozenXid and/or NewRelminMxid trackers |
| 7405 |
* for us (the "freeze page" variants, specifically). That'll |
| 7406 |
* make it safe for our caller to freeze the page later on, while |
| 7407 |
* leaving this particular xmax undisturbed. |
| 7408 |
* |
| 7409 |
* FreezeMultiXactId is _not_ responsible for the "no freeze" |
| 7410 |
* NewRelfrozenXid/NewRelminMxid trackers, though -- that's our |
| 7411 |
* job. A call to heap_tuple_should_freeze for this same tuple |
| 7412 |
* will take place below if 'freeze_required' isn't set already. |
| 7413 |
* (This repeats work from FreezeMultiXactId, but allows "no |
| 7414 |
* freeze" tracker maintenance to happen in only one place.) |
| 7415 |
*/ |
| 7416 |
Assert(!MultiXactIdPrecedes(newxmax, cutoffs->MultiXactCutoff)); |
| 7417 |
Assert(MultiXactIdIsValid(newxmax) && xid == newxmax); |
| 7418 |
} |
| 7419 |
else if (flags & FRM_RETURN_IS_XID) |
| 7420 |
{ |
| 7421 |
/* |
| 7422 |
* xmax will become an updater Xid (original MultiXact's updater |
| 7423 |
* member Xid will be carried forward as a simple Xid in Xmax). |
| 7424 |
*/ |
| 7425 |
Assert(!TransactionIdPrecedes(newxmax, cutoffs->OldestXmin)); |
| 7426 |
|
| 7427 |
/* |
| 7428 |
* NB -- some of these transformations are only valid because we |
| 7429 |
* know the return Xid is a tuple updater (i.e. not merely a |
| 7430 |
* locker.) Also note that the only reason we don't explicitly |
| 7431 |
* worry about HEAP_KEYS_UPDATED is because it lives in |
| 7432 |
* t_infomask2 rather than t_infomask. |
| 7433 |
*/ |
| 7434 |
frz->t_infomask &= ~HEAP_XMAX_BITS; |
| 7435 |
frz->xmax = newxmax; |
| 7436 |
if (flags & FRM_MARK_COMMITTED) |
| 7437 |
frz->t_infomask |= HEAP_XMAX_COMMITTED; |
| 7438 |
replace_xmax = true; |
| 7439 |
} |
| 7440 |
else if (flags & FRM_RETURN_IS_MULTI) |
| 7441 |
{ |
| 7442 |
uint16 newbits; |
| 7443 |
uint16 newbits2; |
| 7444 |
|
| 7445 |
/* |
| 7446 |
* xmax is an old MultiXactId that we have to replace with a new |
| 7447 |
* MultiXactId, to carry forward two or more original member XIDs. |
| 7448 |
*/ |
| 7449 |
Assert(!MultiXactIdPrecedes(newxmax, cutoffs->OldestMxact)); |
| 7450 |
|
| 7451 |
/* |
| 7452 |
* We can't use GetMultiXactIdHintBits directly on the new multi |
| 7453 |
* here; that routine initializes the masks to all zeroes, which |
| 7454 |
* would lose other bits we need. Doing it this way ensures all |
| 7455 |
* unrelated bits remain untouched. |
| 7456 |
*/ |
| 7457 |
frz->t_infomask &= ~HEAP_XMAX_BITS; |
| 7458 |
frz->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 7459 |
GetMultiXactIdHintBits(newxmax, &newbits, &newbits2); |
| 7460 |
frz->t_infomask |= newbits; |
| 7461 |
frz->t_infomask2 |= newbits2; |
| 7462 |
frz->xmax = newxmax; |
| 7463 |
replace_xmax = true; |
| 7464 |
} |
| 7465 |
else |
| 7466 |
{ |
| 7467 |
/* |
| 7468 |
* Freeze plan for tuple "freezes xmax" in the strictest sense: |
| 7469 |
* it'll leave nothing in xmax (neither an Xid nor a MultiXactId). |
| 7470 |
*/ |
| 7471 |
Assert(flags & FRM_INVALIDATE_XMAX); |
| 7472 |
Assert(!TransactionIdIsValid(newxmax)); |
| 7473 |
|
| 7474 |
/* Will set freeze_xmax flags in freeze plan below */ |
| 7475 |
freeze_xmax = true; |
| 7476 |
} |
| 7477 |
|
| 7478 |
/* MultiXactId processing forces freezing (barring FRM_NOOP case) */ |
| 7479 |
Assert(pagefrz->freeze_required || (!freeze_xmax && !replace_xmax)); |
| 7480 |
} |
| 7481 |
else if (TransactionIdIsNormal(xid)) |
| 7482 |
{ |
| 7483 |
/* Raw xmax is normal XID */ |
| 7484 |
if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid)) |
| 7485 |
ereport(ERROR, |
| 7486 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7487 |
errmsg_internal("found xmax %u from before relfrozenxid %u", |
| 7488 |
xid, cutoffs->relfrozenxid))); |
| 7489 |
|
| 7490 |
/* Will set freeze_xmax flags in freeze plan below */ |
| 7491 |
freeze_xmax = TransactionIdPrecedes(xid, cutoffs->OldestXmin); |
| 7492 |
|
| 7493 |
/* |
| 7494 |
* Verify that xmax aborted if and when freeze plan is executed, |
| 7495 |
* provided it's from an update. (A lock-only xmax can be removed |
| 7496 |
* independent of this, since the lock is released at xact end.) |
| 7497 |
*/ |
| 7498 |
if (freeze_xmax && !HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)) |
| 7499 |
frz->checkflags |= HEAP_FREEZE_CHECK_XMAX_ABORTED; |
| 7500 |
} |
| 7501 |
else if (!TransactionIdIsValid(xid)) |
| 7502 |
{ |
| 7503 |
/* Raw xmax is InvalidTransactionId XID */ |
| 7504 |
Assert((tuple->t_infomask & HEAP_XMAX_IS_MULTI) == 0); |
| 7505 |
xmax_already_frozen = true; |
| 7506 |
} |
| 7507 |
else |
| 7508 |
ereport(ERROR, |
| 7509 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7510 |
errmsg_internal("found raw xmax %u (infomask 0x%04x) not invalid and not multi", |
| 7511 |
xid, tuple->t_infomask))); |
| 7512 |
|
| 7513 |
if (freeze_xmin) |
| 7514 |
{ |
| 7515 |
Assert(!xmin_already_frozen); |
| 7516 |
|
| 7517 |
frz->t_infomask |= HEAP_XMIN_FROZEN; |
| 7518 |
} |
| 7519 |
if (replace_xvac) |
| 7520 |
{ |
| 7521 |
/* |
| 7522 |
* If a MOVED_OFF tuple is not dead, the xvac transaction must have |
| 7523 |
* failed; whereas a non-dead MOVED_IN tuple must mean the xvac |
| 7524 |
* transaction succeeded. |
| 7525 |
*/ |
| 7526 |
Assert(pagefrz->freeze_required); |
| 7527 |
if (tuple->t_infomask & HEAP_MOVED_OFF) |
| 7528 |
frz->frzflags |= XLH_INVALID_XVAC; |
| 7529 |
else |
| 7530 |
frz->frzflags |= XLH_FREEZE_XVAC; |
| 7531 |
} |
| 7532 |
if (replace_xmax) |
| 7533 |
{ |
| 7534 |
Assert(!xmax_already_frozen && !freeze_xmax); |
| 7535 |
Assert(pagefrz->freeze_required); |
| 7536 |
|
| 7537 |
/* Already set replace_xmax flags in freeze plan earlier */ |
| 7538 |
} |
| 7539 |
if (freeze_xmax) |
| 7540 |
{ |
| 7541 |
Assert(!xmax_already_frozen && !replace_xmax); |
| 7542 |
|
| 7543 |
frz->xmax = InvalidTransactionId; |
| 7544 |
|
| 7545 |
/* |
| 7546 |
* The tuple might be marked either XMAX_INVALID or XMAX_COMMITTED + |
| 7547 |
* LOCKED. Normalize to INVALID just to be sure no one gets confused. |
| 7548 |
* Also get rid of the HEAP_KEYS_UPDATED bit. |
| 7549 |
*/ |
| 7550 |
frz->t_infomask &= ~HEAP_XMAX_BITS; |
| 7551 |
frz->t_infomask |= HEAP_XMAX_INVALID; |
| 7552 |
frz->t_infomask2 &= ~HEAP_HOT_UPDATED; |
| 7553 |
frz->t_infomask2 &= ~HEAP_KEYS_UPDATED; |
| 7554 |
} |
| 7555 |
|
| 7556 |
/* |
| 7557 |
* Determine if this tuple is already totally frozen, or will become |
| 7558 |
* totally frozen (provided caller executes freeze plans for the page) |
| 7559 |
*/ |
| 7560 |
*totally_frozen = ((freeze_xmin || xmin_already_frozen) && |
| 7561 |
(freeze_xmax || xmax_already_frozen)); |
| 7562 |
|
| 7563 |
if (!pagefrz->freeze_required && !(xmin_already_frozen && |
| 7564 |
xmax_already_frozen)) |
| 7565 |
{ |
| 7566 |
/* |
| 7567 |
* So far no previous tuple from the page made freezing mandatory. |
| 7568 |
* Does this tuple force caller to freeze the entire page? |
| 7569 |
*/ |
| 7570 |
pagefrz->freeze_required = |
| 7571 |
heap_tuple_should_freeze(tuple, cutoffs, |
| 7572 |
&pagefrz->NoFreezePageRelfrozenXid, |
| 7573 |
&pagefrz->NoFreezePageRelminMxid); |
| 7574 |
} |
| 7575 |
|
| 7576 |
/* Tell caller if this tuple has a usable freeze plan set in *frz */ |
| 7577 |
return freeze_xmin || replace_xvac || replace_xmax || freeze_xmax; |
| 7578 |
} |
| 7579 |
|
| 7580 |
/* |
| 7581 |
* Perform xmin/xmax XID status sanity checks before actually executing freeze |
| 7582 |
* plans. |
| 7583 |
* |
| 7584 |
* heap_prepare_freeze_tuple doesn't perform these checks directly because |
| 7585 |
* pg_xact lookups are relatively expensive. They shouldn't be repeated by |
| 7586 |
* successive VACUUMs that each decide against freezing the same page. |
| 7587 |
*/ |
| 7588 |
void |
| 7589 |
heap_pre_freeze_checks(Buffer buffer, |
| 7590 |
HeapTupleFreeze *tuples, int ntuples) |
| 7591 |
{ |
| 7592 |
Page page = BufferGetPage(buffer); |
| 7593 |
|
| 7594 |
for (int i = 0; i < ntuples; i++) |
| 7595 |
{ |
| 7596 |
HeapTupleFreeze *frz = tuples + i; |
| 7597 |
ItemId itemid = PageGetItemId(page, frz->offset); |
| 7598 |
HeapTupleHeader htup; |
| 7599 |
|
| 7600 |
htup = (HeapTupleHeader) PageGetItem(page, itemid); |
| 7601 |
|
| 7602 |
/* Deliberately avoid relying on tuple hint bits here */ |
| 7603 |
if (frz->checkflags & HEAP_FREEZE_CHECK_XMIN_COMMITTED) |
| 7604 |
{ |
| 7605 |
TransactionId xmin = HeapTupleHeaderGetRawXmin(htup); |
| 7606 |
|
| 7607 |
Assert(!HeapTupleHeaderXminFrozen(htup)); |
| 7608 |
if (unlikely(!TransactionIdDidCommit(xmin))) |
| 7609 |
ereport(ERROR, |
| 7610 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7611 |
errmsg_internal("uncommitted xmin %u needs to be frozen", |
| 7612 |
xmin))); |
| 7613 |
} |
| 7614 |
|
| 7615 |
/* |
| 7616 |
* TransactionIdDidAbort won't work reliably in the presence of XIDs |
| 7617 |
* left behind by transactions that were in progress during a crash, |
| 7618 |
* so we can only check that xmax didn't commit |
| 7619 |
*/ |
| 7620 |
if (frz->checkflags & HEAP_FREEZE_CHECK_XMAX_ABORTED) |
| 7621 |
{ |
| 7622 |
TransactionId xmax = HeapTupleHeaderGetRawXmax(htup); |
| 7623 |
|
| 7624 |
Assert(TransactionIdIsNormal(xmax)); |
| 7625 |
if (unlikely(TransactionIdDidCommit(xmax))) |
| 7626 |
ereport(ERROR, |
| 7627 |
(errcode(ERRCODE_DATA_CORRUPTED), |
| 7628 |
errmsg_internal("cannot freeze committed xmax %u", |
| 7629 |
xmax))); |
| 7630 |
} |
| 7631 |
} |
| 7632 |
} |
| 7633 |
|
| 7634 |
/* |
| 7635 |
* Helper which executes freezing of one or more heap tuples on a page on |
| 7636 |
* behalf of caller. Caller passes an array of tuple plans from |
| 7637 |
* heap_prepare_freeze_tuple. Caller must set 'offset' in each plan for us. |
| 7638 |
* Must be called in a critical section that also marks the buffer dirty and, |
| 7639 |
* if needed, emits WAL. |
| 7640 |
*/ |
| 7641 |
void |
| 7642 |
heap_freeze_prepared_tuples(Buffer buffer, HeapTupleFreeze *tuples, int ntuples) |
| 7643 |
{ |
| 7644 |
Page page = BufferGetPage(buffer); |
| 7645 |
|
| 7646 |
for (int i = 0; i < ntuples; i++) |
| 7647 |
{ |
| 7648 |
HeapTupleFreeze *frz = tuples + i; |
| 7649 |
ItemId itemid = PageGetItemId(page, frz->offset); |
| 7650 |
HeapTupleHeader htup; |
| 7651 |
|
| 7652 |
htup = (HeapTupleHeader) PageGetItem(page, itemid); |
| 7653 |
heap_execute_freeze_tuple(htup, frz); |
| 7654 |
} |
| 7655 |
} |
| 7656 |
|
| 7657 |
/* |
| 7658 |
* heap_freeze_tuple |
| 7659 |
* Freeze tuple in place, without WAL logging. |
| 7660 |
* |
| 7661 |
* Useful for callers like CLUSTER that perform their own WAL logging. |
| 7662 |
*/ |
| 7663 |
bool |
| 7664 |
heap_freeze_tuple(HeapTupleHeader tuple, |
| 7665 |
TransactionId relfrozenxid, TransactionId relminmxid, |
| 7666 |
TransactionId FreezeLimit, TransactionId MultiXactCutoff) |
| 7667 |
{ |
| 7668 |
HeapTupleFreeze frz; |
| 7669 |
bool do_freeze; |
| 7670 |
bool totally_frozen; |
| 7671 |
struct VacuumCutoffs cutoffs; |
| 7672 |
HeapPageFreeze pagefrz; |
| 7673 |
|
| 7674 |
cutoffs.relfrozenxid = relfrozenxid; |
| 7675 |
cutoffs.relminmxid = relminmxid; |
| 7676 |
cutoffs.OldestXmin = FreezeLimit; |
| 7677 |
cutoffs.OldestMxact = MultiXactCutoff; |
| 7678 |
cutoffs.FreezeLimit = FreezeLimit; |
| 7679 |
cutoffs.MultiXactCutoff = MultiXactCutoff; |
| 7680 |
|
| 7681 |
pagefrz.freeze_required = true; |
| 7682 |
pagefrz.FreezePageRelfrozenXid = FreezeLimit; |
| 7683 |
pagefrz.FreezePageRelminMxid = MultiXactCutoff; |
| 7684 |
pagefrz.NoFreezePageRelfrozenXid = FreezeLimit; |
| 7685 |
pagefrz.NoFreezePageRelminMxid = MultiXactCutoff; |
| 7686 |
|
| 7687 |
do_freeze = heap_prepare_freeze_tuple(tuple, &cutoffs, |
| 7688 |
&pagefrz, &frz, &totally_frozen); |
| 7689 |
|
| 7690 |
/* |
| 7691 |
* Note that because this is not a WAL-logged operation, we don't need to |
| 7692 |
* fill in the offset in the freeze record. |
| 7693 |
*/ |
| 7694 |
|
| 7695 |
if (do_freeze) |
| 7696 |
heap_execute_freeze_tuple(tuple, &frz); |
| 7697 |
return do_freeze; |
| 7698 |
} |
| 7699 |
|
| 7700 |
/* |
| 7701 |
* For a given MultiXactId, return the hint bits that should be set in the |
| 7702 |
* tuple's infomask. |
| 7703 |
* |
| 7704 |
* Normally this should be called for a multixact that was just created, and |
| 7705 |
* so is on our local cache, so the GetMembers call is fast. |
| 7706 |
*/ |
| 7707 |
static void |
| 7708 |
GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask, |
| 7709 |
uint16 *new_infomask2) |
| 7710 |
{ |
| 7711 |
int nmembers; |
| 7712 |
MultiXactMember *members; |
| 7713 |
int i; |
| 7714 |
uint16 bits = HEAP_XMAX_IS_MULTI; |
| 7715 |
uint16 bits2 = 0; |
| 7716 |
bool has_update = false; |
| 7717 |
LockTupleMode strongest = LockTupleKeyShare; |
| 7718 |
|
| 7719 |
/* |
| 7720 |
* We only use this in multis we just created, so they cannot be values |
| 7721 |
* pre-pg_upgrade. |
| 7722 |
*/ |
| 7723 |
nmembers = GetMultiXactIdMembers(multi, &members, false, false); |
| 7724 |
|
| 7725 |
for (i = 0; i < nmembers; i++) |
| 7726 |
{ |
| 7727 |
LockTupleMode mode; |
| 7728 |
|
| 7729 |
/* |
| 7730 |
* Remember the strongest lock mode held by any member of the |
| 7731 |
* multixact. |
| 7732 |
*/ |
| 7733 |
mode = TUPLOCK_from_mxstatus(members[i].status); |
| 7734 |
if (mode > strongest) |
| 7735 |
strongest = mode; |
| 7736 |
|
| 7737 |
/* See what other bits we need */ |
| 7738 |
switch (members[i].status) |
| 7739 |
{ |
| 7740 |
case MultiXactStatusForKeyShare: |
| 7741 |
case MultiXactStatusForShare: |
| 7742 |
case MultiXactStatusForNoKeyUpdate: |
| 7743 |
break; |
| 7744 |
|
| 7745 |
case MultiXactStatusForUpdate: |
| 7746 |
bits2 |= HEAP_KEYS_UPDATED; |
| 7747 |
break; |
| 7748 |
|
| 7749 |
case MultiXactStatusNoKeyUpdate: |
| 7750 |
has_update = true; |
| 7751 |
break; |
| 7752 |
|
| 7753 |
case MultiXactStatusUpdate: |
| 7754 |
bits2 |= HEAP_KEYS_UPDATED; |
| 7755 |
has_update = true; |
| 7756 |
break; |
| 7757 |
} |
| 7758 |
} |
| 7759 |
|
| 7760 |
if (strongest == LockTupleExclusive || |
| 7761 |
strongest == LockTupleNoKeyExclusive) |
| 7762 |
bits |= HEAP_XMAX_EXCL_LOCK; |
| 7763 |
else if (strongest == LockTupleShare) |
| 7764 |
bits |= HEAP_XMAX_SHR_LOCK; |
| 7765 |
else if (strongest == LockTupleKeyShare) |
| 7766 |
bits |= HEAP_XMAX_KEYSHR_LOCK; |
| 7767 |
|
| 7768 |
if (!has_update) |
| 7769 |
bits |= HEAP_XMAX_LOCK_ONLY; |
| 7770 |
|
| 7771 |
if (nmembers > 0) |
| 7772 |
pfree(members); |
| 7773 |
|
| 7774 |
*new_infomask = bits; |
| 7775 |
*new_infomask2 = bits2; |
| 7776 |
} |
| 7777 |
|
| 7778 |
/* |
| 7779 |
* MultiXactIdGetUpdateXid |
| 7780 |
* |
| 7781 |
* Given a multixact Xmax and corresponding infomask, which does not have the |
| 7782 |
* HEAP_XMAX_LOCK_ONLY bit set, obtain and return the Xid of the updating |
| 7783 |
* transaction. |
| 7784 |
* |
| 7785 |
* Caller is expected to check the status of the updating transaction, if |
| 7786 |
* necessary. |
| 7787 |
*/ |
| 7788 |
static TransactionId |
| 7789 |
MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask) |
| 7790 |
{ |
| 7791 |
TransactionId update_xact = InvalidTransactionId; |
| 7792 |
MultiXactMember *members; |
| 7793 |
int nmembers; |
| 7794 |
|
| 7795 |
Assert(!(t_infomask & HEAP_XMAX_LOCK_ONLY)); |
| 7796 |
Assert(t_infomask & HEAP_XMAX_IS_MULTI); |
| 7797 |
|
| 7798 |
/* |
| 7799 |
* Since we know the LOCK_ONLY bit is not set, this cannot be a multi from |
| 7800 |
* pre-pg_upgrade. |
| 7801 |
*/ |
| 7802 |
nmembers = GetMultiXactIdMembers(xmax, &members, false, false); |
| 7803 |
|
| 7804 |
if (nmembers > 0) |
| 7805 |
{ |
| 7806 |
int i; |
| 7807 |
|
| 7808 |
for (i = 0; i < nmembers; i++) |
| 7809 |
{ |
| 7810 |
/* Ignore lockers */ |
| 7811 |
if (!ISUPDATE_from_mxstatus(members[i].status)) |
| 7812 |
continue; |
| 7813 |
|
| 7814 |
/* there can be at most one updater */ |
| 7815 |
Assert(update_xact == InvalidTransactionId); |
| 7816 |
update_xact = members[i].xid; |
| 7817 |
#ifndef USE_ASSERT_CHECKING |
| 7818 |
|
| 7819 |
/* |
| 7820 |
* in an assert-enabled build, walk the whole array to ensure |
| 7821 |
* there's no other updater. |
| 7822 |
*/ |
| 7823 |
break; |
| 7824 |
#endif |
| 7825 |
} |
| 7826 |
|
| 7827 |
pfree(members); |
| 7828 |
} |
| 7829 |
|
| 7830 |
return update_xact; |
| 7831 |
} |
| 7832 |
|
| 7833 |
/* |
| 7834 |
* HeapTupleGetUpdateXid |
| 7835 |
* As above, but use a HeapTupleHeader |
| 7836 |
* |
| 7837 |
* See also HeapTupleHeaderGetUpdateXid, which can be used without previously |
| 7838 |
* checking the hint bits. |
| 7839 |
*/ |
| 7840 |
TransactionId |
| 7841 |
HeapTupleGetUpdateXid(const HeapTupleHeaderData *tup) |
| 7842 |
{ |
| 7843 |
return MultiXactIdGetUpdateXid(HeapTupleHeaderGetRawXmax(tup), |
| 7844 |
tup->t_infomask); |
| 7845 |
} |
| 7846 |
|
| 7847 |
/* |
| 7848 |
* Does the given multixact conflict with the current transaction grabbing a |
| 7849 |
* tuple lock of the given strength? |
| 7850 |
* |
| 7851 |
* The passed infomask pairs up with the given multixact in the tuple header. |
| 7852 |
* |
| 7853 |
* If current_is_member is not NULL, it is set to 'true' if the current |
| 7854 |
* transaction is a member of the given multixact. |
| 7855 |
*/ |
| 7856 |
static bool |
| 7857 |
DoesMultiXactIdConflict(MultiXactId multi, uint16 infomask, |
| 7858 |
LockTupleMode lockmode, bool *current_is_member) |
| 7859 |
{ |
| 7860 |
int nmembers; |
| 7861 |
MultiXactMember *members; |
| 7862 |
bool result = false; |
| 7863 |
LOCKMODE wanted = tupleLockExtraInfo[lockmode].hwlock; |
| 7864 |
|
| 7865 |
if (HEAP_LOCKED_UPGRADED(infomask)) |
| 7866 |
return false; |
| 7867 |
|
| 7868 |
nmembers = GetMultiXactIdMembers(multi, &members, false, |
| 7869 |
HEAP_XMAX_IS_LOCKED_ONLY(infomask)); |
| 7870 |
if (nmembers >= 0) |
| 7871 |
{ |
| 7872 |
int i; |
| 7873 |
|
| 7874 |
for (i = 0; i < nmembers; i++) |
| 7875 |
{ |
| 7876 |
TransactionId memxid; |
| 7877 |
LOCKMODE memlockmode; |
| 7878 |
|
| 7879 |
if (result && (current_is_member == NULL || *current_is_member)) |
| 7880 |
break; |
| 7881 |
|
| 7882 |
memlockmode = LOCKMODE_from_mxstatus(members[i].status); |
| 7883 |
|
| 7884 |
/* ignore members from current xact (but track their presence) */ |
| 7885 |
memxid = members[i].xid; |
| 7886 |
if (TransactionIdIsCurrentTransactionId(memxid)) |
| 7887 |
{ |
| 7888 |
if (current_is_member != NULL) |
| 7889 |
*current_is_member = true; |
| 7890 |
continue; |
| 7891 |
} |
| 7892 |
else if (result) |
| 7893 |
continue; |
| 7894 |
|
| 7895 |
/* ignore members that don't conflict with the lock we want */ |
| 7896 |
if (!DoLockModesConflict(memlockmode, wanted)) |
| 7897 |
continue; |
| 7898 |
|
| 7899 |
if (ISUPDATE_from_mxstatus(members[i].status)) |
| 7900 |
{ |
| 7901 |
/* ignore aborted updaters */ |
| 7902 |
if (TransactionIdDidAbort(memxid)) |
| 7903 |
continue; |
| 7904 |
} |
| 7905 |
else |
| 7906 |
{ |
| 7907 |
/* ignore lockers-only that are no longer in progress */ |
| 7908 |
if (!TransactionIdIsInProgress(memxid)) |
| 7909 |
continue; |
| 7910 |
} |
| 7911 |
|
| 7912 |
/* |
| 7913 |
* Whatever remains are either live lockers that conflict with our |
| 7914 |
* wanted lock, and updaters that are not aborted. Those conflict |
| 7915 |
* with what we want. Set up to return true, but keep going to |
| 7916 |
* look for the current transaction among the multixact members, |
| 7917 |
* if needed. |
| 7918 |
*/ |
| 7919 |
result = true; |
| 7920 |
} |
| 7921 |
pfree(members); |
| 7922 |
} |
| 7923 |
|
| 7924 |
return result; |
| 7925 |
} |
| 7926 |
|
| 7927 |
/* |
| 7928 |
* Do_MultiXactIdWait |
| 7929 |
* Actual implementation for the two functions below. |
| 7930 |
* |
| 7931 |
* 'multi', 'status' and 'infomask' indicate what to sleep on (the status is |
| 7932 |
* needed to ensure we only sleep on conflicting members, and the infomask is |
| 7933 |
* used to optimize multixact access in case it's a lock-only multi); 'nowait' |
| 7934 |
* indicates whether to use conditional lock acquisition, to allow callers to |
| 7935 |
* fail if lock is unavailable. 'rel', 'ctid' and 'oper' are used to set up |
| 7936 |
* context information for error messages. 'remaining', if not NULL, receives |
| 7937 |
* the number of members that are still running, including any (non-aborted) |
| 7938 |
* subtransactions of our own transaction. 'logLockFailure' indicates whether |
| 7939 |
* to log details when a lock acquisition fails with 'nowait' enabled. |
| 7940 |
* |
| 7941 |
* We do this by sleeping on each member using XactLockTableWait. Any |
| 7942 |
* members that belong to the current backend are *not* waited for, however; |
| 7943 |
* this would not merely be useless but would lead to Assert failure inside |
| 7944 |
* XactLockTableWait. By the time this returns, it is certain that all |
| 7945 |
* transactions *of other backends* that were members of the MultiXactId |
| 7946 |
* that conflict with the requested status are dead (and no new ones can have |
| 7947 |
* been added, since it is not legal to add members to an existing |
| 7948 |
* MultiXactId). |
| 7949 |
* |
| 7950 |
* But by the time we finish sleeping, someone else may have changed the Xmax |
| 7951 |
* of the containing tuple, so the caller needs to iterate on us somehow. |
| 7952 |
* |
| 7953 |
* Note that in case we return false, the number of remaining members is |
| 7954 |
* not to be trusted. |
| 7955 |
*/ |
| 7956 |
static bool |
| 7957 |
Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status, |
| 7958 |
uint16 infomask, bool nowait, |
| 7959 |
Relation rel, ItemPointer ctid, XLTW_Oper oper, |
| 7960 |
int *remaining, bool logLockFailure) |
| 7961 |
{ |
| 7962 |
bool result = true; |
| 7963 |
MultiXactMember *members; |
| 7964 |
int nmembers; |
| 7965 |
int remain = 0; |
| 7966 |
|
| 7967 |
/* for pre-pg_upgrade tuples, no need to sleep at all */ |
| 7968 |
nmembers = HEAP_LOCKED_UPGRADED(infomask) ? -1 : |
| 7969 |
GetMultiXactIdMembers(multi, &members, false, |
| 7970 |
HEAP_XMAX_IS_LOCKED_ONLY(infomask)); |
| 7971 |
|
| 7972 |
if (nmembers >= 0) |
| 7973 |
{ |
| 7974 |
int i; |
| 7975 |
|
| 7976 |
for (i = 0; i < nmembers; i++) |
| 7977 |
{ |
| 7978 |
TransactionId memxid = members[i].xid; |
| 7979 |
MultiXactStatus memstatus = members[i].status; |
| 7980 |
|
| 7981 |
if (TransactionIdIsCurrentTransactionId(memxid)) |
| 7982 |
{ |
| 7983 |
remain++; |
| 7984 |
continue; |
| 7985 |
} |
| 7986 |
|
| 7987 |
if (!DoLockModesConflict(LOCKMODE_from_mxstatus(memstatus), |
| 7988 |
LOCKMODE_from_mxstatus(status))) |
| 7989 |
{ |
| 7990 |
if (remaining && TransactionIdIsInProgress(memxid)) |
| 7991 |
remain++; |
| 7992 |
continue; |
| 7993 |
} |
| 7994 |
|
| 7995 |
/* |
| 7996 |
* This member conflicts with our multi, so we have to sleep (or |
| 7997 |
* return failure, if asked to avoid waiting.) |
| 7998 |
* |
| 7999 |
* Note that we don't set up an error context callback ourselves, |
| 8000 |
* but instead we pass the info down to XactLockTableWait. This |
| 8001 |
* might seem a bit wasteful because the context is set up and |
| 8002 |
* tore down for each member of the multixact, but in reality it |
| 8003 |
* should be barely noticeable, and it avoids duplicate code. |
| 8004 |
*/ |
| 8005 |
if (nowait) |
| 8006 |
{ |
| 8007 |
result = ConditionalXactLockTableWait(memxid, logLockFailure); |
| 8008 |
if (!result) |
| 8009 |
break; |
| 8010 |
} |
| 8011 |
else |
| 8012 |
XactLockTableWait(memxid, rel, ctid, oper); |
| 8013 |
} |
| 8014 |
|
| 8015 |
pfree(members); |
| 8016 |
} |
| 8017 |
|
| 8018 |
if (remaining) |
| 8019 |
*remaining = remain; |
| 8020 |
|
| 8021 |
return result; |
| 8022 |
} |
| 8023 |
|
| 8024 |
/* |
| 8025 |
* MultiXactIdWait |
| 8026 |
* Sleep on a MultiXactId. |
| 8027 |
* |
| 8028 |
* By the time we finish sleeping, someone else may have changed the Xmax |
| 8029 |
* of the containing tuple, so the caller needs to iterate on us somehow. |
| 8030 |
* |
| 8031 |
* We return (in *remaining, if not NULL) the number of members that are still |
| 8032 |
* running, including any (non-aborted) subtransactions of our own transaction. |
| 8033 |
*/ |
| 8034 |
static void |
| 8035 |
MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, |
| 8036 |
Relation rel, ItemPointer ctid, XLTW_Oper oper, |
| 8037 |
int *remaining) |
| 8038 |
{ |
| 8039 |
(void) Do_MultiXactIdWait(multi, status, infomask, false, |
| 8040 |
rel, ctid, oper, remaining, false); |
| 8041 |
} |
| 8042 |
|
| 8043 |
/* |
| 8044 |
* ConditionalMultiXactIdWait |
| 8045 |
* As above, but only lock if we can get the lock without blocking. |
| 8046 |
* |
| 8047 |
* By the time we finish sleeping, someone else may have changed the Xmax |
| 8048 |
* of the containing tuple, so the caller needs to iterate on us somehow. |
| 8049 |
* |
| 8050 |
* If the multixact is now all gone, return true. Returns false if some |
| 8051 |
* transactions might still be running. |
| 8052 |
* |
| 8053 |
* We return (in *remaining, if not NULL) the number of members that are still |
| 8054 |
* running, including any (non-aborted) subtransactions of our own transaction. |
| 8055 |
*/ |
| 8056 |
static bool |
| 8057 |
ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status, |
| 8058 |
uint16 infomask, Relation rel, int *remaining, |
| 8059 |
bool logLockFailure) |
| 8060 |
{ |
| 8061 |
return Do_MultiXactIdWait(multi, status, infomask, true, |
| 8062 |
rel, NULL, XLTW_None, remaining, logLockFailure); |
| 8063 |
} |
| 8064 |
|
| 8065 |
/* |
| 8066 |
* heap_tuple_needs_eventual_freeze |
| 8067 |
* |
| 8068 |
* Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac) |
| 8069 |
* will eventually require freezing (if tuple isn't removed by pruning first). |
| 8070 |
*/ |
| 8071 |
bool |
| 8072 |
heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple) |
| 8073 |
{ |
| 8074 |
TransactionId xid; |
| 8075 |
|
| 8076 |
/* |
| 8077 |
* If xmin is a normal transaction ID, this tuple is definitely not |
| 8078 |
* frozen. |
| 8079 |
*/ |
| 8080 |
xid = HeapTupleHeaderGetXmin(tuple); |
| 8081 |
if (TransactionIdIsNormal(xid)) |
| 8082 |
return true; |
| 8083 |
|
| 8084 |
/* |
| 8085 |
* If xmax is a valid xact or multixact, this tuple is also not frozen. |
| 8086 |
*/ |
| 8087 |
if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) |
| 8088 |
{ |
| 8089 |
MultiXactId multi; |
| 8090 |
|
| 8091 |
multi = HeapTupleHeaderGetRawXmax(tuple); |
| 8092 |
if (MultiXactIdIsValid(multi)) |
| 8093 |
return true; |
| 8094 |
} |
| 8095 |
else |
| 8096 |
{ |
| 8097 |
xid = HeapTupleHeaderGetRawXmax(tuple); |
| 8098 |
if (TransactionIdIsNormal(xid)) |
| 8099 |
return true; |
| 8100 |
} |
| 8101 |
|
| 8102 |
if (tuple->t_infomask & HEAP_MOVED) |
| 8103 |
{ |
| 8104 |
xid = HeapTupleHeaderGetXvac(tuple); |
| 8105 |
if (TransactionIdIsNormal(xid)) |
| 8106 |
return true; |
| 8107 |
} |
| 8108 |
|
| 8109 |
return false; |
| 8110 |
} |
| 8111 |
|
| 8112 |
/* |
| 8113 |
* heap_tuple_should_freeze |
| 8114 |
* |
| 8115 |
* Return value indicates if heap_prepare_freeze_tuple sibling function would |
| 8116 |
* (or should) force freezing of the heap page that contains caller's tuple. |
| 8117 |
* Tuple header XIDs/MXIDs < FreezeLimit/MultiXactCutoff trigger freezing. |
| 8118 |
* This includes (xmin, xmax, xvac) fields, as well as MultiXact member XIDs. |
| 8119 |
* |
| 8120 |
* The *NoFreezePageRelfrozenXid and *NoFreezePageRelminMxid input/output |
| 8121 |
* arguments help VACUUM track the oldest extant XID/MXID remaining in rel. |
| 8122 |
* Our working assumption is that caller won't decide to freeze this tuple. |
| 8123 |
* It's up to caller to only ratchet back its own top-level trackers after the |
| 8124 |
* point that it fully commits to not freezing the tuple/page in question. |
| 8125 |
*/ |
| 8126 |
bool |
| 8127 |
heap_tuple_should_freeze(HeapTupleHeader tuple, |
| 8128 |
const struct VacuumCutoffs *cutoffs, |
| 8129 |
TransactionId *NoFreezePageRelfrozenXid, |
| 8130 |
MultiXactId *NoFreezePageRelminMxid) |
| 8131 |
{ |
| 8132 |
TransactionId xid; |
| 8133 |
MultiXactId multi; |
| 8134 |
bool freeze = false; |
| 8135 |
|
| 8136 |
/* First deal with xmin */ |
| 8137 |
xid = HeapTupleHeaderGetXmin(tuple); |
| 8138 |
if (TransactionIdIsNormal(xid)) |
| 8139 |
{ |
| 8140 |
Assert(TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid, xid)); |
| 8141 |
if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid)) |
| 8142 |
*NoFreezePageRelfrozenXid = xid; |
| 8143 |
if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit)) |
| 8144 |
freeze = true; |
| 8145 |
} |
| 8146 |
|
| 8147 |
/* Now deal with xmax */ |
| 8148 |
xid = InvalidTransactionId; |
| 8149 |
multi = InvalidMultiXactId; |
| 8150 |
if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) |
| 8151 |
multi = HeapTupleHeaderGetRawXmax(tuple); |
| 8152 |
else |
| 8153 |
xid = HeapTupleHeaderGetRawXmax(tuple); |
| 8154 |
|
| 8155 |
if (TransactionIdIsNormal(xid)) |
| 8156 |
{ |
| 8157 |
Assert(TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid, xid)); |
| 8158 |
/* xmax is a non-permanent XID */ |
| 8159 |
if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid)) |
| 8160 |
*NoFreezePageRelfrozenXid = xid; |
| 8161 |
if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit)) |
| 8162 |
freeze = true; |
| 8163 |
} |
| 8164 |
else if (!MultiXactIdIsValid(multi)) |
| 8165 |
{ |
| 8166 |
/* xmax is a permanent XID or invalid MultiXactId/XID */ |
| 8167 |
} |
| 8168 |
else if (HEAP_LOCKED_UPGRADED(tuple->t_infomask)) |
| 8169 |
{ |
| 8170 |
/* xmax is a pg_upgrade'd MultiXact, which can't have updater XID */ |
| 8171 |
if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid)) |
| 8172 |
*NoFreezePageRelminMxid = multi; |
| 8173 |
/* heap_prepare_freeze_tuple always freezes pg_upgrade'd xmax */ |
| 8174 |
freeze = true; |
| 8175 |
} |
| 8176 |
else |
| 8177 |
{ |
| 8178 |
/* xmax is a MultiXactId that may have an updater XID */ |
| 8179 |
MultiXactMember *members; |
| 8180 |
int nmembers; |
| 8181 |
|
| 8182 |
Assert(MultiXactIdPrecedesOrEquals(cutoffs->relminmxid, multi)); |
| 8183 |
if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid)) |
| 8184 |
*NoFreezePageRelminMxid = multi; |
| 8185 |
if (MultiXactIdPrecedes(multi, cutoffs->MultiXactCutoff)) |
| 8186 |
freeze = true; |
| 8187 |
|
| 8188 |
/* need to check whether any member of the mxact is old */ |
| 8189 |
nmembers = GetMultiXactIdMembers(multi, &members, false, |
| 8190 |
HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)); |
| 8191 |
|
| 8192 |
for (int i = 0; i < nmembers; i++) |
| 8193 |
{ |
| 8194 |
xid = members[i].xid; |
| 8195 |
Assert(TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid, xid)); |
| 8196 |
if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid)) |
| 8197 |
*NoFreezePageRelfrozenXid = xid; |
| 8198 |
if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit)) |
| 8199 |
freeze = true; |
| 8200 |
} |
| 8201 |
if (nmembers > 0) |
| 8202 |
pfree(members); |
| 8203 |
} |
| 8204 |
|
| 8205 |
if (tuple->t_infomask & HEAP_MOVED) |
| 8206 |
{ |
| 8207 |
xid = HeapTupleHeaderGetXvac(tuple); |
| 8208 |
if (TransactionIdIsNormal(xid)) |
| 8209 |
{ |
| 8210 |
Assert(TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid, xid)); |
| 8211 |
if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid)) |
| 8212 |
*NoFreezePageRelfrozenXid = xid; |
| 8213 |
/* heap_prepare_freeze_tuple forces xvac freezing */ |
| 8214 |
freeze = true; |
| 8215 |
} |
| 8216 |
} |
| 8217 |
|
| 8218 |
return freeze; |
| 8219 |
} |
| 8220 |
|
| 8221 |
/* |
| 8222 |
* Maintain snapshotConflictHorizon for caller by ratcheting forward its value |
| 8223 |
* using any committed XIDs contained in 'tuple', an obsolescent heap tuple |
| 8224 |
* that caller is in the process of physically removing, e.g. via HOT pruning |
| 8225 |
* or index deletion. |
| 8226 |
* |
| 8227 |
* Caller must initialize its value to InvalidTransactionId, which is |
| 8228 |
* generally interpreted as "definitely no need for a recovery conflict". |
| 8229 |
* Final value must reflect all heap tuples that caller will physically remove |
| 8230 |
* (or remove TID references to) via its ongoing pruning/deletion operation. |
| 8231 |
* ResolveRecoveryConflictWithSnapshot() is passed the final value (taken from |
| 8232 |
* caller's WAL record) by REDO routine when it replays caller's operation. |
| 8233 |
*/ |
| 8234 |
void |
| 8235 |
HeapTupleHeaderAdvanceConflictHorizon(HeapTupleHeader tuple, |
| 8236 |
TransactionId *snapshotConflictHorizon) |
| 8237 |
{ |
| 8238 |
TransactionId xmin = HeapTupleHeaderGetXmin(tuple); |
| 8239 |
TransactionId xmax = HeapTupleHeaderGetUpdateXid(tuple); |
| 8240 |
TransactionId xvac = HeapTupleHeaderGetXvac(tuple); |
| 8241 |
|
| 8242 |
if (tuple->t_infomask & HEAP_MOVED) |
| 8243 |
{ |
| 8244 |
if (TransactionIdPrecedes(*snapshotConflictHorizon, xvac)) |
| 8245 |
*snapshotConflictHorizon = xvac; |
| 8246 |
} |
| 8247 |
|
| 8248 |
/* |
| 8249 |
* Ignore tuples inserted by an aborted transaction or if the tuple was |
| 8250 |
* updated/deleted by the inserting transaction. |
| 8251 |
* |
| 8252 |
* Look for a committed hint bit, or if no xmin bit is set, check clog. |
| 8253 |
*/ |
| 8254 |
if (HeapTupleHeaderXminCommitted(tuple) || |
| 8255 |
(!HeapTupleHeaderXminInvalid(tuple) && TransactionIdDidCommit(xmin))) |
| 8256 |
{ |
| 8257 |
if (xmax != xmin && |
| 8258 |
TransactionIdFollows(xmax, *snapshotConflictHorizon)) |
| 8259 |
*snapshotConflictHorizon = xmax; |
| 8260 |
} |
| 8261 |
} |
| 8262 |
|
| 8263 |
#ifdef USE_PREFETCH |
| 8264 |
/* |
| 8265 |
* Helper function for heap_index_delete_tuples. Issues prefetch requests for |
| 8266 |
* prefetch_count buffers. The prefetch_state keeps track of all the buffers |
| 8267 |
* we can prefetch, and which have already been prefetched; each call to this |
| 8268 |
* function picks up where the previous call left off. |
| 8269 |
* |
| 8270 |
* Note: we expect the deltids array to be sorted in an order that groups TIDs |
| 8271 |
* by heap block, with all TIDs for each block appearing together in exactly |
| 8272 |
* one group. |
| 8273 |
*/ |
| 8274 |
static void |
| 8275 |
index_delete_prefetch_buffer(Relation rel, |
| 8276 |
IndexDeletePrefetchState *prefetch_state, |
| 8277 |
int prefetch_count) |
| 8278 |
{ |
| 8279 |
BlockNumber cur_hblkno = prefetch_state->cur_hblkno; |
| 8280 |
int count = 0; |
| 8281 |
int i; |
| 8282 |
int ndeltids = prefetch_state->ndeltids; |
| 8283 |
TM_IndexDelete *deltids = prefetch_state->deltids; |
| 8284 |
|
| 8285 |
for (i = prefetch_state->next_item; |
| 8286 |
i < ndeltids && count < prefetch_count; |
| 8287 |
i++) |
| 8288 |
{ |
| 8289 |
ItemPointer htid = &deltids[i].tid; |
| 8290 |
|
| 8291 |
if (cur_hblkno == InvalidBlockNumber || |
| 8292 |
ItemPointerGetBlockNumber(htid) != cur_hblkno) |
| 8293 |
{ |
| 8294 |
cur_hblkno = ItemPointerGetBlockNumber(htid); |
| 8295 |
PrefetchBuffer(rel, MAIN_FORKNUM, cur_hblkno); |
| 8296 |
count++; |
| 8297 |
} |
| 8298 |
} |
| 8299 |
|
| 8300 |
/* |
| 8301 |
* Save the prefetch position so that next time we can continue from that |
| 8302 |
* position. |
| 8303 |
*/ |
| 8304 |
prefetch_state->next_item = i; |
| 8305 |
prefetch_state->cur_hblkno = cur_hblkno; |
| 8306 |
} |
| 8307 |
#endif |
| 8308 |
|
| 8309 |
/* |
| 8310 |
* Helper function for heap_index_delete_tuples. Checks for index corruption |
| 8311 |
* involving an invalid TID in index AM caller's index page. |
| 8312 |
* |
| 8313 |
* This is an ideal place for these checks. The index AM must hold a buffer |
| 8314 |
* lock on the index page containing the TIDs we examine here, so we don't |
| 8315 |
* have to worry about concurrent VACUUMs at all. We can be sure that the |
| 8316 |
* index is corrupt when htid points directly to an LP_UNUSED item or |
| 8317 |
* heap-only tuple, which is not the case during standard index scans. |
| 8318 |
*/ |
| 8319 |
static inline void |
| 8320 |
index_delete_check_htid(TM_IndexDeleteOp *delstate, |
| 8321 |
Page page, OffsetNumber maxoff, |
| 8322 |
ItemPointer htid, TM_IndexStatus *istatus) |
| 8323 |
{ |
| 8324 |
OffsetNumber indexpagehoffnum = ItemPointerGetOffsetNumber(htid); |
| 8325 |
ItemId iid; |
| 8326 |
|
| 8327 |
Assert(OffsetNumberIsValid(istatus->idxoffnum)); |
| 8328 |
|
| 8329 |
if (unlikely(indexpagehoffnum > maxoff)) |
| 8330 |
ereport(ERROR, |
| 8331 |
(errcode(ERRCODE_INDEX_CORRUPTED), |
| 8332 |
errmsg_internal("heap tid from index tuple (%u,%u) points past end of heap page line pointer array at offset %u of block %u in index \"%s\"", |
| 8333 |
ItemPointerGetBlockNumber(htid), |
| 8334 |
indexpagehoffnum, |
| 8335 |
istatus->idxoffnum, delstate->iblknum, |
| 8336 |
RelationGetRelationName(delstate->irel)))); |
| 8337 |
|
| 8338 |
iid = PageGetItemId(page, indexpagehoffnum); |
| 8339 |
if (unlikely(!ItemIdIsUsed(iid))) |
| 8340 |
ereport(ERROR, |
| 8341 |
(errcode(ERRCODE_INDEX_CORRUPTED), |
| 8342 |
errmsg_internal("heap tid from index tuple (%u,%u) points to unused heap page item at offset %u of block %u in index \"%s\"", |
| 8343 |
ItemPointerGetBlockNumber(htid), |
| 8344 |
indexpagehoffnum, |
| 8345 |
istatus->idxoffnum, delstate->iblknum, |
| 8346 |
RelationGetRelationName(delstate->irel)))); |
| 8347 |
|
| 8348 |
if (ItemIdHasStorage(iid)) |
| 8349 |
{ |
| 8350 |
HeapTupleHeader htup; |
| 8351 |
|
| 8352 |
Assert(ItemIdIsNormal(iid)); |
| 8353 |
htup = (HeapTupleHeader) PageGetItem(page, iid); |
| 8354 |
|
| 8355 |
if (unlikely(HeapTupleHeaderIsHeapOnly(htup))) |
| 8356 |
ereport(ERROR, |
| 8357 |
(errcode(ERRCODE_INDEX_CORRUPTED), |
| 8358 |
errmsg_internal("heap tid from index tuple (%u,%u) points to heap-only tuple at offset %u of block %u in index \"%s\"", |
| 8359 |
ItemPointerGetBlockNumber(htid), |
| 8360 |
indexpagehoffnum, |
| 8361 |
istatus->idxoffnum, delstate->iblknum, |
| 8362 |
RelationGetRelationName(delstate->irel)))); |
| 8363 |
} |
| 8364 |
} |
| 8365 |
|
| 8366 |
/* |
| 8367 |
* heapam implementation of tableam's index_delete_tuples interface. |
| 8368 |
* |
| 8369 |
* This helper function is called by index AMs during index tuple deletion. |
| 8370 |
* See tableam header comments for an explanation of the interface implemented |
| 8371 |
* here and a general theory of operation. Note that each call here is either |
| 8372 |
* a simple index deletion call, or a bottom-up index deletion call. |
| 8373 |
* |
| 8374 |
* It's possible for this to generate a fair amount of I/O, since we may be |
| 8375 |
* deleting hundreds of tuples from a single index block. To amortize that |
| 8376 |
* cost to some degree, this uses prefetching and combines repeat accesses to |
| 8377 |
* the same heap block. |
| 8378 |
*/ |
| 8379 |
TransactionId |
| 8380 |
heap_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate) |
| 8381 |
{ |
| 8382 |
/* Initial assumption is that earlier pruning took care of conflict */ |
| 8383 |
TransactionId snapshotConflictHorizon = InvalidTransactionId; |
| 8384 |
BlockNumber blkno = InvalidBlockNumber; |
| 8385 |
Buffer buf = InvalidBuffer; |
| 8386 |
Page page = NULL; |
| 8387 |
OffsetNumber maxoff = InvalidOffsetNumber; |
| 8388 |
TransactionId priorXmax; |
| 8389 |
#ifdef USE_PREFETCH |
| 8390 |
IndexDeletePrefetchState prefetch_state; |
| 8391 |
int prefetch_distance; |
| 8392 |
#endif |
| 8393 |
SnapshotData SnapshotNonVacuumable; |
| 8394 |
int finalndeltids = 0, |
| 8395 |
nblocksaccessed = 0; |
| 8396 |
|
| 8397 |
/* State that's only used in bottom-up index deletion case */ |
| 8398 |
int nblocksfavorable = 0; |
| 8399 |
int curtargetfreespace = delstate->bottomupfreespace, |
| 8400 |
lastfreespace = 0, |
| 8401 |
actualfreespace = 0; |
| 8402 |
bool bottomup_final_block = false; |
| 8403 |
|
| 8404 |
InitNonVacuumableSnapshot(SnapshotNonVacuumable, GlobalVisTestFor(rel)); |
| 8405 |
|
| 8406 |
/* Sort caller's deltids array by TID for further processing */ |
| 8407 |
index_delete_sort(delstate); |
| 8408 |
|
| 8409 |
/* |
| 8410 |
* Bottom-up case: resort deltids array in an order attuned to where the |
| 8411 |
* greatest number of promising TIDs are to be found, and determine how |
| 8412 |
* many blocks from the start of sorted array should be considered |
| 8413 |
* favorable. This will also shrink the deltids array in order to |
| 8414 |
* eliminate completely unfavorable blocks up front. |
| 8415 |
*/ |
| 8416 |
if (delstate->bottomup) |
| 8417 |
nblocksfavorable = bottomup_sort_and_shrink(delstate); |
| 8418 |
|
| 8419 |
#ifdef USE_PREFETCH |
| 8420 |
/* Initialize prefetch state. */ |
| 8421 |
prefetch_state.cur_hblkno = InvalidBlockNumber; |
| 8422 |
prefetch_state.next_item = 0; |
| 8423 |
prefetch_state.ndeltids = delstate->ndeltids; |
| 8424 |
prefetch_state.deltids = delstate->deltids; |
| 8425 |
|
| 8426 |
/* |
| 8427 |
* Determine the prefetch distance that we will attempt to maintain. |
| 8428 |
* |
| 8429 |
* Since the caller holds a buffer lock somewhere in rel, we'd better make |
| 8430 |
* sure that isn't a catalog relation before we call code that does |
| 8431 |
* syscache lookups, to avoid risk of deadlock. |
| 8432 |
*/ |
| 8433 |
if (IsCatalogRelation(rel)) |
| 8434 |
prefetch_distance = maintenance_io_concurrency; |
| 8435 |
else |
| 8436 |
prefetch_distance = |
| 8437 |
get_tablespace_maintenance_io_concurrency(rel->rd_rel->reltablespace); |
| 8438 |
|
| 8439 |
/* Cap initial prefetch distance for bottom-up deletion caller */ |
| 8440 |
if (delstate->bottomup) |
| 8441 |
{ |
| 8442 |
Assert(nblocksfavorable >= 1); |
| 8443 |
Assert(nblocksfavorable <= BOTTOMUP_MAX_NBLOCKS); |
| 8444 |
prefetch_distance = Min(prefetch_distance, nblocksfavorable); |
| 8445 |
} |
| 8446 |
|
| 8447 |
/* Start prefetching. */ |
| 8448 |
index_delete_prefetch_buffer(rel, &prefetch_state, prefetch_distance); |
| 8449 |
#endif |
| 8450 |
|
| 8451 |
/* Iterate over deltids, determine which to delete, check their horizon */ |
| 8452 |
Assert(delstate->ndeltids > 0); |
| 8453 |
for (int i = 0; i < delstate->ndeltids; i++) |
| 8454 |
{ |
| 8455 |
TM_IndexDelete *ideltid = &delstate->deltids[i]; |
| 8456 |
TM_IndexStatus *istatus = delstate->status + ideltid->id; |
| 8457 |
ItemPointer htid = &ideltid->tid; |
| 8458 |
OffsetNumber offnum; |
| 8459 |
|
| 8460 |
/* |
| 8461 |
* Read buffer, and perform required extra steps each time a new block |
| 8462 |
* is encountered. Avoid refetching if it's the same block as the one |
| 8463 |
* from the last htid. |
| 8464 |
*/ |
| 8465 |
if (blkno == InvalidBlockNumber || |
| 8466 |
ItemPointerGetBlockNumber(htid) != blkno) |
| 8467 |
{ |
| 8468 |
/* |
| 8469 |
* Consider giving up early for bottom-up index deletion caller |
| 8470 |
* first. (Only prefetch next-next block afterwards, when it |
| 8471 |
* becomes clear that we're at least going to access the next |
| 8472 |
* block in line.) |
| 8473 |
* |
| 8474 |
* Sometimes the first block frees so much space for bottom-up |
| 8475 |
* caller that the deletion process can end without accessing any |
| 8476 |
* more blocks. It is usually necessary to access 2 or 3 blocks |
| 8477 |
* per bottom-up deletion operation, though. |
| 8478 |
*/ |
| 8479 |
if (delstate->bottomup) |
| 8480 |
{ |
| 8481 |
/* |
| 8482 |
* We often allow caller to delete a few additional items |
| 8483 |
* whose entries we reached after the point that space target |
| 8484 |
* from caller was satisfied. The cost of accessing the page |
| 8485 |
* was already paid at that point, so it made sense to finish |
| 8486 |
* it off. When that happened, we finalize everything here |
| 8487 |
* (by finishing off the whole bottom-up deletion operation |
| 8488 |
* without needlessly paying the cost of accessing any more |
| 8489 |
* blocks). |
| 8490 |
*/ |
| 8491 |
if (bottomup_final_block) |
| 8492 |
break; |
| 8493 |
|
| 8494 |
/* |
| 8495 |
* Give up when we didn't enable our caller to free any |
| 8496 |
* additional space as a result of processing the page that we |
| 8497 |
* just finished up with. This rule is the main way in which |
| 8498 |
* we keep the cost of bottom-up deletion under control. |
| 8499 |
*/ |
| 8500 |
if (nblocksaccessed >= 1 && actualfreespace == lastfreespace) |
| 8501 |
break; |
| 8502 |
lastfreespace = actualfreespace; /* for next time */ |
| 8503 |
|
| 8504 |
/* |
| 8505 |
* Deletion operation (which is bottom-up) will definitely |
| 8506 |
* access the next block in line. Prepare for that now. |
| 8507 |
* |
| 8508 |
* Decay target free space so that we don't hang on for too |
| 8509 |
* long with a marginal case. (Space target is only truly |
| 8510 |
* helpful when it allows us to recognize that we don't need |
| 8511 |
* to access more than 1 or 2 blocks to satisfy caller due to |
| 8512 |
* agreeable workload characteristics.) |
| 8513 |
* |
| 8514 |
* We are a bit more patient when we encounter contiguous |
| 8515 |
* blocks, though: these are treated as favorable blocks. The |
| 8516 |
* decay process is only applied when the next block in line |
| 8517 |
* is not a favorable/contiguous block. This is not an |
| 8518 |
* exception to the general rule; we still insist on finding |
| 8519 |
* at least one deletable item per block accessed. See |
| 8520 |
* bottomup_nblocksfavorable() for full details of the theory |
| 8521 |
* behind favorable blocks and heap block locality in general. |
| 8522 |
* |
| 8523 |
* Note: The first block in line is always treated as a |
| 8524 |
* favorable block, so the earliest possible point that the |
| 8525 |
* decay can be applied is just before we access the second |
| 8526 |
* block in line. The Assert() verifies this for us. |
| 8527 |
*/ |
| 8528 |
Assert(nblocksaccessed > 0 || nblocksfavorable > 0); |
| 8529 |
if (nblocksfavorable > 0) |
| 8530 |
nblocksfavorable--; |
| 8531 |
else |
| 8532 |
curtargetfreespace /= 2; |
| 8533 |
} |
| 8534 |
|
| 8535 |
/* release old buffer */ |
| 8536 |
if (BufferIsValid(buf)) |
| 8537 |
UnlockReleaseBuffer(buf); |
| 8538 |
|
| 8539 |
blkno = ItemPointerGetBlockNumber(htid); |
| 8540 |
buf = ReadBuffer(rel, blkno); |
| 8541 |
nblocksaccessed++; |
| 8542 |
Assert(!delstate->bottomup || |
| 8543 |
nblocksaccessed <= BOTTOMUP_MAX_NBLOCKS); |
| 8544 |
|
| 8545 |
#ifdef USE_PREFETCH |
| 8546 |
|
| 8547 |
/* |
| 8548 |
* To maintain the prefetch distance, prefetch one more page for |
| 8549 |
* each page we read. |
| 8550 |
*/ |
| 8551 |
index_delete_prefetch_buffer(rel, &prefetch_state, 1); |
| 8552 |
#endif |
| 8553 |
|
| 8554 |
LockBuffer(buf, BUFFER_LOCK_SHARE); |
| 8555 |
|
| 8556 |
page = BufferGetPage(buf); |
| 8557 |
maxoff = PageGetMaxOffsetNumber(page); |
| 8558 |
} |
| 8559 |
|
| 8560 |
/* |
| 8561 |
* In passing, detect index corruption involving an index page with a |
| 8562 |
* TID that points to a location in the heap that couldn't possibly be |
| 8563 |
* correct. We only do this with actual TIDs from caller's index page |
| 8564 |
* (not items reached by traversing through a HOT chain). |
| 8565 |
*/ |
| 8566 |
index_delete_check_htid(delstate, page, maxoff, htid, istatus); |
| 8567 |
|
| 8568 |
if (istatus->knowndeletable) |
| 8569 |
Assert(!delstate->bottomup && !istatus->promising); |
| 8570 |
else |
| 8571 |
{ |
| 8572 |
ItemPointerData tmp = *htid; |
| 8573 |
HeapTupleData heapTuple; |
| 8574 |
|
| 8575 |
/* Are any tuples from this HOT chain non-vacuumable? */ |
| 8576 |
if (heap_hot_search_buffer(&tmp, rel, buf, &SnapshotNonVacuumable, |
| 8577 |
&heapTuple, NULL, true)) |
| 8578 |
continue; /* can't delete entry */ |
| 8579 |
|
| 8580 |
/* Caller will delete, since whole HOT chain is vacuumable */ |
| 8581 |
istatus->knowndeletable = true; |
| 8582 |
|
| 8583 |
/* Maintain index free space info for bottom-up deletion case */ |
| 8584 |
if (delstate->bottomup) |
| 8585 |
{ |
| 8586 |
Assert(istatus->freespace > 0); |
| 8587 |
actualfreespace += istatus->freespace; |
| 8588 |
if (actualfreespace >= curtargetfreespace) |
| 8589 |
bottomup_final_block = true; |
| 8590 |
} |
| 8591 |
} |
| 8592 |
|
| 8593 |
/* |
| 8594 |
* Maintain snapshotConflictHorizon value for deletion operation as a |
| 8595 |
* whole by advancing current value using heap tuple headers. This is |
| 8596 |
* loosely based on the logic for pruning a HOT chain. |
| 8597 |
*/ |
| 8598 |
offnum = ItemPointerGetOffsetNumber(htid); |
| 8599 |
priorXmax = InvalidTransactionId; /* cannot check first XMIN */ |
| 8600 |
for (;;) |
| 8601 |
{ |
| 8602 |
ItemId lp; |
| 8603 |
HeapTupleHeader htup; |
| 8604 |
|
| 8605 |
/* Sanity check (pure paranoia) */ |
| 8606 |
if (offnum < FirstOffsetNumber) |
| 8607 |
break; |
| 8608 |
|
| 8609 |
/* |
| 8610 |
* An offset past the end of page's line pointer array is possible |
| 8611 |
* when the array was truncated |
| 8612 |
*/ |
| 8613 |
if (offnum > maxoff) |
| 8614 |
break; |
| 8615 |
|
| 8616 |
lp = PageGetItemId(page, offnum); |
| 8617 |
if (ItemIdIsRedirected(lp)) |
| 8618 |
{ |
| 8619 |
offnum = ItemIdGetRedirect(lp); |
| 8620 |
continue; |
| 8621 |
} |
| 8622 |
|
| 8623 |
/* |
| 8624 |
* We'll often encounter LP_DEAD line pointers (especially with an |
| 8625 |
* entry marked knowndeletable by our caller up front). No heap |
| 8626 |
* tuple headers get examined for an htid that leads us to an |
| 8627 |
* LP_DEAD item. This is okay because the earlier pruning |
| 8628 |
* operation that made the line pointer LP_DEAD in the first place |
| 8629 |
* must have considered the original tuple header as part of |
| 8630 |
* generating its own snapshotConflictHorizon value. |
| 8631 |
* |
| 8632 |
* Relying on XLOG_HEAP2_PRUNE_VACUUM_SCAN records like this is |
| 8633 |
* the same strategy that index vacuuming uses in all cases. Index |
| 8634 |
* VACUUM WAL records don't even have a snapshotConflictHorizon |
| 8635 |
* field of their own for this reason. |
| 8636 |
*/ |
| 8637 |
if (!ItemIdIsNormal(lp)) |
| 8638 |
break; |
| 8639 |
|
| 8640 |
htup = (HeapTupleHeader) PageGetItem(page, lp); |
| 8641 |
|
| 8642 |
/* |
| 8643 |
* Check the tuple XMIN against prior XMAX, if any |
| 8644 |
*/ |
| 8645 |
if (TransactionIdIsValid(priorXmax) && |
| 8646 |
!TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax)) |
| 8647 |
break; |
| 8648 |
|
| 8649 |
HeapTupleHeaderAdvanceConflictHorizon(htup, |
| 8650 |
&snapshotConflictHorizon); |
| 8651 |
|
| 8652 |
/* |
| 8653 |
* If the tuple is not HOT-updated, then we are at the end of this |
| 8654 |
* HOT-chain. No need to visit later tuples from the same update |
| 8655 |
* chain (they get their own index entries) -- just move on to |
| 8656 |
* next htid from index AM caller. |
| 8657 |
*/ |
| 8658 |
if (!HeapTupleHeaderIsHotUpdated(htup)) |
| 8659 |
break; |
| 8660 |
|
| 8661 |
/* Advance to next HOT chain member */ |
| 8662 |
Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == blkno); |
| 8663 |
offnum = ItemPointerGetOffsetNumber(&htup->t_ctid); |
| 8664 |
priorXmax = HeapTupleHeaderGetUpdateXid(htup); |
| 8665 |
} |
| 8666 |
|
| 8667 |
/* Enable further/final shrinking of deltids for caller */ |
| 8668 |
finalndeltids = i + 1; |
| 8669 |
} |
| 8670 |
|
| 8671 |
UnlockReleaseBuffer(buf); |
| 8672 |
|
| 8673 |
/* |
| 8674 |
* Shrink deltids array to exclude non-deletable entries at the end. This |
| 8675 |
* is not just a minor optimization. Final deltids array size might be |
| 8676 |
* zero for a bottom-up caller. Index AM is explicitly allowed to rely on |
| 8677 |
* ndeltids being zero in all cases with zero total deletable entries. |
| 8678 |
*/ |
| 8679 |
Assert(finalndeltids > 0 || delstate->bottomup); |
| 8680 |
delstate->ndeltids = finalndeltids; |
| 8681 |
|
| 8682 |
return snapshotConflictHorizon; |
| 8683 |
} |
| 8684 |
|
| 8685 |
/* |
| 8686 |
* Specialized inlineable comparison function for index_delete_sort() |
| 8687 |
*/ |
| 8688 |
static inline int |
| 8689 |
index_delete_sort_cmp(TM_IndexDelete *deltid1, TM_IndexDelete *deltid2) |
| 8690 |
{ |
| 8691 |
ItemPointer tid1 = &deltid1->tid; |
| 8692 |
ItemPointer tid2 = &deltid2->tid; |
| 8693 |
|
| 8694 |
{ |
| 8695 |
BlockNumber blk1 = ItemPointerGetBlockNumber(tid1); |
| 8696 |
BlockNumber blk2 = ItemPointerGetBlockNumber(tid2); |
| 8697 |
|
| 8698 |
if (blk1 != blk2) |
| 8699 |
return (blk1 < blk2) ? -1 : 1; |
| 8700 |
} |
| 8701 |
{ |
| 8702 |
OffsetNumber pos1 = ItemPointerGetOffsetNumber(tid1); |
| 8703 |
OffsetNumber pos2 = ItemPointerGetOffsetNumber(tid2); |
| 8704 |
|
| 8705 |
if (pos1 != pos2) |
| 8706 |
return (pos1 < pos2) ? -1 : 1; |
| 8707 |
} |
| 8708 |
|
| 8709 |
Assert(false); |
| 8710 |
|
| 8711 |
return 0; |
| 8712 |
} |
| 8713 |
|
| 8714 |
/* |
| 8715 |
* Sort deltids array from delstate by TID. This prepares it for further |
| 8716 |
* processing by heap_index_delete_tuples(). |
| 8717 |
* |
| 8718 |
* This operation becomes a noticeable consumer of CPU cycles with some |
| 8719 |
* workloads, so we go to the trouble of specialization/micro optimization. |
| 8720 |
* We use shellsort for this because it's easy to specialize, compiles to |
| 8721 |
* relatively few instructions, and is adaptive to presorted inputs/subsets |
| 8722 |
* (which are typical here). |
| 8723 |
*/ |
| 8724 |
static void |
| 8725 |
index_delete_sort(TM_IndexDeleteOp *delstate) |
| 8726 |
{ |
| 8727 |
TM_IndexDelete *deltids = delstate->deltids; |
| 8728 |
int ndeltids = delstate->ndeltids; |
| 8729 |
|
| 8730 |
/* |
| 8731 |
* Shellsort gap sequence (taken from Sedgewick-Incerpi paper). |
| 8732 |
* |
| 8733 |
* This implementation is fast with array sizes up to ~4500. This covers |
| 8734 |
* all supported BLCKSZ values. |
| 8735 |
*/ |
| 8736 |
const int gaps[9] = {1968, 861, 336, 112, 48, 21, 7, 3, 1}; |
| 8737 |
|
| 8738 |
/* Think carefully before changing anything here -- keep swaps cheap */ |
| 8739 |
StaticAssertDecl(sizeof(TM_IndexDelete) <= 8, |
| 8740 |
"element size exceeds 8 bytes"); |
| 8741 |
|
| 8742 |
for (int g = 0; g < lengthof(gaps); g++) |
| 8743 |
{ |
| 8744 |
for (int hi = gaps[g], i = hi; i < ndeltids; i++) |
| 8745 |
{ |
| 8746 |
TM_IndexDelete d = deltids[i]; |
| 8747 |
int j = i; |
| 8748 |
|
| 8749 |
while (j >= hi && index_delete_sort_cmp(&deltids[j - hi], &d) >= 0) |
| 8750 |
{ |
| 8751 |
deltids[j] = deltids[j - hi]; |
| 8752 |
j -= hi; |
| 8753 |
} |
| 8754 |
deltids[j] = d; |
| 8755 |
} |
| 8756 |
} |
| 8757 |
} |
| 8758 |
|
| 8759 |
/* |
| 8760 |
* Returns how many blocks should be considered favorable/contiguous for a |
| 8761 |
* bottom-up index deletion pass. This is a number of heap blocks that starts |
| 8762 |
* from and includes the first block in line. |
| 8763 |
* |
| 8764 |
* There is always at least one favorable block during bottom-up index |
| 8765 |
* deletion. In the worst case (i.e. with totally random heap blocks) the |
| 8766 |
* first block in line (the only favorable block) can be thought of as a |
| 8767 |
* degenerate array of contiguous blocks that consists of a single block. |
| 8768 |
* heap_index_delete_tuples() will expect this. |
| 8769 |
* |
| 8770 |
* Caller passes blockgroups, a description of the final order that deltids |
| 8771 |
* will be sorted in for heap_index_delete_tuples() bottom-up index deletion |
| 8772 |
* processing. Note that deltids need not actually be sorted just yet (caller |
| 8773 |
* only passes deltids to us so that we can interpret blockgroups). |
| 8774 |
* |
| 8775 |
* You might guess that the existence of contiguous blocks cannot matter much, |
| 8776 |
* since in general the main factor that determines which blocks we visit is |
| 8777 |
* the number of promising TIDs, which is a fixed hint from the index AM. |
| 8778 |
* We're not really targeting the general case, though -- the actual goal is |
| 8779 |
* to adapt our behavior to a wide variety of naturally occurring conditions. |
| 8780 |
* The effects of most of the heuristics we apply are only noticeable in the |
| 8781 |
* aggregate, over time and across many _related_ bottom-up index deletion |
| 8782 |
* passes. |
| 8783 |
* |
| 8784 |
* Deeming certain blocks favorable allows heapam to recognize and adapt to |
| 8785 |
* workloads where heap blocks visited during bottom-up index deletion can be |
| 8786 |
* accessed contiguously, in the sense that each newly visited block is the |
| 8787 |
* neighbor of the block that bottom-up deletion just finished processing (or |
| 8788 |
* close enough to it). It will likely be cheaper to access more favorable |
| 8789 |
* blocks sooner rather than later (e.g. in this pass, not across a series of |
| 8790 |
* related bottom-up passes). Either way it is probably only a matter of time |
| 8791 |
* (or a matter of further correlated version churn) before all blocks that |
| 8792 |
* appear together as a single large batch of favorable blocks get accessed by |
| 8793 |
* _some_ bottom-up pass. Large batches of favorable blocks tend to either |
| 8794 |
* appear almost constantly or not even once (it all depends on per-index |
| 8795 |
* workload characteristics). |
| 8796 |
* |
| 8797 |
* Note that the blockgroups sort order applies a power-of-two bucketing |
| 8798 |
* scheme that creates opportunities for contiguous groups of blocks to get |
| 8799 |
* batched together, at least with workloads that are naturally amenable to |
| 8800 |
* being driven by heap block locality. This doesn't just enhance the spatial |
| 8801 |
* locality of bottom-up heap block processing in the obvious way. It also |
| 8802 |
* enables temporal locality of access, since sorting by heap block number |
| 8803 |
* naturally tends to make the bottom-up processing order deterministic. |
| 8804 |
* |
| 8805 |
* Consider the following example to get a sense of how temporal locality |
| 8806 |
* might matter: There is a heap relation with several indexes, each of which |
| 8807 |
* is low to medium cardinality. It is subject to constant non-HOT updates. |
| 8808 |
* The updates are skewed (in one part of the primary key, perhaps). None of |
| 8809 |
* the indexes are logically modified by the UPDATE statements (if they were |
| 8810 |
* then bottom-up index deletion would not be triggered in the first place). |
| 8811 |
* Naturally, each new round of index tuples (for each heap tuple that gets a |
| 8812 |
* heap_update() call) will have the same heap TID in each and every index. |
| 8813 |
* Since these indexes are low cardinality and never get logically modified, |
| 8814 |
* heapam processing during bottom-up deletion passes will access heap blocks |
| 8815 |
* in approximately sequential order. Temporal locality of access occurs due |
| 8816 |
* to bottom-up deletion passes behaving very similarly across each of the |
| 8817 |
* indexes at any given moment. This keeps the number of buffer misses needed |
| 8818 |
* to visit heap blocks to a minimum. |
| 8819 |
*/ |
| 8820 |
static int |
| 8821 |
bottomup_nblocksfavorable(IndexDeleteCounts *blockgroups, int nblockgroups, |
| 8822 |
TM_IndexDelete *deltids) |
| 8823 |
{ |
| 8824 |
int64 lastblock = -1; |
| 8825 |
int nblocksfavorable = 0; |
| 8826 |
|
| 8827 |
Assert(nblockgroups >= 1); |
| 8828 |
Assert(nblockgroups <= BOTTOMUP_MAX_NBLOCKS); |
| 8829 |
|
| 8830 |
/* |
| 8831 |
* We tolerate heap blocks that will be accessed only slightly out of |
| 8832 |
* physical order. Small blips occur when a pair of almost-contiguous |
| 8833 |
* blocks happen to fall into different buckets (perhaps due only to a |
| 8834 |
* small difference in npromisingtids that the bucketing scheme didn't |
| 8835 |
* quite manage to ignore). We effectively ignore these blips by applying |
| 8836 |
* a small tolerance. The precise tolerance we use is a little arbitrary, |
| 8837 |
* but it works well enough in practice. |
| 8838 |
*/ |
| 8839 |
for (int b = 0; b < nblockgroups; b++) |
| 8840 |
{ |
| 8841 |
IndexDeleteCounts *group = blockgroups + b; |
| 8842 |
TM_IndexDelete *firstdtid = deltids + group->ifirsttid; |
| 8843 |
BlockNumber block = ItemPointerGetBlockNumber(&firstdtid->tid); |
| 8844 |
|
| 8845 |
if (lastblock != -1 && |
| 8846 |
((int64) block < lastblock - BOTTOMUP_TOLERANCE_NBLOCKS || |
| 8847 |
(int64) block > lastblock + BOTTOMUP_TOLERANCE_NBLOCKS)) |
| 8848 |
break; |
| 8849 |
|
| 8850 |
nblocksfavorable++; |
| 8851 |
lastblock = block; |
| 8852 |
} |
| 8853 |
|
| 8854 |
/* Always indicate that there is at least 1 favorable block */ |
| 8855 |
Assert(nblocksfavorable >= 1); |
| 8856 |
|
| 8857 |
return nblocksfavorable; |
| 8858 |
} |
| 8859 |
|
| 8860 |
/* |
| 8861 |
* qsort comparison function for bottomup_sort_and_shrink() |
| 8862 |
*/ |
| 8863 |
static int |
| 8864 |
bottomup_sort_and_shrink_cmp(const void *arg1, const void *arg2) |
| 8865 |
{ |
| 8866 |
const IndexDeleteCounts *group1 = (const IndexDeleteCounts *) arg1; |
| 8867 |
const IndexDeleteCounts *group2 = (const IndexDeleteCounts *) arg2; |
| 8868 |
|
| 8869 |
/* |
| 8870 |
* Most significant field is npromisingtids (which we invert the order of |
| 8871 |
* so as to sort in desc order). |
| 8872 |
* |
| 8873 |
* Caller should have already normalized npromisingtids fields into |
| 8874 |
* power-of-two values (buckets). |
| 8875 |
*/ |
| 8876 |
if (group1->npromisingtids > group2->npromisingtids) |
| 8877 |
return -1; |
| 8878 |
if (group1->npromisingtids < group2->npromisingtids) |
| 8879 |
return 1; |
| 8880 |
|
| 8881 |
/* |
| 8882 |
* Tiebreak: desc ntids sort order. |
| 8883 |
* |
| 8884 |
* We cannot expect power-of-two values for ntids fields. We should |
| 8885 |
* behave as if they were already rounded up for us instead. |
| 8886 |
*/ |
| 8887 |
if (group1->ntids != group2->ntids) |
| 8888 |
{ |
| 8889 |
uint32 ntids1 = pg_nextpower2_32((uint32) group1->ntids); |
| 8890 |
uint32 ntids2 = pg_nextpower2_32((uint32) group2->ntids); |
| 8891 |
|
| 8892 |
if (ntids1 > ntids2) |
| 8893 |
return -1; |
| 8894 |
if (ntids1 < ntids2) |
| 8895 |
return 1; |
| 8896 |
} |
| 8897 |
|
| 8898 |
/* |
| 8899 |
* Tiebreak: asc offset-into-deltids-for-block (offset to first TID for |
| 8900 |
* block in deltids array) order. |
| 8901 |
* |
| 8902 |
* This is equivalent to sorting in ascending heap block number order |
| 8903 |
* (among otherwise equal subsets of the array). This approach allows us |
| 8904 |
* to avoid accessing the out-of-line TID. (We rely on the assumption |
| 8905 |
* that the deltids array was sorted in ascending heap TID order when |
| 8906 |
* these offsets to the first TID from each heap block group were formed.) |
| 8907 |
*/ |
| 8908 |
if (group1->ifirsttid > group2->ifirsttid) |
| 8909 |
return 1; |
| 8910 |
if (group1->ifirsttid < group2->ifirsttid) |
| 8911 |
return -1; |
| 8912 |
|
| 8913 |
pg_unreachable(); |
| 8914 |
|
| 8915 |
return 0; |
| 8916 |
} |
| 8917 |
|
| 8918 |
/* |
| 8919 |
* heap_index_delete_tuples() helper function for bottom-up deletion callers. |
| 8920 |
* |
| 8921 |
* Sorts deltids array in the order needed for useful processing by bottom-up |
| 8922 |
* deletion. The array should already be sorted in TID order when we're |
| 8923 |
* called. The sort process groups heap TIDs from deltids into heap block |
| 8924 |
* groupings. Earlier/more-promising groups/blocks are usually those that are |
| 8925 |
* known to have the most "promising" TIDs. |
| 8926 |
* |
| 8927 |
* Sets new size of deltids array (ndeltids) in state. deltids will only have |
| 8928 |
* TIDs from the BOTTOMUP_MAX_NBLOCKS most promising heap blocks when we |
| 8929 |
* return. This often means that deltids will be shrunk to a small fraction |
| 8930 |
* of its original size (we eliminate many heap blocks from consideration for |
| 8931 |
* caller up front). |
| 8932 |
* |
| 8933 |
* Returns the number of "favorable" blocks. See bottomup_nblocksfavorable() |
| 8934 |
* for a definition and full details. |
| 8935 |
*/ |
| 8936 |
static int |
| 8937 |
bottomup_sort_and_shrink(TM_IndexDeleteOp *delstate) |
| 8938 |
{ |
| 8939 |
IndexDeleteCounts *blockgroups; |
| 8940 |
TM_IndexDelete *reordereddeltids; |
| 8941 |
BlockNumber curblock = InvalidBlockNumber; |
| 8942 |
int nblockgroups = 0; |
| 8943 |
int ncopied = 0; |
| 8944 |
int nblocksfavorable = 0; |
| 8945 |
|
| 8946 |
Assert(delstate->bottomup); |
| 8947 |
Assert(delstate->ndeltids > 0); |
| 8948 |
|
| 8949 |
/* Calculate per-heap-block count of TIDs */ |
| 8950 |
blockgroups = palloc(sizeof(IndexDeleteCounts) * delstate->ndeltids); |
| 8951 |
for (int i = 0; i < delstate->ndeltids; i++) |
| 8952 |
{ |
| 8953 |
TM_IndexDelete *ideltid = &delstate->deltids[i]; |
| 8954 |
TM_IndexStatus *istatus = delstate->status + ideltid->id; |
| 8955 |
ItemPointer htid = &ideltid->tid; |
| 8956 |
bool promising = istatus->promising; |
| 8957 |
|
| 8958 |
if (curblock != ItemPointerGetBlockNumber(htid)) |
| 8959 |
{ |
| 8960 |
/* New block group */ |
| 8961 |
nblockgroups++; |
| 8962 |
|
| 8963 |
Assert(curblock < ItemPointerGetBlockNumber(htid) || |
| 8964 |
!BlockNumberIsValid(curblock)); |
| 8965 |
|
| 8966 |
curblock = ItemPointerGetBlockNumber(htid); |
| 8967 |
blockgroups[nblockgroups - 1].ifirsttid = i; |
| 8968 |
blockgroups[nblockgroups - 1].ntids = 1; |
| 8969 |
blockgroups[nblockgroups - 1].npromisingtids = 0; |
| 8970 |
} |
| 8971 |
else |
| 8972 |
{ |
| 8973 |
blockgroups[nblockgroups - 1].ntids++; |
| 8974 |
} |
| 8975 |
|
| 8976 |
if (promising) |
| 8977 |
blockgroups[nblockgroups - 1].npromisingtids++; |
| 8978 |
} |
| 8979 |
|
| 8980 |
/* |
| 8981 |
* We're about ready to sort block groups to determine the optimal order |
| 8982 |
* for visiting heap blocks. But before we do, round the number of |
| 8983 |
* promising tuples for each block group up to the next power-of-two, |
| 8984 |
* unless it is very low (less than 4), in which case we round up to 4. |
| 8985 |
* npromisingtids is far too noisy to trust when choosing between a pair |
| 8986 |
* of block groups that both have very low values. |
| 8987 |
* |
| 8988 |
* This scheme divides heap blocks/block groups into buckets. Each bucket |
| 8989 |
* contains blocks that have _approximately_ the same number of promising |
| 8990 |
* TIDs as each other. The goal is to ignore relatively small differences |
| 8991 |
* in the total number of promising entries, so that the whole process can |
| 8992 |
* give a little weight to heapam factors (like heap block locality) |
| 8993 |
* instead. This isn't a trade-off, really -- we have nothing to lose. It |
| 8994 |
* would be foolish to interpret small differences in npromisingtids |
| 8995 |
* values as anything more than noise. |
| 8996 |
* |
| 8997 |
* We tiebreak on nhtids when sorting block group subsets that have the |
| 8998 |
* same npromisingtids, but this has the same issues as npromisingtids, |
| 8999 |
* and so nhtids is subject to the same power-of-two bucketing scheme. The |
| 9000 |
* only reason that we don't fix nhtids in the same way here too is that |
| 9001 |
* we'll need accurate nhtids values after the sort. We handle nhtids |
| 9002 |
* bucketization dynamically instead (in the sort comparator). |
| 9003 |
* |
| 9004 |
* See bottomup_nblocksfavorable() for a full explanation of when and how |
| 9005 |
* heap locality/favorable blocks can significantly influence when and how |
| 9006 |
* heap blocks are accessed. |
| 9007 |
*/ |
| 9008 |
for (int b = 0; b < nblockgroups; b++) |
| 9009 |
{ |
| 9010 |
IndexDeleteCounts *group = blockgroups + b; |
| 9011 |
|
| 9012 |
/* Better off falling back on nhtids with low npromisingtids */ |
| 9013 |
if (group->npromisingtids <= 4) |
| 9014 |
group->npromisingtids = 4; |
| 9015 |
else |
| 9016 |
group->npromisingtids = |
| 9017 |
pg_nextpower2_32((uint32) group->npromisingtids); |
| 9018 |
} |
| 9019 |
|
| 9020 |
/* Sort groups and rearrange caller's deltids array */ |
| 9021 |
qsort(blockgroups, nblockgroups, sizeof(IndexDeleteCounts), |
| 9022 |
bottomup_sort_and_shrink_cmp); |
| 9023 |
reordereddeltids = palloc(delstate->ndeltids * sizeof(TM_IndexDelete)); |
| 9024 |
|
| 9025 |
nblockgroups = Min(BOTTOMUP_MAX_NBLOCKS, nblockgroups); |
| 9026 |
/* Determine number of favorable blocks at the start of final deltids */ |
| 9027 |
nblocksfavorable = bottomup_nblocksfavorable(blockgroups, nblockgroups, |
| 9028 |
delstate->deltids); |
| 9029 |
|
| 9030 |
for (int b = 0; b < nblockgroups; b++) |
| 9031 |
{ |
| 9032 |
IndexDeleteCounts *group = blockgroups + b; |
| 9033 |
TM_IndexDelete *firstdtid = delstate->deltids + group->ifirsttid; |
| 9034 |
|
| 9035 |
memcpy(reordereddeltids + ncopied, firstdtid, |
| 9036 |
sizeof(TM_IndexDelete) * group->ntids); |
| 9037 |
ncopied += group->ntids; |
| 9038 |
} |
| 9039 |
|
| 9040 |
/* Copy final grouped and sorted TIDs back into start of caller's array */ |
| 9041 |
memcpy(delstate->deltids, reordereddeltids, |
| 9042 |
sizeof(TM_IndexDelete) * ncopied); |
| 9043 |
delstate->ndeltids = ncopied; |
| 9044 |
|
| 9045 |
pfree(reordereddeltids); |
| 9046 |
pfree(blockgroups); |
| 9047 |
|
| 9048 |
return nblocksfavorable; |
| 9049 |
} |
| 9050 |
|
| 9051 |
/* |
| 9052 |
* Perform XLogInsert for a heap-visible operation. 'block' is the block |
| 9053 |
* being marked all-visible, and vm_buffer is the buffer containing the |
| 9054 |
* corresponding visibility map block. Both should have already been modified |
| 9055 |
* and dirtied. |
| 9056 |
* |
| 9057 |
* snapshotConflictHorizon comes from the largest xmin on the page being |
| 9058 |
* marked all-visible. REDO routine uses it to generate recovery conflicts. |
| 9059 |
* |
| 9060 |
* If checksums or wal_log_hints are enabled, we may also generate a full-page |
| 9061 |
* image of heap_buffer. Otherwise, we optimize away the FPI (by specifying |
| 9062 |
* REGBUF_NO_IMAGE for the heap buffer), in which case the caller should *not* |
| 9063 |
* update the heap page's LSN. |
| 9064 |
*/ |
| 9065 |
XLogRecPtr |
| 9066 |
log_heap_visible(Relation rel, Buffer heap_buffer, Buffer vm_buffer, |
| 9067 |
TransactionId snapshotConflictHorizon, uint8 vmflags) |
| 9068 |
{ |
| 9069 |
xl_heap_visible xlrec; |
| 9070 |
XLogRecPtr recptr; |
| 9071 |
uint8 flags; |
| 9072 |
|
| 9073 |
Assert(BufferIsValid(heap_buffer)); |
| 9074 |
Assert(BufferIsValid(vm_buffer)); |
| 9075 |
|
| 9076 |
xlrec.snapshotConflictHorizon = snapshotConflictHorizon; |
| 9077 |
xlrec.flags = vmflags; |
| 9078 |
if (RelationIsAccessibleInLogicalDecoding(rel)) |
| 9079 |
xlrec.flags |= VISIBILITYMAP_XLOG_CATALOG_REL; |
| 9080 |
XLogBeginInsert(); |
| 9081 |
XLogRegisterData(&xlrec, SizeOfHeapVisible); |
| 9082 |
|
| 9083 |
XLogRegisterBuffer(0, vm_buffer, 0); |
| 9084 |
|
| 9085 |
flags = REGBUF_STANDARD; |
| 9086 |
if (!XLogHintBitIsNeeded()) |
| 9087 |
flags |= REGBUF_NO_IMAGE; |
| 9088 |
XLogRegisterBuffer(1, heap_buffer, flags); |
| 9089 |
|
| 9090 |
recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_VISIBLE); |
| 9091 |
|
| 9092 |
return recptr; |
| 9093 |
} |
| 9094 |
|
| 9095 |
/* |
| 9096 |
* Perform XLogInsert for a heap-update operation. Caller must already |
| 9097 |
* have modified the buffer(s) and marked them dirty. |
| 9098 |
*/ |
| 9099 |
static XLogRecPtr |
| 9100 |
log_heap_update(Relation reln, Buffer oldbuf, Buffer vmbuffer_old, |
| 9101 |
Buffer newbuf, Buffer vmbuffer_new, |
| 9102 |
HeapTuple oldtup, HeapTuple newtup, |
| 9103 |
HeapTuple old_key_tuple, |
| 9104 |
bool all_visible_cleared, bool new_all_visible_cleared) |
| 9105 |
{ |
| 9106 |
xl_heap_update xlrec; |
| 9107 |
xl_heap_header xlhdr; |
| 9108 |
xl_heap_header xlhdr_idx; |
| 9109 |
uint8 info; |
| 9110 |
uint16 prefix_suffix[2]; |
| 9111 |
uint16 prefixlen = 0, |
| 9112 |
suffixlen = 0; |
| 9113 |
XLogRecPtr recptr; |
| 9114 |
Page page = BufferGetPage(newbuf); |
| 9115 |
bool need_tuple_data = RelationIsLogicallyLogged(reln); |
| 9116 |
bool init; |
| 9117 |
int bufflags; |
| 9118 |
|
| 9119 |
/* Caller should not call me on a non-WAL-logged relation */ |
| 9120 |
Assert(RelationNeedsWAL(reln)); |
| 9121 |
|
| 9122 |
XLogBeginInsert(); |
| 9123 |
|
| 9124 |
if (HeapTupleIsHeapOnly(newtup)) |
| 9125 |
info = XLOG_HEAP_HOT_UPDATE; |
| 9126 |
else |
| 9127 |
info = XLOG_HEAP_UPDATE; |
| 9128 |
|
| 9129 |
/* |
| 9130 |
* If the old and new tuple are on the same page, we only need to log the |
| 9131 |
* parts of the new tuple that were changed. That saves on the amount of |
| 9132 |
* WAL we need to write. Currently, we just count any unchanged bytes in |
| 9133 |
* the beginning and end of the tuple. That's quick to check, and |
| 9134 |
* perfectly covers the common case that only one field is updated. |
| 9135 |
* |
| 9136 |
* We could do this even if the old and new tuple are on different pages, |
| 9137 |
* but only if we don't make a full-page image of the old page, which is |
| 9138 |
* difficult to know in advance. Also, if the old tuple is corrupt for |
| 9139 |
* some reason, it would allow the corruption to propagate the new page, |
| 9140 |
* so it seems best to avoid. Under the general assumption that most |
| 9141 |
* updates tend to create the new tuple version on the same page, there |
| 9142 |
* isn't much to be gained by doing this across pages anyway. |
| 9143 |
* |
| 9144 |
* Skip this if we're taking a full-page image of the new page, as we |
| 9145 |
* don't include the new tuple in the WAL record in that case. Also |
| 9146 |
* disable if wal_level='logical', as logical decoding needs to be able to |
| 9147 |
* read the new tuple in whole from the WAL record alone. |
| 9148 |
*/ |
| 9149 |
if (oldbuf == newbuf && !need_tuple_data && |
| 9150 |
!XLogCheckBufferNeedsBackup(newbuf)) |
| 9151 |
{ |
| 9152 |
char *oldp = (char *) oldtup->t_data + oldtup->t_data->t_hoff; |
| 9153 |
char *newp = (char *) newtup->t_data + newtup->t_data->t_hoff; |
| 9154 |
int oldlen = oldtup->t_len - oldtup->t_data->t_hoff; |
| 9155 |
int newlen = newtup->t_len - newtup->t_data->t_hoff; |
| 9156 |
|
| 9157 |
/* Check for common prefix between old and new tuple */ |
| 9158 |
for (prefixlen = 0; prefixlen < Min(oldlen, newlen); prefixlen++) |
| 9159 |
{ |
| 9160 |
if (newp[prefixlen] != oldp[prefixlen]) |
| 9161 |
break; |
| 9162 |
} |
| 9163 |
|
| 9164 |
/* |
| 9165 |
* Storing the length of the prefix takes 2 bytes, so we need to save |
| 9166 |
* at least 3 bytes or there's no point. |
| 9167 |
*/ |
| 9168 |
if (prefixlen < 3) |
| 9169 |
prefixlen = 0; |
| 9170 |
|
| 9171 |
/* Same for suffix */ |
| 9172 |
for (suffixlen = 0; suffixlen < Min(oldlen, newlen) - prefixlen; suffixlen++) |
| 9173 |
{ |
| 9174 |
if (newp[newlen - suffixlen - 1] != oldp[oldlen - suffixlen - 1]) |
| 9175 |
break; |
| 9176 |
} |
| 9177 |
if (suffixlen < 3) |
| 9178 |
suffixlen = 0; |
| 9179 |
} |
| 9180 |
|
| 9181 |
/* Prepare main WAL data chain */ |
| 9182 |
xlrec.flags = 0; |
| 9183 |
if (all_visible_cleared) |
| 9184 |
xlrec.flags |= XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED; |
| 9185 |
if (new_all_visible_cleared) |
| 9186 |
xlrec.flags |= XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED; |
| 9187 |
if (prefixlen > 0) |
| 9188 |
xlrec.flags |= XLH_UPDATE_PREFIX_FROM_OLD; |
| 9189 |
if (suffixlen > 0) |
| 9190 |
xlrec.flags |= XLH_UPDATE_SUFFIX_FROM_OLD; |
| 9191 |
if (need_tuple_data) |
| 9192 |
{ |
| 9193 |
xlrec.flags |= XLH_UPDATE_CONTAINS_NEW_TUPLE; |
| 9194 |
if (old_key_tuple) |
| 9195 |
{ |
| 9196 |
if (reln->rd_rel->relreplident == REPLICA_IDENTITY_FULL) |
| 9197 |
xlrec.flags |= XLH_UPDATE_CONTAINS_OLD_TUPLE; |
| 9198 |
else |
| 9199 |
xlrec.flags |= XLH_UPDATE_CONTAINS_OLD_KEY; |
| 9200 |
} |
| 9201 |
} |
| 9202 |
|
| 9203 |
/* If new tuple is the single and first tuple on page... */ |
| 9204 |
if (ItemPointerGetOffsetNumber(&(newtup->t_self)) == FirstOffsetNumber && |
| 9205 |
PageGetMaxOffsetNumber(page) == FirstOffsetNumber) |
| 9206 |
{ |
| 9207 |
info |= XLOG_HEAP_INIT_PAGE; |
| 9208 |
init = true; |
| 9209 |
} |
| 9210 |
else |
| 9211 |
init = false; |
| 9212 |
|
| 9213 |
/* Prepare WAL data for the old page */ |
| 9214 |
xlrec.old_offnum = ItemPointerGetOffsetNumber(&oldtup->t_self); |
| 9215 |
xlrec.old_xmax = HeapTupleHeaderGetRawXmax(oldtup->t_data); |
| 9216 |
xlrec.old_infobits_set = compute_infobits(oldtup->t_data->t_infomask, |
| 9217 |
oldtup->t_data->t_infomask2); |
| 9218 |
|
| 9219 |
/* Prepare WAL data for the new page */ |
| 9220 |
xlrec.new_offnum = ItemPointerGetOffsetNumber(&newtup->t_self); |
| 9221 |
xlrec.new_xmax = HeapTupleHeaderGetRawXmax(newtup->t_data); |
| 9222 |
|
| 9223 |
bufflags = REGBUF_STANDARD; |
| 9224 |
if (init) |
| 9225 |
bufflags |= REGBUF_WILL_INIT; |
| 9226 |
if (need_tuple_data) |
| 9227 |
bufflags |= REGBUF_KEEP_DATA; |
| 9228 |
|
| 9229 |
XLogRegisterBuffer(HEAP_UPDATE_BLKREF_HEAP_NEW, newbuf, bufflags); |
| 9230 |
if (oldbuf != newbuf) |
| 9231 |
XLogRegisterBuffer(HEAP_UPDATE_BLKREF_HEAP_OLD, oldbuf, REGBUF_STANDARD); |
| 9232 |
|
| 9233 |
XLogRegisterData(&xlrec, SizeOfHeapUpdate); |
| 9234 |
|
| 9235 |
/* |
| 9236 |
* Prepare WAL data for the new tuple. |
| 9237 |
*/ |
| 9238 |
if (prefixlen > 0 || suffixlen > 0) |
| 9239 |
{ |
| 9240 |
if (prefixlen > 0 && suffixlen > 0) |
| 9241 |
{ |
| 9242 |
prefix_suffix[0] = prefixlen; |
| 9243 |
prefix_suffix[1] = suffixlen; |
| 9244 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, &prefix_suffix, |
| 9245 |
sizeof(uint16) * 2); |
| 9246 |
} |
| 9247 |
else if (prefixlen > 0) |
| 9248 |
{ |
| 9249 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, &prefixlen, |
| 9250 |
sizeof(uint16)); |
| 9251 |
} |
| 9252 |
else |
| 9253 |
{ |
| 9254 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, &suffixlen, |
| 9255 |
sizeof(uint16)); |
| 9256 |
} |
| 9257 |
} |
| 9258 |
|
| 9259 |
xlhdr.t_infomask2 = newtup->t_data->t_infomask2; |
| 9260 |
xlhdr.t_infomask = newtup->t_data->t_infomask; |
| 9261 |
xlhdr.t_hoff = newtup->t_data->t_hoff; |
| 9262 |
Assert(SizeofHeapTupleHeader + prefixlen + suffixlen <= newtup->t_len); |
| 9263 |
|
| 9264 |
/* |
| 9265 |
* PG73FORMAT: write bitmap [+ padding] [+ oid] + data |
| 9266 |
* |
| 9267 |
* The 'data' doesn't include the common prefix or suffix. |
| 9268 |
*/ |
| 9269 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, &xlhdr, SizeOfHeapHeader); |
| 9270 |
if (prefixlen == 0) |
| 9271 |
{ |
| 9272 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, |
| 9273 |
(char *) newtup->t_data + SizeofHeapTupleHeader, |
| 9274 |
newtup->t_len - SizeofHeapTupleHeader - suffixlen); |
| 9275 |
} |
| 9276 |
else |
| 9277 |
{ |
| 9278 |
/* |
| 9279 |
* Have to write the null bitmap and data after the common prefix as |
| 9280 |
* two separate rdata entries. |
| 9281 |
*/ |
| 9282 |
/* bitmap [+ padding] [+ oid] */ |
| 9283 |
if (newtup->t_data->t_hoff - SizeofHeapTupleHeader > 0) |
| 9284 |
{ |
| 9285 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, |
| 9286 |
(char *) newtup->t_data + SizeofHeapTupleHeader, |
| 9287 |
newtup->t_data->t_hoff - SizeofHeapTupleHeader); |
| 9288 |
} |
| 9289 |
|
| 9290 |
/* data after common prefix */ |
| 9291 |
XLogRegisterBufData(HEAP_UPDATE_BLKREF_HEAP_NEW, |
| 9292 |
(char *) newtup->t_data + newtup->t_data->t_hoff + prefixlen, |
| 9293 |
newtup->t_len - newtup->t_data->t_hoff - prefixlen - suffixlen); |
| 9294 |
} |
| 9295 |
|
| 9296 |
/* We need to log a tuple identity */ |
| 9297 |
if (need_tuple_data && old_key_tuple) |
| 9298 |
{ |
| 9299 |
/* don't really need this, but its more comfy to decode */ |
| 9300 |
xlhdr_idx.t_infomask2 = old_key_tuple->t_data->t_infomask2; |
| 9301 |
xlhdr_idx.t_infomask = old_key_tuple->t_data->t_infomask; |
| 9302 |
xlhdr_idx.t_hoff = old_key_tuple->t_data->t_hoff; |
| 9303 |
|
| 9304 |
XLogRegisterData(&xlhdr_idx, SizeOfHeapHeader); |
| 9305 |
|
| 9306 |
/* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */ |
| 9307 |
XLogRegisterData((char *) old_key_tuple->t_data + SizeofHeapTupleHeader, |
| 9308 |
old_key_tuple->t_len - SizeofHeapTupleHeader); |
| 9309 |
} |
| 9310 |
|
| 9311 |
/* |
| 9312 |
* Register VM buffers. If the old and new heap pages' VM bits are on the |
| 9313 |
* same VM page and both their VM bits were cleared, the caller passes |
| 9314 |
* only vmbuffer_new (mirroring the heap page convention where block 0 = |
| 9315 |
* new is always registered). |
| 9316 |
*/ |
| 9317 |
Assert((BufferIsInvalid(vmbuffer_old) && BufferIsInvalid(vmbuffer_new)) || |
| 9318 |
(vmbuffer_old != vmbuffer_new)); |
| 9319 |
|
| 9320 |
if (BufferIsValid(vmbuffer_new)) |
| 9321 |
XLogRegisterBuffer(HEAP_UPDATE_BLKREF_VM_NEW, vmbuffer_new, 0); |
| 9322 |
|
| 9323 |
if (BufferIsValid(vmbuffer_old)) |
| 9324 |
XLogRegisterBuffer(HEAP_UPDATE_BLKREF_VM_OLD, vmbuffer_old, 0); |
| 9325 |
|
| 9326 |
/* filtering by origin on a row level is much more efficient */ |
| 9327 |
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); |
| 9328 |
|
| 9329 |
recptr = XLogInsert(RM_HEAP_ID, info); |
| 9330 |
|
| 9331 |
return recptr; |
| 9332 |
} |
| 9333 |
|
| 9334 |
/* |
| 9335 |
* Perform XLogInsert of an XLOG_HEAP2_NEW_CID record |
| 9336 |
* |
| 9337 |
* This is only used in wal_level >= WAL_LEVEL_LOGICAL, and only for catalog |
| 9338 |
* tuples. |
| 9339 |
*/ |
| 9340 |
static XLogRecPtr |
| 9341 |
log_heap_new_cid(Relation relation, HeapTuple tup) |
| 9342 |
{ |
| 9343 |
xl_heap_new_cid xlrec; |
| 9344 |
|
| 9345 |
XLogRecPtr recptr; |
| 9346 |
HeapTupleHeader hdr = tup->t_data; |
| 9347 |
|
| 9348 |
Assert(ItemPointerIsValid(&tup->t_self)); |
| 9349 |
Assert(tup->t_tableOid != InvalidOid); |
| 9350 |
|
| 9351 |
xlrec.top_xid = GetTopTransactionId(); |
| 9352 |
xlrec.target_locator = relation->rd_locator; |
| 9353 |
xlrec.target_tid = tup->t_self; |
| 9354 |
|
| 9355 |
/* |
| 9356 |
* If the tuple got inserted & deleted in the same TX we definitely have a |
| 9357 |
* combo CID, set cmin and cmax. |
| 9358 |
*/ |
| 9359 |
if (hdr->t_infomask & HEAP_COMBOCID) |
| 9360 |
{ |
| 9361 |
Assert(!(hdr->t_infomask & HEAP_XMAX_INVALID)); |
| 9362 |
Assert(!HeapTupleHeaderXminInvalid(hdr)); |
| 9363 |
xlrec.cmin = HeapTupleHeaderGetCmin(hdr); |
| 9364 |
xlrec.cmax = HeapTupleHeaderGetCmax(hdr); |
| 9365 |
xlrec.combocid = HeapTupleHeaderGetRawCommandId(hdr); |
| 9366 |
} |
| 9367 |
/* No combo CID, so only cmin or cmax can be set by this TX */ |
| 9368 |
else |
| 9369 |
{ |
| 9370 |
/* |
| 9371 |
* Tuple inserted. |
| 9372 |
* |
| 9373 |
* We need to check for LOCK ONLY because multixacts might be |
| 9374 |
* transferred to the new tuple in case of FOR KEY SHARE updates in |
| 9375 |
* which case there will be an xmax, although the tuple just got |
| 9376 |
* inserted. |
| 9377 |
*/ |
| 9378 |
if (hdr->t_infomask & HEAP_XMAX_INVALID || |
| 9379 |
HEAP_XMAX_IS_LOCKED_ONLY(hdr->t_infomask)) |
| 9380 |
{ |
| 9381 |
xlrec.cmin = HeapTupleHeaderGetRawCommandId(hdr); |
| 9382 |
xlrec.cmax = InvalidCommandId; |
| 9383 |
} |
| 9384 |
/* Tuple from a different tx updated or deleted. */ |
| 9385 |
else |
| 9386 |
{ |
| 9387 |
xlrec.cmin = InvalidCommandId; |
| 9388 |
xlrec.cmax = HeapTupleHeaderGetRawCommandId(hdr); |
| 9389 |
} |
| 9390 |
xlrec.combocid = InvalidCommandId; |
| 9391 |
} |
| 9392 |
|
| 9393 |
/* |
| 9394 |
* Note that we don't need to register the buffer here, because this |
| 9395 |
* operation does not modify the page. The insert/update/delete that |
| 9396 |
* called us certainly did, but that's WAL-logged separately. |
| 9397 |
*/ |
| 9398 |
XLogBeginInsert(); |
| 9399 |
XLogRegisterData(&xlrec, SizeOfHeapNewCid); |
| 9400 |
|
| 9401 |
/* will be looked at irrespective of origin */ |
| 9402 |
|
| 9403 |
recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_NEW_CID); |
| 9404 |
|
| 9405 |
return recptr; |
| 9406 |
} |
| 9407 |
|
| 9408 |
/* |
| 9409 |
* Build a heap tuple representing the configured REPLICA IDENTITY to represent |
| 9410 |
* the old tuple in an UPDATE or DELETE. |
| 9411 |
* |
| 9412 |
* Returns NULL if there's no need to log an identity or if there's no suitable |
| 9413 |
* key defined. |
| 9414 |
* |
| 9415 |
* Pass key_required true if any replica identity columns changed value, or if |
| 9416 |
* any of them have any external data. Delete must always pass true. |
| 9417 |
* |
| 9418 |
* *copy is set to true if the returned tuple is a modified copy rather than |
| 9419 |
* the same tuple that was passed in. |
| 9420 |
*/ |
| 9421 |
static HeapTuple |
| 9422 |
ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required, |
| 9423 |
bool *copy) |
| 9424 |
{ |
| 9425 |
TupleDesc desc = RelationGetDescr(relation); |
| 9426 |
char replident = relation->rd_rel->relreplident; |
| 9427 |
Bitmapset *idattrs; |
| 9428 |
HeapTuple key_tuple; |
| 9429 |
bool nulls[MaxHeapAttributeNumber]; |
| 9430 |
Datum values[MaxHeapAttributeNumber]; |
| 9431 |
|
| 9432 |
*copy = false; |
| 9433 |
|
| 9434 |
if (!RelationIsLogicallyLogged(relation)) |
| 9435 |
return NULL; |
| 9436 |
|
| 9437 |
if (replident == REPLICA_IDENTITY_NOTHING) |
| 9438 |
return NULL; |
| 9439 |
|
| 9440 |
if (replident == REPLICA_IDENTITY_FULL) |
| 9441 |
{ |
| 9442 |
/* |
| 9443 |
* When logging the entire old tuple, it very well could contain |
| 9444 |
* toasted columns. If so, force them to be inlined. |
| 9445 |
*/ |
| 9446 |
if (HeapTupleHasExternal(tp)) |
| 9447 |
{ |
| 9448 |
*copy = true; |
| 9449 |
tp = toast_flatten_tuple(tp, desc); |
| 9450 |
} |
| 9451 |
return tp; |
| 9452 |
} |
| 9453 |
|
| 9454 |
/* if the key isn't required and we're only logging the key, we're done */ |
| 9455 |
if (!key_required) |
| 9456 |
return NULL; |
| 9457 |
|
| 9458 |
/* find out the replica identity columns */ |
| 9459 |
idattrs = RelationGetIndexAttrBitmap(relation, |
| 9460 |
INDEX_ATTR_BITMAP_IDENTITY_KEY); |
| 9461 |
|
| 9462 |
/* |
| 9463 |
* If there's no defined replica identity columns, treat as !key_required. |
| 9464 |
* (This case should not be reachable from heap_update, since that should |
| 9465 |
* calculate key_required accurately. But heap_delete just passes |
| 9466 |
* constant true for key_required, so we can hit this case in deletes.) |
| 9467 |
*/ |
| 9468 |
if (bms_is_empty(idattrs)) |
| 9469 |
return NULL; |
| 9470 |
|
| 9471 |
/* |
| 9472 |
* Construct a new tuple containing only the replica identity columns, |
| 9473 |
* with nulls elsewhere. While we're at it, assert that the replica |
| 9474 |
* identity columns aren't null. |
| 9475 |
*/ |
| 9476 |
heap_deform_tuple(tp, desc, values, nulls); |
| 9477 |
|
| 9478 |
for (int i = 0; i < desc->natts; i++) |
| 9479 |
{ |
| 9480 |
if (bms_is_member(i + 1 - FirstLowInvalidHeapAttributeNumber, |
| 9481 |
idattrs)) |
| 9482 |
Assert(!nulls[i]); |
| 9483 |
else |
| 9484 |
nulls[i] = true; |
| 9485 |
} |
| 9486 |
|
| 9487 |
key_tuple = heap_form_tuple(desc, values, nulls); |
| 9488 |
*copy = true; |
| 9489 |
|
| 9490 |
bms_free(idattrs); |
| 9491 |
|
| 9492 |
/* |
| 9493 |
* If the tuple, which by here only contains indexed columns, still has |
| 9494 |
* toasted columns, force them to be inlined. This is somewhat unlikely |
| 9495 |
* since there's limits on the size of indexed columns, so we don't |
| 9496 |
* duplicate toast_flatten_tuple()s functionality in the above loop over |
| 9497 |
* the indexed columns, even if it would be more efficient. |
| 9498 |
*/ |
| 9499 |
if (HeapTupleHasExternal(key_tuple)) |
| 9500 |
{ |
| 9501 |
HeapTuple oldtup = key_tuple; |
| 9502 |
|
| 9503 |
key_tuple = toast_flatten_tuple(oldtup, desc); |
| 9504 |
heap_freetuple(oldtup); |
| 9505 |
} |
| 9506 |
|
| 9507 |
return key_tuple; |
| 9508 |
} |
| 9509 |
|
| 9510 |
/* |
| 9511 |
* HeapCheckForSerializableConflictOut |
| 9512 |
* We are reading a tuple. If it's not visible, there may be a |
| 9513 |
* rw-conflict out with the inserter. Otherwise, if it is visible to us |
| 9514 |
* but has been deleted, there may be a rw-conflict out with the deleter. |
| 9515 |
* |
| 9516 |
* We will determine the top level xid of the writing transaction with which |
| 9517 |
* we may be in conflict, and ask CheckForSerializableConflictOut() to check |
| 9518 |
* for overlap with our own transaction. |
| 9519 |
* |
| 9520 |
* This function should be called just about anywhere in heapam.c where a |
| 9521 |
* tuple has been read. The caller must hold at least a shared lock on the |
| 9522 |
* buffer, because this function might set hint bits on the tuple. There is |
| 9523 |
* currently no known reason to call this function from an index AM. |
| 9524 |
*/ |
| 9525 |
void |
| 9526 |
HeapCheckForSerializableConflictOut(bool visible, Relation relation, |
| 9527 |
HeapTuple tuple, Buffer buffer, |
| 9528 |
Snapshot snapshot) |
| 9529 |
{ |
| 9530 |
TransactionId xid; |
| 9531 |
HTSV_Result htsvResult; |
| 9532 |
|
| 9533 |
if (!CheckForSerializableConflictOutNeeded(relation, snapshot)) |
| 9534 |
return; |
| 9535 |
|
| 9536 |
/* |
| 9537 |
* Check to see whether the tuple has been written to by a concurrent |
| 9538 |
* transaction, either to create it not visible to us, or to delete it |
| 9539 |
* while it is visible to us. The "visible" bool indicates whether the |
| 9540 |
* tuple is visible to us, while HeapTupleSatisfiesVacuum checks what else |
| 9541 |
* is going on with it. |
| 9542 |
* |
| 9543 |
* In the event of a concurrently inserted tuple that also happens to have |
| 9544 |
* been concurrently updated (by a separate transaction), the xmin of the |
| 9545 |
* tuple will be used -- not the updater's xid. |
| 9546 |
*/ |
| 9547 |
htsvResult = HeapTupleSatisfiesVacuum(tuple, TransactionXmin, buffer); |
| 9548 |
switch (htsvResult) |
| 9549 |
{ |
| 9550 |
case HEAPTUPLE_LIVE: |
| 9551 |
if (visible) |
| 9552 |
return; |
| 9553 |
xid = HeapTupleHeaderGetXmin(tuple->t_data); |
| 9554 |
break; |
| 9555 |
case HEAPTUPLE_RECENTLY_DEAD: |
| 9556 |
case HEAPTUPLE_DELETE_IN_PROGRESS: |
| 9557 |
if (visible) |
| 9558 |
xid = HeapTupleHeaderGetUpdateXid(tuple->t_data); |
| 9559 |
else |
| 9560 |
xid = HeapTupleHeaderGetXmin(tuple->t_data); |
| 9561 |
|
| 9562 |
if (TransactionIdPrecedes(xid, TransactionXmin)) |
| 9563 |
{ |
| 9564 |
/* This is like the HEAPTUPLE_DEAD case */ |
| 9565 |
Assert(!visible); |
| 9566 |
return; |
| 9567 |
} |
| 9568 |
break; |
| 9569 |
case HEAPTUPLE_INSERT_IN_PROGRESS: |
| 9570 |
xid = HeapTupleHeaderGetXmin(tuple->t_data); |
| 9571 |
break; |
| 9572 |
case HEAPTUPLE_DEAD: |
| 9573 |
Assert(!visible); |
| 9574 |
return; |
| 9575 |
default: |
| 9576 |
|
| 9577 |
/* |
| 9578 |
* The only way to get to this default clause is if a new value is |
| 9579 |
* added to the enum type without adding it to this switch |
| 9580 |
* statement. That's a bug, so elog. |
| 9581 |
*/ |
| 9582 |
elog(ERROR, "unrecognized return value from HeapTupleSatisfiesVacuum: %u", htsvResult); |
| 9583 |
|
| 9584 |
/* |
| 9585 |
* In spite of having all enum values covered and calling elog on |
| 9586 |
* this default, some compilers think this is a code path which |
| 9587 |
* allows xid to be used below without initialization. Silence |
| 9588 |
* that warning. |
| 9589 |
*/ |
| 9590 |
xid = InvalidTransactionId; |
| 9591 |
} |
| 9592 |
|
| 9593 |
Assert(TransactionIdIsValid(xid)); |
| 9594 |
Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin)); |
| 9595 |
|
| 9596 |
/* |
| 9597 |
* Find top level xid. Bail out if xid is too early to be a conflict, or |
| 9598 |
* if it's our own xid. |
| 9599 |
*/ |
| 9600 |
if (TransactionIdEquals(xid, GetTopTransactionIdIfAny())) |
| 9601 |
return; |
| 9602 |
xid = SubTransGetTopmostTransaction(xid); |
| 9603 |
if (TransactionIdPrecedes(xid, TransactionXmin)) |
| 9604 |
return; |
| 9605 |
|
| 9606 |
CheckForSerializableConflictOut(relation, xid, snapshot); |
| 9607 |
} |