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