PostgreSQL Source Code  git master
brin.c
Go to the documentation of this file.
1 /*
2  * brin.c
3  * Implementation of BRIN indexes for Postgres
4  *
5  * See src/backend/access/brin/README for details.
6  *
7  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/access/brin/brin.c
12  *
13  * TODO
14  * * ScalarArrayOpExpr (amsearcharray -> SK_SEARCHARRAY)
15  */
16 #include "postgres.h"
17 
18 #include "access/brin.h"
19 #include "access/brin_page.h"
20 #include "access/brin_pageops.h"
21 #include "access/brin_xlog.h"
22 #include "access/relation.h"
23 #include "access/reloptions.h"
24 #include "access/relscan.h"
25 #include "access/table.h"
26 #include "access/tableam.h"
27 #include "access/xloginsert.h"
28 #include "catalog/index.h"
29 #include "catalog/pg_am.h"
30 #include "commands/vacuum.h"
31 #include "miscadmin.h"
32 #include "pgstat.h"
33 #include "postmaster/autovacuum.h"
34 #include "storage/bufmgr.h"
35 #include "storage/freespace.h"
36 #include "utils/acl.h"
37 #include "utils/builtins.h"
38 #include "utils/datum.h"
39 #include "utils/guc.h"
40 #include "utils/index_selfuncs.h"
41 #include "utils/memutils.h"
42 #include "utils/rel.h"
43 
44 
45 /*
46  * We use a BrinBuildState during initial construction of a BRIN index.
47  * The running state is kept in a BrinMemTuple.
48  */
49 typedef struct BrinBuildState
50 {
60 
61 /*
62  * Struct used as "opaque" during index scans
63  */
64 typedef struct BrinOpaque
65 {
70 
71 #define BRIN_ALL_BLOCKRANGES InvalidBlockNumber
72 
74  BrinRevmap *revmap, BlockNumber pagesPerRange);
76 static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange,
77  bool include_partial, double *numSummarized, double *numExisting);
79 static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a,
80  BrinTuple *b);
81 static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy);
82 static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc,
83  BrinMemTuple *dtup, Datum *values, bool *nulls);
84 static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys);
85 
86 /*
87  * BRIN handler function: return IndexAmRoutine with access method parameters
88  * and callbacks.
89  */
90 Datum
92 {
94 
95  amroutine->amstrategies = 0;
98  amroutine->amcanorder = false;
99  amroutine->amcanorderbyop = false;
100  amroutine->amcanbackward = false;
101  amroutine->amcanunique = false;
102  amroutine->amcanmulticol = true;
103  amroutine->amoptionalkey = true;
104  amroutine->amsearcharray = false;
105  amroutine->amsearchnulls = true;
106  amroutine->amstorage = true;
107  amroutine->amclusterable = false;
108  amroutine->ampredlocks = false;
109  amroutine->amcanparallel = false;
110  amroutine->amcaninclude = false;
111  amroutine->amusemaintenanceworkmem = false;
112  amroutine->amparallelvacuumoptions =
114  amroutine->amkeytype = InvalidOid;
115 
116  amroutine->ambuild = brinbuild;
117  amroutine->ambuildempty = brinbuildempty;
118  amroutine->aminsert = brininsert;
119  amroutine->ambulkdelete = brinbulkdelete;
120  amroutine->amvacuumcleanup = brinvacuumcleanup;
121  amroutine->amcanreturn = NULL;
122  amroutine->amcostestimate = brincostestimate;
123  amroutine->amoptions = brinoptions;
124  amroutine->amproperty = NULL;
125  amroutine->ambuildphasename = NULL;
126  amroutine->amvalidate = brinvalidate;
127  amroutine->amadjustmembers = NULL;
128  amroutine->ambeginscan = brinbeginscan;
129  amroutine->amrescan = brinrescan;
130  amroutine->amgettuple = NULL;
131  amroutine->amgetbitmap = bringetbitmap;
132  amroutine->amendscan = brinendscan;
133  amroutine->ammarkpos = NULL;
134  amroutine->amrestrpos = NULL;
135  amroutine->amestimateparallelscan = NULL;
136  amroutine->aminitparallelscan = NULL;
137  amroutine->amparallelrescan = NULL;
138 
139  PG_RETURN_POINTER(amroutine);
140 }
141 
142 /*
143  * A tuple in the heap is being inserted. To keep a brin index up to date,
144  * we need to obtain the relevant index tuple and compare its stored values
145  * with those of the new tuple. If the tuple values are not consistent with
146  * the summary tuple, we need to update the index tuple.
147  *
148  * If autosummarization is enabled, check if we need to summarize the previous
149  * page range.
150  *
151  * If the range is not currently summarized (i.e. the revmap returns NULL for
152  * it), there's nothing to do for this tuple.
153  */
154 bool
155 brininsert(Relation idxRel, Datum *values, bool *nulls,
156  ItemPointer heaptid, Relation heapRel,
157  IndexUniqueCheck checkUnique,
158  bool indexUnchanged,
159  IndexInfo *indexInfo)
160 {
161  BlockNumber pagesPerRange;
162  BlockNumber origHeapBlk;
163  BlockNumber heapBlk;
164  BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache;
165  BrinRevmap *revmap;
167  MemoryContext tupcxt = NULL;
169  bool autosummarize = BrinGetAutoSummarize(idxRel);
170 
171  revmap = brinRevmapInitialize(idxRel, &pagesPerRange, NULL);
172 
173  /*
174  * origHeapBlk is the block number where the insertion occurred. heapBlk
175  * is the first block in the corresponding page range.
176  */
177  origHeapBlk = ItemPointerGetBlockNumber(heaptid);
178  heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
179 
180  for (;;)
181  {
182  bool need_insert = false;
183  OffsetNumber off;
184  BrinTuple *brtup;
185  BrinMemTuple *dtup;
186 
188 
189  /*
190  * If auto-summarization is enabled and we just inserted the first
191  * tuple into the first block of a new non-first page range, request a
192  * summarization run of the previous range.
193  */
194  if (autosummarize &&
195  heapBlk > 0 &&
196  heapBlk == origHeapBlk &&
198  {
199  BlockNumber lastPageRange = heapBlk - 1;
200  BrinTuple *lastPageTuple;
201 
202  lastPageTuple =
203  brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
204  NULL, BUFFER_LOCK_SHARE, NULL);
205  if (!lastPageTuple)
206  {
207  bool recorded;
208 
210  RelationGetRelid(idxRel),
211  lastPageRange);
212  if (!recorded)
213  ereport(LOG,
214  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
215  errmsg("request for BRIN range summarization for index \"%s\" page %u was not recorded",
216  RelationGetRelationName(idxRel),
217  lastPageRange)));
218  }
219  else
221  }
222 
223  brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
224  NULL, BUFFER_LOCK_SHARE, NULL);
225 
226  /* if range is unsummarized, there's nothing to do */
227  if (!brtup)
228  break;
229 
230  /* First time through in this statement? */
231  if (bdesc == NULL)
232  {
233  MemoryContextSwitchTo(indexInfo->ii_Context);
234  bdesc = brin_build_desc(idxRel);
235  indexInfo->ii_AmCache = (void *) bdesc;
236  MemoryContextSwitchTo(oldcxt);
237  }
238  /* First time through in this brininsert call? */
239  if (tupcxt == NULL)
240  {
242  "brininsert cxt",
244  MemoryContextSwitchTo(tupcxt);
245  }
246 
247  dtup = brin_deform_tuple(bdesc, brtup, NULL);
248 
249  need_insert = add_values_to_range(idxRel, bdesc, dtup, values, nulls);
250 
251  if (!need_insert)
252  {
253  /*
254  * The tuple is consistent with the new values, so there's nothing
255  * to do.
256  */
258  }
259  else
260  {
261  Page page = BufferGetPage(buf);
262  ItemId lp = PageGetItemId(page, off);
263  Size origsz;
264  BrinTuple *origtup;
265  Size newsz;
266  BrinTuple *newtup;
267  bool samepage;
268 
269  /*
270  * Make a copy of the old tuple, so that we can compare it after
271  * re-acquiring the lock.
272  */
273  origsz = ItemIdGetLength(lp);
274  origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
275 
276  /*
277  * Before releasing the lock, check if we can attempt a same-page
278  * update. Another process could insert a tuple concurrently in
279  * the same page though, so downstream we must be prepared to cope
280  * if this turns out to not be possible after all.
281  */
282  newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
283  samepage = brin_can_do_samepage_update(buf, origsz, newsz);
285 
286  /*
287  * Try to update the tuple. If this doesn't work for whatever
288  * reason, we need to restart from the top; the revmap might be
289  * pointing at a different tuple for this block now, so we need to
290  * recompute to ensure both our new heap tuple and the other
291  * inserter's are covered by the combined tuple. It might be that
292  * we don't need to update at all.
293  */
294  if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
295  buf, off, origtup, origsz, newtup, newsz,
296  samepage))
297  {
298  /* no luck; start over */
300  continue;
301  }
302  }
303 
304  /* success! */
305  break;
306  }
307 
308  brinRevmapTerminate(revmap);
309  if (BufferIsValid(buf))
311  MemoryContextSwitchTo(oldcxt);
312  if (tupcxt != NULL)
313  MemoryContextDelete(tupcxt);
314 
315  return false;
316 }
317 
318 /*
319  * Initialize state for a BRIN index scan.
320  *
321  * We read the metapage here to determine the pages-per-range number that this
322  * index was built with. Note that since this cannot be changed while we're
323  * holding lock on index, it's not necessary to recompute it during brinrescan.
324  */
326 brinbeginscan(Relation r, int nkeys, int norderbys)
327 {
328  IndexScanDesc scan;
329  BrinOpaque *opaque;
330 
331  scan = RelationGetIndexScan(r, nkeys, norderbys);
332 
333  opaque = palloc_object(BrinOpaque);
334  opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
335  scan->xs_snapshot);
336  opaque->bo_bdesc = brin_build_desc(r);
337  scan->opaque = opaque;
338 
339  return scan;
340 }
341 
342 /*
343  * Execute the index scan.
344  *
345  * This works by reading index TIDs from the revmap, and obtaining the index
346  * tuples pointed to by them; the summary values in the index tuples are
347  * compared to the scan keys. We return into the TID bitmap all the pages in
348  * ranges corresponding to index tuples that match the scan keys.
349  *
350  * If a TID from the revmap is read as InvalidTID, we know that range is
351  * unsummarized. Pages in those ranges need to be returned regardless of scan
352  * keys.
353  */
354 int64
356 {
357  Relation idxRel = scan->indexRelation;
359  BrinDesc *bdesc;
360  Oid heapOid;
361  Relation heapRel;
362  BrinOpaque *opaque;
363  BlockNumber nblocks;
364  BlockNumber heapBlk;
365  int totalpages = 0;
366  FmgrInfo *consistentFn;
367  MemoryContext oldcxt;
368  MemoryContext perRangeCxt;
369  BrinMemTuple *dtup;
370  BrinTuple *btup = NULL;
371  Size btupsz = 0;
372  ScanKey **keys,
373  **nullkeys;
374  int *nkeys,
375  *nnullkeys;
376  char *ptr;
377  Size len;
378  char *tmp PG_USED_FOR_ASSERTS_ONLY;
379 
380  opaque = (BrinOpaque *) scan->opaque;
381  bdesc = opaque->bo_bdesc;
382  pgstat_count_index_scan(idxRel);
383 
384  /*
385  * We need to know the size of the table so that we know how long to
386  * iterate on the revmap.
387  */
388  heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
389  heapRel = table_open(heapOid, AccessShareLock);
390  nblocks = RelationGetNumberOfBlocks(heapRel);
391  table_close(heapRel, AccessShareLock);
392 
393  /*
394  * Make room for the consistent support procedures of indexed columns. We
395  * don't look them up here; we do that lazily the first time we see a scan
396  * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
397  */
398  consistentFn = palloc0_array(FmgrInfo, bdesc->bd_tupdesc->natts);
399 
400  /*
401  * Make room for per-attribute lists of scan keys that we'll pass to the
402  * consistent support procedure. We don't know which attributes have scan
403  * keys, so we allocate space for all attributes. That may use more memory
404  * but it's probably cheaper than determining which attributes are used.
405  *
406  * We keep null and regular keys separate, so that we can pass just the
407  * regular keys to the consistent function easily.
408  *
409  * To reduce the allocation overhead, we allocate one big chunk and then
410  * carve it into smaller arrays ourselves. All the pieces have exactly the
411  * same lifetime, so that's OK.
412  *
413  * XXX The widest index can have 32 attributes, so the amount of wasted
414  * memory is negligible. We could invent a more compact approach (with
415  * just space for used attributes) but that would make the matching more
416  * complex so it's not a good trade-off.
417  */
418  len =
419  MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* regular keys */
420  MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
421  MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts) +
422  MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts) + /* NULL keys */
423  MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys) * bdesc->bd_tupdesc->natts +
424  MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
425 
426  ptr = palloc(len);
427  tmp = ptr;
428 
429  keys = (ScanKey **) ptr;
430  ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
431 
432  nullkeys = (ScanKey **) ptr;
433  ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
434 
435  nkeys = (int *) ptr;
436  ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
437 
438  nnullkeys = (int *) ptr;
439  ptr += MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
440 
441  for (int i = 0; i < bdesc->bd_tupdesc->natts; i++)
442  {
443  keys[i] = (ScanKey *) ptr;
444  ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
445 
446  nullkeys[i] = (ScanKey *) ptr;
447  ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
448  }
449 
450  Assert(tmp + len == ptr);
451 
452  /* zero the number of keys */
453  memset(nkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
454  memset(nnullkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
455 
456  /* Preprocess the scan keys - split them into per-attribute arrays. */
457  for (int keyno = 0; keyno < scan->numberOfKeys; keyno++)
458  {
459  ScanKey key = &scan->keyData[keyno];
460  AttrNumber keyattno = key->sk_attno;
461 
462  /*
463  * The collation of the scan key must match the collation used in the
464  * index column (but only if the search is not IS NULL/ IS NOT NULL).
465  * Otherwise we shouldn't be using this index ...
466  */
467  Assert((key->sk_flags & SK_ISNULL) ||
468  (key->sk_collation ==
469  TupleDescAttr(bdesc->bd_tupdesc,
470  keyattno - 1)->attcollation));
471 
472  /*
473  * First time we see this index attribute, so init as needed.
474  *
475  * This is a bit of an overkill - we don't know how many scan keys are
476  * there for this attribute, so we simply allocate the largest number
477  * possible (as if all keys were for this attribute). This may waste a
478  * bit of memory, but we only expect small number of scan keys in
479  * general, so this should be negligible, and repeated repalloc calls
480  * are not free either.
481  */
482  if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
483  {
484  FmgrInfo *tmp;
485 
486  /* First time we see this attribute, so no key/null keys. */
487  Assert(nkeys[keyattno - 1] == 0);
488  Assert(nnullkeys[keyattno - 1] == 0);
489 
490  tmp = index_getprocinfo(idxRel, keyattno,
492  fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
494  }
495 
496  /* Add key to the proper per-attribute array. */
497  if (key->sk_flags & SK_ISNULL)
498  {
499  nullkeys[keyattno - 1][nnullkeys[keyattno - 1]] = key;
500  nnullkeys[keyattno - 1]++;
501  }
502  else
503  {
504  keys[keyattno - 1][nkeys[keyattno - 1]] = key;
505  nkeys[keyattno - 1]++;
506  }
507  }
508 
509  /* allocate an initial in-memory tuple, out of the per-range memcxt */
510  dtup = brin_new_memtuple(bdesc);
511 
512  /*
513  * Setup and use a per-range memory context, which is reset every time we
514  * loop below. This avoids having to free the tuples within the loop.
515  */
517  "bringetbitmap cxt",
519  oldcxt = MemoryContextSwitchTo(perRangeCxt);
520 
521  /*
522  * Now scan the revmap. We start by querying for heap page 0,
523  * incrementing by the number of pages per range; this gives us a full
524  * view of the table.
525  */
526  for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
527  {
528  bool addrange;
529  bool gottuple = false;
530  BrinTuple *tup;
531  OffsetNumber off;
532  Size size;
533 
535 
537 
538  tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
539  &off, &size, BUFFER_LOCK_SHARE,
540  scan->xs_snapshot);
541  if (tup)
542  {
543  gottuple = true;
544  btup = brin_copy_tuple(tup, size, btup, &btupsz);
546  }
547 
548  /*
549  * For page ranges with no indexed tuple, we must return the whole
550  * range; otherwise, compare it to the scan keys.
551  */
552  if (!gottuple)
553  {
554  addrange = true;
555  }
556  else
557  {
558  dtup = brin_deform_tuple(bdesc, btup, dtup);
559  if (dtup->bt_placeholder)
560  {
561  /*
562  * Placeholder tuples are always returned, regardless of the
563  * values stored in them.
564  */
565  addrange = true;
566  }
567  else
568  {
569  int attno;
570 
571  /*
572  * Compare scan keys with summary values stored for the range.
573  * If scan keys are matched, the page range must be added to
574  * the bitmap. We initially assume the range needs to be
575  * added; in particular this serves the case where there are
576  * no keys.
577  */
578  addrange = true;
579  for (attno = 1; attno <= bdesc->bd_tupdesc->natts; attno++)
580  {
581  BrinValues *bval;
582  Datum add;
583  Oid collation;
584 
585  /*
586  * skip attributes without any scan keys (both regular and
587  * IS [NOT] NULL)
588  */
589  if (nkeys[attno - 1] == 0 && nnullkeys[attno - 1] == 0)
590  continue;
591 
592  bval = &dtup->bt_columns[attno - 1];
593 
594  /*
595  * First check if there are any IS [NOT] NULL scan keys,
596  * and if we're violating them. In that case we can
597  * terminate early, without invoking the support function.
598  *
599  * As there may be more keys, we can only determine
600  * mismatch within this loop.
601  */
602  if (bdesc->bd_info[attno - 1]->oi_regular_nulls &&
603  !check_null_keys(bval, nullkeys[attno - 1],
604  nnullkeys[attno - 1]))
605  {
606  /*
607  * If any of the IS [NOT] NULL keys failed, the page
608  * range as a whole can't pass. So terminate the loop.
609  */
610  addrange = false;
611  break;
612  }
613 
614  /*
615  * So either there are no IS [NOT] NULL keys, or all
616  * passed. If there are no regular scan keys, we're done -
617  * the page range matches. If there are regular keys, but
618  * the page range is marked as 'all nulls' it can't
619  * possibly pass (we're assuming the operators are
620  * strict).
621  */
622 
623  /* No regular scan keys - page range as a whole passes. */
624  if (!nkeys[attno - 1])
625  continue;
626 
627  Assert((nkeys[attno - 1] > 0) &&
628  (nkeys[attno - 1] <= scan->numberOfKeys));
629 
630  /* If it is all nulls, it cannot possibly be consistent. */
631  if (bval->bv_allnulls)
632  {
633  addrange = false;
634  break;
635  }
636 
637  /*
638  * Collation from the first key (has to be the same for
639  * all keys for the same attribute).
640  */
641  collation = keys[attno - 1][0]->sk_collation;
642 
643  /*
644  * Check whether the scan key is consistent with the page
645  * range values; if so, have the pages in the range added
646  * to the output bitmap.
647  *
648  * The opclass may or may not support processing of
649  * multiple scan keys. We can determine that based on the
650  * number of arguments - functions with extra parameter
651  * (number of scan keys) do support this, otherwise we
652  * have to simply pass the scan keys one by one.
653  */
654  if (consistentFn[attno - 1].fn_nargs >= 4)
655  {
656  /* Check all keys at once */
657  add = FunctionCall4Coll(&consistentFn[attno - 1],
658  collation,
659  PointerGetDatum(bdesc),
660  PointerGetDatum(bval),
661  PointerGetDatum(keys[attno - 1]),
662  Int32GetDatum(nkeys[attno - 1]));
663  addrange = DatumGetBool(add);
664  }
665  else
666  {
667  /*
668  * Check keys one by one
669  *
670  * When there are multiple scan keys, failure to meet
671  * the criteria for a single one of them is enough to
672  * discard the range as a whole, so break out of the
673  * loop as soon as a false return value is obtained.
674  */
675  int keyno;
676 
677  for (keyno = 0; keyno < nkeys[attno - 1]; keyno++)
678  {
679  add = FunctionCall3Coll(&consistentFn[attno - 1],
680  keys[attno - 1][keyno]->sk_collation,
681  PointerGetDatum(bdesc),
682  PointerGetDatum(bval),
683  PointerGetDatum(keys[attno - 1][keyno]));
684  addrange = DatumGetBool(add);
685  if (!addrange)
686  break;
687  }
688  }
689  }
690  }
691  }
692 
693  /* add the pages in the range to the output bitmap, if needed */
694  if (addrange)
695  {
696  BlockNumber pageno;
697 
698  for (pageno = heapBlk;
699  pageno <= Min(nblocks, heapBlk + opaque->bo_pagesPerRange) - 1;
700  pageno++)
701  {
702  MemoryContextSwitchTo(oldcxt);
703  tbm_add_page(tbm, pageno);
704  totalpages++;
705  MemoryContextSwitchTo(perRangeCxt);
706  }
707  }
708  }
709 
710  MemoryContextSwitchTo(oldcxt);
711  MemoryContextDelete(perRangeCxt);
712 
713  if (buf != InvalidBuffer)
715 
716  /*
717  * XXX We have an approximation of the number of *pages* that our scan
718  * returns, but we don't have a precise idea of the number of heap tuples
719  * involved.
720  */
721  return totalpages * 10;
722 }
723 
724 /*
725  * Re-initialize state for a BRIN index scan
726  */
727 void
728 brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
729  ScanKey orderbys, int norderbys)
730 {
731  /*
732  * Other index AMs preprocess the scan keys at this point, or sometime
733  * early during the scan; this lets them optimize by removing redundant
734  * keys, or doing early returns when they are impossible to satisfy; see
735  * _bt_preprocess_keys for an example. Something like that could be added
736  * here someday, too.
737  */
738 
739  if (scankey && scan->numberOfKeys > 0)
740  memmove(scan->keyData, scankey,
741  scan->numberOfKeys * sizeof(ScanKeyData));
742 }
743 
744 /*
745  * Close down a BRIN index scan
746  */
747 void
749 {
750  BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
751 
753  brin_free_desc(opaque->bo_bdesc);
754  pfree(opaque);
755 }
756 
757 /*
758  * Per-heap-tuple callback for table_index_build_scan.
759  *
760  * Note we don't worry about the page range at the end of the table here; it is
761  * present in the build state struct after we're called the last time, but not
762  * inserted into the index. Caller must ensure to do so, if appropriate.
763  */
764 static void
766  ItemPointer tid,
767  Datum *values,
768  bool *isnull,
769  bool tupleIsAlive,
770  void *brstate)
771 {
772  BrinBuildState *state = (BrinBuildState *) brstate;
773  BlockNumber thisblock;
774 
775  thisblock = ItemPointerGetBlockNumber(tid);
776 
777  /*
778  * If we're in a block that belongs to a future range, summarize what
779  * we've got and start afresh. Note the scan might have skipped many
780  * pages, if they were devoid of live tuples; make sure to insert index
781  * tuples for those too.
782  */
783  while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
784  {
785 
786  BRIN_elog((DEBUG2,
787  "brinbuildCallback: completed a range: %u--%u",
788  state->bs_currRangeStart,
789  state->bs_currRangeStart + state->bs_pagesPerRange));
790 
791  /* create the index tuple and insert it */
793 
794  /* set state to correspond to the next range */
795  state->bs_currRangeStart += state->bs_pagesPerRange;
796 
797  /* re-initialize state for it */
798  brin_memtuple_initialize(state->bs_dtuple, state->bs_bdesc);
799  }
800 
801  /* Accumulate the current tuple into the running state */
802  (void) add_values_to_range(index, state->bs_bdesc, state->bs_dtuple,
803  values, isnull);
804 }
805 
806 /*
807  * brinbuild() -- build a new BRIN index.
808  */
811 {
812  IndexBuildResult *result;
813  double reltuples;
814  double idxtuples;
815  BrinRevmap *revmap;
817  Buffer meta;
818  BlockNumber pagesPerRange;
819 
820  /*
821  * We expect to be called exactly once for any index relation.
822  */
824  elog(ERROR, "index \"%s\" already contains data",
826 
827  /*
828  * Critical section not required, because on error the creation of the
829  * whole relation will be rolled back.
830  */
831 
832  meta = ReadBuffer(index, P_NEW);
835 
838  MarkBufferDirty(meta);
839 
840  if (RelationNeedsWAL(index))
841  {
842  xl_brin_createidx xlrec;
843  XLogRecPtr recptr;
844  Page page;
845 
848 
849  XLogBeginInsert();
850  XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
852 
853  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
854 
855  page = BufferGetPage(meta);
856  PageSetLSN(page, recptr);
857  }
858 
859  UnlockReleaseBuffer(meta);
860 
861  /*
862  * Initialize our state, including the deformed tuple state.
863  */
864  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
865  state = initialize_brin_buildstate(index, revmap, pagesPerRange);
866 
867  /*
868  * Now scan the relation. No syncscan allowed here because we want the
869  * heap blocks in physical order.
870  */
871  reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
872  brinbuildCallback, (void *) state, NULL);
873 
874  /* process the final batch */
876 
877  /* release resources */
878  idxtuples = state->bs_numtuples;
879  brinRevmapTerminate(state->bs_rmAccess);
881 
882  /*
883  * Return statistics
884  */
886 
887  result->heap_tuples = reltuples;
888  result->index_tuples = idxtuples;
889 
890  return result;
891 }
892 
893 void
895 {
896  Buffer metabuf;
897 
898  /* An empty BRIN index has a metapage only. */
899  metabuf =
902 
903  /* Initialize and xlog metabuffer. */
907  MarkBufferDirty(metabuf);
908  log_newpage_buffer(metabuf, true);
910 
911  UnlockReleaseBuffer(metabuf);
912 }
913 
914 /*
915  * brinbulkdelete
916  * Since there are no per-heap-tuple index tuples in BRIN indexes,
917  * there's not a lot we can do here.
918  *
919  * XXX we could mark item tuples as "dirty" (when a minimum or maximum heap
920  * tuple is deleted), meaning the need to re-run summarization on the affected
921  * range. Would need to add an extra flag in brintuples for that.
922  */
925  IndexBulkDeleteCallback callback, void *callback_state)
926 {
927  /* allocate stats if first time through, else re-use existing struct */
928  if (stats == NULL)
930 
931  return stats;
932 }
933 
934 /*
935  * This routine is in charge of "vacuuming" a BRIN index: we just summarize
936  * ranges that are currently unsummarized.
937  */
940 {
941  Relation heapRel;
942 
943  /* No-op in ANALYZE ONLY mode */
944  if (info->analyze_only)
945  return stats;
946 
947  if (!stats)
949  stats->num_pages = RelationGetNumberOfBlocks(info->index);
950  /* rest of stats is initialized by zeroing */
951 
952  heapRel = table_open(IndexGetRelation(RelationGetRelid(info->index), false),
954 
955  brin_vacuum_scan(info->index, info->strategy);
956 
957  brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES, false,
958  &stats->num_index_tuples, &stats->num_index_tuples);
959 
960  table_close(heapRel, AccessShareLock);
961 
962  return stats;
963 }
964 
965 /*
966  * reloptions processor for BRIN indexes
967  */
968 bytea *
969 brinoptions(Datum reloptions, bool validate)
970 {
971  static const relopt_parse_elt tab[] = {
972  {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
973  {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
974  };
975 
976  return (bytea *) build_reloptions(reloptions, validate,
978  sizeof(BrinOptions),
979  tab, lengthof(tab));
980 }
981 
982 /*
983  * SQL-callable function to scan through an index and summarize all ranges
984  * that are not currently summarized.
985  */
986 Datum
988 {
989  Datum relation = PG_GETARG_DATUM(0);
990 
992  relation,
994 }
995 
996 /*
997  * SQL-callable function to summarize the indicated page range, if not already
998  * summarized. If the second argument is BRIN_ALL_BLOCKRANGES, all
999  * unsummarized ranges are summarized.
1000  */
1001 Datum
1003 {
1004  Oid indexoid = PG_GETARG_OID(0);
1005  int64 heapBlk64 = PG_GETARG_INT64(1);
1006  BlockNumber heapBlk;
1007  Oid heapoid;
1008  Relation indexRel;
1009  Relation heapRel;
1010  Oid save_userid;
1011  int save_sec_context;
1012  int save_nestlevel;
1013  double numSummarized = 0;
1014 
1015  if (RecoveryInProgress())
1016  ereport(ERROR,
1017  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1018  errmsg("recovery is in progress"),
1019  errhint("BRIN control functions cannot be executed during recovery.")));
1020 
1021  if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
1022  ereport(ERROR,
1023  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1024  errmsg("block number out of range: %lld",
1025  (long long) heapBlk64)));
1026  heapBlk = (BlockNumber) heapBlk64;
1027 
1028  /*
1029  * We must lock table before index to avoid deadlocks. However, if the
1030  * passed indexoid isn't an index then IndexGetRelation() will fail.
1031  * Rather than emitting a not-very-helpful error message, postpone
1032  * complaining, expecting that the is-it-an-index test below will fail.
1033  */
1034  heapoid = IndexGetRelation(indexoid, true);
1035  if (OidIsValid(heapoid))
1036  {
1037  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1038 
1039  /*
1040  * Autovacuum calls us. For its benefit, switch to the table owner's
1041  * userid, so that any index functions are run as that user. Also
1042  * lock down security-restricted operations and arrange to make GUC
1043  * variable changes local to this command. This is harmless, albeit
1044  * unnecessary, when called from SQL, because we fail shortly if the
1045  * user does not own the index.
1046  */
1047  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1048  SetUserIdAndSecContext(heapRel->rd_rel->relowner,
1049  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1050  save_nestlevel = NewGUCNestLevel();
1051  }
1052  else
1053  {
1054  heapRel = NULL;
1055  /* Set these just to suppress "uninitialized variable" warnings */
1056  save_userid = InvalidOid;
1057  save_sec_context = -1;
1058  save_nestlevel = -1;
1059  }
1060 
1061  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1062 
1063  /* Must be a BRIN index */
1064  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1065  indexRel->rd_rel->relam != BRIN_AM_OID)
1066  ereport(ERROR,
1067  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1068  errmsg("\"%s\" is not a BRIN index",
1069  RelationGetRelationName(indexRel))));
1070 
1071  /* User must own the index (comparable to privileges needed for VACUUM) */
1072  if (heapRel != NULL && !object_ownercheck(RelationRelationId, indexoid, save_userid))
1074  RelationGetRelationName(indexRel));
1075 
1076  /*
1077  * Since we did the IndexGetRelation call above without any lock, it's
1078  * barely possible that a race against an index drop/recreation could have
1079  * netted us the wrong table. Recheck.
1080  */
1081  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1082  ereport(ERROR,
1084  errmsg("could not open parent table of index \"%s\"",
1085  RelationGetRelationName(indexRel))));
1086 
1087  /* OK, do it */
1088  brinsummarize(indexRel, heapRel, heapBlk, true, &numSummarized, NULL);
1089 
1090  /* Roll back any GUC changes executed by index functions */
1091  AtEOXact_GUC(false, save_nestlevel);
1092 
1093  /* Restore userid and security context */
1094  SetUserIdAndSecContext(save_userid, save_sec_context);
1095 
1098 
1099  PG_RETURN_INT32((int32) numSummarized);
1100 }
1101 
1102 /*
1103  * SQL-callable interface to mark a range as no longer summarized
1104  */
1105 Datum
1107 {
1108  Oid indexoid = PG_GETARG_OID(0);
1109  int64 heapBlk64 = PG_GETARG_INT64(1);
1110  BlockNumber heapBlk;
1111  Oid heapoid;
1112  Relation heapRel;
1113  Relation indexRel;
1114  bool done;
1115 
1116  if (RecoveryInProgress())
1117  ereport(ERROR,
1118  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1119  errmsg("recovery is in progress"),
1120  errhint("BRIN control functions cannot be executed during recovery.")));
1121 
1122  if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
1123  ereport(ERROR,
1124  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1125  errmsg("block number out of range: %lld",
1126  (long long) heapBlk64)));
1127  heapBlk = (BlockNumber) heapBlk64;
1128 
1129  /*
1130  * We must lock table before index to avoid deadlocks. However, if the
1131  * passed indexoid isn't an index then IndexGetRelation() will fail.
1132  * Rather than emitting a not-very-helpful error message, postpone
1133  * complaining, expecting that the is-it-an-index test below will fail.
1134  *
1135  * Unlike brin_summarize_range(), autovacuum never calls this. Hence, we
1136  * don't switch userid.
1137  */
1138  heapoid = IndexGetRelation(indexoid, true);
1139  if (OidIsValid(heapoid))
1140  heapRel = table_open(heapoid, ShareUpdateExclusiveLock);
1141  else
1142  heapRel = NULL;
1143 
1144  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
1145 
1146  /* Must be a BRIN index */
1147  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
1148  indexRel->rd_rel->relam != BRIN_AM_OID)
1149  ereport(ERROR,
1150  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1151  errmsg("\"%s\" is not a BRIN index",
1152  RelationGetRelationName(indexRel))));
1153 
1154  /* User must own the index (comparable to privileges needed for VACUUM) */
1155  if (!object_ownercheck(RelationRelationId, indexoid, GetUserId()))
1157  RelationGetRelationName(indexRel));
1158 
1159  /*
1160  * Since we did the IndexGetRelation call above without any lock, it's
1161  * barely possible that a race against an index drop/recreation could have
1162  * netted us the wrong table. Recheck.
1163  */
1164  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
1165  ereport(ERROR,
1167  errmsg("could not open parent table of index \"%s\"",
1168  RelationGetRelationName(indexRel))));
1169 
1170  /* the revmap does the hard work */
1171  do
1172  {
1173  done = brinRevmapDesummarizeRange(indexRel, heapBlk);
1174  }
1175  while (!done);
1176 
1179 
1180  PG_RETURN_VOID();
1181 }
1182 
1183 /*
1184  * Build a BrinDesc used to create or scan a BRIN index
1185  */
1186 BrinDesc *
1188 {
1189  BrinOpcInfo **opcinfo;
1190  BrinDesc *bdesc;
1191  TupleDesc tupdesc;
1192  int totalstored = 0;
1193  int keyno;
1194  long totalsize;
1195  MemoryContext cxt;
1196  MemoryContext oldcxt;
1197 
1199  "brin desc cxt",
1201  oldcxt = MemoryContextSwitchTo(cxt);
1202  tupdesc = RelationGetDescr(rel);
1203 
1204  /*
1205  * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1206  * the number of columns stored, since the number is opclass-defined.
1207  */
1208  opcinfo = palloc_array(BrinOpcInfo*, tupdesc->natts);
1209  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1210  {
1211  FmgrInfo *opcInfoFn;
1212  Form_pg_attribute attr = TupleDescAttr(tupdesc, keyno);
1213 
1214  opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1215 
1216  opcinfo[keyno] = (BrinOpcInfo *)
1217  DatumGetPointer(FunctionCall1(opcInfoFn, attr->atttypid));
1218  totalstored += opcinfo[keyno]->oi_nstored;
1219  }
1220 
1221  /* Allocate our result struct and fill it in */
1222  totalsize = offsetof(BrinDesc, bd_info) +
1223  sizeof(BrinOpcInfo *) * tupdesc->natts;
1224 
1225  bdesc = palloc(totalsize);
1226  bdesc->bd_context = cxt;
1227  bdesc->bd_index = rel;
1228  bdesc->bd_tupdesc = tupdesc;
1229  bdesc->bd_disktdesc = NULL; /* generated lazily */
1230  bdesc->bd_totalstored = totalstored;
1231 
1232  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1233  bdesc->bd_info[keyno] = opcinfo[keyno];
1234  pfree(opcinfo);
1235 
1236  MemoryContextSwitchTo(oldcxt);
1237 
1238  return bdesc;
1239 }
1240 
1241 void
1243 {
1244  /* make sure the tupdesc is still valid */
1245  Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1246  /* no need for retail pfree */
1248 }
1249 
1250 /*
1251  * Fetch index's statistical data into *stats
1252  */
1253 void
1255 {
1256  Buffer metabuffer;
1257  Page metapage;
1258  BrinMetaPageData *metadata;
1259 
1260  metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1261  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1262  metapage = BufferGetPage(metabuffer);
1263  metadata = (BrinMetaPageData *) PageGetContents(metapage);
1264 
1265  stats->pagesPerRange = metadata->pagesPerRange;
1266  stats->revmapNumPages = metadata->lastRevmapPage - 1;
1267 
1268  UnlockReleaseBuffer(metabuffer);
1269 }
1270 
1271 /*
1272  * Initialize a BrinBuildState appropriate to create tuples on the given index.
1273  */
1274 static BrinBuildState *
1276  BlockNumber pagesPerRange)
1277 {
1279 
1281 
1282  state->bs_irel = idxRel;
1283  state->bs_numtuples = 0;
1284  state->bs_currentInsertBuf = InvalidBuffer;
1285  state->bs_pagesPerRange = pagesPerRange;
1286  state->bs_currRangeStart = 0;
1287  state->bs_rmAccess = revmap;
1288  state->bs_bdesc = brin_build_desc(idxRel);
1289  state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1290 
1291  return state;
1292 }
1293 
1294 /*
1295  * Release resources associated with a BrinBuildState.
1296  */
1297 static void
1299 {
1300  /*
1301  * Release the last index buffer used. We might as well ensure that
1302  * whatever free space remains in that page is available in FSM, too.
1303  */
1304  if (!BufferIsInvalid(state->bs_currentInsertBuf))
1305  {
1306  Page page;
1307  Size freespace;
1308  BlockNumber blk;
1309 
1310  page = BufferGetPage(state->bs_currentInsertBuf);
1311  freespace = PageGetFreeSpace(page);
1312  blk = BufferGetBlockNumber(state->bs_currentInsertBuf);
1313  ReleaseBuffer(state->bs_currentInsertBuf);
1314  RecordPageWithFreeSpace(state->bs_irel, blk, freespace);
1315  FreeSpaceMapVacuumRange(state->bs_irel, blk, blk + 1);
1316  }
1317 
1318  brin_free_desc(state->bs_bdesc);
1319  pfree(state->bs_dtuple);
1320  pfree(state);
1321 }
1322 
1323 /*
1324  * On the given BRIN index, summarize the heap page range that corresponds
1325  * to the heap block number given.
1326  *
1327  * This routine can run in parallel with insertions into the heap. To avoid
1328  * missing those values from the summary tuple, we first insert a placeholder
1329  * index tuple into the index, then execute the heap scan; transactions
1330  * concurrent with the scan update the placeholder tuple. After the scan, we
1331  * union the placeholder tuple with the one computed by this routine. The
1332  * update of the index value happens in a loop, so that if somebody updates
1333  * the placeholder tuple after we read it, we detect the case and try again.
1334  * This ensures that the concurrently inserted tuples are not lost.
1335  *
1336  * A further corner case is this routine being asked to summarize the partial
1337  * range at the end of the table. heapNumBlocks is the (possibly outdated)
1338  * table size; if we notice that the requested range lies beyond that size,
1339  * we re-compute the table size after inserting the placeholder tuple, to
1340  * avoid missing pages that were appended recently.
1341  */
1342 static void
1344  BlockNumber heapBlk, BlockNumber heapNumBlks)
1345 {
1346  Buffer phbuf;
1347  BrinTuple *phtup;
1348  Size phsz;
1349  OffsetNumber offset;
1350  BlockNumber scanNumBlks;
1351 
1352  /*
1353  * Insert the placeholder tuple
1354  */
1355  phbuf = InvalidBuffer;
1356  phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1357  offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1358  state->bs_rmAccess, &phbuf,
1359  heapBlk, phtup, phsz);
1360 
1361  /*
1362  * Compute range end. We hold ShareUpdateExclusive lock on table, so it
1363  * cannot shrink concurrently (but it can grow).
1364  */
1365  Assert(heapBlk % state->bs_pagesPerRange == 0);
1366  if (heapBlk + state->bs_pagesPerRange > heapNumBlks)
1367  {
1368  /*
1369  * If we're asked to scan what we believe to be the final range on the
1370  * table (i.e. a range that might be partial) we need to recompute our
1371  * idea of what the latest page is after inserting the placeholder
1372  * tuple. Anyone that grows the table later will update the
1373  * placeholder tuple, so it doesn't matter that we won't scan these
1374  * pages ourselves. Careful: the table might have been extended
1375  * beyond the current range, so clamp our result.
1376  *
1377  * Fortunately, this should occur infrequently.
1378  */
1379  scanNumBlks = Min(RelationGetNumberOfBlocks(heapRel) - heapBlk,
1380  state->bs_pagesPerRange);
1381  }
1382  else
1383  {
1384  /* Easy case: range is known to be complete */
1385  scanNumBlks = state->bs_pagesPerRange;
1386  }
1387 
1388  /*
1389  * Execute the partial heap scan covering the heap blocks in the specified
1390  * page range, summarizing the heap tuples in it. This scan stops just
1391  * short of brinbuildCallback creating the new index entry.
1392  *
1393  * Note that it is critical we use the "any visible" mode of
1394  * table_index_build_range_scan here: otherwise, we would miss tuples
1395  * inserted by transactions that are still in progress, among other corner
1396  * cases.
1397  */
1398  state->bs_currRangeStart = heapBlk;
1399  table_index_build_range_scan(heapRel, state->bs_irel, indexInfo, false, true, false,
1400  heapBlk, scanNumBlks,
1401  brinbuildCallback, (void *) state, NULL);
1402 
1403  /*
1404  * Now we update the values obtained by the scan with the placeholder
1405  * tuple. We do this in a loop which only terminates if we're able to
1406  * update the placeholder tuple successfully; if we are not, this means
1407  * somebody else modified the placeholder tuple after we read it.
1408  */
1409  for (;;)
1410  {
1411  BrinTuple *newtup;
1412  Size newsize;
1413  bool didupdate;
1414  bool samepage;
1415 
1417 
1418  /*
1419  * Update the summary tuple and try to update.
1420  */
1421  newtup = brin_form_tuple(state->bs_bdesc,
1422  heapBlk, state->bs_dtuple, &newsize);
1423  samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1424  didupdate =
1425  brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1426  state->bs_rmAccess, heapBlk, phbuf, offset,
1427  phtup, phsz, newtup, newsize, samepage);
1428  brin_free_tuple(phtup);
1429  brin_free_tuple(newtup);
1430 
1431  /* If the update succeeded, we're done. */
1432  if (didupdate)
1433  break;
1434 
1435  /*
1436  * If the update didn't work, it might be because somebody updated the
1437  * placeholder tuple concurrently. Extract the new version, union it
1438  * with the values we have from the scan, and start over. (There are
1439  * other reasons for the update to fail, but it's simple to treat them
1440  * the same.)
1441  */
1442  phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1443  &offset, &phsz, BUFFER_LOCK_SHARE,
1444  NULL);
1445  /* the placeholder tuple must exist */
1446  if (phtup == NULL)
1447  elog(ERROR, "missing placeholder tuple");
1448  phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1450 
1451  /* merge it into the tuple from the heap scan */
1452  union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1453  }
1454 
1455  ReleaseBuffer(phbuf);
1456 }
1457 
1458 /*
1459  * Summarize page ranges that are not already summarized. If pageRange is
1460  * BRIN_ALL_BLOCKRANGES then the whole table is scanned; otherwise, only the
1461  * page range containing the given heap page number is scanned.
1462  * If include_partial is true, then the partial range at the end of the table
1463  * is summarized, otherwise not.
1464  *
1465  * For each new index tuple inserted, *numSummarized (if not NULL) is
1466  * incremented; for each existing tuple, *numExisting (if not NULL) is
1467  * incremented.
1468  */
1469 static void
1471  bool include_partial, double *numSummarized, double *numExisting)
1472 {
1473  BrinRevmap *revmap;
1474  BrinBuildState *state = NULL;
1475  IndexInfo *indexInfo = NULL;
1476  BlockNumber heapNumBlocks;
1477  BlockNumber pagesPerRange;
1478  Buffer buf;
1479  BlockNumber startBlk;
1480 
1481  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
1482 
1483  /* determine range of pages to process */
1484  heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1485  if (pageRange == BRIN_ALL_BLOCKRANGES)
1486  startBlk = 0;
1487  else
1488  {
1489  startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1490  heapNumBlocks = Min(heapNumBlocks, startBlk + pagesPerRange);
1491  }
1492  if (startBlk > heapNumBlocks)
1493  {
1494  /* Nothing to do if start point is beyond end of table */
1495  brinRevmapTerminate(revmap);
1496  return;
1497  }
1498 
1499  /*
1500  * Scan the revmap to find unsummarized items.
1501  */
1502  buf = InvalidBuffer;
1503  for (; startBlk < heapNumBlocks; startBlk += pagesPerRange)
1504  {
1505  BrinTuple *tup;
1506  OffsetNumber off;
1507 
1508  /*
1509  * Unless requested to summarize even a partial range, go away now if
1510  * we think the next range is partial. Caller would pass true when it
1511  * is typically run once bulk data loading is done
1512  * (brin_summarize_new_values), and false when it is typically the
1513  * result of arbitrarily-scheduled maintenance command (vacuuming).
1514  */
1515  if (!include_partial &&
1516  (startBlk + pagesPerRange > heapNumBlocks))
1517  break;
1518 
1520 
1521  tup = brinGetTupleForHeapBlock(revmap, startBlk, &buf, &off, NULL,
1522  BUFFER_LOCK_SHARE, NULL);
1523  if (tup == NULL)
1524  {
1525  /* no revmap entry for this heap range. Summarize it. */
1526  if (state == NULL)
1527  {
1528  /* first time through */
1529  Assert(!indexInfo);
1531  pagesPerRange);
1532  indexInfo = BuildIndexInfo(index);
1533  }
1534  summarize_range(indexInfo, state, heapRel, startBlk, heapNumBlocks);
1535 
1536  /* and re-initialize state for the next range */
1537  brin_memtuple_initialize(state->bs_dtuple, state->bs_bdesc);
1538 
1539  if (numSummarized)
1540  *numSummarized += 1.0;
1541  }
1542  else
1543  {
1544  if (numExisting)
1545  *numExisting += 1.0;
1547  }
1548  }
1549 
1550  if (BufferIsValid(buf))
1551  ReleaseBuffer(buf);
1552 
1553  /* free resources */
1554  brinRevmapTerminate(revmap);
1555  if (state)
1556  {
1558  pfree(indexInfo);
1559  }
1560 }
1561 
1562 /*
1563  * Given a deformed tuple in the build state, convert it into the on-disk
1564  * format and insert it into the index, making the revmap point to it.
1565  */
1566 static void
1568 {
1569  BrinTuple *tup;
1570  Size size;
1571 
1572  tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1573  state->bs_dtuple, &size);
1574  brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1575  &state->bs_currentInsertBuf, state->bs_currRangeStart,
1576  tup, size);
1577  state->bs_numtuples++;
1578 
1579  pfree(tup);
1580 }
1581 
1582 /*
1583  * Given two deformed tuples, adjust the first one so that it's consistent
1584  * with the summary values in both.
1585  */
1586 static void
1588 {
1589  int keyno;
1590  BrinMemTuple *db;
1591  MemoryContext cxt;
1592  MemoryContext oldcxt;
1593 
1594  /* Use our own memory context to avoid retail pfree */
1596  "brin union",
1598  oldcxt = MemoryContextSwitchTo(cxt);
1599  db = brin_deform_tuple(bdesc, b, NULL);
1600  MemoryContextSwitchTo(oldcxt);
1601 
1602  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1603  {
1604  FmgrInfo *unionFn;
1605  BrinValues *col_a = &a->bt_columns[keyno];
1606  BrinValues *col_b = &db->bt_columns[keyno];
1607  BrinOpcInfo *opcinfo = bdesc->bd_info[keyno];
1608 
1609  if (opcinfo->oi_regular_nulls)
1610  {
1611  /* Adjust "hasnulls". */
1612  if (!col_a->bv_hasnulls && col_b->bv_hasnulls)
1613  col_a->bv_hasnulls = true;
1614 
1615  /* If there are no values in B, there's nothing left to do. */
1616  if (col_b->bv_allnulls)
1617  continue;
1618 
1619  /*
1620  * Adjust "allnulls". If A doesn't have values, just copy the
1621  * values from B into A, and we're done. We cannot run the
1622  * operators in this case, because values in A might contain
1623  * garbage. Note we already established that B contains values.
1624  */
1625  if (col_a->bv_allnulls)
1626  {
1627  int i;
1628 
1629  col_a->bv_allnulls = false;
1630 
1631  for (i = 0; i < opcinfo->oi_nstored; i++)
1632  col_a->bv_values[i] =
1633  datumCopy(col_b->bv_values[i],
1634  opcinfo->oi_typcache[i]->typbyval,
1635  opcinfo->oi_typcache[i]->typlen);
1636 
1637  continue;
1638  }
1639  }
1640 
1641  unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
1643  FunctionCall3Coll(unionFn,
1644  bdesc->bd_index->rd_indcollation[keyno],
1645  PointerGetDatum(bdesc),
1646  PointerGetDatum(col_a),
1647  PointerGetDatum(col_b));
1648  }
1649 
1650  MemoryContextDelete(cxt);
1651 }
1652 
1653 /*
1654  * brin_vacuum_scan
1655  * Do a complete scan of the index during VACUUM.
1656  *
1657  * This routine scans the complete index looking for uncatalogued index pages,
1658  * i.e. those that might have been lost due to a crash after index extension
1659  * and such.
1660  */
1661 static void
1663 {
1664  BlockNumber nblocks;
1665  BlockNumber blkno;
1666 
1667  /*
1668  * Scan the index in physical order, and clean up any possible mess in
1669  * each page.
1670  */
1671  nblocks = RelationGetNumberOfBlocks(idxrel);
1672  for (blkno = 0; blkno < nblocks; blkno++)
1673  {
1674  Buffer buf;
1675 
1677 
1678  buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
1679  RBM_NORMAL, strategy);
1680 
1681  brin_page_cleanup(idxrel, buf);
1682 
1683  ReleaseBuffer(buf);
1684  }
1685 
1686  /*
1687  * Update all upper pages in the index's FSM, as well. This ensures not
1688  * only that we propagate leaf-page FSM updates made by brin_page_cleanup,
1689  * but also that any pre-existing damage or out-of-dateness is repaired.
1690  */
1691  FreeSpaceMapVacuum(idxrel);
1692 }
1693 
1694 static bool
1696  Datum *values, bool *nulls)
1697 {
1698  int keyno;
1699  bool modified = false;
1700 
1701  /*
1702  * Compare the key values of the new tuple to the stored index values; our
1703  * deformed tuple will get updated if the new tuple doesn't fit the
1704  * original range (note this means we can't break out of the loop early).
1705  * Make a note of whether this happens, so that we know to insert the
1706  * modified tuple later.
1707  */
1708  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1709  {
1710  Datum result;
1711  BrinValues *bval;
1712  FmgrInfo *addValue;
1713 
1714  bval = &dtup->bt_columns[keyno];
1715 
1716  if (bdesc->bd_info[keyno]->oi_regular_nulls && nulls[keyno])
1717  {
1718  /*
1719  * If the new value is null, we record that we saw it if it's the
1720  * first one; otherwise, there's nothing to do.
1721  */
1722  if (!bval->bv_hasnulls)
1723  {
1724  bval->bv_hasnulls = true;
1725  modified = true;
1726  }
1727 
1728  continue;
1729  }
1730 
1731  addValue = index_getprocinfo(idxRel, keyno + 1,
1733  result = FunctionCall4Coll(addValue,
1734  idxRel->rd_indcollation[keyno],
1735  PointerGetDatum(bdesc),
1736  PointerGetDatum(bval),
1737  values[keyno],
1738  nulls[keyno]);
1739  /* if that returned true, we need to insert the updated tuple */
1740  modified |= DatumGetBool(result);
1741  }
1742 
1743  return modified;
1744 }
1745 
1746 static bool
1747 check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
1748 {
1749  int keyno;
1750 
1751  /*
1752  * First check if there are any IS [NOT] NULL scan keys, and if we're
1753  * violating them.
1754  */
1755  for (keyno = 0; keyno < nnullkeys; keyno++)
1756  {
1757  ScanKey key = nullkeys[keyno];
1758 
1759  Assert(key->sk_attno == bval->bv_attno);
1760 
1761  /* Handle only IS NULL/IS NOT NULL tests */
1762  if (!(key->sk_flags & SK_ISNULL))
1763  continue;
1764 
1765  if (key->sk_flags & SK_SEARCHNULL)
1766  {
1767  /* IS NULL scan key, but range has no NULLs */
1768  if (!bval->bv_allnulls && !bval->bv_hasnulls)
1769  return false;
1770  }
1771  else if (key->sk_flags & SK_SEARCHNOTNULL)
1772  {
1773  /*
1774  * For IS NOT NULL, we can only skip ranges that are known to have
1775  * only nulls.
1776  */
1777  if (bval->bv_allnulls)
1778  return false;
1779  }
1780  else
1781  {
1782  /*
1783  * Neither IS NULL nor IS NOT NULL was used; assume all indexable
1784  * operators are strict and thus return false with NULL value in
1785  * the scan key.
1786  */
1787  return false;
1788  }
1789  }
1790 
1791  return true;
1792 }
@ ACLCHECK_NOT_OWNER
Definition: acl.h:186
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3485
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4799
int16 AttrNumber
Definition: attnum.h:21
bool AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3252
@ AVW_BRINSummarizeRange
Definition: autovacuum.h:25
uint32 BlockNumber
Definition: block.h:31
#define MaxBlockNumber
Definition: block.h:35
static Datum values[MAXATTR]
Definition: bootstrap.c:156
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:939
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:1662
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1275
Datum brin_desummarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:1106
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:728
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1298
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:1002
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:71
Datum brin_summarize_new_values(PG_FUNCTION_ARGS)
Definition: brin.c:987
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:326
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:810
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:355
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting)
Definition: brin.c:1470
static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, Datum *values, bool *nulls)
Definition: brin.c:1695
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1567
void brinbuildempty(Relation index)
Definition: brin.c:894
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1242
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:1587
void brinGetStats(Relation index, BrinStatsData *stats)
Definition: brin.c:1254
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:1187
struct BrinBuildState BrinBuildState
struct BrinOpaque BrinOpaque
static void summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
Definition: brin.c:1343
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: brin.c:155
Datum brinhandler(PG_FUNCTION_ARGS)
Definition: brin.c:91
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:969
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:924
static void brinbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:765
void brinendscan(IndexScanDesc scan)
Definition: brin.c:748
static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys)
Definition: brin.c:1747
#define BrinGetPagesPerRange(relation)
Definition: brin.h:39
#define BrinGetAutoSummarize(relation)
Definition: brin.h:45
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:78
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:73
#define BRIN_PROCNUM_OPTIONS
Definition: brin_internal.h:75
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:70
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:72
#define BRIN_elog(args)
Definition: brin_internal.h:85
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:71
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
bool brin_doupdate(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, BlockNumber heapBlk, Buffer oldbuf, OffsetNumber oldoff, const BrinTuple *origtup, Size origsz, const BrinTuple *newtup, Size newsz, bool samepage)
Definition: brin_pageops.c:54
void brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:625
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:343
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:487
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:324
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:328
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:197
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:99
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:479
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:546
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:506
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:443
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:430
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:385
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:37
#define SizeOfBrinCreateIdx
Definition: brin_xlog.h:55
#define XLOG_BRIN_CREATE_INDEX
Definition: brin_xlog.h:31
int Buffer
Definition: buf.h:23
#define BufferIsInvalid(buffer)
Definition: buf.h:31
#define InvalidBuffer
Definition: buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2763
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3931
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3954
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1583
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4172
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:759
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:712
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:105
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:106
#define P_NEW
Definition: bufmgr.h:100
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:156
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:280
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:107
@ RBM_NORMAL
Definition: bufmgr.h:39
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:228
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:907
static char * PageGetContents(Page page)
Definition: bufpage.h:254
Pointer Page
Definition: bufpage.h:78
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
#define Min(x, y)
Definition: c.h:937
#define MAXALIGN(LEN)
Definition: c.h:747
signed int int32
Definition: c.h:430
#define lengthof(array)
Definition: c.h:724
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
#define OidIsValid(objectId)
Definition: c.h:711
size_t Size
Definition: c.h:541
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
int errhint(const char *fmt,...)
Definition: elog.c:1153
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define LOG
Definition: elog.h:27
#define DEBUG2
Definition: elog.h:25
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define palloc_object(type)
Definition: fe_memutils.h:62
#define palloc_array(type, count)
Definition: fe_memutils.h:64
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
#define palloc0_object(type)
Definition: fe_memutils.h:63
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1181
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1156
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:594
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:660
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:354
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:335
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:182
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition: genam.h:86
IndexUniqueCheck
Definition: genam.h:115
int NewGUCNestLevel(void)
Definition: guc.c:2183
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2197
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3522
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2420
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
Assert(fmt[strlen(fmt) - 1] !='\n')
#define AccessShareLock
Definition: lockdefs.h:36
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void pfree(void *pointer)
Definition: mcxt.c:1306
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
void * palloc(Size size)
Definition: mcxt.c:1199
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:70
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:305
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:618
Oid GetUserId(void)
Definition: miscinit.c:497
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:625
#define makeNode(_type_)
Definition: nodes.h:165
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
@ OBJECT_INDEX
Definition: parsenodes.h:1882
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
const void size_t len
static char * buf
Definition: pg_test_fsync.c:67
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:77
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:540
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
#define RelationGetRelid(relation)
Definition: rel.h:501
#define RelationGetDescr(relation)
Definition: rel.h:527
#define RelationGetRelationName(relation)
Definition: rel.h:535
#define RelationNeedsWAL(relation)
Definition: rel.h:626
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:1910
@ RELOPT_KIND_BRIN
Definition: reloptions.h:52
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
@ MAIN_FORKNUM
Definition: relpath.h:50
@ INIT_FORKNUM
Definition: relpath.h:53
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7761
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_SEARCHNULL
Definition: skey.h:121
#define SK_ISNULL
Definition: skey.h:115
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
BrinMemTuple * bs_dtuple
Definition: brin.c:58
Relation bs_irel
Definition: brin.c:51
BlockNumber bs_pagesPerRange
Definition: brin.c:54
Buffer bs_currentInsertBuf
Definition: brin.c:53
int bs_numtuples
Definition: brin.c:52
BrinRevmap * bs_rmAccess
Definition: brin.c:56
BlockNumber bs_currRangeStart
Definition: brin.c:55
BrinDesc * bs_bdesc
Definition: brin.c:57
int bd_totalstored
Definition: brin_internal.h:59
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bd_context
Definition: brin_internal.h:47
TupleDesc bd_disktdesc
Definition: brin_internal.h:56
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:54
bool bt_placeholder
Definition: brin_tuple.h:46
BlockNumber lastRevmapPage
Definition: brin_page.h:69
BlockNumber pagesPerRange
Definition: brin_page.h:68
BlockNumber bo_pagesPerRange
Definition: brin.c:66
BrinDesc * bo_bdesc
Definition: brin.c:68
BrinRevmap * bo_rmAccess
Definition: brin.c:67
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
uint16 oi_nstored
Definition: brin_internal.h:28
bool oi_regular_nulls
Definition: brin_internal.h:31
BlockNumber revmapNumPages
Definition: brin.h:34
BlockNumber pagesPerRange
Definition: brin.h:33
bool bv_hasnulls
Definition: brin_tuple.h:32
Datum * bv_values
Definition: brin_tuple.h:34
AttrNumber bv_attno
Definition: brin_tuple.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
Definition: fmgr.h:57
ambuildphasename_function ambuildphasename
Definition: amapi.h:268
ambuildempty_function ambuildempty
Definition: amapi.h:260
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:263
bool amclusterable
Definition: amapi.h:238
amoptions_function amoptions
Definition: amapi.h:266
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:280
amrestrpos_function amrestrpos
Definition: amapi.h:277
aminsert_function aminsert
Definition: amapi.h:261
amendscan_function amendscan
Definition: amapi.h:275
uint16 amoptsprocnum
Definition: amapi.h:218
amparallelrescan_function amparallelrescan
Definition: amapi.h:282
Oid amkeytype
Definition: amapi.h:250
bool ampredlocks
Definition: amapi.h:240
uint16 amsupport
Definition: amapi.h:216
amcostestimate_function amcostestimate
Definition: amapi.h:265
bool amcanorderbyop
Definition: amapi.h:222
amadjustmembers_function amadjustmembers
Definition: amapi.h:270
ambuild_function ambuild
Definition: amapi.h:259
bool amstorage
Definition: amapi.h:236
uint16 amstrategies
Definition: amapi.h:214
bool amoptionalkey
Definition: amapi.h:230
amgettuple_function amgettuple
Definition: amapi.h:273
amcanreturn_function amcanreturn
Definition: amapi.h:264
bool amcanunique
Definition: amapi.h:226
amgetbitmap_function amgetbitmap
Definition: amapi.h:274
amproperty_function amproperty
Definition: amapi.h:267
ambulkdelete_function ambulkdelete
Definition: amapi.h:262
bool amsearcharray
Definition: amapi.h:232
amvalidate_function amvalidate
Definition: amapi.h:269
ammarkpos_function ammarkpos
Definition: amapi.h:276
bool amcanmulticol
Definition: amapi.h:228
bool amusemaintenanceworkmem
Definition: amapi.h:246
ambeginscan_function ambeginscan
Definition: amapi.h:271
bool amcanparallel
Definition: amapi.h:242
amrescan_function amrescan
Definition: amapi.h:272
bool amcanorder
Definition: amapi.h:220
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:281
uint8 amparallelvacuumoptions
Definition: amapi.h:248
bool amcanbackward
Definition: amapi.h:224
bool amcaninclude
Definition: amapi.h:244
bool amsearchnulls
Definition: amapi.h:234
double heap_tuples
Definition: genam.h:32
double index_tuples
Definition: genam.h:33
BlockNumber num_pages
Definition: genam.h:76
double num_index_tuples
Definition: genam.h:78
void * ii_AmCache
Definition: execnodes.h:199
MemoryContext ii_Context
Definition: execnodes.h:200
struct ScanKeyData * keyData
Definition: relscan.h:122
Relation indexRelation
Definition: relscan.h:118
struct SnapshotData * xs_snapshot
Definition: relscan.h:119
Relation index
Definition: genam.h:46
bool analyze_only
Definition: genam.h:47
BufferAccessStrategy strategy
Definition: genam.h:52
Oid * rd_indcollation
Definition: rel.h:213
Form_pg_class rd_rel
Definition: rel.h:110
Oid sk_collation
Definition: skey.h:70
int tdrefcount
Definition: tupdesc.h:84
bool typbyval
Definition: typcache.h:40
int16 typlen
Definition: typcache.h:39
Definition: type.h:95
Definition: regguts.h:318
Definition: c.h:623
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
static double table_index_build_range_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool anyvisible, bool progress, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1781
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1748
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:442
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:62
bool RecoveryInProgress(void)
Definition: xlog.c:5912
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:351
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:451
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1191
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:243
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33