Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * ginutil.c
4 : * Utility routines for the Postgres inverted index access method.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * IDENTIFICATION
11 : * src/backend/access/gin/ginutil.c
12 : *-------------------------------------------------------------------------
13 : */
14 :
15 : #include "postgres.h"
16 :
17 : #include "access/gin_private.h"
18 : #include "access/ginxlog.h"
19 : #include "access/reloptions.h"
20 : #include "access/xloginsert.h"
21 : #include "catalog/pg_collation.h"
22 : #include "catalog/pg_type.h"
23 : #include "commands/progress.h"
24 : #include "commands/vacuum.h"
25 : #include "miscadmin.h"
26 : #include "storage/indexfsm.h"
27 : #include "utils/builtins.h"
28 : #include "utils/index_selfuncs.h"
29 : #include "utils/rel.h"
30 : #include "utils/typcache.h"
31 :
32 :
33 : /*
34 : * GIN handler function: return IndexAmRoutine with access method parameters
35 : * and callbacks.
36 : */
37 : Datum
38 3518 : ginhandler(PG_FUNCTION_ARGS)
39 : {
40 3518 : IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
41 :
42 3518 : amroutine->amstrategies = 0;
43 3518 : amroutine->amsupport = GINNProcs;
44 3518 : amroutine->amoptsprocnum = GIN_OPTIONS_PROC;
45 3518 : amroutine->amcanorder = false;
46 3518 : amroutine->amcanorderbyop = false;
47 3518 : amroutine->amcanhash = false;
48 3518 : amroutine->amconsistentequality = false;
49 3518 : amroutine->amconsistentordering = false;
50 3518 : amroutine->amcanbackward = false;
51 3518 : amroutine->amcanunique = false;
52 3518 : amroutine->amcanmulticol = true;
53 3518 : amroutine->amoptionalkey = true;
54 3518 : amroutine->amsearcharray = false;
55 3518 : amroutine->amsearchnulls = false;
56 3518 : amroutine->amstorage = true;
57 3518 : amroutine->amclusterable = false;
58 3518 : amroutine->ampredlocks = true;
59 3518 : amroutine->amcanparallel = false;
60 3518 : amroutine->amcanbuildparallel = true;
61 3518 : amroutine->amcaninclude = false;
62 3518 : amroutine->amusemaintenanceworkmem = true;
63 3518 : amroutine->amsummarizing = false;
64 3518 : amroutine->amparallelvacuumoptions =
65 : VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP;
66 3518 : amroutine->amkeytype = InvalidOid;
67 :
68 3518 : amroutine->ambuild = ginbuild;
69 3518 : amroutine->ambuildempty = ginbuildempty;
70 3518 : amroutine->aminsert = gininsert;
71 3518 : amroutine->aminsertcleanup = NULL;
72 3518 : amroutine->ambulkdelete = ginbulkdelete;
73 3518 : amroutine->amvacuumcleanup = ginvacuumcleanup;
74 3518 : amroutine->amcanreturn = NULL;
75 3518 : amroutine->amcostestimate = gincostestimate;
76 3518 : amroutine->amgettreeheight = NULL;
77 3518 : amroutine->amoptions = ginoptions;
78 3518 : amroutine->amproperty = NULL;
79 3518 : amroutine->ambuildphasename = ginbuildphasename;
80 3518 : amroutine->amvalidate = ginvalidate;
81 3518 : amroutine->amadjustmembers = ginadjustmembers;
82 3518 : amroutine->ambeginscan = ginbeginscan;
83 3518 : amroutine->amrescan = ginrescan;
84 3518 : amroutine->amgettuple = NULL;
85 3518 : amroutine->amgetbitmap = gingetbitmap;
86 3518 : amroutine->amendscan = ginendscan;
87 3518 : amroutine->ammarkpos = NULL;
88 3518 : amroutine->amrestrpos = NULL;
89 3518 : amroutine->amestimateparallelscan = NULL;
90 3518 : amroutine->aminitparallelscan = NULL;
91 3518 : amroutine->amparallelrescan = NULL;
92 :
93 3518 : PG_RETURN_POINTER(amroutine);
94 : }
95 :
96 : /*
97 : * initGinState: fill in an empty GinState struct to describe the index
98 : *
99 : * Note: assorted subsidiary data is allocated in the CurrentMemoryContext.
100 : */
101 : void
102 5166 : initGinState(GinState *state, Relation index)
103 : {
104 5166 : TupleDesc origTupdesc = RelationGetDescr(index);
105 : int i;
106 :
107 5166 : MemSet(state, 0, sizeof(GinState));
108 :
109 5166 : state->index = index;
110 5166 : state->oneCol = (origTupdesc->natts == 1);
111 5166 : state->origTupdesc = origTupdesc;
112 :
113 10614 : for (i = 0; i < origTupdesc->natts; i++)
114 : {
115 5448 : Form_pg_attribute attr = TupleDescAttr(origTupdesc, i);
116 :
117 5448 : if (state->oneCol)
118 4884 : state->tupdesc[i] = state->origTupdesc;
119 : else
120 : {
121 564 : state->tupdesc[i] = CreateTemplateTupleDesc(2);
122 :
123 564 : TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL,
124 : INT2OID, -1, 0);
125 564 : TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL,
126 : attr->atttypid,
127 : attr->atttypmod,
128 564 : attr->attndims);
129 564 : TupleDescInitEntryCollation(state->tupdesc[i], (AttrNumber) 2,
130 : attr->attcollation);
131 : }
132 :
133 : /*
134 : * If the compare proc isn't specified in the opclass definition, look
135 : * up the index key type's default btree comparator.
136 : */
137 5448 : if (index_getprocid(index, i + 1, GIN_COMPARE_PROC) != InvalidOid)
138 : {
139 2440 : fmgr_info_copy(&(state->compareFn[i]),
140 2440 : index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
141 : CurrentMemoryContext);
142 : }
143 : else
144 : {
145 : TypeCacheEntry *typentry;
146 :
147 3008 : typentry = lookup_type_cache(attr->atttypid,
148 : TYPECACHE_CMP_PROC_FINFO);
149 3008 : if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
150 0 : ereport(ERROR,
151 : (errcode(ERRCODE_UNDEFINED_FUNCTION),
152 : errmsg("could not identify a comparison function for type %s",
153 : format_type_be(attr->atttypid))));
154 3008 : fmgr_info_copy(&(state->compareFn[i]),
155 : &(typentry->cmp_proc_finfo),
156 : CurrentMemoryContext);
157 : }
158 :
159 : /* Opclass must always provide extract procs */
160 5448 : fmgr_info_copy(&(state->extractValueFn[i]),
161 5448 : index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC),
162 : CurrentMemoryContext);
163 5448 : fmgr_info_copy(&(state->extractQueryFn[i]),
164 5448 : index_getprocinfo(index, i + 1, GIN_EXTRACTQUERY_PROC),
165 : CurrentMemoryContext);
166 :
167 : /*
168 : * Check opclass capability to do tri-state or binary logic consistent
169 : * check.
170 : */
171 5448 : if (index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC) != InvalidOid)
172 : {
173 5042 : fmgr_info_copy(&(state->triConsistentFn[i]),
174 5042 : index_getprocinfo(index, i + 1, GIN_TRICONSISTENT_PROC),
175 : CurrentMemoryContext);
176 : }
177 :
178 5448 : if (index_getprocid(index, i + 1, GIN_CONSISTENT_PROC) != InvalidOid)
179 : {
180 5448 : fmgr_info_copy(&(state->consistentFn[i]),
181 5448 : index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
182 : CurrentMemoryContext);
183 : }
184 :
185 5448 : if (state->consistentFn[i].fn_oid == InvalidOid &&
186 0 : state->triConsistentFn[i].fn_oid == InvalidOid)
187 : {
188 0 : elog(ERROR, "missing GIN support function (%d or %d) for attribute %d of index \"%s\"",
189 : GIN_CONSISTENT_PROC, GIN_TRICONSISTENT_PROC,
190 : i + 1, RelationGetRelationName(index));
191 : }
192 :
193 : /*
194 : * Check opclass capability to do partial match.
195 : */
196 5448 : if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
197 : {
198 606 : fmgr_info_copy(&(state->comparePartialFn[i]),
199 606 : index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
200 : CurrentMemoryContext);
201 606 : state->canPartialMatch[i] = true;
202 : }
203 : else
204 : {
205 4842 : state->canPartialMatch[i] = false;
206 : }
207 :
208 : /*
209 : * If the index column has a specified collation, we should honor that
210 : * while doing comparisons. However, we may have a collatable storage
211 : * type for a noncollatable indexed data type (for instance, hstore
212 : * uses text index entries). If there's no index collation then
213 : * specify default collation in case the support functions need
214 : * collation. This is harmless if the support functions don't care
215 : * about collation, so we just do it unconditionally. (We could
216 : * alternatively call get_typcollation, but that seems like expensive
217 : * overkill --- there aren't going to be any cases where a GIN storage
218 : * type has a nondefault collation.)
219 : */
220 5448 : if (OidIsValid(index->rd_indcollation[i]))
221 346 : state->supportCollation[i] = index->rd_indcollation[i];
222 : else
223 5102 : state->supportCollation[i] = DEFAULT_COLLATION_OID;
224 : }
225 5166 : }
226 :
227 : /*
228 : * Extract attribute (column) number of stored entry from GIN tuple
229 : */
230 : OffsetNumber
231 17258870 : gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
232 : {
233 : OffsetNumber colN;
234 :
235 17258870 : if (ginstate->oneCol)
236 : {
237 : /* column number is not stored explicitly */
238 8592492 : colN = FirstOffsetNumber;
239 : }
240 : else
241 : {
242 : Datum res;
243 : bool isnull;
244 :
245 : /*
246 : * First attribute is always int16, so we can safely use any tuple
247 : * descriptor to obtain first attribute of tuple
248 : */
249 8666378 : res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
250 : &isnull);
251 : Assert(!isnull);
252 :
253 8666378 : colN = DatumGetUInt16(res);
254 : Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
255 : }
256 :
257 17258870 : return colN;
258 : }
259 :
260 : /*
261 : * Extract stored datum (and possible null category) from GIN tuple
262 : */
263 : Datum
264 12927062 : gintuple_get_key(GinState *ginstate, IndexTuple tuple,
265 : GinNullCategory *category)
266 : {
267 : Datum res;
268 : bool isnull;
269 :
270 12927062 : if (ginstate->oneCol)
271 : {
272 : /*
273 : * Single column index doesn't store attribute numbers in tuples
274 : */
275 8594602 : res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
276 : &isnull);
277 : }
278 : else
279 : {
280 : /*
281 : * Since the datum type depends on which index column it's from, we
282 : * must be careful to use the right tuple descriptor here.
283 : */
284 4332460 : OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
285 :
286 4332460 : res = index_getattr(tuple, OffsetNumberNext(FirstOffsetNumber),
287 4332460 : ginstate->tupdesc[colN - 1],
288 : &isnull);
289 : }
290 :
291 12927062 : if (isnull)
292 1848 : *category = GinGetNullCategory(tuple, ginstate);
293 : else
294 12925214 : *category = GIN_CAT_NORM_KEY;
295 :
296 12927062 : return res;
297 : }
298 :
299 : /*
300 : * Allocate a new page (either by recycling, or by extending the index file)
301 : * The returned buffer is already pinned and exclusive-locked
302 : * Caller is responsible for initializing the page by calling GinInitBuffer
303 : */
304 : Buffer
305 9364 : GinNewBuffer(Relation index)
306 : {
307 : Buffer buffer;
308 :
309 : /* First, try to get a page from FSM */
310 : for (;;)
311 0 : {
312 9364 : BlockNumber blkno = GetFreeIndexPage(index);
313 :
314 9364 : if (blkno == InvalidBlockNumber)
315 9256 : break;
316 :
317 108 : buffer = ReadBuffer(index, blkno);
318 :
319 : /*
320 : * We have to guard against the possibility that someone else already
321 : * recycled this page; the buffer may be locked if so.
322 : */
323 108 : if (ConditionalLockBuffer(buffer))
324 : {
325 108 : if (GinPageIsRecyclable(BufferGetPage(buffer)))
326 108 : return buffer; /* OK to use */
327 :
328 0 : LockBuffer(buffer, GIN_UNLOCK);
329 : }
330 :
331 : /* Can't use it, so release buffer and try again */
332 0 : ReleaseBuffer(buffer);
333 : }
334 :
335 : /* Must extend the file */
336 9256 : buffer = ExtendBufferedRel(BMR_REL(index), MAIN_FORKNUM, NULL,
337 : EB_LOCK_FIRST);
338 :
339 9256 : return buffer;
340 : }
341 :
342 : void
343 63300 : GinInitPage(Page page, uint32 f, Size pageSize)
344 : {
345 : GinPageOpaque opaque;
346 :
347 63300 : PageInit(page, pageSize, sizeof(GinPageOpaqueData));
348 :
349 63300 : opaque = GinPageGetOpaque(page);
350 63300 : opaque->flags = f;
351 63300 : opaque->rightlink = InvalidBlockNumber;
352 63300 : }
353 :
354 : void
355 4110 : GinInitBuffer(Buffer b, uint32 f)
356 : {
357 4110 : GinInitPage(BufferGetPage(b), f, BufferGetPageSize(b));
358 4110 : }
359 :
360 : void
361 48414 : GinInitMetabuffer(Buffer b)
362 : {
363 : GinMetaPageData *metadata;
364 48414 : Page page = BufferGetPage(b);
365 :
366 48414 : GinInitPage(page, GIN_META, BufferGetPageSize(b));
367 :
368 48414 : metadata = GinPageGetMeta(page);
369 :
370 48414 : metadata->head = metadata->tail = InvalidBlockNumber;
371 48414 : metadata->tailFreeSize = 0;
372 48414 : metadata->nPendingPages = 0;
373 48414 : metadata->nPendingHeapTuples = 0;
374 48414 : metadata->nTotalPages = 0;
375 48414 : metadata->nEntryPages = 0;
376 48414 : metadata->nDataPages = 0;
377 48414 : metadata->nEntries = 0;
378 48414 : metadata->ginVersion = GIN_CURRENT_VERSION;
379 :
380 : /*
381 : * Set pd_lower just past the end of the metadata. This is essential,
382 : * because without doing so, metadata will be lost if xlog.c compresses
383 : * the page.
384 : */
385 48414 : ((PageHeader) page)->pd_lower =
386 48414 : ((char *) metadata + sizeof(GinMetaPageData)) - (char *) page;
387 48414 : }
388 :
389 : /*
390 : * Compare two keys of the same index column
391 : */
392 : int
393 40041070 : ginCompareEntries(GinState *ginstate, OffsetNumber attnum,
394 : Datum a, GinNullCategory categorya,
395 : Datum b, GinNullCategory categoryb)
396 : {
397 : /* if not of same null category, sort by that first */
398 40041070 : if (categorya != categoryb)
399 624102 : return (categorya < categoryb) ? -1 : 1;
400 :
401 : /* all null items in same category are equal */
402 39416968 : if (categorya != GIN_CAT_NORM_KEY)
403 8312 : return 0;
404 :
405 : /* both not null, so safe to call the compareFn */
406 39408656 : return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
407 39408656 : ginstate->supportCollation[attnum - 1],
408 : a, b));
409 : }
410 :
411 : /*
412 : * Compare two keys of possibly different index columns
413 : */
414 : int
415 40524494 : ginCompareAttEntries(GinState *ginstate,
416 : OffsetNumber attnuma, Datum a, GinNullCategory categorya,
417 : OffsetNumber attnumb, Datum b, GinNullCategory categoryb)
418 : {
419 : /* attribute number is the first sort key */
420 40524494 : if (attnuma != attnumb)
421 539632 : return (attnuma < attnumb) ? -1 : 1;
422 :
423 39984862 : return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
424 : }
425 :
426 :
427 : /*
428 : * Support for sorting key datums in ginExtractEntries
429 : *
430 : * Note: we only have to worry about null and not-null keys here;
431 : * ginExtractEntries never generates more than one placeholder null,
432 : * so it doesn't have to sort those.
433 : */
434 : typedef struct
435 : {
436 : Datum datum;
437 : bool isnull;
438 : } keyEntryData;
439 :
440 : typedef struct
441 : {
442 : FmgrInfo *cmpDatumFunc;
443 : Oid collation;
444 : bool haveDups;
445 : } cmpEntriesArg;
446 :
447 : static int
448 4296456 : cmpEntries(const void *a, const void *b, void *arg)
449 : {
450 4296456 : const keyEntryData *aa = (const keyEntryData *) a;
451 4296456 : const keyEntryData *bb = (const keyEntryData *) b;
452 4296456 : cmpEntriesArg *data = (cmpEntriesArg *) arg;
453 : int res;
454 :
455 4296456 : if (aa->isnull)
456 : {
457 0 : if (bb->isnull)
458 0 : res = 0; /* NULL "=" NULL */
459 : else
460 0 : res = 1; /* NULL ">" not-NULL */
461 : }
462 4296456 : else if (bb->isnull)
463 0 : res = -1; /* not-NULL "<" NULL */
464 : else
465 4296456 : res = DatumGetInt32(FunctionCall2Coll(data->cmpDatumFunc,
466 : data->collation,
467 : aa->datum, bb->datum));
468 :
469 : /*
470 : * Detect if we have any duplicates. If there are equal keys, qsort must
471 : * compare them at some point, else it wouldn't know whether one should go
472 : * before or after the other.
473 : */
474 4296456 : if (res == 0)
475 79308 : data->haveDups = true;
476 :
477 4296456 : return res;
478 : }
479 :
480 :
481 : /*
482 : * Extract the index key values from an indexable item
483 : *
484 : * The resulting key values are sorted, and any duplicates are removed.
485 : * This avoids generating redundant index entries.
486 : */
487 : Datum *
488 1372626 : ginExtractEntries(GinState *ginstate, OffsetNumber attnum,
489 : Datum value, bool isNull,
490 : int32 *nentries, GinNullCategory **categories)
491 : {
492 : Datum *entries;
493 : bool *nullFlags;
494 : int32 i;
495 :
496 : /*
497 : * We don't call the extractValueFn on a null item. Instead generate a
498 : * placeholder.
499 : */
500 1372626 : if (isNull)
501 : {
502 6628 : *nentries = 1;
503 6628 : entries = (Datum *) palloc(sizeof(Datum));
504 6628 : entries[0] = (Datum) 0;
505 6628 : *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
506 6628 : (*categories)[0] = GIN_CAT_NULL_ITEM;
507 6628 : return entries;
508 : }
509 :
510 : /* OK, call the opclass's extractValueFn */
511 1365998 : nullFlags = NULL; /* in case extractValue doesn't set it */
512 : entries = (Datum *)
513 2731996 : DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1],
514 1365998 : ginstate->supportCollation[attnum - 1],
515 : value,
516 : PointerGetDatum(nentries),
517 : PointerGetDatum(&nullFlags)));
518 :
519 : /*
520 : * Generate a placeholder if the item contained no keys.
521 : */
522 1365998 : if (entries == NULL || *nentries <= 0)
523 : {
524 1800 : *nentries = 1;
525 1800 : entries = (Datum *) palloc(sizeof(Datum));
526 1800 : entries[0] = (Datum) 0;
527 1800 : *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
528 1800 : (*categories)[0] = GIN_CAT_EMPTY_ITEM;
529 1800 : return entries;
530 : }
531 :
532 : /*
533 : * If the extractValueFn didn't create a nullFlags array, create one,
534 : * assuming that everything's non-null.
535 : */
536 1364198 : if (nullFlags == NULL)
537 238988 : nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
538 :
539 : /*
540 : * If there's more than one key, sort and unique-ify.
541 : *
542 : * XXX Using qsort here is notationally painful, and the overhead is
543 : * pretty bad too. For small numbers of keys it'd likely be better to use
544 : * a simple insertion sort.
545 : */
546 1364198 : if (*nentries > 1)
547 : {
548 : keyEntryData *keydata;
549 : cmpEntriesArg arg;
550 :
551 601828 : keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
552 3406022 : for (i = 0; i < *nentries; i++)
553 : {
554 2804194 : keydata[i].datum = entries[i];
555 2804194 : keydata[i].isnull = nullFlags[i];
556 : }
557 :
558 601828 : arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
559 601828 : arg.collation = ginstate->supportCollation[attnum - 1];
560 601828 : arg.haveDups = false;
561 601828 : qsort_arg(keydata, *nentries, sizeof(keyEntryData),
562 : cmpEntries, &arg);
563 :
564 601828 : if (arg.haveDups)
565 : {
566 : /* there are duplicates, must get rid of 'em */
567 : int32 j;
568 :
569 29696 : entries[0] = keydata[0].datum;
570 29696 : nullFlags[0] = keydata[0].isnull;
571 29696 : j = 1;
572 227348 : for (i = 1; i < *nentries; i++)
573 : {
574 197652 : if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
575 : {
576 159004 : entries[j] = keydata[i].datum;
577 159004 : nullFlags[j] = keydata[i].isnull;
578 159004 : j++;
579 : }
580 : }
581 29696 : *nentries = j;
582 : }
583 : else
584 : {
585 : /* easy, no duplicates */
586 3148978 : for (i = 0; i < *nentries; i++)
587 : {
588 2576846 : entries[i] = keydata[i].datum;
589 2576846 : nullFlags[i] = keydata[i].isnull;
590 : }
591 : }
592 :
593 601828 : pfree(keydata);
594 : }
595 :
596 : /*
597 : * Create GinNullCategory representation from nullFlags.
598 : */
599 1364198 : *categories = (GinNullCategory *) palloc0(*nentries * sizeof(GinNullCategory));
600 4892114 : for (i = 0; i < *nentries; i++)
601 3527916 : (*categories)[i] = (nullFlags[i] ? GIN_CAT_NULL_KEY : GIN_CAT_NORM_KEY);
602 :
603 1364198 : return entries;
604 : }
605 :
606 : bytea *
607 632 : ginoptions(Datum reloptions, bool validate)
608 : {
609 : static const relopt_parse_elt tab[] = {
610 : {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)},
611 : {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions,
612 : pendingListCleanupSize)}
613 : };
614 :
615 632 : return (bytea *) build_reloptions(reloptions, validate,
616 : RELOPT_KIND_GIN,
617 : sizeof(GinOptions),
618 : tab, lengthof(tab));
619 : }
620 :
621 : /*
622 : * Fetch index's statistical data into *stats
623 : *
624 : * Note: in the result, nPendingPages can be trusted to be up-to-date,
625 : * as can ginVersion; but the other fields are as of the last VACUUM.
626 : */
627 : void
628 2194 : ginGetStats(Relation index, GinStatsData *stats)
629 : {
630 : Buffer metabuffer;
631 : Page metapage;
632 : GinMetaPageData *metadata;
633 :
634 2194 : metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
635 2194 : LockBuffer(metabuffer, GIN_SHARE);
636 2194 : metapage = BufferGetPage(metabuffer);
637 2194 : metadata = GinPageGetMeta(metapage);
638 :
639 2194 : stats->nPendingPages = metadata->nPendingPages;
640 2194 : stats->nTotalPages = metadata->nTotalPages;
641 2194 : stats->nEntryPages = metadata->nEntryPages;
642 2194 : stats->nDataPages = metadata->nDataPages;
643 2194 : stats->nEntries = metadata->nEntries;
644 2194 : stats->ginVersion = metadata->ginVersion;
645 :
646 2194 : UnlockReleaseBuffer(metabuffer);
647 2194 : }
648 :
649 : /*
650 : * Write the given statistics to the index's metapage
651 : *
652 : * Note: nPendingPages and ginVersion are *not* copied over
653 : */
654 : void
655 472 : ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
656 : {
657 : Buffer metabuffer;
658 : Page metapage;
659 : GinMetaPageData *metadata;
660 :
661 472 : metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
662 472 : LockBuffer(metabuffer, GIN_EXCLUSIVE);
663 472 : metapage = BufferGetPage(metabuffer);
664 472 : metadata = GinPageGetMeta(metapage);
665 :
666 472 : START_CRIT_SECTION();
667 :
668 472 : metadata->nTotalPages = stats->nTotalPages;
669 472 : metadata->nEntryPages = stats->nEntryPages;
670 472 : metadata->nDataPages = stats->nDataPages;
671 472 : metadata->nEntries = stats->nEntries;
672 :
673 : /*
674 : * Set pd_lower just past the end of the metadata. This is essential,
675 : * because without doing so, metadata will be lost if xlog.c compresses
676 : * the page. (We must do this here because pre-v11 versions of PG did not
677 : * set the metapage's pd_lower correctly, so a pg_upgraded index might
678 : * contain the wrong value.)
679 : */
680 472 : ((PageHeader) metapage)->pd_lower =
681 472 : ((char *) metadata + sizeof(GinMetaPageData)) - (char *) metapage;
682 :
683 472 : MarkBufferDirty(metabuffer);
684 :
685 472 : if (RelationNeedsWAL(index) && !is_build)
686 : {
687 : XLogRecPtr recptr;
688 : ginxlogUpdateMeta data;
689 :
690 50 : data.locator = index->rd_locator;
691 50 : data.ntuples = 0;
692 50 : data.newRightlink = data.prevTail = InvalidBlockNumber;
693 50 : memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
694 :
695 50 : XLogBeginInsert();
696 50 : XLogRegisterData(&data, sizeof(ginxlogUpdateMeta));
697 50 : XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | REGBUF_STANDARD);
698 :
699 50 : recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
700 50 : PageSetLSN(metapage, recptr);
701 : }
702 :
703 472 : UnlockReleaseBuffer(metabuffer);
704 :
705 472 : END_CRIT_SECTION();
706 472 : }
707 :
708 : /*
709 : * ginbuildphasename() -- Return name of index build phase.
710 : */
711 : char *
712 0 : ginbuildphasename(int64 phasenum)
713 : {
714 0 : switch (phasenum)
715 : {
716 0 : case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
717 0 : return "initializing";
718 0 : case PROGRESS_GIN_PHASE_INDEXBUILD_TABLESCAN:
719 0 : return "scanning table";
720 0 : case PROGRESS_GIN_PHASE_PERFORMSORT_1:
721 0 : return "sorting tuples (workers)";
722 0 : case PROGRESS_GIN_PHASE_MERGE_1:
723 0 : return "merging tuples (workers)";
724 0 : case PROGRESS_GIN_PHASE_PERFORMSORT_2:
725 0 : return "sorting tuples";
726 0 : case PROGRESS_GIN_PHASE_MERGE_2:
727 0 : return "merging tuples";
728 0 : default:
729 0 : return NULL;
730 : }
731 : }
|