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