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