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-2004, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.69 2004/08/29 04:12:28 momjian Exp $
18 *-------------------------------------------------------------------------
22 #include "access/xact.h"
23 #include "catalog/catname.h"
24 #include "catalog/dependency.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_conversion.h"
27 #include "catalog/pg_namespace.h"
28 #include "catalog/pg_opclass.h"
29 #include "catalog/pg_operator.h"
30 #include "catalog/pg_proc.h"
31 #include "catalog/pg_shadow.h"
32 #include "catalog/pg_type.h"
33 #include "commands/dbcommands.h"
34 #include "lib/stringinfo.h"
35 #include "miscadmin.h"
36 #include "nodes/makefuncs.h"
37 #include "storage/backendid.h"
38 #include "storage/ipc.h"
39 #include "utils/acl.h"
40 #include "utils/builtins.h"
41 #include "utils/catcache.h"
42 #include "utils/guc.h"
43 #include "utils/inval.h"
44 #include "utils/lsyscache.h"
45 #include "utils/memutils.h"
46 #include "utils/syscache.h"
50 * The namespace search path is a possibly-empty list of namespace OIDs.
51 * In addition to the explicit list, several implicitly-searched namespaces
54 * 1. If a "special" namespace has been set by PushSpecialNamespace, it is
55 * always searched first. (This is a hack for CREATE SCHEMA.)
57 * 2. If a TEMP table namespace has been initialized in this session, it
58 * is always searched just after any special namespace.
60 * 3. The system catalog namespace is always searched. If the system
61 * namespace is present in the explicit path then it will be searched in
62 * the specified order; otherwise it will be searched after TEMP tables and
63 * *before* the explicit list. (It might seem that the system namespace
64 * should be implicitly last, but this behavior appears to be required by
65 * SQL99. Also, this provides a way to search the system namespace first
66 * without thereby making it the default creation target namespace.)
68 * The default creation target namespace is normally equal to the first
69 * element of the explicit list, but is the "special" namespace when one
70 * has been set. If the explicit list is empty and there is no special
71 * namespace, there is no default target.
73 * In bootstrap mode, the search path is set equal to 'pg_catalog', so that
74 * the system namespace is the only one searched or inserted into.
75 * The initdb script is also careful to set search_path to 'pg_catalog' for
76 * its post-bootstrap standalone backend runs. Otherwise the default search
77 * path is determined by GUC. The factory default path contains the PUBLIC
78 * namespace (if it exists), preceded by the user's personal namespace
81 * If namespaceSearchPathValid is false, then namespaceSearchPath (and other
82 * derived variables) need to be recomputed from namespace_search_path.
83 * We mark it invalid upon an assignment to namespace_search_path or receipt
84 * of a syscache invalidation event for pg_namespace. The recomputation
85 * is done during the next lookup attempt.
87 * Any namespaces mentioned in namespace_search_path that are not readable
88 * by the current user ID are simply left out of namespaceSearchPath; so
89 * we have to be willing to recompute the path when current userid changes.
90 * namespaceUser is the userid the path has been computed for.
93 static List *namespaceSearchPath = NIL;
95 static Oid namespaceUser = InvalidOid;
97 /* default place to create stuff; if InvalidOid, no default */
98 static Oid defaultCreationNamespace = InvalidOid;
100 /* first explicit member of list; usually same as defaultCreationNamespace */
101 static Oid firstExplicitNamespace = InvalidOid;
103 /* The above four values are valid only if namespaceSearchPathValid */
104 static bool namespaceSearchPathValid = true;
107 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
108 * in a particular backend session (this happens when a CREATE TEMP TABLE
109 * command is first executed). Thereafter it's the OID of the temp namespace.
111 * myTempNamespaceXID shows whether we've created the TEMP namespace in the
112 * current transaction. The TransactionId propagates up the transaction tree,
113 * so the main transaction will correctly recognize the flag if all
114 * intermediate subtransactions commit. When it is InvalidTransactionId,
115 * we either haven't made the TEMP namespace yet, or have successfully
116 * committed its creation, depending on whether myTempNamespace is valid.
118 static Oid myTempNamespace = InvalidOid;
120 static TransactionId myTempNamespaceXID = InvalidTransactionId;
123 * "Special" namespace for CREATE SCHEMA. If set, it's the first search
124 * path element, and also the default creation namespace.
126 static Oid mySpecialNamespace = InvalidOid;
129 * This is the text equivalent of the search path --- it's the value
130 * of the GUC variable 'search_path'.
132 char *namespace_search_path = NULL;
135 /* Local functions */
136 static void recomputeNamespacePath(void);
137 static void InitTempTableNamespace(void);
138 static void RemoveTempRelations(Oid tempNamespaceId);
139 static void RemoveTempRelationsCallback(int code, Datum arg);
140 static void NamespaceCallback(Datum arg, Oid relid);
142 /* These don't really need to appear in any header file */
143 Datum pg_table_is_visible(PG_FUNCTION_ARGS);
144 Datum pg_type_is_visible(PG_FUNCTION_ARGS);
145 Datum pg_function_is_visible(PG_FUNCTION_ARGS);
146 Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
147 Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
148 Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
153 * Given a RangeVar describing an existing relation,
154 * select the proper namespace and look up the relation OID.
156 * If the relation is not found, return InvalidOid if failOK = true,
157 * otherwise raise an error.
160 RangeVarGetRelid(const RangeVar *relation, bool failOK)
166 * We check the catalog name and then ignore it.
168 if (relation->catalogname)
170 if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
172 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
173 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
174 relation->catalogname, relation->schemaname,
175 relation->relname)));
178 if (relation->schemaname)
180 /* use exact schema given */
181 namespaceId = LookupExplicitNamespace(relation->schemaname);
182 relId = get_relname_relid(relation->relname, namespaceId);
186 /* search the namespace path */
187 relId = RelnameGetRelid(relation->relname);
190 if (!OidIsValid(relId) && !failOK)
192 if (relation->schemaname)
194 (errcode(ERRCODE_UNDEFINED_TABLE),
195 errmsg("relation \"%s.%s\" does not exist",
196 relation->schemaname, relation->relname)));
199 (errcode(ERRCODE_UNDEFINED_TABLE),
200 errmsg("relation \"%s\" does not exist",
201 relation->relname)));
207 * RangeVarGetCreationNamespace
208 * Given a RangeVar describing a to-be-created relation,
209 * choose which namespace to create it in.
211 * Note: calling this may result in a CommandCounterIncrement operation.
212 * That will happen on the first request for a temp table in any particular
213 * backend run; we will need to either create or clean out the temp schema.
216 RangeVarGetCreationNamespace(const RangeVar *newRelation)
221 * We check the catalog name and then ignore it.
223 if (newRelation->catalogname)
225 if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
227 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
228 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
229 newRelation->catalogname, newRelation->schemaname,
230 newRelation->relname)));
233 if (newRelation->istemp)
235 /* TEMP tables are created in our backend-local temp namespace */
236 if (newRelation->schemaname)
238 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
239 errmsg("temporary tables may not specify a schema name")));
240 /* Initialize temp namespace if first time through */
241 if (!OidIsValid(myTempNamespace))
242 InitTempTableNamespace();
243 return myTempNamespace;
246 if (newRelation->schemaname)
248 /* use exact schema given */
249 namespaceId = GetSysCacheOid(NAMESPACENAME,
250 CStringGetDatum(newRelation->schemaname),
252 if (!OidIsValid(namespaceId))
254 (errcode(ERRCODE_UNDEFINED_SCHEMA),
255 errmsg("schema \"%s\" does not exist",
256 newRelation->schemaname)));
257 /* we do not check for USAGE rights here! */
261 /* use the default creation namespace */
262 recomputeNamespacePath();
263 namespaceId = defaultCreationNamespace;
264 if (!OidIsValid(namespaceId))
266 (errcode(ERRCODE_UNDEFINED_SCHEMA),
267 errmsg("no schema has been selected to create in")));
270 /* Note: callers will check for CREATE rights when appropriate */
277 * Try to resolve an unqualified relation name.
278 * Returns OID if relation found in search path, else InvalidOid.
281 RelnameGetRelid(const char *relname)
286 recomputeNamespacePath();
288 foreach(l, namespaceSearchPath)
290 Oid namespaceId = lfirst_oid(l);
292 relid = get_relname_relid(relname, namespaceId);
293 if (OidIsValid(relid))
297 /* Not found in path */
304 * Determine whether a relation (identified by OID) is visible in the
305 * current search path. Visible means "would be found by searching
306 * for the unqualified relation name".
309 RelationIsVisible(Oid relid)
312 Form_pg_class relform;
316 reltup = SearchSysCache(RELOID,
317 ObjectIdGetDatum(relid),
319 if (!HeapTupleIsValid(reltup))
320 elog(ERROR, "cache lookup failed for relation %u", relid);
321 relform = (Form_pg_class) GETSTRUCT(reltup);
323 recomputeNamespacePath();
326 * Quick check: if it ain't in the path at all, it ain't visible.
327 * Items in the system namespace are surely in the path and so we
328 * needn't even do list_member_oid() for them.
330 relnamespace = relform->relnamespace;
331 if (relnamespace != PG_CATALOG_NAMESPACE &&
332 !list_member_oid(namespaceSearchPath, relnamespace))
337 * If it is in the path, it might still not be visible; it could
338 * be hidden by another relation of the same name earlier in the
339 * path. So we must do a slow check to see if this rel would be
340 * found by RelnameGetRelid.
342 char *relname = NameStr(relform->relname);
344 visible = (RelnameGetRelid(relname) == relid);
347 ReleaseSysCache(reltup);
355 * Try to resolve an unqualified datatype name.
356 * Returns OID if type found in search path, else InvalidOid.
358 * This is essentially the same as RelnameGetRelid.
361 TypenameGetTypid(const char *typname)
366 recomputeNamespacePath();
368 foreach(l, namespaceSearchPath)
370 Oid namespaceId = lfirst_oid(l);
372 typid = GetSysCacheOid(TYPENAMENSP,
373 PointerGetDatum(typname),
374 ObjectIdGetDatum(namespaceId),
376 if (OidIsValid(typid))
380 /* Not found in path */
386 * Determine whether a type (identified by OID) is visible in the
387 * current search path. Visible means "would be found by searching
388 * for the unqualified type name".
391 TypeIsVisible(Oid typid)
394 Form_pg_type typform;
398 typtup = SearchSysCache(TYPEOID,
399 ObjectIdGetDatum(typid),
401 if (!HeapTupleIsValid(typtup))
402 elog(ERROR, "cache lookup failed for type %u", typid);
403 typform = (Form_pg_type) GETSTRUCT(typtup);
405 recomputeNamespacePath();
408 * Quick check: if it ain't in the path at all, it ain't visible.
409 * Items in the system namespace are surely in the path and so we
410 * needn't even do list_member_oid() for them.
412 typnamespace = typform->typnamespace;
413 if (typnamespace != PG_CATALOG_NAMESPACE &&
414 !list_member_oid(namespaceSearchPath, typnamespace))
419 * If it is in the path, it might still not be visible; it could
420 * be hidden by another type of the same name earlier in the path.
421 * So we must do a slow check to see if this type would be found
422 * by TypenameGetTypid.
424 char *typname = NameStr(typform->typname);
426 visible = (TypenameGetTypid(typname) == typid);
429 ReleaseSysCache(typtup);
436 * FuncnameGetCandidates
437 * Given a possibly-qualified function name and argument count,
438 * retrieve a list of the possible matches.
440 * If nargs is -1, we return all functions matching the given name,
441 * regardless of argument count.
443 * We search a single namespace if the function name is qualified, else
444 * all namespaces in the search path. The return list will never contain
445 * multiple entries with identical argument lists --- in the multiple-
446 * namespace case, we arrange for entries in earlier namespaces to mask
447 * identical entries in later namespaces.
450 FuncnameGetCandidates(List *names, int nargs)
452 FuncCandidateList resultList = NULL;
459 /* deconstruct the name list */
460 DeconstructQualifiedName(names, &schemaname, &funcname);
464 /* use exact schema given */
465 namespaceId = LookupExplicitNamespace(schemaname);
469 /* flag to indicate we need namespace search */
470 namespaceId = InvalidOid;
471 recomputeNamespacePath();
474 /* Search syscache by name and (optionally) nargs only */
476 catlist = SearchSysCacheList(PROCNAMENSP, 2,
477 CStringGetDatum(funcname),
478 Int16GetDatum(nargs),
481 catlist = SearchSysCacheList(PROCNAMENSP, 1,
482 CStringGetDatum(funcname),
485 for (i = 0; i < catlist->n_members; i++)
487 HeapTuple proctup = &catlist->members[i]->tuple;
488 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
490 FuncCandidateList newResult;
492 nargs = procform->pronargs;
494 if (OidIsValid(namespaceId))
496 /* Consider only procs in specified namespace */
497 if (procform->pronamespace != namespaceId)
499 /* No need to check args, they must all be different */
503 /* Consider only procs that are in the search path */
506 foreach(nsp, namespaceSearchPath)
508 if (procform->pronamespace == lfirst_oid(nsp))
513 continue; /* proc is not in search path */
516 * Okay, it's in the search path, but does it have the same
517 * arguments as something we already accepted? If so, keep
518 * only the one that appears earlier in the search path.
520 * If we have an ordered list from SearchSysCacheList (the normal
521 * case), then any conflicting proc must immediately adjoin
522 * this one in the list, so we only need to look at the newest
523 * result item. If we have an unordered list, we have to scan
524 * the whole result list.
528 FuncCandidateList prevResult;
530 if (catlist->ordered)
532 if (nargs == resultList->nargs &&
533 memcmp(procform->proargtypes, resultList->args,
534 nargs * sizeof(Oid)) == 0)
535 prevResult = resultList;
541 for (prevResult = resultList;
543 prevResult = prevResult->next)
545 if (nargs == prevResult->nargs &&
546 memcmp(procform->proargtypes, prevResult->args,
547 nargs * sizeof(Oid)) == 0)
553 /* We have a match with a previous result */
554 Assert(pathpos != prevResult->pathpos);
555 if (pathpos > prevResult->pathpos)
556 continue; /* keep previous result */
557 /* replace previous result */
558 prevResult->pathpos = pathpos;
559 prevResult->oid = HeapTupleGetOid(proctup);
560 continue; /* args are same, of course */
566 * Okay to add it to result list
568 newResult = (FuncCandidateList)
569 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
570 + nargs * sizeof(Oid));
571 newResult->pathpos = pathpos;
572 newResult->oid = HeapTupleGetOid(proctup);
573 newResult->nargs = nargs;
574 memcpy(newResult->args, procform->proargtypes, nargs * sizeof(Oid));
576 newResult->next = resultList;
577 resultList = newResult;
580 ReleaseSysCacheList(catlist);
587 * Determine whether a function (identified by OID) is visible in the
588 * current search path. Visible means "would be found by searching
589 * for the unqualified function name with exact argument matches".
592 FunctionIsVisible(Oid funcid)
595 Form_pg_proc procform;
599 proctup = SearchSysCache(PROCOID,
600 ObjectIdGetDatum(funcid),
602 if (!HeapTupleIsValid(proctup))
603 elog(ERROR, "cache lookup failed for function %u", funcid);
604 procform = (Form_pg_proc) GETSTRUCT(proctup);
606 recomputeNamespacePath();
609 * Quick check: if it ain't in the path at all, it ain't visible.
610 * Items in the system namespace are surely in the path and so we
611 * needn't even do list_member_oid() for them.
613 pronamespace = procform->pronamespace;
614 if (pronamespace != PG_CATALOG_NAMESPACE &&
615 !list_member_oid(namespaceSearchPath, pronamespace))
620 * If it is in the path, it might still not be visible; it could
621 * be hidden by another proc of the same name and arguments
622 * earlier in the path. So we must do a slow check to see if this
623 * is the same proc that would be found by FuncnameGetCandidates.
625 char *proname = NameStr(procform->proname);
626 int nargs = procform->pronargs;
627 FuncCandidateList clist;
631 clist = FuncnameGetCandidates(list_make1(makeString(proname)), nargs);
633 for (; clist; clist = clist->next)
635 if (memcmp(clist->args, procform->proargtypes,
636 nargs * sizeof(Oid)) == 0)
638 /* Found the expected entry; is it the right proc? */
639 visible = (clist->oid == funcid);
645 ReleaseSysCache(proctup);
652 * OpernameGetCandidates
653 * Given a possibly-qualified operator name and operator kind,
654 * retrieve a list of the possible matches.
656 * If oprkind is '\0', we return all operators matching the given name,
657 * regardless of arguments.
659 * We search a single namespace if the operator name is qualified, else
660 * all namespaces in the search path. The return list will never contain
661 * multiple entries with identical argument lists --- in the multiple-
662 * namespace case, we arrange for entries in earlier namespaces to mask
663 * identical entries in later namespaces.
665 * The returned items always have two args[] entries --- one or the other
666 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
669 OpernameGetCandidates(List *names, char oprkind)
671 FuncCandidateList resultList = NULL;
672 char *resultSpace = NULL;
680 /* deconstruct the name list */
681 DeconstructQualifiedName(names, &schemaname, &opername);
685 /* use exact schema given */
686 namespaceId = LookupExplicitNamespace(schemaname);
690 /* flag to indicate we need namespace search */
691 namespaceId = InvalidOid;
692 recomputeNamespacePath();
695 /* Search syscache by name only */
696 catlist = SearchSysCacheList(OPERNAMENSP, 1,
697 CStringGetDatum(opername),
701 * In typical scenarios, most if not all of the operators found by the
702 * catcache search will end up getting returned; and there can be quite
703 * a few, for common operator names such as '=' or '+'. To reduce the
704 * time spent in palloc, we allocate the result space as an array large
705 * enough to hold all the operators. The original coding of this routine
706 * did a separate palloc for each operator, but profiling revealed that
707 * the pallocs used an unreasonably large fraction of parsing time.
709 #define SPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
711 if (catlist->n_members > 0)
712 resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
714 for (i = 0; i < catlist->n_members; i++)
716 HeapTuple opertup = &catlist->members[i]->tuple;
717 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
719 FuncCandidateList newResult;
721 /* Ignore operators of wrong kind, if specific kind requested */
722 if (oprkind && operform->oprkind != oprkind)
725 if (OidIsValid(namespaceId))
727 /* Consider only opers in specified namespace */
728 if (operform->oprnamespace != namespaceId)
730 /* No need to check args, they must all be different */
734 /* Consider only opers that are in the search path */
737 foreach(nsp, namespaceSearchPath)
739 if (operform->oprnamespace == lfirst_oid(nsp))
744 continue; /* oper is not in search path */
747 * Okay, it's in the search path, but does it have the same
748 * arguments as something we already accepted? If so, keep
749 * only the one that appears earlier in the search path.
751 * If we have an ordered list from SearchSysCacheList (the normal
752 * case), then any conflicting oper must immediately adjoin
753 * this one in the list, so we only need to look at the newest
754 * result item. If we have an unordered list, we have to scan
755 * the whole result list.
759 FuncCandidateList prevResult;
761 if (catlist->ordered)
763 if (operform->oprleft == resultList->args[0] &&
764 operform->oprright == resultList->args[1])
765 prevResult = resultList;
771 for (prevResult = resultList;
773 prevResult = prevResult->next)
775 if (operform->oprleft == prevResult->args[0] &&
776 operform->oprright == prevResult->args[1])
782 /* We have a match with a previous result */
783 Assert(pathpos != prevResult->pathpos);
784 if (pathpos > prevResult->pathpos)
785 continue; /* keep previous result */
786 /* replace previous result */
787 prevResult->pathpos = pathpos;
788 prevResult->oid = HeapTupleGetOid(opertup);
789 continue; /* args are same, of course */
795 * Okay to add it to result list
797 newResult = (FuncCandidateList) (resultSpace + nextResult);
798 nextResult += SPACE_PER_OP;
800 newResult->pathpos = pathpos;
801 newResult->oid = HeapTupleGetOid(opertup);
802 newResult->nargs = 2;
803 newResult->args[0] = operform->oprleft;
804 newResult->args[1] = operform->oprright;
805 newResult->next = resultList;
806 resultList = newResult;
809 ReleaseSysCacheList(catlist);
816 * Determine whether an operator (identified by OID) is visible in the
817 * current search path. Visible means "would be found by searching
818 * for the unqualified operator name with exact argument matches".
821 OperatorIsVisible(Oid oprid)
824 Form_pg_operator oprform;
828 oprtup = SearchSysCache(OPEROID,
829 ObjectIdGetDatum(oprid),
831 if (!HeapTupleIsValid(oprtup))
832 elog(ERROR, "cache lookup failed for operator %u", oprid);
833 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
835 recomputeNamespacePath();
838 * Quick check: if it ain't in the path at all, it ain't visible.
839 * Items in the system namespace are surely in the path and so we
840 * needn't even do list_member_oid() for them.
842 oprnamespace = oprform->oprnamespace;
843 if (oprnamespace != PG_CATALOG_NAMESPACE &&
844 !list_member_oid(namespaceSearchPath, oprnamespace))
849 * If it is in the path, it might still not be visible; it could
850 * be hidden by another operator of the same name and arguments
851 * earlier in the path. So we must do a slow check to see if this
852 * is the same operator that would be found by
853 * OpernameGetCandidates.
855 char *oprname = NameStr(oprform->oprname);
856 FuncCandidateList clist;
860 clist = OpernameGetCandidates(list_make1(makeString(oprname)),
863 for (; clist; clist = clist->next)
865 if (clist->args[0] == oprform->oprleft &&
866 clist->args[1] == oprform->oprright)
868 /* Found the expected entry; is it the right op? */
869 visible = (clist->oid == oprid);
875 ReleaseSysCache(oprtup);
882 * OpclassGetCandidates
883 * Given an index access method OID, retrieve a list of all the
884 * opclasses for that AM that are visible in the search path.
886 * NOTE: the opcname_tmp field in the returned structs should not be used
887 * by callers, because it points at syscache entries that we release at
888 * the end of this routine. If any callers needed the name information,
889 * we could pstrdup() the names ... but at present it'd be wasteful.
892 OpclassGetCandidates(Oid amid)
894 OpclassCandidateList resultList = NULL;
898 /* Search syscache by AM OID only */
899 catlist = SearchSysCacheList(CLAAMNAMENSP, 1,
900 ObjectIdGetDatum(amid),
903 recomputeNamespacePath();
905 for (i = 0; i < catlist->n_members; i++)
907 HeapTuple opctup = &catlist->members[i]->tuple;
908 Form_pg_opclass opcform = (Form_pg_opclass) GETSTRUCT(opctup);
910 OpclassCandidateList newResult;
913 /* Consider only opclasses that are in the search path */
914 foreach(nsp, namespaceSearchPath)
916 if (opcform->opcnamespace == lfirst_oid(nsp))
921 continue; /* opclass is not in search path */
924 * Okay, it's in the search path, but does it have the same name
925 * as something we already accepted? If so, keep only the one
926 * that appears earlier in the search path.
928 * If we have an ordered list from SearchSysCacheList (the normal
929 * case), then any conflicting opclass must immediately adjoin
930 * this one in the list, so we only need to look at the newest
931 * result item. If we have an unordered list, we have to scan the
936 OpclassCandidateList prevResult;
938 if (catlist->ordered)
940 if (strcmp(NameStr(opcform->opcname),
941 resultList->opcname_tmp) == 0)
942 prevResult = resultList;
948 for (prevResult = resultList;
950 prevResult = prevResult->next)
952 if (strcmp(NameStr(opcform->opcname),
953 prevResult->opcname_tmp) == 0)
959 /* We have a match with a previous result */
960 Assert(pathpos != prevResult->pathpos);
961 if (pathpos > prevResult->pathpos)
962 continue; /* keep previous result */
963 /* replace previous result */
964 prevResult->opcname_tmp = NameStr(opcform->opcname);
965 prevResult->pathpos = pathpos;
966 prevResult->oid = HeapTupleGetOid(opctup);
967 prevResult->opcintype = opcform->opcintype;
968 prevResult->opcdefault = opcform->opcdefault;
969 prevResult->opckeytype = opcform->opckeytype;
975 * Okay to add it to result list
977 newResult = (OpclassCandidateList)
978 palloc(sizeof(struct _OpclassCandidateList));
979 newResult->opcname_tmp = NameStr(opcform->opcname);
980 newResult->pathpos = pathpos;
981 newResult->oid = HeapTupleGetOid(opctup);
982 newResult->opcintype = opcform->opcintype;
983 newResult->opcdefault = opcform->opcdefault;
984 newResult->opckeytype = opcform->opckeytype;
985 newResult->next = resultList;
986 resultList = newResult;
989 ReleaseSysCacheList(catlist);
995 * OpclassnameGetOpcid
996 * Try to resolve an unqualified index opclass name.
997 * Returns OID if opclass found in search path, else InvalidOid.
999 * This is essentially the same as TypenameGetTypid, but we have to have
1000 * an extra argument for the index AM OID.
1003 OpclassnameGetOpcid(Oid amid, const char *opcname)
1008 recomputeNamespacePath();
1010 foreach(l, namespaceSearchPath)
1012 Oid namespaceId = lfirst_oid(l);
1014 opcid = GetSysCacheOid(CLAAMNAMENSP,
1015 ObjectIdGetDatum(amid),
1016 PointerGetDatum(opcname),
1017 ObjectIdGetDatum(namespaceId),
1019 if (OidIsValid(opcid))
1023 /* Not found in path */
1029 * Determine whether an opclass (identified by OID) is visible in the
1030 * current search path. Visible means "would be found by searching
1031 * for the unqualified opclass name".
1034 OpclassIsVisible(Oid opcid)
1037 Form_pg_opclass opcform;
1041 opctup = SearchSysCache(CLAOID,
1042 ObjectIdGetDatum(opcid),
1044 if (!HeapTupleIsValid(opctup))
1045 elog(ERROR, "cache lookup failed for opclass %u", opcid);
1046 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1048 recomputeNamespacePath();
1051 * Quick check: if it ain't in the path at all, it ain't visible.
1052 * Items in the system namespace are surely in the path and so we
1053 * needn't even do list_member_oid() for them.
1055 opcnamespace = opcform->opcnamespace;
1056 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1057 !list_member_oid(namespaceSearchPath, opcnamespace))
1062 * If it is in the path, it might still not be visible; it could
1063 * be hidden by another opclass of the same name earlier in the
1064 * path. So we must do a slow check to see if this opclass would
1065 * be found by OpclassnameGetOpcid.
1067 char *opcname = NameStr(opcform->opcname);
1069 visible = (OpclassnameGetOpcid(opcform->opcamid, opcname) == opcid);
1072 ReleaseSysCache(opctup);
1078 * ConversionGetConid
1079 * Try to resolve an unqualified conversion name.
1080 * Returns OID if conversion found in search path, else InvalidOid.
1082 * This is essentially the same as RelnameGetRelid.
1085 ConversionGetConid(const char *conname)
1090 recomputeNamespacePath();
1092 foreach(l, namespaceSearchPath)
1094 Oid namespaceId = lfirst_oid(l);
1096 conid = GetSysCacheOid(CONNAMENSP,
1097 PointerGetDatum(conname),
1098 ObjectIdGetDatum(namespaceId),
1100 if (OidIsValid(conid))
1104 /* Not found in path */
1109 * ConversionIsVisible
1110 * Determine whether a conversion (identified by OID) is visible in the
1111 * current search path. Visible means "would be found by searching
1112 * for the unqualified conversion name".
1115 ConversionIsVisible(Oid conid)
1118 Form_pg_conversion conform;
1122 contup = SearchSysCache(CONOID,
1123 ObjectIdGetDatum(conid),
1125 if (!HeapTupleIsValid(contup))
1126 elog(ERROR, "cache lookup failed for conversion %u", conid);
1127 conform = (Form_pg_conversion) GETSTRUCT(contup);
1129 recomputeNamespacePath();
1132 * Quick check: if it ain't in the path at all, it ain't visible.
1133 * Items in the system namespace are surely in the path and so we
1134 * needn't even do list_member_oid() for them.
1136 connamespace = conform->connamespace;
1137 if (connamespace != PG_CATALOG_NAMESPACE &&
1138 !list_member_oid(namespaceSearchPath, connamespace))
1143 * If it is in the path, it might still not be visible; it could
1144 * be hidden by another conversion of the same name earlier in the
1145 * path. So we must do a slow check to see if this conversion
1146 * would be found by ConversionGetConid.
1148 char *conname = NameStr(conform->conname);
1150 visible = (ConversionGetConid(conname) == conid);
1153 ReleaseSysCache(contup);
1159 * DeconstructQualifiedName
1160 * Given a possibly-qualified name expressed as a list of String nodes,
1161 * extract the schema name and object name.
1163 * *nspname_p is set to NULL if there is no explicit schema name.
1166 DeconstructQualifiedName(List *names,
1171 char *schemaname = NULL;
1172 char *objname = NULL;
1174 switch (list_length(names))
1177 objname = strVal(linitial(names));
1180 schemaname = strVal(linitial(names));
1181 objname = strVal(lsecond(names));
1184 catalogname = strVal(linitial(names));
1185 schemaname = strVal(lsecond(names));
1186 objname = strVal(lthird(names));
1189 * We check the catalog name and then ignore it.
1191 if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
1193 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1194 errmsg("cross-database references are not implemented: %s",
1195 NameListToString(names))));
1199 (errcode(ERRCODE_SYNTAX_ERROR),
1200 errmsg("improper qualified name (too many dotted names): %s",
1201 NameListToString(names))));
1205 *nspname_p = schemaname;
1206 *objname_p = objname;
1210 * LookupExplicitNamespace
1211 * Process an explicitly-specified schema name: look up the schema
1212 * and verify we have USAGE (lookup) rights in it.
1214 * Returns the namespace OID. Raises ereport if any problem.
1217 LookupExplicitNamespace(const char *nspname)
1220 AclResult aclresult;
1222 namespaceId = GetSysCacheOid(NAMESPACENAME,
1223 CStringGetDatum(nspname),
1225 if (!OidIsValid(namespaceId))
1227 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1228 errmsg("schema \"%s\" does not exist", nspname)));
1230 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
1231 if (aclresult != ACLCHECK_OK)
1232 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
1239 * QualifiedNameGetCreationNamespace
1240 * Given a possibly-qualified name for an object (in List-of-Values
1241 * format), determine what namespace the object should be created in.
1242 * Also extract and return the object name (last component of list).
1244 * This is *not* used for tables. Hence, the TEMP table namespace is
1245 * never selected as the creation target.
1248 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
1254 /* deconstruct the name list */
1255 DeconstructQualifiedName(names, &schemaname, &objname);
1259 /* use exact schema given */
1260 namespaceId = GetSysCacheOid(NAMESPACENAME,
1261 CStringGetDatum(schemaname),
1263 if (!OidIsValid(namespaceId))
1265 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1266 errmsg("schema \"%s\" does not exist", schemaname)));
1267 /* we do not check for USAGE rights here! */
1271 /* use the default creation namespace */
1272 recomputeNamespacePath();
1273 namespaceId = defaultCreationNamespace;
1274 if (!OidIsValid(namespaceId))
1276 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1277 errmsg("no schema has been selected to create in")));
1280 /* Note: callers will check for CREATE rights when appropriate */
1282 *objname_p = objname;
1287 * makeRangeVarFromNameList
1288 * Utility routine to convert a qualified-name list into RangeVar form.
1291 makeRangeVarFromNameList(List *names)
1293 RangeVar *rel = makeRangeVar(NULL, NULL);
1295 switch (list_length(names))
1298 rel->relname = strVal(linitial(names));
1301 rel->schemaname = strVal(linitial(names));
1302 rel->relname = strVal(lsecond(names));
1305 rel->catalogname = strVal(linitial(names));
1306 rel->schemaname = strVal(lsecond(names));
1307 rel->relname = strVal(lthird(names));
1311 (errcode(ERRCODE_SYNTAX_ERROR),
1312 errmsg("improper relation name (too many dotted names): %s",
1313 NameListToString(names))));
1322 * Utility routine to convert a qualified-name list into a string.
1324 * This is used primarily to form error messages, and so we do not quote
1325 * the list elements, for the sake of legibility.
1328 NameListToString(List *names)
1330 StringInfoData string;
1333 initStringInfo(&string);
1337 if (l != list_head(names))
1338 appendStringInfoChar(&string, '.');
1339 appendStringInfoString(&string, strVal(lfirst(l)));
1346 * NameListToQuotedString
1347 * Utility routine to convert a qualified-name list into a string.
1349 * Same as above except that names will be double-quoted where necessary,
1350 * so the string could be re-parsed (eg, by textToQualifiedNameList).
1353 NameListToQuotedString(List *names)
1355 StringInfoData string;
1358 initStringInfo(&string);
1362 if (l != list_head(names))
1363 appendStringInfoChar(&string, '.');
1364 appendStringInfoString(&string, quote_identifier(strVal(lfirst(l))));
1371 * isTempNamespace - is the given namespace my temporary-table namespace?
1374 isTempNamespace(Oid namespaceId)
1376 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
1382 * isOtherTempNamespace - is the given namespace some other backend's
1383 * temporary-table namespace?
1386 isOtherTempNamespace(Oid namespaceId)
1391 /* If it's my own temp namespace, say "false" */
1392 if (isTempNamespace(namespaceId))
1394 /* Else, if the namespace name starts with "pg_temp_", say "true" */
1395 nspname = get_namespace_name(namespaceId);
1397 return false; /* no such namespace? */
1398 result = (strncmp(nspname, "pg_temp_", 8) == 0);
1404 * PushSpecialNamespace - push a "special" namespace onto the front of the
1407 * This is a slightly messy hack intended only for support of CREATE SCHEMA.
1408 * Although the API is defined to allow a stack of pushed namespaces, we
1409 * presently only support one at a time.
1411 * The pushed namespace will be removed from the search path at end of
1412 * transaction, whether commit or abort.
1415 PushSpecialNamespace(Oid namespaceId)
1417 Assert(!OidIsValid(mySpecialNamespace));
1418 mySpecialNamespace = namespaceId;
1419 namespaceSearchPathValid = false;
1423 * PopSpecialNamespace - remove previously pushed special namespace.
1426 PopSpecialNamespace(Oid namespaceId)
1428 Assert(mySpecialNamespace == namespaceId);
1429 mySpecialNamespace = InvalidOid;
1430 namespaceSearchPathValid = false;
1434 * FindConversionByName - find a conversion by possibly qualified name
1437 FindConversionByName(List *name)
1440 char *conversion_name;
1445 /* deconstruct the name list */
1446 DeconstructQualifiedName(name, &schemaname, &conversion_name);
1450 /* use exact schema given */
1451 namespaceId = LookupExplicitNamespace(schemaname);
1452 return FindConversion(conversion_name, namespaceId);
1456 /* search for it in search path */
1457 recomputeNamespacePath();
1459 foreach(l, namespaceSearchPath)
1461 namespaceId = lfirst_oid(l);
1462 conoid = FindConversion(conversion_name, namespaceId);
1463 if (OidIsValid(conoid))
1468 /* Not found in path */
1473 * FindDefaultConversionProc - find default encoding conversion proc
1476 FindDefaultConversionProc(int4 for_encoding, int4 to_encoding)
1481 recomputeNamespacePath();
1483 foreach(l, namespaceSearchPath)
1485 Oid namespaceId = lfirst_oid(l);
1487 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
1488 if (OidIsValid(proc))
1492 /* Not found in path */
1497 * recomputeNamespacePath - recompute path derived variables if needed.
1500 recomputeNamespacePath(void)
1502 AclId userId = GetUserId();
1509 MemoryContext oldcxt;
1512 * Do nothing if path is already valid.
1514 if (namespaceSearchPathValid && namespaceUser == userId)
1517 /* Need a modifiable copy of namespace_search_path string */
1518 rawname = pstrdup(namespace_search_path);
1520 /* Parse string into list of identifiers */
1521 if (!SplitIdentifierString(rawname, ',', &namelist))
1523 /* syntax error in name list */
1524 /* this should not happen if GUC checked check_search_path */
1525 elog(ERROR, "invalid list syntax");
1529 * Convert the list of names to a list of OIDs. If any names are not
1530 * recognizable or we don't have read access, just leave them out of
1531 * the list. (We can't raise an error, since the search_path setting
1532 * has already been accepted.) Don't make duplicate entries, either.
1535 foreach(l, namelist)
1537 char *curname = (char *) lfirst(l);
1540 if (strcmp(curname, "$user") == 0)
1542 /* $user --- substitute namespace matching user name, if any */
1545 tuple = SearchSysCache(SHADOWSYSID,
1546 ObjectIdGetDatum(userId),
1548 if (HeapTupleIsValid(tuple))
1552 uname = NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename);
1553 namespaceId = GetSysCacheOid(NAMESPACENAME,
1554 CStringGetDatum(uname),
1556 ReleaseSysCache(tuple);
1557 if (OidIsValid(namespaceId) &&
1558 !list_member_oid(oidlist, namespaceId) &&
1559 pg_namespace_aclcheck(namespaceId, userId,
1560 ACL_USAGE) == ACLCHECK_OK)
1561 oidlist = lappend_oid(oidlist, namespaceId);
1566 /* normal namespace reference */
1567 namespaceId = GetSysCacheOid(NAMESPACENAME,
1568 CStringGetDatum(curname),
1570 if (OidIsValid(namespaceId) &&
1571 !list_member_oid(oidlist, namespaceId) &&
1572 pg_namespace_aclcheck(namespaceId, userId,
1573 ACL_USAGE) == ACLCHECK_OK)
1574 oidlist = lappend_oid(oidlist, namespaceId);
1579 * Remember the first member of the explicit list.
1582 firstNS = InvalidOid;
1584 firstNS = linitial_oid(oidlist);
1587 * Add any implicitly-searched namespaces to the list. Note these go
1588 * on the front, not the back; also notice that we do not check USAGE
1589 * permissions for these.
1591 if (!list_member_oid(oidlist, PG_CATALOG_NAMESPACE))
1592 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
1594 if (OidIsValid(myTempNamespace) &&
1595 !list_member_oid(oidlist, myTempNamespace))
1596 oidlist = lcons_oid(myTempNamespace, oidlist);
1598 if (OidIsValid(mySpecialNamespace) &&
1599 !list_member_oid(oidlist, mySpecialNamespace))
1600 oidlist = lcons_oid(mySpecialNamespace, oidlist);
1603 * Now that we've successfully built the new list of namespace OIDs,
1604 * save it in permanent storage.
1606 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1607 newpath = list_copy(oidlist);
1608 MemoryContextSwitchTo(oldcxt);
1610 /* Now safe to assign to state variable. */
1611 list_free(namespaceSearchPath);
1612 namespaceSearchPath = newpath;
1615 * Update info derived from search path.
1617 firstExplicitNamespace = firstNS;
1618 if (OidIsValid(mySpecialNamespace))
1619 defaultCreationNamespace = mySpecialNamespace;
1621 defaultCreationNamespace = firstNS;
1623 /* Mark the path valid. */
1624 namespaceSearchPathValid = true;
1625 namespaceUser = userId;
1629 list_free(namelist);
1634 * InitTempTableNamespace
1635 * Initialize temp table namespace on first use in a particular backend
1638 InitTempTableNamespace(void)
1640 char namespaceName[NAMEDATALEN];
1644 * First, do permission check to see if we are authorized to make temp
1645 * tables. We use a nonstandard error message here since
1646 * "databasename: permission denied" might be a tad cryptic.
1648 * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
1649 * that's necessary since current user ID could change during the session.
1650 * But there's no need to make the namespace in the first place until a
1651 * temp table creation request is made by someone with appropriate rights.
1653 if (pg_database_aclcheck(MyDatabaseId, GetUserId(),
1654 ACL_CREATE_TEMP) != ACLCHECK_OK)
1656 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1657 errmsg("permission denied to create temporary tables in database \"%s\"",
1658 get_database_name(MyDatabaseId))));
1660 snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
1662 namespaceId = GetSysCacheOid(NAMESPACENAME,
1663 CStringGetDatum(namespaceName),
1665 if (!OidIsValid(namespaceId))
1668 * First use of this temp namespace in this database; create it.
1669 * The temp namespaces are always owned by the superuser. We
1670 * leave their permissions at default --- i.e., no access except
1671 * to superuser --- to ensure that unprivileged users can't peek
1672 * at other backends' temp tables. This works because the places
1673 * that access the temp namespace for my own backend skip
1674 * permissions checks on it.
1676 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_USESYSID, 0);
1677 /* Advance command counter to make namespace visible */
1678 CommandCounterIncrement();
1683 * If the namespace already exists, clean it out (in case the
1684 * former owner crashed without doing so).
1686 RemoveTempRelations(namespaceId);
1690 * Okay, we've prepared the temp namespace ... but it's not committed
1691 * yet, so all our work could be undone by transaction rollback. Set
1692 * flag for AtEOXact_Namespace to know what to do.
1694 myTempNamespace = namespaceId;
1696 /* It should not be done already. */
1697 AssertState(myTempNamespaceXID == InvalidTransactionId);
1698 myTempNamespaceXID = GetCurrentTransactionId();
1700 namespaceSearchPathValid = false; /* need to rebuild list */
1704 * End-of-transaction cleanup for namespaces.
1707 AtEOXact_Namespace(bool isCommit)
1710 * If we abort the transaction in which a temp namespace was selected,
1711 * we'll have to do any creation or cleanout work over again. So,
1712 * just forget the namespace entirely until next time. On the other
1713 * hand, if we commit then register an exit callback to clean out the
1714 * temp tables at backend shutdown. (We only want to register the
1715 * callback once per session, so this is a good place to do it.)
1717 if (myTempNamespaceXID == GetCurrentTransactionId())
1720 on_shmem_exit(RemoveTempRelationsCallback, 0);
1723 myTempNamespace = InvalidOid;
1724 namespaceSearchPathValid = false; /* need to rebuild list */
1726 myTempNamespaceXID = InvalidTransactionId;
1730 * Clean up if someone failed to do PopSpecialNamespace
1732 if (OidIsValid(mySpecialNamespace))
1734 mySpecialNamespace = InvalidOid;
1735 namespaceSearchPathValid = false; /* need to rebuild list */
1740 * AtEOSubXact_Namespace
1742 * At subtransaction commit, propagate the temp-namespace-creation
1743 * flag to the parent transaction.
1745 * At subtransaction abort, forget the flag if we set it up.
1748 AtEOSubXact_Namespace(bool isCommit, TransactionId myXid,
1749 TransactionId parentXid)
1751 if (myTempNamespaceXID == myXid)
1754 myTempNamespaceXID = parentXid;
1757 myTempNamespaceXID = InvalidTransactionId;
1758 /* TEMP namespace creation failed, so reset state */
1759 myTempNamespace = InvalidOid;
1760 namespaceSearchPathValid = false; /* need to rebuild list */
1766 * Remove all relations in the specified temp namespace.
1768 * This is called at backend shutdown (if we made any temp relations).
1769 * It is also called when we begin using a pre-existing temp namespace,
1770 * in order to clean out any relations that might have been created by
1771 * a crashed backend.
1774 RemoveTempRelations(Oid tempNamespaceId)
1776 ObjectAddress object;
1779 * We want to get rid of everything in the target namespace, but not
1780 * the namespace itself (deleting it only to recreate it later would
1781 * be a waste of cycles). We do this by finding everything that has a
1782 * dependency on the namespace.
1784 object.classId = get_system_catalog_relid(NamespaceRelationName);
1785 object.objectId = tempNamespaceId;
1786 object.objectSubId = 0;
1788 deleteWhatDependsOn(&object, false);
1792 * Callback to remove temp relations at backend exit.
1795 RemoveTempRelationsCallback(int code, Datum arg)
1797 if (OidIsValid(myTempNamespace)) /* should always be true */
1799 /* Need to ensure we have a usable transaction. */
1800 AbortOutOfAnyTransaction();
1801 StartTransactionCommand();
1803 RemoveTempRelations(myTempNamespace);
1805 CommitTransactionCommand();
1811 * Routines for handling the GUC variable 'search_path'.
1814 /* assign_hook: validate new search_path, do extra actions as needed */
1816 assign_search_path(const char *newval, bool doit, GucSource source)
1822 /* Need a modifiable copy of string */
1823 rawname = pstrdup(newval);
1825 /* Parse string into list of identifiers */
1826 if (!SplitIdentifierString(rawname, ',', &namelist))
1828 /* syntax error in name list */
1830 list_free(namelist);
1835 * If we aren't inside a transaction, we cannot do database access so
1836 * cannot verify the individual names. Must accept the list on faith.
1838 if (source >= PGC_S_INTERACTIVE && IsTransactionState())
1841 * Verify that all the names are either valid namespace names or
1842 * "$user". We do not require $user to correspond to a valid
1843 * namespace. We do not check for USAGE rights, either; should
1846 * When source == PGC_S_TEST, we are checking the argument of an
1847 * ALTER DATABASE SET or ALTER USER SET command. It could be that
1848 * the intended use of the search path is for some other database,
1849 * so we should not error out if it mentions schemas not present
1850 * in the current database. We reduce the message to NOTICE instead.
1852 foreach(l, namelist)
1854 char *curname = (char *) lfirst(l);
1856 if (strcmp(curname, "$user") == 0)
1858 if (!SearchSysCacheExists(NAMESPACENAME,
1859 CStringGetDatum(curname),
1861 ereport((source == PGC_S_TEST) ? NOTICE : ERROR,
1862 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1863 errmsg("schema \"%s\" does not exist", curname)));
1868 list_free(namelist);
1871 * We mark the path as needing recomputation, but don't do anything
1872 * until it's needed. This avoids trying to do database access during
1873 * GUC initialization.
1876 namespaceSearchPathValid = false;
1882 * InitializeSearchPath: initialize module during InitPostgres.
1884 * This is called after we are up enough to be able to do catalog lookups.
1887 InitializeSearchPath(void)
1889 if (IsBootstrapProcessingMode())
1892 * In bootstrap mode, the search path must be 'pg_catalog' so that
1893 * tables are created in the proper namespace; ignore the GUC
1896 MemoryContext oldcxt;
1898 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1899 namespaceSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
1900 MemoryContextSwitchTo(oldcxt);
1901 defaultCreationNamespace = PG_CATALOG_NAMESPACE;
1902 firstExplicitNamespace = PG_CATALOG_NAMESPACE;
1903 namespaceSearchPathValid = true;
1904 namespaceUser = GetUserId();
1909 * In normal mode, arrange for a callback on any syscache
1910 * invalidation of pg_namespace rows.
1912 CacheRegisterSyscacheCallback(NAMESPACEOID,
1915 /* Force search path to be recomputed on next use */
1916 namespaceSearchPathValid = false;
1922 * Syscache inval callback function
1925 NamespaceCallback(Datum arg, Oid relid)
1927 /* Force search path to be recomputed on next use */
1928 namespaceSearchPathValid = false;
1932 * Fetch the active search path. The return value is a palloc'ed list
1933 * of OIDs; the caller is responsible for freeing this storage as
1936 * The returned list includes the implicitly-prepended namespaces only if
1937 * includeImplicit is true.
1940 fetch_search_path(bool includeImplicit)
1944 recomputeNamespacePath();
1946 result = list_copy(namespaceSearchPath);
1947 if (!includeImplicit)
1949 while (result && linitial_oid(result) != firstExplicitNamespace)
1950 result = list_delete_first(result);
1957 * Export the FooIsVisible functions as SQL-callable functions.
1961 pg_table_is_visible(PG_FUNCTION_ARGS)
1963 Oid oid = PG_GETARG_OID(0);
1965 PG_RETURN_BOOL(RelationIsVisible(oid));
1969 pg_type_is_visible(PG_FUNCTION_ARGS)
1971 Oid oid = PG_GETARG_OID(0);
1973 PG_RETURN_BOOL(TypeIsVisible(oid));
1977 pg_function_is_visible(PG_FUNCTION_ARGS)
1979 Oid oid = PG_GETARG_OID(0);
1981 PG_RETURN_BOOL(FunctionIsVisible(oid));
1985 pg_operator_is_visible(PG_FUNCTION_ARGS)
1987 Oid oid = PG_GETARG_OID(0);
1989 PG_RETURN_BOOL(OperatorIsVisible(oid));
1993 pg_opclass_is_visible(PG_FUNCTION_ARGS)
1995 Oid oid = PG_GETARG_OID(0);
1997 PG_RETURN_BOOL(OpclassIsVisible(oid));
2001 pg_conversion_is_visible(PG_FUNCTION_ARGS)
2003 Oid oid = PG_GETARG_OID(0);
2005 PG_RETURN_BOOL(ConversionIsVisible(oid));