PostgreSQL Source Code git master
brin_internal.h File Reference
#include "access/amapi.h"
#include "storage/bufpage.h"
#include "utils/typcache.h"
Include dependency graph for brin_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BrinOpcInfo
 
struct  BrinDesc
 

Macros

#define SizeofBrinOpcInfo(ncols)    (offsetof(BrinOpcInfo, oi_typcache) + sizeof(TypeCacheEntry *) * ncols)
 
#define BRIN_PROCNUM_OPCINFO   1
 
#define BRIN_PROCNUM_ADDVALUE   2
 
#define BRIN_PROCNUM_CONSISTENT   3
 
#define BRIN_PROCNUM_UNION   4
 
#define BRIN_MANDATORY_NPROCS   4
 
#define BRIN_PROCNUM_OPTIONS   5 /* optional */
 
#define BRIN_FIRST_OPTIONAL_PROCNUM   11
 
#define BRIN_LAST_OPTIONAL_PROCNUM   15
 
#define BRIN_elog(args)   ((void) 0)
 

Typedefs

typedef struct BrinOpcInfo BrinOpcInfo
 
typedef struct BrinDesc BrinDesc
 

Functions

BrinDescbrin_build_desc (Relation rel)
 
void brin_free_desc (BrinDesc *bdesc)
 
IndexBuildResultbrinbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void brinbuildempty (Relation index)
 
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)
 
IndexBulkDeleteResultbrinbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultbrinvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
byteabrinoptions (Datum reloptions, bool validate)
 
bool brinvalidate (Oid opclassoid)
 

Macro Definition Documentation

◆ BRIN_elog

#define BRIN_elog (   args)    ((void) 0)

Definition at line 85 of file brin_internal.h.

◆ BRIN_FIRST_OPTIONAL_PROCNUM

#define BRIN_FIRST_OPTIONAL_PROCNUM   11

Definition at line 77 of file brin_internal.h.

◆ BRIN_LAST_OPTIONAL_PROCNUM

#define BRIN_LAST_OPTIONAL_PROCNUM   15

Definition at line 78 of file brin_internal.h.

◆ BRIN_MANDATORY_NPROCS

#define BRIN_MANDATORY_NPROCS   4

Definition at line 74 of file brin_internal.h.

◆ BRIN_PROCNUM_ADDVALUE

#define BRIN_PROCNUM_ADDVALUE   2

Definition at line 71 of file brin_internal.h.

◆ BRIN_PROCNUM_CONSISTENT

#define BRIN_PROCNUM_CONSISTENT   3

Definition at line 72 of file brin_internal.h.

◆ BRIN_PROCNUM_OPCINFO

#define BRIN_PROCNUM_OPCINFO   1

Definition at line 70 of file brin_internal.h.

◆ BRIN_PROCNUM_OPTIONS

#define BRIN_PROCNUM_OPTIONS   5 /* optional */

Definition at line 75 of file brin_internal.h.

◆ BRIN_PROCNUM_UNION

#define BRIN_PROCNUM_UNION   4

Definition at line 73 of file brin_internal.h.

◆ SizeofBrinOpcInfo

#define SizeofBrinOpcInfo (   ncols)     (offsetof(BrinOpcInfo, oi_typcache) + sizeof(TypeCacheEntry *) * ncols)

Definition at line 41 of file brin_internal.h.

Typedef Documentation

◆ BrinDesc

typedef struct BrinDesc BrinDesc

◆ BrinOpcInfo

typedef struct BrinOpcInfo BrinOpcInfo

Function Documentation

◆ 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, ObjectIdGetDatum(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:702
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:917
void pfree(void *pointer)
Definition: mcxt.c:1616
void * palloc(Size size)
Definition: mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:342
#define RelationGetDescr(relation)
Definition: rel.h:541
int bd_totalstored
Definition: brin_internal.h:59
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
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
Definition: fmgr.h:57
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

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, ObjectIdGetDatum(), BrinOpcInfo::oi_nstored, palloc(), palloc_array, pfree(), RelationGetDescr, and TupleDescAttr().

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

◆ 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}
Assert(PointerIsAligned(start, uint64))
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:472
int tdrefcount
Definition: tupdesc.h:140

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

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

◆ brinbeginscan()

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

Definition at line 540 of file brin.c.

541{
542 IndexScanDesc scan;
543 BrinOpaque *opaque;
544
545 scan = RelationGetIndexScan(r, nkeys, norderbys);
546
547 opaque = palloc_object(BrinOpaque);
549 opaque->bo_bdesc = brin_build_desc(r);
550 scan->opaque = opaque;
551
552 return scan;
553}
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 1106 of file brin.c.

1107{
1108 IndexBuildResult *result;
1109 double reltuples;
1110 double idxtuples;
1111 BrinRevmap *revmap;
1113 Buffer meta;
1114 BlockNumber pagesPerRange;
1115
1116 /*
1117 * We expect to be called exactly once for any index relation.
1118 */
1120 elog(ERROR, "index \"%s\" already contains data",
1122
1123 /*
1124 * Critical section not required, because on error the creation of the
1125 * whole relation will be rolled back.
1126 */
1127
1131
1134 MarkBufferDirty(meta);
1135
1137 {
1138 xl_brin_createidx xlrec;
1139 XLogRecPtr recptr;
1140 Page page;
1141
1144
1148
1149 recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
1150
1151 page = BufferGetPage(meta);
1152 PageSetLSN(page, recptr);
1153 }
1154
1155 UnlockReleaseBuffer(meta);
1156
1157 /*
1158 * Initialize our state, including the deformed tuple state.
1159 */
1160 revmap = brinRevmapInitialize(index, &pagesPerRange);
1161 state = initialize_brin_buildstate(index, revmap, pagesPerRange,
1163
1164 /*
1165 * Attempt to launch parallel worker scan when required
1166 *
1167 * XXX plan_create_index_workers makes the number of workers dependent on
1168 * maintenance_work_mem, requiring 32MB for each worker. That makes sense
1169 * for btree, but not for BRIN, which can do with much less memory. So
1170 * maybe make that somehow less strict, optionally?
1171 */
1172 if (indexInfo->ii_ParallelWorkers > 0)
1173 _brin_begin_parallel(state, heap, index, indexInfo->ii_Concurrent,
1174 indexInfo->ii_ParallelWorkers);
1175
1176 /*
1177 * If parallel build requested and at least one worker process was
1178 * successfully launched, set up coordination state, wait for workers to
1179 * complete. Then read all tuples from the shared tuplesort and insert
1180 * them into the index.
1181 *
1182 * In serial mode, simply scan the table and build the index one index
1183 * tuple at a time.
1184 */
1185 if (state->bs_leader)
1186 {
1187 SortCoordinate coordinate;
1188
1189 coordinate = palloc0_object(SortCoordinateData);
1190 coordinate->isWorker = false;
1191 coordinate->nParticipants =
1192 state->bs_leader->nparticipanttuplesorts;
1193 coordinate->sharedsort = state->bs_leader->sharedsort;
1194
1195 /*
1196 * Begin leader tuplesort.
1197 *
1198 * In cases where parallelism is involved, the leader receives the
1199 * same share of maintenance_work_mem as a serial sort (it is
1200 * generally treated in the same way as a serial sort once we return).
1201 * Parallel worker Tuplesortstates will have received only a fraction
1202 * of maintenance_work_mem, though.
1203 *
1204 * We rely on the lifetime of the Leader Tuplesortstate almost not
1205 * overlapping with any worker Tuplesortstate's lifetime. There may
1206 * be some small overlap, but that's okay because we rely on leader
1207 * Tuplesortstate only allocating a small, fixed amount of memory
1208 * here. When its tuplesort_performsort() is called (by our caller),
1209 * and significant amounts of memory are likely to be used, all
1210 * workers must have already freed almost all memory held by their
1211 * Tuplesortstates (they are about to go away completely, too). The
1212 * overall effect is that maintenance_work_mem always represents an
1213 * absolute high watermark on the amount of memory used by a CREATE
1214 * INDEX operation, regardless of the use of parallelism or any other
1215 * factor.
1216 */
1217 state->bs_sortstate =
1220
1221 /* scan the relation and merge per-worker results */
1222 reltuples = _brin_parallel_merge(state);
1223
1224 _brin_end_parallel(state->bs_leader, state);
1225 }
1226 else /* no parallel index build */
1227 {
1228 /*
1229 * Now scan the relation. No syncscan allowed here because we want
1230 * the heap blocks in physical order (we want to produce the ranges
1231 * starting from block 0, and the callback also relies on this to not
1232 * generate summary for the same range twice).
1233 */
1234 reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
1235 brinbuildCallback, state, NULL);
1236
1237 /*
1238 * process the final batch
1239 *
1240 * XXX Note this does not update state->bs_currRangeStart, i.e. it
1241 * stays set to the last range added to the index. This is OK, because
1242 * that's what brin_fill_empty_ranges expects.
1243 */
1245
1246 /*
1247 * Backfill the final ranges with empty data.
1248 *
1249 * This saves us from doing what amounts to full table scans when the
1250 * index with a predicate like WHERE (nonnull_column IS NULL), or
1251 * other very selective predicates.
1252 */
1254 state->bs_currRangeStart,
1255 state->bs_maxRangeStart);
1256 }
1257
1258 /* release resources */
1259 idxtuples = state->bs_numtuples;
1260 brinRevmapTerminate(state->bs_rmAccess);
1262
1263 /*
1264 * Return statistics
1265 */
1267
1268 result->heap_tuples = reltuples;
1269 result->index_tuples = idxtuples;
1270
1271 return result;
1272}
uint32 BlockNumber
Definition: block.h:31
static double _brin_parallel_merge(BrinBuildState *state)
Definition: brin.c:2635
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1717
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1986
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange, BlockNumber tablePages)
Definition: brin.c:1670
static void _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, bool isconcurrent, int request)
Definition: brin.c:2378
static void _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state)
Definition: brin.c:2563
static void brin_fill_empty_ranges(BrinBuildState *state, BlockNumber prevRange, BlockNumber nextRange)
Definition: brin.c:3008
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:996
#define BrinGetPagesPerRange(relation)
Definition: brin.h:41
#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:484
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
int Buffer
Definition: buf.h:23
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:4318
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:939
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5478
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:3037
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:294
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:436
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:75
@ EB_LOCK_FIRST
Definition: bufmgr.h:87
#define BMR_REL(p_rel)
Definition: bufmgr.h:114
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:390
PageData * Page
Definition: bufpage.h:81
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define palloc0_object(type)
Definition: fe_memutils.h:75
int maintenance_work_mem
Definition: globals.c:133
#define RelationGetRelationName(relation)
Definition: rel.h:549
#define RelationNeedsWAL(relation)
Definition: rel.h:638
@ MAIN_FORKNUM
Definition: relpath.h:58
double heap_tuples
Definition: genam.h:59
double index_tuples
Definition: genam.h:60
int ii_ParallelWorkers
Definition: execnodes.h:218
bool ii_Concurrent
Definition: execnodes.h:210
Sharedsort * sharedsort
Definition: tuplesort.h:59
Definition: type.h:96
Definition: regguts.h:323
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
static double table_index_build_scan(Relation table_rel, Relation index_rel, IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1754
#define TUPLESORT_NONE
Definition: tuplesort.h:94
Tuplesortstate * tuplesort_begin_index_brin(int workMem, SortCoordinate coordinate, int sortopt)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:478
void XLogRegisterData(const void *data, uint32 len)
Definition: xloginsert.c:368
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:245
void XLogBeginInsert(void)
Definition: xloginsert.c:152
#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_object, 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().

◆ brinbuildempty()

void brinbuildempty ( Relation  index)

Definition at line 1275 of file brin.c.

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

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 1304 of file brin.c.

1306{
1307 /* allocate stats if first time through, else re-use existing struct */
1308 if (stats == NULL)
1310
1311 return stats;
1312}

References palloc0_object.

Referenced by brinhandler().

◆ brinendscan()

void brinendscan ( IndexScanDesc  scan)

Definition at line 979 of file brin.c.

980{
981 BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
982
984 brin_free_desc(opaque->bo_bdesc);
985 pfree(opaque);
986}
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 568 of file brin.c.

569{
570 Relation idxRel = scan->indexRelation;
572 BrinDesc *bdesc;
573 Oid heapOid;
574 Relation heapRel;
575 BrinOpaque *opaque;
576 BlockNumber nblocks;
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. We make use of uint64 for heapBlk as a BlockNumber
739 * could wrap for tables with close to 2^32 pages.
740 */
741 for (uint64 heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
742 {
743 bool addrange;
744 bool gottuple = false;
745 BrinTuple *tup;
746 OffsetNumber off;
747 Size size;
748
750
751 MemoryContextReset(perRangeCxt);
752
753 tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, (BlockNumber) heapBlk, &buf,
754 &off, &size, BUFFER_LOCK_SHARE);
755 if (tup)
756 {
757 gottuple = true;
758 btup = brin_copy_tuple(tup, size, btup, &btupsz);
760 }
761
762 /*
763 * For page ranges with no indexed tuple, we must return the whole
764 * range; otherwise, compare it to the scan keys.
765 */
766 if (!gottuple)
767 {
768 addrange = true;
769 }
770 else
771 {
772 dtup = brin_deform_tuple(bdesc, btup, dtup);
773 if (dtup->bt_placeholder)
774 {
775 /*
776 * Placeholder tuples are always returned, regardless of the
777 * values stored in them.
778 */
779 addrange = true;
780 }
781 else
782 {
783 int attno;
784
785 /*
786 * Compare scan keys with summary values stored for the range.
787 * If scan keys are matched, the page range must be added to
788 * the bitmap. We initially assume the range needs to be
789 * added; in particular this serves the case where there are
790 * no keys.
791 */
792 addrange = true;
793 for (attno = 1; attno <= bdesc->bd_tupdesc->natts; attno++)
794 {
795 BrinValues *bval;
796 Datum add;
797 Oid collation;
798
799 /*
800 * skip attributes without any scan keys (both regular and
801 * IS [NOT] NULL)
802 */
803 if (nkeys[attno - 1] == 0 && nnullkeys[attno - 1] == 0)
804 continue;
805
806 bval = &dtup->bt_columns[attno - 1];
807
808 /*
809 * If the BRIN tuple indicates that this range is empty,
810 * we can skip it: there's nothing to match. We don't
811 * need to examine the next columns.
812 */
813 if (dtup->bt_empty_range)
814 {
815 addrange = false;
816 break;
817 }
818
819 /*
820 * First check if there are any IS [NOT] NULL scan keys,
821 * and if we're violating them. In that case we can
822 * terminate early, without invoking the support function.
823 *
824 * As there may be more keys, we can only determine
825 * mismatch within this loop.
826 */
827 if (bdesc->bd_info[attno - 1]->oi_regular_nulls &&
828 !check_null_keys(bval, nullkeys[attno - 1],
829 nnullkeys[attno - 1]))
830 {
831 /*
832 * If any of the IS [NOT] NULL keys failed, the page
833 * range as a whole can't pass. So terminate the loop.
834 */
835 addrange = false;
836 break;
837 }
838
839 /*
840 * So either there are no IS [NOT] NULL keys, or all
841 * passed. If there are no regular scan keys, we're done -
842 * the page range matches. If there are regular keys, but
843 * the page range is marked as 'all nulls' it can't
844 * possibly pass (we're assuming the operators are
845 * strict).
846 */
847
848 /* No regular scan keys - page range as a whole passes. */
849 if (!nkeys[attno - 1])
850 continue;
851
852 Assert((nkeys[attno - 1] > 0) &&
853 (nkeys[attno - 1] <= scan->numberOfKeys));
854
855 /* If it is all nulls, it cannot possibly be consistent. */
856 if (bval->bv_allnulls)
857 {
858 addrange = false;
859 break;
860 }
861
862 /*
863 * Collation from the first key (has to be the same for
864 * all keys for the same attribute).
865 */
866 collation = keys[attno - 1][0]->sk_collation;
867
868 /*
869 * Check whether the scan key is consistent with the page
870 * range values; if so, have the pages in the range added
871 * to the output bitmap.
872 *
873 * The opclass may or may not support processing of
874 * multiple scan keys. We can determine that based on the
875 * number of arguments - functions with extra parameter
876 * (number of scan keys) do support this, otherwise we
877 * have to simply pass the scan keys one by one.
878 */
879 if (consistentFn[attno - 1].fn_nargs >= 4)
880 {
881 /* Check all keys at once */
882 add = FunctionCall4Coll(&consistentFn[attno - 1],
883 collation,
884 PointerGetDatum(bdesc),
885 PointerGetDatum(bval),
886 PointerGetDatum(keys[attno - 1]),
887 Int32GetDatum(nkeys[attno - 1]));
888 addrange = DatumGetBool(add);
889 }
890 else
891 {
892 /*
893 * Check keys one by one
894 *
895 * When there are multiple scan keys, failure to meet
896 * the criteria for a single one of them is enough to
897 * discard the range as a whole, so break out of the
898 * loop as soon as a false return value is obtained.
899 */
900 int keyno;
901
902 for (keyno = 0; keyno < nkeys[attno - 1]; keyno++)
903 {
904 add = FunctionCall3Coll(&consistentFn[attno - 1],
905 keys[attno - 1][keyno]->sk_collation,
906 PointerGetDatum(bdesc),
907 PointerGetDatum(bval),
908 PointerGetDatum(keys[attno - 1][keyno]));
909 addrange = DatumGetBool(add);
910 if (!addrange)
911 break;
912 }
913 }
914
915 /*
916 * If we found a scan key eliminating the range, no need
917 * to check additional ones.
918 */
919 if (!addrange)
920 break;
921 }
922 }
923 }
924
925 /* add the pages in the range to the output bitmap, if needed */
926 if (addrange)
927 {
928 uint64 pageno;
929
930 for (pageno = heapBlk;
931 pageno <= Min(nblocks, heapBlk + opaque->bo_pagesPerRange) - 1;
932 pageno++)
933 {
934 MemoryContextSwitchTo(oldcxt);
935 tbm_add_page(tbm, pageno);
936 totalpages++;
937 MemoryContextSwitchTo(perRangeCxt);
938 }
939 }
940 }
941
942 MemoryContextSwitchTo(oldcxt);
943 MemoryContextDelete(perRangeCxt);
944
945 if (buf != InvalidBuffer)
947
948 /*
949 * XXX We have an approximation of the number of *pages* that our scan
950 * returns, but we don't have a precise idea of the number of heap tuples
951 * involved.
952 */
953 return totalpages * 10;
954}
int16 AttrNumber
Definition: attnum.h:21
static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
Definition: brin.c:2314
#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:445
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:481
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:552
#define InvalidBuffer
Definition: buf.h:25
void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition: bufmgr.c:5699
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5461
@ BUFFER_LOCK_SHARE
Definition: bufmgr.h:206
@ BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:205
#define Min(x, y)
Definition: c.h:1003
#define MAXALIGN(LEN)
Definition: c.h:832
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:229
int64_t int64
Definition: c.h:549
uint64_t uint64
Definition: c.h:553
size_t Size
Definition: c.h:625
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1197
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1172
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:581
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3581
int i
Definition: isn.c:77
#define AccessShareLock
Definition: lockdefs.h:36
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:403
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
uint16 OffsetNumber
Definition: off.h:24
const void size_t len
static char buf[DEFAULT_XLOG_SEG_SIZE]
Definition: pg_test_fsync.c:71
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:705
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:352
uint64_t Datum
Definition: postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
#define RelationGetRelid(relation)
Definition: rel.h:515
#define SK_ISNULL
Definition: skey.h:115
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:55
bool bt_placeholder
Definition: brin_tuple.h:46
bool bt_empty_range
Definition: brin_tuple.h:47
bool oi_regular_nulls
Definition: brin_internal.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
struct ScanKeyData * keyData
Definition: relscan.h:143
struct IndexScanInstrumentation * instrument
Definition: relscan.h:161
Relation indexRelation
Definition: relscan.h:139
Oid sk_collation
Definition: skey.h:70
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
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().

◆ brininsert()

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

Definition at line 345 of file brin.c.

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

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 513 of file brin.c.

514{
515 BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache;
516
517 /* bail out if cache not initialized */
518 if (bistate == NULL)
519 return;
520
521 /* do this first to avoid dangling pointer if we fail partway through */
522 indexInfo->ii_AmCache = NULL;
523
524 /*
525 * Clean up the revmap. Note that the brinDesc has already been cleaned up
526 * as part of its own memory context.
527 */
529 pfree(bistate);
530}
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 1349 of file brin.c.

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

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 960 of file brin.c.

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

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

Referenced by brinhandler().

◆ brinvacuumcleanup()

IndexBulkDeleteResult * brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 1319 of file brin.c.

1320{
1321 Relation heapRel;
1322
1323 /* No-op in ANALYZE ONLY mode */
1324 if (info->analyze_only)
1325 return stats;
1326
1327 if (!stats)
1330 /* rest of stats is initialized by zeroing */
1331
1332 heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
1334
1335 brin_vacuum_scan(info->index, info->strategy);
1336
1337 brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
1338 &stats->num_index_tuples, &stats->num_index_tuples);
1339
1340 table_close(heapRel, AccessShareLock);
1341
1342 return stats;
1343}
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:2173
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:209
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1888
BlockNumber num_pages
Definition: genam.h:104
double num_index_tuples
Definition: genam.h:106
Relation index
Definition: genam.h:73
bool analyze_only
Definition: genam.h:75
BufferAccessStrategy strategy
Definition: genam.h:80

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().

◆ brinvalidate()

bool brinvalidate ( Oid  opclassoid)

Definition at line 37 of file brin_validate.c.

38{
39 bool result = true;
40 HeapTuple classtup;
41 Form_pg_opclass classform;
42 Oid opfamilyoid;
43 Oid opcintype;
44 char *opclassname;
45 char *opfamilyname;
46 CatCList *proclist,
47 *oprlist;
48 uint64 allfuncs = 0;
49 uint64 allops = 0;
50 List *grouplist;
51 OpFamilyOpFuncGroup *opclassgroup;
52 int i;
53 ListCell *lc;
54
55 /* Fetch opclass information */
56 classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
57 if (!HeapTupleIsValid(classtup))
58 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
59 classform = (Form_pg_opclass) GETSTRUCT(classtup);
60
61 opfamilyoid = classform->opcfamily;
62 opcintype = classform->opcintype;
63 opclassname = NameStr(classform->opcname);
64
65 /* Fetch opfamily information */
66 opfamilyname = get_opfamily_name(opfamilyoid, false);
67
68 /* Fetch all operators and support functions of the opfamily */
69 oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
70 proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
71
72 /* Check individual support functions */
73 for (i = 0; i < proclist->n_members; i++)
74 {
75 HeapTuple proctup = &proclist->members[i]->tuple;
76 Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
77 bool ok;
78
79 /* Check procedure numbers and function signatures */
80 switch (procform->amprocnum)
81 {
83 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
84 1, 1, INTERNALOID);
85 break;
87 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
88 4, 4, INTERNALOID, INTERNALOID,
89 INTERNALOID, INTERNALOID);
90 break;
92 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
93 3, 4, INTERNALOID, INTERNALOID,
94 INTERNALOID, INT4OID);
95 break;
97 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
98 3, 3, INTERNALOID, INTERNALOID,
99 INTERNALOID);
100 break;
102 ok = check_amoptsproc_signature(procform->amproc);
103 break;
104 default:
105 /* Complain if it's not a valid optional proc number */
106 if (procform->amprocnum < BRIN_FIRST_OPTIONAL_PROCNUM ||
107 procform->amprocnum > BRIN_LAST_OPTIONAL_PROCNUM)
108 {
110 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
111 errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
112 opfamilyname, "brin",
113 format_procedure(procform->amproc),
114 procform->amprocnum)));
115 result = false;
116 continue; /* omit bad proc numbers from allfuncs */
117 }
118 /* Can't check signatures of optional procs, so assume OK */
119 ok = true;
120 break;
121 }
122
123 if (!ok)
124 {
126 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
127 errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
128 opfamilyname, "brin",
129 format_procedure(procform->amproc),
130 procform->amprocnum)));
131 result = false;
132 }
133
134 /* Track all valid procedure numbers seen in opfamily */
135 allfuncs |= ((uint64) 1) << procform->amprocnum;
136 }
137
138 /* Check individual operators */
139 for (i = 0; i < oprlist->n_members; i++)
140 {
141 HeapTuple oprtup = &oprlist->members[i]->tuple;
142 Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
143
144 /* Check that only allowed strategy numbers exist */
145 if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
146 {
148 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
149 errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
150 opfamilyname, "brin",
151 format_operator(oprform->amopopr),
152 oprform->amopstrategy)));
153 result = false;
154 }
155 else
156 {
157 /*
158 * The set of operators supplied varies across BRIN opfamilies.
159 * Our plan is to identify all operator strategy numbers used in
160 * the opfamily and then complain about datatype combinations that
161 * are missing any operator(s). However, consider only numbers
162 * that appear in some non-cross-type case, since cross-type
163 * operators may have unique strategies. (This is not a great
164 * heuristic, in particular an erroneous number used in a
165 * cross-type operator will not get noticed; but the core BRIN
166 * opfamilies are messy enough to make it necessary.)
167 */
168 if (oprform->amoplefttype == oprform->amoprighttype)
169 allops |= ((uint64) 1) << oprform->amopstrategy;
170 }
171
172 /* brin doesn't support ORDER BY operators */
173 if (oprform->amoppurpose != AMOP_SEARCH ||
174 OidIsValid(oprform->amopsortfamily))
175 {
177 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
178 errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
179 opfamilyname, "brin",
180 format_operator(oprform->amopopr))));
181 result = false;
182 }
183
184 /* Check operator signature --- same for all brin strategies */
185 if (!check_amop_signature(oprform->amopopr, BOOLOID,
186 oprform->amoplefttype,
187 oprform->amoprighttype))
188 {
190 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
191 errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
192 opfamilyname, "brin",
193 format_operator(oprform->amopopr))));
194 result = false;
195 }
196 }
197
198 /* Now check for inconsistent groups of operators/functions */
199 grouplist = identify_opfamily_groups(oprlist, proclist);
200 opclassgroup = NULL;
201 foreach(lc, grouplist)
202 {
204
205 /* Remember the group exactly matching the test opclass */
206 if (thisgroup->lefttype == opcintype &&
207 thisgroup->righttype == opcintype)
208 opclassgroup = thisgroup;
209
210 /*
211 * Some BRIN opfamilies expect cross-type support functions to exist,
212 * and some don't. We don't know exactly which are which, so if we
213 * find a cross-type operator for which there are no support functions
214 * at all, let it pass. (Don't expect that all operators exist for
215 * such cross-type cases, either.)
216 */
217 if (thisgroup->functionset == 0 &&
218 thisgroup->lefttype != thisgroup->righttype)
219 continue;
220
221 /*
222 * Else complain if there seems to be an incomplete set of either
223 * operators or support functions for this datatype pair.
224 */
225 if (thisgroup->operatorset != allops)
226 {
228 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
229 errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
230 opfamilyname, "brin",
231 format_type_be(thisgroup->lefttype),
232 format_type_be(thisgroup->righttype))));
233 result = false;
234 }
235 if (thisgroup->functionset != allfuncs)
236 {
238 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
239 errmsg("operator family \"%s\" of access method %s is missing support function(s) for types %s and %s",
240 opfamilyname, "brin",
241 format_type_be(thisgroup->lefttype),
242 format_type_be(thisgroup->righttype))));
243 result = false;
244 }
245 }
246
247 /* Check that the originally-named opclass is complete */
248 if (!opclassgroup || opclassgroup->operatorset != allops)
249 {
251 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
252 errmsg("operator class \"%s\" of access method %s is missing operator(s)",
253 opclassname, "brin")));
254 result = false;
255 }
256 for (i = 1; i <= BRIN_MANDATORY_NPROCS; i++)
257 {
258 if (opclassgroup &&
259 (opclassgroup->functionset & (((int64) 1) << i)) != 0)
260 continue; /* got it */
262 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
263 errmsg("operator class \"%s\" of access method %s is missing support function %d",
264 opclassname, "brin", i)));
265 result = false;
266 }
267
268 ReleaseCatCacheList(proclist);
269 ReleaseCatCacheList(oprlist);
270 ReleaseSysCache(classtup);
271
272 return result;
273}
bool check_amproc_signature(Oid funcid, Oid restype, bool exact, int minargs, int maxargs,...)
Definition: amvalidate.c:152
bool check_amop_signature(Oid opno, Oid restype, Oid lefttype, Oid righttype)
Definition: amvalidate.c:206
List * identify_opfamily_groups(CatCList *oprlist, CatCList *proclist)
Definition: amvalidate.c:43
bool check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:192
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:78
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:73
#define BRIN_MANDATORY_NPROCS
Definition: brin_internal.h:74
#define BRIN_PROCNUM_OPTIONS
Definition: brin_internal.h:75
#define BRIN_FIRST_OPTIONAL_PROCNUM
Definition: brin_internal.h:77
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:71
#define NameStr(name)
Definition: c.h:771
#define OidIsValid(objectId)
Definition: c.h:794
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:2114
#define INFO
Definition: elog.h:34
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
char * get_opfamily_name(Oid opfid, bool missing_ok)
Definition: lsyscache.c:1403
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.h:68
#define lfirst(lc)
Definition: pg_list.h:172
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:305
char * format_operator(Oid operator_oid)
Definition: regproc.c:801
Definition: pg_list.h:54
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:182
int n_members
Definition: catcache.h:180
HeapTupleData tuple
Definition: catcache.h:124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:127

References BRIN_FIRST_OPTIONAL_PROCNUM, BRIN_LAST_OPTIONAL_PROCNUM, BRIN_MANDATORY_NPROCS, BRIN_PROCNUM_ADDVALUE, BRIN_PROCNUM_CONSISTENT, BRIN_PROCNUM_OPCINFO, BRIN_PROCNUM_OPTIONS, BRIN_PROCNUM_UNION, check_amop_signature(), check_amoptsproc_signature(), check_amproc_signature(), elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), format_type_be(), OpFamilyOpFuncGroup::functionset, get_opfamily_name(), GETSTRUCT(), HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, catclist::members, catclist::n_members, NameStr, ObjectIdGetDatum(), OidIsValid, OpFamilyOpFuncGroup::operatorset, ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by brinhandler().