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