LCOV - differential code coverage report
Current view: top level - src/include/access - tableam.h (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 95.0 % 159 151 8 5 146 4
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 48 48 2 46 2
Baseline: 16@8cea358b128 Branches: 46.4 % 84 39 45 2 37
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 9 9 5 4
(240..) days: 94.7 % 150 142 8 142
Function coverage date bins:
[..60] days: 100.0 % 2 2 1 1
(240..) days: 100.0 % 46 46 1 45
Branch coverage date bins:
[..60] days: 100.0 % 2 2 2
(240..) days: 45.1 % 82 37 45 37

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

Generated by: LCOV version 2.1-beta2-3-g6141622