| 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 |
ginhandler(PG_FUNCTION_ARGS) |
| 39 |
{ |
| 40 |
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine); |
| 41 |
|
| 42 |
amroutine->amstrategies = 0; |
| 43 |
amroutine->amsupport = GINNProcs; |
| 44 |
amroutine->amoptsprocnum = GIN_OPTIONS_PROC; |
| 45 |
amroutine->amcanorder = false; |
| 46 |
amroutine->amcanorderbyop = false; |
| 47 |
amroutine->amcanhash = false; |
| 48 |
amroutine->amconsistentequality = false; |
| 49 |
amroutine->amconsistentordering = false; |
| 50 |
amroutine->amcanbackward = false; |
| 51 |
amroutine->amcanunique = false; |
| 52 |
amroutine->amcanmulticol = true; |
| 53 |
amroutine->amoptionalkey = true; |
| 54 |
amroutine->amsearcharray = false; |
| 55 |
amroutine->amsearchnulls = false; |
| 56 |
amroutine->amstorage = true; |
| 57 |
amroutine->amclusterable = false; |
| 58 |
amroutine->ampredlocks = true; |
| 59 |
amroutine->amcanparallel = false; |
| 60 |
amroutine->amcanbuildparallel = true; |
| 61 |
amroutine->amcaninclude = false; |
| 62 |
amroutine->amusemaintenanceworkmem = true; |
| 63 |
amroutine->amsummarizing = false; |
| 64 |
amroutine->amparallelvacuumoptions = |
| 65 |
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP; |
| 66 |
amroutine->amkeytype = InvalidOid; |
| 67 |
|
| 68 |
amroutine->ambuild = ginbuild; |
| 69 |
amroutine->ambuildempty = ginbuildempty; |
| 70 |
amroutine->aminsert = gininsert; |
| 71 |
amroutine->aminsertcleanup = NULL; |
| 72 |
amroutine->ambulkdelete = ginbulkdelete; |
| 73 |
amroutine->amvacuumcleanup = ginvacuumcleanup; |
| 74 |
amroutine->amcanreturn = NULL; |
| 75 |
amroutine->amcostestimate = gincostestimate; |
| 76 |
amroutine->amgettreeheight = NULL; |
| 77 |
amroutine->amoptions = ginoptions; |
| 78 |
amroutine->amproperty = NULL; |
| 79 |
amroutine->ambuildphasename = ginbuildphasename; |
| 80 |
amroutine->amvalidate = ginvalidate; |
| 81 |
amroutine->amadjustmembers = ginadjustmembers; |
| 82 |
amroutine->ambeginscan = ginbeginscan; |
| 83 |
amroutine->amrescan = ginrescan; |
| 84 |
amroutine->amgettuple = NULL; |
| 85 |
amroutine->amgetbitmap = gingetbitmap; |
| 86 |
amroutine->amendscan = ginendscan; |
| 87 |
amroutine->ammarkpos = NULL; |
| 88 |
amroutine->amrestrpos = NULL; |
| 89 |
amroutine->amestimateparallelscan = NULL; |
| 90 |
amroutine->aminitparallelscan = NULL; |
| 91 |
amroutine->amparallelrescan = NULL; |
| 92 |
|
| 93 |
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 |
initGinState(GinState *state, Relation index) |
| 103 |
{ |
| 104 |
TupleDesc origTupdesc = RelationGetDescr(index); |
| 105 |
int i; |
| 106 |
|
| 107 |
MemSet(state, 0, sizeof(GinState)); |
| 108 |
|
| 109 |
state->index = index; |
| 110 |
state->oneCol = (origTupdesc->natts == 1); |
| 111 |
state->origTupdesc = origTupdesc; |
| 112 |
|
| 113 |
for (i = 0; i < origTupdesc->natts; i++) |
| 114 |
{ |
| 115 |
Form_pg_attribute attr = TupleDescAttr(origTupdesc, i); |
| 116 |
|
| 117 |
if (state->oneCol) |
| 118 |
state->tupdesc[i] = state->origTupdesc; |
| 119 |
else |
| 120 |
{ |
| 121 |
state->tupdesc[i] = CreateTemplateTupleDesc(2); |
| 122 |
|
| 123 |
TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL, |
| 124 |
INT2OID, -1, 0); |
| 125 |
TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL, |
| 126 |
attr->atttypid, |
| 127 |
attr->atttypmod, |
| 128 |
attr->attndims); |
| 129 |
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 |
if (index_getprocid(index, i + 1, GIN_COMPARE_PROC) != InvalidOid) |
| 138 |
{ |
| 139 |
fmgr_info_copy(&(state->compareFn[i]), |
| 140 |
index_getprocinfo(index, i + 1, GIN_COMPARE_PROC), |
| 141 |
CurrentMemoryContext); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
TypeCacheEntry *typentry; |
| 146 |
|
| 147 |
typentry = lookup_type_cache(attr->atttypid, |
| 148 |
TYPECACHE_CMP_PROC_FINFO); |
| 149 |
if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid)) |
| 150 |
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 |
fmgr_info_copy(&(state->compareFn[i]), |
| 155 |
&(typentry->cmp_proc_finfo), |
| 156 |
CurrentMemoryContext); |
| 157 |
} |
| 158 |
|
| 159 |
/* Opclass must always provide extract procs */ |
| 160 |
fmgr_info_copy(&(state->extractValueFn[i]), |
| 161 |
index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC), |
| 162 |
CurrentMemoryContext); |
| 163 |
fmgr_info_copy(&(state->extractQueryFn[i]), |
| 164 |
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 |
if (index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC) != InvalidOid) |
| 172 |
{ |
| 173 |
fmgr_info_copy(&(state->triConsistentFn[i]), |
| 174 |
index_getprocinfo(index, i + 1, GIN_TRICONSISTENT_PROC), |
| 175 |
CurrentMemoryContext); |
| 176 |
} |
| 177 |
|
| 178 |
if (index_getprocid(index, i + 1, GIN_CONSISTENT_PROC) != InvalidOid) |
| 179 |
{ |
| 180 |
fmgr_info_copy(&(state->consistentFn[i]), |
| 181 |
index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC), |
| 182 |
CurrentMemoryContext); |
| 183 |
} |
| 184 |
|
| 185 |
if (state->consistentFn[i].fn_oid == InvalidOid && |
| 186 |
state->triConsistentFn[i].fn_oid == InvalidOid) |
| 187 |
{ |
| 188 |
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 |
if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid) |
| 197 |
{ |
| 198 |
fmgr_info_copy(&(state->comparePartialFn[i]), |
| 199 |
index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC), |
| 200 |
CurrentMemoryContext); |
| 201 |
state->canPartialMatch[i] = true; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
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 |
if (OidIsValid(index->rd_indcollation[i])) |
| 221 |
state->supportCollation[i] = index->rd_indcollation[i]; |
| 222 |
else |
| 223 |
state->supportCollation[i] = DEFAULT_COLLATION_OID; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Extract attribute (column) number of stored entry from GIN tuple |
| 229 |
*/ |
| 230 |
OffsetNumber |
| 231 |
gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple) |
| 232 |
{ |
| 233 |
OffsetNumber colN; |
| 234 |
|
| 235 |
if (ginstate->oneCol) |
| 236 |
{ |
| 237 |
/* column number is not stored explicitly */ |
| 238 |
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 |
res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0], |
| 250 |
&isnull); |
| 251 |
Assert(!isnull); |
| 252 |
|
| 253 |
colN = DatumGetUInt16(res); |
| 254 |
Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts); |
| 255 |
} |
| 256 |
|
| 257 |
return colN; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* Extract stored datum (and possible null category) from GIN tuple |
| 262 |
*/ |
| 263 |
Datum |
| 264 |
gintuple_get_key(GinState *ginstate, IndexTuple tuple, |
| 265 |
GinNullCategory *category) |
| 266 |
{ |
| 267 |
Datum res; |
| 268 |
bool isnull; |
| 269 |
|
| 270 |
if (ginstate->oneCol) |
| 271 |
{ |
| 272 |
/* |
| 273 |
* Single column index doesn't store attribute numbers in tuples |
| 274 |
*/ |
| 275 |
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 |
OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple); |
| 285 |
|
| 286 |
res = index_getattr(tuple, OffsetNumberNext(FirstOffsetNumber), |
| 287 |
ginstate->tupdesc[colN - 1], |
| 288 |
&isnull); |
| 289 |
} |
| 290 |
|
| 291 |
if (isnull) |
| 292 |
*category = GinGetNullCategory(tuple, ginstate); |
| 293 |
else |
| 294 |
*category = GIN_CAT_NORM_KEY; |
| 295 |
|
| 296 |
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 |
GinNewBuffer(Relation index) |
| 306 |
{ |
| 307 |
Buffer buffer; |
| 308 |
|
| 309 |
/* First, try to get a page from FSM */ |
| 310 |
for (;;) |
| 311 |
{ |
| 312 |
BlockNumber blkno = GetFreeIndexPage(index); |
| 313 |
|
| 314 |
if (blkno == InvalidBlockNumber) |
| 315 |
break; |
| 316 |
|
| 317 |
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 |
if (ConditionalLockBuffer(buffer)) |
| 324 |
{ |
| 325 |
if (GinPageIsRecyclable(BufferGetPage(buffer))) |
| 326 |
return buffer; /* OK to use */ |
| 327 |
|
| 328 |
LockBuffer(buffer, GIN_UNLOCK); |
| 329 |
} |
| 330 |
|
| 331 |
/* Can't use it, so release buffer and try again */ |
| 332 |
ReleaseBuffer(buffer); |
| 333 |
} |
| 334 |
|
| 335 |
/* Must extend the file */ |
| 336 |
buffer = ExtendBufferedRel(BMR_REL(index), MAIN_FORKNUM, NULL, |
| 337 |
EB_LOCK_FIRST); |
| 338 |
|
| 339 |
return buffer; |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
GinInitPage(Page page, uint32 f, Size pageSize) |
| 344 |
{ |
| 345 |
GinPageOpaque opaque; |
| 346 |
|
| 347 |
PageInit(page, pageSize, sizeof(GinPageOpaqueData)); |
| 348 |
|
| 349 |
opaque = GinPageGetOpaque(page); |
| 350 |
opaque->flags = f; |
| 351 |
opaque->rightlink = InvalidBlockNumber; |
| 352 |
} |
| 353 |
|
| 354 |
void |
| 355 |
GinInitBuffer(Buffer b, uint32 f) |
| 356 |
{ |
| 357 |
GinInitPage(BufferGetPage(b), f, BufferGetPageSize(b)); |
| 358 |
} |
| 359 |
|
| 360 |
void |
| 361 |
GinInitMetabuffer(Buffer b) |
| 362 |
{ |
| 363 |
GinMetaPageData *metadata; |
| 364 |
Page page = BufferGetPage(b); |
| 365 |
|
| 366 |
GinInitPage(page, GIN_META, BufferGetPageSize(b)); |
| 367 |
|
| 368 |
metadata = GinPageGetMeta(page); |
| 369 |
|
| 370 |
metadata->head = metadata->tail = InvalidBlockNumber; |
| 371 |
metadata->tailFreeSize = 0; |
| 372 |
metadata->nPendingPages = 0; |
| 373 |
metadata->nPendingHeapTuples = 0; |
| 374 |
metadata->nTotalPages = 0; |
| 375 |
metadata->nEntryPages = 0; |
| 376 |
metadata->nDataPages = 0; |
| 377 |
metadata->nEntries = 0; |
| 378 |
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 |
((PageHeader) page)->pd_lower = |
| 386 |
((char *) metadata + sizeof(GinMetaPageData)) - (char *) page; |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
* Compare two keys of the same index column |
| 391 |
*/ |
| 392 |
int |
| 393 |
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 |
if (categorya != categoryb) |
| 399 |
return (categorya < categoryb) ? -1 : 1; |
| 400 |
|
| 401 |
/* all null items in same category are equal */ |
| 402 |
if (categorya != GIN_CAT_NORM_KEY) |
| 403 |
return 0; |
| 404 |
|
| 405 |
/* both not null, so safe to call the compareFn */ |
| 406 |
return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1], |
| 407 |
ginstate->supportCollation[attnum - 1], |
| 408 |
a, b)); |
| 409 |
} |
| 410 |
|
| 411 |
/* |
| 412 |
* Compare two keys of possibly different index columns |
| 413 |
*/ |
| 414 |
int |
| 415 |
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 |
if (attnuma != attnumb) |
| 421 |
return (attnuma < attnumb) ? -1 : 1; |
| 422 |
|
| 423 |
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 |
cmpEntries(const void *a, const void *b, void *arg) |
| 449 |
{ |
| 450 |
const keyEntryData *aa = (const keyEntryData *) a; |
| 451 |
const keyEntryData *bb = (const keyEntryData *) b; |
| 452 |
cmpEntriesArg *data = (cmpEntriesArg *) arg; |
| 453 |
int res; |
| 454 |
|
| 455 |
if (aa->isnull) |
| 456 |
{ |
| 457 |
if (bb->isnull) |
| 458 |
res = 0; /* NULL "=" NULL */ |
| 459 |
else |
| 460 |
res = 1; /* NULL ">" not-NULL */ |
| 461 |
} |
| 462 |
else if (bb->isnull) |
| 463 |
res = -1; /* not-NULL "<" NULL */ |
| 464 |
else |
| 465 |
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 |
if (res == 0) |
| 475 |
data->haveDups = true; |
| 476 |
|
| 477 |
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 |
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 |
if (isNull) |
| 501 |
{ |
| 502 |
*nentries = 1; |
| 503 |
entries = (Datum *) palloc(sizeof(Datum)); |
| 504 |
entries[0] = (Datum) 0; |
| 505 |
*categories = (GinNullCategory *) palloc(sizeof(GinNullCategory)); |
| 506 |
(*categories)[0] = GIN_CAT_NULL_ITEM; |
| 507 |
return entries; |
| 508 |
} |
| 509 |
|
| 510 |
/* OK, call the opclass's extractValueFn */ |
| 511 |
nullFlags = NULL; /* in case extractValue doesn't set it */ |
| 512 |
entries = (Datum *) |
| 513 |
DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1], |
| 514 |
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 |
if (entries == NULL || *nentries <= 0) |
| 523 |
{ |
| 524 |
*nentries = 1; |
| 525 |
entries = (Datum *) palloc(sizeof(Datum)); |
| 526 |
entries[0] = (Datum) 0; |
| 527 |
*categories = (GinNullCategory *) palloc(sizeof(GinNullCategory)); |
| 528 |
(*categories)[0] = GIN_CAT_EMPTY_ITEM; |
| 529 |
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 |
if (nullFlags == NULL) |
| 537 |
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 |
if (*nentries > 1) |
| 547 |
{ |
| 548 |
keyEntryData *keydata; |
| 549 |
cmpEntriesArg arg; |
| 550 |
|
| 551 |
keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData)); |
| 552 |
for (i = 0; i < *nentries; i++) |
| 553 |
{ |
| 554 |
keydata[i].datum = entries[i]; |
| 555 |
keydata[i].isnull = nullFlags[i]; |
| 556 |
} |
| 557 |
|
| 558 |
arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1]; |
| 559 |
arg.collation = ginstate->supportCollation[attnum - 1]; |
| 560 |
arg.haveDups = false; |
| 561 |
qsort_arg(keydata, *nentries, sizeof(keyEntryData), |
| 562 |
cmpEntries, &arg); |
| 563 |
|
| 564 |
if (arg.haveDups) |
| 565 |
{ |
| 566 |
/* there are duplicates, must get rid of 'em */ |
| 567 |
int32 j; |
| 568 |
|
| 569 |
entries[0] = keydata[0].datum; |
| 570 |
nullFlags[0] = keydata[0].isnull; |
| 571 |
j = 1; |
| 572 |
for (i = 1; i < *nentries; i++) |
| 573 |
{ |
| 574 |
if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0) |
| 575 |
{ |
| 576 |
entries[j] = keydata[i].datum; |
| 577 |
nullFlags[j] = keydata[i].isnull; |
| 578 |
j++; |
| 579 |
} |
| 580 |
} |
| 581 |
*nentries = j; |
| 582 |
} |
| 583 |
else |
| 584 |
{ |
| 585 |
/* easy, no duplicates */ |
| 586 |
for (i = 0; i < *nentries; i++) |
| 587 |
{ |
| 588 |
entries[i] = keydata[i].datum; |
| 589 |
nullFlags[i] = keydata[i].isnull; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
pfree(keydata); |
| 594 |
} |
| 595 |
|
| 596 |
/* |
| 597 |
* Create GinNullCategory representation from nullFlags. |
| 598 |
*/ |
| 599 |
*categories = (GinNullCategory *) palloc0(*nentries * sizeof(GinNullCategory)); |
| 600 |
for (i = 0; i < *nentries; i++) |
| 601 |
(*categories)[i] = (nullFlags[i] ? GIN_CAT_NULL_KEY : GIN_CAT_NORM_KEY); |
| 602 |
|
| 603 |
return entries; |
| 604 |
} |
| 605 |
|
| 606 |
bytea * |
| 607 |
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 |
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 |
ginGetStats(Relation index, GinStatsData *stats) |
| 629 |
{ |
| 630 |
Buffer metabuffer; |
| 631 |
Page metapage; |
| 632 |
GinMetaPageData *metadata; |
| 633 |
|
| 634 |
metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO); |
| 635 |
LockBuffer(metabuffer, GIN_SHARE); |
| 636 |
metapage = BufferGetPage(metabuffer); |
| 637 |
metadata = GinPageGetMeta(metapage); |
| 638 |
|
| 639 |
stats->nPendingPages = metadata->nPendingPages; |
| 640 |
stats->nTotalPages = metadata->nTotalPages; |
| 641 |
stats->nEntryPages = metadata->nEntryPages; |
| 642 |
stats->nDataPages = metadata->nDataPages; |
| 643 |
stats->nEntries = metadata->nEntries; |
| 644 |
stats->ginVersion = metadata->ginVersion; |
| 645 |
|
| 646 |
UnlockReleaseBuffer(metabuffer); |
| 647 |
} |
| 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 |
ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build) |
| 656 |
{ |
| 657 |
Buffer metabuffer; |
| 658 |
Page metapage; |
| 659 |
GinMetaPageData *metadata; |
| 660 |
|
| 661 |
metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO); |
| 662 |
LockBuffer(metabuffer, GIN_EXCLUSIVE); |
| 663 |
metapage = BufferGetPage(metabuffer); |
| 664 |
metadata = GinPageGetMeta(metapage); |
| 665 |
|
| 666 |
START_CRIT_SECTION(); |
| 667 |
|
| 668 |
metadata->nTotalPages = stats->nTotalPages; |
| 669 |
metadata->nEntryPages = stats->nEntryPages; |
| 670 |
metadata->nDataPages = stats->nDataPages; |
| 671 |
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 |
((PageHeader) metapage)->pd_lower = |
| 681 |
((char *) metadata + sizeof(GinMetaPageData)) - (char *) metapage; |
| 682 |
|
| 683 |
MarkBufferDirty(metabuffer); |
| 684 |
|
| 685 |
if (RelationNeedsWAL(index) && !is_build) |
| 686 |
{ |
| 687 |
XLogRecPtr recptr; |
| 688 |
ginxlogUpdateMeta data; |
| 689 |
|
| 690 |
data.locator = index->rd_locator; |
| 691 |
data.ntuples = 0; |
| 692 |
data.newRightlink = data.prevTail = InvalidBlockNumber; |
| 693 |
memcpy(&data.metadata, metadata, sizeof(GinMetaPageData)); |
| 694 |
|
| 695 |
XLogBeginInsert(); |
| 696 |
XLogRegisterData(&data, sizeof(ginxlogUpdateMeta)); |
| 697 |
XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | REGBUF_STANDARD); |
| 698 |
|
| 699 |
recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE); |
| 700 |
PageSetLSN(metapage, recptr); |
| 701 |
} |
| 702 |
|
| 703 |
UnlockReleaseBuffer(metabuffer); |
| 704 |
|
| 705 |
END_CRIT_SECTION(); |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
* ginbuildphasename() -- Return name of index build phase. |
| 710 |
*/ |
| 711 |
char * |
| 712 |
ginbuildphasename(int64 phasenum) |
| 713 |
{ |
| 714 |
switch (phasenum) |
| 715 |
{ |
| 716 |
case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE: |
| 717 |
return "initializing"; |
| 718 |
case PROGRESS_GIN_PHASE_INDEXBUILD_TABLESCAN: |
| 719 |
return "scanning table"; |
| 720 |
case PROGRESS_GIN_PHASE_PERFORMSORT_1: |
| 721 |
return "sorting tuples (workers)"; |
| 722 |
case PROGRESS_GIN_PHASE_MERGE_1: |
| 723 |
return "merging tuples (workers)"; |
| 724 |
case PROGRESS_GIN_PHASE_PERFORMSORT_2: |
| 725 |
return "sorting tuples"; |
| 726 |
case PROGRESS_GIN_PHASE_MERGE_2: |
| 727 |
return "merging tuples"; |
| 728 |
default: |
| 729 |
return NULL; |
| 730 |
} |
| 731 |
} |