LCOV - code coverage report
Current view: top level - src/include/access - tableam.h (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 128 129 99.2 %
Date: 2020-06-03 10:06:28 Functions: 46 46 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13