PostgreSQL Source Code  git master
ginutil.c File Reference
#include "postgres.h"
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/reloptions.h"
#include "access/xloginsert.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/typcache.h"
Include dependency graph for ginutil.c:

Go to the source code of this file.

Data Structures

struct  keyEntryData
 
struct  cmpEntriesArg
 

Functions

Datum ginhandler (PG_FUNCTION_ARGS)
 
void initGinState (GinState *state, Relation index)
 
OffsetNumber gintuple_get_attrnum (GinState *ginstate, IndexTuple tuple)
 
Datum gintuple_get_key (GinState *ginstate, IndexTuple tuple, GinNullCategory *category)
 
Buffer GinNewBuffer (Relation index)
 
void GinInitPage (Page page, uint32 f, Size pageSize)
 
void GinInitBuffer (Buffer b, uint32 f)
 
void GinInitMetabuffer (Buffer b)
 
int ginCompareEntries (GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
 
int ginCompareAttEntries (GinState *ginstate, OffsetNumber attnuma, Datum a, GinNullCategory categorya, OffsetNumber attnumb, Datum b, GinNullCategory categoryb)
 
static int cmpEntries (const void *a, const void *b, void *arg)
 
DatumginExtractEntries (GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
 
byteaginoptions (Datum reloptions, bool validate)
 
void ginGetStats (Relation index, GinStatsData *stats)
 
void ginUpdateStats (Relation index, const GinStatsData *stats, bool is_build)
 

Function Documentation

◆ cmpEntries()

static int cmpEntries ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 450 of file ginutil.c.

451 {
452  const keyEntryData *aa = (const keyEntryData *) a;
453  const keyEntryData *bb = (const keyEntryData *) b;
455  int res;
456 
457  if (aa->isnull)
458  {
459  if (bb->isnull)
460  res = 0; /* NULL "=" NULL */
461  else
462  res = 1; /* NULL ">" not-NULL */
463  }
464  else if (bb->isnull)
465  res = -1; /* not-NULL "<" NULL */
466  else
467  res = DatumGetInt32(FunctionCall2Coll(data->cmpDatumFunc,
468  data->collation,
469  aa->datum, bb->datum));
470 
471  /*
472  * Detect if we have any duplicates. If there are equal keys, qsort must
473  * compare them at some point, else it wouldn't know whether one should go
474  * before or after the other.
475  */
476  if (res == 0)
477  data->haveDups = true;
478 
479  return res;
480 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1134
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * arg
const void * data
#define DatumGetInt32(X)
Definition: postgres.h:516
bool isnull
Definition: ginutil.c:439
Datum datum
Definition: ginutil.c:438

References a, arg, b, data, keyEntryData::datum, DatumGetInt32, FunctionCall2Coll(), keyEntryData::isnull, and res.

Referenced by ginExtractEntries().

◆ ginCompareAttEntries()

int ginCompareAttEntries ( GinState ginstate,
OffsetNumber  attnuma,
Datum  a,
GinNullCategory  categorya,
OffsetNumber  attnumb,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 417 of file ginutil.c.

420 {
421  /* attribute number is the first sort key */
422  if (attnuma != attnumb)
423  return (attnuma < attnumb) ? -1 : 1;
424 
425  return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
426 }
int ginCompareEntries(GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
Definition: ginutil.c:395

References a, b, and ginCompareEntries().

Referenced by cmpEntryAccumulator(), entryIsMoveRight(), entryLocateEntry(), and entryLocateLeafEntry().

◆ ginCompareEntries()

int ginCompareEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  a,
GinNullCategory  categorya,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 395 of file ginutil.c.

398 {
399  /* if not of same null category, sort by that first */
400  if (categorya != categoryb)
401  return (categorya < categoryb) ? -1 : 1;
402 
403  /* all null items in same category are equal */
404  if (categorya != GIN_CAT_NORM_KEY)
405  return 0;
406 
407  /* both not null, so safe to call the compareFn */
408  return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
409  ginstate->supportCollation[attnum - 1],
410  a, b));
411 }
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:208
int16 attnum
Definition: pg_attribute.h:83
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:87
FmgrInfo compareFn[INDEX_MAX_KEYS]
Definition: gin_private.h:78

References a, attnum, b, GinState::compareFn, DatumGetInt32, FunctionCall2Coll(), GIN_CAT_NORM_KEY, and GinState::supportCollation.

Referenced by collectMatchBitmap(), collectMatchesForHeapRow(), ginCompareAttEntries(), and ginFillScanEntry().

◆ ginExtractEntries()

Datum* ginExtractEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
int32 nentries,
GinNullCategory **  categories 
)

Definition at line 490 of file ginutil.c.

493 {
494  Datum *entries;
495  bool *nullFlags;
496  int32 i;
497 
498  /*
499  * We don't call the extractValueFn on a null item. Instead generate a
500  * placeholder.
501  */
502  if (isNull)
503  {
504  *nentries = 1;
505  entries = (Datum *) palloc(sizeof(Datum));
506  entries[0] = (Datum) 0;
507  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
508  (*categories)[0] = GIN_CAT_NULL_ITEM;
509  return entries;
510  }
511 
512  /* OK, call the opclass's extractValueFn */
513  nullFlags = NULL; /* in case extractValue doesn't set it */
514  entries = (Datum *)
516  ginstate->supportCollation[attnum - 1],
517  value,
518  PointerGetDatum(nentries),
519  PointerGetDatum(&nullFlags)));
520 
521  /*
522  * Generate a placeholder if the item contained no keys.
523  */
524  if (entries == NULL || *nentries <= 0)
525  {
526  *nentries = 1;
527  entries = (Datum *) palloc(sizeof(Datum));
528  entries[0] = (Datum) 0;
529  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
530  (*categories)[0] = GIN_CAT_EMPTY_ITEM;
531  return entries;
532  }
533 
534  /*
535  * If the extractValueFn didn't create a nullFlags array, create one,
536  * assuming that everything's non-null.
537  */
538  if (nullFlags == NULL)
539  nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
540 
541  /*
542  * If there's more than one key, sort and unique-ify.
543  *
544  * XXX Using qsort here is notationally painful, and the overhead is
545  * pretty bad too. For small numbers of keys it'd likely be better to use
546  * a simple insertion sort.
547  */
548  if (*nentries > 1)
549  {
550  keyEntryData *keydata;
552 
553  keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
554  for (i = 0; i < *nentries; i++)
555  {
556  keydata[i].datum = entries[i];
557  keydata[i].isnull = nullFlags[i];
558  }
559 
560  arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
561  arg.collation = ginstate->supportCollation[attnum - 1];
562  arg.haveDups = false;
563  qsort_arg(keydata, *nentries, sizeof(keyEntryData),
564  cmpEntries, (void *) &arg);
565 
566  if (arg.haveDups)
567  {
568  /* there are duplicates, must get rid of 'em */
569  int32 j;
570 
571  entries[0] = keydata[0].datum;
572  nullFlags[0] = keydata[0].isnull;
573  j = 1;
574  for (i = 1; i < *nentries; i++)
575  {
576  if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
577  {
578  entries[j] = keydata[i].datum;
579  nullFlags[j] = keydata[i].isnull;
580  j++;
581  }
582  }
583  *nentries = j;
584  }
585  else
586  {
587  /* easy, no duplicates */
588  for (i = 0; i < *nentries; i++)
589  {
590  entries[i] = keydata[i].datum;
591  nullFlags[i] = keydata[i].isnull;
592  }
593  }
594 
595  pfree(keydata);
596  }
597 
598  /*
599  * Create GinNullCategory representation from nullFlags.
600  */
601  *categories = (GinNullCategory *) palloc0(*nentries * sizeof(GinNullCategory));
602  for (i = 0; i < *nentries; i++)
603  (*categories)[i] = (nullFlags[i] ? GIN_CAT_NULL_KEY : GIN_CAT_NORM_KEY);
604 
605  return entries;
606 }
signed int int32
Definition: c.h:429
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1156
#define GIN_CAT_EMPTY_ITEM
Definition: ginblock.h:210
signed char GinNullCategory
Definition: ginblock.h:206
#define GIN_CAT_NULL_ITEM
Definition: ginblock.h:211
#define GIN_CAT_NULL_KEY
Definition: ginblock.h:209
static int cmpEntries(const void *a, const void *b, void *arg)
Definition: ginutil.c:450
static struct @151 value
int j
Definition: isn.c:74
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1175
void * palloc0(Size size)
Definition: mcxt.c:1099
void * palloc(Size size)
Definition: mcxt.c:1068
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
#define PointerGetDatum(X)
Definition: postgres.h:600
FmgrInfo extractValueFn[INDEX_MAX_KEYS]
Definition: gin_private.h:79

References arg, attnum, cmpEntries(), GinState::compareFn, keyEntryData::datum, DatumGetPointer, GinState::extractValueFn, FunctionCall3Coll(), GIN_CAT_EMPTY_ITEM, GIN_CAT_NORM_KEY, GIN_CAT_NULL_ITEM, GIN_CAT_NULL_KEY, i, keyEntryData::isnull, j, palloc(), palloc0(), pfree(), PointerGetDatum, qsort_arg(), GinState::supportCollation, and value.

Referenced by ginHeapTupleBulkInsert(), ginHeapTupleFastCollect(), and ginHeapTupleInsert().

◆ ginGetStats()

void ginGetStats ( Relation  index,
GinStatsData stats 
)

Definition at line 630 of file ginutil.c.

631 {
632  Buffer metabuffer;
633  Page metapage;
634  GinMetaPageData *metadata;
635 
636  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
637  LockBuffer(metabuffer, GIN_SHARE);
638  metapage = BufferGetPage(metabuffer);
639  metadata = GinPageGetMeta(metapage);
640 
641  stats->nPendingPages = metadata->nPendingPages;
642  stats->nTotalPages = metadata->nTotalPages;
643  stats->nEntryPages = metadata->nEntryPages;
644  stats->nDataPages = metadata->nDataPages;
645  stats->nEntries = metadata->nEntries;
646  stats->ginVersion = metadata->ginVersion;
647 
648  UnlockReleaseBuffer(metabuffer);
649 }
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3938
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:702
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Pointer Page
Definition: bufpage.h:78
#define GIN_SHARE
Definition: gin_private.h:49
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:51
#define GinPageGetMeta(p)
Definition: ginblock.h:104
int64 nEntries
Definition: ginblock.h:82
int32 ginVersion
Definition: ginblock.h:99
BlockNumber nEntryPages
Definition: ginblock.h:80
BlockNumber nPendingPages
Definition: ginblock.h:73
BlockNumber nTotalPages
Definition: ginblock.h:79
BlockNumber nDataPages
Definition: ginblock.h:81
BlockNumber nDataPages
Definition: gin.h:47
BlockNumber nPendingPages
Definition: gin.h:44
BlockNumber nEntryPages
Definition: gin.h:46
int64 nEntries
Definition: gin.h:48
BlockNumber nTotalPages
Definition: gin.h:45
int32 ginVersion
Definition: gin.h:49
Definition: type.h:90

References BufferGetPage, GIN_METAPAGE_BLKNO, GIN_SHARE, GinPageGetMeta, GinStatsData::ginVersion, GinMetaPageData::ginVersion, LockBuffer(), GinStatsData::nDataPages, GinMetaPageData::nDataPages, GinStatsData::nEntries, GinMetaPageData::nEntries, GinStatsData::nEntryPages, GinMetaPageData::nEntryPages, GinStatsData::nPendingPages, GinMetaPageData::nPendingPages, GinStatsData::nTotalPages, GinMetaPageData::nTotalPages, ReadBuffer(), and UnlockReleaseBuffer().

Referenced by gincostestimate().

◆ ginhandler()

Datum ginhandler ( PG_FUNCTION_ARGS  )

Definition at line 38 of file ginutil.c.

39 {
41 
42  amroutine->amstrategies = 0;
43  amroutine->amsupport = GINNProcs;
44  amroutine->amoptsprocnum = GIN_OPTIONS_PROC;
45  amroutine->amcanorder = false;
46  amroutine->amcanorderbyop = false;
47  amroutine->amcanbackward = false;
48  amroutine->amcanunique = false;
49  amroutine->amcanmulticol = true;
50  amroutine->amoptionalkey = true;
51  amroutine->amsearcharray = false;
52  amroutine->amsearchnulls = false;
53  amroutine->amstorage = true;
54  amroutine->amclusterable = false;
55  amroutine->ampredlocks = true;
56  amroutine->amcanparallel = false;
57  amroutine->amcaninclude = false;
58  amroutine->amusemaintenanceworkmem = true;
59  amroutine->amhotblocking = true;
60  amroutine->amparallelvacuumoptions =
62  amroutine->amkeytype = InvalidOid;
63 
64  amroutine->ambuild = ginbuild;
65  amroutine->ambuildempty = ginbuildempty;
66  amroutine->aminsert = gininsert;
67  amroutine->ambulkdelete = ginbulkdelete;
68  amroutine->amvacuumcleanup = ginvacuumcleanup;
69  amroutine->amcanreturn = NULL;
70  amroutine->amcostestimate = gincostestimate;
71  amroutine->amoptions = ginoptions;
72  amroutine->amproperty = NULL;
73  amroutine->ambuildphasename = NULL;
74  amroutine->amvalidate = ginvalidate;
75  amroutine->amadjustmembers = ginadjustmembers;
76  amroutine->ambeginscan = ginbeginscan;
77  amroutine->amrescan = ginrescan;
78  amroutine->amgettuple = NULL;
79  amroutine->amgetbitmap = gingetbitmap;
80  amroutine->amendscan = ginendscan;
81  amroutine->ammarkpos = NULL;
82  amroutine->amrestrpos = NULL;
83  amroutine->amestimateparallelscan = NULL;
84  amroutine->aminitparallelscan = NULL;
85  amroutine->amparallelrescan = NULL;
86 
87  PG_RETURN_POINTER(amroutine);
88 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define GINNProcs
Definition: gin.h:29
#define GIN_OPTIONS_PROC
Definition: gin.h:28
int64 gingetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: ginget.c:1918
IndexBuildResult * ginbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gininsert.c:320
void ginbuildempty(Relation index)
Definition: gininsert.c:437
bool gininsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: gininsert.c:488
IndexScanDesc ginbeginscan(Relation rel, int nkeys, int norderbys)
Definition: ginscan.c:25
void ginendscan(IndexScanDesc scan)
Definition: ginscan.c:458
void ginrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: ginscan.c:442
bytea * ginoptions(Datum reloptions, bool validate)
Definition: ginutil.c:609
IndexBulkDeleteResult * ginbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: ginvacuum.c:565
IndexBulkDeleteResult * ginvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: ginvacuum.c:688
bool ginvalidate(Oid opclassoid)
Definition: ginvalidate.c:33
void ginadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
Definition: ginvalidate.c:279
#define makeNode(_type_)
Definition: nodes.h:621
#define InvalidOid
Definition: postgres_ext.h:36
void gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7403
ambuildphasename_function ambuildphasename
Definition: amapi.h:270
ambuildempty_function ambuildempty
Definition: amapi.h:262
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:265
bool amclusterable
Definition: amapi.h:238
amoptions_function amoptions
Definition: amapi.h:268
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:282
amrestrpos_function amrestrpos
Definition: amapi.h:279
aminsert_function aminsert
Definition: amapi.h:263
amendscan_function amendscan
Definition: amapi.h:277
uint16 amoptsprocnum
Definition: amapi.h:218
amparallelrescan_function amparallelrescan
Definition: amapi.h:284
Oid amkeytype
Definition: amapi.h:252
bool ampredlocks
Definition: amapi.h:240
uint16 amsupport
Definition: amapi.h:216
amcostestimate_function amcostestimate
Definition: amapi.h:267
bool amcanorderbyop
Definition: amapi.h:222
amadjustmembers_function amadjustmembers
Definition: amapi.h:272
ambuild_function ambuild
Definition: amapi.h:261
bool amstorage
Definition: amapi.h:236
uint16 amstrategies
Definition: amapi.h:214
bool amoptionalkey
Definition: amapi.h:230
amgettuple_function amgettuple
Definition: amapi.h:275
amcanreturn_function amcanreturn
Definition: amapi.h:266
bool amcanunique
Definition: amapi.h:226
amgetbitmap_function amgetbitmap
Definition: amapi.h:276
amproperty_function amproperty
Definition: amapi.h:269
ambulkdelete_function ambulkdelete
Definition: amapi.h:264
bool amsearcharray
Definition: amapi.h:232
amvalidate_function amvalidate
Definition: amapi.h:271
ammarkpos_function ammarkpos
Definition: amapi.h:278
bool amcanmulticol
Definition: amapi.h:228
bool amusemaintenanceworkmem
Definition: amapi.h:246
ambeginscan_function ambeginscan
Definition: amapi.h:273
bool amcanparallel
Definition: amapi.h:242
amrescan_function amrescan
Definition: amapi.h:274
bool amcanorder
Definition: amapi.h:220
bool amhotblocking
Definition: amapi.h:248
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:283
uint8 amparallelvacuumoptions
Definition: amapi.h:250
bool amcanbackward
Definition: amapi.h:224
bool amcaninclude
Definition: amapi.h:244
bool amsearchnulls
Definition: amapi.h:234
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:62
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:47

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::amhotblocking, 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, GIN_OPTIONS_PROC, ginadjustmembers(), ginbeginscan(), ginbuild(), ginbuildempty(), ginbulkdelete(), gincostestimate(), ginendscan(), gingetbitmap(), gininsert(), GINNProcs, ginoptions(), ginrescan(), ginvacuumcleanup(), ginvalidate(), InvalidOid, makeNode, PG_RETURN_POINTER, VACUUM_OPTION_PARALLEL_BULKDEL, and VACUUM_OPTION_PARALLEL_CLEANUP.

◆ GinInitBuffer()

void GinInitBuffer ( Buffer  b,
uint32  f 
)

Definition at line 357 of file ginutil.c.

358 {
360 }
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:345

References b, BufferGetPage, BufferGetPageSize, and GinInitPage().

Referenced by ginbuild(), ginbuildempty(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoInsertListPage(), and writeListPage().

◆ GinInitMetabuffer()

void GinInitMetabuffer ( Buffer  b)

Definition at line 363 of file ginutil.c.

364 {
365  GinMetaPageData *metadata;
366  Page page = BufferGetPage(b);
367 
369 
370  metadata = GinPageGetMeta(page);
371 
372  metadata->head = metadata->tail = InvalidBlockNumber;
373  metadata->tailFreeSize = 0;
374  metadata->nPendingPages = 0;
375  metadata->nPendingHeapTuples = 0;
376  metadata->nTotalPages = 0;
377  metadata->nEntryPages = 0;
378  metadata->nDataPages = 0;
379  metadata->nEntries = 0;
380  metadata->ginVersion = GIN_CURRENT_VERSION;
381 
382  /*
383  * Set pd_lower just past the end of the metadata. This is essential,
384  * because without doing so, metadata will be lost if xlog.c compresses
385  * the page.
386  */
387  ((PageHeader) page)->pd_lower =
388  ((char *) metadata + sizeof(GinMetaPageData)) - (char *) page;
389 }
#define InvalidBlockNumber
Definition: block.h:33
PageHeaderData * PageHeader
Definition: bufpage.h:166
#define GIN_CURRENT_VERSION
Definition: ginblock.h:102
#define GIN_META
Definition: ginblock.h:44
BlockNumber tail
Definition: ginblock.h:62
uint32 tailFreeSize
Definition: ginblock.h:67
int64 nPendingHeapTuples
Definition: ginblock.h:74
BlockNumber head
Definition: ginblock.h:61

References b, BufferGetPage, BufferGetPageSize, GIN_CURRENT_VERSION, GIN_META, GinInitPage(), GinPageGetMeta, GinMetaPageData::ginVersion, GinMetaPageData::head, InvalidBlockNumber, GinMetaPageData::nDataPages, GinMetaPageData::nEntries, GinMetaPageData::nEntryPages, GinMetaPageData::nPendingHeapTuples, GinMetaPageData::nPendingPages, GinMetaPageData::nTotalPages, GinMetaPageData::tail, and GinMetaPageData::tailFreeSize.

Referenced by ginbuild(), ginbuildempty(), ginRedoDeleteListPages(), and ginRedoUpdateMetapage().

◆ GinInitPage()

void GinInitPage ( Page  page,
uint32  f,
Size  pageSize 
)

Definition at line 345 of file ginutil.c.

346 {
347  GinPageOpaque opaque;
348 
349  PageInit(page, pageSize, sizeof(GinPageOpaqueData));
350 
351  opaque = GinPageGetOpaque(page);
352  opaque->flags = f;
353  opaque->rightlink = InvalidBlockNumber;
354 }
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
#define GinPageGetOpaque(page)
Definition: ginblock.h:110
BlockNumber rightlink
Definition: ginblock.h:32

References GinPageOpaqueData::flags, GinPageGetOpaque, InvalidBlockNumber, PageInit(), and GinPageOpaqueData::rightlink.

Referenced by createPostingTree(), dataPlaceToPageLeafSplit(), dataSplitPageInternal(), entrySplitPage(), GinInitBuffer(), GinInitMetabuffer(), and ginPlaceToPage().

◆ GinNewBuffer()

Buffer GinNewBuffer ( Relation  index)

Definition at line 299 of file ginutil.c.

300 {
301  Buffer buffer;
302  bool needLock;
303 
304  /* First, try to get a page from FSM */
305  for (;;)
306  {
308 
309  if (blkno == InvalidBlockNumber)
310  break;
311 
312  buffer = ReadBuffer(index, blkno);
313 
314  /*
315  * We have to guard against the possibility that someone else already
316  * recycled this page; the buffer may be locked if so.
317  */
318  if (ConditionalLockBuffer(buffer))
319  {
320  if (GinPageIsRecyclable(BufferGetPage(buffer)))
321  return buffer; /* OK to use */
322 
323  LockBuffer(buffer, GIN_UNLOCK);
324  }
325 
326  /* Can't use it, so release buffer and try again */
327  ReleaseBuffer(buffer);
328  }
329 
330  /* Must extend the file */
331  needLock = !RELATION_IS_LOCAL(index);
332  if (needLock)
334 
335  buffer = ReadBuffer(index, P_NEW);
336  LockBuffer(buffer, GIN_EXCLUSIVE);
337 
338  if (needLock)
340 
341  return buffer;
342 }
uint32 BlockNumber
Definition: block.h:31
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4182
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3915
#define P_NEW
Definition: bufmgr.h:91
#define GIN_UNLOCK
Definition: gin_private.h:48
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
bool GinPageIsRecyclable(Page page)
Definition: ginvacuum.c:802
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:431
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:481
#define ExclusiveLock
Definition: lockdefs.h:42
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:633

References BufferGetPage, ConditionalLockBuffer(), ExclusiveLock, GetFreeIndexPage(), GIN_EXCLUSIVE, GIN_UNLOCK, GinPageIsRecyclable(), InvalidBlockNumber, LockBuffer(), LockRelationForExtension(), P_NEW, ReadBuffer(), RELATION_IS_LOCAL, ReleaseBuffer(), and UnlockRelationForExtension().

Referenced by createPostingTree(), ginbuild(), ginPlaceToPage(), and makeSublist().

◆ ginoptions()

bytea* ginoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 609 of file ginutil.c.

610 {
611  static const relopt_parse_elt tab[] = {
612  {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)},
613  {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions,
614  pendingListCleanupSize)}
615  };
616 
617  return (bytea *) build_reloptions(reloptions, validate,
619  sizeof(GinOptions),
620  tab, lengthof(tab));
621 }
#define offsetof(type, field)
Definition: c.h:727
#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:1913
@ RELOPT_KIND_GIN
Definition: reloptions.h:46
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
Definition: c.h:622

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

Referenced by ginhandler().

◆ gintuple_get_attrnum()

OffsetNumber gintuple_get_attrnum ( GinState ginstate,
IndexTuple  tuple 
)

Definition at line 225 of file ginutil.c.

226 {
227  OffsetNumber colN;
228 
229  if (ginstate->oneCol)
230  {
231  /* column number is not stored explicitly */
232  colN = FirstOffsetNumber;
233  }
234  else
235  {
236  Datum res;
237  bool isnull;
238 
239  /*
240  * First attribute is always int16, so we can safely use any tuple
241  * descriptor to obtain first attribute of tuple
242  */
243  res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
244  &isnull);
245  Assert(!isnull);
246 
247  colN = DatumGetUInt16(res);
248  Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
249  }
250 
251  return colN;
252 }
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:99
Assert(fmt[strlen(fmt) - 1] !='\n')
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define DatumGetUInt16(X)
Definition: postgres.h:502
bool oneCol
Definition: gin_private.h:59
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:73

References Assert(), DatumGetUInt16, FirstOffsetNumber, index_getattr, GinState::oneCol, res, and GinState::tupdesc.

Referenced by addItemPointersToLeafTuple(), collectMatchBitmap(), collectMatchesForHeapRow(), entryIsMoveRight(), entryLocateEntry(), entryLocateLeafEntry(), gintuple_get_key(), ginVacuumEntryPage(), matchPartialInPendingList(), and processPendingPage().

◆ gintuple_get_key()

Datum gintuple_get_key ( GinState ginstate,
IndexTuple  tuple,
GinNullCategory category 
)

Definition at line 258 of file ginutil.c.

260 {
261  Datum res;
262  bool isnull;
263 
264  if (ginstate->oneCol)
265  {
266  /*
267  * Single column index doesn't store attribute numbers in tuples
268  */
269  res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
270  &isnull);
271  }
272  else
273  {
274  /*
275  * Since the datum type depends on which index column it's from, we
276  * must be careful to use the right tuple descriptor here.
277  */
278  OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
279 
281  ginstate->tupdesc[colN - 1],
282  &isnull);
283  }
284 
285  if (isnull)
286  *category = GinGetNullCategory(tuple, ginstate);
287  else
288  *category = GIN_CAT_NORM_KEY;
289 
290  return res;
291 }
#define GinGetNullCategory(itup, ginstate)
Definition: ginblock.h:220
OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
Definition: ginutil.c:225
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
TupleDesc origTupdesc
Definition: gin_private.h:72

References FirstOffsetNumber, GIN_CAT_NORM_KEY, GinGetNullCategory, gintuple_get_attrnum(), index_getattr, OffsetNumberNext, GinState::oneCol, GinState::origTupdesc, res, and GinState::tupdesc.

Referenced by addItemPointersToLeafTuple(), collectMatchBitmap(), collectMatchesForHeapRow(), entryIsMoveRight(), entryLocateEntry(), entryLocateLeafEntry(), ginVacuumEntryPage(), matchPartialInPendingList(), and processPendingPage().

◆ ginUpdateStats()

void ginUpdateStats ( Relation  index,
const GinStatsData stats,
bool  is_build 
)

Definition at line 657 of file ginutil.c.

658 {
659  Buffer metabuffer;
660  Page metapage;
661  GinMetaPageData *metadata;
662 
663  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
664  LockBuffer(metabuffer, GIN_EXCLUSIVE);
665  metapage = BufferGetPage(metabuffer);
666  metadata = GinPageGetMeta(metapage);
667 
669 
670  metadata->nTotalPages = stats->nTotalPages;
671  metadata->nEntryPages = stats->nEntryPages;
672  metadata->nDataPages = stats->nDataPages;
673  metadata->nEntries = stats->nEntries;
674 
675  /*
676  * Set pd_lower just past the end of the metadata. This is essential,
677  * because without doing so, metadata will be lost if xlog.c compresses
678  * the page. (We must do this here because pre-v11 versions of PG did not
679  * set the metapage's pd_lower correctly, so a pg_upgraded index might
680  * contain the wrong value.)
681  */
682  ((PageHeader) metapage)->pd_lower =
683  ((char *) metadata + sizeof(GinMetaPageData)) - (char *) metapage;
684 
685  MarkBufferDirty(metabuffer);
686 
687  if (RelationNeedsWAL(index) && !is_build)
688  {
689  XLogRecPtr recptr;
691 
692  data.node = index->rd_node;
693  data.ntuples = 0;
694  data.newRightlink = data.prevTail = InvalidBlockNumber;
695  memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
696 
697  XLogBeginInsert();
698  XLogRegisterData((char *) &data, sizeof(ginxlogUpdateMeta));
700 
701  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
702  PageSetLSN(metapage, recptr);
703  }
704 
705  UnlockReleaseBuffer(metabuffer);
706 
708 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1573
#define PageSetLSN(page, lsn)
Definition: bufpage.h:367
#define XLOG_GIN_UPDATE_META_PAGE
Definition: ginxlog.h:162
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
#define RelationNeedsWAL(relation)
Definition: rel.h:613
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:443
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:243
void XLogBeginInsert(void)
Definition: xloginsert.c:150
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:351
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33

References BufferGetPage, data, END_CRIT_SECTION, GIN_EXCLUSIVE, GIN_METAPAGE_BLKNO, GinPageGetMeta, InvalidBlockNumber, LockBuffer(), MarkBufferDirty(), GinStatsData::nDataPages, GinMetaPageData::nDataPages, GinStatsData::nEntries, GinMetaPageData::nEntries, GinStatsData::nEntryPages, GinMetaPageData::nEntryPages, GinStatsData::nTotalPages, GinMetaPageData::nTotalPages, PageSetLSN, ReadBuffer(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationNeedsWAL, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_GIN_UPDATE_META_PAGE, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by ginbuild(), and ginvacuumcleanup().

◆ initGinState()

void initGinState ( GinState state,
Relation  index 
)

Definition at line 96 of file ginutil.c.

97 {
98  TupleDesc origTupdesc = RelationGetDescr(index);
99  int i;
100 
101  MemSet(state, 0, sizeof(GinState));
102 
103  state->index = index;
104  state->oneCol = (origTupdesc->natts == 1);
105  state->origTupdesc = origTupdesc;
106 
107  for (i = 0; i < origTupdesc->natts; i++)
108  {
109  Form_pg_attribute attr = TupleDescAttr(origTupdesc, i);
110 
111  if (state->oneCol)
112  state->tupdesc[i] = state->origTupdesc;
113  else
114  {
115  state->tupdesc[i] = CreateTemplateTupleDesc(2);
116 
117  TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL,
118  INT2OID, -1, 0);
119  TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL,
120  attr->atttypid,
121  attr->atttypmod,
122  attr->attndims);
124  attr->attcollation);
125  }
126 
127  /*
128  * If the compare proc isn't specified in the opclass definition, look
129  * up the index key type's default btree comparator.
130  */
132  {
133  fmgr_info_copy(&(state->compareFn[i]),
136  }
137  else
138  {
139  TypeCacheEntry *typentry;
140 
141  typentry = lookup_type_cache(attr->atttypid,
143  if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
144  ereport(ERROR,
145  (errcode(ERRCODE_UNDEFINED_FUNCTION),
146  errmsg("could not identify a comparison function for type %s",
147  format_type_be(attr->atttypid))));
148  fmgr_info_copy(&(state->compareFn[i]),
149  &(typentry->cmp_proc_finfo),
151  }
152 
153  /* Opclass must always provide extract procs */
154  fmgr_info_copy(&(state->extractValueFn[i]),
157  fmgr_info_copy(&(state->extractQueryFn[i]),
160 
161  /*
162  * Check opclass capability to do tri-state or binary logic consistent
163  * check.
164  */
166  {
167  fmgr_info_copy(&(state->triConsistentFn[i]),
170  }
171 
173  {
174  fmgr_info_copy(&(state->consistentFn[i]),
177  }
178 
179  if (state->consistentFn[i].fn_oid == InvalidOid &&
180  state->triConsistentFn[i].fn_oid == InvalidOid)
181  {
182  elog(ERROR, "missing GIN support function (%d or %d) for attribute %d of index \"%s\"",
185  }
186 
187  /*
188  * Check opclass capability to do partial match.
189  */
191  {
192  fmgr_info_copy(&(state->comparePartialFn[i]),
195  state->canPartialMatch[i] = true;
196  }
197  else
198  {
199  state->canPartialMatch[i] = false;
200  }
201 
202  /*
203  * If the index column has a specified collation, we should honor that
204  * while doing comparisons. However, we may have a collatable storage
205  * type for a noncollatable indexed data type (for instance, hstore
206  * uses text index entries). If there's no index collation then
207  * specify default collation in case the support functions need
208  * collation. This is harmless if the support functions don't care
209  * about collation, so we just do it unconditionally. (We could
210  * alternatively call get_typcollation, but that seems like expensive
211  * overkill --- there aren't going to be any cases where a GIN storage
212  * type has a nondefault collation.)
213  */
214  if (OidIsValid(index->rd_indcollation[i]))
215  state->supportCollation[i] = index->rd_indcollation[i];
216  else
217  state->supportCollation[i] = DEFAULT_COLLATION_OID;
218  }
219 }
int16 AttrNumber
Definition: attnum.h:21
#define MemSet(start, val, len)
Definition: c.h:1008
#define OidIsValid(objectId)
Definition: c.h:710
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:594
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define GIN_COMPARE_PROC
Definition: gin.h:22
#define GIN_CONSISTENT_PROC
Definition: gin.h:25
#define GIN_EXTRACTQUERY_PROC
Definition: gin.h:24
#define GIN_EXTRACTVALUE_PROC
Definition: gin.h:23
#define GIN_TRICONSISTENT_PROC
Definition: gin.h:27
#define GIN_COMPARE_PARTIAL_PROC
Definition: gin.h:26
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:769
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define RelationGetDescr(relation)
Definition: rel.h:515
#define RelationGetRelationName(relation)
Definition: rel.h:523
Oid fn_oid
Definition: fmgr.h:59
FmgrInfo cmp_proc_finfo
Definition: typcache.h:76
Definition: regguts.h:318
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:754
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:142

References TypeCacheEntry::cmp_proc_finfo, CreateTemplateTupleDesc(), CurrentMemoryContext, elog, ereport, errcode(), errmsg(), ERROR, fmgr_info_copy(), FmgrInfo::fn_oid, format_type_be(), GIN_COMPARE_PARTIAL_PROC, GIN_COMPARE_PROC, GIN_CONSISTENT_PROC, GIN_EXTRACTQUERY_PROC, GIN_EXTRACTVALUE_PROC, GIN_TRICONSISTENT_PROC, i, index_getprocid(), index_getprocinfo(), InvalidOid, lookup_type_cache(), MemSet, TupleDescData::natts, OidIsValid, RelationGetDescr, RelationGetRelationName, TupleDescAttr, TupleDescInitEntry(), TupleDescInitEntryCollation(), and TYPECACHE_CMP_PROC_FINFO.

Referenced by gin_clean_pending_list(), ginbeginscan(), ginbuild(), ginbulkdelete(), gininsert(), and ginvacuumcleanup().