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/datum.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)
 
static bool add_values_to_range (Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, Datum *values, bool *nulls)
 
static bool check_null_keys (BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
 
Datum brinhandler (PG_FUNCTION_ARGS)
 
bool brininsert (Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, 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

◆ add_values_to_range()

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

Definition at line 1670 of file brin.c.

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

Referenced by brinbuildCallback(), and brininsert().

1672 {
1673  int keyno;
1674  bool modified = false;
1675 
1676  /*
1677  * Compare the key values of the new tuple to the stored index values; our
1678  * deformed tuple will get updated if the new tuple doesn't fit the
1679  * original range (note this means we can't break out of the loop early).
1680  * Make a note of whether this happens, so that we know to insert the
1681  * modified tuple later.
1682  */
1683  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1684  {
1685  Datum result;
1686  BrinValues *bval;
1687  FmgrInfo *addValue;
1688 
1689  bval = &dtup->bt_columns[keyno];
1690 
1691  if (bdesc->bd_info[keyno]->oi_regular_nulls && nulls[keyno])
1692  {
1693  /*
1694  * If the new value is null, we record that we saw it if it's the
1695  * first one; otherwise, there's nothing to do.
1696  */
1697  if (!bval->bv_hasnulls)
1698  {
1699  bval->bv_hasnulls = true;
1700  modified = true;
1701  }
1702 
1703  continue;
1704  }
1705 
1706  addValue = index_getprocinfo(idxRel, keyno + 1,
1708  result = FunctionCall4Coll(addValue,
1709  idxRel->rd_indcollation[keyno],
1710  PointerGetDatum(bdesc),
1711  PointerGetDatum(bval),
1712  values[keyno],
1713  nulls[keyno]);
1714  /* if that returned true, we need to insert the updated tuple */
1715  modified |= DatumGetBool(result);
1716  }
1717 
1718  return modified;
1719 }
Definition: fmgr.h:56
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1195
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:71
Oid * rd_indcollation
Definition: rel.h:212
bool bv_hasnulls
Definition: brin_tuple.h:32
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:54
#define DatumGetBool(X)
Definition: postgres.h:437
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
bool oi_regular_nulls
Definition: brin_internal.h:31
uintptr_t Datum
Definition: postgres.h:411
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
static Datum values[MAXATTR]
Definition: bootstrap.c:156

◆ 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:503
#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:207
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_desummarize_range()

Datum brin_desummarize_range ( PG_FUNCTION_ARGS  )

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

1080 {
1081  Oid indexoid = PG_GETARG_OID(0);
1082  int64 heapBlk64 = PG_GETARG_INT64(1);
1083  BlockNumber heapBlk;
1084  Oid heapoid;
1085  Relation heapRel;
1086  Relation indexRel;
1087  bool done;
1088 
1089  if (RecoveryInProgress())
1090  ereport(ERROR,
1091  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1092  errmsg("recovery is in progress"),
1093  errhint("BRIN control functions cannot be executed during recovery.")));
1094 
1095  if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
1096  {
1097  char *blk = psprintf(INT64_FORMAT, heapBlk64);
1098 
1099  ereport(ERROR,
1100  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1101  errmsg("block number out of range: %s", blk)));
1102  }
1103  heapBlk = (BlockNumber) heapBlk64;
1104 
1105  /*
1106  * We must lock table before index to avoid deadlocks. However, if the
1107  * passed indexoid isn't an index then IndexGetRelation() will fail.
1108  * Rather than emitting a not-very-helpful error message, postpone
1109  * complaining, expecting that the is-it-an-index test below will fail.
1110  */
1111  heapoid = IndexGetRelation(indexoid, true);
1112  if (OidIsValid(heapoid))
1113  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1114  else
1115  heapRel = NULL;
1116 
1117  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1118 
1119  /* Must be a BRIN index */
1120  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1121  indexRel->rd_rel->relam != BRIN_AM_OID)
1122  ereport(ERROR,
1123  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1124  errmsg("\"%s\" is not a BRIN index",
1125  RelationGetRelationName(indexRel))));
1126 
1127  /* User must own the index (comparable to privileges needed for VACUUM) */
1128  if (!pg_class_ownercheck(indexoid, GetUserId()))
1130  RelationGetRelationName(indexRel));
1131 
1132  /*
1133  * Since we did the IndexGetRelation call above without any lock, it's
1134  * barely possible that a race against an index drop/recreation could have
1135  * netted us the wrong table. Recheck.
1136  */
1137  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1138  ereport(ERROR,
1140  errmsg("could not open parent table of index \"%s\"",
1141  RelationGetRelationName(indexRel))));
1142 
1143  /* the revmap does the hard work */
1144  do
1145  {
1146  done = brinRevmapDesummarizeRange(indexRel, heapBlk);
1147  }
1148  while (!done);
1149 
1152 
1153  PG_RETURN_VOID();
1154 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3475
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
Oid GetUserId(void)
Definition: miscinit.c:495
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:698
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:8328
#define OidIsValid(objectId)
Definition: c.h:710
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define ERROR
Definition: elog.h:46
#define MaxBlockNumber
Definition: block.h:35
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define RelationGetRelationName(relation)
Definition: rel.h:511
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#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:4818
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:328
#define INT64_FORMAT
Definition: c.h:483
int errmsg(const char *fmt,...)
Definition: elog.c:909
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ 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

◆ brin_summarize_new_values()

Datum brin_summarize_new_values ( PG_FUNCTION_ARGS  )

Definition at line 987 of file brin.c.

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

988 {
989  Datum relation = PG_GETARG_DATUM(0);
990 
992  relation,
994 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:1002
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
uintptr_t Datum
Definition: postgres.h:411
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:70
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ brin_summarize_range()

Datum brin_summarize_range ( PG_FUNCTION_ARGS  )

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

1003 {
1004  Oid indexoid = PG_GETARG_OID(0);
1005  int64 heapBlk64 = PG_GETARG_INT64(1);
1006  BlockNumber heapBlk;
1007  Oid heapoid;
1008  Relation indexRel;
1009  Relation heapRel;
1010  double numSummarized = 0;
1011 
1012  if (RecoveryInProgress())
1013  ereport(ERROR,
1014  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1015  errmsg("recovery is in progress"),
1016  errhint("BRIN control functions cannot be executed during recovery.")));
1017 
1018  if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
1019  {
1020  char *blk = psprintf(INT64_FORMAT, heapBlk64);
1021 
1022  ereport(ERROR,
1023  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1024  errmsg("block number out of range: %s", blk)));
1025  }
1026  heapBlk = (BlockNumber) heapBlk64;
1027 
1028  /*
1029  * We must lock table before index to avoid deadlocks. However, if the
1030  * passed indexoid isn't an index then IndexGetRelation() will fail.
1031  * Rather than emitting a not-very-helpful error message, postpone
1032  * complaining, expecting that the is-it-an-index test below will fail.
1033  */
1034  heapoid = IndexGetRelation(indexoid, true);
1035  if (OidIsValid(heapoid))
1036  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1037  else
1038  heapRel = NULL;
1039 
1040  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1041 
1042  /* Must be a BRIN index */
1043  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1044  indexRel->rd_rel->relam != BRIN_AM_OID)
1045  ereport(ERROR,
1046  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1047  errmsg("\"%s\" is not a BRIN index",
1048  RelationGetRelationName(indexRel))));
1049 
1050  /* User must own the index (comparable to privileges needed for VACUUM) */
1051  if (!pg_class_ownercheck(indexoid, GetUserId()))
1053  RelationGetRelationName(indexRel));
1054 
1055  /*
1056  * Since we did the IndexGetRelation call above without any lock, it's
1057  * barely possible that a race against an index drop/recreation could have
1058  * netted us the wrong table. Recheck.
1059  */
1060  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1061  ereport(ERROR,
1063  errmsg("could not open parent table of index \"%s\"",
1064  RelationGetRelationName(indexRel))));
1065 
1066  /* OK, do it */
1067  brinsummarize(indexRel, heapRel, heapBlk, true, &numSummarized, NULL);
1068 
1071 
1072  PG_RETURN_INT32((int32) numSummarized);
1073 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3475
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
Oid GetUserId(void)
Definition: miscinit.c:495
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:698
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:8328
#define OidIsValid(objectId)
Definition: c.h:710
signed int int32
Definition: c.h:429
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define ERROR
Definition: elog.h:46
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define RelationGetRelationName(relation)
Definition: rel.h:511
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1445
#define ereport(elevel,...)
Definition: elog.h:157
#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:4818
#define INT64_FORMAT
Definition: c.h:483
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:70
int errmsg(const char *fmt,...)
Definition: elog.c:909
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
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 1637 of file brin.c.

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

Referenced by brinvacuumcleanup().

1638 {
1639  BlockNumber nblocks;
1640  BlockNumber blkno;
1641 
1642  /*
1643  * Scan the index in physical order, and clean up any possible mess in
1644  * each page.
1645  */
1646  nblocks = RelationGetNumberOfBlocks(idxrel);
1647  for (blkno = 0; blkno < nblocks; blkno++)
1648  {
1649  Buffer buf;
1650 
1652 
1653  buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
1654  RBM_NORMAL, strategy);
1655 
1656  brin_page_cleanup(idxrel, buf);
1657 
1658  ReleaseBuffer(buf);
1659  }
1660 
1661  /*
1662  * Update all upper pages in the index's FSM, as well. This ensures not
1663  * only that we propagate leaf-page FSM updates made by brin_page_cleanup,
1664  * but also that any pre-existing damage or out-of-dateness is repaired.
1665  */
1666  FreeSpaceMapVacuum(idxrel);
1667 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:741
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3768
void brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:620
static char * buf
Definition: pg_test_fsync.c:68
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:333
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
int Buffer
Definition: buf.h:23

◆ 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,
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:1565
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:232
#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:3791
#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:511
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:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:434
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
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:694
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define RelationNeedsWAL(relation)
Definition: rel.h:601
int bs_numtuples
Definition: brin.c:51
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
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:135
#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

◆ brinbuildCallback()

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

Definition at line 765 of file brin.c.

References add_values_to_range(), BRIN_elog, brin_memtuple_initialize(), BrinBuildState::bs_bdesc, BrinBuildState::bs_currRangeStart, BrinBuildState::bs_dtuple, BrinBuildState::bs_pagesPerRange, DEBUG2, form_and_insert_tuple(), and ItemPointerGetBlockNumber.

Referenced by brinbuild(), and summarize_range().

771 {
772  BrinBuildState *state = (BrinBuildState *) brstate;
773  BlockNumber thisblock;
774 
775  thisblock = ItemPointerGetBlockNumber(tid);
776 
777  /*
778  * If we're in a block that belongs to a future range, summarize what
779  * we've got and start afresh. Note the scan might have skipped many
780  * pages, if they were devoid of live tuples; make sure to insert index
781  * tuples for those too.
782  */
783  while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
784  {
785 
786  BRIN_elog((DEBUG2,
787  "brinbuildCallback: completed a range: %u--%u",
788  state->bs_currRangeStart,
789  state->bs_currRangeStart + state->bs_pagesPerRange));
790 
791  /* create the index tuple and insert it */
792  form_and_insert_tuple(state);
793 
794  /* set state to correspond to the next range */
795  state->bs_currRangeStart += state->bs_pagesPerRange;
796 
797  /* re-initialize state for it */
799  }
800 
801  /* Accumulate the current tuple into the running state */
802  (void) add_values_to_range(index, state->bs_bdesc, state->bs_dtuple,
803  values, isnull);
804 }
#define BRIN_elog(args)
Definition: brin_internal.h:85
static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, Datum *values, bool *nulls)
Definition: brin.c:1670
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1542
BlockNumber bs_currRangeStart
Definition: brin.c:54
BlockNumber bs_pagesPerRange
Definition: brin.c:53
#define DEBUG2
Definition: elog.h:24
Definition: regguts.h:317
static Datum values[MAXATTR]
Definition: bootstrap.c:156
BrinMemTuple * bs_dtuple
Definition: brin.c:57
BrinDesc * bs_bdesc
Definition: brin.c:56
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:506

◆ 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:1144
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1565
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:741
#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:3791
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
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:3475
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:443
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:3768
unsigned int Oid
Definition: postgres_ext.h:31
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:479
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:546
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:1067
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:4007
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
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:477
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

◆ brinGetStats()

void brinGetStats ( Relation  index,
BrinStatsData stats 
)

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

1228 {
1229  Buffer metabuffer;
1230  Page metapage;
1231  BrinMetaPageData *metadata;
1232 
1233  metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1234  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1235  metapage = BufferGetPage(metabuffer);
1236  metadata = (BrinMetaPageData *) PageGetContents(metapage);
1237 
1238  stats->pagesPerRange = metadata->pagesPerRange;
1239  stats->revmapNumPages = metadata->lastRevmapPage - 1;
1240 
1241  UnlockReleaseBuffer(metabuffer);
1242 }
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3791
#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:4007
BlockNumber pagesPerRange
Definition: brin.h:33
BlockNumber pagesPerRange
Definition: brin_page.h:68
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:694
#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 90 of file brin.c.

References IndexAmRoutine::amadjustmembers, 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.

91 {
93 
94  amroutine->amstrategies = 0;
97  amroutine->amcanorder = false;
98  amroutine->amcanorderbyop = false;
99  amroutine->amcanbackward = false;
100  amroutine->amcanunique = false;
101  amroutine->amcanmulticol = true;
102  amroutine->amoptionalkey = true;
103  amroutine->amsearcharray = false;
104  amroutine->amsearchnulls = true;
105  amroutine->amstorage = true;
106  amroutine->amclusterable = false;
107  amroutine->ampredlocks = false;
108  amroutine->amcanparallel = false;
109  amroutine->amcaninclude = false;
110  amroutine->amusemaintenanceworkmem = false;
111  amroutine->amparallelvacuumoptions =
113  amroutine->amkeytype = InvalidOid;
114 
115  amroutine->ambuild = brinbuild;
116  amroutine->ambuildempty = brinbuildempty;
117  amroutine->aminsert = brininsert;
118  amroutine->ambulkdelete = brinbulkdelete;
119  amroutine->amvacuumcleanup = brinvacuumcleanup;
120  amroutine->amcanreturn = NULL;
121  amroutine->amcostestimate = brincostestimate;
122  amroutine->amoptions = brinoptions;
123  amroutine->amproperty = NULL;
124  amroutine->ambuildphasename = NULL;
125  amroutine->amvalidate = brinvalidate;
126  amroutine->amadjustmembers = NULL;
127  amroutine->ambeginscan = brinbeginscan;
128  amroutine->amrescan = brinrescan;
129  amroutine->amgettuple = NULL;
130  amroutine->amgetbitmap = bringetbitmap;
131  amroutine->amendscan = brinendscan;
132  amroutine->ammarkpos = NULL;
133  amroutine->amrestrpos = NULL;
134  amroutine->amestimateparallelscan = NULL;
135  amroutine->aminitparallelscan = NULL;
136  amroutine->amparallelrescan = NULL;
137 
138  PG_RETURN_POINTER(amroutine);
139 }
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:924
ambeginscan_function ambeginscan
Definition: amapi.h:271
uint8 amparallelvacuumoptions
Definition: amapi.h:248
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
ambulkdelete_function ambulkdelete
Definition: amapi.h:262
bool amcanmulticol
Definition: amapi.h:228
uint16 amsupport
Definition: amapi.h:216
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:810
amgettuple_function amgettuple
Definition: amapi.h:273
bool amcanorderbyop
Definition: amapi.h:222
amproperty_function amproperty
Definition: amapi.h:267
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:37
amparallelrescan_function amparallelrescan
Definition: amapi.h:282
bool amstorage
Definition: amapi.h:236
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:354
bool ampredlocks
Definition: amapi.h:240
aminsert_function aminsert
Definition: amapi.h:261
Oid amkeytype
Definition: amapi.h:250
bool amoptionalkey
Definition: amapi.h:230
void brinendscan(IndexScanDesc scan)
Definition: brin.c:748
amvalidate_function amvalidate
Definition: amapi.h:269
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:969
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: brin.c:154
amgetbitmap_function amgetbitmap
Definition: amapi.h:274
ambuild_function ambuild
Definition: amapi.h:259
amoptions_function amoptions
Definition: amapi.h:266
bool amcaninclude
Definition: amapi.h:244
amcostestimate_function amcostestimate
Definition: amapi.h:265
bool amcanunique
Definition: amapi.h:226
#define BRIN_PROCNUM_OPTIONS
Definition: brin_internal.h:75
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:263
amendscan_function amendscan
Definition: amapi.h:275
bool amcanbackward
Definition: amapi.h:224
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:728
amrescan_function amrescan
Definition: amapi.h:272
bool amcanparallel
Definition: amapi.h:242
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:325
bool amsearchnulls
Definition: amapi.h:234
bool amclusterable
Definition: amapi.h:238
bool amsearcharray
Definition: amapi.h:232
#define InvalidOid
Definition: postgres_ext.h:36
bool amusemaintenanceworkmem
Definition: amapi.h:246
#define makeNode(_type_)
Definition: nodes.h:584
amadjustmembers_function amadjustmembers
Definition: amapi.h:270
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7706
ammarkpos_function ammarkpos
Definition: amapi.h:276
bool amcanorder
Definition: amapi.h:220
ambuildphasename_function ambuildphasename
Definition: amapi.h:268
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:280
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:78
uint16 amstrategies
Definition: amapi.h:214
uint16 amoptsprocnum
Definition: amapi.h:218
ambuildempty_function ambuildempty
Definition: amapi.h:260
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:939
void brinbuildempty(Relation index)
Definition: brin.c:894
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:60
amcanreturn_function amcanreturn
Definition: amapi.h:264
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:281
amrestrpos_function amrestrpos
Definition: amapi.h:277

◆ brininsert()

bool brininsert ( Relation  idxRel,
Datum values,
bool nulls,
ItemPointer  heaptid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
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:3348
#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:443
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:3768
#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:546
#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:511
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:4007
#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:156
#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:477
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:1904
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

◆ brinsummarize()

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

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

1447 {
1448  BrinRevmap *revmap;
1449  BrinBuildState *state = NULL;
1450  IndexInfo *indexInfo = NULL;
1451  BlockNumber heapNumBlocks;
1452  BlockNumber pagesPerRange;
1453  Buffer buf;
1454  BlockNumber startBlk;
1455 
1456  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
1457 
1458  /* determine range of pages to process */
1459  heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1460  if (pageRange == BRIN_ALL_BLOCKRANGES)
1461  startBlk = 0;
1462  else
1463  {
1464  startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1465  heapNumBlocks = Min(heapNumBlocks, startBlk + pagesPerRange);
1466  }
1467  if (startBlk > heapNumBlocks)
1468  {
1469  /* Nothing to do if start point is beyond end of table */
1470  brinRevmapTerminate(revmap);
1471  return;
1472  }
1473 
1474  /*
1475  * Scan the revmap to find unsummarized items.
1476  */
1477  buf = InvalidBuffer;
1478  for (; startBlk < heapNumBlocks; startBlk += pagesPerRange)
1479  {
1480  BrinTuple *tup;
1481  OffsetNumber off;
1482 
1483  /*
1484  * Unless requested to summarize even a partial range, go away now if
1485  * we think the next range is partial. Caller would pass true when it
1486  * is typically run once bulk data loading is done
1487  * (brin_summarize_new_values), and false when it is typically the
1488  * result of arbitrarily-scheduled maintenance command (vacuuming).
1489  */
1490  if (!include_partial &&
1491  (startBlk + pagesPerRange > heapNumBlocks))
1492  break;
1493 
1495 
1496  tup = brinGetTupleForHeapBlock(revmap, startBlk, &buf, &off, NULL,
1497  BUFFER_LOCK_SHARE, NULL);
1498  if (tup == NULL)
1499  {
1500  /* no revmap entry for this heap range. Summarize it. */
1501  if (state == NULL)
1502  {
1503  /* first time through */
1504  Assert(!indexInfo);
1505  state = initialize_brin_buildstate(index, revmap,
1506  pagesPerRange);
1507  indexInfo = BuildIndexInfo(index);
1508  }
1509  summarize_range(indexInfo, state, heapRel, startBlk, heapNumBlocks);
1510 
1511  /* and re-initialize state for the next range */
1513 
1514  if (numSummarized)
1515  *numSummarized += 1.0;
1516  }
1517  else
1518  {
1519  if (numExisting)
1520  *numExisting += 1.0;
1522  }
1523  }
1524 
1525  if (BufferIsValid(buf))
1526  ReleaseBuffer(buf);
1527 
1528  /* free resources */
1529  brinRevmapTerminate(revmap);
1530  if (state)
1531  {
1533  pfree(indexInfo);
1534  }
1535 }
#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:1318
#define Min(x, y)
Definition: c.h:986
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3768
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2378
uint16 OffsetNumber
Definition: off.h:24
void pfree(void *pointer)
Definition: mcxt.c:1169
static char * buf
Definition: pg_test_fsync.c:68
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
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:70
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
BrinMemTuple * bs_dtuple
Definition: brin.c:57
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1248
BrinDesc * bs_bdesc
Definition: brin.c:56
int Buffer
Definition: buf.h:23
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:506
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1273

◆ 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:3475
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:212
#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:477

◆ check_null_keys()

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

Definition at line 1722 of file brin.c.

References Assert, BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_hasnulls, sort-test::key, ScanKeyData::sk_attno, ScanKeyData::sk_flags, SK_ISNULL, SK_SEARCHNOTNULL, and SK_SEARCHNULL.

Referenced by bringetbitmap().

1723 {
1724  int keyno;
1725 
1726  /*
1727  * First check if there are any IS [NOT] NULL scan keys, and if we're
1728  * violating them.
1729  */
1730  for (keyno = 0; keyno < nnullkeys; keyno++)
1731  {
1732  ScanKey key = nullkeys[keyno];
1733 
1734  Assert(key->sk_attno == bval->bv_attno);
1735 
1736  /* Handle only IS NULL/IS NOT NULL tests */
1737  if (!(key->sk_flags & SK_ISNULL))
1738  continue;
1739 
1740  if (key->sk_flags & SK_SEARCHNULL)
1741  {
1742  /* IS NULL scan key, but range has no NULLs */
1743  if (!bval->bv_allnulls && !bval->bv_hasnulls)
1744  return false;
1745  }
1746  else if (key->sk_flags & SK_SEARCHNOTNULL)
1747  {
1748  /*
1749  * For IS NOT NULL, we can only skip ranges that are known to have
1750  * only nulls.
1751  */
1752  if (bval->bv_allnulls)
1753  return false;
1754  }
1755  else
1756  {
1757  /*
1758  * Neither IS NULL nor IS NOT NULL was used; assume all indexable
1759  * operators are strict and thus return false with NULL value in
1760  * the scan key.
1761  */
1762  return false;
1763  }
1764  }
1765 
1766  return true;
1767 }
bool bv_allnulls
Definition: brin_tuple.h:33
bool bv_hasnulls
Definition: brin_tuple.h:32
AttrNumber bv_attno
Definition: brin_tuple.h:31
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_ISNULL
Definition: skey.h:115
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:804
#define SK_SEARCHNULL
Definition: skey.h:121
AttrNumber sk_attno
Definition: skey.h:67

◆ form_and_insert_tuple()

static void form_and_insert_tuple ( BrinBuildState state)
static

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

1543 {
1544  BrinTuple *tup;
1545  Size size;
1546 
1547  tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1548  state->bs_dtuple, &size);
1549  brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1550  &state->bs_currentInsertBuf, state->bs_currRangeStart,
1551  tup, size);
1552  state->bs_numtuples++;
1553 
1554  pfree(tup);
1555 }
Buffer bs_currentInsertBuf
Definition: brin.c:52
Relation bs_irel
Definition: brin.c:50
BlockNumber bs_currRangeStart
Definition: brin.c:54
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:53
void pfree(void *pointer)
Definition: mcxt.c:1169
BrinRevmap * bs_rmAccess
Definition: brin.c:55
size_t Size
Definition: c.h:540
int bs_numtuples
Definition: brin.c:51
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:99
BrinMemTuple * bs_dtuple
Definition: brin.c:57
BrinDesc * bs_bdesc
Definition: brin.c:56

◆ initialize_brin_buildstate()

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

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

1250 {
1252 
1253  state = palloc(sizeof(BrinBuildState));
1254 
1255  state->bs_irel = idxRel;
1256  state->bs_numtuples = 0;
1258  state->bs_pagesPerRange = pagesPerRange;
1259  state->bs_currRangeStart = 0;
1260  state->bs_rmAccess = revmap;
1261  state->bs_bdesc = brin_build_desc(idxRel);
1262  state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1263 
1265 
1266  return state;
1267 }
Buffer bs_currentInsertBuf
Definition: brin.c:52
#define InvalidBuffer
Definition: buf.h:25
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:479
Relation bs_irel
Definition: brin.c:50
BlockNumber bs_currRangeStart
Definition: brin.c:54
BlockNumber bs_pagesPerRange
Definition: brin.c:53
BrinRevmap * bs_rmAccess
Definition: brin.c:55
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1160
Definition: regguts.h:317
int bs_numtuples
Definition: brin.c:51
void * palloc(Size size)
Definition: mcxt.c:1062
BrinMemTuple * bs_dtuple
Definition: brin.c:57
BrinDesc * bs_bdesc
Definition: brin.c:56
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:506

◆ summarize_range()

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

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

1320 {
1321  Buffer phbuf;
1322  BrinTuple *phtup;
1323  Size phsz;
1324  OffsetNumber offset;
1325  BlockNumber scanNumBlks;
1326 
1327  /*
1328  * Insert the placeholder tuple
1329  */
1330  phbuf = InvalidBuffer;
1331  phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1332  offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1333  state->bs_rmAccess, &phbuf,
1334  heapBlk, phtup, phsz);
1335 
1336  /*
1337  * Compute range end. We hold ShareUpdateExclusive lock on table, so it
1338  * cannot shrink concurrently (but it can grow).
1339  */
1340  Assert(heapBlk % state->bs_pagesPerRange == 0);
1341  if (heapBlk + state->bs_pagesPerRange > heapNumBlks)
1342  {
1343  /*
1344  * If we're asked to scan what we believe to be the final range on the
1345  * table (i.e. a range that might be partial) we need to recompute our
1346  * idea of what the latest page is after inserting the placeholder
1347  * tuple. Anyone that grows the table later will update the
1348  * placeholder tuple, so it doesn't matter that we won't scan these
1349  * pages ourselves. Careful: the table might have been extended
1350  * beyond the current range, so clamp our result.
1351  *
1352  * Fortunately, this should occur infrequently.
1353  */
1354  scanNumBlks = Min(RelationGetNumberOfBlocks(heapRel) - heapBlk,
1355  state->bs_pagesPerRange);
1356  }
1357  else
1358  {
1359  /* Easy case: range is known to be complete */
1360  scanNumBlks = state->bs_pagesPerRange;
1361  }
1362 
1363  /*
1364  * Execute the partial heap scan covering the heap blocks in the specified
1365  * page range, summarizing the heap tuples in it. This scan stops just
1366  * short of brinbuildCallback creating the new index entry.
1367  *
1368  * Note that it is critical we use the "any visible" mode of
1369  * table_index_build_range_scan here: otherwise, we would miss tuples
1370  * inserted by transactions that are still in progress, among other corner
1371  * cases.
1372  */
1373  state->bs_currRangeStart = heapBlk;
1374  table_index_build_range_scan(heapRel, state->bs_irel, indexInfo, false, true, false,
1375  heapBlk, scanNumBlks,
1376  brinbuildCallback, (void *) state, NULL);
1377 
1378  /*
1379  * Now we update the values obtained by the scan with the placeholder
1380  * tuple. We do this in a loop which only terminates if we're able to
1381  * update the placeholder tuple successfully; if we are not, this means
1382  * somebody else modified the placeholder tuple after we read it.
1383  */
1384  for (;;)
1385  {
1386  BrinTuple *newtup;
1387  Size newsize;
1388  bool didupdate;
1389  bool samepage;
1390 
1392 
1393  /*
1394  * Update the summary tuple and try to update.
1395  */
1396  newtup = brin_form_tuple(state->bs_bdesc,
1397  heapBlk, state->bs_dtuple, &newsize);
1398  samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1399  didupdate =
1400  brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1401  state->bs_rmAccess, heapBlk, phbuf, offset,
1402  phtup, phsz, newtup, newsize, samepage);
1403  brin_free_tuple(phtup);
1404  brin_free_tuple(newtup);
1405 
1406  /* If the update succeeded, we're done. */
1407  if (didupdate)
1408  break;
1409 
1410  /*
1411  * If the update didn't work, it might be because somebody updated the
1412  * placeholder tuple concurrently. Extract the new version, union it
1413  * with the values we have from the scan, and start over. (There are
1414  * other reasons for the update to fail, but it's simple to treat them
1415  * the same.)
1416  */
1417  phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1418  &offset, &phsz, BUFFER_LOCK_SHARE,
1419  NULL);
1420  /* the placeholder tuple must exist */
1421  if (phtup == NULL)
1422  elog(ERROR, "missing placeholder tuple");
1423  phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1425 
1426  /* merge it into the tuple from the heap scan */
1427  union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1428  }
1429 
1430  ReleaseBuffer(phbuf);
1431 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:443
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:1562
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:986
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3768
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:50
BlockNumber bs_currRangeStart
Definition: brin.c:54
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:53
#define ERROR
Definition: elog.h:46
static double table_index_build_range_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool anyvisible, bool progress, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1778
BrinRevmap * bs_rmAccess
Definition: brin.c:55
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:765
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:4007
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:430
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:99
#define elog(elevel,...)
Definition: elog.h:232
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
BrinMemTuple * bs_dtuple
Definition: brin.c:57
BrinDesc * bs_bdesc
Definition: brin.c:56
int Buffer
Definition: buf.h:23
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:385

◆ terminate_brin_buildstate()

static void terminate_brin_buildstate ( BrinBuildState state)
static

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

1274 {
1275  /*
1276  * Release the last index buffer used. We might as well ensure that
1277  * whatever free space remains in that page is available in FSM, too.
1278  */
1279  if (!BufferIsInvalid(state->bs_currentInsertBuf))
1280  {
1281  Page page;
1282  Size freespace;
1283  BlockNumber blk;
1284 
1285  page = BufferGetPage(state->bs_currentInsertBuf);
1286  freespace = PageGetFreeSpace(page);
1289  RecordPageWithFreeSpace(state->bs_irel, blk, freespace);
1290  FreeSpaceMapVacuumRange(state->bs_irel, blk, blk + 1);
1291  }
1292 
1293  brin_free_desc(state->bs_bdesc);
1294  pfree(state->bs_dtuple);
1295  pfree(state);
1296 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1215
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
Buffer bs_currentInsertBuf
Definition: brin.c:52
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3768
Relation bs_irel
Definition: brin.c:50
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:900
void pfree(void *pointer)
Definition: mcxt.c:1169
#define BufferIsInvalid(buffer)
Definition: buf.h:31
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
size_t Size
Definition: c.h:540
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
BrinMemTuple * bs_dtuple
Definition: brin.c:57
BrinDesc * bs_bdesc
Definition: brin.c:56
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:352
Pointer Page
Definition: bufpage.h:78

◆ union_tuples()

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

Definition at line 1562 of file brin.c.

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

Referenced by summarize_range().

1563 {
1564  int keyno;
1565  BrinMemTuple *db;
1566  MemoryContext cxt;
1567  MemoryContext oldcxt;
1568 
1569  /* Use our own memory context to avoid retail pfree */
1571  "brin union",
1573  oldcxt = MemoryContextSwitchTo(cxt);
1574  db = brin_deform_tuple(bdesc, b, NULL);
1575  MemoryContextSwitchTo(oldcxt);
1576 
1577  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1578  {
1579  FmgrInfo *unionFn;
1580  BrinValues *col_a = &a->bt_columns[keyno];
1581  BrinValues *col_b = &db->bt_columns[keyno];
1582  BrinOpcInfo *opcinfo = bdesc->bd_info[keyno];
1583 
1584  if (opcinfo->oi_regular_nulls)
1585  {
1586  /* Adjust "hasnulls". */
1587  if (!col_a->bv_hasnulls && col_b->bv_hasnulls)
1588  col_a->bv_hasnulls = true;
1589 
1590  /* If there are no values in B, there's nothing left to do. */
1591  if (col_b->bv_allnulls)
1592  continue;
1593 
1594  /*
1595  * Adjust "allnulls". If A doesn't have values, just copy the
1596  * values from B into A, and we're done. We cannot run the
1597  * operators in this case, because values in A might contain
1598  * garbage. Note we already established that B contains values.
1599  */
1600  if (col_a->bv_allnulls)
1601  {
1602  int i;
1603 
1604  col_a->bv_allnulls = false;
1605 
1606  for (i = 0; i < opcinfo->oi_nstored; i++)
1607  col_a->bv_values[i] =
1608  datumCopy(col_b->bv_values[i],
1609  opcinfo->oi_typcache[i]->typbyval,
1610  opcinfo->oi_typcache[i]->typlen);
1611 
1612  continue;
1613  }
1614  }
1615 
1616  unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
1618  FunctionCall3Coll(unionFn,
1619  bdesc->bd_index->rd_indcollation[keyno],
1620  PointerGetDatum(bdesc),
1621  PointerGetDatum(col_a),
1622  PointerGetDatum(col_b));
1623  }
1624 
1625  MemoryContextDelete(cxt);
1626 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
#define PointerGetDatum(X)
Definition: postgres.h:600
bool bv_allnulls
Definition: brin_tuple.h:33
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int16 typlen
Definition: typcache.h:39
bool typbyval
Definition: typcache.h:40
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:546
Oid * rd_indcollation
Definition: rel.h:212
Relation bd_index
Definition: brin_internal.h:50
bool bv_hasnulls
Definition: brin_tuple.h:32
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
uint16 oi_nstored
Definition: brin_internal.h:28
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:54
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
bool oi_regular_nulls
Definition: brin_internal.h:31
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1170
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:73
int i
Datum * bv_values
Definition: brin_tuple.h:34