Disallow empty passwords in LDAP authentication, the same way
[PostgreSQL.git] / src / backend / commands / indexcmds.c
blob99ab0e5fee8837a43566213969dad8b1dd57d797
1 /*-------------------------------------------------------------------------
3 * indexcmds.c
4 * POSTGRES define and remove index code.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include "access/genam.h"
19 #include "access/heapam.h"
20 #include "access/reloptions.h"
21 #include "access/transam.h"
22 #include "access/xact.h"
23 #include "catalog/catalog.h"
24 #include "catalog/heap.h"
25 #include "catalog/index.h"
26 #include "catalog/indexing.h"
27 #include "catalog/pg_opclass.h"
28 #include "catalog/pg_tablespace.h"
29 #include "commands/dbcommands.h"
30 #include "commands/defrem.h"
31 #include "commands/tablecmds.h"
32 #include "commands/tablespace.h"
33 #include "mb/pg_wchar.h"
34 #include "miscadmin.h"
35 #include "nodes/nodeFuncs.h"
36 #include "optimizer/clauses.h"
37 #include "parser/parse_coerce.h"
38 #include "parser/parse_func.h"
39 #include "parser/parsetree.h"
40 #include "storage/lmgr.h"
41 #include "storage/proc.h"
42 #include "storage/procarray.h"
43 #include "utils/acl.h"
44 #include "utils/builtins.h"
45 #include "utils/fmgroids.h"
46 #include "utils/inval.h"
47 #include "utils/lsyscache.h"
48 #include "utils/memutils.h"
49 #include "utils/relcache.h"
50 #include "utils/snapmgr.h"
51 #include "utils/syscache.h"
52 #include "utils/tqual.h"
55 /* non-export function prototypes */
56 static void CheckPredicate(Expr *predicate);
57 static void ComputeIndexAttrs(IndexInfo *indexInfo,
58 Oid *classOidP,
59 int16 *colOptionP,
60 List *attList,
61 Oid relId,
62 char *accessMethodName, Oid accessMethodId,
63 bool amcanorder,
64 bool isconstraint);
65 static Oid GetIndexOpClass(List *opclass, Oid attrType,
66 char *accessMethodName, Oid accessMethodId);
67 static bool relationHasPrimaryKey(Relation rel);
71 * DefineIndex
72 * Creates a new index.
74 * 'heapRelation': the relation the index will apply to.
75 * 'indexRelationName': the name for the new index, or NULL to indicate
76 * that a nonconflicting default name should be picked.
77 * 'indexRelationId': normally InvalidOid, but during bootstrap can be
78 * nonzero to specify a preselected OID for the index.
79 * 'accessMethodName': name of the AM to use.
80 * 'tableSpaceName': name of the tablespace to create the index in.
81 * NULL specifies using the appropriate default.
82 * 'attributeList': a list of IndexElem specifying columns and expressions
83 * to index on.
84 * 'predicate': the partial-index condition, or NULL if none.
85 * 'options': reloptions from WITH (in list-of-DefElem form).
86 * 'unique': make the index enforce uniqueness.
87 * 'primary': mark the index as a primary key in the catalogs.
88 * 'isconstraint': index is for a PRIMARY KEY or UNIQUE constraint,
89 * so build a pg_constraint entry for it.
90 * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
91 * 'check_rights': check for CREATE rights in the namespace. (This should
92 * be true except when ALTER is deleting/recreating an index.)
93 * 'skip_build': make the catalog entries but leave the index file empty;
94 * it will be filled later.
95 * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
96 * 'concurrent': avoid blocking writers to the table while building.
98 void
99 DefineIndex(RangeVar *heapRelation,
100 char *indexRelationName,
101 Oid indexRelationId,
102 char *accessMethodName,
103 char *tableSpaceName,
104 List *attributeList,
105 Expr *predicate,
106 List *options,
107 bool unique,
108 bool primary,
109 bool isconstraint,
110 bool is_alter_table,
111 bool check_rights,
112 bool skip_build,
113 bool quiet,
114 bool concurrent)
116 Oid *classObjectId;
117 Oid accessMethodId;
118 Oid relationId;
119 Oid namespaceId;
120 Oid tablespaceId;
121 Relation rel;
122 Relation indexRelation;
123 HeapTuple tuple;
124 Form_pg_am accessMethodForm;
125 bool amcanorder;
126 RegProcedure amoptions;
127 Datum reloptions;
128 int16 *coloptions;
129 IndexInfo *indexInfo;
130 int numberOfAttributes;
131 VirtualTransactionId *old_lockholders;
132 VirtualTransactionId *old_snapshots;
133 int n_old_snapshots;
134 LockRelId heaprelid;
135 LOCKTAG heaplocktag;
136 Snapshot snapshot;
137 Relation pg_index;
138 HeapTuple indexTuple;
139 Form_pg_index indexForm;
140 int i;
143 * count attributes in index
145 numberOfAttributes = list_length(attributeList);
146 if (numberOfAttributes <= 0)
147 ereport(ERROR,
148 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
149 errmsg("must specify at least one column")));
150 if (numberOfAttributes > INDEX_MAX_KEYS)
151 ereport(ERROR,
152 (errcode(ERRCODE_TOO_MANY_COLUMNS),
153 errmsg("cannot use more than %d columns in an index",
154 INDEX_MAX_KEYS)));
157 * Open heap relation, acquire a suitable lock on it, remember its OID
159 * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
160 * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
161 * (but not VACUUM).
163 rel = heap_openrv(heapRelation,
164 (concurrent ? ShareUpdateExclusiveLock : ShareLock));
166 relationId = RelationGetRelid(rel);
167 namespaceId = RelationGetNamespace(rel);
169 /* Note: during bootstrap may see uncataloged relation */
170 if (rel->rd_rel->relkind != RELKIND_RELATION &&
171 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
172 ereport(ERROR,
173 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
174 errmsg("\"%s\" is not a table",
175 heapRelation->relname)));
178 * Don't try to CREATE INDEX on temp tables of other backends.
180 if (RELATION_IS_OTHER_TEMP(rel))
181 ereport(ERROR,
182 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
183 errmsg("cannot create indexes on temporary tables of other sessions")));
186 * Verify we (still) have CREATE rights in the rel's namespace.
187 * (Presumably we did when the rel was created, but maybe not anymore.)
188 * Skip check if caller doesn't want it. Also skip check if
189 * bootstrapping, since permissions machinery may not be working yet.
191 if (check_rights && !IsBootstrapProcessingMode())
193 AclResult aclresult;
195 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
196 ACL_CREATE);
197 if (aclresult != ACLCHECK_OK)
198 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
199 get_namespace_name(namespaceId));
203 * Select tablespace to use. If not specified, use default tablespace
204 * (which may in turn default to database's default).
206 if (tableSpaceName)
208 tablespaceId = get_tablespace_oid(tableSpaceName);
209 if (!OidIsValid(tablespaceId))
210 ereport(ERROR,
211 (errcode(ERRCODE_UNDEFINED_OBJECT),
212 errmsg("tablespace \"%s\" does not exist",
213 tableSpaceName)));
215 else
217 tablespaceId = GetDefaultTablespace(rel->rd_istemp);
218 /* note InvalidOid is OK in this case */
221 /* Check permissions except when using database's default */
222 if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
224 AclResult aclresult;
226 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
227 ACL_CREATE);
228 if (aclresult != ACLCHECK_OK)
229 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
230 get_tablespace_name(tablespaceId));
234 * Force shared indexes into the pg_global tablespace. This is a bit of a
235 * hack but seems simpler than marking them in the BKI commands.
237 if (rel->rd_rel->relisshared)
238 tablespaceId = GLOBALTABLESPACE_OID;
241 * Select name for index if caller didn't specify
243 if (indexRelationName == NULL)
245 if (primary)
246 indexRelationName = ChooseRelationName(RelationGetRelationName(rel),
247 NULL,
248 "pkey",
249 namespaceId);
250 else
252 IndexElem *iparam = (IndexElem *) linitial(attributeList);
254 indexRelationName = ChooseRelationName(RelationGetRelationName(rel),
255 iparam->name,
256 "key",
257 namespaceId);
262 * look up the access method, verify it can handle the requested features
264 tuple = SearchSysCache(AMNAME,
265 PointerGetDatum(accessMethodName),
266 0, 0, 0);
267 if (!HeapTupleIsValid(tuple))
270 * Hack to provide more-or-less-transparent updating of old RTREE
271 * indexes to GIST: if RTREE is requested and not found, use GIST.
273 if (strcmp(accessMethodName, "rtree") == 0)
275 ereport(NOTICE,
276 (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
277 accessMethodName = "gist";
278 tuple = SearchSysCache(AMNAME,
279 PointerGetDatum(accessMethodName),
280 0, 0, 0);
283 if (!HeapTupleIsValid(tuple))
284 ereport(ERROR,
285 (errcode(ERRCODE_UNDEFINED_OBJECT),
286 errmsg("access method \"%s\" does not exist",
287 accessMethodName)));
289 accessMethodId = HeapTupleGetOid(tuple);
290 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
292 if (unique && !accessMethodForm->amcanunique)
293 ereport(ERROR,
294 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
295 errmsg("access method \"%s\" does not support unique indexes",
296 accessMethodName)));
297 if (numberOfAttributes > 1 && !accessMethodForm->amcanmulticol)
298 ereport(ERROR,
299 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
300 errmsg("access method \"%s\" does not support multicolumn indexes",
301 accessMethodName)));
303 amcanorder = accessMethodForm->amcanorder;
304 amoptions = accessMethodForm->amoptions;
306 ReleaseSysCache(tuple);
309 * Validate predicate, if given
311 if (predicate)
312 CheckPredicate(predicate);
315 * Extra checks when creating a PRIMARY KEY index.
317 if (primary)
319 List *cmds;
320 ListCell *keys;
323 * If ALTER TABLE, check that there isn't already a PRIMARY KEY. In
324 * CREATE TABLE, we have faith that the parser rejected multiple pkey
325 * clauses; and CREATE INDEX doesn't have a way to say PRIMARY KEY, so
326 * it's no problem either.
328 if (is_alter_table &&
329 relationHasPrimaryKey(rel))
331 ereport(ERROR,
332 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
333 errmsg("multiple primary keys for table \"%s\" are not allowed",
334 RelationGetRelationName(rel))));
338 * Check that all of the attributes in a primary key are marked as not
339 * null, otherwise attempt to ALTER TABLE .. SET NOT NULL
341 cmds = NIL;
342 foreach(keys, attributeList)
344 IndexElem *key = (IndexElem *) lfirst(keys);
345 HeapTuple atttuple;
347 if (!key->name)
348 ereport(ERROR,
349 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
350 errmsg("primary keys cannot be expressions")));
352 /* System attributes are never null, so no problem */
353 if (SystemAttributeByName(key->name, rel->rd_rel->relhasoids))
354 continue;
356 atttuple = SearchSysCacheAttName(relationId, key->name);
357 if (HeapTupleIsValid(atttuple))
359 if (!((Form_pg_attribute) GETSTRUCT(atttuple))->attnotnull)
361 /* Add a subcommand to make this one NOT NULL */
362 AlterTableCmd *cmd = makeNode(AlterTableCmd);
364 cmd->subtype = AT_SetNotNull;
365 cmd->name = key->name;
367 cmds = lappend(cmds, cmd);
369 ReleaseSysCache(atttuple);
371 else
374 * This shouldn't happen during CREATE TABLE, but can happen
375 * during ALTER TABLE. Keep message in sync with
376 * transformIndexConstraints() in parser/parse_utilcmd.c.
378 ereport(ERROR,
379 (errcode(ERRCODE_UNDEFINED_COLUMN),
380 errmsg("column \"%s\" named in key does not exist",
381 key->name)));
386 * XXX: Shouldn't the ALTER TABLE .. SET NOT NULL cascade to child
387 * tables? Currently, since the PRIMARY KEY itself doesn't cascade,
388 * we don't cascade the notnull constraint(s) either; but this is
389 * pretty debatable.
391 * XXX: possible future improvement: when being called from ALTER
392 * TABLE, it would be more efficient to merge this with the outer
393 * ALTER TABLE, so as to avoid two scans. But that seems to
394 * complicate DefineIndex's API unduly.
396 if (cmds)
397 AlterTableInternal(relationId, cmds, false);
401 * Parse AM-specific options, convert to text array form, validate.
403 reloptions = transformRelOptions((Datum) 0, options, NULL, NULL, false, false);
405 (void) index_reloptions(amoptions, reloptions, true);
408 * Prepare arguments for index_create, primarily an IndexInfo structure.
409 * Note that ii_Predicate must be in implicit-AND format.
411 indexInfo = makeNode(IndexInfo);
412 indexInfo->ii_NumIndexAttrs = numberOfAttributes;
413 indexInfo->ii_Expressions = NIL; /* for now */
414 indexInfo->ii_ExpressionsState = NIL;
415 indexInfo->ii_Predicate = make_ands_implicit(predicate);
416 indexInfo->ii_PredicateState = NIL;
417 indexInfo->ii_Unique = unique;
418 /* In a concurrent build, mark it not-ready-for-inserts */
419 indexInfo->ii_ReadyForInserts = !concurrent;
420 indexInfo->ii_Concurrent = concurrent;
421 indexInfo->ii_BrokenHotChain = false;
423 classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
424 coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
425 ComputeIndexAttrs(indexInfo, classObjectId, coloptions, attributeList,
426 relationId, accessMethodName, accessMethodId,
427 amcanorder, isconstraint);
430 * Report index creation if appropriate (delay this till after most of the
431 * error checks)
433 if (isconstraint && !quiet)
434 ereport(NOTICE,
435 (errmsg("%s %s will create implicit index \"%s\" for table \"%s\"",
436 is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
437 primary ? "PRIMARY KEY" : "UNIQUE",
438 indexRelationName, RelationGetRelationName(rel))));
440 /* save lockrelid and locktag for below, then close rel */
441 heaprelid = rel->rd_lockInfo.lockRelId;
442 SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
443 heap_close(rel, NoLock);
445 if (!concurrent)
447 indexRelationId =
448 index_create(relationId, indexRelationName, indexRelationId,
449 indexInfo, accessMethodId, tablespaceId, classObjectId,
450 coloptions, reloptions, primary, isconstraint,
451 allowSystemTableMods, skip_build, concurrent);
453 return; /* We're done, in the standard case */
457 * For a concurrent build, we next insert the catalog entry and add
458 * constraints. We don't build the index just yet; we must first make the
459 * catalog entry so that the new index is visible to updating
460 * transactions. That will prevent them from making incompatible HOT
461 * updates. The new index will be marked not indisready and not
462 * indisvalid, so that no one else tries to either insert into it or use
463 * it for queries. We pass skip_build = true to prevent the build.
465 indexRelationId =
466 index_create(relationId, indexRelationName, indexRelationId,
467 indexInfo, accessMethodId, tablespaceId, classObjectId,
468 coloptions, reloptions, primary, isconstraint,
469 allowSystemTableMods, true, concurrent);
472 * We must commit our current transaction so that the index becomes
473 * visible; then start another. Note that all the data structures we just
474 * built are lost in the commit. The only data we keep past here are the
475 * relation IDs.
477 * Before committing, get a session-level lock on the table, to ensure
478 * that neither it nor the index can be dropped before we finish. This
479 * cannot block, even if someone else is waiting for access, because we
480 * already have the same lock within our transaction.
482 * Note: we don't currently bother with a session lock on the index,
483 * because there are no operations that could change its state while we
484 * hold lock on the parent table. This might need to change later.
486 LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
488 PopActiveSnapshot();
489 CommitTransactionCommand();
490 StartTransactionCommand();
493 * Phase 2 of concurrent index build (see comments for validate_index()
494 * for an overview of how this works)
496 * Now we must wait until no running transaction could have the table open
497 * with the old list of indexes. To do this, inquire which xacts
498 * currently would conflict with ShareLock on the table -- ie, which ones
499 * have a lock that permits writing the table. Then wait for each of
500 * these xacts to commit or abort. Note we do not need to worry about
501 * xacts that open the table for writing after this point; they will see
502 * the new index when they open it.
504 * Note: the reason we use actual lock acquisition here, rather than just
505 * checking the ProcArray and sleeping, is that deadlock is possible if
506 * one of the transactions in question is blocked trying to acquire an
507 * exclusive lock on our table. The lock code will detect deadlock and
508 * error out properly.
510 * Note: GetLockConflicts() never reports our own xid, hence we need not
511 * check for that. Also, prepared xacts are not reported, which is fine
512 * since they certainly aren't going to do anything more.
514 old_lockholders = GetLockConflicts(&heaplocktag, ShareLock);
516 while (VirtualTransactionIdIsValid(*old_lockholders))
518 VirtualXactLockTableWait(*old_lockholders);
519 old_lockholders++;
523 * At this moment we are sure that there are no transactions with the
524 * table open for write that don't have this new index in their list of
525 * indexes. We have waited out all the existing transactions and any new
526 * transaction will have the new index in its list, but the index is still
527 * marked as "not-ready-for-inserts". The index is consulted while
528 * deciding HOT-safety though. This arrangement ensures that no new HOT
529 * chains can be created where the new tuple and the old tuple in the
530 * chain have different index keys.
532 * We now take a new snapshot, and build the index using all tuples that
533 * are visible in this snapshot. We can be sure that any HOT updates to
534 * these tuples will be compatible with the index, since any updates made
535 * by transactions that didn't know about the index are now committed or
536 * rolled back. Thus, each visible tuple is either the end of its
537 * HOT-chain or the extension of the chain is HOT-safe for this index.
540 /* Open and lock the parent heap relation */
541 rel = heap_openrv(heapRelation, ShareUpdateExclusiveLock);
543 /* And the target index relation */
544 indexRelation = index_open(indexRelationId, RowExclusiveLock);
546 /* Set ActiveSnapshot since functions in the indexes may need it */
547 PushActiveSnapshot(GetTransactionSnapshot());
549 /* We have to re-build the IndexInfo struct, since it was lost in commit */
550 indexInfo = BuildIndexInfo(indexRelation);
551 Assert(!indexInfo->ii_ReadyForInserts);
552 indexInfo->ii_Concurrent = true;
553 indexInfo->ii_BrokenHotChain = false;
555 /* Now build the index */
556 index_build(rel, indexRelation, indexInfo, primary);
558 /* Close both the relations, but keep the locks */
559 heap_close(rel, NoLock);
560 index_close(indexRelation, NoLock);
563 * Update the pg_index row to mark the index as ready for inserts. Once we
564 * commit this transaction, any new transactions that open the table must
565 * insert new entries into the index for insertions and non-HOT updates.
567 pg_index = heap_open(IndexRelationId, RowExclusiveLock);
569 indexTuple = SearchSysCacheCopy(INDEXRELID,
570 ObjectIdGetDatum(indexRelationId),
571 0, 0, 0);
572 if (!HeapTupleIsValid(indexTuple))
573 elog(ERROR, "cache lookup failed for index %u", indexRelationId);
574 indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
576 Assert(!indexForm->indisready);
577 Assert(!indexForm->indisvalid);
579 indexForm->indisready = true;
581 simple_heap_update(pg_index, &indexTuple->t_self, indexTuple);
582 CatalogUpdateIndexes(pg_index, indexTuple);
584 heap_close(pg_index, RowExclusiveLock);
586 /* we can do away with our snapshot */
587 PopActiveSnapshot();
590 * Commit this transaction to make the indisready update visible.
592 CommitTransactionCommand();
593 StartTransactionCommand();
596 * Phase 3 of concurrent index build
598 * We once again wait until no transaction can have the table open with
599 * the index marked as read-only for updates.
601 old_lockholders = GetLockConflicts(&heaplocktag, ShareLock);
603 while (VirtualTransactionIdIsValid(*old_lockholders))
605 VirtualXactLockTableWait(*old_lockholders);
606 old_lockholders++;
610 * Now take the "reference snapshot" that will be used by validate_index()
611 * to filter candidate tuples. Beware! There might still be snapshots in
612 * use that treat some transaction as in-progress that our reference
613 * snapshot treats as committed. If such a recently-committed transaction
614 * deleted tuples in the table, we will not include them in the index; yet
615 * those transactions which see the deleting one as still-in-progress will
616 * expect such tuples to be there once we mark the index as valid.
618 * We solve this by waiting for all endangered transactions to exit before
619 * we mark the index as valid.
621 * We also set ActiveSnapshot to this snap, since functions in indexes may
622 * need a snapshot.
624 snapshot = RegisterSnapshot(GetTransactionSnapshot());
625 PushActiveSnapshot(snapshot);
628 * Scan the index and the heap, insert any missing index entries.
630 validate_index(relationId, indexRelationId, snapshot);
633 * The index is now valid in the sense that it contains all currently
634 * interesting tuples. But since it might not contain tuples deleted just
635 * before the reference snap was taken, we have to wait out any
636 * transactions that might have older snapshots. Obtain a list of VXIDs
637 * of such transactions, and wait for them individually.
639 * We can exclude any running transactions that have xmin > the xmin of
640 * our reference snapshot; their oldest snapshot must be newer than ours.
641 * We can also exclude any transactions that have xmin = zero, since they
642 * evidently have no live snapshot at all (and any one they might be in
643 * process of taking is certainly newer than ours). Transactions in other
644 * DBs can be ignored too, since they'll never even be able to see this
645 * index.
647 * We can also exclude autovacuum processes and processes running manual
648 * lazy VACUUMs, because they won't be fazed by missing index entries
649 * either. (Manual ANALYZEs, however, can't be excluded because they
650 * might be within transactions that are going to do arbitrary operations
651 * later.)
653 * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
654 * check for that.
656 * If a process goes idle-in-transaction with xmin zero, we do not need to
657 * wait for it anymore, per the above argument. We do not have the
658 * infrastructure right now to stop waiting if that happens, but we can at
659 * least avoid the folly of waiting when it is idle at the time we would
660 * begin to wait. We do this by repeatedly rechecking the output of
661 * GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
662 * doesn't show up in the output, we know we can forget about it.
664 old_snapshots = GetCurrentVirtualXIDs(snapshot->xmin, true, false,
665 PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
666 &n_old_snapshots);
668 for (i = 0; i < n_old_snapshots; i++)
670 if (!VirtualTransactionIdIsValid(old_snapshots[i]))
671 continue; /* found uninteresting in previous cycle */
673 if (i > 0)
675 /* see if anything's changed ... */
676 VirtualTransactionId *newer_snapshots;
677 int n_newer_snapshots;
678 int j;
679 int k;
681 newer_snapshots = GetCurrentVirtualXIDs(snapshot->xmin,
682 true, false,
683 PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
684 &n_newer_snapshots);
685 for (j = i; j < n_old_snapshots; j++)
687 if (!VirtualTransactionIdIsValid(old_snapshots[j]))
688 continue; /* found uninteresting in previous cycle */
689 for (k = 0; k < n_newer_snapshots; k++)
691 if (VirtualTransactionIdEquals(old_snapshots[j],
692 newer_snapshots[k]))
693 break;
695 if (k >= n_newer_snapshots) /* not there anymore */
696 SetInvalidVirtualTransactionId(old_snapshots[j]);
698 pfree(newer_snapshots);
701 if (VirtualTransactionIdIsValid(old_snapshots[i]))
702 VirtualXactLockTableWait(old_snapshots[i]);
706 * Index can now be marked valid -- update its pg_index entry
708 pg_index = heap_open(IndexRelationId, RowExclusiveLock);
710 indexTuple = SearchSysCacheCopy(INDEXRELID,
711 ObjectIdGetDatum(indexRelationId),
712 0, 0, 0);
713 if (!HeapTupleIsValid(indexTuple))
714 elog(ERROR, "cache lookup failed for index %u", indexRelationId);
715 indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
717 Assert(indexForm->indisready);
718 Assert(!indexForm->indisvalid);
720 indexForm->indisvalid = true;
722 simple_heap_update(pg_index, &indexTuple->t_self, indexTuple);
723 CatalogUpdateIndexes(pg_index, indexTuple);
725 heap_close(pg_index, RowExclusiveLock);
728 * The pg_index update will cause backends (including this one) to update
729 * relcache entries for the index itself, but we should also send a
730 * relcache inval on the parent table to force replanning of cached plans.
731 * Otherwise existing sessions might fail to use the new index where it
732 * would be useful. (Note that our earlier commits did not create reasons
733 * to replan; relcache flush on the index itself was sufficient.)
735 CacheInvalidateRelcacheByRelid(heaprelid.relId);
737 /* we can now do away with our active snapshot */
738 PopActiveSnapshot();
740 /* And we can remove the validating snapshot too */
741 UnregisterSnapshot(snapshot);
744 * Last thing to do is release the session-level lock on the parent table.
746 UnlockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
751 * CheckPredicate
752 * Checks that the given partial-index predicate is valid.
754 * This used to also constrain the form of the predicate to forms that
755 * indxpath.c could do something with. However, that seems overly
756 * restrictive. One useful application of partial indexes is to apply
757 * a UNIQUE constraint across a subset of a table, and in that scenario
758 * any evaluatable predicate will work. So accept any predicate here
759 * (except ones requiring a plan), and let indxpath.c fend for itself.
761 static void
762 CheckPredicate(Expr *predicate)
765 * We don't currently support generation of an actual query plan for a
766 * predicate, only simple scalar expressions; hence these restrictions.
768 if (contain_subplans((Node *) predicate))
769 ereport(ERROR,
770 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
771 errmsg("cannot use subquery in index predicate")));
772 if (contain_agg_clause((Node *) predicate))
773 ereport(ERROR,
774 (errcode(ERRCODE_GROUPING_ERROR),
775 errmsg("cannot use aggregate in index predicate")));
778 * A predicate using mutable functions is probably wrong, for the same
779 * reasons that we don't allow an index expression to use one.
781 if (contain_mutable_functions((Node *) predicate))
782 ereport(ERROR,
783 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
784 errmsg("functions in index predicate must be marked IMMUTABLE")));
788 * Compute per-index-column information, including indexed column numbers
789 * or index expressions, opclasses, and indoptions.
791 static void
792 ComputeIndexAttrs(IndexInfo *indexInfo,
793 Oid *classOidP,
794 int16 *colOptionP,
795 List *attList, /* list of IndexElem's */
796 Oid relId,
797 char *accessMethodName,
798 Oid accessMethodId,
799 bool amcanorder,
800 bool isconstraint)
802 ListCell *rest;
803 int attn = 0;
806 * process attributeList
808 foreach(rest, attList)
810 IndexElem *attribute = (IndexElem *) lfirst(rest);
811 Oid atttype;
814 * Process the column-or-expression to be indexed.
816 if (attribute->name != NULL)
818 /* Simple index attribute */
819 HeapTuple atttuple;
820 Form_pg_attribute attform;
822 Assert(attribute->expr == NULL);
823 atttuple = SearchSysCacheAttName(relId, attribute->name);
824 if (!HeapTupleIsValid(atttuple))
826 /* difference in error message spellings is historical */
827 if (isconstraint)
828 ereport(ERROR,
829 (errcode(ERRCODE_UNDEFINED_COLUMN),
830 errmsg("column \"%s\" named in key does not exist",
831 attribute->name)));
832 else
833 ereport(ERROR,
834 (errcode(ERRCODE_UNDEFINED_COLUMN),
835 errmsg("column \"%s\" does not exist",
836 attribute->name)));
838 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
839 indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
840 atttype = attform->atttypid;
841 ReleaseSysCache(atttuple);
843 else if (attribute->expr && IsA(attribute->expr, Var) &&
844 ((Var *) attribute->expr)->varattno != InvalidAttrNumber)
846 /* Tricky tricky, he wrote (column) ... treat as simple attr */
847 Var *var = (Var *) attribute->expr;
849 indexInfo->ii_KeyAttrNumbers[attn] = var->varattno;
850 atttype = get_atttype(relId, var->varattno);
852 else
854 /* Index expression */
855 Assert(attribute->expr != NULL);
856 indexInfo->ii_KeyAttrNumbers[attn] = 0; /* marks expression */
857 indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
858 attribute->expr);
859 atttype = exprType(attribute->expr);
862 * We don't currently support generation of an actual query plan
863 * for an index expression, only simple scalar expressions; hence
864 * these restrictions.
866 if (contain_subplans(attribute->expr))
867 ereport(ERROR,
868 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
869 errmsg("cannot use subquery in index expression")));
870 if (contain_agg_clause(attribute->expr))
871 ereport(ERROR,
872 (errcode(ERRCODE_GROUPING_ERROR),
873 errmsg("cannot use aggregate function in index expression")));
876 * A expression using mutable functions is probably wrong, since
877 * if you aren't going to get the same result for the same data
878 * every time, it's not clear what the index entries mean at all.
880 if (contain_mutable_functions(attribute->expr))
881 ereport(ERROR,
882 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
883 errmsg("functions in index expression must be marked IMMUTABLE")));
887 * Identify the opclass to use.
889 classOidP[attn] = GetIndexOpClass(attribute->opclass,
890 atttype,
891 accessMethodName,
892 accessMethodId);
895 * Set up the per-column options (indoption field). For now, this is
896 * zero for any un-ordered index, while ordered indexes have DESC and
897 * NULLS FIRST/LAST options.
899 colOptionP[attn] = 0;
900 if (amcanorder)
902 /* default ordering is ASC */
903 if (attribute->ordering == SORTBY_DESC)
904 colOptionP[attn] |= INDOPTION_DESC;
905 /* default null ordering is LAST for ASC, FIRST for DESC */
906 if (attribute->nulls_ordering == SORTBY_NULLS_DEFAULT)
908 if (attribute->ordering == SORTBY_DESC)
909 colOptionP[attn] |= INDOPTION_NULLS_FIRST;
911 else if (attribute->nulls_ordering == SORTBY_NULLS_FIRST)
912 colOptionP[attn] |= INDOPTION_NULLS_FIRST;
914 else
916 /* index AM does not support ordering */
917 if (attribute->ordering != SORTBY_DEFAULT)
918 ereport(ERROR,
919 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
920 errmsg("access method \"%s\" does not support ASC/DESC options",
921 accessMethodName)));
922 if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
923 ereport(ERROR,
924 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
925 errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
926 accessMethodName)));
929 attn++;
934 * Resolve possibly-defaulted operator class specification
936 static Oid
937 GetIndexOpClass(List *opclass, Oid attrType,
938 char *accessMethodName, Oid accessMethodId)
940 char *schemaname;
941 char *opcname;
942 HeapTuple tuple;
943 Oid opClassId,
944 opInputType;
947 * Release 7.0 removed network_ops, timespan_ops, and datetime_ops, so we
948 * ignore those opclass names so the default *_ops is used. This can be
949 * removed in some later release. bjm 2000/02/07
951 * Release 7.1 removes lztext_ops, so suppress that too for a while. tgl
952 * 2000/07/30
954 * Release 7.2 renames timestamp_ops to timestamptz_ops, so suppress that
955 * too for awhile. I'm starting to think we need a better approach. tgl
956 * 2000/10/01
958 * Release 8.0 removes bigbox_ops (which was dead code for a long while
959 * anyway). tgl 2003/11/11
961 if (list_length(opclass) == 1)
963 char *claname = strVal(linitial(opclass));
965 if (strcmp(claname, "network_ops") == 0 ||
966 strcmp(claname, "timespan_ops") == 0 ||
967 strcmp(claname, "datetime_ops") == 0 ||
968 strcmp(claname, "lztext_ops") == 0 ||
969 strcmp(claname, "timestamp_ops") == 0 ||
970 strcmp(claname, "bigbox_ops") == 0)
971 opclass = NIL;
974 if (opclass == NIL)
976 /* no operator class specified, so find the default */
977 opClassId = GetDefaultOpClass(attrType, accessMethodId);
978 if (!OidIsValid(opClassId))
979 ereport(ERROR,
980 (errcode(ERRCODE_UNDEFINED_OBJECT),
981 errmsg("data type %s has no default operator class for access method \"%s\"",
982 format_type_be(attrType), accessMethodName),
983 errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
984 return opClassId;
988 * Specific opclass name given, so look up the opclass.
991 /* deconstruct the name list */
992 DeconstructQualifiedName(opclass, &schemaname, &opcname);
994 if (schemaname)
996 /* Look in specific schema only */
997 Oid namespaceId;
999 namespaceId = LookupExplicitNamespace(schemaname);
1000 tuple = SearchSysCache(CLAAMNAMENSP,
1001 ObjectIdGetDatum(accessMethodId),
1002 PointerGetDatum(opcname),
1003 ObjectIdGetDatum(namespaceId),
1006 else
1008 /* Unqualified opclass name, so search the search path */
1009 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
1010 if (!OidIsValid(opClassId))
1011 ereport(ERROR,
1012 (errcode(ERRCODE_UNDEFINED_OBJECT),
1013 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
1014 opcname, accessMethodName)));
1015 tuple = SearchSysCache(CLAOID,
1016 ObjectIdGetDatum(opClassId),
1017 0, 0, 0);
1020 if (!HeapTupleIsValid(tuple))
1021 ereport(ERROR,
1022 (errcode(ERRCODE_UNDEFINED_OBJECT),
1023 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
1024 NameListToString(opclass), accessMethodName)));
1027 * Verify that the index operator class accepts this datatype. Note we
1028 * will accept binary compatibility.
1030 opClassId = HeapTupleGetOid(tuple);
1031 opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
1033 if (!IsBinaryCoercible(attrType, opInputType))
1034 ereport(ERROR,
1035 (errcode(ERRCODE_DATATYPE_MISMATCH),
1036 errmsg("operator class \"%s\" does not accept data type %s",
1037 NameListToString(opclass), format_type_be(attrType))));
1039 ReleaseSysCache(tuple);
1041 return opClassId;
1045 * GetDefaultOpClass
1047 * Given the OIDs of a datatype and an access method, find the default
1048 * operator class, if any. Returns InvalidOid if there is none.
1051 GetDefaultOpClass(Oid type_id, Oid am_id)
1053 Oid result = InvalidOid;
1054 int nexact = 0;
1055 int ncompatible = 0;
1056 int ncompatiblepreferred = 0;
1057 Relation rel;
1058 ScanKeyData skey[1];
1059 SysScanDesc scan;
1060 HeapTuple tup;
1061 TYPCATEGORY tcategory;
1063 /* If it's a domain, look at the base type instead */
1064 type_id = getBaseType(type_id);
1066 tcategory = TypeCategory(type_id);
1069 * We scan through all the opclasses available for the access method,
1070 * looking for one that is marked default and matches the target type
1071 * (either exactly or binary-compatibly, but prefer an exact match).
1073 * We could find more than one binary-compatible match. If just one is
1074 * for a preferred type, use that one; otherwise we fail, forcing the user
1075 * to specify which one he wants. (The preferred-type special case is a
1076 * kluge for varchar: it's binary-compatible to both text and bpchar, so
1077 * we need a tiebreaker.) If we find more than one exact match, then
1078 * someone put bogus entries in pg_opclass.
1080 rel = heap_open(OperatorClassRelationId, AccessShareLock);
1082 ScanKeyInit(&skey[0],
1083 Anum_pg_opclass_opcmethod,
1084 BTEqualStrategyNumber, F_OIDEQ,
1085 ObjectIdGetDatum(am_id));
1087 scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
1088 SnapshotNow, 1, skey);
1090 while (HeapTupleIsValid(tup = systable_getnext(scan)))
1092 Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
1094 /* ignore altogether if not a default opclass */
1095 if (!opclass->opcdefault)
1096 continue;
1097 if (opclass->opcintype == type_id)
1099 nexact++;
1100 result = HeapTupleGetOid(tup);
1102 else if (nexact == 0 &&
1103 IsBinaryCoercible(type_id, opclass->opcintype))
1105 if (IsPreferredType(tcategory, opclass->opcintype))
1107 ncompatiblepreferred++;
1108 result = HeapTupleGetOid(tup);
1110 else if (ncompatiblepreferred == 0)
1112 ncompatible++;
1113 result = HeapTupleGetOid(tup);
1118 systable_endscan(scan);
1120 heap_close(rel, AccessShareLock);
1122 /* raise error if pg_opclass contains inconsistent data */
1123 if (nexact > 1)
1124 ereport(ERROR,
1125 (errcode(ERRCODE_DUPLICATE_OBJECT),
1126 errmsg("there are multiple default operator classes for data type %s",
1127 format_type_be(type_id))));
1129 if (nexact == 1 ||
1130 ncompatiblepreferred == 1 ||
1131 (ncompatiblepreferred == 0 && ncompatible == 1))
1132 return result;
1134 return InvalidOid;
1138 * makeObjectName()
1140 * Create a name for an implicitly created index, sequence, constraint, etc.
1142 * The parameters are typically: the original table name, the original field
1143 * name, and a "type" string (such as "seq" or "pkey"). The field name
1144 * and/or type can be NULL if not relevant.
1146 * The result is a palloc'd string.
1148 * The basic result we want is "name1_name2_label", omitting "_name2" or
1149 * "_label" when those parameters are NULL. However, we must generate
1150 * a name with less than NAMEDATALEN characters! So, we truncate one or
1151 * both names if necessary to make a short-enough string. The label part
1152 * is never truncated (so it had better be reasonably short).
1154 * The caller is responsible for checking uniqueness of the generated
1155 * name and retrying as needed; retrying will be done by altering the
1156 * "label" string (which is why we never truncate that part).
1158 char *
1159 makeObjectName(const char *name1, const char *name2, const char *label)
1161 char *name;
1162 int overhead = 0; /* chars needed for label and underscores */
1163 int availchars; /* chars available for name(s) */
1164 int name1chars; /* chars allocated to name1 */
1165 int name2chars; /* chars allocated to name2 */
1166 int ndx;
1168 name1chars = strlen(name1);
1169 if (name2)
1171 name2chars = strlen(name2);
1172 overhead++; /* allow for separating underscore */
1174 else
1175 name2chars = 0;
1176 if (label)
1177 overhead += strlen(label) + 1;
1179 availchars = NAMEDATALEN - 1 - overhead;
1180 Assert(availchars > 0); /* else caller chose a bad label */
1183 * If we must truncate, preferentially truncate the longer name. This
1184 * logic could be expressed without a loop, but it's simple and obvious as
1185 * a loop.
1187 while (name1chars + name2chars > availchars)
1189 if (name1chars > name2chars)
1190 name1chars--;
1191 else
1192 name2chars--;
1195 name1chars = pg_mbcliplen(name1, name1chars, name1chars);
1196 if (name2)
1197 name2chars = pg_mbcliplen(name2, name2chars, name2chars);
1199 /* Now construct the string using the chosen lengths */
1200 name = palloc(name1chars + name2chars + overhead + 1);
1201 memcpy(name, name1, name1chars);
1202 ndx = name1chars;
1203 if (name2)
1205 name[ndx++] = '_';
1206 memcpy(name + ndx, name2, name2chars);
1207 ndx += name2chars;
1209 if (label)
1211 name[ndx++] = '_';
1212 strcpy(name + ndx, label);
1214 else
1215 name[ndx] = '\0';
1217 return name;
1221 * Select a nonconflicting name for a new relation. This is ordinarily
1222 * used to choose index names (which is why it's here) but it can also
1223 * be used for sequences, or any autogenerated relation kind.
1225 * name1, name2, and label are used the same way as for makeObjectName(),
1226 * except that the label can't be NULL; digits will be appended to the label
1227 * if needed to create a name that is unique within the specified namespace.
1229 * Note: it is theoretically possible to get a collision anyway, if someone
1230 * else chooses the same name concurrently. This is fairly unlikely to be
1231 * a problem in practice, especially if one is holding an exclusive lock on
1232 * the relation identified by name1. However, if choosing multiple names
1233 * within a single command, you'd better create the new object and do
1234 * CommandCounterIncrement before choosing the next one!
1236 * Returns a palloc'd string.
1238 char *
1239 ChooseRelationName(const char *name1, const char *name2,
1240 const char *label, Oid namespace)
1242 int pass = 0;
1243 char *relname = NULL;
1244 char modlabel[NAMEDATALEN];
1246 /* try the unmodified label first */
1247 StrNCpy(modlabel, label, sizeof(modlabel));
1249 for (;;)
1251 relname = makeObjectName(name1, name2, modlabel);
1253 if (!OidIsValid(get_relname_relid(relname, namespace)))
1254 break;
1256 /* found a conflict, so try a new name component */
1257 pfree(relname);
1258 snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
1261 return relname;
1265 * relationHasPrimaryKey -
1267 * See whether an existing relation has a primary key.
1269 static bool
1270 relationHasPrimaryKey(Relation rel)
1272 bool result = false;
1273 List *indexoidlist;
1274 ListCell *indexoidscan;
1277 * Get the list of index OIDs for the table from the relcache, and look up
1278 * each one in the pg_index syscache until we find one marked primary key
1279 * (hopefully there isn't more than one such).
1281 indexoidlist = RelationGetIndexList(rel);
1283 foreach(indexoidscan, indexoidlist)
1285 Oid indexoid = lfirst_oid(indexoidscan);
1286 HeapTuple indexTuple;
1288 indexTuple = SearchSysCache(INDEXRELID,
1289 ObjectIdGetDatum(indexoid),
1290 0, 0, 0);
1291 if (!HeapTupleIsValid(indexTuple)) /* should not happen */
1292 elog(ERROR, "cache lookup failed for index %u", indexoid);
1293 result = ((Form_pg_index) GETSTRUCT(indexTuple))->indisprimary;
1294 ReleaseSysCache(indexTuple);
1295 if (result)
1296 break;
1299 list_free(indexoidlist);
1301 return result;
1305 * ReindexIndex
1306 * Recreate a specific index.
1308 void
1309 ReindexIndex(RangeVar *indexRelation)
1311 Oid indOid;
1312 HeapTuple tuple;
1314 indOid = RangeVarGetRelid(indexRelation, false);
1315 tuple = SearchSysCache(RELOID,
1316 ObjectIdGetDatum(indOid),
1317 0, 0, 0);
1318 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1319 elog(ERROR, "cache lookup failed for relation %u", indOid);
1321 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
1322 ereport(ERROR,
1323 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1324 errmsg("\"%s\" is not an index",
1325 indexRelation->relname)));
1327 /* Check permissions */
1328 if (!pg_class_ownercheck(indOid, GetUserId()))
1329 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
1330 indexRelation->relname);
1332 ReleaseSysCache(tuple);
1334 reindex_index(indOid);
1338 * ReindexTable
1339 * Recreate all indexes of a table (and of its toast table, if any)
1341 void
1342 ReindexTable(RangeVar *relation)
1344 Oid heapOid;
1345 HeapTuple tuple;
1347 heapOid = RangeVarGetRelid(relation, false);
1348 tuple = SearchSysCache(RELOID,
1349 ObjectIdGetDatum(heapOid),
1350 0, 0, 0);
1351 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1352 elog(ERROR, "cache lookup failed for relation %u", heapOid);
1354 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_RELATION &&
1355 ((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_TOASTVALUE)
1356 ereport(ERROR,
1357 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1358 errmsg("\"%s\" is not a table",
1359 relation->relname)));
1361 /* Check permissions */
1362 if (!pg_class_ownercheck(heapOid, GetUserId()))
1363 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
1364 relation->relname);
1366 /* Can't reindex shared tables except in standalone mode */
1367 if (((Form_pg_class) GETSTRUCT(tuple))->relisshared && IsUnderPostmaster)
1368 ereport(ERROR,
1369 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1370 errmsg("shared table \"%s\" can only be reindexed in stand-alone mode",
1371 relation->relname)));
1373 ReleaseSysCache(tuple);
1375 if (!reindex_relation(heapOid, true))
1376 ereport(NOTICE,
1377 (errmsg("table \"%s\" has no indexes",
1378 relation->relname)));
1382 * ReindexDatabase
1383 * Recreate indexes of a database.
1385 * To reduce the probability of deadlocks, each table is reindexed in a
1386 * separate transaction, so we can release the lock on it right away.
1387 * That means this must not be called within a user transaction block!
1389 void
1390 ReindexDatabase(const char *databaseName, bool do_system, bool do_user)
1392 Relation relationRelation;
1393 HeapScanDesc scan;
1394 HeapTuple tuple;
1395 MemoryContext private_context;
1396 MemoryContext old;
1397 List *relids = NIL;
1398 ListCell *l;
1400 AssertArg(databaseName);
1402 if (strcmp(databaseName, get_database_name(MyDatabaseId)) != 0)
1403 ereport(ERROR,
1404 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1405 errmsg("can only reindex the currently open database")));
1407 if (!pg_database_ownercheck(MyDatabaseId, GetUserId()))
1408 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1409 databaseName);
1412 * Create a memory context that will survive forced transaction commits we
1413 * do below. Since it is a child of PortalContext, it will go away
1414 * eventually even if we suffer an error; there's no need for special
1415 * abort cleanup logic.
1417 private_context = AllocSetContextCreate(PortalContext,
1418 "ReindexDatabase",
1419 ALLOCSET_DEFAULT_MINSIZE,
1420 ALLOCSET_DEFAULT_INITSIZE,
1421 ALLOCSET_DEFAULT_MAXSIZE);
1424 * We always want to reindex pg_class first. This ensures that if there
1425 * is any corruption in pg_class' indexes, they will be fixed before we
1426 * process any other tables. This is critical because reindexing itself
1427 * will try to update pg_class.
1429 if (do_system)
1431 old = MemoryContextSwitchTo(private_context);
1432 relids = lappend_oid(relids, RelationRelationId);
1433 MemoryContextSwitchTo(old);
1437 * Scan pg_class to build a list of the relations we need to reindex.
1439 * We only consider plain relations here (toast rels will be processed
1440 * indirectly by reindex_relation).
1442 relationRelation = heap_open(RelationRelationId, AccessShareLock);
1443 scan = heap_beginscan(relationRelation, SnapshotNow, 0, NULL);
1444 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1446 Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
1448 if (classtuple->relkind != RELKIND_RELATION)
1449 continue;
1451 /* Skip temp tables of other backends; we can't reindex them at all */
1452 if (classtuple->relistemp &&
1453 !isTempNamespace(classtuple->relnamespace))
1454 continue;
1456 /* Check user/system classification, and optionally skip */
1457 if (IsSystemClass(classtuple))
1459 if (!do_system)
1460 continue;
1462 else
1464 if (!do_user)
1465 continue;
1468 if (IsUnderPostmaster) /* silently ignore shared tables */
1470 if (classtuple->relisshared)
1471 continue;
1474 if (HeapTupleGetOid(tuple) == RelationRelationId)
1475 continue; /* got it already */
1477 old = MemoryContextSwitchTo(private_context);
1478 relids = lappend_oid(relids, HeapTupleGetOid(tuple));
1479 MemoryContextSwitchTo(old);
1481 heap_endscan(scan);
1482 heap_close(relationRelation, AccessShareLock);
1484 /* Now reindex each rel in a separate transaction */
1485 PopActiveSnapshot();
1486 CommitTransactionCommand();
1487 foreach(l, relids)
1489 Oid relid = lfirst_oid(l);
1491 StartTransactionCommand();
1492 /* functions in indexes may want a snapshot set */
1493 PushActiveSnapshot(GetTransactionSnapshot());
1494 if (reindex_relation(relid, true))
1495 ereport(NOTICE,
1496 (errmsg("table \"%s\" was reindexed",
1497 get_rel_name(relid))));
1498 PopActiveSnapshot();
1499 CommitTransactionCommand();
1501 StartTransactionCommand();
1503 MemoryContextDelete(private_context);