Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * tableam.h
4 : * POSTGRES table access method definitions.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * src/include/access/tableam.h
11 : *
12 : * NOTES
13 : * See tableam.sgml for higher level documentation.
14 : *
15 : *-------------------------------------------------------------------------
16 : */
17 : #ifndef TABLEAM_H
18 : #define TABLEAM_H
19 :
20 : #include "access/relscan.h"
21 : #include "access/sdir.h"
22 : #include "access/xact.h"
23 : #include "executor/tuptable.h"
24 : #include "utils/rel.h"
25 : #include "utils/snapshot.h"
26 :
27 :
28 : #define DEFAULT_TABLE_ACCESS_METHOD "heap"
29 :
30 : /* GUCs */
31 : extern PGDLLIMPORT char *default_table_access_method;
32 : extern PGDLLIMPORT bool synchronize_seqscans;
33 :
34 :
35 : struct BulkInsertStateData;
36 : struct IndexInfo;
37 : struct SampleScanState;
38 : struct TBMIterateResult;
39 : struct VacuumParams;
40 : struct ValidateIndexState;
41 :
42 : /*
43 : * Bitmask values for the flags argument to the scan_begin callback.
44 : */
45 : typedef enum ScanOptions
46 : {
47 : /* one of SO_TYPE_* may be specified */
48 : SO_TYPE_SEQSCAN = 1 << 0,
49 : SO_TYPE_BITMAPSCAN = 1 << 1,
50 : SO_TYPE_SAMPLESCAN = 1 << 2,
51 : SO_TYPE_TIDSCAN = 1 << 3,
52 : SO_TYPE_TIDRANGESCAN = 1 << 4,
53 : SO_TYPE_ANALYZE = 1 << 5,
54 :
55 : /* several of SO_ALLOW_* may be specified */
56 : /* allow or disallow use of access strategy */
57 : SO_ALLOW_STRAT = 1 << 6,
58 : /* report location to syncscan logic? */
59 : SO_ALLOW_SYNC = 1 << 7,
60 : /* verify visibility page-at-a-time? */
61 : SO_ALLOW_PAGEMODE = 1 << 8,
62 :
63 : /* unregister snapshot at scan end? */
64 : SO_TEMP_SNAPSHOT = 1 << 9,
65 : } ScanOptions;
66 :
67 : /*
68 : * Result codes for table_{update,delete,lock_tuple}, and for visibility
69 : * routines inside table AMs.
70 : */
71 : typedef enum TM_Result
72 : {
73 : /*
74 : * Signals that the action succeeded (i.e. update/delete performed, lock
75 : * was acquired)
76 : */
77 : TM_Ok,
78 :
79 : /* The affected tuple wasn't visible to the relevant snapshot */
80 : TM_Invisible,
81 :
82 : /* The affected tuple was already modified by the calling backend */
83 : TM_SelfModified,
84 :
85 : /*
86 : * The affected tuple was updated by another transaction. This includes
87 : * the case where tuple was moved to another partition.
88 : */
89 : TM_Updated,
90 :
91 : /* The affected tuple was deleted by another transaction */
92 : TM_Deleted,
93 :
94 : /*
95 : * The affected tuple is currently being modified by another session. This
96 : * will only be returned if table_(update/delete/lock_tuple) are
97 : * instructed not to wait.
98 : */
99 : TM_BeingModified,
100 :
101 : /* lock couldn't be acquired, action skipped. Only used by lock_tuple */
102 : TM_WouldBlock,
103 : } TM_Result;
104 :
105 : /*
106 : * Result codes for table_update(..., update_indexes*..).
107 : * Used to determine which indexes to update.
108 : */
109 : typedef enum TU_UpdateIndexes
110 : {
111 : /* No indexed columns were updated (incl. TID addressing of tuple) */
112 : TU_None,
113 :
114 : /* A non-summarizing indexed column was updated, or the TID has changed */
115 : TU_All,
116 :
117 : /* Only summarized columns were updated, TID is unchanged */
118 : TU_Summarizing,
119 : } TU_UpdateIndexes;
120 :
121 : /*
122 : * When table_tuple_update, table_tuple_delete, or table_tuple_lock fail
123 : * because the target tuple is already outdated, they fill in this struct to
124 : * provide information to the caller about what happened.
125 : *
126 : * ctid is the target's ctid link: it is the same as the target's TID if the
127 : * target was deleted, or the location of the replacement tuple if the target
128 : * was updated.
129 : *
130 : * xmax is the outdating transaction's XID. If the caller wants to visit the
131 : * replacement tuple, it must check that this matches before believing the
132 : * replacement is really a match.
133 : *
134 : * cmax is the outdating command's CID, but only when the failure code is
135 : * TM_SelfModified (i.e., something in the current transaction outdated the
136 : * tuple); otherwise cmax is zero. (We make this restriction because
137 : * HeapTupleHeaderGetCmax doesn't work for tuples outdated in other
138 : * transactions.)
139 : */
140 : typedef struct TM_FailureData
141 : {
142 : ItemPointerData ctid;
143 : TransactionId xmax;
144 : CommandId cmax;
145 : bool traversed;
146 : } TM_FailureData;
147 :
148 : /*
149 : * State used when calling table_index_delete_tuples().
150 : *
151 : * Represents the status of table tuples, referenced by table TID and taken by
152 : * index AM from index tuples. State consists of high level parameters of the
153 : * deletion operation, plus two mutable palloc()'d arrays for information
154 : * about the status of individual table tuples. These are conceptually one
155 : * single array. Using two arrays keeps the TM_IndexDelete struct small,
156 : * which makes sorting the first array (the deltids array) fast.
157 : *
158 : * Some index AM callers perform simple index tuple deletion (by specifying
159 : * bottomup = false), and include only known-dead deltids. These known-dead
160 : * entries are all marked knowndeletable = true directly (typically these are
161 : * TIDs from LP_DEAD-marked index tuples), but that isn't strictly required.
162 : *
163 : * Callers that specify bottomup = true are "bottom-up index deletion"
164 : * callers. The considerations for the tableam are more subtle with these
165 : * callers because they ask the tableam to perform highly speculative work,
166 : * and might only expect the tableam to check a small fraction of all entries.
167 : * Caller is not allowed to specify knowndeletable = true for any entry
168 : * because everything is highly speculative. Bottom-up caller provides
169 : * context and hints to tableam -- see comments below for details on how index
170 : * AMs and tableams should coordinate during bottom-up index deletion.
171 : *
172 : * Simple index deletion callers may ask the tableam to perform speculative
173 : * work, too. This is a little like bottom-up deletion, but not too much.
174 : * The tableam will only perform speculative work when it's practically free
175 : * to do so in passing for simple deletion caller (while always performing
176 : * whatever work is needed to enable knowndeletable/LP_DEAD index tuples to
177 : * be deleted within index AM). This is the real reason why it's possible for
178 : * simple index deletion caller to specify knowndeletable = false up front
179 : * (this means "check if it's possible for me to delete corresponding index
180 : * tuple when it's cheap to do so in passing"). The index AM should only
181 : * include "extra" entries for index tuples whose TIDs point to a table block
182 : * that tableam is expected to have to visit anyway (in the event of a block
183 : * orientated tableam). The tableam isn't strictly obligated to check these
184 : * "extra" TIDs, but a block-based AM should always manage to do so in
185 : * practice.
186 : *
187 : * The final contents of the deltids/status arrays are interesting to callers
188 : * that ask tableam to perform speculative work (i.e. when _any_ items have
189 : * knowndeletable set to false up front). These index AM callers will
190 : * naturally need to consult final state to determine which index tuples are
191 : * in fact deletable.
192 : *
193 : * The index AM can keep track of which index tuple relates to which deltid by
194 : * setting idxoffnum (and/or relying on each entry being uniquely identifiable
195 : * using tid), which is important when the final contents of the array will
196 : * need to be interpreted -- the array can shrink from initial size after
197 : * tableam processing and/or have entries in a new order (tableam may sort
198 : * deltids array for its own reasons). Bottom-up callers may find that final
199 : * ndeltids is 0 on return from call to tableam, in which case no index tuple
200 : * deletions are possible. Simple deletion callers can rely on any entries
201 : * they know to be deletable appearing in the final array as deletable.
202 : */
203 : typedef struct TM_IndexDelete
204 : {
205 : ItemPointerData tid; /* table TID from index tuple */
206 : int16 id; /* Offset into TM_IndexStatus array */
207 : } TM_IndexDelete;
208 :
209 : typedef struct TM_IndexStatus
210 : {
211 : OffsetNumber idxoffnum; /* Index am page offset number */
212 : bool knowndeletable; /* Currently known to be deletable? */
213 :
214 : /* Bottom-up index deletion specific fields follow */
215 : bool promising; /* Promising (duplicate) index tuple? */
216 : int16 freespace; /* Space freed in index if deleted */
217 : } TM_IndexStatus;
218 :
219 : /*
220 : * Index AM/tableam coordination is central to the design of bottom-up index
221 : * deletion. The index AM provides hints about where to look to the tableam
222 : * by marking some entries as "promising". Index AM does this with duplicate
223 : * index tuples that are strongly suspected to be old versions left behind by
224 : * UPDATEs that did not logically modify indexed values. Index AM may find it
225 : * helpful to only mark entries as promising when they're thought to have been
226 : * affected by such an UPDATE in the recent past.
227 : *
228 : * Bottom-up index deletion casts a wide net at first, usually by including
229 : * all TIDs on a target index page. It is up to the tableam to worry about
230 : * the cost of checking transaction status information. The tableam is in
231 : * control, but needs careful guidance from the index AM. Index AM requests
232 : * that bottomupfreespace target be met, while tableam measures progress
233 : * towards that goal by tallying the per-entry freespace value for known
234 : * deletable entries. (All !bottomup callers can just set these space related
235 : * fields to zero.)
236 : */
237 : typedef struct TM_IndexDeleteOp
238 : {
239 : Relation irel; /* Target index relation */
240 : BlockNumber iblknum; /* Index block number (for error reports) */
241 : bool bottomup; /* Bottom-up (not simple) deletion? */
242 : int bottomupfreespace; /* Bottom-up space target */
243 :
244 : /* Mutable per-TID information follows (index AM initializes entries) */
245 : int ndeltids; /* Current # of deltids/status elements */
246 : TM_IndexDelete *deltids;
247 : TM_IndexStatus *status;
248 : } TM_IndexDeleteOp;
249 :
250 : /* "options" flag bits for table_tuple_insert */
251 : /* TABLE_INSERT_SKIP_WAL was 0x0001; RelationNeedsWAL() now governs */
252 : #define TABLE_INSERT_SKIP_FSM 0x0002
253 : #define TABLE_INSERT_FROZEN 0x0004
254 : #define TABLE_INSERT_NO_LOGICAL 0x0008
255 :
256 : /* flag bits for table_tuple_lock */
257 : /* Follow tuples whose update is in progress if lock modes don't conflict */
258 : #define TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS (1 << 0)
259 : /* Follow update chain and lock latest version of tuple */
260 : #define TUPLE_LOCK_FLAG_FIND_LAST_VERSION (1 << 1)
261 :
262 : /*
263 : * "options" flag bits for table_tuple_update and table_tuple_delete,
264 : * Wait for any conflicting update to commit/abort */
265 : #define TABLE_MODIFY_WAIT 0x0001
266 : /* Fetch the existing tuple into a dedicated slot */
267 : #define TABLE_MODIFY_FETCH_OLD_TUPLE 0x0002
268 : /* On concurrent update, follow the update chain and lock latest version of tuple */
269 : #define TABLE_MODIFY_LOCK_UPDATED 0x0004
270 :
271 :
272 : /* Typedef for callback function for table_index_build_scan */
273 : typedef void (*IndexBuildCallback) (Relation index,
274 : ItemPointer tid,
275 : Datum *values,
276 : bool *isnull,
277 : bool tupleIsAlive,
278 : void *state);
279 :
280 : /*
281 : * API struct for a table AM. Note this must be allocated in a
282 : * server-lifetime manner, typically as a static const struct, which then gets
283 : * returned by FormData_pg_am.amhandler.
284 : *
285 : * In most cases it's not appropriate to call the callbacks directly, use the
286 : * table_* wrapper functions instead.
287 : *
288 : * GetTableAmRoutine() asserts that required callbacks are filled in, remember
289 : * to update when adding a callback.
290 : */
291 : typedef struct TableAmRoutine
292 : {
293 : /* this must be set to T_TableAmRoutine */
294 : NodeTag type;
295 :
296 :
297 : /* ------------------------------------------------------------------------
298 : * Slot related callbacks.
299 : * ------------------------------------------------------------------------
300 : */
301 :
302 : /*
303 : * Return slot implementation suitable for storing a tuple of this AM.
304 : */
305 : const TupleTableSlotOps *(*slot_callbacks) (Relation rel);
306 :
307 :
308 : /* ------------------------------------------------------------------------
309 : * Table scan callbacks.
310 : * ------------------------------------------------------------------------
311 : */
312 :
313 : /*
314 : * Start a scan of `rel`. The callback has to return a TableScanDesc,
315 : * which will typically be embedded in a larger, AM specific, struct.
316 : *
317 : * If nkeys != 0, the results need to be filtered by those scan keys.
318 : *
319 : * pscan, if not NULL, will have already been initialized with
320 : * parallelscan_initialize(), and has to be for the same relation. Will
321 : * only be set coming from table_beginscan_parallel().
322 : *
323 : * `flags` is a bitmask indicating the type of scan (ScanOptions's
324 : * SO_TYPE_*, currently only one may be specified), options controlling
325 : * the scan's behaviour (ScanOptions's SO_ALLOW_*, several may be
326 : * specified, an AM may ignore unsupported ones) and whether the snapshot
327 : * needs to be deallocated at scan_end (ScanOptions's SO_TEMP_SNAPSHOT).
328 : */
329 : TableScanDesc (*scan_begin) (Relation rel,
330 : Snapshot snapshot,
331 : int nkeys, struct ScanKeyData *key,
332 : ParallelTableScanDesc pscan,
333 : uint32 flags);
334 :
335 : /*
336 : * Release resources and deallocate scan. If TableScanDesc.temp_snap,
337 : * TableScanDesc.rs_snapshot needs to be unregistered.
338 : */
339 : void (*scan_end) (TableScanDesc scan);
340 :
341 : /*
342 : * Restart relation scan. If set_params is set to true, allow_{strat,
343 : * sync, pagemode} (see scan_begin) changes should be taken into account.
344 : */
345 : void (*scan_rescan) (TableScanDesc scan, struct ScanKeyData *key,
346 : bool set_params, bool allow_strat,
347 : bool allow_sync, bool allow_pagemode);
348 :
349 : /*
350 : * Return next tuple from `scan`, store in slot.
351 : */
352 : bool (*scan_getnextslot) (TableScanDesc scan,
353 : ScanDirection direction,
354 : TupleTableSlot *slot);
355 :
356 : /*-----------
357 : * Optional functions to provide scanning for ranges of ItemPointers.
358 : * Implementations must either provide both of these functions, or neither
359 : * of them.
360 : *
361 : * Implementations of scan_set_tidrange must themselves handle
362 : * ItemPointers of any value. i.e, they must handle each of the following:
363 : *
364 : * 1) mintid or maxtid is beyond the end of the table; and
365 : * 2) mintid is above maxtid; and
366 : * 3) item offset for mintid or maxtid is beyond the maximum offset
367 : * allowed by the AM.
368 : *
369 : * Implementations can assume that scan_set_tidrange is always called
370 : * before scan_getnextslot_tidrange or after scan_rescan and before any
371 : * further calls to scan_getnextslot_tidrange.
372 : */
373 : void (*scan_set_tidrange) (TableScanDesc scan,
374 : ItemPointer mintid,
375 : ItemPointer maxtid);
376 :
377 : /*
378 : * Return next tuple from `scan` that's in the range of TIDs defined by
379 : * scan_set_tidrange.
380 : */
381 : bool (*scan_getnextslot_tidrange) (TableScanDesc scan,
382 : ScanDirection direction,
383 : TupleTableSlot *slot);
384 :
385 : /* ------------------------------------------------------------------------
386 : * Parallel table scan related functions.
387 : * ------------------------------------------------------------------------
388 : */
389 :
390 : /*
391 : * Estimate the size of shared memory needed for a parallel scan of this
392 : * relation. The snapshot does not need to be accounted for.
393 : */
394 : Size (*parallelscan_estimate) (Relation rel);
395 :
396 : /*
397 : * Initialize ParallelTableScanDesc for a parallel scan of this relation.
398 : * `pscan` will be sized according to parallelscan_estimate() for the same
399 : * relation.
400 : */
401 : Size (*parallelscan_initialize) (Relation rel,
402 : ParallelTableScanDesc pscan);
403 :
404 : /*
405 : * Reinitialize `pscan` for a new scan. `rel` will be the same relation as
406 : * when `pscan` was initialized by parallelscan_initialize.
407 : */
408 : void (*parallelscan_reinitialize) (Relation rel,
409 : ParallelTableScanDesc pscan);
410 :
411 :
412 : /* ------------------------------------------------------------------------
413 : * Index Scan Callbacks
414 : * ------------------------------------------------------------------------
415 : */
416 :
417 : /*
418 : * Prepare to fetch tuples from the relation, as needed when fetching
419 : * tuples for an index scan. The callback has to return an
420 : * IndexFetchTableData, which the AM will typically embed in a larger
421 : * structure with additional information.
422 : *
423 : * Tuples for an index scan can then be fetched via index_fetch_tuple.
424 : */
425 : struct IndexFetchTableData *(*index_fetch_begin) (Relation rel);
426 :
427 : /*
428 : * Reset index fetch. Typically this will release cross index fetch
429 : * resources held in IndexFetchTableData.
430 : */
431 : void (*index_fetch_reset) (struct IndexFetchTableData *data);
432 :
433 : /*
434 : * Release resources and deallocate index fetch.
435 : */
436 : void (*index_fetch_end) (struct IndexFetchTableData *data);
437 :
438 : /*
439 : * Fetch tuple at `tid` into `slot`, after doing a visibility test
440 : * according to `snapshot`. If a tuple was found and passed the visibility
441 : * test, return true, false otherwise.
442 : *
443 : * Note that AMs that do not necessarily update indexes when indexed
444 : * columns do not change, need to return the current/correct version of
445 : * the tuple that is visible to the snapshot, even if the tid points to an
446 : * older version of the tuple.
447 : *
448 : * *call_again is false on the first call to index_fetch_tuple for a tid.
449 : * If there potentially is another tuple matching the tid, *call_again
450 : * needs to be set to true by index_fetch_tuple, signaling to the caller
451 : * that index_fetch_tuple should be called again for the same tid.
452 : *
453 : * *all_dead, if all_dead is not NULL, should be set to true by
454 : * index_fetch_tuple iff it is guaranteed that no backend needs to see
455 : * that tuple. Index AMs can use that to avoid returning that tid in
456 : * future searches.
457 : */
458 : bool (*index_fetch_tuple) (struct IndexFetchTableData *scan,
459 : ItemPointer tid,
460 : Snapshot snapshot,
461 : TupleTableSlot *slot,
462 : bool *call_again, bool *all_dead);
463 :
464 :
465 : /* ------------------------------------------------------------------------
466 : * Callbacks for non-modifying operations on individual tuples
467 : * ------------------------------------------------------------------------
468 : */
469 :
470 : /*
471 : * Fetch tuple at `tid` into `slot`, after doing a visibility test
472 : * according to `snapshot`. If a tuple was found and passed the visibility
473 : * test, returns true, false otherwise.
474 : */
475 : bool (*tuple_fetch_row_version) (Relation rel,
476 : ItemPointer tid,
477 : Snapshot snapshot,
478 : TupleTableSlot *slot);
479 :
480 : /*
481 : * Is tid valid for a scan of this relation.
482 : */
483 : bool (*tuple_tid_valid) (TableScanDesc scan,
484 : ItemPointer tid);
485 :
486 : /*
487 : * Return the latest version of the tuple at `tid`, by updating `tid` to
488 : * point at the newest version.
489 : */
490 : void (*tuple_get_latest_tid) (TableScanDesc scan,
491 : ItemPointer tid);
492 :
493 : /*
494 : * Does the tuple in `slot` satisfy `snapshot`? The slot needs to be of
495 : * the appropriate type for the AM.
496 : */
497 : bool (*tuple_satisfies_snapshot) (Relation rel,
498 : TupleTableSlot *slot,
499 : Snapshot snapshot);
500 :
501 : /* see table_index_delete_tuples() */
502 : TransactionId (*index_delete_tuples) (Relation rel,
503 : TM_IndexDeleteOp *delstate);
504 :
505 :
506 : /* ------------------------------------------------------------------------
507 : * Manipulations of physical tuples.
508 : * ------------------------------------------------------------------------
509 : */
510 :
511 : /* see table_tuple_insert() for reference about parameters */
512 : TupleTableSlot *(*tuple_insert) (Relation rel, TupleTableSlot *slot,
513 : CommandId cid, int options,
514 : struct BulkInsertStateData *bistate);
515 :
516 : /* see table_tuple_insert_speculative() for reference about parameters */
517 : void (*tuple_insert_speculative) (Relation rel,
518 : TupleTableSlot *slot,
519 : CommandId cid,
520 : int options,
521 : struct BulkInsertStateData *bistate,
522 : uint32 specToken);
523 :
524 : /* see table_tuple_complete_speculative() for reference about parameters */
525 : void (*tuple_complete_speculative) (Relation rel,
526 : TupleTableSlot *slot,
527 : uint32 specToken,
528 : bool succeeded);
529 :
530 : /* see table_multi_insert() for reference about parameters */
531 : void (*multi_insert) (Relation rel, TupleTableSlot **slots, int nslots,
532 : CommandId cid, int options, struct BulkInsertStateData *bistate);
533 :
534 : /* see table_tuple_delete() for reference about parameters */
535 : TM_Result (*tuple_delete) (Relation rel,
536 : ItemPointer tid,
537 : CommandId cid,
538 : Snapshot snapshot,
539 : Snapshot crosscheck,
540 : int options,
541 : TM_FailureData *tmfd,
542 : bool changingPart,
543 : TupleTableSlot *oldSlot);
544 :
545 : /* see table_tuple_update() for reference about parameters */
546 : TM_Result (*tuple_update) (Relation rel,
547 : ItemPointer otid,
548 : TupleTableSlot *slot,
549 : CommandId cid,
550 : Snapshot snapshot,
551 : Snapshot crosscheck,
552 : int options,
553 : TM_FailureData *tmfd,
554 : LockTupleMode *lockmode,
555 : TU_UpdateIndexes *update_indexes,
556 : TupleTableSlot *oldSlot);
557 :
558 : /* see table_tuple_lock() for reference about parameters */
559 : TM_Result (*tuple_lock) (Relation rel,
560 : ItemPointer tid,
561 : Snapshot snapshot,
562 : TupleTableSlot *slot,
563 : CommandId cid,
564 : LockTupleMode mode,
565 : LockWaitPolicy wait_policy,
566 : uint8 flags,
567 : TM_FailureData *tmfd);
568 :
569 : /*
570 : * Perform operations necessary to complete insertions made via
571 : * tuple_insert and multi_insert with a BulkInsertState specified. In-tree
572 : * access methods ceased to use this.
573 : *
574 : * Typically callers of tuple_insert and multi_insert will just pass all
575 : * the flags that apply to them, and each AM has to decide which of them
576 : * make sense for it, and then only take actions in finish_bulk_insert for
577 : * those flags, and ignore others.
578 : *
579 : * Optional callback.
580 : */
581 : void (*finish_bulk_insert) (Relation rel, int options);
582 :
583 :
584 : /* ------------------------------------------------------------------------
585 : * DDL related functionality.
586 : * ------------------------------------------------------------------------
587 : */
588 :
589 : /*
590 : * This callback needs to create new relation storage for `rel`, with
591 : * appropriate durability behaviour for `persistence`.
592 : *
593 : * Note that only the subset of the relcache filled by
594 : * RelationBuildLocalRelation() can be relied upon and that the relation's
595 : * catalog entries will either not yet exist (new relation), or will still
596 : * reference the old relfilelocator.
597 : *
598 : * As output *freezeXid, *minmulti must be set to the values appropriate
599 : * for pg_class.{relfrozenxid, relminmxid}. For AMs that don't need those
600 : * fields to be filled they can be set to InvalidTransactionId and
601 : * InvalidMultiXactId, respectively.
602 : *
603 : * See also table_relation_set_new_filelocator().
604 : */
605 : void (*relation_set_new_filelocator) (Relation rel,
606 : const RelFileLocator *newrlocator,
607 : char persistence,
608 : TransactionId *freezeXid,
609 : MultiXactId *minmulti);
610 :
611 : /*
612 : * This callback needs to remove all contents from `rel`'s current
613 : * relfilelocator. No provisions for transactional behaviour need to be
614 : * made. Often this can be implemented by truncating the underlying
615 : * storage to its minimal size.
616 : *
617 : * See also table_relation_nontransactional_truncate().
618 : */
619 : void (*relation_nontransactional_truncate) (Relation rel);
620 :
621 : /*
622 : * See table_relation_copy_data().
623 : *
624 : * This can typically be implemented by directly copying the underlying
625 : * storage, unless it contains references to the tablespace internally.
626 : */
627 : void (*relation_copy_data) (Relation rel,
628 : const RelFileLocator *newrlocator);
629 :
630 : /* See table_relation_copy_for_cluster() */
631 : void (*relation_copy_for_cluster) (Relation NewTable,
632 : Relation OldTable,
633 : Relation OldIndex,
634 : bool use_sort,
635 : TransactionId OldestXmin,
636 : TransactionId *xid_cutoff,
637 : MultiXactId *multi_cutoff,
638 : double *num_tuples,
639 : double *tups_vacuumed,
640 : double *tups_recently_dead);
641 :
642 : /*
643 : * React to VACUUM command on the relation. The VACUUM can be triggered by
644 : * a user or by autovacuum. The specific actions performed by the AM will
645 : * depend heavily on the individual AM.
646 : *
647 : * On entry a transaction is already established, and the relation is
648 : * locked with a ShareUpdateExclusive lock.
649 : *
650 : * Note that neither VACUUM FULL (and CLUSTER), nor ANALYZE go through
651 : * this routine, even if (for ANALYZE) it is part of the same VACUUM
652 : * command.
653 : *
654 : * There probably, in the future, needs to be a separate callback to
655 : * integrate with autovacuum's scheduling.
656 : */
657 : void (*relation_vacuum) (Relation rel,
658 : struct VacuumParams *params,
659 : BufferAccessStrategy bstrategy);
660 :
661 : /*
662 : * Prepare to analyze block `blockno` of `scan`. The scan has been started
663 : * with table_beginscan_analyze(). See also
664 : * table_scan_analyze_next_block().
665 : *
666 : * The callback may acquire resources like locks that are held until
667 : * table_scan_analyze_next_tuple() returns false. It e.g. can make sense
668 : * to hold a lock until all tuples on a block have been analyzed by
669 : * scan_analyze_next_tuple.
670 : *
671 : * The callback can return false if the block is not suitable for
672 : * sampling, e.g. because it's a metapage that could never contain tuples.
673 : *
674 : * XXX: This obviously is primarily suited for block-based AMs. It's not
675 : * clear what a good interface for non block based AMs would be, so there
676 : * isn't one yet.
677 : */
678 : bool (*scan_analyze_next_block) (TableScanDesc scan,
679 : BlockNumber blockno,
680 : BufferAccessStrategy bstrategy);
681 :
682 : /*
683 : * See table_scan_analyze_next_tuple().
684 : *
685 : * Not every AM might have a meaningful concept of dead rows, in which
686 : * case it's OK to not increment *deadrows - but note that that may
687 : * influence autovacuum scheduling (see comment for relation_vacuum
688 : * callback).
689 : */
690 : bool (*scan_analyze_next_tuple) (TableScanDesc scan,
691 : TransactionId OldestXmin,
692 : double *liverows,
693 : double *deadrows,
694 : TupleTableSlot *slot);
695 :
696 : /* see table_index_build_range_scan for reference about parameters */
697 : double (*index_build_range_scan) (Relation table_rel,
698 : Relation index_rel,
699 : struct IndexInfo *index_info,
700 : bool allow_sync,
701 : bool anyvisible,
702 : bool progress,
703 : BlockNumber start_blockno,
704 : BlockNumber numblocks,
705 : IndexBuildCallback callback,
706 : void *callback_state,
707 : TableScanDesc scan);
708 :
709 : /* see table_index_validate_scan for reference about parameters */
710 : void (*index_validate_scan) (Relation table_rel,
711 : Relation index_rel,
712 : struct IndexInfo *index_info,
713 : Snapshot snapshot,
714 : struct ValidateIndexState *state);
715 :
716 :
717 : /* ------------------------------------------------------------------------
718 : * Miscellaneous functions.
719 : * ------------------------------------------------------------------------
720 : */
721 :
722 : /*
723 : * This callback frees relation private cache data stored in rd_amcache.
724 : * After the call all memory related to rd_amcache must be freed,
725 : * rd_amcache must be set to NULL. If this callback is not provided,
726 : * rd_amcache is assumed to point to a single memory chunk.
727 : */
728 : void (*free_rd_amcache) (Relation rel);
729 :
730 : /*
731 : * See table_relation_size().
732 : *
733 : * Note that currently a few callers use the MAIN_FORKNUM size to figure
734 : * out the range of potentially interesting blocks (brin, analyze). It's
735 : * probable that we'll need to revise the interface for those at some
736 : * point.
737 : */
738 : uint64 (*relation_size) (Relation rel, ForkNumber forkNumber);
739 :
740 :
741 : /*
742 : * This callback should return true if the relation requires a TOAST table
743 : * and false if it does not. It may wish to examine the relation's tuple
744 : * descriptor before making a decision, but if it uses some other method
745 : * of storing large values (or if it does not support them) it can simply
746 : * return false.
747 : */
748 : bool (*relation_needs_toast_table) (Relation rel);
749 :
750 : /*
751 : * This callback should return the OID of the table AM that implements
752 : * TOAST tables for this AM. If the relation_needs_toast_table callback
753 : * always returns false, this callback is not required.
754 : */
755 : Oid (*relation_toast_am) (Relation rel);
756 :
757 : /*
758 : * This callback is invoked when detoasting a value stored in a toast
759 : * table implemented by this AM. See table_relation_fetch_toast_slice()
760 : * for more details.
761 : */
762 : void (*relation_fetch_toast_slice) (Relation toastrel, Oid valueid,
763 : int32 attrsize,
764 : int32 sliceoffset,
765 : int32 slicelength,
766 : struct varlena *result);
767 :
768 :
769 : /* ------------------------------------------------------------------------
770 : * Planner related functions.
771 : * ------------------------------------------------------------------------
772 : */
773 :
774 : /*
775 : * See table_relation_estimate_size().
776 : *
777 : * While block oriented, it shouldn't be too hard for an AM that doesn't
778 : * internally use blocks to convert into a usable representation.
779 : *
780 : * This differs from the relation_size callback by returning size
781 : * estimates (both relation size and tuple count) for planning purposes,
782 : * rather than returning a currently correct estimate.
783 : */
784 : void (*relation_estimate_size) (Relation rel, int32 *attr_widths,
785 : BlockNumber *pages, double *tuples,
786 : double *allvisfrac);
787 :
788 :
789 : /* ------------------------------------------------------------------------
790 : * Executor related functions.
791 : * ------------------------------------------------------------------------
792 : */
793 :
794 : /*
795 : * Prepare to fetch / check / return tuples from `tbmres->blockno` as part
796 : * of a bitmap table scan. `scan` was started via table_beginscan_bm().
797 : * Return false if there are no tuples to be found on the page, true
798 : * otherwise.
799 : *
800 : * This will typically read and pin the target block, and do the necessary
801 : * work to allow scan_bitmap_next_tuple() to return tuples (e.g. it might
802 : * make sense to perform tuple visibility checks at this time). For some
803 : * AMs it will make more sense to do all the work referencing `tbmres`
804 : * contents here, for others it might be better to defer more work to
805 : * scan_bitmap_next_tuple.
806 : *
807 : * If `tbmres->blockno` is -1, this is a lossy scan and all visible tuples
808 : * on the page have to be returned, otherwise the tuples at offsets in
809 : * `tbmres->offsets` need to be returned.
810 : *
811 : * XXX: Currently this may only be implemented if the AM uses md.c as its
812 : * storage manager, and uses ItemPointer->ip_blkid in a manner that maps
813 : * blockids directly to the underlying storage. nodeBitmapHeapscan.c
814 : * performs prefetching directly using that interface. This probably
815 : * needs to be rectified at a later point.
816 : *
817 : * XXX: Currently this may only be implemented if the AM uses the
818 : * visibilitymap, as nodeBitmapHeapscan.c unconditionally accesses it to
819 : * perform prefetching. This probably needs to be rectified at a later
820 : * point.
821 : *
822 : * Optional callback, but either both scan_bitmap_next_block and
823 : * scan_bitmap_next_tuple need to exist, or neither.
824 : */
825 : bool (*scan_bitmap_next_block) (TableScanDesc scan,
826 : struct TBMIterateResult *tbmres);
827 :
828 : /*
829 : * Fetch the next tuple of a bitmap table scan into `slot` and return true
830 : * if a visible tuple was found, false otherwise.
831 : *
832 : * For some AMs it will make more sense to do all the work referencing
833 : * `tbmres` contents in scan_bitmap_next_block, for others it might be
834 : * better to defer more work to this callback.
835 : *
836 : * Optional callback, but either both scan_bitmap_next_block and
837 : * scan_bitmap_next_tuple need to exist, or neither.
838 : */
839 : bool (*scan_bitmap_next_tuple) (TableScanDesc scan,
840 : struct TBMIterateResult *tbmres,
841 : TupleTableSlot *slot);
842 :
843 : /*
844 : * Prepare to fetch tuples from the next block in a sample scan. Return
845 : * false if the sample scan is finished, true otherwise. `scan` was
846 : * started via table_beginscan_sampling().
847 : *
848 : * Typically this will first determine the target block by calling the
849 : * TsmRoutine's NextSampleBlock() callback if not NULL, or alternatively
850 : * perform a sequential scan over all blocks. The determined block is
851 : * then typically read and pinned.
852 : *
853 : * As the TsmRoutine interface is block based, a block needs to be passed
854 : * to NextSampleBlock(). If that's not appropriate for an AM, it
855 : * internally needs to perform mapping between the internal and a block
856 : * based representation.
857 : *
858 : * Note that it's not acceptable to hold deadlock prone resources such as
859 : * lwlocks until scan_sample_next_tuple() has exhausted the tuples on the
860 : * block - the tuple is likely to be returned to an upper query node, and
861 : * the next call could be off a long while. Holding buffer pins and such
862 : * is obviously OK.
863 : *
864 : * Currently it is required to implement this interface, as there's no
865 : * alternative way (contrary e.g. to bitmap scans) to implement sample
866 : * scans. If infeasible to implement, the AM may raise an error.
867 : */
868 : bool (*scan_sample_next_block) (TableScanDesc scan,
869 : struct SampleScanState *scanstate);
870 :
871 : /*
872 : * This callback, only called after scan_sample_next_block has returned
873 : * true, should determine the next tuple to be returned from the selected
874 : * block using the TsmRoutine's NextSampleTuple() callback.
875 : *
876 : * The callback needs to perform visibility checks, and only return
877 : * visible tuples. That obviously can mean calling NextSampleTuple()
878 : * multiple times.
879 : *
880 : * The TsmRoutine interface assumes that there's a maximum offset on a
881 : * given page, so if that doesn't apply to an AM, it needs to emulate that
882 : * assumption somehow.
883 : */
884 : bool (*scan_sample_next_tuple) (TableScanDesc scan,
885 : struct SampleScanState *scanstate,
886 : TupleTableSlot *slot);
887 :
888 : } TableAmRoutine;
889 :
890 :
891 : /* ----------------------------------------------------------------------------
892 : * Slot functions.
893 : * ----------------------------------------------------------------------------
894 : */
895 :
896 : /*
897 : * Returns slot callbacks suitable for holding tuples of the appropriate type
898 : * for the relation. Works for tables, views, foreign tables and partitioned
899 : * tables.
900 : */
901 : extern const TupleTableSlotOps *table_slot_callbacks(Relation relation);
902 :
903 : /*
904 : * Returns slot using the callbacks returned by table_slot_callbacks(), and
905 : * registers it on *reglist.
906 : */
907 : extern TupleTableSlot *table_slot_create(Relation relation, List **reglist);
908 :
909 :
910 : /* ----------------------------------------------------------------------------
911 : * Table scan functions.
912 : * ----------------------------------------------------------------------------
913 : */
914 :
915 : /*
916 : * Start a scan of `rel`. Returned tuples pass a visibility test of
917 : * `snapshot`, and if nkeys != 0, the results are filtered by those scan keys.
918 : */
919 : static inline TableScanDesc
920 169040 : table_beginscan(Relation rel, Snapshot snapshot,
921 : int nkeys, struct ScanKeyData *key)
922 : {
923 169040 : uint32 flags = SO_TYPE_SEQSCAN |
924 : SO_ALLOW_STRAT | SO_ALLOW_SYNC | SO_ALLOW_PAGEMODE;
925 :
926 169040 : return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
927 : }
928 :
929 : /*
930 : * Like table_beginscan(), but for scanning catalog. It'll automatically use a
931 : * snapshot appropriate for scanning catalog relations.
932 : */
933 : extern TableScanDesc table_beginscan_catalog(Relation relation, int nkeys,
934 : struct ScanKeyData *key);
935 :
936 : /*
937 : * Like table_beginscan(), but table_beginscan_strat() offers an extended API
938 : * that lets the caller control whether a nondefault buffer access strategy
939 : * can be used, and whether syncscan can be chosen (possibly resulting in the
940 : * scan not starting from block zero). Both of these default to true with
941 : * plain table_beginscan.
942 : */
943 : static inline TableScanDesc
944 316812 : table_beginscan_strat(Relation rel, Snapshot snapshot,
945 : int nkeys, struct ScanKeyData *key,
946 : bool allow_strat, bool allow_sync)
947 : {
948 316812 : uint32 flags = SO_TYPE_SEQSCAN | SO_ALLOW_PAGEMODE;
949 :
950 316812 : if (allow_strat)
951 316812 : flags |= SO_ALLOW_STRAT;
952 316812 : if (allow_sync)
953 47442 : flags |= SO_ALLOW_SYNC;
954 :
955 316812 : return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
956 : }
957 :
958 : /*
959 : * table_beginscan_bm is an alternative entry point for setting up a
960 : * TableScanDesc for a bitmap heap scan. Although that scan technology is
961 : * really quite unlike a standard seqscan, there is just enough commonality to
962 : * make it worth using the same data structure.
963 : */
964 : static inline TableScanDesc
965 21268 : table_beginscan_bm(Relation rel, Snapshot snapshot,
966 : int nkeys, struct ScanKeyData *key)
967 : {
968 21268 : uint32 flags = SO_TYPE_BITMAPSCAN | SO_ALLOW_PAGEMODE;
969 :
970 21268 : return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
971 : }
972 :
973 : /*
974 : * table_beginscan_sampling is an alternative entry point for setting up a
975 : * TableScanDesc for a TABLESAMPLE scan. As with bitmap scans, it's worth
976 : * using the same data structure although the behavior is rather different.
977 : * In addition to the options offered by table_beginscan_strat, this call
978 : * also allows control of whether page-mode visibility checking is used.
979 : */
980 : static inline TableScanDesc
981 146 : table_beginscan_sampling(Relation rel, Snapshot snapshot,
982 : int nkeys, struct ScanKeyData *key,
983 : bool allow_strat, bool allow_sync,
984 : bool allow_pagemode)
985 : {
986 146 : uint32 flags = SO_TYPE_SAMPLESCAN;
987 :
988 146 : if (allow_strat)
989 134 : flags |= SO_ALLOW_STRAT;
990 146 : if (allow_sync)
991 66 : flags |= SO_ALLOW_SYNC;
992 146 : if (allow_pagemode)
993 122 : flags |= SO_ALLOW_PAGEMODE;
994 :
995 146 : return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
996 : }
997 :
998 : /*
999 : * table_beginscan_tid is an alternative entry point for setting up a
1000 : * TableScanDesc for a Tid scan. As with bitmap scans, it's worth using
1001 : * the same data structure although the behavior is rather different.
1002 : */
1003 : static inline TableScanDesc
1004 652 : table_beginscan_tid(Relation rel, Snapshot snapshot)
1005 : {
1006 652 : uint32 flags = SO_TYPE_TIDSCAN;
1007 :
1008 652 : return rel->rd_tableam->scan_begin(rel, snapshot, 0, NULL, NULL, flags);
1009 : }
1010 :
1011 : /*
1012 : * table_beginscan_analyze is an alternative entry point for setting up a
1013 : * TableScanDesc for an ANALYZE scan. As with bitmap scans, it's worth using
1014 : * the same data structure although the behavior is rather different.
1015 : */
1016 : static inline TableScanDesc
1017 13300 : table_beginscan_analyze(Relation rel)
1018 : {
1019 13300 : uint32 flags = SO_TYPE_ANALYZE;
1020 :
1021 13300 : return rel->rd_tableam->scan_begin(rel, NULL, 0, NULL, NULL, flags);
1022 : }
1023 :
1024 : /*
1025 : * End relation scan.
1026 : */
1027 : static inline void
1028 599316 : table_endscan(TableScanDesc scan)
1029 : {
1030 599316 : scan->rs_rd->rd_tableam->scan_end(scan);
1031 599316 : }
1032 :
1033 : /*
1034 : * Restart a relation scan.
1035 : */
1036 : static inline void
1037 989238 : table_rescan(TableScanDesc scan,
1038 : struct ScanKeyData *key)
1039 : {
1040 989238 : scan->rs_rd->rd_tableam->scan_rescan(scan, key, false, false, false, false);
1041 989238 : }
1042 :
1043 : /*
1044 : * Restart a relation scan after changing params.
1045 : *
1046 : * This call allows changing the buffer strategy, syncscan, and pagemode
1047 : * options before starting a fresh scan. Note that although the actual use of
1048 : * syncscan might change (effectively, enabling or disabling reporting), the
1049 : * previously selected startblock will be kept.
1050 : */
1051 : static inline void
1052 30 : table_rescan_set_params(TableScanDesc scan, struct ScanKeyData *key,
1053 : bool allow_strat, bool allow_sync, bool allow_pagemode)
1054 : {
1055 30 : scan->rs_rd->rd_tableam->scan_rescan(scan, key, true,
1056 : allow_strat, allow_sync,
1057 : allow_pagemode);
1058 30 : }
1059 :
1060 : /*
1061 : * Return next tuple from `scan`, store in slot.
1062 : */
1063 : static inline bool
1064 73204868 : table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
1065 : {
1066 73204868 : slot->tts_tableOid = RelationGetRelid(sscan->rs_rd);
1067 :
1068 : /* We don't expect actual scans using NoMovementScanDirection */
1069 : Assert(direction == ForwardScanDirection ||
1070 : direction == BackwardScanDirection);
1071 :
1072 : /*
1073 : * We don't expect direct calls to table_scan_getnextslot with valid
1074 : * CheckXidAlive for catalog or regular tables. See detailed comments in
1075 : * xact.c where these variables are declared.
1076 : */
1077 73204868 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
1078 0 : elog(ERROR, "unexpected table_scan_getnextslot call during logical decoding");
1079 :
1080 73204868 : return sscan->rs_rd->rd_tableam->scan_getnextslot(sscan, direction, slot);
1081 : }
1082 :
1083 : /* ----------------------------------------------------------------------------
1084 : * TID Range scanning related functions.
1085 : * ----------------------------------------------------------------------------
1086 : */
1087 :
1088 : /*
1089 : * table_beginscan_tidrange is the entry point for setting up a TableScanDesc
1090 : * for a TID range scan.
1091 : */
1092 : static inline TableScanDesc
1093 112 : table_beginscan_tidrange(Relation rel, Snapshot snapshot,
1094 : ItemPointer mintid,
1095 : ItemPointer maxtid)
1096 : {
1097 : TableScanDesc sscan;
1098 112 : uint32 flags = SO_TYPE_TIDRANGESCAN | SO_ALLOW_PAGEMODE;
1099 :
1100 112 : sscan = rel->rd_tableam->scan_begin(rel, snapshot, 0, NULL, NULL, flags);
1101 :
1102 : /* Set the range of TIDs to scan */
1103 112 : sscan->rs_rd->rd_tableam->scan_set_tidrange(sscan, mintid, maxtid);
1104 :
1105 112 : return sscan;
1106 : }
1107 :
1108 : /*
1109 : * table_rescan_tidrange resets the scan position and sets the minimum and
1110 : * maximum TID range to scan for a TableScanDesc created by
1111 : * table_beginscan_tidrange.
1112 : */
1113 : static inline void
1114 66 : table_rescan_tidrange(TableScanDesc sscan, ItemPointer mintid,
1115 : ItemPointer maxtid)
1116 : {
1117 : /* Ensure table_beginscan_tidrange() was used. */
1118 : Assert((sscan->rs_flags & SO_TYPE_TIDRANGESCAN) != 0);
1119 :
1120 66 : sscan->rs_rd->rd_tableam->scan_rescan(sscan, NULL, false, false, false, false);
1121 66 : sscan->rs_rd->rd_tableam->scan_set_tidrange(sscan, mintid, maxtid);
1122 66 : }
1123 :
1124 : /*
1125 : * Fetch the next tuple from `sscan` for a TID range scan created by
1126 : * table_beginscan_tidrange(). Stores the tuple in `slot` and returns true,
1127 : * or returns false if no more tuples exist in the range.
1128 : */
1129 : static inline bool
1130 5940 : table_scan_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction,
1131 : TupleTableSlot *slot)
1132 : {
1133 : /* Ensure table_beginscan_tidrange() was used. */
1134 : Assert((sscan->rs_flags & SO_TYPE_TIDRANGESCAN) != 0);
1135 :
1136 : /* We don't expect actual scans using NoMovementScanDirection */
1137 : Assert(direction == ForwardScanDirection ||
1138 : direction == BackwardScanDirection);
1139 :
1140 5940 : return sscan->rs_rd->rd_tableam->scan_getnextslot_tidrange(sscan,
1141 : direction,
1142 : slot);
1143 : }
1144 :
1145 :
1146 : /* ----------------------------------------------------------------------------
1147 : * Parallel table scan related functions.
1148 : * ----------------------------------------------------------------------------
1149 : */
1150 :
1151 : /*
1152 : * Estimate the size of shared memory needed for a parallel scan of this
1153 : * relation.
1154 : */
1155 : extern Size table_parallelscan_estimate(Relation rel, Snapshot snapshot);
1156 :
1157 : /*
1158 : * Initialize ParallelTableScanDesc for a parallel scan of this
1159 : * relation. `pscan` needs to be sized according to parallelscan_estimate()
1160 : * for the same relation. Call this just once in the leader process; then,
1161 : * individual workers attach via table_beginscan_parallel.
1162 : */
1163 : extern void table_parallelscan_initialize(Relation rel,
1164 : ParallelTableScanDesc pscan,
1165 : Snapshot snapshot);
1166 :
1167 : /*
1168 : * Begin a parallel scan. `pscan` needs to have been initialized with
1169 : * table_parallelscan_initialize(), for the same relation. The initialization
1170 : * does not need to have happened in this backend.
1171 : *
1172 : * Caller must hold a suitable lock on the relation.
1173 : */
1174 : extern TableScanDesc table_beginscan_parallel(Relation relation,
1175 : ParallelTableScanDesc pscan);
1176 :
1177 : /*
1178 : * Restart a parallel scan. Call this in the leader process. Caller is
1179 : * responsible for making sure that all workers have finished the scan
1180 : * beforehand.
1181 : */
1182 : static inline void
1183 228 : table_parallelscan_reinitialize(Relation rel, ParallelTableScanDesc pscan)
1184 : {
1185 228 : rel->rd_tableam->parallelscan_reinitialize(rel, pscan);
1186 228 : }
1187 :
1188 :
1189 : /* ----------------------------------------------------------------------------
1190 : * Index scan related functions.
1191 : * ----------------------------------------------------------------------------
1192 : */
1193 :
1194 : /*
1195 : * Prepare to fetch tuples from the relation, as needed when fetching tuples
1196 : * for an index scan.
1197 : *
1198 : * Tuples for an index scan can then be fetched via table_index_fetch_tuple().
1199 : */
1200 : static inline IndexFetchTableData *
1201 22042470 : table_index_fetch_begin(Relation rel)
1202 : {
1203 22042470 : return rel->rd_tableam->index_fetch_begin(rel);
1204 : }
1205 :
1206 : /*
1207 : * Reset index fetch. Typically this will release cross index fetch resources
1208 : * held in IndexFetchTableData.
1209 : */
1210 : static inline void
1211 16534278 : table_index_fetch_reset(struct IndexFetchTableData *scan)
1212 : {
1213 16534278 : scan->rel->rd_tableam->index_fetch_reset(scan);
1214 16534278 : }
1215 :
1216 : /*
1217 : * Release resources and deallocate index fetch.
1218 : */
1219 : static inline void
1220 22040918 : table_index_fetch_end(struct IndexFetchTableData *scan)
1221 : {
1222 22040918 : scan->rel->rd_tableam->index_fetch_end(scan);
1223 22040918 : }
1224 :
1225 : /*
1226 : * Fetches, as part of an index scan, tuple at `tid` into `slot`, after doing
1227 : * a visibility test according to `snapshot`. If a tuple was found and passed
1228 : * the visibility test, returns true, false otherwise. Note that *tid may be
1229 : * modified when we return true (see later remarks on multiple row versions
1230 : * reachable via a single index entry).
1231 : *
1232 : * *call_again needs to be false on the first call to table_index_fetch_tuple() for
1233 : * a tid. If there potentially is another tuple matching the tid, *call_again
1234 : * will be set to true, signaling that table_index_fetch_tuple() should be called
1235 : * again for the same tid.
1236 : *
1237 : * *all_dead, if all_dead is not NULL, will be set to true by
1238 : * table_index_fetch_tuple() iff it is guaranteed that no backend needs to see
1239 : * that tuple. Index AMs can use that to avoid returning that tid in future
1240 : * searches.
1241 : *
1242 : * The difference between this function and table_tuple_fetch_row_version()
1243 : * is that this function returns the currently visible version of a row if
1244 : * the AM supports storing multiple row versions reachable via a single index
1245 : * entry (like heap's HOT). Whereas table_tuple_fetch_row_version() only
1246 : * evaluates the tuple exactly at `tid`. Outside of index entry ->table tuple
1247 : * lookups, table_tuple_fetch_row_version() is what's usually needed.
1248 : */
1249 : static inline bool
1250 30771412 : table_index_fetch_tuple(struct IndexFetchTableData *scan,
1251 : ItemPointer tid,
1252 : Snapshot snapshot,
1253 : TupleTableSlot *slot,
1254 : bool *call_again, bool *all_dead)
1255 : {
1256 : /*
1257 : * We don't expect direct calls to table_index_fetch_tuple with valid
1258 : * CheckXidAlive for catalog or regular tables. See detailed comments in
1259 : * xact.c where these variables are declared.
1260 : */
1261 30771412 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
1262 0 : elog(ERROR, "unexpected table_index_fetch_tuple call during logical decoding");
1263 :
1264 30771412 : return scan->rel->rd_tableam->index_fetch_tuple(scan, tid, snapshot,
1265 : slot, call_again,
1266 : all_dead);
1267 : }
1268 :
1269 : /*
1270 : * This is a convenience wrapper around table_index_fetch_tuple() which
1271 : * returns whether there are table tuple items corresponding to an index
1272 : * entry. This likely is only useful to verify if there's a conflict in a
1273 : * unique index.
1274 : */
1275 : extern bool table_index_fetch_tuple_check(Relation rel,
1276 : ItemPointer tid,
1277 : Snapshot snapshot,
1278 : bool *all_dead);
1279 :
1280 :
1281 : /* ------------------------------------------------------------------------
1282 : * Functions for non-modifying operations on individual tuples
1283 : * ------------------------------------------------------------------------
1284 : */
1285 :
1286 :
1287 : /*
1288 : * Fetch tuple at `tid` into `slot`, after doing a visibility test according to
1289 : * `snapshot`. If a tuple was found and passed the visibility test, returns
1290 : * true, false otherwise.
1291 : *
1292 : * See table_index_fetch_tuple's comment about what the difference between
1293 : * these functions is. It is correct to use this function outside of index
1294 : * entry->table tuple lookups.
1295 : */
1296 : static inline bool
1297 602484 : table_tuple_fetch_row_version(Relation rel,
1298 : ItemPointer tid,
1299 : Snapshot snapshot,
1300 : TupleTableSlot *slot)
1301 : {
1302 : /*
1303 : * We don't expect direct calls to table_tuple_fetch_row_version with
1304 : * valid CheckXidAlive for catalog or regular tables. See detailed
1305 : * comments in xact.c where these variables are declared.
1306 : */
1307 602484 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
1308 0 : elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
1309 :
1310 602484 : return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
1311 : }
1312 :
1313 : /*
1314 : * Verify that `tid` is a potentially valid tuple identifier. That doesn't
1315 : * mean that the pointed to row needs to exist or be visible, but that
1316 : * attempting to fetch the row (e.g. with table_tuple_get_latest_tid() or
1317 : * table_tuple_fetch_row_version()) should not error out if called with that
1318 : * tid.
1319 : *
1320 : * `scan` needs to have been started via table_beginscan().
1321 : */
1322 : static inline bool
1323 278 : table_tuple_tid_valid(TableScanDesc scan, ItemPointer tid)
1324 : {
1325 278 : return scan->rs_rd->rd_tableam->tuple_tid_valid(scan, tid);
1326 : }
1327 :
1328 : /*
1329 : * Return the latest version of the tuple at `tid`, by updating `tid` to
1330 : * point at the newest version.
1331 : */
1332 : extern void table_tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid);
1333 :
1334 : /*
1335 : * Return true iff tuple in slot satisfies the snapshot.
1336 : *
1337 : * This assumes the slot's tuple is valid, and of the appropriate type for the
1338 : * AM.
1339 : *
1340 : * Some AMs might modify the data underlying the tuple as a side-effect. If so
1341 : * they ought to mark the relevant buffer dirty.
1342 : */
1343 : static inline bool
1344 197146 : table_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
1345 : Snapshot snapshot)
1346 : {
1347 197146 : return rel->rd_tableam->tuple_satisfies_snapshot(rel, slot, snapshot);
1348 : }
1349 :
1350 : /*
1351 : * Determine which index tuples are safe to delete based on their table TID.
1352 : *
1353 : * Determines which entries from index AM caller's TM_IndexDeleteOp state
1354 : * point to vacuumable table tuples. Entries that are found by tableam to be
1355 : * vacuumable are naturally safe for index AM to delete, and so get directly
1356 : * marked as deletable. See comments above TM_IndexDelete and comments above
1357 : * TM_IndexDeleteOp for full details.
1358 : *
1359 : * Returns a snapshotConflictHorizon transaction ID that caller places in
1360 : * its index deletion WAL record. This might be used during subsequent REDO
1361 : * of the WAL record when in Hot Standby mode -- a recovery conflict for the
1362 : * index deletion operation might be required on the standby.
1363 : */
1364 : static inline TransactionId
1365 10558 : table_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate)
1366 : {
1367 10558 : return rel->rd_tableam->index_delete_tuples(rel, delstate);
1368 : }
1369 :
1370 :
1371 : /* ----------------------------------------------------------------------------
1372 : * Functions for manipulations of physical tuples.
1373 : * ----------------------------------------------------------------------------
1374 : */
1375 :
1376 : /*
1377 : * Insert a tuple from a slot into table AM routine.
1378 : *
1379 : * The options bitmask allows the caller to specify options that may change the
1380 : * behaviour of the AM. The AM will ignore options that it does not support.
1381 : *
1382 : * If the TABLE_INSERT_SKIP_FSM option is specified, AMs are free to not reuse
1383 : * free space in the relation. This can save some cycles when we know the
1384 : * relation is new and doesn't contain useful amounts of free space.
1385 : * TABLE_INSERT_SKIP_FSM is commonly passed directly to
1386 : * RelationGetBufferForTuple. See that method for more information.
1387 : *
1388 : * TABLE_INSERT_FROZEN should only be specified for inserts into
1389 : * relation storage created during the current subtransaction and when
1390 : * there are no prior snapshots or pre-existing portals open.
1391 : * This causes rows to be frozen, which is an MVCC violation and
1392 : * requires explicit options chosen by user.
1393 : *
1394 : * TABLE_INSERT_NO_LOGICAL force-disables the emitting of logical decoding
1395 : * information for the tuple. This should solely be used during table rewrites
1396 : * where RelationIsLogicallyLogged(relation) is not yet accurate for the new
1397 : * relation.
1398 : *
1399 : * Note that most of these options will be applied when inserting into the
1400 : * heap's TOAST table, too, if the tuple requires any out-of-line data.
1401 : *
1402 : * The BulkInsertState object (if any; bistate can be NULL for default
1403 : * behavior) is also just passed through to RelationGetBufferForTuple. If
1404 : * `bistate` is provided, table_finish_bulk_insert() needs to be called.
1405 : *
1406 : * Returns the slot containing the inserted tuple, which may differ from the
1407 : * given slot. For instance, the source slot may be VirtualTupleTableSlot, but
1408 : * the result slot may correspond to the table AM. On return the slot's
1409 : * tts_tid and tts_tableOid are updated to reflect the insertion. But note
1410 : * that any toasting of fields within the slot is NOT reflected in the slots
1411 : * contents.
1412 : */
1413 : static inline TupleTableSlot *
1414 13619254 : table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid,
1415 : int options, struct BulkInsertStateData *bistate)
1416 : {
1417 13619254 : return rel->rd_tableam->tuple_insert(rel, slot, cid, options,
1418 : bistate);
1419 : }
1420 :
1421 : /*
1422 : * Perform a "speculative insertion". These can be backed out afterwards
1423 : * without aborting the whole transaction. Other sessions can wait for the
1424 : * speculative insertion to be confirmed, turning it into a regular tuple, or
1425 : * aborted, as if it never existed. Speculatively inserted tuples behave as
1426 : * "value locks" of short duration, used to implement INSERT .. ON CONFLICT.
1427 : *
1428 : * A transaction having performed a speculative insertion has to either abort,
1429 : * or finish the speculative insertion with
1430 : * table_tuple_complete_speculative(succeeded = ...).
1431 : */
1432 : static inline void
1433 4026 : table_tuple_insert_speculative(Relation rel, TupleTableSlot *slot,
1434 : CommandId cid, int options,
1435 : struct BulkInsertStateData *bistate,
1436 : uint32 specToken)
1437 : {
1438 4026 : rel->rd_tableam->tuple_insert_speculative(rel, slot, cid, options,
1439 : bistate, specToken);
1440 4026 : }
1441 :
1442 : /*
1443 : * Complete "speculative insertion" started in the same transaction. If
1444 : * succeeded is true, the tuple is fully inserted, if false, it's removed.
1445 : */
1446 : static inline void
1447 4020 : table_tuple_complete_speculative(Relation rel, TupleTableSlot *slot,
1448 : uint32 specToken, bool succeeded)
1449 : {
1450 4020 : rel->rd_tableam->tuple_complete_speculative(rel, slot, specToken,
1451 : succeeded);
1452 4020 : }
1453 :
1454 : /*
1455 : * Insert multiple tuples into a table.
1456 : *
1457 : * This is like table_tuple_insert(), but inserts multiple tuples in one
1458 : * operation. That's often faster than calling table_tuple_insert() in a loop,
1459 : * because e.g. the AM can reduce WAL logging and page locking overhead.
1460 : *
1461 : * Except for taking `nslots` tuples as input, and an array of TupleTableSlots
1462 : * in `slots`, the parameters for table_multi_insert() are the same as for
1463 : * table_tuple_insert().
1464 : *
1465 : * Note: this leaks memory into the current memory context. You can create a
1466 : * temporary context before calling this, if that's a problem.
1467 : */
1468 : static inline void
1469 2314 : table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
1470 : CommandId cid, int options, struct BulkInsertStateData *bistate)
1471 : {
1472 2314 : rel->rd_tableam->multi_insert(rel, slots, nslots,
1473 : cid, options, bistate);
1474 2314 : }
1475 :
1476 : /*
1477 : * Delete a tuple (and optionally lock the last tuple version).
1478 : *
1479 : * NB: do not call this directly unless prepared to deal with
1480 : * concurrent-update conditions. Use simple_table_tuple_delete instead.
1481 : *
1482 : * Input parameters:
1483 : * relation - table to be modified (caller must hold suitable lock)
1484 : * tid - TID of tuple to be deleted
1485 : * cid - delete command ID (used for visibility test, and stored into
1486 : * cmax if successful)
1487 : * crosscheck - if not InvalidSnapshot, also check tuple against this
1488 : * options:
1489 : * If TABLE_MODIFY_WAIT, wait for any conflicting update to commit/abort.
1490 : * If TABLE_MODIFY_FETCH_OLD_TUPLE option is given, the existing tuple is
1491 : * fetched into oldSlot when the update is successful.
1492 : * If TABLE_MODIFY_LOCK_UPDATED option is given and the tuple is
1493 : * concurrently updated, then the last tuple version is locked and fetched
1494 : * into oldSlot.
1495 : *
1496 : * Output parameters:
1497 : * tmfd - filled in failure cases (see below)
1498 : * changingPart - true iff the tuple is being moved to another partition
1499 : * table due to an update of the partition key. Otherwise, false.
1500 : * oldSlot - slot to save the deleted or locked tuple. Can be NULL if none of
1501 : * TABLE_MODIFY_FETCH_OLD_TUPLE or TABLE_MODIFY_LOCK_UPDATED options
1502 : * is specified.
1503 : *
1504 : * Normal, successful return value is TM_Ok, which means we did actually
1505 : * delete it. Failure return codes are TM_SelfModified, TM_Updated, and
1506 : * TM_BeingModified (the last only possible if wait == false).
1507 : *
1508 : * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
1509 : * t_xmax, and, if possible, t_cmax. See comments for struct
1510 : * TM_FailureData for additional info.
1511 : */
1512 : static inline TM_Result
1513 1606504 : table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid,
1514 : Snapshot snapshot, Snapshot crosscheck, int options,
1515 : TM_FailureData *tmfd, bool changingPart,
1516 : TupleTableSlot *oldSlot)
1517 : {
1518 1606504 : return rel->rd_tableam->tuple_delete(rel, tid, cid,
1519 : snapshot, crosscheck,
1520 : options, tmfd, changingPart,
1521 : oldSlot);
1522 : }
1523 :
1524 : /*
1525 : * Update a tuple (and optionally lock the last tuple version).
1526 : *
1527 : * NB: do not call this directly unless you are prepared to deal with
1528 : * concurrent-update conditions. Use simple_table_tuple_update instead.
1529 : *
1530 : * Input parameters:
1531 : * relation - table to be modified (caller must hold suitable lock)
1532 : * otid - TID of old tuple to be replaced
1533 : * slot - newly constructed tuple data to store
1534 : * cid - update command ID (used for visibility test, and stored into
1535 : * cmax/cmin if successful)
1536 : * crosscheck - if not InvalidSnapshot, also check old tuple against this
1537 : * options:
1538 : * If TABLE_MODIFY_WAIT, wait for any conflicting update to commit/abort.
1539 : * If TABLE_MODIFY_FETCH_OLD_TUPLE option is given, the existing tuple is
1540 : * fetched into oldSlot when the update is successful.
1541 : * If TABLE_MODIFY_LOCK_UPDATED option is given and the tuple is
1542 : * concurrently updated, then the last tuple version is locked and fetched
1543 : * into oldSlot.
1544 : *
1545 : * Output parameters:
1546 : * tmfd - filled in failure cases (see below)
1547 : * lockmode - filled with lock mode acquired on tuple
1548 : * update_indexes - in success cases this is set to true if new index entries
1549 : * are required for this tuple
1550 : * oldSlot - slot to save the deleted or locked tuple. Can be NULL if none of
1551 : * TABLE_MODIFY_FETCH_OLD_TUPLE or TABLE_MODIFY_LOCK_UPDATED options
1552 : * is specified.
1553 :
1554 : * Normal, successful return value is TM_Ok, which means we did actually
1555 : * update it. Failure return codes are TM_SelfModified, TM_Updated, and
1556 : * TM_BeingModified (the last only possible if wait == false).
1557 : *
1558 : * On success, the slot's tts_tid and tts_tableOid are updated to match the new
1559 : * stored tuple; in particular, slot->tts_tid is set to the TID where the
1560 : * new tuple was inserted, and its HEAP_ONLY_TUPLE flag is set iff a HOT
1561 : * update was done. However, any TOAST changes in the new tuple's
1562 : * data are not reflected into *newtup.
1563 : *
1564 : * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
1565 : * t_xmax, and, if possible, t_cmax. See comments for struct TM_FailureData
1566 : * for additional info.
1567 : */
1568 : static inline TM_Result
1569 376382 : table_tuple_update(Relation rel, ItemPointer otid, TupleTableSlot *slot,
1570 : CommandId cid, Snapshot snapshot, Snapshot crosscheck,
1571 : int options, TM_FailureData *tmfd, LockTupleMode *lockmode,
1572 : TU_UpdateIndexes *update_indexes,
1573 : TupleTableSlot *oldSlot)
1574 : {
1575 376382 : return rel->rd_tableam->tuple_update(rel, otid, slot,
1576 : cid, snapshot, crosscheck,
1577 : options, tmfd,
1578 : lockmode, update_indexes,
1579 : oldSlot);
1580 : }
1581 :
1582 : /*
1583 : * Lock a tuple in the specified mode.
1584 : *
1585 : * Input parameters:
1586 : * relation: relation containing tuple (caller must hold suitable lock)
1587 : * tid: TID of tuple to lock
1588 : * snapshot: snapshot to use for visibility determinations
1589 : * cid: current command ID (used for visibility test, and stored into
1590 : * tuple's cmax if lock is successful)
1591 : * mode: lock mode desired
1592 : * wait_policy: what to do if tuple lock is not available
1593 : * flags:
1594 : * If TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS, follow the update chain to
1595 : * also lock descendant tuples if lock modes don't conflict.
1596 : * If TUPLE_LOCK_FLAG_FIND_LAST_VERSION, follow the update chain and lock
1597 : * latest version.
1598 : *
1599 : * Output parameters:
1600 : * *slot: contains the target tuple
1601 : * *tmfd: filled in failure cases (see below)
1602 : *
1603 : * Function result may be:
1604 : * TM_Ok: lock was successfully acquired
1605 : * TM_Invisible: lock failed because tuple was never visible to us
1606 : * TM_SelfModified: lock failed because tuple updated by self
1607 : * TM_Updated: lock failed because tuple updated by other xact
1608 : * TM_Deleted: lock failed because tuple deleted by other xact
1609 : * TM_WouldBlock: lock couldn't be acquired and wait_policy is skip
1610 : *
1611 : * In the failure cases other than TM_Invisible and TM_Deleted, the routine
1612 : * fills *tmfd with the tuple's t_ctid, t_xmax, and, if possible, t_cmax. See
1613 : * comments for struct TM_FailureData for additional info.
1614 : */
1615 : static inline TM_Result
1616 164780 : table_tuple_lock(Relation rel, ItemPointer tid, Snapshot snapshot,
1617 : TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
1618 : LockWaitPolicy wait_policy, uint8 flags,
1619 : TM_FailureData *tmfd)
1620 : {
1621 164780 : return rel->rd_tableam->tuple_lock(rel, tid, snapshot, slot,
1622 : cid, mode, wait_policy,
1623 : flags, tmfd);
1624 : }
1625 :
1626 : /*
1627 : * Perform operations necessary to complete insertions made via
1628 : * tuple_insert and multi_insert with a BulkInsertState specified.
1629 : */
1630 : static inline void
1631 3714 : table_finish_bulk_insert(Relation rel, int options)
1632 : {
1633 : /* optional callback */
1634 3714 : if (rel->rd_tableam && rel->rd_tableam->finish_bulk_insert)
1635 0 : rel->rd_tableam->finish_bulk_insert(rel, options);
1636 3714 : }
1637 :
1638 :
1639 : /* ------------------------------------------------------------------------
1640 : * DDL related functionality.
1641 : * ------------------------------------------------------------------------
1642 : */
1643 :
1644 : /*
1645 : * Create storage for `rel` in `newrlocator`, with persistence set to
1646 : * `persistence`.
1647 : *
1648 : * This is used both during relation creation and various DDL operations to
1649 : * create new rel storage that can be filled from scratch. When creating
1650 : * new storage for an existing relfilelocator, this should be called before the
1651 : * relcache entry has been updated.
1652 : *
1653 : * *freezeXid, *minmulti are set to the xid / multixact horizon for the table
1654 : * that pg_class.{relfrozenxid, relminmxid} have to be set to.
1655 : */
1656 : static inline void
1657 57102 : table_relation_set_new_filelocator(Relation rel,
1658 : const RelFileLocator *newrlocator,
1659 : char persistence,
1660 : TransactionId *freezeXid,
1661 : MultiXactId *minmulti)
1662 : {
1663 57102 : rel->rd_tableam->relation_set_new_filelocator(rel, newrlocator,
1664 : persistence, freezeXid,
1665 : minmulti);
1666 57102 : }
1667 :
1668 : /*
1669 : * Remove all table contents from `rel`, in a non-transactional manner.
1670 : * Non-transactional meaning that there's no need to support rollbacks. This
1671 : * commonly only is used to perform truncations for relation storage created in
1672 : * the current transaction.
1673 : */
1674 : static inline void
1675 576 : table_relation_nontransactional_truncate(Relation rel)
1676 : {
1677 576 : rel->rd_tableam->relation_nontransactional_truncate(rel);
1678 576 : }
1679 :
1680 : /*
1681 : * Copy data from `rel` into the new relfilelocator `newrlocator`. The new
1682 : * relfilelocator may not have storage associated before this function is
1683 : * called. This is only supposed to be used for low level operations like
1684 : * changing a relation's tablespace.
1685 : */
1686 : static inline void
1687 100 : table_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
1688 : {
1689 100 : rel->rd_tableam->relation_copy_data(rel, newrlocator);
1690 100 : }
1691 :
1692 : /*
1693 : * Copy data from `OldTable` into `NewTable`, as part of a CLUSTER or VACUUM
1694 : * FULL.
1695 : *
1696 : * Additional Input parameters:
1697 : * - use_sort - if true, the table contents are sorted appropriate for
1698 : * `OldIndex`; if false and OldIndex is not InvalidOid, the data is copied
1699 : * in that index's order; if false and OldIndex is InvalidOid, no sorting is
1700 : * performed
1701 : * - OldIndex - see use_sort
1702 : * - OldestXmin - computed by vacuum_get_cutoffs(), even when
1703 : * not needed for the relation's AM
1704 : * - *xid_cutoff - ditto
1705 : * - *multi_cutoff - ditto
1706 : *
1707 : * Output parameters:
1708 : * - *xid_cutoff - rel's new relfrozenxid value, may be invalid
1709 : * - *multi_cutoff - rel's new relminmxid value, may be invalid
1710 : * - *tups_vacuumed - stats, for logging, if appropriate for AM
1711 : * - *tups_recently_dead - stats, for logging, if appropriate for AM
1712 : */
1713 : static inline void
1714 528 : table_relation_copy_for_cluster(Relation OldTable, Relation NewTable,
1715 : Relation OldIndex,
1716 : bool use_sort,
1717 : TransactionId OldestXmin,
1718 : TransactionId *xid_cutoff,
1719 : MultiXactId *multi_cutoff,
1720 : double *num_tuples,
1721 : double *tups_vacuumed,
1722 : double *tups_recently_dead)
1723 : {
1724 528 : OldTable->rd_tableam->relation_copy_for_cluster(OldTable, NewTable, OldIndex,
1725 : use_sort, OldestXmin,
1726 : xid_cutoff, multi_cutoff,
1727 : num_tuples, tups_vacuumed,
1728 : tups_recently_dead);
1729 528 : }
1730 :
1731 : /*
1732 : * Perform VACUUM on the relation. The VACUUM can be triggered by a user or by
1733 : * autovacuum. The specific actions performed by the AM will depend heavily on
1734 : * the individual AM.
1735 : *
1736 : * On entry a transaction needs to already been established, and the
1737 : * table is locked with a ShareUpdateExclusive lock.
1738 : *
1739 : * Note that neither VACUUM FULL (and CLUSTER), nor ANALYZE go through this
1740 : * routine, even if (for ANALYZE) it is part of the same VACUUM command.
1741 : */
1742 : static inline void
1743 19092 : table_relation_vacuum(Relation rel, struct VacuumParams *params,
1744 : BufferAccessStrategy bstrategy)
1745 : {
1746 19092 : rel->rd_tableam->relation_vacuum(rel, params, bstrategy);
1747 19092 : }
1748 :
1749 : /*
1750 : * Prepare to analyze block `blockno` of `scan`. The scan needs to have been
1751 : * started with table_beginscan_analyze(). Note that this routine might
1752 : * acquire resources like locks that are held until
1753 : * table_scan_analyze_next_tuple() returns false.
1754 : *
1755 : * Returns false if block is unsuitable for sampling, true otherwise.
1756 : */
1757 : static inline bool
1758 95870 : table_scan_analyze_next_block(TableScanDesc scan, BlockNumber blockno,
1759 : BufferAccessStrategy bstrategy)
1760 : {
1761 95870 : return scan->rs_rd->rd_tableam->scan_analyze_next_block(scan, blockno,
1762 : bstrategy);
1763 : }
1764 :
1765 : /*
1766 : * Iterate over tuples in the block selected with
1767 : * table_scan_analyze_next_block() (which needs to have returned true, and
1768 : * this routine may not have returned false for the same block before). If a
1769 : * tuple that's suitable for sampling is found, true is returned and a tuple
1770 : * is stored in `slot`.
1771 : *
1772 : * *liverows and *deadrows are incremented according to the encountered
1773 : * tuples.
1774 : */
1775 : static inline bool
1776 8362068 : table_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin,
1777 : double *liverows, double *deadrows,
1778 : TupleTableSlot *slot)
1779 : {
1780 8362068 : return scan->rs_rd->rd_tableam->scan_analyze_next_tuple(scan, OldestXmin,
1781 : liverows, deadrows,
1782 : slot);
1783 : }
1784 :
1785 : /*
1786 : * table_index_build_scan - scan the table to find tuples to be indexed
1787 : *
1788 : * This is called back from an access-method-specific index build procedure
1789 : * after the AM has done whatever setup it needs. The parent table relation
1790 : * is scanned to find tuples that should be entered into the index. Each
1791 : * such tuple is passed to the AM's callback routine, which does the right
1792 : * things to add it to the new index. After we return, the AM's index
1793 : * build procedure does whatever cleanup it needs.
1794 : *
1795 : * The total count of live tuples is returned. This is for updating pg_class
1796 : * statistics. (It's annoying not to be able to do that here, but we want to
1797 : * merge that update with others; see index_update_stats.) Note that the
1798 : * index AM itself must keep track of the number of index tuples; we don't do
1799 : * so here because the AM might reject some of the tuples for its own reasons,
1800 : * such as being unable to store NULLs.
1801 : *
1802 : * If 'progress', the PROGRESS_SCAN_BLOCKS_TOTAL counter is updated when
1803 : * starting the scan, and PROGRESS_SCAN_BLOCKS_DONE is updated as we go along.
1804 : *
1805 : * A side effect is to set indexInfo->ii_BrokenHotChain to true if we detect
1806 : * any potentially broken HOT chains. Currently, we set this if there are any
1807 : * RECENTLY_DEAD or DELETE_IN_PROGRESS entries in a HOT chain, without trying
1808 : * very hard to detect whether they're really incompatible with the chain tip.
1809 : * This only really makes sense for heap AM, it might need to be generalized
1810 : * for other AMs later.
1811 : */
1812 : static inline double
1813 47806 : table_index_build_scan(Relation table_rel,
1814 : Relation index_rel,
1815 : struct IndexInfo *index_info,
1816 : bool allow_sync,
1817 : bool progress,
1818 : IndexBuildCallback callback,
1819 : void *callback_state,
1820 : TableScanDesc scan)
1821 : {
1822 47806 : return table_rel->rd_tableam->index_build_range_scan(table_rel,
1823 : index_rel,
1824 : index_info,
1825 : allow_sync,
1826 : false,
1827 : progress,
1828 : 0,
1829 : InvalidBlockNumber,
1830 : callback,
1831 : callback_state,
1832 : scan);
1833 : }
1834 :
1835 : /*
1836 : * As table_index_build_scan(), except that instead of scanning the complete
1837 : * table, only the given number of blocks are scanned. Scan to end-of-rel can
1838 : * be signaled by passing InvalidBlockNumber as numblocks. Note that
1839 : * restricting the range to scan cannot be done when requesting syncscan.
1840 : *
1841 : * When "anyvisible" mode is requested, all tuples visible to any transaction
1842 : * are indexed and counted as live, including those inserted or deleted by
1843 : * transactions that are still in progress.
1844 : */
1845 : static inline double
1846 2934 : table_index_build_range_scan(Relation table_rel,
1847 : Relation index_rel,
1848 : struct IndexInfo *index_info,
1849 : bool allow_sync,
1850 : bool anyvisible,
1851 : bool progress,
1852 : BlockNumber start_blockno,
1853 : BlockNumber numblocks,
1854 : IndexBuildCallback callback,
1855 : void *callback_state,
1856 : TableScanDesc scan)
1857 : {
1858 2934 : return table_rel->rd_tableam->index_build_range_scan(table_rel,
1859 : index_rel,
1860 : index_info,
1861 : allow_sync,
1862 : anyvisible,
1863 : progress,
1864 : start_blockno,
1865 : numblocks,
1866 : callback,
1867 : callback_state,
1868 : scan);
1869 : }
1870 :
1871 : /*
1872 : * table_index_validate_scan - second table scan for concurrent index build
1873 : *
1874 : * See validate_index() for an explanation.
1875 : */
1876 : static inline void
1877 590 : table_index_validate_scan(Relation table_rel,
1878 : Relation index_rel,
1879 : struct IndexInfo *index_info,
1880 : Snapshot snapshot,
1881 : struct ValidateIndexState *state)
1882 : {
1883 590 : table_rel->rd_tableam->index_validate_scan(table_rel,
1884 : index_rel,
1885 : index_info,
1886 : snapshot,
1887 : state);
1888 590 : }
1889 :
1890 :
1891 : /* ----------------------------------------------------------------------------
1892 : * Miscellaneous functionality
1893 : * ----------------------------------------------------------------------------
1894 : */
1895 :
1896 : /*
1897 : * Frees relation private cache data stored in rd_amcache. Uses
1898 : * free_rd_amcache method if provided. Assumes rd_amcache to point to single
1899 : * memory chunk otherwise.
1900 : */
1901 : static inline void
1902 2248910 : table_free_rd_amcache(Relation rel)
1903 : {
1904 2248910 : if (rel->rd_tableam && rel->rd_tableam->free_rd_amcache)
1905 : {
1906 0 : rel->rd_tableam->free_rd_amcache(rel);
1907 :
1908 : /*
1909 : * We are assuming free_rd_amcache() did clear the cache and left NULL
1910 : * in rd_amcache.
1911 : */
1912 0 : Assert(rel->rd_amcache == NULL);
1913 : }
1914 : else
1915 : {
1916 2248910 : if (rel->rd_amcache)
1917 75330 : pfree(rel->rd_amcache);
1918 2248910 : rel->rd_amcache = NULL;
1919 : }
1920 2248910 : }
1921 :
1922 : /*
1923 : * Return the current size of `rel` in bytes. If `forkNumber` is
1924 : * InvalidForkNumber, return the relation's overall size, otherwise the size
1925 : * for the indicated fork.
1926 : *
1927 : * Note that the overall size might not be the equivalent of the sum of sizes
1928 : * for the individual forks for some AMs, e.g. because the AMs storage does
1929 : * not neatly map onto the builtin types of forks.
1930 : */
1931 : static inline uint64
1932 2110858 : table_relation_size(Relation rel, ForkNumber forkNumber)
1933 : {
1934 2110858 : return rel->rd_tableam->relation_size(rel, forkNumber);
1935 : }
1936 :
1937 : /*
1938 : * table_relation_needs_toast_table - does this relation need a toast table?
1939 : */
1940 : static inline bool
1941 42296 : table_relation_needs_toast_table(Relation rel)
1942 : {
1943 42296 : return rel->rd_tableam->relation_needs_toast_table(rel);
1944 : }
1945 :
1946 : /*
1947 : * Return the OID of the AM that should be used to implement the TOAST table
1948 : * for this relation.
1949 : */
1950 : static inline Oid
1951 15692 : table_relation_toast_am(Relation rel)
1952 : {
1953 15692 : return rel->rd_tableam->relation_toast_am(rel);
1954 : }
1955 :
1956 : /*
1957 : * Fetch all or part of a TOAST value from a TOAST table.
1958 : *
1959 : * If this AM is never used to implement a TOAST table, then this callback
1960 : * is not needed. But, if toasted values are ever stored in a table of this
1961 : * type, then you will need this callback.
1962 : *
1963 : * toastrel is the relation in which the toasted value is stored.
1964 : *
1965 : * valueid identifies which toast value is to be fetched. For the heap,
1966 : * this corresponds to the values stored in the chunk_id column.
1967 : *
1968 : * attrsize is the total size of the toast value to be fetched.
1969 : *
1970 : * sliceoffset is the offset within the toast value of the first byte that
1971 : * should be fetched.
1972 : *
1973 : * slicelength is the number of bytes from the toast value that should be
1974 : * fetched.
1975 : *
1976 : * result is caller-allocated space into which the fetched bytes should be
1977 : * stored.
1978 : */
1979 : static inline void
1980 17464 : table_relation_fetch_toast_slice(Relation toastrel, Oid valueid,
1981 : int32 attrsize, int32 sliceoffset,
1982 : int32 slicelength, struct varlena *result)
1983 : {
1984 17464 : toastrel->rd_tableam->relation_fetch_toast_slice(toastrel, valueid,
1985 : attrsize,
1986 : sliceoffset, slicelength,
1987 : result);
1988 17464 : }
1989 :
1990 :
1991 : /* ----------------------------------------------------------------------------
1992 : * Planner related functionality
1993 : * ----------------------------------------------------------------------------
1994 : */
1995 :
1996 : /*
1997 : * Estimate the current size of the relation, as an AM specific workhorse for
1998 : * estimate_rel_size(). Look there for an explanation of the parameters.
1999 : */
2000 : static inline void
2001 384048 : table_relation_estimate_size(Relation rel, int32 *attr_widths,
2002 : BlockNumber *pages, double *tuples,
2003 : double *allvisfrac)
2004 : {
2005 384048 : rel->rd_tableam->relation_estimate_size(rel, attr_widths, pages, tuples,
2006 : allvisfrac);
2007 384048 : }
2008 :
2009 :
2010 : /* ----------------------------------------------------------------------------
2011 : * Executor related functionality
2012 : * ----------------------------------------------------------------------------
2013 : */
2014 :
2015 : /*
2016 : * Prepare to fetch / check / return tuples from `tbmres->blockno` as part of
2017 : * a bitmap table scan. `scan` needs to have been started via
2018 : * table_beginscan_bm(). Returns false if there are no tuples to be found on
2019 : * the page, true otherwise.
2020 : *
2021 : * Note, this is an optionally implemented function, therefore should only be
2022 : * used after verifying the presence (at plan time or such).
2023 : */
2024 : static inline bool
2025 368104 : table_scan_bitmap_next_block(TableScanDesc scan,
2026 : struct TBMIterateResult *tbmres)
2027 : {
2028 : /*
2029 : * We don't expect direct calls to table_scan_bitmap_next_block with valid
2030 : * CheckXidAlive for catalog or regular tables. See detailed comments in
2031 : * xact.c where these variables are declared.
2032 : */
2033 368104 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
2034 0 : elog(ERROR, "unexpected table_scan_bitmap_next_block call during logical decoding");
2035 :
2036 368104 : return scan->rs_rd->rd_tableam->scan_bitmap_next_block(scan,
2037 : tbmres);
2038 : }
2039 :
2040 : /*
2041 : * Fetch the next tuple of a bitmap table scan into `slot` and return true if
2042 : * a visible tuple was found, false otherwise.
2043 : * table_scan_bitmap_next_block() needs to previously have selected a
2044 : * block (i.e. returned true), and no previous
2045 : * table_scan_bitmap_next_tuple() for the same block may have
2046 : * returned false.
2047 : */
2048 : static inline bool
2049 6115936 : table_scan_bitmap_next_tuple(TableScanDesc scan,
2050 : struct TBMIterateResult *tbmres,
2051 : TupleTableSlot *slot)
2052 : {
2053 : /*
2054 : * We don't expect direct calls to table_scan_bitmap_next_tuple with valid
2055 : * CheckXidAlive for catalog or regular tables. See detailed comments in
2056 : * xact.c where these variables are declared.
2057 : */
2058 6115936 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
2059 0 : elog(ERROR, "unexpected table_scan_bitmap_next_tuple call during logical decoding");
2060 :
2061 6115936 : return scan->rs_rd->rd_tableam->scan_bitmap_next_tuple(scan,
2062 : tbmres,
2063 : slot);
2064 : }
2065 :
2066 : /*
2067 : * Prepare to fetch tuples from the next block in a sample scan. Returns false
2068 : * if the sample scan is finished, true otherwise. `scan` needs to have been
2069 : * started via table_beginscan_sampling().
2070 : *
2071 : * This will call the TsmRoutine's NextSampleBlock() callback if necessary
2072 : * (i.e. NextSampleBlock is not NULL), or perform a sequential scan over the
2073 : * underlying relation.
2074 : */
2075 : static inline bool
2076 12910 : table_scan_sample_next_block(TableScanDesc scan,
2077 : struct SampleScanState *scanstate)
2078 : {
2079 : /*
2080 : * We don't expect direct calls to table_scan_sample_next_block with valid
2081 : * CheckXidAlive for catalog or regular tables. See detailed comments in
2082 : * xact.c where these variables are declared.
2083 : */
2084 12910 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
2085 0 : elog(ERROR, "unexpected table_scan_sample_next_block call during logical decoding");
2086 12910 : return scan->rs_rd->rd_tableam->scan_sample_next_block(scan, scanstate);
2087 : }
2088 :
2089 : /*
2090 : * Fetch the next sample tuple into `slot` and return true if a visible tuple
2091 : * was found, false otherwise. table_scan_sample_next_block() needs to
2092 : * previously have selected a block (i.e. returned true), and no previous
2093 : * table_scan_sample_next_tuple() for the same block may have returned false.
2094 : *
2095 : * This will call the TsmRoutine's NextSampleTuple() callback.
2096 : */
2097 : static inline bool
2098 253894 : table_scan_sample_next_tuple(TableScanDesc scan,
2099 : struct SampleScanState *scanstate,
2100 : TupleTableSlot *slot)
2101 : {
2102 : /*
2103 : * We don't expect direct calls to table_scan_sample_next_tuple with valid
2104 : * CheckXidAlive for catalog or regular tables. See detailed comments in
2105 : * xact.c where these variables are declared.
2106 : */
2107 253894 : if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
2108 0 : elog(ERROR, "unexpected table_scan_sample_next_tuple call during logical decoding");
2109 253894 : return scan->rs_rd->rd_tableam->scan_sample_next_tuple(scan, scanstate,
2110 : slot);
2111 : }
2112 :
2113 :
2114 : /* ----------------------------------------------------------------------------
2115 : * Functions to make modifications a bit simpler.
2116 : * ----------------------------------------------------------------------------
2117 : */
2118 :
2119 : extern void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot);
2120 : extern void simple_table_tuple_delete(Relation rel, ItemPointer tid,
2121 : Snapshot snapshot,
2122 : TupleTableSlot *oldSlot);
2123 : extern void simple_table_tuple_update(Relation rel, ItemPointer otid,
2124 : TupleTableSlot *slot, Snapshot snapshot,
2125 : TU_UpdateIndexes *update_indexes,
2126 : TupleTableSlot *oldSlot);
2127 :
2128 :
2129 : /* ----------------------------------------------------------------------------
2130 : * Helper functions to implement parallel scans for block oriented AMs.
2131 : * ----------------------------------------------------------------------------
2132 : */
2133 :
2134 : extern Size table_block_parallelscan_estimate(Relation rel);
2135 : extern Size table_block_parallelscan_initialize(Relation rel,
2136 : ParallelTableScanDesc pscan);
2137 : extern void table_block_parallelscan_reinitialize(Relation rel,
2138 : ParallelTableScanDesc pscan);
2139 : extern BlockNumber table_block_parallelscan_nextpage(Relation rel,
2140 : ParallelBlockTableScanWorker pbscanwork,
2141 : ParallelBlockTableScanDesc pbscan);
2142 : extern void table_block_parallelscan_startblock_init(Relation rel,
2143 : ParallelBlockTableScanWorker pbscanwork,
2144 : ParallelBlockTableScanDesc pbscan);
2145 :
2146 :
2147 : /* ----------------------------------------------------------------------------
2148 : * Helper functions to implement relation sizing for block oriented AMs.
2149 : * ----------------------------------------------------------------------------
2150 : */
2151 :
2152 : extern uint64 table_block_relation_size(Relation rel, ForkNumber forkNumber);
2153 : extern void table_block_relation_estimate_size(Relation rel,
2154 : int32 *attr_widths,
2155 : BlockNumber *pages,
2156 : double *tuples,
2157 : double *allvisfrac,
2158 : Size overhead_bytes_per_tuple,
2159 : Size usable_bytes_per_page);
2160 :
2161 : /* ----------------------------------------------------------------------------
2162 : * Functions in tableamapi.c
2163 : * ----------------------------------------------------------------------------
2164 : */
2165 :
2166 : extern const TableAmRoutine *GetTableAmRoutine(Oid amhandler);
2167 :
2168 : /* ----------------------------------------------------------------------------
2169 : * Functions in heapam_handler.c
2170 : * ----------------------------------------------------------------------------
2171 : */
2172 :
2173 : extern const TableAmRoutine *GetHeapamTableAmRoutine(void);
2174 :
2175 : #endif /* TABLEAM_H */
|