1 /*-------------------------------------------------------------------------
4 * code to support accessing and searching namespaces
6 * This is separate from pg_namespace.c, which contains the routines that
7 * directly manipulate the pg_namespace system catalog. This module
8 * provides routines associated with defining a "namespace search path"
9 * and implementing search-path-controlled searches.
12 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * src/backend/catalog/namespace.c
18 *-------------------------------------------------------------------------
22 #include "access/htup_details.h"
23 #include "access/xact.h"
24 #include "catalog/dependency.h"
25 #include "catalog/pg_authid.h"
26 #include "catalog/pg_collation.h"
27 #include "catalog/pg_conversion.h"
28 #include "catalog/pg_conversion_fn.h"
29 #include "catalog/pg_namespace.h"
30 #include "catalog/pg_opclass.h"
31 #include "catalog/pg_operator.h"
32 #include "catalog/pg_opfamily.h"
33 #include "catalog/pg_proc.h"
34 #include "catalog/pg_ts_config.h"
35 #include "catalog/pg_ts_dict.h"
36 #include "catalog/pg_ts_parser.h"
37 #include "catalog/pg_ts_template.h"
38 #include "catalog/pg_type.h"
39 #include "commands/dbcommands.h"
41 #include "mb/pg_wchar.h"
42 #include "miscadmin.h"
43 #include "nodes/makefuncs.h"
44 #include "parser/parse_func.h"
45 #include "storage/ipc.h"
46 #include "storage/lmgr.h"
47 #include "storage/sinval.h"
48 #include "utils/acl.h"
49 #include "utils/builtins.h"
50 #include "utils/catcache.h"
51 #include "utils/guc.h"
52 #include "utils/inval.h"
53 #include "utils/lsyscache.h"
54 #include "utils/memutils.h"
55 #include "utils/syscache.h"
59 * The namespace search path is a possibly-empty list of namespace OIDs.
60 * In addition to the explicit list, implicitly-searched namespaces
63 * 1. If a TEMP table namespace has been initialized in this session, it
64 * is implicitly searched first. (The only time this doesn't happen is
65 * when we are obeying an override search path spec that says not to use the
66 * temp namespace, or the temp namespace is included in the explicit list.)
68 * 2. The system catalog namespace is always searched. If the system
69 * namespace is present in the explicit path then it will be searched in
70 * the specified order; otherwise it will be searched after TEMP tables and
71 * *before* the explicit list. (It might seem that the system namespace
72 * should be implicitly last, but this behavior appears to be required by
73 * SQL99. Also, this provides a way to search the system namespace first
74 * without thereby making it the default creation target namespace.)
76 * For security reasons, searches using the search path will ignore the temp
77 * namespace when searching for any object type other than relations and
78 * types. (We must allow types since temp tables have rowtypes.)
80 * The default creation target namespace is always the first element of the
81 * explicit list. If the explicit list is empty, there is no default target.
83 * The textual specification of search_path can include "$user" to refer to
84 * the namespace named the same as the current user, if any. (This is just
85 * ignored if there is no such namespace.) Also, it can include "pg_temp"
86 * to refer to the current backend's temp namespace. This is usually also
87 * ignorable if the temp namespace hasn't been set up, but there's a special
88 * case: if "pg_temp" appears first then it should be the default creation
89 * target. We kluge this case a little bit so that the temp namespace isn't
90 * set up until the first attempt to create something in it. (The reason for
91 * klugery is that we can't create the temp namespace outside a transaction,
92 * but initial GUC processing of search_path happens outside a transaction.)
93 * activeTempCreationPending is TRUE if "pg_temp" appears first in the string
94 * but is not reflected in activeCreationNamespace because the namespace isn't
97 * In bootstrap mode, the search path is set equal to "pg_catalog", so that
98 * the system namespace is the only one searched or inserted into.
99 * initdb is also careful to set search_path to "pg_catalog" for its
100 * post-bootstrap standalone backend runs. Otherwise the default search
101 * path is determined by GUC. The factory default path contains the PUBLIC
102 * namespace (if it exists), preceded by the user's personal namespace
105 * We support a stack of "override" search path settings for use within
106 * specific sections of backend code. namespace_search_path is ignored
107 * whenever the override stack is nonempty. activeSearchPath is always
108 * the actually active path; it points either to the search list of the
109 * topmost stack entry, or to baseSearchPath which is the list derived
110 * from namespace_search_path.
112 * If baseSearchPathValid is false, then baseSearchPath (and other
113 * derived variables) need to be recomputed from namespace_search_path.
114 * We mark it invalid upon an assignment to namespace_search_path or receipt
115 * of a syscache invalidation event for pg_namespace. The recomputation
116 * is done during the next non-overridden lookup attempt. Note that an
117 * override spec is never subject to recomputation.
119 * Any namespaces mentioned in namespace_search_path that are not readable
120 * by the current user ID are simply left out of baseSearchPath; so
121 * we have to be willing to recompute the path when current userid changes.
122 * namespaceUser is the userid the path has been computed for.
124 * Note: all data pointed to by these List variables is in TopMemoryContext.
127 /* These variables define the actually active state: */
129 static List *activeSearchPath = NIL;
131 /* default place to create stuff; if InvalidOid, no default */
132 static Oid activeCreationNamespace = InvalidOid;
134 /* if TRUE, activeCreationNamespace is wrong, it should be temp namespace */
135 static bool activeTempCreationPending = false;
137 /* These variables are the values last derived from namespace_search_path: */
139 static List *baseSearchPath = NIL;
141 static Oid baseCreationNamespace = InvalidOid;
143 static bool baseTempCreationPending = false;
145 static Oid namespaceUser = InvalidOid;
147 /* The above four values are valid only if baseSearchPathValid */
148 static bool baseSearchPathValid = true;
150 /* Override requests are remembered in a stack of OverrideStackEntry structs */
154 List *searchPath; /* the desired search path */
155 Oid creationNamespace; /* the desired creation namespace */
156 int nestLevel; /* subtransaction nesting level */
157 } OverrideStackEntry;
159 static List *overrideStack = NIL;
162 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
163 * in a particular backend session (this happens when a CREATE TEMP TABLE
164 * command is first executed). Thereafter it's the OID of the temp namespace.
166 * myTempToastNamespace is the OID of the namespace for my temp tables' toast
167 * tables. It is set when myTempNamespace is, and is InvalidOid before that.
169 * myTempNamespaceSubID shows whether we've created the TEMP namespace in the
170 * current subtransaction. The flag propagates up the subtransaction tree,
171 * so the main transaction will correctly recognize the flag if all
172 * intermediate subtransactions commit. When it is InvalidSubTransactionId,
173 * we either haven't made the TEMP namespace yet, or have successfully
174 * committed its creation, depending on whether myTempNamespace is valid.
176 static Oid myTempNamespace = InvalidOid;
178 static Oid myTempToastNamespace = InvalidOid;
180 static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId;
183 * This is the user's textual search path specification --- it's the value
184 * of the GUC variable 'search_path'.
186 char *namespace_search_path = NULL;
189 /* Local functions */
190 static void recomputeNamespacePath(void);
191 static void InitTempTableNamespace(void);
192 static void RemoveTempRelations(Oid tempNamespaceId);
193 static void RemoveTempRelationsCallback(int code, Datum arg);
194 static void NamespaceCallback(Datum arg, int cacheid, uint32 hashvalue);
195 static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
198 /* These don't really need to appear in any header file */
199 Datum pg_table_is_visible(PG_FUNCTION_ARGS);
200 Datum pg_type_is_visible(PG_FUNCTION_ARGS);
201 Datum pg_function_is_visible(PG_FUNCTION_ARGS);
202 Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
203 Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
204 Datum pg_opfamily_is_visible(PG_FUNCTION_ARGS);
205 Datum pg_collation_is_visible(PG_FUNCTION_ARGS);
206 Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
207 Datum pg_ts_parser_is_visible(PG_FUNCTION_ARGS);
208 Datum pg_ts_dict_is_visible(PG_FUNCTION_ARGS);
209 Datum pg_ts_template_is_visible(PG_FUNCTION_ARGS);
210 Datum pg_ts_config_is_visible(PG_FUNCTION_ARGS);
211 Datum pg_my_temp_schema(PG_FUNCTION_ARGS);
212 Datum pg_is_other_temp_schema(PG_FUNCTION_ARGS);
217 * Given a RangeVar describing an existing relation,
218 * select the proper namespace and look up the relation OID.
220 * If the relation is not found, return InvalidOid if missing_ok = true,
221 * otherwise raise an error.
223 * If nowait = true, throw an error if we'd have to wait for a lock.
225 * Callback allows caller to check permissions or acquire additional locks
226 * prior to grabbing the relation lock.
229 RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
230 bool missing_ok, bool nowait,
231 RangeVarGetRelidCallback callback, void *callback_arg)
235 Oid oldRelId = InvalidOid;
239 * We check the catalog name and then ignore it.
241 if (relation->catalogname)
243 if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
245 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
246 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
247 relation->catalogname, relation->schemaname,
248 relation->relname)));
252 * DDL operations can change the results of a name lookup. Since all such
253 * operations will generate invalidation messages, we keep track of
254 * whether any such messages show up while we're performing the operation,
255 * and retry until either (1) no more invalidation messages show up or (2)
256 * the answer doesn't change.
258 * But if lockmode = NoLock, then we assume that either the caller is OK
259 * with the answer changing under them, or that they already hold some
260 * appropriate lock, and therefore return the first answer we get without
261 * checking for invalidation messages. Also, if the requested lock is
262 * already held, no LockRelationOid will not AcceptInvalidationMessages,
263 * so we may fail to notice a change. We could protect against that case
264 * by calling AcceptInvalidationMessages() before beginning this loop, but
265 * that would add a significant amount overhead, so for now we don't.
270 * Remember this value, so that, after looking up the relation name
271 * and locking its OID, we can check whether any invalidation messages
272 * have been processed that might require a do-over.
274 inval_count = SharedInvalidMessageCounter;
277 * Some non-default relpersistence value may have been specified. The
278 * parser never generates such a RangeVar in simple DML, but it can
279 * happen in contexts such as "CREATE TEMP TABLE foo (f1 int PRIMARY
280 * KEY)". Such a command will generate an added CREATE INDEX
281 * operation, which must be careful to find the temp table, even when
282 * pg_temp is not first in the search path.
284 if (relation->relpersistence == RELPERSISTENCE_TEMP)
286 if (!OidIsValid(myTempNamespace))
287 relId = InvalidOid; /* this probably can't happen? */
290 if (relation->schemaname)
294 namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
296 * For missing_ok, allow a non-existant schema name
297 * to throw the error below (namespaceId == InvalidOid).
299 if (namespaceId != myTempNamespace)
301 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
302 errmsg("temporary tables cannot specify a schema name")));
305 relId = get_relname_relid(relation->relname, myTempNamespace);
308 else if (relation->schemaname)
312 /* use exact schema given */
313 namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
314 if (missing_ok && !OidIsValid(namespaceId))
317 relId = get_relname_relid(relation->relname, namespaceId);
321 /* search the namespace path */
322 relId = RelnameGetRelid(relation->relname);
326 * Invoke caller-supplied callback, if any.
328 * This callback is a good place to check permissions: we haven't
329 * taken the table lock yet (and it's really best to check permissions
330 * before locking anything!), but we've gotten far enough to know what
331 * OID we think we should lock. Of course, concurrent DDL might
332 * change things while we're waiting for the lock, but in that case
333 * the callback will be invoked again for the new OID.
336 callback(relation, relId, oldRelId, callback_arg);
339 * If no lock requested, we assume the caller knows what they're
340 * doing. They should have already acquired a heavyweight lock on
341 * this relation earlier in the processing of this same statement, so
342 * it wouldn't be appropriate to AcceptInvalidationMessages() here, as
343 * that might pull the rug out from under them.
345 if (lockmode == NoLock)
349 * If, upon retry, we get back the same OID we did last time, then the
350 * invalidation messages we processed did not change the final answer.
353 * If we got a different OID, we've locked the relation that used to
354 * have this name rather than the one that does now. So release the
359 if (relId == oldRelId)
361 if (OidIsValid(oldRelId))
362 UnlockRelationOid(oldRelId, lockmode);
366 * Lock relation. This will also accept any pending invalidation
367 * messages. If we got back InvalidOid, indicating not found, then
368 * there's nothing to lock, but we accept invalidation messages
369 * anyway, to flush any negative catcache entries that may be
372 if (!OidIsValid(relId))
373 AcceptInvalidationMessages();
375 LockRelationOid(relId, lockmode);
376 else if (!ConditionalLockRelationOid(relId, lockmode))
378 if (relation->schemaname)
380 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
381 errmsg("could not obtain lock on relation \"%s.%s\"",
382 relation->schemaname, relation->relname)));
385 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
386 errmsg("could not obtain lock on relation \"%s\"",
387 relation->relname)));
391 * If no invalidation message were processed, we're done!
393 if (inval_count == SharedInvalidMessageCounter)
397 * Something may have changed. Let's repeat the name lookup, to make
398 * sure this name still references the same relation it did
405 if (!OidIsValid(relId) && !missing_ok)
407 if (relation->schemaname)
409 (errcode(ERRCODE_UNDEFINED_TABLE),
410 errmsg("relation \"%s.%s\" does not exist",
411 relation->schemaname, relation->relname)));
414 (errcode(ERRCODE_UNDEFINED_TABLE),
415 errmsg("relation \"%s\" does not exist",
416 relation->relname)));
422 * RangeVarGetCreationNamespace
423 * Given a RangeVar describing a to-be-created relation,
424 * choose which namespace to create it in.
426 * Note: calling this may result in a CommandCounterIncrement operation.
427 * That will happen on the first request for a temp table in any particular
428 * backend run; we will need to either create or clean out the temp schema.
431 RangeVarGetCreationNamespace(const RangeVar *newRelation)
436 * We check the catalog name and then ignore it.
438 if (newRelation->catalogname)
440 if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
442 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
443 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
444 newRelation->catalogname, newRelation->schemaname,
445 newRelation->relname)));
448 if (newRelation->schemaname)
450 /* check for pg_temp alias */
451 if (strcmp(newRelation->schemaname, "pg_temp") == 0)
453 /* Initialize temp namespace if first time through */
454 if (!OidIsValid(myTempNamespace))
455 InitTempTableNamespace();
456 return myTempNamespace;
458 /* use exact schema given */
459 namespaceId = get_namespace_oid(newRelation->schemaname, false);
460 /* we do not check for USAGE rights here! */
462 else if (newRelation->relpersistence == RELPERSISTENCE_TEMP)
464 /* Initialize temp namespace if first time through */
465 if (!OidIsValid(myTempNamespace))
466 InitTempTableNamespace();
467 return myTempNamespace;
471 /* use the default creation namespace */
472 recomputeNamespacePath();
473 if (activeTempCreationPending)
475 /* Need to initialize temp namespace */
476 InitTempTableNamespace();
477 return myTempNamespace;
479 namespaceId = activeCreationNamespace;
480 if (!OidIsValid(namespaceId))
482 (errcode(ERRCODE_UNDEFINED_SCHEMA),
483 errmsg("no schema has been selected to create in")));
486 /* Note: callers will check for CREATE rights when appropriate */
492 * RangeVarGetAndCheckCreationNamespace
494 * This function returns the OID of the namespace in which a new relation
495 * with a given name should be created. If the user does not have CREATE
496 * permission on the target namespace, this function will instead signal
499 * If non-NULL, *existing_oid is set to the OID of any existing relation with
500 * the same name which already exists in that namespace, or to InvalidOid if
501 * no such relation exists.
503 * If lockmode != NoLock, the specified lock mode is acquire on the existing
504 * relation, if any, provided that the current user owns the target relation.
505 * However, if lockmode != NoLock and the user does not own the target
506 * relation, we throw an ERROR, as we must not try to lock relations the
507 * user does not have permissions on.
509 * As a side effect, this function acquires AccessShareLock on the target
510 * namespace. Without this, the namespace could be dropped before our
511 * transaction commits, leaving behind relations with relnamespace pointing
512 * to a no-longer-exstant namespace.
514 * As a further side-effect, if the select namespace is a temporary namespace,
515 * we mark the RangeVar as RELPERSISTENCE_TEMP.
518 RangeVarGetAndCheckCreationNamespace(RangeVar *relation,
520 Oid *existing_relation_id)
524 Oid oldrelid = InvalidOid;
526 Oid oldnspid = InvalidOid;
530 * We check the catalog name and then ignore it.
532 if (relation->catalogname)
534 if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
536 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
537 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
538 relation->catalogname, relation->schemaname,
539 relation->relname)));
543 * As in RangeVarGetRelidExtended(), we guard against concurrent DDL
544 * operations by tracking whether any invalidation messages are processed
545 * while we're doing the name lookups and acquiring locks. See comments
546 * in that function for a more detailed explanation of this logic.
552 inval_count = SharedInvalidMessageCounter;
554 /* Look up creation namespace and check for existing relation. */
555 nspid = RangeVarGetCreationNamespace(relation);
556 Assert(OidIsValid(nspid));
557 if (existing_relation_id != NULL)
558 relid = get_relname_relid(relation->relname, nspid);
563 * In bootstrap processing mode, we don't bother with permissions or
564 * locking. Permissions might not be working yet, and locking is
567 if (IsBootstrapProcessingMode())
570 /* Check namespace permissions. */
571 aclresult = pg_namespace_aclcheck(nspid, GetUserId(), ACL_CREATE);
572 if (aclresult != ACLCHECK_OK)
573 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
574 get_namespace_name(nspid));
578 /* If nothing changed, we're done. */
579 if (relid == oldrelid && nspid == oldnspid)
581 /* If creation namespace has changed, give up old lock. */
582 if (nspid != oldnspid)
583 UnlockDatabaseObject(NamespaceRelationId, oldnspid, 0,
585 /* If name points to something different, give up old lock. */
586 if (relid != oldrelid && OidIsValid(oldrelid) && lockmode != NoLock)
587 UnlockRelationOid(oldrelid, lockmode);
590 /* Lock namespace. */
591 if (nspid != oldnspid)
592 LockDatabaseObject(NamespaceRelationId, nspid, 0, AccessShareLock);
594 /* Lock relation, if required if and we have permission. */
595 if (lockmode != NoLock && OidIsValid(relid))
597 if (!pg_class_ownercheck(relid, GetUserId()))
598 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
600 if (relid != oldrelid)
601 LockRelationOid(relid, lockmode);
604 /* If no invalidation message were processed, we're done! */
605 if (inval_count == SharedInvalidMessageCounter)
608 /* Something may have changed, so recheck our work. */
614 RangeVarAdjustRelationPersistence(relation, nspid);
615 if (existing_relation_id != NULL)
616 *existing_relation_id = relid;
621 * Adjust the relpersistence for an about-to-be-created relation based on the
622 * creation namespace, and throw an error for invalid combinations.
625 RangeVarAdjustRelationPersistence(RangeVar *newRelation, Oid nspid)
627 switch (newRelation->relpersistence)
629 case RELPERSISTENCE_TEMP:
630 if (!isTempOrToastNamespace(nspid))
632 if (isAnyTempNamespace(nspid))
634 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
635 errmsg("cannot create relations in temporary schemas of other sessions")));
638 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
639 errmsg("cannot create temporary relation in non-temporary schema")));
642 case RELPERSISTENCE_PERMANENT:
643 if (isTempOrToastNamespace(nspid))
644 newRelation->relpersistence = RELPERSISTENCE_TEMP;
645 else if (isAnyTempNamespace(nspid))
647 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
648 errmsg("cannot create relations in temporary schemas of other sessions")));
651 if (isAnyTempNamespace(nspid))
653 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
654 errmsg("only temporary relations may be created in temporary schemas")));
660 * Try to resolve an unqualified relation name.
661 * Returns OID if relation found in search path, else InvalidOid.
664 RelnameGetRelid(const char *relname)
669 recomputeNamespacePath();
671 foreach(l, activeSearchPath)
673 Oid namespaceId = lfirst_oid(l);
675 relid = get_relname_relid(relname, namespaceId);
676 if (OidIsValid(relid))
680 /* Not found in path */
687 * Determine whether a relation (identified by OID) is visible in the
688 * current search path. Visible means "would be found by searching
689 * for the unqualified relation name".
692 RelationIsVisible(Oid relid)
695 Form_pg_class relform;
699 reltup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
700 if (!HeapTupleIsValid(reltup))
701 elog(ERROR, "cache lookup failed for relation %u", relid);
702 relform = (Form_pg_class) GETSTRUCT(reltup);
704 recomputeNamespacePath();
707 * Quick check: if it ain't in the path at all, it ain't visible. Items in
708 * the system namespace are surely in the path and so we needn't even do
709 * list_member_oid() for them.
711 relnamespace = relform->relnamespace;
712 if (relnamespace != PG_CATALOG_NAMESPACE &&
713 !list_member_oid(activeSearchPath, relnamespace))
718 * If it is in the path, it might still not be visible; it could be
719 * hidden by another relation of the same name earlier in the path. So
720 * we must do a slow check for conflicting relations.
722 char *relname = NameStr(relform->relname);
726 foreach(l, activeSearchPath)
728 Oid namespaceId = lfirst_oid(l);
730 if (namespaceId == relnamespace)
732 /* Found it first in path */
736 if (OidIsValid(get_relname_relid(relname, namespaceId)))
738 /* Found something else first in path */
744 ReleaseSysCache(reltup);
752 * Try to resolve an unqualified datatype name.
753 * Returns OID if type found in search path, else InvalidOid.
755 * This is essentially the same as RelnameGetRelid.
758 TypenameGetTypid(const char *typname)
763 recomputeNamespacePath();
765 foreach(l, activeSearchPath)
767 Oid namespaceId = lfirst_oid(l);
769 typid = GetSysCacheOid2(TYPENAMENSP,
770 PointerGetDatum(typname),
771 ObjectIdGetDatum(namespaceId));
772 if (OidIsValid(typid))
776 /* Not found in path */
782 * Determine whether a type (identified by OID) is visible in the
783 * current search path. Visible means "would be found by searching
784 * for the unqualified type name".
787 TypeIsVisible(Oid typid)
790 Form_pg_type typform;
794 typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
795 if (!HeapTupleIsValid(typtup))
796 elog(ERROR, "cache lookup failed for type %u", typid);
797 typform = (Form_pg_type) GETSTRUCT(typtup);
799 recomputeNamespacePath();
802 * Quick check: if it ain't in the path at all, it ain't visible. Items in
803 * the system namespace are surely in the path and so we needn't even do
804 * list_member_oid() for them.
806 typnamespace = typform->typnamespace;
807 if (typnamespace != PG_CATALOG_NAMESPACE &&
808 !list_member_oid(activeSearchPath, typnamespace))
813 * If it is in the path, it might still not be visible; it could be
814 * hidden by another type of the same name earlier in the path. So we
815 * must do a slow check for conflicting types.
817 char *typname = NameStr(typform->typname);
821 foreach(l, activeSearchPath)
823 Oid namespaceId = lfirst_oid(l);
825 if (namespaceId == typnamespace)
827 /* Found it first in path */
831 if (SearchSysCacheExists2(TYPENAMENSP,
832 PointerGetDatum(typname),
833 ObjectIdGetDatum(namespaceId)))
835 /* Found something else first in path */
841 ReleaseSysCache(typtup);
848 * FuncnameGetCandidates
849 * Given a possibly-qualified function name and argument count,
850 * retrieve a list of the possible matches.
852 * If nargs is -1, we return all functions matching the given name,
853 * regardless of argument count. (argnames must be NIL, and expand_variadic
854 * and expand_defaults must be false, in this case.)
856 * If argnames isn't NIL, we are considering a named- or mixed-notation call,
857 * and only functions having all the listed argument names will be returned.
858 * (We assume that length(argnames) <= nargs and all the passed-in names are
859 * distinct.) The returned structs will include an argnumbers array showing
860 * the actual argument index for each logical argument position.
862 * If expand_variadic is true, then variadic functions having the same number
863 * or fewer arguments will be retrieved, with the variadic argument and any
864 * additional argument positions filled with the variadic element type.
865 * nvargs in the returned struct is set to the number of such arguments.
866 * If expand_variadic is false, variadic arguments are not treated specially,
867 * and the returned nvargs will always be zero.
869 * If expand_defaults is true, functions that could match after insertion of
870 * default argument values will also be retrieved. In this case the returned
871 * structs could have nargs > passed-in nargs, and ndargs is set to the number
872 * of additional args (which can be retrieved from the function's
873 * proargdefaults entry).
875 * It is not possible for nvargs and ndargs to both be nonzero in the same
876 * list entry, since default insertion allows matches to functions with more
877 * than nargs arguments while the variadic transformation requires the same
880 * When argnames isn't NIL, the returned args[] type arrays are not ordered
881 * according to the functions' declarations, but rather according to the call:
882 * first any positional arguments, then the named arguments, then defaulted
883 * arguments (if needed and allowed by expand_defaults). The argnumbers[]
884 * array can be used to map this back to the catalog information.
885 * argnumbers[k] is set to the proargtypes index of the k'th call argument.
887 * We search a single namespace if the function name is qualified, else
888 * all namespaces in the search path. In the multiple-namespace case,
889 * we arrange for entries in earlier namespaces to mask identical entries in
892 * When expanding variadics, we arrange for non-variadic functions to mask
893 * variadic ones if the expanded argument list is the same. It is still
894 * possible for there to be conflicts between different variadic functions,
897 * It is guaranteed that the return list will never contain multiple entries
898 * with identical argument lists. When expand_defaults is true, the entries
899 * could have more than nargs positions, but we still guarantee that they are
900 * distinct in the first nargs positions. However, if argnames isn't NIL or
901 * either expand_variadic or expand_defaults is true, there might be multiple
902 * candidate functions that expand to identical argument lists. Rather than
903 * throw error here, we report such situations by returning a single entry
904 * with oid = 0 that represents a set of such conflicting candidates.
905 * The caller might end up discarding such an entry anyway, but if it selects
906 * such an entry it should react as though the call were ambiguous.
909 FuncnameGetCandidates(List *names, int nargs, List *argnames,
910 bool expand_variadic, bool expand_defaults)
912 FuncCandidateList resultList = NULL;
913 bool any_special = false;
920 /* check for caller error */
921 Assert(nargs >= 0 || !(expand_variadic | expand_defaults));
923 /* deconstruct the name list */
924 DeconstructQualifiedName(names, &schemaname, &funcname);
928 /* use exact schema given */
929 namespaceId = LookupExplicitNamespace(schemaname, false);
933 /* flag to indicate we need namespace search */
934 namespaceId = InvalidOid;
935 recomputeNamespacePath();
938 /* Search syscache by name only */
939 catlist = SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(funcname));
941 for (i = 0; i < catlist->n_members; i++)
943 HeapTuple proctup = &catlist->members[i]->tuple;
944 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
945 int pronargs = procform->pronargs;
951 int *argnumbers = NULL;
952 FuncCandidateList newResult;
954 if (OidIsValid(namespaceId))
956 /* Consider only procs in specified namespace */
957 if (procform->pronamespace != namespaceId)
963 * Consider only procs that are in the search path and are not in
964 * the temp namespace.
968 foreach(nsp, activeSearchPath)
970 if (procform->pronamespace == lfirst_oid(nsp) &&
971 procform->pronamespace != myTempNamespace)
976 continue; /* proc is not in search path */
982 * Call uses named or mixed notation
984 * Named or mixed notation can match a variadic function only if
985 * expand_variadic is off; otherwise there is no way to match the
986 * presumed-nameless parameters expanded from the variadic array.
988 if (OidIsValid(procform->provariadic) && expand_variadic)
990 va_elem_type = InvalidOid;
994 * Check argument count.
996 Assert(nargs >= 0); /* -1 not supported with argnames */
998 if (pronargs > nargs && expand_defaults)
1000 /* Ignore if not enough default expressions */
1001 if (nargs + procform->pronargdefaults < pronargs)
1003 use_defaults = true;
1006 use_defaults = false;
1008 /* Ignore if it doesn't match requested argument count */
1009 if (pronargs != nargs && !use_defaults)
1012 /* Check for argument name match, generate positional mapping */
1013 if (!MatchNamedCall(proctup, nargs, argnames,
1017 /* Named argument matching is always "special" */
1023 * Call uses positional notation
1025 * Check if function is variadic, and get variadic element type if
1026 * so. If expand_variadic is false, we should just ignore
1029 if (pronargs <= nargs && expand_variadic)
1031 va_elem_type = procform->provariadic;
1032 variadic = OidIsValid(va_elem_type);
1033 any_special |= variadic;
1037 va_elem_type = InvalidOid;
1042 * Check if function can match by using parameter defaults.
1044 if (pronargs > nargs && expand_defaults)
1046 /* Ignore if not enough default expressions */
1047 if (nargs + procform->pronargdefaults < pronargs)
1049 use_defaults = true;
1053 use_defaults = false;
1055 /* Ignore if it doesn't match requested argument count */
1056 if (nargs >= 0 && pronargs != nargs && !variadic && !use_defaults)
1061 * We must compute the effective argument list so that we can easily
1062 * compare it to earlier results. We waste a palloc cycle if it gets
1063 * masked by an earlier result, but really that's a pretty infrequent
1064 * case so it's not worth worrying about.
1066 effective_nargs = Max(pronargs, nargs);
1067 newResult = (FuncCandidateList)
1068 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
1069 + effective_nargs * sizeof(Oid));
1070 newResult->pathpos = pathpos;
1071 newResult->oid = HeapTupleGetOid(proctup);
1072 newResult->nargs = effective_nargs;
1073 newResult->argnumbers = argnumbers;
1076 /* Re-order the argument types into call's logical order */
1077 Oid *proargtypes = procform->proargtypes.values;
1080 for (i = 0; i < pronargs; i++)
1081 newResult->args[i] = proargtypes[argnumbers[i]];
1085 /* Simple positional case, just copy proargtypes as-is */
1086 memcpy(newResult->args, procform->proargtypes.values,
1087 pronargs * sizeof(Oid));
1093 newResult->nvargs = effective_nargs - pronargs + 1;
1094 /* Expand variadic argument into N copies of element type */
1095 for (i = pronargs - 1; i < effective_nargs; i++)
1096 newResult->args[i] = va_elem_type;
1099 newResult->nvargs = 0;
1100 newResult->ndargs = use_defaults ? pronargs - nargs : 0;
1103 * Does it have the same arguments as something we already accepted?
1104 * If so, decide what to do to avoid returning duplicate argument
1105 * lists. We can skip this check for the single-namespace case if no
1106 * special (named, variadic or defaults) match has been made, since
1107 * then the unique index on pg_proc guarantees all the matches have
1108 * different argument lists.
1110 if (resultList != NULL &&
1111 (any_special || !OidIsValid(namespaceId)))
1114 * If we have an ordered list from SearchSysCacheList (the normal
1115 * case), then any conflicting proc must immediately adjoin this
1116 * one in the list, so we only need to look at the newest result
1117 * item. If we have an unordered list, we have to scan the whole
1118 * result list. Also, if either the current candidate or any
1119 * previous candidate is a special match, we can't assume that
1120 * conflicts are adjacent.
1122 * We ignore defaulted arguments in deciding what is a match.
1124 FuncCandidateList prevResult;
1126 if (catlist->ordered && !any_special)
1128 /* ndargs must be 0 if !any_special */
1129 if (effective_nargs == resultList->nargs &&
1130 memcmp(newResult->args,
1132 effective_nargs * sizeof(Oid)) == 0)
1133 prevResult = resultList;
1139 int cmp_nargs = newResult->nargs - newResult->ndargs;
1141 for (prevResult = resultList;
1143 prevResult = prevResult->next)
1145 if (cmp_nargs == prevResult->nargs - prevResult->ndargs &&
1146 memcmp(newResult->args,
1148 cmp_nargs * sizeof(Oid)) == 0)
1156 * We have a match with a previous result. Decide which one
1157 * to keep, or mark it ambiguous if we can't decide. The
1158 * logic here is preference > 0 means prefer the old result,
1159 * preference < 0 means prefer the new, preference = 0 means
1164 if (pathpos != prevResult->pathpos)
1167 * Prefer the one that's earlier in the search path.
1169 preference = pathpos - prevResult->pathpos;
1171 else if (variadic && prevResult->nvargs == 0)
1174 * With variadic functions we could have, for example,
1175 * both foo(numeric) and foo(variadic numeric[]) in the
1176 * same namespace; if so we prefer the non-variadic match
1177 * on efficiency grounds.
1181 else if (!variadic && prevResult->nvargs > 0)
1188 * We can't decide. This can happen with, for example,
1189 * both foo(numeric, variadic numeric[]) and
1190 * foo(variadic numeric[]) in the same namespace, or
1191 * both foo(int) and foo (int, int default something)
1192 * in the same namespace, or both foo(a int, b text)
1193 * and foo(b text, a int) in the same namespace.
1201 /* keep previous result */
1205 else if (preference < 0)
1207 /* remove previous result from the list */
1208 if (prevResult == resultList)
1209 resultList = prevResult->next;
1212 FuncCandidateList prevPrevResult;
1214 for (prevPrevResult = resultList;
1216 prevPrevResult = prevPrevResult->next)
1218 if (prevResult == prevPrevResult->next)
1220 prevPrevResult->next = prevResult->next;
1224 Assert(prevPrevResult); /* assert we found it */
1227 /* fall through to add newResult to list */
1231 /* mark old result as ambiguous, discard new */
1232 prevResult->oid = InvalidOid;
1240 * Okay to add it to result list
1242 newResult->next = resultList;
1243 resultList = newResult;
1246 ReleaseSysCacheList(catlist);
1253 * Given a pg_proc heap tuple and a call's list of argument names,
1254 * check whether the function could match the call.
1256 * The call could match if all supplied argument names are accepted by
1257 * the function, in positions after the last positional argument, and there
1258 * are defaults for all unsupplied arguments.
1260 * The number of positional arguments is nargs - list_length(argnames).
1261 * Note caller has already done basic checks on argument count.
1263 * On match, return true and fill *argnumbers with a palloc'd array showing
1264 * the mapping from call argument positions to actual function argument
1265 * numbers. Defaulted arguments are included in this map, at positions
1266 * after the last supplied argument.
1269 MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
1272 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
1273 int pronargs = procform->pronargs;
1274 int numposargs = nargs - list_length(argnames);
1279 bool arggiven[FUNC_MAX_ARGS];
1281 int ap; /* call args position */
1282 int pp; /* proargs position */
1285 Assert(argnames != NIL);
1286 Assert(numposargs >= 0);
1287 Assert(nargs <= pronargs);
1289 /* Ignore this function if its proargnames is null */
1290 (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proargnames,
1295 /* OK, let's extract the argument names and types */
1296 pronallargs = get_func_arg_info(proctup,
1297 &p_argtypes, &p_argnames, &p_argmodes);
1298 Assert(p_argnames != NULL);
1300 /* initialize state for matching */
1301 *argnumbers = (int *) palloc(pronargs * sizeof(int));
1302 memset(arggiven, false, pronargs * sizeof(bool));
1304 /* there are numposargs positional args before the named args */
1305 for (ap = 0; ap < numposargs; ap++)
1307 (*argnumbers)[ap] = ap;
1308 arggiven[ap] = true;
1311 /* now examine the named args */
1312 foreach(lc, argnames)
1314 char *argname = (char *) lfirst(lc);
1320 for (i = 0; i < pronallargs; i++)
1322 /* consider only input parameters */
1324 (p_argmodes[i] != FUNC_PARAM_IN &&
1325 p_argmodes[i] != FUNC_PARAM_INOUT &&
1326 p_argmodes[i] != FUNC_PARAM_VARIADIC))
1328 if (p_argnames[i] && strcmp(p_argnames[i], argname) == 0)
1330 /* fail if argname matches a positional argument */
1333 arggiven[pp] = true;
1334 (*argnumbers)[ap] = pp;
1338 /* increase pp only for input parameters */
1341 /* if name isn't in proargnames, fail */
1347 Assert(ap == nargs); /* processed all actual parameters */
1349 /* Check for default arguments */
1350 if (nargs < pronargs)
1352 int first_arg_with_default = pronargs - procform->pronargdefaults;
1354 for (pp = numposargs; pp < pronargs; pp++)
1358 /* fail if arg not given and no default available */
1359 if (pp < first_arg_with_default)
1361 (*argnumbers)[ap++] = pp;
1365 Assert(ap == pronargs); /* processed all function parameters */
1372 * Determine whether a function (identified by OID) is visible in the
1373 * current search path. Visible means "would be found by searching
1374 * for the unqualified function name with exact argument matches".
1377 FunctionIsVisible(Oid funcid)
1380 Form_pg_proc procform;
1384 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1385 if (!HeapTupleIsValid(proctup))
1386 elog(ERROR, "cache lookup failed for function %u", funcid);
1387 procform = (Form_pg_proc) GETSTRUCT(proctup);
1389 recomputeNamespacePath();
1392 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1393 * the system namespace are surely in the path and so we needn't even do
1394 * list_member_oid() for them.
1396 pronamespace = procform->pronamespace;
1397 if (pronamespace != PG_CATALOG_NAMESPACE &&
1398 !list_member_oid(activeSearchPath, pronamespace))
1403 * If it is in the path, it might still not be visible; it could be
1404 * hidden by another proc of the same name and arguments earlier in
1405 * the path. So we must do a slow check to see if this is the same
1406 * proc that would be found by FuncnameGetCandidates.
1408 char *proname = NameStr(procform->proname);
1409 int nargs = procform->pronargs;
1410 FuncCandidateList clist;
1414 clist = FuncnameGetCandidates(list_make1(makeString(proname)),
1415 nargs, NIL, false, false);
1417 for (; clist; clist = clist->next)
1419 if (memcmp(clist->args, procform->proargtypes.values,
1420 nargs * sizeof(Oid)) == 0)
1422 /* Found the expected entry; is it the right proc? */
1423 visible = (clist->oid == funcid);
1429 ReleaseSysCache(proctup);
1437 * Given a possibly-qualified operator name and exact input datatypes,
1438 * look up the operator. Returns InvalidOid if not found.
1440 * Pass oprleft = InvalidOid for a prefix op, oprright = InvalidOid for
1443 * If the operator name is not schema-qualified, it is sought in the current
1444 * namespace search path.
1447 OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
1454 /* deconstruct the name list */
1455 DeconstructQualifiedName(names, &schemaname, &opername);
1459 /* search only in exact schema given */
1463 namespaceId = LookupExplicitNamespace(schemaname, false);
1464 opertup = SearchSysCache4(OPERNAMENSP,
1465 CStringGetDatum(opername),
1466 ObjectIdGetDatum(oprleft),
1467 ObjectIdGetDatum(oprright),
1468 ObjectIdGetDatum(namespaceId));
1469 if (HeapTupleIsValid(opertup))
1471 Oid result = HeapTupleGetOid(opertup);
1473 ReleaseSysCache(opertup);
1479 /* Search syscache by name and argument types */
1480 catlist = SearchSysCacheList3(OPERNAMENSP,
1481 CStringGetDatum(opername),
1482 ObjectIdGetDatum(oprleft),
1483 ObjectIdGetDatum(oprright));
1485 if (catlist->n_members == 0)
1487 /* no hope, fall out early */
1488 ReleaseSysCacheList(catlist);
1493 * We have to find the list member that is first in the search path, if
1494 * there's more than one. This doubly-nested loop looks ugly, but in
1495 * practice there should usually be few catlist members.
1497 recomputeNamespacePath();
1499 foreach(l, activeSearchPath)
1501 Oid namespaceId = lfirst_oid(l);
1504 if (namespaceId == myTempNamespace)
1505 continue; /* do not look in temp namespace */
1507 for (i = 0; i < catlist->n_members; i++)
1509 HeapTuple opertup = &catlist->members[i]->tuple;
1510 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1512 if (operform->oprnamespace == namespaceId)
1514 Oid result = HeapTupleGetOid(opertup);
1516 ReleaseSysCacheList(catlist);
1522 ReleaseSysCacheList(catlist);
1527 * OpernameGetCandidates
1528 * Given a possibly-qualified operator name and operator kind,
1529 * retrieve a list of the possible matches.
1531 * If oprkind is '\0', we return all operators matching the given name,
1532 * regardless of arguments.
1534 * We search a single namespace if the operator name is qualified, else
1535 * all namespaces in the search path. The return list will never contain
1536 * multiple entries with identical argument lists --- in the multiple-
1537 * namespace case, we arrange for entries in earlier namespaces to mask
1538 * identical entries in later namespaces.
1540 * The returned items always have two args[] entries --- one or the other
1541 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
1544 OpernameGetCandidates(List *names, char oprkind)
1546 FuncCandidateList resultList = NULL;
1547 char *resultSpace = NULL;
1555 /* deconstruct the name list */
1556 DeconstructQualifiedName(names, &schemaname, &opername);
1560 /* use exact schema given */
1561 namespaceId = LookupExplicitNamespace(schemaname, false);
1565 /* flag to indicate we need namespace search */
1566 namespaceId = InvalidOid;
1567 recomputeNamespacePath();
1570 /* Search syscache by name only */
1571 catlist = SearchSysCacheList1(OPERNAMENSP, CStringGetDatum(opername));
1574 * In typical scenarios, most if not all of the operators found by the
1575 * catcache search will end up getting returned; and there can be quite a
1576 * few, for common operator names such as '=' or '+'. To reduce the time
1577 * spent in palloc, we allocate the result space as an array large enough
1578 * to hold all the operators. The original coding of this routine did a
1579 * separate palloc for each operator, but profiling revealed that the
1580 * pallocs used an unreasonably large fraction of parsing time.
1582 #define SPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
1584 if (catlist->n_members > 0)
1585 resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
1587 for (i = 0; i < catlist->n_members; i++)
1589 HeapTuple opertup = &catlist->members[i]->tuple;
1590 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1592 FuncCandidateList newResult;
1594 /* Ignore operators of wrong kind, if specific kind requested */
1595 if (oprkind && operform->oprkind != oprkind)
1598 if (OidIsValid(namespaceId))
1600 /* Consider only opers in specified namespace */
1601 if (operform->oprnamespace != namespaceId)
1603 /* No need to check args, they must all be different */
1608 * Consider only opers that are in the search path and are not in
1609 * the temp namespace.
1613 foreach(nsp, activeSearchPath)
1615 if (operform->oprnamespace == lfirst_oid(nsp) &&
1616 operform->oprnamespace != myTempNamespace)
1621 continue; /* oper is not in search path */
1624 * Okay, it's in the search path, but does it have the same
1625 * arguments as something we already accepted? If so, keep only
1626 * the one that appears earlier in the search path.
1628 * If we have an ordered list from SearchSysCacheList (the normal
1629 * case), then any conflicting oper must immediately adjoin this
1630 * one in the list, so we only need to look at the newest result
1631 * item. If we have an unordered list, we have to scan the whole
1636 FuncCandidateList prevResult;
1638 if (catlist->ordered)
1640 if (operform->oprleft == resultList->args[0] &&
1641 operform->oprright == resultList->args[1])
1642 prevResult = resultList;
1648 for (prevResult = resultList;
1650 prevResult = prevResult->next)
1652 if (operform->oprleft == prevResult->args[0] &&
1653 operform->oprright == prevResult->args[1])
1659 /* We have a match with a previous result */
1660 Assert(pathpos != prevResult->pathpos);
1661 if (pathpos > prevResult->pathpos)
1662 continue; /* keep previous result */
1663 /* replace previous result */
1664 prevResult->pathpos = pathpos;
1665 prevResult->oid = HeapTupleGetOid(opertup);
1666 continue; /* args are same, of course */
1672 * Okay to add it to result list
1674 newResult = (FuncCandidateList) (resultSpace + nextResult);
1675 nextResult += SPACE_PER_OP;
1677 newResult->pathpos = pathpos;
1678 newResult->oid = HeapTupleGetOid(opertup);
1679 newResult->nargs = 2;
1680 newResult->nvargs = 0;
1681 newResult->ndargs = 0;
1682 newResult->argnumbers = NULL;
1683 newResult->args[0] = operform->oprleft;
1684 newResult->args[1] = operform->oprright;
1685 newResult->next = resultList;
1686 resultList = newResult;
1689 ReleaseSysCacheList(catlist);
1696 * Determine whether an operator (identified by OID) is visible in the
1697 * current search path. Visible means "would be found by searching
1698 * for the unqualified operator name with exact argument matches".
1701 OperatorIsVisible(Oid oprid)
1704 Form_pg_operator oprform;
1708 oprtup = SearchSysCache1(OPEROID, ObjectIdGetDatum(oprid));
1709 if (!HeapTupleIsValid(oprtup))
1710 elog(ERROR, "cache lookup failed for operator %u", oprid);
1711 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
1713 recomputeNamespacePath();
1716 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1717 * the system namespace are surely in the path and so we needn't even do
1718 * list_member_oid() for them.
1720 oprnamespace = oprform->oprnamespace;
1721 if (oprnamespace != PG_CATALOG_NAMESPACE &&
1722 !list_member_oid(activeSearchPath, oprnamespace))
1727 * If it is in the path, it might still not be visible; it could be
1728 * hidden by another operator of the same name and arguments earlier
1729 * in the path. So we must do a slow check to see if this is the same
1730 * operator that would be found by OpernameGetOprId.
1732 char *oprname = NameStr(oprform->oprname);
1734 visible = (OpernameGetOprid(list_make1(makeString(oprname)),
1735 oprform->oprleft, oprform->oprright)
1739 ReleaseSysCache(oprtup);
1746 * OpclassnameGetOpcid
1747 * Try to resolve an unqualified index opclass name.
1748 * Returns OID if opclass found in search path, else InvalidOid.
1750 * This is essentially the same as TypenameGetTypid, but we have to have
1751 * an extra argument for the index AM OID.
1754 OpclassnameGetOpcid(Oid amid, const char *opcname)
1759 recomputeNamespacePath();
1761 foreach(l, activeSearchPath)
1763 Oid namespaceId = lfirst_oid(l);
1765 if (namespaceId == myTempNamespace)
1766 continue; /* do not look in temp namespace */
1768 opcid = GetSysCacheOid3(CLAAMNAMENSP,
1769 ObjectIdGetDatum(amid),
1770 PointerGetDatum(opcname),
1771 ObjectIdGetDatum(namespaceId));
1772 if (OidIsValid(opcid))
1776 /* Not found in path */
1782 * Determine whether an opclass (identified by OID) is visible in the
1783 * current search path. Visible means "would be found by searching
1784 * for the unqualified opclass name".
1787 OpclassIsVisible(Oid opcid)
1790 Form_pg_opclass opcform;
1794 opctup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opcid));
1795 if (!HeapTupleIsValid(opctup))
1796 elog(ERROR, "cache lookup failed for opclass %u", opcid);
1797 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1799 recomputeNamespacePath();
1802 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1803 * the system namespace are surely in the path and so we needn't even do
1804 * list_member_oid() for them.
1806 opcnamespace = opcform->opcnamespace;
1807 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1808 !list_member_oid(activeSearchPath, opcnamespace))
1813 * If it is in the path, it might still not be visible; it could be
1814 * hidden by another opclass of the same name earlier in the path. So
1815 * we must do a slow check to see if this opclass would be found by
1816 * OpclassnameGetOpcid.
1818 char *opcname = NameStr(opcform->opcname);
1820 visible = (OpclassnameGetOpcid(opcform->opcmethod, opcname) == opcid);
1823 ReleaseSysCache(opctup);
1829 * OpfamilynameGetOpfid
1830 * Try to resolve an unqualified index opfamily name.
1831 * Returns OID if opfamily found in search path, else InvalidOid.
1833 * This is essentially the same as TypenameGetTypid, but we have to have
1834 * an extra argument for the index AM OID.
1837 OpfamilynameGetOpfid(Oid amid, const char *opfname)
1842 recomputeNamespacePath();
1844 foreach(l, activeSearchPath)
1846 Oid namespaceId = lfirst_oid(l);
1848 if (namespaceId == myTempNamespace)
1849 continue; /* do not look in temp namespace */
1851 opfid = GetSysCacheOid3(OPFAMILYAMNAMENSP,
1852 ObjectIdGetDatum(amid),
1853 PointerGetDatum(opfname),
1854 ObjectIdGetDatum(namespaceId));
1855 if (OidIsValid(opfid))
1859 /* Not found in path */
1865 * Determine whether an opfamily (identified by OID) is visible in the
1866 * current search path. Visible means "would be found by searching
1867 * for the unqualified opfamily name".
1870 OpfamilyIsVisible(Oid opfid)
1873 Form_pg_opfamily opfform;
1877 opftup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
1878 if (!HeapTupleIsValid(opftup))
1879 elog(ERROR, "cache lookup failed for opfamily %u", opfid);
1880 opfform = (Form_pg_opfamily) GETSTRUCT(opftup);
1882 recomputeNamespacePath();
1885 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1886 * the system namespace are surely in the path and so we needn't even do
1887 * list_member_oid() for them.
1889 opfnamespace = opfform->opfnamespace;
1890 if (opfnamespace != PG_CATALOG_NAMESPACE &&
1891 !list_member_oid(activeSearchPath, opfnamespace))
1896 * If it is in the path, it might still not be visible; it could be
1897 * hidden by another opfamily of the same name earlier in the path. So
1898 * we must do a slow check to see if this opfamily would be found by
1899 * OpfamilynameGetOpfid.
1901 char *opfname = NameStr(opfform->opfname);
1903 visible = (OpfamilynameGetOpfid(opfform->opfmethod, opfname) == opfid);
1906 ReleaseSysCache(opftup);
1912 * CollationGetCollid
1913 * Try to resolve an unqualified collation name.
1914 * Returns OID if collation found in search path, else InvalidOid.
1917 CollationGetCollid(const char *collname)
1919 int32 dbencoding = GetDatabaseEncoding();
1922 recomputeNamespacePath();
1924 foreach(l, activeSearchPath)
1926 Oid namespaceId = lfirst_oid(l);
1929 if (namespaceId == myTempNamespace)
1930 continue; /* do not look in temp namespace */
1932 /* Check for database-encoding-specific entry */
1933 collid = GetSysCacheOid3(COLLNAMEENCNSP,
1934 PointerGetDatum(collname),
1935 Int32GetDatum(dbencoding),
1936 ObjectIdGetDatum(namespaceId));
1937 if (OidIsValid(collid))
1940 /* Check for any-encoding entry */
1941 collid = GetSysCacheOid3(COLLNAMEENCNSP,
1942 PointerGetDatum(collname),
1944 ObjectIdGetDatum(namespaceId));
1945 if (OidIsValid(collid))
1949 /* Not found in path */
1954 * CollationIsVisible
1955 * Determine whether a collation (identified by OID) is visible in the
1956 * current search path. Visible means "would be found by searching
1957 * for the unqualified collation name".
1960 CollationIsVisible(Oid collid)
1963 Form_pg_collation collform;
1967 colltup = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1968 if (!HeapTupleIsValid(colltup))
1969 elog(ERROR, "cache lookup failed for collation %u", collid);
1970 collform = (Form_pg_collation) GETSTRUCT(colltup);
1972 recomputeNamespacePath();
1975 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1976 * the system namespace are surely in the path and so we needn't even do
1977 * list_member_oid() for them.
1979 collnamespace = collform->collnamespace;
1980 if (collnamespace != PG_CATALOG_NAMESPACE &&
1981 !list_member_oid(activeSearchPath, collnamespace))
1986 * If it is in the path, it might still not be visible; it could be
1987 * hidden by another conversion of the same name earlier in the path.
1988 * So we must do a slow check to see if this conversion would be found
1989 * by CollationGetCollid.
1991 char *collname = NameStr(collform->collname);
1993 visible = (CollationGetCollid(collname) == collid);
1996 ReleaseSysCache(colltup);
2003 * ConversionGetConid
2004 * Try to resolve an unqualified conversion name.
2005 * Returns OID if conversion found in search path, else InvalidOid.
2007 * This is essentially the same as RelnameGetRelid.
2010 ConversionGetConid(const char *conname)
2015 recomputeNamespacePath();
2017 foreach(l, activeSearchPath)
2019 Oid namespaceId = lfirst_oid(l);
2021 if (namespaceId == myTempNamespace)
2022 continue; /* do not look in temp namespace */
2024 conid = GetSysCacheOid2(CONNAMENSP,
2025 PointerGetDatum(conname),
2026 ObjectIdGetDatum(namespaceId));
2027 if (OidIsValid(conid))
2031 /* Not found in path */
2036 * ConversionIsVisible
2037 * Determine whether a conversion (identified by OID) is visible in the
2038 * current search path. Visible means "would be found by searching
2039 * for the unqualified conversion name".
2042 ConversionIsVisible(Oid conid)
2045 Form_pg_conversion conform;
2049 contup = SearchSysCache1(CONVOID, ObjectIdGetDatum(conid));
2050 if (!HeapTupleIsValid(contup))
2051 elog(ERROR, "cache lookup failed for conversion %u", conid);
2052 conform = (Form_pg_conversion) GETSTRUCT(contup);
2054 recomputeNamespacePath();
2057 * Quick check: if it ain't in the path at all, it ain't visible. Items in
2058 * the system namespace are surely in the path and so we needn't even do
2059 * list_member_oid() for them.
2061 connamespace = conform->connamespace;
2062 if (connamespace != PG_CATALOG_NAMESPACE &&
2063 !list_member_oid(activeSearchPath, connamespace))
2068 * If it is in the path, it might still not be visible; it could be
2069 * hidden by another conversion of the same name earlier in the path.
2070 * So we must do a slow check to see if this conversion would be found
2071 * by ConversionGetConid.
2073 char *conname = NameStr(conform->conname);
2075 visible = (ConversionGetConid(conname) == conid);
2078 ReleaseSysCache(contup);
2084 * get_ts_parser_oid - find a TS parser by possibly qualified name
2086 * If not found, returns InvalidOid if missing_ok, else throws error
2089 get_ts_parser_oid(List *names, bool missing_ok)
2094 Oid prsoid = InvalidOid;
2097 /* deconstruct the name list */
2098 DeconstructQualifiedName(names, &schemaname, &parser_name);
2102 /* use exact schema given */
2103 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2104 if (missing_ok && !OidIsValid(namespaceId))
2105 prsoid = InvalidOid;
2107 prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
2108 PointerGetDatum(parser_name),
2109 ObjectIdGetDatum(namespaceId));
2113 /* search for it in search path */
2114 recomputeNamespacePath();
2116 foreach(l, activeSearchPath)
2118 namespaceId = lfirst_oid(l);
2120 if (namespaceId == myTempNamespace)
2121 continue; /* do not look in temp namespace */
2123 prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
2124 PointerGetDatum(parser_name),
2125 ObjectIdGetDatum(namespaceId));
2126 if (OidIsValid(prsoid))
2131 if (!OidIsValid(prsoid) && !missing_ok)
2133 (errcode(ERRCODE_UNDEFINED_OBJECT),
2134 errmsg("text search parser \"%s\" does not exist",
2135 NameListToString(names))));
2142 * Determine whether a parser (identified by OID) is visible in the
2143 * current search path. Visible means "would be found by searching
2144 * for the unqualified parser name".
2147 TSParserIsVisible(Oid prsId)
2150 Form_pg_ts_parser form;
2154 tup = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
2155 if (!HeapTupleIsValid(tup))
2156 elog(ERROR, "cache lookup failed for text search parser %u", prsId);
2157 form = (Form_pg_ts_parser) GETSTRUCT(tup);
2159 recomputeNamespacePath();
2162 * Quick check: if it ain't in the path at all, it ain't visible. Items in
2163 * the system namespace are surely in the path and so we needn't even do
2164 * list_member_oid() for them.
2166 namespace = form->prsnamespace;
2167 if (namespace != PG_CATALOG_NAMESPACE &&
2168 !list_member_oid(activeSearchPath, namespace))
2173 * If it is in the path, it might still not be visible; it could be
2174 * hidden by another parser of the same name earlier in the path. So
2175 * we must do a slow check for conflicting parsers.
2177 char *name = NameStr(form->prsname);
2181 foreach(l, activeSearchPath)
2183 Oid namespaceId = lfirst_oid(l);
2185 if (namespaceId == myTempNamespace)
2186 continue; /* do not look in temp namespace */
2188 if (namespaceId == namespace)
2190 /* Found it first in path */
2194 if (SearchSysCacheExists2(TSPARSERNAMENSP,
2195 PointerGetDatum(name),
2196 ObjectIdGetDatum(namespaceId)))
2198 /* Found something else first in path */
2204 ReleaseSysCache(tup);
2210 * get_ts_dict_oid - find a TS dictionary by possibly qualified name
2212 * If not found, returns InvalidOid if failOK, else throws error
2215 get_ts_dict_oid(List *names, bool missing_ok)
2220 Oid dictoid = InvalidOid;
2223 /* deconstruct the name list */
2224 DeconstructQualifiedName(names, &schemaname, &dict_name);
2228 /* use exact schema given */
2229 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2230 if (missing_ok && !OidIsValid(namespaceId))
2231 dictoid = InvalidOid;
2233 dictoid = GetSysCacheOid2(TSDICTNAMENSP,
2234 PointerGetDatum(dict_name),
2235 ObjectIdGetDatum(namespaceId));
2239 /* search for it in search path */
2240 recomputeNamespacePath();
2242 foreach(l, activeSearchPath)
2244 namespaceId = lfirst_oid(l);
2246 if (namespaceId == myTempNamespace)
2247 continue; /* do not look in temp namespace */
2249 dictoid = GetSysCacheOid2(TSDICTNAMENSP,
2250 PointerGetDatum(dict_name),
2251 ObjectIdGetDatum(namespaceId));
2252 if (OidIsValid(dictoid))
2257 if (!OidIsValid(dictoid) && !missing_ok)
2259 (errcode(ERRCODE_UNDEFINED_OBJECT),
2260 errmsg("text search dictionary \"%s\" does not exist",
2261 NameListToString(names))));
2267 * TSDictionaryIsVisible
2268 * Determine whether a dictionary (identified by OID) is visible in the
2269 * current search path. Visible means "would be found by searching
2270 * for the unqualified dictionary name".
2273 TSDictionaryIsVisible(Oid dictId)
2276 Form_pg_ts_dict form;
2280 tup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
2281 if (!HeapTupleIsValid(tup))
2282 elog(ERROR, "cache lookup failed for text search dictionary %u",
2284 form = (Form_pg_ts_dict) GETSTRUCT(tup);
2286 recomputeNamespacePath();
2289 * Quick check: if it ain't in the path at all, it ain't visible. Items in
2290 * the system namespace are surely in the path and so we needn't even do
2291 * list_member_oid() for them.
2293 namespace = form->dictnamespace;
2294 if (namespace != PG_CATALOG_NAMESPACE &&
2295 !list_member_oid(activeSearchPath, namespace))
2300 * If it is in the path, it might still not be visible; it could be
2301 * hidden by another dictionary of the same name earlier in the path.
2302 * So we must do a slow check for conflicting dictionaries.
2304 char *name = NameStr(form->dictname);
2308 foreach(l, activeSearchPath)
2310 Oid namespaceId = lfirst_oid(l);
2312 if (namespaceId == myTempNamespace)
2313 continue; /* do not look in temp namespace */
2315 if (namespaceId == namespace)
2317 /* Found it first in path */
2321 if (SearchSysCacheExists2(TSDICTNAMENSP,
2322 PointerGetDatum(name),
2323 ObjectIdGetDatum(namespaceId)))
2325 /* Found something else first in path */
2331 ReleaseSysCache(tup);
2337 * get_ts_template_oid - find a TS template by possibly qualified name
2339 * If not found, returns InvalidOid if missing_ok, else throws error
2342 get_ts_template_oid(List *names, bool missing_ok)
2345 char *template_name;
2347 Oid tmploid = InvalidOid;
2350 /* deconstruct the name list */
2351 DeconstructQualifiedName(names, &schemaname, &template_name);
2355 /* use exact schema given */
2356 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2357 if (missing_ok && !OidIsValid(namespaceId))
2358 tmploid = InvalidOid;
2360 tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
2361 PointerGetDatum(template_name),
2362 ObjectIdGetDatum(namespaceId));
2366 /* search for it in search path */
2367 recomputeNamespacePath();
2369 foreach(l, activeSearchPath)
2371 namespaceId = lfirst_oid(l);
2373 if (namespaceId == myTempNamespace)
2374 continue; /* do not look in temp namespace */
2376 tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
2377 PointerGetDatum(template_name),
2378 ObjectIdGetDatum(namespaceId));
2379 if (OidIsValid(tmploid))
2384 if (!OidIsValid(tmploid) && !missing_ok)
2386 (errcode(ERRCODE_UNDEFINED_OBJECT),
2387 errmsg("text search template \"%s\" does not exist",
2388 NameListToString(names))));
2394 * TSTemplateIsVisible
2395 * Determine whether a template (identified by OID) is visible in the
2396 * current search path. Visible means "would be found by searching
2397 * for the unqualified template name".
2400 TSTemplateIsVisible(Oid tmplId)
2403 Form_pg_ts_template form;
2407 tup = SearchSysCache1(TSTEMPLATEOID, ObjectIdGetDatum(tmplId));
2408 if (!HeapTupleIsValid(tup))
2409 elog(ERROR, "cache lookup failed for text search template %u", tmplId);
2410 form = (Form_pg_ts_template) GETSTRUCT(tup);
2412 recomputeNamespacePath();
2415 * Quick check: if it ain't in the path at all, it ain't visible. Items in
2416 * the system namespace are surely in the path and so we needn't even do
2417 * list_member_oid() for them.
2419 namespace = form->tmplnamespace;
2420 if (namespace != PG_CATALOG_NAMESPACE &&
2421 !list_member_oid(activeSearchPath, namespace))
2426 * If it is in the path, it might still not be visible; it could be
2427 * hidden by another template of the same name earlier in the path. So
2428 * we must do a slow check for conflicting templates.
2430 char *name = NameStr(form->tmplname);
2434 foreach(l, activeSearchPath)
2436 Oid namespaceId = lfirst_oid(l);
2438 if (namespaceId == myTempNamespace)
2439 continue; /* do not look in temp namespace */
2441 if (namespaceId == namespace)
2443 /* Found it first in path */
2447 if (SearchSysCacheExists2(TSTEMPLATENAMENSP,
2448 PointerGetDatum(name),
2449 ObjectIdGetDatum(namespaceId)))
2451 /* Found something else first in path */
2457 ReleaseSysCache(tup);
2463 * get_ts_config_oid - find a TS config by possibly qualified name
2465 * If not found, returns InvalidOid if missing_ok, else throws error
2468 get_ts_config_oid(List *names, bool missing_ok)
2473 Oid cfgoid = InvalidOid;
2476 /* deconstruct the name list */
2477 DeconstructQualifiedName(names, &schemaname, &config_name);
2481 /* use exact schema given */
2482 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2483 if (missing_ok && !OidIsValid(namespaceId))
2484 cfgoid = InvalidOid;
2486 cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
2487 PointerGetDatum(config_name),
2488 ObjectIdGetDatum(namespaceId));
2492 /* search for it in search path */
2493 recomputeNamespacePath();
2495 foreach(l, activeSearchPath)
2497 namespaceId = lfirst_oid(l);
2499 if (namespaceId == myTempNamespace)
2500 continue; /* do not look in temp namespace */
2502 cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
2503 PointerGetDatum(config_name),
2504 ObjectIdGetDatum(namespaceId));
2505 if (OidIsValid(cfgoid))
2510 if (!OidIsValid(cfgoid) && !missing_ok)
2512 (errcode(ERRCODE_UNDEFINED_OBJECT),
2513 errmsg("text search configuration \"%s\" does not exist",
2514 NameListToString(names))));
2521 * Determine whether a text search configuration (identified by OID)
2522 * is visible in the current search path. Visible means "would be found
2523 * by searching for the unqualified text search configuration name".
2526 TSConfigIsVisible(Oid cfgid)
2529 Form_pg_ts_config form;
2533 tup = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgid));
2534 if (!HeapTupleIsValid(tup))
2535 elog(ERROR, "cache lookup failed for text search configuration %u",
2537 form = (Form_pg_ts_config) GETSTRUCT(tup);
2539 recomputeNamespacePath();
2542 * Quick check: if it ain't in the path at all, it ain't visible. Items in
2543 * the system namespace are surely in the path and so we needn't even do
2544 * list_member_oid() for them.
2546 namespace = form->cfgnamespace;
2547 if (namespace != PG_CATALOG_NAMESPACE &&
2548 !list_member_oid(activeSearchPath, namespace))
2553 * If it is in the path, it might still not be visible; it could be
2554 * hidden by another configuration of the same name earlier in the
2555 * path. So we must do a slow check for conflicting configurations.
2557 char *name = NameStr(form->cfgname);
2561 foreach(l, activeSearchPath)
2563 Oid namespaceId = lfirst_oid(l);
2565 if (namespaceId == myTempNamespace)
2566 continue; /* do not look in temp namespace */
2568 if (namespaceId == namespace)
2570 /* Found it first in path */
2574 if (SearchSysCacheExists2(TSCONFIGNAMENSP,
2575 PointerGetDatum(name),
2576 ObjectIdGetDatum(namespaceId)))
2578 /* Found something else first in path */
2584 ReleaseSysCache(tup);
2591 * DeconstructQualifiedName
2592 * Given a possibly-qualified name expressed as a list of String nodes,
2593 * extract the schema name and object name.
2595 * *nspname_p is set to NULL if there is no explicit schema name.
2598 DeconstructQualifiedName(List *names,
2603 char *schemaname = NULL;
2604 char *objname = NULL;
2606 switch (list_length(names))
2609 objname = strVal(linitial(names));
2612 schemaname = strVal(linitial(names));
2613 objname = strVal(lsecond(names));
2616 catalogname = strVal(linitial(names));
2617 schemaname = strVal(lsecond(names));
2618 objname = strVal(lthird(names));
2621 * We check the catalog name and then ignore it.
2623 if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
2625 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2626 errmsg("cross-database references are not implemented: %s",
2627 NameListToString(names))));
2631 (errcode(ERRCODE_SYNTAX_ERROR),
2632 errmsg("improper qualified name (too many dotted names): %s",
2633 NameListToString(names))));
2637 *nspname_p = schemaname;
2638 *objname_p = objname;
2642 * LookupNamespaceNoError
2643 * Look up a schema name.
2645 * Returns the namespace OID, or InvalidOid if not found.
2647 * Note this does NOT perform any permissions check --- callers are
2648 * responsible for being sure that an appropriate check is made.
2649 * In the majority of cases LookupExplicitNamespace is preferable.
2652 LookupNamespaceNoError(const char *nspname)
2654 /* check for pg_temp alias */
2655 if (strcmp(nspname, "pg_temp") == 0)
2657 if (OidIsValid(myTempNamespace))
2658 return myTempNamespace;
2661 * Since this is used only for looking up existing objects, there is
2662 * no point in trying to initialize the temp namespace here; and doing
2663 * so might create problems for some callers. Just report "not found".
2668 return get_namespace_oid(nspname, true);
2672 * LookupExplicitNamespace
2673 * Process an explicitly-specified schema name: look up the schema
2674 * and verify we have USAGE (lookup) rights in it.
2676 * Returns the namespace OID
2679 LookupExplicitNamespace(const char *nspname, bool missing_ok)
2682 AclResult aclresult;
2684 /* check for pg_temp alias */
2685 if (strcmp(nspname, "pg_temp") == 0)
2687 if (OidIsValid(myTempNamespace))
2688 return myTempNamespace;
2691 * Since this is used only for looking up existing objects, there is
2692 * no point in trying to initialize the temp namespace here; and doing
2693 * so might create problems for some callers --- just fall through.
2697 namespaceId = get_namespace_oid(nspname, missing_ok);
2698 if (missing_ok && !OidIsValid(namespaceId))
2701 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
2702 if (aclresult != ACLCHECK_OK)
2703 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2710 * LookupCreationNamespace
2711 * Look up the schema and verify we have CREATE rights on it.
2713 * This is just like LookupExplicitNamespace except for the different
2714 * permission check, and that we are willing to create pg_temp if needed.
2716 * Note: calling this may result in a CommandCounterIncrement operation,
2717 * if we have to create or clean out the temp namespace.
2720 LookupCreationNamespace(const char *nspname)
2723 AclResult aclresult;
2725 /* check for pg_temp alias */
2726 if (strcmp(nspname, "pg_temp") == 0)
2728 /* Initialize temp namespace if first time through */
2729 if (!OidIsValid(myTempNamespace))
2730 InitTempTableNamespace();
2731 return myTempNamespace;
2734 namespaceId = get_namespace_oid(nspname, false);
2736 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
2737 if (aclresult != ACLCHECK_OK)
2738 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2745 * Common checks on switching namespaces.
2747 * We complain if (1) the old and new namespaces are the same, (2) either the
2748 * old or new namespaces is a temporary schema (or temporary toast schema), or
2749 * (3) either the old or new namespaces is the TOAST schema.
2752 CheckSetNamespace(Oid oldNspOid, Oid nspOid, Oid classid, Oid objid)
2754 if (oldNspOid == nspOid)
2756 (classid == RelationRelationId ?
2757 errcode(ERRCODE_DUPLICATE_TABLE) :
2758 classid == ProcedureRelationId ?
2759 errcode(ERRCODE_DUPLICATE_FUNCTION) :
2760 errcode(ERRCODE_DUPLICATE_OBJECT),
2761 errmsg("%s is already in schema \"%s\"",
2762 getObjectDescriptionOids(classid, objid),
2763 get_namespace_name(nspOid))));
2765 /* disallow renaming into or out of temp schemas */
2766 if (isAnyTempNamespace(nspOid) || isAnyTempNamespace(oldNspOid))
2768 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2769 errmsg("cannot move objects into or out of temporary schemas")));
2771 /* same for TOAST schema */
2772 if (nspOid == PG_TOAST_NAMESPACE || oldNspOid == PG_TOAST_NAMESPACE)
2774 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2775 errmsg("cannot move objects into or out of TOAST schema")));
2779 * QualifiedNameGetCreationNamespace
2780 * Given a possibly-qualified name for an object (in List-of-Values
2781 * format), determine what namespace the object should be created in.
2782 * Also extract and return the object name (last component of list).
2784 * Note: this does not apply any permissions check. Callers must check
2785 * for CREATE rights on the selected namespace when appropriate.
2787 * Note: calling this may result in a CommandCounterIncrement operation,
2788 * if we have to create or clean out the temp namespace.
2791 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
2796 /* deconstruct the name list */
2797 DeconstructQualifiedName(names, &schemaname, objname_p);
2801 /* check for pg_temp alias */
2802 if (strcmp(schemaname, "pg_temp") == 0)
2804 /* Initialize temp namespace if first time through */
2805 if (!OidIsValid(myTempNamespace))
2806 InitTempTableNamespace();
2807 return myTempNamespace;
2809 /* use exact schema given */
2810 namespaceId = get_namespace_oid(schemaname, false);
2811 /* we do not check for USAGE rights here! */
2815 /* use the default creation namespace */
2816 recomputeNamespacePath();
2817 if (activeTempCreationPending)
2819 /* Need to initialize temp namespace */
2820 InitTempTableNamespace();
2821 return myTempNamespace;
2823 namespaceId = activeCreationNamespace;
2824 if (!OidIsValid(namespaceId))
2826 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2827 errmsg("no schema has been selected to create in")));
2834 * get_namespace_oid - given a namespace name, look up the OID
2836 * If missing_ok is false, throw an error if namespace name not found. If
2837 * true, just return InvalidOid.
2840 get_namespace_oid(const char *nspname, bool missing_ok)
2844 oid = GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(nspname));
2845 if (!OidIsValid(oid) && !missing_ok)
2847 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2848 errmsg("schema \"%s\" does not exist", nspname)));
2854 * makeRangeVarFromNameList
2855 * Utility routine to convert a qualified-name list into RangeVar form.
2858 makeRangeVarFromNameList(List *names)
2860 RangeVar *rel = makeRangeVar(NULL, NULL, -1);
2862 switch (list_length(names))
2865 rel->relname = strVal(linitial(names));
2868 rel->schemaname = strVal(linitial(names));
2869 rel->relname = strVal(lsecond(names));
2872 rel->catalogname = strVal(linitial(names));
2873 rel->schemaname = strVal(lsecond(names));
2874 rel->relname = strVal(lthird(names));
2878 (errcode(ERRCODE_SYNTAX_ERROR),
2879 errmsg("improper relation name (too many dotted names): %s",
2880 NameListToString(names))));
2889 * Utility routine to convert a qualified-name list into a string.
2891 * This is used primarily to form error messages, and so we do not quote
2892 * the list elements, for the sake of legibility.
2894 * In most scenarios the list elements should always be Value strings,
2895 * but we also allow A_Star for the convenience of ColumnRef processing.
2898 NameListToString(List *names)
2900 StringInfoData string;
2903 initStringInfo(&string);
2907 Node *name = (Node *) lfirst(l);
2909 if (l != list_head(names))
2910 appendStringInfoChar(&string, '.');
2912 if (IsA(name, String))
2913 appendStringInfoString(&string, strVal(name));
2914 else if (IsA(name, A_Star))
2915 appendStringInfoString(&string, "*");
2917 elog(ERROR, "unexpected node type in name list: %d",
2918 (int) nodeTag(name));
2925 * NameListToQuotedString
2926 * Utility routine to convert a qualified-name list into a string.
2928 * Same as above except that names will be double-quoted where necessary,
2929 * so the string could be re-parsed (eg, by textToQualifiedNameList).
2932 NameListToQuotedString(List *names)
2934 StringInfoData string;
2937 initStringInfo(&string);
2941 if (l != list_head(names))
2942 appendStringInfoChar(&string, '.');
2943 appendStringInfoString(&string, quote_identifier(strVal(lfirst(l))));
2950 * isTempNamespace - is the given namespace my temporary-table namespace?
2953 isTempNamespace(Oid namespaceId)
2955 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
2961 * isTempToastNamespace - is the given namespace my temporary-toast-table
2965 isTempToastNamespace(Oid namespaceId)
2967 if (OidIsValid(myTempToastNamespace) && myTempToastNamespace == namespaceId)
2973 * isTempOrToastNamespace - is the given namespace my temporary-table
2974 * namespace or my temporary-toast-table namespace?
2977 isTempOrToastNamespace(Oid namespaceId)
2979 if (OidIsValid(myTempNamespace) &&
2980 (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId))
2986 * isAnyTempNamespace - is the given namespace a temporary-table namespace
2987 * (either my own, or another backend's)? Temporary-toast-table namespaces
2988 * are included, too.
2991 isAnyTempNamespace(Oid namespaceId)
2996 /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
2997 nspname = get_namespace_name(namespaceId);
2999 return false; /* no such namespace? */
3000 result = (strncmp(nspname, "pg_temp_", 8) == 0) ||
3001 (strncmp(nspname, "pg_toast_temp_", 14) == 0);
3007 * isOtherTempNamespace - is the given namespace some other backend's
3008 * temporary-table namespace (including temporary-toast-table namespaces)?
3010 * Note: for most purposes in the C code, this function is obsolete. Use
3011 * RELATION_IS_OTHER_TEMP() instead to detect non-local temp relations.
3014 isOtherTempNamespace(Oid namespaceId)
3016 /* If it's my own temp namespace, say "false" */
3017 if (isTempOrToastNamespace(namespaceId))
3019 /* Else, if it's any temp namespace, say "true" */
3020 return isAnyTempNamespace(namespaceId);
3024 * GetTempNamespaceBackendId - if the given namespace is a temporary-table
3025 * namespace (either my own, or another backend's), return the BackendId
3026 * that owns it. Temporary-toast-table namespaces are included, too.
3027 * If it isn't a temp namespace, return InvalidBackendId.
3030 GetTempNamespaceBackendId(Oid namespaceId)
3035 /* See if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
3036 nspname = get_namespace_name(namespaceId);
3038 return InvalidBackendId; /* no such namespace? */
3039 if (strncmp(nspname, "pg_temp_", 8) == 0)
3040 result = atoi(nspname + 8);
3041 else if (strncmp(nspname, "pg_toast_temp_", 14) == 0)
3042 result = atoi(nspname + 14);
3044 result = InvalidBackendId;
3050 * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
3051 * which must already be assigned. (This is only used when creating a toast
3052 * table for a temp table, so we must have already done InitTempTableNamespace)
3055 GetTempToastNamespace(void)
3057 Assert(OidIsValid(myTempToastNamespace));
3058 return myTempToastNamespace;
3063 * GetOverrideSearchPath - fetch current search path definition in form
3064 * used by PushOverrideSearchPath.
3066 * The result structure is allocated in the specified memory context
3067 * (which might or might not be equal to CurrentMemoryContext); but any
3068 * junk created by revalidation calculations will be in CurrentMemoryContext.
3070 OverrideSearchPath *
3071 GetOverrideSearchPath(MemoryContext context)
3073 OverrideSearchPath *result;
3075 MemoryContext oldcxt;
3077 recomputeNamespacePath();
3079 oldcxt = MemoryContextSwitchTo(context);
3081 result = (OverrideSearchPath *) palloc0(sizeof(OverrideSearchPath));
3082 schemas = list_copy(activeSearchPath);
3083 while (schemas && linitial_oid(schemas) != activeCreationNamespace)
3085 if (linitial_oid(schemas) == myTempNamespace)
3086 result->addTemp = true;
3089 Assert(linitial_oid(schemas) == PG_CATALOG_NAMESPACE);
3090 result->addCatalog = true;
3092 schemas = list_delete_first(schemas);
3094 result->schemas = schemas;
3096 MemoryContextSwitchTo(oldcxt);
3102 * CopyOverrideSearchPath - copy the specified OverrideSearchPath.
3104 * The result structure is allocated in CurrentMemoryContext.
3106 OverrideSearchPath *
3107 CopyOverrideSearchPath(OverrideSearchPath *path)
3109 OverrideSearchPath *result;
3111 result = (OverrideSearchPath *) palloc(sizeof(OverrideSearchPath));
3112 result->schemas = list_copy(path->schemas);
3113 result->addCatalog = path->addCatalog;
3114 result->addTemp = path->addTemp;
3120 * OverrideSearchPathMatchesCurrent - does path match current setting?
3123 OverrideSearchPathMatchesCurrent(OverrideSearchPath *path)
3125 /* Easiest way to do this is GetOverrideSearchPath() and compare */
3127 OverrideSearchPath *cur;
3129 cur = GetOverrideSearchPath(CurrentMemoryContext);
3130 if (path->addCatalog == cur->addCatalog &&
3131 path->addTemp == cur->addTemp &&
3132 equal(path->schemas, cur->schemas))
3136 list_free(cur->schemas);
3142 * PushOverrideSearchPath - temporarily override the search path
3144 * We allow nested overrides, hence the push/pop terminology. The GUC
3145 * search_path variable is ignored while an override is active.
3147 * It's possible that newpath->useTemp is set but there is no longer any
3148 * active temp namespace, if the path was saved during a transaction that
3149 * created a temp namespace and was later rolled back. In that case we just
3150 * ignore useTemp. A plausible alternative would be to create a new temp
3151 * namespace, but for existing callers that's not necessary because an empty
3152 * temp namespace wouldn't affect their results anyway.
3154 * It's also worth noting that other schemas listed in newpath might not
3155 * exist anymore either. We don't worry about this because OIDs that match
3156 * no existing namespace will simply not produce any hits during searches.
3159 PushOverrideSearchPath(OverrideSearchPath *newpath)
3161 OverrideStackEntry *entry;
3164 MemoryContext oldcxt;
3167 * Copy the list for safekeeping, and insert implicitly-searched
3168 * namespaces as needed. This code should track recomputeNamespacePath.
3170 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
3172 oidlist = list_copy(newpath->schemas);
3175 * Remember the first member of the explicit list.
3178 firstNS = InvalidOid;
3180 firstNS = linitial_oid(oidlist);
3183 * Add any implicitly-searched namespaces to the list. Note these go on
3184 * the front, not the back; also notice that we do not check USAGE
3185 * permissions for these.
3187 if (newpath->addCatalog)
3188 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
3190 if (newpath->addTemp && OidIsValid(myTempNamespace))
3191 oidlist = lcons_oid(myTempNamespace, oidlist);
3194 * Build the new stack entry, then insert it at the head of the list.
3196 entry = (OverrideStackEntry *) palloc(sizeof(OverrideStackEntry));
3197 entry->searchPath = oidlist;
3198 entry->creationNamespace = firstNS;
3199 entry->nestLevel = GetCurrentTransactionNestLevel();
3201 overrideStack = lcons(entry, overrideStack);
3203 /* And make it active. */
3204 activeSearchPath = entry->searchPath;
3205 activeCreationNamespace = entry->creationNamespace;
3206 activeTempCreationPending = false; /* XXX is this OK? */
3208 MemoryContextSwitchTo(oldcxt);
3212 * PopOverrideSearchPath - undo a previous PushOverrideSearchPath
3214 * Any push during a (sub)transaction will be popped automatically at abort.
3215 * But it's caller error if a push isn't popped in normal control flow.
3218 PopOverrideSearchPath(void)
3220 OverrideStackEntry *entry;
3222 /* Sanity checks. */
3223 if (overrideStack == NIL)
3224 elog(ERROR, "bogus PopOverrideSearchPath call");
3225 entry = (OverrideStackEntry *) linitial(overrideStack);
3226 if (entry->nestLevel != GetCurrentTransactionNestLevel())
3227 elog(ERROR, "bogus PopOverrideSearchPath call");
3229 /* Pop the stack and free storage. */
3230 overrideStack = list_delete_first(overrideStack);
3231 list_free(entry->searchPath);
3234 /* Activate the next level down. */
3237 entry = (OverrideStackEntry *) linitial(overrideStack);
3238 activeSearchPath = entry->searchPath;
3239 activeCreationNamespace = entry->creationNamespace;
3240 activeTempCreationPending = false; /* XXX is this OK? */
3244 /* If not baseSearchPathValid, this is useless but harmless */
3245 activeSearchPath = baseSearchPath;
3246 activeCreationNamespace = baseCreationNamespace;
3247 activeTempCreationPending = baseTempCreationPending;
3253 * get_collation_oid - find a collation by possibly qualified name
3256 get_collation_oid(List *name, bool missing_ok)
3259 char *collation_name;
3260 int32 dbencoding = GetDatabaseEncoding();
3265 /* deconstruct the name list */
3266 DeconstructQualifiedName(name, &schemaname, &collation_name);
3270 /* use exact schema given */
3271 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3272 if (missing_ok && !OidIsValid(namespaceId))
3275 /* first try for encoding-specific entry, then any-encoding */
3276 colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3277 PointerGetDatum(collation_name),
3278 Int32GetDatum(dbencoding),
3279 ObjectIdGetDatum(namespaceId));
3280 if (OidIsValid(colloid))
3282 colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3283 PointerGetDatum(collation_name),
3285 ObjectIdGetDatum(namespaceId));
3286 if (OidIsValid(colloid))
3291 /* search for it in search path */
3292 recomputeNamespacePath();
3294 foreach(l, activeSearchPath)
3296 namespaceId = lfirst_oid(l);
3298 if (namespaceId == myTempNamespace)
3299 continue; /* do not look in temp namespace */
3301 colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3302 PointerGetDatum(collation_name),
3303 Int32GetDatum(dbencoding),
3304 ObjectIdGetDatum(namespaceId));
3305 if (OidIsValid(colloid))
3307 colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3308 PointerGetDatum(collation_name),
3310 ObjectIdGetDatum(namespaceId));
3311 if (OidIsValid(colloid))
3316 /* Not found in path */
3319 (errcode(ERRCODE_UNDEFINED_OBJECT),
3320 errmsg("collation \"%s\" for encoding \"%s\" does not exist",
3321 NameListToString(name), GetDatabaseEncodingName())));
3326 * get_conversion_oid - find a conversion by possibly qualified name
3329 get_conversion_oid(List *name, bool missing_ok)
3332 char *conversion_name;
3334 Oid conoid = InvalidOid;
3337 /* deconstruct the name list */
3338 DeconstructQualifiedName(name, &schemaname, &conversion_name);
3342 /* use exact schema given */
3343 namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3344 if (missing_ok && !OidIsValid(namespaceId))
3345 conoid = InvalidOid;
3347 conoid = GetSysCacheOid2(CONNAMENSP,
3348 PointerGetDatum(conversion_name),
3349 ObjectIdGetDatum(namespaceId));
3353 /* search for it in search path */
3354 recomputeNamespacePath();
3356 foreach(l, activeSearchPath)
3358 namespaceId = lfirst_oid(l);
3360 if (namespaceId == myTempNamespace)
3361 continue; /* do not look in temp namespace */
3363 conoid = GetSysCacheOid2(CONNAMENSP,
3364 PointerGetDatum(conversion_name),
3365 ObjectIdGetDatum(namespaceId));
3366 if (OidIsValid(conoid))
3371 /* Not found in path */
3372 if (!OidIsValid(conoid) && !missing_ok)
3374 (errcode(ERRCODE_UNDEFINED_OBJECT),
3375 errmsg("conversion \"%s\" does not exist",
3376 NameListToString(name))));
3381 * FindDefaultConversionProc - find default encoding conversion proc
3384 FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
3389 recomputeNamespacePath();
3391 foreach(l, activeSearchPath)
3393 Oid namespaceId = lfirst_oid(l);
3395 if (namespaceId == myTempNamespace)
3396 continue; /* do not look in temp namespace */
3398 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
3399 if (OidIsValid(proc))
3403 /* Not found in path */
3408 * recomputeNamespacePath - recompute path derived variables if needed.
3411 recomputeNamespacePath(void)
3413 Oid roleid = GetUserId();
3421 MemoryContext oldcxt;
3423 /* Do nothing if an override search spec is active. */
3427 /* Do nothing if path is already valid. */
3428 if (baseSearchPathValid && namespaceUser == roleid)
3431 /* Need a modifiable copy of namespace_search_path string */
3432 rawname = pstrdup(namespace_search_path);
3434 /* Parse string into list of identifiers */
3435 if (!SplitIdentifierString(rawname, ',', &namelist))
3437 /* syntax error in name list */
3438 /* this should not happen if GUC checked check_search_path */
3439 elog(ERROR, "invalid list syntax");
3443 * Convert the list of names to a list of OIDs. If any names are not
3444 * recognizable or we don't have read access, just leave them out of the
3445 * list. (We can't raise an error, since the search_path setting has
3446 * already been accepted.) Don't make duplicate entries, either.
3449 temp_missing = false;
3450 foreach(l, namelist)
3452 char *curname = (char *) lfirst(l);
3455 if (strcmp(curname, "$user") == 0)
3457 /* $user --- substitute namespace matching user name, if any */
3460 tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
3461 if (HeapTupleIsValid(tuple))
3465 rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
3466 namespaceId = get_namespace_oid(rname, true);
3467 ReleaseSysCache(tuple);
3468 if (OidIsValid(namespaceId) &&
3469 !list_member_oid(oidlist, namespaceId) &&
3470 pg_namespace_aclcheck(namespaceId, roleid,
3471 ACL_USAGE) == ACLCHECK_OK)
3472 oidlist = lappend_oid(oidlist, namespaceId);
3475 else if (strcmp(curname, "pg_temp") == 0)
3477 /* pg_temp --- substitute temp namespace, if any */
3478 if (OidIsValid(myTempNamespace))
3480 if (!list_member_oid(oidlist, myTempNamespace))
3481 oidlist = lappend_oid(oidlist, myTempNamespace);
3485 /* If it ought to be the creation namespace, set flag */
3487 temp_missing = true;
3492 /* normal namespace reference */
3493 namespaceId = get_namespace_oid(curname, true);
3494 if (OidIsValid(namespaceId) &&
3495 !list_member_oid(oidlist, namespaceId) &&
3496 pg_namespace_aclcheck(namespaceId, roleid,
3497 ACL_USAGE) == ACLCHECK_OK)
3498 oidlist = lappend_oid(oidlist, namespaceId);
3503 * Remember the first member of the explicit list. (Note: this is
3504 * nominally wrong if temp_missing, but we need it anyway to distinguish
3505 * explicit from implicit mention of pg_catalog.)
3508 firstNS = InvalidOid;
3510 firstNS = linitial_oid(oidlist);
3513 * Add any implicitly-searched namespaces to the list. Note these go on
3514 * the front, not the back; also notice that we do not check USAGE
3515 * permissions for these.
3517 if (!list_member_oid(oidlist, PG_CATALOG_NAMESPACE))
3518 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
3520 if (OidIsValid(myTempNamespace) &&
3521 !list_member_oid(oidlist, myTempNamespace))
3522 oidlist = lcons_oid(myTempNamespace, oidlist);
3525 * Now that we've successfully built the new list of namespace OIDs, save
3526 * it in permanent storage.
3528 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
3529 newpath = list_copy(oidlist);
3530 MemoryContextSwitchTo(oldcxt);
3532 /* Now safe to assign to state variables. */
3533 list_free(baseSearchPath);
3534 baseSearchPath = newpath;
3535 baseCreationNamespace = firstNS;
3536 baseTempCreationPending = temp_missing;
3538 /* Mark the path valid. */
3539 baseSearchPathValid = true;
3540 namespaceUser = roleid;
3542 /* And make it active. */
3543 activeSearchPath = baseSearchPath;
3544 activeCreationNamespace = baseCreationNamespace;
3545 activeTempCreationPending = baseTempCreationPending;
3549 list_free(namelist);
3554 * InitTempTableNamespace
3555 * Initialize temp table namespace on first use in a particular backend
3558 InitTempTableNamespace(void)
3560 char namespaceName[NAMEDATALEN];
3564 Assert(!OidIsValid(myTempNamespace));
3567 * First, do permission check to see if we are authorized to make temp
3568 * tables. We use a nonstandard error message here since "databasename:
3569 * permission denied" might be a tad cryptic.
3571 * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
3572 * that's necessary since current user ID could change during the session.
3573 * But there's no need to make the namespace in the first place until a
3574 * temp table creation request is made by someone with appropriate rights.
3576 if (pg_database_aclcheck(MyDatabaseId, GetUserId(),
3577 ACL_CREATE_TEMP) != ACLCHECK_OK)
3579 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3580 errmsg("permission denied to create temporary tables in database \"%s\"",
3581 get_database_name(MyDatabaseId))));
3584 * Do not allow a Hot Standby slave session to make temp tables. Aside
3585 * from problems with modifying the system catalogs, there is a naming
3586 * conflict: pg_temp_N belongs to the session with BackendId N on the
3587 * master, not to a slave session with the same BackendId. We should not
3588 * be able to get here anyway due to XactReadOnly checks, but let's just
3589 * make real sure. Note that this also backstops various operations that
3590 * allow XactReadOnly transactions to modify temp tables; they'd need
3591 * RecoveryInProgress checks if not for this.
3593 if (RecoveryInProgress())
3595 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
3596 errmsg("cannot create temporary tables during recovery")));
3598 snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
3600 namespaceId = get_namespace_oid(namespaceName, true);
3601 if (!OidIsValid(namespaceId))
3604 * First use of this temp namespace in this database; create it. The
3605 * temp namespaces are always owned by the superuser. We leave their
3606 * permissions at default --- i.e., no access except to superuser ---
3607 * to ensure that unprivileged users can't peek at other backends'
3608 * temp tables. This works because the places that access the temp
3609 * namespace for my own backend skip permissions checks on it.
3611 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
3613 /* Advance command counter to make namespace visible */
3614 CommandCounterIncrement();
3619 * If the namespace already exists, clean it out (in case the former
3620 * owner crashed without doing so).
3622 RemoveTempRelations(namespaceId);
3626 * If the corresponding toast-table namespace doesn't exist yet, create
3627 * it. (We assume there is no need to clean it out if it does exist, since
3628 * dropping a parent table should make its toast table go away.)
3630 snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
3633 toastspaceId = get_namespace_oid(namespaceName, true);
3634 if (!OidIsValid(toastspaceId))
3636 toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
3638 /* Advance command counter to make namespace visible */
3639 CommandCounterIncrement();
3643 * Okay, we've prepared the temp namespace ... but it's not committed yet,
3644 * so all our work could be undone by transaction rollback. Set flag for
3645 * AtEOXact_Namespace to know what to do.
3647 myTempNamespace = namespaceId;
3648 myTempToastNamespace = toastspaceId;
3650 /* It should not be done already. */
3651 AssertState(myTempNamespaceSubID == InvalidSubTransactionId);
3652 myTempNamespaceSubID = GetCurrentSubTransactionId();
3654 baseSearchPathValid = false; /* need to rebuild list */
3658 * End-of-transaction cleanup for namespaces.
3661 AtEOXact_Namespace(bool isCommit)
3664 * If we abort the transaction in which a temp namespace was selected,
3665 * we'll have to do any creation or cleanout work over again. So, just
3666 * forget the namespace entirely until next time. On the other hand, if
3667 * we commit then register an exit callback to clean out the temp tables
3668 * at backend shutdown. (We only want to register the callback once per
3669 * session, so this is a good place to do it.)
3671 if (myTempNamespaceSubID != InvalidSubTransactionId)
3674 on_shmem_exit(RemoveTempRelationsCallback, 0);
3677 myTempNamespace = InvalidOid;
3678 myTempToastNamespace = InvalidOid;
3679 baseSearchPathValid = false; /* need to rebuild list */
3681 myTempNamespaceSubID = InvalidSubTransactionId;
3685 * Clean up if someone failed to do PopOverrideSearchPath
3690 elog(WARNING, "leaked override search path");
3691 while (overrideStack)
3693 OverrideStackEntry *entry;
3695 entry = (OverrideStackEntry *) linitial(overrideStack);
3696 overrideStack = list_delete_first(overrideStack);
3697 list_free(entry->searchPath);
3700 /* If not baseSearchPathValid, this is useless but harmless */
3701 activeSearchPath = baseSearchPath;
3702 activeCreationNamespace = baseCreationNamespace;
3703 activeTempCreationPending = baseTempCreationPending;
3708 * AtEOSubXact_Namespace
3710 * At subtransaction commit, propagate the temp-namespace-creation
3711 * flag to the parent subtransaction.
3713 * At subtransaction abort, forget the flag if we set it up.
3716 AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid,
3717 SubTransactionId parentSubid)
3719 OverrideStackEntry *entry;
3721 if (myTempNamespaceSubID == mySubid)
3724 myTempNamespaceSubID = parentSubid;
3727 myTempNamespaceSubID = InvalidSubTransactionId;
3728 /* TEMP namespace creation failed, so reset state */
3729 myTempNamespace = InvalidOid;
3730 myTempToastNamespace = InvalidOid;
3731 baseSearchPathValid = false; /* need to rebuild list */
3736 * Clean up if someone failed to do PopOverrideSearchPath
3738 while (overrideStack)
3740 entry = (OverrideStackEntry *) linitial(overrideStack);
3741 if (entry->nestLevel < GetCurrentTransactionNestLevel())
3744 elog(WARNING, "leaked override search path");
3745 overrideStack = list_delete_first(overrideStack);
3746 list_free(entry->searchPath);
3750 /* Activate the next level down. */
3753 entry = (OverrideStackEntry *) linitial(overrideStack);
3754 activeSearchPath = entry->searchPath;
3755 activeCreationNamespace = entry->creationNamespace;
3756 activeTempCreationPending = false; /* XXX is this OK? */
3760 /* If not baseSearchPathValid, this is useless but harmless */
3761 activeSearchPath = baseSearchPath;
3762 activeCreationNamespace = baseCreationNamespace;
3763 activeTempCreationPending = baseTempCreationPending;
3768 * Remove all relations in the specified temp namespace.
3770 * This is called at backend shutdown (if we made any temp relations).
3771 * It is also called when we begin using a pre-existing temp namespace,
3772 * in order to clean out any relations that might have been created by
3773 * a crashed backend.
3776 RemoveTempRelations(Oid tempNamespaceId)
3778 ObjectAddress object;
3781 * We want to get rid of everything in the target namespace, but not the
3782 * namespace itself (deleting it only to recreate it later would be a
3783 * waste of cycles). We do this by finding everything that has a
3784 * dependency on the namespace.
3786 object.classId = NamespaceRelationId;
3787 object.objectId = tempNamespaceId;
3788 object.objectSubId = 0;
3790 deleteWhatDependsOn(&object, false);
3794 * Callback to remove temp relations at backend exit.
3797 RemoveTempRelationsCallback(int code, Datum arg)
3799 if (OidIsValid(myTempNamespace)) /* should always be true */
3801 /* Need to ensure we have a usable transaction. */
3802 AbortOutOfAnyTransaction();
3803 StartTransactionCommand();
3805 RemoveTempRelations(myTempNamespace);
3807 CommitTransactionCommand();
3812 * Remove all temp tables from the temporary namespace.
3815 ResetTempTableNamespace(void)
3817 if (OidIsValid(myTempNamespace))
3818 RemoveTempRelations(myTempNamespace);
3823 * Routines for handling the GUC variable 'search_path'.
3826 /* check_hook: validate new search_path value */
3828 check_search_path(char **newval, void **extra, GucSource source)
3833 /* Need a modifiable copy of string */
3834 rawname = pstrdup(*newval);
3836 /* Parse string into list of identifiers */
3837 if (!SplitIdentifierString(rawname, ',', &namelist))
3839 /* syntax error in name list */
3840 GUC_check_errdetail("List syntax is invalid.");
3842 list_free(namelist);
3847 * We used to try to check that the named schemas exist, but there are
3848 * many valid use-cases for having search_path settings that include
3849 * schemas that don't exist; and often, we are not inside a transaction
3850 * here and so can't consult the system catalogs anyway. So now, the only
3851 * requirement is syntactic validity of the identifier list.
3855 list_free(namelist);
3860 /* assign_hook: do extra actions as needed */
3862 assign_search_path(const char *newval, void *extra)
3865 * We mark the path as needing recomputation, but don't do anything until
3866 * it's needed. This avoids trying to do database access during GUC
3867 * initialization, or outside a transaction.
3869 baseSearchPathValid = false;
3873 * InitializeSearchPath: initialize module during InitPostgres.
3875 * This is called after we are up enough to be able to do catalog lookups.
3878 InitializeSearchPath(void)
3880 if (IsBootstrapProcessingMode())
3883 * In bootstrap mode, the search path must be 'pg_catalog' so that
3884 * tables are created in the proper namespace; ignore the GUC setting.
3886 MemoryContext oldcxt;
3888 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
3889 baseSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
3890 MemoryContextSwitchTo(oldcxt);
3891 baseCreationNamespace = PG_CATALOG_NAMESPACE;
3892 baseTempCreationPending = false;
3893 baseSearchPathValid = true;
3894 namespaceUser = GetUserId();
3895 activeSearchPath = baseSearchPath;
3896 activeCreationNamespace = baseCreationNamespace;
3897 activeTempCreationPending = baseTempCreationPending;
3902 * In normal mode, arrange for a callback on any syscache invalidation
3903 * of pg_namespace rows.
3905 CacheRegisterSyscacheCallback(NAMESPACEOID,
3908 /* Force search path to be recomputed on next use */
3909 baseSearchPathValid = false;
3915 * Syscache inval callback function
3918 NamespaceCallback(Datum arg, int cacheid, uint32 hashvalue)
3920 /* Force search path to be recomputed on next use */
3921 baseSearchPathValid = false;
3925 * Fetch the active search path. The return value is a palloc'ed list
3926 * of OIDs; the caller is responsible for freeing this storage as
3929 * The returned list includes the implicitly-prepended namespaces only if
3930 * includeImplicit is true.
3932 * Note: calling this may result in a CommandCounterIncrement operation,
3933 * if we have to create or clean out the temp namespace.
3936 fetch_search_path(bool includeImplicit)
3940 recomputeNamespacePath();
3943 * If the temp namespace should be first, force it to exist. This is so
3944 * that callers can trust the result to reflect the actual default
3945 * creation namespace. It's a bit bogus to do this here, since
3946 * current_schema() is supposedly a stable function without side-effects,
3947 * but the alternatives seem worse.
3949 if (activeTempCreationPending)
3951 InitTempTableNamespace();
3952 recomputeNamespacePath();
3955 result = list_copy(activeSearchPath);
3956 if (!includeImplicit)
3958 while (result && linitial_oid(result) != activeCreationNamespace)
3959 result = list_delete_first(result);
3966 * Fetch the active search path into a caller-allocated array of OIDs.
3967 * Returns the number of path entries. (If this is more than sarray_len,
3968 * then the data didn't fit and is not all stored.)
3970 * The returned list always includes the implicitly-prepended namespaces,
3971 * but never includes the temp namespace. (This is suitable for existing
3972 * users, which would want to ignore the temp namespace anyway.) This
3973 * definition allows us to not worry about initializing the temp namespace.
3976 fetch_search_path_array(Oid *sarray, int sarray_len)
3981 recomputeNamespacePath();
3983 foreach(l, activeSearchPath)
3985 Oid namespaceId = lfirst_oid(l);
3987 if (namespaceId == myTempNamespace)
3988 continue; /* do not include temp namespace */
3990 if (count < sarray_len)
3991 sarray[count] = namespaceId;
4000 * Export the FooIsVisible functions as SQL-callable functions.
4002 * Note: as of Postgres 8.4, these will silently return NULL if called on
4003 * a nonexistent object OID, rather than failing. This is to avoid race
4004 * condition errors when a query that's scanning a catalog using an MVCC
4005 * snapshot uses one of these functions. The underlying IsVisible functions
4006 * operate on SnapshotNow semantics and so might see the object as already
4007 * gone when it's still visible to the MVCC snapshot. (There is no race
4008 * condition in the current coding because we don't accept sinval messages
4009 * between the SearchSysCacheExists test and the subsequent lookup.)
4013 pg_table_is_visible(PG_FUNCTION_ARGS)
4015 Oid oid = PG_GETARG_OID(0);
4017 if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(oid)))
4020 PG_RETURN_BOOL(RelationIsVisible(oid));
4024 pg_type_is_visible(PG_FUNCTION_ARGS)
4026 Oid oid = PG_GETARG_OID(0);
4028 if (!SearchSysCacheExists1(TYPEOID, ObjectIdGetDatum(oid)))
4031 PG_RETURN_BOOL(TypeIsVisible(oid));
4035 pg_function_is_visible(PG_FUNCTION_ARGS)
4037 Oid oid = PG_GETARG_OID(0);
4039 if (!SearchSysCacheExists1(PROCOID, ObjectIdGetDatum(oid)))
4042 PG_RETURN_BOOL(FunctionIsVisible(oid));
4046 pg_operator_is_visible(PG_FUNCTION_ARGS)
4048 Oid oid = PG_GETARG_OID(0);
4050 if (!SearchSysCacheExists1(OPEROID, ObjectIdGetDatum(oid)))
4053 PG_RETURN_BOOL(OperatorIsVisible(oid));
4057 pg_opclass_is_visible(PG_FUNCTION_ARGS)
4059 Oid oid = PG_GETARG_OID(0);
4061 if (!SearchSysCacheExists1(CLAOID, ObjectIdGetDatum(oid)))
4064 PG_RETURN_BOOL(OpclassIsVisible(oid));
4068 pg_opfamily_is_visible(PG_FUNCTION_ARGS)
4070 Oid oid = PG_GETARG_OID(0);
4072 if (!SearchSysCacheExists1(OPFAMILYOID, ObjectIdGetDatum(oid)))
4075 PG_RETURN_BOOL(OpfamilyIsVisible(oid));
4079 pg_collation_is_visible(PG_FUNCTION_ARGS)
4081 Oid oid = PG_GETARG_OID(0);
4083 if (!SearchSysCacheExists1(COLLOID, ObjectIdGetDatum(oid)))
4086 PG_RETURN_BOOL(CollationIsVisible(oid));
4090 pg_conversion_is_visible(PG_FUNCTION_ARGS)
4092 Oid oid = PG_GETARG_OID(0);
4094 if (!SearchSysCacheExists1(CONVOID, ObjectIdGetDatum(oid)))
4097 PG_RETURN_BOOL(ConversionIsVisible(oid));
4101 pg_ts_parser_is_visible(PG_FUNCTION_ARGS)
4103 Oid oid = PG_GETARG_OID(0);
4105 if (!SearchSysCacheExists1(TSPARSEROID, ObjectIdGetDatum(oid)))
4108 PG_RETURN_BOOL(TSParserIsVisible(oid));
4112 pg_ts_dict_is_visible(PG_FUNCTION_ARGS)
4114 Oid oid = PG_GETARG_OID(0);
4116 if (!SearchSysCacheExists1(TSDICTOID, ObjectIdGetDatum(oid)))
4119 PG_RETURN_BOOL(TSDictionaryIsVisible(oid));
4123 pg_ts_template_is_visible(PG_FUNCTION_ARGS)
4125 Oid oid = PG_GETARG_OID(0);
4127 if (!SearchSysCacheExists1(TSTEMPLATEOID, ObjectIdGetDatum(oid)))
4130 PG_RETURN_BOOL(TSTemplateIsVisible(oid));
4134 pg_ts_config_is_visible(PG_FUNCTION_ARGS)
4136 Oid oid = PG_GETARG_OID(0);
4138 if (!SearchSysCacheExists1(TSCONFIGOID, ObjectIdGetDatum(oid)))
4141 PG_RETURN_BOOL(TSConfigIsVisible(oid));
4145 pg_my_temp_schema(PG_FUNCTION_ARGS)
4147 PG_RETURN_OID(myTempNamespace);
4151 pg_is_other_temp_schema(PG_FUNCTION_ARGS)
4153 Oid oid = PG_GETARG_OID(0);
4155 PG_RETURN_BOOL(isOtherTempNamespace(oid));