| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* index.c |
| 4 |
* code to create and destroy POSTGRES index relations |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/catalog/index.c |
| 12 |
* |
| 13 |
* |
| 14 |
* INTERFACE ROUTINES |
| 15 |
* index_create() - Create a cataloged index relation |
| 16 |
* index_drop() - Removes index relation from catalogs |
| 17 |
* BuildIndexInfo() - Prepare to insert index tuples |
| 18 |
* FormIndexDatum() - Construct datum vector for one index tuple |
| 19 |
* |
| 20 |
*------------------------------------------------------------------------- |
| 21 |
*/ |
| 22 |
#include "postgres.h" |
| 23 |
|
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include "access/amapi.h" |
| 27 |
#include "access/heapam.h" |
| 28 |
#include "access/multixact.h" |
| 29 |
#include "access/relscan.h" |
| 30 |
#include "access/tableam.h" |
| 31 |
#include "access/toast_compression.h" |
| 32 |
#include "access/transam.h" |
| 33 |
#include "access/visibilitymap.h" |
| 34 |
#include "access/xact.h" |
| 35 |
#include "bootstrap/bootstrap.h" |
| 36 |
#include "catalog/binary_upgrade.h" |
| 37 |
#include "catalog/catalog.h" |
| 38 |
#include "catalog/dependency.h" |
| 39 |
#include "catalog/heap.h" |
| 40 |
#include "catalog/index.h" |
| 41 |
#include "catalog/objectaccess.h" |
| 42 |
#include "catalog/partition.h" |
| 43 |
#include "catalog/pg_am.h" |
| 44 |
#include "catalog/pg_collation.h" |
| 45 |
#include "catalog/pg_constraint.h" |
| 46 |
#include "catalog/pg_description.h" |
| 47 |
#include "catalog/pg_inherits.h" |
| 48 |
#include "catalog/pg_opclass.h" |
| 49 |
#include "catalog/pg_operator.h" |
| 50 |
#include "catalog/pg_tablespace.h" |
| 51 |
#include "catalog/pg_trigger.h" |
| 52 |
#include "catalog/pg_type.h" |
| 53 |
#include "catalog/storage.h" |
| 54 |
#include "catalog/storage_xlog.h" |
| 55 |
#include "commands/event_trigger.h" |
| 56 |
#include "commands/progress.h" |
| 57 |
#include "commands/tablecmds.h" |
| 58 |
#include "commands/trigger.h" |
| 59 |
#include "executor/executor.h" |
| 60 |
#include "miscadmin.h" |
| 61 |
#include "nodes/makefuncs.h" |
| 62 |
#include "nodes/nodeFuncs.h" |
| 63 |
#include "optimizer/optimizer.h" |
| 64 |
#include "parser/parser.h" |
| 65 |
#include "pgstat.h" |
| 66 |
#include "postmaster/autovacuum.h" |
| 67 |
#include "rewrite/rewriteManip.h" |
| 68 |
#include "storage/bufmgr.h" |
| 69 |
#include "storage/lmgr.h" |
| 70 |
#include "storage/predicate.h" |
| 71 |
#include "storage/smgr.h" |
| 72 |
#include "utils/builtins.h" |
| 73 |
#include "utils/fmgroids.h" |
| 74 |
#include "utils/guc.h" |
| 75 |
#include "utils/inval.h" |
| 76 |
#include "utils/lsyscache.h" |
| 77 |
#include "utils/memutils.h" |
| 78 |
#include "utils/pg_rusage.h" |
| 79 |
#include "utils/rel.h" |
| 80 |
#include "utils/snapmgr.h" |
| 81 |
#include "utils/syscache.h" |
| 82 |
#include "utils/tuplesort.h" |
| 83 |
|
| 84 |
/* Potentially set by pg_upgrade_support functions */ |
| 85 |
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid; |
| 86 |
RelFileNumber binary_upgrade_next_index_pg_class_relfilenumber = |
| 87 |
InvalidRelFileNumber; |
| 88 |
|
| 89 |
/* |
| 90 |
* Pointer-free representation of variables used when reindexing system |
| 91 |
* catalogs; we use this to propagate those values to parallel workers. |
| 92 |
*/ |
| 93 |
typedef struct |
| 94 |
{ |
| 95 |
Oid currentlyReindexedHeap; |
| 96 |
Oid currentlyReindexedIndex; |
| 97 |
int numPendingReindexedIndexes; |
| 98 |
Oid pendingReindexedIndexes[FLEXIBLE_ARRAY_MEMBER]; |
| 99 |
} SerializedReindexState; |
| 100 |
|
| 101 |
/* non-export function prototypes */ |
| 102 |
static bool relationHasPrimaryKey(Relation rel); |
| 103 |
static TupleDesc ConstructTupleDescriptor(Relation heapRelation, |
| 104 |
const IndexInfo *indexInfo, |
| 105 |
const List *indexColNames, |
| 106 |
Oid accessMethodId, |
| 107 |
const Oid *collationIds, |
| 108 |
const Oid *opclassIds); |
| 109 |
static void InitializeAttributeOids(Relation indexRelation, |
| 110 |
int numatts, Oid indexoid); |
| 111 |
static void AppendAttributeTuples(Relation indexRelation, const Datum *attopts, const NullableDatum *stattargets); |
| 112 |
static void UpdateIndexRelation(Oid indexoid, Oid heapoid, |
| 113 |
Oid parentIndexId, |
| 114 |
const IndexInfo *indexInfo, |
| 115 |
const Oid *collationOids, |
| 116 |
const Oid *opclassOids, |
| 117 |
const int16 *coloptions, |
| 118 |
bool primary, |
| 119 |
bool isexclusion, |
| 120 |
bool immediate, |
| 121 |
bool isvalid, |
| 122 |
bool isready); |
| 123 |
static void index_update_stats(Relation rel, |
| 124 |
bool hasindex, |
| 125 |
double reltuples); |
| 126 |
static void IndexCheckExclusion(Relation heapRelation, |
| 127 |
Relation indexRelation, |
| 128 |
IndexInfo *indexInfo); |
| 129 |
static bool validate_index_callback(ItemPointer itemptr, void *opaque); |
| 130 |
static bool ReindexIsCurrentlyProcessingIndex(Oid indexOid); |
| 131 |
static void SetReindexProcessing(Oid heapOid, Oid indexOid); |
| 132 |
static void ResetReindexProcessing(void); |
| 133 |
static void SetReindexPending(List *indexes); |
| 134 |
static void RemoveReindexPending(Oid indexOid); |
| 135 |
|
| 136 |
|
| 137 |
/* |
| 138 |
* relationHasPrimaryKey |
| 139 |
* See whether an existing relation has a primary key. |
| 140 |
* |
| 141 |
* Caller must have suitable lock on the relation. |
| 142 |
* |
| 143 |
* Note: we intentionally do not check indisvalid here; that's because this |
| 144 |
* is used to enforce the rule that there can be only one indisprimary index, |
| 145 |
* and we want that to be true even if said index is invalid. |
| 146 |
*/ |
| 147 |
static bool |
| 148 |
relationHasPrimaryKey(Relation rel) |
| 149 |
{ |
| 150 |
bool result = false; |
| 151 |
List *indexoidlist; |
| 152 |
ListCell *indexoidscan; |
| 153 |
|
| 154 |
/* |
| 155 |
* Get the list of index OIDs for the table from the relcache, and look up |
| 156 |
* each one in the pg_index syscache until we find one marked primary key |
| 157 |
* (hopefully there isn't more than one such). |
| 158 |
*/ |
| 159 |
indexoidlist = RelationGetIndexList(rel); |
| 160 |
|
| 161 |
foreach(indexoidscan, indexoidlist) |
| 162 |
{ |
| 163 |
Oid indexoid = lfirst_oid(indexoidscan); |
| 164 |
HeapTuple indexTuple; |
| 165 |
|
| 166 |
indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexoid)); |
| 167 |
if (!HeapTupleIsValid(indexTuple)) /* should not happen */ |
| 168 |
elog(ERROR, "cache lookup failed for index %u", indexoid); |
| 169 |
result = ((Form_pg_index) GETSTRUCT(indexTuple))->indisprimary; |
| 170 |
ReleaseSysCache(indexTuple); |
| 171 |
if (result) |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
list_free(indexoidlist); |
| 176 |
|
| 177 |
return result; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* index_check_primary_key |
| 182 |
* Apply special checks needed before creating a PRIMARY KEY index |
| 183 |
* |
| 184 |
* This processing used to be in DefineIndex(), but has been split out |
| 185 |
* so that it can be applied during ALTER TABLE ADD PRIMARY KEY USING INDEX. |
| 186 |
* |
| 187 |
* We check for a pre-existing primary key, and that all columns of the index |
| 188 |
* are simple column references (not expressions), and that all those |
| 189 |
* columns are marked NOT NULL. If not, fail. |
| 190 |
* |
| 191 |
* We used to automatically change unmarked columns to NOT NULL here by doing |
| 192 |
* our own local ALTER TABLE command. But that doesn't work well if we're |
| 193 |
* executing one subcommand of an ALTER TABLE: the operations may not get |
| 194 |
* performed in the right order overall. Now we expect that the parser |
| 195 |
* inserted any required ALTER TABLE SET NOT NULL operations before trying |
| 196 |
* to create a primary-key index. |
| 197 |
* |
| 198 |
* Caller had better have at least ShareLock on the table, else the not-null |
| 199 |
* checking isn't trustworthy. |
| 200 |
*/ |
| 201 |
void |
| 202 |
index_check_primary_key(Relation heapRel, |
| 203 |
const IndexInfo *indexInfo, |
| 204 |
bool is_alter_table, |
| 205 |
const IndexStmt *stmt) |
| 206 |
{ |
| 207 |
int i; |
| 208 |
|
| 209 |
/* |
| 210 |
* If ALTER TABLE or CREATE TABLE .. PARTITION OF, check that there isn't |
| 211 |
* already a PRIMARY KEY. In CREATE TABLE for an ordinary relation, we |
| 212 |
* have faith that the parser rejected multiple pkey clauses; and CREATE |
| 213 |
* INDEX doesn't have a way to say PRIMARY KEY, so it's no problem either. |
| 214 |
*/ |
| 215 |
if ((is_alter_table || heapRel->rd_rel->relispartition) && |
| 216 |
relationHasPrimaryKey(heapRel)) |
| 217 |
{ |
| 218 |
ereport(ERROR, |
| 219 |
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), |
| 220 |
errmsg("multiple primary keys for table \"%s\" are not allowed", |
| 221 |
RelationGetRelationName(heapRel)))); |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* Indexes created with NULLS NOT DISTINCT cannot be used for primary key |
| 226 |
* constraints. While there is no direct syntax to reach here, it can be |
| 227 |
* done by creating a separate index and attaching it via ALTER TABLE .. |
| 228 |
* USING INDEX. |
| 229 |
*/ |
| 230 |
if (indexInfo->ii_NullsNotDistinct) |
| 231 |
{ |
| 232 |
ereport(ERROR, |
| 233 |
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), |
| 234 |
errmsg("primary keys cannot use NULLS NOT DISTINCT indexes"))); |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* Check that all of the attributes in a primary key are marked as not |
| 239 |
* null. (We don't really expect to see that; it'd mean the parser messed |
| 240 |
* up. But it seems wise to check anyway.) |
| 241 |
*/ |
| 242 |
for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++) |
| 243 |
{ |
| 244 |
AttrNumber attnum = indexInfo->ii_IndexAttrNumbers[i]; |
| 245 |
HeapTuple atttuple; |
| 246 |
Form_pg_attribute attform; |
| 247 |
|
| 248 |
if (attnum == 0) |
| 249 |
ereport(ERROR, |
| 250 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 251 |
errmsg("primary keys cannot be expressions"))); |
| 252 |
|
| 253 |
/* System attributes are never null, so no need to check */ |
| 254 |
if (attnum < 0) |
| 255 |
continue; |
| 256 |
|
| 257 |
atttuple = SearchSysCache2(ATTNUM, |
| 258 |
ObjectIdGetDatum(RelationGetRelid(heapRel)), |
| 259 |
Int16GetDatum(attnum)); |
| 260 |
if (!HeapTupleIsValid(atttuple)) |
| 261 |
elog(ERROR, "cache lookup failed for attribute %d of relation %u", |
| 262 |
attnum, RelationGetRelid(heapRel)); |
| 263 |
attform = (Form_pg_attribute) GETSTRUCT(atttuple); |
| 264 |
|
| 265 |
if (!attform->attnotnull) |
| 266 |
ereport(ERROR, |
| 267 |
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), |
| 268 |
errmsg("primary key column \"%s\" is not marked NOT NULL", |
| 269 |
NameStr(attform->attname)))); |
| 270 |
|
| 271 |
ReleaseSysCache(atttuple); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* ConstructTupleDescriptor |
| 277 |
* |
| 278 |
* Build an index tuple descriptor for a new index |
| 279 |
*/ |
| 280 |
static TupleDesc |
| 281 |
ConstructTupleDescriptor(Relation heapRelation, |
| 282 |
const IndexInfo *indexInfo, |
| 283 |
const List *indexColNames, |
| 284 |
Oid accessMethodId, |
| 285 |
const Oid *collationIds, |
| 286 |
const Oid *opclassIds) |
| 287 |
{ |
| 288 |
int numatts = indexInfo->ii_NumIndexAttrs; |
| 289 |
int numkeyatts = indexInfo->ii_NumIndexKeyAttrs; |
| 290 |
ListCell *colnames_item = list_head(indexColNames); |
| 291 |
ListCell *indexpr_item = list_head(indexInfo->ii_Expressions); |
| 292 |
IndexAmRoutine *amroutine; |
| 293 |
TupleDesc heapTupDesc; |
| 294 |
TupleDesc indexTupDesc; |
| 295 |
int natts; /* #atts in heap rel --- for error checks */ |
| 296 |
int i; |
| 297 |
|
| 298 |
/* We need access to the index AM's API struct */ |
| 299 |
amroutine = GetIndexAmRoutineByAmId(accessMethodId, false); |
| 300 |
|
| 301 |
/* ... and to the table's tuple descriptor */ |
| 302 |
heapTupDesc = RelationGetDescr(heapRelation); |
| 303 |
natts = RelationGetForm(heapRelation)->relnatts; |
| 304 |
|
| 305 |
/* |
| 306 |
* allocate the new tuple descriptor |
| 307 |
*/ |
| 308 |
indexTupDesc = CreateTemplateTupleDesc(numatts); |
| 309 |
|
| 310 |
/* |
| 311 |
* Fill in the pg_attribute row. |
| 312 |
*/ |
| 313 |
for (i = 0; i < numatts; i++) |
| 314 |
{ |
| 315 |
AttrNumber atnum = indexInfo->ii_IndexAttrNumbers[i]; |
| 316 |
Form_pg_attribute to = TupleDescAttr(indexTupDesc, i); |
| 317 |
HeapTuple tuple; |
| 318 |
Form_pg_type typeTup; |
| 319 |
Form_pg_opclass opclassTup; |
| 320 |
Oid keyType; |
| 321 |
|
| 322 |
MemSet(to, 0, ATTRIBUTE_FIXED_PART_SIZE); |
| 323 |
to->attnum = i + 1; |
| 324 |
to->attislocal = true; |
| 325 |
to->attcollation = (i < numkeyatts) ? collationIds[i] : InvalidOid; |
| 326 |
|
| 327 |
/* |
| 328 |
* Set the attribute name as specified by caller. |
| 329 |
*/ |
| 330 |
if (colnames_item == NULL) /* shouldn't happen */ |
| 331 |
elog(ERROR, "too few entries in colnames list"); |
| 332 |
namestrcpy(&to->attname, (const char *) lfirst(colnames_item)); |
| 333 |
colnames_item = lnext(indexColNames, colnames_item); |
| 334 |
|
| 335 |
/* |
| 336 |
* For simple index columns, we copy some pg_attribute fields from the |
| 337 |
* parent relation. For expressions we have to look at the expression |
| 338 |
* result. |
| 339 |
*/ |
| 340 |
if (atnum != 0) |
| 341 |
{ |
| 342 |
/* Simple index column */ |
| 343 |
const FormData_pg_attribute *from; |
| 344 |
|
| 345 |
Assert(atnum > 0); /* should've been caught above */ |
| 346 |
|
| 347 |
if (atnum > natts) /* safety check */ |
| 348 |
elog(ERROR, "invalid column number %d", atnum); |
| 349 |
from = TupleDescAttr(heapTupDesc, |
| 350 |
AttrNumberGetAttrOffset(atnum)); |
| 351 |
|
| 352 |
to->atttypid = from->atttypid; |
| 353 |
to->attlen = from->attlen; |
| 354 |
to->attndims = from->attndims; |
| 355 |
to->atttypmod = from->atttypmod; |
| 356 |
to->attbyval = from->attbyval; |
| 357 |
to->attalign = from->attalign; |
| 358 |
to->attstorage = from->attstorage; |
| 359 |
to->attcompression = from->attcompression; |
| 360 |
} |
| 361 |
else |
| 362 |
{ |
| 363 |
/* Expressional index */ |
| 364 |
Node *indexkey; |
| 365 |
|
| 366 |
if (indexpr_item == NULL) /* shouldn't happen */ |
| 367 |
elog(ERROR, "too few entries in indexprs list"); |
| 368 |
indexkey = (Node *) lfirst(indexpr_item); |
| 369 |
indexpr_item = lnext(indexInfo->ii_Expressions, indexpr_item); |
| 370 |
|
| 371 |
/* |
| 372 |
* Lookup the expression type in pg_type for the type length etc. |
| 373 |
*/ |
| 374 |
keyType = exprType(indexkey); |
| 375 |
tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(keyType)); |
| 376 |
if (!HeapTupleIsValid(tuple)) |
| 377 |
elog(ERROR, "cache lookup failed for type %u", keyType); |
| 378 |
typeTup = (Form_pg_type) GETSTRUCT(tuple); |
| 379 |
|
| 380 |
/* |
| 381 |
* Assign some of the attributes values. Leave the rest. |
| 382 |
*/ |
| 383 |
to->atttypid = keyType; |
| 384 |
to->attlen = typeTup->typlen; |
| 385 |
to->atttypmod = exprTypmod(indexkey); |
| 386 |
to->attbyval = typeTup->typbyval; |
| 387 |
to->attalign = typeTup->typalign; |
| 388 |
to->attstorage = typeTup->typstorage; |
| 389 |
|
| 390 |
/* |
| 391 |
* For expression columns, set attcompression invalid, since |
| 392 |
* there's no table column from which to copy the value. Whenever |
| 393 |
* we actually need to compress a value, we'll use whatever the |
| 394 |
* current value of default_toast_compression is at that point in |
| 395 |
* time. |
| 396 |
*/ |
| 397 |
to->attcompression = InvalidCompressionMethod; |
| 398 |
|
| 399 |
ReleaseSysCache(tuple); |
| 400 |
|
| 401 |
/* |
| 402 |
* Make sure the expression yields a type that's safe to store in |
| 403 |
* an index. We need this defense because we have index opclasses |
| 404 |
* for pseudo-types such as "record", and the actually stored type |
| 405 |
* had better be safe; eg, a named composite type is okay, an |
| 406 |
* anonymous record type is not. The test is the same as for |
| 407 |
* whether a table column is of a safe type (which is why we |
| 408 |
* needn't check for the non-expression case). |
| 409 |
*/ |
| 410 |
CheckAttributeType(NameStr(to->attname), |
| 411 |
to->atttypid, to->attcollation, |
| 412 |
NIL, 0); |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* We do not yet have the correct relation OID for the index, so just |
| 417 |
* set it invalid for now. InitializeAttributeOids() will fix it |
| 418 |
* later. |
| 419 |
*/ |
| 420 |
to->attrelid = InvalidOid; |
| 421 |
|
| 422 |
/* |
| 423 |
* Check the opclass and index AM to see if either provides a keytype |
| 424 |
* (overriding the attribute type). Opclass (if exists) takes |
| 425 |
* precedence. |
| 426 |
*/ |
| 427 |
keyType = amroutine->amkeytype; |
| 428 |
|
| 429 |
if (i < indexInfo->ii_NumIndexKeyAttrs) |
| 430 |
{ |
| 431 |
tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassIds[i])); |
| 432 |
if (!HeapTupleIsValid(tuple)) |
| 433 |
elog(ERROR, "cache lookup failed for opclass %u", opclassIds[i]); |
| 434 |
opclassTup = (Form_pg_opclass) GETSTRUCT(tuple); |
| 435 |
if (OidIsValid(opclassTup->opckeytype)) |
| 436 |
keyType = opclassTup->opckeytype; |
| 437 |
|
| 438 |
/* |
| 439 |
* If keytype is specified as ANYELEMENT, and opcintype is |
| 440 |
* ANYARRAY, then the attribute type must be an array (else it'd |
| 441 |
* not have matched this opclass); use its element type. |
| 442 |
* |
| 443 |
* We could also allow ANYCOMPATIBLE/ANYCOMPATIBLEARRAY here, but |
| 444 |
* there seems no need to do so; there's no reason to declare an |
| 445 |
* opclass as taking ANYCOMPATIBLEARRAY rather than ANYARRAY. |
| 446 |
*/ |
| 447 |
if (keyType == ANYELEMENTOID && opclassTup->opcintype == ANYARRAYOID) |
| 448 |
{ |
| 449 |
keyType = get_base_element_type(to->atttypid); |
| 450 |
if (!OidIsValid(keyType)) |
| 451 |
elog(ERROR, "could not get element type of array type %u", |
| 452 |
to->atttypid); |
| 453 |
} |
| 454 |
|
| 455 |
ReleaseSysCache(tuple); |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* If a key type different from the heap value is specified, update |
| 460 |
* the type-related fields in the index tupdesc. |
| 461 |
*/ |
| 462 |
if (OidIsValid(keyType) && keyType != to->atttypid) |
| 463 |
{ |
| 464 |
tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(keyType)); |
| 465 |
if (!HeapTupleIsValid(tuple)) |
| 466 |
elog(ERROR, "cache lookup failed for type %u", keyType); |
| 467 |
typeTup = (Form_pg_type) GETSTRUCT(tuple); |
| 468 |
|
| 469 |
to->atttypid = keyType; |
| 470 |
to->atttypmod = -1; |
| 471 |
to->attlen = typeTup->typlen; |
| 472 |
to->attbyval = typeTup->typbyval; |
| 473 |
to->attalign = typeTup->typalign; |
| 474 |
to->attstorage = typeTup->typstorage; |
| 475 |
/* As above, use the default compression method in this case */ |
| 476 |
to->attcompression = InvalidCompressionMethod; |
| 477 |
|
| 478 |
ReleaseSysCache(tuple); |
| 479 |
} |
| 480 |
|
| 481 |
populate_compact_attribute(indexTupDesc, i); |
| 482 |
} |
| 483 |
|
| 484 |
pfree(amroutine); |
| 485 |
|
| 486 |
return indexTupDesc; |
| 487 |
} |
| 488 |
|
| 489 |
/* ---------------------------------------------------------------- |
| 490 |
* InitializeAttributeOids |
| 491 |
* ---------------------------------------------------------------- |
| 492 |
*/ |
| 493 |
static void |
| 494 |
InitializeAttributeOids(Relation indexRelation, |
| 495 |
int numatts, |
| 496 |
Oid indexoid) |
| 497 |
{ |
| 498 |
TupleDesc tupleDescriptor; |
| 499 |
int i; |
| 500 |
|
| 501 |
tupleDescriptor = RelationGetDescr(indexRelation); |
| 502 |
|
| 503 |
for (i = 0; i < numatts; i += 1) |
| 504 |
TupleDescAttr(tupleDescriptor, i)->attrelid = indexoid; |
| 505 |
} |
| 506 |
|
| 507 |
/* ---------------------------------------------------------------- |
| 508 |
* AppendAttributeTuples |
| 509 |
* ---------------------------------------------------------------- |
| 510 |
*/ |
| 511 |
static void |
| 512 |
AppendAttributeTuples(Relation indexRelation, const Datum *attopts, const NullableDatum *stattargets) |
| 513 |
{ |
| 514 |
Relation pg_attribute; |
| 515 |
CatalogIndexState indstate; |
| 516 |
TupleDesc indexTupDesc; |
| 517 |
FormExtraData_pg_attribute *attrs_extra = NULL; |
| 518 |
|
| 519 |
if (attopts) |
| 520 |
{ |
| 521 |
attrs_extra = palloc0_array(FormExtraData_pg_attribute, indexRelation->rd_att->natts); |
| 522 |
|
| 523 |
for (int i = 0; i < indexRelation->rd_att->natts; i++) |
| 524 |
{ |
| 525 |
if (attopts[i]) |
| 526 |
attrs_extra[i].attoptions.value = attopts[i]; |
| 527 |
else |
| 528 |
attrs_extra[i].attoptions.isnull = true; |
| 529 |
|
| 530 |
if (stattargets) |
| 531 |
attrs_extra[i].attstattarget = stattargets[i]; |
| 532 |
else |
| 533 |
attrs_extra[i].attstattarget.isnull = true; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/* |
| 538 |
* open the attribute relation and its indexes |
| 539 |
*/ |
| 540 |
pg_attribute = table_open(AttributeRelationId, RowExclusiveLock); |
| 541 |
|
| 542 |
indstate = CatalogOpenIndexes(pg_attribute); |
| 543 |
|
| 544 |
/* |
| 545 |
* insert data from new index's tupdesc into pg_attribute |
| 546 |
*/ |
| 547 |
indexTupDesc = RelationGetDescr(indexRelation); |
| 548 |
|
| 549 |
InsertPgAttributeTuples(pg_attribute, indexTupDesc, InvalidOid, attrs_extra, indstate); |
| 550 |
|
| 551 |
CatalogCloseIndexes(indstate); |
| 552 |
|
| 553 |
table_close(pg_attribute, RowExclusiveLock); |
| 554 |
} |
| 555 |
|
| 556 |
/* ---------------------------------------------------------------- |
| 557 |
* UpdateIndexRelation |
| 558 |
* |
| 559 |
* Construct and insert a new entry in the pg_index catalog |
| 560 |
* ---------------------------------------------------------------- |
| 561 |
*/ |
| 562 |
static void |
| 563 |
UpdateIndexRelation(Oid indexoid, |
| 564 |
Oid heapoid, |
| 565 |
Oid parentIndexId, |
| 566 |
const IndexInfo *indexInfo, |
| 567 |
const Oid *collationOids, |
| 568 |
const Oid *opclassOids, |
| 569 |
const int16 *coloptions, |
| 570 |
bool primary, |
| 571 |
bool isexclusion, |
| 572 |
bool immediate, |
| 573 |
bool isvalid, |
| 574 |
bool isready) |
| 575 |
{ |
| 576 |
int2vector *indkey; |
| 577 |
oidvector *indcollation; |
| 578 |
oidvector *indclass; |
| 579 |
int2vector *indoption; |
| 580 |
Datum exprsDatum; |
| 581 |
Datum predDatum; |
| 582 |
Datum values[Natts_pg_index]; |
| 583 |
bool nulls[Natts_pg_index] = {0}; |
| 584 |
Relation pg_index; |
| 585 |
HeapTuple tuple; |
| 586 |
int i; |
| 587 |
|
| 588 |
/* |
| 589 |
* Copy the index key, opclass, and indoption info into arrays (should we |
| 590 |
* make the caller pass them like this to start with?) |
| 591 |
*/ |
| 592 |
indkey = buildint2vector(NULL, indexInfo->ii_NumIndexAttrs); |
| 593 |
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) |
| 594 |
indkey->values[i] = indexInfo->ii_IndexAttrNumbers[i]; |
| 595 |
indcollation = buildoidvector(collationOids, indexInfo->ii_NumIndexKeyAttrs); |
| 596 |
indclass = buildoidvector(opclassOids, indexInfo->ii_NumIndexKeyAttrs); |
| 597 |
indoption = buildint2vector(coloptions, indexInfo->ii_NumIndexKeyAttrs); |
| 598 |
|
| 599 |
/* |
| 600 |
* Convert the index expressions (if any) to a text datum |
| 601 |
*/ |
| 602 |
if (indexInfo->ii_Expressions != NIL) |
| 603 |
{ |
| 604 |
char *exprsString; |
| 605 |
|
| 606 |
exprsString = nodeToString(indexInfo->ii_Expressions); |
| 607 |
exprsDatum = CStringGetTextDatum(exprsString); |
| 608 |
pfree(exprsString); |
| 609 |
} |
| 610 |
else |
| 611 |
exprsDatum = (Datum) 0; |
| 612 |
|
| 613 |
/* |
| 614 |
* Convert the index predicate (if any) to a text datum. Note we convert |
| 615 |
* implicit-AND format to normal explicit-AND for storage. |
| 616 |
*/ |
| 617 |
if (indexInfo->ii_Predicate != NIL) |
| 618 |
{ |
| 619 |
char *predString; |
| 620 |
|
| 621 |
predString = nodeToString(make_ands_explicit(indexInfo->ii_Predicate)); |
| 622 |
predDatum = CStringGetTextDatum(predString); |
| 623 |
pfree(predString); |
| 624 |
} |
| 625 |
else |
| 626 |
predDatum = (Datum) 0; |
| 627 |
|
| 628 |
|
| 629 |
/* |
| 630 |
* open the system catalog index relation |
| 631 |
*/ |
| 632 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 633 |
|
| 634 |
/* |
| 635 |
* Build a pg_index tuple |
| 636 |
*/ |
| 637 |
values[Anum_pg_index_indexrelid - 1] = ObjectIdGetDatum(indexoid); |
| 638 |
values[Anum_pg_index_indrelid - 1] = ObjectIdGetDatum(heapoid); |
| 639 |
values[Anum_pg_index_indnatts - 1] = Int16GetDatum(indexInfo->ii_NumIndexAttrs); |
| 640 |
values[Anum_pg_index_indnkeyatts - 1] = Int16GetDatum(indexInfo->ii_NumIndexKeyAttrs); |
| 641 |
values[Anum_pg_index_indisunique - 1] = BoolGetDatum(indexInfo->ii_Unique); |
| 642 |
values[Anum_pg_index_indnullsnotdistinct - 1] = BoolGetDatum(indexInfo->ii_NullsNotDistinct); |
| 643 |
values[Anum_pg_index_indisprimary - 1] = BoolGetDatum(primary); |
| 644 |
values[Anum_pg_index_indisexclusion - 1] = BoolGetDatum(isexclusion); |
| 645 |
values[Anum_pg_index_indimmediate - 1] = BoolGetDatum(immediate); |
| 646 |
values[Anum_pg_index_indisclustered - 1] = BoolGetDatum(false); |
| 647 |
values[Anum_pg_index_indisvalid - 1] = BoolGetDatum(isvalid); |
| 648 |
values[Anum_pg_index_indcheckxmin - 1] = BoolGetDatum(false); |
| 649 |
values[Anum_pg_index_indisready - 1] = BoolGetDatum(isready); |
| 650 |
values[Anum_pg_index_indislive - 1] = BoolGetDatum(true); |
| 651 |
values[Anum_pg_index_indisreplident - 1] = BoolGetDatum(false); |
| 652 |
values[Anum_pg_index_indkey - 1] = PointerGetDatum(indkey); |
| 653 |
values[Anum_pg_index_indcollation - 1] = PointerGetDatum(indcollation); |
| 654 |
values[Anum_pg_index_indclass - 1] = PointerGetDatum(indclass); |
| 655 |
values[Anum_pg_index_indoption - 1] = PointerGetDatum(indoption); |
| 656 |
values[Anum_pg_index_indexprs - 1] = exprsDatum; |
| 657 |
if (exprsDatum == (Datum) 0) |
| 658 |
nulls[Anum_pg_index_indexprs - 1] = true; |
| 659 |
values[Anum_pg_index_indpred - 1] = predDatum; |
| 660 |
if (predDatum == (Datum) 0) |
| 661 |
nulls[Anum_pg_index_indpred - 1] = true; |
| 662 |
|
| 663 |
tuple = heap_form_tuple(RelationGetDescr(pg_index), values, nulls); |
| 664 |
|
| 665 |
/* |
| 666 |
* insert the tuple into the pg_index catalog |
| 667 |
*/ |
| 668 |
CatalogTupleInsert(pg_index, tuple); |
| 669 |
|
| 670 |
/* |
| 671 |
* close the relation and free the tuple |
| 672 |
*/ |
| 673 |
table_close(pg_index, RowExclusiveLock); |
| 674 |
heap_freetuple(tuple); |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
/* |
| 679 |
* index_create |
| 680 |
* |
| 681 |
* heapRelation: table to build index on (suitably locked by caller) |
| 682 |
* indexRelationName: what it say |
| 683 |
* indexRelationId: normally, pass InvalidOid to let this routine |
| 684 |
* generate an OID for the index. During bootstrap this may be |
| 685 |
* nonzero to specify a preselected OID. |
| 686 |
* parentIndexRelid: if creating an index partition, the OID of the |
| 687 |
* parent index; otherwise InvalidOid. |
| 688 |
* parentConstraintId: if creating a constraint on a partition, the OID |
| 689 |
* of the constraint in the parent; otherwise InvalidOid. |
| 690 |
* relFileNumber: normally, pass InvalidRelFileNumber to get new storage. |
| 691 |
* May be nonzero to attach an existing valid build. |
| 692 |
* indexInfo: same info executor uses to insert into the index |
| 693 |
* indexColNames: column names to use for index (List of char *) |
| 694 |
* accessMethodId: OID of index AM to use |
| 695 |
* tableSpaceId: OID of tablespace to use |
| 696 |
* collationIds: array of collation OIDs, one per index column |
| 697 |
* opclassIds: array of index opclass OIDs, one per index column |
| 698 |
* coloptions: array of per-index-column indoption settings |
| 699 |
* reloptions: AM-specific options |
| 700 |
* flags: bitmask that can include any combination of these bits: |
| 701 |
* INDEX_CREATE_IS_PRIMARY |
| 702 |
* the index is a primary key |
| 703 |
* INDEX_CREATE_ADD_CONSTRAINT: |
| 704 |
* invoke index_constraint_create also |
| 705 |
* INDEX_CREATE_SKIP_BUILD: |
| 706 |
* skip the index_build() step for the moment; caller must do it |
| 707 |
* later (typically via reindex_index()) |
| 708 |
* INDEX_CREATE_CONCURRENT: |
| 709 |
* do not lock the table against writers. The index will be |
| 710 |
* marked "invalid" and the caller must take additional steps |
| 711 |
* to fix it up. |
| 712 |
* INDEX_CREATE_IF_NOT_EXISTS: |
| 713 |
* do not throw an error if a relation with the same name |
| 714 |
* already exists. |
| 715 |
* INDEX_CREATE_PARTITIONED: |
| 716 |
* create a partitioned index (table must be partitioned) |
| 717 |
* INDEX_CREATE_DEFERRABLE: |
| 718 |
* index supports a deferrable constraint, mark it as |
| 719 |
* non-immediate (indimmediate = false). |
| 720 |
* |
| 721 |
* constr_flags: flags passed to index_constraint_create |
| 722 |
* (only if INDEX_CREATE_ADD_CONSTRAINT is set) |
| 723 |
* allow_system_table_mods: allow table to be a system catalog |
| 724 |
* is_internal: if true, post creation hook for new index |
| 725 |
* constraintId: if not NULL, receives OID of created constraint |
| 726 |
* |
| 727 |
* Returns the OID of the created index. |
| 728 |
* |
| 729 |
* NB: Caller is responsible for ensuring the user has USAGE on all types |
| 730 |
* indexInfo->ii_{Expressions,Predicate} depend on. |
| 731 |
*/ |
| 732 |
Oid |
| 733 |
index_create(Relation heapRelation, |
| 734 |
const char *indexRelationName, |
| 735 |
Oid indexRelationId, |
| 736 |
Oid parentIndexRelid, |
| 737 |
Oid parentConstraintId, |
| 738 |
RelFileNumber relFileNumber, |
| 739 |
IndexInfo *indexInfo, |
| 740 |
const List *indexColNames, |
| 741 |
Oid accessMethodId, |
| 742 |
Oid tableSpaceId, |
| 743 |
const Oid *collationIds, |
| 744 |
const Oid *opclassIds, |
| 745 |
const Datum *opclassOptions, |
| 746 |
const int16 *coloptions, |
| 747 |
const NullableDatum *stattargets, |
| 748 |
Datum reloptions, |
| 749 |
bits16 flags, |
| 750 |
bits16 constr_flags, |
| 751 |
bool allow_system_table_mods, |
| 752 |
bool is_internal, |
| 753 |
Oid *constraintId) |
| 754 |
{ |
| 755 |
Oid heapRelationId = RelationGetRelid(heapRelation); |
| 756 |
Relation pg_class; |
| 757 |
Relation indexRelation; |
| 758 |
TupleDesc indexTupDesc; |
| 759 |
bool shared_relation; |
| 760 |
bool mapped_relation; |
| 761 |
bool is_exclusion; |
| 762 |
Oid namespaceId; |
| 763 |
int i; |
| 764 |
char relpersistence; |
| 765 |
bool isprimary = (flags & INDEX_CREATE_IS_PRIMARY) != 0; |
| 766 |
bool invalid = (flags & INDEX_CREATE_INVALID) != 0; |
| 767 |
bool concurrent = (flags & INDEX_CREATE_CONCURRENT) != 0; |
| 768 |
bool partitioned = (flags & INDEX_CREATE_PARTITIONED) != 0; |
| 769 |
char relkind; |
| 770 |
TransactionId relfrozenxid; |
| 771 |
MultiXactId relminmxid; |
| 772 |
bool create_storage = !RelFileNumberIsValid(relFileNumber); |
| 773 |
|
| 774 |
/* constraint flags can only be set when a constraint is requested */ |
| 775 |
Assert((constr_flags == 0) || |
| 776 |
((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0)); |
| 777 |
/* partitioned indexes must never be "built" by themselves */ |
| 778 |
Assert(!partitioned || (flags & INDEX_CREATE_SKIP_BUILD)); |
| 779 |
|
| 780 |
relkind = partitioned ? RELKIND_PARTITIONED_INDEX : RELKIND_INDEX; |
| 781 |
is_exclusion = (indexInfo->ii_ExclusionOps != NULL); |
| 782 |
|
| 783 |
pg_class = table_open(RelationRelationId, RowExclusiveLock); |
| 784 |
|
| 785 |
/* |
| 786 |
* The index will be in the same namespace as its parent table, and is |
| 787 |
* shared across databases if and only if the parent is. Likewise, it |
| 788 |
* will use the relfilenumber map if and only if the parent does; and it |
| 789 |
* inherits the parent's relpersistence. |
| 790 |
*/ |
| 791 |
namespaceId = RelationGetNamespace(heapRelation); |
| 792 |
shared_relation = heapRelation->rd_rel->relisshared; |
| 793 |
mapped_relation = RelationIsMapped(heapRelation); |
| 794 |
relpersistence = heapRelation->rd_rel->relpersistence; |
| 795 |
|
| 796 |
/* |
| 797 |
* check parameters |
| 798 |
*/ |
| 799 |
if (indexInfo->ii_NumIndexAttrs < 1) |
| 800 |
elog(ERROR, "must index at least one column"); |
| 801 |
|
| 802 |
if (!allow_system_table_mods && |
| 803 |
IsSystemRelation(heapRelation) && |
| 804 |
IsNormalProcessingMode()) |
| 805 |
ereport(ERROR, |
| 806 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 807 |
errmsg("user-defined indexes on system catalog tables are not supported"))); |
| 808 |
|
| 809 |
/* |
| 810 |
* Btree text_pattern_ops uses text_eq as the equality operator, which is |
| 811 |
* fine as long as the collation is deterministic; text_eq then reduces to |
| 812 |
* bitwise equality and so it is semantically compatible with the other |
| 813 |
* operators and functions in that opclass. But with a nondeterministic |
| 814 |
* collation, text_eq could yield results that are incompatible with the |
| 815 |
* actual behavior of the index (which is determined by the opclass's |
| 816 |
* comparison function). We prevent such problems by refusing creation of |
| 817 |
* an index with that opclass and a nondeterministic collation. |
| 818 |
* |
| 819 |
* The same applies to varchar_pattern_ops and bpchar_pattern_ops. If we |
| 820 |
* find more cases, we might decide to create a real mechanism for marking |
| 821 |
* opclasses as incompatible with nondeterminism; but for now, this small |
| 822 |
* hack suffices. |
| 823 |
* |
| 824 |
* Another solution is to use a special operator, not text_eq, as the |
| 825 |
* equality opclass member; but that is undesirable because it would |
| 826 |
* prevent index usage in many queries that work fine today. |
| 827 |
*/ |
| 828 |
for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++) |
| 829 |
{ |
| 830 |
Oid collation = collationIds[i]; |
| 831 |
Oid opclass = opclassIds[i]; |
| 832 |
|
| 833 |
if (collation) |
| 834 |
{ |
| 835 |
if ((opclass == TEXT_BTREE_PATTERN_OPS_OID || |
| 836 |
opclass == VARCHAR_BTREE_PATTERN_OPS_OID || |
| 837 |
opclass == BPCHAR_BTREE_PATTERN_OPS_OID) && |
| 838 |
!get_collation_isdeterministic(collation)) |
| 839 |
{ |
| 840 |
HeapTuple classtup; |
| 841 |
|
| 842 |
classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass)); |
| 843 |
if (!HeapTupleIsValid(classtup)) |
| 844 |
elog(ERROR, "cache lookup failed for operator class %u", opclass); |
| 845 |
ereport(ERROR, |
| 846 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 847 |
errmsg("nondeterministic collations are not supported for operator class \"%s\"", |
| 848 |
NameStr(((Form_pg_opclass) GETSTRUCT(classtup))->opcname)))); |
| 849 |
ReleaseSysCache(classtup); |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
/* |
| 855 |
* Concurrent index build on a system catalog is unsafe because we tend to |
| 856 |
* release locks before committing in catalogs. |
| 857 |
*/ |
| 858 |
if (concurrent && |
| 859 |
IsCatalogRelation(heapRelation)) |
| 860 |
ereport(ERROR, |
| 861 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 862 |
errmsg("concurrent index creation on system catalog tables is not supported"))); |
| 863 |
|
| 864 |
/* |
| 865 |
* This case is currently not supported. There's no way to ask for it in |
| 866 |
* the grammar with CREATE INDEX, but it can happen with REINDEX. |
| 867 |
*/ |
| 868 |
if (concurrent && is_exclusion) |
| 869 |
ereport(ERROR, |
| 870 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 871 |
errmsg("concurrent index creation for exclusion constraints is not supported"))); |
| 872 |
|
| 873 |
/* |
| 874 |
* We cannot allow indexing a shared relation after initdb (because |
| 875 |
* there's no way to make the entry in other databases' pg_class). |
| 876 |
*/ |
| 877 |
if (shared_relation && !IsBootstrapProcessingMode()) |
| 878 |
ereport(ERROR, |
| 879 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 880 |
errmsg("shared indexes cannot be created after initdb"))); |
| 881 |
|
| 882 |
/* |
| 883 |
* Shared relations must be in pg_global, too (last-ditch check) |
| 884 |
*/ |
| 885 |
if (shared_relation && tableSpaceId != GLOBALTABLESPACE_OID) |
| 886 |
elog(ERROR, "shared relations must be placed in pg_global tablespace"); |
| 887 |
|
| 888 |
/* |
| 889 |
* Check for duplicate name (both as to the index, and as to the |
| 890 |
* associated constraint if any). Such cases would fail on the relevant |
| 891 |
* catalogs' unique indexes anyway, but we prefer to give a friendlier |
| 892 |
* error message. |
| 893 |
*/ |
| 894 |
if (get_relname_relid(indexRelationName, namespaceId)) |
| 895 |
{ |
| 896 |
if ((flags & INDEX_CREATE_IF_NOT_EXISTS) != 0) |
| 897 |
{ |
| 898 |
ereport(NOTICE, |
| 899 |
(errcode(ERRCODE_DUPLICATE_TABLE), |
| 900 |
errmsg("relation \"%s\" already exists, skipping", |
| 901 |
indexRelationName))); |
| 902 |
table_close(pg_class, RowExclusiveLock); |
| 903 |
return InvalidOid; |
| 904 |
} |
| 905 |
|
| 906 |
ereport(ERROR, |
| 907 |
(errcode(ERRCODE_DUPLICATE_TABLE), |
| 908 |
errmsg("relation \"%s\" already exists", |
| 909 |
indexRelationName))); |
| 910 |
} |
| 911 |
|
| 912 |
if ((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0 && |
| 913 |
ConstraintNameIsUsed(CONSTRAINT_RELATION, heapRelationId, |
| 914 |
indexRelationName)) |
| 915 |
{ |
| 916 |
/* |
| 917 |
* INDEX_CREATE_IF_NOT_EXISTS does not apply here, since the |
| 918 |
* conflicting constraint is not an index. |
| 919 |
*/ |
| 920 |
ereport(ERROR, |
| 921 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 922 |
errmsg("constraint \"%s\" for relation \"%s\" already exists", |
| 923 |
indexRelationName, RelationGetRelationName(heapRelation)))); |
| 924 |
} |
| 925 |
|
| 926 |
/* |
| 927 |
* construct tuple descriptor for index tuples |
| 928 |
*/ |
| 929 |
indexTupDesc = ConstructTupleDescriptor(heapRelation, |
| 930 |
indexInfo, |
| 931 |
indexColNames, |
| 932 |
accessMethodId, |
| 933 |
collationIds, |
| 934 |
opclassIds); |
| 935 |
|
| 936 |
/* |
| 937 |
* Allocate an OID for the index, unless we were told what to use. |
| 938 |
* |
| 939 |
* The OID will be the relfilenumber as well, so make sure it doesn't |
| 940 |
* collide with either pg_class OIDs or existing physical files. |
| 941 |
*/ |
| 942 |
if (!OidIsValid(indexRelationId)) |
| 943 |
{ |
| 944 |
/* Use binary-upgrade override for pg_class.oid and relfilenumber */ |
| 945 |
if (IsBinaryUpgrade) |
| 946 |
{ |
| 947 |
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid)) |
| 948 |
ereport(ERROR, |
| 949 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 950 |
errmsg("pg_class index OID value not set when in binary upgrade mode"))); |
| 951 |
|
| 952 |
indexRelationId = binary_upgrade_next_index_pg_class_oid; |
| 953 |
binary_upgrade_next_index_pg_class_oid = InvalidOid; |
| 954 |
|
| 955 |
/* Override the index relfilenumber */ |
| 956 |
if ((relkind == RELKIND_INDEX) && |
| 957 |
(!RelFileNumberIsValid(binary_upgrade_next_index_pg_class_relfilenumber))) |
| 958 |
ereport(ERROR, |
| 959 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 960 |
errmsg("index relfilenumber value not set when in binary upgrade mode"))); |
| 961 |
relFileNumber = binary_upgrade_next_index_pg_class_relfilenumber; |
| 962 |
binary_upgrade_next_index_pg_class_relfilenumber = InvalidRelFileNumber; |
| 963 |
|
| 964 |
/* |
| 965 |
* Note that we want create_storage = true for binary upgrade. The |
| 966 |
* storage we create here will be replaced later, but we need to |
| 967 |
* have something on disk in the meanwhile. |
| 968 |
*/ |
| 969 |
Assert(create_storage); |
| 970 |
} |
| 971 |
else |
| 972 |
{ |
| 973 |
indexRelationId = |
| 974 |
GetNewRelFileNumber(tableSpaceId, pg_class, relpersistence); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* create the index relation's relcache entry and, if necessary, the |
| 980 |
* physical disk file. (If we fail further down, it's the smgr's |
| 981 |
* responsibility to remove the disk file again, if any.) |
| 982 |
*/ |
| 983 |
indexRelation = heap_create(indexRelationName, |
| 984 |
namespaceId, |
| 985 |
tableSpaceId, |
| 986 |
indexRelationId, |
| 987 |
relFileNumber, |
| 988 |
accessMethodId, |
| 989 |
indexTupDesc, |
| 990 |
relkind, |
| 991 |
relpersistence, |
| 992 |
shared_relation, |
| 993 |
mapped_relation, |
| 994 |
allow_system_table_mods, |
| 995 |
&relfrozenxid, |
| 996 |
&relminmxid, |
| 997 |
create_storage); |
| 998 |
|
| 999 |
Assert(relfrozenxid == InvalidTransactionId); |
| 1000 |
Assert(relminmxid == InvalidMultiXactId); |
| 1001 |
Assert(indexRelationId == RelationGetRelid(indexRelation)); |
| 1002 |
|
| 1003 |
/* |
| 1004 |
* Obtain exclusive lock on it. Although no other transactions can see it |
| 1005 |
* until we commit, this prevents deadlock-risk complaints from lock |
| 1006 |
* manager in cases such as CLUSTER. |
| 1007 |
*/ |
| 1008 |
LockRelation(indexRelation, AccessExclusiveLock); |
| 1009 |
|
| 1010 |
/* |
| 1011 |
* Fill in fields of the index's pg_class entry that are not set correctly |
| 1012 |
* by heap_create. |
| 1013 |
* |
| 1014 |
* XXX should have a cleaner way to create cataloged indexes |
| 1015 |
*/ |
| 1016 |
indexRelation->rd_rel->relowner = heapRelation->rd_rel->relowner; |
| 1017 |
indexRelation->rd_rel->relam = accessMethodId; |
| 1018 |
indexRelation->rd_rel->relispartition = OidIsValid(parentIndexRelid); |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* store index's pg_class entry |
| 1022 |
*/ |
| 1023 |
InsertPgClassTuple(pg_class, indexRelation, |
| 1024 |
RelationGetRelid(indexRelation), |
| 1025 |
(Datum) 0, |
| 1026 |
reloptions); |
| 1027 |
|
| 1028 |
/* done with pg_class */ |
| 1029 |
table_close(pg_class, RowExclusiveLock); |
| 1030 |
|
| 1031 |
/* |
| 1032 |
* now update the object id's of all the attribute tuple forms in the |
| 1033 |
* index relation's tuple descriptor |
| 1034 |
*/ |
| 1035 |
InitializeAttributeOids(indexRelation, |
| 1036 |
indexInfo->ii_NumIndexAttrs, |
| 1037 |
indexRelationId); |
| 1038 |
|
| 1039 |
/* |
| 1040 |
* append ATTRIBUTE tuples for the index |
| 1041 |
*/ |
| 1042 |
AppendAttributeTuples(indexRelation, opclassOptions, stattargets); |
| 1043 |
|
| 1044 |
/* ---------------- |
| 1045 |
* update pg_index |
| 1046 |
* (append INDEX tuple) |
| 1047 |
* |
| 1048 |
* Note that this stows away a representation of "predicate". |
| 1049 |
* (Or, could define a rule to maintain the predicate) --Nels, Feb '92 |
| 1050 |
* ---------------- |
| 1051 |
*/ |
| 1052 |
UpdateIndexRelation(indexRelationId, heapRelationId, parentIndexRelid, |
| 1053 |
indexInfo, |
| 1054 |
collationIds, opclassIds, coloptions, |
| 1055 |
isprimary, is_exclusion, |
| 1056 |
(constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) == 0 && |
| 1057 |
(flags & INDEX_CREATE_DEFERRABLE) == 0, |
| 1058 |
!concurrent && !invalid, |
| 1059 |
!concurrent); |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* Register relcache invalidation on the indexes' heap relation, to |
| 1063 |
* maintain consistency of its index list |
| 1064 |
*/ |
| 1065 |
CacheInvalidateRelcache(heapRelation); |
| 1066 |
|
| 1067 |
/* update pg_inherits and the parent's relhassubclass, if needed */ |
| 1068 |
if (OidIsValid(parentIndexRelid)) |
| 1069 |
{ |
| 1070 |
StoreSingleInheritance(indexRelationId, parentIndexRelid, 1); |
| 1071 |
LockRelationOid(parentIndexRelid, ShareUpdateExclusiveLock); |
| 1072 |
SetRelationHasSubclass(parentIndexRelid, true); |
| 1073 |
} |
| 1074 |
|
| 1075 |
/* |
| 1076 |
* Register constraint and dependencies for the index. |
| 1077 |
* |
| 1078 |
* If the index is from a CONSTRAINT clause, construct a pg_constraint |
| 1079 |
* entry. The index will be linked to the constraint, which in turn is |
| 1080 |
* linked to the table. If it's not a CONSTRAINT, we need to make a |
| 1081 |
* dependency directly on the table. |
| 1082 |
* |
| 1083 |
* We don't need a dependency on the namespace, because there'll be an |
| 1084 |
* indirect dependency via our parent table. |
| 1085 |
* |
| 1086 |
* During bootstrap we can't register any dependencies, and we don't try |
| 1087 |
* to make a constraint either. |
| 1088 |
*/ |
| 1089 |
if (!IsBootstrapProcessingMode()) |
| 1090 |
{ |
| 1091 |
ObjectAddress myself, |
| 1092 |
referenced; |
| 1093 |
ObjectAddresses *addrs; |
| 1094 |
|
| 1095 |
ObjectAddressSet(myself, RelationRelationId, indexRelationId); |
| 1096 |
|
| 1097 |
if ((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0) |
| 1098 |
{ |
| 1099 |
char constraintType; |
| 1100 |
ObjectAddress localaddr; |
| 1101 |
|
| 1102 |
if (isprimary) |
| 1103 |
constraintType = CONSTRAINT_PRIMARY; |
| 1104 |
else if (indexInfo->ii_Unique) |
| 1105 |
constraintType = CONSTRAINT_UNIQUE; |
| 1106 |
else if (is_exclusion) |
| 1107 |
constraintType = CONSTRAINT_EXCLUSION; |
| 1108 |
else |
| 1109 |
{ |
| 1110 |
elog(ERROR, "constraint must be PRIMARY, UNIQUE or EXCLUDE"); |
| 1111 |
constraintType = 0; /* keep compiler quiet */ |
| 1112 |
} |
| 1113 |
|
| 1114 |
localaddr = index_constraint_create(heapRelation, |
| 1115 |
indexRelationId, |
| 1116 |
parentConstraintId, |
| 1117 |
indexInfo, |
| 1118 |
indexRelationName, |
| 1119 |
constraintType, |
| 1120 |
constr_flags, |
| 1121 |
allow_system_table_mods, |
| 1122 |
is_internal); |
| 1123 |
if (constraintId) |
| 1124 |
*constraintId = localaddr.objectId; |
| 1125 |
} |
| 1126 |
else |
| 1127 |
{ |
| 1128 |
bool have_simple_col = false; |
| 1129 |
|
| 1130 |
addrs = new_object_addresses(); |
| 1131 |
|
| 1132 |
/* Create auto dependencies on simply-referenced columns */ |
| 1133 |
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) |
| 1134 |
{ |
| 1135 |
if (indexInfo->ii_IndexAttrNumbers[i] != 0) |
| 1136 |
{ |
| 1137 |
ObjectAddressSubSet(referenced, RelationRelationId, |
| 1138 |
heapRelationId, |
| 1139 |
indexInfo->ii_IndexAttrNumbers[i]); |
| 1140 |
add_exact_object_address(&referenced, addrs); |
| 1141 |
have_simple_col = true; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* |
| 1146 |
* If there are no simply-referenced columns, give the index an |
| 1147 |
* auto dependency on the whole table. In most cases, this will |
| 1148 |
* be redundant, but it might not be if the index expressions and |
| 1149 |
* predicate contain no Vars or only whole-row Vars. |
| 1150 |
*/ |
| 1151 |
if (!have_simple_col) |
| 1152 |
{ |
| 1153 |
ObjectAddressSet(referenced, RelationRelationId, |
| 1154 |
heapRelationId); |
| 1155 |
add_exact_object_address(&referenced, addrs); |
| 1156 |
} |
| 1157 |
|
| 1158 |
record_object_address_dependencies(&myself, addrs, DEPENDENCY_AUTO); |
| 1159 |
free_object_addresses(addrs); |
| 1160 |
} |
| 1161 |
|
| 1162 |
/* |
| 1163 |
* If this is an index partition, create partition dependencies on |
| 1164 |
* both the parent index and the table. (Note: these must be *in |
| 1165 |
* addition to*, not instead of, all other dependencies. Otherwise |
| 1166 |
* we'll be short some dependencies after DETACH PARTITION.) |
| 1167 |
*/ |
| 1168 |
if (OidIsValid(parentIndexRelid)) |
| 1169 |
{ |
| 1170 |
ObjectAddressSet(referenced, RelationRelationId, parentIndexRelid); |
| 1171 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_PRI); |
| 1172 |
|
| 1173 |
ObjectAddressSet(referenced, RelationRelationId, heapRelationId); |
| 1174 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_SEC); |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* placeholder for normal dependencies */ |
| 1178 |
addrs = new_object_addresses(); |
| 1179 |
|
| 1180 |
/* Store dependency on collations */ |
| 1181 |
|
| 1182 |
/* The default collation is pinned, so don't bother recording it */ |
| 1183 |
for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++) |
| 1184 |
{ |
| 1185 |
if (OidIsValid(collationIds[i]) && collationIds[i] != DEFAULT_COLLATION_OID) |
| 1186 |
{ |
| 1187 |
ObjectAddressSet(referenced, CollationRelationId, collationIds[i]); |
| 1188 |
add_exact_object_address(&referenced, addrs); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* Store dependency on operator classes */ |
| 1193 |
for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++) |
| 1194 |
{ |
| 1195 |
ObjectAddressSet(referenced, OperatorClassRelationId, opclassIds[i]); |
| 1196 |
add_exact_object_address(&referenced, addrs); |
| 1197 |
} |
| 1198 |
|
| 1199 |
record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL); |
| 1200 |
free_object_addresses(addrs); |
| 1201 |
|
| 1202 |
/* Store dependencies on anything mentioned in index expressions */ |
| 1203 |
if (indexInfo->ii_Expressions) |
| 1204 |
{ |
| 1205 |
recordDependencyOnSingleRelExpr(&myself, |
| 1206 |
(Node *) indexInfo->ii_Expressions, |
| 1207 |
heapRelationId, |
| 1208 |
DEPENDENCY_NORMAL, |
| 1209 |
DEPENDENCY_AUTO, false); |
| 1210 |
} |
| 1211 |
|
| 1212 |
/* Store dependencies on anything mentioned in predicate */ |
| 1213 |
if (indexInfo->ii_Predicate) |
| 1214 |
{ |
| 1215 |
recordDependencyOnSingleRelExpr(&myself, |
| 1216 |
(Node *) indexInfo->ii_Predicate, |
| 1217 |
heapRelationId, |
| 1218 |
DEPENDENCY_NORMAL, |
| 1219 |
DEPENDENCY_AUTO, false); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
/* Bootstrap mode - assert we weren't asked for constraint support */ |
| 1225 |
Assert((flags & INDEX_CREATE_ADD_CONSTRAINT) == 0); |
| 1226 |
} |
| 1227 |
|
| 1228 |
/* Post creation hook for new index */ |
| 1229 |
InvokeObjectPostCreateHookArg(RelationRelationId, |
| 1230 |
indexRelationId, 0, is_internal); |
| 1231 |
|
| 1232 |
/* |
| 1233 |
* Advance the command counter so that we can see the newly-entered |
| 1234 |
* catalog tuples for the index. |
| 1235 |
*/ |
| 1236 |
CommandCounterIncrement(); |
| 1237 |
|
| 1238 |
/* |
| 1239 |
* In bootstrap mode, we have to fill in the index strategy structure with |
| 1240 |
* information from the catalogs. If we aren't bootstrapping, then the |
| 1241 |
* relcache entry has already been rebuilt thanks to sinval update during |
| 1242 |
* CommandCounterIncrement. |
| 1243 |
*/ |
| 1244 |
if (IsBootstrapProcessingMode()) |
| 1245 |
RelationInitIndexAccessInfo(indexRelation); |
| 1246 |
else |
| 1247 |
Assert(indexRelation->rd_indexcxt != NULL); |
| 1248 |
|
| 1249 |
indexRelation->rd_index->indnkeyatts = indexInfo->ii_NumIndexKeyAttrs; |
| 1250 |
|
| 1251 |
/* Validate opclass-specific options */ |
| 1252 |
if (opclassOptions) |
| 1253 |
for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++) |
| 1254 |
(void) index_opclass_options(indexRelation, i + 1, |
| 1255 |
opclassOptions[i], |
| 1256 |
true); |
| 1257 |
|
| 1258 |
/* |
| 1259 |
* If this is bootstrap (initdb) time, then we don't actually fill in the |
| 1260 |
* index yet. We'll be creating more indexes and classes later, so we |
| 1261 |
* delay filling them in until just before we're done with bootstrapping. |
| 1262 |
* Similarly, if the caller specified to skip the build then filling the |
| 1263 |
* index is delayed till later (ALTER TABLE can save work in some cases |
| 1264 |
* with this). Otherwise, we call the AM routine that constructs the |
| 1265 |
* index. |
| 1266 |
*/ |
| 1267 |
if (IsBootstrapProcessingMode()) |
| 1268 |
{ |
| 1269 |
index_register(heapRelationId, indexRelationId, indexInfo); |
| 1270 |
} |
| 1271 |
else if ((flags & INDEX_CREATE_SKIP_BUILD) != 0) |
| 1272 |
{ |
| 1273 |
/* |
| 1274 |
* Caller is responsible for filling the index later on. However, |
| 1275 |
* we'd better make sure that the heap relation is correctly marked as |
| 1276 |
* having an index. |
| 1277 |
*/ |
| 1278 |
index_update_stats(heapRelation, |
| 1279 |
true, |
| 1280 |
-1.0); |
| 1281 |
/* Make the above update visible */ |
| 1282 |
CommandCounterIncrement(); |
| 1283 |
} |
| 1284 |
else |
| 1285 |
{ |
| 1286 |
index_build(heapRelation, indexRelation, indexInfo, false, true); |
| 1287 |
} |
| 1288 |
|
| 1289 |
/* |
| 1290 |
* Close the index; but we keep the lock that we acquired above until end |
| 1291 |
* of transaction. Closing the heap is caller's responsibility. |
| 1292 |
*/ |
| 1293 |
index_close(indexRelation, NoLock); |
| 1294 |
|
| 1295 |
return indexRelationId; |
| 1296 |
} |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* index_concurrently_create_copy |
| 1300 |
* |
| 1301 |
* Create concurrently an index based on the definition of the one provided by |
| 1302 |
* caller. The index is inserted into catalogs and needs to be built later |
| 1303 |
* on. This is called during concurrent reindex processing. |
| 1304 |
* |
| 1305 |
* "tablespaceOid" is the tablespace to use for this index. |
| 1306 |
*/ |
| 1307 |
Oid |
| 1308 |
index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, |
| 1309 |
Oid tablespaceOid, const char *newName) |
| 1310 |
{ |
| 1311 |
Relation indexRelation; |
| 1312 |
IndexInfo *oldInfo, |
| 1313 |
*newInfo; |
| 1314 |
Oid newIndexId = InvalidOid; |
| 1315 |
HeapTuple indexTuple, |
| 1316 |
classTuple; |
| 1317 |
Datum indclassDatum, |
| 1318 |
colOptionDatum, |
| 1319 |
reloptionsDatum; |
| 1320 |
Datum *opclassOptions; |
| 1321 |
oidvector *indclass; |
| 1322 |
int2vector *indcoloptions; |
| 1323 |
NullableDatum *stattargets; |
| 1324 |
bool isnull; |
| 1325 |
List *indexColNames = NIL; |
| 1326 |
List *indexExprs = NIL; |
| 1327 |
List *indexPreds = NIL; |
| 1328 |
bits16 flags = INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT; |
| 1329 |
Form_pg_index indexForm; |
| 1330 |
|
| 1331 |
indexRelation = index_open(oldIndexId, RowExclusiveLock); |
| 1332 |
|
| 1333 |
/* The new index needs some information from the old index */ |
| 1334 |
oldInfo = BuildIndexInfo(indexRelation); |
| 1335 |
|
| 1336 |
/* |
| 1337 |
* Concurrent build of an index with exclusion constraints is not |
| 1338 |
* supported. |
| 1339 |
*/ |
| 1340 |
if (oldInfo->ii_ExclusionOps != NULL) |
| 1341 |
ereport(ERROR, |
| 1342 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1343 |
errmsg("concurrent index creation for exclusion constraints is not supported"))); |
| 1344 |
|
| 1345 |
/* Get the array of class and column options IDs from index info */ |
| 1346 |
indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldIndexId)); |
| 1347 |
if (!HeapTupleIsValid(indexTuple)) |
| 1348 |
elog(ERROR, "cache lookup failed for index %u", oldIndexId); |
| 1349 |
|
| 1350 |
indexForm = (Form_pg_index) GETSTRUCT(indexTuple); |
| 1351 |
|
| 1352 |
/* Old index is deferrable, do the same for the new index */ |
| 1353 |
if (!indexForm->indimmediate) |
| 1354 |
flags |= INDEX_CREATE_DEFERRABLE; |
| 1355 |
|
| 1356 |
indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, |
| 1357 |
Anum_pg_index_indclass); |
| 1358 |
indclass = (oidvector *) DatumGetPointer(indclassDatum); |
| 1359 |
|
| 1360 |
colOptionDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, |
| 1361 |
Anum_pg_index_indoption); |
| 1362 |
indcoloptions = (int2vector *) DatumGetPointer(colOptionDatum); |
| 1363 |
|
| 1364 |
/* Fetch reloptions of index if any */ |
| 1365 |
classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(oldIndexId)); |
| 1366 |
if (!HeapTupleIsValid(classTuple)) |
| 1367 |
elog(ERROR, "cache lookup failed for relation %u", oldIndexId); |
| 1368 |
reloptionsDatum = SysCacheGetAttr(RELOID, classTuple, |
| 1369 |
Anum_pg_class_reloptions, &isnull); |
| 1370 |
|
| 1371 |
/* |
| 1372 |
* Fetch the list of expressions and predicates directly from the |
| 1373 |
* catalogs. This cannot rely on the information from IndexInfo of the |
| 1374 |
* old index as these have been flattened for the planner. |
| 1375 |
*/ |
| 1376 |
if (oldInfo->ii_Expressions != NIL) |
| 1377 |
{ |
| 1378 |
Datum exprDatum; |
| 1379 |
char *exprString; |
| 1380 |
|
| 1381 |
exprDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, |
| 1382 |
Anum_pg_index_indexprs); |
| 1383 |
exprString = TextDatumGetCString(exprDatum); |
| 1384 |
indexExprs = (List *) stringToNode(exprString); |
| 1385 |
pfree(exprString); |
| 1386 |
} |
| 1387 |
if (oldInfo->ii_Predicate != NIL) |
| 1388 |
{ |
| 1389 |
Datum predDatum; |
| 1390 |
char *predString; |
| 1391 |
|
| 1392 |
predDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple, |
| 1393 |
Anum_pg_index_indpred); |
| 1394 |
predString = TextDatumGetCString(predDatum); |
| 1395 |
indexPreds = (List *) stringToNode(predString); |
| 1396 |
|
| 1397 |
/* Also convert to implicit-AND format */ |
| 1398 |
indexPreds = make_ands_implicit((Expr *) indexPreds); |
| 1399 |
pfree(predString); |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* |
| 1403 |
* Build the index information for the new index. Note that rebuild of |
| 1404 |
* indexes with exclusion constraints is not supported, hence there is no |
| 1405 |
* need to fill all the ii_Exclusion* fields. |
| 1406 |
*/ |
| 1407 |
newInfo = makeIndexInfo(oldInfo->ii_NumIndexAttrs, |
| 1408 |
oldInfo->ii_NumIndexKeyAttrs, |
| 1409 |
oldInfo->ii_Am, |
| 1410 |
indexExprs, |
| 1411 |
indexPreds, |
| 1412 |
oldInfo->ii_Unique, |
| 1413 |
oldInfo->ii_NullsNotDistinct, |
| 1414 |
false, /* not ready for inserts */ |
| 1415 |
true, |
| 1416 |
indexRelation->rd_indam->amsummarizing, |
| 1417 |
oldInfo->ii_WithoutOverlaps); |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* Extract the list of column names and the column numbers for the new |
| 1421 |
* index information. All this information will be used for the index |
| 1422 |
* creation. |
| 1423 |
*/ |
| 1424 |
for (int i = 0; i < oldInfo->ii_NumIndexAttrs; i++) |
| 1425 |
{ |
| 1426 |
TupleDesc indexTupDesc = RelationGetDescr(indexRelation); |
| 1427 |
Form_pg_attribute att = TupleDescAttr(indexTupDesc, i); |
| 1428 |
|
| 1429 |
indexColNames = lappend(indexColNames, NameStr(att->attname)); |
| 1430 |
newInfo->ii_IndexAttrNumbers[i] = oldInfo->ii_IndexAttrNumbers[i]; |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* Extract opclass options for each attribute */ |
| 1434 |
opclassOptions = palloc0(sizeof(Datum) * newInfo->ii_NumIndexAttrs); |
| 1435 |
for (int i = 0; i < newInfo->ii_NumIndexAttrs; i++) |
| 1436 |
opclassOptions[i] = get_attoptions(oldIndexId, i + 1); |
| 1437 |
|
| 1438 |
/* Extract statistic targets for each attribute */ |
| 1439 |
stattargets = palloc0_array(NullableDatum, newInfo->ii_NumIndexAttrs); |
| 1440 |
for (int i = 0; i < newInfo->ii_NumIndexAttrs; i++) |
| 1441 |
{ |
| 1442 |
HeapTuple tp; |
| 1443 |
Datum dat; |
| 1444 |
|
| 1445 |
tp = SearchSysCache2(ATTNUM, ObjectIdGetDatum(oldIndexId), Int16GetDatum(i + 1)); |
| 1446 |
if (!HeapTupleIsValid(tp)) |
| 1447 |
elog(ERROR, "cache lookup failed for attribute %d of relation %u", |
| 1448 |
i + 1, oldIndexId); |
| 1449 |
dat = SysCacheGetAttr(ATTNUM, tp, Anum_pg_attribute_attstattarget, &isnull); |
| 1450 |
ReleaseSysCache(tp); |
| 1451 |
stattargets[i].value = dat; |
| 1452 |
stattargets[i].isnull = isnull; |
| 1453 |
} |
| 1454 |
|
| 1455 |
/* |
| 1456 |
* Now create the new index. |
| 1457 |
* |
| 1458 |
* For a partition index, we adjust the partition dependency later, to |
| 1459 |
* ensure a consistent state at all times. That is why parentIndexRelid |
| 1460 |
* is not set here. |
| 1461 |
*/ |
| 1462 |
newIndexId = index_create(heapRelation, |
| 1463 |
newName, |
| 1464 |
InvalidOid, /* indexRelationId */ |
| 1465 |
InvalidOid, /* parentIndexRelid */ |
| 1466 |
InvalidOid, /* parentConstraintId */ |
| 1467 |
InvalidRelFileNumber, /* relFileNumber */ |
| 1468 |
newInfo, |
| 1469 |
indexColNames, |
| 1470 |
indexRelation->rd_rel->relam, |
| 1471 |
tablespaceOid, |
| 1472 |
indexRelation->rd_indcollation, |
| 1473 |
indclass->values, |
| 1474 |
opclassOptions, |
| 1475 |
indcoloptions->values, |
| 1476 |
stattargets, |
| 1477 |
reloptionsDatum, |
| 1478 |
flags, |
| 1479 |
0, /* constr_flags */ |
| 1480 |
true, /* allow table to be a system catalog? */ |
| 1481 |
false, /* is_internal? */ |
| 1482 |
NULL); |
| 1483 |
|
| 1484 |
/* Close the relations used and clean up */ |
| 1485 |
index_close(indexRelation, NoLock); |
| 1486 |
ReleaseSysCache(indexTuple); |
| 1487 |
ReleaseSysCache(classTuple); |
| 1488 |
|
| 1489 |
return newIndexId; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* |
| 1493 |
* index_concurrently_build |
| 1494 |
* |
| 1495 |
* Build index for a concurrent operation. Low-level locks are taken when |
| 1496 |
* this operation is performed to prevent only schema changes, but they need |
| 1497 |
* to be kept until the end of the transaction performing this operation. |
| 1498 |
* 'indexOid' refers to an index relation OID already created as part of |
| 1499 |
* previous processing, and 'heapOid' refers to its parent heap relation. |
| 1500 |
*/ |
| 1501 |
void |
| 1502 |
index_concurrently_build(Oid heapRelationId, |
| 1503 |
Oid indexRelationId) |
| 1504 |
{ |
| 1505 |
Relation heapRel; |
| 1506 |
Oid save_userid; |
| 1507 |
int save_sec_context; |
| 1508 |
int save_nestlevel; |
| 1509 |
Relation indexRelation; |
| 1510 |
IndexInfo *indexInfo; |
| 1511 |
|
| 1512 |
/* This had better make sure that a snapshot is active */ |
| 1513 |
Assert(ActiveSnapshotSet()); |
| 1514 |
|
| 1515 |
/* Open and lock the parent heap relation */ |
| 1516 |
heapRel = table_open(heapRelationId, ShareUpdateExclusiveLock); |
| 1517 |
|
| 1518 |
/* |
| 1519 |
* Switch to the table owner's userid, so that any index functions are run |
| 1520 |
* as that user. Also lock down security-restricted operations and |
| 1521 |
* arrange to make GUC variable changes local to this command. |
| 1522 |
*/ |
| 1523 |
GetUserIdAndSecContext(&save_userid, &save_sec_context); |
| 1524 |
SetUserIdAndSecContext(heapRel->rd_rel->relowner, |
| 1525 |
save_sec_context | SECURITY_RESTRICTED_OPERATION); |
| 1526 |
save_nestlevel = NewGUCNestLevel(); |
| 1527 |
RestrictSearchPath(); |
| 1528 |
|
| 1529 |
indexRelation = index_open(indexRelationId, RowExclusiveLock); |
| 1530 |
|
| 1531 |
/* |
| 1532 |
* We have to re-build the IndexInfo struct, since it was lost in the |
| 1533 |
* commit of the transaction where this concurrent index was created at |
| 1534 |
* the catalog level. |
| 1535 |
*/ |
| 1536 |
indexInfo = BuildIndexInfo(indexRelation); |
| 1537 |
Assert(!indexInfo->ii_ReadyForInserts); |
| 1538 |
indexInfo->ii_Concurrent = true; |
| 1539 |
indexInfo->ii_BrokenHotChain = false; |
| 1540 |
|
| 1541 |
/* Now build the index */ |
| 1542 |
index_build(heapRel, indexRelation, indexInfo, false, true); |
| 1543 |
|
| 1544 |
/* Roll back any GUC changes executed by index functions */ |
| 1545 |
AtEOXact_GUC(false, save_nestlevel); |
| 1546 |
|
| 1547 |
/* Restore userid and security context */ |
| 1548 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 1549 |
|
| 1550 |
/* Close both the relations, but keep the locks */ |
| 1551 |
table_close(heapRel, NoLock); |
| 1552 |
index_close(indexRelation, NoLock); |
| 1553 |
|
| 1554 |
/* |
| 1555 |
* Update the pg_index row to mark the index as ready for inserts. Once we |
| 1556 |
* commit this transaction, any new transactions that open the table must |
| 1557 |
* insert new entries into the index for insertions and non-HOT updates. |
| 1558 |
*/ |
| 1559 |
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_READY); |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* |
| 1563 |
* index_concurrently_swap |
| 1564 |
* |
| 1565 |
* Swap name, dependencies, and constraints of the old index over to the new |
| 1566 |
* index, while marking the old index as invalid and the new as valid. |
| 1567 |
*/ |
| 1568 |
void |
| 1569 |
index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName) |
| 1570 |
{ |
| 1571 |
Relation pg_class, |
| 1572 |
pg_index, |
| 1573 |
pg_constraint, |
| 1574 |
pg_trigger; |
| 1575 |
Relation oldClassRel, |
| 1576 |
newClassRel; |
| 1577 |
HeapTuple oldClassTuple, |
| 1578 |
newClassTuple; |
| 1579 |
Form_pg_class oldClassForm, |
| 1580 |
newClassForm; |
| 1581 |
HeapTuple oldIndexTuple, |
| 1582 |
newIndexTuple; |
| 1583 |
Form_pg_index oldIndexForm, |
| 1584 |
newIndexForm; |
| 1585 |
bool isPartition; |
| 1586 |
Oid indexConstraintOid; |
| 1587 |
List *constraintOids = NIL; |
| 1588 |
ListCell *lc; |
| 1589 |
|
| 1590 |
/* |
| 1591 |
* Take a necessary lock on the old and new index before swapping them. |
| 1592 |
*/ |
| 1593 |
oldClassRel = relation_open(oldIndexId, ShareUpdateExclusiveLock); |
| 1594 |
newClassRel = relation_open(newIndexId, ShareUpdateExclusiveLock); |
| 1595 |
|
| 1596 |
/* Now swap names and dependencies of those indexes */ |
| 1597 |
pg_class = table_open(RelationRelationId, RowExclusiveLock); |
| 1598 |
|
| 1599 |
oldClassTuple = SearchSysCacheCopy1(RELOID, |
| 1600 |
ObjectIdGetDatum(oldIndexId)); |
| 1601 |
if (!HeapTupleIsValid(oldClassTuple)) |
| 1602 |
elog(ERROR, "could not find tuple for relation %u", oldIndexId); |
| 1603 |
newClassTuple = SearchSysCacheCopy1(RELOID, |
| 1604 |
ObjectIdGetDatum(newIndexId)); |
| 1605 |
if (!HeapTupleIsValid(newClassTuple)) |
| 1606 |
elog(ERROR, "could not find tuple for relation %u", newIndexId); |
| 1607 |
|
| 1608 |
oldClassForm = (Form_pg_class) GETSTRUCT(oldClassTuple); |
| 1609 |
newClassForm = (Form_pg_class) GETSTRUCT(newClassTuple); |
| 1610 |
|
| 1611 |
/* Swap the names */ |
| 1612 |
namestrcpy(&newClassForm->relname, NameStr(oldClassForm->relname)); |
| 1613 |
namestrcpy(&oldClassForm->relname, oldName); |
| 1614 |
|
| 1615 |
/* Swap the partition flags to track inheritance properly */ |
| 1616 |
isPartition = newClassForm->relispartition; |
| 1617 |
newClassForm->relispartition = oldClassForm->relispartition; |
| 1618 |
oldClassForm->relispartition = isPartition; |
| 1619 |
|
| 1620 |
CatalogTupleUpdate(pg_class, &oldClassTuple->t_self, oldClassTuple); |
| 1621 |
CatalogTupleUpdate(pg_class, &newClassTuple->t_self, newClassTuple); |
| 1622 |
|
| 1623 |
heap_freetuple(oldClassTuple); |
| 1624 |
heap_freetuple(newClassTuple); |
| 1625 |
|
| 1626 |
/* Now swap index info */ |
| 1627 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 1628 |
|
| 1629 |
oldIndexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 1630 |
ObjectIdGetDatum(oldIndexId)); |
| 1631 |
if (!HeapTupleIsValid(oldIndexTuple)) |
| 1632 |
elog(ERROR, "could not find tuple for relation %u", oldIndexId); |
| 1633 |
newIndexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 1634 |
ObjectIdGetDatum(newIndexId)); |
| 1635 |
if (!HeapTupleIsValid(newIndexTuple)) |
| 1636 |
elog(ERROR, "could not find tuple for relation %u", newIndexId); |
| 1637 |
|
| 1638 |
oldIndexForm = (Form_pg_index) GETSTRUCT(oldIndexTuple); |
| 1639 |
newIndexForm = (Form_pg_index) GETSTRUCT(newIndexTuple); |
| 1640 |
|
| 1641 |
/* |
| 1642 |
* Copy constraint flags from the old index. This is safe because the old |
| 1643 |
* index guaranteed uniqueness. |
| 1644 |
*/ |
| 1645 |
newIndexForm->indisprimary = oldIndexForm->indisprimary; |
| 1646 |
oldIndexForm->indisprimary = false; |
| 1647 |
newIndexForm->indisexclusion = oldIndexForm->indisexclusion; |
| 1648 |
oldIndexForm->indisexclusion = false; |
| 1649 |
newIndexForm->indimmediate = oldIndexForm->indimmediate; |
| 1650 |
oldIndexForm->indimmediate = true; |
| 1651 |
|
| 1652 |
/* Preserve indisreplident in the new index */ |
| 1653 |
newIndexForm->indisreplident = oldIndexForm->indisreplident; |
| 1654 |
|
| 1655 |
/* Preserve indisclustered in the new index */ |
| 1656 |
newIndexForm->indisclustered = oldIndexForm->indisclustered; |
| 1657 |
|
| 1658 |
/* |
| 1659 |
* Mark the new index as valid, and the old index as invalid similarly to |
| 1660 |
* what index_set_state_flags() does. |
| 1661 |
*/ |
| 1662 |
newIndexForm->indisvalid = true; |
| 1663 |
oldIndexForm->indisvalid = false; |
| 1664 |
oldIndexForm->indisclustered = false; |
| 1665 |
oldIndexForm->indisreplident = false; |
| 1666 |
|
| 1667 |
CatalogTupleUpdate(pg_index, &oldIndexTuple->t_self, oldIndexTuple); |
| 1668 |
CatalogTupleUpdate(pg_index, &newIndexTuple->t_self, newIndexTuple); |
| 1669 |
|
| 1670 |
heap_freetuple(oldIndexTuple); |
| 1671 |
heap_freetuple(newIndexTuple); |
| 1672 |
|
| 1673 |
/* |
| 1674 |
* Move constraints and triggers over to the new index |
| 1675 |
*/ |
| 1676 |
|
| 1677 |
constraintOids = get_index_ref_constraints(oldIndexId); |
| 1678 |
|
| 1679 |
indexConstraintOid = get_index_constraint(oldIndexId); |
| 1680 |
|
| 1681 |
if (OidIsValid(indexConstraintOid)) |
| 1682 |
constraintOids = lappend_oid(constraintOids, indexConstraintOid); |
| 1683 |
|
| 1684 |
pg_constraint = table_open(ConstraintRelationId, RowExclusiveLock); |
| 1685 |
pg_trigger = table_open(TriggerRelationId, RowExclusiveLock); |
| 1686 |
|
| 1687 |
foreach(lc, constraintOids) |
| 1688 |
{ |
| 1689 |
HeapTuple constraintTuple, |
| 1690 |
triggerTuple; |
| 1691 |
Form_pg_constraint conForm; |
| 1692 |
ScanKeyData key[1]; |
| 1693 |
SysScanDesc scan; |
| 1694 |
Oid constraintOid = lfirst_oid(lc); |
| 1695 |
|
| 1696 |
/* Move the constraint from the old to the new index */ |
| 1697 |
constraintTuple = SearchSysCacheCopy1(CONSTROID, |
| 1698 |
ObjectIdGetDatum(constraintOid)); |
| 1699 |
if (!HeapTupleIsValid(constraintTuple)) |
| 1700 |
elog(ERROR, "could not find tuple for constraint %u", constraintOid); |
| 1701 |
|
| 1702 |
conForm = ((Form_pg_constraint) GETSTRUCT(constraintTuple)); |
| 1703 |
|
| 1704 |
if (conForm->conindid == oldIndexId) |
| 1705 |
{ |
| 1706 |
conForm->conindid = newIndexId; |
| 1707 |
|
| 1708 |
CatalogTupleUpdate(pg_constraint, &constraintTuple->t_self, constraintTuple); |
| 1709 |
} |
| 1710 |
|
| 1711 |
heap_freetuple(constraintTuple); |
| 1712 |
|
| 1713 |
/* Search for trigger records */ |
| 1714 |
ScanKeyInit(&key[0], |
| 1715 |
Anum_pg_trigger_tgconstraint, |
| 1716 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1717 |
ObjectIdGetDatum(constraintOid)); |
| 1718 |
|
| 1719 |
scan = systable_beginscan(pg_trigger, TriggerConstraintIndexId, true, |
| 1720 |
NULL, 1, key); |
| 1721 |
|
| 1722 |
while (HeapTupleIsValid((triggerTuple = systable_getnext(scan)))) |
| 1723 |
{ |
| 1724 |
Form_pg_trigger tgForm = (Form_pg_trigger) GETSTRUCT(triggerTuple); |
| 1725 |
|
| 1726 |
if (tgForm->tgconstrindid != oldIndexId) |
| 1727 |
continue; |
| 1728 |
|
| 1729 |
/* Make a modifiable copy */ |
| 1730 |
triggerTuple = heap_copytuple(triggerTuple); |
| 1731 |
tgForm = (Form_pg_trigger) GETSTRUCT(triggerTuple); |
| 1732 |
|
| 1733 |
tgForm->tgconstrindid = newIndexId; |
| 1734 |
|
| 1735 |
CatalogTupleUpdate(pg_trigger, &triggerTuple->t_self, triggerTuple); |
| 1736 |
|
| 1737 |
heap_freetuple(triggerTuple); |
| 1738 |
} |
| 1739 |
|
| 1740 |
systable_endscan(scan); |
| 1741 |
} |
| 1742 |
|
| 1743 |
/* |
| 1744 |
* Move comment if any |
| 1745 |
*/ |
| 1746 |
{ |
| 1747 |
Relation description; |
| 1748 |
ScanKeyData skey[3]; |
| 1749 |
SysScanDesc sd; |
| 1750 |
HeapTuple tuple; |
| 1751 |
Datum values[Natts_pg_description] = {0}; |
| 1752 |
bool nulls[Natts_pg_description] = {0}; |
| 1753 |
bool replaces[Natts_pg_description] = {0}; |
| 1754 |
|
| 1755 |
values[Anum_pg_description_objoid - 1] = ObjectIdGetDatum(newIndexId); |
| 1756 |
replaces[Anum_pg_description_objoid - 1] = true; |
| 1757 |
|
| 1758 |
ScanKeyInit(&skey[0], |
| 1759 |
Anum_pg_description_objoid, |
| 1760 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1761 |
ObjectIdGetDatum(oldIndexId)); |
| 1762 |
ScanKeyInit(&skey[1], |
| 1763 |
Anum_pg_description_classoid, |
| 1764 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1765 |
ObjectIdGetDatum(RelationRelationId)); |
| 1766 |
ScanKeyInit(&skey[2], |
| 1767 |
Anum_pg_description_objsubid, |
| 1768 |
BTEqualStrategyNumber, F_INT4EQ, |
| 1769 |
Int32GetDatum(0)); |
| 1770 |
|
| 1771 |
description = table_open(DescriptionRelationId, RowExclusiveLock); |
| 1772 |
|
| 1773 |
sd = systable_beginscan(description, DescriptionObjIndexId, true, |
| 1774 |
NULL, 3, skey); |
| 1775 |
|
| 1776 |
while ((tuple = systable_getnext(sd)) != NULL) |
| 1777 |
{ |
| 1778 |
tuple = heap_modify_tuple(tuple, RelationGetDescr(description), |
| 1779 |
values, nulls, replaces); |
| 1780 |
CatalogTupleUpdate(description, &tuple->t_self, tuple); |
| 1781 |
|
| 1782 |
break; /* Assume there can be only one match */ |
| 1783 |
} |
| 1784 |
|
| 1785 |
systable_endscan(sd); |
| 1786 |
table_close(description, NoLock); |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* |
| 1790 |
* Swap inheritance relationship with parent index |
| 1791 |
*/ |
| 1792 |
if (get_rel_relispartition(oldIndexId)) |
| 1793 |
{ |
| 1794 |
List *ancestors = get_partition_ancestors(oldIndexId); |
| 1795 |
Oid parentIndexRelid = linitial_oid(ancestors); |
| 1796 |
|
| 1797 |
DeleteInheritsTuple(oldIndexId, parentIndexRelid, false, NULL); |
| 1798 |
StoreSingleInheritance(newIndexId, parentIndexRelid, 1); |
| 1799 |
|
| 1800 |
list_free(ancestors); |
| 1801 |
} |
| 1802 |
|
| 1803 |
/* |
| 1804 |
* Swap all dependencies of and on the old index to the new one, and |
| 1805 |
* vice-versa. Note that a call to CommandCounterIncrement() would cause |
| 1806 |
* duplicate entries in pg_depend, so this should not be done. |
| 1807 |
*/ |
| 1808 |
changeDependenciesOf(RelationRelationId, newIndexId, oldIndexId); |
| 1809 |
changeDependenciesOn(RelationRelationId, newIndexId, oldIndexId); |
| 1810 |
|
| 1811 |
changeDependenciesOf(RelationRelationId, oldIndexId, newIndexId); |
| 1812 |
changeDependenciesOn(RelationRelationId, oldIndexId, newIndexId); |
| 1813 |
|
| 1814 |
/* copy over statistics from old to new index */ |
| 1815 |
pgstat_copy_relation_stats(newClassRel, oldClassRel); |
| 1816 |
|
| 1817 |
/* Copy data of pg_statistic from the old index to the new one */ |
| 1818 |
CopyStatistics(oldIndexId, newIndexId); |
| 1819 |
|
| 1820 |
/* Close relations */ |
| 1821 |
table_close(pg_class, RowExclusiveLock); |
| 1822 |
table_close(pg_index, RowExclusiveLock); |
| 1823 |
table_close(pg_constraint, RowExclusiveLock); |
| 1824 |
table_close(pg_trigger, RowExclusiveLock); |
| 1825 |
|
| 1826 |
/* The lock taken previously is not released until the end of transaction */ |
| 1827 |
relation_close(oldClassRel, NoLock); |
| 1828 |
relation_close(newClassRel, NoLock); |
| 1829 |
} |
| 1830 |
|
| 1831 |
/* |
| 1832 |
* index_concurrently_set_dead |
| 1833 |
* |
| 1834 |
* Perform the last invalidation stage of DROP INDEX CONCURRENTLY or REINDEX |
| 1835 |
* CONCURRENTLY before actually dropping the index. After calling this |
| 1836 |
* function, the index is seen by all the backends as dead. Low-level locks |
| 1837 |
* taken here are kept until the end of the transaction calling this function. |
| 1838 |
*/ |
| 1839 |
void |
| 1840 |
index_concurrently_set_dead(Oid heapId, Oid indexId) |
| 1841 |
{ |
| 1842 |
Relation userHeapRelation; |
| 1843 |
Relation userIndexRelation; |
| 1844 |
|
| 1845 |
/* |
| 1846 |
* No more predicate locks will be acquired on this index, and we're about |
| 1847 |
* to stop doing inserts into the index which could show conflicts with |
| 1848 |
* existing predicate locks, so now is the time to move them to the heap |
| 1849 |
* relation. |
| 1850 |
*/ |
| 1851 |
userHeapRelation = table_open(heapId, ShareUpdateExclusiveLock); |
| 1852 |
userIndexRelation = index_open(indexId, ShareUpdateExclusiveLock); |
| 1853 |
TransferPredicateLocksToHeapRelation(userIndexRelation); |
| 1854 |
|
| 1855 |
/* |
| 1856 |
* Now we are sure that nobody uses the index for queries; they just might |
| 1857 |
* have it open for updating it. So now we can unset indisready and |
| 1858 |
* indislive, then wait till nobody could be using it at all anymore. |
| 1859 |
*/ |
| 1860 |
index_set_state_flags(indexId, INDEX_DROP_SET_DEAD); |
| 1861 |
|
| 1862 |
/* |
| 1863 |
* Invalidate the relcache for the table, so that after this commit all |
| 1864 |
* sessions will refresh the table's index list. Forgetting just the |
| 1865 |
* index's relcache entry is not enough. |
| 1866 |
*/ |
| 1867 |
CacheInvalidateRelcache(userHeapRelation); |
| 1868 |
|
| 1869 |
/* |
| 1870 |
* Close the relations again, though still holding session lock. |
| 1871 |
*/ |
| 1872 |
table_close(userHeapRelation, NoLock); |
| 1873 |
index_close(userIndexRelation, NoLock); |
| 1874 |
} |
| 1875 |
|
| 1876 |
/* |
| 1877 |
* index_constraint_create |
| 1878 |
* |
| 1879 |
* Set up a constraint associated with an index. Return the new constraint's |
| 1880 |
* address. |
| 1881 |
* |
| 1882 |
* heapRelation: table owning the index (must be suitably locked by caller) |
| 1883 |
* indexRelationId: OID of the index |
| 1884 |
* parentConstraintId: if constraint is on a partition, the OID of the |
| 1885 |
* constraint in the parent. |
| 1886 |
* indexInfo: same info executor uses to insert into the index |
| 1887 |
* constraintName: what it say (generally, should match name of index) |
| 1888 |
* constraintType: one of CONSTRAINT_PRIMARY, CONSTRAINT_UNIQUE, or |
| 1889 |
* CONSTRAINT_EXCLUSION |
| 1890 |
* flags: bitmask that can include any combination of these bits: |
| 1891 |
* INDEX_CONSTR_CREATE_MARK_AS_PRIMARY: index is a PRIMARY KEY |
| 1892 |
* INDEX_CONSTR_CREATE_DEFERRABLE: constraint is DEFERRABLE |
| 1893 |
* INDEX_CONSTR_CREATE_INIT_DEFERRED: constraint is INITIALLY DEFERRED |
| 1894 |
* INDEX_CONSTR_CREATE_UPDATE_INDEX: update the pg_index row |
| 1895 |
* INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS: remove existing dependencies |
| 1896 |
* of index on table's columns |
| 1897 |
* INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS: constraint uses WITHOUT OVERLAPS |
| 1898 |
* allow_system_table_mods: allow table to be a system catalog |
| 1899 |
* is_internal: index is constructed due to internal process |
| 1900 |
*/ |
| 1901 |
ObjectAddress |
| 1902 |
index_constraint_create(Relation heapRelation, |
| 1903 |
Oid indexRelationId, |
| 1904 |
Oid parentConstraintId, |
| 1905 |
const IndexInfo *indexInfo, |
| 1906 |
const char *constraintName, |
| 1907 |
char constraintType, |
| 1908 |
bits16 constr_flags, |
| 1909 |
bool allow_system_table_mods, |
| 1910 |
bool is_internal) |
| 1911 |
{ |
| 1912 |
Oid namespaceId = RelationGetNamespace(heapRelation); |
| 1913 |
ObjectAddress myself, |
| 1914 |
idxaddr; |
| 1915 |
Oid conOid; |
| 1916 |
bool deferrable; |
| 1917 |
bool initdeferred; |
| 1918 |
bool mark_as_primary; |
| 1919 |
bool islocal; |
| 1920 |
bool noinherit; |
| 1921 |
bool is_without_overlaps; |
| 1922 |
int16 inhcount; |
| 1923 |
|
| 1924 |
deferrable = (constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) != 0; |
| 1925 |
initdeferred = (constr_flags & INDEX_CONSTR_CREATE_INIT_DEFERRED) != 0; |
| 1926 |
mark_as_primary = (constr_flags & INDEX_CONSTR_CREATE_MARK_AS_PRIMARY) != 0; |
| 1927 |
is_without_overlaps = (constr_flags & INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS) != 0; |
| 1928 |
|
| 1929 |
/* constraint creation support doesn't work while bootstrapping */ |
| 1930 |
Assert(!IsBootstrapProcessingMode()); |
| 1931 |
|
| 1932 |
/* enforce system-table restriction */ |
| 1933 |
if (!allow_system_table_mods && |
| 1934 |
IsSystemRelation(heapRelation) && |
| 1935 |
IsNormalProcessingMode()) |
| 1936 |
ereport(ERROR, |
| 1937 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1938 |
errmsg("user-defined indexes on system catalog tables are not supported"))); |
| 1939 |
|
| 1940 |
/* primary/unique constraints shouldn't have any expressions */ |
| 1941 |
if (indexInfo->ii_Expressions && |
| 1942 |
constraintType != CONSTRAINT_EXCLUSION) |
| 1943 |
elog(ERROR, "constraints cannot have index expressions"); |
| 1944 |
|
| 1945 |
/* |
| 1946 |
* If we're manufacturing a constraint for a pre-existing index, we need |
| 1947 |
* to get rid of the existing auto dependencies for the index (the ones |
| 1948 |
* that index_create() would have made instead of calling this function). |
| 1949 |
* |
| 1950 |
* Note: this code would not necessarily do the right thing if the index |
| 1951 |
* has any expressions or predicate, but we'd never be turning such an |
| 1952 |
* index into a UNIQUE or PRIMARY KEY constraint. |
| 1953 |
*/ |
| 1954 |
if (constr_flags & INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS) |
| 1955 |
deleteDependencyRecordsForClass(RelationRelationId, indexRelationId, |
| 1956 |
RelationRelationId, DEPENDENCY_AUTO); |
| 1957 |
|
| 1958 |
if (OidIsValid(parentConstraintId)) |
| 1959 |
{ |
| 1960 |
islocal = false; |
| 1961 |
inhcount = 1; |
| 1962 |
noinherit = false; |
| 1963 |
} |
| 1964 |
else |
| 1965 |
{ |
| 1966 |
islocal = true; |
| 1967 |
inhcount = 0; |
| 1968 |
noinherit = true; |
| 1969 |
} |
| 1970 |
|
| 1971 |
/* |
| 1972 |
* Construct a pg_constraint entry. |
| 1973 |
*/ |
| 1974 |
conOid = CreateConstraintEntry(constraintName, |
| 1975 |
namespaceId, |
| 1976 |
constraintType, |
| 1977 |
deferrable, |
| 1978 |
initdeferred, |
| 1979 |
true, /* Is Enforced */ |
| 1980 |
true, |
| 1981 |
parentConstraintId, |
| 1982 |
RelationGetRelid(heapRelation), |
| 1983 |
indexInfo->ii_IndexAttrNumbers, |
| 1984 |
indexInfo->ii_NumIndexKeyAttrs, |
| 1985 |
indexInfo->ii_NumIndexAttrs, |
| 1986 |
InvalidOid, /* no domain */ |
| 1987 |
indexRelationId, /* index OID */ |
| 1988 |
InvalidOid, /* no foreign key */ |
| 1989 |
NULL, |
| 1990 |
NULL, |
| 1991 |
NULL, |
| 1992 |
NULL, |
| 1993 |
0, |
| 1994 |
' ', |
| 1995 |
' ', |
| 1996 |
NULL, |
| 1997 |
0, |
| 1998 |
' ', |
| 1999 |
indexInfo->ii_ExclusionOps, |
| 2000 |
NULL, /* no check constraint */ |
| 2001 |
NULL, |
| 2002 |
islocal, |
| 2003 |
inhcount, |
| 2004 |
noinherit, |
| 2005 |
is_without_overlaps, |
| 2006 |
is_internal); |
| 2007 |
|
| 2008 |
/* |
| 2009 |
* Register the index as internally dependent on the constraint. |
| 2010 |
* |
| 2011 |
* Note that the constraint has a dependency on the table, so we don't |
| 2012 |
* need (or want) any direct dependency from the index to the table. |
| 2013 |
*/ |
| 2014 |
ObjectAddressSet(myself, ConstraintRelationId, conOid); |
| 2015 |
ObjectAddressSet(idxaddr, RelationRelationId, indexRelationId); |
| 2016 |
recordDependencyOn(&idxaddr, &myself, DEPENDENCY_INTERNAL); |
| 2017 |
|
| 2018 |
/* |
| 2019 |
* Also, if this is a constraint on a partition, give it partition-type |
| 2020 |
* dependencies on the parent constraint as well as the table. |
| 2021 |
*/ |
| 2022 |
if (OidIsValid(parentConstraintId)) |
| 2023 |
{ |
| 2024 |
ObjectAddress referenced; |
| 2025 |
|
| 2026 |
ObjectAddressSet(referenced, ConstraintRelationId, parentConstraintId); |
| 2027 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_PRI); |
| 2028 |
ObjectAddressSet(referenced, RelationRelationId, |
| 2029 |
RelationGetRelid(heapRelation)); |
| 2030 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_SEC); |
| 2031 |
} |
| 2032 |
|
| 2033 |
/* |
| 2034 |
* If the constraint is deferrable, create the deferred uniqueness |
| 2035 |
* checking trigger. (The trigger will be given an internal dependency on |
| 2036 |
* the constraint by CreateTrigger.) |
| 2037 |
*/ |
| 2038 |
if (deferrable) |
| 2039 |
{ |
| 2040 |
CreateTrigStmt *trigger = makeNode(CreateTrigStmt); |
| 2041 |
|
| 2042 |
trigger->replace = false; |
| 2043 |
trigger->isconstraint = true; |
| 2044 |
trigger->trigname = (constraintType == CONSTRAINT_PRIMARY) ? |
| 2045 |
"PK_ConstraintTrigger" : |
| 2046 |
"Unique_ConstraintTrigger"; |
| 2047 |
trigger->relation = NULL; |
| 2048 |
trigger->funcname = SystemFuncName("unique_key_recheck"); |
| 2049 |
trigger->args = NIL; |
| 2050 |
trigger->row = true; |
| 2051 |
trigger->timing = TRIGGER_TYPE_AFTER; |
| 2052 |
trigger->events = TRIGGER_TYPE_INSERT | TRIGGER_TYPE_UPDATE; |
| 2053 |
trigger->columns = NIL; |
| 2054 |
trigger->whenClause = NULL; |
| 2055 |
trigger->transitionRels = NIL; |
| 2056 |
trigger->deferrable = true; |
| 2057 |
trigger->initdeferred = initdeferred; |
| 2058 |
trigger->constrrel = NULL; |
| 2059 |
|
| 2060 |
(void) CreateTrigger(trigger, NULL, RelationGetRelid(heapRelation), |
| 2061 |
InvalidOid, conOid, indexRelationId, InvalidOid, |
| 2062 |
InvalidOid, NULL, true, false); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* |
| 2066 |
* If needed, mark the index as primary and/or deferred in pg_index. |
| 2067 |
* |
| 2068 |
* Note: When making an existing index into a constraint, caller must have |
| 2069 |
* a table lock that prevents concurrent table updates; otherwise, there |
| 2070 |
* is a risk that concurrent readers of the table will miss seeing this |
| 2071 |
* index at all. |
| 2072 |
*/ |
| 2073 |
if ((constr_flags & INDEX_CONSTR_CREATE_UPDATE_INDEX) && |
| 2074 |
(mark_as_primary || deferrable)) |
| 2075 |
{ |
| 2076 |
Relation pg_index; |
| 2077 |
HeapTuple indexTuple; |
| 2078 |
Form_pg_index indexForm; |
| 2079 |
bool dirty = false; |
| 2080 |
bool marked_as_primary = false; |
| 2081 |
|
| 2082 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 2083 |
|
| 2084 |
indexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 2085 |
ObjectIdGetDatum(indexRelationId)); |
| 2086 |
if (!HeapTupleIsValid(indexTuple)) |
| 2087 |
elog(ERROR, "cache lookup failed for index %u", indexRelationId); |
| 2088 |
indexForm = (Form_pg_index) GETSTRUCT(indexTuple); |
| 2089 |
|
| 2090 |
if (mark_as_primary && !indexForm->indisprimary) |
| 2091 |
{ |
| 2092 |
indexForm->indisprimary = true; |
| 2093 |
dirty = true; |
| 2094 |
marked_as_primary = true; |
| 2095 |
} |
| 2096 |
|
| 2097 |
if (deferrable && indexForm->indimmediate) |
| 2098 |
{ |
| 2099 |
indexForm->indimmediate = false; |
| 2100 |
dirty = true; |
| 2101 |
} |
| 2102 |
|
| 2103 |
if (dirty) |
| 2104 |
{ |
| 2105 |
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple); |
| 2106 |
|
| 2107 |
/* |
| 2108 |
* When we mark an existing index as primary, force a relcache |
| 2109 |
* flush on its parent table, so that all sessions will become |
| 2110 |
* aware that the table now has a primary key. This is important |
| 2111 |
* because it affects some replication behaviors. |
| 2112 |
*/ |
| 2113 |
if (marked_as_primary) |
| 2114 |
CacheInvalidateRelcache(heapRelation); |
| 2115 |
|
| 2116 |
InvokeObjectPostAlterHookArg(IndexRelationId, indexRelationId, 0, |
| 2117 |
InvalidOid, is_internal); |
| 2118 |
} |
| 2119 |
|
| 2120 |
heap_freetuple(indexTuple); |
| 2121 |
table_close(pg_index, RowExclusiveLock); |
| 2122 |
} |
| 2123 |
|
| 2124 |
return myself; |
| 2125 |
} |
| 2126 |
|
| 2127 |
/* |
| 2128 |
* index_drop |
| 2129 |
* |
| 2130 |
* NOTE: this routine should now only be called through performDeletion(), |
| 2131 |
* else associated dependencies won't be cleaned up. |
| 2132 |
* |
| 2133 |
* If concurrent is true, do a DROP INDEX CONCURRENTLY. If concurrent is |
| 2134 |
* false but concurrent_lock_mode is true, then do a normal DROP INDEX but |
| 2135 |
* take a lock for CONCURRENTLY processing. That is used as part of REINDEX |
| 2136 |
* CONCURRENTLY. |
| 2137 |
*/ |
| 2138 |
void |
| 2139 |
index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode) |
| 2140 |
{ |
| 2141 |
Oid heapId; |
| 2142 |
Relation userHeapRelation; |
| 2143 |
Relation userIndexRelation; |
| 2144 |
Relation indexRelation; |
| 2145 |
HeapTuple tuple; |
| 2146 |
bool hasexprs; |
| 2147 |
LockRelId heaprelid, |
| 2148 |
indexrelid; |
| 2149 |
LOCKTAG heaplocktag; |
| 2150 |
LOCKMODE lockmode; |
| 2151 |
|
| 2152 |
/* |
| 2153 |
* A temporary relation uses a non-concurrent DROP. Other backends can't |
| 2154 |
* access a temporary relation, so there's no harm in grabbing a stronger |
| 2155 |
* lock (see comments in RemoveRelations), and a non-concurrent DROP is |
| 2156 |
* more efficient. |
| 2157 |
*/ |
| 2158 |
Assert(get_rel_persistence(indexId) != RELPERSISTENCE_TEMP || |
| 2159 |
(!concurrent && !concurrent_lock_mode)); |
| 2160 |
|
| 2161 |
/* |
| 2162 |
* To drop an index safely, we must grab exclusive lock on its parent |
| 2163 |
* table. Exclusive lock on the index alone is insufficient because |
| 2164 |
* another backend might be about to execute a query on the parent table. |
| 2165 |
* If it relies on a previously cached list of index OIDs, then it could |
| 2166 |
* attempt to access the just-dropped index. We must therefore take a |
| 2167 |
* table lock strong enough to prevent all queries on the table from |
| 2168 |
* proceeding until we commit and send out a shared-cache-inval notice |
| 2169 |
* that will make them update their index lists. |
| 2170 |
* |
| 2171 |
* In the concurrent case we avoid this requirement by disabling index use |
| 2172 |
* in multiple steps and waiting out any transactions that might be using |
| 2173 |
* the index, so we don't need exclusive lock on the parent table. Instead |
| 2174 |
* we take ShareUpdateExclusiveLock, to ensure that two sessions aren't |
| 2175 |
* doing CREATE/DROP INDEX CONCURRENTLY on the same index. (We will get |
| 2176 |
* AccessExclusiveLock on the index below, once we're sure nobody else is |
| 2177 |
* using it.) |
| 2178 |
*/ |
| 2179 |
heapId = IndexGetRelation(indexId, false); |
| 2180 |
lockmode = (concurrent || concurrent_lock_mode) ? ShareUpdateExclusiveLock : AccessExclusiveLock; |
| 2181 |
userHeapRelation = table_open(heapId, lockmode); |
| 2182 |
userIndexRelation = index_open(indexId, lockmode); |
| 2183 |
|
| 2184 |
/* |
| 2185 |
* We might still have open queries using it in our own session, which the |
| 2186 |
* above locking won't prevent, so test explicitly. |
| 2187 |
*/ |
| 2188 |
CheckTableNotInUse(userIndexRelation, "DROP INDEX"); |
| 2189 |
|
| 2190 |
/* |
| 2191 |
* Drop Index Concurrently is more or less the reverse process of Create |
| 2192 |
* Index Concurrently. |
| 2193 |
* |
| 2194 |
* First we unset indisvalid so queries starting afterwards don't use the |
| 2195 |
* index to answer queries anymore. We have to keep indisready = true so |
| 2196 |
* transactions that are still scanning the index can continue to see |
| 2197 |
* valid index contents. For instance, if they are using READ COMMITTED |
| 2198 |
* mode, and another transaction makes changes and commits, they need to |
| 2199 |
* see those new tuples in the index. |
| 2200 |
* |
| 2201 |
* After all transactions that could possibly have used the index for |
| 2202 |
* queries end, we can unset indisready and indislive, then wait till |
| 2203 |
* nobody could be touching it anymore. (Note: we need indislive because |
| 2204 |
* this state must be distinct from the initial state during CREATE INDEX |
| 2205 |
* CONCURRENTLY, which has indislive true while indisready and indisvalid |
| 2206 |
* are false. That's because in that state, transactions must examine the |
| 2207 |
* index for HOT-safety decisions, while in this state we don't want them |
| 2208 |
* to open it at all.) |
| 2209 |
* |
| 2210 |
* Since all predicate locks on the index are about to be made invalid, we |
| 2211 |
* must promote them to predicate locks on the heap. In the |
| 2212 |
* non-concurrent case we can just do that now. In the concurrent case |
| 2213 |
* it's a bit trickier. The predicate locks must be moved when there are |
| 2214 |
* no index scans in progress on the index and no more can subsequently |
| 2215 |
* start, so that no new predicate locks can be made on the index. Also, |
| 2216 |
* they must be moved before heap inserts stop maintaining the index, else |
| 2217 |
* the conflict with the predicate lock on the index gap could be missed |
| 2218 |
* before the lock on the heap relation is in place to detect a conflict |
| 2219 |
* based on the heap tuple insert. |
| 2220 |
*/ |
| 2221 |
if (concurrent) |
| 2222 |
{ |
| 2223 |
/* |
| 2224 |
* We must commit our transaction in order to make the first pg_index |
| 2225 |
* state update visible to other sessions. If the DROP machinery has |
| 2226 |
* already performed any other actions (removal of other objects, |
| 2227 |
* pg_depend entries, etc), the commit would make those actions |
| 2228 |
* permanent, which would leave us with inconsistent catalog state if |
| 2229 |
* we fail partway through the following sequence. Since DROP INDEX |
| 2230 |
* CONCURRENTLY is restricted to dropping just one index that has no |
| 2231 |
* dependencies, we should get here before anything's been done --- |
| 2232 |
* but let's check that to be sure. We can verify that the current |
| 2233 |
* transaction has not executed any transactional updates by checking |
| 2234 |
* that no XID has been assigned. |
| 2235 |
*/ |
| 2236 |
if (GetTopTransactionIdIfAny() != InvalidTransactionId) |
| 2237 |
ereport(ERROR, |
| 2238 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2239 |
errmsg("DROP INDEX CONCURRENTLY must be first action in transaction"))); |
| 2240 |
|
| 2241 |
/* |
| 2242 |
* Mark index invalid by updating its pg_index entry |
| 2243 |
*/ |
| 2244 |
index_set_state_flags(indexId, INDEX_DROP_CLEAR_VALID); |
| 2245 |
|
| 2246 |
/* |
| 2247 |
* Invalidate the relcache for the table, so that after this commit |
| 2248 |
* all sessions will refresh any cached plans that might reference the |
| 2249 |
* index. |
| 2250 |
*/ |
| 2251 |
CacheInvalidateRelcache(userHeapRelation); |
| 2252 |
|
| 2253 |
/* save lockrelid and locktag for below, then close but keep locks */ |
| 2254 |
heaprelid = userHeapRelation->rd_lockInfo.lockRelId; |
| 2255 |
SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId); |
| 2256 |
indexrelid = userIndexRelation->rd_lockInfo.lockRelId; |
| 2257 |
|
| 2258 |
table_close(userHeapRelation, NoLock); |
| 2259 |
index_close(userIndexRelation, NoLock); |
| 2260 |
|
| 2261 |
/* |
| 2262 |
* We must commit our current transaction so that the indisvalid |
| 2263 |
* update becomes visible to other transactions; then start another. |
| 2264 |
* Note that any previously-built data structures are lost in the |
| 2265 |
* commit. The only data we keep past here are the relation IDs. |
| 2266 |
* |
| 2267 |
* Before committing, get a session-level lock on the table, to ensure |
| 2268 |
* that neither it nor the index can be dropped before we finish. This |
| 2269 |
* cannot block, even if someone else is waiting for access, because |
| 2270 |
* we already have the same lock within our transaction. |
| 2271 |
*/ |
| 2272 |
LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock); |
| 2273 |
LockRelationIdForSession(&indexrelid, ShareUpdateExclusiveLock); |
| 2274 |
|
| 2275 |
PopActiveSnapshot(); |
| 2276 |
CommitTransactionCommand(); |
| 2277 |
StartTransactionCommand(); |
| 2278 |
|
| 2279 |
/* |
| 2280 |
* Now we must wait until no running transaction could be using the |
| 2281 |
* index for a query. Use AccessExclusiveLock here to check for |
| 2282 |
* running transactions that hold locks of any kind on the table. Note |
| 2283 |
* we do not need to worry about xacts that open the table for reading |
| 2284 |
* after this point; they will see the index as invalid when they open |
| 2285 |
* the relation. |
| 2286 |
* |
| 2287 |
* Note: the reason we use actual lock acquisition here, rather than |
| 2288 |
* just checking the ProcArray and sleeping, is that deadlock is |
| 2289 |
* possible if one of the transactions in question is blocked trying |
| 2290 |
* to acquire an exclusive lock on our table. The lock code will |
| 2291 |
* detect deadlock and error out properly. |
| 2292 |
* |
| 2293 |
* Note: we report progress through WaitForLockers() unconditionally |
| 2294 |
* here, even though it will only be used when we're called by REINDEX |
| 2295 |
* CONCURRENTLY and not when called by DROP INDEX CONCURRENTLY. |
| 2296 |
*/ |
| 2297 |
WaitForLockers(heaplocktag, AccessExclusiveLock, true); |
| 2298 |
|
| 2299 |
/* |
| 2300 |
* Updating pg_index might involve TOAST table access, so ensure we |
| 2301 |
* have a valid snapshot. |
| 2302 |
*/ |
| 2303 |
PushActiveSnapshot(GetTransactionSnapshot()); |
| 2304 |
|
| 2305 |
/* Finish invalidation of index and mark it as dead */ |
| 2306 |
index_concurrently_set_dead(heapId, indexId); |
| 2307 |
|
| 2308 |
PopActiveSnapshot(); |
| 2309 |
|
| 2310 |
/* |
| 2311 |
* Again, commit the transaction to make the pg_index update visible |
| 2312 |
* to other sessions. |
| 2313 |
*/ |
| 2314 |
CommitTransactionCommand(); |
| 2315 |
StartTransactionCommand(); |
| 2316 |
|
| 2317 |
/* |
| 2318 |
* Wait till every transaction that saw the old index state has |
| 2319 |
* finished. See above about progress reporting. |
| 2320 |
*/ |
| 2321 |
WaitForLockers(heaplocktag, AccessExclusiveLock, true); |
| 2322 |
|
| 2323 |
/* |
| 2324 |
* Re-open relations to allow us to complete our actions. |
| 2325 |
* |
| 2326 |
* At this point, nothing should be accessing the index, but lets |
| 2327 |
* leave nothing to chance and grab AccessExclusiveLock on the index |
| 2328 |
* before the physical deletion. |
| 2329 |
*/ |
| 2330 |
userHeapRelation = table_open(heapId, ShareUpdateExclusiveLock); |
| 2331 |
userIndexRelation = index_open(indexId, AccessExclusiveLock); |
| 2332 |
} |
| 2333 |
else |
| 2334 |
{ |
| 2335 |
/* Not concurrent, so just transfer predicate locks and we're good */ |
| 2336 |
TransferPredicateLocksToHeapRelation(userIndexRelation); |
| 2337 |
} |
| 2338 |
|
| 2339 |
/* |
| 2340 |
* Schedule physical removal of the files (if any) |
| 2341 |
*/ |
| 2342 |
if (RELKIND_HAS_STORAGE(userIndexRelation->rd_rel->relkind)) |
| 2343 |
RelationDropStorage(userIndexRelation); |
| 2344 |
|
| 2345 |
/* ensure that stats are dropped if transaction commits */ |
| 2346 |
pgstat_drop_relation(userIndexRelation); |
| 2347 |
|
| 2348 |
/* |
| 2349 |
* Close and flush the index's relcache entry, to ensure relcache doesn't |
| 2350 |
* try to rebuild it while we're deleting catalog entries. We keep the |
| 2351 |
* lock though. |
| 2352 |
*/ |
| 2353 |
index_close(userIndexRelation, NoLock); |
| 2354 |
|
| 2355 |
RelationForgetRelation(indexId); |
| 2356 |
|
| 2357 |
/* |
| 2358 |
* Updating pg_index might involve TOAST table access, so ensure we have a |
| 2359 |
* valid snapshot. |
| 2360 |
*/ |
| 2361 |
PushActiveSnapshot(GetTransactionSnapshot()); |
| 2362 |
|
| 2363 |
/* |
| 2364 |
* fix INDEX relation, and check for expressional index |
| 2365 |
*/ |
| 2366 |
indexRelation = table_open(IndexRelationId, RowExclusiveLock); |
| 2367 |
|
| 2368 |
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId)); |
| 2369 |
if (!HeapTupleIsValid(tuple)) |
| 2370 |
elog(ERROR, "cache lookup failed for index %u", indexId); |
| 2371 |
|
| 2372 |
hasexprs = !heap_attisnull(tuple, Anum_pg_index_indexprs, |
| 2373 |
RelationGetDescr(indexRelation)); |
| 2374 |
|
| 2375 |
CatalogTupleDelete(indexRelation, &tuple->t_self); |
| 2376 |
|
| 2377 |
ReleaseSysCache(tuple); |
| 2378 |
table_close(indexRelation, RowExclusiveLock); |
| 2379 |
|
| 2380 |
PopActiveSnapshot(); |
| 2381 |
|
| 2382 |
/* |
| 2383 |
* if it has any expression columns, we might have stored statistics about |
| 2384 |
* them. |
| 2385 |
*/ |
| 2386 |
if (hasexprs) |
| 2387 |
RemoveStatistics(indexId, 0); |
| 2388 |
|
| 2389 |
/* |
| 2390 |
* fix ATTRIBUTE relation |
| 2391 |
*/ |
| 2392 |
DeleteAttributeTuples(indexId); |
| 2393 |
|
| 2394 |
/* |
| 2395 |
* fix RELATION relation |
| 2396 |
*/ |
| 2397 |
DeleteRelationTuple(indexId); |
| 2398 |
|
| 2399 |
/* |
| 2400 |
* fix INHERITS relation |
| 2401 |
*/ |
| 2402 |
DeleteInheritsTuple(indexId, InvalidOid, false, NULL); |
| 2403 |
|
| 2404 |
/* |
| 2405 |
* We are presently too lazy to attempt to compute the new correct value |
| 2406 |
* of relhasindex (the next VACUUM will fix it if necessary). So there is |
| 2407 |
* no need to update the pg_class tuple for the owning relation. But we |
| 2408 |
* must send out a shared-cache-inval notice on the owning relation to |
| 2409 |
* ensure other backends update their relcache lists of indexes. (In the |
| 2410 |
* concurrent case, this is redundant but harmless.) |
| 2411 |
*/ |
| 2412 |
CacheInvalidateRelcache(userHeapRelation); |
| 2413 |
|
| 2414 |
/* |
| 2415 |
* Close owning rel, but keep lock |
| 2416 |
*/ |
| 2417 |
table_close(userHeapRelation, NoLock); |
| 2418 |
|
| 2419 |
/* |
| 2420 |
* Release the session locks before we go. |
| 2421 |
*/ |
| 2422 |
if (concurrent) |
| 2423 |
{ |
| 2424 |
UnlockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock); |
| 2425 |
UnlockRelationIdForSession(&indexrelid, ShareUpdateExclusiveLock); |
| 2426 |
} |
| 2427 |
} |
| 2428 |
|
| 2429 |
/* ---------------------------------------------------------------- |
| 2430 |
* index_build support |
| 2431 |
* ---------------------------------------------------------------- |
| 2432 |
*/ |
| 2433 |
|
| 2434 |
/* ---------------- |
| 2435 |
* BuildIndexInfo |
| 2436 |
* Construct an IndexInfo record for an open index |
| 2437 |
* |
| 2438 |
* IndexInfo stores the information about the index that's needed by |
| 2439 |
* FormIndexDatum, which is used for both index_build() and later insertion |
| 2440 |
* of individual index tuples. Normally we build an IndexInfo for an index |
| 2441 |
* just once per command, and then use it for (potentially) many tuples. |
| 2442 |
* ---------------- |
| 2443 |
*/ |
| 2444 |
IndexInfo * |
| 2445 |
BuildIndexInfo(Relation index) |
| 2446 |
{ |
| 2447 |
IndexInfo *ii; |
| 2448 |
Form_pg_index indexStruct = index->rd_index; |
| 2449 |
int i; |
| 2450 |
int numAtts; |
| 2451 |
|
| 2452 |
/* check the number of keys, and copy attr numbers into the IndexInfo */ |
| 2453 |
numAtts = indexStruct->indnatts; |
| 2454 |
if (numAtts < 1 || numAtts > INDEX_MAX_KEYS) |
| 2455 |
elog(ERROR, "invalid indnatts %d for index %u", |
| 2456 |
numAtts, RelationGetRelid(index)); |
| 2457 |
|
| 2458 |
/* |
| 2459 |
* Create the node, fetching any expressions needed for expressional |
| 2460 |
* indexes and index predicate if any. |
| 2461 |
*/ |
| 2462 |
ii = makeIndexInfo(indexStruct->indnatts, |
| 2463 |
indexStruct->indnkeyatts, |
| 2464 |
index->rd_rel->relam, |
| 2465 |
RelationGetIndexExpressions(index), |
| 2466 |
RelationGetIndexPredicate(index), |
| 2467 |
indexStruct->indisunique, |
| 2468 |
indexStruct->indnullsnotdistinct, |
| 2469 |
indexStruct->indisready, |
| 2470 |
false, |
| 2471 |
index->rd_indam->amsummarizing, |
| 2472 |
indexStruct->indisexclusion && indexStruct->indisunique); |
| 2473 |
|
| 2474 |
/* fill in attribute numbers */ |
| 2475 |
for (i = 0; i < numAtts; i++) |
| 2476 |
ii->ii_IndexAttrNumbers[i] = indexStruct->indkey.values[i]; |
| 2477 |
|
| 2478 |
/* fetch exclusion constraint info if any */ |
| 2479 |
if (indexStruct->indisexclusion) |
| 2480 |
{ |
| 2481 |
RelationGetExclusionInfo(index, |
| 2482 |
&ii->ii_ExclusionOps, |
| 2483 |
&ii->ii_ExclusionProcs, |
| 2484 |
&ii->ii_ExclusionStrats); |
| 2485 |
} |
| 2486 |
|
| 2487 |
return ii; |
| 2488 |
} |
| 2489 |
|
| 2490 |
/* ---------------- |
| 2491 |
* BuildDummyIndexInfo |
| 2492 |
* Construct a dummy IndexInfo record for an open index |
| 2493 |
* |
| 2494 |
* This differs from the real BuildIndexInfo in that it will never run any |
| 2495 |
* user-defined code that might exist in index expressions or predicates. |
| 2496 |
* Instead of the real index expressions, we return null constants that have |
| 2497 |
* the right types/typmods/collations. Predicates and exclusion clauses are |
| 2498 |
* just ignored. This is sufficient for the purpose of truncating an index, |
| 2499 |
* since we will not need to actually evaluate the expressions or predicates; |
| 2500 |
* the only thing that's likely to be done with the data is construction of |
| 2501 |
* a tupdesc describing the index's rowtype. |
| 2502 |
* ---------------- |
| 2503 |
*/ |
| 2504 |
IndexInfo * |
| 2505 |
BuildDummyIndexInfo(Relation index) |
| 2506 |
{ |
| 2507 |
IndexInfo *ii; |
| 2508 |
Form_pg_index indexStruct = index->rd_index; |
| 2509 |
int i; |
| 2510 |
int numAtts; |
| 2511 |
|
| 2512 |
/* check the number of keys, and copy attr numbers into the IndexInfo */ |
| 2513 |
numAtts = indexStruct->indnatts; |
| 2514 |
if (numAtts < 1 || numAtts > INDEX_MAX_KEYS) |
| 2515 |
elog(ERROR, "invalid indnatts %d for index %u", |
| 2516 |
numAtts, RelationGetRelid(index)); |
| 2517 |
|
| 2518 |
/* |
| 2519 |
* Create the node, using dummy index expressions, and pretending there is |
| 2520 |
* no predicate. |
| 2521 |
*/ |
| 2522 |
ii = makeIndexInfo(indexStruct->indnatts, |
| 2523 |
indexStruct->indnkeyatts, |
| 2524 |
index->rd_rel->relam, |
| 2525 |
RelationGetDummyIndexExpressions(index), |
| 2526 |
NIL, |
| 2527 |
indexStruct->indisunique, |
| 2528 |
indexStruct->indnullsnotdistinct, |
| 2529 |
indexStruct->indisready, |
| 2530 |
false, |
| 2531 |
index->rd_indam->amsummarizing, |
| 2532 |
indexStruct->indisexclusion && indexStruct->indisunique); |
| 2533 |
|
| 2534 |
/* fill in attribute numbers */ |
| 2535 |
for (i = 0; i < numAtts; i++) |
| 2536 |
ii->ii_IndexAttrNumbers[i] = indexStruct->indkey.values[i]; |
| 2537 |
|
| 2538 |
/* We ignore the exclusion constraint if any */ |
| 2539 |
|
| 2540 |
return ii; |
| 2541 |
} |
| 2542 |
|
| 2543 |
/* |
| 2544 |
* CompareIndexInfo |
| 2545 |
* Return whether the properties of two indexes (in different tables) |
| 2546 |
* indicate that they have the "same" definitions. |
| 2547 |
* |
| 2548 |
* Note: passing collations and opfamilies separately is a kludge. Adding |
| 2549 |
* them to IndexInfo may result in better coding here and elsewhere. |
| 2550 |
* |
| 2551 |
* Use build_attrmap_by_name(index2, index1) to build the attmap. |
| 2552 |
*/ |
| 2553 |
bool |
| 2554 |
CompareIndexInfo(const IndexInfo *info1, const IndexInfo *info2, |
| 2555 |
const Oid *collations1, const Oid *collations2, |
| 2556 |
const Oid *opfamilies1, const Oid *opfamilies2, |
| 2557 |
const AttrMap *attmap) |
| 2558 |
{ |
| 2559 |
int i; |
| 2560 |
|
| 2561 |
if (info1->ii_Unique != info2->ii_Unique) |
| 2562 |
return false; |
| 2563 |
|
| 2564 |
if (info1->ii_NullsNotDistinct != info2->ii_NullsNotDistinct) |
| 2565 |
return false; |
| 2566 |
|
| 2567 |
/* indexes are only equivalent if they have the same access method */ |
| 2568 |
if (info1->ii_Am != info2->ii_Am) |
| 2569 |
return false; |
| 2570 |
|
| 2571 |
/* and same number of attributes */ |
| 2572 |
if (info1->ii_NumIndexAttrs != info2->ii_NumIndexAttrs) |
| 2573 |
return false; |
| 2574 |
|
| 2575 |
/* and same number of key attributes */ |
| 2576 |
if (info1->ii_NumIndexKeyAttrs != info2->ii_NumIndexKeyAttrs) |
| 2577 |
return false; |
| 2578 |
|
| 2579 |
/* |
| 2580 |
* and columns match through the attribute map (actual attribute numbers |
| 2581 |
* might differ!) Note that this checks that index columns that are |
| 2582 |
* expressions appear in the same positions. We will next compare the |
| 2583 |
* expressions themselves. |
| 2584 |
*/ |
| 2585 |
for (i = 0; i < info1->ii_NumIndexAttrs; i++) |
| 2586 |
{ |
| 2587 |
if (attmap->maplen < info2->ii_IndexAttrNumbers[i]) |
| 2588 |
elog(ERROR, "incorrect attribute map"); |
| 2589 |
|
| 2590 |
/* ignore expressions for now (but check their collation/opfamily) */ |
| 2591 |
if (!(info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber && |
| 2592 |
info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber)) |
| 2593 |
{ |
| 2594 |
/* fail if just one index has an expression in this column */ |
| 2595 |
if (info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber || |
| 2596 |
info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber) |
| 2597 |
return false; |
| 2598 |
|
| 2599 |
/* both are columns, so check for match after mapping */ |
| 2600 |
if (attmap->attnums[info2->ii_IndexAttrNumbers[i] - 1] != |
| 2601 |
info1->ii_IndexAttrNumbers[i]) |
| 2602 |
return false; |
| 2603 |
} |
| 2604 |
|
| 2605 |
/* collation and opfamily are not valid for included columns */ |
| 2606 |
if (i >= info1->ii_NumIndexKeyAttrs) |
| 2607 |
continue; |
| 2608 |
|
| 2609 |
if (collations1[i] != collations2[i]) |
| 2610 |
return false; |
| 2611 |
if (opfamilies1[i] != opfamilies2[i]) |
| 2612 |
return false; |
| 2613 |
} |
| 2614 |
|
| 2615 |
/* |
| 2616 |
* For expression indexes: either both are expression indexes, or neither |
| 2617 |
* is; if they are, make sure the expressions match. |
| 2618 |
*/ |
| 2619 |
if ((info1->ii_Expressions != NIL) != (info2->ii_Expressions != NIL)) |
| 2620 |
return false; |
| 2621 |
if (info1->ii_Expressions != NIL) |
| 2622 |
{ |
| 2623 |
bool found_whole_row; |
| 2624 |
Node *mapped; |
| 2625 |
|
| 2626 |
mapped = map_variable_attnos((Node *) info2->ii_Expressions, |
| 2627 |
1, 0, attmap, |
| 2628 |
InvalidOid, &found_whole_row); |
| 2629 |
if (found_whole_row) |
| 2630 |
{ |
| 2631 |
/* |
| 2632 |
* we could throw an error here, but seems out of scope for this |
| 2633 |
* routine. |
| 2634 |
*/ |
| 2635 |
return false; |
| 2636 |
} |
| 2637 |
|
| 2638 |
if (!equal(info1->ii_Expressions, mapped)) |
| 2639 |
return false; |
| 2640 |
} |
| 2641 |
|
| 2642 |
/* Partial index predicates must be identical, if they exist */ |
| 2643 |
if ((info1->ii_Predicate == NULL) != (info2->ii_Predicate == NULL)) |
| 2644 |
return false; |
| 2645 |
if (info1->ii_Predicate != NULL) |
| 2646 |
{ |
| 2647 |
bool found_whole_row; |
| 2648 |
Node *mapped; |
| 2649 |
|
| 2650 |
mapped = map_variable_attnos((Node *) info2->ii_Predicate, |
| 2651 |
1, 0, attmap, |
| 2652 |
InvalidOid, &found_whole_row); |
| 2653 |
if (found_whole_row) |
| 2654 |
{ |
| 2655 |
/* |
| 2656 |
* we could throw an error here, but seems out of scope for this |
| 2657 |
* routine. |
| 2658 |
*/ |
| 2659 |
return false; |
| 2660 |
} |
| 2661 |
if (!equal(info1->ii_Predicate, mapped)) |
| 2662 |
return false; |
| 2663 |
} |
| 2664 |
|
| 2665 |
/* If they're exclusion indexes, their properties must be identical */ |
| 2666 |
if ((info1->ii_ExclusionOps == NULL) != (info2->ii_ExclusionOps == NULL)) |
| 2667 |
return false; |
| 2668 |
if (info1->ii_ExclusionOps != NULL) |
| 2669 |
{ |
| 2670 |
for (i = 0; i < info1->ii_NumIndexKeyAttrs; i++) |
| 2671 |
{ |
| 2672 |
if (info1->ii_ExclusionOps[i] != info2->ii_ExclusionOps[i]) |
| 2673 |
return false; |
| 2674 |
if (info1->ii_ExclusionProcs[i] != info2->ii_ExclusionProcs[i]) |
| 2675 |
return false; |
| 2676 |
if (info1->ii_ExclusionStrats[i] != info2->ii_ExclusionStrats[i]) |
| 2677 |
return false; |
| 2678 |
} |
| 2679 |
} |
| 2680 |
|
| 2681 |
return true; |
| 2682 |
} |
| 2683 |
|
| 2684 |
/* ---------------- |
| 2685 |
* BuildSpeculativeIndexInfo |
| 2686 |
* Add extra state to IndexInfo record |
| 2687 |
* |
| 2688 |
* For unique indexes, we usually don't want to add info to the IndexInfo for |
| 2689 |
* checking uniqueness, since the B-Tree AM handles that directly. However, in |
| 2690 |
* the case of speculative insertion and conflict detection in logical |
| 2691 |
* replication, additional support is required. |
| 2692 |
* |
| 2693 |
* Do this processing here rather than in BuildIndexInfo() to not incur the |
| 2694 |
* overhead in the common non-speculative cases. |
| 2695 |
* ---------------- |
| 2696 |
*/ |
| 2697 |
void |
| 2698 |
BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii) |
| 2699 |
{ |
| 2700 |
int indnkeyatts; |
| 2701 |
int i; |
| 2702 |
|
| 2703 |
indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index); |
| 2704 |
|
| 2705 |
/* |
| 2706 |
* fetch info for checking unique indexes |
| 2707 |
*/ |
| 2708 |
Assert(ii->ii_Unique); |
| 2709 |
|
| 2710 |
ii->ii_UniqueOps = (Oid *) palloc(sizeof(Oid) * indnkeyatts); |
| 2711 |
ii->ii_UniqueProcs = (Oid *) palloc(sizeof(Oid) * indnkeyatts); |
| 2712 |
ii->ii_UniqueStrats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts); |
| 2713 |
|
| 2714 |
/* |
| 2715 |
* We have to look up the operator's strategy number. This provides a |
| 2716 |
* cross-check that the operator does match the index. |
| 2717 |
*/ |
| 2718 |
/* We need the func OIDs and strategy numbers too */ |
| 2719 |
for (i = 0; i < indnkeyatts; i++) |
| 2720 |
{ |
| 2721 |
ii->ii_UniqueStrats[i] = |
| 2722 |
IndexAmTranslateCompareType(COMPARE_EQ, |
| 2723 |
index->rd_rel->relam, |
| 2724 |
index->rd_opfamily[i], |
| 2725 |
false); |
| 2726 |
ii->ii_UniqueOps[i] = |
| 2727 |
get_opfamily_member(index->rd_opfamily[i], |
| 2728 |
index->rd_opcintype[i], |
| 2729 |
index->rd_opcintype[i], |
| 2730 |
ii->ii_UniqueStrats[i]); |
| 2731 |
if (!OidIsValid(ii->ii_UniqueOps[i])) |
| 2732 |
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u", |
| 2733 |
ii->ii_UniqueStrats[i], index->rd_opcintype[i], |
| 2734 |
index->rd_opcintype[i], index->rd_opfamily[i]); |
| 2735 |
ii->ii_UniqueProcs[i] = get_opcode(ii->ii_UniqueOps[i]); |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
/* ---------------- |
| 2740 |
* FormIndexDatum |
| 2741 |
* Construct values[] and isnull[] arrays for a new index tuple. |
| 2742 |
* |
| 2743 |
* indexInfo Info about the index |
| 2744 |
* slot Heap tuple for which we must prepare an index entry |
| 2745 |
* estate executor state for evaluating any index expressions |
| 2746 |
* values Array of index Datums (output area) |
| 2747 |
* isnull Array of is-null indicators (output area) |
| 2748 |
* |
| 2749 |
* When there are no index expressions, estate may be NULL. Otherwise it |
| 2750 |
* must be supplied, *and* the ecxt_scantuple slot of its per-tuple expr |
| 2751 |
* context must point to the heap tuple passed in. |
| 2752 |
* |
| 2753 |
* Notice we don't actually call index_form_tuple() here; we just prepare |
| 2754 |
* its input arrays values[] and isnull[]. This is because the index AM |
| 2755 |
* may wish to alter the data before storage. |
| 2756 |
* ---------------- |
| 2757 |
*/ |
| 2758 |
void |
| 2759 |
FormIndexDatum(IndexInfo *indexInfo, |
| 2760 |
TupleTableSlot *slot, |
| 2761 |
EState *estate, |
| 2762 |
Datum *values, |
| 2763 |
bool *isnull) |
| 2764 |
{ |
| 2765 |
ListCell *indexpr_item; |
| 2766 |
int i; |
| 2767 |
|
| 2768 |
if (indexInfo->ii_Expressions != NIL && |
| 2769 |
indexInfo->ii_ExpressionsState == NIL) |
| 2770 |
{ |
| 2771 |
/* First time through, set up expression evaluation state */ |
| 2772 |
indexInfo->ii_ExpressionsState = |
| 2773 |
ExecPrepareExprList(indexInfo->ii_Expressions, estate); |
| 2774 |
/* Check caller has set up context correctly */ |
| 2775 |
Assert(GetPerTupleExprContext(estate)->ecxt_scantuple == slot); |
| 2776 |
} |
| 2777 |
indexpr_item = list_head(indexInfo->ii_ExpressionsState); |
| 2778 |
|
| 2779 |
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) |
| 2780 |
{ |
| 2781 |
int keycol = indexInfo->ii_IndexAttrNumbers[i]; |
| 2782 |
Datum iDatum; |
| 2783 |
bool isNull; |
| 2784 |
|
| 2785 |
if (keycol < 0) |
| 2786 |
iDatum = slot_getsysattr(slot, keycol, &isNull); |
| 2787 |
else if (keycol != 0) |
| 2788 |
{ |
| 2789 |
/* |
| 2790 |
* Plain index column; get the value we need directly from the |
| 2791 |
* heap tuple. |
| 2792 |
*/ |
| 2793 |
iDatum = slot_getattr(slot, keycol, &isNull); |
| 2794 |
} |
| 2795 |
else |
| 2796 |
{ |
| 2797 |
/* |
| 2798 |
* Index expression --- need to evaluate it. |
| 2799 |
*/ |
| 2800 |
if (indexpr_item == NULL) |
| 2801 |
elog(ERROR, "wrong number of index expressions"); |
| 2802 |
iDatum = ExecEvalExprSwitchContext((ExprState *) lfirst(indexpr_item), |
| 2803 |
GetPerTupleExprContext(estate), |
| 2804 |
&isNull); |
| 2805 |
indexpr_item = lnext(indexInfo->ii_ExpressionsState, indexpr_item); |
| 2806 |
} |
| 2807 |
values[i] = iDatum; |
| 2808 |
isnull[i] = isNull; |
| 2809 |
} |
| 2810 |
|
| 2811 |
if (indexpr_item != NULL) |
| 2812 |
elog(ERROR, "wrong number of index expressions"); |
| 2813 |
} |
| 2814 |
|
| 2815 |
|
| 2816 |
/* |
| 2817 |
* index_update_stats --- update pg_class entry after CREATE INDEX or REINDEX |
| 2818 |
* |
| 2819 |
* This routine updates the pg_class row of either an index or its parent |
| 2820 |
* relation after CREATE INDEX or REINDEX. Its rather bizarre API is designed |
| 2821 |
* to ensure we can do all the necessary work in just one update. |
| 2822 |
* |
| 2823 |
* hasindex: set relhasindex to this value |
| 2824 |
* reltuples: if >= 0, set reltuples to this value; else no change |
| 2825 |
* |
| 2826 |
* If reltuples >= 0, relpages, relallvisible, and relallfrozen are also |
| 2827 |
* updated (using RelationGetNumberOfBlocks() and visibilitymap_count()). |
| 2828 |
* |
| 2829 |
* NOTE: an important side-effect of this operation is that an SI invalidation |
| 2830 |
* message is sent out to all backends --- including me --- causing relcache |
| 2831 |
* entries to be flushed or updated with the new data. This must happen even |
| 2832 |
* if we find that no change is needed in the pg_class row. When updating |
| 2833 |
* a heap entry, this ensures that other backends find out about the new |
| 2834 |
* index. When updating an index, it's important because some index AMs |
| 2835 |
* expect a relcache flush to occur after REINDEX. |
| 2836 |
*/ |
| 2837 |
static void |
| 2838 |
index_update_stats(Relation rel, |
| 2839 |
bool hasindex, |
| 2840 |
double reltuples) |
| 2841 |
{ |
| 2842 |
bool update_stats; |
| 2843 |
BlockNumber relpages = 0; /* keep compiler quiet */ |
| 2844 |
BlockNumber relallvisible = 0; |
| 2845 |
BlockNumber relallfrozen = 0; |
| 2846 |
Oid relid = RelationGetRelid(rel); |
| 2847 |
Relation pg_class; |
| 2848 |
ScanKeyData key[1]; |
| 2849 |
HeapTuple tuple; |
| 2850 |
void *state; |
| 2851 |
Form_pg_class rd_rel; |
| 2852 |
bool dirty; |
| 2853 |
|
| 2854 |
/* |
| 2855 |
* As a special hack, if we are dealing with an empty table and the |
| 2856 |
* existing reltuples is -1, we leave that alone. This ensures that |
| 2857 |
* creating an index as part of CREATE TABLE doesn't cause the table to |
| 2858 |
* prematurely look like it's been vacuumed. The rd_rel we modify may |
| 2859 |
* differ from rel->rd_rel due to e.g. commit of concurrent GRANT, but the |
| 2860 |
* commands that change reltuples take locks conflicting with ours. (Even |
| 2861 |
* if a command changed reltuples under a weaker lock, this affects only |
| 2862 |
* statistics for an empty table.) |
| 2863 |
*/ |
| 2864 |
if (reltuples == 0 && rel->rd_rel->reltuples < 0) |
| 2865 |
reltuples = -1; |
| 2866 |
|
| 2867 |
/* |
| 2868 |
* Don't update statistics during binary upgrade, because the indexes are |
| 2869 |
* created before the data is moved into place. |
| 2870 |
*/ |
| 2871 |
update_stats = reltuples >= 0 && !IsBinaryUpgrade; |
| 2872 |
|
| 2873 |
/* |
| 2874 |
* If autovacuum is off, user may not be expecting table relstats to |
| 2875 |
* change. This can be important when restoring a dump that includes |
| 2876 |
* statistics, as the table statistics may be restored before the index is |
| 2877 |
* created, and we want to preserve the restored table statistics. |
| 2878 |
*/ |
| 2879 |
if (rel->rd_rel->relkind == RELKIND_RELATION || |
| 2880 |
rel->rd_rel->relkind == RELKIND_TOASTVALUE || |
| 2881 |
rel->rd_rel->relkind == RELKIND_MATVIEW) |
| 2882 |
{ |
| 2883 |
if (AutoVacuumingActive()) |
| 2884 |
{ |
| 2885 |
StdRdOptions *options = (StdRdOptions *) rel->rd_options; |
| 2886 |
|
| 2887 |
if (options != NULL && !options->autovacuum.enabled) |
| 2888 |
update_stats = false; |
| 2889 |
} |
| 2890 |
else |
| 2891 |
update_stats = false; |
| 2892 |
} |
| 2893 |
|
| 2894 |
/* |
| 2895 |
* Finish I/O and visibility map buffer locks before |
| 2896 |
* systable_inplace_update_begin() locks the pg_class buffer. The rd_rel |
| 2897 |
* we modify may differ from rel->rd_rel due to e.g. commit of concurrent |
| 2898 |
* GRANT, but no command changes a relkind from non-index to index. (Even |
| 2899 |
* if one did, relallvisible doesn't break functionality.) |
| 2900 |
*/ |
| 2901 |
if (update_stats) |
| 2902 |
{ |
| 2903 |
relpages = RelationGetNumberOfBlocks(rel); |
| 2904 |
|
| 2905 |
if (rel->rd_rel->relkind != RELKIND_INDEX) |
| 2906 |
visibilitymap_count(rel, &relallvisible, &relallfrozen); |
| 2907 |
} |
| 2908 |
|
| 2909 |
/* |
| 2910 |
* We always update the pg_class row using a non-transactional, |
| 2911 |
* overwrite-in-place update. There are several reasons for this: |
| 2912 |
* |
| 2913 |
* 1. In bootstrap mode, we have no choice --- UPDATE wouldn't work. |
| 2914 |
* |
| 2915 |
* 2. We could be reindexing pg_class itself, in which case we can't move |
| 2916 |
* its pg_class row because CatalogTupleInsert/CatalogTupleUpdate might |
| 2917 |
* not know about all the indexes yet (see reindex_relation). |
| 2918 |
* |
| 2919 |
* 3. Because we execute CREATE INDEX with just share lock on the parent |
| 2920 |
* rel (to allow concurrent index creations), an ordinary update could |
| 2921 |
* suffer a tuple-concurrently-updated failure against another CREATE |
| 2922 |
* INDEX committing at about the same time. We can avoid that by having |
| 2923 |
* them both do nontransactional updates (we assume they will both be |
| 2924 |
* trying to change the pg_class row to the same thing, so it doesn't |
| 2925 |
* matter which goes first). |
| 2926 |
* |
| 2927 |
* It is safe to use a non-transactional update even though our |
| 2928 |
* transaction could still fail before committing. Setting relhasindex |
| 2929 |
* true is safe even if there are no indexes (VACUUM will eventually fix |
| 2930 |
* it). And of course the new relpages and reltuples counts are correct |
| 2931 |
* regardless. However, we don't want to change relpages (or |
| 2932 |
* relallvisible) if the caller isn't providing an updated reltuples |
| 2933 |
* count, because that would bollix the reltuples/relpages ratio which is |
| 2934 |
* what's really important. |
| 2935 |
*/ |
| 2936 |
|
| 2937 |
pg_class = table_open(RelationRelationId, RowExclusiveLock); |
| 2938 |
|
| 2939 |
ScanKeyInit(&key[0], |
| 2940 |
Anum_pg_class_oid, |
| 2941 |
BTEqualStrategyNumber, F_OIDEQ, |
| 2942 |
ObjectIdGetDatum(relid)); |
| 2943 |
systable_inplace_update_begin(pg_class, ClassOidIndexId, true, NULL, |
| 2944 |
1, key, &tuple, &state); |
| 2945 |
|
| 2946 |
if (!HeapTupleIsValid(tuple)) |
| 2947 |
elog(ERROR, "could not find tuple for relation %u", relid); |
| 2948 |
rd_rel = (Form_pg_class) GETSTRUCT(tuple); |
| 2949 |
|
| 2950 |
/* Should this be a more comprehensive test? */ |
| 2951 |
Assert(rd_rel->relkind != RELKIND_PARTITIONED_INDEX); |
| 2952 |
|
| 2953 |
/* Apply required updates, if any, to copied tuple */ |
| 2954 |
|
| 2955 |
dirty = false; |
| 2956 |
if (rd_rel->relhasindex != hasindex) |
| 2957 |
{ |
| 2958 |
rd_rel->relhasindex = hasindex; |
| 2959 |
dirty = true; |
| 2960 |
} |
| 2961 |
|
| 2962 |
if (update_stats) |
| 2963 |
{ |
| 2964 |
if (rd_rel->relpages != (int32) relpages) |
| 2965 |
{ |
| 2966 |
rd_rel->relpages = (int32) relpages; |
| 2967 |
dirty = true; |
| 2968 |
} |
| 2969 |
if (rd_rel->reltuples != (float4) reltuples) |
| 2970 |
{ |
| 2971 |
rd_rel->reltuples = (float4) reltuples; |
| 2972 |
dirty = true; |
| 2973 |
} |
| 2974 |
if (rd_rel->relallvisible != (int32) relallvisible) |
| 2975 |
{ |
| 2976 |
rd_rel->relallvisible = (int32) relallvisible; |
| 2977 |
dirty = true; |
| 2978 |
} |
| 2979 |
if (rd_rel->relallfrozen != (int32) relallfrozen) |
| 2980 |
{ |
| 2981 |
rd_rel->relallfrozen = (int32) relallfrozen; |
| 2982 |
dirty = true; |
| 2983 |
} |
| 2984 |
} |
| 2985 |
|
| 2986 |
/* |
| 2987 |
* If anything changed, write out the tuple |
| 2988 |
*/ |
| 2989 |
if (dirty) |
| 2990 |
{ |
| 2991 |
systable_inplace_update_finish(state, tuple); |
| 2992 |
/* the above sends transactional and immediate cache inval messages */ |
| 2993 |
} |
| 2994 |
else |
| 2995 |
{ |
| 2996 |
systable_inplace_update_cancel(state); |
| 2997 |
|
| 2998 |
/* |
| 2999 |
* While we didn't change relhasindex, CREATE INDEX needs a |
| 3000 |
* transactional inval for when the new index's catalog rows become |
| 3001 |
* visible. Other CREATE INDEX and REINDEX code happens to also queue |
| 3002 |
* this inval, but keep this in case rare callers rely on this part of |
| 3003 |
* our API contract. |
| 3004 |
*/ |
| 3005 |
CacheInvalidateRelcacheByTuple(tuple); |
| 3006 |
} |
| 3007 |
|
| 3008 |
heap_freetuple(tuple); |
| 3009 |
|
| 3010 |
table_close(pg_class, RowExclusiveLock); |
| 3011 |
} |
| 3012 |
|
| 3013 |
|
| 3014 |
/* |
| 3015 |
* index_build - invoke access-method-specific index build procedure |
| 3016 |
* |
| 3017 |
* On entry, the index's catalog entries are valid, and its physical disk |
| 3018 |
* file has been created but is empty. We call the AM-specific build |
| 3019 |
* procedure to fill in the index contents. We then update the pg_class |
| 3020 |
* entries of the index and heap relation as needed, using statistics |
| 3021 |
* returned by ambuild as well as data passed by the caller. |
| 3022 |
* |
| 3023 |
* isreindex indicates we are recreating a previously-existing index. |
| 3024 |
* parallel indicates if parallelism may be useful. |
| 3025 |
* |
| 3026 |
* Note: before Postgres 8.2, the passed-in heap and index Relations |
| 3027 |
* were automatically closed by this routine. This is no longer the case. |
| 3028 |
* The caller opened 'em, and the caller should close 'em. |
| 3029 |
*/ |
| 3030 |
void |
| 3031 |
index_build(Relation heapRelation, |
| 3032 |
Relation indexRelation, |
| 3033 |
IndexInfo *indexInfo, |
| 3034 |
bool isreindex, |
| 3035 |
bool parallel) |
| 3036 |
{ |
| 3037 |
IndexBuildResult *stats; |
| 3038 |
Oid save_userid; |
| 3039 |
int save_sec_context; |
| 3040 |
int save_nestlevel; |
| 3041 |
|
| 3042 |
/* |
| 3043 |
* sanity checks |
| 3044 |
*/ |
| 3045 |
Assert(RelationIsValid(indexRelation)); |
| 3046 |
Assert(PointerIsValid(indexRelation->rd_indam)); |
| 3047 |
Assert(PointerIsValid(indexRelation->rd_indam->ambuild)); |
| 3048 |
Assert(PointerIsValid(indexRelation->rd_indam->ambuildempty)); |
| 3049 |
|
| 3050 |
/* |
| 3051 |
* Determine worker process details for parallel CREATE INDEX. Currently, |
| 3052 |
* only btree, GIN, and BRIN have support for parallel builds. |
| 3053 |
* |
| 3054 |
* Note that planner considers parallel safety for us. |
| 3055 |
*/ |
| 3056 |
if (parallel && IsNormalProcessingMode() && |
| 3057 |
indexRelation->rd_indam->amcanbuildparallel) |
| 3058 |
indexInfo->ii_ParallelWorkers = |
| 3059 |
plan_create_index_workers(RelationGetRelid(heapRelation), |
| 3060 |
RelationGetRelid(indexRelation)); |
| 3061 |
|
| 3062 |
if (indexInfo->ii_ParallelWorkers == 0) |
| 3063 |
ereport(DEBUG1, |
| 3064 |
(errmsg_internal("building index \"%s\" on table \"%s\" serially", |
| 3065 |
RelationGetRelationName(indexRelation), |
| 3066 |
RelationGetRelationName(heapRelation)))); |
| 3067 |
else |
| 3068 |
ereport(DEBUG1, |
| 3069 |
(errmsg_internal("building index \"%s\" on table \"%s\" with request for %d parallel workers", |
| 3070 |
RelationGetRelationName(indexRelation), |
| 3071 |
RelationGetRelationName(heapRelation), |
| 3072 |
indexInfo->ii_ParallelWorkers))); |
| 3073 |
|
| 3074 |
/* |
| 3075 |
* Switch to the table owner's userid, so that any index functions are run |
| 3076 |
* as that user. Also lock down security-restricted operations and |
| 3077 |
* arrange to make GUC variable changes local to this command. |
| 3078 |
*/ |
| 3079 |
GetUserIdAndSecContext(&save_userid, &save_sec_context); |
| 3080 |
SetUserIdAndSecContext(heapRelation->rd_rel->relowner, |
| 3081 |
save_sec_context | SECURITY_RESTRICTED_OPERATION); |
| 3082 |
save_nestlevel = NewGUCNestLevel(); |
| 3083 |
RestrictSearchPath(); |
| 3084 |
|
| 3085 |
/* Set up initial progress report status */ |
| 3086 |
{ |
| 3087 |
const int progress_index[] = { |
| 3088 |
PROGRESS_CREATEIDX_PHASE, |
| 3089 |
PROGRESS_CREATEIDX_SUBPHASE, |
| 3090 |
PROGRESS_CREATEIDX_TUPLES_DONE, |
| 3091 |
PROGRESS_CREATEIDX_TUPLES_TOTAL, |
| 3092 |
PROGRESS_SCAN_BLOCKS_DONE, |
| 3093 |
PROGRESS_SCAN_BLOCKS_TOTAL |
| 3094 |
}; |
| 3095 |
const int64 progress_vals[] = { |
| 3096 |
PROGRESS_CREATEIDX_PHASE_BUILD, |
| 3097 |
PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE, |
| 3098 |
0, 0, 0, 0 |
| 3099 |
}; |
| 3100 |
|
| 3101 |
pgstat_progress_update_multi_param(6, progress_index, progress_vals); |
| 3102 |
} |
| 3103 |
|
| 3104 |
/* |
| 3105 |
* Call the access method's build procedure |
| 3106 |
*/ |
| 3107 |
stats = indexRelation->rd_indam->ambuild(heapRelation, indexRelation, |
| 3108 |
indexInfo); |
| 3109 |
Assert(PointerIsValid(stats)); |
| 3110 |
|
| 3111 |
/* |
| 3112 |
* If this is an unlogged index, we may need to write out an init fork for |
| 3113 |
* it -- but we must first check whether one already exists. If, for |
| 3114 |
* example, an unlogged relation is truncated in the transaction that |
| 3115 |
* created it, or truncated twice in a subsequent transaction, the |
| 3116 |
* relfilenumber won't change, and nothing needs to be done here. |
| 3117 |
*/ |
| 3118 |
if (indexRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED && |
| 3119 |
!smgrexists(RelationGetSmgr(indexRelation), INIT_FORKNUM)) |
| 3120 |
{ |
| 3121 |
smgrcreate(RelationGetSmgr(indexRelation), INIT_FORKNUM, false); |
| 3122 |
log_smgrcreate(&indexRelation->rd_locator, INIT_FORKNUM); |
| 3123 |
indexRelation->rd_indam->ambuildempty(indexRelation); |
| 3124 |
} |
| 3125 |
|
| 3126 |
/* |
| 3127 |
* If we found any potentially broken HOT chains, mark the index as not |
| 3128 |
* being usable until the current transaction is below the event horizon. |
| 3129 |
* See src/backend/access/heap/README.HOT for discussion. While it might |
| 3130 |
* become safe to use the index earlier based on actual cleanup activity |
| 3131 |
* and other active transactions, the test for that would be much more |
| 3132 |
* complex and would require some form of blocking, so keep it simple and |
| 3133 |
* fast by just using the current transaction. |
| 3134 |
* |
| 3135 |
* However, when reindexing an existing index, we should do nothing here. |
| 3136 |
* Any HOT chains that are broken with respect to the index must predate |
| 3137 |
* the index's original creation, so there is no need to change the |
| 3138 |
* index's usability horizon. Moreover, we *must not* try to change the |
| 3139 |
* index's pg_index entry while reindexing pg_index itself, and this |
| 3140 |
* optimization nicely prevents that. The more complex rules needed for a |
| 3141 |
* reindex are handled separately after this function returns. |
| 3142 |
* |
| 3143 |
* We also need not set indcheckxmin during a concurrent index build, |
| 3144 |
* because we won't set indisvalid true until all transactions that care |
| 3145 |
* about the broken HOT chains are gone. |
| 3146 |
* |
| 3147 |
* Therefore, this code path can only be taken during non-concurrent |
| 3148 |
* CREATE INDEX. Thus the fact that heap_update will set the pg_index |
| 3149 |
* tuple's xmin doesn't matter, because that tuple was created in the |
| 3150 |
* current transaction anyway. That also means we don't need to worry |
| 3151 |
* about any concurrent readers of the tuple; no other transaction can see |
| 3152 |
* it yet. |
| 3153 |
*/ |
| 3154 |
if (indexInfo->ii_BrokenHotChain && |
| 3155 |
!isreindex && |
| 3156 |
!indexInfo->ii_Concurrent) |
| 3157 |
{ |
| 3158 |
Oid indexId = RelationGetRelid(indexRelation); |
| 3159 |
Relation pg_index; |
| 3160 |
HeapTuple indexTuple; |
| 3161 |
Form_pg_index indexForm; |
| 3162 |
|
| 3163 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 3164 |
|
| 3165 |
indexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 3166 |
ObjectIdGetDatum(indexId)); |
| 3167 |
if (!HeapTupleIsValid(indexTuple)) |
| 3168 |
elog(ERROR, "cache lookup failed for index %u", indexId); |
| 3169 |
indexForm = (Form_pg_index) GETSTRUCT(indexTuple); |
| 3170 |
|
| 3171 |
/* If it's a new index, indcheckxmin shouldn't be set ... */ |
| 3172 |
Assert(!indexForm->indcheckxmin); |
| 3173 |
|
| 3174 |
indexForm->indcheckxmin = true; |
| 3175 |
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple); |
| 3176 |
|
| 3177 |
heap_freetuple(indexTuple); |
| 3178 |
table_close(pg_index, RowExclusiveLock); |
| 3179 |
} |
| 3180 |
|
| 3181 |
/* |
| 3182 |
* Update heap and index pg_class rows |
| 3183 |
*/ |
| 3184 |
index_update_stats(heapRelation, |
| 3185 |
true, |
| 3186 |
stats->heap_tuples); |
| 3187 |
|
| 3188 |
index_update_stats(indexRelation, |
| 3189 |
false, |
| 3190 |
stats->index_tuples); |
| 3191 |
|
| 3192 |
/* Make the updated catalog row versions visible */ |
| 3193 |
CommandCounterIncrement(); |
| 3194 |
|
| 3195 |
/* |
| 3196 |
* If it's for an exclusion constraint, make a second pass over the heap |
| 3197 |
* to verify that the constraint is satisfied. We must not do this until |
| 3198 |
* the index is fully valid. (Broken HOT chains shouldn't matter, though; |
| 3199 |
* see comments for IndexCheckExclusion.) |
| 3200 |
*/ |
| 3201 |
if (indexInfo->ii_ExclusionOps != NULL) |
| 3202 |
IndexCheckExclusion(heapRelation, indexRelation, indexInfo); |
| 3203 |
|
| 3204 |
/* Roll back any GUC changes executed by index functions */ |
| 3205 |
AtEOXact_GUC(false, save_nestlevel); |
| 3206 |
|
| 3207 |
/* Restore userid and security context */ |
| 3208 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 3209 |
} |
| 3210 |
|
| 3211 |
/* |
| 3212 |
* IndexCheckExclusion - verify that a new exclusion constraint is satisfied |
| 3213 |
* |
| 3214 |
* When creating an exclusion constraint, we first build the index normally |
| 3215 |
* and then rescan the heap to check for conflicts. We assume that we only |
| 3216 |
* need to validate tuples that are live according to an up-to-date snapshot, |
| 3217 |
* and that these were correctly indexed even in the presence of broken HOT |
| 3218 |
* chains. This should be OK since we are holding at least ShareLock on the |
| 3219 |
* table, meaning there can be no uncommitted updates from other transactions. |
| 3220 |
* (Note: that wouldn't necessarily work for system catalogs, since many |
| 3221 |
* operations release write lock early on the system catalogs.) |
| 3222 |
*/ |
| 3223 |
static void |
| 3224 |
IndexCheckExclusion(Relation heapRelation, |
| 3225 |
Relation indexRelation, |
| 3226 |
IndexInfo *indexInfo) |
| 3227 |
{ |
| 3228 |
TableScanDesc scan; |
| 3229 |
Datum values[INDEX_MAX_KEYS]; |
| 3230 |
bool isnull[INDEX_MAX_KEYS]; |
| 3231 |
ExprState *predicate; |
| 3232 |
TupleTableSlot *slot; |
| 3233 |
EState *estate; |
| 3234 |
ExprContext *econtext; |
| 3235 |
Snapshot snapshot; |
| 3236 |
|
| 3237 |
/* |
| 3238 |
* If we are reindexing the target index, mark it as no longer being |
| 3239 |
* reindexed, to forestall an Assert in index_beginscan when we try to use |
| 3240 |
* the index for probes. This is OK because the index is now fully valid. |
| 3241 |
*/ |
| 3242 |
if (ReindexIsCurrentlyProcessingIndex(RelationGetRelid(indexRelation))) |
| 3243 |
ResetReindexProcessing(); |
| 3244 |
|
| 3245 |
/* |
| 3246 |
* Need an EState for evaluation of index expressions and partial-index |
| 3247 |
* predicates. Also a slot to hold the current tuple. |
| 3248 |
*/ |
| 3249 |
estate = CreateExecutorState(); |
| 3250 |
econtext = GetPerTupleExprContext(estate); |
| 3251 |
slot = table_slot_create(heapRelation, NULL); |
| 3252 |
|
| 3253 |
/* Arrange for econtext's scan tuple to be the tuple under test */ |
| 3254 |
econtext->ecxt_scantuple = slot; |
| 3255 |
|
| 3256 |
/* Set up execution state for predicate, if any. */ |
| 3257 |
predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate); |
| 3258 |
|
| 3259 |
/* |
| 3260 |
* Scan all live tuples in the base relation. |
| 3261 |
*/ |
| 3262 |
snapshot = RegisterSnapshot(GetLatestSnapshot()); |
| 3263 |
scan = table_beginscan_strat(heapRelation, /* relation */ |
| 3264 |
snapshot, /* snapshot */ |
| 3265 |
0, /* number of keys */ |
| 3266 |
NULL, /* scan key */ |
| 3267 |
true, /* buffer access strategy OK */ |
| 3268 |
true); /* syncscan OK */ |
| 3269 |
|
| 3270 |
while (table_scan_getnextslot(scan, ForwardScanDirection, slot)) |
| 3271 |
{ |
| 3272 |
CHECK_FOR_INTERRUPTS(); |
| 3273 |
|
| 3274 |
/* |
| 3275 |
* In a partial index, ignore tuples that don't satisfy the predicate. |
| 3276 |
*/ |
| 3277 |
if (predicate != NULL) |
| 3278 |
{ |
| 3279 |
if (!ExecQual(predicate, econtext)) |
| 3280 |
continue; |
| 3281 |
} |
| 3282 |
|
| 3283 |
/* |
| 3284 |
* Extract index column values, including computing expressions. |
| 3285 |
*/ |
| 3286 |
FormIndexDatum(indexInfo, |
| 3287 |
slot, |
| 3288 |
estate, |
| 3289 |
values, |
| 3290 |
isnull); |
| 3291 |
|
| 3292 |
/* |
| 3293 |
* Check that this tuple has no conflicts. |
| 3294 |
*/ |
| 3295 |
check_exclusion_constraint(heapRelation, |
| 3296 |
indexRelation, indexInfo, |
| 3297 |
&(slot->tts_tid), values, isnull, |
| 3298 |
estate, true); |
| 3299 |
|
| 3300 |
MemoryContextReset(econtext->ecxt_per_tuple_memory); |
| 3301 |
} |
| 3302 |
|
| 3303 |
table_endscan(scan); |
| 3304 |
UnregisterSnapshot(snapshot); |
| 3305 |
|
| 3306 |
ExecDropSingleTupleTableSlot(slot); |
| 3307 |
|
| 3308 |
FreeExecutorState(estate); |
| 3309 |
|
| 3310 |
/* These may have been pointing to the now-gone estate */ |
| 3311 |
indexInfo->ii_ExpressionsState = NIL; |
| 3312 |
indexInfo->ii_PredicateState = NULL; |
| 3313 |
} |
| 3314 |
|
| 3315 |
/* |
| 3316 |
* validate_index - support code for concurrent index builds |
| 3317 |
* |
| 3318 |
* We do a concurrent index build by first inserting the catalog entry for the |
| 3319 |
* index via index_create(), marking it not indisready and not indisvalid. |
| 3320 |
* Then we commit our transaction and start a new one, then we wait for all |
| 3321 |
* transactions that could have been modifying the table to terminate. Now |
| 3322 |
* we know that any subsequently-started transactions will see the index and |
| 3323 |
* honor its constraints on HOT updates; so while existing HOT-chains might |
| 3324 |
* be broken with respect to the index, no currently live tuple will have an |
| 3325 |
* incompatible HOT update done to it. We now build the index normally via |
| 3326 |
* index_build(), while holding a weak lock that allows concurrent |
| 3327 |
* insert/update/delete. Also, we index only tuples that are valid |
| 3328 |
* as of the start of the scan (see table_index_build_scan), whereas a normal |
| 3329 |
* build takes care to include recently-dead tuples. This is OK because |
| 3330 |
* we won't mark the index valid until all transactions that might be able |
| 3331 |
* to see those tuples are gone. The reason for doing that is to avoid |
| 3332 |
* bogus unique-index failures due to concurrent UPDATEs (we might see |
| 3333 |
* different versions of the same row as being valid when we pass over them, |
| 3334 |
* if we used HeapTupleSatisfiesVacuum). This leaves us with an index that |
| 3335 |
* does not contain any tuples added to the table while we built the index. |
| 3336 |
* |
| 3337 |
* Next, we mark the index "indisready" (but still not "indisvalid") and |
| 3338 |
* commit the second transaction and start a third. Again we wait for all |
| 3339 |
* transactions that could have been modifying the table to terminate. Now |
| 3340 |
* we know that any subsequently-started transactions will see the index and |
| 3341 |
* insert their new tuples into it. We then take a new reference snapshot |
| 3342 |
* which is passed to validate_index(). Any tuples that are valid according |
| 3343 |
* to this snap, but are not in the index, must be added to the index. |
| 3344 |
* (Any tuples committed live after the snap will be inserted into the |
| 3345 |
* index by their originating transaction. Any tuples committed dead before |
| 3346 |
* the snap need not be indexed, because we will wait out all transactions |
| 3347 |
* that might care about them before we mark the index valid.) |
| 3348 |
* |
| 3349 |
* validate_index() works by first gathering all the TIDs currently in the |
| 3350 |
* index, using a bulkdelete callback that just stores the TIDs and doesn't |
| 3351 |
* ever say "delete it". (This should be faster than a plain indexscan; |
| 3352 |
* also, not all index AMs support full-index indexscan.) Then we sort the |
| 3353 |
* TIDs, and finally scan the table doing a "merge join" against the TID list |
| 3354 |
* to see which tuples are missing from the index. Thus we will ensure that |
| 3355 |
* all tuples valid according to the reference snapshot are in the index. |
| 3356 |
* |
| 3357 |
* Building a unique index this way is tricky: we might try to insert a |
| 3358 |
* tuple that is already dead or is in process of being deleted, and we |
| 3359 |
* mustn't have a uniqueness failure against an updated version of the same |
| 3360 |
* row. We could try to check the tuple to see if it's already dead and tell |
| 3361 |
* index_insert() not to do the uniqueness check, but that still leaves us |
| 3362 |
* with a race condition against an in-progress update. To handle that, |
| 3363 |
* we expect the index AM to recheck liveness of the to-be-inserted tuple |
| 3364 |
* before it declares a uniqueness error. |
| 3365 |
* |
| 3366 |
* After completing validate_index(), we wait until all transactions that |
| 3367 |
* were alive at the time of the reference snapshot are gone; this is |
| 3368 |
* necessary to be sure there are none left with a transaction snapshot |
| 3369 |
* older than the reference (and hence possibly able to see tuples we did |
| 3370 |
* not index). Then we mark the index "indisvalid" and commit. Subsequent |
| 3371 |
* transactions will be able to use it for queries. |
| 3372 |
* |
| 3373 |
* Doing two full table scans is a brute-force strategy. We could try to be |
| 3374 |
* cleverer, eg storing new tuples in a special area of the table (perhaps |
| 3375 |
* making the table append-only by setting use_fsm). However that would |
| 3376 |
* add yet more locking issues. |
| 3377 |
*/ |
| 3378 |
void |
| 3379 |
validate_index(Oid heapId, Oid indexId, Snapshot snapshot) |
| 3380 |
{ |
| 3381 |
Relation heapRelation, |
| 3382 |
indexRelation; |
| 3383 |
IndexInfo *indexInfo; |
| 3384 |
IndexVacuumInfo ivinfo; |
| 3385 |
ValidateIndexState state; |
| 3386 |
Oid save_userid; |
| 3387 |
int save_sec_context; |
| 3388 |
int save_nestlevel; |
| 3389 |
|
| 3390 |
{ |
| 3391 |
const int progress_index[] = { |
| 3392 |
PROGRESS_CREATEIDX_PHASE, |
| 3393 |
PROGRESS_CREATEIDX_TUPLES_DONE, |
| 3394 |
PROGRESS_CREATEIDX_TUPLES_TOTAL, |
| 3395 |
PROGRESS_SCAN_BLOCKS_DONE, |
| 3396 |
PROGRESS_SCAN_BLOCKS_TOTAL |
| 3397 |
}; |
| 3398 |
const int64 progress_vals[] = { |
| 3399 |
PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN, |
| 3400 |
0, 0, 0, 0 |
| 3401 |
}; |
| 3402 |
|
| 3403 |
pgstat_progress_update_multi_param(5, progress_index, progress_vals); |
| 3404 |
} |
| 3405 |
|
| 3406 |
/* Open and lock the parent heap relation */ |
| 3407 |
heapRelation = table_open(heapId, ShareUpdateExclusiveLock); |
| 3408 |
|
| 3409 |
/* |
| 3410 |
* Switch to the table owner's userid, so that any index functions are run |
| 3411 |
* as that user. Also lock down security-restricted operations and |
| 3412 |
* arrange to make GUC variable changes local to this command. |
| 3413 |
*/ |
| 3414 |
GetUserIdAndSecContext(&save_userid, &save_sec_context); |
| 3415 |
SetUserIdAndSecContext(heapRelation->rd_rel->relowner, |
| 3416 |
save_sec_context | SECURITY_RESTRICTED_OPERATION); |
| 3417 |
save_nestlevel = NewGUCNestLevel(); |
| 3418 |
RestrictSearchPath(); |
| 3419 |
|
| 3420 |
indexRelation = index_open(indexId, RowExclusiveLock); |
| 3421 |
|
| 3422 |
/* |
| 3423 |
* Fetch info needed for index_insert. (You might think this should be |
| 3424 |
* passed in from DefineIndex, but its copy is long gone due to having |
| 3425 |
* been built in a previous transaction.) |
| 3426 |
*/ |
| 3427 |
indexInfo = BuildIndexInfo(indexRelation); |
| 3428 |
|
| 3429 |
/* mark build is concurrent just for consistency */ |
| 3430 |
indexInfo->ii_Concurrent = true; |
| 3431 |
|
| 3432 |
/* |
| 3433 |
* Scan the index and gather up all the TIDs into a tuplesort object. |
| 3434 |
*/ |
| 3435 |
ivinfo.index = indexRelation; |
| 3436 |
ivinfo.heaprel = heapRelation; |
| 3437 |
ivinfo.analyze_only = false; |
| 3438 |
ivinfo.report_progress = true; |
| 3439 |
ivinfo.estimated_count = true; |
| 3440 |
ivinfo.message_level = DEBUG2; |
| 3441 |
ivinfo.num_heap_tuples = heapRelation->rd_rel->reltuples; |
| 3442 |
ivinfo.strategy = NULL; |
| 3443 |
|
| 3444 |
/* |
| 3445 |
* Encode TIDs as int8 values for the sort, rather than directly sorting |
| 3446 |
* item pointers. This can be significantly faster, primarily because TID |
| 3447 |
* is a pass-by-reference type on all platforms, whereas int8 is |
| 3448 |
* pass-by-value on most platforms. |
| 3449 |
*/ |
| 3450 |
state.tuplesort = tuplesort_begin_datum(INT8OID, Int8LessOperator, |
| 3451 |
InvalidOid, false, |
| 3452 |
maintenance_work_mem, |
| 3453 |
NULL, TUPLESORT_NONE); |
| 3454 |
state.htups = state.itups = state.tups_inserted = 0; |
| 3455 |
|
| 3456 |
/* ambulkdelete updates progress metrics */ |
| 3457 |
(void) index_bulk_delete(&ivinfo, NULL, |
| 3458 |
validate_index_callback, &state); |
| 3459 |
|
| 3460 |
/* Execute the sort */ |
| 3461 |
{ |
| 3462 |
const int progress_index[] = { |
| 3463 |
PROGRESS_CREATEIDX_PHASE, |
| 3464 |
PROGRESS_SCAN_BLOCKS_DONE, |
| 3465 |
PROGRESS_SCAN_BLOCKS_TOTAL |
| 3466 |
}; |
| 3467 |
const int64 progress_vals[] = { |
| 3468 |
PROGRESS_CREATEIDX_PHASE_VALIDATE_SORT, |
| 3469 |
0, 0 |
| 3470 |
}; |
| 3471 |
|
| 3472 |
pgstat_progress_update_multi_param(3, progress_index, progress_vals); |
| 3473 |
} |
| 3474 |
tuplesort_performsort(state.tuplesort); |
| 3475 |
|
| 3476 |
/* |
| 3477 |
* Now scan the heap and "merge" it with the index |
| 3478 |
*/ |
| 3479 |
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE, |
| 3480 |
PROGRESS_CREATEIDX_PHASE_VALIDATE_TABLESCAN); |
| 3481 |
table_index_validate_scan(heapRelation, |
| 3482 |
indexRelation, |
| 3483 |
indexInfo, |
| 3484 |
snapshot, |
| 3485 |
&state); |
| 3486 |
|
| 3487 |
/* Done with tuplesort object */ |
| 3488 |
tuplesort_end(state.tuplesort); |
| 3489 |
|
| 3490 |
/* Make sure to release resources cached in indexInfo (if needed). */ |
| 3491 |
index_insert_cleanup(indexRelation, indexInfo); |
| 3492 |
|
| 3493 |
elog(DEBUG2, |
| 3494 |
"validate_index found %.0f heap tuples, %.0f index tuples; inserted %.0f missing tuples", |
| 3495 |
state.htups, state.itups, state.tups_inserted); |
| 3496 |
|
| 3497 |
/* Roll back any GUC changes executed by index functions */ |
| 3498 |
AtEOXact_GUC(false, save_nestlevel); |
| 3499 |
|
| 3500 |
/* Restore userid and security context */ |
| 3501 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 3502 |
|
| 3503 |
/* Close rels, but keep locks */ |
| 3504 |
index_close(indexRelation, NoLock); |
| 3505 |
table_close(heapRelation, NoLock); |
| 3506 |
} |
| 3507 |
|
| 3508 |
/* |
| 3509 |
* validate_index_callback - bulkdelete callback to collect the index TIDs |
| 3510 |
*/ |
| 3511 |
static bool |
| 3512 |
validate_index_callback(ItemPointer itemptr, void *opaque) |
| 3513 |
{ |
| 3514 |
ValidateIndexState *state = (ValidateIndexState *) opaque; |
| 3515 |
int64 encoded = itemptr_encode(itemptr); |
| 3516 |
|
| 3517 |
tuplesort_putdatum(state->tuplesort, Int64GetDatum(encoded), false); |
| 3518 |
state->itups += 1; |
| 3519 |
return false; /* never actually delete anything */ |
| 3520 |
} |
| 3521 |
|
| 3522 |
/* |
| 3523 |
* index_set_state_flags - adjust pg_index state flags |
| 3524 |
* |
| 3525 |
* This is used during CREATE/DROP INDEX CONCURRENTLY to adjust the pg_index |
| 3526 |
* flags that denote the index's state. |
| 3527 |
* |
| 3528 |
* Note that CatalogTupleUpdate() sends a cache invalidation message for the |
| 3529 |
* tuple, so other sessions will hear about the update as soon as we commit. |
| 3530 |
*/ |
| 3531 |
void |
| 3532 |
index_set_state_flags(Oid indexId, IndexStateFlagsAction action) |
| 3533 |
{ |
| 3534 |
Relation pg_index; |
| 3535 |
HeapTuple indexTuple; |
| 3536 |
Form_pg_index indexForm; |
| 3537 |
|
| 3538 |
/* Open pg_index and fetch a writable copy of the index's tuple */ |
| 3539 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 3540 |
|
| 3541 |
indexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 3542 |
ObjectIdGetDatum(indexId)); |
| 3543 |
if (!HeapTupleIsValid(indexTuple)) |
| 3544 |
elog(ERROR, "cache lookup failed for index %u", indexId); |
| 3545 |
indexForm = (Form_pg_index) GETSTRUCT(indexTuple); |
| 3546 |
|
| 3547 |
/* Perform the requested state change on the copy */ |
| 3548 |
switch (action) |
| 3549 |
{ |
| 3550 |
case INDEX_CREATE_SET_READY: |
| 3551 |
/* Set indisready during a CREATE INDEX CONCURRENTLY sequence */ |
| 3552 |
Assert(indexForm->indislive); |
| 3553 |
Assert(!indexForm->indisready); |
| 3554 |
Assert(!indexForm->indisvalid); |
| 3555 |
indexForm->indisready = true; |
| 3556 |
break; |
| 3557 |
case INDEX_CREATE_SET_VALID: |
| 3558 |
/* Set indisvalid during a CREATE INDEX CONCURRENTLY sequence */ |
| 3559 |
Assert(indexForm->indislive); |
| 3560 |
Assert(indexForm->indisready); |
| 3561 |
Assert(!indexForm->indisvalid); |
| 3562 |
indexForm->indisvalid = true; |
| 3563 |
break; |
| 3564 |
case INDEX_DROP_CLEAR_VALID: |
| 3565 |
|
| 3566 |
/* |
| 3567 |
* Clear indisvalid during a DROP INDEX CONCURRENTLY sequence |
| 3568 |
* |
| 3569 |
* If indisready == true we leave it set so the index still gets |
| 3570 |
* maintained by active transactions. We only need to ensure that |
| 3571 |
* indisvalid is false. (We don't assert that either is initially |
| 3572 |
* true, though, since we want to be able to retry a DROP INDEX |
| 3573 |
* CONCURRENTLY that failed partway through.) |
| 3574 |
* |
| 3575 |
* Note: the CLUSTER logic assumes that indisclustered cannot be |
| 3576 |
* set on any invalid index, so clear that flag too. For |
| 3577 |
* cleanliness, also clear indisreplident. |
| 3578 |
*/ |
| 3579 |
indexForm->indisvalid = false; |
| 3580 |
indexForm->indisclustered = false; |
| 3581 |
indexForm->indisreplident = false; |
| 3582 |
break; |
| 3583 |
case INDEX_DROP_SET_DEAD: |
| 3584 |
|
| 3585 |
/* |
| 3586 |
* Clear indisready/indislive during DROP INDEX CONCURRENTLY |
| 3587 |
* |
| 3588 |
* We clear both indisready and indislive, because we not only |
| 3589 |
* want to stop updates, we want to prevent sessions from touching |
| 3590 |
* the index at all. |
| 3591 |
*/ |
| 3592 |
Assert(!indexForm->indisvalid); |
| 3593 |
Assert(!indexForm->indisclustered); |
| 3594 |
Assert(!indexForm->indisreplident); |
| 3595 |
indexForm->indisready = false; |
| 3596 |
indexForm->indislive = false; |
| 3597 |
break; |
| 3598 |
} |
| 3599 |
|
| 3600 |
/* ... and update it */ |
| 3601 |
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple); |
| 3602 |
|
| 3603 |
table_close(pg_index, RowExclusiveLock); |
| 3604 |
} |
| 3605 |
|
| 3606 |
|
| 3607 |
/* |
| 3608 |
* IndexGetRelation: given an index's relation OID, get the OID of the |
| 3609 |
* relation it is an index on. Uses the system cache. |
| 3610 |
*/ |
| 3611 |
Oid |
| 3612 |
IndexGetRelation(Oid indexId, bool missing_ok) |
| 3613 |
{ |
| 3614 |
HeapTuple tuple; |
| 3615 |
Form_pg_index index; |
| 3616 |
Oid result; |
| 3617 |
|
| 3618 |
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId)); |
| 3619 |
if (!HeapTupleIsValid(tuple)) |
| 3620 |
{ |
| 3621 |
if (missing_ok) |
| 3622 |
return InvalidOid; |
| 3623 |
elog(ERROR, "cache lookup failed for index %u", indexId); |
| 3624 |
} |
| 3625 |
index = (Form_pg_index) GETSTRUCT(tuple); |
| 3626 |
Assert(index->indexrelid == indexId); |
| 3627 |
|
| 3628 |
result = index->indrelid; |
| 3629 |
ReleaseSysCache(tuple); |
| 3630 |
return result; |
| 3631 |
} |
| 3632 |
|
| 3633 |
/* |
| 3634 |
* reindex_index - This routine is used to recreate a single index |
| 3635 |
*/ |
| 3636 |
void |
| 3637 |
reindex_index(const ReindexStmt *stmt, Oid indexId, |
| 3638 |
bool skip_constraint_checks, char persistence, |
| 3639 |
const ReindexParams *params) |
| 3640 |
{ |
| 3641 |
Relation iRel, |
| 3642 |
heapRelation; |
| 3643 |
Oid heapId; |
| 3644 |
Oid save_userid; |
| 3645 |
int save_sec_context; |
| 3646 |
int save_nestlevel; |
| 3647 |
IndexInfo *indexInfo; |
| 3648 |
volatile bool skipped_constraint = false; |
| 3649 |
PGRUsage ru0; |
| 3650 |
bool progress = ((params->options & REINDEXOPT_REPORT_PROGRESS) != 0); |
| 3651 |
bool set_tablespace = false; |
| 3652 |
|
| 3653 |
pg_rusage_init(&ru0); |
| 3654 |
|
| 3655 |
/* |
| 3656 |
* Open and lock the parent heap relation. ShareLock is sufficient since |
| 3657 |
* we only need to be sure no schema or data changes are going on. |
| 3658 |
*/ |
| 3659 |
heapId = IndexGetRelation(indexId, |
| 3660 |
(params->options & REINDEXOPT_MISSING_OK) != 0); |
| 3661 |
/* if relation is missing, leave */ |
| 3662 |
if (!OidIsValid(heapId)) |
| 3663 |
return; |
| 3664 |
|
| 3665 |
if ((params->options & REINDEXOPT_MISSING_OK) != 0) |
| 3666 |
heapRelation = try_table_open(heapId, ShareLock); |
| 3667 |
else |
| 3668 |
heapRelation = table_open(heapId, ShareLock); |
| 3669 |
|
| 3670 |
/* if relation is gone, leave */ |
| 3671 |
if (!heapRelation) |
| 3672 |
return; |
| 3673 |
|
| 3674 |
/* |
| 3675 |
* Switch to the table owner's userid, so that any index functions are run |
| 3676 |
* as that user. Also lock down security-restricted operations and |
| 3677 |
* arrange to make GUC variable changes local to this command. |
| 3678 |
*/ |
| 3679 |
GetUserIdAndSecContext(&save_userid, &save_sec_context); |
| 3680 |
SetUserIdAndSecContext(heapRelation->rd_rel->relowner, |
| 3681 |
save_sec_context | SECURITY_RESTRICTED_OPERATION); |
| 3682 |
save_nestlevel = NewGUCNestLevel(); |
| 3683 |
RestrictSearchPath(); |
| 3684 |
|
| 3685 |
if (progress) |
| 3686 |
{ |
| 3687 |
const int progress_cols[] = { |
| 3688 |
PROGRESS_CREATEIDX_COMMAND, |
| 3689 |
PROGRESS_CREATEIDX_INDEX_OID |
| 3690 |
}; |
| 3691 |
const int64 progress_vals[] = { |
| 3692 |
PROGRESS_CREATEIDX_COMMAND_REINDEX, |
| 3693 |
indexId |
| 3694 |
}; |
| 3695 |
|
| 3696 |
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, |
| 3697 |
heapId); |
| 3698 |
pgstat_progress_update_multi_param(2, progress_cols, progress_vals); |
| 3699 |
} |
| 3700 |
|
| 3701 |
/* |
| 3702 |
* Open the target index relation and get an exclusive lock on it, to |
| 3703 |
* ensure that no one else is touching this particular index. |
| 3704 |
*/ |
| 3705 |
if ((params->options & REINDEXOPT_MISSING_OK) != 0) |
| 3706 |
iRel = try_index_open(indexId, AccessExclusiveLock); |
| 3707 |
else |
| 3708 |
iRel = index_open(indexId, AccessExclusiveLock); |
| 3709 |
|
| 3710 |
/* if index relation is gone, leave */ |
| 3711 |
if (!iRel) |
| 3712 |
{ |
| 3713 |
/* Roll back any GUC changes */ |
| 3714 |
AtEOXact_GUC(false, save_nestlevel); |
| 3715 |
|
| 3716 |
/* Restore userid and security context */ |
| 3717 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 3718 |
|
| 3719 |
/* Close parent heap relation, but keep locks */ |
| 3720 |
table_close(heapRelation, NoLock); |
| 3721 |
return; |
| 3722 |
} |
| 3723 |
|
| 3724 |
if (progress) |
| 3725 |
pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID, |
| 3726 |
iRel->rd_rel->relam); |
| 3727 |
|
| 3728 |
/* |
| 3729 |
* If a statement is available, telling that this comes from a REINDEX |
| 3730 |
* command, collect the index for event triggers. |
| 3731 |
*/ |
| 3732 |
if (stmt) |
| 3733 |
{ |
| 3734 |
ObjectAddress address; |
| 3735 |
|
| 3736 |
ObjectAddressSet(address, RelationRelationId, indexId); |
| 3737 |
EventTriggerCollectSimpleCommand(address, |
| 3738 |
InvalidObjectAddress, |
| 3739 |
(Node *) stmt); |
| 3740 |
} |
| 3741 |
|
| 3742 |
/* |
| 3743 |
* Partitioned indexes should never get processed here, as they have no |
| 3744 |
* physical storage. |
| 3745 |
*/ |
| 3746 |
if (iRel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) |
| 3747 |
elog(ERROR, "cannot reindex partitioned index \"%s.%s\"", |
| 3748 |
get_namespace_name(RelationGetNamespace(iRel)), |
| 3749 |
RelationGetRelationName(iRel)); |
| 3750 |
|
| 3751 |
/* |
| 3752 |
* Don't allow reindex on temp tables of other backends ... their local |
| 3753 |
* buffer manager is not going to cope. |
| 3754 |
*/ |
| 3755 |
if (RELATION_IS_OTHER_TEMP(iRel)) |
| 3756 |
ereport(ERROR, |
| 3757 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 3758 |
errmsg("cannot reindex temporary tables of other sessions"))); |
| 3759 |
|
| 3760 |
/* |
| 3761 |
* Don't allow reindex of an invalid index on TOAST table. This is a |
| 3762 |
* leftover from a failed REINDEX CONCURRENTLY, and if rebuilt it would |
| 3763 |
* not be possible to drop it anymore. |
| 3764 |
*/ |
| 3765 |
if (IsToastNamespace(RelationGetNamespace(iRel)) && |
| 3766 |
!get_index_isvalid(indexId)) |
| 3767 |
ereport(ERROR, |
| 3768 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 3769 |
errmsg("cannot reindex invalid index on TOAST table"))); |
| 3770 |
|
| 3771 |
/* |
| 3772 |
* System relations cannot be moved even if allow_system_table_mods is |
| 3773 |
* enabled to keep things consistent with the concurrent case where all |
| 3774 |
* the indexes of a relation are processed in series, including indexes of |
| 3775 |
* toast relations. |
| 3776 |
* |
| 3777 |
* Note that this check is not part of CheckRelationTableSpaceMove() as it |
| 3778 |
* gets used for ALTER TABLE SET TABLESPACE that could cascade across |
| 3779 |
* toast relations. |
| 3780 |
*/ |
| 3781 |
if (OidIsValid(params->tablespaceOid) && |
| 3782 |
IsSystemRelation(iRel)) |
| 3783 |
ereport(ERROR, |
| 3784 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 3785 |
errmsg("cannot move system relation \"%s\"", |
| 3786 |
RelationGetRelationName(iRel)))); |
| 3787 |
|
| 3788 |
/* Check if the tablespace of this index needs to be changed */ |
| 3789 |
if (OidIsValid(params->tablespaceOid) && |
| 3790 |
CheckRelationTableSpaceMove(iRel, params->tablespaceOid)) |
| 3791 |
set_tablespace = true; |
| 3792 |
|
| 3793 |
/* |
| 3794 |
* Also check for active uses of the index in the current transaction; we |
| 3795 |
* don't want to reindex underneath an open indexscan. |
| 3796 |
*/ |
| 3797 |
CheckTableNotInUse(iRel, "REINDEX INDEX"); |
| 3798 |
|
| 3799 |
/* Set new tablespace, if requested */ |
| 3800 |
if (set_tablespace) |
| 3801 |
{ |
| 3802 |
/* Update its pg_class row */ |
| 3803 |
SetRelationTableSpace(iRel, params->tablespaceOid, InvalidOid); |
| 3804 |
|
| 3805 |
/* |
| 3806 |
* Schedule unlinking of the old index storage at transaction commit. |
| 3807 |
*/ |
| 3808 |
RelationDropStorage(iRel); |
| 3809 |
RelationAssumeNewRelfilelocator(iRel); |
| 3810 |
|
| 3811 |
/* Make sure the reltablespace change is visible */ |
| 3812 |
CommandCounterIncrement(); |
| 3813 |
} |
| 3814 |
|
| 3815 |
/* |
| 3816 |
* All predicate locks on the index are about to be made invalid. Promote |
| 3817 |
* them to relation locks on the heap. |
| 3818 |
*/ |
| 3819 |
TransferPredicateLocksToHeapRelation(iRel); |
| 3820 |
|
| 3821 |
/* Fetch info needed for index_build */ |
| 3822 |
indexInfo = BuildIndexInfo(iRel); |
| 3823 |
|
| 3824 |
/* If requested, skip checking uniqueness/exclusion constraints */ |
| 3825 |
if (skip_constraint_checks) |
| 3826 |
{ |
| 3827 |
if (indexInfo->ii_Unique || indexInfo->ii_ExclusionOps != NULL) |
| 3828 |
skipped_constraint = true; |
| 3829 |
indexInfo->ii_Unique = false; |
| 3830 |
indexInfo->ii_ExclusionOps = NULL; |
| 3831 |
indexInfo->ii_ExclusionProcs = NULL; |
| 3832 |
indexInfo->ii_ExclusionStrats = NULL; |
| 3833 |
} |
| 3834 |
|
| 3835 |
/* Suppress use of the target index while rebuilding it */ |
| 3836 |
SetReindexProcessing(heapId, indexId); |
| 3837 |
|
| 3838 |
/* Create a new physical relation for the index */ |
| 3839 |
RelationSetNewRelfilenumber(iRel, persistence); |
| 3840 |
|
| 3841 |
/* Initialize the index and rebuild */ |
| 3842 |
/* Note: we do not need to re-establish pkey setting */ |
| 3843 |
index_build(heapRelation, iRel, indexInfo, true, true); |
| 3844 |
|
| 3845 |
/* Re-allow use of target index */ |
| 3846 |
ResetReindexProcessing(); |
| 3847 |
|
| 3848 |
/* |
| 3849 |
* If the index is marked invalid/not-ready/dead (ie, it's from a failed |
| 3850 |
* CREATE INDEX CONCURRENTLY, or a DROP INDEX CONCURRENTLY failed midway), |
| 3851 |
* and we didn't skip a uniqueness check, we can now mark it valid. This |
| 3852 |
* allows REINDEX to be used to clean up in such cases. |
| 3853 |
* |
| 3854 |
* We can also reset indcheckxmin, because we have now done a |
| 3855 |
* non-concurrent index build, *except* in the case where index_build |
| 3856 |
* found some still-broken HOT chains. If it did, and we don't have to |
| 3857 |
* change any of the other flags, we just leave indcheckxmin alone (note |
| 3858 |
* that index_build won't have changed it, because this is a reindex). |
| 3859 |
* This is okay and desirable because not updating the tuple leaves the |
| 3860 |
* index's usability horizon (recorded as the tuple's xmin value) the same |
| 3861 |
* as it was. |
| 3862 |
* |
| 3863 |
* But, if the index was invalid/not-ready/dead and there were broken HOT |
| 3864 |
* chains, we had better force indcheckxmin true, because the normal |
| 3865 |
* argument that the HOT chains couldn't conflict with the index is |
| 3866 |
* suspect for an invalid index. (A conflict is definitely possible if |
| 3867 |
* the index was dead. It probably shouldn't happen otherwise, but let's |
| 3868 |
* be conservative.) In this case advancing the usability horizon is |
| 3869 |
* appropriate. |
| 3870 |
* |
| 3871 |
* Another reason for avoiding unnecessary updates here is that while |
| 3872 |
* reindexing pg_index itself, we must not try to update tuples in it. |
| 3873 |
* pg_index's indexes should always have these flags in their clean state, |
| 3874 |
* so that won't happen. |
| 3875 |
*/ |
| 3876 |
if (!skipped_constraint) |
| 3877 |
{ |
| 3878 |
Relation pg_index; |
| 3879 |
HeapTuple indexTuple; |
| 3880 |
Form_pg_index indexForm; |
| 3881 |
bool index_bad; |
| 3882 |
|
| 3883 |
pg_index = table_open(IndexRelationId, RowExclusiveLock); |
| 3884 |
|
| 3885 |
indexTuple = SearchSysCacheCopy1(INDEXRELID, |
| 3886 |
ObjectIdGetDatum(indexId)); |
| 3887 |
if (!HeapTupleIsValid(indexTuple)) |
| 3888 |
elog(ERROR, "cache lookup failed for index %u", indexId); |
| 3889 |
indexForm = (Form_pg_index) GETSTRUCT(indexTuple); |
| 3890 |
|
| 3891 |
index_bad = (!indexForm->indisvalid || |
| 3892 |
!indexForm->indisready || |
| 3893 |
!indexForm->indislive); |
| 3894 |
if (index_bad || |
| 3895 |
(indexForm->indcheckxmin && !indexInfo->ii_BrokenHotChain)) |
| 3896 |
{ |
| 3897 |
if (!indexInfo->ii_BrokenHotChain) |
| 3898 |
indexForm->indcheckxmin = false; |
| 3899 |
else if (index_bad) |
| 3900 |
indexForm->indcheckxmin = true; |
| 3901 |
indexForm->indisvalid = true; |
| 3902 |
indexForm->indisready = true; |
| 3903 |
indexForm->indislive = true; |
| 3904 |
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple); |
| 3905 |
|
| 3906 |
/* |
| 3907 |
* Invalidate the relcache for the table, so that after we commit |
| 3908 |
* all sessions will refresh the table's index list. This ensures |
| 3909 |
* that if anyone misses seeing the pg_index row during this |
| 3910 |
* update, they'll refresh their list before attempting any update |
| 3911 |
* on the table. |
| 3912 |
*/ |
| 3913 |
CacheInvalidateRelcache(heapRelation); |
| 3914 |
} |
| 3915 |
|
| 3916 |
table_close(pg_index, RowExclusiveLock); |
| 3917 |
} |
| 3918 |
|
| 3919 |
/* Log what we did */ |
| 3920 |
if ((params->options & REINDEXOPT_VERBOSE) != 0) |
| 3921 |
ereport(INFO, |
| 3922 |
(errmsg("index \"%s\" was reindexed", |
| 3923 |
get_rel_name(indexId)), |
| 3924 |
errdetail_internal("%s", |
| 3925 |
pg_rusage_show(&ru0)))); |
| 3926 |
|
| 3927 |
/* Roll back any GUC changes executed by index functions */ |
| 3928 |
AtEOXact_GUC(false, save_nestlevel); |
| 3929 |
|
| 3930 |
/* Restore userid and security context */ |
| 3931 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 3932 |
|
| 3933 |
/* Close rels, but keep locks */ |
| 3934 |
index_close(iRel, NoLock); |
| 3935 |
table_close(heapRelation, NoLock); |
| 3936 |
|
| 3937 |
if (progress) |
| 3938 |
pgstat_progress_end_command(); |
| 3939 |
} |
| 3940 |
|
| 3941 |
/* |
| 3942 |
* reindex_relation - This routine is used to recreate all indexes |
| 3943 |
* of a relation (and optionally its toast relation too, if any). |
| 3944 |
* |
| 3945 |
* "flags" is a bitmask that can include any combination of these bits: |
| 3946 |
* |
| 3947 |
* REINDEX_REL_PROCESS_TOAST: if true, process the toast table too (if any). |
| 3948 |
* |
| 3949 |
* REINDEX_REL_SUPPRESS_INDEX_USE: if true, the relation was just completely |
| 3950 |
* rebuilt by an operation such as VACUUM FULL or CLUSTER, and therefore its |
| 3951 |
* indexes are inconsistent with it. This makes things tricky if the relation |
| 3952 |
* is a system catalog that we might consult during the reindexing. To deal |
| 3953 |
* with that case, we mark all of the indexes as pending rebuild so that they |
| 3954 |
* won't be trusted until rebuilt. The caller is required to call us *without* |
| 3955 |
* having made the rebuilt table visible by doing CommandCounterIncrement; |
| 3956 |
* we'll do CCI after having collected the index list. (This way we can still |
| 3957 |
* use catalog indexes while collecting the list.) |
| 3958 |
* |
| 3959 |
* REINDEX_REL_CHECK_CONSTRAINTS: if true, recheck unique and exclusion |
| 3960 |
* constraint conditions, else don't. To avoid deadlocks, VACUUM FULL or |
| 3961 |
* CLUSTER on a system catalog must omit this flag. REINDEX should be used to |
| 3962 |
* rebuild an index if constraint inconsistency is suspected. For optimal |
| 3963 |
* performance, other callers should include the flag only after transforming |
| 3964 |
* the data in a manner that risks a change in constraint validity. |
| 3965 |
* |
| 3966 |
* REINDEX_REL_FORCE_INDEXES_UNLOGGED: if true, set the persistence of the |
| 3967 |
* rebuilt indexes to unlogged. |
| 3968 |
* |
| 3969 |
* REINDEX_REL_FORCE_INDEXES_PERMANENT: if true, set the persistence of the |
| 3970 |
* rebuilt indexes to permanent. |
| 3971 |
* |
| 3972 |
* Returns true if any indexes were rebuilt (including toast table's index |
| 3973 |
* when relevant). Note that a CommandCounterIncrement will occur after each |
| 3974 |
* index rebuild. |
| 3975 |
*/ |
| 3976 |
bool |
| 3977 |
reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, |
| 3978 |
const ReindexParams *params) |
| 3979 |
{ |
| 3980 |
Relation rel; |
| 3981 |
Oid toast_relid; |
| 3982 |
List *indexIds; |
| 3983 |
char persistence; |
| 3984 |
bool result = false; |
| 3985 |
ListCell *indexId; |
| 3986 |
int i; |
| 3987 |
|
| 3988 |
/* |
| 3989 |
* Open and lock the relation. ShareLock is sufficient since we only need |
| 3990 |
* to prevent schema and data changes in it. The lock level used here |
| 3991 |
* should match ReindexTable(). |
| 3992 |
*/ |
| 3993 |
if ((params->options & REINDEXOPT_MISSING_OK) != 0) |
| 3994 |
rel = try_table_open(relid, ShareLock); |
| 3995 |
else |
| 3996 |
rel = table_open(relid, ShareLock); |
| 3997 |
|
| 3998 |
/* if relation is gone, leave */ |
| 3999 |
if (!rel) |
| 4000 |
return false; |
| 4001 |
|
| 4002 |
/* |
| 4003 |
* Partitioned tables should never get processed here, as they have no |
| 4004 |
* physical storage. |
| 4005 |
*/ |
| 4006 |
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) |
| 4007 |
elog(ERROR, "cannot reindex partitioned table \"%s.%s\"", |
| 4008 |
get_namespace_name(RelationGetNamespace(rel)), |
| 4009 |
RelationGetRelationName(rel)); |
| 4010 |
|
| 4011 |
toast_relid = rel->rd_rel->reltoastrelid; |
| 4012 |
|
| 4013 |
/* |
| 4014 |
* Get the list of index OIDs for this relation. (We trust the relcache |
| 4015 |
* to get this with a sequential scan if ignoring system indexes.) |
| 4016 |
*/ |
| 4017 |
indexIds = RelationGetIndexList(rel); |
| 4018 |
|
| 4019 |
if (flags & REINDEX_REL_SUPPRESS_INDEX_USE) |
| 4020 |
{ |
| 4021 |
/* Suppress use of all the indexes until they are rebuilt */ |
| 4022 |
SetReindexPending(indexIds); |
| 4023 |
|
| 4024 |
/* |
| 4025 |
* Make the new heap contents visible --- now things might be |
| 4026 |
* inconsistent! |
| 4027 |
*/ |
| 4028 |
CommandCounterIncrement(); |
| 4029 |
} |
| 4030 |
|
| 4031 |
/* |
| 4032 |
* Reindex the toast table, if any, before the main table. |
| 4033 |
* |
| 4034 |
* This helps in cases where a corruption in the toast table's index would |
| 4035 |
* otherwise error and stop REINDEX TABLE command when it tries to fetch a |
| 4036 |
* toasted datum. This way. the toast table's index is rebuilt and fixed |
| 4037 |
* before it is used for reindexing the main table. |
| 4038 |
* |
| 4039 |
* It is critical to call reindex_relation() *after* the call to |
| 4040 |
* RelationGetIndexList() returning the list of indexes on the relation, |
| 4041 |
* because reindex_relation() will call CommandCounterIncrement() after |
| 4042 |
* every reindex_index(). See REINDEX_REL_SUPPRESS_INDEX_USE for more |
| 4043 |
* details. |
| 4044 |
*/ |
| 4045 |
if ((flags & REINDEX_REL_PROCESS_TOAST) && OidIsValid(toast_relid)) |
| 4046 |
{ |
| 4047 |
/* |
| 4048 |
* Note that this should fail if the toast relation is missing, so |
| 4049 |
* reset REINDEXOPT_MISSING_OK. Even if a new tablespace is set for |
| 4050 |
* the parent relation, the indexes on its toast table are not moved. |
| 4051 |
* This rule is enforced by setting tablespaceOid to InvalidOid. |
| 4052 |
*/ |
| 4053 |
ReindexParams newparams = *params; |
| 4054 |
|
| 4055 |
newparams.options &= ~(REINDEXOPT_MISSING_OK); |
| 4056 |
newparams.tablespaceOid = InvalidOid; |
| 4057 |
result |= reindex_relation(stmt, toast_relid, flags, &newparams); |
| 4058 |
} |
| 4059 |
|
| 4060 |
/* |
| 4061 |
* Compute persistence of indexes: same as that of owning rel, unless |
| 4062 |
* caller specified otherwise. |
| 4063 |
*/ |
| 4064 |
if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED) |
| 4065 |
persistence = RELPERSISTENCE_UNLOGGED; |
| 4066 |
else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT) |
| 4067 |
persistence = RELPERSISTENCE_PERMANENT; |
| 4068 |
else |
| 4069 |
persistence = rel->rd_rel->relpersistence; |
| 4070 |
|
| 4071 |
/* Reindex all the indexes. */ |
| 4072 |
i = 1; |
| 4073 |
foreach(indexId, indexIds) |
| 4074 |
{ |
| 4075 |
Oid indexOid = lfirst_oid(indexId); |
| 4076 |
Oid indexNamespaceId = get_rel_namespace(indexOid); |
| 4077 |
|
| 4078 |
/* |
| 4079 |
* Skip any invalid indexes on a TOAST table. These can only be |
| 4080 |
* duplicate leftovers from a failed REINDEX CONCURRENTLY, and if |
| 4081 |
* rebuilt it would not be possible to drop them anymore. |
| 4082 |
*/ |
| 4083 |
if (IsToastNamespace(indexNamespaceId) && |
| 4084 |
!get_index_isvalid(indexOid)) |
| 4085 |
{ |
| 4086 |
ereport(WARNING, |
| 4087 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 4088 |
errmsg("cannot reindex invalid index \"%s.%s\" on TOAST table, skipping", |
| 4089 |
get_namespace_name(indexNamespaceId), |
| 4090 |
get_rel_name(indexOid)))); |
| 4091 |
|
| 4092 |
/* |
| 4093 |
* Remove this invalid toast index from the reindex pending list, |
| 4094 |
* as it is skipped here due to the hard failure that would happen |
| 4095 |
* in reindex_index(), should we try to process it. |
| 4096 |
*/ |
| 4097 |
if (flags & REINDEX_REL_SUPPRESS_INDEX_USE) |
| 4098 |
RemoveReindexPending(indexOid); |
| 4099 |
continue; |
| 4100 |
} |
| 4101 |
|
| 4102 |
reindex_index(stmt, indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS), |
| 4103 |
persistence, params); |
| 4104 |
|
| 4105 |
CommandCounterIncrement(); |
| 4106 |
|
| 4107 |
/* Index should no longer be in the pending list */ |
| 4108 |
Assert(!ReindexIsProcessingIndex(indexOid)); |
| 4109 |
|
| 4110 |
/* Set index rebuild count */ |
| 4111 |
pgstat_progress_update_param(PROGRESS_CLUSTER_INDEX_REBUILD_COUNT, |
| 4112 |
i); |
| 4113 |
i++; |
| 4114 |
} |
| 4115 |
|
| 4116 |
/* |
| 4117 |
* Close rel, but continue to hold the lock. |
| 4118 |
*/ |
| 4119 |
table_close(rel, NoLock); |
| 4120 |
|
| 4121 |
result |= (indexIds != NIL); |
| 4122 |
|
| 4123 |
return result; |
| 4124 |
} |
| 4125 |
|
| 4126 |
|
| 4127 |
/* ---------------------------------------------------------------- |
| 4128 |
* System index reindexing support |
| 4129 |
* |
| 4130 |
* When we are busy reindexing a system index, this code provides support |
| 4131 |
* for preventing catalog lookups from using that index. We also make use |
| 4132 |
* of this to catch attempted uses of user indexes during reindexing of |
| 4133 |
* those indexes. This information is propagated to parallel workers; |
| 4134 |
* attempting to change it during a parallel operation is not permitted. |
| 4135 |
* ---------------------------------------------------------------- |
| 4136 |
*/ |
| 4137 |
|
| 4138 |
static Oid currentlyReindexedHeap = InvalidOid; |
| 4139 |
static Oid currentlyReindexedIndex = InvalidOid; |
| 4140 |
static List *pendingReindexedIndexes = NIL; |
| 4141 |
static int reindexingNestLevel = 0; |
| 4142 |
|
| 4143 |
/* |
| 4144 |
* ReindexIsProcessingHeap |
| 4145 |
* True if heap specified by OID is currently being reindexed. |
| 4146 |
*/ |
| 4147 |
bool |
| 4148 |
ReindexIsProcessingHeap(Oid heapOid) |
| 4149 |
{ |
| 4150 |
return heapOid == currentlyReindexedHeap; |
| 4151 |
} |
| 4152 |
|
| 4153 |
/* |
| 4154 |
* ReindexIsCurrentlyProcessingIndex |
| 4155 |
* True if index specified by OID is currently being reindexed. |
| 4156 |
*/ |
| 4157 |
static bool |
| 4158 |
ReindexIsCurrentlyProcessingIndex(Oid indexOid) |
| 4159 |
{ |
| 4160 |
return indexOid == currentlyReindexedIndex; |
| 4161 |
} |
| 4162 |
|
| 4163 |
/* |
| 4164 |
* ReindexIsProcessingIndex |
| 4165 |
* True if index specified by OID is currently being reindexed, |
| 4166 |
* or should be treated as invalid because it is awaiting reindex. |
| 4167 |
*/ |
| 4168 |
bool |
| 4169 |
ReindexIsProcessingIndex(Oid indexOid) |
| 4170 |
{ |
| 4171 |
return indexOid == currentlyReindexedIndex || |
| 4172 |
list_member_oid(pendingReindexedIndexes, indexOid); |
| 4173 |
} |
| 4174 |
|
| 4175 |
/* |
| 4176 |
* SetReindexProcessing |
| 4177 |
* Set flag that specified heap/index are being reindexed. |
| 4178 |
*/ |
| 4179 |
static void |
| 4180 |
SetReindexProcessing(Oid heapOid, Oid indexOid) |
| 4181 |
{ |
| 4182 |
Assert(OidIsValid(heapOid) && OidIsValid(indexOid)); |
| 4183 |
/* Reindexing is not re-entrant. */ |
| 4184 |
if (OidIsValid(currentlyReindexedHeap)) |
| 4185 |
elog(ERROR, "cannot reindex while reindexing"); |
| 4186 |
currentlyReindexedHeap = heapOid; |
| 4187 |
currentlyReindexedIndex = indexOid; |
| 4188 |
/* Index is no longer "pending" reindex. */ |
| 4189 |
RemoveReindexPending(indexOid); |
| 4190 |
/* This may have been set already, but in case it isn't, do so now. */ |
| 4191 |
reindexingNestLevel = GetCurrentTransactionNestLevel(); |
| 4192 |
} |
| 4193 |
|
| 4194 |
/* |
| 4195 |
* ResetReindexProcessing |
| 4196 |
* Unset reindexing status. |
| 4197 |
*/ |
| 4198 |
static void |
| 4199 |
ResetReindexProcessing(void) |
| 4200 |
{ |
| 4201 |
currentlyReindexedHeap = InvalidOid; |
| 4202 |
currentlyReindexedIndex = InvalidOid; |
| 4203 |
/* reindexingNestLevel remains set till end of (sub)transaction */ |
| 4204 |
} |
| 4205 |
|
| 4206 |
/* |
| 4207 |
* SetReindexPending |
| 4208 |
* Mark the given indexes as pending reindex. |
| 4209 |
* |
| 4210 |
* NB: we assume that the current memory context stays valid throughout. |
| 4211 |
*/ |
| 4212 |
static void |
| 4213 |
SetReindexPending(List *indexes) |
| 4214 |
{ |
| 4215 |
/* Reindexing is not re-entrant. */ |
| 4216 |
if (pendingReindexedIndexes) |
| 4217 |
elog(ERROR, "cannot reindex while reindexing"); |
| 4218 |
if (IsInParallelMode()) |
| 4219 |
elog(ERROR, "cannot modify reindex state during a parallel operation"); |
| 4220 |
pendingReindexedIndexes = list_copy(indexes); |
| 4221 |
reindexingNestLevel = GetCurrentTransactionNestLevel(); |
| 4222 |
} |
| 4223 |
|
| 4224 |
/* |
| 4225 |
* RemoveReindexPending |
| 4226 |
* Remove the given index from the pending list. |
| 4227 |
*/ |
| 4228 |
static void |
| 4229 |
RemoveReindexPending(Oid indexOid) |
| 4230 |
{ |
| 4231 |
if (IsInParallelMode()) |
| 4232 |
elog(ERROR, "cannot modify reindex state during a parallel operation"); |
| 4233 |
pendingReindexedIndexes = list_delete_oid(pendingReindexedIndexes, |
| 4234 |
indexOid); |
| 4235 |
} |
| 4236 |
|
| 4237 |
/* |
| 4238 |
* ResetReindexState |
| 4239 |
* Clear all reindexing state during (sub)transaction abort. |
| 4240 |
*/ |
| 4241 |
void |
| 4242 |
ResetReindexState(int nestLevel) |
| 4243 |
{ |
| 4244 |
/* |
| 4245 |
* Because reindexing is not re-entrant, we don't need to cope with nested |
| 4246 |
* reindexing states. We just need to avoid messing up the outer-level |
| 4247 |
* state in case a subtransaction fails within a REINDEX. So checking the |
| 4248 |
* current nest level against that of the reindex operation is sufficient. |
| 4249 |
*/ |
| 4250 |
if (reindexingNestLevel >= nestLevel) |
| 4251 |
{ |
| 4252 |
currentlyReindexedHeap = InvalidOid; |
| 4253 |
currentlyReindexedIndex = InvalidOid; |
| 4254 |
|
| 4255 |
/* |
| 4256 |
* We needn't try to release the contents of pendingReindexedIndexes; |
| 4257 |
* that list should be in a transaction-lifespan context, so it will |
| 4258 |
* go away automatically. |
| 4259 |
*/ |
| 4260 |
pendingReindexedIndexes = NIL; |
| 4261 |
|
| 4262 |
reindexingNestLevel = 0; |
| 4263 |
} |
| 4264 |
} |
| 4265 |
|
| 4266 |
/* |
| 4267 |
* EstimateReindexStateSpace |
| 4268 |
* Estimate space needed to pass reindex state to parallel workers. |
| 4269 |
*/ |
| 4270 |
Size |
| 4271 |
EstimateReindexStateSpace(void) |
| 4272 |
{ |
| 4273 |
return offsetof(SerializedReindexState, pendingReindexedIndexes) |
| 4274 |
+ mul_size(sizeof(Oid), list_length(pendingReindexedIndexes)); |
| 4275 |
} |
| 4276 |
|
| 4277 |
/* |
| 4278 |
* SerializeReindexState |
| 4279 |
* Serialize reindex state for parallel workers. |
| 4280 |
*/ |
| 4281 |
void |
| 4282 |
SerializeReindexState(Size maxsize, char *start_address) |
| 4283 |
{ |
| 4284 |
SerializedReindexState *sistate = (SerializedReindexState *) start_address; |
| 4285 |
int c = 0; |
| 4286 |
ListCell *lc; |
| 4287 |
|
| 4288 |
sistate->currentlyReindexedHeap = currentlyReindexedHeap; |
| 4289 |
sistate->currentlyReindexedIndex = currentlyReindexedIndex; |
| 4290 |
sistate->numPendingReindexedIndexes = list_length(pendingReindexedIndexes); |
| 4291 |
foreach(lc, pendingReindexedIndexes) |
| 4292 |
sistate->pendingReindexedIndexes[c++] = lfirst_oid(lc); |
| 4293 |
} |
| 4294 |
|
| 4295 |
/* |
| 4296 |
* RestoreReindexState |
| 4297 |
* Restore reindex state in a parallel worker. |
| 4298 |
*/ |
| 4299 |
void |
| 4300 |
RestoreReindexState(const void *reindexstate) |
| 4301 |
{ |
| 4302 |
const SerializedReindexState *sistate = (const SerializedReindexState *) reindexstate; |
| 4303 |
int c = 0; |
| 4304 |
MemoryContext oldcontext; |
| 4305 |
|
| 4306 |
currentlyReindexedHeap = sistate->currentlyReindexedHeap; |
| 4307 |
currentlyReindexedIndex = sistate->currentlyReindexedIndex; |
| 4308 |
|
| 4309 |
Assert(pendingReindexedIndexes == NIL); |
| 4310 |
oldcontext = MemoryContextSwitchTo(TopMemoryContext); |
| 4311 |
for (c = 0; c < sistate->numPendingReindexedIndexes; ++c) |
| 4312 |
pendingReindexedIndexes = |
| 4313 |
lappend_oid(pendingReindexedIndexes, |
| 4314 |
sistate->pendingReindexedIndexes[c]); |
| 4315 |
MemoryContextSwitchTo(oldcontext); |
| 4316 |
|
| 4317 |
/* Note the worker has its own transaction nesting level */ |
| 4318 |
reindexingNestLevel = GetCurrentTransactionNestLevel(); |
| 4319 |
} |