PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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/reloptions.h"
#include "access/relscan.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for brin.c:

Go to the source code of this file.

Data Structures

struct  BrinBuildState
 
struct  BrinOpaque
 

Macros

#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber
 

Typedefs

typedef struct BrinBuildState BrinBuildState
 
typedef struct BrinOpaque BrinOpaque
 

Functions

static BrinBuildStateinitialize_brin_buildstate (Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
 
static void terminate_brin_buildstate (BrinBuildState *state)
 
static void brinsummarize (Relation index, Relation heapRel, BlockNumber pageRange, double *numSummarized, double *numExisting)
 
static void form_and_insert_tuple (BrinBuildState *state)
 
static void union_tuples (BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
 
static void brin_vacuum_scan (Relation idxrel, BufferAccessStrategy strategy)
 
Datum brinhandler (PG_FUNCTION_ARGS)
 
bool brininsert (Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
 
IndexScanDesc brinbeginscan (Relation r, int nkeys, int norderbys)
 
int64 bringetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
void brinrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void brinendscan (IndexScanDesc scan)
 
static void brinbuildCallback (Relation index, HeapTuple htup, 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

#define BRIN_ALL_BLOCKRANGES   InvalidBlockNumber

Typedef Documentation

Function Documentation

BrinDesc* brin_build_desc ( Relation  rel)

Definition at line 995 of file brin.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), tupleDesc::attrs, 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(), tupleDesc::natts, NULL, offsetof, BrinOpcInfo::oi_nstored, palloc(), pfree(), RelationGetDescr, and totalsize.

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

996 {
997  BrinOpcInfo **opcinfo;
998  BrinDesc *bdesc;
999  TupleDesc tupdesc;
1000  int totalstored = 0;
1001  int keyno;
1002  long totalsize;
1003  MemoryContext cxt;
1004  MemoryContext oldcxt;
1005 
1007  "brin desc cxt",
1009  oldcxt = MemoryContextSwitchTo(cxt);
1010  tupdesc = RelationGetDescr(rel);
1011 
1012  /*
1013  * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1014  * the number of columns stored, since the number is opclass-defined.
1015  */
1016  opcinfo = (BrinOpcInfo **) palloc(sizeof(BrinOpcInfo *) * tupdesc->natts);
1017  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1018  {
1019  FmgrInfo *opcInfoFn;
1020 
1021  opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1022 
1023  opcinfo[keyno] = (BrinOpcInfo *)
1024  DatumGetPointer(FunctionCall1(opcInfoFn,
1025  tupdesc->attrs[keyno]->atttypid));
1026  totalstored += opcinfo[keyno]->oi_nstored;
1027  }
1028 
1029  /* Allocate our result struct and fill it in */
1030  totalsize = offsetof(BrinDesc, bd_info) +
1031  sizeof(BrinOpcInfo *) * tupdesc->natts;
1032 
1033  bdesc = palloc(totalsize);
1034  bdesc->bd_context = cxt;
1035  bdesc->bd_index = rel;
1036  bdesc->bd_tupdesc = tupdesc;
1037  bdesc->bd_disktdesc = NULL; /* generated lazily */
1038  bdesc->bd_totalstored = totalstored;
1039 
1040  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1041  bdesc->bd_info[keyno] = opcinfo[keyno];
1042  pfree(opcinfo);
1043 
1044  MemoryContextSwitchTo(oldcxt);
1045 
1046  return bdesc;
1047 }
Definition: fmgr.h:56
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
#define RelationGetDescr(relation)
Definition: rel.h:429
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:67
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:950
Relation bd_index
Definition: brin_internal.h:47
uint16 oi_nstored
Definition: brin_internal.h:28
int bd_totalstored
Definition: brin_internal.h:56
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static uint64 totalsize
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:59
#define NULL
Definition: c.h:229
#define DatumGetPointer(X)
Definition: postgres.h:555
MemoryContext bd_context
Definition: brin_internal.h:44
void * palloc(Size size)
Definition: mcxt.c:849
TupleDesc bd_disktdesc
Definition: brin_internal.h:53
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:602
#define offsetof(type, field)
Definition: c.h:555
Datum brin_desummarize_range ( PG_FUNCTION_ARGS  )

Definition at line 921 of file brin.c.

References ACL_KIND_CLASS, aclcheck_error(), ACLCHECK_NOT_OWNER, BRIN_AM_OID, brinRevmapDesummarizeRange(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, GetUserId(), heap_open(), index_open(), IndexGetRelation(), INT64_FORMAT, MaxBlockNumber, NULL, OidIsValid, pg_class_ownercheck(), PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_VOID, psprintf(), RelationData::rd_rel, relation_close(), RelationGetRelationName, RELKIND_INDEX, and ShareUpdateExclusiveLock.

922 {
923  Oid indexoid = PG_GETARG_OID(0);
924  int64 heapBlk64 = PG_GETARG_INT64(1);
925  BlockNumber heapBlk;
926  Oid heapoid;
927  Relation heapRel;
928  Relation indexRel;
929  bool done;
930 
931  if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
932  {
933  char *blk = psprintf(INT64_FORMAT, heapBlk64);
934 
935  ereport(ERROR,
936  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
937  errmsg("block number out of range: %s", blk)));
938  }
939  heapBlk = (BlockNumber) heapBlk64;
940 
941  /*
942  * We must lock table before index to avoid deadlocks. However, if the
943  * passed indexoid isn't an index then IndexGetRelation() will fail.
944  * Rather than emitting a not-very-helpful error message, postpone
945  * complaining, expecting that the is-it-an-index test below will fail.
946  */
947  heapoid = IndexGetRelation(indexoid, true);
948  if (OidIsValid(heapoid))
949  heapRel = heap_open(heapoid, ShareUpdateExclusiveLock);
950  else
951  heapRel = NULL;
952 
953  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
954 
955  /* Must be a BRIN index */
956  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
957  indexRel->rd_rel->relam != BRIN_AM_OID)
958  ereport(ERROR,
959  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
960  errmsg("\"%s\" is not a BRIN index",
961  RelationGetRelationName(indexRel))));
962 
963  /* User must own the index (comparable to privileges needed for VACUUM) */
964  if (!pg_class_ownercheck(indexoid, GetUserId()))
966  RelationGetRelationName(indexRel));
967 
968  /*
969  * Since we did the IndexGetRelation call above without any lock, it's
970  * barely possible that a race against an index drop/recreation could have
971  * netted us the wrong table. Recheck.
972  */
973  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
974  ereport(ERROR,
976  errmsg("could not open parent table of index %s",
977  RelationGetRelationName(indexRel))));
978 
979  /* the revmap does the hard work */
980  do {
981  done = brinRevmapDesummarizeRange(indexRel, heapBlk);
982  }
983  while (!done);
984 
987 
988  PG_RETURN_VOID();
989 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3275
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
#define BRIN_AM_OID
Definition: pg_am.h:85
Oid GetUserId(void)
Definition: miscinit.c:283
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define ERROR
Definition: elog.h:43
#define MaxBlockNumber
Definition: block.h:35
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define NULL
Definition: c.h:229
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4546
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:318
#define INT64_FORMAT
Definition: c.h:315
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_INDEX
Definition: pg_class.h:161
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
void brin_free_desc ( BrinDesc bdesc)

Definition at line 1050 of file brin.c.

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

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

1051 {
1052  /* make sure the tupdesc is still valid */
1053  Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1054  /* no need for retail pfree */
1056 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
#define Assert(condition)
Definition: c.h:675
MemoryContext bd_context
Definition: brin_internal.h:44
int tdrefcount
Definition: tupdesc.h:80
Datum brin_summarize_new_values ( PG_FUNCTION_ARGS  )

Definition at line 835 of file brin.c.

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

836 {
837  Datum relation = PG_GETARG_DATUM(0);
838 
840  relation,
842 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:850
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
uintptr_t Datum
Definition: postgres.h:372
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:64
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum brin_summarize_range ( PG_FUNCTION_ARGS  )

Definition at line 850 of file brin.c.

References ACL_KIND_CLASS, aclcheck_error(), ACLCHECK_NOT_OWNER, BRIN_ALL_BLOCKRANGES, BRIN_AM_OID, brinsummarize(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, GetUserId(), heap_open(), index_open(), IndexGetRelation(), INT64_FORMAT, NULL, OidIsValid, pg_class_ownercheck(), PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_INT32, psprintf(), RelationData::rd_rel, relation_close(), RelationGetRelationName, RELKIND_INDEX, and ShareUpdateExclusiveLock.

Referenced by brin_summarize_new_values(), and perform_work_item().

851 {
852  Oid indexoid = PG_GETARG_OID(0);
853  int64 heapBlk64 = PG_GETARG_INT64(1);
854  BlockNumber heapBlk;
855  Oid heapoid;
856  Relation indexRel;
857  Relation heapRel;
858  double numSummarized = 0;
859 
860  if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
861  {
862  char *blk = psprintf(INT64_FORMAT, heapBlk64);
863 
864  ereport(ERROR,
865  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
866  errmsg("block number out of range: %s", blk)));
867  }
868  heapBlk = (BlockNumber) heapBlk64;
869 
870  /*
871  * We must lock table before index to avoid deadlocks. However, if the
872  * passed indexoid isn't an index then IndexGetRelation() will fail.
873  * Rather than emitting a not-very-helpful error message, postpone
874  * complaining, expecting that the is-it-an-index test below will fail.
875  */
876  heapoid = IndexGetRelation(indexoid, true);
877  if (OidIsValid(heapoid))
878  heapRel = heap_open(heapoid, ShareUpdateExclusiveLock);
879  else
880  heapRel = NULL;
881 
882  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
883 
884  /* Must be a BRIN index */
885  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
886  indexRel->rd_rel->relam != BRIN_AM_OID)
887  ereport(ERROR,
888  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
889  errmsg("\"%s\" is not a BRIN index",
890  RelationGetRelationName(indexRel))));
891 
892  /* User must own the index (comparable to privileges needed for VACUUM) */
893  if (!pg_class_ownercheck(indexoid, GetUserId()))
895  RelationGetRelationName(indexRel));
896 
897  /*
898  * Since we did the IndexGetRelation call above without any lock, it's
899  * barely possible that a race against an index drop/recreation could have
900  * netted us the wrong table. Recheck.
901  */
902  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
903  ereport(ERROR,
905  errmsg("could not open parent table of index %s",
906  RelationGetRelationName(indexRel))));
907 
908  /* OK, do it */
909  brinsummarize(indexRel, heapRel, heapBlk, &numSummarized, NULL);
910 
913 
914  PG_RETURN_INT32((int32) numSummarized);
915 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3275
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
#define BRIN_AM_OID
Definition: pg_am.h:85
Oid GetUserId(void)
Definition: miscinit.c:283
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, double *numSummarized, double *numExisting)
Definition: brin.c:1239
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define NULL
Definition: c.h:229
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4546
#define INT64_FORMAT
Definition: c.h:315
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:64
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_INDEX
Definition: pg_class.h:161
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
static void brin_vacuum_scan ( Relation  idxrel,
BufferAccessStrategy  strategy 
)
static

Definition at line 1397 of file brin.c.

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

Referenced by brinvacuumcleanup().

1398 {
1399  bool vacuum_fsm = false;
1400  BlockNumber blkno;
1401 
1402  /*
1403  * Scan the index in physical order, and clean up any possible mess in
1404  * each page.
1405  */
1406  for (blkno = 0; blkno < RelationGetNumberOfBlocks(idxrel); blkno++)
1407  {
1408  Buffer buf;
1409 
1411 
1412  buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
1413  RBM_NORMAL, strategy);
1414 
1415  vacuum_fsm |= brin_page_cleanup(idxrel, buf);
1416 
1417  ReleaseBuffer(buf);
1418  }
1419 
1420  /*
1421  * If we made any change to the FSM, make sure the new info is visible all
1422  * the way to the top.
1423  */
1424  if (vacuum_fsm)
1425  FreeSpaceMapVacuum(idxrel);
1426 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
static char * buf
Definition: pg_test_fsync.c:66
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
bool brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:599
int Buffer
Definition: buf.h:23
IndexScanDesc brinbeginscan ( Relation  r,
int  nkeys,
int  norderbys 
)

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

324 {
325  IndexScanDesc scan;
326  BrinOpaque *opaque;
327 
328  scan = RelationGetIndexScan(r, nkeys, norderbys);
329 
330  opaque = (BrinOpaque *) palloc(sizeof(BrinOpaque));
331  opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
332  scan->xs_snapshot);
333  opaque->bo_bdesc = brin_build_desc(r);
334  scan->opaque = opaque;
335 
336  return scan;
337 }
Snapshot xs_snapshot
Definition: relscan.h:90
BlockNumber bo_pagesPerRange
Definition: brin.c:59
BrinRevmap * bo_rmAccess
Definition: brin.c:60
BrinDesc * bo_bdesc
Definition: brin.c:61
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:995
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
void * palloc(Size size)
Definition: mcxt.c:849
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
IndexBuildResult* brinbuild ( Relation  heap,
Relation  index,
IndexInfo indexInfo 
)

Definition at line 644 of file brin.c.

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

Referenced by brinhandler().

645 {
647  double reltuples;
648  double idxtuples;
649  BrinRevmap *revmap;
651  Buffer meta;
652  BlockNumber pagesPerRange;
653 
654  /*
655  * We expect to be called exactly once for any index relation.
656  */
657  if (RelationGetNumberOfBlocks(index) != 0)
658  elog(ERROR, "index \"%s\" already contains data",
659  RelationGetRelationName(index));
660 
661  /*
662  * Critical section not required, because on error the creation of the
663  * whole relation will be rolled back.
664  */
665 
666  meta = ReadBuffer(index, P_NEW);
669 
672  MarkBufferDirty(meta);
673 
674  if (RelationNeedsWAL(index))
675  {
676  xl_brin_createidx xlrec;
677  XLogRecPtr recptr;
678  Page page;
679 
681  xlrec.pagesPerRange = BrinGetPagesPerRange(index);
682 
683  XLogBeginInsert();
684  XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
686 
687  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
688 
689  page = BufferGetPage(meta);
690  PageSetLSN(page, recptr);
691  }
692 
693  UnlockReleaseBuffer(meta);
694 
695  /*
696  * Initialize our state, including the deformed tuple state.
697  */
698  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
699  state = initialize_brin_buildstate(index, revmap, pagesPerRange);
700 
701  /*
702  * Now scan the relation. No syncscan allowed here because we want the
703  * heap blocks in physical order.
704  */
705  reltuples = IndexBuildHeapScan(heap, index, indexInfo, false,
706  brinbuildCallback, (void *) state);
707 
708  /* process the final batch */
709  form_and_insert_tuple(state);
710 
711  /* release resources */
712  idxtuples = state->bs_numtuples;
715 
716  /*
717  * Return statistics
718  */
719  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
720 
721  result->heap_tuples = reltuples;
722  result->index_tuples = idxtuples;
723 
724  return result;
725 }
#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:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define XLOG_BRIN_CREATE_INDEX
Definition: brin_xlog.h:31
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
static void brinbuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:582
return result
Definition: formatting.c:1618
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1335
#define P_NEW
Definition: bufmgr.h:82
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:480
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
BrinRevmap * bs_rmAccess
Definition: brin.c:49
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
Definition: regguts.h:298
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define RelationNeedsWAL(relation)
Definition: rel.h:506
int bs_numtuples
Definition: brin.c:45
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2169
void * palloc(Size size)
Definition: mcxt.c:849
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1083
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1108
static void brinbuildCallback ( Relation  index,
HeapTuple  htup,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  brstate 
)
static

Definition at line 582 of file brin.c.

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

Referenced by brinbuild(), and summarize_range().

588 {
589  BrinBuildState *state = (BrinBuildState *) brstate;
590  BlockNumber thisblock;
591  int i;
592 
593  thisblock = ItemPointerGetBlockNumber(&htup->t_self);
594 
595  /*
596  * If we're in a block that belongs to a future range, summarize what
597  * we've got and start afresh. Note the scan might have skipped many
598  * pages, if they were devoid of live tuples; make sure to insert index
599  * tuples for those too.
600  */
601  while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
602  {
603 
604  BRIN_elog((DEBUG2,
605  "brinbuildCallback: completed a range: %u--%u",
606  state->bs_currRangeStart,
607  state->bs_currRangeStart + state->bs_pagesPerRange));
608 
609  /* create the index tuple and insert it */
610  form_and_insert_tuple(state);
611 
612  /* set state to correspond to the next range */
613  state->bs_currRangeStart += state->bs_pagesPerRange;
614 
615  /* re-initialize state for it */
617  }
618 
619  /* Accumulate the current tuple into the running state */
620  for (i = 0; i < state->bs_bdesc->bd_tupdesc->natts; i++)
621  {
622  FmgrInfo *addValue;
623  BrinValues *col;
624 
625  col = &state->bs_dtuple->bt_columns[i];
626  addValue = index_getprocinfo(index, i + 1,
628 
629  /*
630  * Update dtuple state, if and as necessary.
631  */
632  FunctionCall4Coll(addValue,
633  state->bs_bdesc->bd_tupdesc->attrs[i]->attcollation,
634  PointerGetDatum(state->bs_bdesc),
635  PointerGetDatum(col),
636  values[i], isnull[i]);
637  }
638 }
Definition: fmgr.h:56
#define BRIN_elog(args)
Definition: brin_internal.h:81
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
#define PointerGetDatum(X)
Definition: postgres.h:562
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1094
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1335
BlockNumber bs_currRangeStart
Definition: brin.c:48
int natts
Definition: tupdesc.h:73
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:68
BlockNumber bs_pagesPerRange
Definition: brin.c:47
ItemPointerData t_self
Definition: htup.h:65
#define DEBUG2
Definition: elog.h:24
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
Definition: regguts.h:298
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int i
BrinMemTuple * bs_dtuple
Definition: brin.c:51
BrinDesc * bs_bdesc
Definition: brin.c:50
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:75
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:384
void brinbuildempty ( Relation  index)

Definition at line 728 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(), NULL, P_NEW, RBM_NORMAL, ReadBufferExtended(), START_CRIT_SECTION, and UnlockReleaseBuffer().

Referenced by brinhandler().

729 {
730  Buffer metabuf;
731 
732  /* An empty BRIN index has a metapage only. */
733  metabuf =
736 
737  /* Initialize and xlog metabuffer. */
741  MarkBufferDirty(metabuf);
742  log_newpage_buffer(metabuf, false);
744 
745  UnlockReleaseBuffer(metabuf);
746 }
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1010
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
#define P_NEW
Definition: bufmgr.h:82
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:480
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define NULL
Definition: c.h:229
int Buffer
Definition: buf.h:23
IndexBulkDeleteResult* brinbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 758 of file brin.c.

References NULL, and palloc0().

Referenced by brinhandler().

760 {
761  /* allocate stats if first time through, else re-use existing struct */
762  if (stats == NULL)
764 
765  return stats;
766 }
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
void brinendscan ( IndexScanDesc  scan)

Definition at line 565 of file brin.c.

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

Referenced by brinhandler().

566 {
567  BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
568 
570  brin_free_desc(opaque->bo_bdesc);
571  pfree(opaque);
572 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1050
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
void pfree(void *pointer)
Definition: mcxt.c:950
BrinRevmap * bo_rmAccess
Definition: brin.c:60
BrinDesc * bo_bdesc
Definition: brin.c:61
int64 bringetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 352 of file brin.c.

References AccessShareLock, addrange(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, tupleDesc::attrs, BrinDesc::bd_tupdesc, BrinOpaque::bo_bdesc, BrinOpaque::bo_pagesPerRange, BrinOpaque::bo_rmAccess, brin_copy_tuple(), brin_deform_tuple(), brin_new_memtuple(), BRIN_PROCNUM_CONSISTENT, brinGetTupleForHeapBlock(), BrinMemTuple::bt_columns, BrinMemTuple::bt_placeholder, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, DatumGetBool, fmgr_info_copy(), FunctionCall3Coll(), heap_close, heap_open(), index_getprocinfo(), IndexGetRelation(), IndexScanDescData::indexRelation, InvalidBuffer, InvalidOid, IndexScanDescData::keyData, LockBuffer(), MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), tupleDesc::natts, NULL, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, palloc0(), pgstat_count_index_scan, PointerGetDatum, RelationGetNumberOfBlocks, RelationGetRelid, ReleaseBuffer(), ScanKeyData::sk_attno, ScanKeyData::sk_collation, ScanKeyData::sk_flags, SK_ISNULL, tbm_add_page(), and IndexScanDescData::xs_snapshot.

Referenced by brinhandler().

353 {
354  Relation idxRel = scan->indexRelation;
356  BrinDesc *bdesc;
357  Oid heapOid;
358  Relation heapRel;
359  BrinOpaque *opaque;
360  BlockNumber nblocks;
361  BlockNumber heapBlk;
362  int totalpages = 0;
363  FmgrInfo *consistentFn;
364  MemoryContext oldcxt;
365  MemoryContext perRangeCxt;
366  BrinMemTuple *dtup;
367  BrinTuple *btup = NULL;
368  Size btupsz = 0;
369 
370  opaque = (BrinOpaque *) scan->opaque;
371  bdesc = opaque->bo_bdesc;
372  pgstat_count_index_scan(idxRel);
373 
374  /*
375  * We need to know the size of the table so that we know how long to
376  * iterate on the revmap.
377  */
378  heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
379  heapRel = heap_open(heapOid, AccessShareLock);
380  nblocks = RelationGetNumberOfBlocks(heapRel);
381  heap_close(heapRel, AccessShareLock);
382 
383  /*
384  * Make room for the consistent support procedures of indexed columns. We
385  * don't look them up here; we do that lazily the first time we see a scan
386  * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
387  */
388  consistentFn = palloc0(sizeof(FmgrInfo) * bdesc->bd_tupdesc->natts);
389 
390  /* allocate an initial in-memory tuple, out of the per-range memcxt */
391  dtup = brin_new_memtuple(bdesc);
392 
393  /*
394  * Setup and use a per-range memory context, which is reset every time we
395  * loop below. This avoids having to free the tuples within the loop.
396  */
398  "bringetbitmap cxt",
400  oldcxt = MemoryContextSwitchTo(perRangeCxt);
401 
402  /*
403  * Now scan the revmap. We start by querying for heap page 0,
404  * incrementing by the number of pages per range; this gives us a full
405  * view of the table.
406  */
407  for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
408  {
409  bool addrange;
410  bool gottuple = false;
411  BrinTuple *tup;
412  OffsetNumber off;
413  Size size;
414 
416 
418 
419  tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
420  &off, &size, BUFFER_LOCK_SHARE,
421  scan->xs_snapshot);
422  if (tup)
423  {
424  gottuple = true;
425  btup = brin_copy_tuple(tup, size, btup, &btupsz);
427  }
428 
429  /*
430  * For page ranges with no indexed tuple, we must return the whole
431  * range; otherwise, compare it to the scan keys.
432  */
433  if (!gottuple)
434  {
435  addrange = true;
436  }
437  else
438  {
439  dtup = brin_deform_tuple(bdesc, btup, dtup);
440  if (dtup->bt_placeholder)
441  {
442  /*
443  * Placeholder tuples are always returned, regardless of the
444  * values stored in them.
445  */
446  addrange = true;
447  }
448  else
449  {
450  int keyno;
451 
452  /*
453  * Compare scan keys with summary values stored for the range.
454  * If scan keys are matched, the page range must be added to
455  * the bitmap. We initially assume the range needs to be
456  * added; in particular this serves the case where there are
457  * no keys.
458  */
459  addrange = true;
460  for (keyno = 0; keyno < scan->numberOfKeys; keyno++)
461  {
462  ScanKey key = &scan->keyData[keyno];
463  AttrNumber keyattno = key->sk_attno;
464  BrinValues *bval = &dtup->bt_columns[keyattno - 1];
465  Datum add;
466 
467  /*
468  * The collation of the scan key must match the collation
469  * used in the index column (but only if the search is not
470  * IS NULL/ IS NOT NULL). Otherwise we shouldn't be using
471  * this index ...
472  */
473  Assert((key->sk_flags & SK_ISNULL) ||
474  (key->sk_collation ==
475  bdesc->bd_tupdesc->attrs[keyattno - 1]->attcollation));
476 
477  /* First time this column? look up consistent function */
478  if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
479  {
480  FmgrInfo *tmp;
481 
482  tmp = index_getprocinfo(idxRel, keyattno,
484  fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
486  }
487 
488  /*
489  * Check whether the scan key is consistent with the page
490  * range values; if so, have the pages in the range added
491  * to the output bitmap.
492  *
493  * When there are multiple scan keys, failure to meet the
494  * criteria for a single one of them is enough to discard
495  * the range as a whole, so break out of the loop as soon
496  * as a false return value is obtained.
497  */
498  add = FunctionCall3Coll(&consistentFn[keyattno - 1],
499  key->sk_collation,
500  PointerGetDatum(bdesc),
501  PointerGetDatum(bval),
502  PointerGetDatum(key));
503  addrange = DatumGetBool(add);
504  if (!addrange)
505  break;
506  }
507  }
508  }
509 
510  /* add the pages in the range to the output bitmap, if needed */
511  if (addrange)
512  {
513  BlockNumber pageno;
514 
515  for (pageno = heapBlk;
516  pageno <= heapBlk + opaque->bo_pagesPerRange - 1;
517  pageno++)
518  {
519  MemoryContextSwitchTo(oldcxt);
520  tbm_add_page(tbm, pageno);
521  totalpages++;
522  MemoryContextSwitchTo(perRangeCxt);
523  }
524  }
525  }
526 
527  MemoryContextSwitchTo(oldcxt);
528  MemoryContextDelete(perRangeCxt);
529 
530  if (buf != InvalidBuffer)
531  ReleaseBuffer(buf);
532 
533  /*
534  * XXX We have an approximation of the number of *pages* that our scan
535  * returns, but we don't have a precise idea of the number of heap tuples
536  * involved.
537  */
538  return totalpages * 10;
539 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
Definition: fmgr.h:56
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3275
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:321
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:194
#define PointerGetDatum(X)
Definition: postgres.h:562
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
Snapshot xs_snapshot
Definition: relscan.h:90
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:357
int natts
Definition: tupdesc.h:73
BlockNumber bo_pagesPerRange
Definition: brin.c:59
Relation indexRelation
Definition: relscan.h:89
uint16 OffsetNumber
Definition: off.h:24
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:422
bool bt_placeholder
Definition: brin_tuple.h:38
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:524
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
static char * buf
Definition: pg_test_fsync.c:66
BrinRevmap * bo_rmAccess
Definition: brin.c:60
#define DatumGetBool(X)
Definition: postgres.h:399
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1263
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define SK_ISNULL
Definition: skey.h:115
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
BrinDesc * bo_bdesc
Definition: brin.c:61
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define InvalidOid
Definition: postgres_ext.h:36
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1069
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:69
size_t Size
Definition: c.h:356
ScanKey keyData
Definition: relscan.h:93
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:469
Oid sk_collation
Definition: skey.h:70
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int Buffer
Definition: buf.h:23
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:417
AttrNumber sk_attno
Definition: skey.h:67
void brinGetStats ( Relation  index,
BrinStatsData stats 
)

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

1063 {
1064  Buffer metabuffer;
1065  Page metapage;
1066  BrinMetaPageData *metadata;
1067 
1068  metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1069  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1070  metapage = BufferGetPage(metabuffer);
1071  metadata = (BrinMetaPageData *) PageGetContents(metapage);
1072 
1073  stats->pagesPerRange = metadata->pagesPerRange;
1074  stats->revmapNumPages = metadata->lastRevmapPage - 1;
1075 
1076  UnlockReleaseBuffer(metabuffer);
1077 }
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
BlockNumber lastRevmapPage
Definition: brin_page.h:69
#define PageGetContents(page)
Definition: bufpage.h:243
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber pagesPerRange
Definition: brin.h:34
BlockNumber pagesPerRange
Definition: brin_page.h:68
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
BlockNumber revmapNumPages
Definition: brin.h:35
Datum brinhandler ( PG_FUNCTION_ARGS  )

Definition at line 82 of file brin.c.

References IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, 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::amparallelrescan, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, BRIN_LAST_OPTIONAL_PROCNUM, brinbeginscan(), brinbuild(), brinbuildempty(), brinbulkdelete(), brincostestimate(), brinendscan(), bringetbitmap(), brininsert(), brinoptions(), brinrescan(), brinvacuumcleanup(), brinvalidate(), InvalidOid, makeNode, NULL, and PG_RETURN_POINTER.

83 {
85 
86  amroutine->amstrategies = 0;
88  amroutine->amcanorder = false;
89  amroutine->amcanorderbyop = false;
90  amroutine->amcanbackward = false;
91  amroutine->amcanunique = false;
92  amroutine->amcanmulticol = true;
93  amroutine->amoptionalkey = true;
94  amroutine->amsearcharray = false;
95  amroutine->amsearchnulls = true;
96  amroutine->amstorage = true;
97  amroutine->amclusterable = false;
98  amroutine->ampredlocks = false;
99  amroutine->amcanparallel = false;
100  amroutine->amkeytype = InvalidOid;
101 
102  amroutine->ambuild = brinbuild;
103  amroutine->ambuildempty = brinbuildempty;
104  amroutine->aminsert = brininsert;
105  amroutine->ambulkdelete = brinbulkdelete;
106  amroutine->amvacuumcleanup = brinvacuumcleanup;
107  amroutine->amcanreturn = NULL;
108  amroutine->amcostestimate = brincostestimate;
109  amroutine->amoptions = brinoptions;
110  amroutine->amproperty = NULL;
111  amroutine->amvalidate = brinvalidate;
112  amroutine->ambeginscan = brinbeginscan;
113  amroutine->amrescan = brinrescan;
114  amroutine->amgettuple = NULL;
115  amroutine->amgetbitmap = bringetbitmap;
116  amroutine->amendscan = brinendscan;
117  amroutine->ammarkpos = NULL;
118  amroutine->amrestrpos = NULL;
119  amroutine->amestimateparallelscan = NULL;
120  amroutine->aminitparallelscan = NULL;
121  amroutine->amparallelrescan = NULL;
122 
123  PG_RETURN_POINTER(amroutine);
124 }
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:758
ambeginscan_function ambeginscan
Definition: amapi.h:208
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
ambulkdelete_function ambulkdelete
Definition: amapi.h:201
bool amcanmulticol
Definition: amapi.h:179
uint16 amsupport
Definition: amapi.h:169
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:644
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: brin.c:139
amgettuple_function amgettuple
Definition: amapi.h:210
bool amcanorderbyop
Definition: amapi.h:173
amproperty_function amproperty
Definition: amapi.h:206
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:38
amparallelrescan_function amparallelrescan
Definition: amapi.h:219
bool amstorage
Definition: amapi.h:187
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:352
bool ampredlocks
Definition: amapi.h:191
aminsert_function aminsert
Definition: amapi.h:200
Oid amkeytype
Definition: amapi.h:195
bool amoptionalkey
Definition: amapi.h:181
void brinendscan(IndexScanDesc scan)
Definition: brin.c:565
amvalidate_function amvalidate
Definition: amapi.h:207
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:803
amgetbitmap_function amgetbitmap
Definition: amapi.h:211
ambuild_function ambuild
Definition: amapi.h:198
amoptions_function amoptions
Definition: amapi.h:205
amcostestimate_function amcostestimate
Definition: amapi.h:204
bool amcanunique
Definition: amapi.h:177
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:202
amendscan_function amendscan
Definition: amapi.h:212
bool amcanbackward
Definition: amapi.h:175
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:545
amrescan_function amrescan
Definition: amapi.h:209
bool amcanparallel
Definition: amapi.h:193
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:323
bool amsearchnulls
Definition: amapi.h:185
bool amclusterable
Definition: amapi.h:189
bool amsearcharray
Definition: amapi.h:183
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7711
ammarkpos_function ammarkpos
Definition: amapi.h:213
bool amcanorder
Definition: amapi.h:171
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:217
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:74
uint16 amstrategies
Definition: amapi.h:167
ambuildempty_function ambuildempty
Definition: amapi.h:199
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:773
void brinbuildempty(Relation index)
Definition: brin.c:728
amcanreturn_function amcanreturn
Definition: amapi.h:203
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:218
amrestrpos_function amrestrpos
Definition: amapi.h:214
bool brininsert ( Relation  idxRel,
Datum values,
bool nulls,
ItemPointer  heaptid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
IndexInfo indexInfo 
)

Definition at line 139 of file brin.c.

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

Referenced by brinhandler().

143 {
144  BlockNumber pagesPerRange;
145  BlockNumber origHeapBlk;
146  BlockNumber heapBlk;
147  BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache;
148  BrinRevmap *revmap;
150  MemoryContext tupcxt = NULL;
152  bool autosummarize = BrinGetAutoSummarize(idxRel);
153 
154  revmap = brinRevmapInitialize(idxRel, &pagesPerRange, NULL);
155 
156  /*
157  * origHeapBlk is the block number where the insertion occurred. heapBlk
158  * is the first block in the corresponding page range.
159  */
160  origHeapBlk = ItemPointerGetBlockNumber(heaptid);
161  heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
162 
163  for (;;)
164  {
165  bool need_insert = false;
166  OffsetNumber off;
167  BrinTuple *brtup;
168  BrinMemTuple *dtup;
169  int keyno;
170 
172 
173  /*
174  * If auto-summarization is enabled and we just inserted the first
175  * tuple into the first block of a new non-first page range, request a
176  * summarization run of the previous range.
177  */
178  if (autosummarize &&
179  heapBlk > 0 &&
180  heapBlk == origHeapBlk &&
182  {
183  BlockNumber lastPageRange = heapBlk - 1;
184  BrinTuple *lastPageTuple;
185 
186  lastPageTuple =
187  brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
189  if (!lastPageTuple)
191  RelationGetRelid(idxRel),
192  lastPageRange);
193  brin_free_tuple(lastPageTuple);
194  }
195 
196  brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
198 
199  /* if range is unsummarized, there's nothing to do */
200  if (!brtup)
201  break;
202 
203  /* First time through in this statement? */
204  if (bdesc == NULL)
205  {
206  MemoryContextSwitchTo(indexInfo->ii_Context);
207  bdesc = brin_build_desc(idxRel);
208  indexInfo->ii_AmCache = (void *) bdesc;
209  MemoryContextSwitchTo(oldcxt);
210  }
211  /* First time through in this brininsert call? */
212  if (tupcxt == NULL)
213  {
215  "brininsert cxt",
217  MemoryContextSwitchTo(tupcxt);
218  }
219 
220  dtup = brin_deform_tuple(bdesc, brtup, NULL);
221 
222  /*
223  * Compare the key values of the new tuple to the stored index values;
224  * our deformed tuple will get updated if the new tuple doesn't fit
225  * the original range (note this means we can't break out of the loop
226  * early). Make a note of whether this happens, so that we know to
227  * insert the modified tuple later.
228  */
229  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
230  {
231  Datum result;
232  BrinValues *bval;
233  FmgrInfo *addValue;
234 
235  bval = &dtup->bt_columns[keyno];
236  addValue = index_getprocinfo(idxRel, keyno + 1,
238  result = FunctionCall4Coll(addValue,
239  idxRel->rd_indcollation[keyno],
240  PointerGetDatum(bdesc),
241  PointerGetDatum(bval),
242  values[keyno],
243  nulls[keyno]);
244  /* if that returned true, we need to insert the updated tuple */
245  need_insert |= DatumGetBool(result);
246  }
247 
248  if (!need_insert)
249  {
250  /*
251  * The tuple is consistent with the new values, so there's nothing
252  * to do.
253  */
255  }
256  else
257  {
258  Page page = BufferGetPage(buf);
259  ItemId lp = PageGetItemId(page, off);
260  Size origsz;
261  BrinTuple *origtup;
262  Size newsz;
263  BrinTuple *newtup;
264  bool samepage;
265 
266  /*
267  * Make a copy of the old tuple, so that we can compare it after
268  * re-acquiring the lock.
269  */
270  origsz = ItemIdGetLength(lp);
271  origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
272 
273  /*
274  * Before releasing the lock, check if we can attempt a same-page
275  * update. Another process could insert a tuple concurrently in
276  * the same page though, so downstream we must be prepared to cope
277  * if this turns out to not be possible after all.
278  */
279  newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
280  samepage = brin_can_do_samepage_update(buf, origsz, newsz);
282 
283  /*
284  * Try to update the tuple. If this doesn't work for whatever
285  * reason, we need to restart from the top; the revmap might be
286  * pointing at a different tuple for this block now, so we need to
287  * recompute to ensure both our new heap tuple and the other
288  * inserter's are covered by the combined tuple. It might be that
289  * we don't need to update at all.
290  */
291  if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
292  buf, off, origtup, origsz, newtup, newsz,
293  samepage))
294  {
295  /* no luck; start over */
297  continue;
298  }
299  }
300 
301  /* success! */
302  break;
303  }
304 
305  brinRevmapTerminate(revmap);
306  if (BufferIsValid(buf))
308  MemoryContextSwitchTo(oldcxt);
309  if (tupcxt != NULL)
310  MemoryContextDelete(tupcxt);
311 
312  return false;
313 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
MemoryContext ii_Context
Definition: execnodes.h:151
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:321
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:194
#define PointerGetDatum(X)
Definition: postgres.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define InvalidBuffer
Definition: buf.h:25
return result
Definition: formatting.c:1618
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1094
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
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:55
int natts
Definition: tupdesc.h:73
uint16 OffsetNumber
Definition: off.h:24
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:422
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:68
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
Oid * rd_indcollation
Definition: rel.h:193
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
static char * buf
Definition: pg_test_fsync.c:66
#define FirstOffsetNumber
Definition: off.h:27
#define DatumGetBool(X)
Definition: postgres.h:399
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:323
void * ii_AmCache
Definition: execnodes.h:150
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
uintptr_t Datum
Definition: postgres.h:372
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:308
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:995
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:94
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define BrinGetAutoSummarize(relation)
Definition: brin.h:44
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:89
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
void AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3230
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:75
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:417
Pointer Page
Definition: bufpage.h:74
bytea* brinoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 803 of file brin.c.

References allocateReloptStruct(), fillRelOptions(), lengthof, NULL, offsetof, options, parseRelOptions(), pfree(), RELOPT_KIND_BRIN, RELOPT_TYPE_BOOL, and RELOPT_TYPE_INT.

Referenced by brinhandler().

804 {
806  BrinOptions *rdopts;
807  int numoptions;
808  static const relopt_parse_elt tab[] = {
809  {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
810  {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
811  };
812 
813  options = parseRelOptions(reloptions, validate, RELOPT_KIND_BRIN,
814  &numoptions);
815 
816  /* if none set, we're done */
817  if (numoptions == 0)
818  return NULL;
819 
820  rdopts = allocateReloptStruct(sizeof(BrinOptions), options, numoptions);
821 
822  fillRelOptions((void *) rdopts, sizeof(BrinOptions), options, numoptions,
823  validate, tab, lengthof(tab));
824 
825  pfree(options);
826 
827  return (bytea *) rdopts;
828 }
#define lengthof(array)
Definition: c.h:562
void pfree(void *pointer)
Definition: mcxt.c:950
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1215
static char ** options
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int numelems)
Definition: reloptions.c:1239
#define NULL
Definition: c.h:229
Definition: c.h:439
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:1020
#define offsetof(type, field)
Definition: c.h:555
void brinrescan ( IndexScanDesc  scan,
ScanKey  scankey,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 545 of file brin.c.

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

Referenced by brinhandler().

547 {
548  /*
549  * Other index AMs preprocess the scan keys at this point, or sometime
550  * early during the scan; this lets them optimize by removing redundant
551  * keys, or doing early returns when they are impossible to satisfy; see
552  * _bt_preprocess_keys for an example. Something like that could be added
553  * here someday, too.
554  */
555 
556  if (scankey && scan->numberOfKeys > 0)
557  memmove(scan->keyData, scankey,
558  scan->numberOfKeys * sizeof(ScanKeyData));
559 }
#define memmove(d, s, c)
Definition: c.h:1058
ScanKey keyData
Definition: relscan.h:93
static void brinsummarize ( Relation  index,
Relation  heapRel,
BlockNumber  pageRange,
double *  numSummarized,
double *  numExisting 
)
static

Definition at line 1239 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(), NULL, pfree(), RelationGetNumberOfBlocks, ReleaseBuffer(), summarize_range(), and terminate_brin_buildstate().

Referenced by brin_summarize_range(), and brinvacuumcleanup().

1241 {
1242  BrinRevmap *revmap;
1244  IndexInfo *indexInfo = NULL;
1245  BlockNumber heapNumBlocks;
1246  BlockNumber heapBlk;
1247  BlockNumber pagesPerRange;
1248  Buffer buf;
1249  BlockNumber startBlk;
1250  BlockNumber endBlk;
1251 
1252  /* determine range of pages to process; nothing to do for an empty table */
1253  heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1254  if (heapNumBlocks == 0)
1255  return;
1256 
1257  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
1258 
1259  if (pageRange == BRIN_ALL_BLOCKRANGES)
1260  {
1261  startBlk = 0;
1262  endBlk = heapNumBlocks;
1263  }
1264  else
1265  {
1266  startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1267  /* Nothing to do if start point is beyond end of table */
1268  if (startBlk > heapNumBlocks)
1269  {
1270  brinRevmapTerminate(revmap);
1271  return;
1272  }
1273  endBlk = startBlk + pagesPerRange;
1274  if (endBlk > heapNumBlocks)
1275  endBlk = heapNumBlocks;
1276  }
1277 
1278  /*
1279  * Scan the revmap to find unsummarized items.
1280  */
1281  buf = InvalidBuffer;
1282  for (heapBlk = startBlk; heapBlk < endBlk; heapBlk += pagesPerRange)
1283  {
1284  BrinTuple *tup;
1285  OffsetNumber off;
1286 
1288 
1289  tup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off, NULL,
1291  if (tup == NULL)
1292  {
1293  /* no revmap entry for this heap range. Summarize it. */
1294  if (state == NULL)
1295  {
1296  /* first time through */
1297  Assert(!indexInfo);
1298  state = initialize_brin_buildstate(index, revmap,
1299  pagesPerRange);
1300  indexInfo = BuildIndexInfo(index);
1301  }
1302  summarize_range(indexInfo, state, heapRel, heapBlk, heapNumBlocks);
1303 
1304  /* and re-initialize state for the next range */
1306 
1307  if (numSummarized)
1308  *numSummarized += 1.0;
1309  }
1310  else
1311  {
1312  if (numExisting)
1313  *numExisting += 1.0;
1315  }
1316  }
1317 
1318  if (BufferIsValid(buf))
1319  ReleaseBuffer(buf);
1320 
1321  /* free resources */
1322  brinRevmapTerminate(revmap);
1323  if (state)
1324  {
1326  pfree(indexInfo);
1327  }
1328 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
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:194
static void summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
Definition: brin.c:1140
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:1640
uint16 OffsetNumber
Definition: off.h:24
void pfree(void *pointer)
Definition: mcxt.c:950
static char * buf
Definition: pg_test_fsync.c:66
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Definition: regguts.h:298
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:64
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
BrinMemTuple * bs_dtuple
Definition: brin.c:51
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1083
BrinDesc * bs_bdesc
Definition: brin.c:50
int Buffer
Definition: buf.h:23
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:384
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1108
IndexBulkDeleteResult* brinvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 773 of file brin.c.

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

Referenced by brinhandler().

774 {
775  Relation heapRel;
776 
777  /* No-op in ANALYZE ONLY mode */
778  if (info->analyze_only)
779  return stats;
780 
781  if (!stats)
783  stats->num_pages = RelationGetNumberOfBlocks(info->index);
784  /* rest of stats is initialized by zeroing */
785 
786  heapRel = heap_open(IndexGetRelation(RelationGetRelid(info->index), false),
788 
789  brin_vacuum_scan(info->index, info->strategy);
790 
791  brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES,
792  &stats->num_index_tuples, &stats->num_index_tuples);
793 
794  heap_close(heapRel, AccessShareLock);
795 
796  return stats;
797 }
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3275
bool analyze_only
Definition: genam.h:47
BufferAccessStrategy strategy
Definition: genam.h:51
#define AccessShareLock
Definition: lockdefs.h:36
Relation index
Definition: genam.h:46
#define heap_close(r, l)
Definition: heapam.h:97
BlockNumber num_pages
Definition: genam.h:73
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:1397
void * palloc0(Size size)
Definition: mcxt.c:878
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, double *numSummarized, double *numExisting)
Definition: brin.c:1239
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:64
double num_index_tuples
Definition: genam.h:76
#define RelationGetRelid(relation)
Definition: rel.h:417
static void form_and_insert_tuple ( BrinBuildState state)
static

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

1336 {
1337  BrinTuple *tup;
1338  Size size;
1339 
1340  tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1341  state->bs_dtuple, &size);
1342  brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1343  &state->bs_currentInsertBuf, state->bs_currRangeStart,
1344  tup, size);
1345  state->bs_numtuples++;
1346 
1347  pfree(tup);
1348 }
Buffer bs_currentInsertBuf
Definition: brin.c:46
Relation bs_irel
Definition: brin.c:44
BlockNumber bs_currRangeStart
Definition: brin.c:48
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:342
BlockNumber bs_pagesPerRange
Definition: brin.c:47
void pfree(void *pointer)
Definition: mcxt.c:950
BrinRevmap * bs_rmAccess
Definition: brin.c:49
size_t Size
Definition: c.h:356
int bs_numtuples
Definition: brin.c:45
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:89
BrinMemTuple * bs_dtuple
Definition: brin.c:51
BrinDesc * bs_bdesc
Definition: brin.c:50
static BrinBuildState * initialize_brin_buildstate ( Relation  idxRel,
BrinRevmap revmap,
BlockNumber  pagesPerRange 
)
static

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

1085 {
1087 
1088  state = palloc(sizeof(BrinBuildState));
1089 
1090  state->bs_irel = idxRel;
1091  state->bs_numtuples = 0;
1093  state->bs_pagesPerRange = pagesPerRange;
1094  state->bs_currRangeStart = 0;
1095  state->bs_rmAccess = revmap;
1096  state->bs_bdesc = brin_build_desc(idxRel);
1097  state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1098 
1100 
1101  return state;
1102 }
Buffer bs_currentInsertBuf
Definition: brin.c:46
#define InvalidBuffer
Definition: buf.h:25
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:357
Relation bs_irel
Definition: brin.c:44
BlockNumber bs_currRangeStart
Definition: brin.c:48
BlockNumber bs_pagesPerRange
Definition: brin.c:47
BrinRevmap * bs_rmAccess
Definition: brin.c:49
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:995
Definition: regguts.h:298
int bs_numtuples
Definition: brin.c:45
void * palloc(Size size)
Definition: mcxt.c:849
BrinMemTuple * bs_dtuple
Definition: brin.c:51
BrinDesc * bs_bdesc
Definition: brin.c:50
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:384
static void summarize_range ( IndexInfo indexInfo,
BrinBuildState state,
Relation  heapRel,
BlockNumber  heapBlk,
BlockNumber  heapNumBlks 
)
static

Definition at line 1140 of file brin.c.

References 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_dtuple, BrinBuildState::bs_irel, BrinBuildState::bs_pagesPerRange, BrinBuildState::bs_rmAccess, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, CHECK_FOR_INTERRUPTS, elog, ERROR, IndexBuildHeapRangeScan(), InvalidBuffer, LockBuffer(), NULL, ReleaseBuffer(), and union_tuples().

Referenced by brinsummarize().

1142 {
1143  Buffer phbuf;
1144  BrinTuple *phtup;
1145  Size phsz;
1146  OffsetNumber offset;
1147  BlockNumber scanNumBlks;
1148 
1149  /*
1150  * Insert the placeholder tuple
1151  */
1152  phbuf = InvalidBuffer;
1153  phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1154  offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1155  state->bs_rmAccess, &phbuf,
1156  heapBlk, phtup, phsz);
1157 
1158  /*
1159  * Execute the partial heap scan covering the heap blocks in the specified
1160  * page range, summarizing the heap tuples in it. This scan stops just
1161  * short of brinbuildCallback creating the new index entry.
1162  *
1163  * Note that it is critical we use the "any visible" mode of
1164  * IndexBuildHeapRangeScan here: otherwise, we would miss tuples inserted
1165  * by transactions that are still in progress, among other corner cases.
1166  */
1167  state->bs_currRangeStart = heapBlk;
1168  scanNumBlks = heapBlk + state->bs_pagesPerRange <= heapNumBlks ?
1169  state->bs_pagesPerRange : heapNumBlks - heapBlk;
1170  IndexBuildHeapRangeScan(heapRel, state->bs_irel, indexInfo, false, true,
1171  heapBlk, scanNumBlks,
1172  brinbuildCallback, (void *) state);
1173 
1174  /*
1175  * Now we update the values obtained by the scan with the placeholder
1176  * tuple. We do this in a loop which only terminates if we're able to
1177  * update the placeholder tuple successfully; if we are not, this means
1178  * somebody else modified the placeholder tuple after we read it.
1179  */
1180  for (;;)
1181  {
1182  BrinTuple *newtup;
1183  Size newsize;
1184  bool didupdate;
1185  bool samepage;
1186 
1188 
1189  /*
1190  * Update the summary tuple and try to update.
1191  */
1192  newtup = brin_form_tuple(state->bs_bdesc,
1193  heapBlk, state->bs_dtuple, &newsize);
1194  samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1195  didupdate =
1196  brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1197  state->bs_rmAccess, heapBlk, phbuf, offset,
1198  phtup, phsz, newtup, newsize, samepage);
1199  brin_free_tuple(phtup);
1200  brin_free_tuple(newtup);
1201 
1202  /* If the update succeeded, we're done. */
1203  if (didupdate)
1204  break;
1205 
1206  /*
1207  * If the update didn't work, it might be because somebody updated the
1208  * placeholder tuple concurrently. Extract the new version, union it
1209  * with the values we have from the scan, and start over. (There are
1210  * other reasons for the update to fail, but it's simple to treat them
1211  * the same.)
1212  */
1213  phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1214  &offset, &phsz, BUFFER_LOCK_SHARE,
1215  NULL);
1216  /* the placeholder tuple must exist */
1217  if (phtup == NULL)
1218  elog(ERROR, "missing placeholder tuple");
1219  phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1221 
1222  /* merge it into the tuple from the heap scan */
1223  union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1224  }
1225 
1226  ReleaseBuffer(phbuf);
1227 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:321
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:1355
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:194
#define InvalidBuffer
Definition: buf.h:25
static void brinbuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:582
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
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:55
Relation bs_irel
Definition: brin.c:44
BlockNumber bs_currRangeStart
Definition: brin.c:48
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:342
BlockNumber bs_pagesPerRange
Definition: brin.c:47
#define ERROR
Definition: elog.h:43
BrinRevmap * bs_rmAccess
Definition: brin.c:49
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:323
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:308
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:89
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
BrinMemTuple * bs_dtuple
Definition: brin.c:51
BrinDesc * bs_bdesc
Definition: brin.c:50
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:263
double IndexBuildHeapRangeScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, bool anyvisible, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2194
static void terminate_brin_buildstate ( BrinBuildState state)
static

Definition at line 1108 of file brin.c.

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

Referenced by brinbuild(), and brinsummarize().

1109 {
1110  /* release the last index buffer used */
1111  if (!BufferIsInvalid(state->bs_currentInsertBuf))
1112  {
1113  Page page;
1114 
1115  page = BufferGetPage(state->bs_currentInsertBuf);
1118  PageGetFreeSpace(page));
1120  }
1121 
1122  brin_free_desc(state->bs_bdesc);
1123  pfree(state->bs_dtuple);
1124  pfree(state);
1125 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1050
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
Buffer bs_currentInsertBuf
Definition: brin.c:46
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
Relation bs_irel
Definition: brin.c:44
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
void pfree(void *pointer)
Definition: mcxt.c:950
#define BufferIsInvalid(buffer)
Definition: buf.h:31
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
BrinMemTuple * bs_dtuple
Definition: brin.c:51
BrinDesc * bs_bdesc
Definition: brin.c:50
Pointer Page
Definition: bufpage.h:74
static void union_tuples ( BrinDesc bdesc,
BrinMemTuple a,
BrinTuple b 
)
static

Definition at line 1355 of file brin.c.

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

Referenced by summarize_range().

1356 {
1357  int keyno;
1358  BrinMemTuple *db;
1359  MemoryContext cxt;
1360  MemoryContext oldcxt;
1361 
1362  /* Use our own memory context to avoid retail pfree */
1364  "brin union",
1366  oldcxt = MemoryContextSwitchTo(cxt);
1367  db = brin_deform_tuple(bdesc, b, NULL);
1368  MemoryContextSwitchTo(oldcxt);
1369 
1370  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1371  {
1372  FmgrInfo *unionFn;
1373  BrinValues *col_a = &a->bt_columns[keyno];
1374  BrinValues *col_b = &db->bt_columns[keyno];
1375 
1376  unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
1378  FunctionCall3Coll(unionFn,
1379  bdesc->bd_index->rd_indcollation[keyno],
1380  PointerGetDatum(bdesc),
1381  PointerGetDatum(col_a),
1382  PointerGetDatum(col_b));
1383  }
1384 
1385  MemoryContextDelete(cxt);
1386 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
#define PointerGetDatum(X)
Definition: postgres.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int natts
Definition: tupdesc.h:73
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:422
Oid * rd_indcollation
Definition: rel.h:193
Relation bd_index
Definition: brin_internal.h:47
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1069
#define NULL
Definition: c.h:229
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:70