Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * indexcmds.c
4 : : * POSTGRES define and remove index code.
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/commands/indexcmds.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include "access/amapi.h"
19 : : #include "access/attmap.h"
20 : : #include "access/gist.h"
21 : : #include "access/heapam.h"
22 : : #include "access/htup_details.h"
23 : : #include "access/reloptions.h"
24 : : #include "access/sysattr.h"
25 : : #include "access/tableam.h"
26 : : #include "access/xact.h"
27 : : #include "catalog/catalog.h"
28 : : #include "catalog/index.h"
29 : : #include "catalog/indexing.h"
30 : : #include "catalog/namespace.h"
31 : : #include "catalog/pg_am.h"
32 : : #include "catalog/pg_authid.h"
33 : : #include "catalog/pg_collation.h"
34 : : #include "catalog/pg_constraint.h"
35 : : #include "catalog/pg_database.h"
36 : : #include "catalog/pg_inherits.h"
37 : : #include "catalog/pg_namespace.h"
38 : : #include "catalog/pg_opclass.h"
39 : : #include "catalog/pg_tablespace.h"
40 : : #include "catalog/pg_type.h"
41 : : #include "commands/comment.h"
42 : : #include "commands/defrem.h"
43 : : #include "commands/event_trigger.h"
44 : : #include "commands/progress.h"
45 : : #include "commands/tablecmds.h"
46 : : #include "commands/tablespace.h"
47 : : #include "mb/pg_wchar.h"
48 : : #include "miscadmin.h"
49 : : #include "nodes/makefuncs.h"
50 : : #include "nodes/nodeFuncs.h"
51 : : #include "optimizer/optimizer.h"
52 : : #include "parser/parse_coerce.h"
53 : : #include "parser/parse_oper.h"
54 : : #include "parser/parse_utilcmd.h"
55 : : #include "partitioning/partdesc.h"
56 : : #include "pgstat.h"
57 : : #include "rewrite/rewriteManip.h"
58 : : #include "storage/lmgr.h"
59 : : #include "storage/proc.h"
60 : : #include "storage/procarray.h"
61 : : #include "utils/acl.h"
62 : : #include "utils/builtins.h"
63 : : #include "utils/fmgroids.h"
64 : : #include "utils/guc.h"
65 : : #include "utils/injection_point.h"
66 : : #include "utils/inval.h"
67 : : #include "utils/lsyscache.h"
68 : : #include "utils/memutils.h"
69 : : #include "utils/partcache.h"
70 : : #include "utils/pg_rusage.h"
71 : : #include "utils/regproc.h"
72 : : #include "utils/snapmgr.h"
73 : : #include "utils/syscache.h"
74 : :
75 : :
76 : : /* non-export function prototypes */
77 : : static bool CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts);
78 : : static void CheckPredicate(Expr *predicate);
79 : : static void ComputeIndexAttrs(ParseState *pstate,
80 : : IndexInfo *indexInfo,
81 : : Oid *typeOids,
82 : : Oid *collationOids,
83 : : Oid *opclassOids,
84 : : Datum *opclassOptions,
85 : : int16 *colOptions,
86 : : const List *attList,
87 : : const List *exclusionOpNames,
88 : : Oid relId,
89 : : const char *accessMethodName,
90 : : Oid accessMethodId,
91 : : bool amcanorder,
92 : : bool isconstraint,
93 : : bool iswithoutoverlaps,
94 : : Oid ddl_userid,
95 : : int ddl_sec_context,
96 : : int *ddl_save_nestlevel);
97 : : static char *ChooseIndexName(const char *tabname, Oid namespaceId,
98 : : const List *colnames, const List *exclusionOpNames,
99 : : bool primary, bool isconstraint);
100 : : static char *ChooseIndexNameAddition(const List *colnames);
101 : : static List *ChooseIndexColumnNames(const List *indexElems);
102 : : static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params,
103 : : bool isTopLevel);
104 : : static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
105 : : Oid relId, Oid oldRelId, void *arg);
106 : : static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params,
107 : : bool isTopLevel);
108 : : static void ReindexMultipleTables(const ReindexStmt *stmt,
109 : : const ReindexParams *params);
110 : : static void reindex_error_callback(void *arg);
111 : : static void ReindexPartitions(const ReindexStmt *stmt, Oid relid,
112 : : const ReindexParams *params, bool isTopLevel);
113 : : static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids,
114 : : const ReindexParams *params);
115 : : static bool ReindexRelationConcurrently(const ReindexStmt *stmt,
116 : : Oid relationOid,
117 : : const ReindexParams *params);
118 : : static void update_relispartition(Oid relationId, bool newval);
119 : : static inline void set_indexsafe_procflags(void);
120 : :
121 : : /*
122 : : * callback argument type for RangeVarCallbackForReindexIndex()
123 : : */
124 : : struct ReindexIndexCallbackState
125 : : {
126 : : ReindexParams params; /* options from statement */
127 : : Oid locked_table_oid; /* tracks previously locked table */
128 : : };
129 : :
130 : : /*
131 : : * callback arguments for reindex_error_callback()
132 : : */
133 : : typedef struct ReindexErrorInfo
134 : : {
135 : : char *relname;
136 : : char *relnamespace;
137 : : char relkind;
138 : : } ReindexErrorInfo;
139 : :
140 : : /*
141 : : * CheckIndexCompatible
142 : : * Determine whether an existing index definition is compatible with a
143 : : * prospective index definition, such that the existing index storage
144 : : * could become the storage of the new index, avoiding a rebuild.
145 : : *
146 : : * 'oldId': the OID of the existing index
147 : : * 'accessMethodName': name of the AM to use.
148 : : * 'attributeList': a list of IndexElem specifying columns and expressions
149 : : * to index on.
150 : : * 'exclusionOpNames': list of names of exclusion-constraint operators,
151 : : * or NIL if not an exclusion constraint.
152 : : * 'isWithoutOverlaps': true iff this index has a WITHOUT OVERLAPS clause.
153 : : *
154 : : * This is tailored to the needs of ALTER TABLE ALTER TYPE, which recreates
155 : : * any indexes that depended on a changing column from their pg_get_indexdef
156 : : * or pg_get_constraintdef definitions. We omit some of the sanity checks of
157 : : * DefineIndex. We assume that the old and new indexes have the same number
158 : : * of columns and that if one has an expression column or predicate, both do.
159 : : * Errors arising from the attribute list still apply.
160 : : *
161 : : * Most column type changes that can skip a table rewrite do not invalidate
162 : : * indexes. We acknowledge this when all operator classes, collations and
163 : : * exclusion operators match. Though we could further permit intra-opfamily
164 : : * changes for btree and hash indexes, that adds subtle complexity with no
165 : : * concrete benefit for core types. Note, that INCLUDE columns aren't
166 : : * checked by this function, for them it's enough that table rewrite is
167 : : * skipped.
168 : : *
169 : : * When a comparison or exclusion operator has a polymorphic input type, the
170 : : * actual input types must also match. This defends against the possibility
171 : : * that operators could vary behavior in response to get_fn_expr_argtype().
172 : : * At present, this hazard is theoretical: check_exclusion_constraint() and
173 : : * all core index access methods decline to set fn_expr for such calls.
174 : : *
175 : : * We do not yet implement a test to verify compatibility of expression
176 : : * columns or predicates, so assume any such index is incompatible.
177 : : */
178 : : bool
5405 rhaas@postgresql.org 179 :CBC 73 : CheckIndexCompatible(Oid oldId,
180 : : const char *accessMethodName,
181 : : const List *attributeList,
182 : : const List *exclusionOpNames,
183 : : bool isWithoutOverlaps)
184 : : {
185 : : bool isconstraint;
186 : : Oid *typeIds;
187 : : Oid *collationIds;
188 : : Oid *opclassIds;
189 : : Datum *opclassOptions;
190 : : Oid accessMethodId;
191 : : Oid relationId;
192 : : HeapTuple tuple;
193 : : Form_pg_index indexForm;
194 : : Form_pg_am accessMethodForm;
195 : : const IndexAmRoutine *amRoutine;
196 : : bool amcanorder;
197 : : bool amsummarizing;
198 : : int16 *coloptions;
199 : : IndexInfo *indexInfo;
200 : : int numberOfAttributes;
201 : : int old_natts;
202 : 73 : bool ret = true;
203 : : oidvector *old_indclass;
204 : : oidvector *old_indcollation;
205 : : Relation irel;
206 : : int i;
207 : : Datum d;
208 : :
209 : : /* Caller should already have the relation locked in some way. */
4460 210 : 73 : relationId = IndexGetRelation(oldId, false);
211 : :
212 : : /*
213 : : * We can pretend isconstraint = false unconditionally. It only serves to
214 : : * decide the text of an error message that should never happen for us.
215 : : */
5405 216 : 73 : isconstraint = false;
217 : :
218 : 73 : numberOfAttributes = list_length(attributeList);
219 [ - + ]: 73 : Assert(numberOfAttributes > 0);
220 [ - + ]: 73 : Assert(numberOfAttributes <= INDEX_MAX_KEYS);
221 : :
222 : : /* look up the access method */
223 : 73 : tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
224 [ - + ]: 73 : if (!HeapTupleIsValid(tuple))
5405 rhaas@postgresql.org 225 [ # # ]:UBC 0 : ereport(ERROR,
226 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
227 : : errmsg("access method \"%s\" does not exist",
228 : : accessMethodName)));
5405 rhaas@postgresql.org 229 :CBC 73 : accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
2723 andres@anarazel.de 230 : 73 : accessMethodId = accessMethodForm->oid;
3761 tgl@sss.pgh.pa.us 231 : 73 : amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
5405 rhaas@postgresql.org 232 : 73 : ReleaseSysCache(tuple);
233 : :
3761 tgl@sss.pgh.pa.us 234 : 73 : amcanorder = amRoutine->amcanorder;
1142 tomas.vondra@postgre 235 : 73 : amsummarizing = amRoutine->amsummarizing;
236 : :
237 : : /*
238 : : * Compute the operator classes, collations, and exclusion operators for
239 : : * the new index, so we can test whether it's compatible with the existing
240 : : * one. Note that ComputeIndexAttrs might fail here, but that's OK:
241 : : * DefineIndex would have failed later. Our attributeList contains only
242 : : * key attributes, thus we're filling ii_NumIndexAttrs and
243 : : * ii_NumIndexKeyAttrs with same value.
244 : : */
2472 michael@paquier.xyz 245 : 73 : indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
246 : : accessMethodId, NIL, NIL, false, false,
247 : : false, false, amsummarizing, isWithoutOverlaps);
986 peter@eisentraut.org 248 : 73 : typeIds = palloc_array(Oid, numberOfAttributes);
249 : 73 : collationIds = palloc_array(Oid, numberOfAttributes);
250 : 73 : opclassIds = palloc_array(Oid, numberOfAttributes);
945 251 : 73 : opclassOptions = palloc_array(Datum, numberOfAttributes);
1331 252 : 73 : coloptions = palloc_array(int16, numberOfAttributes);
121 tgl@sss.pgh.pa.us 253 :GNC 73 : ComputeIndexAttrs(NULL, indexInfo,
254 : : typeIds, collationIds, opclassIds, opclassOptions,
255 : : coloptions, attributeList,
256 : : exclusionOpNames, relationId,
257 : : accessMethodName, accessMethodId,
258 : : amcanorder, isconstraint, isWithoutOverlaps, InvalidOid,
259 : : 0, NULL);
260 : :
261 : : /* Get the soon-obsolete pg_index tuple. */
5405 rhaas@postgresql.org 262 :CBC 73 : tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldId));
263 [ - + ]: 73 : if (!HeapTupleIsValid(tuple))
5405 rhaas@postgresql.org 264 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u", oldId);
4906 tgl@sss.pgh.pa.us 265 :CBC 73 : indexForm = (Form_pg_index) GETSTRUCT(tuple);
266 : :
267 : : /*
268 : : * We don't assess expressions or predicates; assume incompatibility.
269 : : * Also, if the index is invalid for any reason, treat it as incompatible.
270 : : */
2960 andrew@dunslane.net 271 [ + - + + ]: 146 : if (!(heap_attisnull(tuple, Anum_pg_index_indpred, NULL) &&
272 : 73 : heap_attisnull(tuple, Anum_pg_index_indexprs, NULL) &&
2686 peter_e@gmx.net 273 [ - + ]: 69 : indexForm->indisvalid))
274 : : {
5405 rhaas@postgresql.org 275 :GBC 4 : ReleaseSysCache(tuple);
276 : 4 : return false;
277 : : }
278 : :
279 : : /* Any change in operator class or collation breaks compatibility. */
2950 teodor@sigaev.ru 280 :CBC 69 : old_natts = indexForm->indnkeyatts;
5405 rhaas@postgresql.org 281 [ - + ]: 69 : Assert(old_natts == numberOfAttributes);
282 : :
1137 dgustafsson@postgres 283 : 69 : d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indcollation);
5405 rhaas@postgresql.org 284 : 69 : old_indcollation = (oidvector *) DatumGetPointer(d);
285 : :
1137 dgustafsson@postgres 286 : 69 : d = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indclass);
5405 rhaas@postgresql.org 287 : 69 : old_indclass = (oidvector *) DatumGetPointer(d);
288 : :
986 peter@eisentraut.org 289 [ + - ]: 138 : ret = (memcmp(old_indclass->values, opclassIds, old_natts * sizeof(Oid)) == 0 &&
290 [ + - ]: 69 : memcmp(old_indcollation->values, collationIds, old_natts * sizeof(Oid)) == 0);
291 : :
5405 rhaas@postgresql.org 292 : 69 : ReleaseSysCache(tuple);
293 : :
5213 294 [ - + ]: 69 : if (!ret)
5213 rhaas@postgresql.org 295 :UBC 0 : return false;
296 : :
297 : : /* For polymorphic opcintype, column type changes break compatibility. */
5077 bruce@momjian.us 298 :CBC 69 : irel = index_open(oldId, AccessShareLock); /* caller probably has a lock */
5213 rhaas@postgresql.org 299 [ + + ]: 142 : for (i = 0; i < old_natts; i++)
300 : : {
986 peter@eisentraut.org 301 [ + - + - : 73 : if (IsPolymorphicType(get_opclass_input_type(opclassIds[i])) &&
+ - + - +
- + - + -
+ - + - +
- - + ]
986 peter@eisentraut.org 302 [ # # ]:UBC 0 : TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
303 : : {
5213 rhaas@postgresql.org 304 : 0 : ret = false;
305 : 0 : break;
306 : : }
307 : : }
308 : :
309 : : /* Any change in opclass options break compatibility. */
2227 akorotkov@postgresql 310 [ + - ]:CBC 69 : if (ret)
311 : : {
945 peter@eisentraut.org 312 : 69 : Datum *oldOpclassOptions = palloc_array(Datum, old_natts);
313 : :
314 [ + + ]: 142 : for (i = 0; i < old_natts; i++)
315 : 73 : oldOpclassOptions[i] = get_attoptions(oldId, i + 1);
316 : :
317 : 69 : ret = CompareOpclassOptions(oldOpclassOptions, opclassOptions, old_natts);
318 : :
319 : 69 : pfree(oldOpclassOptions);
320 : : }
321 : :
322 : : /* Any change in exclusion operator selections breaks compatibility. */
5214 rhaas@postgresql.org 323 [ + - - + ]: 69 : if (ret && indexInfo->ii_ExclusionOps != NULL)
324 : : {
325 : : Oid *old_operators,
326 : : *old_procs;
327 : : uint16 *old_strats;
328 : :
5405 rhaas@postgresql.org 329 :UBC 0 : RelationGetExclusionInfo(irel, &old_operators, &old_procs, &old_strats);
5214 330 : 0 : ret = memcmp(old_operators, indexInfo->ii_ExclusionOps,
331 : : old_natts * sizeof(Oid)) == 0;
332 : :
333 : : /* Require an exact input type match for polymorphic operators. */
5213 334 [ # # ]: 0 : if (ret)
335 : : {
336 [ # # # # ]: 0 : for (i = 0; i < old_natts && ret; i++)
337 : : {
338 : : Oid left,
339 : : right;
340 : :
341 : 0 : op_input_types(indexInfo->ii_ExclusionOps[i], &left, &right);
342 [ # # # # : 0 : if ((IsPolymorphicType(left) || IsPolymorphicType(right)) &&
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
986 peter@eisentraut.org 343 [ # # ]: 0 : TupleDescAttr(irel->rd_att, i)->atttypid != typeIds[i])
344 : : {
5213 rhaas@postgresql.org 345 : 0 : ret = false;
346 : 0 : break;
347 : : }
348 : : }
349 : : }
350 : : }
351 : :
5214 rhaas@postgresql.org 352 :CBC 69 : index_close(irel, NoLock);
5405 353 : 69 : return ret;
354 : : }
355 : :
356 : : /*
357 : : * CompareOpclassOptions
358 : : *
359 : : * Compare per-column opclass options which are represented by arrays of text[]
360 : : * datums. Both elements of arrays and array themselves can be NULL.
361 : : */
362 : : static bool
986 peter@eisentraut.org 363 : 69 : CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
364 : : {
365 : : int i;
366 : : FmgrInfo fm;
367 : :
2227 akorotkov@postgresql 368 [ - + - - ]: 69 : if (!opts1 && !opts2)
2227 akorotkov@postgresql 369 :UBC 0 : return true;
370 : :
539 akorotkov@postgresql 371 :CBC 69 : fmgr_info(F_ARRAY_EQ, &fm);
2227 372 [ + + ]: 142 : for (i = 0; i < natts; i++)
373 : : {
374 [ + - ]: 73 : Datum opt1 = opts1 ? opts1[i] : (Datum) 0;
375 [ + - ]: 73 : Datum opt2 = opts2 ? opts2[i] : (Datum) 0;
376 : :
377 [ + + ]: 73 : if (opt1 == (Datum) 0)
378 : : {
379 [ + - ]: 72 : if (opt2 == (Datum) 0)
380 : 72 : continue;
381 : : else
2227 akorotkov@postgresql 382 :UBC 0 : return false;
383 : : }
2227 akorotkov@postgresql 384 [ - + ]:CBC 1 : else if (opt2 == (Datum) 0)
2227 akorotkov@postgresql 385 :UBC 0 : return false;
386 : :
387 : : /*
388 : : * Compare non-NULL text[] datums. Use C collation to enforce binary
389 : : * equivalence of texts, because we don't know anything about the
390 : : * semantics of opclass options.
391 : : */
539 akorotkov@postgresql 392 [ - + ]:CBC 1 : if (!DatumGetBool(FunctionCall2Coll(&fm, C_COLLATION_OID, opt1, opt2)))
2227 akorotkov@postgresql 393 :UBC 0 : return false;
394 : : }
395 : :
2227 akorotkov@postgresql 396 :CBC 69 : return true;
397 : : }
398 : :
399 : : /*
400 : : * WaitForOlderSnapshots
401 : : *
402 : : * Wait for transactions that might have an older snapshot than the given xmin
403 : : * limit, because it might not contain tuples deleted just before it has
404 : : * been taken. Obtain a list of VXIDs of such transactions, and wait for them
405 : : * individually. This is used when building an index concurrently.
406 : : *
407 : : * We can exclude any running transactions that have xmin > the xmin given;
408 : : * their oldest snapshot must be newer than our xmin limit.
409 : : * We can also exclude any transactions that have xmin = zero, since they
410 : : * evidently have no live snapshot at all (and any one they might be in
411 : : * process of taking is certainly newer than ours). Transactions in other
412 : : * DBs can be ignored too, since they'll never even be able to see the
413 : : * index being worked on.
414 : : *
415 : : * We can also exclude autovacuum processes and processes running manual
416 : : * lazy VACUUMs, because they won't be fazed by missing index entries
417 : : * either. (Manual ANALYZEs, however, can't be excluded because they
418 : : * might be within transactions that are going to do arbitrary operations
419 : : * later.) Processes running CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY
420 : : * on indexes that are neither expressional nor partial are also safe to
421 : : * ignore, since we know that those processes won't examine any data
422 : : * outside the table they're indexing.
423 : : *
424 : : * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
425 : : * check for that.
426 : : *
427 : : * If a process goes idle-in-transaction with xmin zero, we do not need to
428 : : * wait for it anymore, per the above argument. We do not have the
429 : : * infrastructure right now to stop waiting if that happens, but we can at
430 : : * least avoid the folly of waiting when it is idle at the time we would
431 : : * begin to wait. We do this by repeatedly rechecking the output of
432 : : * GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
433 : : * doesn't show up in the output, we know we can forget about it.
434 : : */
435 : : void
2590 alvherre@alvh.no-ip. 436 : 406 : WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
437 : : {
438 : : int n_old_snapshots;
439 : : int i;
440 : : VirtualTransactionId *old_snapshots;
441 : :
2594 peter@eisentraut.org 442 : 406 : old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false,
443 : : PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
444 : : | PROC_IN_SAFE_IC,
445 : : &n_old_snapshots);
2590 alvherre@alvh.no-ip. 446 [ + + ]: 406 : if (progress)
447 : 399 : pgstat_progress_update_param(PROGRESS_WAITFOR_TOTAL, n_old_snapshots);
448 : :
2594 peter@eisentraut.org 449 [ + + ]: 639 : for (i = 0; i < n_old_snapshots; i++)
450 : : {
451 [ + + ]: 233 : if (!VirtualTransactionIdIsValid(old_snapshots[i]))
452 : 62 : continue; /* found uninteresting in previous cycle */
453 : :
454 [ + + ]: 171 : if (i > 0)
455 : : {
456 : : /* see if anything's changed ... */
457 : : VirtualTransactionId *newer_snapshots;
458 : : int n_newer_snapshots;
459 : : int j;
460 : : int k;
461 : :
462 : 87 : newer_snapshots = GetCurrentVirtualXIDs(limitXmin,
463 : : true, false,
464 : : PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
465 : : | PROC_IN_SAFE_IC,
466 : : &n_newer_snapshots);
467 [ + + ]: 356 : for (j = i; j < n_old_snapshots; j++)
468 : : {
469 [ + + ]: 269 : if (!VirtualTransactionIdIsValid(old_snapshots[j]))
470 : 48 : continue; /* found uninteresting in previous cycle */
471 [ + + ]: 853 : for (k = 0; k < n_newer_snapshots; k++)
472 : : {
473 [ + + + + ]: 751 : if (VirtualTransactionIdEquals(old_snapshots[j],
474 : : newer_snapshots[k]))
475 : 119 : break;
476 : : }
477 [ + + ]: 221 : if (k >= n_newer_snapshots) /* not there anymore */
478 : 102 : SetInvalidVirtualTransactionId(old_snapshots[j]);
479 : : }
480 : 87 : pfree(newer_snapshots);
481 : : }
482 : :
483 [ + + ]: 171 : if (VirtualTransactionIdIsValid(old_snapshots[i]))
484 : : {
485 : : /* If requested, publish who we're going to wait for. */
2590 alvherre@alvh.no-ip. 486 [ + - ]: 131 : if (progress)
487 : : {
793 heikki.linnakangas@i 488 : 131 : PGPROC *holder = ProcNumberGetProc(old_snapshots[i].procNumber);
489 : :
2393 alvherre@alvh.no-ip. 490 [ + - ]: 131 : if (holder)
491 : 131 : pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID,
492 : 131 : holder->pid);
493 : : }
2594 peter@eisentraut.org 494 : 131 : VirtualXactLock(old_snapshots[i], true);
495 : : }
496 : :
2590 alvherre@alvh.no-ip. 497 [ + - ]: 171 : if (progress)
498 : 171 : pgstat_progress_update_param(PROGRESS_WAITFOR_DONE, i + 1);
499 : : }
2594 peter@eisentraut.org 500 : 406 : }
501 : :
502 : :
503 : : /*
504 : : * DefineIndex
505 : : * Creates a new index.
506 : : *
507 : : * This function manages the current userid according to the needs of pg_dump.
508 : : * Recreating old-database catalog entries in new-database is fine, regardless
509 : : * of which users would have permission to recreate those entries now. That's
510 : : * just preservation of state. Running opaque expressions, like calling a
511 : : * function named in a catalog entry or evaluating a pg_node_tree in a catalog
512 : : * entry, as anyone other than the object owner, is not fine. To adhere to
513 : : * those principles and to remain fail-safe, use the table owner userid for
514 : : * most ACL checks. Use the original userid for ACL checks reached without
515 : : * traversing opaque expressions. (pg_dump can predict such ACL checks from
516 : : * catalogs.) Overall, this is a mess. Future DDL development should
517 : : * consider offering one DDL command for catalog setup and a separate DDL
518 : : * command for steps that run opaque expressions.
519 : : *
520 : : * 'pstate': ParseState struct (used only for error reports; pass NULL if
521 : : * not available)
522 : : * 'tableId': the OID of the table relation on which the index is to be
523 : : * created
524 : : * 'stmt': IndexStmt describing the properties of the new index.
525 : : * 'indexRelationId': normally InvalidOid, but during bootstrap can be
526 : : * nonzero to specify a preselected OID for the index.
527 : : * 'parentIndexId': the OID of the parent index; InvalidOid if not the child
528 : : * of a partitioned index.
529 : : * 'parentConstraintId': the OID of the parent constraint; InvalidOid if not
530 : : * the child of a constraint (only used when recursing)
531 : : * 'total_parts': total number of direct and indirect partitions of relation;
532 : : * pass -1 if not known or rel is not partitioned.
533 : : * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
534 : : * 'check_rights': check for CREATE rights in namespace and tablespace. (This
535 : : * should be true except when ALTER is deleting/recreating an index.)
536 : : * 'check_not_in_use': check for table not already in use in current session.
537 : : * This should be true unless caller is holding the table open, in which
538 : : * case the caller had better have checked it earlier.
539 : : * 'skip_build': make the catalog entries but don't create the index files
540 : : * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
541 : : *
542 : : * Returns the object address of the created index.
543 : : */
544 : : ObjectAddress
121 tgl@sss.pgh.pa.us 545 :GNC 19965 : DefineIndex(ParseState *pstate,
546 : : Oid tableId,
547 : : const IndexStmt *stmt,
548 : : Oid indexRelationId,
549 : : Oid parentIndexId,
550 : : Oid parentConstraintId,
551 : : int total_parts,
552 : : bool is_alter_table,
553 : : bool check_rights,
554 : : bool check_not_in_use,
555 : : bool skip_build,
556 : : bool quiet)
557 : : {
558 : : bool concurrent;
559 : : char *indexRelationName;
560 : : char *accessMethodName;
561 : : Oid *typeIds;
562 : : Oid *collationIds;
563 : : Oid *opclassIds;
564 : : Datum *opclassOptions;
565 : : Oid accessMethodId;
566 : : Oid namespaceId;
567 : : Oid tablespaceId;
2997 alvherre@alvh.no-ip. 568 :CBC 19965 : Oid createdConstraintId = InvalidOid;
569 : : List *indexColNames;
570 : : List *allIndexParams;
571 : : Relation rel;
572 : : HeapTuple tuple;
573 : : Form_pg_am accessMethodForm;
574 : : const IndexAmRoutine *amRoutine;
575 : : bool amcanorder;
576 : : bool amissummarizing;
577 : : amoptions_function amoptions;
578 : : bool exclusion;
579 : : bool partitioned;
580 : : bool safe_index;
581 : : Datum reloptions;
582 : : int16 *coloptions;
583 : : IndexInfo *indexInfo;
584 : : uint16 flags;
585 : : uint16 constr_flags;
586 : : int numberOfAttributes;
587 : : int numberOfKeyAttributes;
588 : : TransactionId limitXmin;
589 : : ObjectAddress address;
590 : : LockRelId heaprelid;
591 : : LOCKTAG heaplocktag;
592 : : LOCKMODE lockmode;
593 : : Snapshot snapshot;
594 : : Oid root_save_userid;
595 : : int root_save_sec_context;
596 : : int root_save_nestlevel;
597 : :
1457 noah@leadboat.com 598 : 19965 : root_save_nestlevel = NewGUCNestLevel();
599 : :
792 jdavis@postgresql.or 600 : 19965 : RestrictSearchPath();
601 : :
602 : : /*
603 : : * Some callers need us to run with an empty default_tablespace; this is a
604 : : * necessary hack to be able to reproduce catalog state accurately when
605 : : * recreating indexes after table-rewriting ALTER TABLE.
606 : : */
2567 alvherre@alvh.no-ip. 607 [ + + ]: 19965 : if (stmt->reset_default_tblspc)
608 : 306 : (void) set_config_option("default_tablespace", "",
609 : : PGC_USERSET, PGC_S_SESSION,
610 : : GUC_ACTION_SAVE, true, 0, false);
611 : :
612 : : /*
613 : : * Force non-concurrent build on temporary relations, even if CONCURRENTLY
614 : : * was requested. Other backends can't access a temporary relation, so
615 : : * there's no harm in grabbing a stronger lock, and a non-concurrent DROP
616 : : * is more efficient. Do this before any use of the concurrent option is
617 : : * done.
618 : : */
986 peter@eisentraut.org 619 [ + + + + ]: 19965 : if (stmt->concurrent && get_rel_persistence(tableId) != RELPERSISTENCE_TEMP)
2295 michael@paquier.xyz 620 : 116 : concurrent = true;
621 : : else
622 : 19849 : concurrent = false;
623 : :
624 : : /*
625 : : * Start progress report. If we're building a partition, this was already
626 : : * done.
627 : : */
2590 alvherre@alvh.no-ip. 628 [ + + ]: 19965 : if (!OidIsValid(parentIndexId))
629 : : {
986 peter@eisentraut.org 630 : 17875 : pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, tableId);
2527 631 [ + + ]: 17875 : pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
632 : : concurrent ?
633 : : PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY :
634 : : PROGRESS_CREATEIDX_COMMAND_CREATE);
635 : : }
636 : :
637 : : /*
638 : : * No index OID to report yet
639 : : */
2585 640 : 19965 : pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID,
641 : : InvalidOid);
642 : :
643 : : /*
644 : : * count key attributes in index
645 : : */
2950 teodor@sigaev.ru 646 : 19965 : numberOfKeyAttributes = list_length(stmt->indexParams);
647 : :
648 : : /*
649 : : * Calculate the new list of index columns including both key columns and
650 : : * INCLUDE columns. Later we can determine which of these are key
651 : : * columns, and which are just part of the INCLUDE list by checking the
652 : : * list position. A list item in a position less than ii_NumIndexKeyAttrs
653 : : * is part of the key columns, and anything equal to and over is part of
654 : : * the INCLUDE columns.
655 : : */
2458 tgl@sss.pgh.pa.us 656 : 19965 : allIndexParams = list_concat_copy(stmt->indexParams,
657 : 19965 : stmt->indexIncludingParams);
2945 teodor@sigaev.ru 658 : 19965 : numberOfAttributes = list_length(allIndexParams);
659 : :
1997 tgl@sss.pgh.pa.us 660 [ - + ]: 19965 : if (numberOfKeyAttributes <= 0)
3679 teodor@sigaev.ru 661 [ # # ]:UBC 0 : ereport(ERROR,
662 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
663 : : errmsg("must specify at least one column")));
9610 tgl@sss.pgh.pa.us 664 [ - + ]:CBC 19965 : if (numberOfAttributes > INDEX_MAX_KEYS)
8325 tgl@sss.pgh.pa.us 665 [ # # ]:UBC 0 : ereport(ERROR,
666 : : (errcode(ERRCODE_TOO_MANY_COLUMNS),
667 : : errmsg("cannot use more than %d columns in an index",
668 : : INDEX_MAX_KEYS)));
669 : :
670 : : /*
671 : : * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
672 : : * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
673 : : * (but not VACUUM).
674 : : *
675 : : * NB: Caller is responsible for making sure that tableId refers to the
676 : : * relation on which the index should be built; except in bootstrap mode,
677 : : * this will typically require the caller to have already locked the
678 : : * relation. To avoid lock upgrade hazards, that lock should be at least
679 : : * as strong as the one we take here.
680 : : *
681 : : * NB: If the lock strength here ever changes, code that is run by
682 : : * parallel workers under the control of certain particular ambuild
683 : : * functions will need to be updated, too.
684 : : */
2295 michael@paquier.xyz 685 [ + + ]:CBC 19965 : lockmode = concurrent ? ShareUpdateExclusiveLock : ShareLock;
986 peter@eisentraut.org 686 : 19965 : rel = table_open(tableId, lockmode);
687 : :
688 : : /*
689 : : * Switch to the table owner's userid, so that any index functions are run
690 : : * as that user. Also lock down security-restricted operations. We
691 : : * already arranged to make GUC variable changes local to this command.
692 : : */
1457 noah@leadboat.com 693 : 19965 : GetUserIdAndSecContext(&root_save_userid, &root_save_sec_context);
694 : 19965 : SetUserIdAndSecContext(rel->rd_rel->relowner,
695 : : root_save_sec_context | SECURITY_RESTRICTED_OPERATION);
696 : :
7193 tgl@sss.pgh.pa.us 697 : 19965 : namespaceId = RelationGetNamespace(rel);
698 : :
699 : : /*
700 : : * It has exclusion constraint behavior if it's an EXCLUDE constraint or a
701 : : * temporal PRIMARY KEY/UNIQUE constraint
702 : : */
595 peter@eisentraut.org 703 [ + + + + ]: 19965 : exclusion = stmt->excludeOpNames || stmt->iswithoutoverlaps;
704 : :
705 : : /* Ensure that it makes sense to index this kind of relation */
3123 alvherre@alvh.no-ip. 706 [ + + ]: 19965 : switch (rel->rd_rel->relkind)
707 : : {
708 : 19961 : case RELKIND_RELATION:
709 : : case RELKIND_MATVIEW:
710 : : case RELKIND_PARTITIONED_TABLE:
711 : : /* OK */
712 : 19961 : break;
713 : 4 : default:
5479 magnus@hagander.net 714 [ + - ]: 4 : ereport(ERROR,
715 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
716 : : errmsg("cannot create index on relation \"%s\"",
717 : : RelationGetRelationName(rel)),
718 : : errdetail_relkind_not_supported(rel->rd_rel->relkind)));
719 : : break;
720 : : }
721 : :
722 : : /*
723 : : * Establish behavior for partitioned tables, and verify sanity of
724 : : * parameters.
725 : : *
726 : : * We do not build an actual index in this case; we only create a few
727 : : * catalog entries. The actual indexes are built by recursing for each
728 : : * partition.
729 : : */
3028 alvherre@alvh.no-ip. 730 : 19961 : partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
731 [ + + ]: 19961 : if (partitioned)
732 : : {
733 : : /*
734 : : * Note: we check 'stmt->concurrent' rather than 'concurrent', so that
735 : : * the error is thrown also for temporary tables. Seems better to be
736 : : * consistent, even though we could do it on temporary table because
737 : : * we're not actually doing it concurrently.
738 : : */
739 [ + + ]: 1487 : if (stmt->concurrent)
740 [ + - ]: 4 : ereport(ERROR,
741 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
742 : : errmsg("cannot create index on partitioned table \"%s\" concurrently",
743 : : RelationGetRelationName(rel))));
744 : : }
745 : :
746 : : /*
747 : : * Don't try to CREATE INDEX on temp tables of other backends.
748 : : */
6244 tgl@sss.pgh.pa.us 749 [ + + - + ]: 19957 : if (RELATION_IS_OTHER_TEMP(rel))
7193 tgl@sss.pgh.pa.us 750 [ # # ]:UBC 0 : ereport(ERROR,
751 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
752 : : errmsg("cannot create indexes on temporary tables of other sessions")));
753 : :
754 : : /*
755 : : * Unless our caller vouches for having checked this already, insist that
756 : : * the table not be in use by our own session, either. Otherwise we might
757 : : * fail to make entries in the new index (for instance, if an INSERT or
758 : : * UPDATE is in progress and has already made its list of target indexes).
759 : : */
3257 tgl@sss.pgh.pa.us 760 [ + + ]:CBC 19957 : if (check_not_in_use)
761 : 9652 : CheckTableNotInUse(rel, "CREATE INDEX");
762 : :
763 : : /*
764 : : * Verify we (still) have CREATE rights in the rel's namespace.
765 : : * (Presumably we did when the rel was created, but maybe not anymore.)
766 : : * Skip check if caller doesn't want it. Also skip check if
767 : : * bootstrapping, since permissions machinery may not be working yet.
768 : : */
8035 769 [ + + + - ]: 19953 : if (check_rights && !IsBootstrapProcessingMode())
770 : : {
771 : : AclResult aclresult;
772 : :
1269 peter@eisentraut.org 773 : 10439 : aclresult = object_aclcheck(NamespaceRelationId, namespaceId, root_save_userid,
774 : : ACL_CREATE);
8774 tgl@sss.pgh.pa.us 775 [ - + ]: 10439 : if (aclresult != ACLCHECK_OK)
3076 peter_e@gmx.net 776 :UBC 0 : aclcheck_error(aclresult, OBJECT_SCHEMA,
8313 tgl@sss.pgh.pa.us 777 : 0 : get_namespace_name(namespaceId));
778 : : }
779 : :
780 : : /*
781 : : * Select tablespace to use. If not specified, use default tablespace
782 : : * (which may in turn default to database's default).
783 : : */
5041 tgl@sss.pgh.pa.us 784 [ + + ]:CBC 19953 : if (stmt->tableSpace)
785 : : {
786 : 160 : tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
2567 alvherre@alvh.no-ip. 787 [ + + + + ]: 160 : if (partitioned && tablespaceId == MyDatabaseTableSpace)
788 [ + - ]: 4 : ereport(ERROR,
789 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
790 : : errmsg("cannot specify default tablespace for partitioned relations")));
791 : : }
792 : : else
793 : : {
794 : 19793 : tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence,
795 : : partitioned);
796 : : /* note InvalidOid is OK in this case */
797 : : }
798 : :
799 : : /* Check tablespace permissions */
3369 noah@leadboat.com 800 [ + + + + ]: 19945 : if (check_rights &&
801 [ + - ]: 73 : OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
802 : : {
803 : : AclResult aclresult;
804 : :
1269 peter@eisentraut.org 805 : 73 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceId, root_save_userid,
806 : : ACL_CREATE);
7991 tgl@sss.pgh.pa.us 807 [ - + ]: 73 : if (aclresult != ACLCHECK_OK)
3076 peter_e@gmx.net 808 :UBC 0 : aclcheck_error(aclresult, OBJECT_TABLESPACE,
7851 tgl@sss.pgh.pa.us 809 : 0 : get_tablespace_name(tablespaceId));
810 : : }
811 : :
812 : : /*
813 : : * Force shared indexes into the pg_global tablespace. This is a bit of a
814 : : * hack but seems simpler than marking them in the BKI commands. On the
815 : : * other hand, if it's not shared, don't allow it to be placed there.
816 : : */
7851 tgl@sss.pgh.pa.us 817 [ + + ]:CBC 19945 : if (rel->rd_rel->relisshared)
818 : 1197 : tablespaceId = GLOBALTABLESPACE_OID;
5931 819 [ - + ]: 18748 : else if (tablespaceId == GLOBALTABLESPACE_OID)
5931 tgl@sss.pgh.pa.us 820 [ # # ]:UBC 0 : ereport(ERROR,
821 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
822 : : errmsg("only shared relations can be placed in pg_global tablespace")));
823 : :
824 : : /*
825 : : * Choose the index column names.
826 : : */
2945 teodor@sigaev.ru 827 :CBC 19945 : indexColNames = ChooseIndexColumnNames(allIndexParams);
828 : :
829 : : /*
830 : : * Select name for index if caller didn't specify
831 : : */
5041 tgl@sss.pgh.pa.us 832 : 19945 : indexRelationName = stmt->idxname;
8035 833 [ + + ]: 19945 : if (indexRelationName == NULL)
5977 834 : 8064 : indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
835 : : namespaceId,
836 : : indexColNames,
5041 837 : 8064 : stmt->excludeOpNames,
838 : 8064 : stmt->primary,
839 : 8064 : stmt->isconstraint);
840 : :
841 : : /*
842 : : * look up the access method, verify it can handle the requested features
843 : : */
844 : 19945 : accessMethodName = stmt->accessMethod;
5924 rhaas@postgresql.org 845 : 19945 : tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
9060 tgl@sss.pgh.pa.us 846 [ + + ]: 19945 : if (!HeapTupleIsValid(tuple))
847 : : {
848 : : /*
849 : : * Hack to provide more-or-less-transparent updating of old RTREE
850 : : * indexes to GiST: if RTREE is requested and not found, use GIST.
851 : : */
7484 852 [ + - ]: 4 : if (strcmp(accessMethodName, "rtree") == 0)
853 : : {
854 [ + - ]: 4 : ereport(NOTICE,
855 : : (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
856 : 4 : accessMethodName = "gist";
5924 rhaas@postgresql.org 857 : 4 : tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
858 : : }
859 : :
7484 tgl@sss.pgh.pa.us 860 [ - + ]: 4 : if (!HeapTupleIsValid(tuple))
7484 tgl@sss.pgh.pa.us 861 [ # # ]:UBC 0 : ereport(ERROR,
862 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
863 : : errmsg("access method \"%s\" does not exist",
864 : : accessMethodName)));
865 : : }
9060 tgl@sss.pgh.pa.us 866 :CBC 19945 : accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
2723 andres@anarazel.de 867 : 19945 : accessMethodId = accessMethodForm->oid;
3761 tgl@sss.pgh.pa.us 868 : 19945 : amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
869 : :
2590 alvherre@alvh.no-ip. 870 : 19945 : pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID,
871 : : accessMethodId);
872 : :
595 peter@eisentraut.org 873 [ + + + + : 19945 : if (stmt->unique && !stmt->iswithoutoverlaps && !amRoutine->amcanunique)
- + ]
8325 tgl@sss.pgh.pa.us 874 [ # # ]:UBC 0 : ereport(ERROR,
875 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
876 : : errmsg("access method \"%s\" does not support unique indexes",
877 : : accessMethodName)));
2848 tgl@sss.pgh.pa.us 878 [ + + + + ]:CBC 19945 : if (stmt->indexIncludingParams != NIL && !amRoutine->amcaninclude)
2950 teodor@sigaev.ru 879 [ + - ]: 12 : ereport(ERROR,
880 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
881 : : errmsg("access method \"%s\" does not support included columns",
882 : : accessMethodName)));
1997 tgl@sss.pgh.pa.us 883 [ + + - + ]: 19933 : if (numberOfKeyAttributes > 1 && !amRoutine->amcanmulticol)
8325 tgl@sss.pgh.pa.us 884 [ # # ]:UBC 0 : ereport(ERROR,
885 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
886 : : errmsg("access method \"%s\" does not support multicolumn indexes",
887 : : accessMethodName)));
595 peter@eisentraut.org 888 [ + + - + ]:CBC 19933 : if (exclusion && amRoutine->amgettuple == NULL)
5993 tgl@sss.pgh.pa.us 889 [ # # ]:UBC 0 : ereport(ERROR,
890 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
891 : : errmsg("access method \"%s\" does not support exclusion constraints",
892 : : accessMethodName)));
595 peter@eisentraut.org 893 [ + + - + ]:CBC 19933 : if (stmt->iswithoutoverlaps && strcmp(accessMethodName, "gist") != 0)
595 peter@eisentraut.org 894 [ # # ]:UBC 0 : ereport(ERROR,
895 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
896 : : errmsg("access method \"%s\" does not support WITHOUT OVERLAPS constraints",
897 : : accessMethodName)));
898 : :
3761 tgl@sss.pgh.pa.us 899 :CBC 19933 : amcanorder = amRoutine->amcanorder;
900 : 19933 : amoptions = amRoutine->amoptions;
1142 tomas.vondra@postgre 901 : 19933 : amissummarizing = amRoutine->amsummarizing;
902 : :
9060 tgl@sss.pgh.pa.us 903 : 19933 : ReleaseSysCache(tuple);
904 : :
905 : : /*
906 : : * Validate predicate, if given
907 : : */
5041 908 [ + + ]: 19933 : if (stmt->whereClause)
909 : 281 : CheckPredicate((Expr *) stmt->whereClause);
910 : :
911 : : /*
912 : : * Parse AM-specific options, convert to text array form, validate.
913 : : */
914 : 19933 : reloptions = transformRelOptions((Datum) 0, stmt->options,
915 : : NULL, NULL, false, false);
916 : :
7246 917 : 19929 : (void) index_reloptions(amoptions, reloptions, true);
918 : :
919 : : /*
920 : : * Prepare arguments for index_create, primarily an IndexInfo structure.
921 : : * Note that predicates must be in implicit-AND format. In a concurrent
922 : : * build, mark it not-ready-for-inserts.
923 : : */
2472 michael@paquier.xyz 924 : 19887 : indexInfo = makeIndexInfo(numberOfAttributes,
925 : : numberOfKeyAttributes,
926 : : accessMethodId,
927 : : NIL, /* expressions, NIL for now */
928 : 19887 : make_ands_implicit((Expr *) stmt->whereClause),
929 : 19887 : stmt->unique,
1552 peter@eisentraut.org 930 : 19887 : stmt->nulls_not_distinct,
2295 michael@paquier.xyz 931 : 19887 : !concurrent,
932 : : concurrent,
933 : : amissummarizing,
595 peter@eisentraut.org 934 : 19887 : stmt->iswithoutoverlaps);
935 : :
986 936 : 19887 : typeIds = palloc_array(Oid, numberOfAttributes);
937 : 19887 : collationIds = palloc_array(Oid, numberOfAttributes);
938 : 19887 : opclassIds = palloc_array(Oid, numberOfAttributes);
945 939 : 19887 : opclassOptions = palloc_array(Datum, numberOfAttributes);
1331 940 : 19887 : coloptions = palloc_array(int16, numberOfAttributes);
121 tgl@sss.pgh.pa.us 941 :GNC 19887 : ComputeIndexAttrs(pstate,
942 : : indexInfo,
943 : : typeIds, collationIds, opclassIds, opclassOptions,
944 : : coloptions, allIndexParams,
986 peter@eisentraut.org 945 :CBC 19887 : stmt->excludeOpNames, tableId,
946 : : accessMethodName, accessMethodId,
595 947 : 19887 : amcanorder, stmt->isconstraint, stmt->iswithoutoverlaps,
948 : : root_save_userid, root_save_sec_context,
949 : : &root_save_nestlevel);
950 : :
951 : : /*
952 : : * Extra checks when creating a PRIMARY KEY index.
953 : : */
5041 tgl@sss.pgh.pa.us 954 [ + + ]: 19670 : if (stmt->primary)
2768 alvherre@alvh.no-ip. 955 : 5932 : index_check_primary_key(rel, indexInfo, is_alter_table, stmt);
956 : :
957 : : /*
958 : : * If this table is partitioned and we're creating a unique index, primary
959 : : * key, or exclusion constraint, make sure that the partition key is a
960 : : * subset of the index's columns. Otherwise it would be possible to
961 : : * violate uniqueness by putting values that ought to be unique in
962 : : * different partitions.
963 : : *
964 : : * We could lift this limitation if we had global indexes, but those have
965 : : * their own problems, so this is a useful feature combination.
966 : : */
595 peter@eisentraut.org 967 [ + + + + : 19646 : if (partitioned && (stmt->unique || exclusion))
+ + ]
968 : : {
2323 tgl@sss.pgh.pa.us 969 : 860 : PartitionKey key = RelationGetPartitionKey(rel);
970 : : const char *constraint_type;
971 : : int i;
972 : :
2225 973 [ + + ]: 860 : if (stmt->primary)
974 : 628 : constraint_type = "PRIMARY KEY";
975 [ + + ]: 232 : else if (stmt->unique)
976 : 166 : constraint_type = "UNIQUE";
1028 peter@eisentraut.org 977 [ + - ]: 66 : else if (stmt->excludeOpNames)
2225 tgl@sss.pgh.pa.us 978 : 66 : constraint_type = "EXCLUDE";
979 : : else
980 : : {
2225 tgl@sss.pgh.pa.us 981 [ # # ]:UBC 0 : elog(ERROR, "unknown constraint type");
982 : : constraint_type = NULL; /* keep compiler quiet */
983 : : }
984 : :
985 : : /*
986 : : * Verify that all the columns in the partition key appear in the
987 : : * unique key definition, with the same notion of equality.
988 : : */
2997 alvherre@alvh.no-ip. 989 [ + + ]:CBC 1731 : for (i = 0; i < key->partnatts; i++)
990 : : {
2931 tgl@sss.pgh.pa.us 991 : 940 : bool found = false;
992 : : int eq_strategy;
993 : : Oid ptkey_eqop;
994 : : int j;
995 : :
996 : : /*
997 : : * Identify the equality operator associated with this partkey
998 : : * column. For list and range partitioning, partkeys use btree
999 : : * operator classes; hash partitioning uses hash operator classes.
1000 : : * (Keep this in sync with ComputePartitionAttrs!)
1001 : : */
2225 1002 [ + + ]: 940 : if (key->strategy == PARTITION_STRATEGY_HASH)
1003 : 42 : eq_strategy = HTEqualStrategyNumber;
1004 : : else
1005 : 898 : eq_strategy = BTEqualStrategyNumber;
1006 : :
1007 : 940 : ptkey_eqop = get_opfamily_member(key->partopfamily[i],
1008 : 940 : key->partopcintype[i],
1009 : 940 : key->partopcintype[i],
1010 : : eq_strategy);
1011 [ - + ]: 940 : if (!OidIsValid(ptkey_eqop))
2225 tgl@sss.pgh.pa.us 1012 [ # # ]:UBC 0 : elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
1013 : : eq_strategy, key->partopcintype[i], key->partopcintype[i],
1014 : : key->partopfamily[i]);
1015 : :
1016 : : /*
1017 : : * It may be possible to support UNIQUE constraints when partition
1018 : : * keys are expressions, but is it worth it? Give up for now.
1019 : : */
2997 alvherre@alvh.no-ip. 1020 [ + + ]:CBC 940 : if (key->partattrs[i] == 0)
1021 [ + - ]: 8 : ereport(ERROR,
1022 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1023 : : errmsg("unsupported %s constraint with partition key definition",
1024 : : constraint_type),
1025 : : errdetail("%s constraints cannot be used when partition keys include expressions.",
1026 : : constraint_type)));
1027 : :
1028 : : /* Search the index column(s) for a match */
2668 1029 [ + + ]: 1065 : for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
1030 : : {
2945 teodor@sigaev.ru 1031 [ + + ]: 1013 : if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
1032 : : {
1033 : : /*
1034 : : * Matched the column, now what about the collation and
1035 : : * equality op?
1036 : : */
1037 : : Oid idx_opfamily;
1038 : : Oid idx_opcintype;
1039 : :
886 peter@eisentraut.org 1040 [ - + ]: 880 : if (key->partcollation[i] != collationIds[j])
886 peter@eisentraut.org 1041 :UBC 0 : continue;
1042 : :
986 peter@eisentraut.org 1043 [ + - ]:CBC 880 : if (get_opclass_opfamily_and_input_type(opclassIds[j],
1044 : : &idx_opfamily,
1045 : : &idx_opcintype))
1046 : : {
1028 1047 : 880 : Oid idx_eqop = InvalidOid;
1048 : :
595 1049 [ + + + + ]: 880 : if (stmt->unique && !stmt->iswithoutoverlaps)
413 1050 : 778 : idx_eqop = get_opfamily_member_for_cmptype(idx_opfamily,
1051 : : idx_opcintype,
1052 : : idx_opcintype,
1053 : : COMPARE_EQ);
595 1054 [ + - ]: 102 : else if (exclusion)
1028 1055 : 102 : idx_eqop = indexInfo->ii_ExclusionOps[j];
1056 : :
413 1057 [ - + ]: 880 : if (!idx_eqop)
413 peter@eisentraut.org 1058 [ # # ]:UBC 0 : ereport(ERROR,
1059 : : errcode(ERRCODE_UNDEFINED_OBJECT),
1060 : : errmsg("could not identify an equality operator for type %s", format_type_be(idx_opcintype)),
1061 : : errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
1062 : : get_opfamily_name(idx_opfamily, false), get_am_name(get_opfamily_method(idx_opfamily))));
1063 : :
2225 tgl@sss.pgh.pa.us 1064 [ + + ]:CBC 880 : if (ptkey_eqop == idx_eqop)
1065 : : {
1066 : 871 : found = true;
1067 : 871 : break;
1068 : : }
595 peter@eisentraut.org 1069 [ + - ]: 9 : else if (exclusion)
1070 : : {
1071 : : /*
1072 : : * We found a match, but it's not an equality
1073 : : * operator. Instead of failing below with an
1074 : : * error message about a missing column, fail now
1075 : : * and explain that the operator is wrong.
1076 : : */
1028 1077 : 9 : Form_pg_attribute att = TupleDescAttr(RelationGetDescr(rel), key->partattrs[i] - 1);
1078 : :
1079 [ + - ]: 9 : ereport(ERROR,
1080 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1081 : : errmsg("cannot match partition key to index on column \"%s\" using non-equal operator \"%s\"",
1082 : : NameStr(att->attname),
1083 : : get_opname(indexInfo->ii_ExclusionOps[j]))));
1084 : : }
1085 : : }
1086 : : }
1087 : : }
1088 : :
2997 alvherre@alvh.no-ip. 1089 [ + + ]: 923 : if (!found)
1090 : : {
1091 : : Form_pg_attribute att;
1092 : :
2225 tgl@sss.pgh.pa.us 1093 : 52 : att = TupleDescAttr(RelationGetDescr(rel),
1094 : 52 : key->partattrs[i] - 1);
2997 alvherre@alvh.no-ip. 1095 [ + - ]: 52 : ereport(ERROR,
1096 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1097 : : /* translator: %s is UNIQUE, PRIMARY KEY, etc */
1098 : : errmsg("%s constraint on partitioned table must include all partitioning columns",
1099 : : constraint_type),
1100 : : /* translator: first %s is UNIQUE, PRIMARY KEY, etc */
1101 : : errdetail("%s constraint on table \"%s\" lacks column \"%s\" which is part of the partition key.",
1102 : : constraint_type, RelationGetRelationName(rel),
1103 : : NameStr(att->attname))));
1104 : : }
1105 : : }
1106 : : }
1107 : :
1108 : :
1109 : : /*
1110 : : * We disallow indexes on system columns. They would not necessarily get
1111 : : * updated correctly, and they don't seem useful anyway.
1112 : : *
1113 : : * Also disallow virtual generated columns in indexes (use expression
1114 : : * index instead).
1115 : : */
1350 drowley@postgresql.o 1116 [ + + ]: 47085 : for (int i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
1117 : : {
2945 teodor@sigaev.ru 1118 : 27524 : AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i];
1119 : :
2723 andres@anarazel.de 1120 [ + + ]: 27524 : if (attno < 0)
3671 tgl@sss.pgh.pa.us 1121 [ + - ]: 4 : ereport(ERROR,
1122 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1123 : : errmsg("index creation on system columns is not supported")));
1124 : :
1125 : :
42 rhaas@postgresql.org 1126 [ + + ]: 27520 : if (attno > 0 &&
1127 [ + + ]: 26805 : TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
452 peter@eisentraut.org 1128 [ + - + + : 12 : ereport(ERROR,
+ - ]
1129 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1130 : : stmt->primary ?
1131 : : errmsg("primary keys on virtual generated columns are not supported") :
1132 : : stmt->isconstraint ?
1133 : : errmsg("unique constraints on virtual generated columns are not supported") :
1134 : : errmsg("indexes on virtual generated columns are not supported"));
1135 : : }
1136 : :
1137 : : /*
1138 : : * Also check for system and generated columns used in expressions or
1139 : : * predicates.
1140 : : */
3671 tgl@sss.pgh.pa.us 1141 [ + + + + ]: 19561 : if (indexInfo->ii_Expressions || indexInfo->ii_Predicate)
1142 : : {
1143 : 914 : Bitmapset *indexattrs = NULL;
1144 : : int j;
1145 : :
1146 : 914 : pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
1147 : 914 : pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
1148 : :
1350 drowley@postgresql.o 1149 [ + + ]: 6390 : for (int i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
1150 : : {
2723 andres@anarazel.de 1151 [ + + ]: 5484 : if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
1152 : : indexattrs))
3671 tgl@sss.pgh.pa.us 1153 [ + - ]: 8 : ereport(ERROR,
1154 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1155 : : errmsg("index creation on system columns is not supported")));
1156 : : }
1157 : :
1158 : : /*
1159 : : * XXX Virtual generated columns in index expressions or predicates
1160 : : * could be supported, but it needs support in
1161 : : * RelationGetIndexExpressions() and RelationGetIndexPredicate().
1162 : : */
452 peter@eisentraut.org 1163 : 906 : j = -1;
1164 [ + + ]: 1980 : while ((j = bms_next_member(indexattrs, j)) >= 0)
1165 : : {
1166 : 1074 : AttrNumber attno = j + FirstLowInvalidHeapAttributeNumber;
1167 : :
42 rhaas@postgresql.org 1168 [ + + ]: 1074 : if (attno > 0 &&
1169 [ - + ]: 1070 : TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
452 peter@eisentraut.org 1170 [ # # # # ]:UBC 0 : ereport(ERROR,
1171 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1172 : : stmt->isconstraint ?
1173 : : errmsg("unique constraints on virtual generated columns are not supported") :
1174 : : errmsg("indexes on virtual generated columns are not supported")));
1175 : : }
1176 : : }
1177 : :
1178 : : /* Is index safe for others to ignore? See set_indexsafe_procflags() */
1987 alvherre@alvh.no-ip. 1179 [ + + ]:CBC 38411 : safe_index = indexInfo->ii_Expressions == NIL &&
1180 [ + + ]: 18858 : indexInfo->ii_Predicate == NIL;
1181 : :
1182 : : /*
1183 : : * Report index creation if appropriate (delay this till after most of the
1184 : : * error checks)
1185 : : */
5041 tgl@sss.pgh.pa.us 1186 [ + + + + ]: 19553 : if (stmt->isconstraint && !quiet)
1187 : : {
1188 : : const char *constraint_type;
1189 : :
1190 [ + + ]: 6554 : if (stmt->primary)
5993 1191 : 5796 : constraint_type = "PRIMARY KEY";
5041 1192 [ + + ]: 758 : else if (stmt->unique)
5993 1193 : 644 : constraint_type = "UNIQUE";
1028 peter@eisentraut.org 1194 [ + - ]: 114 : else if (stmt->excludeOpNames)
5993 tgl@sss.pgh.pa.us 1195 : 114 : constraint_type = "EXCLUDE";
1196 : : else
1197 : : {
5993 tgl@sss.pgh.pa.us 1198 [ # # ]:UBC 0 : elog(ERROR, "unknown constraint type");
1199 : : constraint_type = NULL; /* keep compiler quiet */
1200 : : }
1201 : :
5053 rhaas@postgresql.org 1202 [ + + - + ]:CBC 6554 : ereport(DEBUG1,
1203 : : (errmsg_internal("%s %s will create implicit index \"%s\" for table \"%s\"",
1204 : : is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
1205 : : constraint_type,
1206 : : indexRelationName, RelationGetRelationName(rel))));
1207 : : }
1208 : :
1209 : : /*
1210 : : * A valid stmt->oldNumber implies that we already have a built form of
1211 : : * the index. The caller should also decline any index build.
1212 : : */
1399 1213 [ + + + - : 19553 : Assert(!RelFileNumberIsValid(stmt->oldNumber) || (skip_build && !concurrent));
- + ]
1214 : :
1215 : : /*
1216 : : * Make the catalog entries for the index, including constraints. This
1217 : : * step also actually builds the index, except if caller requested not to
1218 : : * or in concurrent mode, in which case it'll be done later, or doing a
1219 : : * partitioned index (because those don't have storage).
1220 : : */
3094 alvherre@alvh.no-ip. 1221 : 19553 : flags = constr_flags = 0;
1222 [ + + ]: 19553 : if (stmt->isconstraint)
1223 : 6714 : flags |= INDEX_CREATE_ADD_CONSTRAINT;
2295 michael@paquier.xyz 1224 [ + + + + : 19553 : if (skip_build || concurrent || partitioned)
+ + ]
3094 alvherre@alvh.no-ip. 1225 : 9502 : flags |= INDEX_CREATE_SKIP_BUILD;
1226 [ + + ]: 19553 : if (stmt->if_not_exists)
1227 : 12 : flags |= INDEX_CREATE_IF_NOT_EXISTS;
2295 michael@paquier.xyz 1228 [ + + ]: 19553 : if (concurrent)
3094 alvherre@alvh.no-ip. 1229 : 112 : flags |= INDEX_CREATE_CONCURRENT;
3028 1230 [ + + ]: 19553 : if (partitioned)
1231 : 1406 : flags |= INDEX_CREATE_PARTITIONED;
3094 1232 [ + + ]: 19553 : if (stmt->primary)
1233 : 5880 : flags |= INDEX_CREATE_IS_PRIMARY;
1234 : :
1235 : : /*
1236 : : * If the table is partitioned, and recursion was declined but partitions
1237 : : * exist, mark the index as invalid.
1238 : : */
3028 1239 [ + + + + : 19553 : if (partitioned && stmt->relation && !stmt->relation->inh)
+ + ]
1240 : : {
1839 1241 : 175 : PartitionDesc pd = RelationGetPartitionDesc(rel, true);
1242 : :
2708 1243 [ + + ]: 175 : if (pd->nparts != 0)
1244 : 158 : flags |= INDEX_CREATE_INVALID;
1245 : : }
1246 : :
3094 1247 [ + + ]: 19553 : if (stmt->deferrable)
1248 : 90 : constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE;
1249 [ + + ]: 19553 : if (stmt->initdeferred)
1250 : 24 : constr_flags |= INDEX_CONSTR_CREATE_INIT_DEFERRED;
595 peter@eisentraut.org 1251 [ + + ]: 19553 : if (stmt->iswithoutoverlaps)
1252 : 587 : constr_flags |= INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS;
1253 : :
1254 : : indexRelationId =
3028 alvherre@alvh.no-ip. 1255 : 19553 : index_create(rel, indexRelationName, indexRelationId, parentIndexId,
1256 : : parentConstraintId,
1399 rhaas@postgresql.org 1257 :GIC 19553 : stmt->oldNumber, indexInfo, indexColNames,
1258 : : accessMethodId, tablespaceId,
1259 : : collationIds, opclassIds, opclassOptions,
1260 : : coloptions, NULL, reloptions,
1261 : : flags, constr_flags,
2997 alvherre@alvh.no-ip. 1262 :CBC 19553 : allowSystemTableMods, !check_rights,
1263 : 19553 : &createdConstraintId);
1264 : :
4081 1265 : 19403 : ObjectAddressSet(address, RelationRelationId, indexRelationId);
1266 : :
4198 fujii@postgresql.org 1267 [ + + ]: 19403 : if (!OidIsValid(indexRelationId))
1268 : : {
1269 : : /*
1270 : : * Roll back any GUC changes executed by index functions. Also revert
1271 : : * to original default_tablespace if we changed it above.
1272 : : */
1457 noah@leadboat.com 1273 : 12 : AtEOXact_GUC(false, root_save_nestlevel);
1274 : :
1275 : : /* Restore userid and security context */
1276 : 12 : SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1277 : :
2661 andres@anarazel.de 1278 : 12 : table_close(rel, NoLock);
1279 : :
1280 : : /* If this is the top-level index, we're done */
2590 alvherre@alvh.no-ip. 1281 [ + - ]: 12 : if (!OidIsValid(parentIndexId))
1282 : 12 : pgstat_progress_end_command();
1283 : :
4081 1284 : 12 : return address;
1285 : : }
1286 : :
1287 : : /*
1288 : : * Roll back any GUC changes executed by index functions, and keep
1289 : : * subsequent changes local to this command. This is essential if some
1290 : : * index function changed a behavior-affecting GUC, e.g. search_path.
1291 : : */
1457 noah@leadboat.com 1292 : 19391 : AtEOXact_GUC(false, root_save_nestlevel);
1293 : 19391 : root_save_nestlevel = NewGUCNestLevel();
659 jdavis@postgresql.or 1294 : 19391 : RestrictSearchPath();
1295 : :
1296 : : /* Add any requested comment */
5041 tgl@sss.pgh.pa.us 1297 [ + + ]: 19391 : if (stmt->idxcomment != NULL)
1298 : 52 : CreateComments(indexRelationId, RelationRelationId, 0,
1299 : 52 : stmt->idxcomment);
1300 : :
3028 alvherre@alvh.no-ip. 1301 [ + + ]: 19391 : if (partitioned)
1302 : : {
1303 : : PartitionDesc partdesc;
1304 : :
1305 : : /*
1306 : : * Unless caller specified to skip this step (via ONLY), process each
1307 : : * partition to make sure they all contain a corresponding index.
1308 : : *
1309 : : * If we're called internally (no stmt->relation), recurse always.
1310 : : */
1839 1311 : 1406 : partdesc = RelationGetPartitionDesc(rel, true);
1981 1312 [ + + + + : 1406 : if ((!stmt->relation || stmt->relation->inh) && partdesc->nparts > 0)
+ + ]
1313 : : {
3028 1314 : 411 : int nparts = partdesc->nparts;
1331 peter@eisentraut.org 1315 : 411 : Oid *part_oids = palloc_array(Oid, nparts);
3028 alvherre@alvh.no-ip. 1316 : 411 : bool invalidate_parent = false;
1317 : : Relation parentIndex;
1318 : : TupleDesc parentDesc;
1319 : :
1320 : : /*
1321 : : * Report the total number of partitions at the start of the
1322 : : * command; don't update it when being called recursively.
1323 : : */
1137 tgl@sss.pgh.pa.us 1324 [ + + ]: 411 : if (!OidIsValid(parentIndexId))
1325 : : {
1326 : : /*
1327 : : * When called by ProcessUtilitySlow, the number of partitions
1328 : : * is passed in as an optimization; but other callers pass -1
1329 : : * since they don't have the value handy. This should count
1330 : : * partitions the same way, ie one less than the number of
1331 : : * relations find_all_inheritors reports.
1332 : : *
1333 : : * We assume we needn't ask find_all_inheritors to take locks,
1334 : : * because that should have happened already for all callers.
1335 : : * Even if it did not, this is safe as long as we don't try to
1336 : : * touch the partitions here; the worst consequence would be a
1337 : : * bogus progress-reporting total.
1338 : : */
1339 [ + + ]: 337 : if (total_parts < 0)
1340 : : {
986 peter@eisentraut.org 1341 : 84 : List *children = find_all_inheritors(tableId, NoLock, NULL);
1342 : :
1137 tgl@sss.pgh.pa.us 1343 : 84 : total_parts = list_length(children) - 1;
1344 : 84 : list_free(children);
1345 : : }
1346 : :
1347 : 337 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_TOTAL,
1348 : : total_parts);
1349 : : }
1350 : :
1351 : : /* Make a local copy of partdesc->oids[], just for safety */
3028 alvherre@alvh.no-ip. 1352 : 411 : memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts);
1353 : :
1354 : : /*
1355 : : * We'll need an IndexInfo describing the parent index. The one
1356 : : * built above is almost good enough, but not quite, because (for
1357 : : * example) its predicate expression if any hasn't been through
1358 : : * expression preprocessing. The most reliable way to get an
1359 : : * IndexInfo that will match those for child indexes is to build
1360 : : * it the same way, using BuildIndexInfo().
1361 : : */
1356 tgl@sss.pgh.pa.us 1362 : 411 : parentIndex = index_open(indexRelationId, lockmode);
1363 : 411 : indexInfo = BuildIndexInfo(parentIndex);
1364 : :
2504 alvherre@alvh.no-ip. 1365 : 411 : parentDesc = RelationGetDescr(rel);
1366 : :
1367 : : /*
1368 : : * For each partition, scan all existing indexes; if one matches
1369 : : * our index definition and is not already attached to some other
1370 : : * parent index, attach it to the one we just created.
1371 : : *
1372 : : * If none matches, build a new index by calling ourselves
1373 : : * recursively with the same options (except for the index name).
1374 : : */
1350 drowley@postgresql.o 1375 [ + + ]: 1132 : for (int i = 0; i < nparts; i++)
1376 : : {
2931 tgl@sss.pgh.pa.us 1377 : 737 : Oid childRelid = part_oids[i];
1378 : : Relation childrel;
1379 : : Oid child_save_userid;
1380 : : int child_save_sec_context;
1381 : : int child_save_nestlevel;
1382 : : List *childidxs;
1383 : : ListCell *cell;
1384 : : AttrMap *attmap;
1385 : 737 : bool found = false;
1386 : :
2661 andres@anarazel.de 1387 : 737 : childrel = table_open(childRelid, lockmode);
1388 : :
1457 noah@leadboat.com 1389 : 737 : GetUserIdAndSecContext(&child_save_userid,
1390 : : &child_save_sec_context);
1391 : 737 : SetUserIdAndSecContext(childrel->rd_rel->relowner,
1392 : : child_save_sec_context | SECURITY_RESTRICTED_OPERATION);
1393 : 737 : child_save_nestlevel = NewGUCNestLevel();
792 jdavis@postgresql.or 1394 : 737 : RestrictSearchPath();
1395 : :
1396 : : /*
1397 : : * Don't try to create indexes on foreign tables, though. Skip
1398 : : * those if a regular index, or fail if trying to create a
1399 : : * constraint index.
1400 : : */
2505 alvherre@alvh.no-ip. 1401 [ + + ]: 737 : if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1402 : : {
1403 [ + + - + ]: 12 : if (stmt->unique || stmt->primary)
1404 [ + - ]: 8 : ereport(ERROR,
1405 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1406 : : errmsg("cannot create unique index on partitioned table \"%s\"",
1407 : : RelationGetRelationName(rel)),
1408 : : errdetail("Table \"%s\" contains partitions that are foreign tables.",
1409 : : RelationGetRelationName(rel))));
1410 : :
1457 noah@leadboat.com 1411 : 4 : AtEOXact_GUC(false, child_save_nestlevel);
1412 : 4 : SetUserIdAndSecContext(child_save_userid,
1413 : : child_save_sec_context);
2505 alvherre@alvh.no-ip. 1414 : 4 : table_close(childrel, lockmode);
1415 : 4 : continue;
1416 : : }
1417 : :
3028 1418 : 725 : childidxs = RelationGetIndexList(childrel);
1419 : : attmap =
2330 michael@paquier.xyz 1420 : 725 : build_attrmap_by_name(RelationGetDescr(childrel),
1421 : : parentDesc,
1422 : : false);
1423 : :
3028 alvherre@alvh.no-ip. 1424 [ + + + + : 960 : foreach(cell, childidxs)
+ + ]
1425 : : {
1426 : 287 : Oid cldidxid = lfirst_oid(cell);
1427 : : Relation cldidx;
1428 : : IndexInfo *cldIdxInfo;
1429 : :
1430 : : /* this index is already partition of another one */
1431 [ + + ]: 287 : if (has_superclass(cldidxid))
1432 : 219 : continue;
1433 : :
1434 : 68 : cldidx = index_open(cldidxid, lockmode);
1435 : 68 : cldIdxInfo = BuildIndexInfo(cldidx);
1436 [ + + ]: 68 : if (CompareIndexInfo(cldIdxInfo, indexInfo,
1437 : 68 : cldidx->rd_indcollation,
1356 tgl@sss.pgh.pa.us 1438 : 68 : parentIndex->rd_indcollation,
3028 alvherre@alvh.no-ip. 1439 : 68 : cldidx->rd_opfamily,
1356 tgl@sss.pgh.pa.us 1440 : 68 : parentIndex->rd_opfamily,
1441 : : attmap))
1442 : : {
2931 1443 : 52 : Oid cldConstrOid = InvalidOid;
1444 : :
1445 : : /*
1446 : : * Found a match.
1447 : : *
1448 : : * If this index is being created in the parent
1449 : : * because of a constraint, then the child needs to
1450 : : * have a constraint also, so look for one. If there
1451 : : * is no such constraint, this index is no good, so
1452 : : * keep looking.
1453 : : */
2997 alvherre@alvh.no-ip. 1454 [ + + ]: 52 : if (createdConstraintId != InvalidOid)
1455 : : {
1456 : : cldConstrOid =
1457 : 8 : get_relation_idx_constraint_oid(childRelid,
1458 : : cldidxid);
1459 [ - + ]: 8 : if (cldConstrOid == InvalidOid)
1460 : : {
2997 alvherre@alvh.no-ip. 1461 :UBC 0 : index_close(cldidx, lockmode);
1462 : 0 : continue;
1463 : : }
1464 : : }
1465 : :
1466 : : /* Attach index to parent and we're done. */
3028 alvherre@alvh.no-ip. 1467 :CBC 52 : IndexSetParentIndex(cldidx, indexRelationId);
2997 1468 [ + + ]: 52 : if (createdConstraintId != InvalidOid)
1469 : 8 : ConstraintSetParentConstraint(cldConstrOid,
1470 : : createdConstraintId,
1471 : : childRelid);
1472 : :
2686 peter_e@gmx.net 1473 [ + + ]: 52 : if (!cldidx->rd_index->indisvalid)
3028 alvherre@alvh.no-ip. 1474 : 12 : invalidate_parent = true;
1475 : :
1476 : 52 : found = true;
1477 : :
1478 : : /*
1479 : : * Report this partition as processed. Note that if
1480 : : * the partition has children itself, we'd ideally
1481 : : * count the children and update the progress report
1482 : : * for all of them; but that seems unduly expensive.
1483 : : * Instead, the progress report will act like all such
1484 : : * indirect children were processed in zero time at
1485 : : * the end of the command.
1486 : : */
1137 tgl@sss.pgh.pa.us 1487 : 52 : pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1488 : :
1489 : : /* keep lock till commit */
3028 alvherre@alvh.no-ip. 1490 : 52 : index_close(cldidx, NoLock);
1491 : 52 : break;
1492 : : }
1493 : :
1494 : 16 : index_close(cldidx, lockmode);
1495 : : }
1496 : :
1497 : 725 : list_free(childidxs);
1457 noah@leadboat.com 1498 : 725 : AtEOXact_GUC(false, child_save_nestlevel);
1499 : 725 : SetUserIdAndSecContext(child_save_userid,
1500 : : child_save_sec_context);
2661 andres@anarazel.de 1501 : 725 : table_close(childrel, NoLock);
1502 : :
1503 : : /*
1504 : : * If no matching index was found, create our own.
1505 : : */
3028 alvherre@alvh.no-ip. 1506 [ + + ]: 725 : if (!found)
1507 : : {
1508 : : IndexStmt *childStmt;
1509 : : ObjectAddress childAddr;
1510 : :
1511 : : /*
1512 : : * Build an IndexStmt describing the desired child index
1513 : : * in the same way that we do during ATTACH PARTITION.
1514 : : * Notably, we rely on generateClonedIndexStmt to produce
1515 : : * a search-path-independent representation, which the
1516 : : * original IndexStmt might not be.
1517 : : */
577 tgl@sss.pgh.pa.us 1518 : 673 : childStmt = generateClonedIndexStmt(NULL,
1519 : : parentIndex,
1520 : : attmap,
1521 : : NULL);
1522 : :
1523 : : /*
1524 : : * Recurse as the starting user ID. Callee will use that
1525 : : * for permission checks, then switch again.
1526 : : */
1457 noah@leadboat.com 1527 [ - + ]: 673 : Assert(GetUserId() == child_save_userid);
1528 : 673 : SetUserIdAndSecContext(root_save_userid,
1529 : : root_save_sec_context);
1530 : : childAddr =
121 tgl@sss.pgh.pa.us 1531 :GNC 673 : DefineIndex(NULL, /* original pstate not applicable */
1532 : : childRelid, childStmt,
1533 : : InvalidOid, /* no predefined OID */
1534 : : indexRelationId, /* this is our child */
1535 : : createdConstraintId,
1536 : : -1,
1537 : : is_alter_table, check_rights,
1538 : : check_not_in_use,
1539 : : skip_build, quiet);
1457 noah@leadboat.com 1540 :CBC 665 : SetUserIdAndSecContext(child_save_userid,
1541 : : child_save_sec_context);
1542 : :
1543 : : /*
1544 : : * Check if the index just created is valid or not, as it
1545 : : * could be possible that it has been switched as invalid
1546 : : * when recursing across multiple partition levels.
1547 : : */
1040 michael@paquier.xyz 1548 [ + + ]: 665 : if (!get_index_isvalid(childAddr.objectId))
1549 : 4 : invalidate_parent = true;
1550 : : }
1551 : :
2330 1552 : 717 : free_attrmap(attmap);
1553 : : }
1554 : :
1356 tgl@sss.pgh.pa.us 1555 : 395 : index_close(parentIndex, lockmode);
1556 : :
1557 : : /*
1558 : : * The pg_index row we inserted for this index was marked
1559 : : * indisvalid=true. But if we attached an existing index that is
1560 : : * invalid, this is incorrect, so update our row to invalid too.
1561 : : */
3028 alvherre@alvh.no-ip. 1562 [ + + ]: 395 : if (invalidate_parent)
1563 : : {
2661 andres@anarazel.de 1564 : 16 : Relation pg_index = table_open(IndexRelationId, RowExclusiveLock);
1565 : : HeapTuple tup,
1566 : : newtup;
1567 : :
3028 alvherre@alvh.no-ip. 1568 : 16 : tup = SearchSysCache1(INDEXRELID,
1569 : : ObjectIdGetDatum(indexRelationId));
2557 tgl@sss.pgh.pa.us 1570 [ - + ]: 16 : if (!HeapTupleIsValid(tup))
3028 alvherre@alvh.no-ip. 1571 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u",
1572 : : indexRelationId);
3028 alvherre@alvh.no-ip. 1573 :CBC 16 : newtup = heap_copytuple(tup);
1574 : 16 : ((Form_pg_index) GETSTRUCT(newtup))->indisvalid = false;
1575 : 16 : CatalogTupleUpdate(pg_index, &tup->t_self, newtup);
1576 : 16 : ReleaseSysCache(tup);
2661 andres@anarazel.de 1577 : 16 : table_close(pg_index, RowExclusiveLock);
3028 alvherre@alvh.no-ip. 1578 : 16 : heap_freetuple(newtup);
1579 : :
1580 : : /*
1581 : : * CCI here to make this update visible, in case this recurses
1582 : : * across multiple partition levels.
1583 : : */
1040 michael@paquier.xyz 1584 : 16 : CommandCounterIncrement();
1585 : : }
1586 : : }
1587 : :
1588 : : /*
1589 : : * Indexes on partitioned tables are not themselves built, so we're
1590 : : * done here.
1591 : : */
1457 noah@leadboat.com 1592 : 1390 : AtEOXact_GUC(false, root_save_nestlevel);
1593 : 1390 : SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
2504 alvherre@alvh.no-ip. 1594 : 1390 : table_close(rel, NoLock);
2590 1595 [ + + ]: 1390 : if (!OidIsValid(parentIndexId))
1596 : 1191 : pgstat_progress_end_command();
1597 : : else
1598 : : {
1599 : : /* Update progress for an intermediate partitioned index itself */
1137 tgl@sss.pgh.pa.us 1600 : 199 : pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1601 : : }
1602 : :
3028 alvherre@alvh.no-ip. 1603 : 1390 : return address;
1604 : : }
1605 : :
1457 noah@leadboat.com 1606 : 17985 : AtEOXact_GUC(false, root_save_nestlevel);
1607 : 17985 : SetUserIdAndSecContext(root_save_userid, root_save_sec_context);
1608 : :
2295 michael@paquier.xyz 1609 [ + + ]: 17985 : if (!concurrent)
1610 : : {
1611 : : /* Close the heap and we're done, in the non-concurrent case */
2661 andres@anarazel.de 1612 : 17881 : table_close(rel, NoLock);
1613 : :
1614 : : /*
1615 : : * If this is the top-level index, the command is done overall;
1616 : : * otherwise, increment progress to report one child index is done.
1617 : : */
2590 alvherre@alvh.no-ip. 1618 [ + + ]: 17881 : if (!OidIsValid(parentIndexId))
1619 : 16014 : pgstat_progress_end_command();
1620 : : else
1137 tgl@sss.pgh.pa.us 1621 : 1867 : pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1);
1622 : :
4081 alvherre@alvh.no-ip. 1623 : 17881 : return address;
1624 : : }
1625 : :
1626 : : /* save lockrelid and locktag for below, then close rel */
5579 tgl@sss.pgh.pa.us 1627 : 104 : heaprelid = rel->rd_lockInfo.lockRelId;
1628 : 104 : SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
2661 andres@anarazel.de 1629 : 104 : table_close(rel, NoLock);
1630 : :
1631 : : /*
1632 : : * For a concurrent build, it's important to make the catalog entries
1633 : : * visible to other transactions before we start to build the index. That
1634 : : * will prevent them from making incompatible HOT updates. The new index
1635 : : * will be marked not indisready and not indisvalid, so that no one else
1636 : : * tries to either insert into it or use it for queries.
1637 : : *
1638 : : * We must commit our current transaction so that the index becomes
1639 : : * visible; then start another. Note that all the data structures we just
1640 : : * built are lost in the commit. The only data we keep past here are the
1641 : : * relation IDs.
1642 : : *
1643 : : * Before committing, get a session-level lock on the table, to ensure
1644 : : * that neither it nor the index can be dropped before we finish. This
1645 : : * cannot block, even if someone else is waiting for access, because we
1646 : : * already have the same lock within our transaction.
1647 : : *
1648 : : * Note: we don't currently bother with a session lock on the index,
1649 : : * because there are no operations that could change its state while we
1650 : : * hold lock on the parent table. This might need to change later.
1651 : : */
7193 tgl@sss.pgh.pa.us 1652 : 104 : LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1653 : :
6567 alvherre@alvh.no-ip. 1654 : 104 : PopActiveSnapshot();
7193 tgl@sss.pgh.pa.us 1655 : 104 : CommitTransactionCommand();
1656 : 104 : StartTransactionCommand();
1657 : :
1658 : : /* Tell concurrent index builds to ignore us, if index qualifies */
1987 alvherre@alvh.no-ip. 1659 [ + + ]: 104 : if (safe_index)
1660 : 62 : set_indexsafe_procflags();
1661 : :
1662 : : /*
1663 : : * The index is now visible, so we can report the OID. While on it,
1664 : : * include the report for the beginning of phase 2.
1665 : : */
1666 : : {
1898 michael@paquier.xyz 1667 : 104 : const int progress_cols[] = {
1668 : : PROGRESS_CREATEIDX_INDEX_OID,
1669 : : PROGRESS_CREATEIDX_PHASE
1670 : : };
1671 : 104 : const int64 progress_vals[] = {
1672 : : indexRelationId,
1673 : : PROGRESS_CREATEIDX_PHASE_WAIT_1
1674 : : };
1675 : :
1676 : 104 : pgstat_progress_update_multi_param(2, progress_cols, progress_vals);
1677 : : }
1678 : :
1679 : : /*
1680 : : * Phase 2 of concurrent index build (see comments for validate_index()
1681 : : * for an overview of how this works)
1682 : : *
1683 : : * Now we must wait until no running transaction could have the table open
1684 : : * with the old list of indexes. Use ShareLock to consider running
1685 : : * transactions that hold locks that permit writing to the table. Note we
1686 : : * do not need to worry about xacts that open the table for writing after
1687 : : * this point; they will see the new index when they open it.
1688 : : *
1689 : : * Note: the reason we use actual lock acquisition here, rather than just
1690 : : * checking the ProcArray and sleeping, is that deadlock is possible if
1691 : : * one of the transactions in question is blocked trying to acquire an
1692 : : * exclusive lock on our table. The lock code will detect deadlock and
1693 : : * error out properly.
1694 : : */
2590 alvherre@alvh.no-ip. 1695 : 104 : WaitForLockers(heaplocktag, ShareLock, true);
1696 : :
1697 : : /*
1698 : : * At this moment we are sure that there are no transactions with the
1699 : : * table open for write that don't have this new index in their list of
1700 : : * indexes. We have waited out all the existing transactions and any new
1701 : : * transaction will have the new index in its list, but the index is still
1702 : : * marked as "not-ready-for-inserts". The index is consulted while
1703 : : * deciding HOT-safety though. This arrangement ensures that no new HOT
1704 : : * chains can be created where the new tuple and the old tuple in the
1705 : : * chain have different index keys.
1706 : : *
1707 : : * We now take a new snapshot, and build the index using all tuples that
1708 : : * are visible in this snapshot. We can be sure that any HOT updates to
1709 : : * these tuples will be compatible with the index, since any updates made
1710 : : * by transactions that didn't know about the index are now committed or
1711 : : * rolled back. Thus, each visible tuple is either the end of its
1712 : : * HOT-chain or the extension of the chain is HOT-safe for this index.
1713 : : */
1714 : :
1715 : : /* Set ActiveSnapshot since functions in the indexes may need it */
6567 1716 : 104 : PushActiveSnapshot(GetTransactionSnapshot());
1717 : :
1718 : : /* Perform concurrent build of index */
986 peter@eisentraut.org 1719 : 104 : index_concurrently_build(tableId, indexRelationId);
1720 : :
1721 : : /* we can do away with our snapshot */
6567 alvherre@alvh.no-ip. 1722 : 80 : PopActiveSnapshot();
1723 : :
1724 : : /*
1725 : : * Commit this transaction to make the indisready update visible.
1726 : : */
6802 tgl@sss.pgh.pa.us 1727 : 80 : CommitTransactionCommand();
1728 : 80 : StartTransactionCommand();
1729 : :
1730 : : /* Tell concurrent index builds to ignore us, if index qualifies */
1987 alvherre@alvh.no-ip. 1731 [ + + ]: 80 : if (safe_index)
1732 : 54 : set_indexsafe_procflags();
1733 : :
1734 : : /*
1735 : : * Phase 3 of concurrent index build
1736 : : *
1737 : : * We once again wait until no transaction can have the table open with
1738 : : * the index marked as read-only for updates.
1739 : : */
2590 1740 : 80 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
1741 : : PROGRESS_CREATEIDX_PHASE_WAIT_2);
1742 : 80 : WaitForLockers(heaplocktag, ShareLock, true);
1743 : :
1744 : : /*
1745 : : * Now take the "reference snapshot" that will be used by validate_index()
1746 : : * to filter candidate tuples. Beware! There might still be snapshots in
1747 : : * use that treat some transaction as in-progress that our reference
1748 : : * snapshot treats as committed. If such a recently-committed transaction
1749 : : * deleted tuples in the table, we will not include them in the index; yet
1750 : : * those transactions which see the deleting one as still-in-progress will
1751 : : * expect such tuples to be there once we mark the index as valid.
1752 : : *
1753 : : * We solve this by waiting for all endangered transactions to exit before
1754 : : * we mark the index as valid.
1755 : : *
1756 : : * We also set ActiveSnapshot to this snap, since functions in indexes may
1757 : : * need a snapshot.
1758 : : */
6567 1759 : 80 : snapshot = RegisterSnapshot(GetTransactionSnapshot());
1760 : 80 : PushActiveSnapshot(snapshot);
1761 : :
1762 : : /*
1763 : : * Scan the index and the heap, insert any missing index entries.
1764 : : */
986 peter@eisentraut.org 1765 : 80 : validate_index(tableId, indexRelationId, snapshot);
1766 : :
1767 : : /*
1768 : : * Drop the reference snapshot. We must do this before waiting out other
1769 : : * snapshot holders, else we will deadlock against other processes also
1770 : : * doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
1771 : : * they must wait for. But first, save the snapshot's xmin to use as
1772 : : * limitXmin for GetCurrentVirtualXIDs().
1773 : : */
4758 tgl@sss.pgh.pa.us 1774 : 80 : limitXmin = snapshot->xmin;
1775 : :
1776 : 80 : PopActiveSnapshot();
1777 : 80 : UnregisterSnapshot(snapshot);
1778 : :
1779 : : /*
1780 : : * The snapshot subsystem could still contain registered snapshots that
1781 : : * are holding back our process's advertised xmin; in particular, if
1782 : : * default_transaction_isolation = serializable, there is a transaction
1783 : : * snapshot that is still active. The CatalogSnapshot is likewise a
1784 : : * hazard. To ensure no deadlocks, we must commit and start yet another
1785 : : * transaction, and do our wait before any snapshot has been taken in it.
1786 : : */
2939 1787 : 80 : CommitTransactionCommand();
1788 : 80 : StartTransactionCommand();
1789 : :
1790 : : /* Tell concurrent index builds to ignore us, if index qualifies */
1987 alvherre@alvh.no-ip. 1791 [ + + ]: 80 : if (safe_index)
1792 : 54 : set_indexsafe_procflags();
1793 : :
1794 : : /* We should now definitely not be advertising any xmin. */
2091 andres@anarazel.de 1795 [ - + ]: 80 : Assert(MyProc->xmin == InvalidTransactionId);
1796 : :
1797 : : /*
1798 : : * The index is now valid in the sense that it contains all currently
1799 : : * interesting tuples. But since it might not contain tuples deleted just
1800 : : * before the reference snap was taken, we have to wait out any
1801 : : * transactions that might have older snapshots.
1802 : : */
162 alvherre@kurilemu.de 1803 :GNC 80 : INJECTION_POINT("define-index-before-set-valid", NULL);
2590 alvherre@alvh.no-ip. 1804 :CBC 80 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
1805 : : PROGRESS_CREATEIDX_PHASE_WAIT_3);
1806 : 80 : WaitForOlderSnapshots(limitXmin, true);
1807 : :
1808 : : /*
1809 : : * Updating pg_index might involve TOAST table access, so ensure we have a
1810 : : * valid snapshot.
1811 : : */
586 nathan@postgresql.or 1812 : 80 : PushActiveSnapshot(GetTransactionSnapshot());
1813 : :
1814 : : /*
1815 : : * Index can now be marked valid -- update its pg_index entry
1816 : : */
4906 tgl@sss.pgh.pa.us 1817 : 80 : index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID);
1818 : :
586 nathan@postgresql.or 1819 : 80 : PopActiveSnapshot();
1820 : :
1821 : : /*
1822 : : * The pg_index update will cause backends (including this one) to update
1823 : : * relcache entries for the index itself, but we should also send a
1824 : : * relcache inval on the parent table to force replanning of cached plans.
1825 : : * Otherwise existing sessions might fail to use the new index where it
1826 : : * would be useful. (Note that our earlier commits did not create reasons
1827 : : * to replan; so relcache flush on the index itself was sufficient.)
1828 : : */
6943 tgl@sss.pgh.pa.us 1829 : 80 : CacheInvalidateRelcacheByRelid(heaprelid.relId);
1830 : :
1831 : : /*
1832 : : * Last thing to do is release the session-level lock on the parent table.
1833 : : */
7193 1834 : 80 : UnlockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1835 : :
2590 alvherre@alvh.no-ip. 1836 : 80 : pgstat_progress_end_command();
1837 : :
4081 1838 : 80 : return address;
1839 : : }
1840 : :
1841 : :
1842 : : /*
1843 : : * CheckPredicate
1844 : : * Checks that the given partial-index predicate is valid.
1845 : : *
1846 : : * This used to also constrain the form of the predicate to forms that
1847 : : * indxpath.c could do something with. However, that seems overly
1848 : : * restrictive. One useful application of partial indexes is to apply
1849 : : * a UNIQUE constraint across a subset of a table, and in that scenario
1850 : : * any evaluable predicate will work. So accept any predicate here
1851 : : * (except ones requiring a plan), and let indxpath.c fend for itself.
1852 : : */
1853 : : static void
8164 tgl@sss.pgh.pa.us 1854 : 281 : CheckPredicate(Expr *predicate)
1855 : : {
1856 : : /*
1857 : : * transformExpr() should have already rejected subqueries, aggregates,
1858 : : * and window functions, based on the EXPR_KIND_ for a predicate.
1859 : : */
1860 : :
1861 : : /*
1862 : : * A predicate using mutable functions is probably wrong, for the same
1863 : : * reasons that we don't allow an index expression to use one.
1864 : : */
901 1865 [ - + ]: 281 : if (contain_mutable_functions_after_planning(predicate))
8325 tgl@sss.pgh.pa.us 1866 [ # # ]:UBC 0 : ereport(ERROR,
1867 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1868 : : errmsg("functions in index predicate must be marked IMMUTABLE")));
10892 scrappy@hub.org 1869 :CBC 281 : }
1870 : :
1871 : : /*
1872 : : * Compute per-index-column information, including indexed column numbers
1873 : : * or index expressions, opclasses and their options. Note, all output vectors
1874 : : * should be allocated for all columns, including "including" ones.
1875 : : *
1876 : : * If the caller switched to the table owner, ddl_userid is the role for ACL
1877 : : * checks reached without traversing opaque expressions. Otherwise, it's
1878 : : * InvalidOid, and other ddl_* arguments are undefined.
1879 : : */
1880 : : static void
121 tgl@sss.pgh.pa.us 1881 :GNC 19960 : ComputeIndexAttrs(ParseState *pstate,
1882 : : IndexInfo *indexInfo,
1883 : : Oid *typeOids,
1884 : : Oid *collationOids,
1885 : : Oid *opclassOids,
1886 : : Datum *opclassOptions,
1887 : : int16 *colOptions,
1888 : : const List *attList, /* list of IndexElem's */
1889 : : const List *exclusionOpNames,
1890 : : Oid relId,
1891 : : const char *accessMethodName,
1892 : : Oid accessMethodId,
1893 : : bool amcanorder,
1894 : : bool isconstraint,
1895 : : bool iswithoutoverlaps,
1896 : : Oid ddl_userid,
1897 : : int ddl_sec_context,
1898 : : int *ddl_save_nestlevel)
1899 : : {
1900 : : ListCell *nextExclOp;
1901 : : ListCell *lc;
1902 : : int attn;
2950 teodor@sigaev.ru 1903 :CBC 19960 : int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1904 : : Oid save_userid;
1905 : : int save_sec_context;
1906 : :
1907 : : /* Allocate space for exclusion operator info, if needed */
5993 tgl@sss.pgh.pa.us 1908 [ + + ]: 19960 : if (exclusionOpNames)
1909 : : {
2950 teodor@sigaev.ru 1910 [ - + ]: 228 : Assert(list_length(exclusionOpNames) == nkeycols);
1331 peter@eisentraut.org 1911 : 228 : indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
1912 : 228 : indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
1913 : 228 : indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
5993 tgl@sss.pgh.pa.us 1914 : 228 : nextExclOp = list_head(exclusionOpNames);
1915 : : }
1916 : : else
1917 : 19732 : nextExclOp = NULL;
1918 : :
1919 : : /*
1920 : : * If this is a WITHOUT OVERLAPS constraint, we need space for exclusion
1921 : : * ops, but we don't need to parse anything, so we can let nextExclOp be
1922 : : * NULL. Note that for partitions/inheriting/LIKE, exclusionOpNames will
1923 : : * be set, so we already allocated above.
1924 : : */
595 peter@eisentraut.org 1925 [ + + ]: 19960 : if (iswithoutoverlaps)
1926 : : {
1927 [ + + ]: 587 : if (exclusionOpNames == NIL)
1928 : : {
1929 : 515 : indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
1930 : 515 : indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
1931 : 515 : indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
1932 : : }
1933 : 587 : nextExclOp = NULL;
1934 : : }
1935 : :
1410 noah@leadboat.com 1936 [ + + ]: 19960 : if (OidIsValid(ddl_userid))
1937 : 19887 : GetUserIdAndSecContext(&save_userid, &save_sec_context);
1938 : :
1939 : : /*
1940 : : * process attributeList
1941 : : */
5993 tgl@sss.pgh.pa.us 1942 : 19960 : attn = 0;
1943 [ + - + + : 47667 : foreach(lc, attList)
+ + ]
1944 : : {
1945 : 27924 : IndexElem *attribute = (IndexElem *) lfirst(lc);
1946 : : Oid atttype;
1947 : : Oid attcollation;
1948 : :
1949 : : /*
1950 : : * Process the column-or-expression to be indexed.
1951 : : */
8378 1952 [ + + ]: 27924 : if (attribute->name != NULL)
1953 : : {
1954 : : /* Simple index attribute */
1955 : : HeapTuple atttuple;
1956 : : Form_pg_attribute attform;
1957 : :
1958 [ - + ]: 27012 : Assert(attribute->expr == NULL);
1959 : 27012 : atttuple = SearchSysCacheAttName(relId, attribute->name);
1960 [ + + ]: 27012 : if (!HeapTupleIsValid(atttuple))
1961 : : {
1962 : : /* difference in error message spellings is historical */
8035 1963 [ + + ]: 20 : if (isconstraint)
1964 [ + - ]: 12 : ereport(ERROR,
1965 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
1966 : : errmsg("column \"%s\" named in key does not exist",
1967 : : attribute->name),
1968 : : parser_errposition(pstate, attribute->location)));
1969 : : else
1970 [ + - ]: 8 : ereport(ERROR,
1971 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
1972 : : errmsg("column \"%s\" does not exist",
1973 : : attribute->name),
1974 : : parser_errposition(pstate, attribute->location)));
1975 : : }
8378 1976 : 26992 : attform = (Form_pg_attribute) GETSTRUCT(atttuple);
2945 teodor@sigaev.ru 1977 : 26992 : indexInfo->ii_IndexAttrNumbers[attn] = attform->attnum;
8378 tgl@sss.pgh.pa.us 1978 : 26992 : atttype = attform->atttypid;
5565 peter_e@gmx.net 1979 : 26992 : attcollation = attform->attcollation;
8378 tgl@sss.pgh.pa.us 1980 : 26992 : ReleaseSysCache(atttuple);
1981 : : }
1982 : : else
1983 : : {
1984 : : /* Index expression */
5504 bruce@momjian.us 1985 : 912 : Node *expr = attribute->expr;
1986 : :
5521 tgl@sss.pgh.pa.us 1987 [ - + ]: 912 : Assert(expr != NULL);
1988 : :
2950 teodor@sigaev.ru 1989 [ - + ]: 912 : if (attn >= nkeycols)
2950 teodor@sigaev.ru 1990 [ # # ]:UBC 0 : ereport(ERROR,
1991 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1992 : : errmsg("expressions are not supported in included columns"),
1993 : : parser_errposition(pstate, attribute->location)));
5521 tgl@sss.pgh.pa.us 1994 :CBC 912 : atttype = exprType(expr);
1995 : 912 : attcollation = exprCollation(expr);
1996 : :
1997 : : /*
1998 : : * Strip any top-level COLLATE clause. This ensures that we treat
1999 : : * "x COLLATE y" and "(x COLLATE y)" alike.
2000 : : */
2001 [ + + ]: 932 : while (IsA(expr, CollateExpr))
2002 : 20 : expr = (Node *) ((CollateExpr *) expr)->arg;
2003 : :
2004 [ + + ]: 912 : if (IsA(expr, Var) &&
2005 [ + - ]: 8 : ((Var *) expr)->varattno != InvalidAttrNumber)
2006 : : {
2007 : : /*
2008 : : * User wrote "(column)" or "(column COLLATE something)".
2009 : : * Treat it like simple attribute anyway.
2010 : : */
2945 teodor@sigaev.ru 2011 : 8 : indexInfo->ii_IndexAttrNumbers[attn] = ((Var *) expr)->varattno;
2012 : : }
2013 : : else
2014 : : {
2931 tgl@sss.pgh.pa.us 2015 : 904 : indexInfo->ii_IndexAttrNumbers[attn] = 0; /* marks expression */
5521 2016 : 904 : indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
2017 : : expr);
2018 : :
2019 : : /*
2020 : : * transformExpr() should have already rejected subqueries,
2021 : : * aggregates, and window functions, based on the EXPR_KIND_
2022 : : * for an index expression.
2023 : : */
2024 : :
2025 : : /*
2026 : : * An expression using mutable functions is probably wrong,
2027 : : * since if you aren't going to get the same result for the
2028 : : * same data every time, it's not clear what the index entries
2029 : : * mean at all.
2030 : : */
901 2031 [ + + ]: 904 : if (contain_mutable_functions_after_planning((Expr *) expr))
5521 2032 [ + - ]: 184 : ereport(ERROR,
2033 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2034 : : errmsg("functions in index expression must be marked IMMUTABLE"),
2035 : : parser_errposition(pstate, attribute->location)));
2036 : : }
2037 : : }
2038 : :
986 peter@eisentraut.org 2039 : 27720 : typeOids[attn] = atttype;
2040 : :
2041 : : /*
2042 : : * Included columns have no collation, no opclass and no ordering
2043 : : * options.
2044 : : */
2945 teodor@sigaev.ru 2045 [ + + ]: 27720 : if (attn >= nkeycols)
2046 : : {
2047 [ - + ]: 410 : if (attribute->collation)
2945 teodor@sigaev.ru 2048 [ # # ]:UBC 0 : ereport(ERROR,
2049 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2050 : : errmsg("including column does not support a collation"),
2051 : : parser_errposition(pstate, attribute->location)));
2945 teodor@sigaev.ru 2052 [ - + ]:CBC 410 : if (attribute->opclass)
2945 teodor@sigaev.ru 2053 [ # # ]:UBC 0 : ereport(ERROR,
2054 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2055 : : errmsg("including column does not support an operator class"),
2056 : : parser_errposition(pstate, attribute->location)));
2945 teodor@sigaev.ru 2057 [ - + ]:CBC 410 : if (attribute->ordering != SORTBY_DEFAULT)
2945 teodor@sigaev.ru 2058 [ # # ]:UBC 0 : ereport(ERROR,
2059 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2060 : : errmsg("including column does not support ASC/DESC options"),
2061 : : parser_errposition(pstate, attribute->location)));
2945 teodor@sigaev.ru 2062 [ - + ]:CBC 410 : if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
2945 teodor@sigaev.ru 2063 [ # # ]:UBC 0 : ereport(ERROR,
2064 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2065 : : errmsg("including column does not support NULLS FIRST/LAST options"),
2066 : : parser_errposition(pstate, attribute->location)));
2067 : :
986 peter@eisentraut.org 2068 :CBC 410 : opclassOids[attn] = InvalidOid;
945 2069 : 410 : opclassOptions[attn] = (Datum) 0;
986 2070 : 410 : colOptions[attn] = 0;
2071 : 410 : collationOids[attn] = InvalidOid;
2945 teodor@sigaev.ru 2072 : 410 : attn++;
2073 : :
2074 : 410 : continue;
2075 : : }
2076 : :
2077 : : /*
2078 : : * Apply collation override if any. Use of ddl_userid is necessary
2079 : : * due to ACL checks therein, and it's safe because collations don't
2080 : : * contain opaque expressions (or non-opaque expressions).
2081 : : */
5565 peter_e@gmx.net 2082 [ + + ]: 27310 : if (attribute->collation)
2083 : : {
1410 noah@leadboat.com 2084 [ + - ]: 76 : if (OidIsValid(ddl_userid))
2085 : : {
2086 : 76 : AtEOXact_GUC(false, *ddl_save_nestlevel);
2087 : 76 : SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2088 : : }
5526 tgl@sss.pgh.pa.us 2089 : 76 : attcollation = get_collation_oid(attribute->collation, false);
1410 noah@leadboat.com 2090 [ + - ]: 75 : if (OidIsValid(ddl_userid))
2091 : : {
2092 : 75 : SetUserIdAndSecContext(save_userid, save_sec_context);
2093 : 75 : *ddl_save_nestlevel = NewGUCNestLevel();
659 jdavis@postgresql.or 2094 : 75 : RestrictSearchPath();
2095 : : }
2096 : : }
2097 : :
2098 : : /*
2099 : : * Check we have a collation iff it's a collatable type. The only
2100 : : * expected failures here are (1) COLLATE applied to a noncollatable
2101 : : * type, or (2) index expression had an unresolved collation. But we
2102 : : * might as well code this to be a complete consistency check.
2103 : : */
5526 tgl@sss.pgh.pa.us 2104 [ + + ]: 27309 : if (type_is_collatable(atttype))
2105 : : {
2106 [ - + ]: 4070 : if (!OidIsValid(attcollation))
5526 tgl@sss.pgh.pa.us 2107 [ # # ]:UBC 0 : ereport(ERROR,
2108 : : (errcode(ERRCODE_INDETERMINATE_COLLATION),
2109 : : errmsg("could not determine which collation to use for index expression"),
2110 : : errhint("Use the COLLATE clause to set the collation explicitly."),
2111 : : parser_errposition(pstate, attribute->location)));
2112 : : }
2113 : : else
2114 : : {
5526 tgl@sss.pgh.pa.us 2115 [ + + ]:CBC 23239 : if (OidIsValid(attcollation))
5565 peter_e@gmx.net 2116 [ + - ]: 8 : ereport(ERROR,
2117 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2118 : : errmsg("collations are not supported by type %s",
2119 : : format_type_be(atttype)),
2120 : : parser_errposition(pstate, attribute->location)));
2121 : : }
2122 : :
986 peter@eisentraut.org 2123 : 27301 : collationOids[attn] = attcollation;
2124 : :
2125 : : /*
2126 : : * Identify the opclass to use. Use of ddl_userid is necessary due to
2127 : : * ACL checks therein. This is safe despite opclasses containing
2128 : : * opaque expressions (specifically, functions), because only
2129 : : * superusers can define opclasses.
2130 : : */
1410 noah@leadboat.com 2131 [ + + ]: 27301 : if (OidIsValid(ddl_userid))
2132 : : {
2133 : 27224 : AtEOXact_GUC(false, *ddl_save_nestlevel);
2134 : 27224 : SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2135 : : }
986 peter@eisentraut.org 2136 : 27301 : opclassOids[attn] = ResolveOpClass(attribute->opclass,
2137 : : atttype,
2138 : : accessMethodName,
2139 : : accessMethodId);
1410 noah@leadboat.com 2140 [ + + ]: 27297 : if (OidIsValid(ddl_userid))
2141 : : {
2142 : 27220 : SetUserIdAndSecContext(save_userid, save_sec_context);
2143 : 27220 : *ddl_save_nestlevel = NewGUCNestLevel();
659 jdavis@postgresql.or 2144 : 27220 : RestrictSearchPath();
2145 : : }
2146 : :
2147 : : /*
2148 : : * Identify the exclusion operator, if any.
2149 : : */
5993 tgl@sss.pgh.pa.us 2150 [ + + ]: 27297 : if (nextExclOp)
2151 : : {
5912 bruce@momjian.us 2152 : 226 : List *opname = (List *) lfirst(nextExclOp);
2153 : : Oid opid;
2154 : : Oid opfamily;
2155 : : int strat;
2156 : :
2157 : : /*
2158 : : * Find the operator --- it must accept the column datatype
2159 : : * without runtime coercion (but binary compatibility is OK).
2160 : : * Operators contain opaque expressions (specifically, functions).
2161 : : * compatible_oper_opid() boils down to oper() and
2162 : : * IsBinaryCoercible(). PostgreSQL would have security problems
2163 : : * elsewhere if oper() started calling opaque expressions.
2164 : : */
1410 noah@leadboat.com 2165 [ + - ]: 226 : if (OidIsValid(ddl_userid))
2166 : : {
2167 : 226 : AtEOXact_GUC(false, *ddl_save_nestlevel);
2168 : 226 : SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2169 : : }
5993 tgl@sss.pgh.pa.us 2170 : 226 : opid = compatible_oper_opid(opname, atttype, atttype, false);
1410 noah@leadboat.com 2171 [ + - ]: 226 : if (OidIsValid(ddl_userid))
2172 : : {
2173 : 226 : SetUserIdAndSecContext(save_userid, save_sec_context);
2174 : 226 : *ddl_save_nestlevel = NewGUCNestLevel();
659 jdavis@postgresql.or 2175 : 226 : RestrictSearchPath();
2176 : : }
2177 : :
2178 : : /*
2179 : : * Only allow commutative operators to be used in exclusion
2180 : : * constraints. If X conflicts with Y, but Y does not conflict
2181 : : * with X, bad things will happen.
2182 : : */
5993 tgl@sss.pgh.pa.us 2183 [ - + ]: 226 : if (get_commutator(opid) != opid)
5993 tgl@sss.pgh.pa.us 2184 [ # # ]:UBC 0 : ereport(ERROR,
2185 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2186 : : errmsg("operator %s is not commutative",
2187 : : format_operator(opid)),
2188 : : errdetail("Only commutative operators can be used in exclusion constraints."),
2189 : : parser_errposition(pstate, attribute->location)));
2190 : :
2191 : : /*
2192 : : * Operator must be a member of the right opfamily, too
2193 : : */
986 peter@eisentraut.org 2194 :CBC 226 : opfamily = get_opclass_family(opclassOids[attn]);
5993 tgl@sss.pgh.pa.us 2195 : 226 : strat = get_op_opfamily_strategy(opid, opfamily);
2196 [ - + ]: 226 : if (strat == 0)
5993 tgl@sss.pgh.pa.us 2197 [ # # ]:UBC 0 : ereport(ERROR,
2198 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2199 : : errmsg("operator %s is not a member of operator family \"%s\"",
2200 : : format_operator(opid),
2201 : : get_opfamily_name(opfamily, false)),
2202 : : errdetail("The exclusion operator must be related to the index operator class for the constraint."),
2203 : : parser_errposition(pstate, attribute->location)));
2204 : :
5993 tgl@sss.pgh.pa.us 2205 :CBC 226 : indexInfo->ii_ExclusionOps[attn] = opid;
2206 : 226 : indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
2207 : 226 : indexInfo->ii_ExclusionStrats[attn] = strat;
2486 2208 : 226 : nextExclOp = lnext(exclusionOpNames, nextExclOp);
2209 : : }
595 peter@eisentraut.org 2210 [ + + ]: 27071 : else if (iswithoutoverlaps)
2211 : : {
2212 : : CompareType cmptype;
2213 : : StrategyNumber strat;
2214 : : Oid opid;
2215 : :
2216 [ + + ]: 1196 : if (attn == nkeycols - 1)
475 2217 : 587 : cmptype = COMPARE_OVERLAP;
2218 : : else
2219 : 609 : cmptype = COMPARE_EQ;
2220 : 1196 : GetOperatorFromCompareType(opclassOids[attn], InvalidOid, cmptype, &opid, &strat);
595 2221 : 1196 : indexInfo->ii_ExclusionOps[attn] = opid;
2222 : 1196 : indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
2223 : 1196 : indexInfo->ii_ExclusionStrats[attn] = strat;
2224 : : }
2225 : :
2226 : : /*
2227 : : * Set up the per-column options (indoption field). For now, this is
2228 : : * zero for any un-ordered index, while ordered indexes have DESC and
2229 : : * NULLS FIRST/LAST options.
2230 : : */
986 2231 : 27297 : colOptions[attn] = 0;
7056 tgl@sss.pgh.pa.us 2232 [ + + ]: 27297 : if (amcanorder)
2233 : : {
2234 : : /* default ordering is ASC */
2235 [ + + ]: 24362 : if (attribute->ordering == SORTBY_DESC)
986 peter@eisentraut.org 2236 : 28 : colOptions[attn] |= INDOPTION_DESC;
2237 : : /* default null ordering is LAST for ASC, FIRST for DESC */
7056 tgl@sss.pgh.pa.us 2238 [ + + ]: 24362 : if (attribute->nulls_ordering == SORTBY_NULLS_DEFAULT)
2239 : : {
2240 [ + + ]: 24342 : if (attribute->ordering == SORTBY_DESC)
986 peter@eisentraut.org 2241 : 20 : colOptions[attn] |= INDOPTION_NULLS_FIRST;
2242 : : }
7056 tgl@sss.pgh.pa.us 2243 [ + + ]: 20 : else if (attribute->nulls_ordering == SORTBY_NULLS_FIRST)
986 peter@eisentraut.org 2244 : 8 : colOptions[attn] |= INDOPTION_NULLS_FIRST;
2245 : : }
2246 : : else
2247 : : {
2248 : : /* index AM does not support ordering */
7056 tgl@sss.pgh.pa.us 2249 [ - + ]: 2935 : if (attribute->ordering != SORTBY_DEFAULT)
7056 tgl@sss.pgh.pa.us 2250 [ # # ]:UBC 0 : ereport(ERROR,
2251 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2252 : : errmsg("access method \"%s\" does not support ASC/DESC options",
2253 : : accessMethodName),
2254 : : parser_errposition(pstate, attribute->location)));
7056 tgl@sss.pgh.pa.us 2255 [ - + ]:CBC 2935 : if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
7056 tgl@sss.pgh.pa.us 2256 [ # # ]:UBC 0 : ereport(ERROR,
2257 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2258 : : errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
2259 : : accessMethodName),
2260 : : parser_errposition(pstate, attribute->location)));
2261 : : }
2262 : :
2263 : : /* Set up the per-column opclass options (attoptions field). */
2227 akorotkov@postgresql 2264 [ + + ]:CBC 27297 : if (attribute->opclassopts)
2265 : : {
2266 [ - + ]: 88 : Assert(attn < nkeycols);
2267 : :
945 peter@eisentraut.org 2268 : 88 : opclassOptions[attn] =
2227 akorotkov@postgresql 2269 : 88 : transformRelOptions((Datum) 0, attribute->opclassopts,
2270 : : NULL, NULL, false, false);
2271 : : }
2272 : : else
945 peter@eisentraut.org 2273 : 27209 : opclassOptions[attn] = (Datum) 0;
2274 : :
9426 tgl@sss.pgh.pa.us 2275 : 27297 : attn++;
2276 : : }
9566 2277 : 19743 : }
2278 : :
2279 : : /*
2280 : : * Resolve possibly-defaulted operator class specification
2281 : : *
2282 : : * Note: This is used to resolve operator class specifications in index and
2283 : : * partition key definitions.
2284 : : */
2285 : : Oid
986 peter@eisentraut.org 2286 : 27393 : ResolveOpClass(const List *opclass, Oid attrType,
2287 : : const char *accessMethodName, Oid accessMethodId)
2288 : : {
2289 : : char *schemaname;
2290 : : char *opcname;
2291 : : HeapTuple tuple;
2292 : : Form_pg_opclass opform;
2293 : : Oid opClassId,
2294 : : opInputType;
2295 : :
8378 tgl@sss.pgh.pa.us 2296 [ + + ]: 27393 : if (opclass == NIL)
2297 : : {
2298 : : /* no operator class specified, so find the default */
9023 2299 : 13976 : opClassId = GetDefaultOpClass(attrType, accessMethodId);
2300 [ + + ]: 13976 : if (!OidIsValid(opClassId))
8325 2301 [ + - ]: 4 : ereport(ERROR,
2302 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2303 : : errmsg("data type %s has no default operator class for access method \"%s\"",
2304 : : format_type_be(attrType), accessMethodName),
2305 : : errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
9023 2306 : 13972 : return opClassId;
2307 : : }
2308 : :
2309 : : /*
2310 : : * Specific opclass name given, so look up the opclass.
2311 : : */
2312 : :
2313 : : /* deconstruct the name list */
8378 2314 : 13417 : DeconstructQualifiedName(opclass, &schemaname, &opcname);
2315 : :
8784 2316 [ + + ]: 13417 : if (schemaname)
2317 : : {
2318 : : /* Look in specific schema only */
2319 : : Oid namespaceId;
2320 : :
4847 bruce@momjian.us 2321 : 18 : namespaceId = LookupExplicitNamespace(schemaname, false);
5924 rhaas@postgresql.org 2322 : 18 : tuple = SearchSysCache3(CLAAMNAMENSP,
2323 : : ObjectIdGetDatum(accessMethodId),
2324 : : PointerGetDatum(opcname),
2325 : : ObjectIdGetDatum(namespaceId));
2326 : : }
2327 : : else
2328 : : {
2329 : : /* Unqualified opclass name, so search the search path */
8784 tgl@sss.pgh.pa.us 2330 : 13399 : opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
2331 [ + + ]: 13399 : if (!OidIsValid(opClassId))
8325 2332 [ + - ]: 8 : ereport(ERROR,
2333 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2334 : : errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2335 : : opcname, accessMethodName)));
5924 rhaas@postgresql.org 2336 : 13391 : tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opClassId));
2337 : : }
2338 : :
9023 tgl@sss.pgh.pa.us 2339 [ - + ]: 13409 : if (!HeapTupleIsValid(tuple))
8325 tgl@sss.pgh.pa.us 2340 [ # # ]:UBC 0 : ereport(ERROR,
2341 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2342 : : errmsg("operator class \"%s\" does not exist for access method \"%s\"",
2343 : : NameListToString(opclass), accessMethodName)));
2344 : :
2345 : : /*
2346 : : * Verify that the index operator class accepts this datatype. Note we
2347 : : * will accept binary compatibility.
2348 : : */
2723 andres@anarazel.de 2349 :CBC 13409 : opform = (Form_pg_opclass) GETSTRUCT(tuple);
2350 : 13409 : opClassId = opform->oid;
2351 : 13409 : opInputType = opform->opcintype;
2352 : :
8630 tgl@sss.pgh.pa.us 2353 [ - + ]: 13409 : if (!IsBinaryCoercible(attrType, opInputType))
8325 tgl@sss.pgh.pa.us 2354 [ # # ]:UBC 0 : ereport(ERROR,
2355 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2356 : : errmsg("operator class \"%s\" does not accept data type %s",
2357 : : NameListToString(opclass), format_type_be(attrType))));
2358 : :
8784 tgl@sss.pgh.pa.us 2359 :CBC 13409 : ReleaseSysCache(tuple);
2360 : :
9023 2361 : 13409 : return opClassId;
2362 : : }
2363 : :
2364 : : /*
2365 : : * GetDefaultOpClass
2366 : : *
2367 : : * Given the OIDs of a datatype and an access method, find the default
2368 : : * operator class, if any. Returns InvalidOid if there is none.
2369 : : */
2370 : : Oid
7389 2371 : 71826 : GetDefaultOpClass(Oid type_id, Oid am_id)
2372 : : {
7073 2373 : 71826 : Oid result = InvalidOid;
9023 2374 : 71826 : int nexact = 0;
2375 : 71826 : int ncompatible = 0;
7073 2376 : 71826 : int ncompatiblepreferred = 0;
2377 : : Relation rel;
2378 : : ScanKeyData skey[1];
2379 : : SysScanDesc scan;
2380 : : HeapTuple tup;
2381 : : TYPCATEGORY tcategory;
2382 : :
2383 : : /* If it's a domain, look at the base type instead */
7389 2384 : 71826 : type_id = getBaseType(type_id);
2385 : :
7073 2386 : 71826 : tcategory = TypeCategory(type_id);
2387 : :
2388 : : /*
2389 : : * We scan through all the opclasses available for the access method,
2390 : : * looking for one that is marked default and matches the target type
2391 : : * (either exactly or binary-compatibly, but prefer an exact match).
2392 : : *
2393 : : * We could find more than one binary-compatible match. If just one is
2394 : : * for a preferred type, use that one; otherwise we fail, forcing the user
2395 : : * to specify which one he wants. (The preferred-type special case is a
2396 : : * kluge for varchar: it's binary-compatible to both text and bpchar, so
2397 : : * we need a tiebreaker.) If we find more than one exact match, then
2398 : : * someone put bogus entries in pg_opclass.
2399 : : */
2661 andres@anarazel.de 2400 : 71826 : rel = table_open(OperatorClassRelationId, AccessShareLock);
2401 : :
7389 tgl@sss.pgh.pa.us 2402 : 71826 : ScanKeyInit(&skey[0],
2403 : : Anum_pg_opclass_opcmethod,
2404 : : BTEqualStrategyNumber, F_OIDEQ,
2405 : : ObjectIdGetDatum(am_id));
2406 : :
2407 : 71826 : scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
2408 : : NULL, 1, skey);
2409 : :
2410 [ + + ]: 3170874 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
2411 : : {
2412 : 3099048 : Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
2413 : :
2414 : : /* ignore altogether if not a default opclass */
7073 2415 [ + + ]: 3099048 : if (!opclass->opcdefault)
2416 : 443179 : continue;
2417 [ + + ]: 2655869 : if (opclass->opcintype == type_id)
2418 : : {
2419 : 62305 : nexact++;
2723 andres@anarazel.de 2420 : 62305 : result = opclass->oid;
2421 : : }
7073 tgl@sss.pgh.pa.us 2422 [ + + + + ]: 3889638 : else if (nexact == 0 &&
2423 : 1296074 : IsBinaryCoercible(type_id, opclass->opcintype))
2424 : : {
2425 [ + + ]: 16437 : if (IsPreferredType(tcategory, opclass->opcintype))
2426 : : {
2427 : 1300 : ncompatiblepreferred++;
2723 andres@anarazel.de 2428 : 1300 : result = opclass->oid;
2429 : : }
7073 tgl@sss.pgh.pa.us 2430 [ + - ]: 15137 : else if (ncompatiblepreferred == 0)
2431 : : {
9023 2432 : 15137 : ncompatible++;
2723 andres@anarazel.de 2433 : 15137 : result = opclass->oid;
2434 : : }
2435 : : }
2436 : : }
2437 : :
7389 tgl@sss.pgh.pa.us 2438 : 71826 : systable_endscan(scan);
2439 : :
2661 andres@anarazel.de 2440 : 71826 : table_close(rel, AccessShareLock);
2441 : :
2442 : : /* raise error if pg_opclass contains inconsistent data */
7073 tgl@sss.pgh.pa.us 2443 [ - + ]: 71826 : if (nexact > 1)
8325 tgl@sss.pgh.pa.us 2444 [ # # ]:UBC 0 : ereport(ERROR,
2445 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
2446 : : errmsg("there are multiple default operator classes for data type %s",
2447 : : format_type_be(type_id))));
2448 : :
7073 tgl@sss.pgh.pa.us 2449 [ + + + + ]:CBC 71826 : if (nexact == 1 ||
2450 [ + - ]: 8222 : ncompatiblepreferred == 1 ||
2451 [ + + ]: 8222 : (ncompatiblepreferred == 0 && ncompatible == 1))
2452 : 71013 : return result;
2453 : :
9023 2454 : 813 : return InvalidOid;
2455 : : }
2456 : :
2457 : : /*
2458 : : * GetOperatorFromCompareType
2459 : : *
2460 : : * opclass - the opclass to use
2461 : : * rhstype - the type for the right-hand side, or InvalidOid to use the type of the given opclass.
2462 : : * cmptype - kind of operator to find
2463 : : * opid - holds the operator we found
2464 : : * strat - holds the output strategy number
2465 : : *
2466 : : * Finds an operator from a CompareType. This is used for temporal index
2467 : : * constraints (and other temporal features) to look up equality and overlaps
2468 : : * operators. We ask an opclass support function to translate from the
2469 : : * compare type to the internal strategy numbers. Raises ERROR on search
2470 : : * failure.
2471 : : */
2472 : : void
475 peter@eisentraut.org 2473 : 2573 : GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype,
2474 : : Oid *opid, StrategyNumber *strat)
2475 : : {
2476 : : Oid amid;
2477 : : Oid opfamily;
2478 : : Oid opcintype;
2479 : :
2480 [ + + + + : 2573 : Assert(cmptype == COMPARE_EQ || cmptype == COMPARE_OVERLAP || cmptype == COMPARE_CONTAINED_BY);
- + ]
2481 : :
2482 : : /*
2483 : : * Use the opclass to get the opfamily, opcintype, and access method. If
2484 : : * any of this fails, quit early.
2485 : : */
119 drowley@postgresql.o 2486 [ - + ]:GNC 2573 : if (!get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
119 drowley@postgresql.o 2487 [ # # ]:UNC 0 : elog(ERROR, "cache lookup failed for opclass %u", opclass);
2488 : :
119 drowley@postgresql.o 2489 :GNC 2573 : amid = get_opclass_method(opclass);
2490 : :
2491 : : /*
2492 : : * Ask the index AM to translate to its internal stratnum
2493 : : */
2494 : 2573 : *strat = IndexAmTranslateCompareType(cmptype, amid, opfamily, true);
2495 [ - + ]: 2573 : if (*strat == InvalidStrategy)
119 drowley@postgresql.o 2496 [ # # # # :UNC 0 : ereport(ERROR,
# # # # ]
2497 : : errcode(ERRCODE_UNDEFINED_OBJECT),
2498 : : cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
2499 : : cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
2500 : : cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
2501 : : errdetail("Could not translate compare type %d for operator family \"%s\" of access method \"%s\".",
2502 : : cmptype, get_opfamily_name(opfamily, false), get_am_name(amid)));
2503 : :
2504 : : /*
2505 : : * We parameterize rhstype so foreign keys can ask for a <@ operator whose
2506 : : * rhs matches the aggregate function. For example range_agg returns
2507 : : * anymultirange.
2508 : : */
119 drowley@postgresql.o 2509 [ + + ]:GNC 2573 : if (!OidIsValid(rhstype))
2510 : 2314 : rhstype = opcintype;
2511 : 2573 : *opid = get_opfamily_member(opfamily, opcintype, rhstype, *strat);
2512 : :
595 peter@eisentraut.org 2513 [ - + ]:CBC 2573 : if (!OidIsValid(*opid))
595 peter@eisentraut.org 2514 [ # # # # :UBC 0 : ereport(ERROR,
# # # # ]
2515 : : errcode(ERRCODE_UNDEFINED_OBJECT),
2516 : : cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
2517 : : cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
2518 : : cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
2519 : : errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
2520 : : get_opfamily_name(opfamily, false), get_am_name(amid)));
595 peter@eisentraut.org 2521 :CBC 2573 : }
2522 : :
2523 : : /*
2524 : : * makeObjectName()
2525 : : *
2526 : : * Create a name for an implicitly created index, sequence, constraint,
2527 : : * extended statistics, etc.
2528 : : *
2529 : : * The parameters are typically: the original table name, the original field
2530 : : * name, and a "type" string (such as "seq" or "pkey"). The field name
2531 : : * and/or type can be NULL if not relevant.
2532 : : *
2533 : : * The result is a palloc'd string.
2534 : : *
2535 : : * The basic result we want is "name1_name2_label", omitting "_name2" or
2536 : : * "_label" when those parameters are NULL. However, we must generate
2537 : : * a name with less than NAMEDATALEN characters! So, we truncate one or
2538 : : * both names if necessary to make a short-enough string. The label part
2539 : : * is never truncated (so it had better be reasonably short).
2540 : : *
2541 : : * The caller is responsible for checking uniqueness of the generated
2542 : : * name and retrying as needed; retrying will be done by altering the
2543 : : * "label" string (which is why we never truncate that part).
2544 : : */
2545 : : char *
7999 tgl@sss.pgh.pa.us 2546 : 77033 : makeObjectName(const char *name1, const char *name2, const char *label)
2547 : : {
2548 : : char *name;
2549 : 77033 : int overhead = 0; /* chars needed for label and underscores */
2550 : : int availchars; /* chars available for name(s) */
2551 : : int name1chars; /* chars allocated to name1 */
2552 : : int name2chars; /* chars allocated to name2 */
2553 : : int ndx;
2554 : :
2555 : 77033 : name1chars = strlen(name1);
2556 [ + + ]: 77033 : if (name2)
2557 : : {
2558 : 67776 : name2chars = strlen(name2);
2559 : 67776 : overhead++; /* allow for separating underscore */
2560 : : }
2561 : : else
2562 : 9257 : name2chars = 0;
2563 [ + + ]: 77033 : if (label)
2564 : 29612 : overhead += strlen(label) + 1;
2565 : :
2566 : 77033 : availchars = NAMEDATALEN - 1 - overhead;
2567 [ - + ]: 77033 : Assert(availchars > 0); /* else caller chose a bad label */
2568 : :
2569 : : /*
2570 : : * If we must truncate, preferentially truncate the longer name. This
2571 : : * logic could be expressed without a loop, but it's simple and obvious as
2572 : : * a loop.
2573 : : */
2574 [ + + ]: 77077 : while (name1chars + name2chars > availchars)
2575 : : {
2576 [ - + ]: 44 : if (name1chars > name2chars)
7999 tgl@sss.pgh.pa.us 2577 :UBC 0 : name1chars--;
2578 : : else
7999 tgl@sss.pgh.pa.us 2579 :CBC 44 : name2chars--;
2580 : : }
2581 : :
7623 neilc@samurai.com 2582 : 77033 : name1chars = pg_mbcliplen(name1, name1chars, name1chars);
7999 tgl@sss.pgh.pa.us 2583 [ + + ]: 77033 : if (name2)
2584 : 67776 : name2chars = pg_mbcliplen(name2, name2chars, name2chars);
2585 : :
2586 : : /* Now construct the string using the chosen lengths */
2587 : 77033 : name = palloc(name1chars + name2chars + overhead + 1);
2588 : 77033 : memcpy(name, name1, name1chars);
2589 : 77033 : ndx = name1chars;
2590 [ + + ]: 77033 : if (name2)
2591 : : {
2592 : 67776 : name[ndx++] = '_';
2593 : 67776 : memcpy(name + ndx, name2, name2chars);
2594 : 67776 : ndx += name2chars;
2595 : : }
2596 [ + + ]: 77033 : if (label)
2597 : : {
2598 : 29612 : name[ndx++] = '_';
2599 : 29612 : strcpy(name + ndx, label);
2600 : : }
2601 : : else
2602 : 47421 : name[ndx] = '\0';
2603 : :
2604 : 77033 : return name;
2605 : : }
2606 : :
2607 : : /*
2608 : : * Select a nonconflicting name for a new relation. This is ordinarily
2609 : : * used to choose index names (which is why it's here) but it can also
2610 : : * be used for sequences, or any autogenerated relation kind.
2611 : : *
2612 : : * name1, name2, and label are used the same way as for makeObjectName(),
2613 : : * except that the label can't be NULL; digits will be appended to the label
2614 : : * if needed to create a name that is unique within the specified namespace.
2615 : : *
2616 : : * If isconstraint is true, we also avoid choosing a name matching any
2617 : : * existing constraint in the same namespace. (This is stricter than what
2618 : : * Postgres itself requires, but the SQL standard says that constraint names
2619 : : * should be unique within schemas, so we follow that for autogenerated
2620 : : * constraint names.)
2621 : : *
2622 : : * Note: it is theoretically possible to get a collision anyway, if someone
2623 : : * else chooses the same name concurrently. We shorten the race condition
2624 : : * window by checking for conflicting relations using SnapshotDirty, but
2625 : : * that doesn't close the window entirely. This is fairly unlikely to be
2626 : : * a problem in practice, especially if one is holding an exclusive lock on
2627 : : * the relation identified by name1. However, if choosing multiple names
2628 : : * within a single command, you'd better create the new object and do
2629 : : * CommandCounterIncrement before choosing the next one!
2630 : : *
2631 : : * Returns a palloc'd string.
2632 : : */
2633 : : char *
2634 : 9561 : ChooseRelationName(const char *name1, const char *name2,
2635 : : const char *label, Oid namespaceid,
2636 : : bool isconstraint)
2637 : : {
2638 : 9561 : int pass = 0;
2639 : 9561 : char *relname = NULL;
2640 : : char modlabel[NAMEDATALEN];
2641 : : SnapshotData SnapshotDirty;
2642 : : Relation pgclassrel;
2643 : :
2644 : : /* prepare to search pg_class with a dirty snapshot */
319 2645 : 9561 : InitDirtySnapshot(SnapshotDirty);
2646 : 9561 : pgclassrel = table_open(RelationRelationId, AccessShareLock);
2647 : :
2648 : : /* try the unmodified label first */
2094 peter@eisentraut.org 2649 : 9561 : strlcpy(modlabel, label, sizeof(modlabel));
2650 : :
2651 : : for (;;)
8035 tgl@sss.pgh.pa.us 2652 : 1275 : {
2653 : : ScanKeyData key[2];
2654 : : SysScanDesc scan;
2655 : : bool collides;
2656 : :
7999 2657 : 10836 : relname = makeObjectName(name1, name2, modlabel);
2658 : :
2659 : : /* is there any conflicting relation name? */
319 2660 : 10836 : ScanKeyInit(&key[0],
2661 : : Anum_pg_class_relname,
2662 : : BTEqualStrategyNumber, F_NAMEEQ,
2663 : : CStringGetDatum(relname));
2664 : 10836 : ScanKeyInit(&key[1],
2665 : : Anum_pg_class_relnamespace,
2666 : : BTEqualStrategyNumber, F_OIDEQ,
2667 : : ObjectIdGetDatum(namespaceid));
2668 : :
2669 : 10836 : scan = systable_beginscan(pgclassrel, ClassNameNspIndexId,
2670 : : true /* indexOK */ ,
2671 : : &SnapshotDirty,
2672 : : 2, key);
2673 : :
2674 : 10836 : collides = HeapTupleIsValid(systable_getnext(scan));
2675 : :
2676 : 10836 : systable_endscan(scan);
2677 : :
2678 : : /* break out of loop if no conflict */
2679 [ + + ]: 10836 : if (!collides)
2680 : : {
2800 2681 [ + + ]: 9565 : if (!isconstraint ||
2682 [ + + ]: 5909 : !ConstraintNameExists(relname, namespaceid))
2683 : : break;
2684 : : }
2685 : :
2686 : : /* found a conflict, so try a new name component */
7999 2687 : 1275 : pfree(relname);
2688 : 1275 : snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
2689 : : }
2690 : :
319 2691 : 9561 : table_close(pgclassrel, AccessShareLock);
2692 : :
7999 2693 : 9561 : return relname;
2694 : : }
2695 : :
2696 : : /*
2697 : : * Select the name to be used for an index.
2698 : : *
2699 : : * The argument list is pretty ad-hoc :-(
2700 : : */
2701 : : static char *
5977 2702 : 8064 : ChooseIndexName(const char *tabname, Oid namespaceId,
2703 : : const List *colnames, const List *exclusionOpNames,
2704 : : bool primary, bool isconstraint)
2705 : : {
2706 : : char *indexname;
2707 : :
2708 [ + + ]: 8064 : if (primary)
2709 : : {
2710 : : /* the primary key's name does not depend on the specific column(s) */
2711 : 5251 : indexname = ChooseRelationName(tabname,
2712 : : NULL,
2713 : : "pkey",
2714 : : namespaceId,
2715 : : true);
2716 : : }
2717 [ + + ]: 2813 : else if (exclusionOpNames != NIL)
2718 : : {
2719 : 155 : indexname = ChooseRelationName(tabname,
2720 : 155 : ChooseIndexNameAddition(colnames),
2721 : : "excl",
2722 : : namespaceId,
2723 : : true);
2724 : : }
2725 [ + + ]: 2658 : else if (isconstraint)
2726 : : {
2727 : 499 : indexname = ChooseRelationName(tabname,
2728 : 499 : ChooseIndexNameAddition(colnames),
2729 : : "key",
2730 : : namespaceId,
2731 : : true);
2732 : : }
2733 : : else
2734 : : {
2735 : 2159 : indexname = ChooseRelationName(tabname,
2736 : 2159 : ChooseIndexNameAddition(colnames),
2737 : : "idx",
2738 : : namespaceId,
2739 : : false);
2740 : : }
2741 : :
2742 : 8064 : return indexname;
2743 : : }
2744 : :
2745 : : /*
2746 : : * Generate "name2" for a new index given the list of column names for it
2747 : : * (as produced by ChooseIndexColumnNames). This will be passed to
2748 : : * ChooseRelationName along with the parent table name and a suitable label.
2749 : : *
2750 : : * We know that less than NAMEDATALEN characters will actually be used,
2751 : : * so we can truncate the result once we've generated that many.
2752 : : *
2753 : : * XXX See also ChooseForeignKeyConstraintNameAddition and
2754 : : * ChooseExtendedStatisticNameAddition.
2755 : : */
2756 : : static char *
986 peter@eisentraut.org 2757 : 2813 : ChooseIndexNameAddition(const List *colnames)
2758 : : {
2759 : : char buf[NAMEDATALEN * 2];
5977 tgl@sss.pgh.pa.us 2760 : 2813 : int buflen = 0;
2761 : : ListCell *lc;
2762 : :
2763 : 2813 : buf[0] = '\0';
2764 [ + - + + : 6344 : foreach(lc, colnames)
+ + ]
2765 : : {
2766 : 3531 : const char *name = (const char *) lfirst(lc);
2767 : :
2768 [ + + ]: 3531 : if (buflen > 0)
5912 bruce@momjian.us 2769 : 718 : buf[buflen++] = '_'; /* insert _ between names */
2770 : :
2771 : : /*
2772 : : * At this point we have buflen <= NAMEDATALEN. name should be less
2773 : : * than NAMEDATALEN already, but use strlcpy for paranoia.
2774 : : */
5977 tgl@sss.pgh.pa.us 2775 : 3531 : strlcpy(buf + buflen, name, NAMEDATALEN);
2776 : 3531 : buflen += strlen(buf + buflen);
2777 [ - + ]: 3531 : if (buflen >= NAMEDATALEN)
5977 tgl@sss.pgh.pa.us 2778 :UBC 0 : break;
2779 : : }
5977 tgl@sss.pgh.pa.us 2780 :CBC 2813 : return pstrdup(buf);
2781 : : }
2782 : :
2783 : : /*
2784 : : * Select the actual names to be used for the columns of an index, given the
2785 : : * list of IndexElems for the columns. This is mostly about ensuring the
2786 : : * names are unique so we don't get a conflicting-attribute-names error.
2787 : : *
2788 : : * Returns a List of plain strings (char *, not String nodes).
2789 : : */
2790 : : static List *
986 peter@eisentraut.org 2791 : 19945 : ChooseIndexColumnNames(const List *indexElems)
2792 : : {
5977 tgl@sss.pgh.pa.us 2793 : 19945 : List *result = NIL;
2794 : : ListCell *lc;
2795 : :
2796 [ + - + + : 47888 : foreach(lc, indexElems)
+ + ]
2797 : : {
2798 : 27943 : IndexElem *ielem = (IndexElem *) lfirst(lc);
2799 : : const char *origname;
2800 : : const char *curname;
2801 : : int i;
2802 : : char buf[NAMEDATALEN];
2803 : :
2804 : : /* Get the preliminary name from the IndexElem */
2805 [ + + ]: 27943 : if (ielem->indexcolname)
3240 2806 : 3056 : origname = ielem->indexcolname; /* caller-specified name */
5977 2807 [ + + ]: 24887 : else if (ielem->name)
3240 2808 : 24594 : origname = ielem->name; /* simple column reference */
2809 : : else
5912 bruce@momjian.us 2810 : 293 : origname = "expr"; /* default name for expression */
2811 : :
2812 : : /* If it conflicts with any previous column, tweak it */
5977 tgl@sss.pgh.pa.us 2813 : 27943 : curname = origname;
2814 : 27943 : for (i = 1;; i++)
2815 : 37 : {
2816 : : ListCell *lc2;
2817 : : char nbuf[32];
2818 : : int nlen;
2819 : :
2820 [ + + + + : 43467 : foreach(lc2, result)
+ + ]
2821 : : {
2822 [ + + ]: 15524 : if (strcmp(curname, (char *) lfirst(lc2)) == 0)
2823 : 37 : break;
2824 : : }
2825 [ + + ]: 27980 : if (lc2 == NULL)
2826 : 27943 : break; /* found nonconflicting name */
2827 : :
2828 : 37 : sprintf(nbuf, "%d", i);
2829 : :
2830 : : /* Ensure generated names are shorter than NAMEDATALEN */
2831 : 37 : nlen = pg_mbcliplen(origname, strlen(origname),
2832 : 37 : NAMEDATALEN - 1 - strlen(nbuf));
2833 : 37 : memcpy(buf, origname, nlen);
2834 : 37 : strcpy(buf + nlen, nbuf);
2835 : 37 : curname = buf;
2836 : : }
2837 : :
2838 : : /* And attach to the result list */
2839 : 27943 : result = lappend(result, pstrdup(curname));
2840 : : }
2841 : 19945 : return result;
2842 : : }
2843 : :
2844 : : /*
2845 : : * ExecReindex
2846 : : *
2847 : : * Primary entry point for manual REINDEX commands. This is mainly a
2848 : : * preparation wrapper for the real operations that will happen in
2849 : : * each subroutine of REINDEX.
2850 : : */
2851 : : void
986 peter@eisentraut.org 2852 : 699 : ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
2853 : : {
1933 michael@paquier.xyz 2854 : 699 : ReindexParams params = {0};
2855 : : ListCell *lc;
1979 2856 : 699 : bool concurrently = false;
2857 : 699 : bool verbose = false;
1916 2858 : 699 : char *tablespacename = NULL;
2859 : :
2860 : : /* Parse option list */
1979 2861 [ + + + + : 1176 : foreach(lc, stmt->params)
+ + ]
2862 : : {
2863 : 477 : DefElem *opt = (DefElem *) lfirst(lc);
2864 : :
2865 [ + + ]: 477 : if (strcmp(opt->defname, "verbose") == 0)
2866 : 8 : verbose = defGetBoolean(opt);
2867 [ + + ]: 469 : else if (strcmp(opt->defname, "concurrently") == 0)
2868 : 388 : concurrently = defGetBoolean(opt);
1916 2869 [ + - ]: 81 : else if (strcmp(opt->defname, "tablespace") == 0)
2870 : 81 : tablespacename = defGetString(opt);
2871 : : else
1979 michael@paquier.xyz 2872 [ # # ]:UBC 0 : ereport(ERROR,
2873 : : (errcode(ERRCODE_SYNTAX_ERROR),
2874 : : errmsg("unrecognized %s option \"%s\"",
2875 : : "REINDEX", opt->defname),
2876 : : parser_errposition(pstate, opt->location)));
2877 : : }
2878 : :
1933 michael@paquier.xyz 2879 [ + + ]:CBC 699 : if (concurrently)
2880 : 388 : PreventInTransactionBlock(isTopLevel,
2881 : : "REINDEX CONCURRENTLY");
2882 : :
2883 : 682 : params.options =
1979 2884 : 1364 : (verbose ? REINDEXOPT_VERBOSE : 0) |
2885 [ + + ]: 682 : (concurrently ? REINDEXOPT_CONCURRENTLY : 0);
2886 : :
2887 : : /*
2888 : : * Assign the tablespace OID to move indexes to, with InvalidOid to do
2889 : : * nothing.
2890 : : */
1916 2891 [ + + ]: 682 : if (tablespacename != NULL)
2892 : : {
2893 : 81 : params.tablespaceOid = get_tablespace_oid(tablespacename, false);
2894 : :
2895 : : /* Check permissions except when moving to database's default */
2896 [ + - ]: 81 : if (OidIsValid(params.tablespaceOid) &&
2897 [ + - ]: 81 : params.tablespaceOid != MyDatabaseTableSpace)
2898 : : {
2899 : : AclResult aclresult;
2900 : :
1269 peter@eisentraut.org 2901 : 81 : aclresult = object_aclcheck(TableSpaceRelationId, params.tablespaceOid,
2902 : : GetUserId(), ACL_CREATE);
1916 michael@paquier.xyz 2903 [ + + ]: 81 : if (aclresult != ACLCHECK_OK)
2904 : 6 : aclcheck_error(aclresult, OBJECT_TABLESPACE,
2905 : 6 : get_tablespace_name(params.tablespaceOid));
2906 : : }
2907 : : }
2908 : : else
2909 : 601 : params.tablespaceOid = InvalidOid;
2910 : :
1933 2911 [ + + + - ]: 676 : switch (stmt->kind)
2912 : : {
2913 : 256 : case REINDEX_OBJECT_INDEX:
883 2914 : 256 : ReindexIndex(stmt, ¶ms, isTopLevel);
1933 2915 : 187 : break;
2916 : 310 : case REINDEX_OBJECT_TABLE:
883 2917 : 310 : ReindexTable(stmt, ¶ms, isTopLevel);
1933 2918 : 231 : break;
2919 : 110 : case REINDEX_OBJECT_SCHEMA:
2920 : : case REINDEX_OBJECT_SYSTEM:
2921 : : case REINDEX_OBJECT_DATABASE:
2922 : :
2923 : : /*
2924 : : * This cannot run inside a user transaction block; if we were
2925 : : * inside a transaction, then its commit- and
2926 : : * start-transaction-command calls would not have the intended
2927 : : * effect!
2928 : : */
2929 : 110 : PreventInTransactionBlock(isTopLevel,
2930 [ + + ]: 146 : (stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" :
2931 [ + + ]: 36 : (stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" :
2932 : : "REINDEX DATABASE");
883 2933 : 106 : ReindexMultipleTables(stmt, ¶ms);
1933 2934 : 73 : break;
1933 michael@paquier.xyz 2935 :UBC 0 : default:
2936 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d",
2937 : : (int) stmt->kind);
2938 : : break;
2939 : : }
1979 michael@paquier.xyz 2940 :CBC 491 : }
2941 : :
2942 : : /*
2943 : : * ReindexIndex
2944 : : * Recreate a specific index.
2945 : : */
2946 : : static void
883 2947 : 256 : ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
2948 : : {
2949 : 256 : const RangeVar *indexRelation = stmt->relation;
2950 : : struct ReindexIndexCallbackState state;
2951 : : Oid indOid;
2952 : : char persistence;
2953 : : char relkind;
2954 : :
2955 : : /*
2956 : : * Find and lock index, and check permissions on table; use callback to
2957 : : * obtain lock on table first, to avoid deadlock hazard. The lock level
2958 : : * used here must match the index lock obtained in reindex_index().
2959 : : *
2960 : : * If it's a temporary index, we will perform a non-concurrent reindex,
2961 : : * even if CONCURRENTLY was requested. In that case, reindex_index() will
2962 : : * upgrade the lock, but that's OK, because other sessions can't hold
2963 : : * locks on our temporary table.
2964 : : */
1933 2965 : 256 : state.params = *params;
2554 peter@eisentraut.org 2966 : 256 : state.locked_table_oid = InvalidOid;
2594 2967 : 256 : indOid = RangeVarGetRelidExtended(indexRelation,
1933 michael@paquier.xyz 2968 [ + + ]: 256 : (params->options & REINDEXOPT_CONCURRENTLY) != 0 ?
2969 : : ShareUpdateExclusiveLock : AccessExclusiveLock,
2970 : : 0,
2971 : : RangeVarCallbackForReindexIndex,
2972 : : &state);
2973 : :
2974 : : /*
2975 : : * Obtain the current persistence and kind of the existing index. We
2976 : : * already hold a lock on the index.
2977 : : */
2065 2978 : 225 : persistence = get_rel_persistence(indOid);
2979 : 225 : relkind = get_rel_relkind(indOid);
2980 : :
2981 [ + + ]: 225 : if (relkind == RELKIND_PARTITIONED_INDEX)
883 2982 : 24 : ReindexPartitions(stmt, indOid, params, isTopLevel);
1933 2983 [ + + + + ]: 201 : else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2984 : : persistence != RELPERSISTENCE_TEMP)
883 2985 : 118 : ReindexRelationConcurrently(stmt, indOid, params);
2986 : : else
2987 : : {
1933 2988 : 83 : ReindexParams newparams = *params;
2989 : :
2990 : 83 : newparams.options |= REINDEXOPT_REPORT_PROGRESS;
883 2991 : 83 : reindex_index(stmt, indOid, false, persistence, &newparams);
2992 : : }
5270 rhaas@postgresql.org 2993 : 187 : }
2994 : :
2995 : : /*
2996 : : * Check permissions on table before acquiring relation lock; also lock
2997 : : * the heap before the RangeVarGetRelidExtended takes the index lock, to avoid
2998 : : * deadlocks.
2999 : : */
3000 : : static void
3001 : 261 : RangeVarCallbackForReindexIndex(const RangeVar *relation,
3002 : : Oid relId, Oid oldRelId, void *arg)
3003 : : {
3004 : : char relkind;
2554 peter@eisentraut.org 3005 : 261 : struct ReindexIndexCallbackState *state = arg;
3006 : : LOCKMODE table_lockmode;
3007 : : Oid table_oid;
3008 : : AclResult aclresult;
3009 : :
3010 : : /*
3011 : : * Lock level here should match table lock in reindex_index() for
3012 : : * non-concurrent case and table locks used by index_concurrently_*() for
3013 : : * concurrent case.
3014 : : */
1933 michael@paquier.xyz 3015 : 522 : table_lockmode = (state->params.options & REINDEXOPT_CONCURRENTLY) != 0 ?
2069 3016 [ + + ]: 261 : ShareUpdateExclusiveLock : ShareLock;
3017 : :
3018 : : /*
3019 : : * If we previously locked some other index's heap, and the name we're
3020 : : * looking up no longer refers to that relation, release the now-useless
3021 : : * lock.
3022 : : */
5270 rhaas@postgresql.org 3023 [ + + + + ]: 261 : if (relId != oldRelId && OidIsValid(oldRelId))
3024 : : {
2554 peter@eisentraut.org 3025 : 3 : UnlockRelationOid(state->locked_table_oid, table_lockmode);
3026 : 3 : state->locked_table_oid = InvalidOid;
3027 : : }
3028 : :
3029 : : /* If the relation does not exist, there's nothing more to do. */
5270 rhaas@postgresql.org 3030 [ + + ]: 261 : if (!OidIsValid(relId))
3031 : 7 : return;
3032 : :
3033 : : /* If the relation does exist, check whether it's an index. */
3034 : 254 : relkind = get_rel_relkind(relId);
3028 alvherre@alvh.no-ip. 3035 [ + + + + ]: 254 : if (relkind != RELKIND_INDEX &&
3036 : : relkind != RELKIND_PARTITIONED_INDEX)
8325 tgl@sss.pgh.pa.us 3037 [ + - ]: 16 : ereport(ERROR,
3038 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3039 : : errmsg("\"%s\" is not an index", relation->relname)));
3040 : :
3041 : : /* Look up the index's table. */
202 nathan@postgresql.or 3042 :GNC 238 : table_oid = IndexGetRelation(relId, false);
3043 : :
3044 : : /*
3045 : : * In the unlikely event that, upon retry, we get the same index OID with
3046 : : * a different table OID, fail. RangeVarGetRelidExtended() will have
3047 : : * already locked the index in this case, and it won't retry again, so we
3048 : : * can't lock the newly discovered table OID without risking deadlock.
3049 : : * Also, while this corner case is indeed possible, it is extremely
3050 : : * unlikely to happen in practice, so it's probably not worth any more
3051 : : * effort than this.
3052 : : */
3053 [ + + - + ]: 238 : if (relId == oldRelId && table_oid != state->locked_table_oid)
202 nathan@postgresql.or 3054 [ # # ]:UNC 0 : ereport(ERROR,
3055 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3056 : : errmsg("index \"%s\" was concurrently dropped",
3057 : : relation->relname)));
3058 : :
3059 : : /* Check permissions. */
202 nathan@postgresql.or 3060 :GNC 238 : aclresult = pg_class_aclcheck(table_oid, GetUserId(), ACL_MAINTAIN);
3061 [ + + ]: 238 : if (aclresult != ACLCHECK_OK)
3062 : 8 : aclcheck_error(aclresult, OBJECT_INDEX, relation->relname);
3063 : :
3064 : : /* Lock heap before index to avoid deadlock. */
5270 rhaas@postgresql.org 3065 [ + + ]:CBC 230 : if (relId != oldRelId)
3066 : : {
202 nathan@postgresql.or 3067 :GNC 228 : LockRelationOid(table_oid, table_lockmode);
3068 : 228 : state->locked_table_oid = table_oid;
3069 : : }
3070 : : }
3071 : :
3072 : : /*
3073 : : * ReindexTable
3074 : : * Recreate all indexes of a table (and of its toast table, if any)
3075 : : */
3076 : : static Oid
883 michael@paquier.xyz 3077 :CBC 310 : ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
3078 : : {
3079 : : Oid heapOid;
3080 : : bool result;
3081 : 310 : const RangeVar *relation = stmt->relation;
3082 : :
3083 : : /*
3084 : : * The lock level used here should match reindex_relation().
3085 : : *
3086 : : * If it's a temporary table, we will perform a non-concurrent reindex,
3087 : : * even if CONCURRENTLY was requested. In that case, reindex_relation()
3088 : : * will upgrade the lock, but that's OK, because other sessions can't hold
3089 : : * locks on our temporary table.
3090 : : */
2594 peter@eisentraut.org 3091 : 310 : heapOid = RangeVarGetRelidExtended(relation,
1933 michael@paquier.xyz 3092 [ + + ]: 310 : (params->options & REINDEXOPT_CONCURRENTLY) != 0 ?
3093 : : ShareUpdateExclusiveLock : ShareLock,
3094 : : 0,
3095 : : RangeVarCallbackMaintainsTable, NULL);
3096 : :
2065 3097 [ + + ]: 281 : if (get_rel_relkind(heapOid) == RELKIND_PARTITIONED_TABLE)
883 3098 : 47 : ReindexPartitions(stmt, heapOid, params, isTopLevel);
1933 3099 [ + + + + ]: 379 : else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2065 3100 : 145 : get_rel_persistence(heapOid) != RELPERSISTENCE_TEMP)
3101 : : {
883 3102 : 137 : result = ReindexRelationConcurrently(stmt, heapOid, params);
3103 : :
2526 drowley@postgresql.o 3104 [ + + ]: 112 : if (!result)
3105 [ + - ]: 12 : ereport(NOTICE,
3106 : : (errmsg("table \"%s\" has no indexes that can be reindexed concurrently",
3107 : : relation->relname)));
3108 : : }
3109 : : else
3110 : : {
1933 michael@paquier.xyz 3111 : 97 : ReindexParams newparams = *params;
3112 : :
3113 : 97 : newparams.options |= REINDEXOPT_REPORT_PROGRESS;
883 3114 : 97 : result = reindex_relation(stmt, heapOid,
3115 : : REINDEX_REL_PROCESS_TOAST |
3116 : : REINDEX_REL_CHECK_CONSTRAINTS,
3117 : : &newparams);
2526 drowley@postgresql.o 3118 [ + + ]: 76 : if (!result)
3119 [ + - ]: 8 : ereport(NOTICE,
3120 : : (errmsg("table \"%s\" has no indexes to reindex",
3121 : : relation->relname)));
3122 : : }
3123 : :
4875 rhaas@postgresql.org 3124 : 231 : return heapOid;
3125 : : }
3126 : :
3127 : : /*
3128 : : * ReindexMultipleTables
3129 : : * Recreate indexes of tables selected by objectName/objectKind.
3130 : : *
3131 : : * To reduce the probability of deadlocks, each table is reindexed in a
3132 : : * separate transaction, so we can release the lock on it right away.
3133 : : * That means this must not be called within a user transaction block!
3134 : : */
3135 : : static void
883 michael@paquier.xyz 3136 : 106 : ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params)
3137 : : {
3138 : :
3139 : : Oid objectOid;
3140 : : Relation relationRelation;
3141 : : TableScanDesc scan;
3142 : : ScanKeyData scan_keys[1];
3143 : : HeapTuple tuple;
3144 : : MemoryContext private_context;
3145 : : MemoryContext old;
7919 bruce@momjian.us 3146 : 106 : List *relids = NIL;
3147 : : int num_keys;
2594 peter@eisentraut.org 3148 : 106 : bool concurrent_warning = false;
1916 michael@paquier.xyz 3149 : 106 : bool tablespace_warning = false;
883 3150 : 106 : const char *objectName = stmt->name;
3151 : 106 : const ReindexObjectType objectKind = stmt->kind;
3152 : :
4165 simon@2ndQuadrant.co 3153 [ + + + + : 106 : Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
- + ]
3154 : : objectKind == REINDEX_OBJECT_SYSTEM ||
3155 : : objectKind == REINDEX_OBJECT_DATABASE);
3156 : :
3157 : : /*
3158 : : * This matches the options enforced by the grammar, where the object name
3159 : : * is optional for DATABASE and SYSTEM.
3160 : : */
1285 peter@eisentraut.org 3161 [ + + - + ]: 106 : Assert(objectName || objectKind != REINDEX_OBJECT_SCHEMA);
3162 : :
2069 michael@paquier.xyz 3163 [ + + ]: 106 : if (objectKind == REINDEX_OBJECT_SYSTEM &&
1933 3164 [ + + ]: 20 : (params->options & REINDEXOPT_CONCURRENTLY) != 0)
2594 peter@eisentraut.org 3165 [ + - ]: 13 : ereport(ERROR,
3166 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3167 : : errmsg("cannot reindex system catalogs concurrently")));
3168 : :
3169 : : /*
3170 : : * Get OID of object to reindex, being the database currently being used
3171 : : * by session for a database or for system catalogs, or the schema defined
3172 : : * by caller. At the same time do permission checks that need different
3173 : : * processing depending on the object type.
3174 : : */
4165 simon@2ndQuadrant.co 3175 [ + + ]: 93 : if (objectKind == REINDEX_OBJECT_SCHEMA)
3176 : : {
3177 : 70 : objectOid = get_namespace_oid(objectName, false);
3178 : :
783 nathan@postgresql.or 3179 [ + + ]: 66 : if (!object_ownercheck(NamespaceRelationId, objectOid, GetUserId()) &&
3180 [ + + ]: 16 : !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3076 peter_e@gmx.net 3181 : 12 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SCHEMA,
3182 : : objectName);
3183 : : }
3184 : : else
3185 : : {
4165 simon@2ndQuadrant.co 3186 : 23 : objectOid = MyDatabaseId;
3187 : :
1386 michael@paquier.xyz 3188 [ + - + + ]: 23 : if (objectName && strcmp(objectName, get_database_name(objectOid)) != 0)
4165 simon@2ndQuadrant.co 3189 [ + - ]: 4 : ereport(ERROR,
3190 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3191 : : errmsg("can only reindex the currently open database")));
783 nathan@postgresql.or 3192 [ - + ]: 19 : if (!object_ownercheck(DatabaseRelationId, objectOid, GetUserId()) &&
783 nathan@postgresql.or 3193 [ # # ]:UBC 0 : !has_privs_of_role(GetUserId(), ROLE_PG_MAINTAIN))
3076 peter_e@gmx.net 3194 : 0 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE,
1386 michael@paquier.xyz 3195 : 0 : get_database_name(objectOid));
3196 : : }
3197 : :
3198 : : /*
3199 : : * Create a memory context that will survive forced transaction commits we
3200 : : * do below. Since it is a child of PortalContext, it will go away
3201 : : * eventually even if we suffer an error; there's no need for special
3202 : : * abort cleanup logic.
3203 : : */
8404 tgl@sss.pgh.pa.us 3204 :CBC 73 : private_context = AllocSetContextCreate(PortalContext,
3205 : : "ReindexMultipleTables",
3206 : : ALLOCSET_SMALL_SIZES);
3207 : :
3208 : : /*
3209 : : * Define the search keys to find the objects to reindex. For a schema, we
3210 : : * select target relations using relnamespace, something not necessary for
3211 : : * a database-wide operation.
3212 : : */
4165 simon@2ndQuadrant.co 3213 [ + + ]: 73 : if (objectKind == REINDEX_OBJECT_SCHEMA)
3214 : : {
4163 3215 : 54 : num_keys = 1;
4165 3216 : 54 : ScanKeyInit(&scan_keys[0],
3217 : : Anum_pg_class_relnamespace,
3218 : : BTEqualStrategyNumber, F_OIDEQ,
3219 : : ObjectIdGetDatum(objectOid));
3220 : : }
3221 : : else
3222 : 19 : num_keys = 0;
3223 : :
3224 : : /*
3225 : : * Scan pg_class to build a list of the relations we need to reindex.
3226 : : *
3227 : : * We only consider plain relations and materialized views here (toast
3228 : : * rels will be processed indirectly by reindex_relation).
3229 : : */
2661 andres@anarazel.de 3230 : 73 : relationRelation = table_open(RelationRelationId, AccessShareLock);
2612 3231 : 73 : scan = table_beginscan_catalog(relationRelation, num_keys, scan_keys);
8751 tgl@sss.pgh.pa.us 3232 [ + + ]: 10413 : while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
3233 : : {
8259 3234 : 10340 : Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
2723 andres@anarazel.de 3235 : 10340 : Oid relid = classtuple->oid;
3236 : :
3237 : : /*
3238 : : * Only regular tables and matviews can have indexes, so ignore any
3239 : : * other kind of relation.
3240 : : *
3241 : : * Partitioned tables/indexes are skipped but matching leaf partitions
3242 : : * are processed.
3243 : : */
4811 kgrittn@postgresql.o 3244 [ + + ]: 10340 : if (classtuple->relkind != RELKIND_RELATION &&
3245 [ + + ]: 8493 : classtuple->relkind != RELKIND_MATVIEW)
8259 tgl@sss.pgh.pa.us 3246 : 8481 : continue;
3247 : :
3248 : : /* Skip temp tables of other backends; we can't reindex them at all */
5622 rhaas@postgresql.org 3249 [ + + ]: 1859 : if (classtuple->relpersistence == RELPERSISTENCE_TEMP &&
6244 tgl@sss.pgh.pa.us 3250 [ - + ]: 24 : !isTempNamespace(classtuple->relnamespace))
6812 alvherre@alvh.no-ip. 3251 :UBC 0 : continue;
3252 : :
3253 : : /*
3254 : : * Check user/system classification. SYSTEM processes all the
3255 : : * catalogs, and DATABASE processes everything that's not a catalog.
3256 : : */
4076 tgl@sss.pgh.pa.us 3257 [ + + ]:CBC 1859 : if (objectKind == REINDEX_OBJECT_SYSTEM &&
1386 michael@paquier.xyz 3258 [ + + ]: 527 : !IsCatalogRelationOid(relid))
3259 : 44 : continue;
3260 [ + + + + ]: 2698 : else if (objectKind == REINDEX_OBJECT_DATABASE &&
3261 : 883 : IsCatalogRelationOid(relid))
4165 simon@2ndQuadrant.co 3262 : 828 : continue;
3263 : :
3264 : : /*
3265 : : * We already checked privileges on the database or schema, but we
3266 : : * further restrict reindexing shared catalogs to roles with the
3267 : : * MAINTAIN privilege on the relation.
3268 : : */
2826 michael@paquier.xyz 3269 [ + + - + ]: 1119 : if (classtuple->relisshared &&
783 nathan@postgresql.or 3270 : 132 : pg_class_aclcheck(relid, GetUserId(), ACL_MAINTAIN) != ACLCHECK_OK)
2826 michael@paquier.xyz 3271 :UBC 0 : continue;
3272 : :
3273 : : /*
3274 : : * Skip system tables, since index_create() would reject indexing them
3275 : : * concurrently (and it would likely fail if we tried).
3276 : : */
1933 michael@paquier.xyz 3277 [ + + + + ]:CBC 1325 : if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
2554 tgl@sss.pgh.pa.us 3278 : 338 : IsCatalogRelationOid(relid))
3279 : : {
2594 peter@eisentraut.org 3280 [ + + ]: 276 : if (!concurrent_warning)
3281 [ + - ]: 4 : ereport(WARNING,
3282 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3283 : : errmsg("cannot reindex system catalogs concurrently, skipping all")));
3284 : 276 : concurrent_warning = true;
3285 : 276 : continue;
3286 : : }
3287 : :
3288 : : /*
3289 : : * If a new tablespace is set, check if this relation has to be
3290 : : * skipped.
3291 : : */
1916 michael@paquier.xyz 3292 [ - + ]: 711 : if (OidIsValid(params->tablespaceOid))
3293 : : {
1916 michael@paquier.xyz 3294 :UBC 0 : bool skip_rel = false;
3295 : :
3296 : : /*
3297 : : * Mapped relations cannot be moved to different tablespaces (in
3298 : : * particular this eliminates all shared catalogs.).
3299 : : */
3300 [ # # # # : 0 : if (RELKIND_HAS_STORAGE(classtuple->relkind) &&
# # # # #
# ]
1399 rhaas@postgresql.org 3301 [ # # ]: 0 : !RelFileNumberIsValid(classtuple->relfilenode))
1916 michael@paquier.xyz 3302 : 0 : skip_rel = true;
3303 : :
3304 : : /*
3305 : : * A system relation is always skipped, even with
3306 : : * allow_system_table_mods enabled.
3307 : : */
3308 [ # # ]: 0 : if (IsSystemClass(relid, classtuple))
3309 : 0 : skip_rel = true;
3310 : :
3311 [ # # ]: 0 : if (skip_rel)
3312 : : {
3313 [ # # ]: 0 : if (!tablespace_warning)
3314 [ # # ]: 0 : ereport(WARNING,
3315 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3316 : : errmsg("cannot move system relations, skipping all")));
3317 : 0 : tablespace_warning = true;
3318 : 0 : continue;
3319 : : }
3320 : : }
3321 : :
3322 : : /* Save the list of relation OIDs in private context */
4076 tgl@sss.pgh.pa.us 3323 :CBC 711 : old = MemoryContextSwitchTo(private_context);
3324 : :
3325 : : /*
3326 : : * We always want to reindex pg_class first if it's selected to be
3327 : : * reindexed. This ensures that if there is any corruption in
3328 : : * pg_class' indexes, they will be fixed before we process any other
3329 : : * tables. This is critical because reindexing itself will try to
3330 : : * update pg_class.
3331 : : */
3332 [ + + ]: 711 : if (relid == RelationRelationId)
3333 : 8 : relids = lcons_oid(relid, relids);
3334 : : else
3335 : 703 : relids = lappend_oid(relids, relid);
3336 : :
8259 3337 : 711 : MemoryContextSwitchTo(old);
3338 : : }
2612 andres@anarazel.de 3339 : 73 : table_endscan(scan);
2661 3340 : 73 : table_close(relationRelation, AccessShareLock);
3341 : :
3342 : : /*
3343 : : * Process each relation listed in a separate transaction. Note that this
3344 : : * commits and then starts a new transaction immediately.
3345 : : */
883 michael@paquier.xyz 3346 : 73 : ReindexMultipleInternal(stmt, relids, params);
3347 : :
2065 3348 : 73 : MemoryContextDelete(private_context);
3349 : 73 : }
3350 : :
3351 : : /*
3352 : : * Error callback specific to ReindexPartitions().
3353 : : */
3354 : : static void
3355 : 8 : reindex_error_callback(void *arg)
3356 : : {
3357 : 8 : ReindexErrorInfo *errinfo = (ReindexErrorInfo *) arg;
3358 : :
1614 peter@eisentraut.org 3359 [ + + - + ]: 8 : Assert(RELKIND_HAS_PARTITIONS(errinfo->relkind));
3360 : :
2065 michael@paquier.xyz 3361 [ + + ]: 8 : if (errinfo->relkind == RELKIND_PARTITIONED_TABLE)
3362 : 4 : errcontext("while reindexing partitioned table \"%s.%s\"",
3363 : : errinfo->relnamespace, errinfo->relname);
3364 [ + - ]: 4 : else if (errinfo->relkind == RELKIND_PARTITIONED_INDEX)
3365 : 4 : errcontext("while reindexing partitioned index \"%s.%s\"",
3366 : : errinfo->relnamespace, errinfo->relname);
3367 : 8 : }
3368 : :
3369 : : /*
3370 : : * ReindexPartitions
3371 : : *
3372 : : * Reindex a set of partitions, per the partitioned index or table given
3373 : : * by the caller.
3374 : : */
3375 : : static void
883 3376 : 71 : ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel)
3377 : : {
2065 3378 : 71 : List *partitions = NIL;
3379 : 71 : char relkind = get_rel_relkind(relid);
3380 : 71 : char *relname = get_rel_name(relid);
3381 : 71 : char *relnamespace = get_namespace_name(get_rel_namespace(relid));
3382 : : MemoryContext reindex_context;
3383 : : List *inhoids;
3384 : : ListCell *lc;
3385 : : ErrorContextCallback errcallback;
3386 : : ReindexErrorInfo errinfo;
3387 : :
1614 peter@eisentraut.org 3388 [ + + - + ]: 71 : Assert(RELKIND_HAS_PARTITIONS(relkind));
3389 : :
3390 : : /*
3391 : : * Check if this runs in a transaction block, with an error callback to
3392 : : * provide more context under which a problem happens.
3393 : : */
2065 michael@paquier.xyz 3394 : 71 : errinfo.relname = pstrdup(relname);
3395 : 71 : errinfo.relnamespace = pstrdup(relnamespace);
3396 : 71 : errinfo.relkind = relkind;
3397 : 71 : errcallback.callback = reindex_error_callback;
523 peter@eisentraut.org 3398 : 71 : errcallback.arg = &errinfo;
2065 michael@paquier.xyz 3399 : 71 : errcallback.previous = error_context_stack;
3400 : 71 : error_context_stack = &errcallback;
3401 : :
3402 [ + + ]: 71 : PreventInTransactionBlock(isTopLevel,
3403 : : relkind == RELKIND_PARTITIONED_TABLE ?
3404 : : "REINDEX TABLE" : "REINDEX INDEX");
3405 : :
3406 : : /* Pop the error context stack */
3407 : 63 : error_context_stack = errcallback.previous;
3408 : :
3409 : : /*
3410 : : * Create special memory context for cross-transaction storage.
3411 : : *
3412 : : * Since it is a child of PortalContext, it will go away eventually even
3413 : : * if we suffer an error so there is no need for special abort cleanup
3414 : : * logic.
3415 : : */
3416 : 63 : reindex_context = AllocSetContextCreate(PortalContext, "Reindex",
3417 : : ALLOCSET_DEFAULT_SIZES);
3418 : :
3419 : : /* ShareLock is enough to prevent schema modifications */
3420 : 63 : inhoids = find_all_inheritors(relid, ShareLock, NULL);
3421 : :
3422 : : /*
3423 : : * The list of relations to reindex are the physical partitions of the
3424 : : * tree so discard any partitioned table or index.
3425 : : */
3426 [ + - + + : 245 : foreach(lc, inhoids)
+ + ]
3427 : : {
3428 : 182 : Oid partoid = lfirst_oid(lc);
3429 : 182 : char partkind = get_rel_relkind(partoid);
3430 : : MemoryContext old_context;
3431 : :
3432 : : /*
3433 : : * This discards partitioned tables, partitioned indexes and foreign
3434 : : * tables.
3435 : : */
3436 [ + + + + : 182 : if (!RELKIND_HAS_STORAGE(partkind))
+ - + - +
- ]
3437 : 105 : continue;
3438 : :
3439 [ + + - + ]: 77 : Assert(partkind == RELKIND_INDEX ||
3440 : : partkind == RELKIND_RELATION);
3441 : :
3442 : : /* Save partition OID */
3443 : 77 : old_context = MemoryContextSwitchTo(reindex_context);
3444 : 77 : partitions = lappend_oid(partitions, partoid);
3445 : 77 : MemoryContextSwitchTo(old_context);
3446 : : }
3447 : :
3448 : : /*
3449 : : * Process each partition listed in a separate transaction. Note that
3450 : : * this commits and then starts a new transaction immediately.
3451 : : */
883 3452 : 63 : ReindexMultipleInternal(stmt, partitions, params);
3453 : :
3454 : : /*
3455 : : * Clean up working storage --- note we must do this after
3456 : : * StartTransactionCommand, else we might be trying to delete the active
3457 : : * context!
3458 : : */
2065 3459 : 63 : MemoryContextDelete(reindex_context);
3460 : 63 : }
3461 : :
3462 : : /*
3463 : : * ReindexMultipleInternal
3464 : : *
3465 : : * Reindex a list of relations, each one being processed in its own
3466 : : * transaction. This commits the existing transaction immediately,
3467 : : * and starts a new transaction when finished.
3468 : : */
3469 : : static void
883 3470 : 136 : ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params)
3471 : : {
3472 : : ListCell *l;
3473 : :
6567 alvherre@alvh.no-ip. 3474 : 136 : PopActiveSnapshot();
8392 tgl@sss.pgh.pa.us 3475 : 136 : CommitTransactionCommand();
3476 : :
8014 neilc@samurai.com 3477 [ + + + + : 924 : foreach(l, relids)
+ + ]
3478 : : {
7919 bruce@momjian.us 3479 : 788 : Oid relid = lfirst_oid(l);
3480 : : char relkind;
3481 : : char relpersistence;
3482 : :
8392 tgl@sss.pgh.pa.us 3483 : 788 : StartTransactionCommand();
3484 : :
3485 : : /* functions in indexes may want a snapshot set */
6567 alvherre@alvh.no-ip. 3486 : 788 : PushActiveSnapshot(GetTransactionSnapshot());
3487 : :
3488 : : /* check if the relation still exists */
2071 michael@paquier.xyz 3489 [ + + ]: 788 : if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid)))
3490 : : {
3491 : 2 : PopActiveSnapshot();
3492 : 2 : CommitTransactionCommand();
3493 : 2 : continue;
3494 : : }
3495 : :
3496 : : /*
3497 : : * Check permissions except when moving to database's default if a new
3498 : : * tablespace is chosen. Note that this check also happens in
3499 : : * ExecReindex(), but we do an extra check here as this runs across
3500 : : * multiple transactions.
3501 : : */
1916 3502 [ + + ]: 786 : if (OidIsValid(params->tablespaceOid) &&
3503 [ + - ]: 8 : params->tablespaceOid != MyDatabaseTableSpace)
3504 : : {
3505 : : AclResult aclresult;
3506 : :
1269 peter@eisentraut.org 3507 : 8 : aclresult = object_aclcheck(TableSpaceRelationId, params->tablespaceOid,
3508 : : GetUserId(), ACL_CREATE);
1916 michael@paquier.xyz 3509 [ - + ]: 8 : if (aclresult != ACLCHECK_OK)
1916 michael@paquier.xyz 3510 :UBC 0 : aclcheck_error(aclresult, OBJECT_TABLESPACE,
3511 : 0 : get_tablespace_name(params->tablespaceOid));
3512 : : }
3513 : :
2065 michael@paquier.xyz 3514 :CBC 786 : relkind = get_rel_relkind(relid);
3515 : 786 : relpersistence = get_rel_persistence(relid);
3516 : :
3517 : : /*
3518 : : * Partitioned tables and indexes can never be processed directly, and
3519 : : * a list of their leaves should be built first.
3520 : : */
1614 peter@eisentraut.org 3521 [ + - - + ]: 786 : Assert(!RELKIND_HAS_PARTITIONS(relkind));
3522 : :
1933 michael@paquier.xyz 3523 [ + + + + ]: 786 : if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
3524 : : relpersistence != RELPERSISTENCE_TEMP)
2594 peter@eisentraut.org 3525 : 81 : {
1933 michael@paquier.xyz 3526 : 81 : ReindexParams newparams = *params;
3527 : :
3528 : 81 : newparams.options |= REINDEXOPT_MISSING_OK;
883 3529 : 81 : (void) ReindexRelationConcurrently(stmt, relid, &newparams);
880 3530 [ + + ]: 81 : if (ActiveSnapshotSet())
3531 : 17 : PopActiveSnapshot();
3532 : : /* ReindexRelationConcurrently() does the verbose output */
3533 : : }
2065 3534 [ + + ]: 705 : else if (relkind == RELKIND_INDEX)
3535 : : {
1933 3536 : 12 : ReindexParams newparams = *params;
3537 : :
3538 : 12 : newparams.options |=
3539 : : REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK;
883 3540 : 12 : reindex_index(stmt, relid, false, relpersistence, &newparams);
2065 3541 : 12 : PopActiveSnapshot();
3542 : : /* reindex_index() does the verbose output */
3543 : : }
3544 : : else
3545 : : {
3546 : : bool result;
1933 3547 : 693 : ReindexParams newparams = *params;
3548 : :
3549 : 693 : newparams.options |=
3550 : : REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK;
883 3551 : 693 : result = reindex_relation(stmt, relid,
3552 : : REINDEX_REL_PROCESS_TOAST |
3553 : : REINDEX_REL_CHECK_CONSTRAINTS,
3554 : : &newparams);
3555 : :
1933 3556 [ + + - + ]: 693 : if (result && (params->options & REINDEXOPT_VERBOSE) != 0)
4008 fujii@postgresql.org 3557 [ # # ]:UBC 0 : ereport(INFO,
3558 : : (errmsg("table \"%s.%s\" was reindexed",
3559 : : get_namespace_name(get_rel_namespace(relid)),
3560 : : get_rel_name(relid))));
3561 : :
2594 peter@eisentraut.org 3562 :CBC 693 : PopActiveSnapshot();
3563 : : }
3564 : :
3565 : 786 : CommitTransactionCommand();
3566 : : }
3567 : :
2065 michael@paquier.xyz 3568 : 136 : StartTransactionCommand();
2594 peter@eisentraut.org 3569 : 136 : }
3570 : :
3571 : :
3572 : : /*
3573 : : * ReindexRelationConcurrently - process REINDEX CONCURRENTLY for given
3574 : : * relation OID
3575 : : *
3576 : : * 'relationOid' can either belong to an index, a table or a materialized
3577 : : * view. For tables and materialized views, all its indexes will be rebuilt,
3578 : : * excluding invalid indexes and any indexes used in exclusion constraints,
3579 : : * but including its associated toast table indexes. For indexes, the index
3580 : : * itself will be rebuilt.
3581 : : *
3582 : : * The locks taken on parent tables and involved indexes are kept until the
3583 : : * transaction is committed, at which point a session lock is taken on each
3584 : : * relation. Both of these protect against concurrent schema changes.
3585 : : *
3586 : : * Returns true if any indexes have been rebuilt (including toast table's
3587 : : * indexes, when relevant), otherwise returns false.
3588 : : *
3589 : : * NOTE: This cannot be used on temporary relations. A concurrent build would
3590 : : * cause issues with ON COMMIT actions triggered by the transactions of the
3591 : : * concurrent build. Temporary relations are not subject to concurrent
3592 : : * concerns, so there's no need for the more complicated concurrent build,
3593 : : * anyway, and a non-concurrent reindex is more efficient.
3594 : : */
3595 : : static bool
883 michael@paquier.xyz 3596 : 336 : ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params)
3597 : : {
3598 : : typedef struct ReindexIndexInfo
3599 : : {
3600 : : Oid indexId;
3601 : : Oid tableId;
3602 : : Oid amId;
3603 : : bool safe; /* for set_indexsafe_procflags */
3604 : : } ReindexIndexInfo;
2594 peter@eisentraut.org 3605 : 336 : List *heapRelationIds = NIL;
3606 : 336 : List *indexIds = NIL;
3607 : 336 : List *newIndexIds = NIL;
3608 : 336 : List *relationLocks = NIL;
3609 : 336 : List *lockTags = NIL;
3610 : : ListCell *lc,
3611 : : *lc2;
3612 : : MemoryContext private_context;
3613 : : MemoryContext oldcontext;
3614 : : char relkind;
3615 : 336 : char *relationName = NULL;
3616 : 336 : char *relationNamespace = NULL;
3617 : : PGRUsage ru0;
2044 michael@paquier.xyz 3618 : 336 : const int progress_index[] = {
3619 : : PROGRESS_CREATEIDX_COMMAND,
3620 : : PROGRESS_CREATEIDX_PHASE,
3621 : : PROGRESS_CREATEIDX_INDEX_OID,
3622 : : PROGRESS_CREATEIDX_ACCESS_METHOD_OID
3623 : : };
3624 : : int64 progress_vals[4];
3625 : :
3626 : : /*
3627 : : * Create a memory context that will survive forced transaction commits we
3628 : : * do below. Since it is a child of PortalContext, it will go away
3629 : : * eventually even if we suffer an error; there's no need for special
3630 : : * abort cleanup logic.
3631 : : */
2594 peter@eisentraut.org 3632 : 336 : private_context = AllocSetContextCreate(PortalContext,
3633 : : "ReindexConcurrent",
3634 : : ALLOCSET_SMALL_SIZES);
3635 : :
1933 michael@paquier.xyz 3636 [ + + ]: 336 : if ((params->options & REINDEXOPT_VERBOSE) != 0)
3637 : : {
3638 : : /* Save data needed by REINDEX VERBOSE in private context */
2594 peter@eisentraut.org 3639 : 2 : oldcontext = MemoryContextSwitchTo(private_context);
3640 : :
3641 : 2 : relationName = get_rel_name(relationOid);
3642 : 2 : relationNamespace = get_namespace_name(get_rel_namespace(relationOid));
3643 : :
3644 : 2 : pg_rusage_init(&ru0);
3645 : :
3646 : 2 : MemoryContextSwitchTo(oldcontext);
3647 : : }
3648 : :
3649 : 336 : relkind = get_rel_relkind(relationOid);
3650 : :
3651 : : /*
3652 : : * Extract the list of indexes that are going to be rebuilt based on the
3653 : : * relation Oid given by caller.
3654 : : */
3655 [ + + - ]: 336 : switch (relkind)
3656 : : {
3657 : 202 : case RELKIND_RELATION:
3658 : : case RELKIND_MATVIEW:
3659 : : case RELKIND_TOASTVALUE:
3660 : : {
3661 : : /*
3662 : : * In the case of a relation, find all its indexes including
3663 : : * toast indexes.
3664 : : */
3665 : : Relation heapRelation;
3666 : :
3667 : : /* Save the list of relation OIDs in private context */
3668 : 202 : oldcontext = MemoryContextSwitchTo(private_context);
3669 : :
3670 : : /* Track this relation for session locks */
3671 : 202 : heapRelationIds = lappend_oid(heapRelationIds, relationOid);
3672 : :
3673 : 202 : MemoryContextSwitchTo(oldcontext);
3674 : :
2552 michael@paquier.xyz 3675 [ + + ]: 202 : if (IsCatalogRelationOid(relationOid))
3676 [ + - ]: 24 : ereport(ERROR,
3677 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3678 : : errmsg("cannot reindex system catalogs concurrently")));
3679 : :
3680 : : /* Open relation to get its indexes */
1933 3681 [ + + ]: 178 : if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3682 : : {
2071 3683 : 65 : heapRelation = try_table_open(relationOid,
3684 : : ShareUpdateExclusiveLock);
3685 : : /* leave if relation does not exist */
3686 [ - + ]: 65 : if (!heapRelation)
2071 michael@paquier.xyz 3687 :UBC 0 : break;
3688 : : }
3689 : : else
2071 michael@paquier.xyz 3690 :CBC 113 : heapRelation = table_open(relationOid,
3691 : : ShareUpdateExclusiveLock);
3692 : :
1916 3693 [ + + + + ]: 192 : if (OidIsValid(params->tablespaceOid) &&
3694 : 14 : IsSystemRelation(heapRelation))
3695 [ + - ]: 1 : ereport(ERROR,
3696 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3697 : : errmsg("cannot move system relation \"%s\"",
3698 : : RelationGetRelationName(heapRelation))));
3699 : :
3700 : : /* Add all the valid indexes of relation to list */
2594 peter@eisentraut.org 3701 [ + + + + : 344 : foreach(lc, RelationGetIndexList(heapRelation))
+ + ]
3702 : : {
3703 : 167 : Oid cellOid = lfirst_oid(lc);
3704 : 167 : Relation indexRelation = index_open(cellOid,
3705 : : ShareUpdateExclusiveLock);
3706 : :
3707 [ + + ]: 167 : if (!indexRelation->rd_index->indisvalid)
3708 [ + - ]: 4 : ereport(WARNING,
3709 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3710 : : errmsg("skipping reindex of invalid index \"%s.%s\"",
3711 : : get_namespace_name(get_rel_namespace(cellOid)),
3712 : : get_rel_name(cellOid)),
3713 : : errhint("Use DROP INDEX or REINDEX INDEX.")));
3714 [ + + ]: 163 : else if (indexRelation->rd_index->indisexclusion)
3715 [ + - ]: 4 : ereport(WARNING,
3716 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3717 : : errmsg("cannot reindex exclusion constraint index \"%s.%s\" concurrently, skipping",
3718 : : get_namespace_name(get_rel_namespace(cellOid)),
3719 : : get_rel_name(cellOid))));
3720 : : else
3721 : : {
3722 : : ReindexIndexInfo *idx;
3723 : :
3724 : : /* Save the list of relation OIDs in private context */
3725 : 159 : oldcontext = MemoryContextSwitchTo(private_context);
3726 : :
1331 3727 : 159 : idx = palloc_object(ReindexIndexInfo);
1939 alvherre@alvh.no-ip. 3728 : 159 : idx->indexId = cellOid;
3729 : : /* other fields set later */
3730 : :
3731 : 159 : indexIds = lappend(indexIds, idx);
3732 : :
2594 peter@eisentraut.org 3733 : 159 : MemoryContextSwitchTo(oldcontext);
3734 : : }
3735 : :
3736 : 167 : index_close(indexRelation, NoLock);
3737 : : }
3738 : :
3739 : : /* Also add the toast indexes */
3740 [ + + ]: 177 : if (OidIsValid(heapRelation->rd_rel->reltoastrelid))
3741 : : {
3742 : 51 : Oid toastOid = heapRelation->rd_rel->reltoastrelid;
3743 : 51 : Relation toastRelation = table_open(toastOid,
3744 : : ShareUpdateExclusiveLock);
3745 : :
3746 : : /* Save the list of relation OIDs in private context */
3747 : 51 : oldcontext = MemoryContextSwitchTo(private_context);
3748 : :
3749 : : /* Track this relation for session locks */
3750 : 51 : heapRelationIds = lappend_oid(heapRelationIds, toastOid);
3751 : :
3752 : 51 : MemoryContextSwitchTo(oldcontext);
3753 : :
3754 [ + - + + : 102 : foreach(lc2, RelationGetIndexList(toastRelation))
+ + ]
3755 : : {
3756 : 51 : Oid cellOid = lfirst_oid(lc2);
3757 : 51 : Relation indexRelation = index_open(cellOid,
3758 : : ShareUpdateExclusiveLock);
3759 : :
3760 [ - + ]: 51 : if (!indexRelation->rd_index->indisvalid)
2594 peter@eisentraut.org 3761 [ # # ]:UBC 0 : ereport(WARNING,
3762 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3763 : : errmsg("skipping reindex of invalid index \"%s.%s\"",
3764 : : get_namespace_name(get_rel_namespace(cellOid)),
3765 : : get_rel_name(cellOid)),
3766 : : errhint("Use DROP INDEX or REINDEX INDEX.")));
3767 : : else
3768 : : {
3769 : : ReindexIndexInfo *idx;
3770 : :
3771 : : /*
3772 : : * Save the list of relation OIDs in private
3773 : : * context
3774 : : */
2594 peter@eisentraut.org 3775 :CBC 51 : oldcontext = MemoryContextSwitchTo(private_context);
3776 : :
1331 3777 : 51 : idx = palloc_object(ReindexIndexInfo);
1939 alvherre@alvh.no-ip. 3778 : 51 : idx->indexId = cellOid;
3779 : 51 : indexIds = lappend(indexIds, idx);
3780 : : /* other fields set later */
3781 : :
2594 peter@eisentraut.org 3782 : 51 : MemoryContextSwitchTo(oldcontext);
3783 : : }
3784 : :
3785 : 51 : index_close(indexRelation, NoLock);
3786 : : }
3787 : :
3788 : 51 : table_close(toastRelation, NoLock);
3789 : : }
3790 : :
3791 : 177 : table_close(heapRelation, NoLock);
3792 : 177 : break;
3793 : : }
3794 : 134 : case RELKIND_INDEX:
3795 : : {
2071 michael@paquier.xyz 3796 : 134 : Oid heapId = IndexGetRelation(relationOid,
1933 3797 : 134 : (params->options & REINDEXOPT_MISSING_OK) != 0);
3798 : : Relation heapRelation;
3799 : : ReindexIndexInfo *idx;
3800 : :
3801 : : /* if relation is missing, leave */
2071 3802 [ - + ]: 134 : if (!OidIsValid(heapId))
2071 michael@paquier.xyz 3803 :UBC 0 : break;
3804 : :
2554 tgl@sss.pgh.pa.us 3805 [ + + ]:CBC 134 : if (IsCatalogRelationOid(heapId))
2594 peter@eisentraut.org 3806 [ + - ]: 12 : ereport(ERROR,
3807 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3808 : : errmsg("cannot reindex system catalogs concurrently")));
3809 : :
3810 : : /*
3811 : : * Don't allow reindex for an invalid index on TOAST table, as
3812 : : * if rebuilt it would not be possible to drop it. Match
3813 : : * error message in reindex_index().
3814 : : */
2247 michael@paquier.xyz 3815 [ + + ]: 122 : if (IsToastNamespace(get_rel_namespace(relationOid)) &&
3816 [ - + ]: 28 : !get_index_isvalid(relationOid))
2247 michael@paquier.xyz 3817 [ # # ]:UBC 0 : ereport(ERROR,
3818 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3819 : : errmsg("cannot reindex invalid index on TOAST table")));
3820 : :
3821 : : /*
3822 : : * Check if parent relation can be locked and if it exists,
3823 : : * this needs to be done at this stage as the list of indexes
3824 : : * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
3825 : : * should not be used once all the session locks are taken.
3826 : : */
1933 michael@paquier.xyz 3827 [ + + ]:CBC 122 : if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3828 : : {
2071 3829 : 16 : heapRelation = try_table_open(heapId,
3830 : : ShareUpdateExclusiveLock);
3831 : : /* leave if relation does not exist */
3832 [ - + ]: 16 : if (!heapRelation)
2071 michael@paquier.xyz 3833 :UBC 0 : break;
3834 : : }
3835 : : else
2071 michael@paquier.xyz 3836 :CBC 106 : heapRelation = table_open(heapId,
3837 : : ShareUpdateExclusiveLock);
3838 : :
1916 3839 [ + + + + ]: 127 : if (OidIsValid(params->tablespaceOid) &&
3840 : 5 : IsSystemRelation(heapRelation))
3841 [ + - ]: 1 : ereport(ERROR,
3842 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3843 : : errmsg("cannot move system relation \"%s\"",
3844 : : get_rel_name(relationOid))));
3845 : :
2071 3846 : 121 : table_close(heapRelation, NoLock);
3847 : :
3848 : : /* Save the list of relation OIDs in private context */
2594 peter@eisentraut.org 3849 : 121 : oldcontext = MemoryContextSwitchTo(private_context);
3850 : :
3851 : : /* Track the heap relation of this index for session locks */
3852 : 121 : heapRelationIds = list_make1_oid(heapId);
3853 : :
3854 : : /*
3855 : : * Save the list of relation OIDs in private context. Note
3856 : : * that invalid indexes are allowed here.
3857 : : */
1331 3858 : 121 : idx = palloc_object(ReindexIndexInfo);
1939 alvherre@alvh.no-ip. 3859 : 121 : idx->indexId = relationOid;
3860 : 121 : indexIds = lappend(indexIds, idx);
3861 : : /* other fields set later */
3862 : :
2575 michael@paquier.xyz 3863 : 121 : MemoryContextSwitchTo(oldcontext);
2594 peter@eisentraut.org 3864 : 121 : break;
3865 : : }
3866 : :
2594 peter@eisentraut.org 3867 :UBC 0 : case RELKIND_PARTITIONED_TABLE:
3868 : : case RELKIND_PARTITIONED_INDEX:
3869 : : default:
3870 : : /* Return error if type of relation is not supported */
3871 [ # # ]: 0 : ereport(ERROR,
3872 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3873 : : errmsg("cannot reindex this type of relation concurrently")));
3874 : : break;
3875 : : }
3876 : :
3877 : : /*
3878 : : * Definitely no indexes, so leave. Any checks based on
3879 : : * REINDEXOPT_MISSING_OK should be done only while the list of indexes to
3880 : : * work on is built as the session locks taken before this transaction
3881 : : * commits will make sure that they cannot be dropped by a concurrent
3882 : : * session until this operation completes.
3883 : : */
2594 peter@eisentraut.org 3884 [ + + ]:CBC 298 : if (indexIds == NIL)
3885 : 29 : return false;
3886 : :
3887 : : /* It's not a shared catalog, so refuse to move it to shared tablespace */
1916 michael@paquier.xyz 3888 [ + + ]: 269 : if (params->tablespaceOid == GLOBALTABLESPACE_OID)
3889 [ + - ]: 4 : ereport(ERROR,
3890 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3891 : : errmsg("cannot move non-shared relation to tablespace \"%s\"",
3892 : : get_tablespace_name(params->tablespaceOid))));
3893 : :
2594 peter@eisentraut.org 3894 [ - + ]: 265 : Assert(heapRelationIds != NIL);
3895 : :
3896 : : /*-----
3897 : : * Now we have all the indexes we want to process in indexIds.
3898 : : *
3899 : : * The phases now are:
3900 : : *
3901 : : * 1. create new indexes in the catalog
3902 : : * 2. build new indexes
3903 : : * 3. let new indexes catch up with tuples inserted in the meantime
3904 : : * 4. swap index names
3905 : : * 5. mark old indexes as dead
3906 : : * 6. drop old indexes
3907 : : *
3908 : : * We process each phase for all indexes before moving to the next phase,
3909 : : * for efficiency.
3910 : : */
3911 : :
3912 : : /*
3913 : : * Phase 1 of REINDEX CONCURRENTLY
3914 : : *
3915 : : * Create a new index with the same properties as the old one, but it is
3916 : : * only registered in catalogs and will be built later. Then get session
3917 : : * locks on all involved tables. See analogous code in DefineIndex() for
3918 : : * more detailed comments.
3919 : : */
3920 : :
3921 [ + - + + : 588 : foreach(lc, indexIds)
+ + ]
3922 : : {
3923 : : char *concurrentName;
1939 alvherre@alvh.no-ip. 3924 : 327 : ReindexIndexInfo *idx = lfirst(lc);
3925 : : ReindexIndexInfo *newidx;
3926 : : Oid newIndexId;
3927 : : Relation indexRel;
3928 : : Relation heapRel;
3929 : : Oid save_userid;
3930 : : int save_sec_context;
3931 : : int save_nestlevel;
3932 : : Relation newIndexRel;
3933 : : LockRelId *lockrelid;
3934 : : Oid tablespaceid;
3935 : :
3936 : 327 : indexRel = index_open(idx->indexId, ShareUpdateExclusiveLock);
2594 peter@eisentraut.org 3937 : 327 : heapRel = table_open(indexRel->rd_index->indrelid,
3938 : : ShareUpdateExclusiveLock);
3939 : :
3940 : : /*
3941 : : * Switch to the table owner's userid, so that any index functions are
3942 : : * run as that user. Also lock down security-restricted operations
3943 : : * and arrange to make GUC variable changes local to this command.
3944 : : */
1457 noah@leadboat.com 3945 : 327 : GetUserIdAndSecContext(&save_userid, &save_sec_context);
3946 : 327 : SetUserIdAndSecContext(heapRel->rd_rel->relowner,
3947 : : save_sec_context | SECURITY_RESTRICTED_OPERATION);
3948 : 327 : save_nestlevel = NewGUCNestLevel();
792 jdavis@postgresql.or 3949 : 327 : RestrictSearchPath();
3950 : :
3951 : : /* determine safety of this index for set_indexsafe_procflags */
603 michael@paquier.xyz 3952 [ + + + + ]: 620 : idx->safe = (RelationGetIndexExpressions(indexRel) == NIL &&
3953 : 293 : RelationGetIndexPredicate(indexRel) == NIL);
3954 : :
3955 : : #ifdef USE_INJECTION_POINTS
3956 [ + + ]: 327 : if (idx->safe)
360 3957 : 288 : INJECTION_POINT("reindex-conc-index-safe", NULL);
3958 : : else
3959 : 39 : INJECTION_POINT("reindex-conc-index-not-safe", NULL);
3960 : : #endif
3961 : :
1939 alvherre@alvh.no-ip. 3962 : 327 : idx->tableId = RelationGetRelid(heapRel);
3963 : 327 : idx->amId = indexRel->rd_rel->relam;
3964 : :
3965 : : /* This function shouldn't be called for temporary relations. */
2295 michael@paquier.xyz 3966 [ - + ]: 327 : if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
2295 michael@paquier.xyz 3967 [ # # ]:UBC 0 : elog(ERROR, "cannot reindex a temporary table concurrently");
3968 : :
986 peter@eisentraut.org 3969 :CBC 327 : pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, idx->tableId);
3970 : :
2044 michael@paquier.xyz 3971 : 327 : progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3972 : 327 : progress_vals[1] = 0; /* initializing */
1939 alvherre@alvh.no-ip. 3973 : 327 : progress_vals[2] = idx->indexId;
3974 : 327 : progress_vals[3] = idx->amId;
2044 michael@paquier.xyz 3975 : 327 : pgstat_progress_update_multi_param(4, progress_index, progress_vals);
3976 : :
3977 : : /* Choose a temporary relation name for the new index */
1939 alvherre@alvh.no-ip. 3978 : 327 : concurrentName = ChooseRelationName(get_rel_name(idx->indexId),
3979 : : NULL,
3980 : : "ccnew",
2594 peter@eisentraut.org 3981 : 327 : get_rel_namespace(indexRel->rd_index->indrelid),
3982 : : false);
3983 : :
3984 : : /* Choose the new tablespace, indexes of toast tables are not moved */
1916 michael@paquier.xyz 3985 [ + + ]: 327 : if (OidIsValid(params->tablespaceOid) &&
3986 [ + + ]: 18 : heapRel->rd_rel->relkind != RELKIND_TOASTVALUE)
3987 : 13 : tablespaceid = params->tablespaceOid;
3988 : : else
3989 : 314 : tablespaceid = indexRel->rd_rel->reltablespace;
3990 : :
3991 : : /* Create new index definition based on given index */
31 alvherre@kurilemu.de 3992 :GNC 327 : newIndexId = index_create_copy(heapRel,
3993 : : INDEX_CREATE_CONCURRENT |
3994 : : INDEX_CREATE_SKIP_BUILD |
3995 : : INDEX_CREATE_SUPPRESS_PROGRESS,
3996 : : idx->indexId,
3997 : : tablespaceid,
3998 : : concurrentName);
3999 : :
4000 : : /*
4001 : : * Now open the relation of the new index, a session-level lock is
4002 : : * also needed on it.
4003 : : */
2386 michael@paquier.xyz 4004 :CBC 323 : newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
4005 : :
4006 : : /*
4007 : : * Save the list of OIDs and locks in private context
4008 : : */
2594 peter@eisentraut.org 4009 : 323 : oldcontext = MemoryContextSwitchTo(private_context);
4010 : :
1331 4011 : 323 : newidx = palloc_object(ReindexIndexInfo);
1939 alvherre@alvh.no-ip. 4012 : 323 : newidx->indexId = newIndexId;
1936 4013 : 323 : newidx->safe = idx->safe;
1939 4014 : 323 : newidx->tableId = idx->tableId;
4015 : 323 : newidx->amId = idx->amId;
4016 : :
4017 : 323 : newIndexIds = lappend(newIndexIds, newidx);
4018 : :
4019 : : /*
4020 : : * Save lockrelid to protect each relation from drop then close
4021 : : * relations. The lockrelid on parent relation is not taken here to
4022 : : * avoid multiple locks taken on the same relation, instead we rely on
4023 : : * parentRelationIds built earlier.
4024 : : */
1331 peter@eisentraut.org 4025 : 323 : lockrelid = palloc_object(LockRelId);
2594 4026 : 323 : *lockrelid = indexRel->rd_lockInfo.lockRelId;
4027 : 323 : relationLocks = lappend(relationLocks, lockrelid);
1331 4028 : 323 : lockrelid = palloc_object(LockRelId);
2594 4029 : 323 : *lockrelid = newIndexRel->rd_lockInfo.lockRelId;
4030 : 323 : relationLocks = lappend(relationLocks, lockrelid);
4031 : :
4032 : 323 : MemoryContextSwitchTo(oldcontext);
4033 : :
4034 : 323 : index_close(indexRel, NoLock);
4035 : 323 : index_close(newIndexRel, NoLock);
4036 : :
4037 : : /* Roll back any GUC changes executed by index functions */
1457 noah@leadboat.com 4038 : 323 : AtEOXact_GUC(false, save_nestlevel);
4039 : :
4040 : : /* Restore userid and security context */
4041 : 323 : SetUserIdAndSecContext(save_userid, save_sec_context);
4042 : :
2594 peter@eisentraut.org 4043 : 323 : table_close(heapRel, NoLock);
4044 : :
4045 : : /*
4046 : : * If a statement is available, telling that this comes from a REINDEX
4047 : : * command, collect the new index for event triggers.
4048 : : */
883 michael@paquier.xyz 4049 [ + - ]: 323 : if (stmt)
4050 : : {
4051 : : ObjectAddress address;
4052 : :
4053 : 323 : ObjectAddressSet(address, RelationRelationId, newIndexId);
4054 : 323 : EventTriggerCollectSimpleCommand(address,
4055 : : InvalidObjectAddress,
4056 : : (const Node *) stmt);
4057 : : }
4058 : : }
4059 : :
4060 : : /*
4061 : : * Save the heap lock for following visibility checks with other backends
4062 : : * might conflict with this session.
4063 : : */
2594 peter@eisentraut.org 4064 [ + - + + : 573 : foreach(lc, heapRelationIds)
+ + ]
4065 : : {
4066 : 312 : Relation heapRelation = table_open(lfirst_oid(lc), ShareUpdateExclusiveLock);
4067 : : LockRelId *lockrelid;
4068 : : LOCKTAG *heaplocktag;
4069 : :
4070 : : /* Save the list of locks in private context */
4071 : 312 : oldcontext = MemoryContextSwitchTo(private_context);
4072 : :
4073 : : /* Add lockrelid of heap relation to the list of locked relations */
1331 4074 : 312 : lockrelid = palloc_object(LockRelId);
2594 4075 : 312 : *lockrelid = heapRelation->rd_lockInfo.lockRelId;
4076 : 312 : relationLocks = lappend(relationLocks, lockrelid);
4077 : :
1331 4078 : 312 : heaplocktag = palloc_object(LOCKTAG);
4079 : :
4080 : : /* Save the LOCKTAG for this parent relation for the wait phase */
2594 4081 : 312 : SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);
4082 : 312 : lockTags = lappend(lockTags, heaplocktag);
4083 : :
4084 : 312 : MemoryContextSwitchTo(oldcontext);
4085 : :
4086 : : /* Close heap relation */
4087 : 312 : table_close(heapRelation, NoLock);
4088 : : }
4089 : :
4090 : : /* Get a session-level lock on each table. */
4091 [ + - + + : 1219 : foreach(lc, relationLocks)
+ + ]
4092 : : {
2540 tgl@sss.pgh.pa.us 4093 : 958 : LockRelId *lockrelid = (LockRelId *) lfirst(lc);
4094 : :
2594 peter@eisentraut.org 4095 : 958 : LockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
4096 : : }
4097 : :
4098 : 261 : PopActiveSnapshot();
4099 : 261 : CommitTransactionCommand();
4100 : 261 : StartTransactionCommand();
4101 : :
4102 : : /*
4103 : : * Because we don't take a snapshot in this transaction, there's no need
4104 : : * to set the PROC_IN_SAFE_IC flag here.
4105 : : */
4106 : :
4107 : : /*
4108 : : * Phase 2 of REINDEX CONCURRENTLY
4109 : : *
4110 : : * Build the new indexes in a separate transaction for each index to avoid
4111 : : * having open transactions for an unnecessary long time. But before
4112 : : * doing that, wait until no running transactions could have the table of
4113 : : * the index open with the old list of indexes. See "phase 2" in
4114 : : * DefineIndex() for more details.
4115 : : */
4116 : :
2585 4117 : 261 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4118 : : PROGRESS_CREATEIDX_PHASE_WAIT_1);
4119 : 261 : WaitForLockersMultiple(lockTags, ShareLock, true);
2594 4120 : 261 : CommitTransactionCommand();
4121 : :
2044 michael@paquier.xyz 4122 [ + - + + : 580 : foreach(lc, newIndexIds)
+ + ]
4123 : : {
1939 alvherre@alvh.no-ip. 4124 : 323 : ReindexIndexInfo *newidx = lfirst(lc);
4125 : :
4126 : : /* Start new transaction for this index's concurrent build */
2594 peter@eisentraut.org 4127 : 323 : StartTransactionCommand();
4128 : :
4129 : : /*
4130 : : * Check for user-requested abort. This is inside a transaction so as
4131 : : * xact.c does not issue a useless WARNING, and ensures that
4132 : : * session-level locks are cleaned up on abort.
4133 : : */
2384 michael@paquier.xyz 4134 [ - + ]: 323 : CHECK_FOR_INTERRUPTS();
4135 : :
4136 : : /* Tell concurrent indexing to ignore us, if index qualifies */
1936 alvherre@alvh.no-ip. 4137 [ + + ]: 323 : if (newidx->safe)
4138 : 284 : set_indexsafe_procflags();
4139 : :
4140 : : /* Set ActiveSnapshot since functions in the indexes may need it */
2594 peter@eisentraut.org 4141 : 323 : PushActiveSnapshot(GetTransactionSnapshot());
4142 : :
4143 : : /*
4144 : : * Update progress for the index to build, with the correct parent
4145 : : * table involved.
4146 : : */
1939 alvherre@alvh.no-ip. 4147 : 323 : pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
2044 michael@paquier.xyz 4148 : 323 : progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
4149 : 323 : progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD;
1939 alvherre@alvh.no-ip. 4150 : 323 : progress_vals[2] = newidx->indexId;
4151 : 323 : progress_vals[3] = newidx->amId;
2044 michael@paquier.xyz 4152 : 323 : pgstat_progress_update_multi_param(4, progress_index, progress_vals);
4153 : :
4154 : : /* Perform concurrent build of new index */
1939 alvherre@alvh.no-ip. 4155 : 323 : index_concurrently_build(newidx->tableId, newidx->indexId);
4156 : :
2594 peter@eisentraut.org 4157 : 319 : PopActiveSnapshot();
4158 : 319 : CommitTransactionCommand();
4159 : : }
4160 : :
4161 : 257 : StartTransactionCommand();
4162 : :
4163 : : /*
4164 : : * Because we don't take a snapshot or Xid in this transaction, there's no
4165 : : * need to set the PROC_IN_SAFE_IC flag here.
4166 : : */
4167 : :
4168 : : /*
4169 : : * Phase 3 of REINDEX CONCURRENTLY
4170 : : *
4171 : : * During this phase the old indexes catch up with any new tuples that
4172 : : * were created during the previous phase. See "phase 3" in DefineIndex()
4173 : : * for more details.
4174 : : */
4175 : :
2585 4176 : 257 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4177 : : PROGRESS_CREATEIDX_PHASE_WAIT_2);
4178 : 257 : WaitForLockersMultiple(lockTags, ShareLock, true);
2594 4179 : 257 : CommitTransactionCommand();
4180 : :
4181 [ + - + + : 576 : foreach(lc, newIndexIds)
+ + ]
4182 : : {
1939 alvherre@alvh.no-ip. 4183 : 319 : ReindexIndexInfo *newidx = lfirst(lc);
4184 : : TransactionId limitXmin;
4185 : : Snapshot snapshot;
4186 : :
2594 peter@eisentraut.org 4187 : 319 : StartTransactionCommand();
4188 : :
4189 : : /*
4190 : : * Check for user-requested abort. This is inside a transaction so as
4191 : : * xact.c does not issue a useless WARNING, and ensures that
4192 : : * session-level locks are cleaned up on abort.
4193 : : */
2384 michael@paquier.xyz 4194 [ - + ]: 319 : CHECK_FOR_INTERRUPTS();
4195 : :
4196 : : /* Tell concurrent indexing to ignore us, if index qualifies */
1936 alvherre@alvh.no-ip. 4197 [ + + ]: 319 : if (newidx->safe)
4198 : 280 : set_indexsafe_procflags();
4199 : :
4200 : : /*
4201 : : * Take the "reference snapshot" that will be used by validate_index()
4202 : : * to filter candidate tuples.
4203 : : */
2594 peter@eisentraut.org 4204 : 319 : snapshot = RegisterSnapshot(GetTransactionSnapshot());
4205 : 319 : PushActiveSnapshot(snapshot);
4206 : :
4207 : : /*
4208 : : * Update progress for the index to build, with the correct parent
4209 : : * table involved.
4210 : : */
986 4211 : 319 : pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
2044 michael@paquier.xyz 4212 : 319 : progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
4213 : 319 : progress_vals[1] = PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN;
1939 alvherre@alvh.no-ip. 4214 : 319 : progress_vals[2] = newidx->indexId;
4215 : 319 : progress_vals[3] = newidx->amId;
2044 michael@paquier.xyz 4216 : 319 : pgstat_progress_update_multi_param(4, progress_index, progress_vals);
4217 : :
1939 alvherre@alvh.no-ip. 4218 : 319 : validate_index(newidx->tableId, newidx->indexId, snapshot);
4219 : :
4220 : : /*
4221 : : * We can now do away with our active snapshot, we still need to save
4222 : : * the xmin limit to wait for older snapshots.
4223 : : */
2594 peter@eisentraut.org 4224 : 319 : limitXmin = snapshot->xmin;
4225 : :
6567 alvherre@alvh.no-ip. 4226 : 319 : PopActiveSnapshot();
2594 peter@eisentraut.org 4227 : 319 : UnregisterSnapshot(snapshot);
4228 : :
4229 : : /*
4230 : : * To ensure no deadlocks, we must commit and start yet another
4231 : : * transaction, and do our wait before any snapshot has been taken in
4232 : : * it.
4233 : : */
4234 : 319 : CommitTransactionCommand();
4235 : 319 : StartTransactionCommand();
4236 : :
4237 : : /*
4238 : : * The index is now valid in the sense that it contains all currently
4239 : : * interesting tuples. But since it might not contain tuples deleted
4240 : : * just before the reference snap was taken, we have to wait out any
4241 : : * transactions that might have older snapshots.
4242 : : *
4243 : : * Because we don't take a snapshot or Xid in this transaction,
4244 : : * there's no need to set the PROC_IN_SAFE_IC flag here.
4245 : : */
2585 4246 : 319 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4247 : : PROGRESS_CREATEIDX_PHASE_WAIT_3);
4248 : 319 : WaitForOlderSnapshots(limitXmin, true);
4249 : :
8392 tgl@sss.pgh.pa.us 4250 : 319 : CommitTransactionCommand();
4251 : : }
4252 : :
4253 : : /*
4254 : : * Phase 4 of REINDEX CONCURRENTLY
4255 : : *
4256 : : * Now that the new indexes have been validated, swap each new index with
4257 : : * its corresponding old index.
4258 : : *
4259 : : * We mark the new indexes as valid and the old indexes as not valid at
4260 : : * the same time to make sure we only get constraint violations from the
4261 : : * indexes with the correct names.
4262 : : */
4263 : :
162 alvherre@kurilemu.de 4264 :GNC 257 : INJECTION_POINT("reindex-relation-concurrently-before-swap", NULL);
8392 tgl@sss.pgh.pa.us 4265 :CBC 257 : StartTransactionCommand();
4266 : :
4267 : : /*
4268 : : * Because this transaction only does catalog manipulations and doesn't do
4269 : : * any index operations, we can set the PROC_IN_SAFE_IC flag here
4270 : : * unconditionally.
4271 : : */
1936 alvherre@alvh.no-ip. 4272 : 257 : set_indexsafe_procflags();
4273 : :
2594 peter@eisentraut.org 4274 [ + - + + : 576 : forboth(lc, indexIds, lc2, newIndexIds)
+ - + + +
+ + - +
+ ]
4275 : : {
1939 alvherre@alvh.no-ip. 4276 : 319 : ReindexIndexInfo *oldidx = lfirst(lc);
4277 : 319 : ReindexIndexInfo *newidx = lfirst(lc2);
4278 : : char *oldName;
4279 : :
4280 : : /*
4281 : : * Check for user-requested abort. This is inside a transaction so as
4282 : : * xact.c does not issue a useless WARNING, and ensures that
4283 : : * session-level locks are cleaned up on abort.
4284 : : */
2594 peter@eisentraut.org 4285 [ - + ]: 319 : CHECK_FOR_INTERRUPTS();
4286 : :
4287 : : /* Choose a relation name for old index */
1939 alvherre@alvh.no-ip. 4288 : 319 : oldName = ChooseRelationName(get_rel_name(oldidx->indexId),
4289 : : NULL,
4290 : : "ccold",
4291 : : get_rel_namespace(oldidx->tableId),
4292 : : false);
4293 : :
4294 : : /*
4295 : : * Swapping the indexes might involve TOAST table access, so ensure we
4296 : : * have a valid snapshot.
4297 : : */
586 nathan@postgresql.or 4298 : 319 : PushActiveSnapshot(GetTransactionSnapshot());
4299 : :
4300 : : /*
4301 : : * Swap old index with the new one. This also marks the new one as
4302 : : * valid and the old one as not valid.
4303 : : */
1939 alvherre@alvh.no-ip. 4304 : 319 : index_concurrently_swap(newidx->indexId, oldidx->indexId, oldName);
4305 : :
586 nathan@postgresql.or 4306 : 319 : PopActiveSnapshot();
4307 : :
4308 : : /*
4309 : : * Invalidate the relcache for the table, so that after this commit
4310 : : * all sessions will refresh any cached plans that might reference the
4311 : : * index.
4312 : : */
1939 alvherre@alvh.no-ip. 4313 : 319 : CacheInvalidateRelcacheByRelid(oldidx->tableId);
4314 : :
4315 : : /*
4316 : : * CCI here so that subsequent iterations see the oldName in the
4317 : : * catalog and can choose a nonconflicting name for their oldName.
4318 : : * Otherwise, this could lead to conflicts if a table has two indexes
4319 : : * whose names are equal for the first NAMEDATALEN-minus-a-few
4320 : : * characters.
4321 : : */
2594 peter@eisentraut.org 4322 : 319 : CommandCounterIncrement();
4323 : : }
4324 : :
4325 : : /* Commit this transaction and make index swaps visible */
4326 : 257 : CommitTransactionCommand();
4327 : 257 : StartTransactionCommand();
4328 : :
4329 : : /*
4330 : : * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4331 : : * real need for that, because we only acquire an Xid after the wait is
4332 : : * done, and that lasts for a very short period.
4333 : : */
4334 : :
4335 : : /*
4336 : : * Phase 5 of REINDEX CONCURRENTLY
4337 : : *
4338 : : * Mark the old indexes as dead. First we must wait until no running
4339 : : * transaction could be using the index for a query. See also
4340 : : * index_drop() for more details.
4341 : : */
4342 : :
162 alvherre@kurilemu.de 4343 :GNC 257 : INJECTION_POINT("reindex-relation-concurrently-before-set-dead", NULL);
2585 peter@eisentraut.org 4344 :CBC 257 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4345 : : PROGRESS_CREATEIDX_PHASE_WAIT_4);
4346 : 257 : WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
4347 : :
2594 4348 [ + - + + : 576 : foreach(lc, indexIds)
+ + ]
4349 : : {
1939 alvherre@alvh.no-ip. 4350 : 319 : ReindexIndexInfo *oldidx = lfirst(lc);
4351 : :
4352 : : /*
4353 : : * Check for user-requested abort. This is inside a transaction so as
4354 : : * xact.c does not issue a useless WARNING, and ensures that
4355 : : * session-level locks are cleaned up on abort.
4356 : : */
2594 peter@eisentraut.org 4357 [ - + ]: 319 : CHECK_FOR_INTERRUPTS();
4358 : :
4359 : : /*
4360 : : * Updating pg_index might involve TOAST table access, so ensure we
4361 : : * have a valid snapshot.
4362 : : */
586 nathan@postgresql.or 4363 : 319 : PushActiveSnapshot(GetTransactionSnapshot());
4364 : :
1939 alvherre@alvh.no-ip. 4365 : 319 : index_concurrently_set_dead(oldidx->tableId, oldidx->indexId);
4366 : :
586 nathan@postgresql.or 4367 : 319 : PopActiveSnapshot();
4368 : : }
4369 : :
4370 : : /* Commit this transaction to make the updates visible. */
2594 peter@eisentraut.org 4371 : 257 : CommitTransactionCommand();
4372 : 257 : StartTransactionCommand();
4373 : :
4374 : : /*
4375 : : * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4376 : : * real need for that, because we only acquire an Xid after the wait is
4377 : : * done, and that lasts for a very short period.
4378 : : */
4379 : :
4380 : : /*
4381 : : * Phase 6 of REINDEX CONCURRENTLY
4382 : : *
4383 : : * Drop the old indexes.
4384 : : */
4385 : :
2585 4386 : 257 : pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
4387 : : PROGRESS_CREATEIDX_PHASE_WAIT_5);
4388 : 257 : WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
4389 : :
2594 4390 : 257 : PushActiveSnapshot(GetTransactionSnapshot());
4391 : :
4392 : : {
4393 : 257 : ObjectAddresses *objects = new_object_addresses();
4394 : :
4395 [ + - + + : 576 : foreach(lc, indexIds)
+ + ]
4396 : : {
1939 alvherre@alvh.no-ip. 4397 : 319 : ReindexIndexInfo *idx = lfirst(lc);
4398 : : ObjectAddress object;
4399 : :
2593 peter@eisentraut.org 4400 : 319 : object.classId = RelationRelationId;
1939 alvherre@alvh.no-ip. 4401 : 319 : object.objectId = idx->indexId;
2593 peter@eisentraut.org 4402 : 319 : object.objectSubId = 0;
4403 : :
4404 : 319 : add_exact_object_address(&object, objects);
4405 : : }
4406 : :
4407 : : /*
4408 : : * Use PERFORM_DELETION_CONCURRENT_LOCK so that index_drop() uses the
4409 : : * right lock level.
4410 : : */
2594 4411 : 257 : performMultipleDeletions(objects, DROP_RESTRICT,
4412 : : PERFORM_DELETION_CONCURRENT_LOCK | PERFORM_DELETION_INTERNAL);
4413 : : }
4414 : :
4415 : 257 : PopActiveSnapshot();
4416 : 257 : CommitTransactionCommand();
4417 : :
4418 : : /*
4419 : : * Finally, release the session-level lock on the table.
4420 : : */
4421 [ + - + + : 1203 : foreach(lc, relationLocks)
+ + ]
4422 : : {
2540 tgl@sss.pgh.pa.us 4423 : 946 : LockRelId *lockrelid = (LockRelId *) lfirst(lc);
4424 : :
2594 peter@eisentraut.org 4425 : 946 : UnlockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
4426 : : }
4427 : :
4428 : : /* Start a new transaction to finish process properly */
4429 : 257 : StartTransactionCommand();
4430 : :
4431 : : /* Log what we did */
1933 michael@paquier.xyz 4432 [ + + ]: 257 : if ((params->options & REINDEXOPT_VERBOSE) != 0)
4433 : : {
2594 peter@eisentraut.org 4434 [ - + ]: 2 : if (relkind == RELKIND_INDEX)
2594 peter@eisentraut.org 4435 [ # # ]:UBC 0 : ereport(INFO,
4436 : : (errmsg("index \"%s.%s\" was reindexed",
4437 : : relationNamespace, relationName),
4438 : : errdetail("%s.",
4439 : : pg_rusage_show(&ru0))));
4440 : : else
4441 : : {
2594 peter@eisentraut.org 4442 [ + - + + :CBC 6 : foreach(lc, newIndexIds)
+ + ]
4443 : : {
1939 alvherre@alvh.no-ip. 4444 : 4 : ReindexIndexInfo *idx = lfirst(lc);
4445 : 4 : Oid indOid = idx->indexId;
4446 : :
2594 peter@eisentraut.org 4447 [ + - ]: 4 : ereport(INFO,
4448 : : (errmsg("index \"%s.%s\" was reindexed",
4449 : : get_namespace_name(get_rel_namespace(indOid)),
4450 : : get_rel_name(indOid))));
4451 : : /* Don't show rusage here, since it's not per index. */
4452 : : }
4453 : :
4454 [ + - ]: 2 : ereport(INFO,
4455 : : (errmsg("table \"%s.%s\" was reindexed",
4456 : : relationNamespace, relationName),
4457 : : errdetail("%s.",
4458 : : pg_rusage_show(&ru0))));
4459 : : }
4460 : : }
4461 : :
9442 tgl@sss.pgh.pa.us 4462 : 257 : MemoryContextDelete(private_context);
4463 : :
2585 peter@eisentraut.org 4464 : 257 : pgstat_progress_end_command();
4465 : :
2594 4466 : 257 : return true;
4467 : : }
4468 : :
4469 : : /*
4470 : : * Insert or delete an appropriate pg_inherits tuple to make the given index
4471 : : * be a partition of the indicated parent index.
4472 : : *
4473 : : * This also corrects the pg_depend information for the affected index.
4474 : : */
4475 : : void
3028 alvherre@alvh.no-ip. 4476 : 704 : IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
4477 : : {
4478 : : Relation pg_inherits;
4479 : : ScanKeyData key[2];
4480 : : SysScanDesc scan;
4481 : 704 : Oid partRelid = RelationGetRelid(partitionIdx);
4482 : : HeapTuple tuple;
4483 : : bool fix_dependencies;
4484 : :
4485 : : /* Make sure this is an index */
4486 [ + + - + ]: 704 : Assert(partitionIdx->rd_rel->relkind == RELKIND_INDEX ||
4487 : : partitionIdx->rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
4488 : :
4489 : : /*
4490 : : * Scan pg_inherits for rows linking our index to some parent.
4491 : : */
4492 : 704 : pg_inherits = relation_open(InheritsRelationId, RowExclusiveLock);
4493 : 704 : ScanKeyInit(&key[0],
4494 : : Anum_pg_inherits_inhrelid,
4495 : : BTEqualStrategyNumber, F_OIDEQ,
4496 : : ObjectIdGetDatum(partRelid));
4497 : 704 : ScanKeyInit(&key[1],
4498 : : Anum_pg_inherits_inhseqno,
4499 : : BTEqualStrategyNumber, F_INT4EQ,
4500 : : Int32GetDatum(1));
4501 : 704 : scan = systable_beginscan(pg_inherits, InheritsRelidSeqnoIndexId, true,
4502 : : NULL, 2, key);
4503 : 704 : tuple = systable_getnext(scan);
4504 : :
4505 [ + + ]: 704 : if (!HeapTupleIsValid(tuple))
4506 : : {
4507 [ - + ]: 406 : if (parentOid == InvalidOid)
4508 : : {
4509 : : /*
4510 : : * No pg_inherits row, and no parent wanted: nothing to do in this
4511 : : * case.
4512 : : */
3028 alvherre@alvh.no-ip. 4513 :UBC 0 : fix_dependencies = false;
4514 : : }
4515 : : else
4516 : : {
1867 alvherre@alvh.no-ip. 4517 :CBC 406 : StoreSingleInheritance(partRelid, parentOid, 1);
3028 4518 : 406 : fix_dependencies = true;
4519 : : }
4520 : : }
4521 : : else
4522 : : {
2931 tgl@sss.pgh.pa.us 4523 : 298 : Form_pg_inherits inhForm = (Form_pg_inherits) GETSTRUCT(tuple);
4524 : :
3028 alvherre@alvh.no-ip. 4525 [ + - ]: 298 : if (parentOid == InvalidOid)
4526 : : {
4527 : : /*
4528 : : * There exists a pg_inherits row, which we want to clear; do so.
4529 : : */
4530 : 298 : CatalogTupleDelete(pg_inherits, &tuple->t_self);
4531 : 298 : fix_dependencies = true;
4532 : : }
4533 : : else
4534 : : {
4535 : : /*
4536 : : * A pg_inherits row exists. If it's the same we want, then we're
4537 : : * good; if it differs, that amounts to a corrupt catalog and
4538 : : * should not happen.
4539 : : */
3028 alvherre@alvh.no-ip. 4540 [ # # ]:UBC 0 : if (inhForm->inhparent != parentOid)
4541 : : {
4542 : : /* unexpected: we should not get called in this case */
4543 [ # # ]: 0 : elog(ERROR, "bogus pg_inherit row: inhrelid %u inhparent %u",
4544 : : inhForm->inhrelid, inhForm->inhparent);
4545 : : }
4546 : :
4547 : : /* already in the right state */
4548 : 0 : fix_dependencies = false;
4549 : : }
4550 : : }
4551 : :
4552 : : /* done with pg_inherits */
3028 alvherre@alvh.no-ip. 4553 :CBC 704 : systable_endscan(scan);
4554 : 704 : relation_close(pg_inherits, RowExclusiveLock);
4555 : :
4556 : : /* set relhassubclass if an index partition has been added to the parent */
2752 michael@paquier.xyz 4557 [ + + ]: 704 : if (OidIsValid(parentOid))
4558 : : {
677 noah@leadboat.com 4559 : 406 : LockRelationOid(parentOid, ShareUpdateExclusiveLock);
2752 michael@paquier.xyz 4560 : 406 : SetRelationHasSubclass(parentOid, true);
4561 : : }
4562 : :
4563 : : /* set relispartition correctly on the partition */
2567 alvherre@alvh.no-ip. 4564 : 704 : update_relispartition(partRelid, OidIsValid(parentOid));
4565 : :
3028 4566 [ + - ]: 704 : if (fix_dependencies)
4567 : : {
4568 : : /*
4569 : : * Insert/delete pg_depend rows. If setting a parent, add PARTITION
4570 : : * dependencies on the parent index and the table; if removing a
4571 : : * parent, delete PARTITION dependencies.
4572 : : */
4573 [ + + ]: 704 : if (OidIsValid(parentOid))
4574 : : {
4575 : : ObjectAddress partIdx;
4576 : : ObjectAddress parentIdx;
4577 : : ObjectAddress partitionTbl;
4578 : :
2640 tgl@sss.pgh.pa.us 4579 : 406 : ObjectAddressSet(partIdx, RelationRelationId, partRelid);
3028 alvherre@alvh.no-ip. 4580 : 406 : ObjectAddressSet(parentIdx, RelationRelationId, parentOid);
2640 tgl@sss.pgh.pa.us 4581 : 406 : ObjectAddressSet(partitionTbl, RelationRelationId,
4582 : : partitionIdx->rd_index->indrelid);
4583 : 406 : recordDependencyOn(&partIdx, &parentIdx,
4584 : : DEPENDENCY_PARTITION_PRI);
4585 : 406 : recordDependencyOn(&partIdx, &partitionTbl,
4586 : : DEPENDENCY_PARTITION_SEC);
4587 : : }
4588 : : else
4589 : : {
3028 alvherre@alvh.no-ip. 4590 : 298 : deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4591 : : RelationRelationId,
4592 : : DEPENDENCY_PARTITION_PRI);
2640 tgl@sss.pgh.pa.us 4593 : 298 : deleteDependencyRecordsForClass(RelationRelationId, partRelid,
4594 : : RelationRelationId,
4595 : : DEPENDENCY_PARTITION_SEC);
4596 : : }
4597 : :
4598 : : /* make our updates visible */
2968 alvherre@alvh.no-ip. 4599 : 704 : CommandCounterIncrement();
4600 : : }
3028 4601 : 704 : }
4602 : :
4603 : : /*
4604 : : * Subroutine of IndexSetParentIndex to update the relispartition flag of the
4605 : : * given index to the given value.
4606 : : */
4607 : : static void
2567 4608 : 704 : update_relispartition(Oid relationId, bool newval)
4609 : : {
4610 : : HeapTuple tup;
4611 : : Relation classRel;
4612 : : ItemPointerData otid;
4613 : :
4614 : 704 : classRel = table_open(RelationRelationId, RowExclusiveLock);
588 noah@leadboat.com 4615 : 704 : tup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relationId));
2557 tgl@sss.pgh.pa.us 4616 [ - + ]: 704 : if (!HeapTupleIsValid(tup))
2557 tgl@sss.pgh.pa.us 4617 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relationId);
588 noah@leadboat.com 4618 :CBC 704 : otid = tup->t_self;
2567 alvherre@alvh.no-ip. 4619 [ - + ]: 704 : Assert(((Form_pg_class) GETSTRUCT(tup))->relispartition != newval);
4620 : 704 : ((Form_pg_class) GETSTRUCT(tup))->relispartition = newval;
588 noah@leadboat.com 4621 : 704 : CatalogTupleUpdate(classRel, &otid, tup);
4622 : 704 : UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
2567 alvherre@alvh.no-ip. 4623 : 704 : heap_freetuple(tup);
4624 : 704 : table_close(classRel, RowExclusiveLock);
4625 : 704 : }
4626 : :
4627 : : /*
4628 : : * Set the PROC_IN_SAFE_IC flag in MyProc->statusFlags.
4629 : : *
4630 : : * When doing concurrent index builds, we can set this flag
4631 : : * to tell other processes concurrently running CREATE
4632 : : * INDEX CONCURRENTLY or REINDEX CONCURRENTLY to ignore us when
4633 : : * doing their waits for concurrent snapshots. On one hand it
4634 : : * avoids pointlessly waiting for a process that's not interesting
4635 : : * anyway; but more importantly it avoids deadlocks in some cases.
4636 : : *
4637 : : * This can be done safely only for indexes that don't execute any
4638 : : * expressions that could access other tables, so index must not be
4639 : : * expressional nor partial. Caller is responsible for only calling
4640 : : * this routine when that assumption holds true.
4641 : : *
4642 : : * (The flag is reset automatically at transaction end, so it must be
4643 : : * set for each transaction.)
4644 : : */
4645 : : static inline void
1987 4646 : 991 : set_indexsafe_procflags(void)
4647 : : {
4648 : : /*
4649 : : * This should only be called before installing xid or xmin in MyProc;
4650 : : * otherwise, concurrent processes could see an Xmin that moves backwards.
4651 : : */
4652 [ + - - + ]: 991 : Assert(MyProc->xid == InvalidTransactionId &&
4653 : : MyProc->xmin == InvalidTransactionId);
4654 : :
4655 : 991 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4656 : 991 : MyProc->statusFlags |= PROC_IN_SAFE_IC;
4657 : 991 : ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
4658 : 991 : LWLockRelease(ProcArrayLock);
4659 : 991 : }
|