PostgreSQL Source Code  git master
nbtree.h File Reference
#include "access/amapi.h"
#include "access/itup.h"
#include "access/sdir.h"
#include "access/tableam.h"
#include "access/xlogreader.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_index.h"
#include "lib/stringinfo.h"
#include "storage/bufmgr.h"
#include "storage/shm_toc.h"
Include dependency graph for nbtree.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BTPageOpaqueData
 
struct  BTMetaPageData
 
struct  BTDeletedPageData
 
struct  BTPendingFSM
 
struct  BTVacState
 
struct  BTStackData
 
struct  BTScanInsertData
 
struct  BTInsertStateData
 
struct  BTDedupInterval
 
struct  BTDedupStateData
 
struct  BTVacuumPostingData
 
struct  BTScanPosItem
 
struct  BTScanPosData
 
struct  BTArrayKeyInfo
 
struct  BTScanOpaqueData
 
struct  BTOptions
 

Macros

#define BTPageGetOpaque(page)   ((BTPageOpaque) PageGetSpecialPointer(page))
 
#define BTP_LEAF   (1 << 0) /* leaf page, i.e. not internal page */
 
#define BTP_ROOT   (1 << 1) /* root page (has no parent) */
 
#define BTP_DELETED   (1 << 2) /* page has been deleted from tree */
 
#define BTP_META   (1 << 3) /* meta-page */
 
#define BTP_HALF_DEAD   (1 << 4) /* empty, but still in tree */
 
#define BTP_SPLIT_END   (1 << 5) /* rightmost page of split group */
 
#define BTP_HAS_GARBAGE   (1 << 6) /* page has LP_DEAD tuples (deprecated) */
 
#define BTP_INCOMPLETE_SPLIT   (1 << 7) /* right sibling's downlink is missing */
 
#define BTP_HAS_FULLXID   (1 << 8) /* contains BTDeletedPageData */
 
#define MAX_BT_CYCLE_ID   0xFF7F
 
#define BTPageGetMeta(p)    ((BTMetaPageData *) PageGetContents(p))
 
#define BTREE_METAPAGE   0 /* first page is meta */
 
#define BTREE_MAGIC   0x053162 /* magic number in metapage */
 
#define BTREE_VERSION   4 /* current version number */
 
#define BTREE_MIN_VERSION   2 /* minimum supported version */
 
#define BTREE_NOVAC_VERSION   3 /* version with all meta fields set */
 
#define BTMaxItemSize(page)
 
#define BTMaxItemSizeNoHeapTid(page)
 
#define MaxTIDsPerBTreePage
 
#define BTREE_MIN_FILLFACTOR   10
 
#define BTREE_DEFAULT_FILLFACTOR   90
 
#define BTREE_NONLEAF_FILLFACTOR   70
 
#define BTREE_SINGLEVAL_FILLFACTOR   96
 
#define P_NONE   0
 
#define P_LEFTMOST(opaque)   ((opaque)->btpo_prev == P_NONE)
 
#define P_RIGHTMOST(opaque)   ((opaque)->btpo_next == P_NONE)
 
#define P_ISLEAF(opaque)   (((opaque)->btpo_flags & BTP_LEAF) != 0)
 
#define P_ISROOT(opaque)   (((opaque)->btpo_flags & BTP_ROOT) != 0)
 
#define P_ISDELETED(opaque)   (((opaque)->btpo_flags & BTP_DELETED) != 0)
 
#define P_ISMETA(opaque)   (((opaque)->btpo_flags & BTP_META) != 0)
 
#define P_ISHALFDEAD(opaque)   (((opaque)->btpo_flags & BTP_HALF_DEAD) != 0)
 
#define P_IGNORE(opaque)   (((opaque)->btpo_flags & (BTP_DELETED|BTP_HALF_DEAD)) != 0)
 
#define P_HAS_GARBAGE(opaque)   (((opaque)->btpo_flags & BTP_HAS_GARBAGE) != 0)
 
#define P_INCOMPLETE_SPLIT(opaque)   (((opaque)->btpo_flags & BTP_INCOMPLETE_SPLIT) != 0)
 
#define P_HAS_FULLXID(opaque)   (((opaque)->btpo_flags & BTP_HAS_FULLXID) != 0)
 
#define P_HIKEY   ((OffsetNumber) 1)
 
#define P_FIRSTKEY   ((OffsetNumber) 2)
 
#define P_FIRSTDATAKEY(opaque)   (P_RIGHTMOST(opaque) ? P_HIKEY : P_FIRSTKEY)
 
#define INDEX_ALT_TID_MASK   INDEX_AM_RESERVED_BIT
 
#define BT_OFFSET_MASK   0x0FFF
 
#define BT_STATUS_OFFSET_MASK   0xF000
 
#define BT_PIVOT_HEAP_TID_ATTR   0x1000
 
#define BT_IS_POSTING   0x2000
 
#define BTreeTupleGetNAtts(itup, rel)
 
#define BTCommuteStrategyNumber(strat)   (BTMaxStrategyNumber + 1 - (strat))
 
#define BTORDER_PROC   1
 
#define BTSORTSUPPORT_PROC   2
 
#define BTINRANGE_PROC   3
 
#define BTEQUALIMAGE_PROC   4
 
#define BTOPTIONS_PROC   5
 
#define BTNProcs   5
 
#define BT_READ   BUFFER_LOCK_SHARE
 
#define BT_WRITE   BUFFER_LOCK_EXCLUSIVE
 
#define BTScanPosIsPinned(scanpos)
 
#define BTScanPosUnpin(scanpos)
 
#define BTScanPosUnpinIfPinned(scanpos)
 
#define BTScanPosIsValid(scanpos)
 
#define BTScanPosInvalidate(scanpos)
 
#define SK_BT_REQFWD   0x00010000 /* required to continue forward scan */
 
#define SK_BT_REQBKWD   0x00020000 /* required to continue backward scan */
 
#define SK_BT_INDOPTION_SHIFT   24 /* must clear the above bits */
 
#define SK_BT_DESC   (INDOPTION_DESC << SK_BT_INDOPTION_SHIFT)
 
#define SK_BT_NULLS_FIRST   (INDOPTION_NULLS_FIRST << SK_BT_INDOPTION_SHIFT)
 
#define BTGetFillFactor(relation)
 
#define BTGetTargetPageFreeSpace(relation)    (BLCKSZ * (100 - BTGetFillFactor(relation)) / 100)
 
#define BTGetDeduplicateItems(relation)
 
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN   2
 
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1   3
 
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2   4
 
#define PROGRESS_BTREE_PHASE_LEAF_LOAD   5
 

Typedefs

typedef uint16 BTCycleId
 
typedef struct BTPageOpaqueData BTPageOpaqueData
 
typedef BTPageOpaqueDataBTPageOpaque
 
typedef struct BTMetaPageData BTMetaPageData
 
typedef struct BTDeletedPageData BTDeletedPageData
 
typedef struct BTPendingFSM BTPendingFSM
 
typedef struct BTVacState BTVacState
 
typedef struct BTStackData BTStackData
 
typedef BTStackDataBTStack
 
typedef struct BTScanInsertData BTScanInsertData
 
typedef BTScanInsertDataBTScanInsert
 
typedef struct BTInsertStateData BTInsertStateData
 
typedef BTInsertStateDataBTInsertState
 
typedef struct BTDedupInterval BTDedupInterval
 
typedef struct BTDedupStateData BTDedupStateData
 
typedef BTDedupStateDataBTDedupState
 
typedef struct BTVacuumPostingData BTVacuumPostingData
 
typedef BTVacuumPostingDataBTVacuumPosting
 
typedef struct BTScanPosItem BTScanPosItem
 
typedef struct BTScanPosData BTScanPosData
 
typedef BTScanPosDataBTScanPos
 
typedef struct BTArrayKeyInfo BTArrayKeyInfo
 
typedef struct BTScanOpaqueData BTScanOpaqueData
 
typedef BTScanOpaqueDataBTScanOpaque
 
typedef struct BTOptions BTOptions
 

Functions

static void BTPageSetDeleted (Page page, FullTransactionId safexid)
 
static FullTransactionId BTPageGetDeleteXid (Page page)
 
static bool BTPageIsRecyclable (Page page, Relation heaprel)
 
 StaticAssertDecl (BT_OFFSET_MASK >=INDEX_MAX_KEYS, "BT_OFFSET_MASK can't fit INDEX_MAX_KEYS")
 
static bool BTreeTupleIsPivot (IndexTuple itup)
 
static bool BTreeTupleIsPosting (IndexTuple itup)
 
static void BTreeTupleSetPosting (IndexTuple itup, uint16 nhtids, int postingoffset)
 
static uint16 BTreeTupleGetNPosting (IndexTuple posting)
 
static uint32 BTreeTupleGetPostingOffset (IndexTuple posting)
 
static ItemPointer BTreeTupleGetPosting (IndexTuple posting)
 
static ItemPointer BTreeTupleGetPostingN (IndexTuple posting, int n)
 
static BlockNumber BTreeTupleGetDownLink (IndexTuple pivot)
 
static void BTreeTupleSetDownLink (IndexTuple pivot, BlockNumber blkno)
 
static void BTreeTupleSetNAtts (IndexTuple itup, uint16 nkeyatts, bool heaptid)
 
static BlockNumber BTreeTupleGetTopParent (IndexTuple leafhikey)
 
static void BTreeTupleSetTopParent (IndexTuple leafhikey, BlockNumber blkno)
 
static ItemPointer BTreeTupleGetHeapTID (IndexTuple itup)
 
static ItemPointer BTreeTupleGetMaxHeapTID (IndexTuple itup)
 
void btbuildempty (Relation index)
 
bool btinsert (Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo)
 
IndexScanDesc btbeginscan (Relation rel, int nkeys, int norderbys)
 
Size btestimateparallelscan (void)
 
void btinitparallelscan (void *target)
 
bool btgettuple (IndexScanDesc scan, ScanDirection dir)
 
int64 btgetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
void btrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void btparallelrescan (IndexScanDesc scan)
 
void btendscan (IndexScanDesc scan)
 
void btmarkpos (IndexScanDesc scan)
 
void btrestrpos (IndexScanDesc scan)
 
IndexBulkDeleteResultbtbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultbtvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
bool btcanreturn (Relation index, int attno)
 
bool _bt_parallel_seize (IndexScanDesc scan, BlockNumber *pageno)
 
void _bt_parallel_release (IndexScanDesc scan, BlockNumber scan_page)
 
void _bt_parallel_done (IndexScanDesc scan)
 
void _bt_parallel_advance_array_keys (IndexScanDesc scan)
 
void _bt_dedup_pass (Relation rel, Buffer buf, IndexTuple newitem, Size newitemsz, bool bottomupdedup)
 
bool _bt_bottomupdel_pass (Relation rel, Buffer buf, Relation heapRel, Size newitemsz)
 
void _bt_dedup_start_pending (BTDedupState state, IndexTuple base, OffsetNumber baseoff)
 
bool _bt_dedup_save_htid (BTDedupState state, IndexTuple itup)
 
Size _bt_dedup_finish_pending (Page newpage, BTDedupState state)
 
IndexTuple _bt_form_posting (IndexTuple base, ItemPointer htids, int nhtids)
 
void _bt_update_posting (BTVacuumPosting vacposting)
 
IndexTuple _bt_swap_posting (IndexTuple newitem, IndexTuple oposting, int postingoff)
 
bool _bt_doinsert (Relation rel, IndexTuple itup, IndexUniqueCheck checkUnique, bool indexUnchanged, Relation heapRel)
 
void _bt_finish_split (Relation rel, Relation heaprel, Buffer lbuf, BTStack stack)
 
Buffer _bt_getstackbuf (Relation rel, Relation heaprel, BTStack stack, BlockNumber child)
 
OffsetNumber _bt_findsplitloc (Relation rel, Page origpage, OffsetNumber newitemoff, Size newitemsz, IndexTuple newitem, bool *newitemonleft)
 
void _bt_initmetapage (Page page, BlockNumber rootbknum, uint32 level, bool allequalimage)
 
bool _bt_vacuum_needs_cleanup (Relation rel)
 
void _bt_set_cleanup_info (Relation rel, BlockNumber num_delpages)
 
void _bt_upgrademetapage (Page page)
 
Buffer _bt_getroot (Relation rel, Relation heaprel, int access)
 
Buffer _bt_gettrueroot (Relation rel)
 
int _bt_getrootheight (Relation rel)
 
void _bt_metaversion (Relation rel, bool *heapkeyspace, bool *allequalimage)
 
void _bt_checkpage (Relation rel, Buffer buf)
 
Buffer _bt_getbuf (Relation rel, BlockNumber blkno, int access)
 
Buffer _bt_allocbuf (Relation rel, Relation heaprel)
 
Buffer _bt_relandgetbuf (Relation rel, Buffer obuf, BlockNumber blkno, int access)
 
void _bt_relbuf (Relation rel, Buffer buf)
 
void _bt_lockbuf (Relation rel, Buffer buf, int access)
 
void _bt_unlockbuf (Relation rel, Buffer buf)
 
bool _bt_conditionallockbuf (Relation rel, Buffer buf)
 
void _bt_upgradelockbufcleanup (Relation rel, Buffer buf)
 
void _bt_pageinit (Page page, Size size)
 
void _bt_delitems_vacuum (Relation rel, Buffer buf, OffsetNumber *deletable, int ndeletable, BTVacuumPosting *updatable, int nupdatable)
 
void _bt_delitems_delete_check (Relation rel, Buffer buf, Relation heapRel, TM_IndexDeleteOp *delstate)
 
void _bt_pagedel (Relation rel, Buffer leafbuf, BTVacState *vstate)
 
void _bt_pendingfsm_init (Relation rel, BTVacState *vstate, bool cleanuponly)
 
void _bt_pendingfsm_finalize (Relation rel, BTVacState *vstate)
 
BTStack _bt_search (Relation rel, Relation heaprel, BTScanInsert key, Buffer *bufP, int access)
 
Buffer _bt_moveright (Relation rel, Relation heaprel, BTScanInsert key, Buffer buf, bool forupdate, BTStack stack, int access)
 
OffsetNumber _bt_binsrch_insert (Relation rel, BTInsertState insertstate)
 
int32 _bt_compare (Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
 
bool _bt_first (IndexScanDesc scan, ScanDirection dir)
 
bool _bt_next (IndexScanDesc scan, ScanDirection dir)
 
Buffer _bt_get_endpoint (Relation rel, uint32 level, bool rightmost)
 
BTScanInsert _bt_mkscankey (Relation rel, IndexTuple itup)
 
void _bt_freestack (BTStack stack)
 
void _bt_preprocess_array_keys (IndexScanDesc scan)
 
void _bt_start_array_keys (IndexScanDesc scan, ScanDirection dir)
 
bool _bt_advance_array_keys (IndexScanDesc scan, ScanDirection dir)
 
void _bt_mark_array_keys (IndexScanDesc scan)
 
void _bt_restore_array_keys (IndexScanDesc scan)
 
void _bt_preprocess_keys (IndexScanDesc scan)
 
bool _bt_checkkeys (IndexScanDesc scan, IndexTuple tuple, int tupnatts, ScanDirection dir, bool *continuescan, bool requiredMatchedByPrecheck, bool haveFirstMatch)
 
void _bt_killitems (IndexScanDesc scan)
 
BTCycleId _bt_vacuum_cycleid (Relation rel)
 
BTCycleId _bt_start_vacuum (Relation rel)
 
void _bt_end_vacuum (Relation rel)
 
void _bt_end_vacuum_callback (int code, Datum arg)
 
Size BTreeShmemSize (void)
 
void BTreeShmemInit (void)
 
byteabtoptions (Datum reloptions, bool validate)
 
bool btproperty (Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
 
char * btbuildphasename (int64 phasenum)
 
IndexTuple _bt_truncate (Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
 
int _bt_keep_natts_fast (Relation rel, IndexTuple lastleft, IndexTuple firstright)
 
bool _bt_check_natts (Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
 
void _bt_check_third_page (Relation rel, Relation heap, bool needheaptidspace, Page page, IndexTuple newtup)
 
bool _bt_allequalimage (Relation rel, bool debugmessage)
 
bool btvalidate (Oid opclassoid)
 
void btadjustmembers (Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
 
IndexBuildResultbtbuild (Relation heap, Relation index, struct IndexInfo *indexInfo)
 
void _bt_parallel_build_main (dsm_segment *seg, shm_toc *toc)
 

Macro Definition Documentation

◆ BT_IS_POSTING

#define BT_IS_POSTING   0x2000

Definition at line 466 of file nbtree.h.

◆ BT_OFFSET_MASK

#define BT_OFFSET_MASK   0x0FFF

Definition at line 462 of file nbtree.h.

◆ BT_PIVOT_HEAP_TID_ATTR

#define BT_PIVOT_HEAP_TID_ATTR   0x1000

Definition at line 465 of file nbtree.h.

◆ BT_READ

#define BT_READ   BUFFER_LOCK_SHARE

Definition at line 719 of file nbtree.h.

◆ BT_STATUS_OFFSET_MASK

#define BT_STATUS_OFFSET_MASK   0xF000

Definition at line 463 of file nbtree.h.

◆ BT_WRITE

#define BT_WRITE   BUFFER_LOCK_EXCLUSIVE

Definition at line 720 of file nbtree.h.

◆ BTCommuteStrategyNumber

#define BTCommuteStrategyNumber (   strat)    (BTMaxStrategyNumber + 1 - (strat))

Definition at line 685 of file nbtree.h.

◆ BTEQUALIMAGE_PROC

#define BTEQUALIMAGE_PROC   4

Definition at line 710 of file nbtree.h.

◆ BTGetDeduplicateItems

#define BTGetDeduplicateItems (   relation)
Value:
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
relation->rd_rel->relam == BTREE_AM_OID), \
((relation)->rd_options ? \
((BTOptions *) (relation)->rd_options)->deduplicate_items : true))
#define AssertMacro(condition)
Definition: c.h:846

Definition at line 1105 of file nbtree.h.

◆ BTGetFillFactor

#define BTGetFillFactor (   relation)
Value:
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
relation->rd_rel->relam == BTREE_AM_OID), \
(relation)->rd_options ? \
((BTOptions *) (relation)->rd_options)->fillfactor : \
BTREE_DEFAULT_FILLFACTOR)

Definition at line 1097 of file nbtree.h.

◆ BTGetTargetPageFreeSpace

#define BTGetTargetPageFreeSpace (   relation)     (BLCKSZ * (100 - BTGetFillFactor(relation)) / 100)

Definition at line 1103 of file nbtree.h.

◆ BTINRANGE_PROC

#define BTINRANGE_PROC   3

Definition at line 709 of file nbtree.h.

◆ BTMaxItemSize

#define BTMaxItemSize (   page)
Value:
MAXALIGN(sizeof(BTPageOpaqueData))) / 3) - \
MAXALIGN(sizeof(ItemPointerData)))
static Size PageGetPageSize(Page page)
Definition: bufpage.h:273
#define SizeOfPageHeaderData
Definition: bufpage.h:213
#define MAXALIGN_DOWN(LEN)
Definition: c.h:810
#define MAXALIGN(LEN)
Definition: c.h:798

Definition at line 164 of file nbtree.h.

◆ BTMaxItemSizeNoHeapTid

#define BTMaxItemSizeNoHeapTid (   page)
Value:

Definition at line 169 of file nbtree.h.

◆ BTNProcs

#define BTNProcs   5

Definition at line 712 of file nbtree.h.

◆ BTOPTIONS_PROC

#define BTOPTIONS_PROC   5

Definition at line 711 of file nbtree.h.

◆ BTORDER_PROC

#define BTORDER_PROC   1

Definition at line 707 of file nbtree.h.

◆ BTP_DELETED

#define BTP_DELETED   (1 << 2) /* page has been deleted from tree */

Definition at line 78 of file nbtree.h.

◆ BTP_HALF_DEAD

#define BTP_HALF_DEAD   (1 << 4) /* empty, but still in tree */

Definition at line 80 of file nbtree.h.

◆ BTP_HAS_FULLXID

#define BTP_HAS_FULLXID   (1 << 8) /* contains BTDeletedPageData */

Definition at line 84 of file nbtree.h.

◆ BTP_HAS_GARBAGE

#define BTP_HAS_GARBAGE   (1 << 6) /* page has LP_DEAD tuples (deprecated) */

Definition at line 82 of file nbtree.h.

◆ BTP_INCOMPLETE_SPLIT

#define BTP_INCOMPLETE_SPLIT   (1 << 7) /* right sibling's downlink is missing */

Definition at line 83 of file nbtree.h.

◆ BTP_LEAF

#define BTP_LEAF   (1 << 0) /* leaf page, i.e. not internal page */

Definition at line 76 of file nbtree.h.

◆ BTP_META

#define BTP_META   (1 << 3) /* meta-page */

Definition at line 79 of file nbtree.h.

◆ BTP_ROOT

#define BTP_ROOT   (1 << 1) /* root page (has no parent) */

Definition at line 77 of file nbtree.h.

◆ BTP_SPLIT_END

#define BTP_SPLIT_END   (1 << 5) /* rightmost page of split group */

Definition at line 81 of file nbtree.h.

◆ BTPageGetMeta

#define BTPageGetMeta (   p)     ((BTMetaPageData *) PageGetContents(p))

Definition at line 121 of file nbtree.h.

◆ BTPageGetOpaque

#define BTPageGetOpaque (   page)    ((BTPageOpaque) PageGetSpecialPointer(page))

Definition at line 73 of file nbtree.h.

◆ BTREE_DEFAULT_FILLFACTOR

#define BTREE_DEFAULT_FILLFACTOR   90

Definition at line 200 of file nbtree.h.

◆ BTREE_MAGIC

#define BTREE_MAGIC   0x053162 /* magic number in metapage */

Definition at line 149 of file nbtree.h.

◆ BTREE_METAPAGE

#define BTREE_METAPAGE   0 /* first page is meta */

Definition at line 148 of file nbtree.h.

◆ BTREE_MIN_FILLFACTOR

#define BTREE_MIN_FILLFACTOR   10

Definition at line 199 of file nbtree.h.

◆ BTREE_MIN_VERSION

#define BTREE_MIN_VERSION   2 /* minimum supported version */

Definition at line 151 of file nbtree.h.

◆ BTREE_NONLEAF_FILLFACTOR

#define BTREE_NONLEAF_FILLFACTOR   70

Definition at line 201 of file nbtree.h.

◆ BTREE_NOVAC_VERSION

#define BTREE_NOVAC_VERSION   3 /* version with all meta fields set */

Definition at line 152 of file nbtree.h.

◆ BTREE_SINGLEVAL_FILLFACTOR

#define BTREE_SINGLEVAL_FILLFACTOR   96

Definition at line 202 of file nbtree.h.

◆ BTREE_VERSION

#define BTREE_VERSION   4 /* current version number */

Definition at line 150 of file nbtree.h.

◆ BTreeTupleGetNAtts

#define BTreeTupleGetNAtts (   itup,
  rel 
)
Value:
( \
(BTreeTupleIsPivot(itup)) ? \
( \
) \
: \
)
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:114
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:480
#define BT_OFFSET_MASK
Definition: nbtree.h:462
#define IndexRelationGetNumberOfAttributes(relation)
Definition: rel.h:517

Definition at line 577 of file nbtree.h.

◆ BTScanPosInvalidate

#define BTScanPosInvalidate (   scanpos)
Value:
do { \
(scanpos).currPage = InvalidBlockNumber; \
(scanpos).nextPage = InvalidBlockNumber; \
(scanpos).buf = InvalidBuffer; \
(scanpos).lsn = InvalidXLogRecPtr; \
(scanpos).nextTupleOffset = 0; \
} while (0)
#define InvalidBlockNumber
Definition: block.h:33
#define InvalidBuffer
Definition: buf.h:25
static char * buf
Definition: pg_test_fsync.c:73
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

Definition at line 1013 of file nbtree.h.

◆ BTScanPosIsPinned

#define BTScanPosIsPinned (   scanpos)
Value:
( \
AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
!BufferIsValid((scanpos).buf)), \
BufferIsValid((scanpos).buf) \
)
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:301

Definition at line 990 of file nbtree.h.

◆ BTScanPosIsValid

#define BTScanPosIsValid (   scanpos)
Value:
( \
AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
!BufferIsValid((scanpos).buf)), \
BlockNumberIsValid((scanpos).currPage) \
)

Definition at line 1007 of file nbtree.h.

◆ BTScanPosUnpin

#define BTScanPosUnpin (   scanpos)
Value:
do { \
ReleaseBuffer((scanpos).buf); \
(scanpos).buf = InvalidBuffer; \
} while (0)

Definition at line 996 of file nbtree.h.

◆ BTScanPosUnpinIfPinned

#define BTScanPosUnpinIfPinned (   scanpos)
Value:
do { \
if (BTScanPosIsPinned(scanpos)) \
BTScanPosUnpin(scanpos); \
} while (0)
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:990

Definition at line 1001 of file nbtree.h.

◆ BTSORTSUPPORT_PROC

#define BTSORTSUPPORT_PROC   2

Definition at line 708 of file nbtree.h.

◆ INDEX_ALT_TID_MASK

#define INDEX_ALT_TID_MASK   INDEX_AM_RESERVED_BIT

Definition at line 459 of file nbtree.h.

◆ MAX_BT_CYCLE_ID

#define MAX_BT_CYCLE_ID   0xFF7F

Definition at line 93 of file nbtree.h.

◆ MaxTIDsPerBTreePage

#define MaxTIDsPerBTreePage
Value:
(int) ((BLCKSZ - SizeOfPageHeaderData - sizeof(BTPageOpaqueData)) / \
sizeof(ItemPointerData))

Definition at line 185 of file nbtree.h.

◆ P_FIRSTDATAKEY

#define P_FIRSTDATAKEY (   opaque)    (P_RIGHTMOST(opaque) ? P_HIKEY : P_FIRSTKEY)

Definition at line 369 of file nbtree.h.

◆ P_FIRSTKEY

#define P_FIRSTKEY   ((OffsetNumber) 2)

Definition at line 368 of file nbtree.h.

◆ P_HAS_FULLXID

#define P_HAS_FULLXID (   opaque)    (((opaque)->btpo_flags & BTP_HAS_FULLXID) != 0)

Definition at line 228 of file nbtree.h.

◆ P_HAS_GARBAGE

#define P_HAS_GARBAGE (   opaque)    (((opaque)->btpo_flags & BTP_HAS_GARBAGE) != 0)

Definition at line 226 of file nbtree.h.

◆ P_HIKEY

#define P_HIKEY   ((OffsetNumber) 1)

Definition at line 367 of file nbtree.h.

◆ P_IGNORE

#define P_IGNORE (   opaque)    (((opaque)->btpo_flags & (BTP_DELETED|BTP_HALF_DEAD)) != 0)

Definition at line 225 of file nbtree.h.

◆ P_INCOMPLETE_SPLIT

#define P_INCOMPLETE_SPLIT (   opaque)    (((opaque)->btpo_flags & BTP_INCOMPLETE_SPLIT) != 0)

Definition at line 227 of file nbtree.h.

◆ P_ISDELETED

#define P_ISDELETED (   opaque)    (((opaque)->btpo_flags & BTP_DELETED) != 0)

Definition at line 222 of file nbtree.h.

◆ P_ISHALFDEAD

#define P_ISHALFDEAD (   opaque)    (((opaque)->btpo_flags & BTP_HALF_DEAD) != 0)

Definition at line 224 of file nbtree.h.

◆ P_ISLEAF

#define P_ISLEAF (   opaque)    (((opaque)->btpo_flags & BTP_LEAF) != 0)

Definition at line 220 of file nbtree.h.

◆ P_ISMETA

#define P_ISMETA (   opaque)    (((opaque)->btpo_flags & BTP_META) != 0)

Definition at line 223 of file nbtree.h.

◆ P_ISROOT

#define P_ISROOT (   opaque)    (((opaque)->btpo_flags & BTP_ROOT) != 0)

Definition at line 221 of file nbtree.h.

◆ P_LEFTMOST

#define P_LEFTMOST (   opaque)    ((opaque)->btpo_prev == P_NONE)

Definition at line 218 of file nbtree.h.

◆ P_NONE

#define P_NONE   0

Definition at line 212 of file nbtree.h.

◆ P_RIGHTMOST

#define P_RIGHTMOST (   opaque)    ((opaque)->btpo_next == P_NONE)

Definition at line 219 of file nbtree.h.

◆ PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN

#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN   2

Definition at line 1116 of file nbtree.h.

◆ PROGRESS_BTREE_PHASE_LEAF_LOAD

#define PROGRESS_BTREE_PHASE_LEAF_LOAD   5

Definition at line 1119 of file nbtree.h.

◆ PROGRESS_BTREE_PHASE_PERFORMSORT_1

#define PROGRESS_BTREE_PHASE_PERFORMSORT_1   3

Definition at line 1117 of file nbtree.h.

◆ PROGRESS_BTREE_PHASE_PERFORMSORT_2

#define PROGRESS_BTREE_PHASE_PERFORMSORT_2   4

Definition at line 1118 of file nbtree.h.

◆ SK_BT_DESC

#define SK_BT_DESC   (INDOPTION_DESC << SK_BT_INDOPTION_SHIFT)

Definition at line 1086 of file nbtree.h.

◆ SK_BT_INDOPTION_SHIFT

#define SK_BT_INDOPTION_SHIFT   24 /* must clear the above bits */

Definition at line 1085 of file nbtree.h.

◆ SK_BT_NULLS_FIRST

#define SK_BT_NULLS_FIRST   (INDOPTION_NULLS_FIRST << SK_BT_INDOPTION_SHIFT)

Definition at line 1087 of file nbtree.h.

◆ SK_BT_REQBKWD

#define SK_BT_REQBKWD   0x00020000 /* required to continue backward scan */

Definition at line 1084 of file nbtree.h.

◆ SK_BT_REQFWD

#define SK_BT_REQFWD   0x00010000 /* required to continue forward scan */

Definition at line 1083 of file nbtree.h.

Typedef Documentation

◆ BTArrayKeyInfo

◆ BTCycleId

typedef uint16 BTCycleId

Definition at line 29 of file nbtree.h.

◆ BTDedupInterval

◆ BTDedupState

Definition at line 893 of file nbtree.h.

◆ BTDedupStateData

◆ BTDeletedPageData

◆ BTInsertState

Definition at line 835 of file nbtree.h.

◆ BTInsertStateData

◆ BTMetaPageData

◆ BTOptions

typedef struct BTOptions BTOptions

◆ BTPageOpaque

Definition at line 71 of file nbtree.h.

◆ BTPageOpaqueData

◆ BTPendingFSM

typedef struct BTPendingFSM BTPendingFSM

◆ BTScanInsert

Definition at line 796 of file nbtree.h.

◆ BTScanInsertData

◆ BTScanOpaque

Definition at line 1076 of file nbtree.h.

◆ BTScanOpaqueData

◆ BTScanPos

Definition at line 988 of file nbtree.h.

◆ BTScanPosData

typedef struct BTScanPosData BTScanPosData

◆ BTScanPosItem

typedef struct BTScanPosItem BTScanPosItem

◆ BTStack

typedef BTStackData* BTStack

Definition at line 739 of file nbtree.h.

◆ BTStackData

typedef struct BTStackData BTStackData

◆ BTVacState

typedef struct BTVacState BTVacState

◆ BTVacuumPosting

Definition at line 914 of file nbtree.h.

◆ BTVacuumPostingData

Function Documentation

◆ _bt_advance_array_keys()

bool _bt_advance_array_keys ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 544 of file nbtutils.c.

545 {
546  BTScanOpaque so = (BTScanOpaque) scan->opaque;
547  bool found = false;
548  int i;
549 
550  /*
551  * We must advance the last array key most quickly, since it will
552  * correspond to the lowest-order index column among the available
553  * qualifications. This is necessary to ensure correct ordering of output
554  * when there are multiple array keys.
555  */
556  for (i = so->numArrayKeys - 1; i >= 0; i--)
557  {
558  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
559  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
560  int cur_elem = curArrayKey->cur_elem;
561  int num_elems = curArrayKey->num_elems;
562 
563  if (ScanDirectionIsBackward(dir))
564  {
565  if (--cur_elem < 0)
566  {
567  cur_elem = num_elems - 1;
568  found = false; /* need to advance next array key */
569  }
570  else
571  found = true;
572  }
573  else
574  {
575  if (++cur_elem >= num_elems)
576  {
577  cur_elem = 0;
578  found = false; /* need to advance next array key */
579  }
580  else
581  found = true;
582  }
583 
584  curArrayKey->cur_elem = cur_elem;
585  skey->sk_argument = curArrayKey->elem_values[cur_elem];
586  if (found)
587  break;
588  }
589 
590  /* advance parallel scan */
591  if (scan->parallel_scan != NULL)
593 
594  /*
595  * When no new array keys were found, the scan is "past the end" of the
596  * array keys. _bt_start_array_keys can still "restart" the array keys if
597  * a rescan is required.
598  */
599  if (!found)
600  so->arraysStarted = false;
601 
602  return found;
603 }
int i
Definition: isn.c:73
void _bt_parallel_advance_array_keys(IndexScanDesc scan)
Definition: nbtree.c:746
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:1076
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:50
Datum * elem_values
Definition: nbtree.h:1029
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:1047
bool arraysStarted
Definition: nbtree.h:1041
ScanKey arrayKeyData
Definition: nbtree.h:1040
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:166
Datum sk_argument
Definition: skey.h:72

References _bt_parallel_advance_array_keys(), BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, BTScanOpaqueData::arraysStarted, BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, IndexScanDescData::parallel_scan, BTArrayKeyInfo::scan_key, ScanDirectionIsBackward, and ScanKeyData::sk_argument.

Referenced by btgetbitmap(), and btgettuple().

◆ _bt_allequalimage()

bool _bt_allequalimage ( Relation  rel,
bool  debugmessage 
)

Definition at line 2740 of file nbtutils.c.

2741 {
2742  bool allequalimage = true;
2743 
2744  /* INCLUDE indexes can never support deduplication */
2747  return false;
2748 
2749  for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(rel); i++)
2750  {
2751  Oid opfamily = rel->rd_opfamily[i];
2752  Oid opcintype = rel->rd_opcintype[i];
2753  Oid collation = rel->rd_indcollation[i];
2754  Oid equalimageproc;
2755 
2756  equalimageproc = get_opfamily_proc(opfamily, opcintype, opcintype,
2758 
2759  /*
2760  * If there is no BTEQUALIMAGE_PROC then deduplication is assumed to
2761  * be unsafe. Otherwise, actually call proc and see what it says.
2762  */
2763  if (!OidIsValid(equalimageproc) ||
2764  !DatumGetBool(OidFunctionCall1Coll(equalimageproc, collation,
2765  ObjectIdGetDatum(opcintype))))
2766  {
2767  allequalimage = false;
2768  break;
2769  }
2770  }
2771 
2772  if (debugmessage)
2773  {
2774  if (allequalimage)
2775  elog(DEBUG1, "index \"%s\" can safely use deduplication",
2777  else
2778  elog(DEBUG1, "index \"%s\" cannot use deduplication",
2780  }
2781 
2782  return allequalimage;
2783 }
#define OidIsValid(objectId)
Definition: c.h:762
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition: fmgr.c:1411
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
#define BTEQUALIMAGE_PROC
Definition: nbtree.h:710
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:524
Oid * rd_opcintype
Definition: rel.h:208
Oid * rd_opfamily
Definition: rel.h:207
Oid * rd_indcollation
Definition: rel.h:217

References BTEQUALIMAGE_PROC, DatumGetBool(), DEBUG1, elog, get_opfamily_proc(), i, IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, ObjectIdGetDatum(), OidFunctionCall1Coll(), OidIsValid, RelationData::rd_indcollation, RelationData::rd_opcintype, RelationData::rd_opfamily, and RelationGetRelationName.

Referenced by _bt_leafbuild(), bt_index_check_internal(), and btbuildempty().

◆ _bt_allocbuf()

Buffer _bt_allocbuf ( Relation  rel,
Relation  heaprel 
)

Definition at line 869 of file nbtpage.c.

870 {
871  Buffer buf;
872  BlockNumber blkno;
873  Page page;
874 
875  Assert(heaprel != NULL);
876 
877  /*
878  * First see if the FSM knows of any free pages.
879  *
880  * We can't trust the FSM's report unreservedly; we have to check that the
881  * page is still free. (For example, an already-free page could have been
882  * re-used between the time the last VACUUM scanned it and the time the
883  * VACUUM made its FSM updates.)
884  *
885  * In fact, it's worse than that: we can't even assume that it's safe to
886  * take a lock on the reported page. If somebody else has a lock on it,
887  * or even worse our own caller does, we could deadlock. (The own-caller
888  * scenario is actually not improbable. Consider an index on a serial or
889  * timestamp column. Nearly all splits will be at the rightmost page, so
890  * it's entirely likely that _bt_split will call us while holding a lock
891  * on the page most recently acquired from FSM. A VACUUM running
892  * concurrently with the previous split could well have placed that page
893  * back in FSM.)
894  *
895  * To get around that, we ask for only a conditional lock on the reported
896  * page. If we fail, then someone else is using the page, and we may
897  * reasonably assume it's not free. (If we happen to be wrong, the worst
898  * consequence is the page will be lost to use till the next VACUUM, which
899  * is no big problem.)
900  */
901  for (;;)
902  {
903  blkno = GetFreeIndexPage(rel);
904  if (blkno == InvalidBlockNumber)
905  break;
906  buf = ReadBuffer(rel, blkno);
907  if (_bt_conditionallockbuf(rel, buf))
908  {
909  page = BufferGetPage(buf);
910 
911  /*
912  * It's possible to find an all-zeroes page in an index. For
913  * example, a backend might successfully extend the relation one
914  * page and then crash before it is able to make a WAL entry for
915  * adding the page. If we find a zeroed page then reclaim it
916  * immediately.
917  */
918  if (PageIsNew(page))
919  {
920  /* Okay to use page. Initialize and return it. */
922  return buf;
923  }
924 
925  if (BTPageIsRecyclable(page, heaprel))
926  {
927  /*
928  * If we are generating WAL for Hot Standby then create a WAL
929  * record that will allow us to conflict with queries running
930  * on standby, in case they have snapshots older than safexid
931  * value
932  */
934  {
935  xl_btree_reuse_page xlrec_reuse;
936 
937  /*
938  * Note that we don't register the buffer with the record,
939  * because this operation doesn't modify the page (that
940  * already happened, back when VACUUM deleted the page).
941  * This record only exists to provide a conflict point for
942  * Hot Standby. See record REDO routine comments.
943  */
944  xlrec_reuse.locator = rel->rd_locator;
945  xlrec_reuse.block = blkno;
946  xlrec_reuse.snapshotConflictHorizon = BTPageGetDeleteXid(page);
947  xlrec_reuse.isCatalogRel =
949 
950  XLogBeginInsert();
951  XLogRegisterData((char *) &xlrec_reuse, SizeOfBtreeReusePage);
952 
953  XLogInsert(RM_BTREE_ID, XLOG_BTREE_REUSE_PAGE);
954  }
955 
956  /* Okay to use page. Re-initialize and return it. */
958  return buf;
959  }
960  elog(DEBUG2, "FSM returned nonrecyclable page");
961  _bt_relbuf(rel, buf);
962  }
963  else
964  {
965  elog(DEBUG2, "FSM returned nonlockable page");
966  /* couldn't get lock, so just drop pin */
968  }
969  }
970 
971  /*
972  * Extend the relation by one page. Need to use RBM_ZERO_AND_LOCK or we
973  * risk a race condition against btvacuumscan --- see comments therein.
974  * This forces us to repeat the valgrind request that _bt_lockbuf()
975  * otherwise would make, as we can't use _bt_lockbuf() without introducing
976  * a race.
977  */
979  if (!RelationUsesLocalBuffers(rel))
981 
982  /* Initialize the new page before returning it */
983  page = BufferGetPage(buf);
984  Assert(PageIsNew(page));
986 
987  return buf;
988 }
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:838
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4560
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:734
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:339
@ EB_LOCK_FIRST
Definition: bufmgr.h:85
#define BMR_REL(p_rel)
Definition: bufmgr.h:106
Pointer Page
Definition: bufpage.h:78
static bool PageIsNew(Page page)
Definition: bufpage.h:230
#define DEBUG2
Definition: elog.h:29
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
Assert(fmt[strlen(fmt) - 1] !='\n')
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1023
void _bt_pageinit(Page page, Size size)
Definition: nbtpage.c:1129
bool _bt_conditionallockbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1093
static FullTransactionId BTPageGetDeleteXid(Page page)
Definition: nbtree.h:260
static bool BTPageIsRecyclable(Page page, Relation heaprel)
Definition: nbtree.h:291
#define XLOG_BTREE_REUSE_PAGE
Definition: nbtxlog.h:40
#define SizeOfBtreeReusePage
Definition: nbtxlog.h:192
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:684
#define RelationNeedsWAL(relation)
Definition: rel.h:628
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:637
@ MAIN_FORKNUM
Definition: relpath.h:50
RelFileLocator rd_locator
Definition: rel.h:57
FullTransactionId snapshotConflictHorizon
Definition: nbtxlog.h:187
RelFileLocator locator
Definition: nbtxlog.h:185
BlockNumber block
Definition: nbtxlog.h:186
#define XLogStandbyInfoActive()
Definition: xlog.h:121
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:364
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogBeginInsert(void)
Definition: xloginsert.c:149

References _bt_conditionallockbuf(), _bt_pageinit(), _bt_relbuf(), Assert(), xl_btree_reuse_page::block, BMR_REL, BTPageGetDeleteXid(), BTPageIsRecyclable(), buf, BufferGetPage(), BufferGetPageSize(), DEBUG2, EB_LOCK_FIRST, elog, ExtendBufferedRel(), GetFreeIndexPage(), InvalidBlockNumber, xl_btree_reuse_page::isCatalogRel, xl_btree_reuse_page::locator, MAIN_FORKNUM, PageIsNew(), RelationData::rd_locator, ReadBuffer(), RelationIsAccessibleInLogicalDecoding, RelationNeedsWAL, RelationUsesLocalBuffers, ReleaseBuffer(), SizeOfBtreeReusePage, xl_btree_reuse_page::snapshotConflictHorizon, VALGRIND_MAKE_MEM_DEFINED, XLOG_BTREE_REUSE_PAGE, XLogBeginInsert(), XLogInsert(), XLogRegisterData(), and XLogStandbyInfoActive.

Referenced by _bt_getroot(), _bt_newlevel(), and _bt_split().

◆ _bt_binsrch_insert()

OffsetNumber _bt_binsrch_insert ( Relation  rel,
BTInsertState  insertstate 
)

Definition at line 468 of file nbtsearch.c.

469 {
470  BTScanInsert key = insertstate->itup_key;
471  Page page;
472  BTPageOpaque opaque;
473  OffsetNumber low,
474  high,
475  stricthigh;
476  int32 result,
477  cmpval;
478 
479  page = BufferGetPage(insertstate->buf);
480  opaque = BTPageGetOpaque(page);
481 
482  Assert(P_ISLEAF(opaque));
483  Assert(!key->nextkey);
484  Assert(insertstate->postingoff == 0);
485 
486  if (!insertstate->bounds_valid)
487  {
488  /* Start new binary search */
489  low = P_FIRSTDATAKEY(opaque);
490  high = PageGetMaxOffsetNumber(page);
491  }
492  else
493  {
494  /* Restore result of previous binary search against same page */
495  low = insertstate->low;
496  high = insertstate->stricthigh;
497  }
498 
499  /* If there are no keys on the page, return the first available slot */
500  if (unlikely(high < low))
501  {
502  /* Caller can't reuse bounds */
503  insertstate->low = InvalidOffsetNumber;
504  insertstate->stricthigh = InvalidOffsetNumber;
505  insertstate->bounds_valid = false;
506  return low;
507  }
508 
509  /*
510  * Binary search to find the first key on the page >= scan key. (nextkey
511  * is always false when inserting).
512  *
513  * The loop invariant is: all slots before 'low' are < scan key, all slots
514  * at or after 'high' are >= scan key. 'stricthigh' is > scan key, and is
515  * maintained to save additional search effort for caller.
516  *
517  * We can fall out when high == low.
518  */
519  if (!insertstate->bounds_valid)
520  high++; /* establish the loop invariant for high */
521  stricthigh = high; /* high initially strictly higher */
522 
523  cmpval = 1; /* !nextkey comparison value */
524 
525  while (high > low)
526  {
527  OffsetNumber mid = low + ((high - low) / 2);
528 
529  /* We have low <= mid < high, so mid points at a real slot */
530 
531  result = _bt_compare(rel, key, page, mid);
532 
533  if (result >= cmpval)
534  low = mid + 1;
535  else
536  {
537  high = mid;
538  if (result != 0)
539  stricthigh = high;
540  }
541 
542  /*
543  * If tuple at offset located by binary search is a posting list whose
544  * TID range overlaps with caller's scantid, perform posting list
545  * binary search to set postingoff for caller. Caller must split the
546  * posting list when postingoff is set. This should happen
547  * infrequently.
548  */
549  if (unlikely(result == 0 && key->scantid != NULL))
550  {
551  /*
552  * postingoff should never be set more than once per leaf page
553  * binary search. That would mean that there are duplicate table
554  * TIDs in the index, which is never okay. Check for that here.
555  */
556  if (insertstate->postingoff != 0)
557  ereport(ERROR,
558  (errcode(ERRCODE_INDEX_CORRUPTED),
559  errmsg_internal("table tid from new index tuple (%u,%u) cannot find insert offset between offsets %u and %u of block %u in index \"%s\"",
560  ItemPointerGetBlockNumber(key->scantid),
562  low, stricthigh,
563  BufferGetBlockNumber(insertstate->buf),
564  RelationGetRelationName(rel))));
565 
566  insertstate->postingoff = _bt_binsrch_posting(key, page, mid);
567  }
568  }
569 
570  /*
571  * On a leaf page, a binary search always returns the first key >= scan
572  * key (at least in !nextkey case), which could be the last slot + 1. This
573  * is also the lower bound of cached search.
574  *
575  * stricthigh may also be the last slot + 1, which prevents caller from
576  * using bounds directly, but is still useful to us if we're called a
577  * second time with cached bounds (cached low will be < stricthigh when
578  * that happens).
579  */
580  insertstate->low = low;
581  insertstate->stricthigh = stricthigh;
582  insertstate->bounds_valid = true;
583 
584  return low;
585 }
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3377
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
signed int int32
Definition: c.h:481
#define unlikely(x)
Definition: c.h:298
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errcode(int sqlerrcode)
Definition: elog.c:859
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
#define P_ISLEAF(opaque)
Definition: nbtree.h:220
#define BTPageGetOpaque(page)
Definition: nbtree.h:73
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:369
static int _bt_binsrch_posting(BTScanInsert key, Page page, OffsetNumber offnum)
Definition: nbtsearch.c:596
int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
Definition: nbtsearch.c:682
#define InvalidOffsetNumber
Definition: off.h:26
uint16 OffsetNumber
Definition: off.h:24
OffsetNumber stricthigh
Definition: nbtree.h:825
bool bounds_valid
Definition: nbtree.h:823
OffsetNumber low
Definition: nbtree.h:824
BTScanInsert itup_key
Definition: nbtree.h:813

References _bt_binsrch_posting(), _bt_compare(), Assert(), BTInsertStateData::bounds_valid, BTPageGetOpaque, BTInsertStateData::buf, BufferGetBlockNumber(), BufferGetPage(), ereport, errcode(), errmsg_internal(), ERROR, InvalidOffsetNumber, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), BTInsertStateData::itup_key, sort-test::key, BTInsertStateData::low, P_FIRSTDATAKEY, P_ISLEAF, PageGetMaxOffsetNumber(), BTInsertStateData::postingoff, RelationGetRelationName, BTInsertStateData::stricthigh, and unlikely.

Referenced by _bt_check_unique(), _bt_findinsertloc(), and bt_rootdescend().

◆ _bt_bottomupdel_pass()

bool _bt_bottomupdel_pass ( Relation  rel,
Buffer  buf,
Relation  heapRel,
Size  newitemsz 
)

Definition at line 307 of file nbtdedup.c.

309 {
310  OffsetNumber offnum,
311  minoff,
312  maxoff;
313  Page page = BufferGetPage(buf);
314  BTPageOpaque opaque = BTPageGetOpaque(page);
316  TM_IndexDeleteOp delstate;
317  bool neverdedup;
318  int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
319 
320  /* Passed-in newitemsz is MAXALIGNED but does not include line pointer */
321  newitemsz += sizeof(ItemIdData);
322 
323  /* Initialize deduplication state */
325  state->deduplicate = true;
326  state->nmaxitems = 0;
327  state->maxpostingsize = BLCKSZ; /* We're not really deduplicating */
328  state->base = NULL;
329  state->baseoff = InvalidOffsetNumber;
330  state->basetupsize = 0;
331  state->htids = palloc(state->maxpostingsize);
332  state->nhtids = 0;
333  state->nitems = 0;
334  state->phystupsize = 0;
335  state->nintervals = 0;
336 
337  /*
338  * Initialize tableam state that describes bottom-up index deletion
339  * operation.
340  *
341  * We'll go on to ask the tableam to search for TIDs whose index tuples we
342  * can safely delete. The tableam will search until our leaf page space
343  * target is satisfied, or until the cost of continuing with the tableam
344  * operation seems too high. It focuses its efforts on TIDs associated
345  * with duplicate index tuples that we mark "promising".
346  *
347  * This space target is a little arbitrary. The tableam must be able to
348  * keep the costs and benefits in balance. We provide the tableam with
349  * exhaustive information about what might work, without directly
350  * concerning ourselves with avoiding work during the tableam call. Our
351  * role in costing the bottom-up deletion process is strictly advisory.
352  */
353  delstate.irel = rel;
354  delstate.iblknum = BufferGetBlockNumber(buf);
355  delstate.bottomup = true;
356  delstate.bottomupfreespace = Max(BLCKSZ / 16, newitemsz);
357  delstate.ndeltids = 0;
358  delstate.deltids = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
359  delstate.status = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
360 
361  minoff = P_FIRSTDATAKEY(opaque);
362  maxoff = PageGetMaxOffsetNumber(page);
363  for (offnum = minoff;
364  offnum <= maxoff;
365  offnum = OffsetNumberNext(offnum))
366  {
367  ItemId itemid = PageGetItemId(page, offnum);
368  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
369 
370  Assert(!ItemIdIsDead(itemid));
371 
372  if (offnum == minoff)
373  {
374  /* itup starts first pending interval */
375  _bt_dedup_start_pending(state, itup, offnum);
376  }
377  else if (_bt_keep_natts_fast(rel, state->base, itup) > nkeyatts &&
378  _bt_dedup_save_htid(state, itup))
379  {
380  /* Tuple is equal; just added its TIDs to pending interval */
381  }
382  else
383  {
384  /* Finalize interval -- move its TIDs to delete state */
385  _bt_bottomupdel_finish_pending(page, state, &delstate);
386 
387  /* itup starts new pending interval */
388  _bt_dedup_start_pending(state, itup, offnum);
389  }
390  }
391  /* Finalize final interval -- move its TIDs to delete state */
392  _bt_bottomupdel_finish_pending(page, state, &delstate);
393 
394  /*
395  * We don't give up now in the event of having few (or even zero)
396  * promising tuples for the tableam because it's not up to us as the index
397  * AM to manage costs (note that the tableam might have heuristics of its
398  * own that work out what to do). We should at least avoid having our
399  * caller do a useless deduplication pass after we return in the event of
400  * zero promising tuples, though.
401  */
402  neverdedup = false;
403  if (state->nintervals == 0)
404  neverdedup = true;
405 
406  pfree(state->htids);
407  pfree(state);
408 
409  /* Ask tableam which TIDs are deletable, then physically delete them */
410  _bt_delitems_delete_check(rel, buf, heapRel, &delstate);
411 
412  pfree(delstate.deltids);
413  pfree(delstate.status);
414 
415  /* Report "success" to caller unconditionally to avoid deduplication */
416  if (neverdedup)
417  return true;
418 
419  /* Don't dedup when we won't end up back here any time soon anyway */
420  return PageGetExactFreeSpace(page) >= Max(BLCKSZ / 24, newitemsz);
421 }
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:958
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:351
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
#define Max(x, y)
Definition: c.h:985
struct ItemIdData ItemIdData
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
IndexTupleData * IndexTuple
Definition: itup.h:53
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc(Size size)
Definition: mcxt.c:1304
bool _bt_dedup_save_htid(BTDedupState state, IndexTuple itup)
Definition: nbtdedup.c:484
void _bt_dedup_start_pending(BTDedupState state, IndexTuple base, OffsetNumber baseoff)
Definition: nbtdedup.c:433
static void _bt_bottomupdel_finish_pending(Page page, BTDedupState state, TM_IndexDeleteOp *delstate)
Definition: nbtdedup.c:648
void _bt_delitems_delete_check(Relation rel, Buffer buf, Relation heapRel, TM_IndexDeleteOp *delstate)
Definition: nbtpage.c:1513
#define MaxTIDsPerBTreePage
Definition: nbtree.h:185
BTDedupStateData * BTDedupState
Definition: nbtree.h:893
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
Definition: nbtutils.c:2475
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
TM_IndexStatus * status
Definition: tableam.h:247
int bottomupfreespace
Definition: tableam.h:242
Relation irel
Definition: tableam.h:239
TM_IndexDelete * deltids
Definition: tableam.h:246
BlockNumber iblknum
Definition: tableam.h:240
Definition: regguts.h:323

References _bt_bottomupdel_finish_pending(), _bt_dedup_save_htid(), _bt_dedup_start_pending(), _bt_delitems_delete_check(), _bt_keep_natts_fast(), Assert(), TM_IndexDeleteOp::bottomup, TM_IndexDeleteOp::bottomupfreespace, BTPageGetOpaque, buf, BufferGetBlockNumber(), BufferGetPage(), TM_IndexDeleteOp::deltids, TM_IndexDeleteOp::iblknum, IndexRelationGetNumberOfKeyAttributes, InvalidOffsetNumber, TM_IndexDeleteOp::irel, ItemIdIsDead, Max, MaxTIDsPerBTreePage, TM_IndexDeleteOp::ndeltids, OffsetNumberNext, P_FIRSTDATAKEY, PageGetExactFreeSpace(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), palloc(), pfree(), and TM_IndexDeleteOp::status.

Referenced by _bt_delete_or_dedup_one_page().

◆ _bt_check_natts()

bool _bt_check_natts ( Relation  rel,
bool  heapkeyspace,
Page  page,
OffsetNumber  offnum 
)

Definition at line 2522 of file nbtutils.c.

2523 {
2526  BTPageOpaque opaque = BTPageGetOpaque(page);
2527  IndexTuple itup;
2528  int tupnatts;
2529 
2530  /*
2531  * We cannot reliably test a deleted or half-dead page, since they have
2532  * dummy high keys
2533  */
2534  if (P_IGNORE(opaque))
2535  return true;
2536 
2537  Assert(offnum >= FirstOffsetNumber &&
2538  offnum <= PageGetMaxOffsetNumber(page));
2539 
2540  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2541  tupnatts = BTreeTupleGetNAtts(itup, rel);
2542 
2543  /* !heapkeyspace indexes do not support deduplication */
2544  if (!heapkeyspace && BTreeTupleIsPosting(itup))
2545  return false;
2546 
2547  /* Posting list tuples should never have "pivot heap TID" bit set */
2548  if (BTreeTupleIsPosting(itup) &&
2550  BT_PIVOT_HEAP_TID_ATTR) != 0)
2551  return false;
2552 
2553  /* INCLUDE indexes do not support deduplication */
2554  if (natts != nkeyatts && BTreeTupleIsPosting(itup))
2555  return false;
2556 
2557  if (P_ISLEAF(opaque))
2558  {
2559  if (offnum >= P_FIRSTDATAKEY(opaque))
2560  {
2561  /*
2562  * Non-pivot tuple should never be explicitly marked as a pivot
2563  * tuple
2564  */
2565  if (BTreeTupleIsPivot(itup))
2566  return false;
2567 
2568  /*
2569  * Leaf tuples that are not the page high key (non-pivot tuples)
2570  * should never be truncated. (Note that tupnatts must have been
2571  * inferred, even with a posting list tuple, because only pivot
2572  * tuples store tupnatts directly.)
2573  */
2574  return tupnatts == natts;
2575  }
2576  else
2577  {
2578  /*
2579  * Rightmost page doesn't contain a page high key, so tuple was
2580  * checked above as ordinary leaf tuple
2581  */
2582  Assert(!P_RIGHTMOST(opaque));
2583 
2584  /*
2585  * !heapkeyspace high key tuple contains only key attributes. Note
2586  * that tupnatts will only have been explicitly represented in
2587  * !heapkeyspace indexes that happen to have non-key attributes.
2588  */
2589  if (!heapkeyspace)
2590  return tupnatts == nkeyatts;
2591 
2592  /* Use generic heapkeyspace pivot tuple handling */
2593  }
2594  }
2595  else /* !P_ISLEAF(opaque) */
2596  {
2597  if (offnum == P_FIRSTDATAKEY(opaque))
2598  {
2599  /*
2600  * The first tuple on any internal page (possibly the first after
2601  * its high key) is its negative infinity tuple. Negative
2602  * infinity tuples are always truncated to zero attributes. They
2603  * are a particular kind of pivot tuple.
2604  */
2605  if (heapkeyspace)
2606  return tupnatts == 0;
2607 
2608  /*
2609  * The number of attributes won't be explicitly represented if the
2610  * negative infinity tuple was generated during a page split that
2611  * occurred with a version of Postgres before v11. There must be
2612  * a problem when there is an explicit representation that is
2613  * non-zero, or when there is no explicit representation and the
2614  * tuple is evidently not a pre-pg_upgrade tuple.
2615  *
2616  * Prior to v11, downlinks always had P_HIKEY as their offset.
2617  * Accept that as an alternative indication of a valid
2618  * !heapkeyspace negative infinity tuple.
2619  */
2620  return tupnatts == 0 ||
2622  }
2623  else
2624  {
2625  /*
2626  * !heapkeyspace downlink tuple with separator key contains only
2627  * key attributes. Note that tupnatts will only have been
2628  * explicitly represented in !heapkeyspace indexes that happen to
2629  * have non-key attributes.
2630  */
2631  if (!heapkeyspace)
2632  return tupnatts == nkeyatts;
2633 
2634  /* Use generic heapkeyspace pivot tuple handling */
2635  }
2636  }
2637 
2638  /* Handle heapkeyspace pivot tuples (excluding minus infinity items) */
2639  Assert(heapkeyspace);
2640 
2641  /*
2642  * Explicit representation of the number of attributes is mandatory with
2643  * heapkeyspace index pivot tuples, regardless of whether or not there are
2644  * non-key attributes.
2645  */
2646  if (!BTreeTupleIsPivot(itup))
2647  return false;
2648 
2649  /* Pivot tuple should not use posting list representation (redundant) */
2650  if (BTreeTupleIsPosting(itup))
2651  return false;
2652 
2653  /*
2654  * Heap TID is a tiebreaker key attribute, so it cannot be untruncated
2655  * when any other key attribute is truncated
2656  */
2657  if (BTreeTupleGetHeapTID(itup) != NULL && tupnatts != nkeyatts)
2658  return false;
2659 
2660  /*
2661  * Pivot tuple must have at least one untruncated key attribute (minus
2662  * infinity pivot tuples are the only exception). Pivot tuples can never
2663  * represent that there is a value present for a key attribute that
2664  * exceeds pg_index.indnkeyatts for the index.
2665  */
2666  return tupnatts > 0 && tupnatts <= nkeyatts;
2667 }
signed short int16
Definition: c.h:480
#define BT_PIVOT_HEAP_TID_ATTR
Definition: nbtree.h:465
#define P_HIKEY
Definition: nbtree.h:367
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:219
#define P_IGNORE(opaque)
Definition: nbtree.h:225
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:492
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
Definition: nbtree.h:638
#define BTreeTupleGetNAtts(itup, rel)
Definition: nbtree.h:577
#define FirstOffsetNumber
Definition: off.h:27
ItemPointerData t_tid
Definition: itup.h:37

References Assert(), BT_PIVOT_HEAP_TID_ATTR, BTPageGetOpaque, BTreeTupleGetHeapTID(), BTreeTupleGetNAtts, BTreeTupleIsPivot(), BTreeTupleIsPosting(), FirstOffsetNumber, IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, ItemPointerGetOffsetNumber(), ItemPointerGetOffsetNumberNoCheck(), P_FIRSTDATAKEY, P_HIKEY, P_IGNORE, P_ISLEAF, P_RIGHTMOST, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), and IndexTupleData::t_tid.

Referenced by _bt_compare(), and bt_target_page_check().

◆ _bt_check_third_page()

void _bt_check_third_page ( Relation  rel,
Relation  heap,
bool  needheaptidspace,
Page  page,
IndexTuple  newtup 
)

Definition at line 2682 of file nbtutils.c.

2684 {
2685  Size itemsz;
2686  BTPageOpaque opaque;
2687 
2688  itemsz = MAXALIGN(IndexTupleSize(newtup));
2689 
2690  /* Double check item size against limit */
2691  if (itemsz <= BTMaxItemSize(page))
2692  return;
2693 
2694  /*
2695  * Tuple is probably too large to fit on page, but it's possible that the
2696  * index uses version 2 or version 3, or that page is an internal page, in
2697  * which case a slightly higher limit applies.
2698  */
2699  if (!needheaptidspace && itemsz <= BTMaxItemSizeNoHeapTid(page))
2700  return;
2701 
2702  /*
2703  * Internal page insertions cannot fail here, because that would mean that
2704  * an earlier leaf level insertion that should have failed didn't
2705  */
2706  opaque = BTPageGetOpaque(page);
2707  if (!P_ISLEAF(opaque))
2708  elog(ERROR, "cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
2709  itemsz, RelationGetRelationName(rel));
2710 
2711  ereport(ERROR,
2712  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2713  errmsg("index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
2714  itemsz,
2715  needheaptidspace ? BTREE_VERSION : BTREE_NOVAC_VERSION,
2716  needheaptidspace ? BTMaxItemSize(page) :
2717  BTMaxItemSizeNoHeapTid(page),
2719  errdetail("Index row references tuple (%u,%u) in relation \"%s\".",
2722  RelationGetRelationName(heap)),
2723  errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
2724  "Consider a function index of an MD5 hash of the value, "
2725  "or use full text indexing."),
2727 }
size_t Size
Definition: c.h:592
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define IndexTupleSize(itup)
Definition: itup.h:70
#define BTMaxItemSizeNoHeapTid(page)
Definition: nbtree.h:169
#define BTREE_VERSION
Definition: nbtree.h:150
#define BTMaxItemSize(page)
Definition: nbtree.h:164
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:152
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5988

References BTMaxItemSize, BTMaxItemSizeNoHeapTid, BTPageGetOpaque, BTREE_NOVAC_VERSION, BTREE_VERSION, BTreeTupleGetHeapTID(), elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, errtableconstraint(), IndexTupleSize, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), MAXALIGN, P_ISLEAF, and RelationGetRelationName.

Referenced by _bt_buildadd(), and _bt_findinsertloc().

◆ _bt_checkkeys()

bool _bt_checkkeys ( IndexScanDesc  scan,
IndexTuple  tuple,
int  tupnatts,
ScanDirection  dir,
bool continuescan,
bool  requiredMatchedByPrecheck,
bool  haveFirstMatch 
)

Definition at line 1372 of file nbtutils.c.

1375 {
1376  TupleDesc tupdesc;
1377  BTScanOpaque so;
1378  int keysz;
1379  int ikey;
1380  ScanKey key;
1381 
1382  Assert(BTreeTupleGetNAtts(tuple, scan->indexRelation) == tupnatts);
1383 
1384  *continuescan = true; /* default assumption */
1385 
1386  tupdesc = RelationGetDescr(scan->indexRelation);
1387  so = (BTScanOpaque) scan->opaque;
1388  keysz = so->numberOfKeys;
1389 
1390  for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
1391  {
1392  Datum datum;
1393  bool isNull;
1394  Datum test;
1395  bool requiredSameDir = false,
1396  requiredOppositeDir = false;
1397 
1398  /*
1399  * Check if the key is required for ordered scan in the same or
1400  * opposite direction. Save as flag variables for future usage.
1401  */
1402  if (((key->sk_flags & SK_BT_REQFWD) && ScanDirectionIsForward(dir)) ||
1403  ((key->sk_flags & SK_BT_REQBKWD) && ScanDirectionIsBackward(dir)))
1404  requiredSameDir = true;
1405  else if (((key->sk_flags & SK_BT_REQFWD) && ScanDirectionIsBackward(dir)) ||
1406  ((key->sk_flags & SK_BT_REQBKWD) && ScanDirectionIsForward(dir)))
1407  requiredOppositeDir = true;
1408 
1409  /*
1410  * If the caller told us the *continuescan flag is known to be true
1411  * for the last item on the page, then we know the keys required for
1412  * the current direction scan should be matched. Otherwise, the
1413  * *continuescan flag would be set for the current item and
1414  * subsequently the last item on the page accordingly.
1415  *
1416  * If the key is required for the opposite direction scan, we can skip
1417  * the check if the caller tells us there was already at least one
1418  * matching item on the page. Also, we require the *continuescan flag
1419  * to be true for the last item on the page to know there are no
1420  * NULLs.
1421  *
1422  * Both cases above work except for the row keys, where NULLs could be
1423  * found in the middle of matching values.
1424  */
1425  if ((requiredSameDir || (requiredOppositeDir && haveFirstMatch)) &&
1426  !(key->sk_flags & SK_ROW_HEADER) && continuescanPrechecked)
1427  continue;
1428 
1429  if (key->sk_attno > tupnatts)
1430  {
1431  /*
1432  * This attribute is truncated (must be high key). The value for
1433  * this attribute in the first non-pivot tuple on the page to the
1434  * right could be any possible value. Assume that truncated
1435  * attribute passes the qual.
1436  */
1438  Assert(BTreeTupleIsPivot(tuple));
1439  continue;
1440  }
1441 
1442  /* row-comparison keys need special processing */
1443  if (key->sk_flags & SK_ROW_HEADER)
1444  {
1445  if (_bt_check_rowcompare(key, tuple, tupnatts, tupdesc, dir,
1446  continuescan))
1447  continue;
1448  return false;
1449  }
1450 
1451  datum = index_getattr(tuple,
1452  key->sk_attno,
1453  tupdesc,
1454  &isNull);
1455 
1456  if (key->sk_flags & SK_ISNULL)
1457  {
1458  /* Handle IS NULL/NOT NULL tests */
1459  if (key->sk_flags & SK_SEARCHNULL)
1460  {
1461  if (isNull)
1462  continue; /* tuple satisfies this qual */
1463  }
1464  else
1465  {
1466  Assert(key->sk_flags & SK_SEARCHNOTNULL);
1467  if (!isNull)
1468  continue; /* tuple satisfies this qual */
1469  }
1470 
1471  /*
1472  * Tuple fails this qual. If it's a required qual for the current
1473  * scan direction, then we can conclude no further tuples will
1474  * pass, either.
1475  */
1476  if (requiredSameDir)
1477  *continuescan = false;
1478 
1479  /*
1480  * In any case, this indextuple doesn't match the qual.
1481  */
1482  return false;
1483  }
1484 
1485  if (isNull)
1486  {
1487  if (key->sk_flags & SK_BT_NULLS_FIRST)
1488  {
1489  /*
1490  * Since NULLs are sorted before non-NULLs, we know we have
1491  * reached the lower limit of the range of values for this
1492  * index attr. On a backward scan, we can stop if this qual
1493  * is one of the "must match" subset. We can stop regardless
1494  * of whether the qual is > or <, so long as it's required,
1495  * because it's not possible for any future tuples to pass. On
1496  * a forward scan, however, we must keep going, because we may
1497  * have initially positioned to the start of the index.
1498  */
1499  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1501  *continuescan = false;
1502  }
1503  else
1504  {
1505  /*
1506  * Since NULLs are sorted after non-NULLs, we know we have
1507  * reached the upper limit of the range of values for this
1508  * index attr. On a forward scan, we can stop if this qual is
1509  * one of the "must match" subset. We can stop regardless of
1510  * whether the qual is > or <, so long as it's required,
1511  * because it's not possible for any future tuples to pass. On
1512  * a backward scan, however, we must keep going, because we
1513  * may have initially positioned to the end of the index.
1514  */
1515  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1517  *continuescan = false;
1518  }
1519 
1520  /*
1521  * In any case, this indextuple doesn't match the qual.
1522  */
1523  return false;
1524  }
1525 
1526  /*
1527  * Apply the key-checking function. When the key is required for the
1528  * opposite direction scan, it must be already satisfied as soon as
1529  * there is already match on the page. Except for the NULLs checking,
1530  * which have already done above.
1531  */
1532  if (!(requiredOppositeDir && haveFirstMatch))
1533  {
1534  test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
1535  datum, key->sk_argument);
1536  }
1537  else
1538  {
1539  test = true;
1540  Assert(test == FunctionCall2Coll(&key->sk_func, key->sk_collation,
1541  datum, key->sk_argument));
1542  }
1543 
1544  if (!DatumGetBool(test))
1545  {
1546  /*
1547  * Tuple fails this qual. If it's a required qual for the current
1548  * scan direction, then we can conclude no further tuples will
1549  * pass, either.
1550  *
1551  * Note: because we stop the scan as soon as any required equality
1552  * qual fails, it is critical that equality quals be used for the
1553  * initial positioning in _bt_first() when they are available. See
1554  * comments in _bt_first().
1555  */
1556  if (requiredSameDir)
1557  *continuescan = false;
1558 
1559  /*
1560  * In any case, this indextuple doesn't match the qual.
1561  */
1562  return false;
1563  }
1564  }
1565 
1566  /* If we get here, the tuple passes all index quals. */
1567  return true;
1568 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: itup.h:117
#define SK_BT_REQBKWD
Definition: nbtree.h:1084
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:1087
#define SK_BT_REQFWD
Definition: nbtree.h:1083
static bool _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
Definition: nbtutils.c:1580
uintptr_t Datum
Definition: postgres.h:64
static void test(void)
#define RelationGetDescr(relation)
Definition: rel.h:531
#define ScanDirectionIsForward(direction)
Definition: sdir.h:64
#define SK_ROW_HEADER
Definition: skey.h:117
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_SEARCHNULL
Definition: skey.h:121
#define SK_ISNULL
Definition: skey.h:115
ScanKey keyData
Definition: nbtree.h:1037
Relation indexRelation
Definition: relscan.h:118

References _bt_check_rowcompare(), Assert(), BTreeTupleGetNAtts, BTreeTupleIsPivot(), DatumGetBool(), FunctionCall2Coll(), index_getattr(), IndexScanDescData::indexRelation, sort-test::key, BTScanOpaqueData::keyData, BTScanOpaqueData::numberOfKeys, IndexScanDescData::opaque, RelationGetDescr, ScanDirectionIsBackward, ScanDirectionIsForward, SK_BT_NULLS_FIRST, SK_BT_REQBKWD, SK_BT_REQFWD, SK_ISNULL, SK_ROW_HEADER, SK_SEARCHNOTNULL, SK_SEARCHNULL, and test().

Referenced by _bt_readpage().

◆ _bt_checkpage()

void _bt_checkpage ( Relation  rel,
Buffer  buf 
)

Definition at line 797 of file nbtpage.c.

798 {
799  Page page = BufferGetPage(buf);
800 
801  /*
802  * ReadBuffer verifies that every newly-read page passes
803  * PageHeaderIsValid, which means it either contains a reasonably sane
804  * page header or is all-zero. We have to defend against the all-zero
805  * case, however.
806  */
807  if (PageIsNew(page))
808  ereport(ERROR,
809  (errcode(ERRCODE_INDEX_CORRUPTED),
810  errmsg("index \"%s\" contains unexpected zero page at block %u",
813  errhint("Please REINDEX it.")));
814 
815  /*
816  * Additionally check that the special area looks sane.
817  */
818  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(BTPageOpaqueData)))
819  ereport(ERROR,
820  (errcode(ERRCODE_INDEX_CORRUPTED),
821  errmsg("index \"%s\" contains corrupted page at block %u",
824  errhint("Please REINDEX it.")));
825 }
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:313

References buf, BufferGetBlockNumber(), BufferGetPage(), ereport, errcode(), errhint(), errmsg(), ERROR, MAXALIGN, PageGetSpecialSize(), PageIsNew(), and RelationGetRelationName.

Referenced by _bt_getbuf(), _bt_relandgetbuf(), _bt_search_insert(), bt_recheck_sibling_links(), btvacuumpage(), and palloc_btree_page().

◆ _bt_compare()

int32 _bt_compare ( Relation  rel,
BTScanInsert  key,
Page  page,
OffsetNumber  offnum 
)

Definition at line 682 of file nbtsearch.c.

686 {
687  TupleDesc itupdesc = RelationGetDescr(rel);
688  BTPageOpaque opaque = BTPageGetOpaque(page);
689  IndexTuple itup;
690  ItemPointer heapTid;
691  ScanKey scankey;
692  int ncmpkey;
693  int ntupatts;
694  int32 result;
695 
696  Assert(_bt_check_natts(rel, key->heapkeyspace, page, offnum));
698  Assert(key->heapkeyspace || key->scantid == NULL);
699 
700  /*
701  * Force result ">" if target item is first data item on an internal page
702  * --- see NOTE above.
703  */
704  if (!P_ISLEAF(opaque) && offnum == P_FIRSTDATAKEY(opaque))
705  return 1;
706 
707  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
708  ntupatts = BTreeTupleGetNAtts(itup, rel);
709 
710  /*
711  * The scan key is set up with the attribute number associated with each
712  * term in the key. It is important that, if the index is multi-key, the
713  * scan contain the first k key attributes, and that they be in order. If
714  * you think about how multi-key ordering works, you'll understand why
715  * this is.
716  *
717  * We don't test for violation of this condition here, however. The
718  * initial setup for the index scan had better have gotten it right (see
719  * _bt_first).
720  */
721 
722  ncmpkey = Min(ntupatts, key->keysz);
723  Assert(key->heapkeyspace || ncmpkey == key->keysz);
724  Assert(!BTreeTupleIsPosting(itup) || key->allequalimage);
725  scankey = key->scankeys;
726  for (int i = 1; i <= ncmpkey; i++)
727  {
728  Datum datum;
729  bool isNull;
730 
731  datum = index_getattr(itup, scankey->sk_attno, itupdesc, &isNull);
732 
733  if (scankey->sk_flags & SK_ISNULL) /* key is NULL */
734  {
735  if (isNull)
736  result = 0; /* NULL "=" NULL */
737  else if (scankey->sk_flags & SK_BT_NULLS_FIRST)
738  result = -1; /* NULL "<" NOT_NULL */
739  else
740  result = 1; /* NULL ">" NOT_NULL */
741  }
742  else if (isNull) /* key is NOT_NULL and item is NULL */
743  {
744  if (scankey->sk_flags & SK_BT_NULLS_FIRST)
745  result = 1; /* NOT_NULL ">" NULL */
746  else
747  result = -1; /* NOT_NULL "<" NULL */
748  }
749  else
750  {
751  /*
752  * The sk_func needs to be passed the index value as left arg and
753  * the sk_argument as right arg (they might be of different
754  * types). Since it is convenient for callers to think of
755  * _bt_compare as comparing the scankey to the index item, we have
756  * to flip the sign of the comparison result. (Unless it's a DESC
757  * column, in which case we *don't* flip the sign.)
758  */
759  result = DatumGetInt32(FunctionCall2Coll(&scankey->sk_func,
760  scankey->sk_collation,
761  datum,
762  scankey->sk_argument));
763 
764  if (!(scankey->sk_flags & SK_BT_DESC))
765  INVERT_COMPARE_RESULT(result);
766  }
767 
768  /* if the keys are unequal, return the difference */
769  if (result != 0)
770  return result;
771 
772  scankey++;
773  }
774 
775  /*
776  * All non-truncated attributes (other than heap TID) were found to be
777  * equal. Treat truncated attributes as minus infinity when scankey has a
778  * key attribute value that would otherwise be compared directly.
779  *
780  * Note: it doesn't matter if ntupatts includes non-key attributes;
781  * scankey won't, so explicitly excluding non-key attributes isn't
782  * necessary.
783  */
784  if (key->keysz > ntupatts)
785  return 1;
786 
787  /*
788  * Use the heap TID attribute and scantid to try to break the tie. The
789  * rules are the same as any other key attribute -- only the
790  * representation differs.
791  */
792  heapTid = BTreeTupleGetHeapTID(itup);
793  if (key->scantid == NULL)
794  {
795  /*
796  * Forward scans have a scankey that is considered greater than a
797  * truncated pivot tuple if and when the scankey has equal values for
798  * attributes up to and including the least significant untruncated
799  * attribute in tuple. Even attributes that were omitted from the
800  * scan key are considered greater than -inf truncated attributes.
801  * (See _bt_binsrch for an explanation of our backward scan behavior.)
802  *
803  * For example, if an index has the minimum two attributes (single
804  * user key attribute, plus heap TID attribute), and a page's high key
805  * is ('foo', -inf), and scankey is ('foo', <omitted>), the search
806  * will not descend to the page to the left. The search will descend
807  * right instead. The truncated attribute in pivot tuple means that
808  * all non-pivot tuples on the page to the left are strictly < 'foo',
809  * so it isn't necessary to descend left. In other words, search
810  * doesn't have to descend left because it isn't interested in a match
811  * that has a heap TID value of -inf.
812  *
813  * Note: the heap TID part of the test ensures that scankey is being
814  * compared to a pivot tuple with one or more truncated -inf key
815  * attributes. The heap TID attribute is the last key attribute in
816  * every index, of course, but other than that it isn't special.
817  */
818  if (!key->backward && key->keysz == ntupatts && heapTid == NULL &&
819  key->heapkeyspace)
820  return 1;
821 
822  /* All provided scankey arguments found to be equal */
823  return 0;
824  }
825 
826  /*
827  * Treat truncated heap TID as minus infinity, since scankey has a key
828  * attribute value (scantid) that would otherwise be compared directly
829  */
831  if (heapTid == NULL)
832  return 1;
833 
834  /*
835  * Scankey must be treated as equal to a posting list tuple if its scantid
836  * value falls within the range of the posting list. In all other cases
837  * there can only be a single heap TID value, which is compared directly
838  * with scantid.
839  */
841  result = ItemPointerCompare(key->scantid, heapTid);
842  if (result <= 0 || !BTreeTupleIsPosting(itup))
843  return result;
844  else
845  {
846  result = ItemPointerCompare(key->scantid,
848  if (result > 0)
849  return 1;
850  }
851 
852  return 0;
853 }
#define Min(x, y)
Definition: c.h:991
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1093
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:51
#define SK_BT_DESC
Definition: nbtree.h:1086
static ItemPointer BTreeTupleGetMaxHeapTID(IndexTuple itup)
Definition: nbtree.h:664
bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
Definition: nbtutils.c:2522
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
int sk_flags
Definition: skey.h:66
FmgrInfo sk_func
Definition: skey.h:71
Oid sk_collation
Definition: skey.h:70
AttrNumber sk_attno
Definition: skey.h:67

References _bt_check_natts(), Assert(), BTPageGetOpaque, BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetNAtts, BTreeTupleIsPosting(), DatumGetInt32(), FunctionCall2Coll(), i, index_getattr(), IndexRelationGetNumberOfKeyAttributes, INVERT_COMPARE_RESULT, ItemPointerCompare(), sort-test::key, Min, P_FIRSTDATAKEY, P_ISLEAF, PageGetItem(), PageGetItemId(), RelationGetDescr, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, and SK_ISNULL.

Referenced by _bt_binsrch(), _bt_binsrch_insert(), _bt_check_unique(), _bt_findinsertloc(), _bt_moveright(), _bt_search_insert(), bt_rootdescend(), bt_target_page_check(), invariant_g_offset(), invariant_l_nontarget_offset(), invariant_l_offset(), and invariant_leq_offset().

◆ _bt_conditionallockbuf()

bool _bt_conditionallockbuf ( Relation  rel,
Buffer  buf 
)

Definition at line 1093 of file nbtpage.c.

1094 {
1095  /* ConditionalLockBuffer() asserts that pin is held by this backend */
1096  if (!ConditionalLockBuffer(buf))
1097  return false;
1098 
1099  if (!RelationUsesLocalBuffers(rel))
1101 
1102  return true;
1103 }
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4821

References buf, BufferGetPage(), ConditionalLockBuffer(), RelationUsesLocalBuffers, and VALGRIND_MAKE_MEM_DEFINED.

Referenced by _bt_allocbuf(), and _bt_search_insert().

◆ _bt_dedup_finish_pending()

Size _bt_dedup_finish_pending ( Page  newpage,
BTDedupState  state 
)

Definition at line 555 of file nbtdedup.c.

556 {
557  OffsetNumber tupoff;
558  Size tuplesz;
559  Size spacesaving;
560 
561  Assert(state->nitems > 0);
562  Assert(state->nitems <= state->nhtids);
563  Assert(state->intervals[state->nintervals].baseoff == state->baseoff);
564 
565  tupoff = OffsetNumberNext(PageGetMaxOffsetNumber(newpage));
566  if (state->nitems == 1)
567  {
568  /* Use original, unchanged base tuple */
569  tuplesz = IndexTupleSize(state->base);
570  Assert(tuplesz == MAXALIGN(IndexTupleSize(state->base)));
571  Assert(tuplesz <= BTMaxItemSize(newpage));
572  if (PageAddItem(newpage, (Item) state->base, tuplesz, tupoff,
573  false, false) == InvalidOffsetNumber)
574  elog(ERROR, "deduplication failed to add tuple to page");
575 
576  spacesaving = 0;
577  }
578  else
579  {
580  IndexTuple final;
581 
582  /* Form a tuple with a posting list */
583  final = _bt_form_posting(state->base, state->htids, state->nhtids);
584  tuplesz = IndexTupleSize(final);
585  Assert(tuplesz <= state->maxpostingsize);
586 
587  /* Save final number of items for posting list */
588  state->intervals[state->nintervals].nitems = state->nitems;
589 
590  Assert(tuplesz == MAXALIGN(IndexTupleSize(final)));
591  Assert(tuplesz <= BTMaxItemSize(newpage));
592  if (PageAddItem(newpage, (Item) final, tuplesz, tupoff, false,
593  false) == InvalidOffsetNumber)
594  elog(ERROR, "deduplication failed to add tuple to page");
595 
596  pfree(final);
597  spacesaving = state->phystupsize - (tuplesz + sizeof(ItemIdData));
598  /* Increment nintervals, since we wrote a new posting list tuple */
599  state->nintervals++;
600  Assert(spacesaving > 0 && spacesaving < BLCKSZ);
601  }
602 
603  /* Reset state for next pending posting list */
604  state->nhtids = 0;
605  state->nitems = 0;
606  state->phystupsize = 0;
607 
608  return spacesaving;
609 }
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:468
Pointer Item
Definition: item.h:17
IndexTuple _bt_form_posting(IndexTuple base, ItemPointer htids, int nhtids)
Definition: nbtdedup.c:864

References _bt_form_posting(), Assert(), BTMaxItemSize, elog, ERROR, IndexTupleSize, InvalidOffsetNumber, MAXALIGN, OffsetNumberNext, PageAddItem, PageGetMaxOffsetNumber(), and pfree().

Referenced by _bt_dedup_pass(), and btree_xlog_dedup().

◆ _bt_dedup_pass()

void _bt_dedup_pass ( Relation  rel,
Buffer  buf,
IndexTuple  newitem,
Size  newitemsz,
bool  bottomupdedup 
)

Definition at line 58 of file nbtdedup.c.

60 {
61  OffsetNumber offnum,
62  minoff,
63  maxoff;
64  Page page = BufferGetPage(buf);
65  BTPageOpaque opaque = BTPageGetOpaque(page);
66  Page newpage;
68  Size pagesaving PG_USED_FOR_ASSERTS_ONLY = 0;
69  bool singlevalstrat = false;
70  int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
71 
72  /* Passed-in newitemsz is MAXALIGNED but does not include line pointer */
73  newitemsz += sizeof(ItemIdData);
74 
75  /*
76  * Initialize deduplication state.
77  *
78  * It would be possible for maxpostingsize (limit on posting list tuple
79  * size) to be set to one third of the page. However, it seems like a
80  * good idea to limit the size of posting lists to one sixth of a page.
81  * That ought to leave us with a good split point when pages full of
82  * duplicates can be split several times.
83  */
85  state->deduplicate = true;
86  state->nmaxitems = 0;
87  state->maxpostingsize = Min(BTMaxItemSize(page) / 2, INDEX_SIZE_MASK);
88  /* Metadata about base tuple of current pending posting list */
89  state->base = NULL;
90  state->baseoff = InvalidOffsetNumber;
91  state->basetupsize = 0;
92  /* Metadata about current pending posting list TIDs */
93  state->htids = palloc(state->maxpostingsize);
94  state->nhtids = 0;
95  state->nitems = 0;
96  /* Size of all physical tuples to be replaced by pending posting list */
97  state->phystupsize = 0;
98  /* nintervals should be initialized to zero */
99  state->nintervals = 0;
100 
101  minoff = P_FIRSTDATAKEY(opaque);
102  maxoff = PageGetMaxOffsetNumber(page);
103 
104  /*
105  * Consider applying "single value" strategy, though only if the page
106  * seems likely to be split in the near future
107  */
108  if (!bottomupdedup)
109  singlevalstrat = _bt_do_singleval(rel, page, state, minoff, newitem);
110 
111  /*
112  * Deduplicate items from page, and write them to newpage.
113  *
114  * Copy the original page's LSN into newpage copy. This will become the
115  * updated version of the page. We need this because XLogInsert will
116  * examine the LSN and possibly dump it in a page image.
117  */
118  newpage = PageGetTempPageCopySpecial(page);
119  PageSetLSN(newpage, PageGetLSN(page));
120 
121  /* Copy high key, if any */
122  if (!P_RIGHTMOST(opaque))
123  {
124  ItemId hitemid = PageGetItemId(page, P_HIKEY);
125  Size hitemsz = ItemIdGetLength(hitemid);
126  IndexTuple hitem = (IndexTuple) PageGetItem(page, hitemid);
127 
128  if (PageAddItem(newpage, (Item) hitem, hitemsz, P_HIKEY,
129  false, false) == InvalidOffsetNumber)
130  elog(ERROR, "deduplication failed to add highkey");
131  }
132 
133  for (offnum = minoff;
134  offnum <= maxoff;
135  offnum = OffsetNumberNext(offnum))
136  {
137  ItemId itemid = PageGetItemId(page, offnum);
138  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
139 
140  Assert(!ItemIdIsDead(itemid));
141 
142  if (offnum == minoff)
143  {
144  /*
145  * No previous/base tuple for the data item -- use the data item
146  * as base tuple of pending posting list
147  */
148  _bt_dedup_start_pending(state, itup, offnum);
149  }
150  else if (state->deduplicate &&
151  _bt_keep_natts_fast(rel, state->base, itup) > nkeyatts &&
152  _bt_dedup_save_htid(state, itup))
153  {
154  /*
155  * Tuple is equal to base tuple of pending posting list. Heap
156  * TID(s) for itup have been saved in state.
157  */
158  }
159  else
160  {
161  /*
162  * Tuple is not equal to pending posting list tuple, or
163  * _bt_dedup_save_htid() opted to not merge current item into
164  * pending posting list for some other reason (e.g., adding more
165  * TIDs would have caused posting list to exceed current
166  * maxpostingsize).
167  *
168  * If state contains pending posting list with more than one item,
169  * form new posting tuple and add it to our temp page (newpage).
170  * Else add pending interval's base tuple to the temp page as-is.
171  */
172  pagesaving += _bt_dedup_finish_pending(newpage, state);
173 
174  if (singlevalstrat)
175  {
176  /*
177  * Single value strategy's extra steps.
178  *
179  * Lower maxpostingsize for sixth and final large posting list
180  * tuple at the point where 5 maxpostingsize-capped tuples
181  * have either been formed or observed.
182  *
183  * When a sixth maxpostingsize-capped item is formed/observed,
184  * stop merging together tuples altogether. The few tuples
185  * that remain at the end of the page won't be merged together
186  * at all (at least not until after a future page split takes
187  * place, when this page's newly allocated right sibling page
188  * gets its first deduplication pass).
189  */
190  if (state->nmaxitems == 5)
191  _bt_singleval_fillfactor(page, state, newitemsz);
192  else if (state->nmaxitems == 6)
193  {
194  state->deduplicate = false;
195  singlevalstrat = false; /* won't be back here */
196  }
197  }
198 
199  /* itup starts new pending posting list */
200  _bt_dedup_start_pending(state, itup, offnum);
201  }
202  }
203 
204  /* Handle the last item */
205  pagesaving += _bt_dedup_finish_pending(newpage, state);
206 
207  /*
208  * If no items suitable for deduplication were found, newpage must be
209  * exactly the same as the original page, so just return from function.
210  *
211  * We could determine whether or not to proceed on the basis the space
212  * savings being sufficient to avoid an immediate page split instead. We
213  * don't do that because there is some small value in nbtsplitloc.c always
214  * operating against a page that is fully deduplicated (apart from
215  * newitem). Besides, most of the cost has already been paid.
216  */
217  if (state->nintervals == 0)
218  {
219  /* cannot leak memory here */
220  pfree(newpage);
221  pfree(state->htids);
222  pfree(state);
223  return;
224  }
225 
226  /*
227  * By here, it's clear that deduplication will definitely go ahead.
228  *
229  * Clear the BTP_HAS_GARBAGE page flag. The index must be a heapkeyspace
230  * index, and as such we'll never pay attention to BTP_HAS_GARBAGE anyway.
231  * But keep things tidy.
232  */
233  if (P_HAS_GARBAGE(opaque))
234  {
235  BTPageOpaque nopaque = BTPageGetOpaque(newpage);
236 
237  nopaque->btpo_flags &= ~BTP_HAS_GARBAGE;
238  }
239 
241 
242  PageRestoreTempPage(newpage, page);
244 
245  /* XLOG stuff */
246  if (RelationNeedsWAL(rel))
247  {
248  XLogRecPtr recptr;
249  xl_btree_dedup xlrec_dedup;
250 
251  xlrec_dedup.nintervals = state->nintervals;
252 
253  XLogBeginInsert();
255  XLogRegisterData((char *) &xlrec_dedup, SizeOfBtreeDedup);
256 
257  /*
258  * The intervals array is not in the buffer, but pretend that it is.
259  * When XLogInsert stores the whole buffer, the array need not be
260  * stored too.
261  */
262  XLogRegisterBufData(0, (char *) state->intervals,
263  state->nintervals * sizeof(BTDedupInterval));
264 
265  recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_DEDUP);
266 
267  PageSetLSN(page, recptr);
268  }
269 
271 
272  /* Local space accounting should agree with page accounting */
273  Assert(pagesaving < newitemsz || PageGetExactFreeSpace(page) >= newitemsz);
274 
275  /* cannot leak memory here */
276  pfree(state->htids);
277  pfree(state);
278 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2189
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:424
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:402
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:169
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define INDEX_SIZE_MASK
Definition: itup.h:65
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
static bool _bt_do_singleval(Relation rel, Page page, BTDedupState state, OffsetNumber minoff, IndexTuple newitem)
Definition: nbtdedup.c:782
Size _bt_dedup_finish_pending(Page newpage, BTDedupState state)
Definition: nbtdedup.c:555
static void _bt_singleval_fillfactor(Page page, BTDedupState state, Size newitemsz)
Definition: nbtdedup.c:822
#define P_HAS_GARBAGE(opaque)
Definition: nbtree.h:226
#define BTP_HAS_GARBAGE
Definition: nbtree.h:82
#define XLOG_BTREE_DEDUP
Definition: nbtxlog.h:33
#define SizeOfBtreeDedup
Definition: nbtxlog.h:174
uint16 btpo_flags
Definition: nbtree.h:67
uint16 nintervals
Definition: nbtxlog.h:169
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogRegisterBufData(uint8 block_id, char *data, uint32 len)
Definition: xloginsert.c:405
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:242
#define REGBUF_STANDARD
Definition: xloginsert.h:34

References _bt_dedup_finish_pending(), _bt_dedup_save_htid(), _bt_dedup_start_pending(), _bt_do_singleval(), _bt_keep_natts_fast(), _bt_singleval_fillfactor(), Assert(), BTMaxItemSize, BTP_HAS_GARBAGE, BTPageGetOpaque, BTPageOpaqueData::btpo_flags, buf, BufferGetPage(), elog, END_CRIT_SECTION, ERROR, INDEX_SIZE_MASK, IndexRelationGetNumberOfKeyAttributes, InvalidOffsetNumber, ItemIdGetLength, ItemIdIsDead, MarkBufferDirty(), Min, xl_btree_dedup::nintervals, OffsetNumberNext, P_FIRSTDATAKEY, P_HAS_GARBAGE, P_HIKEY, P_RIGHTMOST, PageAddItem, PageGetExactFreeSpace(), PageGetItem(), PageGetItemId(), PageGetLSN(), PageGetMaxOffsetNumber(), PageGetTempPageCopySpecial(), PageRestoreTempPage(), PageSetLSN(), palloc(), pfree(), PG_USED_FOR_ASSERTS_ONLY, REGBUF_STANDARD, RelationNeedsWAL, SizeOfBtreeDedup, START_CRIT_SECTION, XLOG_BTREE_DEDUP, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by _bt_delete_or_dedup_one_page().

◆ _bt_dedup_save_htid()

bool _bt_dedup_save_htid ( BTDedupState  state,
IndexTuple  itup 
)

Definition at line 484 of file nbtdedup.c.

485 {
486  int nhtids;
487  ItemPointer htids;
488  Size mergedtupsz;
489 
490  Assert(!BTreeTupleIsPivot(itup));
491 
492  if (!BTreeTupleIsPosting(itup))
493  {
494  nhtids = 1;
495  htids = &itup->t_tid;
496  }
497  else
498  {
499  nhtids = BTreeTupleGetNPosting(itup);
500  htids = BTreeTupleGetPosting(itup);
501  }
502 
503  /*
504  * Don't append (have caller finish pending posting list as-is) if
505  * appending heap TID(s) from itup would put us over maxpostingsize limit.
506  *
507  * This calculation needs to match the code used within _bt_form_posting()
508  * for new posting list tuples.
509  */
510  mergedtupsz = MAXALIGN(state->basetupsize +
511  (state->nhtids + nhtids) * sizeof(ItemPointerData));
512 
513  if (mergedtupsz > state->maxpostingsize)
514  {
515  /*
516  * Count this as an oversized item for single value strategy, though
517  * only when there are 50 TIDs in the final posting list tuple. This
518  * limit (which is fairly arbitrary) avoids confusion about how many
519  * 1/6 of a page tuples have been encountered/created by the current
520  * deduplication pass.
521  *
522  * Note: We deliberately don't consider which deduplication pass
523  * merged together tuples to create this item (could be a previous
524  * deduplication pass, or current pass). See _bt_do_singleval()
525  * comments.
526  */
527  if (state->nhtids > 50)
528  state->nmaxitems++;
529 
530  return false;
531  }
532 
533  /*
534  * Save heap TIDs to pending posting list tuple -- itup can be merged into
535  * pending posting list
536  */
537  state->nitems++;
538  memcpy(state->htids + state->nhtids, htids,
539  sizeof(ItemPointerData) * nhtids);
540  state->nhtids += nhtids;
541  state->phystupsize += MAXALIGN(IndexTupleSize(itup)) + sizeof(ItemIdData);
542 
543  return true;
544 }
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:518
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:537

References Assert(), BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), IndexTupleSize, MAXALIGN, and IndexTupleData::t_tid.

Referenced by _bt_bottomupdel_pass(), _bt_dedup_pass(), _bt_load(), and btree_xlog_dedup().

◆ _bt_dedup_start_pending()

void _bt_dedup_start_pending ( BTDedupState  state,
IndexTuple  base,
OffsetNumber  baseoff 
)

Definition at line 433 of file nbtdedup.c.

435 {
436  Assert(state->nhtids == 0);
437  Assert(state->nitems == 0);
438  Assert(!BTreeTupleIsPivot(base));
439 
440  /*
441  * Copy heap TID(s) from new base tuple for new candidate posting list
442  * into working state's array
443  */
444  if (!BTreeTupleIsPosting(base))
445  {
446  memcpy(state->htids, &base->t_tid, sizeof(ItemPointerData));
447  state->nhtids = 1;
448  state->basetupsize = IndexTupleSize(base);
449  }
450  else
451  {
452  int nposting;
453 
454  nposting = BTreeTupleGetNPosting(base);
455  memcpy(state->htids, BTreeTupleGetPosting(base),
456  sizeof(ItemPointerData) * nposting);
457  state->nhtids = nposting;
458  /* basetupsize should not include existing posting list */
459  state->basetupsize = BTreeTupleGetPostingOffset(base);
460  }
461 
462  /*
463  * Save new base tuple itself -- it'll be needed if we actually create a
464  * new posting list from new pending posting list.
465  *
466  * Must maintain physical size of all existing tuples (including line
467  * pointer overhead) so that we can calculate space savings on page.
468  */
469  state->nitems = 1;
470  state->base = base;
471  state->baseoff = baseoff;
472  state->phystupsize = MAXALIGN(IndexTupleSize(base)) + sizeof(ItemIdData);
473  /* Also save baseoff in pending state for interval */
474  state->intervals[state->nintervals].baseoff = state->baseoff;
475 }
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:529

References Assert(), BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), IndexTupleSize, MAXALIGN, and IndexTupleData::t_tid.

Referenced by _bt_bottomupdel_pass(), _bt_dedup_pass(), _bt_load(), and btree_xlog_dedup().

◆ _bt_delitems_delete_check()

void _bt_delitems_delete_check ( Relation  rel,
Buffer  buf,
Relation  heapRel,
TM_IndexDeleteOp delstate 
)

Definition at line 1513 of file nbtpage.c.

1515 {
1516  Page page = BufferGetPage(buf);
1517  TransactionId snapshotConflictHorizon;
1518  bool isCatalogRel;
1519  OffsetNumber postingidxoffnum = InvalidOffsetNumber;
1520  int ndeletable = 0,
1521  nupdatable = 0;
1524 
1525  /* Use tableam interface to determine which tuples to delete first */
1526  snapshotConflictHorizon = table_index_delete_tuples(heapRel, delstate);
1527  isCatalogRel = RelationIsAccessibleInLogicalDecoding(heapRel);
1528 
1529  /* Should not WAL-log snapshotConflictHorizon unless it's required */
1530  if (!XLogStandbyInfoActive())
1531  snapshotConflictHorizon = InvalidTransactionId;
1532 
1533  /*
1534  * Construct a leaf-page-wise description of what _bt_delitems_delete()
1535  * needs to do to physically delete index tuples from the page.
1536  *
1537  * Must sort deltids array to restore leaf-page-wise order (original order
1538  * before call to tableam). This is the order that the loop expects.
1539  *
1540  * Note that deltids array might be a lot smaller now. It might even have
1541  * no entries at all (with bottom-up deletion caller), in which case there
1542  * is nothing left to do.
1543  */
1544  qsort(delstate->deltids, delstate->ndeltids, sizeof(TM_IndexDelete),
1546  if (delstate->ndeltids == 0)
1547  {
1548  Assert(delstate->bottomup);
1549  return;
1550  }
1551 
1552  /* We definitely have to delete at least one index tuple (or one TID) */
1553  for (int i = 0; i < delstate->ndeltids; i++)
1554  {
1555  TM_IndexStatus *dstatus = delstate->status + delstate->deltids[i].id;
1556  OffsetNumber idxoffnum = dstatus->idxoffnum;
1557  ItemId itemid = PageGetItemId(page, idxoffnum);
1558  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
1559  int nestedi,
1560  nitem;
1561  BTVacuumPosting vacposting;
1562 
1563  Assert(OffsetNumberIsValid(idxoffnum));
1564 
1565  if (idxoffnum == postingidxoffnum)
1566  {
1567  /*
1568  * This deltid entry is a TID from a posting list tuple that has
1569  * already been completely processed
1570  */
1571  Assert(BTreeTupleIsPosting(itup));
1573  &delstate->deltids[i].tid) < 0);
1575  &delstate->deltids[i].tid) >= 0);
1576  continue;
1577  }
1578 
1579  if (!BTreeTupleIsPosting(itup))
1580  {
1581  /* Plain non-pivot tuple */
1582  Assert(ItemPointerEquals(&itup->t_tid, &delstate->deltids[i].tid));
1583  if (dstatus->knowndeletable)
1584  deletable[ndeletable++] = idxoffnum;
1585  continue;
1586  }
1587 
1588  /*
1589  * itup is a posting list tuple whose lowest deltids entry (which may
1590  * or may not be for the first TID from itup) is considered here now.
1591  * We should process all of the deltids entries for the posting list
1592  * together now, though (not just the lowest). Remember to skip over
1593  * later itup-related entries during later iterations of outermost
1594  * loop.
1595  */
1596  postingidxoffnum = idxoffnum; /* Remember work in outermost loop */
1597  nestedi = i; /* Initialize for first itup deltids entry */
1598  vacposting = NULL; /* Describes final action for itup */
1599  nitem = BTreeTupleGetNPosting(itup);
1600  for (int p = 0; p < nitem; p++)
1601  {
1602  ItemPointer ptid = BTreeTupleGetPostingN(itup, p);
1603  int ptidcmp = -1;
1604 
1605  /*
1606  * This nested loop reuses work across ptid TIDs taken from itup.
1607  * We take advantage of the fact that both itup's TIDs and deltids
1608  * entries (within a single itup/posting list grouping) must both
1609  * be in ascending TID order.
1610  */
1611  for (; nestedi < delstate->ndeltids; nestedi++)
1612  {
1613  TM_IndexDelete *tcdeltid = &delstate->deltids[nestedi];
1614  TM_IndexStatus *tdstatus = (delstate->status + tcdeltid->id);
1615 
1616  /* Stop once we get past all itup related deltids entries */
1617  Assert(tdstatus->idxoffnum >= idxoffnum);
1618  if (tdstatus->idxoffnum != idxoffnum)
1619  break;
1620 
1621  /* Skip past non-deletable itup related entries up front */
1622  if (!tdstatus->knowndeletable)
1623  continue;
1624 
1625  /* Entry is first partial ptid match (or an exact match)? */
1626  ptidcmp = ItemPointerCompare(&tcdeltid->tid, ptid);
1627  if (ptidcmp >= 0)
1628  {
1629  /* Greater than or equal (partial or exact) match... */
1630  break;
1631  }
1632  }
1633 
1634  /* ...exact ptid match to a deletable deltids entry? */
1635  if (ptidcmp != 0)
1636  continue;
1637 
1638  /* Exact match for deletable deltids entry -- ptid gets deleted */
1639  if (vacposting == NULL)
1640  {
1641  vacposting = palloc(offsetof(BTVacuumPostingData, deletetids) +
1642  nitem * sizeof(uint16));
1643  vacposting->itup = itup;
1644  vacposting->updatedoffset = idxoffnum;
1645  vacposting->ndeletedtids = 0;
1646  }
1647  vacposting->deletetids[vacposting->ndeletedtids++] = p;
1648  }
1649 
1650  /* Final decision on itup, a posting list tuple */
1651 
1652  if (vacposting == NULL)
1653  {
1654  /* No TIDs to delete from itup -- do nothing */
1655  }
1656  else if (vacposting->ndeletedtids == nitem)
1657  {
1658  /* Straight delete of itup (to delete all TIDs) */
1659  deletable[ndeletable++] = idxoffnum;
1660  /* Turns out we won't need granular information */
1661  pfree(vacposting);
1662  }
1663  else
1664  {
1665  /* Delete some (but not all) TIDs from itup */
1666  Assert(vacposting->ndeletedtids > 0 &&
1667  vacposting->ndeletedtids < nitem);
1668  updatable[nupdatable++] = vacposting;
1669  }
1670  }
1671 
1672  /* Physically delete tuples (or TIDs) using deletable (or updatable) */
1673  _bt_delitems_delete(rel, buf, snapshotConflictHorizon, isCatalogRel,
1674  deletable, ndeletable, updatable, nupdatable);
1675 
1676  /* be tidy */
1677  for (int i = 0; i < nupdatable; i++)
1678  pfree(updatable[i]);
1679 }
unsigned short uint16
Definition: c.h:492
uint32 TransactionId
Definition: c.h:639
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
#define MaxIndexTuplesPerPage
Definition: itup.h:165
static void _bt_delitems_delete(Relation rel, Buffer buf, TransactionId snapshotConflictHorizon, bool isCatalogRel, OffsetNumber *deletable, int ndeletable, BTVacuumPosting *updatable, int nupdatable)
Definition: nbtpage.c:1284
static int _bt_delitems_cmp(const void *a, const void *b)
Definition: nbtpage.c:1464
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
Definition: nbtree.h:544
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
#define qsort(a, b, c, d)
Definition: port.h:449
uint16 deletetids[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtree.h:911
uint16 ndeletedtids
Definition: nbtree.h:910
IndexTuple itup
Definition: nbtree.h:906
OffsetNumber updatedoffset
Definition: nbtree.h:907
ItemPointerData tid
Definition: tableam.h:205
bool knowndeletable
Definition: tableam.h:212
OffsetNumber idxoffnum
Definition: tableam.h:211
static TransactionId table_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate)
Definition: tableam.h:1346
#define InvalidTransactionId
Definition: transam.h:31

References _bt_delitems_cmp(), _bt_delitems_delete(), Assert(), TM_IndexDeleteOp::bottomup, BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPosting(), buf, BufferGetPage(), BTVacuumPostingData::deletetids, TM_IndexDeleteOp::deltids, i, TM_IndexDelete::id, TM_IndexStatus::idxoffnum, InvalidOffsetNumber, InvalidTransactionId, ItemPointerCompare(), ItemPointerEquals(), BTVacuumPostingData::itup, TM_IndexStatus::knowndeletable, MaxIndexTuplesPerPage, BTVacuumPostingData::ndeletedtids, TM_IndexDeleteOp::ndeltids, OffsetNumberIsValid, PageGetItem(), PageGetItemId(), palloc(), pfree(), qsort, RelationIsAccessibleInLogicalDecoding, TM_IndexDeleteOp::status, IndexTupleData::t_tid, table_index_delete_tuples(), TM_IndexDelete::tid, BTVacuumPostingData::updatedoffset, and XLogStandbyInfoActive.

Referenced by _bt_bottomupdel_pass(), and _bt_simpledel_pass().

◆ _bt_delitems_vacuum()

void _bt_delitems_vacuum ( Relation  rel,
Buffer  buf,
OffsetNumber deletable,
int  ndeletable,
BTVacuumPosting updatable,
int  nupdatable 
)

Definition at line 1154 of file nbtpage.c.

1157 {
1158  Page page = BufferGetPage(buf);
1159  BTPageOpaque opaque;
1160  bool needswal = RelationNeedsWAL(rel);
1161  char *updatedbuf = NULL;
1162  Size updatedbuflen = 0;
1163  OffsetNumber updatedoffsets[MaxIndexTuplesPerPage];
1164 
1165  /* Shouldn't be called unless there's something to do */
1166  Assert(ndeletable > 0 || nupdatable > 0);
1167 
1168  /* Generate new version of posting lists without deleted TIDs */
1169  if (nupdatable > 0)
1170  updatedbuf = _bt_delitems_update(updatable, nupdatable,
1171  updatedoffsets, &updatedbuflen,
1172  needswal);
1173 
1174  /* No ereport(ERROR) until changes are logged */
1176 
1177  /*
1178  * Handle posting tuple updates.
1179  *
1180  * Deliberately do this before handling simple deletes. If we did it the
1181  * other way around (i.e. WAL record order -- simple deletes before
1182  * updates) then we'd have to make compensating changes to the 'updatable'
1183  * array of offset numbers.
1184  *
1185  * PageIndexTupleOverwrite() won't unset each item's LP_DEAD bit when it
1186  * happens to already be set. It's important that we not interfere with
1187  * any future simple index tuple deletion operations.
1188  */
1189  for (int i = 0; i < nupdatable; i++)
1190  {
1191  OffsetNumber updatedoffset = updatedoffsets[i];
1192  IndexTuple itup;
1193  Size itemsz;
1194 
1195  itup = updatable[i]->itup;
1196  itemsz = MAXALIGN(IndexTupleSize(itup));
1197  if (!PageIndexTupleOverwrite(page, updatedoffset, (Item) itup,
1198  itemsz))
1199  elog(PANIC, "failed to update partially dead item in block %u of index \"%s\"",
1201  }
1202 
1203  /* Now handle simple deletes of entire tuples */
1204  if (ndeletable > 0)
1205  PageIndexMultiDelete(page, deletable, ndeletable);
1206 
1207  /*
1208  * We can clear the vacuum cycle ID since this page has certainly been
1209  * processed by the current vacuum scan.
1210  */
1211  opaque = BTPageGetOpaque(page);
1212  opaque->btpo_cycleid = 0;
1213 
1214  /*
1215  * Clear the BTP_HAS_GARBAGE page flag.
1216  *
1217  * This flag indicates the presence of LP_DEAD items on the page (though
1218  * not reliably). Note that we only rely on it with pg_upgrade'd
1219  * !heapkeyspace indexes. That's why clearing it here won't usually
1220  * interfere with simple index tuple deletion.
1221  */
1222  opaque->btpo_flags &= ~BTP_HAS_GARBAGE;
1223 
1225 
1226  /* XLOG stuff */
1227  if (needswal)
1228  {
1229  XLogRecPtr recptr;
1230  xl_btree_vacuum xlrec_vacuum;
1231 
1232  xlrec_vacuum.ndeleted = ndeletable;
1233  xlrec_vacuum.nupdated = nupdatable;
1234 
1235  XLogBeginInsert();
1237  XLogRegisterData((char *) &xlrec_vacuum, SizeOfBtreeVacuum);
1238 
1239  if (ndeletable > 0)
1240  XLogRegisterBufData(0, (char *) deletable,
1241  ndeletable * sizeof(OffsetNumber));
1242 
1243  if (nupdatable > 0)
1244  {
1245  XLogRegisterBufData(0, (char *) updatedoffsets,
1246  nupdatable * sizeof(OffsetNumber));
1247  XLogRegisterBufData(0, updatedbuf, updatedbuflen);
1248  }
1249 
1250  recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_VACUUM);
1251 
1252  PageSetLSN(page, recptr);
1253  }
1254 
1255  END_CRIT_SECTION();
1256 
1257  /* can't leak memory here */
1258  if (updatedbuf != NULL)
1259  pfree(updatedbuf);
1260  /* free tuples allocated within _bt_delitems_update() */
1261  for (int i = 0; i < nupdatable; i++)
1262  pfree(updatable[i]->itup);
1263 }
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:1161
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1405
#define PANIC
Definition: elog.h:42
static char * _bt_delitems_update(BTVacuumPosting *updatable, int nupdatable, OffsetNumber *updatedoffsets, Size *updatedbuflen, bool needswal)
Definition: nbtpage.c:1405
#define SizeOfBtreeVacuum
Definition: nbtxlog.h:234
#define XLOG_BTREE_VACUUM
Definition: nbtxlog.h:39
BTCycleId btpo_cycleid
Definition: nbtree.h:68
uint16 ndeleted
Definition: nbtxlog.h:222
uint16 nupdated
Definition: nbtxlog.h:223

References _bt_delitems_update(), Assert(), BTP_HAS_GARBAGE, BTPageGetOpaque, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, buf, BufferGetBlockNumber(), BufferGetPage(), elog, END_CRIT_SECTION, i, IndexTupleSize, BTVacuumPostingData::itup, MarkBufferDirty(), MAXALIGN, MaxIndexTuplesPerPage, xl_btree_vacuum::ndeleted, xl_btree_vacuum::nupdated, PageIndexMultiDelete(), PageIndexTupleOverwrite(), PageSetLSN(), PANIC, pfree(), REGBUF_STANDARD, RelationGetRelationName, RelationNeedsWAL, SizeOfBtreeVacuum, START_CRIT_SECTION, XLOG_BTREE_VACUUM, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by btvacuumpage().

◆ _bt_doinsert()

bool _bt_doinsert ( Relation  rel,
IndexTuple  itup,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
Relation  heapRel 
)

Definition at line 102 of file nbtinsert.c.

105 {
106  bool is_unique = false;
107  BTInsertStateData insertstate;
108  BTScanInsert itup_key;
109  BTStack stack;
110  bool checkingunique = (checkUnique != UNIQUE_CHECK_NO);
111 
112  /* we need an insertion scan key to do our search, so build one */
113  itup_key = _bt_mkscankey(rel, itup);
114 
115  if (checkingunique)
116  {
117  if (!itup_key->anynullkeys)
118  {
119  /* No (heapkeyspace) scantid until uniqueness established */
120  itup_key->scantid = NULL;
121  }
122  else
123  {
124  /*
125  * Scan key for new tuple contains NULL key values. Bypass
126  * checkingunique steps. They are unnecessary because core code
127  * considers NULL unequal to every value, including NULL.
128  *
129  * This optimization avoids O(N^2) behavior within the
130  * _bt_findinsertloc() heapkeyspace path when a unique index has a
131  * large number of "duplicates" with NULL key values.
132  */
133  checkingunique = false;
134  /* Tuple is unique in the sense that core code cares about */
135  Assert(checkUnique != UNIQUE_CHECK_EXISTING);
136  is_unique = true;
137  }
138  }
139 
140  /*
141  * Fill in the BTInsertState working area, to track the current page and
142  * position within the page to insert on.
143  *
144  * Note that itemsz is passed down to lower level code that deals with
145  * inserting the item. It must be MAXALIGN()'d. This ensures that space
146  * accounting code consistently considers the alignment overhead that we
147  * expect PageAddItem() will add later. (Actually, index_form_tuple() is
148  * already conservative about alignment, but we don't rely on that from
149  * this distance. Besides, preserving the "true" tuple size in index
150  * tuple headers for the benefit of nbtsplitloc.c might happen someday.
151  * Note that heapam does not MAXALIGN() each heap tuple's lp_len field.)
152  */
153  insertstate.itup = itup;
154  insertstate.itemsz = MAXALIGN(IndexTupleSize(itup));
155  insertstate.itup_key = itup_key;
156  insertstate.bounds_valid = false;
157  insertstate.buf = InvalidBuffer;
158  insertstate.postingoff = 0;
159 
160 search:
161 
162  /*
163  * Find and lock the leaf page that the tuple should be added to by
164  * searching from the root page. insertstate.buf will hold a buffer that
165  * is locked in exclusive mode afterwards.
166  */
167  stack = _bt_search_insert(rel, heapRel, &insertstate);
168 
169  /*
170  * checkingunique inserts are not allowed to go ahead when two tuples with
171  * equal key attribute values would be visible to new MVCC snapshots once
172  * the xact commits. Check for conflicts in the locked page/buffer (if
173  * needed) here.
174  *
175  * It might be necessary to check a page to the right in _bt_check_unique,
176  * though that should be very rare. In practice the first page the value
177  * could be on (with scantid omitted) is almost always also the only page
178  * that a matching tuple might be found on. This is due to the behavior
179  * of _bt_findsplitloc with duplicate tuples -- a group of duplicates can
180  * only be allowed to cross a page boundary when there is no candidate
181  * leaf page split point that avoids it. Also, _bt_check_unique can use
182  * the leaf page high key to determine that there will be no duplicates on
183  * the right sibling without actually visiting it (it uses the high key in
184  * cases where the new item happens to belong at the far right of the leaf
185  * page).
186  *
187  * NOTE: obviously, _bt_check_unique can only detect keys that are already
188  * in the index; so it cannot defend against concurrent insertions of the
189  * same key. We protect against that by means of holding a write lock on
190  * the first page the value could be on, with omitted/-inf value for the
191  * implicit heap TID tiebreaker attribute. Any other would-be inserter of
192  * the same key must acquire a write lock on the same page, so only one
193  * would-be inserter can be making the check at one time. Furthermore,
194  * once we are past the check we hold write locks continuously until we
195  * have performed our insertion, so no later inserter can fail to see our
196  * insertion. (This requires some care in _bt_findinsertloc.)
197  *
198  * If we must wait for another xact, we release the lock while waiting,
199  * and then must perform a new search.
200  *
201  * For a partial uniqueness check, we don't wait for the other xact. Just
202  * let the tuple in and return false for possibly non-unique, or true for
203  * definitely unique.
204  */
205  if (checkingunique)
206  {
207  TransactionId xwait;
208  uint32 speculativeToken;
209 
210  xwait = _bt_check_unique(rel, &insertstate, heapRel, checkUnique,
211  &is_unique, &speculativeToken);
212 
213  if (unlikely(TransactionIdIsValid(xwait)))
214  {
215  /* Have to wait for the other guy ... */
216  _bt_relbuf(rel, insertstate.buf);
217  insertstate.buf = InvalidBuffer;
218 
219  /*
220  * If it's a speculative insertion, wait for it to finish (ie. to
221  * go ahead with the insertion, or kill the tuple). Otherwise
222  * wait for the transaction to finish as usual.
223  */
224  if (speculativeToken)
225  SpeculativeInsertionWait(xwait, speculativeToken);
226  else
227  XactLockTableWait(xwait, rel, &itup->t_tid, XLTW_InsertIndex);
228 
229  /* start over... */
230  if (stack)
231  _bt_freestack(stack);
232  goto search;
233  }
234 
235  /* Uniqueness is established -- restore heap tid as scantid */
236  if (itup_key->heapkeyspace)
237  itup_key->scantid = &itup->t_tid;
238  }
239 
240  if (checkUnique != UNIQUE_CHECK_EXISTING)
241  {
242  OffsetNumber newitemoff;
243 
244  /*
245  * The only conflict predicate locking cares about for indexes is when
246  * an index tuple insert conflicts with an existing lock. We don't
247  * know the actual page we're going to insert on for sure just yet in
248  * checkingunique and !heapkeyspace cases, but it's okay to use the
249  * first page the value could be on (with scantid omitted) instead.
250  */
252 
253  /*
254  * Do the insertion. Note that insertstate contains cached binary
255  * search bounds established within _bt_check_unique when insertion is
256  * checkingunique.
257  */
258  newitemoff = _bt_findinsertloc(rel, &insertstate, checkingunique,
259  indexUnchanged, stack, heapRel);
260  _bt_insertonpg(rel, heapRel, itup_key, insertstate.buf, InvalidBuffer,
261  stack, itup, insertstate.itemsz, newitemoff,
262  insertstate.postingoff, false);
263  }
264  else
265  {
266  /* just release the buffer */
267  _bt_relbuf(rel, insertstate.buf);
268  }
269 
270  /* be tidy */
271  if (stack)
272  _bt_freestack(stack);
273  pfree(itup_key);
274 
275  return is_unique;
276 }
unsigned int uint32
Definition: c.h:493
@ UNIQUE_CHECK_NO
Definition: genam.h:117
@ UNIQUE_CHECK_EXISTING
Definition: genam.h:120
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:667
void SpeculativeInsertionWait(TransactionId xid, uint32 token)
Definition: lmgr.c:824
@ XLTW_InsertIndex
Definition: lmgr.h:31
static BTStack _bt_search_insert(Relation rel, Relation heaprel, BTInsertState insertstate)
Definition: nbtinsert.c:317
static OffsetNumber _bt_findinsertloc(Relation rel, BTInsertState insertstate, bool checkingunique, bool indexUnchanged, BTStack stack, Relation heapRel)
Definition: nbtinsert.c:815
static void _bt_insertonpg(Relation rel, Relation heaprel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, BTStack stack, IndexTuple itup, Size itemsz, OffsetNumber newitemoff, int postingoff, bool split_only_page)
Definition: nbtinsert.c:1105
static TransactionId _bt_check_unique(Relation rel, BTInsertState insertstate, Relation heapRel, IndexUniqueCheck checkUnique, bool *is_unique, uint32 *speculativeToken)
Definition: nbtinsert.c:408
void _bt_freestack(BTStack stack)
Definition: nbtutils.c:173
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
Definition: nbtutils.c:81
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4316
IndexTuple itup
Definition: nbtree.h:811
ItemPointer scantid
Definition: nbtree.h:791
bool heapkeyspace
Definition: nbtree.h:786
bool anynullkeys
Definition: nbtree.h:788
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References _bt_check_unique(), _bt_findinsertloc(), _bt_freestack(), _bt_insertonpg(), _bt_mkscankey(), _bt_relbuf(), _bt_search_insert(), BTScanInsertData::anynullkeys, Assert(), BTInsertStateData::bounds_valid, BTInsertStateData::buf, BufferGetBlockNumber(), CheckForSerializableConflictIn(), BTScanInsertData::heapkeyspace, IndexTupleSize, InvalidBuffer, BTInsertStateData::itemsz, BTInsertStateData::itup, BTInsertStateData::itup_key, MAXALIGN, pfree(), BTInsertStateData::postingoff, BTScanInsertData::scantid, SpeculativeInsertionWait(), IndexTupleData::t_tid, TransactionIdIsValid, UNIQUE_CHECK_EXISTING, UNIQUE_CHECK_NO, unlikely, XactLockTableWait(), and XLTW_InsertIndex.

Referenced by btinsert().

◆ _bt_end_vacuum()

void _bt_end_vacuum ( Relation  rel)

Definition at line 2084 of file nbtutils.c.

2085 {
2086  int i;
2087 
2088  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
2089 
2090  /* Find the array entry */
2091  for (i = 0; i < btvacinfo->num_vacuums; i++)
2092  {
2093  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
2094 
2095  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
2096  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
2097  {
2098  /* Remove it by shifting down the last entry */
2099  *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
2101  break;
2102  }
2103  }
2104 
2105  LWLockRelease(BtreeVacuumLock);
2106 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1172
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1785
@ LW_EXCLUSIVE
Definition: lwlock.h:116
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1980
LockRelId relid
Definition: nbtutils.c:1968
int num_vacuums
Definition: nbtutils.c:1975
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:1977
LockRelId lockRelId
Definition: rel.h:46
Oid relId
Definition: rel.h:40
Oid dbId
Definition: rel.h:41
LockInfoData rd_lockInfo
Definition: rel.h:114

References btvacinfo, LockRelId::dbId, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by _bt_end_vacuum_callback(), and btbulkdelete().

◆ _bt_end_vacuum_callback()

void _bt_end_vacuum_callback ( int  code,
Datum  arg 
)

Definition at line 2112 of file nbtutils.c.

2113 {
2115 }
void _bt_end_vacuum(Relation rel)
Definition: nbtutils.c:2084
void * arg
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

References _bt_end_vacuum(), arg, and DatumGetPointer().

Referenced by btbulkdelete().

◆ _bt_findsplitloc()

OffsetNumber _bt_findsplitloc ( Relation  rel,
Page  origpage,
OffsetNumber  newitemoff,
Size  newitemsz,
IndexTuple  newitem,
bool newitemonleft 
)

Definition at line 129 of file nbtsplitloc.c.

135 {
136  BTPageOpaque opaque;
137  int leftspace,
138  rightspace,
139  olddataitemstotal,
140  olddataitemstoleft,
141  perfectpenalty,
142  leaffillfactor;
144  FindSplitStrat strategy;
145  ItemId itemid;
146  OffsetNumber offnum,
147  maxoff,
148  firstrightoff;
149  double fillfactormult;
150  bool usemult;
151  SplitPoint leftpage,
152  rightpage;
153 
154  opaque = BTPageGetOpaque(origpage);
155  maxoff = PageGetMaxOffsetNumber(origpage);
156 
157  /* Total free space available on a btree page, after fixed overhead */
158  leftspace = rightspace =
160  MAXALIGN(sizeof(BTPageOpaqueData));
161 
162  /* The right page will have the same high key as the old page */
163  if (!P_RIGHTMOST(opaque))
164  {
165  itemid = PageGetItemId(origpage, P_HIKEY);
166  rightspace -= (int) (MAXALIGN(ItemIdGetLength(itemid)) +
167  sizeof(ItemIdData));
168  }
169 
170  /* Count up total space in data items before actually scanning 'em */
171  olddataitemstotal = rightspace - (int) PageGetExactFreeSpace(origpage);
172  leaffillfactor = BTGetFillFactor(rel);
173 
174  /* Passed-in newitemsz is MAXALIGNED but does not include line pointer */
175  newitemsz += sizeof(ItemIdData);
176  state.rel = rel;
177  state.origpage = origpage;
178  state.newitem = newitem;
179  state.newitemsz = newitemsz;
180  state.is_leaf = P_ISLEAF(opaque);
181  state.is_rightmost = P_RIGHTMOST(opaque);
182  state.leftspace = leftspace;
183  state.rightspace = rightspace;
184  state.olddataitemstotal = olddataitemstotal;
185  state.minfirstrightsz = SIZE_MAX;
186  state.newitemoff = newitemoff;
187 
188  /* newitem cannot be a posting list item */
189  Assert(!BTreeTupleIsPosting(newitem));
190 
191  /*
192  * nsplits should never exceed maxoff because there will be at most as
193  * many candidate split points as there are points _between_ tuples, once
194  * you imagine that the new item is already on the original page (the
195  * final number of splits may be slightly lower because not all points
196  * between tuples will be legal).
197  */
198  state.maxsplits = maxoff;
199  state.splits = palloc(sizeof(SplitPoint) * state.maxsplits);
200  state.nsplits = 0;
201 
202  /*
203  * Scan through the data items and calculate space usage for a split at
204  * each possible position
205  */
206  olddataitemstoleft = 0;
207 
208  for (offnum = P_FIRSTDATAKEY(opaque);
209  offnum <= maxoff;
210  offnum = OffsetNumberNext(offnum))
211  {
212  Size itemsz;
213 
214  itemid = PageGetItemId(origpage, offnum);
215  itemsz = MAXALIGN(ItemIdGetLength(itemid)) + sizeof(ItemIdData);
216 
217  /*
218  * When item offset number is not newitemoff, neither side of the
219  * split can be newitem. Record a split after the previous data item
220  * from original page, but before the current data item from original
221  * page. (_bt_recsplitloc() will reject the split when there are no
222  * previous items, which we rely on.)
223  */
224  if (offnum < newitemoff)
225  _bt_recsplitloc(&state, offnum, false, olddataitemstoleft, itemsz);
226  else if (offnum > newitemoff)
227  _bt_recsplitloc(&state, offnum, true, olddataitemstoleft, itemsz);
228  else
229  {
230  /*
231  * Record a split after all "offnum < newitemoff" original page
232  * data items, but before newitem
233  */
234  _bt_recsplitloc(&state, offnum, false, olddataitemstoleft, itemsz);
235 
236  /*
237  * Record a split after newitem, but before data item from
238  * original page at offset newitemoff/current offset
239  */
240  _bt_recsplitloc(&state, offnum, true, olddataitemstoleft, itemsz);
241  }
242 
243  olddataitemstoleft += itemsz;
244  }
245 
246  /*
247  * Record a split after all original page data items, but before newitem.
248  * (Though only when it's possible that newitem will end up alone on new
249  * right page.)
250  */
251  Assert(olddataitemstoleft == olddataitemstotal);
252  if (newitemoff > maxoff)
253  _bt_recsplitloc(&state, newitemoff, false, olddataitemstotal, 0);
254 
255  /*
256  * I believe it is not possible to fail to find a feasible split, but just
257  * in case ...
258  */
259  if (state.nsplits == 0)
260  elog(ERROR, "could not find a feasible split point for index \"%s\"",
262 
263  /*
264  * Start search for a split point among list of legal split points. Give
265  * primary consideration to equalizing available free space in each half
266  * of the split initially (start with default strategy), while applying
267  * rightmost and split-after-new-item optimizations where appropriate.
268  * Either of the two other fallback strategies may be required for cases
269  * with a large number of duplicates around the original/space-optimal
270  * split point.
271  *
272  * Default strategy gives some weight to suffix truncation in deciding a
273  * split point on leaf pages. It attempts to select a split point where a
274  * distinguishing attribute appears earlier in the new high key for the
275  * left side of the split, in order to maximize the number of trailing
276  * attributes that can be truncated away. Only candidate split points
277  * that imply an acceptable balance of free space on each side are
278  * considered. See _bt_defaultinterval().
279  */
280  if (!state.is_leaf)
281  {
282  /* fillfactormult only used on rightmost page */
283  usemult = state.is_rightmost;
284  fillfactormult = BTREE_NONLEAF_FILLFACTOR / 100.0;
285  }
286  else if (state.is_rightmost)
287  {
288  /* Rightmost leaf page -- fillfactormult always used */
289  usemult = true;
290  fillfactormult = leaffillfactor / 100.0;
291  }
292  else if (_bt_afternewitemoff(&state, maxoff, leaffillfactor, &usemult))
293  {
294  /*
295  * New item inserted at rightmost point among a localized grouping on
296  * a leaf page -- apply "split after new item" optimization, either by
297  * applying leaf fillfactor multiplier, or by choosing the exact split
298  * point that leaves newitem as lastleft. (usemult is set for us.)
299  */
300  if (usemult)
301  {
302  /* fillfactormult should be set based on leaf fillfactor */
303  fillfactormult = leaffillfactor / 100.0;
304  }
305  else
306  {
307  /* find precise split point after newitemoff */
308  for (int i = 0; i < state.nsplits; i++)
309  {
310  SplitPoint *split = state.splits + i;
311 
312  if (split->newitemonleft &&
313  newitemoff == split->firstrightoff)
314  {
315  pfree(state.splits);
316  *newitemonleft = true;
317  return newitemoff;
318  }
319  }
320 
321  /*
322  * Cannot legally split after newitemoff; proceed with split
323  * without using fillfactor multiplier. This is defensive, and
324  * should never be needed in practice.
325  */
326  fillfactormult = 0.50;
327  }
328  }
329  else
330  {
331  /* Other leaf page. 50:50 page split. */
332  usemult = false;
333  /* fillfactormult not used, but be tidy */
334  fillfactormult = 0.50;
335  }
336 
337  /*
338  * Save leftmost and rightmost splits for page before original ordinal
339  * sort order is lost by delta/fillfactormult sort
340  */
341  leftpage = state.splits[0];
342  rightpage = state.splits[state.nsplits - 1];
343 
344  /* Give split points a fillfactormult-wise delta, and sort on deltas */
345  _bt_deltasortsplits(&state, fillfactormult, usemult);
346 
347  /* Determine split interval for default strategy */
348  state.interval = _bt_defaultinterval(&state);
349 
350  /*
351  * Determine if default strategy/split interval will produce a
352  * sufficiently distinguishing split, or if we should change strategies.
353  * Alternative strategies change the range of split points that are
354  * considered acceptable (split interval), and possibly change
355  * fillfactormult, in order to deal with pages with a large number of
356  * duplicates gracefully.
357  *
358  * Pass low and high splits for the entire page (actually, they're for an
359  * imaginary version of the page that includes newitem). These are used
360  * when the initial split interval encloses split points that are full of
361  * duplicates, and we need to consider if it's even possible to avoid
362  * appending a heap TID.
363  */
364  perfectpenalty = _bt_strategy(&state, &leftpage, &rightpage, &strategy);
365 
366  if (strategy == SPLIT_DEFAULT)
367  {
368  /*
369  * Default strategy worked out (always works out with internal page).
370  * Original split interval still stands.
371  */
372  }
373 
374  /*
375  * Many duplicates strategy is used when a heap TID would otherwise be
376  * appended, but the page isn't completely full of logical duplicates.
377  *
378  * The split interval is widened to include all legal candidate split
379  * points. There might be a few as two distinct values in the whole-page
380  * split interval, though it's also possible that most of the values on
381  * the page are unique. The final split point will either be to the
382  * immediate left or to the immediate right of the group of duplicate
383  * tuples that enclose the first/delta-optimal split point (perfect
384  * penalty was set so that the lowest delta split point that avoids
385  * appending a heap TID will be chosen). Maximizing the number of
386  * attributes that can be truncated away is not a goal of the many
387  * duplicates strategy.
388  *
389  * Single value strategy is used when it is impossible to avoid appending
390  * a heap TID. It arranges to leave the left page very full. This
391  * maximizes space utilization in cases where tuples with the same
392  * attribute values span many pages. Newly inserted duplicates will tend
393  * to have higher heap TID values, so we'll end up splitting to the right
394  * consistently. (Single value strategy is harmless though not
395  * particularly useful with !heapkeyspace indexes.)
396  */
397  else if (strategy == SPLIT_MANY_DUPLICATES)
398  {
399  Assert(state.is_leaf);
400  /* Shouldn't try to truncate away extra user attributes */
401  Assert(perfectpenalty ==
403  /* No need to resort splits -- no change in fillfactormult/deltas */
404  state.interval = state.nsplits;
405  }
406  else if (strategy == SPLIT_SINGLE_VALUE)
407  {
408  Assert(state.is_leaf);
409  /* Split near the end of the page */
410  usemult = true;
411  fillfactormult = BTREE_SINGLEVAL_FILLFACTOR / 100.0;
412  /* Resort split points with new delta */
413  _bt_deltasortsplits(&state, fillfactormult, usemult);
414  /* Appending a heap TID is unavoidable, so interval of 1 is fine */
415  state.interval = 1;
416  }
417 
418  /*
419  * Search among acceptable split points (using final split interval) for
420  * the entry that has the lowest penalty, and is therefore expected to
421  * maximize fan-out. Sets *newitemonleft for us.
422  */
423  firstrightoff = _bt_bestsplitloc(&state, perfectpenalty, newitemonleft,
424  strategy);
425  pfree(state.splits);
426 
427  return firstrightoff;
428 }
#define BTREE_SINGLEVAL_FILLFACTOR
Definition: nbtree.h:202
#define BTGetFillFactor(relation)
Definition: nbtree.h:1097
#define BTREE_NONLEAF_FILLFACTOR
Definition: nbtree.h:201
static void _bt_deltasortsplits(FindSplitData *state, double fillfactormult, bool usemult)
Definition: nbtsplitloc.c:566
static int _bt_strategy(FindSplitData *state, SplitPoint *leftpage, SplitPoint *rightpage, FindSplitStrat *strategy)
Definition: nbtsplitloc.c:934
static bool _bt_afternewitemoff(FindSplitData *state, OffsetNumber maxoff, int leaffillfactor, bool *usemult)
Definition: nbtsplitloc.c:630
static void _bt_recsplitloc(FindSplitData *state, OffsetNumber firstrightoff, bool newitemonleft, int olddataitemstoleft, Size firstrightofforigpagetuplesz)
Definition: nbtsplitloc.c:449
FindSplitStrat
Definition: nbtsplitloc.c:21
@ SPLIT_DEFAULT
Definition: nbtsplitloc.c:23
@ SPLIT_MANY_DUPLICATES
Definition: nbtsplitloc.c:24
@ SPLIT_SINGLE_VALUE
Definition: nbtsplitloc.c:25
static OffsetNumber _bt_bestsplitloc(FindSplitData *state, int perfectpenalty, bool *newitemonleft, FindSplitStrat strategy)
Definition: nbtsplitloc.c:788
static int _bt_defaultinterval(FindSplitData *state)
Definition: nbtsplitloc.c:876
bool newitemonleft
Definition: nbtsplitloc.c:37
OffsetNumber firstrightoff
Definition: nbtsplitloc.c:36

References _bt_afternewitemoff(), _bt_bestsplitloc(), _bt_defaultinterval(), _bt_deltasortsplits(), _bt_recsplitloc(), _bt_strategy(), Assert(), BTGetFillFactor, BTPageGetOpaque, BTREE_NONLEAF_FILLFACTOR, BTREE_SINGLEVAL_FILLFACTOR, BTreeTupleIsPosting(), elog, ERROR, SplitPoint::firstrightoff, i, IndexRelationGetNumberOfKeyAttributes, ItemIdGetLength, MAXALIGN, SplitPoint::newitemonleft, OffsetNumberNext, P_FIRSTDATAKEY, P_HIKEY, P_ISLEAF, P_RIGHTMOST, PageGetExactFreeSpace(), PageGetItemId(), PageGetMaxOffsetNumber(), PageGetPageSize(), palloc(), pfree(), RelationGetRelationName, SizeOfPageHeaderData, SPLIT_DEFAULT, SPLIT_MANY_DUPLICATES, and SPLIT_SINGLE_VALUE.

Referenced by _bt_split().

◆ _bt_finish_split()

void _bt_finish_split ( Relation  rel,
Relation  heaprel,
Buffer  lbuf,
BTStack  stack 
)

Definition at line 2241 of file nbtinsert.c.

2242 {
2243  Page lpage = BufferGetPage(lbuf);
2244  BTPageOpaque lpageop = BTPageGetOpaque(lpage);
2245  Buffer rbuf;
2246  Page rpage;
2247  BTPageOpaque rpageop;
2248  bool wasroot;
2249  bool wasonly;
2250 
2251  Assert(P_INCOMPLETE_SPLIT(lpageop));
2252  Assert(heaprel != NULL);
2253 
2254  /* Lock right sibling, the one missing the downlink */
2255  rbuf = _bt_getbuf(rel, lpageop->btpo_next, BT_WRITE);
2256  rpage = BufferGetPage(rbuf);
2257  rpageop = BTPageGetOpaque(rpage);
2258 
2259  /* Could this be a root split? */
2260  if (!stack)
2261  {
2262  Buffer metabuf;
2263  Page metapg;
2264  BTMetaPageData *metad;
2265 
2266  /* acquire lock on the metapage */
2267  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE);
2268  metapg = BufferGetPage(metabuf);
2269  metad = BTPageGetMeta(metapg);
2270 
2271  wasroot = (metad->btm_root == BufferGetBlockNumber(lbuf));
2272 
2273  _bt_relbuf(rel, metabuf);
2274  }
2275  else
2276  wasroot = false;
2277 
2278  /* Was this the only page on the level before split? */
2279  wasonly = (P_LEFTMOST(lpageop) && P_RIGHTMOST(rpageop));
2280 
2281  elog(DEBUG1, "finishing incomplete split of %u/%u",
2283 
2284  _bt_insert_parent(rel, heaprel, lbuf, rbuf, stack, wasroot, wasonly);
2285 }
static void _bt_insert_parent(Relation rel, Relation heaprel, Buffer buf, Buffer rbuf, BTStack stack, bool isroot, bool isonly)
Definition: nbtinsert.c:2099
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:845
#define BTPageGetMeta(p)
Definition: nbtree.h:121
#define P_LEFTMOST(opaque)
Definition: nbtree.h:218
#define P_INCOMPLETE_SPLIT(opaque)
Definition: nbtree.h:227
#define BTREE_METAPAGE
Definition: nbtree.h:148
#define BT_WRITE
Definition: nbtree.h:720
BlockNumber btm_root
Definition: nbtree.h:107
BlockNumber btpo_next
Definition: nbtree.h:65

References _bt_getbuf(), _bt_insert_parent(), _bt_relbuf(), Assert(), BT_WRITE, BTMetaPageData::btm_root, BTPageGetMeta, BTPageGetOpaque, BTPageOpaqueData::btpo_next, BTREE_METAPAGE, BufferGetBlockNumber(), BufferGetPage(), DEBUG1, elog, P_INCOMPLETE_SPLIT, P_LEFTMOST, and P_RIGHTMOST.

Referenced by _bt_getstackbuf(), _bt_moveright(), and _bt_stepright().

◆ _bt_first()

bool _bt_first ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 876 of file nbtsearch.c.

877 {
878  Relation rel = scan->indexRelation;
879  BTScanOpaque so = (BTScanOpaque) scan->opaque;
880  Buffer buf;
881  BTStack stack;
882  OffsetNumber offnum;
883  StrategyNumber strat;
884  BTScanInsertData inskey;
885  ScanKey startKeys[INDEX_MAX_KEYS];
886  ScanKeyData notnullkeys[INDEX_MAX_KEYS];
887  int keysz = 0;
888  int i;
889  bool status;
890  StrategyNumber strat_total;
891  BTScanPosItem *currItem;
892  BlockNumber blkno;
893 
895 
897 
898  /*
899  * Examine the scan keys and eliminate any redundant keys; also mark the
900  * keys that must be matched to continue the scan.
901  */
902  _bt_preprocess_keys(scan);
903 
904  /*
905  * Quit now if _bt_preprocess_keys() discovered that the scan keys can
906  * never be satisfied (eg, x == 1 AND x > 2).
907  */
908  if (!so->qual_ok)
909  {
910  /* Notify any other workers that we're done with this scan key. */
911  _bt_parallel_done(scan);
912  return false;
913  }
914 
915  /*
916  * For parallel scans, get the starting page from shared state. If the
917  * scan has not started, proceed to find out first leaf page in the usual
918  * way while keeping other participating processes waiting. If the scan
919  * has already begun, use the page number from the shared structure.
920  */
921  if (scan->parallel_scan != NULL)
922  {
923  status = _bt_parallel_seize(scan, &blkno);
924  if (!status)
925  return false;
926  else if (blkno == P_NONE)
927  {
928  _bt_parallel_done(scan);
929  return false;
930  }
931  else if (blkno != InvalidBlockNumber)
932  {
933  if (!_bt_parallel_readpage(scan, blkno, dir))
934  return false;
935  goto readcomplete;
936  }
937  }
938 
939  /*----------
940  * Examine the scan keys to discover where we need to start the scan.
941  *
942  * We want to identify the keys that can be used as starting boundaries;
943  * these are =, >, or >= keys for a forward scan or =, <, <= keys for
944  * a backwards scan. We can use keys for multiple attributes so long as
945  * the prior attributes had only =, >= (resp. =, <=) keys. Once we accept
946  * a > or < boundary or find an attribute with no boundary (which can be
947  * thought of as the same as "> -infinity"), we can't use keys for any
948  * attributes to its right, because it would break our simplistic notion
949  * of what initial positioning strategy to use.
950  *
951  * When the scan keys include cross-type operators, _bt_preprocess_keys
952  * may not be able to eliminate redundant keys; in such cases we will
953  * arbitrarily pick a usable one for each attribute. This is correct
954  * but possibly not optimal behavior. (For example, with keys like
955  * "x >= 4 AND x >= 5" we would elect to scan starting at x=4 when
956  * x=5 would be more efficient.) Since the situation only arises given
957  * a poorly-worded query plus an incomplete opfamily, live with it.
958  *
959  * When both equality and inequality keys appear for a single attribute
960  * (again, only possible when cross-type operators appear), we *must*
961  * select one of the equality keys for the starting point, because
962  * _bt_checkkeys() will stop the scan as soon as an equality qual fails.
963  * For example, if we have keys like "x >= 4 AND x = 10" and we elect to
964  * start at x=4, we will fail and stop before reaching x=10. If multiple
965  * equality quals survive preprocessing, however, it doesn't matter which
966  * one we use --- by definition, they are either redundant or
967  * contradictory.
968  *
969  * Any regular (not SK_SEARCHNULL) key implies a NOT NULL qualifier.
970  * If the index stores nulls at the end of the index we'll be starting
971  * from, and we have no boundary key for the column (which means the key
972  * we deduced NOT NULL from is an inequality key that constrains the other
973  * end of the index), then we cons up an explicit SK_SEARCHNOTNULL key to
974  * use as a boundary key. If we didn't do this, we might find ourselves
975  * traversing a lot of null entries at the start of the scan.
976  *
977  * In this loop, row-comparison keys are treated the same as keys on their
978  * first (leftmost) columns. We'll add on lower-order columns of the row
979  * comparison below, if possible.
980  *
981  * The selected scan keys (at most one per index column) are remembered by
982  * storing their addresses into the local startKeys[] array.
983  *----------
984  */
985  strat_total = BTEqualStrategyNumber;
986  if (so->numberOfKeys > 0)
987  {
988  AttrNumber curattr;
989  ScanKey chosen;
990  ScanKey impliesNN;
991  ScanKey cur;
992 
993  /*
994  * chosen is the so-far-chosen key for the current attribute, if any.
995  * We don't cast the decision in stone until we reach keys for the
996  * next attribute.
997  */
998  curattr = 1;
999  chosen = NULL;
1000  /* Also remember any scankey that implies a NOT NULL constraint */
1001  impliesNN = NULL;
1002 
1003  /*
1004  * Loop iterates from 0 to numberOfKeys inclusive; we use the last
1005  * pass to handle after-last-key processing. Actual exit from the
1006  * loop is at one of the "break" statements below.
1007  */
1008  for (cur = so->keyData, i = 0;; cur++, i++)
1009  {
1010  if (i >= so->numberOfKeys || cur->sk_attno != curattr)
1011  {
1012  /*
1013  * Done looking at keys for curattr. If we didn't find a
1014  * usable boundary key, see if we can deduce a NOT NULL key.
1015  */
1016  if (chosen == NULL && impliesNN != NULL &&
1017  ((impliesNN->sk_flags & SK_BT_NULLS_FIRST) ?
1018  ScanDirectionIsForward(dir) :
1020  {
1021  /* Yes, so build the key in notnullkeys[keysCount] */
1022  chosen = &notnullkeys[keysz];
1023  ScanKeyEntryInitialize(chosen,
1025  (impliesNN->sk_flags &
1027  curattr,
1028  ((impliesNN->sk_flags & SK_BT_NULLS_FIRST) ?
1031  InvalidOid,
1032  InvalidOid,
1033  InvalidOid,
1034  (Datum) 0);
1035  }
1036 
1037  /*
1038  * If we still didn't find a usable boundary key, quit; else
1039  * save the boundary key pointer in startKeys.
1040  */
1041  if (chosen == NULL)
1042  break;
1043  startKeys[keysz++] = chosen;
1044 
1045  /*
1046  * Adjust strat_total, and quit if we have stored a > or <
1047  * key.
1048  */
1049  strat = chosen->sk_strategy;
1050  if (strat != BTEqualStrategyNumber)
1051  {
1052  strat_total = strat;
1053  if (strat == BTGreaterStrategyNumber ||
1054  strat == BTLessStrategyNumber)
1055  break;
1056  }
1057 
1058  /*
1059  * Done if that was the last attribute, or if next key is not
1060  * in sequence (implying no boundary key is available for the
1061  * next attribute).
1062  */
1063  if (i >= so->numberOfKeys ||
1064  cur->sk_attno != curattr + 1)
1065  break;
1066 
1067  /*
1068  * Reset for next attr.
1069  */
1070  curattr = cur->sk_attno;
1071  chosen = NULL;
1072  impliesNN = NULL;
1073  }
1074 
1075  /*
1076  * Can we use this key as a starting boundary for this attr?
1077  *
1078  * If not, does it imply a NOT NULL constraint? (Because
1079  * SK_SEARCHNULL keys are always assigned BTEqualStrategyNumber,
1080  * *any* inequality key works for that; we need not test.)
1081  */
1082  switch (cur->sk_strategy)
1083  {
1084  case BTLessStrategyNumber:
1086  if (chosen == NULL)
1087  {
1088  if (ScanDirectionIsBackward(dir))
1089  chosen = cur;
1090  else
1091  impliesNN = cur;
1092  }
1093  break;
1094  case BTEqualStrategyNumber:
1095  /* override any non-equality choice */
1096  chosen = cur;
1097  break;
1100  if (chosen == NULL)
1101  {
1102  if (ScanDirectionIsForward(dir))
1103  chosen = cur;
1104  else
1105  impliesNN = cur;
1106  }
1107  break;
1108  }
1109  }
1110  }
1111 
1112  /*
1113  * If we found no usable boundary keys, we have to start from one end of
1114  * the tree. Walk down that edge to the first or last key, and scan from
1115  * there.
1116  */
1117  if (keysz == 0)
1118  {
1119  bool match;
1120 
1121  match = _bt_endpoint(scan, dir);
1122 
1123  if (!match)
1124  {
1125  /* No match, so mark (parallel) scan finished */
1126  _bt_parallel_done(scan);
1127  }
1128 
1129  return match;
1130  }
1131 
1132  /*
1133  * We want to start the scan somewhere within the index. Set up an
1134  * insertion scankey we can use to search for the boundary point we
1135  * identified above. The insertion scankey is built using the keys
1136  * identified by startKeys[]. (Remaining insertion scankey fields are
1137  * initialized after initial-positioning strategy is finalized.)
1138  */
1139  Assert(keysz <= INDEX_MAX_KEYS);
1140  for (i = 0; i < keysz; i++)
1141  {
1142  ScanKey cur = startKeys[i];
1143 
1144  Assert(cur->sk_attno == i + 1);
1145 
1146  if (cur->sk_flags & SK_ROW_HEADER)
1147  {
1148  /*
1149  * Row comparison header: look to the first row member instead.
1150  *
1151  * The member scankeys are already in insertion format (ie, they
1152  * have sk_func = 3-way-comparison function), but we have to watch
1153  * out for nulls, which _bt_preprocess_keys didn't check. A null
1154  * in the first row member makes the condition unmatchable, just
1155  * like qual_ok = false.
1156  */
1157  ScanKey subkey = (ScanKey) DatumGetPointer(cur->sk_argument);
1158 
1159  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1160  if (subkey->sk_flags & SK_ISNULL)
1161  {
1162  _bt_parallel_done(scan);
1163  return false;
1164  }
1165  memcpy(inskey.scankeys + i, subkey, sizeof(ScanKeyData));
1166 
1167  /*
1168  * If the row comparison is the last positioning key we accepted,
1169  * try to add additional keys from the lower-order row members.
1170  * (If we accepted independent conditions on additional index
1171  * columns, we use those instead --- doesn't seem worth trying to
1172  * determine which is more restrictive.) Note that this is OK
1173  * even if the row comparison is of ">" or "<" type, because the
1174  * condition applied to all but the last row member is effectively
1175  * ">=" or "<=", and so the extra keys don't break the positioning
1176  * scheme. But, by the same token, if we aren't able to use all
1177  * the row members, then the part of the row comparison that we
1178  * did use has to be treated as just a ">=" or "<=" condition, and
1179  * so we'd better adjust strat_total accordingly.
1180  */
1181  if (i == keysz - 1)
1182  {
1183  bool used_all_subkeys = false;
1184 
1185  Assert(!(subkey->sk_flags & SK_ROW_END));
1186  for (;;)
1187  {
1188  subkey++;
1189  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1190  if (subkey->sk_attno != keysz + 1)
1191  break; /* out-of-sequence, can't use it */
1192  if (subkey->sk_strategy != cur->sk_strategy)
1193  break; /* wrong direction, can't use it */
1194  if (subkey->sk_flags & SK_ISNULL)
1195  break; /* can't use null keys */
1196  Assert(keysz < INDEX_MAX_KEYS);
1197  memcpy(inskey.scankeys + keysz, subkey,
1198  sizeof(ScanKeyData));
1199  keysz++;
1200  if (subkey->sk_flags & SK_ROW_END)
1201  {
1202  used_all_subkeys = true;
1203  break;
1204  }
1205  }
1206  if (!used_all_subkeys)
1207  {
1208  switch (strat_total)
1209  {
1210  case BTLessStrategyNumber:
1211  strat_total = BTLessEqualStrategyNumber;
1212  break;
1214  strat_total = BTGreaterEqualStrategyNumber;
1215  break;
1216  }
1217  }
1218  break; /* done with outer loop */
1219  }
1220  }
1221  else
1222  {
1223  /*
1224  * Ordinary comparison key. Transform the search-style scan key
1225  * to an insertion scan key by replacing the sk_func with the
1226  * appropriate btree comparison function.
1227  *
1228  * If scankey operator is not a cross-type comparison, we can use
1229  * the cached comparison function; otherwise gotta look it up in
1230  * the catalogs. (That can't lead to infinite recursion, since no
1231  * indexscan initiated by syscache lookup will use cross-data-type
1232  * operators.)
1233  *
1234  * We support the convention that sk_subtype == InvalidOid means
1235  * the opclass input type; this is a hack to simplify life for
1236  * ScanKeyInit().
1237  */
1238  if (cur->sk_subtype == rel->rd_opcintype[i] ||
1239  cur->sk_subtype == InvalidOid)
1240  {
1241  FmgrInfo *procinfo;
1242 
1243  procinfo = index_getprocinfo(rel, cur->sk_attno, BTORDER_PROC);
1244  ScanKeyEntryInitializeWithInfo(inskey.scankeys + i,
1245  cur->sk_flags,
1246  cur->sk_attno,
1248  cur->sk_subtype,
1249  cur->sk_collation,
1250  procinfo,
1251  cur->sk_argument);
1252  }
1253  else
1254  {
1255  RegProcedure cmp_proc;
1256 
1257  cmp_proc = get_opfamily_proc(rel->rd_opfamily[i],
1258  rel->rd_opcintype[i],
1259  cur->sk_subtype,
1260  BTORDER_PROC);
1261  if (!RegProcedureIsValid(cmp_proc))
1262  elog(ERROR, "missing support function %d(%u,%u) for attribute %d of index \"%s\"",
1263  BTORDER_PROC, rel->rd_opcintype[i], cur->sk_subtype,
1264  cur->sk_attno, RelationGetRelationName(rel));
1265  ScanKeyEntryInitialize(inskey.scankeys + i,
1266  cur->sk_flags,
1267  cur->sk_attno,
1269  cur->sk_subtype,
1270  cur->sk_collation,
1271  cmp_proc,
1272  cur->sk_argument);
1273  }
1274  }
1275  }
1276 
1277  /*----------
1278  * Examine the selected initial-positioning strategy to determine exactly
1279  * where we need to start the scan, and set flag variables to control the
1280  * initial descent by _bt_search (and our _bt_binsrch call for the leaf
1281  * page _bt_search returns).
1282  *----------
1283  */
1284  _bt_metaversion(rel, &inskey.heapkeyspace, &inskey.allequalimage);
1285  inskey.anynullkeys = false; /* unused */
1286  inskey.scantid = NULL;
1287  inskey.keysz = keysz;
1288  switch (strat_total)
1289  {
1290  case BTLessStrategyNumber:
1291 
1292  inskey.nextkey = false;
1293  inskey.backward = true;
1294  break;
1295 
1297 
1298  inskey.nextkey = true;
1299  inskey.backward = true;
1300  break;
1301 
1302  case BTEqualStrategyNumber:
1303 
1304  /*
1305  * If a backward scan was specified, need to start with last equal
1306  * item not first one.
1307  */
1308  if (ScanDirectionIsBackward(dir))
1309  {
1310  /*
1311  * This is the same as the <= strategy
1312  */
1313  inskey.nextkey = true;
1314  inskey.backward = true;
1315  }
1316  else
1317  {
1318  /*
1319  * This is the same as the >= strategy
1320  */
1321  inskey.nextkey = false;
1322  inskey.backward = false;
1323  }
1324  break;
1325 
1327 
1328  /*
1329  * Find first item >= scankey
1330  */
1331  inskey.nextkey = false;
1332  inskey.backward = false;
1333  break;
1334 
1336 
1337  /*
1338  * Find first item > scankey
1339  */
1340  inskey.nextkey = true;
1341  inskey.backward = false;
1342  break;
1343 
1344  default:
1345  /* can't get here, but keep compiler quiet */
1346  elog(ERROR, "unrecognized strat_total: %d", (int) strat_total);
1347  return false;
1348  }
1349 
1350  /*
1351  * Use the manufactured insertion scan key to descend the tree and
1352  * position ourselves on the target leaf page.
1353  */
1354  Assert(ScanDirectionIsBackward(dir) == inskey.backward);
1355  stack = _bt_search(rel, NULL, &inskey, &buf, BT_READ);
1356 
1357  /* don't need to keep the stack around... */
1358  _bt_freestack(stack);
1359 
1360  if (!BufferIsValid(buf))
1361  {
1362  /*
1363  * We only get here if the index is completely empty. Lock relation
1364  * because nothing finer to lock exists. Without a buffer lock, it's
1365  * possible for another transaction to insert data between
1366  * _bt_search() and PredicateLockRelation(). We have to try again
1367  * after taking the relation-level predicate lock, to close a narrow
1368  * window where we wouldn't scan concurrently inserted tuples, but the
1369  * writer wouldn't see our predicate lock.
1370  */
1372  {
1373  PredicateLockRelation(rel, scan->xs_snapshot);
1374  stack = _bt_search(rel, NULL, &inskey, &buf, BT_READ);
1375  _bt_freestack(stack);
1376  }
1377 
1378  if (!BufferIsValid(buf))
1379  {
1380  /*
1381  * Mark parallel scan as done, so that all the workers can finish
1382  * their scan.
1383  */
1384  _bt_parallel_done(scan);
1386  return false;
1387  }
1388  }
1389 
1391 
1392  _bt_initialize_more_data(so, dir);
1393 
1394  /* position to the precise item on the page */
1395  offnum = _bt_binsrch(rel, &inskey, buf);
1397  so->currPos.buf = buf;
1398 
1399  /*
1400  * Now load data from the first page of the scan.
1401  *
1402  * If inskey.nextkey = false and inskey.backward = false, offnum is
1403  * positioned at the first non-pivot tuple >= inskey.scankeys.
1404  *
1405  * If inskey.nextkey = false and inskey.backward = true, offnum is
1406  * positioned at the last non-pivot tuple < inskey.scankeys.
1407  *
1408  * If inskey.nextkey = true and inskey.backward = false, offnum is
1409  * positioned at the first non-pivot tuple > inskey.scankeys.
1410  *
1411  * If inskey.nextkey = true and inskey.backward = true, offnum is
1412  * positioned at the last non-pivot tuple <= inskey.scankeys.
1413  *
1414  * It's possible that _bt_binsrch returned an offnum that is out of bounds
1415  * for the page. For example, when inskey is both < the leaf page's high
1416  * key and > all of its non-pivot tuples, offnum will be "maxoff + 1".
1417  */
1418  if (!_bt_readpage(scan, dir, offnum, true))
1419  {
1420  /*
1421  * There's no actually-matching data on this page. Try to advance to
1422  * the next page. Return false if there's no matching data at all.
1423  */
1424  _bt_unlockbuf(scan->indexRelation, so->currPos.buf);
1425  if (!_bt_steppage(scan, dir))
1426  return false;
1427  }
1428  else
1429  {
1430  /* We have at least one item to return as scan's first item */
1432  }
1433 
1434 readcomplete:
1435  /* OK, itemIndex says what to return */
1436  currItem = &so->currPos.items[so->currPos.itemIndex];
1437  scan->xs_heaptid = currItem->heapTid;
1438  if (scan->xs_want_itup)
1439  scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
1440 
1441  return true;
1442 }
int16 AttrNumber
Definition: attnum.h:21
#define RegProcedureIsValid(p)
Definition: c.h:764
regproc RegProcedure
Definition: c.h:637
struct cursor * cur
Definition: ecpg.c:28
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:863
void _bt_metaversion(Relation rel, bool *heapkeyspace, bool *allequalimage)
Definition: nbtpage.c:739
void _bt_unlockbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1070
void _bt_parallel_done(IndexScanDesc scan)
Definition: nbtree.c:705
bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno)
Definition: nbtree.c:624
#define BTORDER_PROC
Definition: nbtree.h:707
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:1007
#define P_NONE
Definition: nbtree.h:212
#define BT_READ
Definition: nbtree.h:719
#define BTScanPosInvalidate(scanpos)
Definition: nbtree.h:1013
static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
Definition: nbtsearch.c:2217
static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)
Definition: nbtsearch.c:61
static OffsetNumber _bt_binsrch(Relation rel, BTScanInsert key, Buffer buf)
Definition: nbtsearch.c:337
static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:2441
static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:1944
BTStack _bt_search(Relation rel, Relation heaprel, BTScanInsert key, Buffer *bufP, int access)
Definition: nbtsearch.c:96
static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum, bool firstPage)
Definition: nbtsearch.c:1522
static void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir)
Definition: nbtsearch.c:2531
void _bt_preprocess_keys(IndexScanDesc scan)
Definition: nbtutils.c:755
#define INDEX_MAX_KEYS
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:625
#define InvalidOid
Definition: postgres_ext.h:36
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition: predicate.c:2579
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition: predicate.c:2556
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
#define SK_ROW_MEMBER
Definition: skey.h:118
#define SK_ROW_END
Definition: skey.h:119
ScanKeyData * ScanKey
Definition: skey.h:75
uint16 StrategyNumber
Definition: stratnum.h:22
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define InvalidStrategy
Definition: stratnum.h:24
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
BTScanPosData currPos
Definition: nbtree.h:1072
char * currTuples
Definition: nbtree.h:1059
Buffer buf
Definition: nbtree.h:953
BTScanPosItem items[MaxTIDsPerBTreePage]
Definition: nbtree.h:985
int itemIndex
Definition: nbtree.h:983
Definition: fmgr.h:57
IndexTuple xs_itup
Definition: relscan.h:142
ItemPointerData xs_heaptid
Definition: relscan.h:147
struct SnapshotData * xs_snapshot
Definition: relscan.h:119
StrategyNumber sk_strategy
Definition: skey.h:68
#define IsolationIsSerializable()
Definition: xact.h:52

References _bt_binsrch(), _bt_drop_lock_and_maybe_pin(), _bt_endpoint(), _bt_freestack(), _bt_initialize_more_data(), _bt_metaversion(), _bt_parallel_done(), _bt_parallel_readpage(), _bt_parallel_seize(), _bt_preprocess_keys(), _bt_readpage(), _bt_search(), _bt_steppage(), _bt_unlockbuf(), Assert(), BT_READ, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, BTScanPosInvalidate, BTScanPosIsValid, buf, BTScanPosData::buf, BufferGetBlockNumber(), BufferIsValid(), cur, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, DatumGetPointer(), elog, ERROR, get_opfamily_proc(), i, index_getprocinfo(), INDEX_MAX_KEYS, IndexScanDescData::indexRelation, InvalidBlockNumber, InvalidOid, InvalidStrategy, IsolationIsSerializable, BTScanPosData::itemIndex, BTScanPosData::items, BTScanOpaqueData::keyData, BTScanOpaqueData::numberOfKeys, IndexScanDescData::opaque, P_NONE, IndexScanDescData::parallel_scan, pgstat_count_index_scan, PredicateLockPage(), PredicateLockRelation(), BTScanOpaqueData::qual_ok, RelationData::rd_opcintype, RelationData::rd_opfamily, RegProcedureIsValid, RelationGetRelationName, ScanDirectionIsBackward, ScanDirectionIsForward, ScanKeyEntryInitialize(), ScanKeyEntryInitializeWithInfo(), ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_flags, SK_ISNULL, SK_ROW_END, SK_ROW_HEADER, SK_ROW_MEMBER, SK_SEARCHNOTNULL, ScanKeyData::sk_strategy, IndexScanDescData::xs_heaptid, IndexScanDescData::xs_itup, IndexScanDescData::xs_snapshot, and IndexScanDescData::xs_want_itup.

Referenced by btgetbitmap(), and btgettuple().

◆ _bt_form_posting()

IndexTuple _bt_form_posting ( IndexTuple  base,
ItemPointer  htids,
int  nhtids 
)

Definition at line 864 of file nbtdedup.c.

865 {
866  uint32 keysize,
867  newsize;
868  IndexTuple itup;
869 
870  if (BTreeTupleIsPosting(base))
871  keysize = BTreeTupleGetPostingOffset(base);
872  else
873  keysize = IndexTupleSize(base);
874 
875  Assert(!BTreeTupleIsPivot(base));
876  Assert(nhtids > 0 && nhtids <= PG_UINT16_MAX);
877  Assert(keysize == MAXALIGN(keysize));
878 
879  /* Determine final size of new tuple */
880  if (nhtids > 1)
881  newsize = MAXALIGN(keysize +
882  nhtids * sizeof(ItemPointerData));
883  else
884  newsize = keysize;
885 
886  Assert(newsize <= INDEX_SIZE_MASK);
887  Assert(newsize == MAXALIGN(newsize));
888 
889  /* Allocate memory using palloc0() (matches index_form_tuple()) */
890  itup = palloc0(newsize);
891  memcpy(itup, base, keysize);
892  itup->t_info &= ~INDEX_SIZE_MASK;
893  itup->t_info |= newsize;
894  if (nhtids > 1)
895  {
896  /* Form posting list tuple */
897  BTreeTupleSetPosting(itup, nhtids, keysize);
898  memcpy(BTreeTupleGetPosting(itup), htids,
899  sizeof(ItemPointerData) * nhtids);
900  Assert(_bt_posting_valid(itup));
901  }
902  else
903  {
904  /* Form standard non-pivot tuple */
905  itup->t_info &= ~INDEX_ALT_TID_MASK;
906  ItemPointerCopy(htids, &itup->t_tid);
908  }
909 
910  return itup;
911 }
#define PG_UINT16_MAX
Definition: c.h:574
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition: itemptr.h:172
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
void * palloc0(Size size)
Definition: mcxt.c:1334
static void BTreeTupleSetPosting(IndexTuple itup, uint16 nhtids, int postingoffset)
Definition: nbtree.h:504
#define INDEX_ALT_TID_MASK
Definition: nbtree.h:459
unsigned short t_info
Definition: itup.h:49

References Assert(), BTreeTupleGetPosting(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTreeTupleSetPosting(), INDEX_ALT_TID_MASK, INDEX_SIZE_MASK, IndexTupleSize, ItemPointerCopy(), ItemPointerIsValid(), MAXALIGN, palloc0(), PG_UINT16_MAX, IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_dedup_finish_pending(), _bt_sort_dedup_finish_pending(), and bt_posting_plain_tuple().

◆ _bt_freestack()

void _bt_freestack ( BTStack  stack)

Definition at line 173 of file nbtutils.c.

174 {
175  BTStack ostack;
176 
177  while (stack != NULL)
178  {
179  ostack = stack;
180  stack = stack->bts_parent;
181  pfree(ostack);
182  }
183 }
struct BTStackData * bts_parent
Definition: nbtree.h:736

References BTStackData::bts_parent, and pfree().

Referenced by _bt_doinsert(), _bt_first(), and bt_rootdescend().

◆ _bt_get_endpoint()

Buffer _bt_get_endpoint ( Relation  rel,
uint32  level,
bool  rightmost 
)

Definition at line 2360 of file nbtsearch.c.

2361 {
2362  Buffer buf;
2363  Page page;
2364  BTPageOpaque opaque;
2365  OffsetNumber offnum;
2366  BlockNumber blkno;
2367  IndexTuple itup;
2368 
2369  /*
2370  * If we are looking for a leaf page, okay to descend from fast root;
2371  * otherwise better descend from true root. (There is no point in being
2372  * smarter about intermediate levels.)
2373  */
2374  if (level == 0)
2375  buf = _bt_getroot(rel, NULL, BT_READ);
2376  else
2377  buf = _bt_gettrueroot(rel);
2378 
2379  if (!BufferIsValid(buf))
2380  return InvalidBuffer;
2381 
2382  page = BufferGetPage(buf);
2383  opaque = BTPageGetOpaque(page);
2384 
2385  for (;;)
2386  {
2387  /*
2388  * If we landed on a deleted page, step right to find a live page
2389  * (there must be one). Also, if we want the rightmost page, step
2390  * right if needed to get to it (this could happen if the page split
2391  * since we obtained a pointer to it).
2392  */
2393  while (P_IGNORE(opaque) ||
2394  (rightmost && !P_RIGHTMOST(opaque)))
2395  {
2396  blkno = opaque->btpo_next;
2397  if (blkno == P_NONE)
2398  elog(ERROR, "fell off the end of index \"%s\"",
2400  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
2401  page = BufferGetPage(buf);
2402  opaque = BTPageGetOpaque(page);
2403  }
2404 
2405  /* Done? */
2406  if (opaque->btpo_level == level)
2407  break;
2408  if (opaque->btpo_level < level)
2409  ereport(ERROR,
2410  (errcode(ERRCODE_INDEX_CORRUPTED),
2411  errmsg_internal("btree level %u not found in index \"%s\"",
2412  level, RelationGetRelationName(rel))));
2413 
2414  /* Descend to leftmost or rightmost child page */
2415  if (rightmost)
2416  offnum = PageGetMaxOffsetNumber(page);
2417  else
2418  offnum = P_FIRSTDATAKEY(opaque);
2419 
2420  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2421  blkno = BTreeTupleGetDownLink(itup);
2422 
2423  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
2424  page = BufferGetPage(buf);
2425  opaque = BTPageGetOpaque(page);
2426  }
2427 
2428  return buf;
2429 }
Buffer _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
Definition: nbtpage.c:1003
Buffer _bt_gettrueroot(Relation rel)
Definition: nbtpage.c:580
Buffer _bt_getroot(Relation rel, Relation heaprel, int access)
Definition: nbtpage.c:344
static BlockNumber BTreeTupleGetDownLink(IndexTuple pivot)
Definition: nbtree.h:556
uint32 btpo_level
Definition: nbtree.h:66

References _bt_getroot(), _bt_gettrueroot(), _bt_relandgetbuf(), BT_READ, BTPageGetOpaque, BTPageOpaqueData::btpo_level, BTPageOpaqueData::btpo_next, BTreeTupleGetDownLink(), buf, BufferGetPage(), BufferIsValid(), elog, ereport, errcode(), errmsg_internal(), ERROR, InvalidBuffer, P_FIRSTDATAKEY, P_IGNORE, P_NONE, P_RIGHTMOST, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), and RelationGetRelationName.

Referenced by _bt_endpoint(), and _bt_insert_parent().

◆ _bt_getbuf()

Buffer _bt_getbuf ( Relation  rel,
BlockNumber  blkno,
int  access 
)

Definition at line 845 of file nbtpage.c.

846 {
847  Buffer buf;
848 
849  Assert(BlockNumberIsValid(blkno));
850 
851  /* Read an existing block of the relation */
852  buf = ReadBuffer(rel, blkno);
853  _bt_lockbuf(rel, buf, access);
854  _bt_checkpage(rel, buf);
855 
856  return buf;
857 }
void _bt_checkpage(Relation rel, Buffer buf)
Definition: nbtpage.c:797
void _bt_lockbuf(Relation rel, Buffer buf, int access)
Definition: nbtpage.c:1039
short access
Definition: preproc-type.c:36

References _bt_checkpage(), _bt_lockbuf(), Assert(), BlockNumberIsValid(), buf, and ReadBuffer().

Referenced by _bt_finish_split(), _bt_getroot(), _bt_getrootheight(), _bt_getstackbuf(), _bt_gettrueroot(), _bt_insertonpg(), _bt_killitems(), _bt_leftsib_splitflag(), _bt_metaversion(), _bt_moveright(), _bt_newlevel(), _bt_pagedel(), _bt_readnextpage(), _bt_rightsib_halfdeadflag(), _bt_set_cleanup_info(), _bt_split(), _bt_unlink_halfdead_page(), _bt_vacuum_needs_cleanup(), and _bt_walk_left().

◆ _bt_getroot()

Buffer _bt_getroot ( Relation  rel,
Relation  heaprel,
int  access 
)

Definition at line 344 of file nbtpage.c.

345 {
346  Buffer metabuf;
347  Buffer rootbuf;
348  Page rootpage;
349  BTPageOpaque rootopaque;
350  BlockNumber rootblkno;
351  uint32 rootlevel;
352  BTMetaPageData *metad;
353 
354  Assert(access == BT_READ || heaprel != NULL);
355 
356  /*
357  * Try to use previously-cached metapage data to find the root. This
358  * normally saves one buffer access per index search, which is a very
359  * helpful savings in bufmgr traffic and hence contention.
360  */
361  if (rel->rd_amcache != NULL)
362  {
363  metad = (BTMetaPageData *) rel->rd_amcache;
364  /* We shouldn't have cached it if any of these fail */
365  Assert(metad->btm_magic == BTREE_MAGIC);
367  Assert(metad->btm_version <= BTREE_VERSION);
368  Assert(!metad->btm_allequalimage ||
370  Assert(metad->btm_root != P_NONE);
371 
372  rootblkno = metad->btm_fastroot;
373  Assert(rootblkno != P_NONE);
374  rootlevel = metad->btm_fastlevel;
375 
376  rootbuf = _bt_getbuf(rel, rootblkno, BT_READ);
377  rootpage = BufferGetPage(rootbuf);
378  rootopaque = BTPageGetOpaque(rootpage);
379 
380  /*
381  * Since the cache might be stale, we check the page more carefully
382  * here than normal. We *must* check that it's not deleted. If it's
383  * not alone on its level, then we reject too --- this may be overly
384  * paranoid but better safe than sorry. Note we don't check P_ISROOT,
385  * because that's not set in a "fast root".
386  */
387  if (!P_IGNORE(rootopaque) &&
388  rootopaque->btpo_level == rootlevel &&
389  P_LEFTMOST(rootopaque) &&
390  P_RIGHTMOST(rootopaque))
391  {
392  /* OK, accept cached page as the root */
393  return rootbuf;
394  }
395  _bt_relbuf(rel, rootbuf);
396  /* Cache is stale, throw it away */
397  if (rel->rd_amcache)
398  pfree(rel->rd_amcache);
399  rel->rd_amcache = NULL;
400  }
401 
402  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
403  metad = _bt_getmeta(rel, metabuf);
404 
405  /* if no root page initialized yet, do it */
406  if (metad->btm_root == P_NONE)
407  {
408  Page metapg;
409 
410  /* If access = BT_READ, caller doesn't want us to create root yet */
411  if (access == BT_READ)
412  {
413  _bt_relbuf(rel, metabuf);
414  return InvalidBuffer;
415  }
416 
417  /* trade in our read lock for a write lock */
418  _bt_unlockbuf(rel, metabuf);
419  _bt_lockbuf(rel, metabuf, BT_WRITE);
420 
421  /*
422  * Race condition: if someone else initialized the metadata between
423  * the time we released the read lock and acquired the write lock, we
424  * must avoid doing it again.
425  */
426  if (metad->btm_root != P_NONE)
427  {
428  /*
429  * Metadata initialized by someone else. In order to guarantee no
430  * deadlocks, we have to release the metadata page and start all
431  * over again. (Is that really true? But it's hardly worth trying
432  * to optimize this case.)
433  */
434  _bt_relbuf(rel, metabuf);
435  return _bt_getroot(rel, heaprel, access);
436  }
437 
438  /*
439  * Get, initialize, write, and leave a lock of the appropriate type on
440  * the new root page. Since this is the first page in the tree, it's
441  * a leaf as well as the root.
442  */
443  rootbuf = _bt_allocbuf(rel, heaprel);
444  rootblkno = BufferGetBlockNumber(rootbuf);
445  rootpage = BufferGetPage(rootbuf);
446  rootopaque = BTPageGetOpaque(rootpage);
447  rootopaque->btpo_prev = rootopaque->btpo_next = P_NONE;
448  rootopaque->btpo_flags = (BTP_LEAF | BTP_ROOT);
449  rootopaque->btpo_level = 0;
450  rootopaque->btpo_cycleid = 0;
451  /* Get raw page pointer for metapage */
452  metapg = BufferGetPage(metabuf);
453 
454  /* NO ELOG(ERROR) till meta is updated */
456 
457  /* upgrade metapage if needed */
458  if (metad->btm_version < BTREE_NOVAC_VERSION)
459  _bt_upgrademetapage(metapg);
460 
461  metad->btm_root = rootblkno;
462  metad->btm_level = 0;
463  metad->btm_fastroot = rootblkno;
464  metad->btm_fastlevel = 0;
466  metad->btm_last_cleanup_num_heap_tuples = -1.0;
467 
468  MarkBufferDirty(rootbuf);
469  MarkBufferDirty(metabuf);
470 
471  /* XLOG stuff */
472  if (RelationNeedsWAL(rel))
473  {
474  xl_btree_newroot xlrec;
475  XLogRecPtr recptr;
477 
478  XLogBeginInsert();
481 
483  md.version = metad->btm_version;
484  md.root = rootblkno;
485  md.level = 0;
486  md.fastroot = rootblkno;
487  md.fastlevel = 0;
489  md.allequalimage = metad->btm_allequalimage;
490 
491  XLogRegisterBufData(2, (char *) &md, sizeof(xl_btree_metadata));
492 
493  xlrec.rootblk = rootblkno;
494  xlrec.level = 0;
495 
496  XLogRegisterData((char *) &xlrec, SizeOfBtreeNewroot);
497 
498  recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_NEWROOT);
499 
500  PageSetLSN(rootpage, recptr);
501  PageSetLSN(metapg, recptr);
502  }
503 
505 
506  /*
507  * swap root write lock for read lock. There is no danger of anyone
508  * else accessing the new root page while it's unlocked, since no one
509  * else knows where it is yet.
510  */
511  _bt_unlockbuf(rel, rootbuf);
512  _bt_lockbuf(rel, rootbuf, BT_READ);
513 
514  /* okay, metadata is correct, release lock on it without caching */
515  _bt_relbuf(rel, metabuf);
516  }
517  else
518  {
519  rootblkno = metad->btm_fastroot;
520  Assert(rootblkno != P_NONE);
521  rootlevel = metad->btm_fastlevel;
522 
523  /*
524  * Cache the metapage data for next time
525  */
527  sizeof(BTMetaPageData));
528  memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData));
529 
530  /*
531  * We are done with the metapage; arrange to release it via first
532  * _bt_relandgetbuf call
533  */
534  rootbuf = metabuf;
535 
536  for (;;)
537  {
538  rootbuf = _bt_relandgetbuf(rel, rootbuf, rootblkno, BT_READ);
539  rootpage = BufferGetPage(rootbuf);
540  rootopaque = BTPageGetOpaque(rootpage);
541 
542  if (!P_IGNORE(rootopaque))
543  break;
544 
545  /* it's dead, Jim. step right one page */
546  if (P_RIGHTMOST(rootopaque))
547  elog(ERROR, "no live root page found in index \"%s\"",
549  rootblkno = rootopaque->btpo_next;
550  }
551 
552  if (rootopaque->btpo_level != rootlevel)
553  elog(ERROR, "root page %u of index \"%s\" has level %u, expected %u",
554  rootblkno, RelationGetRelationName(rel),
555  rootopaque->btpo_level, rootlevel);
556  }
557 
558  /*
559  * By here, we have a pin and read lock on the root page, and no lock set
560  * on the metadata page. Return the root page's buffer.
561  */
562  return rootbuf;
563 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
void _bt_upgrademetapage(Page page)
Definition: nbtpage.c:107
Buffer _bt_allocbuf(Relation rel, Relation heaprel)
Definition: nbtpage.c:869
static BTMetaPageData * _bt_getmeta(Relation rel, Buffer metabuf)
Definition: nbtpage.c:142
#define BTREE_MIN_VERSION
Definition: nbtree.h:151
#define BTP_LEAF
Definition: nbtree.h:76
#define BTREE_MAGIC
Definition: nbtree.h:149
#define BTP_ROOT
Definition: nbtree.h:77
#define SizeOfBtreeNewroot
Definition: nbtxlog.h:347
#define XLOG_BTREE_NEWROOT
Definition: nbtxlog.h:37
uint32 btm_last_cleanup_num_delpages
Definition: nbtree.h:114
uint32 btm_level
Definition: nbtree.h:108
float8 btm_last_cleanup_num_heap_tuples
Definition: nbtree.h:116
BlockNumber btm_fastroot
Definition: nbtree.h:109
uint32 btm_version
Definition: nbtree.h:106
uint32 btm_magic
Definition: nbtree.h:105
bool btm_allequalimage
Definition: nbtree.h:118
uint32 btm_fastlevel
Definition: nbtree.h:110
BlockNumber btpo_prev
Definition: nbtree.h:64
void * rd_amcache
Definition: rel.h:229
MemoryContext rd_indexcxt
Definition: rel.h:204
uint32 level
Definition: nbtxlog.h:50
uint32 version
Definition: nbtxlog.h:48
bool allequalimage
Definition: nbtxlog.h:54
BlockNumber fastroot
Definition: nbtxlog.h:51
uint32 fastlevel
Definition: nbtxlog.h:52
BlockNumber root
Definition: nbtxlog.h:49
uint32 last_cleanup_num_delpages
Definition: nbtxlog.h:53
uint32 level
Definition: nbtxlog.h:344
BlockNumber rootblk
Definition: nbtxlog.h:343
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33

References _bt_allocbuf(), _bt_getbuf(), _bt_getmeta(), _bt_lockbuf(), _bt_relandgetbuf(), _bt_relbuf(), _bt_unlockbuf(), _bt_upgrademetapage(), xl_btree_metadata::allequalimage, Assert(), BT_READ, BT_WRITE, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTP_LEAF, BTP_ROOT, BTPageGetOpaque, BTPageOpaqueData::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageOpaqueData::btpo_level, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, BTREE_MAGIC, BTREE_METAPAGE, BTREE_MIN_VERSION, BTREE_NOVAC_VERSION, BTREE_VERSION, BufferGetBlockNumber(), BufferGetPage(), elog, END_CRIT_SECTION, ERROR, xl_btree_metadata::fastlevel, xl_btree_metadata::fastroot, InvalidBuffer, xl_btree_metadata::last_cleanup_num_delpages, xl_btree_metadata::level, xl_btree_newroot::level, MarkBufferDirty(), MemoryContextAlloc(), P_IGNORE, P_LEFTMOST, P_NONE, P_RIGHTMOST, PageSetLSN(), pfree(), RelationData::rd_amcache, RelationData::rd_indexcxt, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, xl_btree_metadata::root, xl_btree_newroot::rootblk, SizeOfBtreeNewroot, START_CRIT_SECTION, xl_btree_metadata::version, XLOG_BTREE_NEWROOT, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by _bt_get_endpoint(), and _bt_search().

◆ _bt_getrootheight()

int _bt_getrootheight ( Relation  rel)

Definition at line 675 of file nbtpage.c.

676 {
677  BTMetaPageData *metad;
678 
679  if (rel->rd_amcache == NULL)
680  {
681  Buffer metabuf;
682 
683  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
684  metad = _bt_getmeta(rel, metabuf);
685 
686  /*
687  * If there's no root page yet, _bt_getroot() doesn't expect a cache
688  * to be made, so just stop here and report the index height is zero.
689  * (XXX perhaps _bt_getroot() should be changed to allow this case.)
690  */
691  if (metad->btm_root == P_NONE)
692  {
693  _bt_relbuf(rel, metabuf);
694  return 0;
695  }
696 
697  /*
698  * Cache the metapage data for next time
699  */
701  sizeof(BTMetaPageData));
702  memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData));
703  _bt_relbuf(rel, metabuf);
704  }
705 
706  /* Get cached page */
707  metad = (BTMetaPageData *) rel->rd_amcache;
708  /* We shouldn't have cached it if any of these fail */
709  Assert(metad->btm_magic == BTREE_MAGIC);
711  Assert(metad->btm_version <= BTREE_VERSION);
712  Assert(!metad->btm_allequalimage ||
714  Assert(metad->btm_fastroot != P_NONE);
715 
716  return metad->btm_fastlevel;
717 }

References _bt_getbuf(), _bt_getmeta(), _bt_relbuf(), Assert(), BT_READ, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTREE_MAGIC, BTREE_METAPAGE, BTREE_MIN_VERSION, BTREE_NOVAC_VERSION, BTREE_VERSION, MemoryContextAlloc(), P_NONE, RelationData::rd_amcache, and RelationData::rd_indexcxt.

Referenced by _bt_insertonpg(), and get_relation_info().

◆ _bt_getstackbuf()

Buffer _bt_getstackbuf ( Relation  rel,
Relation  heaprel,
BTStack  stack,
BlockNumber  child 
)

Definition at line 2319 of file nbtinsert.c.

2320 {
2321  BlockNumber blkno;
2322  OffsetNumber start;
2323 
2324  blkno = stack->bts_blkno;
2325  start = stack->bts_offset;
2326 
2327  for (;;)
2328  {
2329  Buffer buf;
2330  Page page;
2331  BTPageOpaque opaque;
2332 
2333  buf = _bt_getbuf(rel, blkno, BT_WRITE);
2334  page = BufferGetPage(buf);
2335  opaque = BTPageGetOpaque(page);
2336 
2337  Assert(heaprel != NULL);
2338  if (P_INCOMPLETE_SPLIT(opaque))
2339  {
2340  _bt_finish_split(rel, heaprel, buf, stack->bts_parent);
2341  continue;
2342  }
2343 
2344  if (!P_IGNORE(opaque))
2345  {
2346  OffsetNumber offnum,
2347  minoff,
2348  maxoff;
2349  ItemId itemid;
2350  IndexTuple item;
2351 
2352  minoff = P_FIRSTDATAKEY(opaque);
2353  maxoff = PageGetMaxOffsetNumber(page);
2354 
2355  /*
2356  * start = InvalidOffsetNumber means "search the whole page". We
2357  * need this test anyway due to possibility that page has a high
2358  * key now when it didn't before.
2359  */
2360  if (start < minoff)
2361  start = minoff;
2362 
2363  /*
2364  * Need this check too, to guard against possibility that page
2365  * split since we visited it originally.
2366  */
2367  if (start > maxoff)
2368  start = OffsetNumberNext(maxoff);
2369 
2370  /*
2371  * These loops will check every item on the page --- but in an
2372  * order that's attuned to the probability of where it actually
2373  * is. Scan to the right first, then to the left.
2374  */
2375  for (offnum = start;
2376  offnum <= maxoff;
2377  offnum = OffsetNumberNext(offnum))
2378  {
2379  itemid = PageGetItemId(page, offnum);
2380  item = (IndexTuple) PageGetItem(page, itemid);
2381 
2382  if (BTreeTupleGetDownLink(item) == child)
2383  {
2384  /* Return accurate pointer to where link is now */
2385  stack->bts_blkno = blkno;
2386  stack->bts_offset = offnum;
2387  return buf;
2388  }
2389  }
2390 
2391  for (offnum = OffsetNumberPrev(start);
2392  offnum >= minoff;
2393  offnum = OffsetNumberPrev(offnum))
2394  {
2395  itemid = PageGetItemId(page, offnum);
2396  item = (IndexTuple) PageGetItem(page, itemid);
2397 
2398  if (BTreeTupleGetDownLink(item) == child)
2399  {
2400  /* Return accurate pointer to where link is now */
2401  stack->bts_blkno = blkno;
2402  stack->bts_offset = offnum;
2403  return buf;
2404  }
2405  }
2406  }
2407 
2408  /*
2409  * The item we're looking for moved right at least one page.
2410  *
2411  * Lehman and Yao couple/chain locks when moving right here, which we
2412  * can avoid. See nbtree/README.
2413  */
2414  if (P_RIGHTMOST(opaque))
2415  {
2416  _bt_relbuf(rel, buf);
2417  return InvalidBuffer;
2418  }
2419  blkno = opaque->btpo_next;
2420  start = InvalidOffsetNumber;
2421  _bt_relbuf(rel, buf);
2422  }
2423 }
void _bt_finish_split(Relation rel, Relation heaprel, Buffer lbuf, BTStack stack)
Definition: nbtinsert.c:2241
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
BlockNumber bts_blkno
Definition: nbtree.h:734
OffsetNumber bts_offset
Definition: nbtree.h:735

References _bt_finish_split(), _bt_getbuf(), _bt_relbuf(), Assert(), BT_WRITE, BTPageGetOpaque, BTPageOpaqueData::btpo_next, BTreeTupleGetDownLink(), BTStackData::bts_blkno, BTStackData::bts_offset, BTStackData::bts_parent, buf, BufferGetPage(), InvalidBuffer, InvalidOffsetNumber, OffsetNumberNext, OffsetNumberPrev, P_FIRSTDATAKEY, P_IGNORE, P_INCOMPLETE_SPLIT, P_RIGHTMOST, PageGetItem(), PageGetItemId(), and PageGetMaxOffsetNumber().

Referenced by _bt_insert_parent(), and _bt_lock_subtree_parent().

◆ _bt_gettrueroot()

Buffer _bt_gettrueroot ( Relation  rel)

Definition at line 580 of file nbtpage.c.

581 {
582  Buffer metabuf;
583  Page metapg;
584  BTPageOpaque metaopaque;
585  Buffer rootbuf;
586  Page rootpage;
587  BTPageOpaque rootopaque;
588  BlockNumber rootblkno;
589  uint32 rootlevel;
590  BTMetaPageData *metad;
591 
592  /*
593  * We don't try to use cached metapage data here, since (a) this path is
594  * not performance-critical, and (b) if we are here it suggests our cache
595  * is out-of-date anyway. In light of point (b), it's probably safest to
596  * actively flush any cached metapage info.
597  */
598  if (rel->rd_amcache)
599  pfree(rel->rd_amcache);
600  rel->rd_amcache = NULL;
601 
602  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
603  metapg = BufferGetPage(metabuf);
604  metaopaque = BTPageGetOpaque(metapg);
605  metad = BTPageGetMeta(metapg);
606 
607  if (!P_ISMETA(metaopaque) ||
608  metad->btm_magic != BTREE_MAGIC)
609  ereport(ERROR,
610  (errcode(ERRCODE_INDEX_CORRUPTED),
611  errmsg("index \"%s\" is not a btree",
612  RelationGetRelationName(rel))));
613 
614  if (metad->btm_version < BTREE_MIN_VERSION ||
615  metad->btm_version > BTREE_VERSION)
616  ereport(ERROR,
617  (errcode(ERRCODE_INDEX_CORRUPTED),
618  errmsg("version mismatch in index \"%s\": file version %d, "
619  "current version %d, minimal supported version %d",
622 
623  /* if no root page initialized yet, fail */
624  if (metad->btm_root == P_NONE)
625  {
626  _bt_relbuf(rel, metabuf);
627  return InvalidBuffer;
628  }
629 
630  rootblkno = metad->btm_root;
631  rootlevel = metad->btm_level;
632 
633  /*
634  * We are done with the metapage; arrange to release it via first
635  * _bt_relandgetbuf call
636  */
637  rootbuf = metabuf;
638 
639  for (;;)
640  {
641  rootbuf = _bt_relandgetbuf(rel, rootbuf, rootblkno, BT_READ);
642  rootpage = BufferGetPage(rootbuf);
643  rootopaque = BTPageGetOpaque(rootpage);
644 
645  if (!P_IGNORE(rootopaque))
646  break;
647 
648  /* it's dead, Jim. step right one page */
649  if (P_RIGHTMOST(rootopaque))
650  elog(ERROR, "no live root page found in index \"%s\"",
652  rootblkno = rootopaque->btpo_next;
653  }
654 
655  if (rootopaque->btpo_level != rootlevel)
656  elog(ERROR, "root page %u of index \"%s\" has level %u, expected %u",
657  rootblkno, RelationGetRelationName(rel),
658  rootopaque->btpo_level, rootlevel);
659 
660  return rootbuf;
661 }
#define P_ISMETA(opaque)
Definition: nbtree.h:223

References _bt_getbuf(), _bt_relandgetbuf(), _bt_relbuf(), BT_READ, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, BTPageGetOpaque, BTPageOpaqueData::btpo_level, BTPageOpaqueData::btpo_next, BTREE_MAGIC, BTREE_METAPAGE, BTREE_MIN_VERSION, BTREE_VERSION, BufferGetPage(), elog, ereport, errcode(), errmsg(), ERROR, InvalidBuffer, P_IGNORE, P_ISMETA, P_NONE, P_RIGHTMOST, pfree(), RelationData::rd_amcache, and RelationGetRelationName.

Referenced by _bt_get_endpoint().

◆ _bt_initmetapage()

void _bt_initmetapage ( Page  page,
BlockNumber  rootbknum,
uint32  level,
bool  allequalimage 
)

Definition at line 67 of file nbtpage.c.

69 {
70  BTMetaPageData *metad;
71  BTPageOpaque metaopaque;
72 
73  _bt_pageinit(page, BLCKSZ);
74 
75  metad = BTPageGetMeta(page);
76  metad->btm_magic = BTREE_MAGIC;
77  metad->btm_version = BTREE_VERSION;
78  metad->btm_root = rootbknum;
79  metad->btm_level = level;
80  metad->btm_fastroot = rootbknum;
81  metad->btm_fastlevel = level;
84  metad->btm_allequalimage = allequalimage;
85 
86  metaopaque = BTPageGetOpaque(page);
87  metaopaque->btpo_flags = BTP_META;
88 
89  /*
90  * Set pd_lower just past the end of the metadata. This is essential,
91  * because without doing so, metadata will be lost if xlog.c compresses
92  * the page.
93  */
94  ((PageHeader) page)->pd_lower =
95  ((char *) metad + sizeof(BTMetaPageData)) - (char *) page;
96 }
PageHeaderData * PageHeader
Definition: bufpage.h:170
#define BTP_META
Definition: nbtree.h:79

References _bt_pageinit(), BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTP_META, BTPageGetMeta, BTPageGetOpaque, BTPageOpaqueData::btpo_flags, BTREE_MAGIC, and BTREE_VERSION.

Referenced by _bt_uppershutdown(), and btbuildempty().

◆ _bt_keep_natts_fast()

int _bt_keep_natts_fast ( Relation  rel,
IndexTuple  lastleft,
IndexTuple  firstright 
)

Definition at line 2475 of file nbtutils.c.

2476 {
2477  TupleDesc itupdesc = RelationGetDescr(rel);
2478  int keysz = IndexRelationGetNumberOfKeyAttributes(rel);
2479  int keepnatts;
2480 
2481  keepnatts = 1;
2482  for (int attnum = 1; attnum <= keysz; attnum++)
2483  {
2484  Datum datum1,
2485  datum2;
2486  bool isNull1,
2487  isNull2;
2488  Form_pg_attribute att;
2489 
2490  datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
2491  datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
2492  att = TupleDescAttr(itupdesc, attnum - 1);
2493 
2494  if (isNull1 != isNull2)
2495  break;
2496 
2497  if (!isNull1 &&
2498  !datum_image_eq(datum1, datum2, att->attbyval, att->attlen))
2499  break;
2500 
2501  keepnatts++;
2502  }
2503 
2504  return keepnatts;
2505 }
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:266
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References attnum, datum_image_eq(), index_getattr(), IndexRelationGetNumberOfKeyAttributes, RelationGetDescr, and TupleDescAttr.

Referenced by _bt_afternewitemoff(), _bt_bottomupdel_pass(), _bt_dedup_pass(), _bt_do_singleval(), _bt_keep_natts(), _bt_load(), _bt_split_penalty(), and _bt_strategy().

◆ _bt_killitems()

void _bt_killitems ( IndexScanDesc  scan)

Definition at line 1776 of file nbtutils.c.

1777 {
1778  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1779  Page page;
1780  BTPageOpaque opaque;
1781  OffsetNumber minoff;
1782  OffsetNumber maxoff;
1783  int i;
1784  int numKilled = so->numKilled;
1785  bool killedsomething = false;
1786  bool droppedpin PG_USED_FOR_ASSERTS_ONLY;
1787 
1789 
1790  /*
1791  * Always reset the scan state, so we don't look for same items on other
1792  * pages.
1793  */
1794  so->numKilled = 0;
1795 
1796  if (BTScanPosIsPinned(so->currPos))
1797  {
1798  /*
1799  * We have held the pin on this page since we read the index tuples,
1800  * so all we need to do is lock it. The pin will have prevented
1801  * re-use of any TID on the page, so there is no need to check the
1802  * LSN.
1803  */
1804  droppedpin = false;
1806 
1807  page = BufferGetPage(so->currPos.buf);
1808  }
1809  else
1810  {
1811  Buffer buf;
1812 
1813  droppedpin = true;
1814  /* Attempt to re-read the buffer, getting pin and lock. */
1816 
1817  page = BufferGetPage(buf);
1818  if (BufferGetLSNAtomic(buf) == so->currPos.lsn)
1819  so->currPos.buf = buf;
1820  else
1821  {
1822  /* Modified while not pinned means hinting is not safe. */
1823  _bt_relbuf(scan->indexRelation, buf);
1824  return;
1825  }
1826  }
1827 
1828  opaque = BTPageGetOpaque(page);
1829  minoff = P_FIRSTDATAKEY(opaque);
1830  maxoff = PageGetMaxOffsetNumber(page);
1831 
1832  for (i = 0; i < numKilled; i++)
1833  {
1834  int itemIndex = so->killedItems[i];
1835  BTScanPosItem *kitem = &so->currPos.items[itemIndex];
1836  OffsetNumber offnum = kitem->indexOffset;
1837 
1838  Assert(itemIndex >= so->currPos.firstItem &&
1839  itemIndex <= so->currPos.lastItem);
1840  if (offnum < minoff)
1841  continue; /* pure paranoia */
1842  while (offnum <= maxoff)
1843  {
1844  ItemId iid = PageGetItemId(page, offnum);
1845  IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
1846  bool killtuple = false;
1847 
1848  if (BTreeTupleIsPosting(ituple))
1849  {
1850  int pi = i + 1;
1851  int nposting = BTreeTupleGetNPosting(ituple);
1852  int j;
1853 
1854  /*
1855  * We rely on the convention that heap TIDs in the scanpos
1856  * items array are stored in ascending heap TID order for a
1857  * group of TIDs that originally came from a posting list
1858  * tuple. This convention even applies during backwards
1859  * scans, where returning the TIDs in descending order might
1860  * seem more natural. This is about effectiveness, not
1861  * correctness.
1862  *
1863  * Note that the page may have been modified in almost any way
1864  * since we first read it (in the !droppedpin case), so it's
1865  * possible that this posting list tuple wasn't a posting list
1866  * tuple when we first encountered its heap TIDs.
1867  */
1868  for (j = 0; j < nposting; j++)
1869  {
1870  ItemPointer item = BTreeTupleGetPostingN(ituple, j);
1871 
1872  if (!ItemPointerEquals(item, &kitem->heapTid))
1873  break; /* out of posting list loop */
1874 
1875  /*
1876  * kitem must have matching offnum when heap TIDs match,
1877  * though only in the common case where the page can't
1878  * have been concurrently modified
1879  */
1880  Assert(kitem->indexOffset == offnum || !droppedpin);
1881 
1882  /*
1883  * Read-ahead to later kitems here.
1884  *
1885  * We rely on the assumption that not advancing kitem here
1886  * will prevent us from considering the posting list tuple
1887  * fully dead by not matching its next heap TID in next
1888  * loop iteration.
1889  *
1890  * If, on the other hand, this is the final heap TID in
1891  * the posting list tuple, then tuple gets killed
1892  * regardless (i.e. we handle the case where the last
1893  * kitem is also the last heap TID in the last index tuple
1894  * correctly -- posting tuple still gets killed).
1895  */
1896  if (pi < numKilled)
1897  kitem = &so->currPos.items[so->killedItems[pi++]];
1898  }
1899 
1900  /*
1901  * Don't bother advancing the outermost loop's int iterator to
1902  * avoid processing killed items that relate to the same
1903  * offnum/posting list tuple. This micro-optimization hardly
1904  * seems worth it. (Further iterations of the outermost loop
1905  * will fail to match on this same posting list's first heap
1906  * TID instead, so we'll advance to the next offnum/index
1907  * tuple pretty quickly.)
1908  */
1909  if (j == nposting)
1910  killtuple = true;
1911  }
1912  else if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
1913  killtuple = true;
1914 
1915  /*
1916  * Mark index item as dead, if it isn't already. Since this
1917  * happens while holding a buffer lock possibly in shared mode,
1918  * it's possible that multiple processes attempt to do this
1919  * simultaneously, leading to multiple full-page images being sent
1920  * to WAL (if wal_log_hints or data checksums are enabled), which
1921  * is undesirable.
1922  */
1923  if (killtuple && !ItemIdIsDead(iid))
1924  {
1925  /* found the item/all posting list items */
1926  ItemIdMarkDead(iid);
1927  killedsomething = true;
1928  break; /* out of inner search loop */
1929  }
1930  offnum = OffsetNumberNext(offnum);
1931  }
1932  }
1933 
1934  /*
1935  * Since this can be redone later if needed, mark as dirty hint.
1936  *
1937  * Whenever we mark anything LP_DEAD, we also set the page's
1938  * BTP_HAS_GARBAGE flag, which is likewise just a hint. (Note that we
1939  * only rely on the page-level flag in !heapkeyspace indexes.)
1940  */
1941  if (killedsomething)
1942  {
1943  opaque->btpo_flags |= BTP_HAS_GARBAGE;
1944  MarkBufferDirtyHint(so->currPos.buf, true);
1945  }
1946 
1947  _bt_unlockbuf(scan->indexRelation, so->currPos.buf);
1948 }
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3638
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:4624
int j
Definition: isn.c:74
#define ItemIdMarkDead(itemId)
Definition: itemid.h:179
int * killedItems
Definition: nbtree.h:1051
BlockNumber currPage
Definition: nbtree.h:956
int firstItem
Definition: nbtree.h:981
XLogRecPtr lsn
Definition: nbtree.h:955
ItemPointerData heapTid
Definition: nbtree.h:946
OffsetNumber indexOffset
Definition: nbtree.h:947

References _bt_getbuf(), _bt_lockbuf(), _bt_relbuf(), _bt_unlockbuf(), Assert(), BT_READ, BTP_HAS_GARBAGE, BTPageGetOpaque, BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPosting(), BTScanPosIsPinned, BTScanPosIsValid, buf, BTScanPosData::buf, BufferGetLSNAtomic(), BufferGetPage(), BTScanPosData::currPage, BTScanOpaqueData::currPos, BTScanPosData::firstItem, BTScanPosItem::heapTid, i, BTScanPosItem::indexOffset, IndexScanDescData::indexRelation, ItemIdIsDead, ItemIdMarkDead, ItemPointerEquals(), BTScanPosData::items, j, BTScanOpaqueData::killedItems, BTScanPosData::lsn, MarkBufferDirtyHint(), BTScanOpaqueData::numKilled, OffsetNumberNext, IndexScanDescData::opaque, P_FIRSTDATAKEY, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PG_USED_FOR_ASSERTS_ONLY, and IndexTupleData::t_tid.

Referenced by _bt_steppage(), btendscan(), btrescan(), and btrestrpos().

◆ _bt_lockbuf()

void _bt_lockbuf ( Relation  rel,
Buffer  buf,
int  access 
)

Definition at line 1039 of file nbtpage.c.

1040 {
1041  /* LockBuffer() asserts that pin is held by this backend */
1042  LockBuffer(buf, access);
1043 
1044  /*
1045  * It doesn't matter that _bt_unlockbuf() won't get called in the event of
1046  * an nbtree error (e.g. a unique violation error). That won't cause
1047  * Valgrind false positives.
1048  *
1049  * The nbtree client requests are superimposed on top of the bufmgr.c
1050  * buffer pin client requests. In the event of an nbtree error the buffer
1051  * will certainly get marked as defined when the backend once again
1052  * acquires its first pin on the buffer. (Of course, if the backend never
1053  * touches the buffer again then it doesn't matter that it remains
1054  * non-accessible to Valgrind.)
1055  *
1056  * Note: When an IndexTuple C pointer gets computed using an ItemId read
1057  * from a page while a lock was held, the C pointer becomes unsafe to
1058  * dereference forever as soon as the lock is released. Valgrind can only
1059  * detect cases where the pointer gets dereferenced with no _current_
1060  * lock/pin held, though.
1061  */
1062  if (!RelationUsesLocalBuffers(rel))
1064 }
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4795

References buf, BufferGetPage(), LockBuffer(), RelationUsesLocalBuffers, and VALGRIND_MAKE_MEM_DEFINED.

Referenced by _bt_getbuf(), _bt_getroot(), _bt_killitems(), _bt_moveright(), _bt_pagedel(), _bt_readnextpage(), _bt_relandgetbuf(), _bt_search(), _bt_set_cleanup_info(), _bt_unlink_halfdead_page(), and btvacuumpage().

◆ _bt_mark_array_keys()

void _bt_mark_array_keys ( IndexScanDesc  scan)

Definition at line 611 of file nbtutils.c.

612 {
613  BTScanOpaque so = (BTScanOpaque) scan->opaque;
614  int i;
615 
616  for (i = 0; i < so->numArrayKeys; i++)
617  {
618  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
619 
620  curArrayKey->mark_elem = curArrayKey->cur_elem;
621  }
622 }

References BTScanOpaqueData::arrayKeys, BTArrayKeyInfo::cur_elem, i, BTArrayKeyInfo::mark_elem, BTScanOpaqueData::numArrayKeys, and IndexScanDescData::opaque.

◆ _bt_metaversion()

void _bt_metaversion ( Relation  rel,
bool heapkeyspace,
bool allequalimage 
)

Definition at line 739 of file nbtpage.c.

740 {
741  BTMetaPageData *metad;
742 
743  if (rel->rd_amcache == NULL)
744  {
745  Buffer metabuf;
746 
747  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
748  metad = _bt_getmeta(rel, metabuf);
749 
750  /*
751  * If there's no root page yet, _bt_getroot() doesn't expect a cache
752  * to be made, so just stop here. (XXX perhaps _bt_getroot() should
753  * be changed to allow this case.)
754  */
755  if (metad->btm_root == P_NONE)
756  {
757  *heapkeyspace = metad->btm_version > BTREE_NOVAC_VERSION;
758  *allequalimage = metad->btm_allequalimage;
759 
760  _bt_relbuf(rel, metabuf);
761  return;
762  }
763 
764  /*
765  * Cache the metapage data for next time
766  *
767  * An on-the-fly version upgrade performed by _bt_upgrademetapage()
768  * can change the nbtree version for an index without invalidating any
769  * local cache. This is okay because it can only happen when moving
770  * from version 2 to version 3, both of which are !heapkeyspace
771  * versions.
772  */
774  sizeof(BTMetaPageData));
775  memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData));
776  _bt_relbuf(rel, metabuf);
777  }
778 
779  /* Get cached page */
780  metad = (BTMetaPageData *) rel->rd_amcache;
781  /* We shouldn't have cached it if any of these fail */
782  Assert(metad->btm_magic == BTREE_MAGIC);
784  Assert(metad->btm_version <= BTREE_VERSION);
785  Assert(!metad->btm_allequalimage ||
787  Assert(metad->btm_fastroot != P_NONE);
788 
789  *heapkeyspace = metad->btm_version > BTREE_NOVAC_VERSION;
790  *allequalimage = metad->btm_allequalimage;
791 }

References _bt_getbuf(), _bt_getmeta(), _bt_relbuf(), Assert(), BT_READ, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTREE_MAGIC, BTREE_METAPAGE, BTREE_MIN_VERSION, BTREE_NOVAC_VERSION, BTREE_VERSION, MemoryContextAlloc(), P_NONE, RelationData::rd_amcache, and RelationData::rd_indexcxt.

Referenced by _bt_first(), _bt_mkscankey(), and bt_index_check_internal().

◆ _bt_mkscankey()

BTScanInsert _bt_mkscankey ( Relation  rel,
IndexTuple  itup 
)

Definition at line 81 of file nbtutils.c.

82 {
84  ScanKey skey;
85  TupleDesc itupdesc;
86  int indnkeyatts;
87  int16 *indoption;
88  int tupnatts;
89  int i;
90 
91  itupdesc = RelationGetDescr(rel);
92  indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
93  indoption = rel->rd_indoption;
94  tupnatts = itup ? BTreeTupleGetNAtts(itup, rel) : 0;
95 
97 
98  /*
99  * We'll execute search using scan key constructed on key columns.
100  * Truncated attributes and non-key attributes are omitted from the final
101  * scan key.
102  */
103  key = palloc(offsetof(BTScanInsertData, scankeys) +
104  sizeof(ScanKeyData) * indnkeyatts);
105  if (itup)
106  _bt_metaversion(rel, &key->heapkeyspace, &key->allequalimage);
107  else
108  {
109  /* Utility statement callers can set these fields themselves */
110  key->heapkeyspace = true;
111  key->allequalimage = false;
112  }
113  key->anynullkeys = false; /* initial assumption */
114  key->nextkey = false; /* usual case, required by btinsert */
115  key->backward = false; /* usual case, required by btinsert */
116  key->keysz = Min(indnkeyatts, tupnatts);
117  key->scantid = key->heapkeyspace && itup ?
118  BTreeTupleGetHeapTID(itup) : NULL;
119  skey = key->scankeys;
120  for (i = 0; i < indnkeyatts; i++)
121  {
122  FmgrInfo *procinfo;
123  Datum arg;
124  bool null;
125  int flags;
126 
127  /*
128  * We can use the cached (default) support procs since no cross-type
129  * comparison can be needed.
130  */
131  procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
132 
133  /*
134  * Key arguments built from truncated attributes (or when caller
135  * provides no tuple) are defensively represented as NULL values. They
136  * should never be used.
137  */
138  if (i < tupnatts)
139  arg = index_getattr(itup, i + 1, itupdesc, &null);
140  else
141  {
142  arg = (Datum) 0;
143  null = true;
144  }
145  flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
147  flags,
148  (AttrNumber) (i + 1),
150  InvalidOid,
151  rel->rd_indcollation[i],
152  procinfo,
153  arg);
154  /* Record if any key attribute is NULL (or truncated) */
155  if (null)
156  key->anynullkeys = true;
157  }
158 
159  /*
160  * In NULLS NOT DISTINCT mode, we pretend that there are no null keys, so
161  * that full uniqueness check is done.
162  */
163  if (rel->rd_index->indnullsnotdistinct)
164  key->anynullkeys = false;
165 
166  return key;
167 }
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:1085
int16 * rd_indoption
Definition: rel.h:211
Form_pg_index rd_index
Definition: rel.h:192

References _bt_metaversion(), arg, Assert(), BTORDER_PROC, BTreeTupleGetHeapTID(), BTreeTupleGetNAtts, i, index_getattr(), index_getprocinfo(), IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, InvalidOid, InvalidStrategy, sort-test::key, Min, palloc(), RelationData::rd_indcollation, RelationData::rd_index, RelationData::rd_indoption, RelationGetDescr, ScanKeyEntryInitializeWithInfo(), SK_BT_INDOPTION_SHIFT, and SK_ISNULL.

Referenced by _bt_doinsert(), _bt_leafbuild(), _bt_pagedel(), bt_mkscankey_pivotsearch(), bt_rootdescend(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

◆ _bt_moveright()

Buffer _bt_moveright ( Relation  rel,
Relation  heaprel,
BTScanInsert  key,
Buffer  buf,
bool  forupdate,
BTStack  stack,
int  access 
)

Definition at line 235 of file nbtsearch.c.

242 {
243  Page page;
244  BTPageOpaque opaque;
245  int32 cmpval;
246 
247  Assert(!forupdate || heaprel != NULL);
248 
249  /*
250  * When nextkey = false (normal case): if the scan key that brought us to
251  * this page is > the high key stored on the page, then the page has split
252  * and we need to move right. (pg_upgrade'd !heapkeyspace indexes could
253  * have some duplicates to the right as well as the left, but that's
254  * something that's only ever dealt with on the leaf level, after
255  * _bt_search has found an initial leaf page.)
256  *
257  * When nextkey = true: move right if the scan key is >= page's high key.
258  * (Note that key.scantid cannot be set in this case.)
259  *
260  * The page could even have split more than once, so scan as far as
261  * needed.
262  *
263  * We also have to move right if we followed a link that brought us to a
264  * dead page.
265  */
266  cmpval = key->nextkey ? 0 : 1;
267 
268  for (;;)
269  {
270  page = BufferGetPage(buf);
271  opaque = BTPageGetOpaque(page);
272 
273  if (P_RIGHTMOST(opaque))
274  break;
275 
276  /*
277  * Finish any incomplete splits we encounter along the way.
278  */
279  if (forupdate && P_INCOMPLETE_SPLIT(opaque))
280  {
282 
283  /* upgrade our lock if necessary */
284  if (access == BT_READ)
285  {
286  _bt_unlockbuf(rel, buf);
287  _bt_lockbuf(rel, buf, BT_WRITE);
288  }
289 
290  if (P_INCOMPLETE_SPLIT(opaque))
291  _bt_finish_split(rel, heaprel, buf, stack);
292  else
293  _bt_relbuf(rel, buf);
294 
295  /* re-acquire the lock in the right mode, and re-check */
296  buf = _bt_getbuf(rel, blkno, access);
297  continue;
298  }
299 
300  if (P_IGNORE(opaque) || _bt_compare(rel, key, page, P_HIKEY) >= cmpval)
301  {
302  /* step right one page */
303  buf = _bt_relandgetbuf(rel, buf, opaque->btpo_next, access);
304  continue;
305  }
306  else
307  break;
308  }
309 
310  if (P_IGNORE(opaque))
311  elog(ERROR, "fell off the end of index \"%s\"",
313 
314  return buf;
315 }

References _bt_compare(), _bt_finish_split(), _bt_getbuf(), _bt_lockbuf(), _bt_relandgetbuf(), _bt_relbuf(), _bt_unlockbuf(), Assert(), BT_READ, BT_WRITE, BTPageGetOpaque, BTPageOpaqueData::btpo_next, buf, BufferGetBlockNumber(), BufferGetPage(), elog, ERROR, sort-test::key, P_HIKEY, P_IGNORE, P_INCOMPLETE_SPLIT, P_RIGHTMOST, and RelationGetRelationName.

Referenced by _bt_search().

◆ _bt_next()

bool _bt_next ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 1459 of file nbtsearch.c.

1460 {
1461  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1462  BTScanPosItem *currItem;
1463 
1464  /*
1465  * Advance to next tuple on current page; or if there's no more, try to
1466  * step to the next page with data.
1467  */
1468  if (ScanDirectionIsForward(dir))
1469  {
1470  if (++so->currPos.itemIndex > so->currPos.lastItem)
1471  {
1472  if (!_bt_steppage(scan, dir))
1473  return false;
1474  }
1475  }
1476  else
1477  {
1478  if (--so->currPos.itemIndex < so->currPos.firstItem)
1479  {
1480  if (!_bt_steppage(scan, dir))
1481  return false;
1482  }
1483  }
1484 
1485  /* OK, itemIndex says what to return */
1486  currItem = &so->currPos.items[so->currPos.itemIndex];
1487  scan->xs_heaptid = currItem->heapTid;
1488  if (scan->xs_want_itup)
1489  scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
1490 
1491  return true;
1492 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
int lastItem
Definition: nbtree.h:982

References _bt_steppage(), BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, BTScanPosData::firstItem, if(), BTScanPosData::itemIndex, BTScanPosData::items, BTScanPosData::lastItem, IndexScanDescData::opaque, ScanDirectionIsForward, IndexScanDescData::xs_heaptid, IndexScanDescData::xs_itup, and IndexScanDescData::xs_want_itup.

Referenced by btgetbitmap(), and btgettuple().

◆ _bt_pagedel()

void _bt_pagedel ( Relation  rel,
Buffer  leafbuf,
BTVacState vstate 
)

Definition at line 1802 of file nbtpage.c.

1803 {
1804  BlockNumber rightsib;
1805  bool rightsib_empty;
1806  Page page;
1807  BTPageOpaque opaque;
1808 
1809  /*
1810  * Save original leafbuf block number from caller. Only deleted blocks
1811  * that are <= scanblkno are added to bulk delete stat's pages_deleted
1812  * count.
1813  */
1814  BlockNumber scanblkno = BufferGetBlockNumber(leafbuf);
1815 
1816  /*
1817  * "stack" is a search stack leading (approximately) to the target page.
1818  * It is initially NULL, but when iterating, we keep it to avoid
1819  * duplicated search effort.
1820  *
1821  * Also, when "stack" is not NULL, we have already checked that the
1822  * current page is not the right half of an incomplete split, i.e. the
1823  * left sibling does not have its INCOMPLETE_SPLIT flag set, including
1824  * when the current target page is to the right of caller's initial page
1825  * (the scanblkno page).
1826  */
1827  BTStack stack = NULL;
1828 
1829  for (;;)
1830  {
1831  page = BufferGetPage(leafbuf);
1832  opaque = BTPageGetOpaque(page);
1833 
1834  /*
1835  * Internal pages are never deleted directly, only as part of deleting
1836  * the whole subtree all the way down to leaf level.
1837  *
1838  * Also check for deleted pages here. Caller never passes us a fully
1839  * deleted page. Only VACUUM can delete pages, so there can't have
1840  * been a concurrent deletion. Assume that we reached any deleted
1841  * page encountered here by following a sibling link, and that the
1842  * index is corrupt.
1843  */
1844  Assert(!P_ISDELETED(opaque));
1845  if (!P_ISLEAF(opaque) || P_ISDELETED(opaque))
1846  {
1847  /*
1848  * Pre-9.4 page deletion only marked internal pages as half-dead,
1849  * but now we only use that flag on leaf pages. The old algorithm
1850  * was never supposed to leave half-dead pages in the tree, it was
1851  * just a transient state, but it was nevertheless possible in
1852  * error scenarios. We don't know how to deal with them here. They
1853  * are harmless as far as searches are considered, but inserts
1854  * into the deleted keyspace could add out-of-order downlinks in
1855  * the upper levels. Log a notice, hopefully the admin will notice
1856  * and reindex.
1857  */
1858  if (P_ISHALFDEAD(opaque))
1859  ereport(LOG,
1860  (errcode(ERRCODE_INDEX_CORRUPTED),
1861  errmsg("index \"%s\" contains a half-dead internal page",
1863  errhint("This can be caused by an interrupted VACUUM in version 9.3 or older, before upgrade. Please REINDEX it.")));
1864 
1865  if (P_ISDELETED(opaque))
1866  ereport(LOG,
1867  (errcode(ERRCODE_INDEX_CORRUPTED),
1868  errmsg_internal("found deleted block %u while following right link from block %u in index \"%s\"",
1869  BufferGetBlockNumber(leafbuf),
1870  scanblkno,
1871  RelationGetRelationName(rel))));
1872 
1873  _bt_relbuf(rel, leafbuf);
1874  return;
1875  }
1876 
1877  /*
1878  * We can never delete rightmost pages nor root pages. While at it,
1879  * check that page is empty, since it's possible that the leafbuf page
1880  * was empty a moment ago, but has since had some inserts.
1881  *
1882  * To keep the algorithm simple, we also never delete an incompletely
1883  * split page (they should be rare enough that this doesn't make any
1884  * meaningful difference to disk usage):
1885  *
1886  * The INCOMPLETE_SPLIT flag on the page tells us if the page is the
1887  * left half of an incomplete split, but ensuring that it's not the
1888  * right half is more complicated. For that, we have to check that
1889  * the left sibling doesn't have its INCOMPLETE_SPLIT flag set using
1890  * _bt_leftsib_splitflag(). On the first iteration, we temporarily
1891  * release the lock on scanblkno/leafbuf, check the left sibling, and
1892  * construct a search stack to scanblkno. On subsequent iterations,
1893  * we know we stepped right from a page that passed these tests, so
1894  * it's OK.
1895  */
1896  if (P_RIGHTMOST(opaque) || P_ISROOT(opaque) ||
1897  P_FIRSTDATAKEY(opaque) <= PageGetMaxOffsetNumber(page) ||
1898  P_INCOMPLETE_SPLIT(opaque))
1899  {
1900  /* Should never fail to delete a half-dead page */
1901  Assert(!P_ISHALFDEAD(opaque));
1902 
1903  _bt_relbuf(rel, leafbuf);
1904  return;
1905  }
1906 
1907  /*
1908  * First, remove downlink pointing to the page (or a parent of the
1909  * page, if we are going to delete a taller subtree), and mark the
1910  * leafbuf page half-dead
1911  */
1912  if (!P_ISHALFDEAD(opaque))
1913  {
1914  /*
1915  * We need an approximate pointer to the page's parent page. We
1916  * use a variant of the standard search mechanism to search for
1917  * the page's high key; this will give us a link to either the
1918  * current parent or someplace to its left (if there are multiple
1919  * equal high keys, which is possible with !heapkeyspace indexes).
1920  *
1921  * Also check if this is the right-half of an incomplete split
1922  * (see comment above).
1923  */
1924  if (!stack)
1925  {
1926  BTScanInsert itup_key;
1927  ItemId itemid;
1928  IndexTuple targetkey;
1929  BlockNumber leftsib,
1930  leafblkno;
1931  Buffer sleafbuf;
1932 
1933  itemid = PageGetItemId(page, P_HIKEY);
1934  targetkey = CopyIndexTuple((IndexTuple) PageGetItem(page, itemid));
1935 
1936  leftsib = opaque->btpo_prev;
1937  leafblkno = BufferGetBlockNumber(leafbuf);
1938 
1939  /*
1940  * To avoid deadlocks, we'd better drop the leaf page lock
1941  * before going further.
1942  */
1943  _bt_unlockbuf(rel, leafbuf);
1944 
1945  /*
1946  * Check that the left sibling of leafbuf (if any) is not
1947  * marked with INCOMPLETE_SPLIT flag before proceeding
1948  */
1949  Assert(leafblkno == scanblkno);
1950  if (_bt_leftsib_splitflag(rel, leftsib, leafblkno))
1951  {
1952  ReleaseBuffer(leafbuf);
1953  return;
1954  }
1955 
1956  /*
1957  * We need an insertion scan key, so build one.
1958  *
1959  * _bt_search searches for the leaf page that contains any
1960  * matching non-pivot tuples, but we need it to "search" for
1961  * the high key pivot from the page that we're set to delete.
1962  * Compensate for the mismatch by having _bt_search locate the
1963  * last position < equal-to-untruncated-prefix non-pivots.
1964  */
1965  itup_key = _bt_mkscankey(rel, targetkey);
1966 
1967  /* Set up a BTLessStrategyNumber-like insertion scan key */
1968  itup_key->nextkey = false;
1969  itup_key->backward = true;
1970  stack = _bt_search(rel, NULL, itup_key, &sleafbuf, BT_READ);
1971  /* won't need a second lock or pin on leafbuf */
1972  _bt_relbuf(rel, sleafbuf);
1973 
1974  /*
1975  * Re-lock the leaf page, and start over to use our stack
1976  * within _bt_mark_page_halfdead. We must do it that way
1977  * because it's possible that leafbuf can no longer be
1978  * deleted. We need to recheck.
1979  *
1980  * Note: We can't simply hold on to the sleafbuf lock instead,
1981  * because it's barely possible that sleafbuf is not the same
1982  * page as leafbuf. This happens when leafbuf split after our
1983  * original lock was dropped, but before _bt_search finished
1984  * its descent. We rely on the assumption that we'll find
1985  * leafbuf isn't safe to delete anymore in this scenario.
1986  * (Page deletion can cope with the stack being to the left of
1987  * leafbuf, but not to the right of leafbuf.)
1988  */
1989  _bt_lockbuf(rel, leafbuf, BT_WRITE);
1990  continue;
1991  }
1992 
1993  /*
1994  * See if it's safe to delete the leaf page, and determine how
1995  * many parent/internal pages above the leaf level will be
1996  * deleted. If it's safe then _bt_mark_page_halfdead will also
1997  * perform the first phase of deletion, which includes marking the
1998  * leafbuf page half-dead.
1999  */
2000  Assert(P_ISLEAF(opaque) && !P_IGNORE(opaque));
2001  if (!_bt_mark_page_halfdead(rel, vstate->info->heaprel, leafbuf,
2002  stack))
2003  {
2004  _bt_relbuf(rel, leafbuf);
2005  return;
2006  }
2007  }
2008 
2009  /*
2010  * Then unlink it from its siblings. Each call to
2011  * _bt_unlink_halfdead_page unlinks the topmost page from the subtree,
2012  * making it shallower. Iterate until the leafbuf page is deleted.
2013  */
2014  rightsib_empty = false;
2015  Assert(P_ISLEAF(opaque) && P_ISHALFDEAD(opaque));
2016  while (P_ISHALFDEAD(opaque))
2017  {
2018  /* Check for interrupts in _bt_unlink_halfdead_page */
2019  if (!_bt_unlink_halfdead_page(rel, leafbuf, scanblkno,
2020  &rightsib_empty, vstate))
2021  {
2022  /*
2023  * _bt_unlink_halfdead_page should never fail, since we
2024  * established that deletion is generally safe in
2025  * _bt_mark_page_halfdead -- index must be corrupt.
2026  *
2027  * Note that _bt_unlink_halfdead_page already released the
2028  * lock and pin on leafbuf for us.
2029  */
2030  Assert(false);
2031  return;
2032  }
2033  }
2034 
2035  Assert(P_ISLEAF(opaque) && P_ISDELETED(opaque));
2036 
2037  rightsib = opaque->btpo_next;
2038 
2039  _bt_relbuf(rel, leafbuf);
2040 
2041  /*
2042  * Check here, as calling loops will have locks held, preventing
2043  * interrupts from being processed.
2044  */
2046 
2047  /*
2048  * The page has now been deleted. If its right sibling is completely
2049  * empty, it's possible that the reason we haven't deleted it earlier
2050  * is that it was the rightmost child of the parent. Now that we
2051  * removed the downlink for this page, the right sibling might now be
2052  * the only child of the parent, and could be removed. It would be
2053  * picked up by the next vacuum anyway, but might as well try to
2054  * remove it now, so loop back to process the right sibling.
2055  *
2056  * Note: This relies on the assumption that _bt_getstackbuf() will be
2057  * able to reuse our original descent stack with a different child
2058  * block (provided that the child block is to the right of the
2059  * original leaf page reached by _bt_search()). It will even update
2060  * the descent stack each time we loop around, avoiding repeated work.
2061  */
2062  if (!rightsib_empty)
2063  break;
2064 
2065  leafbuf = _bt_getbuf(rel, rightsib, BT_WRITE);
2066  }
2067 }
#define LOG
Definition: elog.h:31
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:547
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static bool _bt_leftsib_splitflag(Relation rel, BlockNumber leftsib, BlockNumber target)
Definition: nbtpage.c:1695
static bool _bt_mark_page_halfdead(Relation rel, Relation heaprel, Buffer leafbuf, BTStack stack)
Definition: nbtpage.c:2088
static bool _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno, bool *rightsib_empty, BTVacState *vstate)
Definition: nbtpage.c:2314
#define P_ISHALFDEAD(opaque)
Definition: nbtree.h:224
#define P_ISDELETED(opaque)
Definition: nbtree.h:222
#define P_ISROOT(opaque)
Definition: nbtree.h:221
IndexVacuumInfo * info
Definition: nbtree.h:332
Relation heaprel
Definition: genam.h:47

References _bt_getbuf(), _bt_leftsib_splitflag(), _bt_lockbuf(), _bt_mark_page_halfdead(), _bt_mkscankey(), _bt_relbuf(), _bt_search(), _bt_unlink_halfdead_page(), _bt_unlockbuf(), Assert(), BTScanInsertData::backward, BT_READ, BT_WRITE, BTPageGetOpaque, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, BufferGetBlockNumber(), BufferGetPage(), CHECK_FOR_INTERRUPTS, CopyIndexTuple(), ereport, errcode(), errhint(), errmsg(), errmsg_internal(), IndexVacuumInfo::heaprel, BTVacState::info, LOG, BTScanInsertData::nextkey, P_FIRSTDATAKEY, P_HIKEY, P_IGNORE, P_INCOMPLETE_SPLIT, P_ISDELETED, P_ISHALFDEAD, P_ISLEAF, P_ISROOT, P_RIGHTMOST, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), RelationGetRelationName, and ReleaseBuffer().

Referenced by btvacuumpage().

◆ _bt_pageinit()

void _bt_pageinit ( Page  page,
Size  size 
)

Definition at line 1129 of file nbtpage.c.

1130 {
1131  PageInit(page, size, sizeof(BTPageOpaqueData));
1132 }
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
static pg_noinline void Size size
Definition: slab.c:607

References PageInit(), and size.

Referenced by _bt_allocbuf(), _bt_blnewpage(), _bt_initmetapage(), _bt_restore_meta(), _bt_split(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_split(), and btree_xlog_unlink_page().

◆ _bt_parallel_advance_array_keys()

void _bt_parallel_advance_array_keys ( IndexScanDesc  scan)

Definition at line 746 of file nbtree.c.

747 {
748  BTScanOpaque so = (BTScanOpaque) scan->opaque;
749  ParallelIndexScanDesc parallel_scan = scan->parallel_scan;
750  BTParallelScanDesc btscan;
751 
752  btscan = (BTParallelScanDesc) OffsetToPointer((void *) parallel_scan,
753  parallel_scan->ps_offset);
754 
755  so->arrayKeyCount++;
756  SpinLockAcquire(&btscan->btps_mutex);
757  if (btscan->btps_pageStatus == BTPARALLEL_DONE)
758  {
759  btscan->btps_scanPage = InvalidBlockNumber;
760  btscan->btps_pageStatus = BTPARALLEL_NOT_INITIALIZED;
761  btscan->btps_arrayKeyCount++;
762  }
763  SpinLockRelease(&btscan->btps_mutex);
764 }
#define OffsetToPointer(base, offset)
Definition: c.h:759
@ BTPARALLEL_NOT_INITIALIZED
Definition: nbtree.c:54
@ BTPARALLEL_DONE
Definition: nbtree.c:57
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62

References BTScanOpaqueData::arrayKeyCount, BTPARALLEL_DONE, BTPARALLEL_NOT_INITIALIZED, InvalidBlockNumber, OffsetToPointer, IndexScanDescData::opaque, IndexScanDescData::parallel_scan, SpinLockAcquire, and SpinLockRelease.

Referenced by _bt_advance_array_keys().

◆ _bt_parallel_build_main()

void _bt_parallel_build_main ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1740 of file nbtsort.c.

1741 {
1742  char *sharedquery;
1743  BTSpool *btspool;
1744  BTSpool *btspool2;
1745  BTShared *btshared;
1746  Sharedsort *sharedsort;
1747  Sharedsort *sharedsort2;
1748  Relation heapRel;
1749  Relation indexRel;
1750  LOCKMODE heapLockmode;
1751  LOCKMODE indexLockmode;
1752  WalUsage *walusage;
1753  BufferUsage *bufferusage;
1754  int sortmem;
1755 
1756 #ifdef BTREE_BUILD_STATS
1758  ResetUsage();
1759 #endif /* BTREE_BUILD_STATS */
1760 
1761  /*
1762  * The only possible status flag that can be set to the parallel worker is
1763  * PROC_IN_SAFE_IC.
1764  */
1765  Assert((MyProc->statusFlags == 0) ||
1767 
1768  /* Set debug_query_string for individual workers first */
1769  sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);
1770  debug_query_string = sharedquery;
1771 
1772  /* Report the query string from leader */
1774 
1775  /* Look up nbtree shared state */
1776  btshared = shm_toc_lookup(toc, PARALLEL_KEY_BTREE_SHARED, false);
1777 
1778  /* Open relations using lock modes known to be obtained by index.c */
1779  if (!btshared->isconcurrent)
1780  {
1781  heapLockmode = ShareLock;
1782  indexLockmode = AccessExclusiveLock;
1783  }
1784  else
1785  {
1786  heapLockmode = ShareUpdateExclusiveLock;
1787  indexLockmode = RowExclusiveLock;
1788  }
1789 
1790  /* Open relations within worker */
1791  heapRel = table_open(btshared->heaprelid, heapLockmode);
1792  indexRel = index_open(btshared->indexrelid, indexLockmode);
1793 
1794  /* Initialize worker's own spool */
1795  btspool = (BTSpool *) palloc0(sizeof(BTSpool));
1796  btspool->heap = heapRel;
1797  btspool->index = indexRel;
1798  btspool->isunique = btshared->isunique;
1799  btspool->nulls_not_distinct = btshared->nulls_not_distinct;
1800 
1801  /* Look up shared state private to tuplesort.c */
1802  sharedsort = shm_toc_lookup(toc, PARALLEL_KEY_TUPLESORT, false);
1803  tuplesort_attach_shared(sharedsort, seg);
1804  if (!btshared->isunique)
1805  {
1806  btspool2 = NULL;
1807  sharedsort2 = NULL;
1808  }
1809  else
1810  {
1811  /* Allocate memory for worker's own private secondary spool */
1812  btspool2 = (BTSpool *) palloc0(sizeof(BTSpool));
1813 
1814  /* Initialize worker's own secondary spool */
1815  btspool2->heap = btspool->heap;
1816  btspool2->index = btspool->index;
1817  btspool2->isunique = false;
1818  /* Look up shared state private to tuplesort.c */
1819  sharedsort2 = shm_toc_lookup(toc, PARALLEL_KEY_TUPLESORT_SPOOL2, false);
1820  tuplesort_attach_shared(sharedsort2, seg);
1821  }
1822 
1823  /* Prepare to track buffer usage during parallel execution */
1825 
1826  /* Perform sorting of spool, and possibly a spool2 */
1827  sortmem = maintenance_work_mem / btshared->scantuplesortstates;
1828  _bt_parallel_scan_and_sort(btspool, btspool2, btshared, sharedsort,
1829  sharedsort2, sortmem, false);
1830 
1831  /* Report WAL/buffer usage during parallel execution */
1832  bufferusage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1833  walusage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1835  &walusage[ParallelWorkerNumber]);
1836 
1837 #ifdef BTREE_BUILD_STATS
1839  {
1840  ShowUsage("BTREE BUILD (Worker Partial Spool) STATISTICS");
1841  ResetUsage();
1842  }
1843 #endif /* BTREE_BUILD_STATS */
1844 
1845  index_close(indexRel, indexLockmode);
1846  table_close(heapRel, heapLockmode);
1847 }
int ParallelWorkerNumber
Definition: parallel.c:112
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
int maintenance_work_mem
Definition: globals.c:130
bool log_btree_build_stats
Definition: guc_tables.c:508
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
void InstrStartParallelQuery(void)
Definition: instrument.c:200
int LOCKMODE
Definition: lockdefs.h:26
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define ShareLock
Definition: lockdefs.h:40
#define RowExclusiveLock
Definition: lockdefs.h:38
#define PARALLEL_KEY_BUFFER_USAGE
Definition: nbtsort.c:67
#define PARALLEL_KEY_TUPLESORT_SPOOL2
Definition: nbtsort.c:64
static void _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2, BTShared *btshared, Sharedsort *sharedsort, Sharedsort *sharedsort2, int sortmem, bool progress)
Definition: nbtsort.c:1862
#define PARALLEL_KEY_BTREE_SHARED
Definition: nbtsort.c:62
#define PARALLEL_KEY_TUPLESORT
Definition: nbtsort.c:63
#define PARALLEL_KEY_QUERY_TEXT
Definition: nbtsort.c:65
#define PARALLEL_KEY_WAL_USAGE
Definition: nbtsort.c:66
const char * debug_query_string
Definition: postgres.c:87
void ShowUsage(const char *title)
Definition: postgres.c:5000
void ResetUsage(void)
Definition: postgres.c:4993
#define PROC_IN_SAFE_IC
Definition: proc.h:59
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
PGPROC * MyProc
Definition: proc.c:66
bool isconcurrent
Definition: nbtsort.c:105
Oid heaprelid
Definition: nbtsort.c:101
bool isunique
Definition: nbtsort.c:103
int scantuplesortstates
Definition: nbtsort.c:106
Oid indexrelid
Definition: nbtsort.c:102
bool nulls_not_distinct
Definition: nbtsort.c:104
bool isunique
Definition: nbtsort.c:85
bool nulls_not_distinct
Definition: nbtsort.c:86
Relation heap
Definition: nbtsort.c:83
Relation index
Definition: nbtsort.c:84
uint8 statusFlags
Definition: proc.h:238
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void tuplesort_attach_shared(Sharedsort *shared, dsm_segment *seg)
Definition: tuplesort.c:2993

References _bt_parallel_scan_and_sort(), AccessExclusiveLock, Assert(), debug_query_string, BTSpool::heap, BTShared::heaprelid, BTSpool::index, index_close(), index_open(), BTShared::indexrelid, InstrEndParallelQuery(), InstrStartParallelQuery(), BTShared::isconcurrent, BTSpool::isunique, BTShared::isunique, log_btree_build_stats, maintenance_work_mem, MyProc, BTSpool::nulls_not_distinct, BTShared::nulls_not_distinct, palloc0(), PARALLEL_KEY_BTREE_SHARED, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_QUERY_TEXT, PARALLEL_KEY_TUPLESORT, PARALLEL_KEY_TUPLESORT_SPOOL2, PARALLEL_KEY_WAL_USAGE, ParallelWorkerNumber, pgstat_report_activity(), PROC_IN_SAFE_IC, ResetUsage(), RowExclusiveLock, BTShared::scantuplesortstates, ShareLock, ShareUpdateExclusiveLock, shm_toc_lookup(), ShowUsage(), STATE_RUNNING, PGPROC::statusFlags, table_close(), table_open(), and tuplesort_attach_shared().

◆ _bt_parallel_done()

void _bt_parallel_done ( IndexScanDesc  scan)

Definition at line 705 of file nbtree.c.

706 {
707  BTScanOpaque so = (BTScanOpaque) scan->opaque;
708  ParallelIndexScanDesc parallel_scan = scan->parallel_scan;
709  BTParallelScanDesc btscan;
710  bool status_changed = false;
711 
712  /* Do nothing, for non-parallel scans */
713  if (parallel_scan == NULL)
714  return;
715 
716  btscan = (BTParallelScanDesc) OffsetToPointer((void *) parallel_scan,
717  parallel_scan->ps_offset);
718 
719  /*
720  * Mark the parallel scan as done for this combination of scan keys,
721  * unless some other process already did so. See also
722  * _bt_advance_array_keys.
723  */
724  SpinLockAcquire(&btscan->btps_mutex);
725  if (so->arrayKeyCount >= btscan->btps_arrayKeyCount &&
726  btscan->btps_pageStatus != BTPARALLEL_DONE)
727  {
728  btscan->btps_pageStatus = BTPARALLEL_DONE;
729  status_changed = true;
730  }
731  SpinLockRelease(&btscan->btps_mutex);
732 
733  /* wake up all the workers associated with this parallel scan */
734  if (status_changed)
735  ConditionVariableBroadcast(&btscan->btps_cv);
736 }
void ConditionVariableBroadcast(ConditionVariable *cv)
struct BTParallelScanDescData * BTParallelScanDesc
Definition: nbtree.c:76

References BTScanOpaqueData::arrayKeyCount, BTPARALLEL_DONE, ConditionVariableBroadcast(), if(), OffsetToPointer, IndexScanDescData::opaque, IndexScanDescData::parallel_scan, SpinLockAcquire, and SpinLockRelease.

Referenced by _bt_first(), and _bt_readnextpage().

◆ _bt_parallel_release()

void _bt_parallel_release ( IndexScanDesc  scan,
BlockNumber  scan_page 
)

Definition at line 682 of file nbtree.c.

683 {
684  ParallelIndexScanDesc parallel_scan = scan->parallel_scan;
685  BTParallelScanDesc btscan;
686 
687  btscan = (BTParallelScanDesc) OffsetToPointer((void *) parallel_scan,
688  parallel_scan->ps_offset);
689 
690  SpinLockAcquire(&btscan->btps_mutex);
691  btscan->btps_scanPage = scan_page;
693  SpinLockRelease(&btscan->btps_mutex);
695 }
void ConditionVariableSignal(ConditionVariable *cv)
@ BTPARALLEL_IDLE
Definition: nbtree.c:56
slock_t btps_mutex
Definition: nbtree.c:72
BTPS_State btps_pageStatus
Definition: nbtree.c:67
ConditionVariable btps_cv
Definition: nbtree.c:73
BlockNumber btps_scanPage
Definition: nbtree.c:66

References BTPARALLEL_IDLE, BTParallelScanDescData::btps_cv, BTParallelScanDescData::btps_mutex, BTParallelScanDescData::btps_pageStatus, BTParallelScanDescData::btps_scanPage, ConditionVariableSignal(), OffsetToPointer, IndexScanDescData::parallel_scan, ParallelIndexScanDescData::ps_offset, SpinLockAcquire, and SpinLockRelease.

Referenced by _bt_readnextpage(), and _bt_readpage().

◆ _bt_parallel_seize()

bool _bt_parallel_seize ( IndexScanDesc  scan,
BlockNumber pageno 
)

Definition at line 624 of file nbtree.c.

625 {
626  BTScanOpaque so = (BTScanOpaque) scan->opaque;
627  BTPS_State pageStatus;
628  bool exit_loop = false;
629  bool status = true;
630  ParallelIndexScanDesc parallel_scan = scan->parallel_scan;
631  BTParallelScanDesc btscan;
632 
633  *pageno = P_NONE;
634 
635  btscan = (BTParallelScanDesc) OffsetToPointer((void *) parallel_scan,
636  parallel_scan->ps_offset);
637 
638  while (1)
639  {
640  SpinLockAcquire(&btscan->btps_mutex);
641  pageStatus = btscan->btps_pageStatus;
642 
643  if (so->arrayKeyCount < btscan->btps_arrayKeyCount)
644  {
645  /* Parallel scan has already advanced to a new set of scankeys. */
646  status = false;
647  }
648  else if (pageStatus == BTPARALLEL_DONE)
649  {
650  /*
651  * We're done with this set of scankeys. This may be the end, or
652  * there could be more sets to try.
653  */
654  status = false;
655  }
656  else if (pageStatus != BTPARALLEL_ADVANCING)
657  {
658  /*
659  * We have successfully seized control of the scan for the purpose
660  * of advancing it to a new page!
661  */
662  btscan->btps_pageStatus = BTPARALLEL_ADVANCING;
663  *pageno = btscan->btps_scanPage;
664  exit_loop = true;
665  }
666  SpinLockRelease(&btscan->btps_mutex);
667  if (exit_loop || !status)
668  break;
669  ConditionVariableSleep(&btscan->btps_cv, WAIT_EVENT_BTREE_PAGE);
670  }
672 
673  return status;
674 }
bool ConditionVariableCancelSleep(void)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
BTPS_State
Definition: nbtree.c:53
@ BTPARALLEL_ADVANCING
Definition: nbtree.c:55

References BTScanOpaqueData::arrayKeyCount, BTPARALLEL_ADVANCING, BTPARALLEL_DONE, ConditionVariableCancelSleep(), ConditionVariableSleep(), OffsetToPointer, IndexScanDescData::opaque, P_NONE, IndexScanDescData::parallel_scan, SpinLockAcquire, and SpinLockRelease.

Referenced by _bt_first(), _bt_readnextpage(), and _bt_steppage().

◆ _bt_pendingfsm_finalize()

void _bt_pendingfsm_finalize ( Relation  rel,
BTVacState vstate 
)

Definition at line 2995 of file nbtpage.c.

2996 {
2997  IndexBulkDeleteResult *stats = vstate->stats;
2998  Relation heaprel = vstate->info->heaprel;
2999 
3000  Assert(stats->pages_newly_deleted >= vstate->npendingpages);
3001  Assert(heaprel != NULL);
3002 
3003  if (vstate->npendingpages == 0)
3004  {
3005  /* Just free memory when nothing to do */
3006  if (vstate->pendingpages)
3007  pfree(vstate->pendingpages);
3008 
3009  return;
3010  }
3011 
3012 #ifdef DEBUG_BTREE_PENDING_FSM
3013 
3014  /*
3015  * Debugging aid: Sleep for 5 seconds to greatly increase the chances of
3016  * placing pending pages in the FSM. Note that the optimization will
3017  * never be effective without some other backend concurrently consuming an
3018  * XID.
3019  */
3020  pg_usleep(5000000L);
3021 #endif
3022 
3023  /*
3024  * Recompute VACUUM XID boundaries.
3025  *
3026  * We don't actually care about the oldest non-removable XID. Computing
3027  * the oldest such XID has a useful side-effect that we rely on: it
3028  * forcibly updates the XID horizon state for this backend. This step is
3029  * essential; GlobalVisCheckRemovableFullXid() will not reliably recognize
3030  * that it is now safe to recycle newly deleted pages without this step.
3031  */
3033 
3034  for (int i = 0; i < vstate->npendingpages; i++)
3035  {
3036  BlockNumber target = vstate->pendingpages[i].target;
3037  FullTransactionId safexid = vstate->pendingpages[i].safexid;
3038 
3039  /*
3040  * Do the equivalent of checking BTPageIsRecyclable(), but without
3041  * accessing the page again a second time.
3042  *
3043  * Give up on finding the first non-recyclable page -- all later pages
3044  * must be non-recyclable too, since _bt_pendingfsm_add() adds pages
3045  * to the array in safexid order.
3046  */
3047  if (!GlobalVisCheckRemovableFullXid(heaprel, safexid))
3048  break;
3049 
3050  RecordFreeIndexPage(rel, target);
3051  stats->pages_free++;
3052  }
3053 
3054  pfree(vstate->pendingpages);
3055 }
void RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
Definition: indexfsm.c:52
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:1993
bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid)
Definition: procarray.c:4300
void pg_usleep(long microsec)
Definition: signal.c:53
FullTransactionId safexid
Definition: nbtree.h:327
BlockNumber target
Definition: nbtree.h:326
IndexBulkDeleteResult * stats
Definition: nbtree.h:333
BTPendingFSM * pendingpages
Definition: nbtree.h:344
int npendingpages
Definition: nbtree.h:345
BlockNumber pages_newly_deleted
Definition: genam.h:81
BlockNumber pages_free
Definition: genam.h:83

References Assert(), GetOldestNonRemovableTransactionId(), GlobalVisCheckRemovableFullXid(), IndexVacuumInfo::heaprel, i, BTVacState::info, BTVacState::npendingpages, IndexBulkDeleteResult::pages_free, IndexBulkDeleteResult::pages_newly_deleted, BTVacState::pendingpages, pfree(), pg_usleep(), RecordFreeIndexPage(), BTPendingFSM::safexid, BTVacState::stats, and BTPendingFSM::target.

Referenced by btvacuumscan().

◆ _bt_pendingfsm_init()

void _bt_pendingfsm_init ( Relation  rel,
BTVacState vstate,
bool  cleanuponly 
)

Definition at line 2954 of file nbtpage.c.

2955 {
2956  int64 maxbufsize;
2957 
2958  /*
2959  * Don't bother with optimization in cleanup-only case -- we don't expect
2960  * any newly deleted pages. Besides, cleanup-only calls to btvacuumscan()
2961  * can only take place because this optimization didn't work out during
2962  * the last VACUUM.
2963  */
2964  if (cleanuponly)
2965  return;
2966 
2967  /*
2968  * Cap maximum size of array so that we always respect work_mem. Avoid
2969  * int overflow here.
2970  */
2971  vstate->bufsize = 256;
2972  maxbufsize = (work_mem * 1024L) / sizeof(BTPendingFSM);
2973  maxbufsize = Min(maxbufsize, INT_MAX);
2974  maxbufsize = Min(maxbufsize, MaxAllocSize / sizeof(BTPendingFSM));
2975  /* Stay sane with small work_mem */
2976  maxbufsize = Max(maxbufsize, vstate->bufsize);
2977  vstate->maxbufsize = maxbufsize;
2978 
2979  /* Allocate buffer, indicate that there are currently 0 pending pages */
2980  vstate->pendingpages = palloc(sizeof(BTPendingFSM) * vstate->bufsize);
2981  vstate->npendingpages = 0;
2982 }
int work_mem
Definition: globals.c:128
#define MaxAllocSize
Definition: memutils.h:40
int bufsize
Definition: nbtree.h:342
int maxbufsize
Definition: nbtree.h:343

References BTVacState::bufsize, Max, MaxAllocSize, BTVacState::maxbufsize, Min, BTVacState::npendingpages, palloc(), BTVacState::pendingpages, and work_mem.

Referenced by btvacuumscan().

◆ _bt_preprocess_array_keys()

void _bt_preprocess_array_keys ( IndexScanDesc  scan)

Definition at line 201 of file nbtutils.c.

202 {
203  BTScanOpaque so = (BTScanOpaque) scan->opaque;
204  int numberOfKeys = scan->numberOfKeys;
205  int16 *indoption = scan->indexRelation->rd_indoption;
206  int numArrayKeys;
207  ScanKey cur;
208  int i;
209  MemoryContext oldContext;
210 
211  /* Quick check to see if there are any array keys */
212  numArrayKeys = 0;
213  for (i = 0; i < numberOfKeys; i++)
214  {
215  cur = &scan->keyData[i];
216  if (cur->sk_flags & SK_SEARCHARRAY)
217  {
218  numArrayKeys++;
219  Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
220  /* If any arrays are null as a whole, we can quit right now. */
221  if (cur->sk_flags & SK_ISNULL)
222  {
223  so->numArrayKeys = -1;
224  so->arrayKeyData = NULL;
225  return;
226  }
227  }
228  }
229 
230  /* Quit if nothing to do. */
231  if (numArrayKeys == 0)
232  {
233  so->numArrayKeys = 0;
234  so->arrayKeyData = NULL;
235  return;
236  }
237 
238  /*
239  * Make a scan-lifespan context to hold array-associated data, or reset it
240  * if we already have one from a previous rescan cycle.
241  */
242  if (so->arrayContext == NULL)
244  "BTree array context",
246  else
248 
249  oldContext = MemoryContextSwitchTo(so->arrayContext);
250 
251  /* Create modifiable copy of scan->keyData in the workspace context */
252  so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
253  memcpy(so->arrayKeyData,
254  scan->keyData,
255  scan->numberOfKeys * sizeof(ScanKeyData));
256 
257  /* Allocate space for per-array data in the workspace context */
258  so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
259 
260  /* Now process each array key */
261  numArrayKeys = 0;
262  for (i = 0; i < numberOfKeys; i++)
263  {
264  ArrayType *arrayval;
265  int16 elmlen;
266  bool elmbyval;
267  char elmalign;
268  int num_elems;
269  Datum *elem_values;
270  bool *elem_nulls;
271  int num_nonnulls;
272  int j;
273 
274  cur = &so->arrayKeyData[i];
275  if (!(cur->sk_flags & SK_SEARCHARRAY))
276  continue;
277 
278  /*
279  * First, deconstruct the array into elements. Anything allocated
280  * here (including a possibly detoasted array value) is in the
281  * workspace context.
282  */
283  arrayval = DatumGetArrayTypeP(cur->sk_argument);
284  /* We could cache this data, but not clear it's worth it */
286  &elmlen, &elmbyval, &elmalign);
287  deconstruct_array(arrayval,
288  ARR_ELEMTYPE(arrayval),
289  elmlen, elmbyval, elmalign,
290  &elem_values, &elem_nulls, &num_elems);
291 
292  /*
293  * Compress out any null elements. We can ignore them since we assume
294  * all btree operators are strict.
295  */
296  num_nonnulls = 0;
297  for (j = 0; j < num_elems; j++)
298  {
299  if (!elem_nulls[j])
300  elem_values[num_nonnulls++] = elem_values[j];
301  }
302 
303  /* We could pfree(elem_nulls) now, but not worth the cycles */
304 
305  /* If there's no non-nulls, the scan qual is unsatisfiable */
306  if (num_nonnulls == 0)
307  {
308  numArrayKeys = -1;
309  break;
310  }
311 
312  /*
313  * If the comparison operator is not equality, then the array qual
314  * degenerates to a simple comparison against the smallest or largest
315  * non-null array element, as appropriate.
316  */
317  switch (cur->sk_strategy)
318  {
321  cur->sk_argument =
324  elem_values, num_nonnulls);
325  continue;
327  /* proceed with rest of loop */
328  break;
331  cur->sk_argument =
334  elem_values, num_nonnulls);
335  continue;
336  default:
337  elog(ERROR, "unrecognized StrategyNumber: %d",
338  (int) cur->sk_strategy);
339  break;
340  }
341 
342  /*
343  * Sort the non-null elements and eliminate any duplicates. We must
344  * sort in the same ordering used by the index column, so that the
345  * successive primitive indexscans produce data in index order.
346  */
347  num_elems = _bt_sort_array_elements(scan, cur,
348  (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
349  elem_values, num_nonnulls);
350 
351  /*
352  * And set up the BTArrayKeyInfo data.
353  */
354  so->arrayKeys[numArrayKeys].scan_key = i;
355  so->arrayKeys[numArrayKeys].num_elems = num_elems;
356  so->arrayKeys[numArrayKeys].elem_values = elem_values;
357  numArrayKeys++;
358  }
359 
360  so->numArrayKeys = numArrayKeys;
361 
362  MemoryContextSwitchTo(oldContext);
363 }
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3612
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2227
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:371
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey, StrategyNumber strat, Datum *elems, int nelems)
Definition: nbtutils.c:373
static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey, bool reverse, Datum *elems, int nelems)
Definition: nbtutils.c:440
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define SK_SEARCHARRAY
Definition: skey.h:120
MemoryContext arrayContext
Definition: nbtree.h:1048
struct ScanKeyData * keyData
Definition: relscan.h:122

References _bt_find_extreme_element(), _bt_sort_array_elements(), ALLOCSET_SMALL_SIZES, AllocSetContextCreate, ARR_ELEMTYPE, BTScanOpaqueData::arrayContext, BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, cur, CurrentMemoryContext, DatumGetArrayTypeP, deconstruct_array(), BTArrayKeyInfo::elem_values, elog, ERROR, get_typlenbyvalalign(), i, IndexScanDescData::indexRelation, j, IndexScanDescData::keyData, MemoryContextReset(), MemoryContextSwitchTo(), BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, palloc(), palloc0(), RelationData::rd_indoption, BTArrayKeyInfo::scan_key, SK_ISNULL, SK_ROW_HEADER, SK_SEARCHARRAY, SK_SEARCHNOTNULL, and SK_SEARCHNULL.

Referenced by btrescan().

◆ _bt_preprocess_keys()

void _bt_preprocess_keys ( IndexScanDesc  scan)

Definition at line 755 of file nbtutils.c.

756 {
757  BTScanOpaque so = (BTScanOpaque) scan->opaque;
758  int numberOfKeys = scan->numberOfKeys;
759  int16 *indoption = scan->indexRelation->rd_indoption;
760  int new_numberOfKeys;
761  int numberOfEqualCols;
762  ScanKey inkeys;
763  ScanKey outkeys;
764  ScanKey cur;
766  bool test_result;
767  int i,
768  j;
769  AttrNumber attno;
770 
771  /* initialize result variables */
772  so->qual_ok = true;
773  so->numberOfKeys = 0;
774 
775  if (numberOfKeys < 1)
776  return; /* done if qual-less scan */
777 
778  /*
779  * Read so->arrayKeyData if array keys are present, else scan->keyData
780  */
781  if (so->arrayKeyData != NULL)
782  inkeys = so->arrayKeyData;
783  else
784  inkeys = scan->keyData;
785 
786  outkeys = so->keyData;
787  cur = &inkeys[0];
788  /* we check that input keys are correctly ordered */
789  if (cur->sk_attno < 1)
790  elog(ERROR, "btree index keys must be ordered by attribute");
791 
792  /* We can short-circuit most of the work if there's just one key */
793  if (numberOfKeys == 1)
794  {
795  /* Apply indoption to scankey (might change sk_strategy!) */
796  if (!_bt_fix_scankey_strategy(cur, indoption))
797  so->qual_ok = false;
798  memcpy(outkeys, cur, sizeof(ScanKeyData));
799  so->numberOfKeys = 1;
800  /* We can mark the qual as required if it's for first index col */
801  if (cur->sk_attno == 1)
802  _bt_mark_scankey_required(outkeys);
803  return;
804  }
805 
806  /*
807  * Otherwise, do the full set of pushups.
808  */
809  new_numberOfKeys = 0;
810  numberOfEqualCols = 0;
811 
812  /*
813  * Initialize for processing of keys for attr 1.
814  *
815  * xform[i] points to the currently best scan key of strategy type i+1; it
816  * is NULL if we haven't yet found such a key for this attr.
817  */
818  attno = 1;
819  memset(xform, 0, sizeof(xform));
820 
821  /*
822  * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
823  * handle after-last-key processing. Actual exit from the loop is at the
824  * "break" statement below.
825  */
826  for (i = 0;; cur++, i++)
827  {
828  if (i < numberOfKeys)
829  {
830  /* Apply indoption to scankey (might change sk_strategy!) */
831  if (!_bt_fix_scankey_strategy(cur, indoption))
832  {
833  /* NULL can't be matched, so give up */
834  so->qual_ok = false;
835  return;
836  }
837  }
838 
839  /*
840  * If we are at the end of the keys for a particular attr, finish up
841  * processing and emit the cleaned-up keys.
842  */
843  if (i == numberOfKeys || cur->sk_attno != attno)
844  {
845  int priorNumberOfEqualCols = numberOfEqualCols;
846 
847  /* check input keys are correctly ordered */
848  if (i < numberOfKeys && cur->sk_attno < attno)
849  elog(ERROR, "btree index keys must be ordered by attribute");
850 
851  /*
852  * If = has been specified, all other keys can be eliminated as
853  * redundant. If we have a case like key = 1 AND key > 2, we can
854  * set qual_ok to false and abandon further processing.
855  *
856  * We also have to deal with the case of "key IS NULL", which is
857  * unsatisfiable in combination with any other index condition. By
858  * the time we get here, that's been classified as an equality
859  * check, and we've rejected any combination of it with a regular
860  * equality condition; but not with other types of conditions.
861  */
862  if (xform[BTEqualStrategyNumber - 1])
863  {
864  ScanKey eq = xform[BTEqualStrategyNumber - 1];
865 
866  for (j = BTMaxStrategyNumber; --j >= 0;)
867  {
868  ScanKey chk = xform[j];
869 
870  if (!chk || j == (BTEqualStrategyNumber - 1))
871  continue;
872 
873  if (eq->sk_flags & SK_SEARCHNULL)
874  {
875  /* IS NULL is contradictory to anything else */
876  so->qual_ok = false;
877  return;
878  }
879 
880  if (_bt_compare_scankey_args(scan, chk, eq, chk,
881  &test_result))
882  {
883  if (!test_result)
884  {
885  /* keys proven mutually contradictory */
886  so->qual_ok = false;
887  return;
888  }
889  /* else discard the redundant non-equality key */
890  xform[j] = NULL;
891  }
892  /* else, cannot determine redundancy, keep both keys */
893  }
894  /* track number of attrs for which we have "=" keys */
895  numberOfEqualCols++;
896  }
897 
898  /* try to keep only one of <, <= */
899  if (xform[BTLessStrategyNumber - 1]
900  && xform[BTLessEqualStrategyNumber - 1])
901  {
902  ScanKey lt = xform[BTLessStrategyNumber - 1];
903  ScanKey le = xform[BTLessEqualStrategyNumber - 1];
904 
905  if (_bt_compare_scankey_args(scan, le, lt, le,
906  &test_result))
907  {
908  if (test_result)
909  xform[BTLessEqualStrategyNumber - 1] = NULL;
910  else
911  xform[BTLessStrategyNumber - 1] = NULL;
912  }
913  }
914 
915  /* try to keep only one of >, >= */
916  if (xform[BTGreaterStrategyNumber - 1]
917  && xform[BTGreaterEqualStrategyNumber - 1])
918  {
919  ScanKey gt = xform[BTGreaterStrategyNumber - 1];
920  ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
921 
922  if (_bt_compare_scankey_args(scan, ge, gt, ge,
923  &test_result))
924  {
925  if (test_result)
926  xform[BTGreaterEqualStrategyNumber - 1] = NULL;
927  else
928  xform[BTGreaterStrategyNumber - 1] = NULL;
929  }
930  }
931 
932  /*
933  * Emit the cleaned-up keys into the outkeys[] array, and then
934  * mark them if they are required. They are required (possibly
935  * only in one direction) if all attrs before this one had "=".
936  */
937  for (j = BTMaxStrategyNumber; --j >= 0;)
938  {
939  if (xform[j])
940  {
941  ScanKey outkey = &outkeys[new_numberOfKeys++];
942 
943  memcpy(outkey, xform[j], sizeof(ScanKeyData));
944  if (priorNumberOfEqualCols == attno - 1)
946  }
947  }
948 
949  /*
950  * Exit loop here if done.
951  */
952  if (i == numberOfKeys)
953  break;
954 
955  /* Re-initialize for new attno */
956  attno = cur->sk_attno;
957  memset(xform, 0, sizeof(xform));
958  }
959 
960  /* check strategy this key's operator corresponds to */
961  j = cur->sk_strategy - 1;
962 
963  /* if row comparison, push it directly to the output array */
964  if (cur->sk_flags & SK_ROW_HEADER)
965  {
966  ScanKey outkey = &outkeys[new_numberOfKeys++];
967 
968  memcpy(outkey, cur, sizeof(ScanKeyData));
969  if (numberOfEqualCols == attno - 1)
971 
972  /*
973  * We don't support RowCompare using equality; such a qual would
974  * mess up the numberOfEqualCols tracking.
975  */
976  Assert(j != (BTEqualStrategyNumber - 1));
977  continue;
978  }
979 
980  /* have we seen one of these before? */
981  if (xform[j] == NULL)
982  {
983  /* nope, so remember this scankey */
984  xform[j] = cur;
985  }
986  else
987  {
988  /* yup, keep only the more restrictive key */
989  if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
990  &test_result))
991  {
992  if (test_result)
993  xform[j] = cur;
994  else if (j == (BTEqualStrategyNumber - 1))
995  {
996  /* key == a && key == b, but a != b */
997  so->qual_ok = false;
998  return;
999  }
1000  /* else old key is more restrictive, keep it */
1001  }
1002  else
1003  {
1004  /*
1005  * We can't determine which key is more restrictive. Keep the
1006  * previous one in xform[j] and push this one directly to the
1007  * output array.
1008  */
1009  ScanKey outkey = &outkeys[new_numberOfKeys++];
1010 
1011  memcpy(outkey, cur, sizeof(ScanKeyData));
1012  if (numberOfEqualCols == attno - 1)
1013  _bt_mark_scankey_required(outkey);
1014  }
1015  }
1016  }
1017 
1018  so->numberOfKeys = new_numberOfKeys;
1019 }
static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
Definition: nbtutils.c:1209
static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op, ScanKey leftarg, ScanKey rightarg, bool *result)
Definition: nbtutils.c:1045
static void _bt_mark_scankey_required(ScanKey skey)
Definition: nbtutils.c:1311
#define BTMaxStrategyNumber
Definition: stratnum.h:35

References _bt_compare_scankey_args(), _bt_fix_scankey_strategy(), _bt_mark_scankey_required(), BTScanOpaqueData::arrayKeyData, Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTMaxStrategyNumber, cur, elog, ERROR, i, if(), IndexScanDescData::indexRelation, j, BTScanOpaqueData::keyData, IndexScanDescData::keyData, BTScanOpaqueData::numberOfKeys, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, BTScanOpaqueData::qual_ok, RelationData::rd_indoption, ScanKeyData::sk_flags, SK_ROW_HEADER, and SK_SEARCHNULL.

Referenced by _bt_first(), and _bt_restore_array_keys().

◆ _bt_relandgetbuf()

Buffer _bt_relandgetbuf ( Relation  rel,
Buffer  obuf,
BlockNumber  blkno,
int  access 
)

Definition at line 1003 of file nbtpage.c.

1004 {
1005  Buffer buf;
1006 
1007  Assert(BlockNumberIsValid(blkno));
1008  if (BufferIsValid(obuf))
1009  _bt_unlockbuf(rel, obuf);
1010  buf = ReleaseAndReadBuffer(obuf, rel, blkno);
1011  _bt_lockbuf(rel, buf, access);
1012 
1013  _bt_checkpage(rel, buf);
1014  return buf;
1015 }
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:2252

References _bt_checkpage(), _bt_lockbuf(), _bt_unlockbuf(), Assert(), BlockNumberIsValid(), buf, BufferIsValid(), and ReleaseAndReadBuffer().

Referenced by _bt_check_unique(), _bt_get_endpoint(), _bt_getroot(), _bt_gettrueroot(), _bt_moveright(), _bt_search(), _bt_stepright(), and _bt_walk_left().

◆ _bt_relbuf()

◆ _bt_restore_array_keys()

void _bt_restore_array_keys ( IndexScanDesc  scan)

Definition at line 630 of file nbtutils.c.

631 {
632  BTScanOpaque so = (BTScanOpaque) scan->opaque;
633  bool changed = false;
634  int i;
635 
636  /* Restore each array key to its position when the mark was set */
637  for (i = 0; i < so->numArrayKeys; i++)
638  {
639  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
640  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
641  int mark_elem = curArrayKey->mark_elem;
642 
643  if (curArrayKey->cur_elem != mark_elem)
644  {
645  curArrayKey->cur_elem = mark_elem;
646  skey->sk_argument = curArrayKey->elem_values[mark_elem];
647  changed = true;
648  }
649  }
650 
651  /*
652  * If we changed any keys, we must redo _bt_preprocess_keys. That might
653  * sound like overkill, but in cases with multiple keys per index column
654  * it seems necessary to do the full set of pushups.
655  *
656  * Also do this whenever the scan's set of array keys "wrapped around" at
657  * the end of the last primitive index scan. There won't have been a call
658  * to _bt_preprocess_keys from some other place following wrap around, so
659  * we do it for ourselves.
660  */
661  if (changed || !so->arraysStarted)
662  {
663  _bt_preprocess_keys(scan);
664  /* The mark should have been set on a consistent set of keys... */
665  Assert(so->qual_ok);
666  }
667 }

References _bt_preprocess_keys(), BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, BTScanOpaqueData::arraysStarted, Assert(), BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, BTArrayKeyInfo::mark_elem, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, BTScanOpaqueData::qual_ok, BTArrayKeyInfo::scan_key, and ScanKeyData::sk_argument.

Referenced by btrestrpos().

◆ _bt_search()

BTStack _bt_search ( Relation  rel,
Relation  heaprel,
BTScanInsert  key,
Buffer bufP,
int  access 
)

Definition at line 96 of file nbtsearch.c.

98 {
99  BTStack stack_in = NULL;
100  int page_access = BT_READ;
101 
102  /* heaprel must be set whenever _bt_allocbuf is reachable */
103  Assert(access == BT_READ || access == BT_WRITE);
104  Assert(access == BT_READ || heaprel != NULL);
105 
106  /* Get the root page to start with */
107  *bufP = _bt_getroot(rel, heaprel, access);
108 
109  /* If index is empty and access = BT_READ, no root page is created. */
110  if (!BufferIsValid(*bufP))
111  return (BTStack) NULL;
112 
113  /* Loop iterates once per level descended in the tree */
114  for (;;)
115  {
116  Page page;
117  BTPageOpaque opaque;
118  OffsetNumber offnum;
119  ItemId itemid;
120  IndexTuple itup;
121  BlockNumber child;
122  BTStack new_stack;
123 
124  /*
125  * Race -- the page we just grabbed may have split since we read its
126  * downlink in its parent page (or the metapage). If it has, we may
127  * need to move right to its new sibling. Do that.
128  *
129  * In write-mode, allow _bt_moveright to finish any incomplete splits
130  * along the way. Strictly speaking, we'd only need to finish an
131  * incomplete split on the leaf page we're about to insert to, not on
132  * any of the upper levels (internal pages with incomplete splits are
133  * also taken care of in _bt_getstackbuf). But this is a good
134  * opportunity to finish splits of internal pages too.
135  */
136  *bufP = _bt_moveright(rel, heaprel, key, *bufP, (access == BT_WRITE),
137  stack_in, page_access);
138 
139  /* if this is a leaf page, we're done */
140  page = BufferGetPage(*bufP);
141  opaque = BTPageGetOpaque(page);
142  if (P_ISLEAF(opaque))
143  break;
144 
145  /*
146  * Find the appropriate pivot tuple on this page. Its downlink points
147  * to the child page that we're about to descend to.
148  */
149  offnum = _bt_binsrch(rel, key, *bufP);
150  itemid = PageGetItemId(page, offnum);
151  itup = (IndexTuple) PageGetItem(page, itemid);
152  Assert(BTreeTupleIsPivot(itup) || !key->heapkeyspace);
153  child = BTreeTupleGetDownLink(itup);
154 
155  /*
156  * We need to save the location of the pivot tuple we chose in a new
157  * stack entry for this page/level. If caller ends up splitting a
158  * page one level down, it usually ends up inserting a new pivot
159  * tuple/downlink immediately after the location recorded here.
160  */
161  new_stack = (BTStack) palloc(sizeof(BTStackData));
162  new_stack->bts_blkno = BufferGetBlockNumber(*bufP);
163  new_stack->bts_offset = offnum;
164  new_stack->bts_parent = stack_in;
165 
166  /*
167  * Page level 1 is lowest non-leaf page level prior to leaves. So, if
168  * we're on the level 1 and asked to lock leaf page in write mode,
169  * then lock next page in write mode, because it must be a leaf.
170  */
171  if (opaque->btpo_level == 1 && access == BT_WRITE)
172  page_access = BT_WRITE;
173 
174  /* drop the read lock on the page, then acquire one on its child */
175  *bufP = _bt_relandgetbuf(rel, *bufP, child, page_access);
176 
177  /* okay, all set to move down a level */
178  stack_in = new_stack;
179  }
180 
181  /*
182  * If we're asked to lock leaf in write mode, but didn't manage to, then
183  * relock. This should only happen when the root page is a leaf page (and
184  * the only page in the index other than the metapage).
185  */
186  if (access == BT_WRITE && page_access == BT_READ)
187  {
188  /* trade in our read lock for a write lock */
189  _bt_unlockbuf(rel, *bufP);
190  _bt_lockbuf(rel, *bufP, BT_WRITE);
191 
192  /*
193  * Race -- the leaf page may have split after we dropped the read lock
194  * but before we acquired a write lock. If it has, we may need to
195  * move right to its new sibling. Do that.
196  */
197  *bufP = _bt_moveright(rel, heaprel, key, *bufP, true, stack_in, BT_WRITE);
198  }
199 
200  return stack_in;
201 }
BTStackData * BTStack
Definition: nbtree.h:739
Buffer _bt_moveright(Relation rel, Relation heaprel, BTScanInsert key, Buffer buf, bool forupdate, BTStack stack, int access)
Definition: nbtsearch.c:235

References _bt_binsrch(), _bt_getroot(), _bt_lockbuf(), _bt_moveright(), _bt_relandgetbuf(), _bt_unlockbuf(), Assert(), BT_READ, BT_WRITE, BTPageGetOpaque, BTPageOpaqueData::btpo_level, BTreeTupleGetDownLink(), BTreeTupleIsPivot(), BTStackData::bts_blkno, BTStackData::bts_offset, BTStackData::bts_parent, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), sort-test::key, P_ISLEAF, PageGetItem(), PageGetItemId(), and palloc().

Referenced by _bt_first(), _bt_pagedel(), _bt_search_insert(), and bt_rootdescend().

◆ _bt_set_cleanup_info()

void _bt_set_cleanup_info ( Relation  rel,
BlockNumber  num_delpages 
)

Definition at line 232 of file nbtpage.c.

233 {
234  Buffer metabuf;
235  Page metapg;
236  BTMetaPageData *metad;
237 
238  /*
239  * On-disk compatibility note: The btm_last_cleanup_num_delpages metapage
240  * field started out as a TransactionId field called btm_oldest_btpo_xact.
241  * Both "versions" are just uint32 fields. It was convenient to repurpose
242  * the field when we began to use 64-bit XIDs in deleted pages.
243  *
244  * It's possible that a pg_upgrade'd database will contain an XID value in
245  * what is now recognized as the metapage's btm_last_cleanup_num_delpages
246  * field. _bt_vacuum_needs_cleanup() may even believe that this value
247  * indicates that there are lots of pages that it needs to recycle, when
248  * in reality there are only one or two. The worst that can happen is
249  * that there will be a call to btvacuumscan a little earlier, which will
250  * set btm_last_cleanup_num_delpages to a sane value when we're called.
251  *
252  * Note also that the metapage's btm_last_cleanup_num_heap_tuples field is
253  * no longer used as of PostgreSQL 14. We set it to -1.0 on rewrite, just
254  * to be consistent.
255  */
256  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
257  metapg = BufferGetPage(metabuf);
258  metad = BTPageGetMeta(metapg);
259 
260  /* Don't miss chance to upgrade index/metapage when BTREE_MIN_VERSION */
261  if (metad->btm_version >= BTREE_NOVAC_VERSION &&
262  metad->btm_last_cleanup_num_delpages == num_delpages)
263  {
264  /* Usually means index continues to have num_delpages of 0 */
265  _bt_relbuf(rel, metabuf);
266  return;
267  }
268 
269  /* trade in our read lock for a write lock */
270  _bt_unlockbuf(rel, metabuf);
271  _bt_lockbuf(rel, metabuf, BT_WRITE);
272 
274 
275  /* upgrade meta-page if needed */
276  if (metad->btm_version < BTREE_NOVAC_VERSION)
277  _bt_upgrademetapage(metapg);
278 
279  /* update cleanup-related information */
280  metad->btm_last_cleanup_num_delpages = num_delpages;
281  metad->btm_last_cleanup_num_heap_tuples = -1.0;
282  MarkBufferDirty(metabuf);
283 
284  /* write wal record if needed */
285  if (RelationNeedsWAL(rel))
286  {
288  XLogRecPtr recptr;
289 
290  XLogBeginInsert();
292 
294  md.version = metad->btm_version;
295  md.root = metad->btm_root;
296  md.level = metad->btm_level;
297  md.fastroot = metad->btm_fastroot;
298  md.fastlevel = metad->btm_fastlevel;
299  md.last_cleanup_num_delpages = num_delpages;
300  md.allequalimage = metad->btm_allequalimage;
301 
302  XLogRegisterBufData(0, (char *) &md, sizeof(xl_btree_metadata));
303 
304  recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_META_CLEANUP);
305 
306  PageSetLSN(metapg, recptr);
307  }
308 
310 
311  _bt_relbuf(rel, metabuf);
312 }
#define XLOG_BTREE_META_CLEANUP
Definition: nbtxlog.h:41

References _bt_getbuf(), _bt_lockbuf(), _bt_relbuf(), _bt_unlockbuf(), _bt_upgrademetapage(), xl_btree_metadata::allequalimage, Assert(), BT_READ, BT_WRITE, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, BTREE_METAPAGE, BTREE_NOVAC_VERSION, BufferGetPage(), END_CRIT_SECTION, xl_btree_metadata::fastlevel, xl_btree_metadata::fastroot, xl_btree_metadata::last_cleanup_num_delpages, xl_btree_metadata::level, MarkBufferDirty(), PageSetLSN(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationNeedsWAL, xl_btree_metadata::root, START_CRIT_SECTION, xl_btree_metadata::version, XLOG_BTREE_META_CLEANUP, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), and XLogRegisterBuffer().

Referenced by btvacuumcleanup().

◆ _bt_start_array_keys()

void _bt_start_array_keys ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 516 of file nbtutils.c.

517 {
518  BTScanOpaque so = (BTScanOpaque) scan->opaque;
519  int i;
520 
521  for (i = 0; i < so->numArrayKeys; i++)
522  {
523  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
524  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
525 
526  Assert(curArrayKey->num_elems > 0);
527  if (ScanDirectionIsBackward(dir))
528  curArrayKey->cur_elem = curArrayKey->num_elems - 1;
529  else
530  curArrayKey->cur_elem = 0;
531  skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
532  }
533 
534  so->arraysStarted = true;
535 }

References BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, BTScanOpaqueData::arraysStarted, Assert(), BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, BTArrayKeyInfo::scan_key, ScanDirectionIsBackward, and ScanKeyData::sk_argument.

Referenced by btgetbitmap(), and btgettuple().

◆ _bt_start_vacuum()

BTCycleId _bt_start_vacuum ( Relation  rel)

Definition at line 2027 of file nbtutils.c.

2028 {
2029  BTCycleId result;
2030  int i;
2031  BTOneVacInfo *vac;
2032 
2033  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
2034 
2035  /*
2036  * Assign the next cycle ID, being careful to avoid zero as well as the
2037  * reserved high values.
2038  */
2039  result = ++(btvacinfo->cycle_ctr);
2040  if (result == 0 || result > MAX_BT_CYCLE_ID)
2041  result = btvacinfo->cycle_ctr = 1;
2042 
2043  /* Let's just make sure there's no entry already for this index */
2044  for (i = 0; i < btvacinfo->num_vacuums; i++)
2045  {
2046  vac = &btvacinfo->vacuums[i];
2047  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
2048  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
2049  {
2050  /*
2051  * Unlike most places in the backend, we have to explicitly
2052  * release our LWLock before throwing an error. This is because
2053  * we expect _bt_end_vacuum() to be called before transaction
2054  * abort cleanup can run to release LWLocks.
2055  */
2056  LWLockRelease(BtreeVacuumLock);
2057  elog(ERROR, "multiple active vacuums for index \"%s\"",
2059  }
2060  }
2061 
2062  /* OK, add an entry */
2064  {
2065  LWLockRelease(BtreeVacuumLock);
2066  elog(ERROR, "out of btvacinfo slots");
2067  }
2069  vac->relid = rel->rd_lockInfo.lockRelId;
2070  vac->cycleid = result;
2072 
2073  LWLockRelease(BtreeVacuumLock);
2074  return result;
2075 }
#define MAX_BT_CYCLE_ID
Definition: nbtree.h:93
uint16 BTCycleId
Definition: nbtree.h:29
BTCycleId cycleid
Definition: nbtutils.c:1969
BTCycleId cycle_ctr
Definition: nbtutils.c:1974
int max_vacuums
Definition: nbtutils.c:1976

References btvacinfo, BTVacInfo::cycle_ctr, BTOneVacInfo::cycleid, LockRelId::dbId, elog, ERROR, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_BT_CYCLE_ID, BTVacInfo::max_vacuums, BTVacInfo::num_vacuums, RelationData::rd_lockInfo, RelationGetRelationName, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by btbulkdelete().

◆ _bt_swap_posting()

IndexTuple _bt_swap_posting ( IndexTuple  newitem,
IndexTuple  oposting,
int  postingoff 
)

Definition at line 1022 of file nbtdedup.c.

1023 {
1024  int nhtids;
1025  char *replacepos;
1026  char *replaceposright;
1027  Size nmovebytes;
1028  IndexTuple nposting;
1029 
1030  nhtids = BTreeTupleGetNPosting(oposting);
1031  Assert(_bt_posting_valid(oposting));
1032 
1033  /*
1034  * The postingoff argument originated as a _bt_binsrch_posting() return
1035  * value. It will be 0 in the event of corruption that makes a leaf page
1036  * contain a non-pivot tuple that's somehow identical to newitem (no two
1037  * non-pivot tuples should ever have the same TID). This has been known
1038  * to happen in the field from time to time.
1039  *
1040  * Perform a basic sanity check to catch this case now.
1041  */
1042  if (!(postingoff > 0 && postingoff < nhtids))
1043  elog(ERROR, "posting list tuple with %d items cannot be split at offset %d",
1044  nhtids, postingoff);
1045 
1046  /*
1047  * Move item pointers in posting list to make a gap for the new item's
1048  * heap TID. We shift TIDs one place to the right, losing original
1049  * rightmost TID. (nmovebytes must not include TIDs to the left of
1050  * postingoff, nor the existing rightmost/max TID that gets overwritten.)
1051  */
1052  nposting = CopyIndexTuple(oposting);
1053  replacepos = (char *) BTreeTupleGetPostingN(nposting, postingoff);
1054  replaceposright = (char *) BTreeTupleGetPostingN(nposting, postingoff + 1);
1055  nmovebytes = (nhtids - postingoff - 1) * sizeof(ItemPointerData);
1056  memmove(replaceposright, replacepos, nmovebytes);
1057 
1058  /* Fill the gap at postingoff with TID of new item (original new TID) */
1059  Assert(!BTreeTupleIsPivot(newitem) && !BTreeTupleIsPosting(newitem));
1060  ItemPointerCopy(&newitem->t_tid, (ItemPointer) replacepos);
1061 
1062  /* Now copy oposting's rightmost/max TID into new item (final new TID) */
1063  ItemPointerCopy(BTreeTupleGetMaxHeapTID(oposting), &newitem->t_tid);
1064 
1066  BTreeTupleGetHeapTID(newitem)) < 0);
1067  Assert(_bt_posting_valid(nposting));
1068 
1069  return nposting;
1070 }

References Assert(), BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), CopyIndexTuple(), elog, ERROR, ItemPointerCompare(), ItemPointerCopy(), and IndexTupleData::t_tid.

Referenced by _bt_insertonpg(), btree_xlog_insert(), and btree_xlog_split().

◆ _bt_truncate()

IndexTuple _bt_truncate ( Relation  rel,
IndexTuple  lastleft,
IndexTuple  firstright,
BTScanInsert  itup_key 
)

Definition at line 2256 of file nbtutils.c.

2258 {
2259  TupleDesc itupdesc = RelationGetDescr(rel);
2261  int keepnatts;
2262  IndexTuple pivot;
2263  IndexTuple tidpivot;
2264  ItemPointer pivotheaptid;
2265  Size newsize;
2266 
2267  /*
2268  * We should only ever truncate non-pivot tuples from leaf pages. It's
2269  * never okay to truncate when splitting an internal page.
2270  */
2271  Assert(!BTreeTupleIsPivot(lastleft) && !BTreeTupleIsPivot(firstright));
2272 
2273  /* Determine how many attributes must be kept in truncated tuple */
2274  keepnatts = _bt_keep_natts(rel, lastleft, firstright, itup_key);
2275 
2276 #ifdef DEBUG_NO_TRUNCATE
2277  /* Force truncation to be ineffective for testing purposes */
2278  keepnatts = nkeyatts + 1;
2279 #endif
2280 
2281  pivot = index_truncate_tuple(itupdesc, firstright,
2282  Min(keepnatts, nkeyatts));
2283 
2284  if (BTreeTupleIsPosting(pivot))
2285  {
2286  /*
2287  * index_truncate_tuple() just returns a straight copy of firstright
2288  * when it has no attributes to truncate. When that happens, we may
2289  * need to truncate away a posting list here instead.
2290  */
2291  Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
2292  Assert(IndexRelationGetNumberOfAttributes(rel) == nkeyatts);
2293  pivot->t_info &= ~INDEX_SIZE_MASK;
2294  pivot->t_info |= MAXALIGN(BTreeTupleGetPostingOffset(firstright));
2295  }
2296 
2297  /*
2298  * If there is a distinguishing key attribute within pivot tuple, we're
2299  * done
2300  */
2301  if (keepnatts <= nkeyatts)
2302  {
2303  BTreeTupleSetNAtts(pivot, keepnatts, false);
2304  return pivot;
2305  }
2306 
2307  /*
2308  * We have to store a heap TID in the new pivot tuple, since no non-TID
2309  * key attribute value in firstright distinguishes the right side of the
2310  * split from the left side. nbtree conceptualizes this case as an
2311  * inability to truncate away any key attributes, since heap TID is
2312  * treated as just another key attribute (despite lacking a pg_attribute
2313  * entry).
2314  *
2315  * Use enlarged space that holds a copy of pivot. We need the extra space
2316  * to store a heap TID at the end (using the special pivot tuple
2317  * representation). Note that the original pivot already has firstright's
2318  * possible posting list/non-key attribute values removed at this point.
2319  */
2320  newsize = MAXALIGN(IndexTupleSize(pivot)) + MAXALIGN(sizeof(ItemPointerData));
2321  tidpivot = palloc0(newsize);
2322  memcpy(tidpivot, pivot, MAXALIGN(IndexTupleSize(pivot)));
2323  /* Cannot leak memory here */
2324  pfree(pivot);
2325 
2326  /*
2327  * Store all of firstright's key attribute values plus a tiebreaker heap
2328  * TID value in enlarged pivot tuple
2329  */
2330  tidpivot->t_info &= ~INDEX_SIZE_MASK;
2331  tidpivot->t_info |= newsize;
2332  BTreeTupleSetNAtts(tidpivot, nkeyatts, true);
2333  pivotheaptid = BTreeTupleGetHeapTID(tidpivot);
2334 
2335  /*
2336  * Lehman & Yao use lastleft as the leaf high key in all cases, but don't
2337  * consider suffix truncation. It seems like a good idea to follow that
2338  * example in cases where no truncation takes place -- use lastleft's heap
2339  * TID. (This is also the closest value to negative infinity that's
2340  * legally usable.)
2341  */
2342  ItemPointerCopy(BTreeTupleGetMaxHeapTID(lastleft), pivotheaptid);
2343 
2344  /*
2345  * We're done. Assert() that heap TID invariants hold before returning.
2346  *
2347  * Lehman and Yao require that the downlink to the right page, which is to
2348  * be inserted into the parent page in the second phase of a page split be
2349  * a strict lower bound on items on the right page, and a non-strict upper
2350  * bound for items on the left page. Assert that heap TIDs follow these
2351  * invariants, since a heap TID value is apparently needed as a
2352  * tiebreaker.
2353  */
2354 #ifndef DEBUG_NO_TRUNCATE
2356  BTreeTupleGetHeapTID(firstright)) < 0);
2357  Assert(ItemPointerCompare(pivotheaptid,
2358  BTreeTupleGetHeapTID(lastleft)) >= 0);
2359  Assert(ItemPointerCompare(pivotheaptid,
2360  BTreeTupleGetHeapTID(firstright)) < 0);
2361 #else
2362 
2363  /*
2364  * Those invariants aren't guaranteed to hold for lastleft + firstright
2365  * heap TID attribute values when they're considered here only because
2366  * DEBUG_NO_TRUNCATE is defined (a heap TID is probably not actually
2367  * needed as a tiebreaker). DEBUG_NO_TRUNCATE must therefore use a heap
2368  * TID value that always works as a strict lower bound for items to the
2369  * right. In particular, it must avoid using firstright's leading key
2370  * attribute values along with lastleft's heap TID value when lastleft's
2371  * TID happens to be greater than firstright's TID.
2372  */
2373  ItemPointerCopy(BTreeTupleGetHeapTID(firstright), pivotheaptid);
2374 
2375  /*
2376  * Pivot heap TID should never be fully equal to firstright. Note that
2377  * the pivot heap TID will still end up equal to lastleft's heap TID when
2378  * that's the only usable value.
2379  */
2380  ItemPointerSetOffsetNumber(pivotheaptid,
2382  Assert(ItemPointerCompare(pivotheaptid,
2383  BTreeTupleGetHeapTID(firstright)) < 0);
2384 #endif
2385 
2386  return tidpivot;
2387 }
IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
Definition: indextuple.c:576
static void ItemPointerSetOffsetNumber(ItemPointerData *pointer, OffsetNumber offsetNumber)
Definition: itemptr.h:158
static void BTreeTupleSetNAtts(IndexTuple itup, uint16 nkeyatts, bool heaptid)
Definition: nbtree.h:595
static int _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
Definition: nbtutils.c:2401

References _bt_keep_natts(), Assert(), BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTreeTupleSetNAtts(), INDEX_SIZE_MASK, index_truncate_tuple(), IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, IndexTupleSize, ItemPointerCompare(), ItemPointerCopy(), ItemPointerGetOffsetNumber(), ItemPointerSetOffsetNumber(), MAXALIGN, Min, OffsetNumberPrev, palloc0(), pfree(), RelationGetDescr, and IndexTupleData::t_info.

Referenced by _bt_buildadd(), and _bt_split().

◆ _bt_unlockbuf()

void _bt_unlockbuf ( Relation  rel,
Buffer  buf 
)

Definition at line 1070 of file nbtpage.c.

1071 {
1072  /*
1073  * Buffer is pinned and locked, which means that it is expected to be
1074  * defined and addressable. Check that proactively.
1075  */
1077 
1078  /* LockBuffer() asserts that pin is held by this backend */
1080 
1081  if (!RelationUsesLocalBuffers(rel))
1083 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:157
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size)
Definition: memdebug.h:23
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27

References buf, BUFFER_LOCK_UNLOCK, BufferGetPage(), LockBuffer(), RelationUsesLocalBuffers, VALGRIND_CHECK_MEM_IS_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by _bt_drop_lock_and_maybe_pin(), _bt_endpoint(), _bt_first(), _bt_getroot(), _bt_killitems(), _bt_moveright(), _bt_pagedel(), _bt_relandgetbuf(), _bt_relbuf(), _bt_search(), _bt_set_cleanup_info(), and _bt_unlink_halfdead_page().

◆ _bt_update_posting()

void _bt_update_posting ( BTVacuumPosting  vacposting)

Definition at line 924 of file nbtdedup.c.

925 {
926  IndexTuple origtuple = vacposting->itup;
927  uint32 keysize,
928  newsize;
929  IndexTuple itup;
930  int nhtids;
931  int ui,
932  d;
933  ItemPointer htids;
934 
935  nhtids = BTreeTupleGetNPosting(origtuple) - vacposting->ndeletedtids;
936 
937  Assert(_bt_posting_valid(origtuple));
938  Assert(nhtids > 0 && nhtids < BTreeTupleGetNPosting(origtuple));
939 
940  /*
941  * Determine final size of new tuple.
942  *
943  * This calculation needs to match the code used within _bt_form_posting()
944  * for new posting list tuples. We avoid calling _bt_form_posting() here
945  * to save ourselves a second memory allocation for a htids workspace.
946  */
947  keysize = BTreeTupleGetPostingOffset(origtuple);
948  if (nhtids > 1)
949  newsize = MAXALIGN(keysize +
950  nhtids * sizeof(ItemPointerData));
951  else
952  newsize = keysize;
953 
954  Assert(newsize <= INDEX_SIZE_MASK);
955  Assert(newsize == MAXALIGN(newsize));
956 
957  /* Allocate memory using palloc0() (matches index_form_tuple()) */
958  itup = palloc0(newsize);
959  memcpy(itup, origtuple, keysize);
960  itup->t_info &= ~INDEX_SIZE_MASK;
961  itup->t_info |= newsize;
962 
963  if (nhtids > 1)
964  {
965  /* Form posting list tuple */
966  BTreeTupleSetPosting(itup, nhtids, keysize);
967  htids = BTreeTupleGetPosting(itup);
968  }
969  else
970  {
971  /* Form standard non-pivot tuple */
972  itup->t_info &= ~INDEX_ALT_TID_MASK;
973  htids = &itup->t_tid;
974  }
975 
976  ui = 0;
977  d = 0;
978  for (int i = 0; i < BTreeTupleGetNPosting(origtuple); i++)
979  {
980  if (d < vacposting->ndeletedtids && vacposting->deletetids[d] == i)
981  {
982  d++;
983  continue;
984  }
985  htids[ui++] = *BTreeTupleGetPostingN(origtuple, i);
986  }
987  Assert(ui == nhtids);
988  Assert(d == vacposting->ndeletedtids);
989  Assert(nhtids == 1 || _bt_posting_valid(itup));
990  Assert(nhtids > 1 || ItemPointerIsValid(&itup->t_tid));
991 
992  /* vacposting arg's itup will now point to updated version */
993  vacposting->itup = itup;
994 }

References Assert(), BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleGetPostingN(), BTreeTupleGetPostingOffset(), BTreeTupleSetPosting(), BTVacuumPostingData::deletetids, i, INDEX_ALT_TID_MASK, INDEX_SIZE_MASK, ItemPointerIsValid(), BTVacuumPostingData::itup, MAXALIGN, BTVacuumPostingData::ndeletedtids, palloc0(), IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_delitems_update(), and btree_xlog_updates().

◆ _bt_upgradelockbufcleanup()

void _bt_upgradelockbufcleanup ( Relation  rel,
Buffer  buf 
)

Definition at line 1109 of file nbtpage.c.

1110 {
1111  /*
1112  * Buffer is pinned and locked, which means that it is expected to be
1113  * defined and addressable. Check that proactively.
1114  */
1116 
1117  /* LockBuffer() asserts that pin is held by this backend */
1120 }
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4875

References buf, BUFFER_LOCK_UNLOCK, BufferGetPage(), LockBuffer(), LockBufferForCleanup(), and VALGRIND_CHECK_MEM_IS_DEFINED.

Referenced by btvacuumpage().

◆ _bt_upgrademetapage()

void _bt_upgrademetapage ( Page  page)

Definition at line 107 of file nbtpage.c.

108 {
109  BTMetaPageData *metad;
111 
112  metad = BTPageGetMeta(page);
113  metaopaque = BTPageGetOpaque(page);
114 
115  /* It must be really a meta page of upgradable version */
116  Assert(metaopaque->btpo_flags & BTP_META);
119 
120  /* Set version number and fill extra fields added into version 3 */
123  metad->btm_last_cleanup_num_heap_tuples = -1.0;
124  /* Only a REINDEX can set this field */
125  Assert(!metad->btm_allequalimage);
126  metad->btm_allequalimage = false;
127 
128  /* Adjust pd_lower (see _bt_initmetapage() for details) */
129  ((PageHeader) page)->pd_lower =
130  ((char *) metad + sizeof(BTMetaPageData)) - (char *) page;
131 }

References Assert(), BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_version, BTP_META, BTPageGetMeta, BTPageGetOpaque, BTREE_MIN_VERSION, BTREE_NOVAC_VERSION, and PG_USED_FOR_ASSERTS_ONLY.

Referenced by _bt_getroot(), _bt_insertonpg(), _bt_newlevel(), _bt_set_cleanup_info(), and _bt_unlink_halfdead_page().

◆ _bt_vacuum_cycleid()

BTCycleId _bt_vacuum_cycleid ( Relation  rel)

Definition at line 1993 of file nbtutils.c.

1994 {
1995  BTCycleId result = 0;
1996  int i;
1997 
1998  /* Share lock is enough since this is a read-only operation */
1999  LWLockAcquire(BtreeVacuumLock, LW_SHARED);
2000 
2001  for (i = 0; i < btvacinfo->num_vacuums; i++)
2002  {
2003  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
2004 
2005  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
2006  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
2007  {
2008  result = vac->cycleid;
2009  break;
2010  }
2011  }
2012 
2013  LWLockRelease(BtreeVacuumLock);
2014  return result;
2015 }
@ LW_SHARED
Definition: lwlock.h:117

References btvacinfo, BTOneVacInfo::cycleid, LockRelId::dbId, i, LockInfoData::lockRelId, LW_SHARED, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by _bt_split().

◆ _bt_vacuum_needs_cleanup()

bool _bt_vacuum_needs_cleanup ( Relation  rel)

Definition at line 179 of file nbtpage.c.

180 {
181  Buffer metabuf;
182  Page metapg;
183  BTMetaPageData *metad;
184  uint32 btm_version;
185  BlockNumber prev_num_delpages;
186 
187  /*
188  * Copy details from metapage to local variables quickly.
189  *
190  * Note that we deliberately avoid using cached version of metapage here.
191  */
192  metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
193  metapg = BufferGetPage(metabuf);
194  metad = BTPageGetMeta(metapg);
195  btm_version = metad->btm_version;
196 
197  if (btm_version < BTREE_NOVAC_VERSION)
198  {
199  /*
200  * Metapage needs to be dynamically upgraded to store fields that are
201  * only present when btm_version >= BTREE_NOVAC_VERSION
202  */
203  _bt_relbuf(rel, metabuf);
204  return true;
205  }
206 
207  prev_num_delpages = metad->btm_last_cleanup_num_delpages;
208  _bt_relbuf(rel, metabuf);
209 
210  /*
211  * Trigger cleanup in rare cases where prev_num_delpages exceeds 5% of the
212  * total size of the index. We can reasonably expect (though are not
213  * guaranteed) to be able to recycle this many pages if we decide to do a
214  * btvacuumscan call during the ongoing btvacuumcleanup. For further
215  * details see the nbtree/README section on placing deleted pages in the
216  * FSM.
217  */
218  if (prev_num_delpages > 0 &&
219  prev_num_delpages > RelationGetNumberOfBlocks(rel) / 20)
220  return true;
221 
222  return false;
223 }
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:229

References _bt_getbuf(), _bt_relbuf(), BT_READ, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_version, BTPageGetMeta, BTREE_METAPAGE, BTREE_NOVAC_VERSION, BufferGetPage(), and RelationGetNumberOfBlocks.

Referenced by btvacuumcleanup().

◆ btadjustmembers()

void btadjustmembers ( Oid  opfamilyoid,
Oid  opclassoid,
List operators,
List functions 
)

Definition at line 293 of file nbtvalidate.c.

297 {
298  Oid opcintype;
299  ListCell *lc;
300 
301  /*
302  * Btree operators and comparison support functions are always "loose"
303  * members of the opfamily if they are cross-type. If they are not
304  * cross-type, we prefer to tie them to the appropriate opclass ... but if
305  * the user hasn't created one, we can't do that, and must fall back to
306  * using the opfamily dependency. (We mustn't force creation of an
307  * opclass in such a case, as leaving an incomplete opclass laying about
308  * would be bad. Throwing an error is another undesirable alternative.)
309  *
310  * This behavior results in a bit of a dump/reload hazard, in that the
311  * order of restoring objects could affect what dependencies we end up
312  * with. pg_dump's existing behavior will preserve the dependency choices
313  * in most cases, but not if a cross-type operator has been bound tightly
314  * into an opclass. That's a mistake anyway, so silently "fixing" it
315  * isn't awful.
316  *
317  * Optional support functions are always "loose" family members.
318  *
319  * To avoid repeated lookups, we remember the most recently used opclass's
320  * input type.
321  */
322  if (OidIsValid(opclassoid))
323  {
324  /* During CREATE OPERATOR CLASS, need CCI to see the pg_opclass row */
326  opcintype = get_opclass_input_type(opclassoid);
327  }
328  else
329  opcintype = InvalidOid;
330 
331  /*
332  * We handle operators and support functions almost identically, so rather
333  * than duplicate this code block, just join the lists.
334  */
335  foreach(lc, list_concat_copy(operators, functions))
336  {
337  OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
338 
339  if (op->is_func && op->number != BTORDER_PROC)
340  {
341  /* Optional support proc, so always a soft family dependency */
342  op->ref_is_hard = false;
343  op->ref_is_family = true;
344  op->refobjid = opfamilyoid;
345  }
346  else if (op->lefttype != op->righttype)
347  {
348  /* Cross-type, so always a soft family dependency */
349  op->ref_is_hard = false;
350  op->ref_is_family = true;
351  op->refobjid = opfamilyoid;
352  }
353  else
354  {
355  /* Not cross-type; is there a suitable opclass? */
356  if (op->lefttype != opcintype)
357  {
358  /* Avoid repeating this expensive lookup, even if it fails */
359  opcintype = op->lefttype;
360  opclassoid = opclass_for_family_datatype(BTREE_AM_OID,
361  opfamilyoid,
362  opcintype);
363  }
364  if (OidIsValid(opclassoid))
365  {
366  /* Hard dependency on opclass */
367  op->ref_is_hard = true;
368  op->ref_is_family = false;
369  op->refobjid = opclassoid;
370  }
371  else
372  {
373  /* We're stuck, so make a soft dependency on the opfamily */
374  op->ref_is_hard = false;
375  op->ref_is_family = true;
376  op->refobjid = opfamilyoid;
377  }
378  }
379  }
380 }
Oid opclass_for_family_datatype(Oid amoid, Oid opfamilyoid, Oid datatypeoid)
Definition: amvalidate.c:236
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1190
#define lfirst(lc)
Definition: pg_list.h:172
static const struct fns functions
Definition: regcomp.c:356
Oid refobjid
Definition: amapi.h:90
Oid lefttype
Definition: amapi.h:85
bool ref_is_family
Definition: amapi.h:89
Oid righttype
Definition: amapi.h:86
int number
Definition: amapi.h:84
bool is_func
Definition: amapi.h:82
bool ref_is_hard
Definition: amapi.h:88
void CommandCounterIncrement(void)
Definition: xact.c:1079

References BTORDER_PROC, CommandCounterIncrement(), functions, get_opclass_input_type(), InvalidOid, OpFamilyMember::is_func, OpFamilyMember::lefttype, lfirst, list_concat_copy(), OpFamilyMember::number, OidIsValid, opclass_for_family_datatype(), OpFamilyMember::ref_is_family, OpFamilyMember::ref_is_hard, OpFamilyMember::refobjid, and OpFamilyMember::righttype.

Referenced by bthandler().

◆ btbeginscan()

IndexScanDesc btbeginscan ( Relation  rel,
int  nkeys,
int  norderbys 
)

Definition at line 331 of file nbtree.c.

332 {
333  IndexScanDesc scan;
334  BTScanOpaque so;
335 
336  /* no order by operators allowed */
337  Assert(norderbys == 0);
338 
339  /* get the scan */
340  scan = RelationGetIndexScan(rel, nkeys, norderbys);
341 
342  /* allocate private workspace */
343  so = (BTScanOpaque) palloc(sizeof(BTScanOpaqueData));
346  if (scan->numberOfKeys > 0)
347  so->keyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
348  else
349  so->keyData = NULL;
350 
351  so->arrayKeyData = NULL; /* assume no array keys for now */
352  so->arraysStarted = false;
353  so->numArrayKeys = 0;
354  so->arrayKeys = NULL;
355  so->arrayContext = NULL;
356 
357  so->killedItems = NULL; /* until needed */
358  so->numKilled = 0;
359 
360  /*
361  * We don't know yet whether the scan will be index-only, so we do not
362  * allocate the tuple workspace arrays until btrescan. However, we set up
363  * scan->xs_itupdesc whether we'll need it or not, since that's so cheap.
364  */
365  so->currTuples = so->markTuples = NULL;
366 
367  scan->xs_itupdesc = RelationGetDescr(rel);
368 
369  scan->opaque = so;
370 
371  return scan;
372 }
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
char * markTuples
Definition: nbtree.h:1060
BTScanPosData markPos
Definition: nbtree.h:1073
struct TupleDescData * xs_itupdesc
Definition: relscan.h:143

References BTScanOpaqueData::arrayContext, BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, BTScanOpaqueData::arraysStarted, Assert(), BTScanPosInvalidate, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, BTScanOpaqueData::keyData, BTScanOpaqueData::killedItems, BTScanOpaqueData::markPos, BTScanOpaqueData::markTuples, BTScanOpaqueData::numArrayKeys, IndexScanDescData::numberOfKeys, BTScanOpaqueData::numKilled, IndexScanDescData::opaque, palloc(), RelationGetDescr, RelationGetIndexScan(), and IndexScanDescData::xs_itupdesc.

Referenced by bthandler().

◆ btbuild()

IndexBuildResult* btbuild ( Relation  heap,
Relation  index,
struct IndexInfo indexInfo 
)

Definition at line 293 of file nbtsort.c.

294 {
295  IndexBuildResult *result;
296  BTBuildState buildstate;
297  double reltuples;
298 
299 #ifdef BTREE_BUILD_STATS
301  ResetUsage();
302 #endif /* BTREE_BUILD_STATS */
303 
304  buildstate.isunique = indexInfo->ii_Unique;
305  buildstate.nulls_not_distinct = indexInfo->ii_NullsNotDistinct;
306  buildstate.havedead = false;
307  buildstate.heap = heap;
308  buildstate.spool = NULL;
309  buildstate.spool2 = NULL;
310  buildstate.indtuples = 0;
311  buildstate.btleader = NULL;
312 
313  /*
314  * We expect to be called exactly once for any index relation. If that's
315  * not the case, big trouble's what we have.
316  */
318  elog(ERROR, "index \"%s\" already contains data",
320 
321  reltuples = _bt_spools_heapscan(heap, index, &buildstate, indexInfo);
322 
323  /*
324  * Finish the build by (1) completing the sort of the spool file, (2)
325  * inserting the sorted tuples into btree pages and (3) building the upper
326  * levels. Finally, it may also be necessary to end use of parallelism.
327  */
328  _bt_leafbuild(buildstate.spool, buildstate.spool2);
329  _bt_spooldestroy(buildstate.spool);
330  if (buildstate.spool2)
331  _bt_spooldestroy(buildstate.spool2);
332  if (buildstate.btleader)
333  _bt_end_parallel(buildstate.btleader);
334 
335  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
336 
337  result->heap_tuples = reltuples;
338  result->index_tuples = buildstate.indtuples;
339 
340 #ifdef BTREE_BUILD_STATS
342  {
343  ShowUsage("BTREE BUILD STATS");
344  ResetUsage();
345  }
346 #endif /* BTREE_BUILD_STATS */
347 
348  return result;
349 }
static void _bt_end_parallel(BTLeader *btleader)
Definition: nbtsort.c:1607
static void _bt_leafbuild(BTSpool *btspool, BTSpool *btspool2)
Definition: nbtsort.c:536
static double _bt_spools_heapscan(Relation heap, Relation index, BTBuildState *buildstate, IndexInfo *indexInfo)
Definition: nbtsort.c:363
static void _bt_spooldestroy(BTSpool *btspool)
Definition: nbtsort.c:515
bool isunique
Definition: nbtsort.c:204
BTSpool * spool
Definition: nbtsort.c:208
BTLeader * btleader
Definition: nbtsort.c:222
bool nulls_not_distinct
Definition: nbtsort.c:205
bool havedead
Definition: nbtsort.c:206
Relation heap
Definition: nbtsort.c:207
BTSpool * spool2
Definition: nbtsort.c:214
double indtuples
Definition: nbtsort.c:215
double heap_tuples
Definition: genam.h:32
double index_tuples
Definition: genam.h:33
bool ii_Unique
Definition: execnodes.h:197
bool ii_NullsNotDistinct
Definition: execnodes.h:198
Definition: type.h:95

References _bt_end_parallel(), _bt_leafbuild(), _bt_spooldestroy(), _bt_spools_heapscan(), BTBuildState::btleader, elog, ERROR, BTBuildState::havedead, BTBuildState::heap, IndexBuildResult::heap_tuples, IndexInfo::ii_NullsNotDistinct, IndexInfo::ii_Unique, IndexBuildResult::index_tuples, BTBuildState::indtuples, BTBuildState::isunique, log_btree_build_stats, BTBuildState::nulls_not_distinct, palloc(), RelationGetNumberOfBlocks, RelationGetRelationName, ResetUsage(), ShowUsage(), BTBuildState::spool, and BTBuildState::spool2.

Referenced by bthandler().

◆ btbuildempty()

void btbuildempty ( Relation  index)

Definition at line 152 of file nbtree.c.

153 {
154  bool allequalimage = _bt_allequalimage(index, false);
155  BulkWriteState *bulkstate;
156  BulkWriteBuffer metabuf;
157 
158  bulkstate = smgr_bulk_start_rel(index, INIT_FORKNUM);
159 
160  /* Construct metapage. */
161  metabuf = smgr_bulk_get_buf(bulkstate);
162  _bt_initmetapage((Page) metabuf, P_NONE, 0, allequalimage);
163  smgr_bulk_write(bulkstate, BTREE_METAPAGE, metabuf, true);
164 
165  smgr_bulk_finish(bulkstate);
166 }
void smgr_bulk_write(BulkWriteState *bulkstate, BlockNumber blocknum, BulkWriteBuffer buf, bool page_std)
Definition: bulk_write.c:271
BulkWriteBuffer smgr_bulk_get_buf(BulkWriteState *bulkstate)
Definition: bulk_write.c:295
void smgr_bulk_finish(BulkWriteState *bulkstate)
Definition: bulk_write.c:129
BulkWriteState * smgr_bulk_start_rel(Relation rel, ForkNumber forknum)
Definition: bulk_write.c:86
void _bt_initmetapage(Page page, BlockNumber rootbknum, uint32 level, bool allequalimage)
Definition: nbtpage.c:67
bool _bt_allequalimage(Relation rel, bool debugmessage)
Definition: nbtutils.c:2740
@ INIT_FORKNUM
Definition: relpath.h:53

References _bt_allequalimage(), _bt_initmetapage(), BTREE_METAPAGE, INIT_FORKNUM, P_NONE, smgr_bulk_finish(), smgr_bulk_get_buf(), smgr_bulk_start_rel(), and smgr_bulk_write().

Referenced by bthandler().

◆ btbuildphasename()

char* btbuildphasename ( int64  phasenum)

Definition at line 2208 of file nbtutils.c.

2209 {
2210  switch (phasenum)
2211  {
2213  return "initializing";
2215  return "scanning table";
2217  return "sorting live tuples";
2219  return "sorting dead tuples";
2221  return "loading tuples in tree";
2222  default:
2223  return NULL;
2224  }
2225 }
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2
Definition: nbtree.h:1118
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
Definition: nbtree.h:1119
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN
Definition: nbtree.h:1116
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1
Definition: nbtree.h:1117
#define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE
Definition: progress.h:106

References PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN, PROGRESS_BTREE_PHASE_LEAF_LOAD, PROGRESS_BTREE_PHASE_PERFORMSORT_1, PROGRESS_BTREE_PHASE_PERFORMSORT_2, and PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE.

Referenced by bthandler().

◆ btbulkdelete()

IndexBulkDeleteResult* btbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 774 of file nbtree.c.

776 {
777  Relation rel = info->index;
778  BTCycleId cycleid;
779 
780  /* allocate stats if first time through, else re-use existing struct */
781  if (stats == NULL)
783 
784  /* Establish the vacuum cycle ID to use for this scan */
785  /* The ENSURE stuff ensures we clean up shared memory on failure */
787  {
788  cycleid = _bt_start_vacuum(rel);
789 
790  btvacuumscan(info, stats, callback, callback_state, cycleid);
791  }
793  _bt_end_vacuum(rel);
794 
795  return stats;
796 }
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
static void btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state, BTCycleId cycleid)
Definition: nbtree.c:892
void _bt_end_vacuum_callback(int code, Datum arg)
Definition: nbtutils.c:2112
BTCycleId _bt_start_vacuum(Relation rel)
Definition: nbtutils.c:2027
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Relation index
Definition: genam.h:46
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References _bt_end_vacuum(), _bt_end_vacuum_callback(), _bt_start_vacuum(), btvacuumscan(), callback(), IndexVacuumInfo::index, palloc0(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, and PointerGetDatum().

Referenced by bthandler().

◆ btcanreturn()

bool btcanreturn ( Relation  index,
int  attno 
)

Definition at line 1410 of file nbtree.c.

1411 {
1412  return true;
1413 }

Referenced by bthandler().

◆ btendscan()

void btendscan ( IndexScanDesc  scan)

Definition at line 437 of file nbtree.c.

438 {
439  BTScanOpaque so = (BTScanOpaque) scan->opaque;
440 
441  /* we aren't holding any read locks, but gotta drop the pins */
443  {
444  /* Before leaving current page, deal with any killed items */
445  if (so->numKilled > 0)
446  _bt_killitems(scan);
448  }
449 
450  so->markItemIndex = -1;
452 
453  /* No need to invalidate positions, the RAM is about to be freed. */
454 
455  /* Release storage */
456  if (so->keyData != NULL)
457  pfree(so->keyData);
458  /* so->arrayKeyData and so->arrayKeys are in arrayContext */
459  if (so->arrayContext != NULL)
461  if (so->killedItems != NULL)
462  pfree(so->killedItems);
463  if (so->currTuples != NULL)
464  pfree(so->currTuples);
465  /* so->markTuples should not be pfree'd, see btrescan */
466  pfree(so);
467 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:442
#define BTScanPosUnpinIfPinned(scanpos)
Definition: nbtree.h:1001
void _bt_killitems(IndexScanDesc scan)
Definition: nbtutils.c:1776

References _bt_killitems(), BTScanOpaqueData::arrayContext, BTScanPosIsValid, BTScanPosUnpinIfPinned, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, if(), BTScanOpaqueData::keyData, BTScanOpaqueData::killedItems, BTScanOpaqueData::markItemIndex, BTScanOpaqueData::markPos, MemoryContextDelete(), BTScanOpaqueData::numKilled, IndexScanDescData::opaque, and pfree().

Referenced by bthandler().

◆ btestimateparallelscan()

Size btestimateparallelscan ( void  )

Definition at line 559 of file nbtree.c.

560 {
561  return sizeof(BTParallelScanDescData);
562 }
struct BTParallelScanDescData BTParallelScanDescData

Referenced by bthandler().

◆ btgetbitmap()

int64 btgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 273 of file nbtree.c.

274 {
275  BTScanOpaque so = (BTScanOpaque) scan->opaque;
276  int64 ntids = 0;
277  ItemPointer heapTid;
278 
279  /*
280  * If we have any array keys, initialize them.
281  */
282  if (so->numArrayKeys)
283  {
284  /* punt if we have any unsatisfiable array keys */
285  if (so->numArrayKeys < 0)
286  return ntids;
287 
289  }
290 
291  /* This loop handles advancing to the next array elements, if any */
292  do
293  {
294  /* Fetch the first page & tuple */
295  if (_bt_first(scan, ForwardScanDirection))
296  {
297  /* Save tuple ID, and continue scanning */
298  heapTid = &scan->xs_heaptid;
299  tbm_add_tuples(tbm, heapTid, 1, false);
300  ntids++;
301 
302  for (;;)
303  {
304  /*
305  * Advance to next tuple within page. This is the same as the
306  * easy case in _bt_next().
307  */
308  if (++so->currPos.itemIndex > so->currPos.lastItem)
309  {
310  /* let _bt_next do the heavy lifting */
311  if (!_bt_next(scan, ForwardScanDirection))
312  break;
313  }
314 
315  /* Save tuple ID, and continue scanning */
316  heapTid = &so->currPos.items[so->currPos.itemIndex].heapTid;
317  tbm_add_tuples(tbm, heapTid, 1, false);
318  ntids++;
319  }
320  }
321  /* Now see if we have more array keys to deal with */
323 
324  return ntids;
325 }
bool _bt_first(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:876
bool _bt_next(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:1459
bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
Definition: nbtutils.c:544
void _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
Definition: nbtutils.c:516
@ ForwardScanDirection
Definition: sdir.h:28
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:377

References _bt_advance_array_keys(), _bt_first(), _bt_next(), _bt_start_array_keys(), BTScanOpaqueData::currPos, ForwardScanDirection, BTScanPosItem::heapTid, if(), BTScanPosData::itemIndex, BTScanPosData::items, BTScanPosData::lastItem, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, tbm_add_tuples(), and IndexScanDescData::xs_heaptid.

Referenced by bthandler().

◆ btgettuple()

bool btgettuple ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 199 of file nbtree.c.

200 {
201  BTScanOpaque so = (BTScanOpaque) scan->opaque;
202  bool res;
203 
204  /* btree indexes are never lossy */
205  scan->xs_recheck = false;
206 
207  /*
208  * If we have any array keys, initialize them during first call for a
209  * scan. We can't do this in btrescan because we don't know the scan
210  * direction at that time.
211  */
212  if (so->numArrayKeys && !BTScanPosIsValid(so->currPos))
213  {
214  /* punt if we have any unsatisfiable array keys */
215  if (so->numArrayKeys < 0)
216  return false;
217 
218  _bt_start_array_keys(scan, dir);
219  }
220 
221  /* This loop handles advancing to the next array elements, if any */
222  do
223  {
224  /*
225  * If we've already initialized this scan, we can just advance it in
226  * the appropriate direction. If we haven't done so yet, we call
227  * _bt_first() to get the first item in the scan.
228  */
229  if (!BTScanPosIsValid(so->currPos))
230  res = _bt_first(scan, dir);
231  else
232  {
233  /*
234  * Check to see if we should kill the previously-fetched tuple.
235  */
236  if (scan->kill_prior_tuple)
237  {
238  /*
239  * Yes, remember it for later. (We'll deal with all such
240  * tuples at once right before leaving the index page.) The
241  * test for numKilled overrun is not just paranoia: if the
242  * caller reverses direction in the indexscan then the same
243  * item might get entered multiple times. It's not worth
244  * trying to optimize that, so we don't detect it, but instead
245  * just forget any excess entries.
246  */
247  if (so->killedItems == NULL)
248  so->killedItems = (int *)
249  palloc(MaxTIDsPerBTreePage * sizeof(int));
250  if (so->numKilled < MaxTIDsPerBTreePage)
251  so->killedItems[so->numKilled++] = so->currPos.itemIndex;
252  }
253 
254  /*
255  * Now continue the scan.
256  */
257  res = _bt_next(scan, dir);
258  }
259 
260  /* If we have a tuple, return it ... */
261  if (res)
262  break;
263  /* ... otherwise see if we have more array keys to deal with */
264  } while (so->numArrayKeys && _bt_advance_array_keys(scan, dir));
265 
266  return res;
267 }
bool kill_prior_tuple
Definition: relscan.h:128

References _bt_advance_array_keys(), _bt_first(), _bt_next(), _bt_start_array_keys(), BTScanPosIsValid, BTScanOpaqueData::currPos, if(), BTScanPosData::itemIndex, IndexScanDescData::kill_prior_tuple, BTScanOpaqueData::killedItems, MaxTIDsPerBTreePage, BTScanOpaqueData::numArrayKeys, BTScanOpaqueData::numKilled, IndexScanDescData::opaque, palloc(), res, and IndexScanDescData::xs_recheck.

Referenced by bthandler().

◆ btinitparallelscan()

void btinitparallelscan ( void *  target)

◆ btinsert()

bool btinsert ( Relation  rel,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
struct IndexInfo indexInfo 
)

Definition at line 175 of file nbtree.c.

180 {
181  bool result;
182  IndexTuple itup;
183 
184  /* generate an index tuple */
185  itup = index_form_tuple(RelationGetDescr(rel), values, isnull);
186  itup->t_tid = *ht_ctid;
187 
188  result = _bt_doinsert(rel, itup, checkUnique, indexUnchanged, heapRel);
189 
190  pfree(itup);
191 
192  return result;
193 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: indextuple.c:44
bool _bt_doinsert(Relation rel, IndexTuple itup, IndexUniqueCheck checkUnique, bool indexUnchanged, Relation heapRel)
Definition: nbtinsert.c:102

References _bt_doinsert(), index_form_tuple(), pfree(), RelationGetDescr, IndexTupleData::t_tid, and values.

Referenced by bthandler().

◆ btmarkpos()

void btmarkpos ( IndexScanDesc  scan)

Definition at line 473 of file nbtree.c.

474 {
475  BTScanOpaque so = (BTScanOpaque) scan->opaque;
476 
477  /* There may be an old mark with a pin (but no lock). */
479 
480  /*
481  * Just record the current itemIndex. If we later step to next page
482  * before releasing the marked position, _bt_steppage makes a full copy of
483  * the currPos struct in markPos. If (as often happens) the mark is moved
484  * before we leave the page, we don't have to do that work.
485  */
486  if (BTScanPosIsValid(so->currPos))
487  so->markItemIndex = so->currPos.itemIndex;
488  else
489  {
491  so->markItemIndex = -1;
492  }
493 
494  /* Also record the current positions of any array keys */
495  if (so->numArrayKeys)
496  _bt_mark_array_keys(scan);
497 }
void _bt_mark_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:611

References BTScanPosInvalidate, BTScanPosIsValid, BTScanPosUnpinIfPinned, BTScanOpaqueData::currPos, BTScanPosData::itemIndex, BTScanOpaqueData::markItemIndex, BTScanOpaqueData::markPos, and IndexScanDescData::opaque.

Referenced by bthandler().

◆ btoptions()

bytea* btoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 2162 of file nbtutils.c.

2163 {
2164  static const relopt_parse_elt tab[] = {
2165  {"fillfactor", RELOPT_TYPE_INT, offsetof(BTOptions, fillfactor)},
2166  {"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
2167  offsetof(BTOptions, vacuum_cleanup_index_scale_factor)},
2168  {"deduplicate_items", RELOPT_TYPE_BOOL,
2169  offsetof(BTOptions, deduplicate_items)}
2170  };
2171 
2172  return (bytea *) build_reloptions(reloptions, validate,
2174  sizeof(BTOptions),
2175  tab, lengthof(tab));
2176 }
#define lengthof(array)
Definition: c.h:775
int fillfactor
Definition: pgbench.c:187
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1908
@ RELOPT_KIND_BTREE
Definition: reloptions.h:44
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
@ RELOPT_TYPE_REAL
Definition: reloptions.h:33
Definition: c.h:674

References build_reloptions(), fillfactor, lengthof, RELOPT_KIND_BTREE, RELOPT_TYPE_BOOL, RELOPT_TYPE_INT, and RELOPT_TYPE_REAL.

Referenced by bthandler().

◆ BTPageGetDeleteXid()

static FullTransactionId BTPageGetDeleteXid ( Page  page)
inlinestatic

Definition at line 260 of file nbtree.h.

261 {
262  BTPageOpaque opaque;
263  BTDeletedPageData *contents;
264 
265  /* We only expect to be called with a deleted page */
266  Assert(!PageIsNew(page));
267  opaque = BTPageGetOpaque(page);
268  Assert(P_ISDELETED(opaque));
269 
270  /* pg_upgrade'd deleted page -- must be safe to delete now */
271  if (!P_HAS_FULLXID(opaque))
273 
274  /* Get safexid from deleted page */
275  contents = ((BTDeletedPageData *) PageGetContents(page));
276  return contents->safexid;
277 }
static char * PageGetContents(Page page)
Definition: bufpage.h:254
#define P_HAS_FULLXID(opaque)
Definition: nbtree.h:228
FullTransactionId safexid
Definition: nbtree.h:235
#define FirstNormalFullTransactionId
Definition: transam.h:57

References Assert(), BTPageGetOpaque, FirstNormalFullTransactionId, P_HAS_FULLXID, P_ISDELETED, PageGetContents(), PageIsNew(), and BTDeletedPageData::safexid.

Referenced by _bt_allocbuf(), BTPageIsRecyclable(), and GetBTPageStatistics().

◆ BTPageIsRecyclable()

static bool BTPageIsRecyclable ( Page  page,
Relation  heaprel 
)
inlinestatic

Definition at line 291 of file nbtree.h.

292 {
293  BTPageOpaque opaque;
294 
295  Assert(!PageIsNew(page));
296  Assert(heaprel != NULL);
297 
298  /* Recycling okay iff page is deleted and safexid is old enough */
299  opaque = BTPageGetOpaque(page);
300  if (P_ISDELETED(opaque))
301  {
302  FullTransactionId safexid = BTPageGetDeleteXid(page);
303 
304  /*
305  * The page was deleted, but when? If it was just deleted, a scan
306  * might have seen the downlink to it, and will read the page later.
307  * As long as that can happen, we must keep the deleted page around as
308  * a tombstone.
309  *
310  * For that check if the deletion XID could still be visible to
311  * anyone. If not, then no scan that's still in progress could have
312  * seen its downlink, and we can recycle it.
313  */
314  return GlobalVisCheckRemovableFullXid(heaprel, safexid);
315  }
316 
317  return false;
318 }

References Assert(), BTPageGetDeleteXid(), BTPageGetOpaque, GlobalVisCheckRemovableFullXid(), P_ISDELETED, and PageIsNew().

Referenced by _bt_allocbuf(), and btvacuumpage().

◆ BTPageSetDeleted()

static void BTPageSetDeleted ( Page  page,
FullTransactionId  safexid 
)
inlinestatic

Definition at line 239 of file nbtree.h.

240 {
241  BTPageOpaque opaque;
242  PageHeader header;
243  BTDeletedPageData *contents;
244 
245  opaque = BTPageGetOpaque(page);
246  header = ((PageHeader) page);
247 
248  opaque->btpo_flags &= ~BTP_HALF_DEAD;
251  sizeof(BTDeletedPageData);
252  header->pd_upper = header->pd_special;
253 
254  /* Set safexid in deleted page */
255  contents = ((BTDeletedPageData *) PageGetContents(page));
256  contents->safexid = safexid;
257 }
#define BTP_HAS_FULLXID
Definition: nbtree.h:84
#define BTP_HALF_DEAD
Definition: nbtree.h:80
struct BTDeletedPageData BTDeletedPageData
#define BTP_DELETED
Definition: nbtree.h:78
LocationIndex pd_special
Definition: bufpage.h:164
LocationIndex pd_upper
Definition: bufpage.h:163
LocationIndex pd_lower
Definition: bufpage.h:162

References BTP_DELETED, BTP_HALF_DEAD, BTP_HAS_FULLXID, BTPageGetOpaque, BTPageOpaqueData::btpo_flags, MAXALIGN, PageGetContents(), PageHeaderData::pd_lower, PageHeaderData::pd_special, PageHeaderData::pd_upper, BTDeletedPageData::safexid, and SizeOfPageHeaderData.

Referenced by _bt_unlink_halfdead_page(), and btree_xlog_unlink_page().

◆ btparallelrescan()

void btparallelrescan ( IndexScanDesc  scan)

Definition at line 583 of file nbtree.c.

584 {
585  BTParallelScanDesc btscan;
586  ParallelIndexScanDesc parallel_scan = scan->parallel_scan;
587 
588  Assert(parallel_scan);
589 
590  btscan = (BTParallelScanDesc) OffsetToPointer((void *) parallel_scan,
591  parallel_scan->ps_offset);
592 
593  /*
594  * In theory, we don't need to acquire the spinlock here, because there
595  * shouldn't be any other workers running at this point, but we do so for
596  * consistency.
597  */
598  SpinLockAcquire(&btscan->btps_mutex);
601  btscan->btps_arrayKeyCount = 0;
602  SpinLockRelease(&btscan->btps_mutex);
603 }

References Assert(), BTPARALLEL_NOT_INITIALIZED, BTParallelScanDescData::btps_arrayKeyCount, BTParallelScanDescData::btps_mutex, BTParallelScanDescData::btps_pageStatus, BTParallelScanDescData::btps_scanPage, InvalidBlockNumber, OffsetToPointer, IndexScanDescData::parallel_scan, ParallelIndexScanDescData::ps_offset, SpinLockAcquire, and SpinLockRelease.

Referenced by bthandler().

◆ btproperty()

bool btproperty ( Oid  index_oid,
int  attno,
IndexAMProperty  prop,
const char *  propname,
bool res,
bool isnull 
)

Definition at line 2185 of file nbtutils.c.

2188 {
2189  switch (prop)
2190  {
2191  case AMPROP_RETURNABLE:
2192  /* answer only for columns, not AM or whole index */
2193  if (attno == 0)
2194  return false;
2195  /* otherwise, btree can always return data */
2196  *res = true;
2197  return true;
2198 
2199  default:
2200  return false; /* punt to generic code */
2201  }
2202 }
@ AMPROP_RETURNABLE
Definition: amapi.h:43

References AMPROP_RETURNABLE, and res.

Referenced by bthandler().

◆ BTreeShmemInit()

void BTreeShmemInit ( void  )

Definition at line 2134 of file nbtutils.c.

2135 {
2136  bool found;
2137 
2138  btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
2139  BTreeShmemSize(),
2140  &found);
2141 
2142  if (!IsUnderPostmaster)
2143  {
2144  /* Initialize shared memory area */
2145  Assert(!found);
2146 
2147  /*
2148  * It doesn't really matter what the cycle counter starts at, but
2149  * having it always start the same doesn't seem good. Seed with
2150  * low-order bits of time() instead.
2151  */
2152  btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
2153 
2154  btvacinfo->num_vacuums = 0;
2156  }
2157  else
2158  Assert(found);
2159 }
bool IsUnderPostmaster
Definition: globals.c:117
int MaxBackends
Definition: globals.c:143
Size BTreeShmemSize(void)
Definition: nbtutils.c:2121
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387

References Assert(), BTreeShmemSize(), btvacinfo, BTVacInfo::cycle_ctr, IsUnderPostmaster, BTVacInfo::max_vacuums, MaxBackends, BTVacInfo::num_vacuums, and ShmemInitStruct().

Referenced by CreateOrAttachShmemStructs().

◆ BTreeShmemSize()

Size BTreeShmemSize ( void  )

Definition at line 2121 of file nbtutils.c.

2122 {
2123  Size size;
2124 
2125  size = offsetof(BTVacInfo, vacuums);
2127  return size;
2128 }
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510

References add_size(), MaxBackends, mul_size(), and size.

Referenced by BTreeShmemInit(), and CalculateShmemSize().

◆ BTreeTupleGetDownLink()

static BlockNumber BTreeTupleGetDownLink ( IndexTuple  pivot)
inlinestatic

◆ BTreeTupleGetHeapTID()

static ItemPointer BTreeTupleGetHeapTID ( IndexTuple  itup)
inlinestatic

Definition at line 638 of file nbtree.h.

639 {
640  if (BTreeTupleIsPivot(itup))
641  {
642  /* Pivot tuple heap TID representation? */
645  return (ItemPointer) ((char *) itup + IndexTupleSize(itup) -
646  sizeof(ItemPointerData));
647 
648  /* Heap TID attribute was truncated */
649  return NULL;
650  }
651  else if (BTreeTupleIsPosting(itup))
652  return BTreeTupleGetPosting(itup);
653 
654  return &itup->t_tid;
655 }
struct ItemPointerData ItemPointerData

References BT_PIVOT_HEAP_TID_ATTR, BTreeTupleGetPosting(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), IndexTupleSize, ItemPointerGetOffsetNumberNoCheck(), and IndexTupleData::t_tid.

Referenced by _bt_bottomupdel_finish_pending(), _bt_check_natts(), _bt_check_third_page(), _bt_compare(), _bt_delitems_delete_check(), _bt_mkscankey(), _bt_swap_posting(), _bt_truncate(), bt_entry_unique_check(), bt_page_print_tuples(), bt_target_page_check(), BTreeTupleGetHeapTIDCareful(), and BTreeTupleGetPointsToTID().

◆ BTreeTupleGetMaxHeapTID()

static ItemPointer BTreeTupleGetMaxHeapTID ( IndexTuple  itup)
inlinestatic

Definition at line 664 of file nbtree.h.

665 {
666  Assert(!BTreeTupleIsPivot(itup));
667 
668  if (BTreeTupleIsPosting(itup))
669  {
670  uint16 nposting = BTreeTupleGetNPosting(itup);
671 
672  return BTreeTupleGetPostingN(itup, nposting - 1);
673  }
674 
675  return &itup->t_tid;
676 }

References Assert(), BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), and IndexTupleData::t_tid.

Referenced by _bt_bottomupdel_finish_pending(), _bt_compare(), _bt_delitems_delete_check(), _bt_swap_posting(), _bt_truncate(), and bt_target_page_check().

◆ BTreeTupleGetNPosting()

◆ BTreeTupleGetPosting()

static ItemPointer BTreeTupleGetPosting ( IndexTuple  posting)
inlinestatic

◆ BTreeTupleGetPostingN()

◆ BTreeTupleGetPostingOffset()

◆ BTreeTupleGetTopParent()

static BlockNumber BTreeTupleGetTopParent ( IndexTuple  leafhikey)
inlinestatic

Definition at line 620 of file nbtree.h.

621 {
622  return ItemPointerGetBlockNumberNoCheck(&leafhikey->t_tid);
623 }

References ItemPointerGetBlockNumberNoCheck(), and IndexTupleData::t_tid.

Referenced by _bt_unlink_halfdead_page(), and bt_downlink_missing_check().

◆ BTreeTupleIsPivot()

◆ BTreeTupleIsPosting()

◆ BTreeTupleSetDownLink()

static void BTreeTupleSetDownLink ( IndexTuple  pivot,
BlockNumber  blkno 
)
inlinestatic

Definition at line 562 of file nbtree.h.

563 {
564  ItemPointerSetBlockNumber(&pivot->t_tid, blkno);
565 }
static void ItemPointerSetBlockNumber(ItemPointerData *pointer, BlockNumber blockNumber)
Definition: itemptr.h:147

References ItemPointerSetBlockNumber(), and IndexTupleData::t_tid.

Referenced by _bt_buildadd(), _bt_insert_parent(), _bt_mark_page_halfdead(), _bt_newlevel(), _bt_uppershutdown(), and btree_xlog_mark_page_halfdead().

◆ BTreeTupleSetNAtts()

static void BTreeTupleSetNAtts ( IndexTuple  itup,
uint16  nkeyatts,
bool  heaptid 
)
inlinestatic

Definition at line 595 of file nbtree.h.

596 {
597  Assert(nkeyatts <= INDEX_MAX_KEYS);
598  Assert((nkeyatts & BT_STATUS_OFFSET_MASK) == 0);
599  Assert(!heaptid || nkeyatts > 0);
600  Assert(!BTreeTupleIsPivot(itup) || nkeyatts == 0);
601 
602  itup->t_info |= INDEX_ALT_TID_MASK;
603 
604  if (heaptid)
605  nkeyatts |= BT_PIVOT_HEAP_TID_ATTR;
606 
607  /* BT_IS_POSTING bit is deliberately unset here */
608  ItemPointerSetOffsetNumber(&itup->t_tid, nkeyatts);
609  Assert(BTreeTupleIsPivot(itup));
610 }
#define BT_STATUS_OFFSET_MASK
Definition: nbtree.h:463

References Assert(), BT_PIVOT_HEAP_TID_ATTR, BT_STATUS_OFFSET_MASK, BTreeTupleIsPivot(), INDEX_ALT_TID_MASK, INDEX_MAX_KEYS, ItemPointerSetOffsetNumber(), IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_buildadd(), _bt_newlevel(), _bt_pgaddtup(), _bt_sortaddtup(), _bt_truncate(), and BTreeTupleSetTopParent().

◆ BTreeTupleSetPosting()

static void BTreeTupleSetPosting ( IndexTuple  itup,
uint16  nhtids,
int  postingoffset 
)
inlinestatic

Definition at line 504 of file nbtree.h.

505 {
506  Assert(nhtids > 1);
507  Assert((nhtids & BT_STATUS_OFFSET_MASK) == 0);
508  Assert((size_t) postingoffset == MAXALIGN(postingoffset));
509  Assert(postingoffset < INDEX_SIZE_MASK);
510  Assert(!BTreeTupleIsPivot(itup));
511 
512  itup->t_info |= INDEX_ALT_TID_MASK;
513  ItemPointerSetOffsetNumber(&itup->t_tid, (nhtids | BT_IS_POSTING));
514  ItemPointerSetBlockNumber(&itup->t_tid, postingoffset);
515 }

References Assert(), BT_IS_POSTING, BT_STATUS_OFFSET_MASK, BTreeTupleIsPivot(), INDEX_ALT_TID_MASK, INDEX_SIZE_MASK, ItemPointerSetBlockNumber(), ItemPointerSetOffsetNumber(), MAXALIGN, IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_form_posting(), and _bt_update_posting().

◆ BTreeTupleSetTopParent()

static void BTreeTupleSetTopParent ( IndexTuple  leafhikey,
BlockNumber  blkno 
)
inlinestatic

◆ btrescan()

void btrescan ( IndexScanDesc  scan,
ScanKey  scankey,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 378 of file nbtree.c.

380 {
381  BTScanOpaque so = (BTScanOpaque) scan->opaque;
382 
383  /* we aren't holding any read locks, but gotta drop the pins */
385  {
386  /* Before leaving current page, deal with any killed items */
387  if (so->numKilled > 0)
388  _bt_killitems(scan);
391  }
392 
393  so->markItemIndex = -1;
394  so->arrayKeyCount = 0;
397 
398  /*
399  * Allocate tuple workspace arrays, if needed for an index-only scan and
400  * not already done in a previous rescan call. To save on palloc
401  * overhead, both workspaces are allocated as one palloc block; only this
402  * function and btendscan know that.
403  *
404  * NOTE: this data structure also makes it safe to return data from a
405  * "name" column, even though btree name_ops uses an underlying storage
406  * datatype of cstring. The risk there is that "name" is supposed to be
407  * padded to NAMEDATALEN, but the actual index tuple is probably shorter.
408  * However, since we only return data out of tuples sitting in the
409  * currTuples array, a fetch of NAMEDATALEN bytes can at worst pull some
410  * data out of the markTuples array --- running off the end of memory for
411  * a SIGSEGV is not possible. Yeah, this is ugly as sin, but it beats
412  * adding special-case treatment for name_ops elsewhere.
413  */
414  if (scan->xs_want_itup && so->currTuples == NULL)
415  {
416  so->currTuples = (char *) palloc(BLCKSZ * 2);
417  so->markTuples = so->currTuples + BLCKSZ;
418  }
419 
420  /*
421  * Reset the scan keys
422  */
423  if (scankey && scan->numberOfKeys > 0)
424  memmove(scan->keyData,
425  scankey,
426  scan->numberOfKeys * sizeof(ScanKeyData));
427  so->numberOfKeys = 0; /* until _bt_preprocess_keys sets it */
428 
429  /* If any keys are SK_SEARCHARRAY type, set up array-key info */
431 }
void _bt_preprocess_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:201

References _bt_killitems(), _bt_preprocess_array_keys(), BTScanOpaqueData::arrayKeyCount, BTScanPosInvalidate, BTScanPosIsValid, BTScanPosUnpinIfPinned, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, if(), IndexScanDescData::keyData, BTScanOpaqueData::markItemIndex, BTScanOpaqueData::markPos, BTScanOpaqueData::markTuples, BTScanOpaqueData::numberOfKeys, IndexScanDescData::numberOfKeys, BTScanOpaqueData::numKilled, IndexScanDescData::opaque, palloc(), and IndexScanDescData::xs_want_itup.

Referenced by bthandler().

◆ btrestrpos()

void btrestrpos ( IndexScanDesc  scan)

Definition at line 503 of file nbtree.c.

504 {
505  BTScanOpaque so = (BTScanOpaque) scan->opaque;
506 
507  /* Restore the marked positions of any array keys */
508  if (so->numArrayKeys)
510 
511  if (so->markItemIndex >= 0)
512  {
513  /*
514  * The scan has never moved to a new page since the last mark. Just
515  * restore the itemIndex.
516  *
517  * NB: In this case we can't count on anything in so->markPos to be
518  * accurate.
519  */
520  so->currPos.itemIndex = so->markItemIndex;
521  }
522  else
523  {
524  /*
525  * The scan moved to a new page after last mark or restore, and we are
526  * now restoring to the marked page. We aren't holding any read
527  * locks, but if we're still holding the pin for the current position,
528  * we must drop it.
529  */
530  if (BTScanPosIsValid(so->currPos))
531  {
532  /* Before leaving current page, deal with any killed items */
533  if (so->numKilled > 0)
534  _bt_killitems(scan);
536  }
537 
538  if (BTScanPosIsValid(so->markPos))
539  {
540  /* bump pin on mark buffer for assignment to current buffer */
541  if (BTScanPosIsPinned(so->markPos))
543  memcpy(&so->currPos, &so->markPos,
544  offsetof(BTScanPosData, items[1]) +
545  so->markPos.lastItem * sizeof(BTScanPosItem));
546  if (so->currTuples)
547  memcpy(so->currTuples, so->markTuples,
549  }
550  else
552  }
553 }
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:4592
void _bt_restore_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:630
int nextTupleOffset
Definition: nbtree.h:972

References _bt_killitems(), _bt_restore_array_keys(), BTScanPosInvalidate, BTScanPosIsPinned, BTScanPosIsValid, BTScanPosUnpinIfPinned, BTScanPosData::buf, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, if(), IncrBufferRefCount(), BTScanPosData::itemIndex, BTScanPosData::lastItem, BTScanOpaqueData::markItemIndex, BTScanOpaqueData::markPos, BTScanOpaqueData::markTuples, BTScanPosData::nextTupleOffset, BTScanOpaqueData::numArrayKeys, BTScanOpaqueData::numKilled, and IndexScanDescData::opaque.

Referenced by bthandler().

◆ btvacuumcleanup()

IndexBulkDeleteResult* btvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 804 of file nbtree.c.

805 {
806  BlockNumber num_delpages;
807 
808  /* No-op in ANALYZE ONLY mode */
809  if (info->analyze_only)
810  return stats;
811 
812  /*
813  * If btbulkdelete was called, we need not do anything (we just maintain
814  * the information used within _bt_vacuum_needs_cleanup() by calling
815  * _bt_set_cleanup_info() below).
816  *
817  * If btbulkdelete was _not_ called, then we have a choice to make: we
818  * must decide whether or not a btvacuumscan() call is needed now (i.e.
819  * whether the ongoing VACUUM operation can entirely avoid a physical scan
820  * of the index). A call to _bt_vacuum_needs_cleanup() decides it for us
821  * now.
822  */
823  if (stats == NULL)
824  {
825  /* Check if VACUUM operation can entirely avoid btvacuumscan() call */
826  if (!_bt_vacuum_needs_cleanup(info->index))
827  return NULL;
828 
829  /*
830  * Since we aren't going to actually delete any leaf items, there's no
831  * need to go through all the vacuum-cycle-ID pushups here.
832  *
833  * Posting list tuples are a source of inaccuracy for cleanup-only
834  * scans. btvacuumscan() will assume that the number of index tuples
835  * from each page can be used as num_index_tuples, even though
836  * num_index_tuples is supposed to represent the number of TIDs in the
837  * index. This naive approach can underestimate the number of tuples
838  * in the index significantly.
839  *
840  * We handle the problem by making num_index_tuples an estimate in
841  * cleanup-only case.
842  */
844  btvacuumscan(info, stats, NULL, NULL, 0);
845  stats->estimated_count = true;
846  }
847 
848  /*
849  * Maintain num_delpages value in metapage for _bt_vacuum_needs_cleanup().
850  *
851  * num_delpages is the number of deleted pages now in the index that were
852  * not safe to place in the FSM to be recycled just yet. num_delpages is
853  * greater than 0 only when _bt_pagedel() actually deleted pages during
854  * our call to btvacuumscan(). Even then, _bt_pendingfsm_finalize() must
855  * have failed to place any newly deleted pages in the FSM just moments
856  * ago. (Actually, there are edge cases where recycling of the current
857  * VACUUM's newly deleted pages does not even become safe by the time the
858  * next VACUUM comes around. See nbtree/README.)
859  */
860  Assert(stats->pages_deleted >= stats->pages_free);
861  num_delpages = stats->pages_deleted - stats->pages_free;
862  _bt_set_cleanup_info(info->index, num_delpages);
863 
864  /*
865  * It's quite possible for us to be fooled by concurrent page splits into
866  * double-counting some index tuples, so disbelieve any total that exceeds
867  * the underlying heap's count ... if we know that accurately. Otherwise
868  * this might just make matters worse.
869  */
870  if (!info->estimated_count)
871  {
872  if (stats->num_index_tuples > info->num_heap_tuples)
873  stats->num_index_tuples = info->num_heap_tuples;
874  }
875 
876  return stats;
877 }
void _bt_set_cleanup_info(Relation rel, BlockNumber num_delpages)
Definition: nbtpage.c:232
bool _bt_vacuum_needs_cleanup(Relation rel)
Definition: nbtpage.c:179
bool estimated_count
Definition: genam.h:78
BlockNumber pages_deleted
Definition: genam.h:82
double num_index_tuples
Definition: genam.h:79
double num_heap_tuples
Definition: genam.h:52
bool analyze_only
Definition: genam.h:48
bool estimated_count
Definition: genam.h:50

References _bt_set_cleanup_info(), _bt_vacuum_needs_cleanup(), IndexVacuumInfo::analyze_only, Assert(), btvacuumscan(), IndexVacuumInfo::estimated_count, IndexBulkDeleteResult::estimated_count, IndexVacuumInfo::index, IndexVacuumInfo::num_heap_tuples, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::pages_deleted, IndexBulkDeleteResult::pages_free, and palloc0().

Referenced by bthandler().

◆ btvalidate()

bool btvalidate ( Oid  opclassoid)

Definition at line 41 of file nbtvalidate.c.

42 {
43  bool result = true;
44  HeapTuple classtup;
45  Form_pg_opclass classform;
46  Oid opfamilyoid;
47  Oid opcintype;
48  char *opclassname;
49  HeapTuple familytup;
50  Form_pg_opfamily familyform;
51  char *opfamilyname;
52  CatCList *proclist,
53  *oprlist;
54  List *grouplist;
55  OpFamilyOpFuncGroup *opclassgroup;
56  List *familytypes;
57  int usefulgroups;
58  int i;
59  ListCell *lc;
60 
61  /* Fetch opclass information */
62  classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
63  if (!HeapTupleIsValid(classtup))
64  elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
65  classform = (Form_pg_opclass) GETSTRUCT(classtup);
66 
67  opfamilyoid = classform->opcfamily;
68  opcintype = classform->opcintype;
69  opclassname = NameStr(classform->opcname);
70 
71  /* Fetch opfamily information */
72  familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
73  if (!HeapTupleIsValid(familytup))
74  elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
75  familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
76 
77  opfamilyname = NameStr(familyform->opfname);
78 
79  /* Fetch all operators and support functions of the opfamily */
80  oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
81  proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
82 
83  /* Check individual support functions */
84  for (i = 0; i < proclist->n_members; i++)
85  {
86  HeapTuple proctup = &proclist->members[i]->tuple;
87  Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
88  bool ok;
89 
90  /* Check procedure numbers and function signatures */
91  switch (procform->amprocnum)
92  {
93  case BTORDER_PROC:
94  ok = check_amproc_signature(procform->amproc, INT4OID, true,
95  2, 2, procform->amproclefttype,
96  procform->amprocrighttype);
97  break;
98  case BTSORTSUPPORT_PROC:
99  ok = check_amproc_signature(procform->amproc, VOIDOID, true,
100  1, 1, INTERNALOID);
101  break;
102  case BTINRANGE_PROC:
103  ok = check_amproc_signature(procform->amproc, BOOLOID, true,
104  5, 5,
105  procform->amproclefttype,
106  procform->amproclefttype,
107  procform->amprocrighttype,
108  BOOLOID, BOOLOID);
109  break;
110  case BTEQUALIMAGE_PROC:
111  ok = check_amproc_signature(procform->amproc, BOOLOID, true,
112  1, 1, OIDOID);
113  break;
114  case BTOPTIONS_PROC:
115  ok = check_amoptsproc_signature(procform->amproc);
116  break;
117  default:
118  ereport(INFO,
119  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
120  errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
121  opfamilyname, "btree",
122  format_procedure(procform->amproc),
123  procform->amprocnum)));
124  result = false;
125  continue; /* don't want additional message */
126  }
127 
128  if (!ok)
129  {
130  ereport(INFO,
131  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
132  errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
133  opfamilyname, "btree",
134  format_procedure(procform->amproc),
135  procform->amprocnum)));
136  result = false;
137  }
138  }
139 
140  /* Check individual operators */
141  for (i = 0; i < oprlist->n_members; i++)
142  {
143  HeapTuple oprtup = &oprlist->members[i]->tuple;
144  Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
145 
146  /* Check that only allowed strategy numbers exist */
147  if (oprform->amopstrategy < 1 ||
148  oprform->amopstrategy > BTMaxStrategyNumber)
149  {
150  ereport(INFO,
151  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
152  errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
153  opfamilyname, "btree",
154  format_operator(oprform->amopopr),
155  oprform->amopstrategy)));
156  result = false;
157  }
158 
159  /* btree doesn't support ORDER BY operators */
160  if (oprform->amoppurpose != AMOP_SEARCH ||
161  OidIsValid(oprform->amopsortfamily))
162  {
163  ereport(INFO,
164  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
165  errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
166  opfamilyname, "btree",
167  format_operator(oprform->amopopr))));
168  result = false;
169  }
170 
171  /* Check operator signature --- same for all btree strategies */
172  if (!check_amop_signature(oprform->amopopr, BOOLOID,
173  oprform->amoplefttype,
174  oprform->amoprighttype))
175  {
176  ereport(INFO,
177  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
178  errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
179  opfamilyname, "btree",
180  format_operator(oprform->amopopr))));
181  result = false;
182  }
183  }
184 
185  /* Now check for inconsistent groups of operators/functions */
186  grouplist = identify_opfamily_groups(oprlist, proclist);
187  usefulgroups = 0;
188  opclassgroup = NULL;
189  familytypes = NIL;
190  foreach(lc, grouplist)
191  {
192  OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
193 
194  /*
195  * It is possible for an in_range support function to have a RHS type
196  * that is otherwise irrelevant to the opfamily --- for instance, SQL
197  * requires the datetime_ops opclass to have range support with an
198  * interval offset. So, if this group appears to contain only an
199  * in_range function, ignore it: it doesn't represent a pair of
200  * supported types.
201  */
202  if (thisgroup->operatorset == 0 &&
203  thisgroup->functionset == (1 << BTINRANGE_PROC))
204  continue;
205 
206  /* Else count it as a relevant group */
207  usefulgroups++;
208 
209  /* Remember the group exactly matching the test opclass */
210  if (thisgroup->lefttype == opcintype &&
211  thisgroup->righttype == opcintype)
212  opclassgroup = thisgroup;
213 
214  /*
215  * Identify all distinct data types handled in this opfamily. This
216  * implementation is O(N^2), but there aren't likely to be enough
217  * types in the family for it to matter.
218  */
219  familytypes = list_append_unique_oid(familytypes, thisgroup->lefttype);
220  familytypes = list_append_unique_oid(familytypes, thisgroup->righttype);
221 
222  /*
223  * Complain if there seems to be an incomplete set of either operators
224  * or support functions for this datatype pair. The sortsupport,
225  * in_range, and equalimage functions are considered optional.
226  */
227  if (thisgroup->operatorset !=
228  ((1 << BTLessStrategyNumber) |
230  (1 << BTEqualStrategyNumber) |
232  (1 << BTGreaterStrategyNumber)))
233  {
234  ereport(INFO,
235  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
236  errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
237  opfamilyname, "btree",
238  format_type_be(thisgroup->lefttype),
239  format_type_be(thisgroup->righttype))));
240  result = false;
241  }
242  if ((thisgroup->functionset & (1 << BTORDER_PROC)) == 0)
243  {
244  ereport(INFO,
245  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
246  errmsg("operator family \"%s\" of access method %s is missing support function for types %s and %s",
247  opfamilyname, "btree",
248  format_type_be(thisgroup->lefttype),
249  format_type_be(thisgroup->righttype))));
250  result = false;
251  }
252  }
253 
254  /* Check that the originally-named opclass is supported */
255  /* (if group is there, we already checked it adequately above) */
256  if (!opclassgroup)
257  {
258  ereport(INFO,
259  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
260  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
261  opclassname, "btree")));
262  result = false;
263  }
264 
265  /*
266  * Complain if the opfamily doesn't have entries for all possible
267  * combinations of its supported datatypes. While missing cross-type
268  * operators are not fatal, they do limit the planner's ability to derive
269  * additional qual clauses from equivalence classes, so it seems
270  * reasonable to insist that all built-in btree opfamilies be complete.
271  */
272  if (usefulgroups != (list_length(familytypes) * list_length(familytypes)))
273  {
274  ereport(INFO,
275  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
276  errmsg("operator family \"%s\" of access method %s is missing cross-type operator(s)",
277  opfamilyname, "btree")));
278  result = false;
279  }
280 
281  ReleaseCatCacheList(proclist);
282  ReleaseCatCacheList(oprlist);
283  ReleaseSysCache(familytup);
284  ReleaseSysCache(classtup);
285 
286  return result;
287 }
bool check_amproc_signature(Oid funcid, Oid restype, bool exact, int minargs, int maxargs,...)
Definition: amvalidate.c:152
bool check_amop_signature(Oid opno, Oid restype, Oid lefttype, Oid righttype)
Definition: amvalidate.c:206
List * identify_opfamily_groups(CatCList *oprlist, CatCList *proclist)
Definition: amvalidate.c:43
bool check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:192
#define NameStr(name)
Definition: c.h:733
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1889
#define INFO
Definition: elog.h:34
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1380
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:708
#define BTINRANGE_PROC
Definition: nbtree.h:709
#define BTOPTIONS_PROC
Definition: nbtree.h:711
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.h:68
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
char * format_operator(Oid operator_oid)
Definition: regproc.c:793
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:299
Definition: pg_list.h:54
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
int n_members
Definition: catcache.h:176
HeapTupleData tuple
Definition: catcache.h:121
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:122

References BTEQUALIMAGE_PROC, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTINRANGE_PROC, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTMaxStrategyNumber, BTOPTIONS_PROC, BTORDER_PROC, BTSORTSUPPORT_PROC, check_amop_signature(), check_amoptsproc_signature(), check_amproc_signature(), elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), format_type_be(), OpFamilyOpFuncGroup::functionset, GETSTRUCT, HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, list_append_unique_oid(), list_length(), catclist::members, catclist::n_members, NameStr, NIL, ObjectIdGetDatum(), OidIsValid, OpFamilyOpFuncGroup::operatorset, ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by bthandler().

◆ StaticAssertDecl()

StaticAssertDecl ( BT_OFFSET_MASK >=  INDEX_MAX_KEYS,
"BT_OFFSET_MASK can't fit INDEX_MAX_KEYS  
)