PostgreSQL Source Code  git master
brin.c File Reference
#include "postgres.h"
#include "access/brin.h"
#include "access/brin_page.h"
#include "access/brin_pageops.h"
#include "access/brin_xlog.h"
#include "access/relation.h"
#include "access/reloptions.h"
#include "access/relscan.h"
#include "access/table.h"
#include "access/tableam.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for brin.c:

Go to the source code of this file.

Data Structures

struct  BrinBuildState
 
struct  BrinOpaque
 

Macros

#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber
 

Typedefs

typedef struct BrinBuildState BrinBuildState
 
typedef struct BrinOpaque BrinOpaque
 

Functions

static BrinBuildStateinitialize_brin_buildstate (Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
 
static void terminate_brin_buildstate (BrinBuildState *state)
 
static void brinsummarize (Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
 
static void form_and_insert_tuple (BrinBuildState *state)
 
static void union_tuples (BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
 
static void brin_vacuum_scan (Relation idxrel, BufferAccessStrategy strategy)
 
Datum brinhandler (PG_FUNCTION_ARGS)
 
bool brininsert (Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
 
IndexScanDesc brinbeginscan (Relation r, int nkeys, int norderbys)
 
int64 bringetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
void brinrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void brinendscan (IndexScanDesc scan)
 
static void brinbuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
 
IndexBuildResultbrinbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void brinbuildempty (Relation index)
 
IndexBulkDeleteResultbrinbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultbrinvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
byteabrinoptions (Datum reloptions, bool validate)
 
Datum brin_summarize_new_values (PG_FUNCTION_ARGS)
 
Datum brin_summarize_range (PG_FUNCTION_ARGS)
 
Datum brin_desummarize_range (PG_FUNCTION_ARGS)
 
BrinDescbrin_build_desc (Relation rel)
 
void brin_free_desc (BrinDesc *bdesc)
 
void brinGetStats (Relation index, BrinStatsData *stats)
 
static void summarize_range (IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
 

Macro Definition Documentation

◆ BRIN_ALL_BLOCKRANGES

#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber

Typedef Documentation

◆ BrinBuildState

◆ BrinOpaque

typedef struct BrinOpaque BrinOpaque

Function Documentation

◆ brin_build_desc()

BrinDesc* brin_build_desc ( Relation  rel)

Definition at line 1018 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().

1019 {
1020  BrinOpcInfo **opcinfo;
1021  BrinDesc *bdesc;
1022  TupleDesc tupdesc;
1023  int totalstored = 0;
1024  int keyno;
1025  long totalsize;
1026  MemoryContext cxt;
1027  MemoryContext oldcxt;
1028 
1030  "brin desc cxt",
1032  oldcxt = MemoryContextSwitchTo(cxt);
1033  tupdesc = RelationGetDescr(rel);
1034 
1035  /*
1036  * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1037  * the number of columns stored, since the number is opclass-defined.
1038  */
1039  opcinfo = (BrinOpcInfo **) palloc(sizeof(BrinOpcInfo *) * tupdesc->natts);
1040  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1041  {
1042  FmgrInfo *opcInfoFn;
1043  Form_pg_attribute attr = TupleDescAttr(tupdesc, keyno);
1044 
1045  opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1046 
1047  opcinfo[keyno] = (BrinOpcInfo *)
1048  DatumGetPointer(FunctionCall1(opcInfoFn, attr->atttypid));
1049  totalstored += opcinfo[keyno]->oi_nstored;
1050  }
1051 
1052  /* Allocate our result struct and fill it in */
1053  totalsize = offsetof(BrinDesc, bd_info) +
1054  sizeof(BrinOpcInfo *) * tupdesc->natts;
1055 
1056  bdesc = palloc(totalsize);
1057  bdesc->bd_context = cxt;
1058  bdesc->bd_index = rel;
1059  bdesc->bd_tupdesc = tupdesc;
1060  bdesc->bd_disktdesc = NULL; /* generated lazily */
1061  bdesc->bd_totalstored = totalstored;
1062 
1063  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1064  bdesc->bd_info[keyno] = opcinfo[keyno];
1065  pfree(opcinfo);
1066 
1067  MemoryContextSwitchTo(oldcxt);
1068 
1069  return bdesc;
1070 }
Definition: fmgr.h:56
#define AllocSetContextCreate
Definition: memutils.h:170
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
#define RelationGetDescr(relation)
Definition: rel.h:482
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:67
void pfree(void *pointer)
Definition: mcxt.c:1056
Relation bd_index
Definition: brin_internal.h:47
uint16 oi_nstored
Definition: brin_internal.h:28
int bd_totalstored
Definition: brin_internal.h:56
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:59
#define DatumGetPointer(X)
Definition: postgres.h:549
MemoryContext bd_context
Definition: brin_internal.h:44
void * palloc(Size size)
Definition: mcxt.c:949
TupleDesc bd_disktdesc
Definition: brin_internal.h:53
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:642
#define offsetof(type, field)
Definition: c.h:661

◆ brin_desummarize_range()

Datum brin_desummarize_range ( PG_FUNCTION_ARGS  )

Definition at line 937 of file brin.c.

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

938 {
939  Oid indexoid = PG_GETARG_OID(0);
940  int64 heapBlk64 = PG_GETARG_INT64(1);
941  BlockNumber heapBlk;
942  Oid heapoid;
943  Relation heapRel;
944  Relation indexRel;
945  bool done;
946 
947  if (RecoveryInProgress())
948  ereport(ERROR,
949  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
950  errmsg("recovery is in progress"),
951  errhint("BRIN control functions cannot be executed during recovery.")));
952 
953  if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
954  {
955  char *blk = psprintf(INT64_FORMAT, heapBlk64);
956 
957  ereport(ERROR,
958  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
959  errmsg("block number out of range: %s", blk)));
960  }
961  heapBlk = (BlockNumber) heapBlk64;
962 
963  /*
964  * We must lock table before index to avoid deadlocks. However, if the
965  * passed indexoid isn't an index then IndexGetRelation() will fail.
966  * Rather than emitting a not-very-helpful error message, postpone
967  * complaining, expecting that the is-it-an-index test below will fail.
968  */
969  heapoid = IndexGetRelation(indexoid, true);
970  if (OidIsValid(heapoid))
971  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
972  else
973  heapRel = NULL;
974 
975  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
976 
977  /* Must be a BRIN index */
978  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
979  indexRel->rd_rel->relam != BRIN_AM_OID)
980  ereport(ERROR,
981  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
982  errmsg("\"%s\" is not a BRIN index",
983  RelationGetRelationName(indexRel))));
984 
985  /* User must own the index (comparable to privileges needed for VACUUM) */
986  if (!pg_class_ownercheck(indexoid, GetUserId()))
988  RelationGetRelationName(indexRel));
989 
990  /*
991  * Since we did the IndexGetRelation call above without any lock, it's
992  * barely possible that a race against an index drop/recreation could have
993  * netted us the wrong table. Recheck.
994  */
995  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
996  ereport(ERROR,
998  errmsg("could not open parent table of index %s",
999  RelationGetRelationName(indexRel))));
1000 
1001  /* the revmap does the hard work */
1002  do
1003  {
1004  done = brinRevmapDesummarizeRange(indexRel, heapBlk);
1005  }
1006  while (!done);
1007 
1010 
1011  PG_RETURN_VOID();
1012 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3408
int errhint(const char *fmt,...)
Definition: elog.c:1071
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:73
Oid GetUserId(void)
Definition: miscinit.c:448
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:610
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:8069
#define OidIsValid(objectId)
Definition: c.h:644
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3327
#define ERROR
Definition: elog.h:43
#define MaxBlockNumber
Definition: block.h:35
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_RETURN_VOID()
Definition: fmgr.h:348
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4720
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:321
#define INT64_FORMAT
Definition: c.h:409
int errmsg(const char *fmt,...)
Definition: elog.c:824
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ brin_free_desc()

void brin_free_desc ( BrinDesc bdesc)

Definition at line 1073 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().

1074 {
1075  /* make sure the tupdesc is still valid */
1076  Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1077  /* no need for retail pfree */
1079 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
#define Assert(condition)
Definition: c.h:738
MemoryContext bd_context
Definition: brin_internal.h:44
int tdrefcount
Definition: tupdesc.h:84

◆ brin_summarize_new_values()

Datum brin_summarize_new_values ( PG_FUNCTION_ARGS  )

Definition at line 845 of file brin.c.

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

846 {
847  Datum relation = PG_GETARG_DATUM(0);
848 
850  relation,
852 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:860
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1701
uintptr_t Datum
Definition: postgres.h:367
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:69
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ brin_summarize_range()

Datum brin_summarize_range ( PG_FUNCTION_ARGS  )

Definition at line 860 of file brin.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, BRIN_ALL_BLOCKRANGES, brinsummarize(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errhint(), errmsg(), ERROR, GetUserId(), index_open(), IndexGetRelation(), INT64_FORMAT, OBJECT_INDEX, OidIsValid, pg_class_ownercheck(), PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_INT32, psprintf(), RelationData::rd_rel, RecoveryInProgress(), relation_close(), RelationGetRelationName, ShareUpdateExclusiveLock, and table_open().

Referenced by brin_summarize_new_values(), and perform_work_item().

861 {
862  Oid indexoid = PG_GETARG_OID(0);
863  int64 heapBlk64 = PG_GETARG_INT64(1);
864  BlockNumber heapBlk;
865  Oid heapoid;
866  Relation indexRel;
867  Relation heapRel;
868  double numSummarized = 0;
869 
870  if (RecoveryInProgress())
871  ereport(ERROR,
872  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
873  errmsg("recovery is in progress"),
874  errhint("BRIN control functions cannot be executed during recovery.")));
875 
876  if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
877  {
878  char *blk = psprintf(INT64_FORMAT, heapBlk64);
879 
880  ereport(ERROR,
881  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
882  errmsg("block number out of range: %s", blk)));
883  }
884  heapBlk = (BlockNumber) heapBlk64;
885 
886  /*
887  * We must lock table before index to avoid deadlocks. However, if the
888  * passed indexoid isn't an index then IndexGetRelation() will fail.
889  * Rather than emitting a not-very-helpful error message, postpone
890  * complaining, expecting that the is-it-an-index test below will fail.
891  */
892  heapoid = IndexGetRelation(indexoid, true);
893  if (OidIsValid(heapoid))
894  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
895  else
896  heapRel = NULL;
897 
898  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
899 
900  /* Must be a BRIN index */
901  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
902  indexRel->rd_rel->relam != BRIN_AM_OID)
903  ereport(ERROR,
904  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
905  errmsg("\"%s\" is not a BRIN index",
906  RelationGetRelationName(indexRel))));
907 
908  /* User must own the index (comparable to privileges needed for VACUUM) */
909  if (!pg_class_ownercheck(indexoid, GetUserId()))
911  RelationGetRelationName(indexRel));
912 
913  /*
914  * Since we did the IndexGetRelation call above without any lock, it's
915  * barely possible that a race against an index drop/recreation could have
916  * netted us the wrong table. Recheck.
917  */
918  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
919  ereport(ERROR,
921  errmsg("could not open parent table of index %s",
922  RelationGetRelationName(indexRel))));
923 
924  /* OK, do it */
925  brinsummarize(indexRel, heapRel, heapBlk, true, &numSummarized, NULL);
926 
929 
930  PG_RETURN_INT32((int32) numSummarized);
931 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3408
int errhint(const char *fmt,...)
Definition: elog.c:1071
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:73
Oid GetUserId(void)
Definition: miscinit.c:448
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:8069
#define OidIsValid(objectId)
Definition: c.h:644
signed int int32
Definition: c.h:355
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3327
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define RelationGetRelationName(relation)
Definition: rel.h:490
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1303
#define ereport(elevel,...)
Definition: elog.h:144
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4720
#define INT64_FORMAT
Definition: c.h:409
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:69
int errmsg(const char *fmt,...)
Definition: elog.c:824
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ brin_vacuum_scan()

static void brin_vacuum_scan ( Relation  idxrel,
BufferAccessStrategy  strategy 
)
static

Definition at line 1462 of file brin.c.

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

Referenced by brinvacuumcleanup().

1463 {
1464  BlockNumber nblocks;
1465  BlockNumber blkno;
1466 
1467  /*
1468  * Scan the index in physical order, and clean up any possible mess in
1469  * each page.
1470  */
1471  nblocks = RelationGetNumberOfBlocks(idxrel);
1472  for (blkno = 0; blkno < nblocks; blkno++)
1473  {
1474  Buffer buf;
1475 
1477 
1478  buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
1479  RBM_NORMAL, strategy);
1480 
1481  brin_page_cleanup(idxrel, buf);
1482 
1483  ReleaseBuffer(buf);
1484  }
1485 
1486  /*
1487  * Update all upper pages in the index's FSM, as well. This ensures not
1488  * only that we propagate leaf-page FSM updates made by brin_page_cleanup,
1489  * but also that any pre-existing damage or out-of-dateness is repaired.
1490  */
1491  FreeSpaceMapVacuum(idxrel);
1492 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:652
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
void brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:620
static char * buf
Definition: pg_test_fsync.c:67
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:335
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int Buffer
Definition: buf.h:23

◆ brinbeginscan()

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

Definition at line 345 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().

346 {
347  IndexScanDesc scan;
348  BrinOpaque *opaque;
349 
350  scan = RelationGetIndexScan(r, nkeys, norderbys);
351 
352  opaque = (BrinOpaque *) palloc(sizeof(BrinOpaque));
353  opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
354  scan->xs_snapshot);
355  opaque->bo_bdesc = brin_build_desc(r);
356  scan->opaque = opaque;
357 
358  return scan;
359 }
struct SnapshotData * xs_snapshot
Definition: relscan.h:104
BlockNumber bo_pagesPerRange
Definition: brin.c:64
BrinRevmap * bo_rmAccess
Definition: brin.c:65
BrinDesc * bo_bdesc
Definition: brin.c:66
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1018
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
void * palloc(Size size)
Definition: mcxt.c:949
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:80

◆ brinbuild()

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

Definition at line 668 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().

669 {
670  IndexBuildResult *result;
671  double reltuples;
672  double idxtuples;
673  BrinRevmap *revmap;
675  Buffer meta;
676  BlockNumber pagesPerRange;
677 
678  /*
679  * We expect to be called exactly once for any index relation.
680  */
681  if (RelationGetNumberOfBlocks(index) != 0)
682  elog(ERROR, "index \"%s\" already contains data",
683  RelationGetRelationName(index));
684 
685  /*
686  * Critical section not required, because on error the creation of the
687  * whole relation will be rolled back.
688  */
689 
690  meta = ReadBuffer(index, P_NEW);
693 
696  MarkBufferDirty(meta);
697 
698  if (RelationNeedsWAL(index))
699  {
700  xl_brin_createidx xlrec;
701  XLogRecPtr recptr;
702  Page page;
703 
705  xlrec.pagesPerRange = BrinGetPagesPerRange(index);
706 
707  XLogBeginInsert();
708  XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
710 
711  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
712 
713  page = BufferGetPage(meta);
714  PageSetLSN(page, recptr);
715  }
716 
717  UnlockReleaseBuffer(meta);
718 
719  /*
720  * Initialize our state, including the deformed tuple state.
721  */
722  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
723  state = initialize_brin_buildstate(index, revmap, pagesPerRange);
724 
725  /*
726  * Now scan the relation. No syncscan allowed here because we want the
727  * heap blocks in physical order.
728  */
729  reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
730  brinbuildCallback, (void *) state, NULL);
731 
732  /* process the final batch */
733  form_and_insert_tuple(state);
734 
735  /* release resources */
736  idxtuples = state->bs_numtuples;
739 
740  /*
741  * Return statistics
742  */
743  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
744 
745  result->heap_tuples = reltuples;
746  result->index_tuples = idxtuples;
747 
748  return result;
749 }
#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:1468
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:214
#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:1400
#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:3506
#define ERROR
Definition: elog.h:43
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:1524
#define REGBUF_STANDARD
Definition: xloginsert.h:35
BrinRevmap * bs_rmAccess
Definition: brin.c:54
#define RelationGetRelationName(relation)
Definition: rel.h:490
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:605
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:324
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
Definition: regguts.h:298
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:606
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define RelationNeedsWAL(relation)
Definition: rel.h:562
int bs_numtuples
Definition: brin.c:50
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2633
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1106
void XLogBeginInsert(void)
Definition: xloginsert.c:121
#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:1131

◆ brinbuildCallback()

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

Definition at line 605 of file brin.c.

References BrinDesc::bd_tupdesc, BRIN_elog, brin_memtuple_initialize(), BRIN_PROCNUM_ADDVALUE, BrinBuildState::bs_bdesc, BrinBuildState::bs_currRangeStart, BrinBuildState::bs_dtuple, BrinBuildState::bs_pagesPerRange, BrinMemTuple::bt_columns, DEBUG2, form_and_insert_tuple(), FunctionCall4Coll(), i, index_getprocinfo(), ItemPointerGetBlockNumber, TupleDescData::natts, PointerGetDatum, and TupleDescAttr.

Referenced by brinbuild(), and summarize_range().

611 {
612  BrinBuildState *state = (BrinBuildState *) brstate;
613  BlockNumber thisblock;
614  int i;
615 
616  thisblock = ItemPointerGetBlockNumber(tid);
617 
618  /*
619  * If we're in a block that belongs to a future range, summarize what
620  * we've got and start afresh. Note the scan might have skipped many
621  * pages, if they were devoid of live tuples; make sure to insert index
622  * tuples for those too.
623  */
624  while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
625  {
626 
627  BRIN_elog((DEBUG2,
628  "brinbuildCallback: completed a range: %u--%u",
629  state->bs_currRangeStart,
630  state->bs_currRangeStart + state->bs_pagesPerRange));
631 
632  /* create the index tuple and insert it */
633  form_and_insert_tuple(state);
634 
635  /* set state to correspond to the next range */
636  state->bs_currRangeStart += state->bs_pagesPerRange;
637 
638  /* re-initialize state for it */
640  }
641 
642  /* Accumulate the current tuple into the running state */
643  for (i = 0; i < state->bs_bdesc->bd_tupdesc->natts; i++)
644  {
645  FmgrInfo *addValue;
646  BrinValues *col;
648 
649  col = &state->bs_dtuple->bt_columns[i];
650  addValue = index_getprocinfo(index, i + 1,
652 
653  /*
654  * Update dtuple state, if and as necessary.
655  */
656  FunctionCall4Coll(addValue,
657  attr->attcollation,
658  PointerGetDatum(state->bs_bdesc),
659  PointerGetDatum(col),
660  values[i], isnull[i]);
661  }
662 }
Definition: fmgr.h:56
#define BRIN_elog(args)
Definition: brin_internal.h:82
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1199
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1400
BlockNumber bs_currRangeStart
Definition: brin.c:53
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:68
BlockNumber bs_pagesPerRange
Definition: brin.c:52
#define DEBUG2
Definition: elog.h:24
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
Definition: regguts.h:298
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int i
BrinMemTuple * bs_dtuple
Definition: brin.c:56
BrinDesc * bs_bdesc
Definition: brin.c:55
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:383

◆ brinbuildempty()

void brinbuildempty ( Relation  index)

Definition at line 752 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().

753 {
754  Buffer metabuf;
755 
756  /* An empty BRIN index has a metapage only. */
757  metabuf =
760 
761  /* Initialize and xlog metabuffer. */
765  MarkBufferDirty(metabuf);
766  log_newpage_buffer(metabuf, true);
768 
769  UnlockReleaseBuffer(metabuf);
770 }
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1014
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:652
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#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:3506
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
int Buffer
Definition: buf.h:23

◆ brinbulkdelete()

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

Definition at line 782 of file brin.c.

References palloc0().

Referenced by brinhandler().

784 {
785  /* allocate stats if first time through, else re-use existing struct */
786  if (stats == NULL)
788 
789  return stats;
790 }
void * palloc0(Size size)
Definition: mcxt.c:980

◆ brinendscan()

void brinendscan ( IndexScanDesc  scan)

Definition at line 588 of file brin.c.

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

Referenced by brinhandler().

589 {
590  BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
591 
593  brin_free_desc(opaque->bo_bdesc);
594  pfree(opaque);
595 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1073
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
void pfree(void *pointer)
Definition: mcxt.c:1056
BrinRevmap * bo_rmAccess
Definition: brin.c:65
BrinDesc * bo_bdesc
Definition: brin.c:66

◆ bringetbitmap()

int64 bringetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 374 of file brin.c.

References AccessShareLock, addrange(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, 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, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, DatumGetBool, fmgr_info_copy(), FunctionCall3Coll(), index_getprocinfo(), IndexGetRelation(), IndexScanDescData::indexRelation, InvalidBuffer, InvalidOid, sort-test::key, IndexScanDescData::keyData, LockBuffer(), MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), TupleDescData::natts, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, palloc0(), 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().

375 {
376  Relation idxRel = scan->indexRelation;
378  BrinDesc *bdesc;
379  Oid heapOid;
380  Relation heapRel;
381  BrinOpaque *opaque;
382  BlockNumber nblocks;
383  BlockNumber heapBlk;
384  int totalpages = 0;
385  FmgrInfo *consistentFn;
386  MemoryContext oldcxt;
387  MemoryContext perRangeCxt;
388  BrinMemTuple *dtup;
389  BrinTuple *btup = NULL;
390  Size btupsz = 0;
391 
392  opaque = (BrinOpaque *) scan->opaque;
393  bdesc = opaque->bo_bdesc;
394  pgstat_count_index_scan(idxRel);
395 
396  /*
397  * We need to know the size of the table so that we know how long to
398  * iterate on the revmap.
399  */
400  heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
401  heapRel = table_open(heapOid, AccessShareLock);
402  nblocks = RelationGetNumberOfBlocks(heapRel);
403  table_close(heapRel, AccessShareLock);
404 
405  /*
406  * Make room for the consistent support procedures of indexed columns. We
407  * don't look them up here; we do that lazily the first time we see a scan
408  * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
409  */
410  consistentFn = palloc0(sizeof(FmgrInfo) * bdesc->bd_tupdesc->natts);
411 
412  /* allocate an initial in-memory tuple, out of the per-range memcxt */
413  dtup = brin_new_memtuple(bdesc);
414 
415  /*
416  * Setup and use a per-range memory context, which is reset every time we
417  * loop below. This avoids having to free the tuples within the loop.
418  */
420  "bringetbitmap cxt",
422  oldcxt = MemoryContextSwitchTo(perRangeCxt);
423 
424  /*
425  * Now scan the revmap. We start by querying for heap page 0,
426  * incrementing by the number of pages per range; this gives us a full
427  * view of the table.
428  */
429  for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
430  {
431  bool addrange;
432  bool gottuple = false;
433  BrinTuple *tup;
434  OffsetNumber off;
435  Size size;
436 
438 
440 
441  tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
442  &off, &size, BUFFER_LOCK_SHARE,
443  scan->xs_snapshot);
444  if (tup)
445  {
446  gottuple = true;
447  btup = brin_copy_tuple(tup, size, btup, &btupsz);
449  }
450 
451  /*
452  * For page ranges with no indexed tuple, we must return the whole
453  * range; otherwise, compare it to the scan keys.
454  */
455  if (!gottuple)
456  {
457  addrange = true;
458  }
459  else
460  {
461  dtup = brin_deform_tuple(bdesc, btup, dtup);
462  if (dtup->bt_placeholder)
463  {
464  /*
465  * Placeholder tuples are always returned, regardless of the
466  * values stored in them.
467  */
468  addrange = true;
469  }
470  else
471  {
472  int keyno;
473 
474  /*
475  * Compare scan keys with summary values stored for the range.
476  * If scan keys are matched, the page range must be added to
477  * the bitmap. We initially assume the range needs to be
478  * added; in particular this serves the case where there are
479  * no keys.
480  */
481  addrange = true;
482  for (keyno = 0; keyno < scan->numberOfKeys; keyno++)
483  {
484  ScanKey key = &scan->keyData[keyno];
485  AttrNumber keyattno = key->sk_attno;
486  BrinValues *bval = &dtup->bt_columns[keyattno - 1];
487  Datum add;
488 
489  /*
490  * The collation of the scan key must match the collation
491  * used in the index column (but only if the search is not
492  * IS NULL/ IS NOT NULL). Otherwise we shouldn't be using
493  * this index ...
494  */
495  Assert((key->sk_flags & SK_ISNULL) ||
496  (key->sk_collation ==
497  TupleDescAttr(bdesc->bd_tupdesc,
498  keyattno - 1)->attcollation));
499 
500  /* First time this column? look up consistent function */
501  if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
502  {
503  FmgrInfo *tmp;
504 
505  tmp = index_getprocinfo(idxRel, keyattno,
507  fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
509  }
510 
511  /*
512  * Check whether the scan key is consistent with the page
513  * range values; if so, have the pages in the range added
514  * to the output bitmap.
515  *
516  * When there are multiple scan keys, failure to meet the
517  * criteria for a single one of them is enough to discard
518  * the range as a whole, so break out of the loop as soon
519  * as a false return value is obtained.
520  */
521  add = FunctionCall3Coll(&consistentFn[keyattno - 1],
522  key->sk_collation,
523  PointerGetDatum(bdesc),
524  PointerGetDatum(bval),
525  PointerGetDatum(key));
526  addrange = DatumGetBool(add);
527  if (!addrange)
528  break;
529  }
530  }
531  }
532 
533  /* add the pages in the range to the output bitmap, if needed */
534  if (addrange)
535  {
536  BlockNumber pageno;
537 
538  for (pageno = heapBlk;
539  pageno <= heapBlk + opaque->bo_pagesPerRange - 1;
540  pageno++)
541  {
542  MemoryContextSwitchTo(oldcxt);
543  tbm_add_page(tbm, pageno);
544  totalpages++;
545  MemoryContextSwitchTo(perRangeCxt);
546  }
547  }
548  }
549 
550  MemoryContextSwitchTo(oldcxt);
551  MemoryContextDelete(perRangeCxt);
552 
553  if (buf != InvalidBuffer)
554  ReleaseBuffer(buf);
555 
556  /*
557  * XXX We have an approximation of the number of *pages* that our scan
558  * returns, but we don't have a precise idea of the number of heap tuples
559  * involved.
560  */
561  return totalpages * 10;
562 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
Definition: fmgr.h:56
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3408
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:320
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:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
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:104
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
unsigned int Oid
Definition: postgres_ext.h:31
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:356
BlockNumber bo_pagesPerRange
Definition: brin.c:64
Relation indexRelation
Definition: relscan.h:103
uint16 OffsetNumber
Definition: off.h:24
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:421
bool bt_placeholder
Definition: brin_tuple.h:38
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:612
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
static char * buf
Definition: pg_test_fsync.c:67
BrinRevmap * bo_rmAccess
Definition: brin.c:65
#define DatumGetBool(X)
Definition: postgres.h:393
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1411
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define SK_ISNULL
Definition: skey.h:115
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
BrinDesc * bo_bdesc
Definition: brin.c:66
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define InvalidOid
Definition: postgres_ext.h:36
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1174
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:738
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:69
size_t Size
Definition: c.h:466
struct ScanKeyData * keyData
Definition: relscan.h:107
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:442
Oid sk_collation
Definition: skey.h:70
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
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:456
AttrNumber sk_attno
Definition: skey.h:67

◆ brinGetStats()

void brinGetStats ( Relation  index,
BrinStatsData stats 
)

Definition at line 1085 of file brin.c.

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

Referenced by brincostestimate().

1086 {
1087  Buffer metabuffer;
1088  Page metapage;
1089  BrinMetaPageData *metadata;
1090 
1091  metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1092  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1093  metapage = BufferGetPage(metabuffer);
1094  metadata = (BrinMetaPageData *) PageGetContents(metapage);
1095 
1096  stats->pagesPerRange = metadata->pagesPerRange;
1097  stats->revmapNumPages = metadata->lastRevmapPage - 1;
1098 
1099  UnlockReleaseBuffer(metabuffer);
1100 }
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
BlockNumber lastRevmapPage
Definition: brin_page.h:69
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
BlockNumber pagesPerRange
Definition: brin.h:33
BlockNumber pagesPerRange
Definition: brin_page.h:68
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:606
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
BlockNumber revmapNumPages
Definition: brin.h:34

◆ brinhandler()

Datum brinhandler ( PG_FUNCTION_ARGS  )

Definition at line 87 of file brin.c.

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

88 {
90 
91  amroutine->amstrategies = 0;
94  amroutine->amcanorder = false;
95  amroutine->amcanorderbyop = false;
96  amroutine->amcanbackward = false;
97  amroutine->amcanunique = false;
98  amroutine->amcanmulticol = true;
99  amroutine->amoptionalkey = true;
100  amroutine->amsearcharray = false;
101  amroutine->amsearchnulls = true;
102  amroutine->amstorage = true;
103  amroutine->amclusterable = false;
104  amroutine->ampredlocks = false;
105  amroutine->amcanparallel = false;
106  amroutine->amcaninclude = false;
107  amroutine->amusemaintenanceworkmem = false;
108  amroutine->amparallelvacuumoptions =
110  amroutine->amkeytype = InvalidOid;
111 
112  amroutine->ambuild = brinbuild;
113  amroutine->ambuildempty = brinbuildempty;
114  amroutine->aminsert = brininsert;
115  amroutine->ambulkdelete = brinbulkdelete;
116  amroutine->amvacuumcleanup = brinvacuumcleanup;
117  amroutine->amcanreturn = NULL;
118  amroutine->amcostestimate = brincostestimate;
119  amroutine->amoptions = brinoptions;
120  amroutine->amproperty = NULL;
121  amroutine->ambuildphasename = NULL;
122  amroutine->amvalidate = brinvalidate;
123  amroutine->ambeginscan = brinbeginscan;
124  amroutine->amrescan = brinrescan;
125  amroutine->amgettuple = NULL;
126  amroutine->amgetbitmap = bringetbitmap;
127  amroutine->amendscan = brinendscan;
128  amroutine->ammarkpos = NULL;
129  amroutine->amrestrpos = NULL;
130  amroutine->amestimateparallelscan = NULL;
131  amroutine->aminitparallelscan = NULL;
132  amroutine->amparallelrescan = NULL;
133 
134  PG_RETURN_POINTER(amroutine);
135 }
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:782
ambeginscan_function ambeginscan
Definition: amapi.h:227
uint8 amparallelvacuumoptions
Definition: amapi.h:205
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
ambulkdelete_function ambulkdelete
Definition: amapi.h:219
bool amcanmulticol
Definition: amapi.h:185
uint16 amsupport
Definition: amapi.h:173
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:668
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: brin.c:150
amgettuple_function amgettuple
Definition: amapi.h:229
bool amcanorderbyop
Definition: amapi.h:179
amproperty_function amproperty
Definition: amapi.h:224
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:37
amparallelrescan_function amparallelrescan
Definition: amapi.h:238
bool amstorage
Definition: amapi.h:193
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:374
bool ampredlocks
Definition: amapi.h:197
aminsert_function aminsert
Definition: amapi.h:218
Oid amkeytype
Definition: amapi.h:207
bool amoptionalkey
Definition: amapi.h:187
void brinendscan(IndexScanDesc scan)
Definition: brin.c:588
amvalidate_function amvalidate
Definition: amapi.h:226
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:827
amgetbitmap_function amgetbitmap
Definition: amapi.h:230
ambuild_function ambuild
Definition: amapi.h:216
amoptions_function amoptions
Definition: amapi.h:223
bool amcaninclude
Definition: amapi.h:201
amcostestimate_function amcostestimate
Definition: amapi.h:222
bool amcanunique
Definition: amapi.h:183
#define BRIN_PROCNUM_OPTIONS
Definition: brin_internal.h:72
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:220
amendscan_function amendscan
Definition: amapi.h:231
bool amcanbackward
Definition: amapi.h:181
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:568
amrescan_function amrescan
Definition: amapi.h:228
bool amcanparallel
Definition: amapi.h:199
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:345
bool amsearchnulls
Definition: amapi.h:191
bool amclusterable
Definition: amapi.h:195
bool amsearcharray
Definition: amapi.h:189
#define InvalidOid
Definition: postgres_ext.h:36
bool amusemaintenanceworkmem
Definition: amapi.h:203
#define makeNode(_type_)
Definition: nodes.h:577
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7227
ammarkpos_function ammarkpos
Definition: amapi.h:232
bool amcanorder
Definition: amapi.h:177
ambuildphasename_function ambuildphasename
Definition: amapi.h:225
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:236
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:75
uint16 amstrategies
Definition: amapi.h:171
uint16 amoptsprocnum
Definition: amapi.h:175
ambuildempty_function ambuildempty
Definition: amapi.h:217
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:797
void brinbuildempty(Relation index)
Definition: brin.c:752
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:60
amcanreturn_function amcanreturn
Definition: amapi.h:221
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:237
amrestrpos_function amrestrpos
Definition: amapi.h:233

◆ brininsert()

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

Definition at line 150 of file brin.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AutoVacuumRequestWork(), AVW_BRINSummarizeRange, BrinDesc::bd_tupdesc, brin_build_desc(), brin_can_do_samepage_update(), brin_copy_tuple(), brin_deform_tuple(), brin_doupdate(), brin_form_tuple(), BRIN_PROCNUM_ADDVALUE, BrinGetAutoSummarize, brinGetTupleForHeapBlock(), brinRevmapInitialize(), brinRevmapTerminate(), BrinMemTuple::bt_columns, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferIsValid, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, DatumGetBool, ereport, errcode(), errmsg(), FirstOffsetNumber, FunctionCall4Coll(), IndexInfo::ii_AmCache, IndexInfo::ii_Context, index_getprocinfo(), InvalidBuffer, ItemIdGetLength, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockBuffer(), LOG, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), TupleDescData::natts, PageGetItemId, PointerGetDatum, RelationData::rd_indcollation, RelationGetRelationName, RelationGetRelid, and ReleaseBuffer().

Referenced by brinhandler().

154 {
155  BlockNumber pagesPerRange;
156  BlockNumber origHeapBlk;
157  BlockNumber heapBlk;
158  BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache;
159  BrinRevmap *revmap;
161  MemoryContext tupcxt = NULL;
163  bool autosummarize = BrinGetAutoSummarize(idxRel);
164 
165  revmap = brinRevmapInitialize(idxRel, &pagesPerRange, NULL);
166 
167  /*
168  * origHeapBlk is the block number where the insertion occurred. heapBlk
169  * is the first block in the corresponding page range.
170  */
171  origHeapBlk = ItemPointerGetBlockNumber(heaptid);
172  heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
173 
174  for (;;)
175  {
176  bool need_insert = false;
177  OffsetNumber off;
178  BrinTuple *brtup;
179  BrinMemTuple *dtup;
180  int keyno;
181 
183 
184  /*
185  * If auto-summarization is enabled and we just inserted the first
186  * tuple into the first block of a new non-first page range, request a
187  * summarization run of the previous range.
188  */
189  if (autosummarize &&
190  heapBlk > 0 &&
191  heapBlk == origHeapBlk &&
193  {
194  BlockNumber lastPageRange = heapBlk - 1;
195  BrinTuple *lastPageTuple;
196 
197  lastPageTuple =
198  brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
199  NULL, BUFFER_LOCK_SHARE, NULL);
200  if (!lastPageTuple)
201  {
202  bool recorded;
203 
205  RelationGetRelid(idxRel),
206  lastPageRange);
207  if (!recorded)
208  ereport(LOG,
209  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
210  errmsg("request for BRIN range summarization for index \"%s\" page %u was not recorded",
211  RelationGetRelationName(idxRel),
212  lastPageRange)));
213  }
214  else
216  }
217 
218  brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
219  NULL, BUFFER_LOCK_SHARE, NULL);
220 
221  /* if range is unsummarized, there's nothing to do */
222  if (!brtup)
223  break;
224 
225  /* First time through in this statement? */
226  if (bdesc == NULL)
227  {
228  MemoryContextSwitchTo(indexInfo->ii_Context);
229  bdesc = brin_build_desc(idxRel);
230  indexInfo->ii_AmCache = (void *) bdesc;
231  MemoryContextSwitchTo(oldcxt);
232  }
233  /* First time through in this brininsert call? */
234  if (tupcxt == NULL)
235  {
237  "brininsert cxt",
239  MemoryContextSwitchTo(tupcxt);
240  }
241 
242  dtup = brin_deform_tuple(bdesc, brtup, NULL);
243 
244  /*
245  * Compare the key values of the new tuple to the stored index values;
246  * our deformed tuple will get updated if the new tuple doesn't fit
247  * the original range (note this means we can't break out of the loop
248  * early). Make a note of whether this happens, so that we know to
249  * insert the modified tuple later.
250  */
251  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
252  {
253  Datum result;
254  BrinValues *bval;
255  FmgrInfo *addValue;
256 
257  bval = &dtup->bt_columns[keyno];
258  addValue = index_getprocinfo(idxRel, keyno + 1,
260  result = FunctionCall4Coll(addValue,
261  idxRel->rd_indcollation[keyno],
262  PointerGetDatum(bdesc),
263  PointerGetDatum(bval),
264  values[keyno],
265  nulls[keyno]);
266  /* if that returned true, we need to insert the updated tuple */
267  need_insert |= DatumGetBool(result);
268  }
269 
270  if (!need_insert)
271  {
272  /*
273  * The tuple is consistent with the new values, so there's nothing
274  * to do.
275  */
277  }
278  else
279  {
280  Page page = BufferGetPage(buf);
281  ItemId lp = PageGetItemId(page, off);
282  Size origsz;
283  BrinTuple *origtup;
284  Size newsz;
285  BrinTuple *newtup;
286  bool samepage;
287 
288  /*
289  * Make a copy of the old tuple, so that we can compare it after
290  * re-acquiring the lock.
291  */
292  origsz = ItemIdGetLength(lp);
293  origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
294 
295  /*
296  * Before releasing the lock, check if we can attempt a same-page
297  * update. Another process could insert a tuple concurrently in
298  * the same page though, so downstream we must be prepared to cope
299  * if this turns out to not be possible after all.
300  */
301  newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
302  samepage = brin_can_do_samepage_update(buf, origsz, newsz);
304 
305  /*
306  * Try to update the tuple. If this doesn't work for whatever
307  * reason, we need to restart from the top; the revmap might be
308  * pointing at a different tuple for this block now, so we need to
309  * recompute to ensure both our new heap tuple and the other
310  * inserter's are covered by the combined tuple. It might be that
311  * we don't need to update at all.
312  */
313  if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
314  buf, off, origtup, origsz, newtup, newsz,
315  samepage))
316  {
317  /* no luck; start over */
319  continue;
320  }
321  }
322 
323  /* success! */
324  break;
325  }
326 
327  brinRevmapTerminate(revmap);
328  if (BufferIsValid(buf))
330  MemoryContextSwitchTo(oldcxt);
331  if (tupcxt != NULL)
332  MemoryContextDelete(tupcxt);
333 
334  return false;
335 }
bool AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3240
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
MemoryContext ii_Context
Definition: execnodes.h:179
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:320
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
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
int errcode(int sqlerrcode)
Definition: elog.c:610
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1199
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
#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:421
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:68
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
Oid * rd_indcollation
Definition: rel.h:199
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
#define DatumGetBool(X)
Definition: postgres.h:393
#define RelationGetRelationName(relation)
Definition: rel.h:490
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#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
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define ereport(elevel,...)
Definition: elog.h:144
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1018
size_t Size
Definition: c.h:466
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:167
#define BrinGetAutoSummarize(relation)
Definition: brin.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:824
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:88
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:456
Pointer Page
Definition: bufpage.h:78

◆ brinoptions()

bytea* brinoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 827 of file brin.c.

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

Referenced by brinhandler().

828 {
829  static const relopt_parse_elt tab[] = {
830  {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
831  {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
832  };
833 
834  return (bytea *) build_reloptions(reloptions, validate,
836  sizeof(BrinOptions),
837  tab, lengthof(tab));
838 }
#define lengthof(array)
Definition: c.h:668
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:555
#define offsetof(type, field)
Definition: c.h:661

◆ brinrescan()

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

Definition at line 568 of file brin.c.

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

Referenced by brinhandler().

570 {
571  /*
572  * Other index AMs preprocess the scan keys at this point, or sometime
573  * early during the scan; this lets them optimize by removing redundant
574  * keys, or doing early returns when they are impossible to satisfy; see
575  * _bt_preprocess_keys for an example. Something like that could be added
576  * here someday, too.
577  */
578 
579  if (scankey && scan->numberOfKeys > 0)
580  memmove(scan->keyData, scankey,
581  scan->numberOfKeys * sizeof(ScanKeyData));
582 }
struct ScanKeyData * keyData
Definition: relscan.h:107

◆ brinsummarize()

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

Definition at line 1303 of file brin.c.

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

Referenced by brin_summarize_range(), and brinvacuumcleanup().

1305 {
1306  BrinRevmap *revmap;
1307  BrinBuildState *state = NULL;
1308  IndexInfo *indexInfo = NULL;
1309  BlockNumber heapNumBlocks;
1310  BlockNumber pagesPerRange;
1311  Buffer buf;
1312  BlockNumber startBlk;
1313 
1314  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
1315 
1316  /* determine range of pages to process */
1317  heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1318  if (pageRange == BRIN_ALL_BLOCKRANGES)
1319  startBlk = 0;
1320  else
1321  {
1322  startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1323  heapNumBlocks = Min(heapNumBlocks, startBlk + pagesPerRange);
1324  }
1325  if (startBlk > heapNumBlocks)
1326  {
1327  /* Nothing to do if start point is beyond end of table */
1328  brinRevmapTerminate(revmap);
1329  return;
1330  }
1331 
1332  /*
1333  * Scan the revmap to find unsummarized items.
1334  */
1335  buf = InvalidBuffer;
1336  for (; startBlk < heapNumBlocks; startBlk += pagesPerRange)
1337  {
1338  BrinTuple *tup;
1339  OffsetNumber off;
1340 
1341  /*
1342  * Unless requested to summarize even a partial range, go away now if
1343  * we think the next range is partial. Caller would pass true when it
1344  * is typically run once bulk data loading is done
1345  * (brin_summarize_new_values), and false when it is typically the
1346  * result of arbitrarily-scheduled maintenance command (vacuuming).
1347  */
1348  if (!include_partial &&
1349  (startBlk + pagesPerRange > heapNumBlocks))
1350  break;
1351 
1353 
1354  tup = brinGetTupleForHeapBlock(revmap, startBlk, &buf, &off, NULL,
1355  BUFFER_LOCK_SHARE, NULL);
1356  if (tup == NULL)
1357  {
1358  /* no revmap entry for this heap range. Summarize it. */
1359  if (state == NULL)
1360  {
1361  /* first time through */
1362  Assert(!indexInfo);
1363  state = initialize_brin_buildstate(index, revmap,
1364  pagesPerRange);
1365  indexInfo = BuildIndexInfo(index);
1366  }
1367  summarize_range(indexInfo, state, heapRel, startBlk, heapNumBlocks);
1368 
1369  /* and re-initialize state for the next range */
1371 
1372  if (numSummarized)
1373  *numSummarized += 1.0;
1374  }
1375  else
1376  {
1377  if (numExisting)
1378  *numExisting += 1.0;
1380  }
1381  }
1382 
1383  if (BufferIsValid(buf))
1384  ReleaseBuffer(buf);
1385 
1386  /* free resources */
1387  brinRevmapTerminate(revmap);
1388  if (state)
1389  {
1391  pfree(indexInfo);
1392  }
1393 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
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 void summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
Definition: brin.c:1176
#define Min(x, y)
Definition: c.h:920
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2311
uint16 OffsetNumber
Definition: off.h:24
void pfree(void *pointer)
Definition: mcxt.c:1056
static char * buf
Definition: pg_test_fsync.c:67
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define Assert(condition)
Definition: c.h:738
Definition: regguts.h:298
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:69
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
BrinMemTuple * bs_dtuple
Definition: brin.c:56
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1106
BrinDesc * bs_bdesc
Definition: brin.c:55
int Buffer
Definition: buf.h:23
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:383
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1131

◆ brinvacuumcleanup()

IndexBulkDeleteResult* brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 797 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().

798 {
799  Relation heapRel;
800 
801  /* No-op in ANALYZE ONLY mode */
802  if (info->analyze_only)
803  return stats;
804 
805  if (!stats)
807  stats->num_pages = RelationGetNumberOfBlocks(info->index);
808  /* rest of stats is initialized by zeroing */
809 
810  heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
812 
813  brin_vacuum_scan(info->index, info->strategy);
814 
815  brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
816  &stats->num_index_tuples, &stats->num_index_tuples);
817 
818  table_close(heapRel, AccessShareLock);
819 
820  return stats;
821 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3408
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
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:74
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:1462
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1303
void * palloc0(Size size)
Definition: mcxt.c:980
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:69
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
double num_index_tuples
Definition: genam.h:77
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ form_and_insert_tuple()

static void form_and_insert_tuple ( BrinBuildState state)
static

Definition at line 1400 of file brin.c.

References brin_doinsert(), brin_form_tuple(), BrinBuildState::bs_bdesc, BrinBuildState::bs_currentInsertBuf, BrinBuildState::bs_currRangeStart, BrinBuildState::bs_dtuple, BrinBuildState::bs_irel, BrinBuildState::bs_numtuples, BrinBuildState::bs_pagesPerRange, BrinBuildState::bs_rmAccess, and pfree().

Referenced by brinbuild(), and brinbuildCallback().

1401 {
1402  BrinTuple *tup;
1403  Size size;
1404 
1405  tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1406  state->bs_dtuple, &size);
1407  brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1408  &state->bs_currentInsertBuf, state->bs_currRangeStart,
1409  tup, size);
1410  state->bs_numtuples++;
1411 
1412  pfree(tup);
1413 }
Buffer bs_currentInsertBuf
Definition: brin.c:51
Relation bs_irel
Definition: brin.c:49
BlockNumber bs_currRangeStart
Definition: brin.c:53
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:343
BlockNumber bs_pagesPerRange
Definition: brin.c:52
void pfree(void *pointer)
Definition: mcxt.c:1056
BrinRevmap * bs_rmAccess
Definition: brin.c:54
size_t Size
Definition: c.h:466
int bs_numtuples
Definition: brin.c:50
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:88
BrinMemTuple * bs_dtuple
Definition: brin.c:56
BrinDesc * bs_bdesc
Definition: brin.c:55

◆ initialize_brin_buildstate()

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

Definition at line 1106 of file brin.c.

References brin_build_desc(), brin_memtuple_initialize(), brin_new_memtuple(), BrinBuildState::bs_bdesc, BrinBuildState::bs_currentInsertBuf, BrinBuildState::bs_currRangeStart, BrinBuildState::bs_dtuple, BrinBuildState::bs_irel, BrinBuildState::bs_numtuples, BrinBuildState::bs_pagesPerRange, BrinBuildState::bs_rmAccess, InvalidBuffer, and palloc().

Referenced by brinbuild(), and brinsummarize().

1108 {
1110 
1111  state = palloc(sizeof(BrinBuildState));
1112 
1113  state->bs_irel = idxRel;
1114  state->bs_numtuples = 0;
1116  state->bs_pagesPerRange = pagesPerRange;
1117  state->bs_currRangeStart = 0;
1118  state->bs_rmAccess = revmap;
1119  state->bs_bdesc = brin_build_desc(idxRel);
1120  state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1121 
1123 
1124  return state;
1125 }
Buffer bs_currentInsertBuf
Definition: brin.c:51
#define InvalidBuffer
Definition: buf.h:25
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:356
Relation bs_irel
Definition: brin.c:49
BlockNumber bs_currRangeStart
Definition: brin.c:53
BlockNumber bs_pagesPerRange
Definition: brin.c:52
BrinRevmap * bs_rmAccess
Definition: brin.c:54
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1018
Definition: regguts.h:298
int bs_numtuples
Definition: brin.c:50
void * palloc(Size size)
Definition: mcxt.c:949
BrinMemTuple * bs_dtuple
Definition: brin.c:56
BrinDesc * bs_bdesc
Definition: brin.c:55
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:383

◆ summarize_range()

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

Definition at line 1176 of file brin.c.

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

Referenced by brinsummarize().

1178 {
1179  Buffer phbuf;
1180  BrinTuple *phtup;
1181  Size phsz;
1182  OffsetNumber offset;
1183  BlockNumber scanNumBlks;
1184 
1185  /*
1186  * Insert the placeholder tuple
1187  */
1188  phbuf = InvalidBuffer;
1189  phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1190  offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1191  state->bs_rmAccess, &phbuf,
1192  heapBlk, phtup, phsz);
1193 
1194  /*
1195  * Compute range end. We hold ShareUpdateExclusive lock on table, so it
1196  * cannot shrink concurrently (but it can grow).
1197  */
1198  Assert(heapBlk % state->bs_pagesPerRange == 0);
1199  if (heapBlk + state->bs_pagesPerRange > heapNumBlks)
1200  {
1201  /*
1202  * If we're asked to scan what we believe to be the final range on the
1203  * table (i.e. a range that might be partial) we need to recompute our
1204  * idea of what the latest page is after inserting the placeholder
1205  * tuple. Anyone that grows the table later will update the
1206  * placeholder tuple, so it doesn't matter that we won't scan these
1207  * pages ourselves. Careful: the table might have been extended
1208  * beyond the current range, so clamp our result.
1209  *
1210  * Fortunately, this should occur infrequently.
1211  */
1212  scanNumBlks = Min(RelationGetNumberOfBlocks(heapRel) - heapBlk,
1213  state->bs_pagesPerRange);
1214  }
1215  else
1216  {
1217  /* Easy case: range is known to be complete */
1218  scanNumBlks = state->bs_pagesPerRange;
1219  }
1220 
1221  /*
1222  * Execute the partial heap scan covering the heap blocks in the specified
1223  * page range, summarizing the heap tuples in it. This scan stops just
1224  * short of brinbuildCallback creating the new index entry.
1225  *
1226  * Note that it is critical we use the "any visible" mode of
1227  * table_index_build_range_scan here: otherwise, we would miss tuples
1228  * inserted by transactions that are still in progress, among other corner
1229  * cases.
1230  */
1231  state->bs_currRangeStart = heapBlk;
1232  table_index_build_range_scan(heapRel, state->bs_irel, indexInfo, false, true, false,
1233  heapBlk, scanNumBlks,
1234  brinbuildCallback, (void *) state, NULL);
1235 
1236  /*
1237  * Now we update the values obtained by the scan with the placeholder
1238  * tuple. We do this in a loop which only terminates if we're able to
1239  * update the placeholder tuple successfully; if we are not, this means
1240  * somebody else modified the placeholder tuple after we read it.
1241  */
1242  for (;;)
1243  {
1244  BrinTuple *newtup;
1245  Size newsize;
1246  bool didupdate;
1247  bool samepage;
1248 
1250 
1251  /*
1252  * Update the summary tuple and try to update.
1253  */
1254  newtup = brin_form_tuple(state->bs_bdesc,
1255  heapBlk, state->bs_dtuple, &newsize);
1256  samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1257  didupdate =
1258  brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1259  state->bs_rmAccess, heapBlk, phbuf, offset,
1260  phtup, phsz, newtup, newsize, samepage);
1261  brin_free_tuple(phtup);
1262  brin_free_tuple(newtup);
1263 
1264  /* If the update succeeded, we're done. */
1265  if (didupdate)
1266  break;
1267 
1268  /*
1269  * If the update didn't work, it might be because somebody updated the
1270  * placeholder tuple concurrently. Extract the new version, union it
1271  * with the values we have from the scan, and start over. (There are
1272  * other reasons for the update to fail, but it's simple to treat them
1273  * the same.)
1274  */
1275  phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1276  &offset, &phsz, BUFFER_LOCK_SHARE,
1277  NULL);
1278  /* the placeholder tuple must exist */
1279  if (phtup == NULL)
1280  elog(ERROR, "missing placeholder tuple");
1281  phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1283 
1284  /* merge it into the tuple from the heap scan */
1285  union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1286  }
1287 
1288  ReleaseBuffer(phbuf);
1289 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:320
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:1420
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:197
#define Min(x, y)
Definition: c.h:920
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
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
Relation bs_irel
Definition: brin.c:49
BlockNumber bs_currRangeStart
Definition: brin.c:53
uint16 OffsetNumber
Definition: off.h:24
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:343
BlockNumber bs_pagesPerRange
Definition: brin.c:52
#define ERROR
Definition: elog.h:43
static double table_index_build_range_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool anyvisible, bool progress, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1557
BrinRevmap * bs_rmAccess
Definition: brin.c:54
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:605
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:324
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:307
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:88
#define elog(elevel,...)
Definition: elog.h:214
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
BrinMemTuple * bs_dtuple
Definition: brin.c:56
BrinDesc * bs_bdesc
Definition: brin.c:55
int Buffer
Definition: buf.h:23
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:262

◆ terminate_brin_buildstate()

static void terminate_brin_buildstate ( BrinBuildState state)
static

Definition at line 1131 of file brin.c.

References brin_free_desc(), BrinBuildState::bs_bdesc, BrinBuildState::bs_currentInsertBuf, BrinBuildState::bs_dtuple, BrinBuildState::bs_irel, BufferGetBlockNumber(), BufferGetPage, BufferIsInvalid, FreeSpaceMapVacuumRange(), PageGetFreeSpace(), pfree(), RecordPageWithFreeSpace(), and ReleaseBuffer().

Referenced by brinbuild(), and brinsummarize().

1132 {
1133  /*
1134  * Release the last index buffer used. We might as well ensure that
1135  * whatever free space remains in that page is available in FSM, too.
1136  */
1137  if (!BufferIsInvalid(state->bs_currentInsertBuf))
1138  {
1139  Page page;
1140  Size freespace;
1141  BlockNumber blk;
1142 
1143  page = BufferGetPage(state->bs_currentInsertBuf);
1144  freespace = PageGetFreeSpace(page);
1147  RecordPageWithFreeSpace(state->bs_irel, blk, freespace);
1148  FreeSpaceMapVacuumRange(state->bs_irel, blk, blk + 1);
1149  }
1150 
1151  brin_free_desc(state->bs_bdesc);
1152  pfree(state->bs_dtuple);
1153  pfree(state);
1154 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1073
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
Buffer bs_currentInsertBuf
Definition: brin.c:51
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3483
Relation bs_irel
Definition: brin.c:49
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:574
void pfree(void *pointer)
Definition: mcxt.c:1056
#define BufferIsInvalid(buffer)
Definition: buf.h:31
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
size_t Size
Definition: c.h:466
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2633
BrinMemTuple * bs_dtuple
Definition: brin.c:56
BrinDesc * bs_bdesc
Definition: brin.c:55
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:354
Pointer Page
Definition: bufpage.h:78

◆ union_tuples()

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

Definition at line 1420 of file brin.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, BrinDesc::bd_index, BrinDesc::bd_tupdesc, brin_deform_tuple(), BRIN_PROCNUM_UNION, BrinMemTuple::bt_columns, CurrentMemoryContext, FunctionCall3Coll(), index_getprocinfo(), MemoryContextDelete(), MemoryContextSwitchTo(), TupleDescData::natts, PointerGetDatum, and RelationData::rd_indcollation.

Referenced by summarize_range().

1421 {
1422  int keyno;
1423  BrinMemTuple *db;
1424  MemoryContext cxt;
1425  MemoryContext oldcxt;
1426 
1427  /* Use our own memory context to avoid retail pfree */
1429  "brin union",
1431  oldcxt = MemoryContextSwitchTo(cxt);
1432  db = brin_deform_tuple(bdesc, b, NULL);
1433  MemoryContextSwitchTo(oldcxt);
1434 
1435  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1436  {
1437  FmgrInfo *unionFn;
1438  BrinValues *col_a = &a->bt_columns[keyno];
1439  BrinValues *col_b = &db->bt_columns[keyno];
1440 
1441  unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
1443  FunctionCall3Coll(unionFn,
1444  bdesc->bd_index->rd_indcollation[keyno],
1445  PointerGetDatum(bdesc),
1446  PointerGetDatum(col_a),
1447  PointerGetDatum(col_b));
1448  }
1449 
1450  MemoryContextDelete(cxt);
1451 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:800
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:421
Oid * rd_indcollation
Definition: rel.h:199
Relation bd_index
Definition: brin_internal.h:47
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1174
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:70