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