68 #define PARALLEL_KEY_BTREE_SHARED UINT64CONST(0xA000000000000001)
69 #define PARALLEL_KEY_TUPLESORT UINT64CONST(0xA000000000000002)
70 #define PARALLEL_KEY_TUPLESORT_SPOOL2 UINT64CONST(0xA000000000000003)
71 #define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000004)
72 #define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xA000000000000005)
73 #define PARALLEL_KEY_BUFFER_USAGE UINT64CONST(0xA000000000000006)
165 #define ParallelTableScanFromBTShared(shared) \
166 (ParallelTableScanDesc) ((char *) (shared) + BUFFERALIGN(sizeof(BTShared)))
269 bool *isnull,
bool tupleIsAlive,
void *
state);
275 bool newfirstdataitem);
289 bool *brokenhotchain);
307 #ifdef BTREE_BUILD_STATS
315 buildstate.
heap = heap;
316 buildstate.
spool = NULL;
326 elog(
ERROR,
"index \"%s\" already contains data",
348 #ifdef BTREE_BUILD_STATS
376 double reltuples = 0;
384 btspool->
heap = heap;
390 buildstate->
spool = btspool;
452 btspool2->
heap = heap;
456 buildstate->
spool2 = btspool2;
495 const int progress_index[] = {
500 const int64 progress_vals[] = {
513 buildstate->
spool2 = NULL;
548 #ifdef BTREE_BUILD_STATS
551 ShowUsage(
"BTREE BUILD (Spool) STATISTICS");
581 _bt_load(&wstate, btspool, btspool2);
601 if (tupleIsAlive || buildstate->
spool2 == NULL)
711 state->btps_lowkey = NULL;
714 state->btps_lastextra = 0;
715 state->btps_level = level;
723 state->btps_next = NULL;
773 bool newfirstdataitem)
777 if (newfirstdataitem)
788 elog(
ERROR,
"failed to add item to the index page");
845 Size last_truncextra;
856 npage =
state->btps_page;
857 nblkno =
state->btps_blkno;
858 last_off =
state->btps_lastoff;
859 last_truncextra =
state->btps_lastextra;
860 state->btps_lastextra = truncextra;
866 isleaf = (
state->btps_level == 0);
905 Assert(last_truncextra == 0 || isleaf);
907 (pgspc + last_truncextra < state->btps_full && last_off >
P_FIRSTKEY))
987 elog(
ERROR,
"failed to add high key to the index page");
999 if (
state->btps_next == NULL)
1067 state->btps_page = npage;
1068 state->btps_blkno = nblkno;
1069 state->btps_lastoff = last_off;
1101 pfree(postingtuple);
1189 bool merge = (btspool2 != NULL);
1197 int64 tuples_done = 0;
1217 for (
i = 0;
i < keysz;
i++)
1247 else if (itup != NULL)
1251 for (
i = 1;
i <= keysz;
i++)
1259 entry = sortKeys +
i - 1;
1264 attrDatum2, isNull2,
1313 else if (deduplicate)
1323 dstate->
base = NULL;
1327 dstate->
htids = NULL;
1459 int scantuplesortstates;
1470 bool leaderparticipates =
true;
1473 #ifdef DISABLE_LEADER_PARTICIPATION
1474 leaderparticipates =
false;
1486 scantuplesortstates = leaderparticipates ? request + 1 : request;
1550 if (pcxt->
seg == NULL)
1631 btleader->
pcxt = pcxt;
1633 if (leaderparticipates)
1653 if (leaderparticipates)
1716 int nparticipanttuplesorts;
1735 WAIT_EVENT_PARALLEL_CREATE_INDEX_SCAN);
1763 leaderworker2 = NULL;
1770 leaderworker2->
heap = leaderworker->
heap;
1787 #ifdef BTREE_BUILD_STATS
1790 ShowUsage(
"BTREE BUILD (Leader Partial Spool) STATISTICS");
1816 #ifdef BTREE_BUILD_STATS
1856 btspool->
heap = heapRel;
1857 btspool->
index = indexRel;
1889 sharedsort2, sortmem,
false);
1897 #ifdef BTREE_BUILD_STATS
1900 ShowUsage(
"BTREE BUILD (Worker Partial Spool) STATISTICS");
1943 sortmem, coordinate,
1975 buildstate.
spool = btspool;
1976 buildstate.
spool2 = btspool2;
1987 (
void *) &buildstate, scan);
void InitializeParallelDSM(ParallelContext *pcxt)
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
void LaunchParallelWorkers(ParallelContext *pcxt)
void DestroyParallelContext(ParallelContext *pcxt)
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
void WaitForParallelWorkersToAttach(ParallelContext *pcxt)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_report_activity(BackendState state, const char *cmd_str)
static Datum values[MAXATTR]
#define RelationGetNumberOfBlocks(reln)
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Size PageGetFreeSpace(Page page)
PageHeaderData * PageHeader
static Item PageGetItem(Page page, ItemId itemId)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static OffsetNumber PageGetMaxOffsetNumber(Page page)
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
#define MAXALIGN_DOWN(LEN)
bool ConditionVariableCancelSleep(void)
void ConditionVariableInit(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
void ConditionVariableSignal(ConditionVariable *cv)
elog(ERROR, "%s: %s", p2, msg)
static int compare(const void *arg1, const void *arg2)
bool log_btree_build_stats
IndexInfo * BuildIndexInfo(Relation index)
void index_close(Relation relation, LOCKMODE lockmode)
Relation index_open(Oid relationId, LOCKMODE lockmode)
IndexTuple CopyIndexTuple(IndexTuple source)
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
void InstrStartParallelQuery(void)
#define ItemIdGetLength(itemId)
struct ItemIdData ItemIdData
#define ItemIdSetUnused(itemId)
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
IndexTupleData * IndexTuple
#define IndexTupleSize(itup)
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
struct IndexTupleData IndexTupleData
Assert(fmt[strlen(fmt) - 1] !='\n')
#define AccessExclusiveLock
#define ShareUpdateExclusiveLock
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void * palloc_aligned(Size size, Size alignto, int flags)
#define CHECK_FOR_INTERRUPTS()
bool _bt_dedup_save_htid(BTDedupState state, IndexTuple itup)
void _bt_dedup_start_pending(BTDedupState state, IndexTuple base, OffsetNumber baseoff)
IndexTuple _bt_form_posting(IndexTuple base, ItemPointer htids, int nhtids)
void _bt_pageinit(Page page, Size size)
void _bt_initmetapage(Page page, BlockNumber rootbknum, uint32 level, bool allequalimage)
#define BTGetDeduplicateItems(relation)
#define BTGetTargetPageFreeSpace(relation)
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
#define P_LEFTMOST(opaque)
#define BTPageGetOpaque(page)
static void BTreeTupleSetDownLink(IndexTuple pivot, BlockNumber blkno)
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
#define BTMaxItemSize(page)
#define SK_BT_NULLS_FIRST
BTDedupStateData * BTDedupState
static void BTreeTupleSetNAtts(IndexTuple itup, uint16 nkeyatts, bool heaptid)
#define BTreeTupleGetNAtts(itup, rel)
#define BTREE_NONLEAF_FILLFACTOR
#define PARALLEL_KEY_BUFFER_USAGE
#define ParallelTableScanFromBTShared(shared)
static void _bt_sortaddtup(Page page, Size itemsize, IndexTuple itup, OffsetNumber itup_off, bool newfirstdataitem)
static void _bt_slideleft(Page rightmostpage)
static BTPageState * _bt_pagestate(BTWriteState *wstate, uint32 level)
static void _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
static void _bt_blwritepage(BTWriteState *wstate, Page page, BlockNumber blkno)
static void _bt_end_parallel(BTLeader *btleader)
IndexBuildResult * btbuild(Relation heap, Relation index, IndexInfo *indexInfo)
#define PARALLEL_KEY_TUPLESORT_SPOOL2
static void _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2, BTShared *btshared, Sharedsort *sharedsort, Sharedsort *sharedsort2, int sortmem, bool progress)
static Size _bt_parallel_estimate_shared(Relation heap, Snapshot snapshot)
struct BTPageState BTPageState
static void _bt_sort_dedup_finish_pending(BTWriteState *wstate, BTPageState *state, BTDedupState dstate)
static double _bt_parallel_heapscan(BTBuildState *buildstate, bool *brokenhotchain)
static void _bt_leafbuild(BTSpool *btspool, BTSpool *btspool2)
#define PARALLEL_KEY_BTREE_SHARED
static void _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
static void _bt_buildadd(BTWriteState *wstate, BTPageState *state, IndexTuple itup, Size truncextra)
struct BTBuildState BTBuildState
void _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
static void _bt_build_callback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
static double _bt_spools_heapscan(Relation heap, Relation index, BTBuildState *buildstate, IndexInfo *indexInfo)
static void _bt_spooldestroy(BTSpool *btspool)
static Page _bt_blnewpage(uint32 level)
static void _bt_uppershutdown(BTWriteState *wstate, BTPageState *state)
#define PARALLEL_KEY_TUPLESORT
#define PARALLEL_KEY_QUERY_TEXT
#define PARALLEL_KEY_WAL_USAGE
static void _bt_leader_participate_as_worker(BTBuildState *buildstate)
struct BTWriteState BTWriteState
static void _bt_spool(BTSpool *btspool, ItemPointer self, Datum *values, bool *isnull)
void _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace, Page page, IndexTuple newtup)
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
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_allequalimage(Relation rel, bool debugmessage)
#define InvalidOffsetNumber
#define OffsetNumberNext(offsetNumber)
#define OffsetNumberPrev(offsetNumber)
static pairingheap_node * merge(pairingheap *heap, pairingheap_node *a, pairingheap_node *b)
const char * debug_query_string
void ShowUsage(const char *title)
#define PROGRESS_CREATEIDX_TUPLES_TOTAL
#define PROGRESS_SCAN_BLOCKS_DONE
#define PROGRESS_CREATEIDX_TUPLES_DONE
#define PROGRESS_CREATEIDX_SUBPHASE
#define PROGRESS_SCAN_BLOCKS_TOTAL
#define RelationGetRelid(relation)
static SMgrRelation RelationGetSmgr(Relation rel)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define RelationNeedsWAL(relation)
#define IndexRelationGetNumberOfKeyAttributes(relation)
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
#define shm_toc_estimate_chunk(e, sz)
#define shm_toc_estimate_keys(e, cnt)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Snapshot GetTransactionSnapshot(void)
void UnregisterSnapshot(Snapshot snapshot)
Snapshot RegisterSnapshot(Snapshot snapshot)
#define IsMVCCSnapshot(snapshot)
void PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy, SortSupport ssup)
struct SortSupportData * SortSupport
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
#define SpinLockInit(lock)
#define SpinLockRelease(lock)
#define SpinLockAcquire(lock)
#define BTGreaterStrategyNumber
#define BTLessStrategyNumber
int nparticipanttuplesorts
BufferUsage * bufferusage
OffsetNumber btps_lastoff
struct BTPageState * btps_next
ScanKeyData scankeys[INDEX_MAX_KEYS]
ConditionVariable workersdonecv
Tuplesortstate * sortstate
BlockNumber btws_pages_alloced
BlockNumber btws_pages_written
shm_toc_estimator estimator
RelFileLocator rd_locator
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TableScanDesc table_beginscan_parallel(Relation relation, ParallelTableScanDesc pscan)
Size table_parallelscan_estimate(Relation rel, Snapshot snapshot)
void table_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan, Snapshot snapshot)
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
void tuplesort_performsort(Tuplesortstate *state)
void tuplesort_initialize_shared(Sharedsort *shared, int nWorkers, dsm_segment *seg)
Size tuplesort_estimate_shared(int nWorkers)
void tuplesort_end(Tuplesortstate *state)
void tuplesort_attach_shared(Sharedsort *shared, dsm_segment *seg)
struct SortCoordinateData * SortCoordinate
IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward)
Tuplesortstate * tuplesort_begin_index_btree(Relation heapRel, Relation indexRel, bool enforceUnique, bool uniqueNullsNotDistinct, int workMem, SortCoordinate coordinate, int sortopt)
void tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, ItemPointer self, Datum *values, bool *isnull)
void ExitParallelMode(void)
void EnterParallelMode(void)
XLogRecPtr log_newpage(RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blkno, Page page, bool page_std)