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, struct IndexInfo *indexInfo)
 
void brinbuildempty (Relation index)
 
bool brininsert (Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, struct 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)

◆ BRIN_FIRST_OPTIONAL_PROCNUM

#define BRIN_FIRST_OPTIONAL_PROCNUM   11

Definition at line 77 of file brin_internal.h.

Referenced by brinvalidate().

◆ BRIN_LAST_OPTIONAL_PROCNUM

#define BRIN_LAST_OPTIONAL_PROCNUM   15

Definition at line 78 of file brin_internal.h.

Referenced by brinhandler(), and brinvalidate().

◆ BRIN_MANDATORY_NPROCS

#define BRIN_MANDATORY_NPROCS   4

Definition at line 74 of file brin_internal.h.

Referenced by brinvalidate().

◆ BRIN_PROCNUM_ADDVALUE

#define BRIN_PROCNUM_ADDVALUE   2

Definition at line 71 of file brin_internal.h.

Referenced by add_values_to_range(), and brinvalidate().

◆ BRIN_PROCNUM_CONSISTENT

#define BRIN_PROCNUM_CONSISTENT   3

Definition at line 72 of file brin_internal.h.

Referenced by bringetbitmap(), and brinvalidate().

◆ BRIN_PROCNUM_OPCINFO

#define BRIN_PROCNUM_OPCINFO   1

Definition at line 70 of file brin_internal.h.

Referenced by brin_build_desc(), and brinvalidate().

◆ BRIN_PROCNUM_OPTIONS

#define BRIN_PROCNUM_OPTIONS   5 /* optional */

Definition at line 75 of file brin_internal.h.

Referenced by brinhandler(), and brinvalidate().

◆ BRIN_PROCNUM_UNION

#define BRIN_PROCNUM_UNION   4

Definition at line 73 of file brin_internal.h.

Referenced by brinvalidate(), and union_tuples().

◆ SizeofBrinOpcInfo

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

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

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

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

1161 {
1162  BrinOpcInfo **opcinfo;
1163  BrinDesc *bdesc;
1164  TupleDesc tupdesc;
1165  int totalstored = 0;
1166  int keyno;
1167  long totalsize;
1168  MemoryContext cxt;
1169  MemoryContext oldcxt;
1170 
1172  "brin desc cxt",
1174  oldcxt = MemoryContextSwitchTo(cxt);
1175  tupdesc = RelationGetDescr(rel);
1176 
1177  /*
1178  * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1179  * the number of columns stored, since the number is opclass-defined.
1180  */
1181  opcinfo = (BrinOpcInfo **) palloc(sizeof(BrinOpcInfo *) * tupdesc->natts);
1182  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1183  {
1184  FmgrInfo *opcInfoFn;
1185  Form_pg_attribute attr = TupleDescAttr(tupdesc, keyno);
1186 
1187  opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1188 
1189  opcinfo[keyno] = (BrinOpcInfo *)
1190  DatumGetPointer(FunctionCall1(opcInfoFn, attr->atttypid));
1191  totalstored += opcinfo[keyno]->oi_nstored;
1192  }
1193 
1194  /* Allocate our result struct and fill it in */
1195  totalsize = offsetof(BrinDesc, bd_info) +
1196  sizeof(BrinOpcInfo *) * tupdesc->natts;
1197 
1198  bdesc = palloc(totalsize);
1199  bdesc->bd_context = cxt;
1200  bdesc->bd_index = rel;
1201  bdesc->bd_tupdesc = tupdesc;
1202  bdesc->bd_disktdesc = NULL; /* generated lazily */
1203  bdesc->bd_totalstored = totalstored;
1204 
1205  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1206  bdesc->bd_info[keyno] = opcinfo[keyno];
1207  pfree(opcinfo);
1208 
1209  MemoryContextSwitchTo(oldcxt);
1210 
1211  return bdesc;
1212 }
Definition: fmgr.h:56
#define AllocSetContextCreate
Definition: memutils.h:173
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
#define RelationGetDescr(relation)
Definition: rel.h:495
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:70
void pfree(void *pointer)
Definition: mcxt.c:1169
Relation bd_index
Definition: brin_internal.h:50
uint16 oi_nstored
Definition: brin_internal.h:28
int bd_totalstored
Definition: brin_internal.h:59
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
#define DatumGetPointer(X)
Definition: postgres.h:593
MemoryContext bd_context
Definition: brin_internal.h:47
void * palloc(Size size)
Definition: mcxt.c:1062
TupleDesc bd_disktdesc
Definition: brin_internal.h:56
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:644
#define offsetof(type, field)
Definition: c.h:727

◆ brin_free_desc()

void brin_free_desc ( BrinDesc bdesc)

Definition at line 1215 of file brin.c.

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

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

1216 {
1217  /* make sure the tupdesc is still valid */
1218  Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1219  /* no need for retail pfree */
1221 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
#define Assert(condition)
Definition: c.h:804
MemoryContext bd_context
Definition: brin_internal.h:47
int tdrefcount
Definition: tupdesc.h:84

◆ brinbeginscan()

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

Definition at line 325 of file brin.c.

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

Referenced by brinhandler().

326 {
327  IndexScanDesc scan;
328  BrinOpaque *opaque;
329 
330  scan = RelationGetIndexScan(r, nkeys, norderbys);
331 
332  opaque = (BrinOpaque *) palloc(sizeof(BrinOpaque));
333  opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
334  scan->xs_snapshot);
335  opaque->bo_bdesc = brin_build_desc(r);
336  scan->opaque = opaque;
337 
338  return scan;
339 }
struct SnapshotData * xs_snapshot
Definition: relscan.h:119
BlockNumber bo_pagesPerRange
Definition: brin.c:65
BrinRevmap * bo_rmAccess
Definition: brin.c:66
BrinDesc * bo_bdesc
Definition: brin.c:67
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1160
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
void * palloc(Size size)
Definition: mcxt.c:1062
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81

◆ brinbuild()

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

Definition at line 810 of file brin.c.

References Assert, BRIN_CURRENT_VERSION, BRIN_METAPAGE_BLKNO, brin_metapage_init(), brinbuildCallback(), BrinGetPagesPerRange, brinRevmapInitialize(), brinRevmapTerminate(), BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), BufferGetPage, elog, ERROR, form_and_insert_tuple(), IndexBuildResult::heap_tuples, IndexBuildResult::index_tuples, initialize_brin_buildstate(), LockBuffer(), MarkBufferDirty(), P_NEW, PageSetLSN, xl_brin_createidx::pagesPerRange, palloc(), ReadBuffer(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, SizeOfBrinCreateIdx, table_index_build_scan(), terminate_brin_buildstate(), UnlockReleaseBuffer(), xl_brin_createidx::version, XLOG_BRIN_CREATE_INDEX, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by brinhandler().

811 {
812  IndexBuildResult *result;
813  double reltuples;
814  double idxtuples;
815  BrinRevmap *revmap;
817  Buffer meta;
818  BlockNumber pagesPerRange;
819 
820  /*
821  * We expect to be called exactly once for any index relation.
822  */
823  if (RelationGetNumberOfBlocks(index) != 0)
824  elog(ERROR, "index \"%s\" already contains data",
825  RelationGetRelationName(index));
826 
827  /*
828  * Critical section not required, because on error the creation of the
829  * whole relation will be rolled back.
830  */
831 
832  meta = ReadBuffer(index, P_NEW);
835 
838  MarkBufferDirty(meta);
839 
840  if (RelationNeedsWAL(index))
841  {
842  xl_brin_createidx xlrec;
843  XLogRecPtr recptr;
844  Page page;
845 
847  xlrec.pagesPerRange = BrinGetPagesPerRange(index);
848 
849  XLogBeginInsert();
850  XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
852 
853  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
854 
855  page = BufferGetPage(meta);
856  PageSetLSN(page, recptr);
857  }
858 
859  UnlockReleaseBuffer(meta);
860 
861  /*
862  * Initialize our state, including the deformed tuple state.
863  */
864  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
865  state = initialize_brin_buildstate(index, revmap, pagesPerRange);
866 
867  /*
868  * Now scan the relation. No syncscan allowed here because we want the
869  * heap blocks in physical order.
870  */
871  reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
872  brinbuildCallback, (void *) state, NULL);
873 
874  /* process the final batch */
875  form_and_insert_tuple(state);
876 
877  /* release resources */
878  idxtuples = state->bs_numtuples;
881 
882  /*
883  * Return statistics
884  */
885  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
886 
887  result->heap_tuples = reltuples;
888  result->index_tuples = idxtuples;
889 
890  return result;
891 }
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
#define SizeOfBrinCreateIdx
Definition: brin_xlog.h:55
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1562
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:220
#define XLOG_BRIN_CREATE_INDEX
Definition: brin_xlog.h:31
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1542
#define P_NEW
Definition: bufmgr.h:91
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define BrinGetPagesPerRange(relation)
Definition: brin.h:39
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:487
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3807
#define ERROR
Definition: elog.h:46
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1745
#define REGBUF_STANDARD
Definition: xloginsert.h:35
BrinRevmap * bs_rmAccess
Definition: brin.c:55
#define RelationGetRelationName(relation)
Definition: rel.h:503
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:765
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4023
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:213
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:697
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define RelationNeedsWAL(relation)
Definition: rel.h:582
int bs_numtuples
Definition: brin.c:51
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2758
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1248
void XLogBeginInsert(void)
Definition: xloginsert.c:123
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1273

◆ brinbuildempty()

void brinbuildempty ( Relation  index)

Definition at line 894 of file brin.c.

References BRIN_CURRENT_VERSION, brin_metapage_init(), BrinGetPagesPerRange, BUFFER_LOCK_EXCLUSIVE, BufferGetPage, END_CRIT_SECTION, INIT_FORKNUM, LockBuffer(), log_newpage_buffer(), MarkBufferDirty(), P_NEW, RBM_NORMAL, ReadBufferExtended(), START_CRIT_SECTION, and UnlockReleaseBuffer().

Referenced by brinhandler().

895 {
896  Buffer metabuf;
897 
898  /* An empty BRIN index has a metapage only. */
899  metabuf =
902 
903  /* Initialize and xlog metabuffer. */
907  MarkBufferDirty(metabuf);
908  log_newpage_buffer(metabuf, true);
910 
911  UnlockReleaseBuffer(metabuf);
912 }
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1090
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1562
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:744
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
#define P_NEW
Definition: bufmgr.h:91
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define BrinGetPagesPerRange(relation)
Definition: brin.h:39
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:487
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3807
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4023
int Buffer
Definition: buf.h:23

◆ brinbulkdelete()

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

Definition at line 924 of file brin.c.

References palloc0().

Referenced by brinhandler().

926 {
927  /* allocate stats if first time through, else re-use existing struct */
928  if (stats == NULL)
930 
931  return stats;
932 }
void * palloc0(Size size)
Definition: mcxt.c:1093

◆ brinendscan()

void brinendscan ( IndexScanDesc  scan)

Definition at line 748 of file brin.c.

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

Referenced by brinhandler().

749 {
750  BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
751 
753  brin_free_desc(opaque->bo_bdesc);
754  pfree(opaque);
755 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1215
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
void pfree(void *pointer)
Definition: mcxt.c:1169
BrinRevmap * bo_rmAccess
Definition: brin.c:66
BrinDesc * bo_bdesc
Definition: brin.c:67

◆ bringetbitmap()

int64 bringetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 354 of file brin.c.

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_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, Int32GetDatum, InvalidBuffer, InvalidOid, sort-test::key, IndexScanDescData::keyData, LockBuffer(), MAXALIGN, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), Min, TupleDescData::natts, IndexScanDescData::numberOfKeys, BrinOpcInfo::oi_regular_nulls, IndexScanDescData::opaque, palloc(), palloc0(), PG_USED_FOR_ASSERTS_ONLY, pgstat_count_index_scan, PointerGetDatum, RelationGetNumberOfBlocks, RelationGetRelid, ReleaseBuffer(), ScanKeyData::sk_attno, ScanKeyData::sk_collation, ScanKeyData::sk_flags, SK_ISNULL, table_close(), table_open(), tbm_add_page(), TupleDescAttr, and IndexScanDescData::xs_snapshot.

Referenced by brinhandler().

355 {
356  Relation idxRel = scan->indexRelation;
358  BrinDesc *bdesc;
359  Oid heapOid;
360  Relation heapRel;
361  BrinOpaque *opaque;
362  BlockNumber nblocks;
363  BlockNumber heapBlk;
364  int totalpages = 0;
365  FmgrInfo *consistentFn;
366  MemoryContext oldcxt;
367  MemoryContext perRangeCxt;
368  BrinMemTuple *dtup;
369  BrinTuple *btup = NULL;
370  Size btupsz = 0;
371  ScanKey **keys,
372  **nullkeys;
373  int *nkeys,
374  *nnullkeys;
375  int keyno;
376  char *ptr;
377  Size len;
378  char *tmp PG_USED_FOR_ASSERTS_ONLY;
379 
380  opaque = (BrinOpaque *) scan->opaque;
381  bdesc = opaque->bo_bdesc;
382  pgstat_count_index_scan(idxRel);
383 
384  /*
385  * We need to know the size of the table so that we know how long to
386  * iterate on the revmap.
387  */
388  heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
389  heapRel = table_open(heapOid, AccessShareLock);
390  nblocks = RelationGetNumberOfBlocks(heapRel);
391  table_close(heapRel, AccessShareLock);
392 
393  /*
394  * Make room for the consistent support procedures of indexed columns. We
395  * don't look them up here; we do that lazily the first time we see a scan
396  * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
397  */
398  consistentFn = palloc0(sizeof(FmgrInfo) * bdesc->bd_tupdesc->natts);
399 
400  /*
401  * Make room for per-attribute lists of scan keys that we'll pass to the
402  * consistent support procedure. We don't know which attributes have scan
403  * keys, so we allocate space for all attributes. That may use more memory
404  * but it's probably cheaper than determining which attributes are used.
405  *
406  * We keep null and regular keys separate, so that we can pass just the
407  * regular keys to the consistent function easily.
408  *
409  * To reduce the allocation overhead, we allocate one big chunk and then
410  * carve it into smaller arrays ourselves. All the pieces have exactly the
411  * same lifetime, so that's OK.
412  *
413  * XXX The widest index can have 32 attributes, so the amount of wasted
414  * memory is negligible. We could invent a more compact approach (with
415  * just space for used attributes) but that would make the matching more
416  * complex so it's not a good trade-off.
417  */
418  len =
419  MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* regular keys */
420  MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
421  MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts) +
422  MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* NULL keys */
423  MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
424  MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
425 
426  ptr = palloc(len);
427  tmp = ptr;
428 
429  keys = (ScanKey **) ptr;
430  ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
431 
432  nullkeys = (ScanKey **) ptr;
433  ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
434 
435  nkeys = (int *) ptr;
436  ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
437 
438  nnullkeys = (int *) ptr;
439  ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
440 
441  for (int i = 0; i < bdesc->bd_tupdesc->natts; i++)
442  {
443  keys[i] = (ScanKey *) ptr;
444  ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
445 
446  nullkeys[i] = (ScanKey *) ptr;
447  ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
448  }
449 
450  Assert(tmp + len == ptr);
451 
452  /* zero the number of keys */
453  memset(nkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
454  memset(nnullkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
455 
456  /* Preprocess the scan keys - split them into per-attribute arrays. */
457  for (keyno = 0; keyno < scan->numberOfKeys; keyno++)
458  {
459  ScanKey key = &scan->keyData[keyno];
460  AttrNumber keyattno = key->sk_attno;
461 
462  /*
463  * The collation of the scan key must match the collation used in the
464  * index column (but only if the search is not IS NULL/ IS NOT NULL).
465  * Otherwise we shouldn't be using this index ...
466  */
467  Assert((key->sk_flags & SK_ISNULL) ||
468  (key->sk_collation ==
469  TupleDescAttr(bdesc->bd_tupdesc,
470  keyattno - 1)->attcollation));
471 
472  /*
473  * First time we see this index attribute, so init as needed.
474  *
475  * This is a bit of an overkill - we don't know how many scan keys are
476  * there for this attribute, so we simply allocate the largest number
477  * possible (as if all keys were for this attribute). This may waste a
478  * bit of memory, but we only expect small number of scan keys in
479  * general, so this should be negligible, and repeated repalloc calls
480  * are not free either.
481  */
482  if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
483  {
484  FmgrInfo *tmp;
485 
486  /* First time we see this attribute, so no key/null keys. */
487  Assert(nkeys[keyattno - 1] == 0);
488  Assert(nnullkeys[keyattno - 1] == 0);
489 
490  tmp = index_getprocinfo(idxRel, keyattno,
492  fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
494  }
495 
496  /* Add key to the proper per-attribute array. */
497  if (key->sk_flags & SK_ISNULL)
498  {
499  nullkeys[keyattno - 1][nnullkeys[keyattno - 1]] = key;
500  nnullkeys[keyattno - 1]++;
501  }
502  else
503  {
504  keys[keyattno - 1][nkeys[keyattno - 1]] = key;
505  nkeys[keyattno - 1]++;
506  }
507  }
508 
509  /* allocate an initial in-memory tuple, out of the per-range memcxt */
510  dtup = brin_new_memtuple(bdesc);
511 
512  /*
513  * Setup and use a per-range memory context, which is reset every time we
514  * loop below. This avoids having to free the tuples within the loop.
515  */
517  "bringetbitmap cxt",
519  oldcxt = MemoryContextSwitchTo(perRangeCxt);
520 
521  /*
522  * Now scan the revmap. We start by querying for heap page 0,
523  * incrementing by the number of pages per range; this gives us a full
524  * view of the table.
525  */
526  for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
527  {
528  bool addrange;
529  bool gottuple = false;
530  BrinTuple *tup;
531  OffsetNumber off;
532  Size size;
533 
535 
537 
538  tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
539  &off, &size, BUFFER_LOCK_SHARE,
540  scan->xs_snapshot);
541  if (tup)
542  {
543  gottuple = true;
544  btup = brin_copy_tuple(tup, size, btup, &btupsz);
546  }
547 
548  /*
549  * For page ranges with no indexed tuple, we must return the whole
550  * range; otherwise, compare it to the scan keys.
551  */
552  if (!gottuple)
553  {
554  addrange = true;
555  }
556  else
557  {
558  dtup = brin_deform_tuple(bdesc, btup, dtup);
559  if (dtup->bt_placeholder)
560  {
561  /*
562  * Placeholder tuples are always returned, regardless of the
563  * values stored in them.
564  */
565  addrange = true;
566  }
567  else
568  {
569  int attno;
570 
571  /*
572  * Compare scan keys with summary values stored for the range.
573  * If scan keys are matched, the page range must be added to
574  * the bitmap. We initially assume the range needs to be
575  * added; in particular this serves the case where there are
576  * no keys.
577  */
578  addrange = true;
579  for (attno = 1; attno <= bdesc->bd_tupdesc->natts; attno++)
580  {
581  BrinValues *bval;
582  Datum add;
583  Oid collation;
584 
585  /*
586  * skip attributes without any scan keys (both regular and
587  * IS [NOT] NULL)
588  */
589  if (nkeys[attno - 1] == 0 && nnullkeys[attno - 1] == 0)
590  continue;
591 
592  bval = &dtup->bt_columns[attno - 1];
593 
594  /*
595  * First check if there are any IS [NOT] NULL scan keys,
596  * and if we're violating them. In that case we can
597  * terminate early, without invoking the support function.
598  *
599  * As there may be more keys, we can only determine
600  * mismatch within this loop.
601  */
602  if (bdesc->bd_info[attno - 1]->oi_regular_nulls &&
603  !check_null_keys(bval, nullkeys[attno - 1],
604  nnullkeys[attno - 1]))
605  {
606  /*
607  * If any of the IS [NOT] NULL keys failed, the page
608  * range as a whole can't pass. So terminate the loop.
609  */
610  addrange = false;
611  break;
612  }
613 
614  /*
615  * So either there are no IS [NOT] NULL keys, or all
616  * passed. If there are no regular scan keys, we're done -
617  * the page range matches. If there are regular keys, but
618  * the page range is marked as 'all nulls' it can't
619  * possibly pass (we're assuming the operators are
620  * strict).
621  */
622 
623  /* No regular scan keys - page range as a whole passes. */
624  if (!nkeys[attno - 1])
625  continue;
626 
627  Assert((nkeys[attno - 1] > 0) &&
628  (nkeys[attno - 1] <= scan->numberOfKeys));
629 
630  /* If it is all nulls, it cannot possibly be consistent. */
631  if (bval->bv_allnulls)
632  {
633  addrange = false;
634  break;
635  }
636 
637  /*
638  * Collation from the first key (has to be the same for
639  * all keys for the same attribute).
640  */
641  collation = keys[attno - 1][0]->sk_collation;
642 
643  /*
644  * Check whether the scan key is consistent with the page
645  * range values; if so, have the pages in the range added
646  * to the output bitmap.
647  *
648  * The opclass may or may not support processing of
649  * multiple scan keys. We can determine that based on the
650  * number of arguments - functions with extra parameter
651  * (number of scan keys) do support this, otherwise we
652  * have to simply pass the scan keys one by one.
653  */
654  if (consistentFn[attno - 1].fn_nargs >= 4)
655  {
656  /* Check all keys at once */
657  add = FunctionCall4Coll(&consistentFn[attno - 1],
658  collation,
659  PointerGetDatum(bdesc),
660  PointerGetDatum(bval),
661  PointerGetDatum(keys[attno - 1]),
662  Int32GetDatum(nkeys[attno - 1]));
663  addrange = DatumGetBool(add);
664  }
665  else
666  {
667  /*
668  * Check keys one by one
669  *
670  * When there are multiple scan keys, failure to meet
671  * the criteria for a single one of them is enough to
672  * discard the range as a whole, so break out of the
673  * loop as soon as a false return value is obtained.
674  */
675  int keyno;
676 
677  for (keyno = 0; keyno < nkeys[attno - 1]; keyno++)
678  {
679  add = FunctionCall3Coll(&consistentFn[attno - 1],
680  keys[attno - 1][keyno]->sk_collation,
681  PointerGetDatum(bdesc),
682  PointerGetDatum(bval),
683  PointerGetDatum(keys[attno - 1][keyno]));
684  addrange = DatumGetBool(add);
685  if (!addrange)
686  break;
687  }
688  }
689  }
690  }
691  }
692 
693  /* add the pages in the range to the output bitmap, if needed */
694  if (addrange)
695  {
696  BlockNumber pageno;
697 
698  for (pageno = heapBlk;
699  pageno <= Min(nblocks, heapBlk + opaque->bo_pagesPerRange) - 1;
700  pageno++)
701  {
702  MemoryContextSwitchTo(oldcxt);
703  tbm_add_page(tbm, pageno);
704  totalpages++;
705  MemoryContextSwitchTo(perRangeCxt);
706  }
707  }
708  }
709 
710  MemoryContextSwitchTo(oldcxt);
711  MemoryContextDelete(perRangeCxt);
712 
713  if (buf != InvalidBuffer)
714  ReleaseBuffer(buf);
715 
716  /*
717  * XXX We have an approximation of the number of *pages* that our scan
718  * returns, but we don't have a precise idea of the number of heap tuples
719  * involved.
720  */
721  return totalpages * 10;
722 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
Definition: fmgr.h:56
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3474
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:444
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:197
#define PointerGetDatum(X)
Definition: postgres.h:600
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
#define Min(x, y)
Definition: c.h:986
bool bv_allnulls
Definition: brin_tuple.h:33
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
#define InvalidBuffer
Definition: buf.h:25
struct SnapshotData * xs_snapshot
Definition: relscan.h:119
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1195
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3784
unsigned int Oid
Definition: postgres_ext.h:31
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:480
BlockNumber bo_pagesPerRange
Definition: brin.c:65
Relation indexRelation
Definition: relscan.h:118
uint16 OffsetNumber
Definition: off.h:24
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:547
bool bt_placeholder
Definition: brin_tuple.h:46
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:608
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:54
static char * buf
Definition: pg_test_fsync.c:68
BrinRevmap * bo_rmAccess
Definition: brin.c:66
#define DatumGetBool(X)
Definition: postgres.h:437
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1040
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
bool oi_regular_nulls
Definition: brin_internal.h:31
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define SK_ISNULL
Definition: skey.h:115
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
BrinDesc * bo_bdesc
Definition: brin.c:67
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4023
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:213
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
#define InvalidOid
Definition: postgres_ext.h:36
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1170
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:804
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:72
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
struct ScanKeyData * keyData
Definition: relscan.h:122
#define Int32GetDatum(X)
Definition: postgres.h:523
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:442
void * palloc(Size size)
Definition: mcxt.c:1062
Oid sk_collation
Definition: skey.h:70
int i
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
int Buffer
Definition: buf.h:23
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:469
static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
Definition: brin.c:1722
AttrNumber sk_attno
Definition: skey.h:67
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ brininsert()

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

Definition at line 154 of file brin.c.

References add_values_to_range(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AutoVacuumRequestWork(), AVW_BRINSummarizeRange, brin_build_desc(), brin_can_do_samepage_update(), brin_copy_tuple(), brin_deform_tuple(), brin_doupdate(), brin_form_tuple(), BrinGetAutoSummarize, brinGetTupleForHeapBlock(), brinRevmapInitialize(), brinRevmapTerminate(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferIsValid, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, ereport, errcode(), errmsg(), FirstOffsetNumber, IndexInfo::ii_AmCache, IndexInfo::ii_Context, InvalidBuffer, ItemIdGetLength, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockBuffer(), LOG, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), PageGetItemId, RelationGetRelationName, RelationGetRelid, and ReleaseBuffer().

Referenced by brinhandler().

159 {
160  BlockNumber pagesPerRange;
161  BlockNumber origHeapBlk;
162  BlockNumber heapBlk;
163  BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache;
164  BrinRevmap *revmap;
166  MemoryContext tupcxt = NULL;
168  bool autosummarize = BrinGetAutoSummarize(idxRel);
169 
170  revmap = brinRevmapInitialize(idxRel, &pagesPerRange, NULL);
171 
172  /*
173  * origHeapBlk is the block number where the insertion occurred. heapBlk
174  * is the first block in the corresponding page range.
175  */
176  origHeapBlk = ItemPointerGetBlockNumber(heaptid);
177  heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
178 
179  for (;;)
180  {
181  bool need_insert = false;
182  OffsetNumber off;
183  BrinTuple *brtup;
184  BrinMemTuple *dtup;
185 
187 
188  /*
189  * If auto-summarization is enabled and we just inserted the first
190  * tuple into the first block of a new non-first page range, request a
191  * summarization run of the previous range.
192  */
193  if (autosummarize &&
194  heapBlk > 0 &&
195  heapBlk == origHeapBlk &&
197  {
198  BlockNumber lastPageRange = heapBlk - 1;
199  BrinTuple *lastPageTuple;
200 
201  lastPageTuple =
202  brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
203  NULL, BUFFER_LOCK_SHARE, NULL);
204  if (!lastPageTuple)
205  {
206  bool recorded;
207 
209  RelationGetRelid(idxRel),
210  lastPageRange);
211  if (!recorded)
212  ereport(LOG,
213  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
214  errmsg("request for BRIN range summarization for index \"%s\" page %u was not recorded",
215  RelationGetRelationName(idxRel),
216  lastPageRange)));
217  }
218  else
220  }
221 
222  brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
223  NULL, BUFFER_LOCK_SHARE, NULL);
224 
225  /* if range is unsummarized, there's nothing to do */
226  if (!brtup)
227  break;
228 
229  /* First time through in this statement? */
230  if (bdesc == NULL)
231  {
232  MemoryContextSwitchTo(indexInfo->ii_Context);
233  bdesc = brin_build_desc(idxRel);
234  indexInfo->ii_AmCache = (void *) bdesc;
235  MemoryContextSwitchTo(oldcxt);
236  }
237  /* First time through in this brininsert call? */
238  if (tupcxt == NULL)
239  {
241  "brininsert cxt",
243  MemoryContextSwitchTo(tupcxt);
244  }
245 
246  dtup = brin_deform_tuple(bdesc, brtup, NULL);
247 
248  need_insert = add_values_to_range(idxRel, bdesc, dtup, values, nulls);
249 
250  if (!need_insert)
251  {
252  /*
253  * The tuple is consistent with the new values, so there's nothing
254  * to do.
255  */
257  }
258  else
259  {
260  Page page = BufferGetPage(buf);
261  ItemId lp = PageGetItemId(page, off);
262  Size origsz;
263  BrinTuple *origtup;
264  Size newsz;
265  BrinTuple *newtup;
266  bool samepage;
267 
268  /*
269  * Make a copy of the old tuple, so that we can compare it after
270  * re-acquiring the lock.
271  */
272  origsz = ItemIdGetLength(lp);
273  origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
274 
275  /*
276  * Before releasing the lock, check if we can attempt a same-page
277  * update. Another process could insert a tuple concurrently in
278  * the same page though, so downstream we must be prepared to cope
279  * if this turns out to not be possible after all.
280  */
281  newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
282  samepage = brin_can_do_samepage_update(buf, origsz, newsz);
284 
285  /*
286  * Try to update the tuple. If this doesn't work for whatever
287  * reason, we need to restart from the top; the revmap might be
288  * pointing at a different tuple for this block now, so we need to
289  * recompute to ensure both our new heap tuple and the other
290  * inserter's are covered by the combined tuple. It might be that
291  * we don't need to update at all.
292  */
293  if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
294  buf, off, origtup, origsz, newtup, newsz,
295  samepage))
296  {
297  /* no luck; start over */
299  continue;
300  }
301  }
302 
303  /* success! */
304  break;
305  }
306 
307  brinRevmapTerminate(revmap);
308  if (BufferIsValid(buf))
310  MemoryContextSwitchTo(oldcxt);
311  if (tupcxt != NULL)
312  MemoryContextDelete(tupcxt);
313 
314  return false;
315 }
bool AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3386
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
MemoryContext ii_Context
Definition: execnodes.h:179
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:444
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:197
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, Datum *values, bool *nulls)
Definition: brin.c:1670
int errcode(int sqlerrcode)
Definition: elog.c:698
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3784
#define LOG
Definition: elog.h:26
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:54
uint16 OffsetNumber
Definition: off.h:24
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:547
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static char * buf
Definition: pg_test_fsync.c:68
#define FirstOffsetNumber
Definition: off.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:503
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:324
void * ii_AmCache
Definition: execnodes.h:178
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4023
#define ereport(elevel,...)
Definition: elog.h:157
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1160
size_t Size
Definition: c.h:540
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define BrinGetAutoSummarize(relation)
Definition: brin.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:99
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:469
Pointer Page
Definition: bufpage.h:78

◆ brinoptions()

bytea* brinoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 969 of file brin.c.

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

Referenced by brinhandler().

970 {
971  static const relopt_parse_elt tab[] = {
972  {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
973  {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
974  };
975 
976  return (bytea *) build_reloptions(reloptions, validate,
978  sizeof(BrinOptions),
979  tab, lengthof(tab));
980 }
#define lengthof(array)
Definition: c.h:734
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:1887
Definition: c.h:621
#define offsetof(type, field)
Definition: c.h:727

◆ brinrescan()

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

Definition at line 728 of file brin.c.

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

Referenced by brinhandler().

730 {
731  /*
732  * Other index AMs preprocess the scan keys at this point, or sometime
733  * early during the scan; this lets them optimize by removing redundant
734  * keys, or doing early returns when they are impossible to satisfy; see
735  * _bt_preprocess_keys for an example. Something like that could be added
736  * here someday, too.
737  */
738 
739  if (scankey && scan->numberOfKeys > 0)
740  memmove(scan->keyData, scankey,
741  scan->numberOfKeys * sizeof(ScanKeyData));
742 }
struct ScanKeyData * keyData
Definition: relscan.h:122

◆ brinvacuumcleanup()

IndexBulkDeleteResult* brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 939 of file brin.c.

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

Referenced by brinhandler().

940 {
941  Relation heapRel;
942 
943  /* No-op in ANALYZE ONLY mode */
944  if (info->analyze_only)
945  return stats;
946 
947  if (!stats)
949  stats->num_pages = RelationGetNumberOfBlocks(info->index);
950  /* rest of stats is initialized by zeroing */
951 
952  heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
954 
955  brin_vacuum_scan(info->index, info->strategy);
956 
957  brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
958  &stats->num_index_tuples, &stats->num_index_tuples);
959 
960  table_close(heapRel, AccessShareLock);
961 
962  return stats;
963 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3474
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
bool analyze_only
Definition: genam.h:47
BufferAccessStrategy strategy
Definition: genam.h:52
#define AccessShareLock
Definition: lockdefs.h:36
Relation index
Definition: genam.h:46
BlockNumber num_pages
Definition: genam.h:76
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:1637
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1445
void * palloc0(Size size)
Definition: mcxt.c:1093
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:213
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:70
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
double num_index_tuples
Definition: genam.h:78
#define RelationGetRelid(relation)
Definition: rel.h:469

◆ brinvalidate()

bool brinvalidate ( Oid  opclassoid)

Definition at line 37 of file brin_validate.c.

References AMOPSTRATEGY, AMPROCNUM, 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(), CLAOID, elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), format_type_be(), OpFamilyOpFuncGroup::functionset, GETSTRUCT, HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, catclist::members, catclist::n_members, NameStr, ObjectIdGetDatum, OidIsValid, OpFamilyOpFuncGroup::operatorset, OPFAMILYOID, ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by brinhandler().

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