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