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-2002, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.28 2002/08/06 05:40:44 ishii Exp $
18 *-------------------------------------------------------------------------
22 #include "access/heapam.h"
23 #include "access/xact.h"
24 #include "catalog/catalog.h"
25 #include "catalog/catname.h"
26 #include "catalog/dependency.h"
27 #include "catalog/heap.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_conversion.h"
30 #include "catalog/pg_inherits.h"
31 #include "catalog/pg_namespace.h"
32 #include "catalog/pg_opclass.h"
33 #include "catalog/pg_operator.h"
34 #include "catalog/pg_proc.h"
35 #include "catalog/pg_shadow.h"
36 #include "catalog/pg_type.h"
37 #include "lib/stringinfo.h"
38 #include "miscadmin.h"
39 #include "nodes/makefuncs.h"
40 #include "storage/backendid.h"
41 #include "storage/ipc.h"
42 #include "utils/acl.h"
43 #include "utils/builtins.h"
44 #include "utils/catcache.h"
45 #include "utils/fmgroids.h"
46 #include "utils/guc.h"
47 #include "utils/inval.h"
48 #include "utils/lsyscache.h"
49 #include "utils/syscache.h"
53 * The namespace search path is a possibly-empty list of namespace OIDs.
54 * In addition to the explicit list, several implicitly-searched namespaces
57 * 1. If a "special" namespace has been set by PushSpecialNamespace, it is
58 * always searched first. (This is a hack for CREATE SCHEMA.)
60 * 2. If a TEMP table namespace has been initialized in this session, it
61 * is always searched just after any special namespace.
63 * 3. The system catalog namespace is always searched. If the system
64 * namespace is present in the explicit path then it will be searched in
65 * the specified order; otherwise it will be searched after TEMP tables and
66 * *before* the explicit list. (It might seem that the system namespace
67 * should be implicitly last, but this behavior appears to be required by
68 * SQL99. Also, this provides a way to search the system namespace first
69 * without thereby making it the default creation target namespace.)
71 * The default creation target namespace is normally equal to the first
72 * element of the explicit list, but is the "special" namespace when one
73 * has been set. If the explicit list is empty and there is no special
74 * namespace, there is no default target.
76 * In bootstrap mode, the search path is set equal to 'pg_catalog', so that
77 * the system namespace is the only one searched or inserted into.
78 * The initdb script is also careful to set search_path to 'pg_catalog' for
79 * its post-bootstrap standalone backend runs. Otherwise the default search
80 * path is determined by GUC. The factory default path contains the PUBLIC
81 * namespace (if it exists), preceded by the user's personal namespace
84 * If namespaceSearchPathValid is false, then namespaceSearchPath (and other
85 * derived variables) need to be recomputed from namespace_search_path.
86 * We mark it invalid upon an assignment to namespace_search_path or receipt
87 * of a syscache invalidation event for pg_namespace. The recomputation
88 * is done during the next lookup attempt.
90 * Any namespaces mentioned in namespace_search_path that are not readable
91 * by the current user ID are simply left out of namespaceSearchPath; so
92 * we have to be willing to recompute the path when current userid changes.
93 * namespaceUser is the userid the path has been computed for.
96 static List *namespaceSearchPath = NIL;
98 static Oid namespaceUser = InvalidOid;
100 /* default place to create stuff; if InvalidOid, no default */
101 static Oid defaultCreationNamespace = InvalidOid;
103 /* first explicit member of list; usually same as defaultCreationNamespace */
104 static Oid firstExplicitNamespace = InvalidOid;
106 /* The above four values are valid only if namespaceSearchPathValid */
107 static bool namespaceSearchPathValid = true;
110 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
111 * in a particular backend session (this happens when a CREATE TEMP TABLE
112 * command is first executed). Thereafter it's the OID of the temp namespace.
113 * firstTempTransaction flags whether we've committed creation of the TEMP
116 static Oid myTempNamespace = InvalidOid;
118 static bool firstTempTransaction = false;
121 * "Special" namespace for CREATE SCHEMA. If set, it's the first search
122 * path element, and also the default creation namespace.
124 static Oid mySpecialNamespace = InvalidOid;
127 * This is the text equivalent of the search path --- it's the value
128 * of the GUC variable 'search_path'.
130 char *namespace_search_path = NULL;
133 /* Local functions */
134 static void recomputeNamespacePath(void);
135 static void InitTempTableNamespace(void);
136 static void RemoveTempRelations(Oid tempNamespaceId);
137 static void RemoveTempRelationsCallback(void);
138 static void NamespaceCallback(Datum arg, Oid relid);
143 * Given a RangeVar describing an existing relation,
144 * select the proper namespace and look up the relation OID.
146 * If the relation is not found, return InvalidOid if failOK = true,
147 * otherwise raise an error.
150 RangeVarGetRelid(const RangeVar *relation, bool failOK)
156 * We check the catalog name and then ignore it.
158 if (relation->catalogname)
160 if (strcmp(relation->catalogname, DatabaseName) != 0)
161 elog(ERROR, "Cross-database references are not implemented");
164 if (relation->schemaname)
166 /* use exact schema given */
167 namespaceId = LookupExplicitNamespace(relation->schemaname);
168 relId = get_relname_relid(relation->relname, namespaceId);
172 /* search the namespace path */
173 relId = RelnameGetRelid(relation->relname);
176 if (!OidIsValid(relId) && !failOK)
178 if (relation->schemaname)
179 elog(ERROR, "Relation \"%s\".\"%s\" does not exist",
180 relation->schemaname, relation->relname);
182 elog(ERROR, "Relation \"%s\" does not exist",
189 * RangeVarGetCreationNamespace
190 * Given a RangeVar describing a to-be-created relation,
191 * choose which namespace to create it in.
193 * Note: calling this may result in a CommandCounterIncrement operation.
194 * That will happen on the first request for a temp table in any particular
195 * backend run; we will need to either create or clean out the temp schema.
198 RangeVarGetCreationNamespace(const RangeVar *newRelation)
203 * We check the catalog name and then ignore it.
205 if (newRelation->catalogname)
207 if (strcmp(newRelation->catalogname, DatabaseName) != 0)
208 elog(ERROR, "Cross-database references are not implemented");
211 if (newRelation->istemp)
213 /* TEMP tables are created in our backend-local temp namespace */
214 if (newRelation->schemaname)
215 elog(ERROR, "TEMP tables may not specify a namespace");
216 /* Initialize temp namespace if first time through */
217 if (!OidIsValid(myTempNamespace))
218 InitTempTableNamespace();
219 return myTempNamespace;
222 if (newRelation->schemaname)
224 /* use exact schema given */
225 namespaceId = GetSysCacheOid(NAMESPACENAME,
226 CStringGetDatum(newRelation->schemaname),
228 if (!OidIsValid(namespaceId))
229 elog(ERROR, "Namespace \"%s\" does not exist",
230 newRelation->schemaname);
231 /* we do not check for USAGE rights here! */
235 /* use the default creation namespace */
236 recomputeNamespacePath();
237 namespaceId = defaultCreationNamespace;
238 if (!OidIsValid(namespaceId))
239 elog(ERROR, "No namespace has been selected to create in");
242 /* Note: callers will check for CREATE rights when appropriate */
249 * Try to resolve an unqualified relation name.
250 * Returns OID if relation found in search path, else InvalidOid.
253 RelnameGetRelid(const char *relname)
258 recomputeNamespacePath();
260 foreach(lptr, namespaceSearchPath)
262 Oid namespaceId = (Oid) lfirsti(lptr);
264 relid = get_relname_relid(relname, namespaceId);
265 if (OidIsValid(relid))
269 /* Not found in path */
275 * Determine whether a relation (identified by OID) is visible in the
276 * current search path. Visible means "would be found by searching
277 * for the unqualified relation name".
280 RelationIsVisible(Oid relid)
283 Form_pg_class relform;
287 reltup = SearchSysCache(RELOID,
288 ObjectIdGetDatum(relid),
290 if (!HeapTupleIsValid(reltup))
291 elog(ERROR, "Cache lookup failed for relation %u", relid);
292 relform = (Form_pg_class) GETSTRUCT(reltup);
294 recomputeNamespacePath();
297 * Quick check: if it ain't in the path at all, it ain't visible.
298 * Items in the system namespace are surely in the path and so we
299 * needn't even do intMember() for them.
301 relnamespace = relform->relnamespace;
302 if (relnamespace != PG_CATALOG_NAMESPACE &&
303 !intMember(relnamespace, namespaceSearchPath))
308 * If it is in the path, it might still not be visible; it could be
309 * hidden by another relation of the same name earlier in the path.
310 * So we must do a slow check to see if this rel would be found by
313 char *relname = NameStr(relform->relname);
315 visible = (RelnameGetRelid(relname) == relid);
318 ReleaseSysCache(reltup);
326 * Try to resolve an unqualified datatype name.
327 * Returns OID if type found in search path, else InvalidOid.
329 * This is essentially the same as RelnameGetRelid.
332 TypenameGetTypid(const char *typname)
337 recomputeNamespacePath();
339 foreach(lptr, namespaceSearchPath)
341 Oid namespaceId = (Oid) lfirsti(lptr);
343 typid = GetSysCacheOid(TYPENAMENSP,
344 PointerGetDatum(typname),
345 ObjectIdGetDatum(namespaceId),
347 if (OidIsValid(typid))
351 /* Not found in path */
357 * Determine whether a type (identified by OID) is visible in the
358 * current search path. Visible means "would be found by searching
359 * for the unqualified type name".
362 TypeIsVisible(Oid typid)
365 Form_pg_type typform;
369 typtup = SearchSysCache(TYPEOID,
370 ObjectIdGetDatum(typid),
372 if (!HeapTupleIsValid(typtup))
373 elog(ERROR, "Cache lookup failed for type %u", typid);
374 typform = (Form_pg_type) GETSTRUCT(typtup);
376 recomputeNamespacePath();
379 * Quick check: if it ain't in the path at all, it ain't visible.
380 * Items in the system namespace are surely in the path and so we
381 * needn't even do intMember() for them.
383 typnamespace = typform->typnamespace;
384 if (typnamespace != PG_CATALOG_NAMESPACE &&
385 !intMember(typnamespace, namespaceSearchPath))
390 * If it is in the path, it might still not be visible; it could be
391 * hidden by another type of the same name earlier in the path.
392 * So we must do a slow check to see if this type would be found by
395 char *typname = NameStr(typform->typname);
397 visible = (TypenameGetTypid(typname) == typid);
400 ReleaseSysCache(typtup);
407 * FuncnameGetCandidates
408 * Given a possibly-qualified function name and argument count,
409 * retrieve a list of the possible matches.
411 * If nargs is -1, we return all functions matching the given name,
412 * regardless of argument count.
414 * We search a single namespace if the function name is qualified, else
415 * all namespaces in the search path. The return list will never contain
416 * multiple entries with identical argument lists --- in the multiple-
417 * namespace case, we arrange for entries in earlier namespaces to mask
418 * identical entries in later namespaces.
421 FuncnameGetCandidates(List *names, int nargs)
423 FuncCandidateList resultList = NULL;
430 /* deconstruct the name list */
431 DeconstructQualifiedName(names, &schemaname, &funcname);
435 /* use exact schema given */
436 namespaceId = LookupExplicitNamespace(schemaname);
440 /* flag to indicate we need namespace search */
441 namespaceId = InvalidOid;
442 recomputeNamespacePath();
445 /* Search syscache by name and (optionally) nargs only */
447 catlist = SearchSysCacheList(PROCNAMENSP, 2,
448 CStringGetDatum(funcname),
449 Int16GetDatum(nargs),
452 catlist = SearchSysCacheList(PROCNAMENSP, 1,
453 CStringGetDatum(funcname),
456 for (i = 0; i < catlist->n_members; i++)
458 HeapTuple proctup = &catlist->members[i]->tuple;
459 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
461 FuncCandidateList newResult;
463 nargs = procform->pronargs;
465 if (OidIsValid(namespaceId))
467 /* Consider only procs in specified namespace */
468 if (procform->pronamespace != namespaceId)
470 /* No need to check args, they must all be different */
474 /* Consider only procs that are in the search path */
477 foreach(nsp, namespaceSearchPath)
479 if (procform->pronamespace == (Oid) lfirsti(nsp))
484 continue; /* proc is not in search path */
487 * Okay, it's in the search path, but does it have the same
488 * arguments as something we already accepted? If so, keep
489 * only the one that appears earlier in the search path.
491 * If we have an ordered list from SearchSysCacheList (the
492 * normal case), then any conflicting proc must immediately
493 * adjoin this one in the list, so we only need to look at
494 * the newest result item. If we have an unordered list,
495 * we have to scan the whole result list.
499 FuncCandidateList prevResult;
501 if (catlist->ordered)
503 if (nargs == resultList->nargs &&
504 memcmp(procform->proargtypes, resultList->args,
505 nargs * sizeof(Oid)) == 0)
506 prevResult = resultList;
512 for (prevResult = resultList;
514 prevResult = prevResult->next)
516 if (nargs == prevResult->nargs &&
517 memcmp(procform->proargtypes, prevResult->args,
518 nargs * sizeof(Oid)) == 0)
524 /* We have a match with a previous result */
525 Assert(pathpos != prevResult->pathpos);
526 if (pathpos > prevResult->pathpos)
527 continue; /* keep previous result */
528 /* replace previous result */
529 prevResult->pathpos = pathpos;
530 prevResult->oid = HeapTupleGetOid(proctup);
531 continue; /* args are same, of course */
537 * Okay to add it to result list
539 newResult = (FuncCandidateList)
540 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
541 + nargs * sizeof(Oid));
542 newResult->pathpos = pathpos;
543 newResult->oid = HeapTupleGetOid(proctup);
544 newResult->nargs = nargs;
545 memcpy(newResult->args, procform->proargtypes, nargs * sizeof(Oid));
547 newResult->next = resultList;
548 resultList = newResult;
551 ReleaseSysCacheList(catlist);
558 * Determine whether a function (identified by OID) is visible in the
559 * current search path. Visible means "would be found by searching
560 * for the unqualified function name with exact argument matches".
563 FunctionIsVisible(Oid funcid)
566 Form_pg_proc procform;
570 proctup = SearchSysCache(PROCOID,
571 ObjectIdGetDatum(funcid),
573 if (!HeapTupleIsValid(proctup))
574 elog(ERROR, "Cache lookup failed for procedure %u", funcid);
575 procform = (Form_pg_proc) GETSTRUCT(proctup);
577 recomputeNamespacePath();
580 * Quick check: if it ain't in the path at all, it ain't visible.
581 * Items in the system namespace are surely in the path and so we
582 * needn't even do intMember() for them.
584 pronamespace = procform->pronamespace;
585 if (pronamespace != PG_CATALOG_NAMESPACE &&
586 !intMember(pronamespace, namespaceSearchPath))
591 * If it is in the path, it might still not be visible; it could be
592 * hidden by another proc of the same name and arguments earlier
593 * in the path. So we must do a slow check to see if this is the
594 * same proc that would be found by FuncnameGetCandidates.
596 char *proname = NameStr(procform->proname);
597 int nargs = procform->pronargs;
598 FuncCandidateList clist;
602 clist = FuncnameGetCandidates(makeList1(makeString(proname)), nargs);
604 for (; clist; clist = clist->next)
606 if (memcmp(clist->args, procform->proargtypes,
607 nargs * sizeof(Oid)) == 0)
609 /* Found the expected entry; is it the right proc? */
610 visible = (clist->oid == funcid);
616 ReleaseSysCache(proctup);
623 * OpernameGetCandidates
624 * Given a possibly-qualified operator name and operator kind,
625 * retrieve a list of the possible matches.
627 * If oprkind is '\0', we return all operators matching the given name,
628 * regardless of arguments.
630 * We search a single namespace if the operator name is qualified, else
631 * all namespaces in the search path. The return list will never contain
632 * multiple entries with identical argument lists --- in the multiple-
633 * namespace case, we arrange for entries in earlier namespaces to mask
634 * identical entries in later namespaces.
636 * The returned items always have two args[] entries --- one or the other
637 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
640 OpernameGetCandidates(List *names, char oprkind)
642 FuncCandidateList resultList = NULL;
649 /* deconstruct the name list */
650 DeconstructQualifiedName(names, &schemaname, &opername);
654 /* use exact schema given */
655 namespaceId = LookupExplicitNamespace(schemaname);
659 /* flag to indicate we need namespace search */
660 namespaceId = InvalidOid;
661 recomputeNamespacePath();
664 /* Search syscache by name only */
665 catlist = SearchSysCacheList(OPERNAMENSP, 1,
666 CStringGetDatum(opername),
669 for (i = 0; i < catlist->n_members; i++)
671 HeapTuple opertup = &catlist->members[i]->tuple;
672 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
674 FuncCandidateList newResult;
676 /* Ignore operators of wrong kind, if specific kind requested */
677 if (oprkind && operform->oprkind != oprkind)
680 if (OidIsValid(namespaceId))
682 /* Consider only opers in specified namespace */
683 if (operform->oprnamespace != namespaceId)
685 /* No need to check args, they must all be different */
689 /* Consider only opers that are in the search path */
692 foreach(nsp, namespaceSearchPath)
694 if (operform->oprnamespace == (Oid) lfirsti(nsp))
699 continue; /* oper is not in search path */
702 * Okay, it's in the search path, but does it have the same
703 * arguments as something we already accepted? If so, keep
704 * only the one that appears earlier in the search path.
706 * If we have an ordered list from SearchSysCacheList (the
707 * normal case), then any conflicting oper must immediately
708 * adjoin this one in the list, so we only need to look at
709 * the newest result item. If we have an unordered list,
710 * we have to scan the whole result list.
714 FuncCandidateList prevResult;
716 if (catlist->ordered)
718 if (operform->oprleft == resultList->args[0] &&
719 operform->oprright == resultList->args[1])
720 prevResult = resultList;
726 for (prevResult = resultList;
728 prevResult = prevResult->next)
730 if (operform->oprleft == prevResult->args[0] &&
731 operform->oprright == prevResult->args[1])
737 /* We have a match with a previous result */
738 Assert(pathpos != prevResult->pathpos);
739 if (pathpos > prevResult->pathpos)
740 continue; /* keep previous result */
741 /* replace previous result */
742 prevResult->pathpos = pathpos;
743 prevResult->oid = HeapTupleGetOid(opertup);
744 continue; /* args are same, of course */
750 * Okay to add it to result list
752 newResult = (FuncCandidateList)
753 palloc(sizeof(struct _FuncCandidateList) + sizeof(Oid));
754 newResult->pathpos = pathpos;
755 newResult->oid = HeapTupleGetOid(opertup);
756 newResult->nargs = 2;
757 newResult->args[0] = operform->oprleft;
758 newResult->args[1] = operform->oprright;
759 newResult->next = resultList;
760 resultList = newResult;
763 ReleaseSysCacheList(catlist);
770 * Determine whether an operator (identified by OID) is visible in the
771 * current search path. Visible means "would be found by searching
772 * for the unqualified operator name with exact argument matches".
775 OperatorIsVisible(Oid oprid)
778 Form_pg_operator oprform;
782 oprtup = SearchSysCache(OPEROID,
783 ObjectIdGetDatum(oprid),
785 if (!HeapTupleIsValid(oprtup))
786 elog(ERROR, "Cache lookup failed for operator %u", oprid);
787 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
789 recomputeNamespacePath();
792 * Quick check: if it ain't in the path at all, it ain't visible.
793 * Items in the system namespace are surely in the path and so we
794 * needn't even do intMember() for them.
796 oprnamespace = oprform->oprnamespace;
797 if (oprnamespace != PG_CATALOG_NAMESPACE &&
798 !intMember(oprnamespace, namespaceSearchPath))
803 * If it is in the path, it might still not be visible; it could be
804 * hidden by another operator of the same name and arguments earlier
805 * in the path. So we must do a slow check to see if this is the
806 * same operator that would be found by OpernameGetCandidates.
808 char *oprname = NameStr(oprform->oprname);
809 FuncCandidateList clist;
813 clist = OpernameGetCandidates(makeList1(makeString(oprname)),
816 for (; clist; clist = clist->next)
818 if (clist->args[0] == oprform->oprleft &&
819 clist->args[1] == oprform->oprright)
821 /* Found the expected entry; is it the right op? */
822 visible = (clist->oid == oprid);
828 ReleaseSysCache(oprtup);
835 * OpclassGetCandidates
836 * Given an index access method OID, retrieve a list of all the
837 * opclasses for that AM that are visible in the search path.
839 * NOTE: the opcname_tmp field in the returned structs should not be used
840 * by callers, because it points at syscache entries that we release at
841 * the end of this routine. If any callers needed the name information,
842 * we could pstrdup() the names ... but at present it'd be wasteful.
845 OpclassGetCandidates(Oid amid)
847 OpclassCandidateList resultList = NULL;
851 /* Search syscache by AM OID only */
852 catlist = SearchSysCacheList(CLAAMNAMENSP, 1,
853 ObjectIdGetDatum(amid),
856 recomputeNamespacePath();
858 for (i = 0; i < catlist->n_members; i++)
860 HeapTuple opctup = &catlist->members[i]->tuple;
861 Form_pg_opclass opcform = (Form_pg_opclass) GETSTRUCT(opctup);
863 OpclassCandidateList newResult;
866 /* Consider only opclasses that are in the search path */
867 foreach(nsp, namespaceSearchPath)
869 if (opcform->opcnamespace == (Oid) lfirsti(nsp))
874 continue; /* opclass is not in search path */
877 * Okay, it's in the search path, but does it have the same name
878 * as something we already accepted? If so, keep
879 * only the one that appears earlier in the search path.
881 * If we have an ordered list from SearchSysCacheList (the
882 * normal case), then any conflicting opclass must immediately
883 * adjoin this one in the list, so we only need to look at
884 * the newest result item. If we have an unordered list,
885 * we have to scan the whole result list.
889 OpclassCandidateList prevResult;
891 if (catlist->ordered)
893 if (strcmp(NameStr(opcform->opcname),
894 resultList->opcname_tmp) == 0)
895 prevResult = resultList;
901 for (prevResult = resultList;
903 prevResult = prevResult->next)
905 if (strcmp(NameStr(opcform->opcname),
906 prevResult->opcname_tmp) == 0)
912 /* We have a match with a previous result */
913 Assert(pathpos != prevResult->pathpos);
914 if (pathpos > prevResult->pathpos)
915 continue; /* keep previous result */
916 /* replace previous result */
917 prevResult->opcname_tmp = NameStr(opcform->opcname);
918 prevResult->pathpos = pathpos;
919 prevResult->oid = HeapTupleGetOid(opctup);
920 prevResult->opcintype = opcform->opcintype;
921 prevResult->opcdefault = opcform->opcdefault;
922 prevResult->opckeytype = opcform->opckeytype;
928 * Okay to add it to result list
930 newResult = (OpclassCandidateList)
931 palloc(sizeof(struct _OpclassCandidateList));
932 newResult->opcname_tmp = NameStr(opcform->opcname);
933 newResult->pathpos = pathpos;
934 newResult->oid = HeapTupleGetOid(opctup);
935 newResult->opcintype = opcform->opcintype;
936 newResult->opcdefault = opcform->opcdefault;
937 newResult->opckeytype = opcform->opckeytype;
938 newResult->next = resultList;
939 resultList = newResult;
942 ReleaseSysCacheList(catlist);
948 * OpclassnameGetOpcid
949 * Try to resolve an unqualified index opclass name.
950 * Returns OID if opclass found in search path, else InvalidOid.
952 * This is essentially the same as TypenameGetTypid, but we have to have
953 * an extra argument for the index AM OID.
956 OpclassnameGetOpcid(Oid amid, const char *opcname)
961 recomputeNamespacePath();
963 foreach(lptr, namespaceSearchPath)
965 Oid namespaceId = (Oid) lfirsti(lptr);
967 opcid = GetSysCacheOid(CLAAMNAMENSP,
968 ObjectIdGetDatum(amid),
969 PointerGetDatum(opcname),
970 ObjectIdGetDatum(namespaceId),
972 if (OidIsValid(opcid))
976 /* Not found in path */
982 * Determine whether an opclass (identified by OID) is visible in the
983 * current search path. Visible means "would be found by searching
984 * for the unqualified opclass name".
987 OpclassIsVisible(Oid opcid)
990 Form_pg_opclass opcform;
994 opctup = SearchSysCache(CLAOID,
995 ObjectIdGetDatum(opcid),
997 if (!HeapTupleIsValid(opctup))
998 elog(ERROR, "Cache lookup failed for opclass %u", opcid);
999 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1001 recomputeNamespacePath();
1004 * Quick check: if it ain't in the path at all, it ain't visible.
1005 * Items in the system namespace are surely in the path and so we
1006 * needn't even do intMember() for them.
1008 opcnamespace = opcform->opcnamespace;
1009 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1010 !intMember(opcnamespace, namespaceSearchPath))
1015 * If it is in the path, it might still not be visible; it could be
1016 * hidden by another opclass of the same name earlier in the path.
1017 * So we must do a slow check to see if this opclass would be found by
1018 * OpclassnameGetOpcid.
1020 char *opcname = NameStr(opcform->opcname);
1022 visible = (OpclassnameGetOpcid(opcform->opcamid, opcname) == opcid);
1025 ReleaseSysCache(opctup);
1031 * DeconstructQualifiedName
1032 * Given a possibly-qualified name expressed as a list of String nodes,
1033 * extract the schema name and object name.
1035 * *nspname_p is set to NULL if there is no explicit schema name.
1038 DeconstructQualifiedName(List *names,
1043 char *schemaname = NULL;
1044 char *objname = NULL;
1046 switch (length(names))
1049 objname = strVal(lfirst(names));
1052 schemaname = strVal(lfirst(names));
1053 objname = strVal(lsecond(names));
1056 catalogname = strVal(lfirst(names));
1057 schemaname = strVal(lsecond(names));
1058 objname = strVal(lfirst(lnext(lnext(names))));
1060 * We check the catalog name and then ignore it.
1062 if (strcmp(catalogname, DatabaseName) != 0)
1063 elog(ERROR, "Cross-database references are not implemented");
1066 elog(ERROR, "Improper qualified name (too many dotted names): %s",
1067 NameListToString(names));
1071 *nspname_p = schemaname;
1072 *objname_p = objname;
1076 * LookupExplicitNamespace
1077 * Process an explicitly-specified schema name: look up the schema
1078 * and verify we have USAGE (lookup) rights in it.
1080 * Returns the namespace OID. Raises elog if any problem.
1083 LookupExplicitNamespace(char *nspname)
1086 AclResult aclresult;
1088 namespaceId = GetSysCacheOid(NAMESPACENAME,
1089 CStringGetDatum(nspname),
1091 if (!OidIsValid(namespaceId))
1092 elog(ERROR, "Namespace \"%s\" does not exist", nspname);
1094 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
1095 if (aclresult != ACLCHECK_OK)
1096 aclcheck_error(aclresult, nspname);
1102 * QualifiedNameGetCreationNamespace
1103 * Given a possibly-qualified name for an object (in List-of-Values
1104 * format), determine what namespace the object should be created in.
1105 * Also extract and return the object name (last component of list).
1107 * This is *not* used for tables. Hence, the TEMP table namespace is
1108 * never selected as the creation target.
1111 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
1117 /* deconstruct the name list */
1118 DeconstructQualifiedName(names, &schemaname, &objname);
1122 /* use exact schema given */
1123 namespaceId = GetSysCacheOid(NAMESPACENAME,
1124 CStringGetDatum(schemaname),
1126 if (!OidIsValid(namespaceId))
1127 elog(ERROR, "Namespace \"%s\" does not exist",
1129 /* we do not check for USAGE rights here! */
1133 /* use the default creation namespace */
1134 recomputeNamespacePath();
1135 namespaceId = defaultCreationNamespace;
1136 if (!OidIsValid(namespaceId))
1137 elog(ERROR, "No namespace has been selected to create in");
1140 /* Note: callers will check for CREATE rights when appropriate */
1142 *objname_p = objname;
1147 * makeRangeVarFromNameList
1148 * Utility routine to convert a qualified-name list into RangeVar form.
1151 makeRangeVarFromNameList(List *names)
1153 RangeVar *rel = makeRangeVar(NULL, NULL);
1155 switch (length(names))
1158 rel->relname = strVal(lfirst(names));
1161 rel->schemaname = strVal(lfirst(names));
1162 rel->relname = strVal(lsecond(names));
1165 rel->catalogname = strVal(lfirst(names));
1166 rel->schemaname = strVal(lsecond(names));
1167 rel->relname = strVal(lfirst(lnext(lnext(names))));
1170 elog(ERROR, "Improper relation name (too many dotted names)");
1179 * Utility routine to convert a qualified-name list into a string.
1180 * Used primarily to form error messages.
1183 NameListToString(List *names)
1185 StringInfoData string;
1188 initStringInfo(&string);
1193 appendStringInfoChar(&string, '.');
1194 appendStringInfo(&string, "%s", strVal(lfirst(l)));
1201 * isTempNamespace - is the given namespace my temporary-table namespace?
1204 isTempNamespace(Oid namespaceId)
1206 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
1212 * PushSpecialNamespace - push a "special" namespace onto the front of the
1215 * This is a slightly messy hack intended only for support of CREATE SCHEMA.
1216 * Although the API is defined to allow a stack of pushed namespaces, we
1217 * presently only support one at a time.
1219 * The pushed namespace will be removed from the search path at end of
1220 * transaction, whether commit or abort.
1223 PushSpecialNamespace(Oid namespaceId)
1225 Assert(!OidIsValid(mySpecialNamespace));
1226 mySpecialNamespace = namespaceId;
1227 namespaceSearchPathValid = false;
1231 * PopSpecialNamespace - remove previously pushed special namespace.
1234 PopSpecialNamespace(Oid namespaceId)
1236 Assert(mySpecialNamespace == namespaceId);
1237 mySpecialNamespace = InvalidOid;
1238 namespaceSearchPathValid = false;
1242 * FindConversionByName - find a conversion by possibly qualified name
1244 Oid FindConversionByName(List *name)
1246 char *conversion_name;
1251 /* Convert list of names to a name and namespace */
1252 namespaceId = QualifiedNameGetCreationNamespace(name, &conversion_name);
1254 if (length(name) > 1)
1256 /* Check we have usage rights in target namespace */
1257 if (pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE) != ACLCHECK_OK)
1260 return FindConversion(conversion_name, namespaceId);
1263 recomputeNamespacePath();
1265 foreach(lptr, namespaceSearchPath)
1267 Oid namespaceId = (Oid) lfirsti(lptr);
1269 conoid = FindConversion(conversion_name, namespaceId);
1270 if (OidIsValid(conoid))
1274 /* Not found in path */
1279 * FindDefaultConversionProc - find default encoding cnnversion proc
1281 Oid FindDefaultConversionProc(int4 for_encoding, int4 to_encoding)
1286 recomputeNamespacePath();
1288 foreach(lptr, namespaceSearchPath)
1290 Oid namespaceId = (Oid) lfirsti(lptr);
1292 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
1293 if (OidIsValid(proc))
1297 /* Not found in path */
1302 * recomputeNamespacePath - recompute path derived variables if needed.
1305 recomputeNamespacePath(void)
1307 Oid userId = GetUserId();
1314 MemoryContext oldcxt;
1317 * Do nothing if path is already valid.
1319 if (namespaceSearchPathValid && namespaceUser == userId)
1322 /* Need a modifiable copy of namespace_search_path string */
1323 rawname = pstrdup(namespace_search_path);
1325 /* Parse string into list of identifiers */
1326 if (!SplitIdentifierString(rawname, ',', &namelist))
1328 /* syntax error in name list */
1329 /* this should not happen if GUC checked check_search_path */
1330 elog(ERROR, "recomputeNamespacePath: invalid list syntax");
1334 * Convert the list of names to a list of OIDs. If any names are not
1335 * recognizable or we don't have read access, just leave them out of
1336 * the list. (We can't raise an error, since the search_path setting
1337 * has already been accepted.) Don't make duplicate entries, either.
1340 foreach(l, namelist)
1342 char *curname = (char *) lfirst(l);
1345 if (strcmp(curname, "$user") == 0)
1347 /* $user --- substitute namespace matching user name, if any */
1350 tuple = SearchSysCache(SHADOWSYSID,
1351 ObjectIdGetDatum(userId),
1353 if (HeapTupleIsValid(tuple))
1357 uname = NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename);
1358 namespaceId = GetSysCacheOid(NAMESPACENAME,
1359 CStringGetDatum(uname),
1361 ReleaseSysCache(tuple);
1362 if (OidIsValid(namespaceId) &&
1363 !intMember(namespaceId, oidlist) &&
1364 pg_namespace_aclcheck(namespaceId, userId,
1365 ACL_USAGE) == ACLCHECK_OK)
1366 oidlist = lappendi(oidlist, namespaceId);
1371 /* normal namespace reference */
1372 namespaceId = GetSysCacheOid(NAMESPACENAME,
1373 CStringGetDatum(curname),
1375 if (OidIsValid(namespaceId) &&
1376 !intMember(namespaceId, oidlist) &&
1377 pg_namespace_aclcheck(namespaceId, userId,
1378 ACL_USAGE) == ACLCHECK_OK)
1379 oidlist = lappendi(oidlist, namespaceId);
1384 * Remember the first member of the explicit list.
1387 firstNS = InvalidOid;
1389 firstNS = (Oid) lfirsti(oidlist);
1392 * Add any implicitly-searched namespaces to the list. Note these
1393 * go on the front, not the back; also notice that we do not check
1394 * USAGE permissions for these.
1396 if (!intMember(PG_CATALOG_NAMESPACE, oidlist))
1397 oidlist = lconsi(PG_CATALOG_NAMESPACE, oidlist);
1399 if (OidIsValid(myTempNamespace) &&
1400 !intMember(myTempNamespace, oidlist))
1401 oidlist = lconsi(myTempNamespace, oidlist);
1403 if (OidIsValid(mySpecialNamespace) &&
1404 !intMember(mySpecialNamespace, oidlist))
1405 oidlist = lconsi(mySpecialNamespace, oidlist);
1408 * Now that we've successfully built the new list of namespace OIDs,
1409 * save it in permanent storage.
1411 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1412 newpath = listCopy(oidlist);
1413 MemoryContextSwitchTo(oldcxt);
1415 /* Now safe to assign to state variable. */
1416 freeList(namespaceSearchPath);
1417 namespaceSearchPath = newpath;
1420 * Update info derived from search path.
1422 firstExplicitNamespace = firstNS;
1423 if (OidIsValid(mySpecialNamespace))
1424 defaultCreationNamespace = mySpecialNamespace;
1426 defaultCreationNamespace = firstNS;
1428 /* Mark the path valid. */
1429 namespaceSearchPathValid = true;
1430 namespaceUser = userId;
1439 * InitTempTableNamespace
1440 * Initialize temp table namespace on first use in a particular backend
1443 InitTempTableNamespace(void)
1445 char namespaceName[NAMEDATALEN];
1449 * First, do permission check to see if we are authorized to make
1450 * temp tables. We use a nonstandard error message here since
1451 * "databasename: permission denied" might be a tad cryptic.
1453 * Note we apply the check to the session user, not the currently
1454 * active userid, since we are not going to change our minds about
1455 * temp table availability during the session.
1457 if (pg_database_aclcheck(MyDatabaseId, GetSessionUserId(),
1458 ACL_CREATE_TEMP) != ACLCHECK_OK)
1459 elog(ERROR, "%s: not authorized to create temp tables",
1462 snprintf(namespaceName, NAMEDATALEN, "pg_temp_%d", MyBackendId);
1464 namespaceId = GetSysCacheOid(NAMESPACENAME,
1465 CStringGetDatum(namespaceName),
1467 if (!OidIsValid(namespaceId))
1470 * First use of this temp namespace in this database; create it.
1471 * The temp namespaces are always owned by the superuser. We
1472 * leave their permissions at default --- i.e., no access except to
1473 * superuser --- to ensure that unprivileged users can't peek
1474 * at other backends' temp tables. This works because the places
1475 * that access the temp namespace for my own backend skip permissions
1478 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_USESYSID);
1479 /* Advance command counter to make namespace visible */
1480 CommandCounterIncrement();
1485 * If the namespace already exists, clean it out (in case the
1486 * former owner crashed without doing so).
1488 RemoveTempRelations(namespaceId);
1492 * Okay, we've prepared the temp namespace ... but it's not committed
1493 * yet, so all our work could be undone by transaction rollback. Set
1494 * flag for AtEOXact_Namespace to know what to do.
1496 myTempNamespace = namespaceId;
1498 firstTempTransaction = true;
1500 namespaceSearchPathValid = false; /* need to rebuild list */
1504 * End-of-transaction cleanup for namespaces.
1507 AtEOXact_Namespace(bool isCommit)
1510 * If we abort the transaction in which a temp namespace was selected,
1511 * we'll have to do any creation or cleanout work over again. So,
1512 * just forget the namespace entirely until next time. On the other
1513 * hand, if we commit then register an exit callback to clean out the
1514 * temp tables at backend shutdown. (We only want to register the
1515 * callback once per session, so this is a good place to do it.)
1517 if (firstTempTransaction)
1520 on_shmem_exit(RemoveTempRelationsCallback, 0);
1523 myTempNamespace = InvalidOid;
1524 namespaceSearchPathValid = false; /* need to rebuild list */
1526 firstTempTransaction = false;
1529 * Clean up if someone failed to do PopSpecialNamespace
1531 if (OidIsValid(mySpecialNamespace))
1533 mySpecialNamespace = InvalidOid;
1534 namespaceSearchPathValid = false; /* need to rebuild list */
1539 * Remove all relations in the specified temp namespace.
1541 * This is called at backend shutdown (if we made any temp relations).
1542 * It is also called when we begin using a pre-existing temp namespace,
1543 * in order to clean out any relations that might have been created by
1544 * a crashed backend.
1547 RemoveTempRelations(Oid tempNamespaceId)
1553 ObjectAddress object;
1556 * Scan pg_class to find all the relations in the target namespace.
1557 * Ignore indexes, though, on the assumption that they'll go away
1558 * when their tables are deleted.
1560 * NOTE: if there are deletion constraints between temp relations,
1561 * then our CASCADE delete call may cause as-yet-unvisited objects
1562 * to go away. This is okay because we are using SnapshotNow; when
1563 * the scan does reach those pg_class tuples, they'll be ignored as
1566 ScanKeyEntryInitialize(&key, 0x0,
1567 Anum_pg_class_relnamespace,
1569 ObjectIdGetDatum(tempNamespaceId));
1571 pgclass = heap_openr(RelationRelationName, AccessShareLock);
1572 scan = heap_beginscan(pgclass, SnapshotNow, 1, &key);
1574 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1576 switch (((Form_pg_class) GETSTRUCT(tuple))->relkind)
1578 case RELKIND_RELATION:
1579 case RELKIND_SEQUENCE:
1581 AssertTupleDescHasOid(pgclass->rd_att);
1582 object.classId = RelOid_pg_class;
1583 object.objectId = HeapTupleGetOid(tuple);
1584 object.objectSubId = 0;
1585 performDeletion(&object, DROP_CASCADE);
1593 heap_close(pgclass, AccessShareLock);
1597 * Callback to remove temp relations at backend exit.
1600 RemoveTempRelationsCallback(void)
1602 if (OidIsValid(myTempNamespace)) /* should always be true */
1604 /* Need to ensure we have a usable transaction. */
1605 AbortOutOfAnyTransaction();
1606 StartTransactionCommand();
1608 RemoveTempRelations(myTempNamespace);
1610 CommitTransactionCommand();
1616 * Routines for handling the GUC variable 'search_path'.
1619 /* assign_hook: validate new search_path, do extra actions as needed */
1621 assign_search_path(const char *newval, bool doit, bool interactive)
1627 /* Need a modifiable copy of string */
1628 rawname = pstrdup(newval);
1630 /* Parse string into list of identifiers */
1631 if (!SplitIdentifierString(rawname, ',', &namelist))
1633 /* syntax error in name list */
1640 * If we aren't inside a transaction, we cannot do database access so
1641 * cannot verify the individual names. Must accept the list on faith.
1643 if (interactive && IsTransactionState())
1646 * Verify that all the names are either valid namespace names or
1647 * "$user". We do not require $user to correspond to a valid
1648 * namespace. We do not check for USAGE rights, either; should we?
1650 foreach(l, namelist)
1652 char *curname = (char *) lfirst(l);
1654 if (strcmp(curname, "$user") == 0)
1656 if (!SearchSysCacheExists(NAMESPACENAME,
1657 CStringGetDatum(curname),
1659 elog(ERROR, "Namespace \"%s\" does not exist", curname);
1667 * We mark the path as needing recomputation, but don't do anything until
1668 * it's needed. This avoids trying to do database access during GUC
1672 namespaceSearchPathValid = false;
1678 * InitializeSearchPath: initialize module during InitPostgres.
1680 * This is called after we are up enough to be able to do catalog lookups.
1683 InitializeSearchPath(void)
1685 if (IsBootstrapProcessingMode())
1688 * In bootstrap mode, the search path must be 'pg_catalog' so that
1689 * tables are created in the proper namespace; ignore the GUC setting.
1691 MemoryContext oldcxt;
1693 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1694 namespaceSearchPath = makeListi1(PG_CATALOG_NAMESPACE);
1695 MemoryContextSwitchTo(oldcxt);
1696 defaultCreationNamespace = PG_CATALOG_NAMESPACE;
1697 firstExplicitNamespace = PG_CATALOG_NAMESPACE;
1698 namespaceSearchPathValid = true;
1699 namespaceUser = GetUserId();
1704 * In normal mode, arrange for a callback on any syscache invalidation
1705 * of pg_namespace rows.
1707 CacheRegisterSyscacheCallback(NAMESPACEOID,
1710 /* Force search path to be recomputed on next use */
1711 namespaceSearchPathValid = false;
1717 * Syscache inval callback function
1720 NamespaceCallback(Datum arg, Oid relid)
1722 /* Force search path to be recomputed on next use */
1723 namespaceSearchPathValid = false;
1727 * Fetch the active search path, expressed as a List of OIDs.
1729 * The returned list includes the implicitly-prepended namespaces only if
1730 * includeImplicit is true.
1732 * NB: caller must treat the list as read-only!
1735 fetch_search_path(bool includeImplicit)
1739 recomputeNamespacePath();
1741 result = namespaceSearchPath;
1742 if (!includeImplicit)
1744 while (result && (Oid) lfirsti(result) != firstExplicitNamespace)
1745 result = lnext(result);