PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
brin.c File Reference
#include "postgres.h"
#include "access/brin.h"
#include "access/brin_page.h"
#include "access/brin_pageops.h"
#include "access/brin_xlog.h"
#include "access/relation.h"
#include "access/reloptions.h"
#include "access/relscan.h"
#include "access/table.h"
#include "access/tableam.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/datum.h"
#include "utils/fmgrprotos.h"
#include "utils/guc.h"
#include "utils/index_selfuncs.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/tuplesort.h"
Include dependency graph for brin.c:

Go to the source code of this file.

Data Structures

struct  BrinShared
 
struct  BrinLeader
 
struct  BrinBuildState
 
struct  BrinInsertState
 
struct  BrinOpaque
 

Macros

#define PARALLEL_KEY_BRIN_SHARED   UINT64CONST(0xB000000000000001)
 
#define PARALLEL_KEY_TUPLESORT   UINT64CONST(0xB000000000000002)
 
#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xB000000000000003)
 
#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xB000000000000004)
 
#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xB000000000000005)
 
#define ParallelTableScanFromBrinShared(shared)    (ParallelTableScanDesc) ((char *) (shared) + BUFFERALIGN(sizeof(BrinShared)))
 
#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber
 

Typedefs

typedef struct BrinShared BrinShared
 
typedef struct BrinLeader BrinLeader
 
typedef struct BrinBuildState BrinBuildState
 
typedef struct BrinInsertState BrinInsertState
 
typedef struct BrinOpaque BrinOpaque
 

Functions

static BrinBuildStateinitialize_brin_buildstate (Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange, BlockNumber tablePages)
 
static BrinInsertStateinitialize_brin_insertstate (Relation idxRel, IndexInfo *indexInfo)
 
static void terminate_brin_buildstate (BrinBuildState *state)
 
static void brinsummarize (Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
 
static void form_and_insert_tuple (BrinBuildState *state)
 
static void form_and_spill_tuple (BrinBuildState *state)
 
static void union_tuples (BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
 
static void brin_vacuum_scan (Relation idxrel, BufferAccessStrategy strategy)
 
static bool add_values_to_range (Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, const Datum *values, const bool *nulls)
 
static bool check_null_keys (BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
 
static void brin_fill_empty_ranges (BrinBuildState *state, BlockNumber prevRange, BlockNumber nextRange)
 
static void _brin_begin_parallel (BrinBuildState *buildstate, Relation heap, Relation index, bool isconcurrent, int request)
 
static void _brin_end_parallel (BrinLeader *brinleader, BrinBuildState *state)
 
static Size _brin_parallel_estimate_shared (Relation heap, Snapshot snapshot)
 
static double _brin_parallel_heapscan (BrinBuildState *state)
 
static double _brin_parallel_merge (BrinBuildState *state)
 
static void _brin_leader_participate_as_worker (BrinBuildState *buildstate, Relation heap, Relation index)
 
static void _brin_parallel_scan_and_build (BrinBuildState *state, BrinShared *brinshared, Sharedsort *sharedsort, Relation heap, Relation index, int sortmem, bool progress)
 
Datum brinhandler (PG_FUNCTION_ARGS)
 
bool brininsert (Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
 
void brininsertcleanup (Relation index, IndexInfo *indexInfo)
 
IndexScanDesc brinbeginscan (Relation r, int nkeys, int norderbys)
 
int64 bringetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
void brinrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void brinendscan (IndexScanDesc scan)
 
static void brinbuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
 
static void brinbuildCallbackParallel (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
 
IndexBuildResultbrinbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void brinbuildempty (Relation index)
 
IndexBulkDeleteResultbrinbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultbrinvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
byteabrinoptions (Datum reloptions, bool validate)
 
Datum brin_summarize_new_values (PG_FUNCTION_ARGS)
 
Datum brin_summarize_range (PG_FUNCTION_ARGS)
 
Datum brin_desummarize_range (PG_FUNCTION_ARGS)
 
BrinDescbrin_build_desc (Relation rel)
 
void brin_free_desc (BrinDesc *bdesc)
 
void brinGetStats (Relation index, BrinStatsData *stats)
 
static void summarize_range (IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
 
void _brin_parallel_build_main (dsm_segment *seg, shm_toc *toc)
 
static void brin_build_empty_tuple (BrinBuildState *state, BlockNumber blkno)
 

Macro Definition Documentation

◆ BRIN_ALL_BLOCKRANGES

#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber

Definition at line 209 of file brin.c.

◆ PARALLEL_KEY_BRIN_SHARED

#define PARALLEL_KEY_BRIN_SHARED   UINT64CONST(0xB000000000000001)

Definition at line 47 of file brin.c.

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xB000000000000005)

Definition at line 51 of file brin.c.

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xB000000000000003)

Definition at line 49 of file brin.c.

◆ PARALLEL_KEY_TUPLESORT

#define PARALLEL_KEY_TUPLESORT   UINT64CONST(0xB000000000000002)

Definition at line 48 of file brin.c.

◆ PARALLEL_KEY_WAL_USAGE

#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xB000000000000004)

Definition at line 50 of file brin.c.

◆ ParallelTableScanFromBrinShared

#define ParallelTableScanFromBrinShared (   shared)     (ParallelTableScanDesc) ((char *) (shared) + BUFFERALIGN(sizeof(BrinShared)))

Definition at line 116 of file brin.c.

Typedef Documentation

◆ BrinBuildState

◆ BrinInsertState

◆ BrinLeader

typedef struct BrinLeader BrinLeader

◆ BrinOpaque

typedef struct BrinOpaque BrinOpaque

◆ BrinShared

typedef struct BrinShared BrinShared

Function Documentation

◆ _brin_begin_parallel()

static void _brin_begin_parallel ( BrinBuildState buildstate,
Relation  heap,
Relation  index,
bool  isconcurrent,
int  request 
)
static

Definition at line 2364 of file brin.c.

2366{
2367 ParallelContext *pcxt;
2368 int scantuplesortstates;
2369 Snapshot snapshot;
2370 Size estbrinshared;
2371 Size estsort;
2372 BrinShared *brinshared;
2373 Sharedsort *sharedsort;
2374 BrinLeader *brinleader = (BrinLeader *) palloc0(sizeof(BrinLeader));
2375 WalUsage *walusage;
2376 BufferUsage *bufferusage;
2377 bool leaderparticipates = true;
2378 int querylen;
2379
2380#ifdef DISABLE_LEADER_PARTICIPATION
2381 leaderparticipates = false;
2382#endif
2383
2384 /*
2385 * Enter parallel mode, and create context for parallel build of brin
2386 * index
2387 */
2389 Assert(request > 0);
2390 pcxt = CreateParallelContext("postgres", "_brin_parallel_build_main",
2391 request);
2392
2393 scantuplesortstates = leaderparticipates ? request + 1 : request;
2394
2395 /*
2396 * Prepare for scan of the base relation. In a normal index build, we use
2397 * SnapshotAny because we must retrieve all tuples and do our own time
2398 * qual checks (because we have to index RECENTLY_DEAD tuples). In a
2399 * concurrent build, we take a regular MVCC snapshot and index whatever's
2400 * live according to that.
2401 */
2402 if (!isconcurrent)
2403 snapshot = SnapshotAny;
2404 else
2406
2407 /*
2408 * Estimate size for our own PARALLEL_KEY_BRIN_SHARED workspace.
2409 */
2410 estbrinshared = _brin_parallel_estimate_shared(heap, snapshot);
2411 shm_toc_estimate_chunk(&pcxt->estimator, estbrinshared);
2412 estsort = tuplesort_estimate_shared(scantuplesortstates);
2413 shm_toc_estimate_chunk(&pcxt->estimator, estsort);
2414
2416
2417 /*
2418 * Estimate space for WalUsage and BufferUsage -- PARALLEL_KEY_WAL_USAGE
2419 * and PARALLEL_KEY_BUFFER_USAGE.
2420 *
2421 * If there are no extensions loaded that care, we could skip this. We
2422 * have no way of knowing whether anyone's looking at pgWalUsage or
2423 * pgBufferUsage, so do it unconditionally.
2424 */
2426 mul_size(sizeof(WalUsage), pcxt->nworkers));
2429 mul_size(sizeof(BufferUsage), pcxt->nworkers));
2431
2432 /* Finally, estimate PARALLEL_KEY_QUERY_TEXT space */
2434 {
2435 querylen = strlen(debug_query_string);
2436 shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
2438 }
2439 else
2440 querylen = 0; /* keep compiler quiet */
2441
2442 /* Everyone's had a chance to ask for space, so now create the DSM */
2444
2445 /* If no DSM segment was available, back out (do serial build) */
2446 if (pcxt->seg == NULL)
2447 {
2448 if (IsMVCCSnapshot(snapshot))
2449 UnregisterSnapshot(snapshot);
2452 return;
2453 }
2454
2455 /* Store shared build state, for which we reserved space */
2456 brinshared = (BrinShared *) shm_toc_allocate(pcxt->toc, estbrinshared);
2457 /* Initialize immutable state */
2458 brinshared->heaprelid = RelationGetRelid(heap);
2459 brinshared->indexrelid = RelationGetRelid(index);
2460 brinshared->isconcurrent = isconcurrent;
2461 brinshared->scantuplesortstates = scantuplesortstates;
2462 brinshared->pagesPerRange = buildstate->bs_pagesPerRange;
2463 brinshared->queryid = pgstat_get_my_query_id();
2465 SpinLockInit(&brinshared->mutex);
2466
2467 /* Initialize mutable state */
2468 brinshared->nparticipantsdone = 0;
2469 brinshared->reltuples = 0.0;
2470 brinshared->indtuples = 0.0;
2471
2474 snapshot);
2475
2476 /*
2477 * Store shared tuplesort-private state, for which we reserved space.
2478 * Then, initialize opaque state using tuplesort routine.
2479 */
2480 sharedsort = (Sharedsort *) shm_toc_allocate(pcxt->toc, estsort);
2481 tuplesort_initialize_shared(sharedsort, scantuplesortstates,
2482 pcxt->seg);
2483
2484 /*
2485 * Store shared tuplesort-private state, for which we reserved space.
2486 * Then, initialize opaque state using tuplesort routine.
2487 */
2488 shm_toc_insert(pcxt->toc, PARALLEL_KEY_BRIN_SHARED, brinshared);
2489 shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLESORT, sharedsort);
2490
2491 /* Store query string for workers */
2493 {
2494 char *sharedquery;
2495
2496 sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
2497 memcpy(sharedquery, debug_query_string, querylen + 1);
2498 shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, sharedquery);
2499 }
2500
2501 /*
2502 * Allocate space for each worker's WalUsage and BufferUsage; no need to
2503 * initialize.
2504 */
2505 walusage = shm_toc_allocate(pcxt->toc,
2506 mul_size(sizeof(WalUsage), pcxt->nworkers));
2507 shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage);
2508 bufferusage = shm_toc_allocate(pcxt->toc,
2509 mul_size(sizeof(BufferUsage), pcxt->nworkers));
2510 shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufferusage);
2511
2512 /* Launch workers, saving status for leader/caller */
2514 brinleader->pcxt = pcxt;
2515 brinleader->nparticipanttuplesorts = pcxt->nworkers_launched;
2516 if (leaderparticipates)
2517 brinleader->nparticipanttuplesorts++;
2518 brinleader->brinshared = brinshared;
2519 brinleader->sharedsort = sharedsort;
2520 brinleader->snapshot = snapshot;
2521 brinleader->walusage = walusage;
2522 brinleader->bufferusage = bufferusage;
2523
2524 /* If no workers were successfully launched, back out (do serial build) */
2525 if (pcxt->nworkers_launched == 0)
2526 {
2527 _brin_end_parallel(brinleader, NULL);
2528 return;
2529 }
2530
2531 /* Save leader state now that it's clear build will be parallel */
2532 buildstate->bs_leader = brinleader;
2533
2534 /* Join heap scan ourselves */
2535 if (leaderparticipates)
2536 _brin_leader_participate_as_worker(buildstate, heap, index);
2537
2538 /*
2539 * Caller needs to wait for all launched workers when we return. Make
2540 * sure that the failure-to-start case will not hang forever.
2541 */
2543}
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:211
void LaunchParallelWorkers(ParallelContext *pcxt)
Definition: parallel.c:573
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:950
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:173
void WaitForParallelWorkersToAttach(ParallelContext *pcxt)
Definition: parallel.c:693
uint64 pgstat_get_my_query_id(void)
#define PARALLEL_KEY_BUFFER_USAGE
Definition: brin.c:51
#define PARALLEL_KEY_BRIN_SHARED
Definition: brin.c:47
static void _brin_leader_participate_as_worker(BrinBuildState *buildstate, Relation heap, Relation index)
Definition: brin.c:2779
static void _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state)
Definition: brin.c:2549
static Size _brin_parallel_estimate_shared(Relation heap, Snapshot snapshot)
Definition: brin.c:2768
#define ParallelTableScanFromBrinShared(shared)
Definition: brin.c:116
#define PARALLEL_KEY_TUPLESORT
Definition: brin.c:48
#define PARALLEL_KEY_QUERY_TEXT
Definition: brin.c:49
#define PARALLEL_KEY_WAL_USAGE
Definition: brin.c:50
size_t Size
Definition: c.h:576
void ConditionVariableInit(ConditionVariable *cv)
Assert(PointerIsAligned(start, uint64))
void * palloc0(Size size)
Definition: mcxt.c:1347
const char * debug_query_string
Definition: postgres.c:88
#define RelationGetRelid(relation)
Definition: rel.h:513
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition: shmem.c:505
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:271
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:853
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:811
#define SnapshotAny
Definition: snapmgr.h:33
#define IsMVCCSnapshot(snapshot)
Definition: snapmgr.h:55
#define SpinLockInit(lock)
Definition: spin.h:57
BlockNumber bs_pagesPerRange
Definition: brin.c:161
BrinLeader * bs_leader
Definition: brin.c:177
int nparticipanttuplesorts
Definition: brin.c:133
WalUsage * walusage
Definition: brin.c:147
BrinShared * brinshared
Definition: brin.c:144
BufferUsage * bufferusage
Definition: brin.c:148
Snapshot snapshot
Definition: brin.c:146
Sharedsort * sharedsort
Definition: brin.c:145
ParallelContext * pcxt
Definition: brin.c:125
slock_t mutex
Definition: brin.c:87
int scantuplesortstates
Definition: brin.c:68
int nparticipantsdone
Definition: brin.c:99
Oid heaprelid
Definition: brin.c:64
BlockNumber pagesPerRange
Definition: brin.c:67
uint64 queryid
Definition: brin.c:71
ConditionVariable workersdonecv
Definition: brin.c:79
Oid indexrelid
Definition: brin.c:65
bool isconcurrent
Definition: brin.c:66
double indtuples
Definition: brin.c:101
double reltuples
Definition: brin.c:100
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
shm_toc * toc
Definition: parallel.h:44
int nworkers_launched
Definition: parallel.h:37
Definition: type.h:96
void table_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan, Snapshot snapshot)
Definition: tableam.c:146
void tuplesort_initialize_shared(Sharedsort *shared, int nWorkers, dsm_segment *seg)
Definition: tuplesort.c:2938
Size tuplesort_estimate_shared(int nWorkers)
Definition: tuplesort.c:2917
void ExitParallelMode(void)
Definition: xact.c:1064
void EnterParallelMode(void)
Definition: xact.c:1051

References _brin_end_parallel(), _brin_leader_participate_as_worker(), _brin_parallel_estimate_shared(), Assert(), BrinLeader::brinshared, BrinBuildState::bs_leader, BrinBuildState::bs_pagesPerRange, BrinLeader::bufferusage, ConditionVariableInit(), CreateParallelContext(), debug_query_string, DestroyParallelContext(), EnterParallelMode(), ParallelContext::estimator, ExitParallelMode(), GetTransactionSnapshot(), BrinShared::heaprelid, BrinShared::indexrelid, BrinShared::indtuples, InitializeParallelDSM(), BrinShared::isconcurrent, IsMVCCSnapshot, LaunchParallelWorkers(), mul_size(), BrinShared::mutex, BrinShared::nparticipantsdone, BrinLeader::nparticipanttuplesorts, ParallelContext::nworkers, ParallelContext::nworkers_launched, BrinShared::pagesPerRange, palloc0(), PARALLEL_KEY_BRIN_SHARED, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_QUERY_TEXT, PARALLEL_KEY_TUPLESORT, PARALLEL_KEY_WAL_USAGE, ParallelTableScanFromBrinShared, BrinLeader::pcxt, pgstat_get_my_query_id(), BrinShared::queryid, RegisterSnapshot(), RelationGetRelid, BrinShared::reltuples, BrinShared::scantuplesortstates, ParallelContext::seg, BrinLeader::sharedsort, shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), BrinLeader::snapshot, SnapshotAny, SpinLockInit, table_parallelscan_initialize(), ParallelContext::toc, tuplesort_estimate_shared(), tuplesort_initialize_shared(), UnregisterSnapshot(), WaitForParallelWorkersToAttach(), BrinLeader::walusage, and BrinShared::workersdonecv.

Referenced by brinbuild().

◆ _brin_end_parallel()

static void _brin_end_parallel ( BrinLeader brinleader,
BrinBuildState state 
)
static

Definition at line 2549 of file brin.c.

2550{
2551 int i;
2552
2553 /* Shutdown worker processes */
2555
2556 /*
2557 * Next, accumulate WAL usage. (This must wait for the workers to finish,
2558 * or we might get incomplete data.)
2559 */
2560 for (i = 0; i < brinleader->pcxt->nworkers_launched; i++)
2561 InstrAccumParallelQuery(&brinleader->bufferusage[i], &brinleader->walusage[i]);
2562
2563 /* Free last reference to MVCC snapshot, if one was used */
2564 if (IsMVCCSnapshot(brinleader->snapshot))
2565 UnregisterSnapshot(brinleader->snapshot);
2566 DestroyParallelContext(brinleader->pcxt);
2568}
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:796
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:218
int i
Definition: isn.c:77

References BrinLeader::bufferusage, DestroyParallelContext(), ExitParallelMode(), i, InstrAccumParallelQuery(), IsMVCCSnapshot, ParallelContext::nworkers_launched, BrinLeader::pcxt, BrinLeader::snapshot, UnregisterSnapshot(), WaitForParallelWorkersToFinish(), and BrinLeader::walusage.

Referenced by _brin_begin_parallel(), and brinbuild().

◆ _brin_leader_participate_as_worker()

static void _brin_leader_participate_as_worker ( BrinBuildState buildstate,
Relation  heap,
Relation  index 
)
static

Definition at line 2779 of file brin.c.

2780{
2781 BrinLeader *brinleader = buildstate->bs_leader;
2782 int sortmem;
2783
2784 /*
2785 * Might as well use reliable figure when doling out maintenance_work_mem
2786 * (when requested number of workers were not launched, this will be
2787 * somewhat higher than it is for other workers).
2788 */
2789 sortmem = maintenance_work_mem / brinleader->nparticipanttuplesorts;
2790
2791 /* Perform work common to all participants */
2792 _brin_parallel_scan_and_build(buildstate, brinleader->brinshared,
2793 brinleader->sharedsort, heap, index, sortmem, true);
2794}
static void _brin_parallel_scan_and_build(BrinBuildState *state, BrinShared *brinshared, Sharedsort *sharedsort, Relation heap, Relation index, int sortmem, bool progress)
Definition: brin.c:2807
int maintenance_work_mem
Definition: globals.c:132

References _brin_parallel_scan_and_build(), BrinLeader::brinshared, BrinBuildState::bs_leader, maintenance_work_mem, BrinLeader::nparticipanttuplesorts, and BrinLeader::sharedsort.

Referenced by _brin_begin_parallel().

◆ _brin_parallel_build_main()

void _brin_parallel_build_main ( dsm_segment seg,
shm_toc toc 
)

Definition at line 2864 of file brin.c.

2865{
2866 char *sharedquery;
2867 BrinShared *brinshared;
2868 Sharedsort *sharedsort;
2869 BrinBuildState *buildstate;
2870 Relation heapRel;
2871 Relation indexRel;
2872 LOCKMODE heapLockmode;
2873 LOCKMODE indexLockmode;
2874 WalUsage *walusage;
2875 BufferUsage *bufferusage;
2876 int sortmem;
2877
2878 /*
2879 * The only possible status flag that can be set to the parallel worker is
2880 * PROC_IN_SAFE_IC.
2881 */
2882 Assert((MyProc->statusFlags == 0) ||
2884
2885 /* Set debug_query_string for individual workers first */
2886 sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);
2887 debug_query_string = sharedquery;
2888
2889 /* Report the query string from leader */
2891
2892 /* Look up brin shared state */
2893 brinshared = shm_toc_lookup(toc, PARALLEL_KEY_BRIN_SHARED, false);
2894
2895 /* Open relations using lock modes known to be obtained by index.c */
2896 if (!brinshared->isconcurrent)
2897 {
2898 heapLockmode = ShareLock;
2899 indexLockmode = AccessExclusiveLock;
2900 }
2901 else
2902 {
2903 heapLockmode = ShareUpdateExclusiveLock;
2904 indexLockmode = RowExclusiveLock;
2905 }
2906
2907 /* Track query ID */
2908 pgstat_report_query_id(brinshared->queryid, false);
2909
2910 /* Open relations within worker */
2911 heapRel = table_open(brinshared->heaprelid, heapLockmode);
2912 indexRel = index_open(brinshared->indexrelid, indexLockmode);
2913
2914 buildstate = initialize_brin_buildstate(indexRel, NULL,
2915 brinshared->pagesPerRange,
2917
2918 /* Look up shared state private to tuplesort.c */
2919 sharedsort = shm_toc_lookup(toc, PARALLEL_KEY_TUPLESORT, false);
2920 tuplesort_attach_shared(sharedsort, seg);
2921
2922 /* Prepare to track buffer usage during parallel execution */
2924
2925 /*
2926 * Might as well use reliable figure when doling out maintenance_work_mem
2927 * (when requested number of workers were not launched, this will be
2928 * somewhat higher than it is for other workers).
2929 */
2930 sortmem = maintenance_work_mem / brinshared->scantuplesortstates;
2931
2932 _brin_parallel_scan_and_build(buildstate, brinshared, sharedsort,
2933 heapRel, indexRel, sortmem, false);
2934
2935 /* Report WAL/buffer usage during parallel execution */
2936 bufferusage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
2937 walusage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
2939 &walusage[ParallelWorkerNumber]);
2940
2941 index_close(indexRel, indexLockmode);
2942 table_close(heapRel, heapLockmode);
2943}
int ParallelWorkerNumber
Definition: parallel.c:115
void pgstat_report_query_id(uint64 query_id, bool force)
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
#define InvalidBlockNumber
Definition: block.h:33
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange, BlockNumber tablePages)
Definition: brin.c:1670
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 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
uint8 statusFlags
Definition: proc.h:243
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:2961

References _brin_parallel_scan_and_build(), AccessExclusiveLock, Assert(), debug_query_string, BrinShared::heaprelid, index_close(), index_open(), BrinShared::indexrelid, initialize_brin_buildstate(), InstrEndParallelQuery(), InstrStartParallelQuery(), InvalidBlockNumber, BrinShared::isconcurrent, maintenance_work_mem, MyProc, BrinShared::pagesPerRange, PARALLEL_KEY_BRIN_SHARED, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_QUERY_TEXT, PARALLEL_KEY_TUPLESORT, PARALLEL_KEY_WAL_USAGE, ParallelWorkerNumber, pgstat_report_activity(), pgstat_report_query_id(), PROC_IN_SAFE_IC, BrinShared::queryid, RowExclusiveLock, BrinShared::scantuplesortstates, ShareLock, ShareUpdateExclusiveLock, shm_toc_lookup(), STATE_RUNNING, PGPROC::statusFlags, table_close(), table_open(), and tuplesort_attach_shared().

◆ _brin_parallel_estimate_shared()

static Size _brin_parallel_estimate_shared ( Relation  heap,
Snapshot  snapshot 
)
static

Definition at line 2768 of file brin.c.

2769{
2770 /* c.f. shm_toc_allocate as to why BUFFERALIGN is used */
2771 return add_size(BUFFERALIGN(sizeof(BrinShared)),
2772 table_parallelscan_estimate(heap, snapshot));
2773}
#define BUFFERALIGN(LEN)
Definition: c.h:784
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
Size table_parallelscan_estimate(Relation rel, Snapshot snapshot)
Definition: tableam.c:131

References add_size(), BUFFERALIGN, and table_parallelscan_estimate().

Referenced by _brin_begin_parallel().

◆ _brin_parallel_heapscan()

static double _brin_parallel_heapscan ( BrinBuildState state)
static

Definition at line 2580 of file brin.c.

2581{
2582 BrinShared *brinshared = state->bs_leader->brinshared;
2583 int nparticipanttuplesorts;
2584
2585 nparticipanttuplesorts = state->bs_leader->nparticipanttuplesorts;
2586 for (;;)
2587 {
2588 SpinLockAcquire(&brinshared->mutex);
2589 if (brinshared->nparticipantsdone == nparticipanttuplesorts)
2590 {
2591 /* copy the data into leader state */
2592 state->bs_reltuples = brinshared->reltuples;
2593 state->bs_numtuples = brinshared->indtuples;
2594
2595 SpinLockRelease(&brinshared->mutex);
2596 break;
2597 }
2598 SpinLockRelease(&brinshared->mutex);
2599
2601 WAIT_EVENT_PARALLEL_CREATE_INDEX_SCAN);
2602 }
2603
2605
2606 return state->bs_reltuples;
2607}
bool ConditionVariableCancelSleep(void)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
Definition: regguts.h:323

References ConditionVariableCancelSleep(), ConditionVariableSleep(), BrinShared::indtuples, BrinShared::mutex, BrinShared::nparticipantsdone, BrinShared::reltuples, SpinLockAcquire, SpinLockRelease, and BrinShared::workersdonecv.

Referenced by _brin_parallel_merge().

◆ _brin_parallel_merge()

static double _brin_parallel_merge ( BrinBuildState state)
static

Definition at line 2621 of file brin.c.

2622{
2623 BrinTuple *btup;
2624 BrinMemTuple *memtuple = NULL;
2625 Size tuplen;
2626 BlockNumber prevblkno = InvalidBlockNumber;
2627 MemoryContext rangeCxt,
2628 oldCxt;
2629 double reltuples;
2630
2631 /* wait for workers to scan table and produce partial results */
2632 reltuples = _brin_parallel_heapscan(state);
2633
2634 /* do the actual sort in the leader */
2635 tuplesort_performsort(state->bs_sortstate);
2636
2637 /*
2638 * Initialize BrinMemTuple we'll use to union summaries from workers (in
2639 * case they happened to produce parts of the same page range).
2640 */
2641 memtuple = brin_new_memtuple(state->bs_bdesc);
2642
2643 /*
2644 * Create a memory context we'll reset to combine results for a single
2645 * page range (received from the workers). We don't expect huge number of
2646 * overlaps under regular circumstances, because for large tables the
2647 * chunk size is likely larger than the BRIN page range), but it can
2648 * happen, and the union functions may do all kinds of stuff. So we better
2649 * reset the context once in a while.
2650 */
2652 "brin union",
2654 oldCxt = MemoryContextSwitchTo(rangeCxt);
2655
2656 /*
2657 * Read the BRIN tuples from the shared tuplesort, sorted by block number.
2658 * That probably gives us an index that is cheaper to scan, thanks to
2659 * mostly getting data from the same index page as before.
2660 */
2661 while ((btup = tuplesort_getbrintuple(state->bs_sortstate, &tuplen, true)) != NULL)
2662 {
2663 /* Ranges should be multiples of pages_per_range for the index. */
2664 Assert(btup->bt_blkno % state->bs_leader->brinshared->pagesPerRange == 0);
2665
2666 /*
2667 * Do we need to union summaries for the same page range?
2668 *
2669 * If this is the first brin tuple we read, then just deform it into
2670 * the memtuple, and continue with the next one from tuplesort. We
2671 * however may need to insert empty summaries into the index.
2672 *
2673 * If it's the same block as the last we saw, we simply union the brin
2674 * tuple into it, and we're done - we don't even need to insert empty
2675 * ranges, because that was done earlier when we saw the first brin
2676 * tuple (for this range).
2677 *
2678 * Finally, if it's not the first brin tuple, and it's not the same
2679 * page range, we need to do the insert and then deform the tuple into
2680 * the memtuple. Then we'll insert empty ranges before the new brin
2681 * tuple, if needed.
2682 */
2683 if (prevblkno == InvalidBlockNumber)
2684 {
2685 /* First brin tuples, just deform into memtuple. */
2686 memtuple = brin_deform_tuple(state->bs_bdesc, btup, memtuple);
2687
2688 /* continue to insert empty pages before thisblock */
2689 }
2690 else if (memtuple->bt_blkno == btup->bt_blkno)
2691 {
2692 /*
2693 * Not the first brin tuple, but same page range as the previous
2694 * one, so we can merge it into the memtuple.
2695 */
2696 union_tuples(state->bs_bdesc, memtuple, btup);
2697 continue;
2698 }
2699 else
2700 {
2701 BrinTuple *tmp;
2702 Size len;
2703
2704 /*
2705 * We got brin tuple for a different page range, so form a brin
2706 * tuple from the memtuple, insert it, and re-init the memtuple
2707 * from the new brin tuple.
2708 */
2709 tmp = brin_form_tuple(state->bs_bdesc, memtuple->bt_blkno,
2710 memtuple, &len);
2711
2712 brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
2713 &state->bs_currentInsertBuf, tmp->bt_blkno, tmp, len);
2714
2715 /*
2716 * Reset the per-output-range context. This frees all the memory
2717 * possibly allocated by the union functions, and also the BRIN
2718 * tuple we just formed and inserted.
2719 */
2720 MemoryContextReset(rangeCxt);
2721
2722 memtuple = brin_deform_tuple(state->bs_bdesc, btup, memtuple);
2723
2724 /* continue to insert empty pages before thisblock */
2725 }
2726
2727 /* Fill empty ranges for all ranges missing in the tuplesort. */
2728 brin_fill_empty_ranges(state, prevblkno, btup->bt_blkno);
2729
2730 prevblkno = btup->bt_blkno;
2731 }
2732
2733 tuplesort_end(state->bs_sortstate);
2734
2735 /* Fill the BRIN tuple for the last page range with data. */
2736 if (prevblkno != InvalidBlockNumber)
2737 {
2738 BrinTuple *tmp;
2739 Size len;
2740
2741 tmp = brin_form_tuple(state->bs_bdesc, memtuple->bt_blkno,
2742 memtuple, &len);
2743
2744 brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
2745 &state->bs_currentInsertBuf, tmp->bt_blkno, tmp, len);
2746
2747 pfree(tmp);
2748 }
2749
2750 /* Fill empty ranges at the end, for all ranges missing in the tuplesort. */
2751 brin_fill_empty_ranges(state, prevblkno, state->bs_maxRangeStart);
2752
2753 /*
2754 * Switch back to the original memory context, and destroy the one we
2755 * created to isolate the union_tuple calls.
2756 */
2757 MemoryContextSwitchTo(oldCxt);
2758 MemoryContextDelete(rangeCxt);
2759
2760 return reltuples;
2761}
uint32 BlockNumber
Definition: block.h:31
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:2032
static void brin_fill_empty_ranges(BrinBuildState *state, BlockNumber prevRange, BlockNumber nextRange)
Definition: brin.c:2994
static double _brin_parallel_heapscan(BrinBuildState *state)
Definition: brin.c:2580
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:342
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:99
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:482
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:553
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
void pfree(void *pointer)
Definition: mcxt.c:1524
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
const void size_t len
BlockNumber bt_blkno
Definition: brin_tuple.h:48
BlockNumber bt_blkno
Definition: brin_tuple.h:66
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1363
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:951
BrinTuple * tuplesort_getbrintuple(Tuplesortstate *state, Size *len, bool forward)

References _brin_parallel_heapscan(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), brin_deform_tuple(), brin_doinsert(), brin_fill_empty_ranges(), brin_form_tuple(), brin_new_memtuple(), BrinMemTuple::bt_blkno, BrinTuple::bt_blkno, CurrentMemoryContext, InvalidBlockNumber, len, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), pfree(), tuplesort_end(), tuplesort_getbrintuple(), tuplesort_performsort(), and union_tuples().

Referenced by brinbuild().

◆ _brin_parallel_scan_and_build()

static void _brin_parallel_scan_and_build ( BrinBuildState state,
BrinShared brinshared,
Sharedsort sharedsort,
Relation  heap,
Relation  index,
int  sortmem,
bool  progress 
)
static

Definition at line 2807 of file brin.c.

2811{
2812 SortCoordinate coordinate;
2813 TableScanDesc scan;
2814 double reltuples;
2815 IndexInfo *indexInfo;
2816
2817 /* Initialize local tuplesort coordination state */
2818 coordinate = palloc0(sizeof(SortCoordinateData));
2819 coordinate->isWorker = true;
2820 coordinate->nParticipants = -1;
2821 coordinate->sharedsort = sharedsort;
2822
2823 /* Begin "partial" tuplesort */
2824 state->bs_sortstate = tuplesort_begin_index_brin(sortmem, coordinate,
2826
2827 /* Join parallel scan */
2828 indexInfo = BuildIndexInfo(index);
2829 indexInfo->ii_Concurrent = brinshared->isconcurrent;
2830
2831 scan = table_beginscan_parallel(heap,
2833
2834 reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
2836
2837 /* insert the last item */
2839
2840 /* sort the BRIN ranges built by this worker */
2841 tuplesort_performsort(state->bs_sortstate);
2842
2843 state->bs_reltuples += reltuples;
2844
2845 /*
2846 * Done. Record ambuild statistics.
2847 */
2848 SpinLockAcquire(&brinshared->mutex);
2849 brinshared->nparticipantsdone++;
2850 brinshared->reltuples += state->bs_reltuples;
2851 brinshared->indtuples += state->bs_numtuples;
2852 SpinLockRelease(&brinshared->mutex);
2853
2854 /* Notify leader */
2856
2857 tuplesort_end(state->bs_sortstate);
2858}
static void form_and_spill_tuple(BrinBuildState *state)
Definition: brin.c:2007
static void brinbuildCallbackParallel(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:1046
void ConditionVariableSignal(ConditionVariable *cv)
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2428
bool ii_Concurrent
Definition: execnodes.h:214
Sharedsort * sharedsort
Definition: tuplesort.h:59
TableScanDesc table_beginscan_parallel(Relation relation, ParallelTableScanDesc pscan)
Definition: tableam.c:166
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)
Definition: tableam.h:1745
#define TUPLESORT_NONE
Definition: tuplesort.h:94
Tuplesortstate * tuplesort_begin_index_brin(int workMem, SortCoordinate coordinate, int sortopt)

References brinbuildCallbackParallel(), BuildIndexInfo(), ConditionVariableSignal(), form_and_spill_tuple(), IndexInfo::ii_Concurrent, BrinShared::indtuples, BrinShared::isconcurrent, SortCoordinateData::isWorker, BrinShared::mutex, SortCoordinateData::nParticipants, BrinShared::nparticipantsdone, palloc0(), ParallelTableScanFromBrinShared, BrinShared::reltuples, SortCoordinateData::sharedsort, SpinLockAcquire, SpinLockRelease, table_beginscan_parallel(), table_index_build_scan(), tuplesort_begin_index_brin(), tuplesort_end(), TUPLESORT_NONE, tuplesort_performsort(), and BrinShared::workersdonecv.

Referenced by _brin_leader_participate_as_worker(), and _brin_parallel_build_main().

◆ add_values_to_range()

static bool add_values_to_range ( Relation  idxRel,
BrinDesc bdesc,
BrinMemTuple dtup,
const Datum values,
const bool *  nulls 
)
static

Definition at line 2206 of file brin.c.

2208{
2209 int keyno;
2210
2211 /* If the range starts empty, we're certainly going to modify it. */
2212 bool modified = dtup->bt_empty_range;
2213
2214 /*
2215 * Compare the key values of the new tuple to the stored index values; our
2216 * deformed tuple will get updated if the new tuple doesn't fit the
2217 * original range (note this means we can't break out of the loop early).
2218 * Make a note of whether this happens, so that we know to insert the
2219 * modified tuple later.
2220 */
2221 for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
2222 {
2223 Datum result;
2224 BrinValues *bval;
2225 FmgrInfo *addValue;
2226 bool has_nulls;
2227
2228 bval = &dtup->bt_columns[keyno];
2229
2230 /*
2231 * Does the range have actual NULL values? Either of the flags can be
2232 * set, but we ignore the state before adding first row.
2233 *
2234 * We have to remember this, because we'll modify the flags and we
2235 * need to know if the range started as empty.
2236 */
2237 has_nulls = ((!dtup->bt_empty_range) &&
2238 (bval->bv_hasnulls || bval->bv_allnulls));
2239
2240 /*
2241 * If the value we're adding is NULL, handle it locally. Otherwise
2242 * call the BRIN_PROCNUM_ADDVALUE procedure.
2243 */
2244 if (bdesc->bd_info[keyno]->oi_regular_nulls && nulls[keyno])
2245 {
2246 /*
2247 * If the new value is null, we record that we saw it if it's the
2248 * first one; otherwise, there's nothing to do.
2249 */
2250 if (!bval->bv_hasnulls)
2251 {
2252 bval->bv_hasnulls = true;
2253 modified = true;
2254 }
2255
2256 continue;
2257 }
2258
2259 addValue = index_getprocinfo(idxRel, keyno + 1,
2261 result = FunctionCall4Coll(addValue,
2262 idxRel->rd_indcollation[keyno],
2263 PointerGetDatum(bdesc),
2264 PointerGetDatum(bval),
2265 values[keyno],
2266 nulls[keyno]);
2267 /* if that returned true, we need to insert the updated tuple */
2268 modified |= DatumGetBool(result);
2269
2270 /*
2271 * If the range was had actual NULL values (i.e. did not start empty),
2272 * make sure we don't forget about the NULL values. Either the
2273 * allnulls flag is still set to true, or (if the opclass cleared it)
2274 * we need to set hasnulls=true.
2275 *
2276 * XXX This can only happen when the opclass modified the tuple, so
2277 * the modified flag should be set.
2278 */
2279 if (has_nulls && !(bval->bv_hasnulls || bval->bv_allnulls))
2280 {
2281 Assert(modified);
2282 bval->bv_hasnulls = true;
2283 }
2284 }
2285
2286 /*
2287 * After updating summaries for all the keys, mark it as not empty.
2288 *
2289 * If we're actually changing the flag value (i.e. tuple started as
2290 * empty), we should have modified the tuple. So we should not see empty
2291 * range that was not modified.
2292 */
2293 Assert(!dtup->bt_empty_range || modified);
2294 dtup->bt_empty_range = false;
2295
2296 return modified;
2297}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:71
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1196
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:906
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:55
bool bt_empty_range
Definition: brin_tuple.h:47
bool oi_regular_nulls
Definition: brin_internal.h:31
bool bv_hasnulls
Definition: brin_tuple.h:32
bool bv_allnulls
Definition: brin_tuple.h:33
Definition: fmgr.h:57
Oid * rd_indcollation
Definition: rel.h:217

References Assert(), BrinDesc::bd_info, BrinDesc::bd_tupdesc, BRIN_PROCNUM_ADDVALUE, BrinMemTuple::bt_columns, BrinMemTuple::bt_empty_range, BrinValues::bv_allnulls, BrinValues::bv_hasnulls, DatumGetBool(), FunctionCall4Coll(), index_getprocinfo(), TupleDescData::natts, BrinOpcInfo::oi_regular_nulls, PointerGetDatum(), RelationData::rd_indcollation, and values.

Referenced by brinbuildCallback(), brinbuildCallbackParallel(), and brininsert().

◆ brin_build_desc()

BrinDesc * brin_build_desc ( Relation  rel)

Definition at line 1582 of file brin.c.

1583{
1584 BrinOpcInfo **opcinfo;
1585 BrinDesc *bdesc;
1586 TupleDesc tupdesc;
1587 int totalstored = 0;
1588 int keyno;
1589 long totalsize;
1590 MemoryContext cxt;
1591 MemoryContext oldcxt;
1592
1594 "brin desc cxt",
1596 oldcxt = MemoryContextSwitchTo(cxt);
1597 tupdesc = RelationGetDescr(rel);
1598
1599 /*
1600 * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1601 * the number of columns stored, since the number is opclass-defined.
1602 */
1603 opcinfo = palloc_array(BrinOpcInfo *, tupdesc->natts);
1604 for (keyno = 0; keyno < tupdesc->natts; keyno++)
1605 {
1606 FmgrInfo *opcInfoFn;
1607 Form_pg_attribute attr = TupleDescAttr(tupdesc, keyno);
1608
1609 opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1610
1611 opcinfo[keyno] = (BrinOpcInfo *)
1612 DatumGetPointer(FunctionCall1(opcInfoFn, attr->atttypid));
1613 totalstored += opcinfo[keyno]->oi_nstored;
1614 }
1615
1616 /* Allocate our result struct and fill it in */
1617 totalsize = offsetof(BrinDesc, bd_info) +
1618 sizeof(BrinOpcInfo *) * tupdesc->natts;
1619
1620 bdesc = palloc(totalsize);
1621 bdesc->bd_context = cxt;
1622 bdesc->bd_index = rel;
1623 bdesc->bd_tupdesc = tupdesc;
1624 bdesc->bd_disktdesc = NULL; /* generated lazily */
1625 bdesc->bd_totalstored = totalstored;
1626
1627 for (keyno = 0; keyno < tupdesc->natts; keyno++)
1628 bdesc->bd_info[keyno] = opcinfo[keyno];
1629 pfree(opcinfo);
1630
1631 MemoryContextSwitchTo(oldcxt);
1632
1633 return bdesc;
1634}
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:70
#define palloc_array(type, count)
Definition: fe_memutils.h:76
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:700
void * palloc(Size size)
Definition: mcxt.c:1317
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
#define RelationGetDescr(relation)
Definition: rel.h:539
int bd_totalstored
Definition: brin_internal.h:59
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bd_context
Definition: brin_internal.h:47
TupleDesc bd_disktdesc
Definition: brin_internal.h:56
uint16 oi_nstored
Definition: brin_internal.h:28
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:154

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, BrinDesc::bd_context, BrinDesc::bd_disktdesc, BrinDesc::bd_index, BrinDesc::bd_info, BrinDesc::bd_totalstored, BrinDesc::bd_tupdesc, BRIN_PROCNUM_OPCINFO, CurrentMemoryContext, DatumGetPointer(), FunctionCall1, index_getprocinfo(), MemoryContextSwitchTo(), TupleDescData::natts, BrinOpcInfo::oi_nstored, palloc(), palloc_array, pfree(), RelationGetDescr, and TupleDescAttr().

Referenced by brin_page_items(), brinbeginscan(), initialize_brin_buildstate(), and initialize_brin_insertstate().

◆ brin_build_empty_tuple()

static void brin_build_empty_tuple ( BrinBuildState state,
BlockNumber  blkno 
)
static

Definition at line 2957 of file brin.c.

2958{
2959 /* First time an empty tuple is requested? If yes, initialize it. */
2960 if (state->bs_emptyTuple == NULL)
2961 {
2962 MemoryContext oldcxt;
2963 BrinMemTuple *dtuple = brin_new_memtuple(state->bs_bdesc);
2964
2965 /* Allocate the tuple in context for the whole index build. */
2966 oldcxt = MemoryContextSwitchTo(state->bs_context);
2967
2968 state->bs_emptyTuple = brin_form_tuple(state->bs_bdesc, blkno, dtuple,
2969 &state->bs_emptyTupleLen);
2970
2971 MemoryContextSwitchTo(oldcxt);
2972 }
2973 else
2974 {
2975 /* If we already have an empty tuple, just update the block. */
2976 state->bs_emptyTuple->bt_blkno = blkno;
2977 }
2978}

References brin_form_tuple(), brin_new_memtuple(), and MemoryContextSwitchTo().

Referenced by brin_fill_empty_ranges().

◆ brin_desummarize_range()

Datum brin_desummarize_range ( PG_FUNCTION_ARGS  )

Definition at line 1492 of file brin.c.

1493{
1494 Oid indexoid = PG_GETARG_OID(0);
1495 int64 heapBlk64 = PG_GETARG_INT64(1);
1496 BlockNumber heapBlk;
1497 Oid heapoid;
1498 Relation heapRel;
1499 Relation indexRel;
1500 bool done;
1501
1502 if (RecoveryInProgress())
1503 ereport(ERROR,
1504 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1505 errmsg("recovery is in progress"),
1506 errhint("BRIN control functions cannot be executed during recovery.")));
1507
1508 if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
1509 ereport(ERROR,
1510 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1511 errmsg("block number out of range: %lld",
1512 (long long) heapBlk64)));
1513 heapBlk = (BlockNumber) heapBlk64;
1514
1515 /*
1516 * We must lock table before index to avoid deadlocks. However, if the
1517 * passed indexoid isn't an index then IndexGetRelation() will fail.
1518 * Rather than emitting a not-very-helpful error message, postpone
1519 * complaining, expecting that the is-it-an-index test below will fail.
1520 *
1521 * Unlike brin_summarize_range(), autovacuum never calls this. Hence, we
1522 * don't switch userid.
1523 */
1524 heapoid = IndexGetRelation(indexoid, true);
1525 if (OidIsValid(heapoid))
1526 heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1527 else
1528 heapRel = NULL;
1529
1530 indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1531
1532 /* Must be a BRIN index */
1533 if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1534 indexRel->rd_rel->relam != BRIN_AM_OID)
1535 ereport(ERROR,
1536 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1537 errmsg("\"%s\" is not a BRIN index",
1538 RelationGetRelationName(indexRel))));
1539
1540 /* User must own the index (comparable to privileges needed for VACUUM) */
1541 if (!object_ownercheck(RelationRelationId, indexoid, GetUserId()))
1543 RelationGetRelationName(indexRel));
1544
1545 /*
1546 * Since we did the IndexGetRelation call above without any lock, it's
1547 * barely possible that a race against an index drop/recreation could have
1548 * netted us the wrong table. Recheck.
1549 */
1550 if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1551 ereport(ERROR,
1553 errmsg("could not open parent table of index \"%s\"",
1554 RelationGetRelationName(indexRel))));
1555
1556 /* see gin_clean_pending_list() */
1557 if (indexRel->rd_index->indisvalid)
1558 {
1559 /* the revmap does the hard work */
1560 do
1561 {
1562 done = brinRevmapDesummarizeRange(indexRel, heapBlk);
1563 }
1564 while (!done);
1565 }
1566 else
1568 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1569 errmsg("index \"%s\" is not valid",
1570 RelationGetRelationName(indexRel))));
1571
1574
1576}
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4058
#define MaxBlockNumber
Definition: block.h:35
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:323
int64_t int64
Definition: c.h:499
#define OidIsValid(objectId)
Definition: c.h:746
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3583
Oid GetUserId(void)
Definition: miscinit.c:520
@ OBJECT_INDEX
Definition: parsenodes.h:2337
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:79
unsigned int Oid
Definition: postgres_ext.h:30
#define RelationGetRelationName(relation)
Definition: rel.h:547
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Form_pg_index rd_index
Definition: rel.h:192
Form_pg_class rd_rel
Definition: rel.h:111
bool RecoveryInProgress(void)
Definition: xlog.c:6382

References aclcheck_error(), ACLCHECK_NOT_OWNER, brinRevmapDesummarizeRange(), DEBUG1, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errhint(), errmsg(), ERROR, GetUserId(), index_open(), IndexGetRelation(), MaxBlockNumber, OBJECT_INDEX, object_ownercheck(), OidIsValid, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_VOID, RelationData::rd_index, RelationData::rd_rel, RecoveryInProgress(), relation_close(), RelationGetRelationName, ShareUpdateExclusiveLock, and table_open().

◆ brin_fill_empty_ranges()

static void brin_fill_empty_ranges ( BrinBuildState state,
BlockNumber  prevRange,
BlockNumber  nextRange 
)
static

Definition at line 2994 of file brin.c.

2996{
2997 BlockNumber blkno;
2998
2999 /*
3000 * If we already summarized some ranges, we need to start with the next
3001 * one. Otherwise start from the first range of the table.
3002 */
3003 blkno = (prevRange == InvalidBlockNumber) ? 0 : (prevRange + state->bs_pagesPerRange);
3004
3005 /* Generate empty ranges until we hit the next non-empty range. */
3006 while (blkno < nextRange)
3007 {
3008 /* Did we already build the empty tuple? If not, do it now. */
3010
3011 brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
3012 &state->bs_currentInsertBuf,
3013 blkno, state->bs_emptyTuple, state->bs_emptyTupleLen);
3014
3015 /* try next page range */
3016 blkno += state->bs_pagesPerRange;
3017 }
3018}
static void brin_build_empty_tuple(BrinBuildState *state, BlockNumber blkno)
Definition: brin.c:2957

References brin_build_empty_tuple(), brin_doinsert(), and InvalidBlockNumber.

Referenced by _brin_parallel_merge(), and brinbuild().

◆ brin_free_desc()

void brin_free_desc ( BrinDesc bdesc)

Definition at line 1637 of file brin.c.

1638{
1639 /* make sure the tupdesc is still valid */
1640 Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1641 /* no need for retail pfree */
1643}
int tdrefcount
Definition: tupdesc.h:134

References Assert(), BrinDesc::bd_context, BrinDesc::bd_tupdesc, MemoryContextDelete(), and TupleDescData::tdrefcount.

Referenced by brin_page_items(), brinendscan(), and terminate_brin_buildstate().

◆ brin_summarize_new_values()

Datum brin_summarize_new_values ( PG_FUNCTION_ARGS  )

Definition at line 1366 of file brin.c.

1367{
1368 Datum relation = PG_GETARG_DATUM(0);
1369
1371 relation,
1373}
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:1381
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:209
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268

References BRIN_ALL_BLOCKRANGES, brin_summarize_range(), DirectFunctionCall2, Int64GetDatum(), and PG_GETARG_DATUM.

◆ brin_summarize_range()

Datum brin_summarize_range ( PG_FUNCTION_ARGS  )

Definition at line 1381 of file brin.c.

1382{
1383 Oid indexoid = PG_GETARG_OID(0);
1384 int64 heapBlk64 = PG_GETARG_INT64(1);
1385 BlockNumber heapBlk;
1386 Oid heapoid;
1387 Relation indexRel;
1388 Relation heapRel;
1389 Oid save_userid;
1390 int save_sec_context;
1391 int save_nestlevel;
1392 double numSummarized = 0;
1393
1394 if (RecoveryInProgress())
1395 ereport(ERROR,
1396 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1397 errmsg("recovery is in progress"),
1398 errhint("BRIN control functions cannot be executed during recovery.")));
1399
1400 if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
1401 ereport(ERROR,
1402 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1403 errmsg("block number out of range: %lld",
1404 (long long) heapBlk64)));
1405 heapBlk = (BlockNumber) heapBlk64;
1406
1407 /*
1408 * We must lock table before index to avoid deadlocks. However, if the
1409 * passed indexoid isn't an index then IndexGetRelation() will fail.
1410 * Rather than emitting a not-very-helpful error message, postpone
1411 * complaining, expecting that the is-it-an-index test below will fail.
1412 */
1413 heapoid = IndexGetRelation(indexoid, true);
1414 if (OidIsValid(heapoid))
1415 {
1416 heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1417
1418 /*
1419 * Autovacuum calls us. For its benefit, switch to the table owner's
1420 * userid, so that any index functions are run as that user. Also
1421 * lock down security-restricted operations and arrange to make GUC
1422 * variable changes local to this command. This is harmless, albeit
1423 * unnecessary, when called from SQL, because we fail shortly if the
1424 * user does not own the index.
1425 */
1426 GetUserIdAndSecContext(&save_userid, &save_sec_context);
1427 SetUserIdAndSecContext(heapRel->rd_rel->relowner,
1428 save_sec_context | SECURITY_RESTRICTED_OPERATION);
1429 save_nestlevel = NewGUCNestLevel();
1431 }
1432 else
1433 {
1434 heapRel = NULL;
1435 /* Set these just to suppress "uninitialized variable" warnings */
1436 save_userid = InvalidOid;
1437 save_sec_context = -1;
1438 save_nestlevel = -1;
1439 }
1440
1441 indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1442
1443 /* Must be a BRIN index */
1444 if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1445 indexRel->rd_rel->relam != BRIN_AM_OID)
1446 ereport(ERROR,
1447 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1448 errmsg("\"%s\" is not a BRIN index",
1449 RelationGetRelationName(indexRel))));
1450
1451 /* User must own the index (comparable to privileges needed for VACUUM) */
1452 if (heapRel != NULL && !object_ownercheck(RelationRelationId, indexoid, save_userid))
1454 RelationGetRelationName(indexRel));
1455
1456 /*
1457 * Since we did the IndexGetRelation call above without any lock, it's
1458 * barely possible that a race against an index drop/recreation could have
1459 * netted us the wrong table. Recheck.
1460 */
1461 if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1462 ereport(ERROR,
1464 errmsg("could not open parent table of index \"%s\"",
1465 RelationGetRelationName(indexRel))));
1466
1467 /* see gin_clean_pending_list() */
1468 if (indexRel->rd_index->indisvalid)
1469 brinsummarize(indexRel, heapRel, heapBlk, true, &numSummarized, NULL);
1470 else
1472 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1473 errmsg("index \"%s\" is not valid",
1474 RelationGetRelationName(indexRel))));
1475
1476 /* Roll back any GUC changes executed by index functions */
1477 AtEOXact_GUC(false, save_nestlevel);
1478
1479 /* Restore userid and security context */
1480 SetUserIdAndSecContext(save_userid, save_sec_context);
1481
1484
1485 PG_RETURN_INT32((int32) numSummarized);
1486}
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1888
int32_t int32
Definition: c.h:498
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int NewGUCNestLevel(void)
Definition: guc.c:2235
void RestrictSearchPath(void)
Definition: guc.c:2246
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:318
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:663
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:670
#define InvalidOid
Definition: postgres_ext.h:35

References aclcheck_error(), ACLCHECK_NOT_OWNER, AtEOXact_GUC(), BRIN_ALL_BLOCKRANGES, brinsummarize(), DEBUG1, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errhint(), errmsg(), ERROR, GetUserIdAndSecContext(), index_open(), IndexGetRelation(), InvalidOid, NewGUCNestLevel(), OBJECT_INDEX, object_ownercheck(), OidIsValid, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_INT32, RelationData::rd_index, RelationData::rd_rel, RecoveryInProgress(), relation_close(), RelationGetRelationName, RestrictSearchPath(), SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, and table_open().

Referenced by brin_summarize_new_values(), and perform_work_item().

◆ brin_vacuum_scan()

static void brin_vacuum_scan ( Relation  idxrel,
BufferAccessStrategy  strategy 
)
static

Definition at line 2173 of file brin.c.

2174{
2175 BlockNumber nblocks;
2176 BlockNumber blkno;
2177
2178 /*
2179 * Scan the index in physical order, and clean up any possible mess in
2180 * each page.
2181 */
2182 nblocks = RelationGetNumberOfBlocks(idxrel);
2183 for (blkno = 0; blkno < nblocks; blkno++)
2184 {
2185 Buffer buf;
2186
2188
2189 buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
2190 RBM_NORMAL, strategy);
2191
2192 brin_page_cleanup(idxrel, buf);
2193
2195 }
2196
2197 /*
2198 * Update all upper pages in the index's FSM, as well. This ensures not
2199 * only that we propagate leaf-page FSM updates made by brin_page_cleanup,
2200 * but also that any pre-existing damage or out-of-dateness is repaired.
2201 */
2202 FreeSpaceMapVacuum(idxrel);
2203}
void brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:624
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4917
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:798
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:274
@ RBM_NORMAL
Definition: bufmgr.h:45
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:358
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static char * buf
Definition: pg_test_fsync.c:72
@ MAIN_FORKNUM
Definition: relpath.h:58

References brin_page_cleanup(), buf, CHECK_FOR_INTERRUPTS, FreeSpaceMapVacuum(), MAIN_FORKNUM, RBM_NORMAL, ReadBufferExtended(), RelationGetNumberOfBlocks, and ReleaseBuffer().

Referenced by brinvacuumcleanup().

◆ brinbeginscan()

IndexScanDesc brinbeginscan ( Relation  r,
int  nkeys,
int  norderbys 
)

Definition at line 539 of file brin.c.

540{
541 IndexScanDesc scan;
542 BrinOpaque *opaque;
543
544 scan = RelationGetIndexScan(r, nkeys, norderbys);
545
546 opaque = palloc_object(BrinOpaque);
548 opaque->bo_bdesc = brin_build_desc(r);
549 scan->opaque = opaque;
550
551 return scan;
552}
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1582
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange)
Definition: brin_revmap.c:70
#define palloc_object(type)
Definition: fe_memutils.h:74
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:80
BlockNumber bo_pagesPerRange
Definition: brin.c:204
BrinDesc * bo_bdesc
Definition: brin.c:206
BrinRevmap * bo_rmAccess
Definition: brin.c:205

References BrinOpaque::bo_bdesc, BrinOpaque::bo_pagesPerRange, BrinOpaque::bo_rmAccess, brin_build_desc(), brinRevmapInitialize(), IndexScanDescData::opaque, palloc_object, and RelationGetIndexScan().

Referenced by brinhandler().

◆ brinbuild()

IndexBuildResult * brinbuild ( Relation  heap,
Relation  index,
IndexInfo indexInfo 
)

Definition at line 1105 of file brin.c.

1106{
1107 IndexBuildResult *result;
1108 double reltuples;
1109 double idxtuples;
1110 BrinRevmap *revmap;
1112 Buffer meta;
1113 BlockNumber pagesPerRange;
1114
1115 /*
1116 * We expect to be called exactly once for any index relation.
1117 */
1119 elog(ERROR, "index \"%s\" already contains data",
1121
1122 /*
1123 * Critical section not required, because on error the creation of the
1124 * whole relation will be rolled back.
1125 */
1126
1130
1133 MarkBufferDirty(meta);
1134
1136 {
1137 xl_brin_createidx xlrec;
1138 XLogRecPtr recptr;
1139 Page page;
1140
1143
1147
1148 recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
1149
1150 page = BufferGetPage(meta);
1151 PageSetLSN(page, recptr);
1152 }
1153
1154 UnlockReleaseBuffer(meta);
1155
1156 /*
1157 * Initialize our state, including the deformed tuple state.
1158 */
1159 revmap = brinRevmapInitialize(index, &pagesPerRange);
1160 state = initialize_brin_buildstate(index, revmap, pagesPerRange,
1162
1163 /*
1164 * Attempt to launch parallel worker scan when required
1165 *
1166 * XXX plan_create_index_workers makes the number of workers dependent on
1167 * maintenance_work_mem, requiring 32MB for each worker. That makes sense
1168 * for btree, but not for BRIN, which can do with much less memory. So
1169 * maybe make that somehow less strict, optionally?
1170 */
1171 if (indexInfo->ii_ParallelWorkers > 0)
1172 _brin_begin_parallel(state, heap, index, indexInfo->ii_Concurrent,
1173 indexInfo->ii_ParallelWorkers);
1174
1175 /*
1176 * If parallel build requested and at least one worker process was
1177 * successfully launched, set up coordination state, wait for workers to
1178 * complete. Then read all tuples from the shared tuplesort and insert
1179 * them into the index.
1180 *
1181 * In serial mode, simply scan the table and build the index one index
1182 * tuple at a time.
1183 */
1184 if (state->bs_leader)
1185 {
1186 SortCoordinate coordinate;
1187
1188 coordinate = (SortCoordinate) palloc0(sizeof(SortCoordinateData));
1189 coordinate->isWorker = false;
1190 coordinate->nParticipants =
1191 state->bs_leader->nparticipanttuplesorts;
1192 coordinate->sharedsort = state->bs_leader->sharedsort;
1193
1194 /*
1195 * Begin leader tuplesort.
1196 *
1197 * In cases where parallelism is involved, the leader receives the
1198 * same share of maintenance_work_mem as a serial sort (it is
1199 * generally treated in the same way as a serial sort once we return).
1200 * Parallel worker Tuplesortstates will have received only a fraction
1201 * of maintenance_work_mem, though.
1202 *
1203 * We rely on the lifetime of the Leader Tuplesortstate almost not
1204 * overlapping with any worker Tuplesortstate's lifetime. There may
1205 * be some small overlap, but that's okay because we rely on leader
1206 * Tuplesortstate only allocating a small, fixed amount of memory
1207 * here. When its tuplesort_performsort() is called (by our caller),
1208 * and significant amounts of memory are likely to be used, all
1209 * workers must have already freed almost all memory held by their
1210 * Tuplesortstates (they are about to go away completely, too). The
1211 * overall effect is that maintenance_work_mem always represents an
1212 * absolute high watermark on the amount of memory used by a CREATE
1213 * INDEX operation, regardless of the use of parallelism or any other
1214 * factor.
1215 */
1216 state->bs_sortstate =
1219
1220 /* scan the relation and merge per-worker results */
1221 reltuples = _brin_parallel_merge(state);
1222
1223 _brin_end_parallel(state->bs_leader, state);
1224 }
1225 else /* no parallel index build */
1226 {
1227 /*
1228 * Now scan the relation. No syncscan allowed here because we want
1229 * the heap blocks in physical order (we want to produce the ranges
1230 * starting from block 0, and the callback also relies on this to not
1231 * generate summary for the same range twice).
1232 */
1233 reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
1234 brinbuildCallback, state, NULL);
1235
1236 /*
1237 * process the final batch
1238 *
1239 * XXX Note this does not update state->bs_currRangeStart, i.e. it
1240 * stays set to the last range added to the index. This is OK, because
1241 * that's what brin_fill_empty_ranges expects.
1242 */
1244
1245 /*
1246 * Backfill the final ranges with empty data.
1247 *
1248 * This saves us from doing what amounts to full table scans when the
1249 * index with a predicate like WHERE (nonnull_column IS NULL), or
1250 * other very selective predicates.
1251 */
1253 state->bs_currRangeStart,
1254 state->bs_maxRangeStart);
1255 }
1256
1257 /* release resources */
1258 idxtuples = state->bs_numtuples;
1259 brinRevmapTerminate(state->bs_rmAccess);
1261
1262 /*
1263 * Return statistics
1264 */
1266
1267 result->heap_tuples = reltuples;
1268 result->index_tuples = idxtuples;
1269
1270 return result;
1271}
static double _brin_parallel_merge(BrinBuildState *state)
Definition: brin.c:2621
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1717
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1986
static void _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, bool isconcurrent, int request)
Definition: brin.c:2364
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:995
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:486
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:100
#define SizeOfBrinCreateIdx
Definition: brin_xlog.h:55
#define XLOG_BRIN_CREATE_INDEX
Definition: brin_xlog.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3795
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:851
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4934
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2596
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:401
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:74
@ EB_LOCK_FIRST
Definition: bufmgr.h:86
#define BMR_REL(p_rel)
Definition: bufmgr.h:107
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:391
PageData * Page
Definition: bufpage.h:82
#define elog(elevel,...)
Definition: elog.h:225
#define RelationNeedsWAL(relation)
Definition: rel.h:636
double heap_tuples
Definition: genam.h:55
double index_tuples
Definition: genam.h:56
int ii_ParallelWorkers
Definition: execnodes.h:218
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
struct SortCoordinateData * SortCoordinate
Definition: tuplesort.h:62
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogRegisterData(const void *data, uint32 len)
Definition: xloginsert.c:364
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:242
void XLogBeginInsert(void)
Definition: xloginsert.c:149
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define REGBUF_WILL_INIT
Definition: xloginsert.h:34

References _brin_begin_parallel(), _brin_end_parallel(), _brin_parallel_merge(), Assert(), BMR_REL, BRIN_CURRENT_VERSION, brin_fill_empty_ranges(), BRIN_METAPAGE_BLKNO, brin_metapage_init(), brinbuildCallback(), BrinGetPagesPerRange, brinRevmapInitialize(), brinRevmapTerminate(), BufferGetBlockNumber(), BufferGetPage(), EB_LOCK_FIRST, EB_SKIP_EXTENSION_LOCK, elog, ERROR, ExtendBufferedRel(), form_and_insert_tuple(), IndexBuildResult::heap_tuples, IndexInfo::ii_Concurrent, IndexInfo::ii_ParallelWorkers, IndexBuildResult::index_tuples, initialize_brin_buildstate(), SortCoordinateData::isWorker, MAIN_FORKNUM, maintenance_work_mem, MarkBufferDirty(), SortCoordinateData::nParticipants, PageSetLSN(), xl_brin_createidx::pagesPerRange, palloc0(), palloc_object, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, SortCoordinateData::sharedsort, SizeOfBrinCreateIdx, table_index_build_scan(), terminate_brin_buildstate(), tuplesort_begin_index_brin(), TUPLESORT_NONE, UnlockReleaseBuffer(), xl_brin_createidx::version, XLOG_BRIN_CREATE_INDEX, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by brinhandler().

◆ brinbuildCallback()

static void brinbuildCallback ( Relation  index,
ItemPointer  tid,
Datum values,
bool *  isnull,
bool  tupleIsAlive,
void *  brstate 
)
static

Definition at line 995 of file brin.c.

1001{
1002 BrinBuildState *state = (BrinBuildState *) brstate;
1003 BlockNumber thisblock;
1004
1005 thisblock = ItemPointerGetBlockNumber(tid);
1006
1007 /*
1008 * If we're in a block that belongs to a future range, summarize what
1009 * we've got and start afresh. Note the scan might have skipped many
1010 * pages, if they were devoid of live tuples; make sure to insert index
1011 * tuples for those too.
1012 */
1013 while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
1014 {
1015
1017 "brinbuildCallback: completed a range: %u--%u",
1018 state->bs_currRangeStart,
1019 state->bs_currRangeStart + state->bs_pagesPerRange));
1020
1021 /* create the index tuple and insert it */
1023
1024 /* set state to correspond to the next range */
1025 state->bs_currRangeStart += state->bs_pagesPerRange;
1026
1027 /* re-initialize state for it */
1028 brin_memtuple_initialize(state->bs_dtuple, state->bs_bdesc);
1029 }
1030
1031 /* Accumulate the current tuple into the running state */
1032 (void) add_values_to_range(index, state->bs_bdesc, state->bs_dtuple,
1033 values, isnull);
1034}
static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, const Datum *values, const bool *nulls)
Definition: brin.c:2206
#define BRIN_elog(args)
Definition: brin_internal.h:85
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:511
#define DEBUG2
Definition: elog.h:29
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103

References add_values_to_range(), BRIN_elog, brin_memtuple_initialize(), DEBUG2, form_and_insert_tuple(), ItemPointerGetBlockNumber(), and values.

Referenced by brinbuild(), and summarize_range().

◆ brinbuildCallbackParallel()

static void brinbuildCallbackParallel ( Relation  index,
ItemPointer  tid,
Datum values,
bool *  isnull,
bool  tupleIsAlive,
void *  brstate 
)
static

Definition at line 1046 of file brin.c.

1052{
1053 BrinBuildState *state = (BrinBuildState *) brstate;
1054 BlockNumber thisblock;
1055
1056 thisblock = ItemPointerGetBlockNumber(tid);
1057
1058 /*
1059 * If we're in a block that belongs to a different range, summarize what
1060 * we've got and start afresh. Note the scan might have skipped many
1061 * pages, if they were devoid of live tuples; we do not create empty BRIN
1062 * ranges here - the leader is responsible for filling them in.
1063 *
1064 * Unlike serial builds, parallel index builds allow synchronized seqscans
1065 * (because that's what parallel scans do). This means the block may wrap
1066 * around to the beginning of the relation, so the condition needs to
1067 * check for both future and past ranges.
1068 */
1069 if ((thisblock < state->bs_currRangeStart) ||
1070 (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1))
1071 {
1072
1074 "brinbuildCallbackParallel: completed a range: %u--%u",
1075 state->bs_currRangeStart,
1076 state->bs_currRangeStart + state->bs_pagesPerRange));
1077
1078 /* create the index tuple and write it into the tuplesort */
1080
1081 /*
1082 * Set state to correspond to the next range (for this block).
1083 *
1084 * This skips ranges that are either empty (and so we don't get any
1085 * tuples to summarize), or processed by other workers. We can't
1086 * differentiate those cases here easily, so we leave it up to the
1087 * leader to fill empty ranges where needed.
1088 */
1089 state->bs_currRangeStart
1090 = state->bs_pagesPerRange * (thisblock / state->bs_pagesPerRange);
1091
1092 /* re-initialize state for it */
1093 brin_memtuple_initialize(state->bs_dtuple, state->bs_bdesc);
1094 }
1095
1096 /* Accumulate the current tuple into the running state */
1097 (void) add_values_to_range(index, state->bs_bdesc, state->bs_dtuple,
1098 values, isnull);
1099}

References add_values_to_range(), BRIN_elog, brin_memtuple_initialize(), DEBUG2, form_and_spill_tuple(), ItemPointerGetBlockNumber(), and values.

Referenced by _brin_parallel_scan_and_build().

◆ brinbuildempty()

void brinbuildempty ( Relation  index)

Definition at line 1274 of file brin.c.

1275{
1276 Buffer metabuf;
1277
1278 /* An empty BRIN index has a metapage only. */
1279 metabuf = ExtendBufferedRel(BMR_REL(index), INIT_FORKNUM, NULL,
1281
1282 /* Initialize and xlog metabuffer. */
1286 MarkBufferDirty(metabuf);
1287 log_newpage_buffer(metabuf, true);
1289
1290 UnlockReleaseBuffer(metabuf);
1291}
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
@ INIT_FORKNUM
Definition: relpath.h:61
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1237

References BMR_REL, BRIN_CURRENT_VERSION, brin_metapage_init(), BrinGetPagesPerRange, BufferGetPage(), EB_LOCK_FIRST, EB_SKIP_EXTENSION_LOCK, END_CRIT_SECTION, ExtendBufferedRel(), INIT_FORKNUM, log_newpage_buffer(), MarkBufferDirty(), START_CRIT_SECTION, and UnlockReleaseBuffer().

Referenced by brinhandler().

◆ brinbulkdelete()

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

Definition at line 1303 of file brin.c.

1305{
1306 /* allocate stats if first time through, else re-use existing struct */
1307 if (stats == NULL)
1309
1310 return stats;
1311}
#define palloc0_object(type)
Definition: fe_memutils.h:75

References palloc0_object.

Referenced by brinhandler().

◆ brinendscan()

void brinendscan ( IndexScanDesc  scan)

Definition at line 978 of file brin.c.

979{
980 BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
981
983 brin_free_desc(opaque->bo_bdesc);
984 pfree(opaque);
985}
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1637

References BrinOpaque::bo_bdesc, BrinOpaque::bo_rmAccess, brin_free_desc(), brinRevmapTerminate(), IndexScanDescData::opaque, and pfree().

Referenced by brinhandler().

◆ bringetbitmap()

int64 bringetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 567 of file brin.c.

568{
569 Relation idxRel = scan->indexRelation;
571 BrinDesc *bdesc;
572 Oid heapOid;
573 Relation heapRel;
574 BrinOpaque *opaque;
575 BlockNumber nblocks;
576 BlockNumber heapBlk;
577 int64 totalpages = 0;
578 FmgrInfo *consistentFn;
579 MemoryContext oldcxt;
580 MemoryContext perRangeCxt;
581 BrinMemTuple *dtup;
582 BrinTuple *btup = NULL;
583 Size btupsz = 0;
584 ScanKey **keys,
585 **nullkeys;
586 int *nkeys,
587 *nnullkeys;
588 char *ptr;
589 Size len;
590 char *tmp PG_USED_FOR_ASSERTS_ONLY;
591
592 opaque = (BrinOpaque *) scan->opaque;
593 bdesc = opaque->bo_bdesc;
595 if (scan->instrument)
596 scan->instrument->nsearches++;
597
598 /*
599 * We need to know the size of the table so that we know how long to
600 * iterate on the revmap.
601 */
602 heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
603 heapRel = table_open(heapOid, AccessShareLock);
604 nblocks = RelationGetNumberOfBlocks(heapRel);
606
607 /*
608 * Make room for the consistent support procedures of indexed columns. We
609 * don't look them up here; we do that lazily the first time we see a scan
610 * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
611 */
612 consistentFn = palloc0_array(FmgrInfo, bdesc->bd_tupdesc->natts);
613
614 /*
615 * Make room for per-attribute lists of scan keys that we'll pass to the
616 * consistent support procedure. We don't know which attributes have scan
617 * keys, so we allocate space for all attributes. That may use more memory
618 * but it's probably cheaper than determining which attributes are used.
619 *
620 * We keep null and regular keys separate, so that we can pass just the
621 * regular keys to the consistent function easily.
622 *
623 * To reduce the allocation overhead, we allocate one big chunk and then
624 * carve it into smaller arrays ourselves. All the pieces have exactly the
625 * same lifetime, so that's OK.
626 *
627 * XXX The widest index can have 32 attributes, so the amount of wasted
628 * memory is negligible. We could invent a more compact approach (with
629 * just space for used attributes) but that would make the matching more
630 * complex so it's not a good trade-off.
631 */
632 len =
633 MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* regular keys */
634 MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
635 MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts) +
636 MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* NULL keys */
637 MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
638 MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
639
640 ptr = palloc(len);
641 tmp = ptr;
642
643 keys = (ScanKey **) ptr;
644 ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
645
646 nullkeys = (ScanKey **) ptr;
647 ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
648
649 nkeys = (int *) ptr;
650 ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
651
652 nnullkeys = (int *) ptr;
653 ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
654
655 for (int i = 0; i < bdesc->bd_tupdesc->natts; i++)
656 {
657 keys[i] = (ScanKey *) ptr;
658 ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
659
660 nullkeys[i] = (ScanKey *) ptr;
661 ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
662 }
663
664 Assert(tmp + len == ptr);
665
666 /* zero the number of keys */
667 memset(nkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
668 memset(nnullkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
669
670 /* Preprocess the scan keys - split them into per-attribute arrays. */
671 for (int keyno = 0; keyno < scan->numberOfKeys; keyno++)
672 {
673 ScanKey key = &scan->keyData[keyno];
674 AttrNumber keyattno = key->sk_attno;
675
676 /*
677 * The collation of the scan key must match the collation used in the
678 * index column (but only if the search is not IS NULL/ IS NOT NULL).
679 * Otherwise we shouldn't be using this index ...
680 */
681 Assert((key->sk_flags & SK_ISNULL) ||
682 (key->sk_collation ==
684 keyattno - 1)->attcollation));
685
686 /*
687 * First time we see this index attribute, so init as needed.
688 *
689 * This is a bit of an overkill - we don't know how many scan keys are
690 * there for this attribute, so we simply allocate the largest number
691 * possible (as if all keys were for this attribute). This may waste a
692 * bit of memory, but we only expect small number of scan keys in
693 * general, so this should be negligible, and repeated repalloc calls
694 * are not free either.
695 */
696 if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
697 {
698 FmgrInfo *tmp;
699
700 /* First time we see this attribute, so no key/null keys. */
701 Assert(nkeys[keyattno - 1] == 0);
702 Assert(nnullkeys[keyattno - 1] == 0);
703
704 tmp = index_getprocinfo(idxRel, keyattno,
706 fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
708 }
709
710 /* Add key to the proper per-attribute array. */
711 if (key->sk_flags & SK_ISNULL)
712 {
713 nullkeys[keyattno - 1][nnullkeys[keyattno - 1]] = key;
714 nnullkeys[keyattno - 1]++;
715 }
716 else
717 {
718 keys[keyattno - 1][nkeys[keyattno - 1]] = key;
719 nkeys[keyattno - 1]++;
720 }
721 }
722
723 /* allocate an initial in-memory tuple, out of the per-range memcxt */
724 dtup = brin_new_memtuple(bdesc);
725
726 /*
727 * Setup and use a per-range memory context, which is reset every time we
728 * loop below. This avoids having to free the tuples within the loop.
729 */
731 "bringetbitmap cxt",
733 oldcxt = MemoryContextSwitchTo(perRangeCxt);
734
735 /*
736 * Now scan the revmap. We start by querying for heap page 0,
737 * incrementing by the number of pages per range; this gives us a full
738 * view of the table.
739 */
740 for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
741 {
742 bool addrange;
743 bool gottuple = false;
744 BrinTuple *tup;
745 OffsetNumber off;
746 Size size;
747
749
750 MemoryContextReset(perRangeCxt);
751
752 tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
753 &off, &size, BUFFER_LOCK_SHARE);
754 if (tup)
755 {
756 gottuple = true;
757 btup = brin_copy_tuple(tup, size, btup, &btupsz);
759 }
760
761 /*
762 * For page ranges with no indexed tuple, we must return the whole
763 * range; otherwise, compare it to the scan keys.
764 */
765 if (!gottuple)
766 {
767 addrange = true;
768 }
769 else
770 {
771 dtup = brin_deform_tuple(bdesc, btup, dtup);
772 if (dtup->bt_placeholder)
773 {
774 /*
775 * Placeholder tuples are always returned, regardless of the
776 * values stored in them.
777 */
778 addrange = true;
779 }
780 else
781 {
782 int attno;
783
784 /*
785 * Compare scan keys with summary values stored for the range.
786 * If scan keys are matched, the page range must be added to
787 * the bitmap. We initially assume the range needs to be
788 * added; in particular this serves the case where there are
789 * no keys.
790 */
791 addrange = true;
792 for (attno = 1; attno <= bdesc->bd_tupdesc->natts; attno++)
793 {
794 BrinValues *bval;
795 Datum add;
796 Oid collation;
797
798 /*
799 * skip attributes without any scan keys (both regular and
800 * IS [NOT] NULL)
801 */
802 if (nkeys[attno - 1] == 0 && nnullkeys[attno - 1] == 0)
803 continue;
804
805 bval = &dtup->bt_columns[attno - 1];
806
807 /*
808 * If the BRIN tuple indicates that this range is empty,
809 * we can skip it: there's nothing to match. We don't
810 * need to examine the next columns.
811 */
812 if (dtup->bt_empty_range)
813 {
814 addrange = false;
815 break;
816 }
817
818 /*
819 * First check if there are any IS [NOT] NULL scan keys,
820 * and if we're violating them. In that case we can
821 * terminate early, without invoking the support function.
822 *
823 * As there may be more keys, we can only determine
824 * mismatch within this loop.
825 */
826 if (bdesc->bd_info[attno - 1]->oi_regular_nulls &&
827 !check_null_keys(bval, nullkeys[attno - 1],
828 nnullkeys[attno - 1]))
829 {
830 /*
831 * If any of the IS [NOT] NULL keys failed, the page
832 * range as a whole can't pass. So terminate the loop.
833 */
834 addrange = false;
835 break;
836 }
837
838 /*
839 * So either there are no IS [NOT] NULL keys, or all
840 * passed. If there are no regular scan keys, we're done -
841 * the page range matches. If there are regular keys, but
842 * the page range is marked as 'all nulls' it can't
843 * possibly pass (we're assuming the operators are
844 * strict).
845 */
846
847 /* No regular scan keys - page range as a whole passes. */
848 if (!nkeys[attno - 1])
849 continue;
850
851 Assert((nkeys[attno - 1] > 0) &&
852 (nkeys[attno - 1] <= scan->numberOfKeys));
853
854 /* If it is all nulls, it cannot possibly be consistent. */
855 if (bval->bv_allnulls)
856 {
857 addrange = false;
858 break;
859 }
860
861 /*
862 * Collation from the first key (has to be the same for
863 * all keys for the same attribute).
864 */
865 collation = keys[attno - 1][0]->sk_collation;
866
867 /*
868 * Check whether the scan key is consistent with the page
869 * range values; if so, have the pages in the range added
870 * to the output bitmap.
871 *
872 * The opclass may or may not support processing of
873 * multiple scan keys. We can determine that based on the
874 * number of arguments - functions with extra parameter
875 * (number of scan keys) do support this, otherwise we
876 * have to simply pass the scan keys one by one.
877 */
878 if (consistentFn[attno - 1].fn_nargs >= 4)
879 {
880 /* Check all keys at once */
881 add = FunctionCall4Coll(&consistentFn[attno - 1],
882 collation,
883 PointerGetDatum(bdesc),
884 PointerGetDatum(bval),
885 PointerGetDatum(keys[attno - 1]),
886 Int32GetDatum(nkeys[attno - 1]));
887 addrange = DatumGetBool(add);
888 }
889 else
890 {
891 /*
892 * Check keys one by one
893 *
894 * When there are multiple scan keys, failure to meet
895 * the criteria for a single one of them is enough to
896 * discard the range as a whole, so break out of the
897 * loop as soon as a false return value is obtained.
898 */
899 int keyno;
900
901 for (keyno = 0; keyno < nkeys[attno - 1]; keyno++)
902 {
903 add = FunctionCall3Coll(&consistentFn[attno - 1],
904 keys[attno - 1][keyno]->sk_collation,
905 PointerGetDatum(bdesc),
906 PointerGetDatum(bval),
907 PointerGetDatum(keys[attno - 1][keyno]));
908 addrange = DatumGetBool(add);
909 if (!addrange)
910 break;
911 }
912 }
913
914 /*
915 * If we found a scan key eliminating the range, no need
916 * to check additional ones.
917 */
918 if (!addrange)
919 break;
920 }
921 }
922 }
923
924 /* add the pages in the range to the output bitmap, if needed */
925 if (addrange)
926 {
927 BlockNumber pageno;
928
929 for (pageno = heapBlk;
930 pageno <= Min(nblocks, heapBlk + opaque->bo_pagesPerRange) - 1;
931 pageno++)
932 {
933 MemoryContextSwitchTo(oldcxt);
934 tbm_add_page(tbm, pageno);
935 totalpages++;
936 MemoryContextSwitchTo(perRangeCxt);
937 }
938 }
939 }
940
941 MemoryContextSwitchTo(oldcxt);
942 MemoryContextDelete(perRangeCxt);
943
944 if (buf != InvalidBuffer)
946
947 /*
948 * XXX We have an approximation of the number of *pages* that our scan
949 * returns, but we don't have a precise idea of the number of heap tuples
950 * involved.
951 */
952 return totalpages * 10;
953}
int16 AttrNumber
Definition: attnum.h:21
static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
Definition: brin.c:2300
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:72
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode)
Definition: brin_revmap.c:194
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:446
#define InvalidBuffer
Definition: buf.h:25
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5151
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:190
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:191
#define Min(x, y)
Definition: c.h:975
#define MAXALIGN(LEN)
Definition: c.h:782
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:224
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1171
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:580
#define AccessShareLock
Definition: lockdefs.h:36
uint16 OffsetNumber
Definition: off.h:24
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:694
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
#define SK_ISNULL
Definition: skey.h:115
bool bt_placeholder
Definition: brin_tuple.h:46
struct ScanKeyData * keyData
Definition: relscan.h:141
struct IndexScanInstrumentation * instrument
Definition: relscan.h:159
Relation indexRelation
Definition: relscan.h:137
Oid sk_collation
Definition: skey.h:70
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:432

References AccessShareLock, addrange(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), BrinDesc::bd_info, BrinDesc::bd_tupdesc, BrinOpaque::bo_bdesc, BrinOpaque::bo_pagesPerRange, BrinOpaque::bo_rmAccess, brin_copy_tuple(), brin_deform_tuple(), brin_new_memtuple(), BRIN_PROCNUM_CONSISTENT, brinGetTupleForHeapBlock(), BrinMemTuple::bt_columns, BrinMemTuple::bt_empty_range, BrinMemTuple::bt_placeholder, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BrinValues::bv_allnulls, CHECK_FOR_INTERRUPTS, check_null_keys(), CurrentMemoryContext, DatumGetBool(), fmgr_info_copy(), FunctionCall3Coll(), FunctionCall4Coll(), i, index_getprocinfo(), IndexGetRelation(), IndexScanDescData::indexRelation, IndexScanDescData::instrument, Int32GetDatum(), InvalidBuffer, InvalidOid, sort-test::key, IndexScanDescData::keyData, len, LockBuffer(), MAXALIGN, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), Min, TupleDescData::natts, IndexScanInstrumentation::nsearches, IndexScanDescData::numberOfKeys, BrinOpcInfo::oi_regular_nulls, IndexScanDescData::opaque, palloc(), palloc0_array, PG_USED_FOR_ASSERTS_ONLY, pgstat_count_index_scan, PointerGetDatum(), RelationGetNumberOfBlocks, RelationGetRelid, ReleaseBuffer(), ScanKeyData::sk_collation, SK_ISNULL, table_close(), table_open(), tbm_add_page(), and TupleDescAttr().

Referenced by brinhandler().

◆ brinGetStats()

void brinGetStats ( Relation  index,
BrinStatsData stats 
)

Definition at line 1649 of file brin.c.

1650{
1651 Buffer metabuffer;
1652 Page metapage;
1653 BrinMetaPageData *metadata;
1654
1655 metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1656 LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1657 metapage = BufferGetPage(metabuffer);
1658 metadata = (BrinMetaPageData *) PageGetContents(metapage);
1659
1660 stats->pagesPerRange = metadata->pagesPerRange;
1661 stats->revmapNumPages = metadata->lastRevmapPage - 1;
1662
1663 UnlockReleaseBuffer(metabuffer);
1664}
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:751
static char * PageGetContents(Page page)
Definition: bufpage.h:258
BlockNumber lastRevmapPage
Definition: brin_page.h:69
BlockNumber pagesPerRange
Definition: brin_page.h:68
BlockNumber revmapNumPages
Definition: brin.h:35
BlockNumber pagesPerRange
Definition: brin.h:34

References BRIN_METAPAGE_BLKNO, BUFFER_LOCK_SHARE, BufferGetPage(), BrinMetaPageData::lastRevmapPage, LockBuffer(), PageGetContents(), BrinStatsData::pagesPerRange, BrinMetaPageData::pagesPerRange, ReadBuffer(), BrinStatsData::revmapNumPages, and UnlockReleaseBuffer().

Referenced by brincostestimate().

◆ brinhandler()

Datum brinhandler ( PG_FUNCTION_ARGS  )

Definition at line 250 of file brin.c.

251{
253
254 amroutine->amstrategies = 0;
257 amroutine->amcanorder = false;
258 amroutine->amcanorderbyop = false;
259 amroutine->amcanhash = false;
260 amroutine->amconsistentequality = false;
261 amroutine->amconsistentordering = false;
262 amroutine->amcanbackward = false;
263 amroutine->amcanunique = false;
264 amroutine->amcanmulticol = true;
265 amroutine->amoptionalkey = true;
266 amroutine->amsearcharray = false;
267 amroutine->amsearchnulls = true;
268 amroutine->amstorage = true;
269 amroutine->amclusterable = false;
270 amroutine->ampredlocks = false;
271 amroutine->amcanparallel = false;
272 amroutine->amcanbuildparallel = true;
273 amroutine->amcaninclude = false;
274 amroutine->amusemaintenanceworkmem = false;
275 amroutine->amsummarizing = true;
276 amroutine->amparallelvacuumoptions =
278 amroutine->amkeytype = InvalidOid;
279
280 amroutine->ambuild = brinbuild;
281 amroutine->ambuildempty = brinbuildempty;
282 amroutine->aminsert = brininsert;
284 amroutine->ambulkdelete = brinbulkdelete;
286 amroutine->amcanreturn = NULL;
287 amroutine->amcostestimate = brincostestimate;
288 amroutine->amgettreeheight = NULL;
289 amroutine->amoptions = brinoptions;
290 amroutine->amproperty = NULL;
291 amroutine->ambuildphasename = NULL;
292 amroutine->amvalidate = brinvalidate;
293 amroutine->amadjustmembers = NULL;
294 amroutine->ambeginscan = brinbeginscan;
295 amroutine->amrescan = brinrescan;
296 amroutine->amgettuple = NULL;
297 amroutine->amgetbitmap = bringetbitmap;
298 amroutine->amendscan = brinendscan;
299 amroutine->ammarkpos = NULL;
300 amroutine->amrestrpos = NULL;
301 amroutine->amestimateparallelscan = NULL;
302 amroutine->aminitparallelscan = NULL;
303 amroutine->amparallelrescan = NULL;
304 amroutine->amtranslatestrategy = NULL;
305 amroutine->amtranslatecmptype = NULL;
306
307 PG_RETURN_POINTER(amroutine);
308}
void brininsertcleanup(Relation index, IndexInfo *indexInfo)
Definition: brin.c:512
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:959
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:1303
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:539
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:1348
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:567
void brinbuildempty(Relation index)
Definition: brin.c:1274
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:1105
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:1318
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: brin.c:344
void brinendscan(IndexScanDesc scan)
Definition: brin.c:978
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:78
#define BRIN_PROCNUM_OPTIONS
Definition: brin_internal.h:75
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:37
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:161
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:8191
ambuildphasename_function ambuildphasename
Definition: amapi.h:303
ambuildempty_function ambuildempty
Definition: amapi.h:293
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:297
bool amclusterable
Definition: amapi.h:267
amoptions_function amoptions
Definition: amapi.h:301
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:315
amrestrpos_function amrestrpos
Definition: amapi.h:312
aminsert_function aminsert
Definition: amapi.h:294
amendscan_function amendscan
Definition: amapi.h:310
amtranslate_strategy_function amtranslatestrategy
Definition: amapi.h:320
uint16 amoptsprocnum
Definition: amapi.h:241
amparallelrescan_function amparallelrescan
Definition: amapi.h:317
Oid amkeytype
Definition: amapi.h:283
bool amconsistentordering
Definition: amapi.h:251
bool ampredlocks
Definition: amapi.h:269
uint16 amsupport
Definition: amapi.h:239
amtranslate_cmptype_function amtranslatecmptype
Definition: amapi.h:321
amcostestimate_function amcostestimate
Definition: amapi.h:299
bool amcanorderbyop
Definition: amapi.h:245
amadjustmembers_function amadjustmembers
Definition: amapi.h:305
ambuild_function ambuild
Definition: amapi.h:292
bool amstorage
Definition: amapi.h:265
uint16 amstrategies
Definition: amapi.h:237
bool amoptionalkey
Definition: amapi.h:259
amgettuple_function amgettuple
Definition: amapi.h:308
amcanreturn_function amcanreturn
Definition: amapi.h:298
bool amcanunique
Definition: amapi.h:255
amgetbitmap_function amgetbitmap
Definition: amapi.h:309
amproperty_function amproperty
Definition: amapi.h:302
ambulkdelete_function ambulkdelete
Definition: amapi.h:296
bool amsearcharray
Definition: amapi.h:261
bool amsummarizing
Definition: amapi.h:279
amvalidate_function amvalidate
Definition: amapi.h:304
ammarkpos_function ammarkpos
Definition: amapi.h:311
bool amcanmulticol
Definition: amapi.h:257
bool amusemaintenanceworkmem
Definition: amapi.h:277
ambeginscan_function ambeginscan
Definition: amapi.h:306
bool amcanparallel
Definition: amapi.h:271
amrescan_function amrescan
Definition: amapi.h:307
bool amcanorder
Definition: amapi.h:243
bool amcanbuildparallel
Definition: amapi.h:273
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:316
uint8 amparallelvacuumoptions
Definition: amapi.h:281
aminsertcleanup_function aminsertcleanup
Definition: amapi.h:295
bool amcanbackward
Definition: amapi.h:253
amgettreeheight_function amgettreeheight
Definition: amapi.h:300
bool amcaninclude
Definition: amapi.h:275
bool amsearchnulls
Definition: amapi.h:263
bool amconsistentequality
Definition: amapi.h:249
bool amcanhash
Definition: amapi.h:247
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:63

References IndexAmRoutine::amadjustmembers, IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambuildphasename, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcanbuildparallel, IndexAmRoutine::amcanhash, IndexAmRoutine::amcaninclude, IndexAmRoutine::amcanmulticol, IndexAmRoutine::amcanorder, IndexAmRoutine::amcanorderbyop, IndexAmRoutine::amcanparallel, IndexAmRoutine::amcanreturn, IndexAmRoutine::amcanunique, IndexAmRoutine::amclusterable, IndexAmRoutine::amconsistentequality, IndexAmRoutine::amconsistentordering, IndexAmRoutine::amcostestimate, IndexAmRoutine::amendscan, IndexAmRoutine::amestimateparallelscan, IndexAmRoutine::amgetbitmap, IndexAmRoutine::amgettreeheight, IndexAmRoutine::amgettuple, IndexAmRoutine::aminitparallelscan, IndexAmRoutine::aminsert, IndexAmRoutine::aminsertcleanup, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amoptsprocnum, IndexAmRoutine::amparallelrescan, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsummarizing, IndexAmRoutine::amsupport, IndexAmRoutine::amtranslatecmptype, IndexAmRoutine::amtranslatestrategy, IndexAmRoutine::amusemaintenanceworkmem, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, BRIN_LAST_OPTIONAL_PROCNUM, BRIN_PROCNUM_OPTIONS, brinbeginscan(), brinbuild(), brinbuildempty(), brinbulkdelete(), brincostestimate(), brinendscan(), bringetbitmap(), brininsert(), brininsertcleanup(), brinoptions(), brinrescan(), brinvacuumcleanup(), brinvalidate(), InvalidOid, makeNode, PG_RETURN_POINTER, and VACUUM_OPTION_PARALLEL_CLEANUP.

◆ brininsert()

bool brininsert ( Relation  idxRel,
Datum values,
bool *  nulls,
ItemPointer  heaptid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
IndexInfo indexInfo 
)

Definition at line 344 of file brin.c.

349{
350 BlockNumber pagesPerRange;
351 BlockNumber origHeapBlk;
352 BlockNumber heapBlk;
353 BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache;
354 BrinRevmap *revmap;
355 BrinDesc *bdesc;
357 MemoryContext tupcxt = NULL;
359 bool autosummarize = BrinGetAutoSummarize(idxRel);
360
361 /*
362 * If first time through in this statement, initialize the insert state
363 * that we keep for all the inserts in the command.
364 */
365 if (!bistate)
366 bistate = initialize_brin_insertstate(idxRel, indexInfo);
367
368 revmap = bistate->bis_rmAccess;
369 bdesc = bistate->bis_desc;
370 pagesPerRange = bistate->bis_pages_per_range;
371
372 /*
373 * origHeapBlk is the block number where the insertion occurred. heapBlk
374 * is the first block in the corresponding page range.
375 */
376 origHeapBlk = ItemPointerGetBlockNumber(heaptid);
377 heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
378
379 for (;;)
380 {
381 bool need_insert = false;
382 OffsetNumber off;
383 BrinTuple *brtup;
384 BrinMemTuple *dtup;
385
387
388 /*
389 * If auto-summarization is enabled and we just inserted the first
390 * tuple into the first block of a new non-first page range, request a
391 * summarization run of the previous range.
392 */
393 if (autosummarize &&
394 heapBlk > 0 &&
395 heapBlk == origHeapBlk &&
397 {
398 BlockNumber lastPageRange = heapBlk - 1;
399 BrinTuple *lastPageTuple;
400
401 lastPageTuple =
402 brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
403 NULL, BUFFER_LOCK_SHARE);
404 if (!lastPageTuple)
405 {
406 bool recorded;
407
409 RelationGetRelid(idxRel),
410 lastPageRange);
411 if (!recorded)
412 ereport(LOG,
413 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
414 errmsg("request for BRIN range summarization for index \"%s\" page %u was not recorded",
416 lastPageRange)));
417 }
418 else
420 }
421
422 brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
423 NULL, BUFFER_LOCK_SHARE);
424
425 /* if range is unsummarized, there's nothing to do */
426 if (!brtup)
427 break;
428
429 /* First time through in this brininsert call? */
430 if (tupcxt == NULL)
431 {
433 "brininsert cxt",
435 MemoryContextSwitchTo(tupcxt);
436 }
437
438 dtup = brin_deform_tuple(bdesc, brtup, NULL);
439
440 need_insert = add_values_to_range(idxRel, bdesc, dtup, values, nulls);
441
442 if (!need_insert)
443 {
444 /*
445 * The tuple is consistent with the new values, so there's nothing
446 * to do.
447 */
449 }
450 else
451 {
452 Page page = BufferGetPage(buf);
453 ItemId lp = PageGetItemId(page, off);
454 Size origsz;
455 BrinTuple *origtup;
456 Size newsz;
457 BrinTuple *newtup;
458 bool samepage;
459
460 /*
461 * Make a copy of the old tuple, so that we can compare it after
462 * re-acquiring the lock.
463 */
464 origsz = ItemIdGetLength(lp);
465 origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
466
467 /*
468 * Before releasing the lock, check if we can attempt a same-page
469 * update. Another process could insert a tuple concurrently in
470 * the same page though, so downstream we must be prepared to cope
471 * if this turns out to not be possible after all.
472 */
473 newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
474 samepage = brin_can_do_samepage_update(buf, origsz, newsz);
476
477 /*
478 * Try to update the tuple. If this doesn't work for whatever
479 * reason, we need to restart from the top; the revmap might be
480 * pointing at a different tuple for this block now, so we need to
481 * recompute to ensure both our new heap tuple and the other
482 * inserter's are covered by the combined tuple. It might be that
483 * we don't need to update at all.
484 */
485 if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
486 buf, off, origtup, origsz, newtup, newsz,
487 samepage))
488 {
489 /* no luck; start over */
490 MemoryContextReset(tupcxt);
491 continue;
492 }
493 }
494
495 /* success! */
496 break;
497 }
498
499 if (BufferIsValid(buf))
501 MemoryContextSwitchTo(oldcxt);
502 if (tupcxt != NULL)
503 MemoryContextDelete(tupcxt);
504
505 return false;
506}
bool AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3260
@ AVW_BRINSummarizeRange
Definition: autovacuum.h:25
static BrinInsertState * initialize_brin_insertstate(Relation idxRel, IndexInfo *indexInfo)
Definition: brin.c:315
#define BrinGetAutoSummarize(relation)
Definition: brin.h:46
bool brin_doupdate(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, BlockNumber heapBlk, Buffer oldbuf, OffsetNumber oldoff, const BrinTuple *origtup, Size origsz, const BrinTuple *newtup, Size newsz, bool samepage)
Definition: brin_pageops.c:53
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:323
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:352
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:244
#define LOG
Definition: elog.h:31
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
#define FirstOffsetNumber
Definition: off.h:27
BrinDesc * bis_desc
Definition: brin.c:195
BrinRevmap * bis_rmAccess
Definition: brin.c:194
BlockNumber bis_pages_per_range
Definition: brin.c:196
void * ii_AmCache
Definition: execnodes.h:220

References add_values_to_range(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AutoVacuumRequestWork(), AVW_BRINSummarizeRange, BrinInsertState::bis_desc, BrinInsertState::bis_pages_per_range, BrinInsertState::bis_rmAccess, brin_can_do_samepage_update(), brin_copy_tuple(), brin_deform_tuple(), brin_doupdate(), brin_form_tuple(), BrinGetAutoSummarize, brinGetTupleForHeapBlock(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage(), BufferIsValid(), CHECK_FOR_INTERRUPTS, CurrentMemoryContext, ereport, errcode(), errmsg(), FirstOffsetNumber, IndexInfo::ii_AmCache, initialize_brin_insertstate(), InvalidBuffer, ItemIdGetLength, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), LockBuffer(), LOG, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), PageGetItemId(), RelationGetRelationName, RelationGetRelid, ReleaseBuffer(), and values.

Referenced by brinhandler().

◆ brininsertcleanup()

void brininsertcleanup ( Relation  index,
IndexInfo indexInfo 
)

Definition at line 512 of file brin.c.

513{
514 BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache;
515
516 /* bail out if cache not initialized */
517 if (bistate == NULL)
518 return;
519
520 /* do this first to avoid dangling pointer if we fail partway through */
521 indexInfo->ii_AmCache = NULL;
522
523 /*
524 * Clean up the revmap. Note that the brinDesc has already been cleaned up
525 * as part of its own memory context.
526 */
528 pfree(bistate);
529}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81

References BrinInsertState::bis_rmAccess, brinRevmapTerminate(), if(), IndexInfo::ii_AmCache, and pfree().

Referenced by brinhandler().

◆ brinoptions()

bytea * brinoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 1348 of file brin.c.

1349{
1350 static const relopt_parse_elt tab[] = {
1351 {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
1352 {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
1353 };
1354
1355 return (bytea *) build_reloptions(reloptions, validate,
1357 sizeof(BrinOptions),
1358 tab, lengthof(tab));
1359}
static bool validate(Port *port, const char *auth)
Definition: auth-oauth.c:638
#define lengthof(array)
Definition: c.h:759
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:1942
@ RELOPT_KIND_BRIN
Definition: reloptions.h:52
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
Definition: c.h:658

References build_reloptions(), lengthof, RELOPT_KIND_BRIN, RELOPT_TYPE_BOOL, RELOPT_TYPE_INT, and validate().

Referenced by brinhandler().

◆ brinrescan()

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

Definition at line 959 of file brin.c.

961{
962 /*
963 * Other index AMs preprocess the scan keys at this point, or sometime
964 * early during the scan; this lets them optimize by removing redundant
965 * keys, or doing early returns when they are impossible to satisfy; see
966 * _bt_preprocess_keys for an example. Something like that could be added
967 * here someday, too.
968 */
969
970 if (scankey && scan->numberOfKeys > 0)
971 memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData));
972}

References IndexScanDescData::keyData, and IndexScanDescData::numberOfKeys.

Referenced by brinhandler().

◆ brinsummarize()

static void brinsummarize ( Relation  index,
Relation  heapRel,
BlockNumber  pageRange,
bool  include_partial,
double *  numSummarized,
double *  numExisting 
)
static

Definition at line 1888 of file brin.c.

1890{
1891 BrinRevmap *revmap;
1892 BrinBuildState *state = NULL;
1893 IndexInfo *indexInfo = NULL;
1894 BlockNumber heapNumBlocks;
1895 BlockNumber pagesPerRange;
1896 Buffer buf;
1897 BlockNumber startBlk;
1898
1899 revmap = brinRevmapInitialize(index, &pagesPerRange);
1900
1901 /* determine range of pages to process */
1902 heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1903 if (pageRange == BRIN_ALL_BLOCKRANGES)
1904 startBlk = 0;
1905 else
1906 {
1907 startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1908 heapNumBlocks = Min(heapNumBlocks, startBlk + pagesPerRange);
1909 }
1910 if (startBlk > heapNumBlocks)
1911 {
1912 /* Nothing to do if start point is beyond end of table */
1913 brinRevmapTerminate(revmap);
1914 return;
1915 }
1916
1917 /*
1918 * Scan the revmap to find unsummarized items.
1919 */
1921 for (; startBlk < heapNumBlocks; startBlk += pagesPerRange)
1922 {
1923 BrinTuple *tup;
1924 OffsetNumber off;
1925
1926 /*
1927 * Unless requested to summarize even a partial range, go away now if
1928 * we think the next range is partial. Caller would pass true when it
1929 * is typically run once bulk data loading is done
1930 * (brin_summarize_new_values), and false when it is typically the
1931 * result of arbitrarily-scheduled maintenance command (vacuuming).
1932 */
1933 if (!include_partial &&
1934 (startBlk + pagesPerRange > heapNumBlocks))
1935 break;
1936
1938
1939 tup = brinGetTupleForHeapBlock(revmap, startBlk, &buf, &off, NULL,
1941 if (tup == NULL)
1942 {
1943 /* no revmap entry for this heap range. Summarize it. */
1944 if (state == NULL)
1945 {
1946 /* first time through */
1947 Assert(!indexInfo);
1949 pagesPerRange,
1951 indexInfo = BuildIndexInfo(index);
1952 }
1953 summarize_range(indexInfo, state, heapRel, startBlk, heapNumBlocks);
1954
1955 /* and re-initialize state for the next range */
1956 brin_memtuple_initialize(state->bs_dtuple, state->bs_bdesc);
1957
1958 if (numSummarized)
1959 *numSummarized += 1.0;
1960 }
1961 else
1962 {
1963 if (numExisting)
1964 *numExisting += 1.0;
1966 }
1967 }
1968
1969 if (BufferIsValid(buf))
1971
1972 /* free resources */
1973 brinRevmapTerminate(revmap);
1974 if (state)
1975 {
1977 pfree(indexInfo);
1978 }
1979}
static void summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
Definition: brin.c:1762

References Assert(), BRIN_ALL_BLOCKRANGES, brin_memtuple_initialize(), brinGetTupleForHeapBlock(), brinRevmapInitialize(), brinRevmapTerminate(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferIsValid(), BuildIndexInfo(), CHECK_FOR_INTERRUPTS, initialize_brin_buildstate(), InvalidBlockNumber, InvalidBuffer, LockBuffer(), Min, pfree(), RelationGetNumberOfBlocks, ReleaseBuffer(), summarize_range(), and terminate_brin_buildstate().

Referenced by brin_summarize_range(), and brinvacuumcleanup().

◆ brinvacuumcleanup()

IndexBulkDeleteResult * brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 1318 of file brin.c.

1319{
1320 Relation heapRel;
1321
1322 /* No-op in ANALYZE ONLY mode */
1323 if (info->analyze_only)
1324 return stats;
1325
1326 if (!stats)
1329 /* rest of stats is initialized by zeroing */
1330
1331 heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
1333
1334 brin_vacuum_scan(info->index, info->strategy);
1335
1336 brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
1337 &stats->num_index_tuples, &stats->num_index_tuples);
1338
1339 table_close(heapRel, AccessShareLock);
1340
1341 return stats;
1342}
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:2173
BlockNumber num_pages
Definition: genam.h:100
double num_index_tuples
Definition: genam.h:102
Relation index
Definition: genam.h:69
bool analyze_only
Definition: genam.h:71
BufferAccessStrategy strategy
Definition: genam.h:76

References AccessShareLock, IndexVacuumInfo::analyze_only, BRIN_ALL_BLOCKRANGES, brin_vacuum_scan(), brinsummarize(), IndexVacuumInfo::index, IndexGetRelation(), IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, palloc0_object, RelationGetNumberOfBlocks, RelationGetRelid, IndexVacuumInfo::strategy, table_close(), and table_open().

Referenced by brinhandler().

◆ check_null_keys()

static bool check_null_keys ( BrinValues bval,
ScanKey nullkeys,
int  nnullkeys 
)
static

Definition at line 2300 of file brin.c.

2301{
2302 int keyno;
2303
2304 /*
2305 * First check if there are any IS [NOT] NULL scan keys, and if we're
2306 * violating them.
2307 */
2308 for (keyno = 0; keyno < nnullkeys; keyno++)
2309 {
2310 ScanKey key = nullkeys[keyno];
2311
2312 Assert(key->sk_attno == bval->bv_attno);
2313
2314 /* Handle only IS NULL/IS NOT NULL tests */
2315 if (!(key->sk_flags & SK_ISNULL))
2316 continue;
2317
2318 if (key->sk_flags & SK_SEARCHNULL)
2319 {
2320 /* IS NULL scan key, but range has no NULLs */
2321 if (!bval->bv_allnulls && !bval->bv_hasnulls)
2322 return false;
2323 }
2324 else if (key->sk_flags & SK_SEARCHNOTNULL)
2325 {
2326 /*
2327 * For IS NOT NULL, we can only skip ranges that are known to have
2328 * only nulls.
2329 */
2330 if (bval->bv_allnulls)
2331 return false;
2332 }
2333 else
2334 {
2335 /*
2336 * Neither IS NULL nor IS NOT NULL was used; assume all indexable
2337 * operators are strict and thus return false with NULL value in
2338 * the scan key.
2339 */
2340 return false;
2341 }
2342 }
2343
2344 return true;
2345}
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_SEARCHNULL
Definition: skey.h:121
AttrNumber bv_attno
Definition: brin_tuple.h:31

References Assert(), BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_hasnulls, sort-test::key, SK_ISNULL, SK_SEARCHNOTNULL, and SK_SEARCHNULL.

Referenced by bringetbitmap().

◆ form_and_insert_tuple()

static void form_and_insert_tuple ( BrinBuildState state)
static

Definition at line 1986 of file brin.c.

1987{
1988 BrinTuple *tup;
1989 Size size;
1990
1991 tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1992 state->bs_dtuple, &size);
1993 brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1994 &state->bs_currentInsertBuf, state->bs_currRangeStart,
1995 tup, size);
1996 state->bs_numtuples++;
1997
1998 pfree(tup);
1999}

References brin_doinsert(), brin_form_tuple(), and pfree().

Referenced by brinbuild(), and brinbuildCallback().

◆ form_and_spill_tuple()

static void form_and_spill_tuple ( BrinBuildState state)
static

Definition at line 2007 of file brin.c.

2008{
2009 BrinTuple *tup;
2010 Size size;
2011
2012 /* don't insert empty tuples in parallel build */
2013 if (state->bs_dtuple->bt_empty_range)
2014 return;
2015
2016 tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
2017 state->bs_dtuple, &size);
2018
2019 /* write the BRIN tuple to the tuplesort */
2020 tuplesort_putbrintuple(state->bs_sortstate, tup, size);
2021
2022 state->bs_numtuples++;
2023
2024 pfree(tup);
2025}
void tuplesort_putbrintuple(Tuplesortstate *state, BrinTuple *tuple, Size size)

References brin_form_tuple(), pfree(), and tuplesort_putbrintuple().

Referenced by _brin_parallel_scan_and_build(), and brinbuildCallbackParallel().

◆ initialize_brin_buildstate()

static BrinBuildState * initialize_brin_buildstate ( Relation  idxRel,
BrinRevmap revmap,
BlockNumber  pagesPerRange,
BlockNumber  tablePages 
)
static

Definition at line 1670 of file brin.c.

1672{
1674 BlockNumber lastRange = 0;
1675
1677
1678 state->bs_irel = idxRel;
1679 state->bs_numtuples = 0;
1680 state->bs_reltuples = 0;
1681 state->bs_currentInsertBuf = InvalidBuffer;
1682 state->bs_pagesPerRange = pagesPerRange;
1683 state->bs_currRangeStart = 0;
1684 state->bs_rmAccess = revmap;
1685 state->bs_bdesc = brin_build_desc(idxRel);
1686 state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1687 state->bs_leader = NULL;
1688 state->bs_worker_id = 0;
1689 state->bs_sortstate = NULL;
1690 state->bs_context = CurrentMemoryContext;
1691 state->bs_emptyTuple = NULL;
1692 state->bs_emptyTupleLen = 0;
1693
1694 /* Remember the memory context to use for an empty tuple, if needed. */
1695 state->bs_context = CurrentMemoryContext;
1696 state->bs_emptyTuple = NULL;
1697 state->bs_emptyTupleLen = 0;
1698
1699 /*
1700 * Calculate the start of the last page range. Page numbers are 0-based,
1701 * so to calculate the index we need to subtract one. The integer division
1702 * gives us the index of the page range.
1703 */
1704 if (tablePages > 0)
1705 lastRange = ((tablePages - 1) / pagesPerRange) * pagesPerRange;
1706
1707 /* Now calculate the start of the next range. */
1708 state->bs_maxRangeStart = lastRange + state->bs_pagesPerRange;
1709
1710 return state;
1711}

References brin_build_desc(), brin_new_memtuple(), CurrentMemoryContext, InvalidBuffer, and palloc_object.

Referenced by _brin_parallel_build_main(), brinbuild(), and brinsummarize().

◆ initialize_brin_insertstate()

static BrinInsertState * initialize_brin_insertstate ( Relation  idxRel,
IndexInfo indexInfo 
)
static

Definition at line 315 of file brin.c.

316{
317 BrinInsertState *bistate;
318 MemoryContext oldcxt;
319
320 oldcxt = MemoryContextSwitchTo(indexInfo->ii_Context);
321 bistate = palloc0(sizeof(BrinInsertState));
322 bistate->bis_desc = brin_build_desc(idxRel);
323 bistate->bis_rmAccess = brinRevmapInitialize(idxRel,
324 &bistate->bis_pages_per_range);
325 indexInfo->ii_AmCache = bistate;
326 MemoryContextSwitchTo(oldcxt);
327
328 return bistate;
329}
MemoryContext ii_Context
Definition: execnodes.h:221

References BrinInsertState::bis_desc, BrinInsertState::bis_pages_per_range, BrinInsertState::bis_rmAccess, brin_build_desc(), brinRevmapInitialize(), IndexInfo::ii_AmCache, IndexInfo::ii_Context, MemoryContextSwitchTo(), and palloc0().

Referenced by brininsert().

◆ summarize_range()

static void summarize_range ( IndexInfo indexInfo,
BrinBuildState state,
Relation  heapRel,
BlockNumber  heapBlk,
BlockNumber  heapNumBlks 
)
static

Definition at line 1762 of file brin.c.

1764{
1765 Buffer phbuf;
1766 BrinTuple *phtup;
1767 Size phsz;
1768 OffsetNumber offset;
1769 BlockNumber scanNumBlks;
1770
1771 /*
1772 * Insert the placeholder tuple
1773 */
1774 phbuf = InvalidBuffer;
1775 phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1776 offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1777 state->bs_rmAccess, &phbuf,
1778 heapBlk, phtup, phsz);
1779
1780 /*
1781 * Compute range end. We hold ShareUpdateExclusive lock on table, so it
1782 * cannot shrink concurrently (but it can grow).
1783 */
1784 Assert(heapBlk % state->bs_pagesPerRange == 0);
1785 if (heapBlk + state->bs_pagesPerRange > heapNumBlks)
1786 {
1787 /*
1788 * If we're asked to scan what we believe to be the final range on the
1789 * table (i.e. a range that might be partial) we need to recompute our
1790 * idea of what the latest page is after inserting the placeholder
1791 * tuple. Anyone that grows the table later will update the
1792 * placeholder tuple, so it doesn't matter that we won't scan these
1793 * pages ourselves. Careful: the table might have been extended
1794 * beyond the current range, so clamp our result.
1795 *
1796 * Fortunately, this should occur infrequently.
1797 */
1798 scanNumBlks = Min(RelationGetNumberOfBlocks(heapRel) - heapBlk,
1799 state->bs_pagesPerRange);
1800 }
1801 else
1802 {
1803 /* Easy case: range is known to be complete */
1804 scanNumBlks = state->bs_pagesPerRange;
1805 }
1806
1807 /*
1808 * Execute the partial heap scan covering the heap blocks in the specified
1809 * page range, summarizing the heap tuples in it. This scan stops just
1810 * short of brinbuildCallback creating the new index entry.
1811 *
1812 * Note that it is critical we use the "any visible" mode of
1813 * table_index_build_range_scan here: otherwise, we would miss tuples
1814 * inserted by transactions that are still in progress, among other corner
1815 * cases.
1816 */
1817 state->bs_currRangeStart = heapBlk;
1818 table_index_build_range_scan(heapRel, state->bs_irel, indexInfo, false, true, false,
1819 heapBlk, scanNumBlks,
1820 brinbuildCallback, state, NULL);
1821
1822 /*
1823 * Now we update the values obtained by the scan with the placeholder
1824 * tuple. We do this in a loop which only terminates if we're able to
1825 * update the placeholder tuple successfully; if we are not, this means
1826 * somebody else modified the placeholder tuple after we read it.
1827 */
1828 for (;;)
1829 {
1830 BrinTuple *newtup;
1831 Size newsize;
1832 bool didupdate;
1833 bool samepage;
1834
1836
1837 /*
1838 * Update the summary tuple and try to update.
1839 */
1840 newtup = brin_form_tuple(state->bs_bdesc,
1841 heapBlk, state->bs_dtuple, &newsize);
1842 samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1843 didupdate =
1844 brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1845 state->bs_rmAccess, heapBlk, phbuf, offset,
1846 phtup, phsz, newtup, newsize, samepage);
1847 brin_free_tuple(phtup);
1848 brin_free_tuple(newtup);
1849
1850 /* If the update succeeded, we're done. */
1851 if (didupdate)
1852 break;
1853
1854 /*
1855 * If the update didn't work, it might be because somebody updated the
1856 * placeholder tuple concurrently. Extract the new version, union it
1857 * with the values we have from the scan, and start over. (There are
1858 * other reasons for the update to fail, but it's simple to treat them
1859 * the same.)
1860 */
1861 phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1862 &offset, &phsz, BUFFER_LOCK_SHARE);
1863 /* the placeholder tuple must exist */
1864 if (phtup == NULL)
1865 elog(ERROR, "missing placeholder tuple");
1866 phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1868
1869 /* merge it into the tuple from the heap scan */
1870 union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1871 }
1872
1873 ReleaseBuffer(phbuf);
1874}
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:433
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:388
static double table_index_build_range_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool anyvisible, bool progress, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1778

References Assert(), brin_can_do_samepage_update(), brin_copy_tuple(), brin_doinsert(), brin_doupdate(), brin_form_placeholder_tuple(), brin_form_tuple(), brin_free_tuple(), brinbuildCallback(), brinGetTupleForHeapBlock(), BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, CHECK_FOR_INTERRUPTS, elog, ERROR, InvalidBuffer, LockBuffer(), Min, RelationGetNumberOfBlocks, ReleaseBuffer(), table_index_build_range_scan(), and union_tuples().

Referenced by brinsummarize().

◆ terminate_brin_buildstate()

static void terminate_brin_buildstate ( BrinBuildState state)
static

Definition at line 1717 of file brin.c.

1718{
1719 /*
1720 * Release the last index buffer used. We might as well ensure that
1721 * whatever free space remains in that page is available in FSM, too.
1722 */
1723 if (!BufferIsInvalid(state->bs_currentInsertBuf))
1724 {
1725 Page page;
1726 Size freespace;
1727 BlockNumber blk;
1728
1729 page = BufferGetPage(state->bs_currentInsertBuf);
1730 freespace = PageGetFreeSpace(page);
1731 blk = BufferGetBlockNumber(state->bs_currentInsertBuf);
1732 ReleaseBuffer(state->bs_currentInsertBuf);
1733 RecordPageWithFreeSpace(state->bs_irel, blk, freespace);
1734 FreeSpaceMapVacuumRange(state->bs_irel, blk, blk + 1);
1735 }
1736
1737 brin_free_desc(state->bs_bdesc);
1738 pfree(state->bs_dtuple);
1739 pfree(state);
1740}
#define BufferIsInvalid(buffer)
Definition: buf.h:31
Size PageGetFreeSpace(const PageData *page)
Definition: bufpage.c:896
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:377
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:194

References brin_free_desc(), BufferGetBlockNumber(), BufferGetPage(), BufferIsInvalid, FreeSpaceMapVacuumRange(), PageGetFreeSpace(), pfree(), RecordPageWithFreeSpace(), and ReleaseBuffer().

Referenced by brinbuild(), and brinsummarize().

◆ union_tuples()

static void union_tuples ( BrinDesc bdesc,
BrinMemTuple a,
BrinTuple b 
)
static

Definition at line 2032 of file brin.c.

2033{
2034 int keyno;
2035 BrinMemTuple *db;
2036 MemoryContext cxt;
2037 MemoryContext oldcxt;
2038
2039 /* Use our own memory context to avoid retail pfree */
2041 "brin union",
2043 oldcxt = MemoryContextSwitchTo(cxt);
2044 db = brin_deform_tuple(bdesc, b, NULL);
2045 MemoryContextSwitchTo(oldcxt);
2046
2047 /*
2048 * Check if the ranges are empty.
2049 *
2050 * If at least one of them is empty, we don't need to call per-key union
2051 * functions at all. If "b" is empty, we just use "a" as the result (it
2052 * might be empty fine, but that's fine). If "a" is empty but "b" is not,
2053 * we use "b" as the result (but we have to copy the data into "a" first).
2054 *
2055 * Only when both ranges are non-empty, we actually do the per-key merge.
2056 */
2057
2058 /* If "b" is empty - ignore it and just use "a" (even if it's empty etc.). */
2059 if (db->bt_empty_range)
2060 {
2061 /* skip the per-key merge */
2063 return;
2064 }
2065
2066 /*
2067 * Now we know "b" is not empty. If "a" is empty, then "b" is the result.
2068 * But we need to copy the data from "b" to "a" first, because that's how
2069 * we pass result out.
2070 *
2071 * We have to copy all the global/per-key flags etc. too.
2072 */
2073 if (a->bt_empty_range)
2074 {
2075 for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
2076 {
2077 int i;
2078 BrinValues *col_a = &a->bt_columns[keyno];
2079 BrinValues *col_b = &db->bt_columns[keyno];
2080 BrinOpcInfo *opcinfo = bdesc->bd_info[keyno];
2081
2082 col_a->bv_allnulls = col_b->bv_allnulls;
2083 col_a->bv_hasnulls = col_b->bv_hasnulls;
2084
2085 /* If "b" has no data, we're done. */
2086 if (col_b->bv_allnulls)
2087 continue;
2088
2089 for (i = 0; i < opcinfo->oi_nstored; i++)
2090 col_a->bv_values[i] =
2091 datumCopy(col_b->bv_values[i],
2092 opcinfo->oi_typcache[i]->typbyval,
2093 opcinfo->oi_typcache[i]->typlen);
2094 }
2095
2096 /* "a" started empty, but "b" was not empty, so remember that */
2097 a->bt_empty_range = false;
2098
2099 /* skip the per-key merge */
2101 return;
2102 }
2103
2104 /* Now we know neither range is empty. */
2105 for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
2106 {
2107 FmgrInfo *unionFn;
2108 BrinValues *col_a = &a->bt_columns[keyno];
2109 BrinValues *col_b = &db->bt_columns[keyno];
2110 BrinOpcInfo *opcinfo = bdesc->bd_info[keyno];
2111
2112 if (opcinfo->oi_regular_nulls)
2113 {
2114 /* Does the "b" summary represent any NULL values? */
2115 bool b_has_nulls = (col_b->bv_hasnulls || col_b->bv_allnulls);
2116
2117 /* Adjust "hasnulls". */
2118 if (!col_a->bv_allnulls && b_has_nulls)
2119 col_a->bv_hasnulls = true;
2120
2121 /* If there are no values in B, there's nothing left to do. */
2122 if (col_b->bv_allnulls)
2123 continue;
2124
2125 /*
2126 * Adjust "allnulls". If A doesn't have values, just copy the
2127 * values from B into A, and we're done. We cannot run the
2128 * operators in this case, because values in A might contain
2129 * garbage. Note we already established that B contains values.
2130 *
2131 * Also adjust "hasnulls" in order not to forget the summary
2132 * represents NULL values. This is not redundant with the earlier
2133 * update, because that only happens when allnulls=false.
2134 */
2135 if (col_a->bv_allnulls)
2136 {
2137 int i;
2138
2139 col_a->bv_allnulls = false;
2140 col_a->bv_hasnulls = true;
2141
2142 for (i = 0; i < opcinfo->oi_nstored; i++)
2143 col_a->bv_values[i] =
2144 datumCopy(col_b->bv_values[i],
2145 opcinfo->oi_typcache[i]->typbyval,
2146 opcinfo->oi_typcache[i]->typlen);
2147
2148 continue;
2149 }
2150 }
2151
2152 unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
2154 FunctionCall3Coll(unionFn,
2155 bdesc->bd_index->rd_indcollation[keyno],
2156 PointerGetDatum(bdesc),
2157 PointerGetDatum(col_a),
2158 PointerGetDatum(col_b));
2159 }
2160
2162}
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:73
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
int b
Definition: isn.c:74
int a
Definition: isn.c:73
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
Datum * bv_values
Definition: brin_tuple.h:34
bool typbyval
Definition: typcache.h:40
int16 typlen
Definition: typcache.h:39

References a, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, b, BrinDesc::bd_index, BrinDesc::bd_info, BrinDesc::bd_tupdesc, brin_deform_tuple(), BRIN_PROCNUM_UNION, BrinMemTuple::bt_columns, BrinMemTuple::bt_empty_range, BrinValues::bv_allnulls, BrinValues::bv_hasnulls, BrinValues::bv_values, CurrentMemoryContext, datumCopy(), FunctionCall3Coll(), i, index_getprocinfo(), MemoryContextDelete(), MemoryContextSwitchTo(), TupleDescData::natts, BrinOpcInfo::oi_nstored, BrinOpcInfo::oi_regular_nulls, BrinOpcInfo::oi_typcache, PointerGetDatum(), RelationData::rd_indcollation, TypeCacheEntry::typbyval, and TypeCacheEntry::typlen.

Referenced by _brin_parallel_merge(), and summarize_range().