PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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/reloptions.h"
23 #include "access/relscan.h"
24 #include "access/xloginsert.h"
25 #include "catalog/index.h"
26 #include "catalog/pg_am.h"
27 #include "miscadmin.h"
28 #include "pgstat.h"
29 #include "postmaster/autovacuum.h"
30 #include "storage/bufmgr.h"
31 #include "storage/freespace.h"
32 #include "utils/builtins.h"
33 #include "utils/index_selfuncs.h"
34 #include "utils/memutils.h"
35 #include "utils/rel.h"
36 
37 
38 /*
39  * We use a BrinBuildState during initial construction of a BRIN index.
40  * The running state is kept in a BrinMemTuple.
41  */
42 typedef struct BrinBuildState
43 {
53 
54 /*
55  * Struct used as "opaque" during index scans
56  */
57 typedef struct BrinOpaque
58 {
62 } BrinOpaque;
63 
64 #define BRIN_ALL_BLOCKRANGES InvalidBlockNumber
65 
67  BrinRevmap *revmap, BlockNumber pagesPerRange);
69 static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange,
70  double *numSummarized, double *numExisting);
72 static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a,
73  BrinTuple *b);
74 static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy);
75 
76 
77 /*
78  * BRIN handler function: return IndexAmRoutine with access method parameters
79  * and callbacks.
80  */
81 Datum
83 {
85 
86  amroutine->amstrategies = 0;
88  amroutine->amcanorder = false;
89  amroutine->amcanorderbyop = false;
90  amroutine->amcanbackward = false;
91  amroutine->amcanunique = false;
92  amroutine->amcanmulticol = true;
93  amroutine->amoptionalkey = true;
94  amroutine->amsearcharray = false;
95  amroutine->amsearchnulls = true;
96  amroutine->amstorage = true;
97  amroutine->amclusterable = false;
98  amroutine->ampredlocks = false;
99  amroutine->amcanparallel = false;
100  amroutine->amkeytype = InvalidOid;
101 
102  amroutine->ambuild = brinbuild;
103  amroutine->ambuildempty = brinbuildempty;
104  amroutine->aminsert = brininsert;
105  amroutine->ambulkdelete = brinbulkdelete;
106  amroutine->amvacuumcleanup = brinvacuumcleanup;
107  amroutine->amcanreturn = NULL;
108  amroutine->amcostestimate = brincostestimate;
109  amroutine->amoptions = brinoptions;
110  amroutine->amproperty = NULL;
111  amroutine->amvalidate = brinvalidate;
112  amroutine->ambeginscan = brinbeginscan;
113  amroutine->amrescan = brinrescan;
114  amroutine->amgettuple = NULL;
115  amroutine->amgetbitmap = bringetbitmap;
116  amroutine->amendscan = brinendscan;
117  amroutine->ammarkpos = NULL;
118  amroutine->amrestrpos = NULL;
119  amroutine->amestimateparallelscan = NULL;
120  amroutine->aminitparallelscan = NULL;
121  amroutine->amparallelrescan = NULL;
122 
123  PG_RETURN_POINTER(amroutine);
124 }
125 
126 /*
127  * A tuple in the heap is being inserted. To keep a brin index up to date,
128  * we need to obtain the relevant index tuple and compare its stored values
129  * with those of the new tuple. If the tuple values are not consistent with
130  * the summary tuple, we need to update the index tuple.
131  *
132  * If autosummarization is enabled, check if we need to summarize the previous
133  * page range.
134  *
135  * If the range is not currently summarized (i.e. the revmap returns NULL for
136  * it), there's nothing to do for this tuple.
137  */
138 bool
139 brininsert(Relation idxRel, Datum *values, bool *nulls,
140  ItemPointer heaptid, Relation heapRel,
141  IndexUniqueCheck checkUnique,
142  IndexInfo *indexInfo)
143 {
144  BlockNumber pagesPerRange;
145  BlockNumber origHeapBlk;
146  BlockNumber heapBlk;
147  BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache;
148  BrinRevmap *revmap;
150  MemoryContext tupcxt = NULL;
152  bool autosummarize = BrinGetAutoSummarize(idxRel);
153 
154  revmap = brinRevmapInitialize(idxRel, &pagesPerRange, NULL);
155 
156  /*
157  * origHeapBlk is the block number where the insertion occurred. heapBlk
158  * is the first block in the corresponding page range.
159  */
160  origHeapBlk = ItemPointerGetBlockNumber(heaptid);
161  heapBlk = (origHeapBlk / pagesPerRange) * pagesPerRange;
162 
163  for (;;)
164  {
165  bool need_insert = false;
166  OffsetNumber off;
167  BrinTuple *brtup;
168  BrinMemTuple *dtup;
169  int keyno;
170 
172 
173  /*
174  * If auto-summarization is enabled and we just inserted the first
175  * tuple into the first block of a new non-first page range, request a
176  * summarization run of the previous range.
177  */
178  if (autosummarize &&
179  heapBlk > 0 &&
180  heapBlk == origHeapBlk &&
182  {
183  BlockNumber lastPageRange = heapBlk - 1;
184  BrinTuple *lastPageTuple;
185 
186  lastPageTuple =
187  brinGetTupleForHeapBlock(revmap, lastPageRange, &buf, &off,
189  if (!lastPageTuple)
191  RelationGetRelid(idxRel),
192  lastPageRange);
193  else
195  }
196 
197  brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,
199 
200  /* if range is unsummarized, there's nothing to do */
201  if (!brtup)
202  break;
203 
204  /* First time through in this statement? */
205  if (bdesc == NULL)
206  {
207  MemoryContextSwitchTo(indexInfo->ii_Context);
208  bdesc = brin_build_desc(idxRel);
209  indexInfo->ii_AmCache = (void *) bdesc;
210  MemoryContextSwitchTo(oldcxt);
211  }
212  /* First time through in this brininsert call? */
213  if (tupcxt == NULL)
214  {
216  "brininsert cxt",
218  MemoryContextSwitchTo(tupcxt);
219  }
220 
221  dtup = brin_deform_tuple(bdesc, brtup, NULL);
222 
223  /*
224  * Compare the key values of the new tuple to the stored index values;
225  * our deformed tuple will get updated if the new tuple doesn't fit
226  * the original range (note this means we can't break out of the loop
227  * early). Make a note of whether this happens, so that we know to
228  * insert the modified tuple later.
229  */
230  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
231  {
232  Datum result;
233  BrinValues *bval;
234  FmgrInfo *addValue;
235 
236  bval = &dtup->bt_columns[keyno];
237  addValue = index_getprocinfo(idxRel, keyno + 1,
239  result = FunctionCall4Coll(addValue,
240  idxRel->rd_indcollation[keyno],
241  PointerGetDatum(bdesc),
242  PointerGetDatum(bval),
243  values[keyno],
244  nulls[keyno]);
245  /* if that returned true, we need to insert the updated tuple */
246  need_insert |= DatumGetBool(result);
247  }
248 
249  if (!need_insert)
250  {
251  /*
252  * The tuple is consistent with the new values, so there's nothing
253  * to do.
254  */
256  }
257  else
258  {
259  Page page = BufferGetPage(buf);
260  ItemId lp = PageGetItemId(page, off);
261  Size origsz;
262  BrinTuple *origtup;
263  Size newsz;
264  BrinTuple *newtup;
265  bool samepage;
266 
267  /*
268  * Make a copy of the old tuple, so that we can compare it after
269  * re-acquiring the lock.
270  */
271  origsz = ItemIdGetLength(lp);
272  origtup = brin_copy_tuple(brtup, origsz, NULL, NULL);
273 
274  /*
275  * Before releasing the lock, check if we can attempt a same-page
276  * update. Another process could insert a tuple concurrently in
277  * the same page though, so downstream we must be prepared to cope
278  * if this turns out to not be possible after all.
279  */
280  newtup = brin_form_tuple(bdesc, heapBlk, dtup, &newsz);
281  samepage = brin_can_do_samepage_update(buf, origsz, newsz);
283 
284  /*
285  * Try to update the tuple. If this doesn't work for whatever
286  * reason, we need to restart from the top; the revmap might be
287  * pointing at a different tuple for this block now, so we need to
288  * recompute to ensure both our new heap tuple and the other
289  * inserter's are covered by the combined tuple. It might be that
290  * we don't need to update at all.
291  */
292  if (!brin_doupdate(idxRel, pagesPerRange, revmap, heapBlk,
293  buf, off, origtup, origsz, newtup, newsz,
294  samepage))
295  {
296  /* no luck; start over */
298  continue;
299  }
300  }
301 
302  /* success! */
303  break;
304  }
305 
306  brinRevmapTerminate(revmap);
307  if (BufferIsValid(buf))
309  MemoryContextSwitchTo(oldcxt);
310  if (tupcxt != NULL)
311  MemoryContextDelete(tupcxt);
312 
313  return false;
314 }
315 
316 /*
317  * Initialize state for a BRIN index scan.
318  *
319  * We read the metapage here to determine the pages-per-range number that this
320  * index was built with. Note that since this cannot be changed while we're
321  * holding lock on index, it's not necessary to recompute it during brinrescan.
322  */
324 brinbeginscan(Relation r, int nkeys, int norderbys)
325 {
326  IndexScanDesc scan;
327  BrinOpaque *opaque;
328 
329  scan = RelationGetIndexScan(r, nkeys, norderbys);
330 
331  opaque = (BrinOpaque *) palloc(sizeof(BrinOpaque));
332  opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
333  scan->xs_snapshot);
334  opaque->bo_bdesc = brin_build_desc(r);
335  scan->opaque = opaque;
336 
337  return scan;
338 }
339 
340 /*
341  * Execute the index scan.
342  *
343  * This works by reading index TIDs from the revmap, and obtaining the index
344  * tuples pointed to by them; the summary values in the index tuples are
345  * compared to the scan keys. We return into the TID bitmap all the pages in
346  * ranges corresponding to index tuples that match the scan keys.
347  *
348  * If a TID from the revmap is read as InvalidTID, we know that range is
349  * unsummarized. Pages in those ranges need to be returned regardless of scan
350  * keys.
351  */
352 int64
354 {
355  Relation idxRel = scan->indexRelation;
357  BrinDesc *bdesc;
358  Oid heapOid;
359  Relation heapRel;
360  BrinOpaque *opaque;
361  BlockNumber nblocks;
362  BlockNumber heapBlk;
363  int totalpages = 0;
364  FmgrInfo *consistentFn;
365  MemoryContext oldcxt;
366  MemoryContext perRangeCxt;
367  BrinMemTuple *dtup;
368  BrinTuple *btup = NULL;
369  Size btupsz = 0;
370 
371  opaque = (BrinOpaque *) scan->opaque;
372  bdesc = opaque->bo_bdesc;
373  pgstat_count_index_scan(idxRel);
374 
375  /*
376  * We need to know the size of the table so that we know how long to
377  * iterate on the revmap.
378  */
379  heapOid = IndexGetRelation(RelationGetRelid(idxRel), false);
380  heapRel = heap_open(heapOid, AccessShareLock);
381  nblocks = RelationGetNumberOfBlocks(heapRel);
382  heap_close(heapRel, AccessShareLock);
383 
384  /*
385  * Make room for the consistent support procedures of indexed columns. We
386  * don't look them up here; we do that lazily the first time we see a scan
387  * key reference each of them. We rely on zeroing fn_oid to InvalidOid.
388  */
389  consistentFn = palloc0(sizeof(FmgrInfo) * bdesc->bd_tupdesc->natts);
390 
391  /* allocate an initial in-memory tuple, out of the per-range memcxt */
392  dtup = brin_new_memtuple(bdesc);
393 
394  /*
395  * Setup and use a per-range memory context, which is reset every time we
396  * loop below. This avoids having to free the tuples within the loop.
397  */
399  "bringetbitmap cxt",
401  oldcxt = MemoryContextSwitchTo(perRangeCxt);
402 
403  /*
404  * Now scan the revmap. We start by querying for heap page 0,
405  * incrementing by the number of pages per range; this gives us a full
406  * view of the table.
407  */
408  for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
409  {
410  bool addrange;
411  bool gottuple = false;
412  BrinTuple *tup;
413  OffsetNumber off;
414  Size size;
415 
417 
419 
420  tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
421  &off, &size, BUFFER_LOCK_SHARE,
422  scan->xs_snapshot);
423  if (tup)
424  {
425  gottuple = true;
426  btup = brin_copy_tuple(tup, size, btup, &btupsz);
428  }
429 
430  /*
431  * For page ranges with no indexed tuple, we must return the whole
432  * range; otherwise, compare it to the scan keys.
433  */
434  if (!gottuple)
435  {
436  addrange = true;
437  }
438  else
439  {
440  dtup = brin_deform_tuple(bdesc, btup, dtup);
441  if (dtup->bt_placeholder)
442  {
443  /*
444  * Placeholder tuples are always returned, regardless of the
445  * values stored in them.
446  */
447  addrange = true;
448  }
449  else
450  {
451  int keyno;
452 
453  /*
454  * Compare scan keys with summary values stored for the range.
455  * If scan keys are matched, the page range must be added to
456  * the bitmap. We initially assume the range needs to be
457  * added; in particular this serves the case where there are
458  * no keys.
459  */
460  addrange = true;
461  for (keyno = 0; keyno < scan->numberOfKeys; keyno++)
462  {
463  ScanKey key = &scan->keyData[keyno];
464  AttrNumber keyattno = key->sk_attno;
465  BrinValues *bval = &dtup->bt_columns[keyattno - 1];
466  Datum add;
467 
468  /*
469  * The collation of the scan key must match the collation
470  * used in the index column (but only if the search is not
471  * IS NULL/ IS NOT NULL). Otherwise we shouldn't be using
472  * this index ...
473  */
474  Assert((key->sk_flags & SK_ISNULL) ||
475  (key->sk_collation ==
476  bdesc->bd_tupdesc->attrs[keyattno - 1]->attcollation));
477 
478  /* First time this column? look up consistent function */
479  if (consistentFn[keyattno - 1].fn_oid == InvalidOid)
480  {
481  FmgrInfo *tmp;
482 
483  tmp = index_getprocinfo(idxRel, keyattno,
485  fmgr_info_copy(&consistentFn[keyattno - 1], tmp,
487  }
488 
489  /*
490  * Check whether the scan key is consistent with the page
491  * range values; if so, have the pages in the range added
492  * to the output bitmap.
493  *
494  * When there are multiple scan keys, failure to meet the
495  * criteria for a single one of them is enough to discard
496  * the range as a whole, so break out of the loop as soon
497  * as a false return value is obtained.
498  */
499  add = FunctionCall3Coll(&consistentFn[keyattno - 1],
500  key->sk_collation,
501  PointerGetDatum(bdesc),
502  PointerGetDatum(bval),
503  PointerGetDatum(key));
504  addrange = DatumGetBool(add);
505  if (!addrange)
506  break;
507  }
508  }
509  }
510 
511  /* add the pages in the range to the output bitmap, if needed */
512  if (addrange)
513  {
514  BlockNumber pageno;
515 
516  for (pageno = heapBlk;
517  pageno <= heapBlk + opaque->bo_pagesPerRange - 1;
518  pageno++)
519  {
520  MemoryContextSwitchTo(oldcxt);
521  tbm_add_page(tbm, pageno);
522  totalpages++;
523  MemoryContextSwitchTo(perRangeCxt);
524  }
525  }
526  }
527 
528  MemoryContextSwitchTo(oldcxt);
529  MemoryContextDelete(perRangeCxt);
530 
531  if (buf != InvalidBuffer)
532  ReleaseBuffer(buf);
533 
534  /*
535  * XXX We have an approximation of the number of *pages* that our scan
536  * returns, but we don't have a precise idea of the number of heap tuples
537  * involved.
538  */
539  return totalpages * 10;
540 }
541 
542 /*
543  * Re-initialize state for a BRIN index scan
544  */
545 void
546 brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
547  ScanKey orderbys, int norderbys)
548 {
549  /*
550  * Other index AMs preprocess the scan keys at this point, or sometime
551  * early during the scan; this lets them optimize by removing redundant
552  * keys, or doing early returns when they are impossible to satisfy; see
553  * _bt_preprocess_keys for an example. Something like that could be added
554  * here someday, too.
555  */
556 
557  if (scankey && scan->numberOfKeys > 0)
558  memmove(scan->keyData, scankey,
559  scan->numberOfKeys * sizeof(ScanKeyData));
560 }
561 
562 /*
563  * Close down a BRIN index scan
564  */
565 void
567 {
568  BrinOpaque *opaque = (BrinOpaque *) scan->opaque;
569 
571  brin_free_desc(opaque->bo_bdesc);
572  pfree(opaque);
573 }
574 
575 /*
576  * Per-heap-tuple callback for IndexBuildHeapScan.
577  *
578  * Note we don't worry about the page range at the end of the table here; it is
579  * present in the build state struct after we're called the last time, but not
580  * inserted into the index. Caller must ensure to do so, if appropriate.
581  */
582 static void
584  HeapTuple htup,
585  Datum *values,
586  bool *isnull,
587  bool tupleIsAlive,
588  void *brstate)
589 {
590  BrinBuildState *state = (BrinBuildState *) brstate;
591  BlockNumber thisblock;
592  int i;
593 
594  thisblock = ItemPointerGetBlockNumber(&htup->t_self);
595 
596  /*
597  * If we're in a block that belongs to a future range, summarize what
598  * we've got and start afresh. Note the scan might have skipped many
599  * pages, if they were devoid of live tuples; make sure to insert index
600  * tuples for those too.
601  */
602  while (thisblock > state->bs_currRangeStart + state->bs_pagesPerRange - 1)
603  {
604 
605  BRIN_elog((DEBUG2,
606  "brinbuildCallback: completed a range: %u--%u",
607  state->bs_currRangeStart,
608  state->bs_currRangeStart + state->bs_pagesPerRange));
609 
610  /* create the index tuple and insert it */
611  form_and_insert_tuple(state);
612 
613  /* set state to correspond to the next range */
614  state->bs_currRangeStart += state->bs_pagesPerRange;
615 
616  /* re-initialize state for it */
618  }
619 
620  /* Accumulate the current tuple into the running state */
621  for (i = 0; i < state->bs_bdesc->bd_tupdesc->natts; i++)
622  {
623  FmgrInfo *addValue;
624  BrinValues *col;
625 
626  col = &state->bs_dtuple->bt_columns[i];
627  addValue = index_getprocinfo(index, i + 1,
629 
630  /*
631  * Update dtuple state, if and as necessary.
632  */
633  FunctionCall4Coll(addValue,
634  state->bs_bdesc->bd_tupdesc->attrs[i]->attcollation,
635  PointerGetDatum(state->bs_bdesc),
636  PointerGetDatum(col),
637  values[i], isnull[i]);
638  }
639 }
640 
641 /*
642  * brinbuild() -- build a new BRIN index.
643  */
646 {
648  double reltuples;
649  double idxtuples;
650  BrinRevmap *revmap;
652  Buffer meta;
653  BlockNumber pagesPerRange;
654 
655  /*
656  * We expect to be called exactly once for any index relation.
657  */
658  if (RelationGetNumberOfBlocks(index) != 0)
659  elog(ERROR, "index \"%s\" already contains data",
660  RelationGetRelationName(index));
661 
662  /*
663  * Critical section not required, because on error the creation of the
664  * whole relation will be rolled back.
665  */
666 
667  meta = ReadBuffer(index, P_NEW);
670 
673  MarkBufferDirty(meta);
674 
675  if (RelationNeedsWAL(index))
676  {
677  xl_brin_createidx xlrec;
678  XLogRecPtr recptr;
679  Page page;
680 
682  xlrec.pagesPerRange = BrinGetPagesPerRange(index);
683 
684  XLogBeginInsert();
685  XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
687 
688  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
689 
690  page = BufferGetPage(meta);
691  PageSetLSN(page, recptr);
692  }
693 
694  UnlockReleaseBuffer(meta);
695 
696  /*
697  * Initialize our state, including the deformed tuple state.
698  */
699  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
700  state = initialize_brin_buildstate(index, revmap, pagesPerRange);
701 
702  /*
703  * Now scan the relation. No syncscan allowed here because we want the
704  * heap blocks in physical order.
705  */
706  reltuples = IndexBuildHeapScan(heap, index, indexInfo, false,
707  brinbuildCallback, (void *) state);
708 
709  /* process the final batch */
710  form_and_insert_tuple(state);
711 
712  /* release resources */
713  idxtuples = state->bs_numtuples;
716 
717  /*
718  * Return statistics
719  */
720  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
721 
722  result->heap_tuples = reltuples;
723  result->index_tuples = idxtuples;
724 
725  return result;
726 }
727 
728 void
730 {
731  Buffer metabuf;
732 
733  /* An empty BRIN index has a metapage only. */
734  metabuf =
737 
738  /* Initialize and xlog metabuffer. */
742  MarkBufferDirty(metabuf);
743  log_newpage_buffer(metabuf, false);
745 
746  UnlockReleaseBuffer(metabuf);
747 }
748 
749 /*
750  * brinbulkdelete
751  * Since there are no per-heap-tuple index tuples in BRIN indexes,
752  * there's not a lot we can do here.
753  *
754  * XXX we could mark item tuples as "dirty" (when a minimum or maximum heap
755  * tuple is deleted), meaning the need to re-run summarization on the affected
756  * range. Would need to add an extra flag in brintuples for that.
757  */
760  IndexBulkDeleteCallback callback, void *callback_state)
761 {
762  /* allocate stats if first time through, else re-use existing struct */
763  if (stats == NULL)
765 
766  return stats;
767 }
768 
769 /*
770  * This routine is in charge of "vacuuming" a BRIN index: we just summarize
771  * ranges that are currently unsummarized.
772  */
775 {
776  Relation heapRel;
777 
778  /* No-op in ANALYZE ONLY mode */
779  if (info->analyze_only)
780  return stats;
781 
782  if (!stats)
784  stats->num_pages = RelationGetNumberOfBlocks(info->index);
785  /* rest of stats is initialized by zeroing */
786 
787  heapRel = heap_open(IndexGetRelation(RelationGetRelid(info->index), false),
789 
790  brin_vacuum_scan(info->index, info->strategy);
791 
792  brinsummarize(info->index, heapRel, BRIN_ALL_BLOCKRANGES,
793  &stats->num_index_tuples, &stats->num_index_tuples);
794 
795  heap_close(heapRel, AccessShareLock);
796 
797  return stats;
798 }
799 
800 /*
801  * reloptions processor for BRIN indexes
802  */
803 bytea *
804 brinoptions(Datum reloptions, bool validate)
805 {
807  BrinOptions *rdopts;
808  int numoptions;
809  static const relopt_parse_elt tab[] = {
810  {"pages_per_range", RELOPT_TYPE_INT, offsetof(BrinOptions, pagesPerRange)},
811  {"autosummarize", RELOPT_TYPE_BOOL, offsetof(BrinOptions, autosummarize)}
812  };
813 
814  options = parseRelOptions(reloptions, validate, RELOPT_KIND_BRIN,
815  &numoptions);
816 
817  /* if none set, we're done */
818  if (numoptions == 0)
819  return NULL;
820 
821  rdopts = allocateReloptStruct(sizeof(BrinOptions), options, numoptions);
822 
823  fillRelOptions((void *) rdopts, sizeof(BrinOptions), options, numoptions,
824  validate, tab, lengthof(tab));
825 
826  pfree(options);
827 
828  return (bytea *) rdopts;
829 }
830 
831 /*
832  * SQL-callable function to scan through an index and summarize all ranges
833  * that are not currently summarized.
834  */
835 Datum
837 {
838  Datum relation = PG_GETARG_DATUM(0);
839 
841  relation,
843 }
844 
845 /*
846  * SQL-callable function to summarize the indicated page range, if not already
847  * summarized. If the second argument is BRIN_ALL_BLOCKRANGES, all
848  * unsummarized ranges are summarized.
849  */
850 Datum
852 {
853  Oid indexoid = PG_GETARG_OID(0);
854  int64 heapBlk64 = PG_GETARG_INT64(1);
855  BlockNumber heapBlk;
856  Oid heapoid;
857  Relation indexRel;
858  Relation heapRel;
859  double numSummarized = 0;
860 
861  if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
862  {
863  char *blk = psprintf(INT64_FORMAT, heapBlk64);
864 
865  ereport(ERROR,
866  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
867  errmsg("block number out of range: %s", blk)));
868  }
869  heapBlk = (BlockNumber) heapBlk64;
870 
871  /*
872  * We must lock table before index to avoid deadlocks. However, if the
873  * passed indexoid isn't an index then IndexGetRelation() will fail.
874  * Rather than emitting a not-very-helpful error message, postpone
875  * complaining, expecting that the is-it-an-index test below will fail.
876  */
877  heapoid = IndexGetRelation(indexoid, true);
878  if (OidIsValid(heapoid))
879  heapRel = heap_open(heapoid, ShareUpdateExclusiveLock);
880  else
881  heapRel = NULL;
882 
883  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
884 
885  /* Must be a BRIN index */
886  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
887  indexRel->rd_rel->relam != BRIN_AM_OID)
888  ereport(ERROR,
889  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
890  errmsg("\"%s\" is not a BRIN index",
891  RelationGetRelationName(indexRel))));
892 
893  /* User must own the index (comparable to privileges needed for VACUUM) */
894  if (!pg_class_ownercheck(indexoid, GetUserId()))
896  RelationGetRelationName(indexRel));
897 
898  /*
899  * Since we did the IndexGetRelation call above without any lock, it's
900  * barely possible that a race against an index drop/recreation could have
901  * netted us the wrong table. Recheck.
902  */
903  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
904  ereport(ERROR,
906  errmsg("could not open parent table of index %s",
907  RelationGetRelationName(indexRel))));
908 
909  /* OK, do it */
910  brinsummarize(indexRel, heapRel, heapBlk, &numSummarized, NULL);
911 
914 
915  PG_RETURN_INT32((int32) numSummarized);
916 }
917 
918 /*
919  * SQL-callable interface to mark a range as no longer summarized
920  */
921 Datum
923 {
924  Oid indexoid = PG_GETARG_OID(0);
925  int64 heapBlk64 = PG_GETARG_INT64(1);
926  BlockNumber heapBlk;
927  Oid heapoid;
928  Relation heapRel;
929  Relation indexRel;
930  bool done;
931 
932  if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
933  {
934  char *blk = psprintf(INT64_FORMAT, heapBlk64);
935 
936  ereport(ERROR,
937  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
938  errmsg("block number out of range: %s", blk)));
939  }
940  heapBlk = (BlockNumber) heapBlk64;
941 
942  /*
943  * We must lock table before index to avoid deadlocks. However, if the
944  * passed indexoid isn't an index then IndexGetRelation() will fail.
945  * Rather than emitting a not-very-helpful error message, postpone
946  * complaining, expecting that the is-it-an-index test below will fail.
947  */
948  heapoid = IndexGetRelation(indexoid, true);
949  if (OidIsValid(heapoid))
950  heapRel = heap_open(heapoid, ShareUpdateExclusiveLock);
951  else
952  heapRel = NULL;
953 
954  indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
955 
956  /* Must be a BRIN index */
957  if (indexRel->rd_rel->relkind != RELKIND_INDEX ||
958  indexRel->rd_rel->relam != BRIN_AM_OID)
959  ereport(ERROR,
960  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
961  errmsg("\"%s\" is not a BRIN index",
962  RelationGetRelationName(indexRel))));
963 
964  /* User must own the index (comparable to privileges needed for VACUUM) */
965  if (!pg_class_ownercheck(indexoid, GetUserId()))
967  RelationGetRelationName(indexRel));
968 
969  /*
970  * Since we did the IndexGetRelation call above without any lock, it's
971  * barely possible that a race against an index drop/recreation could have
972  * netted us the wrong table. Recheck.
973  */
974  if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
975  ereport(ERROR,
977  errmsg("could not open parent table of index %s",
978  RelationGetRelationName(indexRel))));
979 
980  /* the revmap does the hard work */
981  do
982  {
983  done = brinRevmapDesummarizeRange(indexRel, heapBlk);
984  }
985  while (!done);
986 
989 
990  PG_RETURN_VOID();
991 }
992 
993 /*
994  * Build a BrinDesc used to create or scan a BRIN index
995  */
996 BrinDesc *
998 {
999  BrinOpcInfo **opcinfo;
1000  BrinDesc *bdesc;
1001  TupleDesc tupdesc;
1002  int totalstored = 0;
1003  int keyno;
1004  long totalsize;
1005  MemoryContext cxt;
1006  MemoryContext oldcxt;
1007 
1009  "brin desc cxt",
1011  oldcxt = MemoryContextSwitchTo(cxt);
1012  tupdesc = RelationGetDescr(rel);
1013 
1014  /*
1015  * Obtain BrinOpcInfo for each indexed column. While at it, accumulate
1016  * the number of columns stored, since the number is opclass-defined.
1017  */
1018  opcinfo = (BrinOpcInfo **) palloc(sizeof(BrinOpcInfo *) * tupdesc->natts);
1019  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1020  {
1021  FmgrInfo *opcInfoFn;
1022 
1023  opcInfoFn = index_getprocinfo(rel, keyno + 1, BRIN_PROCNUM_OPCINFO);
1024 
1025  opcinfo[keyno] = (BrinOpcInfo *)
1026  DatumGetPointer(FunctionCall1(opcInfoFn,
1027  tupdesc->attrs[keyno]->atttypid));
1028  totalstored += opcinfo[keyno]->oi_nstored;
1029  }
1030 
1031  /* Allocate our result struct and fill it in */
1032  totalsize = offsetof(BrinDesc, bd_info) +
1033  sizeof(BrinOpcInfo *) * tupdesc->natts;
1034 
1035  bdesc = palloc(totalsize);
1036  bdesc->bd_context = cxt;
1037  bdesc->bd_index = rel;
1038  bdesc->bd_tupdesc = tupdesc;
1039  bdesc->bd_disktdesc = NULL; /* generated lazily */
1040  bdesc->bd_totalstored = totalstored;
1041 
1042  for (keyno = 0; keyno < tupdesc->natts; keyno++)
1043  bdesc->bd_info[keyno] = opcinfo[keyno];
1044  pfree(opcinfo);
1045 
1046  MemoryContextSwitchTo(oldcxt);
1047 
1048  return bdesc;
1049 }
1050 
1051 void
1053 {
1054  /* make sure the tupdesc is still valid */
1055  Assert(bdesc->bd_tupdesc->tdrefcount >= 1);
1056  /* no need for retail pfree */
1058 }
1059 
1060 /*
1061  * Fetch index's statistical data into *stats
1062  */
1063 void
1065 {
1066  Buffer metabuffer;
1067  Page metapage;
1068  BrinMetaPageData *metadata;
1069 
1070  metabuffer = ReadBuffer(index, BRIN_METAPAGE_BLKNO);
1071  LockBuffer(metabuffer, BUFFER_LOCK_SHARE);
1072  metapage = BufferGetPage(metabuffer);
1073  metadata = (BrinMetaPageData *) PageGetContents(metapage);
1074 
1075  stats->pagesPerRange = metadata->pagesPerRange;
1076  stats->revmapNumPages = metadata->lastRevmapPage - 1;
1077 
1078  UnlockReleaseBuffer(metabuffer);
1079 }
1080 
1081 /*
1082  * Initialize a BrinBuildState appropriate to create tuples on the given index.
1083  */
1084 static BrinBuildState *
1086  BlockNumber pagesPerRange)
1087 {
1089 
1090  state = palloc(sizeof(BrinBuildState));
1091 
1092  state->bs_irel = idxRel;
1093  state->bs_numtuples = 0;
1095  state->bs_pagesPerRange = pagesPerRange;
1096  state->bs_currRangeStart = 0;
1097  state->bs_rmAccess = revmap;
1098  state->bs_bdesc = brin_build_desc(idxRel);
1099  state->bs_dtuple = brin_new_memtuple(state->bs_bdesc);
1100 
1102 
1103  return state;
1104 }
1105 
1106 /*
1107  * Release resources associated with a BrinBuildState.
1108  */
1109 static void
1111 {
1112  /* release the last index buffer used */
1113  if (!BufferIsInvalid(state->bs_currentInsertBuf))
1114  {
1115  Page page;
1116 
1117  page = BufferGetPage(state->bs_currentInsertBuf);
1120  PageGetFreeSpace(page));
1122  }
1123 
1124  brin_free_desc(state->bs_bdesc);
1125  pfree(state->bs_dtuple);
1126  pfree(state);
1127 }
1128 
1129 /*
1130  * Summarize the given page range of the given index.
1131  *
1132  * This routine can run in parallel with insertions into the heap. To avoid
1133  * missing those values from the summary tuple, we first insert a placeholder
1134  * index tuple into the index, then execute the heap scan; transactions
1135  * concurrent with the scan update the placeholder tuple. After the scan, we
1136  * union the placeholder tuple with the one computed by this routine. The
1137  * update of the index value happens in a loop, so that if somebody updates
1138  * the placeholder tuple after we read it, we detect the case and try again.
1139  * This ensures that the concurrently inserted tuples are not lost.
1140  */
1141 static void
1143  BlockNumber heapBlk, BlockNumber heapNumBlks)
1144 {
1145  Buffer phbuf;
1146  BrinTuple *phtup;
1147  Size phsz;
1148  OffsetNumber offset;
1149  BlockNumber scanNumBlks;
1150 
1151  /*
1152  * Insert the placeholder tuple
1153  */
1154  phbuf = InvalidBuffer;
1155  phtup = brin_form_placeholder_tuple(state->bs_bdesc, heapBlk, &phsz);
1156  offset = brin_doinsert(state->bs_irel, state->bs_pagesPerRange,
1157  state->bs_rmAccess, &phbuf,
1158  heapBlk, phtup, phsz);
1159 
1160  /*
1161  * Execute the partial heap scan covering the heap blocks in the specified
1162  * page range, summarizing the heap tuples in it. This scan stops just
1163  * short of brinbuildCallback creating the new index entry.
1164  *
1165  * Note that it is critical we use the "any visible" mode of
1166  * IndexBuildHeapRangeScan here: otherwise, we would miss tuples inserted
1167  * by transactions that are still in progress, among other corner cases.
1168  */
1169  state->bs_currRangeStart = heapBlk;
1170  scanNumBlks = heapBlk + state->bs_pagesPerRange <= heapNumBlks ?
1171  state->bs_pagesPerRange : heapNumBlks - heapBlk;
1172  IndexBuildHeapRangeScan(heapRel, state->bs_irel, indexInfo, false, true,
1173  heapBlk, scanNumBlks,
1174  brinbuildCallback, (void *) state);
1175 
1176  /*
1177  * Now we update the values obtained by the scan with the placeholder
1178  * tuple. We do this in a loop which only terminates if we're able to
1179  * update the placeholder tuple successfully; if we are not, this means
1180  * somebody else modified the placeholder tuple after we read it.
1181  */
1182  for (;;)
1183  {
1184  BrinTuple *newtup;
1185  Size newsize;
1186  bool didupdate;
1187  bool samepage;
1188 
1190 
1191  /*
1192  * Update the summary tuple and try to update.
1193  */
1194  newtup = brin_form_tuple(state->bs_bdesc,
1195  heapBlk, state->bs_dtuple, &newsize);
1196  samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
1197  didupdate =
1198  brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
1199  state->bs_rmAccess, heapBlk, phbuf, offset,
1200  phtup, phsz, newtup, newsize, samepage);
1201  brin_free_tuple(phtup);
1202  brin_free_tuple(newtup);
1203 
1204  /* If the update succeeded, we're done. */
1205  if (didupdate)
1206  break;
1207 
1208  /*
1209  * If the update didn't work, it might be because somebody updated the
1210  * placeholder tuple concurrently. Extract the new version, union it
1211  * with the values we have from the scan, and start over. (There are
1212  * other reasons for the update to fail, but it's simple to treat them
1213  * the same.)
1214  */
1215  phtup = brinGetTupleForHeapBlock(state->bs_rmAccess, heapBlk, &phbuf,
1216  &offset, &phsz, BUFFER_LOCK_SHARE,
1217  NULL);
1218  /* the placeholder tuple must exist */
1219  if (phtup == NULL)
1220  elog(ERROR, "missing placeholder tuple");
1221  phtup = brin_copy_tuple(phtup, phsz, NULL, NULL);
1223 
1224  /* merge it into the tuple from the heap scan */
1225  union_tuples(state->bs_bdesc, state->bs_dtuple, phtup);
1226  }
1227 
1228  ReleaseBuffer(phbuf);
1229 }
1230 
1231 /*
1232  * Summarize page ranges that are not already summarized. If pageRange is
1233  * BRIN_ALL_BLOCKRANGES then the whole table is scanned; otherwise, only the
1234  * page range containing the given heap page number is scanned.
1235  *
1236  * For each new index tuple inserted, *numSummarized (if not NULL) is
1237  * incremented; for each existing tuple, *numExisting (if not NULL) is
1238  * incremented.
1239  */
1240 static void
1242  double *numSummarized, double *numExisting)
1243 {
1244  BrinRevmap *revmap;
1246  IndexInfo *indexInfo = NULL;
1247  BlockNumber heapNumBlocks;
1248  BlockNumber heapBlk;
1249  BlockNumber pagesPerRange;
1250  Buffer buf;
1251  BlockNumber startBlk;
1252  BlockNumber endBlk;
1253 
1254  /* determine range of pages to process; nothing to do for an empty table */
1255  heapNumBlocks = RelationGetNumberOfBlocks(heapRel);
1256  if (heapNumBlocks == 0)
1257  return;
1258 
1259  revmap = brinRevmapInitialize(index, &pagesPerRange, NULL);
1260 
1261  if (pageRange == BRIN_ALL_BLOCKRANGES)
1262  {
1263  startBlk = 0;
1264  endBlk = heapNumBlocks;
1265  }
1266  else
1267  {
1268  startBlk = (pageRange / pagesPerRange) * pagesPerRange;
1269  /* Nothing to do if start point is beyond end of table */
1270  if (startBlk > heapNumBlocks)
1271  {
1272  brinRevmapTerminate(revmap);
1273  return;
1274  }
1275  endBlk = startBlk + pagesPerRange;
1276  if (endBlk > heapNumBlocks)
1277  endBlk = heapNumBlocks;
1278  }
1279 
1280  /*
1281  * Scan the revmap to find unsummarized items.
1282  */
1283  buf = InvalidBuffer;
1284  for (heapBlk = startBlk; heapBlk < endBlk; heapBlk += pagesPerRange)
1285  {
1286  BrinTuple *tup;
1287  OffsetNumber off;
1288 
1290 
1291  tup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off, NULL,
1293  if (tup == NULL)
1294  {
1295  /* no revmap entry for this heap range. Summarize it. */
1296  if (state == NULL)
1297  {
1298  /* first time through */
1299  Assert(!indexInfo);
1300  state = initialize_brin_buildstate(index, revmap,
1301  pagesPerRange);
1302  indexInfo = BuildIndexInfo(index);
1303  }
1304  summarize_range(indexInfo, state, heapRel, heapBlk, heapNumBlocks);
1305 
1306  /* and re-initialize state for the next range */
1308 
1309  if (numSummarized)
1310  *numSummarized += 1.0;
1311  }
1312  else
1313  {
1314  if (numExisting)
1315  *numExisting += 1.0;
1317  }
1318  }
1319 
1320  if (BufferIsValid(buf))
1321  ReleaseBuffer(buf);
1322 
1323  /* free resources */
1324  brinRevmapTerminate(revmap);
1325  if (state)
1326  {
1328  pfree(indexInfo);
1329  }
1330 }
1331 
1332 /*
1333  * Given a deformed tuple in the build state, convert it into the on-disk
1334  * format and insert it into the index, making the revmap point to it.
1335  */
1336 static void
1338 {
1339  BrinTuple *tup;
1340  Size size;
1341 
1342  tup = brin_form_tuple(state->bs_bdesc, state->bs_currRangeStart,
1343  state->bs_dtuple, &size);
1344  brin_doinsert(state->bs_irel, state->bs_pagesPerRange, state->bs_rmAccess,
1345  &state->bs_currentInsertBuf, state->bs_currRangeStart,
1346  tup, size);
1347  state->bs_numtuples++;
1348 
1349  pfree(tup);
1350 }
1351 
1352 /*
1353  * Given two deformed tuples, adjust the first one so that it's consistent
1354  * with the summary values in both.
1355  */
1356 static void
1358 {
1359  int keyno;
1360  BrinMemTuple *db;
1361  MemoryContext cxt;
1362  MemoryContext oldcxt;
1363 
1364  /* Use our own memory context to avoid retail pfree */
1366  "brin union",
1368  oldcxt = MemoryContextSwitchTo(cxt);
1369  db = brin_deform_tuple(bdesc, b, NULL);
1370  MemoryContextSwitchTo(oldcxt);
1371 
1372  for (keyno = 0; keyno < bdesc->bd_tupdesc->natts; keyno++)
1373  {
1374  FmgrInfo *unionFn;
1375  BrinValues *col_a = &a->bt_columns[keyno];
1376  BrinValues *col_b = &db->bt_columns[keyno];
1377 
1378  unionFn = index_getprocinfo(bdesc->bd_index, keyno + 1,
1380  FunctionCall3Coll(unionFn,
1381  bdesc->bd_index->rd_indcollation[keyno],
1382  PointerGetDatum(bdesc),
1383  PointerGetDatum(col_a),
1384  PointerGetDatum(col_b));
1385  }
1386 
1387  MemoryContextDelete(cxt);
1388 }
1389 
1390 /*
1391  * brin_vacuum_scan
1392  * Do a complete scan of the index during VACUUM.
1393  *
1394  * This routine scans the complete index looking for uncatalogued index pages,
1395  * i.e. those that might have been lost due to a crash after index extension
1396  * and such.
1397  */
1398 static void
1400 {
1401  bool vacuum_fsm = false;
1402  BlockNumber blkno;
1403 
1404  /*
1405  * Scan the index in physical order, and clean up any possible mess in
1406  * each page.
1407  */
1408  for (blkno = 0; blkno < RelationGetNumberOfBlocks(idxrel); blkno++)
1409  {
1410  Buffer buf;
1411 
1413 
1414  buf = ReadBufferExtended(idxrel, MAIN_FORKNUM, blkno,
1415  RBM_NORMAL, strategy);
1416 
1417  vacuum_fsm |= brin_page_cleanup(idxrel, buf);
1418 
1419  ReleaseBuffer(buf);
1420  }
1421 
1422  /*
1423  * If we made any change to the FSM, make sure the new info is visible all
1424  * the way to the top.
1425  */
1426  if (vacuum_fsm)
1427  FreeSpaceMapVacuum(idxrel);
1428 }
void brin_free_desc(BrinDesc *bdesc)
Definition: brin.c:1052
IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: brin.c:759
ambeginscan_function ambeginscan
Definition: amapi.h:208
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
MemoryContext ii_Context
Definition: execnodes.h:151
Definition: fmgr.h:56
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3278
ambulkdelete_function ambulkdelete
Definition: amapi.h:201
#define BRIN_CURRENT_VERSION
Definition: brin_page.h:72
bool amcanmulticol
Definition: amapi.h:179
uint16 amsupport
Definition: amapi.h:169
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define BRIN_elog(args)
Definition: brin_internal.h:81
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
IndexBuildResult * brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: brin.c:645
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
bool brininsert(Relation idxRel, Datum *values, bool *nulls, ItemPointer heaptid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: brin.c:139
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:321
#define SizeOfBrinCreateIdx
Definition: brin_xlog.h:55
amgettuple_function amgettuple
Definition: amapi.h:210
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
#define BRIN_AM_OID
Definition: pg_am.h:85
#define RelationGetDescr(relation)
Definition: rel.h:428
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:103
Oid GetUserId(void)
Definition: miscinit.c:284
bool amcanorderbyop
Definition: amapi.h:173
static void union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
Definition: brin.c:1357
amproperty_function amproperty
Definition: amapi.h:206
BrinTuple * brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
Definition: brin_revmap.c:197
bool brinvalidate(Oid opclassoid)
Definition: brin_validate.c:38
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
static void addrange(struct cvec *cv, chr from, chr to)
Definition: regc_cvec.c:90
Buffer bs_currentInsertBuf
Definition: brin.c:46
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
static void summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel, BlockNumber heapBlk, BlockNumber heapNumBlks)
Definition: brin.c:1142
bool analyze_only
Definition: genam.h:47
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
amparallelrescan_function amparallelrescan
Definition: amapi.h:219
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
BufferAccessStrategy strategy
Definition: genam.h:51
bool amstorage
Definition: amapi.h:187
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define XLOG_BRIN_CREATE_INDEX
Definition: brin_xlog.h:31
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define AccessShareLock
Definition: lockdefs.h:36
Snapshot xs_snapshot
Definition: relscan.h:91
#define InvalidBuffer
Definition: buf.h:25
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define BRIN_PROCNUM_OPCINFO
Definition: brin_internal.h:67
int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: brin.c:353
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
int errcode(int sqlerrcode)
Definition: elog.c:575
Relation index
Definition: genam.h:46
static void brinbuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *brstate)
Definition: brin.c:583
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
bool ampredlocks
Definition: amapi.h:191
return result
Definition: formatting.c:1633
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1094
uint32 BlockNumber
Definition: block.h:31
static void form_and_insert_tuple(BrinBuildState *state)
Definition: brin.c:1337
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
#define P_NEW
Definition: bufmgr.h:82
#define heap_close(r, l)
Definition: heapam.h:97
aminsert_function aminsert
Definition: amapi.h:200
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:1639
#define lengthof(array)
Definition: c.h:562
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
bool brin_doupdate(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, BlockNumber heapBlk, Buffer oldbuf, OffsetNumber oldoff, const BrinTuple *origtup, Size origsz, const BrinTuple *newtup, Size newsz, bool samepage)
Definition: brin_pageops.c:55
BrinMemTuple * brin_new_memtuple(BrinDesc *brdesc)
Definition: brin_tuple.c:357
Oid amkeytype
Definition: amapi.h:195
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
#define OidIsValid(objectId)
Definition: c.h:538
Relation bs_irel
Definition: brin.c:44
bool amoptionalkey
Definition: amapi.h:181
BlockNumber bs_currRangeStart
Definition: brin.c:48
void brinendscan(IndexScanDesc scan)
Definition: brin.c:566
amvalidate_function amvalidate
Definition: amapi.h:207
int natts
Definition: tupdesc.h:73
BlockNumber bo_pagesPerRange
Definition: brin.c:59
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
signed int int32
Definition: c.h:256
Relation indexRelation
Definition: relscan.h:90
bytea * brinoptions(Datum reloptions, bool validate)
Definition: brin.c:804
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:89
OffsetNumber brin_doinsert(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
Definition: brin_pageops.c:340
Datum brin_summarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:851
BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
Definition: brin_tuple.c:422
IndexUniqueCheck
Definition: genam.h:111
struct BrinOpaque BrinOpaque
#define BRIN_PROCNUM_ADDVALUE
Definition: brin_internal.h:68
BlockNumber bs_pagesPerRange
Definition: brin.c:47
void pfree(void *pointer)
Definition: mcxt.c:950
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
amgetbitmap_function amgetbitmap
Definition: amapi.h:211
void brin_metapage_init(Page page, BlockNumber pagesPerRange, uint16 version)
Definition: brin_pageops.c:476
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
bool bt_placeholder
Definition: brin_tuple.h:38
Oid * rd_indcollation
Definition: rel.h:193
#define ERROR
Definition: elog.h:43
#define MaxBlockNumber
Definition: block.h:35
Relation bd_index
Definition: brin_internal.h:47
ambuild_function ambuild
Definition: amapi.h:198
amoptions_function amoptions
Definition: amapi.h:205
BlockNumber num_pages
Definition: genam.h:73
ItemPointerData t_self
Definition: htup.h:65
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:524
Datum brin_desummarize_range(PG_FUNCTION_ARGS)
Definition: brin.c:922
#define DEBUG2
Definition: elog.h:24
amcostestimate_function amcostestimate
Definition: amapi.h:204
uint16 oi_nstored
Definition: brin_internal.h:28
bool amcanunique
Definition: amapi.h:177
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1215
int bd_totalstored
Definition: brin_internal.h:56
struct BrinBuildState BrinBuildState
BrinValues bt_columns[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_tuple.h:46
#define BufferIsInvalid(buffer)
Definition: buf.h:31
static char * buf
Definition: pg_test_fsync.c:66
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:202
BrinRevmap * bo_rmAccess
Definition: brin.c:60
amendscan_function amendscan
Definition: amapi.h:212
#define memmove(d, s, c)
Definition: c.h:1059
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
bool amcanbackward
Definition: amapi.h:175
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define FirstOffsetNumber
Definition: off.h:27
static void brin_vacuum_scan(Relation idxrel, BufferAccessStrategy strategy)
Definition: brin.c:1399
BrinRevmap * bs_rmAccess
Definition: brin.c:49
#define DatumGetBool(X)
Definition: postgres.h:399
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1266
TupleDesc bd_tupdesc
Definition: brin_internal.h:50
void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: brin.c:546
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static uint64 totalsize
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define SK_ISNULL
Definition: skey.h:115
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:321
amrescan_function amrescan
Definition: amapi.h:209
bool amcanparallel
Definition: amapi.h:193
IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys)
Definition: brin.c:324
void * ii_AmCache
Definition: execnodes.h:150
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
static char ** options
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int numelems)
Definition: reloptions.c:1239
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
BlockNumber lastRevmapPage
Definition: brin_page.h:69
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
bool amsearchnulls
Definition: amapi.h:185
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
BrinDesc * bo_bdesc
Definition: brin.c:61
void * palloc0(Size size)
Definition: mcxt.c:878
#define PageGetContents(page)
Definition: bufpage.h:242
uintptr_t Datum
Definition: postgres.h:372
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
bool amclusterable
Definition: amapi.h:189
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:59
bool amsearcharray
Definition: amapi.h:183
#define InvalidOid
Definition: postgres_ext.h:36
BlockNumber pagesPerRange
Definition: brin.h:34
static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, double *numSummarized, double *numExisting)
Definition: brin.c:1241
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1069
BlockNumber pagesPerRange
Definition: brin_page.h:68
void brin_free_tuple(BrinTuple *tuple)
Definition: brin_tuple.c:308
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define makeNode(_type_)
Definition: nodes.h:557
BrinDesc * brin_build_desc(Relation rel)
Definition: brin.c:997
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:676
Definition: regguts.h:298
#define BRIN_PROCNUM_CONSISTENT
Definition: brin_internal.h:69
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4546
bool brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
Definition: brin_revmap.c:321
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
size_t Size
Definition: c.h:356
void brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7789
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
Definition: brin_revmap.c:71
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
ammarkpos_function ammarkpos
Definition: amapi.h:213
bool amcanorder
Definition: amapi.h:171
ScanKey keyData
Definition: relscan.h:94
#define RelationNeedsWAL(relation)
Definition: rel.h:505
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:217
#define INT64_FORMAT
Definition: c.h:315
#define DatumGetPointer(X)
Definition: postgres.h:555
#define BRIN_ALL_BLOCKRANGES
Definition: brin.c:64
int bs_numtuples
Definition: brin.c:45
MemoryContext bd_context
Definition: brin_internal.h:44
#define BRIN_LAST_OPTIONAL_PROCNUM
Definition: brin_internal.h:74
static Datum values[MAXATTR]
Definition: bootstrap.c:163
uint16 amstrategies
Definition: amapi.h:167
#define BRIN_PROCNUM_UNION
Definition: brin_internal.h:70
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:469
#define BrinGetAutoSummarize(relation)
Definition: brin.h:44
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2172
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
TupleDesc bd_disktdesc
Definition: brin_internal.h:53
BrinTuple * brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple, Size *size)
Definition: brin_tuple.c:89
int tdrefcount
Definition: tupdesc.h:80
Oid sk_collation
Definition: skey.h:70
ambuildempty_function ambuildempty
Definition: amapi.h:199
int i
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:602
#define RELKIND_INDEX
Definition: pg_class.h:161
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
IndexBulkDeleteResult * brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: brin.c:774
BlockNumber pagesPerRange
Definition: brin_xlog.h:52
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
void AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
Definition: autovacuum.c:3186
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:1020
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
Datum brin_summarize_new_values(PG_FUNCTION_ARGS)
Definition: brin.c:836
bool brin_page_cleanup(Relation idxrel, Buffer buf)
Definition: brin_pageops.c:595
BrinMemTuple * bs_dtuple
Definition: brin.c:51
static BrinBuildState * initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange)
Definition: brin.c:1085
void brinbuildempty(Relation index)
Definition: brin.c:729
BrinDesc * bs_bdesc
Definition: brin.c:50
#define elog
Definition: elog.h:219
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
double num_index_tuples
Definition: genam.h:76
int Buffer
Definition: buf.h:23
BrinTuple * brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
Definition: brin_tuple.c:263
amcanreturn_function amcanreturn
Definition: amapi.h:203
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:416
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition: genam.h:83
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum brinhandler(PG_FUNCTION_ARGS)
Definition: brin.c:82
#define offsetof(type, field)
Definition: c.h:555
double IndexBuildHeapRangeScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, bool anyvisible, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2197
AttrNumber sk_attno
Definition: skey.h:67
Pointer Page
Definition: bufpage.h:74
double index_tuples
Definition: genam.h:33
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:218
double heap_tuples
Definition: genam.h:32
BrinMemTuple * brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
Definition: brin_tuple.c:384
void brinGetStats(Relation index, BrinStatsData *stats)
Definition: brin.c:1064
static void terminate_brin_buildstate(BrinBuildState *state)
Definition: brin.c:1110
BlockNumber revmapNumPages
Definition: brin.h:35
amrestrpos_function amrestrpos
Definition: amapi.h:214