PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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.

44{
45 /* Containing memory context */
46 MemoryContext bd_context;
47
48 /* the index relation itself */
49 Relation bd_index;
50
51 /* tuple descriptor of the index relation */
52 TupleDesc bd_tupdesc;
53
54 /* cached copy for on-disk tuples; generated at first use */
55 TupleDesc bd_disktdesc;
56
57 /* total number of Datum entries that are stored on-disk for all columns */
58 int bd_totalstored;
59
60 /* per-column info; bd_tupdesc->natts entries long */
62} BrinDesc;
63
64/*
65 * Globally-known function support numbers for BRIN indexes. Individual
66 * opclasses can define more function support numbers, which must fall into
67 * BRIN_FIRST_OPTIONAL_PROCNUM .. BRIN_LAST_OPTIONAL_PROCNUM.
68 */
69#define BRIN_PROCNUM_OPCINFO 1
70#define BRIN_PROCNUM_ADDVALUE 2
71#define BRIN_PROCNUM_CONSISTENT 3
72#define BRIN_PROCNUM_UNION 4
73#define BRIN_MANDATORY_NPROCS 4
74#define BRIN_PROCNUM_OPTIONS 5 /* optional */
75/* procedure numbers up to 10 are reserved for BRIN future expansion */
76#define BRIN_FIRST_OPTIONAL_PROCNUM 11
77#define BRIN_LAST_OPTIONAL_PROCNUM 15
78
79#undef BRIN_DEBUG
80
81#ifdef BRIN_DEBUG
82#define BRIN_elog(args) elog args
83#else
84#define BRIN_elog(args) ((void) 0)
85#endif
86
87/* brin.c */
89extern void brin_free_desc(BrinDesc *bdesc);
91 IndexInfo *indexInfo);
92extern void brinbuildempty(Relation index);
93extern bool brininsert(Relation idxRel, Datum *values, bool *nulls,
96 bool indexUnchanged,
97 IndexInfo *indexInfo);
98extern void brininsertcleanup(Relation index, IndexInfo *indexInfo);
99extern IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys);
100extern int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm);
101extern void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
102 ScanKey orderbys, int norderbys);
103extern void brinendscan(IndexScanDesc scan);
107 void *callback_state);
109 IndexBulkDeleteResult *stats);
110extern bytea *brinoptions(Datum reloptions, bool validate);
111
112/* brin_validate.c */
113extern bool brinvalidate(Oid opclassoid);
114
115#endif /* BRIN_INTERNAL_H */
static bool validate(Port *port, const char *auth)
Definition auth-oauth.c:638
static Datum values[MAXATTR]
Definition bootstrap.c:147
void brininsertcleanup(Relation index, IndexInfo *indexInfo)
Definition brin.c:514
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition brin.c:961
bool brinvalidate(Oid opclassoid)
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition brin.c:1305
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition brin.c:541
bytea * brinoptions(Datum reloptions, bool validate)
Definition brin.c:1350
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition brin.c:569
void brinbuildempty(Relation index)
Definition brin.c:1276
void brin_free_desc(BrinDesc *bdesc)
Definition brin.c:1638
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition brin.c:1107
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition brin.c:1320
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition brin.c:346
BrinDesc * brin_build_desc(Relation rel)
Definition brin.c:1583
void brinendscan(IndexScanDesc scan)
Definition brin.c:980
int64_t int64
Definition c.h:555
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:492
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition genam.h:93
IndexUniqueCheck
Definition genam.h:122
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
Definition type.h:96
Definition c.h:718
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)

Typedef Documentation

◆ BrinDesc

◆ BrinOpcInfo

Function Documentation

◆ brin_build_desc()

BrinDesc * brin_build_desc ( Relation  rel)
extern

Definition at line 1583 of file brin.c.

1584{
1586 BrinDesc *bdesc;
1587 TupleDesc tupdesc;
1588 int totalstored = 0;
1589 int keyno;
1590 long totalsize;
1591 MemoryContext cxt;
1593
1595 "brin desc cxt",
1598 tupdesc = RelationGetDescr(rel);
1599
1600 /*
1601 * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1602 * the number of columns stored, since the number is opclass-defined.
1603 */
1604 opcinfo = palloc_array(BrinOpcInfo *, tupdesc->natts);
1605 for (keyno = 0; keyno < tupdesc->natts; keyno++)
1606 {
1608 Form_pg_attribute attr = TupleDescAttr(tupdesc, keyno);
1609
1611
1612 opcinfo[keyno] = (BrinOpcInfo *)
1614 totalstored += opcinfo[keyno]->oi_nstored;
1615 }
1616
1617 /* Allocate our result struct and fill it in */
1618 totalsize = offsetof(BrinDesc, bd_info) +
1619 sizeof(BrinOpcInfo *) * tupdesc->natts;
1620
1621 bdesc = palloc(totalsize);
1622 bdesc->bd_context = cxt;
1623 bdesc->bd_index = rel;
1624 bdesc->bd_tupdesc = tupdesc;
1625 bdesc->bd_disktdesc = NULL; /* generated lazily */
1626 bdesc->bd_totalstored = totalstored;
1627
1628 for (keyno = 0; keyno < tupdesc->natts; keyno++)
1629 bdesc->bd_info[keyno] = opcinfo[keyno];
1630 pfree(opcinfo);
1631
1633
1634 return bdesc;
1635}
#define BRIN_PROCNUM_OPCINFO
#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
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define RelationGetDescr(relation)
Definition rel.h:540
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, BRIN_PROCNUM_OPCINFO, CurrentMemoryContext, DatumGetPointer(), fb(), FunctionCall1, index_getprocinfo(), MemoryContextSwitchTo(), TupleDescData::natts, ObjectIdGetDatum(), 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)
extern

Definition at line 1638 of file brin.c.

1639{
1640 /* make sure the tupdesc is still valid */
1641 Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1642 /* no need for retail pfree */
1643 MemoryContextDelete(bdesc->bd_context);
1644}
#define Assert(condition)
Definition c.h:885
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472

References Assert, fb(), and MemoryContextDelete().

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

◆ brinbeginscan()

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

Definition at line 541 of file brin.c.

542{
543 IndexScanDesc scan;
544 BrinOpaque *opaque;
545
546 scan = RelationGetIndexScan(r, nkeys, norderbys);
547
548 opaque = palloc_object(BrinOpaque);
550 opaque->bo_bdesc = brin_build_desc(r);
551 scan->opaque = opaque;
552
553 return scan;
554}
BrinDesc * brin_build_desc(Relation rel)
Definition brin.c:1583
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:205
BrinDesc * bo_bdesc
Definition brin.c:207
BrinRevmap * bo_rmAccess
Definition brin.c:206

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 
)
extern

Definition at line 1107 of file brin.c.

1108{
1109 IndexBuildResult *result;
1110 double reltuples;
1111 double idxtuples;
1114 Buffer meta;
1115 BlockNumber pagesPerRange;
1116
1117 /*
1118 * We expect to be called exactly once for any index relation.
1119 */
1121 elog(ERROR, "index \"%s\" already contains data",
1123
1124 /*
1125 * Critical section not required, because on error the creation of the
1126 * whole relation will be rolled back.
1127 */
1128
1132
1135 MarkBufferDirty(meta);
1136
1138 {
1141 Page page;
1142
1144 xlrec.pagesPerRange = BrinGetPagesPerRange(index);
1145
1149
1151
1152 page = BufferGetPage(meta);
1153 PageSetLSN(page, recptr);
1154 }
1155
1156 UnlockReleaseBuffer(meta);
1157
1158 /*
1159 * Initialize our state, including the deformed tuple state.
1160 */
1161 revmap = brinRevmapInitialize(index, &pagesPerRange);
1162 state = initialize_brin_buildstate(index, revmap, pagesPerRange,
1164
1165 /*
1166 * Attempt to launch parallel worker scan when required
1167 *
1168 * XXX plan_create_index_workers makes the number of workers dependent on
1169 * maintenance_work_mem, requiring 32MB for each worker. That makes sense
1170 * for btree, but not for BRIN, which can do with much less memory. So
1171 * maybe make that somehow less strict, optionally?
1172 */
1173 if (indexInfo->ii_ParallelWorkers > 0)
1174 _brin_begin_parallel(state, heap, index, indexInfo->ii_Concurrent,
1175 indexInfo->ii_ParallelWorkers);
1176
1177 /*
1178 * If parallel build requested and at least one worker process was
1179 * successfully launched, set up coordination state, wait for workers to
1180 * complete. Then read all tuples from the shared tuplesort and insert
1181 * them into the index.
1182 *
1183 * In serial mode, simply scan the table and build the index one index
1184 * tuple at a time.
1185 */
1186 if (state->bs_leader)
1187 {
1189
1191 coordinate->isWorker = false;
1192 coordinate->nParticipants =
1193 state->bs_leader->nparticipanttuplesorts;
1194 coordinate->sharedsort = state->bs_leader->sharedsort;
1195
1196 /*
1197 * Begin leader tuplesort.
1198 *
1199 * In cases where parallelism is involved, the leader receives the
1200 * same share of maintenance_work_mem as a serial sort (it is
1201 * generally treated in the same way as a serial sort once we return).
1202 * Parallel worker Tuplesortstates will have received only a fraction
1203 * of maintenance_work_mem, though.
1204 *
1205 * We rely on the lifetime of the Leader Tuplesortstate almost not
1206 * overlapping with any worker Tuplesortstate's lifetime. There may
1207 * be some small overlap, but that's okay because we rely on leader
1208 * Tuplesortstate only allocating a small, fixed amount of memory
1209 * here. When its tuplesort_performsort() is called (by our caller),
1210 * and significant amounts of memory are likely to be used, all
1211 * workers must have already freed almost all memory held by their
1212 * Tuplesortstates (they are about to go away completely, too). The
1213 * overall effect is that maintenance_work_mem always represents an
1214 * absolute high watermark on the amount of memory used by a CREATE
1215 * INDEX operation, regardless of the use of parallelism or any other
1216 * factor.
1217 */
1218 state->bs_sortstate =
1221
1222 /* scan the relation and merge per-worker results */
1223 reltuples = _brin_parallel_merge(state);
1224
1225 _brin_end_parallel(state->bs_leader, state);
1226 }
1227 else /* no parallel index build */
1228 {
1229 /*
1230 * Now scan the relation. No syncscan allowed here because we want
1231 * the heap blocks in physical order (we want to produce the ranges
1232 * starting from block 0, and the callback also relies on this to not
1233 * generate summary for the same range twice).
1234 */
1235 reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
1237
1238 /*
1239 * process the final batch
1240 *
1241 * XXX Note this does not update state->bs_currRangeStart, i.e. it
1242 * stays set to the last range added to the index. This is OK, because
1243 * that's what brin_fill_empty_ranges expects.
1244 */
1246
1247 /*
1248 * Backfill the final ranges with empty data.
1249 *
1250 * This saves us from doing what amounts to full table scans when the
1251 * index with a predicate like WHERE (nonnull_column IS NULL), or
1252 * other very selective predicates.
1253 */
1255 state->bs_currRangeStart,
1256 state->bs_maxRangeStart);
1257 }
1258
1259 /* release resources */
1260 idxtuples = state->bs_numtuples;
1261 brinRevmapTerminate(state->bs_rmAccess);
1263
1264 /*
1265 * Return statistics
1266 */
1268
1269 result->heap_tuples = reltuples;
1270 result->index_tuples = idxtuples;
1271
1272 return result;
1273}
uint32 BlockNumber
Definition block.h:31
static double _brin_parallel_merge(BrinBuildState *state)
Definition brin.c:2636
static void terminate_brin_buildstate(BrinBuildState *state)
Definition brin.c:1718
static void form_and_insert_tuple(BrinBuildState *state)
Definition brin.c:1987
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange, BlockNumber tablePages)
Definition brin.c:1671
static void _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, bool isconcurrent, int request)
Definition brin.c:2379
static void _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state)
Definition brin.c:2564
static void brin_fill_empty_ranges(BrinBuildState *state, BlockNumber prevRange, BlockNumber nextRange)
Definition brin.c:3009
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition brin.c:997
#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)
void brinRevmapTerminate(BrinRevmap *revmap)
#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:4357
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition bufmgr.c:965
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5519
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3057
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:466
@ 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:548
#define RelationNeedsWAL(relation)
Definition rel.h:637
@ MAIN_FORKNUM
Definition relpath.h:58
double heap_tuples
Definition genam.h:38
double index_tuples
Definition genam.h:39
int ii_ParallelWorkers
Definition execnodes.h:220
bool ii_Concurrent
Definition execnodes.h:212
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:1765
#define TUPLESORT_NONE
Definition tuplesort.h:67
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(), fb(), form_and_insert_tuple(), IndexBuildResult::heap_tuples, IndexInfo::ii_Concurrent, IndexInfo::ii_ParallelWorkers, IndexBuildResult::index_tuples, initialize_brin_buildstate(), MAIN_FORKNUM, maintenance_work_mem, MarkBufferDirty(), PageSetLSN(), palloc0_object, palloc_object, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, 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)
extern

Definition at line 1276 of file brin.c.

1277{
1279
1280 /* An empty BRIN index has a metapage only. */
1283
1284 /* Initialize and xlog metabuffer. */
1291
1293}
#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)

References BMR_REL, BRIN_CURRENT_VERSION, brin_metapage_init(), BrinGetPagesPerRange, BufferGetPage(), EB_LOCK_FIRST, EB_SKIP_EXTENSION_LOCK, END_CRIT_SECTION, ExtendBufferedRel(), fb(), 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 
)
extern

Definition at line 1305 of file brin.c.

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

References fb(), and palloc0_object.

Referenced by brinhandler().

◆ brinendscan()

void brinendscan ( IndexScanDesc  scan)
extern

Definition at line 980 of file brin.c.

981{
982 BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
983
984 brinRevmapTerminate(opaque->bo_rmAccess);
985 brin_free_desc(opaque->bo_bdesc);
986 pfree(opaque);
987}
void brin_free_desc(BrinDesc *bdesc)
Definition brin.c:1638

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 
)
extern

Definition at line 569 of file brin.c.

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

Definition at line 346 of file brin.c.

351{
352 BlockNumber pagesPerRange;
354 BlockNumber heapBlk;
355 BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache;
361 bool autosummarize = BrinGetAutoSummarize(idxRel);
362
363 /*
364 * If first time through in this statement, initialize the insert state
365 * that we keep for all the inserts in the command.
366 */
367 if (!bistate)
368 bistate = initialize_brin_insertstate(idxRel, indexInfo);
369
370 revmap = bistate->bis_rmAccess;
371 bdesc = bistate->bis_desc;
372 pagesPerRange = bistate->bis_pages_per_range;
373
374 /*
375 * origHeapBlk is the block number where the insertion occurred. heapBlk
376 * is the first block in the corresponding page range.
377 */
379 heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
380
381 for (;;)
382 {
383 bool need_insert = false;
384 OffsetNumber off;
387
389
390 /*
391 * If auto-summarization is enabled and we just inserted the first
392 * tuple into the first block of a new non-first page range, request a
393 * summarization run of the previous range.
394 */
395 if (autosummarize &&
396 heapBlk > 0 &&
397 heapBlk == origHeapBlk &&
399 {
400 BlockNumber lastPageRange = heapBlk - 1;
402
406 if (!lastPageTuple)
407 {
408 bool recorded;
409
413 if (!recorded)
414 ereport(LOG,
416 errmsg("request for BRIN range summarization for index \"%s\" page %u was not recorded",
418 lastPageRange)));
419 }
420 else
422 }
423
424 brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
426
427 /* if range is unsummarized, there's nothing to do */
428 if (!brtup)
429 break;
430
431 /* First time through in this brininsert call? */
432 if (tupcxt == NULL)
433 {
435 "brininsert cxt",
438 }
439
441
443
444 if (!need_insert)
445 {
446 /*
447 * The tuple is consistent with the new values, so there's nothing
448 * to do.
449 */
451 }
452 else
453 {
454 Page page = BufferGetPage(buf);
455 ItemId lp = PageGetItemId(page, off);
456 Size origsz;
458 Size newsz;
460 bool samepage;
461
462 /*
463 * Make a copy of the old tuple, so that we can compare it after
464 * re-acquiring the lock.
465 */
468
469 /*
470 * Before releasing the lock, check if we can attempt a same-page
471 * update. Another process could insert a tuple concurrently in
472 * the same page though, so downstream we must be prepared to cope
473 * if this turns out to not be possible after all.
474 */
475 newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
478
479 /*
480 * Try to update the tuple. If this doesn't work for whatever
481 * reason, we need to restart from the top; the revmap might be
482 * pointing at a different tuple for this block now, so we need to
483 * recompute to ensure both our new heap tuple and the other
484 * inserter's are covered by the combined tuple. It might be that
485 * we don't need to update at all.
486 */
487 if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
488 buf, off, origtup, origsz, newtup, newsz,
489 samepage))
490 {
491 /* no luck; start over */
493 continue;
494 }
495 }
496
497 /* success! */
498 break;
499 }
500
501 if (BufferIsValid(buf))
504 if (tupcxt != NULL)
506
507 return false;
508}
bool AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
@ AVW_BRINSummarizeRange
Definition autovacuum.h:25
static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, const Datum *values, const bool *nulls)
Definition brin.c:2221
static BrinInsertState * initialize_brin_insertstate(Relation idxRel, IndexInfo *indexInfo)
Definition brin.c:317
#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)
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
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:417
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:243
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#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
void * ii_AmCache
Definition execnodes.h:225

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(), fb(), 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 
)
extern

Definition at line 514 of file brin.c.

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

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

Referenced by brinhandler().

◆ brinoptions()

bytea * brinoptions ( Datum  reloptions,
bool  validate 
)
extern

Definition at line 1350 of file brin.c.

1351{
1352 static const relopt_parse_elt tab[] = {
1353 {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
1354 {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
1355 };
1356
1357 return (bytea *) build_reloptions(reloptions, validate,
1359 sizeof(BrinOptions),
1360 tab, lengthof(tab));
1361}
#define lengthof(array)
Definition c.h:815
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
@ RELOPT_KIND_BRIN
Definition reloptions.h:53
@ RELOPT_TYPE_INT
Definition reloptions.h:33
@ RELOPT_TYPE_BOOL
Definition reloptions.h:31

References build_reloptions(), fb(), 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 
)
extern

Definition at line 961 of file brin.c.

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

References fb(), IndexScanDescData::keyData, and IndexScanDescData::numberOfKeys.

Referenced by brinhandler().

◆ brinvacuumcleanup()

IndexBulkDeleteResult * brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)
extern

Definition at line 1320 of file brin.c.

1321{
1322 Relation heapRel;
1323
1324 /* No-op in ANALYZE ONLY mode */
1325 if (info->analyze_only)
1326 return stats;
1327
1328 if (!stats)
1331 /* rest of stats is initialized by zeroing */
1332
1333 heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
1335
1336 brin_vacuum_scan(info->index, info->strategy);
1337
1338 brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
1339 &stats->num_index_tuples, &stats->num_index_tuples);
1340
1341 table_close(heapRel, AccessShareLock);
1342
1343 return stats;
1344}
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition brin.c:2174
#define BRIN_ALL_BLOCKRANGES
Definition brin.c:210
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition brin.c:1889
BlockNumber num_pages
Definition genam.h:83
double num_index_tuples
Definition genam.h:85
Relation index
Definition genam.h:52
bool analyze_only
Definition genam.h:54
BufferAccessStrategy strategy
Definition genam.h:59

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)
extern

Definition at line 37 of file brin_validate.c.

38{
39 bool result = true;
43 Oid opcintype;
44 char *opclassname;
45 char *opfamilyname;
47 *oprlist;
48 uint64 allfuncs = 0;
49 uint64 allops = 0;
52 int i;
53 ListCell *lc;
54
55 /* Fetch opclass information */
58 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
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 */
71
72 /* Check individual support functions */
73 for (i = 0; i < proclist->n_members; i++)
74 {
75 HeapTuple proctup = &proclist->members[i]->tuple;
77 bool ok;
78
79 /* Check procedure numbers and function signatures */
80 switch (procform->amprocnum)
81 {
84 1, 1, INTERNALOID);
85 break;
90 break;
95 break;
100 break;
103 break;
104 default:
105 /* Complain if it's not a valid optional proc number */
106 if (procform->amprocnum < BRIN_FIRST_OPTIONAL_PROCNUM ||
108 {
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 {
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;
143
144 /* Check that only allowed strategy numbers exist */
145 if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
146 {
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 {
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 {
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 */
201 foreach(lc, grouplist)
202 {
204
205 /* Remember the group exactly matching the test opclass */
206 if (thisgroup->lefttype == opcintype &&
207 thisgroup->righttype == opcintype)
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 {
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 {
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 {
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 */
263 errmsg("operator class \"%s\" of access method %s is missing support function %d",
264 opclassname, "brin", i)));
265 result = false;
266 }
267
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
#define BRIN_PROCNUM_UNION
#define BRIN_MANDATORY_NPROCS
#define BRIN_PROCNUM_OPTIONS
#define BRIN_FIRST_OPTIONAL_PROCNUM
#define BRIN_PROCNUM_ADDVALUE
#define NameStr(name)
Definition c.h:777
#define OidIsValid(objectId)
Definition c.h:800
void ReleaseCatCacheList(CatCList *list)
Definition catcache.c:2114
#define INFO
Definition elog.h:34
char * format_type_be(Oid type_oid)
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
char * get_opfamily_name(Oid opfid, bool missing_ok)
Definition lsyscache.c:1403
END_CATALOG_STRUCT typedef FormData_pg_amop * Form_pg_amop
Definition pg_amop.h:92
END_CATALOG_STRUCT typedef FormData_pg_amproc * Form_pg_amproc
Definition pg_amproc.h:72
#define lfirst(lc)
Definition pg_list.h:172
END_CATALOG_STRUCT typedef FormData_pg_opclass * Form_pg_opclass
Definition pg_opclass.h:87
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
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier 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, fb(), Form_pg_amop, Form_pg_amproc, Form_pg_opclass, format_operator(), format_procedure(), format_type_be(), get_opfamily_name(), GETSTRUCT(), HeapTupleIsValid, i, identify_opfamily_groups(), INFO, lfirst, NameStr, ObjectIdGetDatum(), OidIsValid, ReleaseCatCacheList(), ReleaseSysCache(), SearchSysCache1(), and SearchSysCacheList1.

Referenced by brinhandler().