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