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-2007, 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.99 2007/08/27 03:36:08 tgl Exp $
18 *-------------------------------------------------------------------------
22 #include "access/xact.h"
23 #include "catalog/dependency.h"
24 #include "catalog/namespace.h"
25 #include "catalog/pg_authid.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_opfamily.h"
31 #include "catalog/pg_proc.h"
32 #include "catalog/pg_ts_config.h"
33 #include "catalog/pg_ts_dict.h"
34 #include "catalog/pg_ts_parser.h"
35 #include "catalog/pg_ts_template.h"
36 #include "catalog/pg_type.h"
37 #include "commands/dbcommands.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/guc.h"
45 #include "utils/inval.h"
46 #include "utils/lsyscache.h"
47 #include "utils/memutils.h"
48 #include "utils/syscache.h"
52 * The namespace search path is a possibly-empty list of namespace OIDs.
53 * In addition to the explicit list, implicitly-searched namespaces
56 * 1. If a TEMP table namespace has been initialized in this session, it
57 * is implicitly searched first. (The only time this doesn't happen is
58 * when we are obeying an override search path spec that says not to use the
59 * temp namespace, or the temp namespace is included in the explicit list.)
61 * 2. The system catalog namespace is always searched. If the system
62 * namespace is present in the explicit path then it will be searched in
63 * the specified order; otherwise it will be searched after TEMP tables and
64 * *before* the explicit list. (It might seem that the system namespace
65 * should be implicitly last, but this behavior appears to be required by
66 * SQL99. Also, this provides a way to search the system namespace first
67 * without thereby making it the default creation target namespace.)
69 * For security reasons, searches using the search path will ignore the temp
70 * namespace when searching for any object type other than relations and
71 * types. (We must allow types since temp tables have rowtypes.)
73 * The default creation target namespace is always the first element of the
74 * explicit list. If the explicit list is empty, there is no default target.
76 * The textual specification of search_path can include "$user" to refer to
77 * the namespace named the same as the current user, if any. (This is just
78 * ignored if there is no such namespace.) Also, it can include "pg_temp"
79 * to refer to the current backend's temp namespace. This is usually also
80 * ignorable if the temp namespace hasn't been set up, but there's a special
81 * case: if "pg_temp" appears first then it should be the default creation
82 * target. We kluge this case a little bit so that the temp namespace isn't
83 * set up until the first attempt to create something in it. (The reason for
84 * klugery is that we can't create the temp namespace outside a transaction,
85 * but initial GUC processing of search_path happens outside a transaction.)
86 * activeTempCreationPending is TRUE if "pg_temp" appears first in the string
87 * but is not reflected in activeCreationNamespace because the namespace isn't
90 * In bootstrap mode, the search path is set equal to "pg_catalog", so that
91 * the system namespace is the only one searched or inserted into.
92 * initdb is also careful to set search_path to "pg_catalog" for its
93 * post-bootstrap standalone backend runs. Otherwise the default search
94 * path is determined by GUC. The factory default path contains the PUBLIC
95 * namespace (if it exists), preceded by the user's personal namespace
98 * We support a stack of "override" search path settings for use within
99 * specific sections of backend code. namespace_search_path is ignored
100 * whenever the override stack is nonempty. activeSearchPath is always
101 * the actually active path; it points either to the search list of the
102 * topmost stack entry, or to baseSearchPath which is the list derived
103 * from namespace_search_path.
105 * If baseSearchPathValid is false, then baseSearchPath (and other
106 * derived variables) need to be recomputed from namespace_search_path.
107 * We mark it invalid upon an assignment to namespace_search_path or receipt
108 * of a syscache invalidation event for pg_namespace. The recomputation
109 * is done during the next non-overridden lookup attempt. Note that an
110 * override spec is never subject to recomputation.
112 * Any namespaces mentioned in namespace_search_path that are not readable
113 * by the current user ID are simply left out of baseSearchPath; so
114 * we have to be willing to recompute the path when current userid changes.
115 * namespaceUser is the userid the path has been computed for.
117 * Note: all data pointed to by these List variables is in TopMemoryContext.
120 /* These variables define the actually active state: */
122 static List *activeSearchPath = NIL;
124 /* default place to create stuff; if InvalidOid, no default */
125 static Oid activeCreationNamespace = InvalidOid;
127 /* if TRUE, activeCreationNamespace is wrong, it should be temp namespace */
128 static bool activeTempCreationPending = false;
130 /* These variables are the values last derived from namespace_search_path: */
132 static List *baseSearchPath = NIL;
134 static Oid baseCreationNamespace = InvalidOid;
136 static bool baseTempCreationPending = false;
138 static Oid namespaceUser = InvalidOid;
140 /* The above four values are valid only if baseSearchPathValid */
141 static bool baseSearchPathValid = true;
143 /* Override requests are remembered in a stack of OverrideStackEntry structs */
147 List *searchPath; /* the desired search path */
148 Oid creationNamespace; /* the desired creation namespace */
149 int nestLevel; /* subtransaction nesting level */
150 } OverrideStackEntry;
152 static List *overrideStack = NIL;
155 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
156 * in a particular backend session (this happens when a CREATE TEMP TABLE
157 * command is first executed). Thereafter it's the OID of the temp namespace.
159 * myTempToastNamespace is the OID of the namespace for my temp tables' toast
160 * tables. It is set when myTempNamespace is, and is InvalidOid before that.
162 * myTempNamespaceSubID shows whether we've created the TEMP namespace in the
163 * current subtransaction. The flag propagates up the subtransaction tree,
164 * so the main transaction will correctly recognize the flag if all
165 * intermediate subtransactions commit. When it is InvalidSubTransactionId,
166 * we either haven't made the TEMP namespace yet, or have successfully
167 * committed its creation, depending on whether myTempNamespace is valid.
169 static Oid myTempNamespace = InvalidOid;
171 static Oid myTempToastNamespace = InvalidOid;
173 static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId;
176 * This is the user's textual search path specification --- it's the value
177 * of the GUC variable 'search_path'.
179 char *namespace_search_path = NULL;
182 /* Local functions */
183 static void recomputeNamespacePath(void);
184 static void InitTempTableNamespace(void);
185 static void RemoveTempRelations(Oid tempNamespaceId);
186 static void RemoveTempRelationsCallback(int code, Datum arg);
187 static void NamespaceCallback(Datum arg, Oid relid);
189 /* These don't really need to appear in any header file */
190 Datum pg_table_is_visible(PG_FUNCTION_ARGS);
191 Datum pg_type_is_visible(PG_FUNCTION_ARGS);
192 Datum pg_function_is_visible(PG_FUNCTION_ARGS);
193 Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
194 Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
195 Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
196 Datum pg_ts_parser_is_visible(PG_FUNCTION_ARGS);
197 Datum pg_ts_dict_is_visible(PG_FUNCTION_ARGS);
198 Datum pg_ts_template_is_visible(PG_FUNCTION_ARGS);
199 Datum pg_ts_config_is_visible(PG_FUNCTION_ARGS);
200 Datum pg_my_temp_schema(PG_FUNCTION_ARGS);
201 Datum pg_is_other_temp_schema(PG_FUNCTION_ARGS);
206 * Given a RangeVar describing an existing relation,
207 * select the proper namespace and look up the relation OID.
209 * If the relation is not found, return InvalidOid if failOK = true,
210 * otherwise raise an error.
213 RangeVarGetRelid(const RangeVar *relation, bool failOK)
219 * We check the catalog name and then ignore it.
221 if (relation->catalogname)
223 if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
225 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
226 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
227 relation->catalogname, relation->schemaname,
228 relation->relname)));
232 * If istemp is set, this is a reference to a temp relation. The parser
233 * never generates such a RangeVar in simple DML, but it can happen in
234 * contexts such as "CREATE TEMP TABLE foo (f1 int PRIMARY KEY)". Such a
235 * command will generate an added CREATE INDEX operation, which must be
236 * careful to find the temp table, even when pg_temp is not first in the
239 if (relation->istemp)
241 if (relation->schemaname)
243 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
244 errmsg("temporary tables cannot specify a schema name")));
245 if (OidIsValid(myTempNamespace))
246 relId = get_relname_relid(relation->relname, myTempNamespace);
247 else /* this probably can't happen? */
250 else if (relation->schemaname)
252 /* use exact schema given */
253 namespaceId = LookupExplicitNamespace(relation->schemaname);
254 relId = get_relname_relid(relation->relname, namespaceId);
258 /* search the namespace path */
259 relId = RelnameGetRelid(relation->relname);
262 if (!OidIsValid(relId) && !failOK)
264 if (relation->schemaname)
266 (errcode(ERRCODE_UNDEFINED_TABLE),
267 errmsg("relation \"%s.%s\" does not exist",
268 relation->schemaname, relation->relname)));
271 (errcode(ERRCODE_UNDEFINED_TABLE),
272 errmsg("relation \"%s\" does not exist",
273 relation->relname)));
279 * RangeVarGetCreationNamespace
280 * Given a RangeVar describing a to-be-created relation,
281 * choose which namespace to create it in.
283 * Note: calling this may result in a CommandCounterIncrement operation.
284 * That will happen on the first request for a temp table in any particular
285 * backend run; we will need to either create or clean out the temp schema.
288 RangeVarGetCreationNamespace(const RangeVar *newRelation)
293 * We check the catalog name and then ignore it.
295 if (newRelation->catalogname)
297 if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
299 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
300 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
301 newRelation->catalogname, newRelation->schemaname,
302 newRelation->relname)));
305 if (newRelation->istemp)
307 /* TEMP tables are created in our backend-local temp namespace */
308 if (newRelation->schemaname)
310 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
311 errmsg("temporary tables cannot specify a schema name")));
312 /* Initialize temp namespace if first time through */
313 if (!OidIsValid(myTempNamespace))
314 InitTempTableNamespace();
315 return myTempNamespace;
318 if (newRelation->schemaname)
320 /* check for pg_temp alias */
321 if (strcmp(newRelation->schemaname, "pg_temp") == 0)
323 /* Initialize temp namespace if first time through */
324 if (!OidIsValid(myTempNamespace))
325 InitTempTableNamespace();
326 return myTempNamespace;
328 /* use exact schema given */
329 namespaceId = GetSysCacheOid(NAMESPACENAME,
330 CStringGetDatum(newRelation->schemaname),
332 if (!OidIsValid(namespaceId))
334 (errcode(ERRCODE_UNDEFINED_SCHEMA),
335 errmsg("schema \"%s\" does not exist",
336 newRelation->schemaname)));
337 /* we do not check for USAGE rights here! */
341 /* use the default creation namespace */
342 recomputeNamespacePath();
343 if (activeTempCreationPending)
345 /* Need to initialize temp namespace */
346 InitTempTableNamespace();
347 return myTempNamespace;
349 namespaceId = activeCreationNamespace;
350 if (!OidIsValid(namespaceId))
352 (errcode(ERRCODE_UNDEFINED_SCHEMA),
353 errmsg("no schema has been selected to create in")));
356 /* Note: callers will check for CREATE rights when appropriate */
363 * Try to resolve an unqualified relation name.
364 * Returns OID if relation found in search path, else InvalidOid.
367 RelnameGetRelid(const char *relname)
372 recomputeNamespacePath();
374 foreach(l, activeSearchPath)
376 Oid namespaceId = lfirst_oid(l);
378 relid = get_relname_relid(relname, namespaceId);
379 if (OidIsValid(relid))
383 /* Not found in path */
390 * Determine whether a relation (identified by OID) is visible in the
391 * current search path. Visible means "would be found by searching
392 * for the unqualified relation name".
395 RelationIsVisible(Oid relid)
398 Form_pg_class relform;
402 reltup = SearchSysCache(RELOID,
403 ObjectIdGetDatum(relid),
405 if (!HeapTupleIsValid(reltup))
406 elog(ERROR, "cache lookup failed for relation %u", relid);
407 relform = (Form_pg_class) GETSTRUCT(reltup);
409 recomputeNamespacePath();
412 * Quick check: if it ain't in the path at all, it ain't visible. Items in
413 * the system namespace are surely in the path and so we needn't even do
414 * list_member_oid() for them.
416 relnamespace = relform->relnamespace;
417 if (relnamespace != PG_CATALOG_NAMESPACE &&
418 !list_member_oid(activeSearchPath, relnamespace))
423 * If it is in the path, it might still not be visible; it could be
424 * hidden by another relation of the same name earlier in the path. So
425 * we must do a slow check for conflicting relations.
427 char *relname = NameStr(relform->relname);
431 foreach(l, activeSearchPath)
433 Oid namespaceId = lfirst_oid(l);
435 if (namespaceId == relnamespace)
437 /* Found it first in path */
441 if (OidIsValid(get_relname_relid(relname, namespaceId)))
443 /* Found something else first in path */
449 ReleaseSysCache(reltup);
457 * Try to resolve an unqualified datatype name.
458 * Returns OID if type found in search path, else InvalidOid.
460 * This is essentially the same as RelnameGetRelid.
463 TypenameGetTypid(const char *typname)
468 recomputeNamespacePath();
470 foreach(l, activeSearchPath)
472 Oid namespaceId = lfirst_oid(l);
474 typid = GetSysCacheOid(TYPENAMENSP,
475 PointerGetDatum(typname),
476 ObjectIdGetDatum(namespaceId),
478 if (OidIsValid(typid))
482 /* Not found in path */
488 * Determine whether a type (identified by OID) is visible in the
489 * current search path. Visible means "would be found by searching
490 * for the unqualified type name".
493 TypeIsVisible(Oid typid)
496 Form_pg_type typform;
500 typtup = SearchSysCache(TYPEOID,
501 ObjectIdGetDatum(typid),
503 if (!HeapTupleIsValid(typtup))
504 elog(ERROR, "cache lookup failed for type %u", typid);
505 typform = (Form_pg_type) GETSTRUCT(typtup);
507 recomputeNamespacePath();
510 * Quick check: if it ain't in the path at all, it ain't visible. Items in
511 * the system namespace are surely in the path and so we needn't even do
512 * list_member_oid() for them.
514 typnamespace = typform->typnamespace;
515 if (typnamespace != PG_CATALOG_NAMESPACE &&
516 !list_member_oid(activeSearchPath, typnamespace))
521 * If it is in the path, it might still not be visible; it could be
522 * hidden by another type of the same name earlier in the path. So we
523 * must do a slow check for conflicting types.
525 char *typname = NameStr(typform->typname);
529 foreach(l, activeSearchPath)
531 Oid namespaceId = lfirst_oid(l);
533 if (namespaceId == typnamespace)
535 /* Found it first in path */
539 if (SearchSysCacheExists(TYPENAMENSP,
540 PointerGetDatum(typname),
541 ObjectIdGetDatum(namespaceId),
544 /* Found something else first in path */
550 ReleaseSysCache(typtup);
557 * FuncnameGetCandidates
558 * Given a possibly-qualified function name and argument count,
559 * retrieve a list of the possible matches.
561 * If nargs is -1, we return all functions matching the given name,
562 * regardless of argument count.
564 * We search a single namespace if the function name is qualified, else
565 * all namespaces in the search path. The return list will never contain
566 * multiple entries with identical argument lists --- in the multiple-
567 * namespace case, we arrange for entries in earlier namespaces to mask
568 * identical entries in later namespaces.
571 FuncnameGetCandidates(List *names, int nargs)
573 FuncCandidateList resultList = NULL;
580 /* deconstruct the name list */
581 DeconstructQualifiedName(names, &schemaname, &funcname);
585 /* use exact schema given */
586 namespaceId = LookupExplicitNamespace(schemaname);
590 /* flag to indicate we need namespace search */
591 namespaceId = InvalidOid;
592 recomputeNamespacePath();
595 /* Search syscache by name only */
596 catlist = SearchSysCacheList(PROCNAMEARGSNSP, 1,
597 CStringGetDatum(funcname),
600 for (i = 0; i < catlist->n_members; i++)
602 HeapTuple proctup = &catlist->members[i]->tuple;
603 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
604 int pronargs = procform->pronargs;
606 FuncCandidateList newResult;
608 /* Ignore if it doesn't match requested argument count */
609 if (nargs >= 0 && pronargs != nargs)
612 if (OidIsValid(namespaceId))
614 /* Consider only procs in specified namespace */
615 if (procform->pronamespace != namespaceId)
617 /* No need to check args, they must all be different */
622 * Consider only procs that are in the search path and are not
623 * in the temp namespace.
627 foreach(nsp, activeSearchPath)
629 if (procform->pronamespace == lfirst_oid(nsp) &&
630 procform->pronamespace != myTempNamespace)
635 continue; /* proc is not in search path */
638 * Okay, it's in the search path, but does it have the same
639 * arguments as something we already accepted? If so, keep only
640 * the one that appears earlier in the search path.
642 * If we have an ordered list from SearchSysCacheList (the normal
643 * case), then any conflicting proc must immediately adjoin this
644 * one in the list, so we only need to look at the newest result
645 * item. If we have an unordered list, we have to scan the whole
650 FuncCandidateList prevResult;
652 if (catlist->ordered)
654 if (pronargs == resultList->nargs &&
655 memcmp(procform->proargtypes.values,
657 pronargs * sizeof(Oid)) == 0)
658 prevResult = resultList;
664 for (prevResult = resultList;
666 prevResult = prevResult->next)
668 if (pronargs == prevResult->nargs &&
669 memcmp(procform->proargtypes.values,
671 pronargs * sizeof(Oid)) == 0)
677 /* We have a match with a previous result */
678 Assert(pathpos != prevResult->pathpos);
679 if (pathpos > prevResult->pathpos)
680 continue; /* keep previous result */
681 /* replace previous result */
682 prevResult->pathpos = pathpos;
683 prevResult->oid = HeapTupleGetOid(proctup);
684 continue; /* args are same, of course */
690 * Okay to add it to result list
692 newResult = (FuncCandidateList)
693 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
694 + pronargs * sizeof(Oid));
695 newResult->pathpos = pathpos;
696 newResult->oid = HeapTupleGetOid(proctup);
697 newResult->nargs = pronargs;
698 memcpy(newResult->args, procform->proargtypes.values,
699 pronargs * sizeof(Oid));
701 newResult->next = resultList;
702 resultList = newResult;
705 ReleaseSysCacheList(catlist);
712 * Determine whether a function (identified by OID) is visible in the
713 * current search path. Visible means "would be found by searching
714 * for the unqualified function name with exact argument matches".
717 FunctionIsVisible(Oid funcid)
720 Form_pg_proc procform;
724 proctup = SearchSysCache(PROCOID,
725 ObjectIdGetDatum(funcid),
727 if (!HeapTupleIsValid(proctup))
728 elog(ERROR, "cache lookup failed for function %u", funcid);
729 procform = (Form_pg_proc) GETSTRUCT(proctup);
731 recomputeNamespacePath();
734 * Quick check: if it ain't in the path at all, it ain't visible. Items in
735 * the system namespace are surely in the path and so we needn't even do
736 * list_member_oid() for them.
738 pronamespace = procform->pronamespace;
739 if (pronamespace != PG_CATALOG_NAMESPACE &&
740 !list_member_oid(activeSearchPath, pronamespace))
745 * If it is in the path, it might still not be visible; it could be
746 * hidden by another proc of the same name and arguments earlier in
747 * the path. So we must do a slow check to see if this is the same
748 * proc that would be found by FuncnameGetCandidates.
750 char *proname = NameStr(procform->proname);
751 int nargs = procform->pronargs;
752 FuncCandidateList clist;
756 clist = FuncnameGetCandidates(list_make1(makeString(proname)), nargs);
758 for (; clist; clist = clist->next)
760 if (memcmp(clist->args, procform->proargtypes.values,
761 nargs * sizeof(Oid)) == 0)
763 /* Found the expected entry; is it the right proc? */
764 visible = (clist->oid == funcid);
770 ReleaseSysCache(proctup);
778 * Given a possibly-qualified operator name and exact input datatypes,
779 * look up the operator. Returns InvalidOid if not found.
781 * Pass oprleft = InvalidOid for a prefix op, oprright = InvalidOid for
784 * If the operator name is not schema-qualified, it is sought in the current
785 * namespace search path.
788 OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
795 /* deconstruct the name list */
796 DeconstructQualifiedName(names, &schemaname, &opername);
800 /* search only in exact schema given */
804 namespaceId = LookupExplicitNamespace(schemaname);
805 opertup = SearchSysCache(OPERNAMENSP,
806 CStringGetDatum(opername),
807 ObjectIdGetDatum(oprleft),
808 ObjectIdGetDatum(oprright),
809 ObjectIdGetDatum(namespaceId));
810 if (HeapTupleIsValid(opertup))
812 Oid result = HeapTupleGetOid(opertup);
814 ReleaseSysCache(opertup);
820 /* Search syscache by name and argument types */
821 catlist = SearchSysCacheList(OPERNAMENSP, 3,
822 CStringGetDatum(opername),
823 ObjectIdGetDatum(oprleft),
824 ObjectIdGetDatum(oprright),
827 if (catlist->n_members == 0)
829 /* no hope, fall out early */
830 ReleaseSysCacheList(catlist);
835 * We have to find the list member that is first in the search path, if
836 * there's more than one. This doubly-nested loop looks ugly, but in
837 * practice there should usually be few catlist members.
839 recomputeNamespacePath();
841 foreach(l, activeSearchPath)
843 Oid namespaceId = lfirst_oid(l);
846 if (namespaceId == myTempNamespace)
847 continue; /* do not look in temp namespace */
849 for (i = 0; i < catlist->n_members; i++)
851 HeapTuple opertup = &catlist->members[i]->tuple;
852 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
854 if (operform->oprnamespace == namespaceId)
856 Oid result = HeapTupleGetOid(opertup);
858 ReleaseSysCacheList(catlist);
864 ReleaseSysCacheList(catlist);
869 * OpernameGetCandidates
870 * Given a possibly-qualified operator name and operator kind,
871 * retrieve a list of the possible matches.
873 * If oprkind is '\0', we return all operators matching the given name,
874 * regardless of arguments.
876 * We search a single namespace if the operator name is qualified, else
877 * all namespaces in the search path. The return list will never contain
878 * multiple entries with identical argument lists --- in the multiple-
879 * namespace case, we arrange for entries in earlier namespaces to mask
880 * identical entries in later namespaces.
882 * The returned items always have two args[] entries --- one or the other
883 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
886 OpernameGetCandidates(List *names, char oprkind)
888 FuncCandidateList resultList = NULL;
889 char *resultSpace = NULL;
897 /* deconstruct the name list */
898 DeconstructQualifiedName(names, &schemaname, &opername);
902 /* use exact schema given */
903 namespaceId = LookupExplicitNamespace(schemaname);
907 /* flag to indicate we need namespace search */
908 namespaceId = InvalidOid;
909 recomputeNamespacePath();
912 /* Search syscache by name only */
913 catlist = SearchSysCacheList(OPERNAMENSP, 1,
914 CStringGetDatum(opername),
918 * In typical scenarios, most if not all of the operators found by the
919 * catcache search will end up getting returned; and there can be quite a
920 * few, for common operator names such as '=' or '+'. To reduce the time
921 * spent in palloc, we allocate the result space as an array large enough
922 * to hold all the operators. The original coding of this routine did a
923 * separate palloc for each operator, but profiling revealed that the
924 * pallocs used an unreasonably large fraction of parsing time.
926 #define SPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
928 if (catlist->n_members > 0)
929 resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
931 for (i = 0; i < catlist->n_members; i++)
933 HeapTuple opertup = &catlist->members[i]->tuple;
934 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
936 FuncCandidateList newResult;
938 /* Ignore operators of wrong kind, if specific kind requested */
939 if (oprkind && operform->oprkind != oprkind)
942 if (OidIsValid(namespaceId))
944 /* Consider only opers in specified namespace */
945 if (operform->oprnamespace != namespaceId)
947 /* No need to check args, they must all be different */
952 * Consider only opers that are in the search path and are not
953 * in the temp namespace.
957 foreach(nsp, activeSearchPath)
959 if (operform->oprnamespace == lfirst_oid(nsp) &&
960 operform->oprnamespace != myTempNamespace)
965 continue; /* oper is not in search path */
968 * Okay, it's in the search path, but does it have the same
969 * arguments as something we already accepted? If so, keep only
970 * the one that appears earlier in the search path.
972 * If we have an ordered list from SearchSysCacheList (the normal
973 * case), then any conflicting oper must immediately adjoin this
974 * one in the list, so we only need to look at the newest result
975 * item. If we have an unordered list, we have to scan the whole
980 FuncCandidateList prevResult;
982 if (catlist->ordered)
984 if (operform->oprleft == resultList->args[0] &&
985 operform->oprright == resultList->args[1])
986 prevResult = resultList;
992 for (prevResult = resultList;
994 prevResult = prevResult->next)
996 if (operform->oprleft == prevResult->args[0] &&
997 operform->oprright == prevResult->args[1])
1003 /* We have a match with a previous result */
1004 Assert(pathpos != prevResult->pathpos);
1005 if (pathpos > prevResult->pathpos)
1006 continue; /* keep previous result */
1007 /* replace previous result */
1008 prevResult->pathpos = pathpos;
1009 prevResult->oid = HeapTupleGetOid(opertup);
1010 continue; /* args are same, of course */
1016 * Okay to add it to result list
1018 newResult = (FuncCandidateList) (resultSpace + nextResult);
1019 nextResult += SPACE_PER_OP;
1021 newResult->pathpos = pathpos;
1022 newResult->oid = HeapTupleGetOid(opertup);
1023 newResult->nargs = 2;
1024 newResult->args[0] = operform->oprleft;
1025 newResult->args[1] = operform->oprright;
1026 newResult->next = resultList;
1027 resultList = newResult;
1030 ReleaseSysCacheList(catlist);
1037 * Determine whether an operator (identified by OID) is visible in the
1038 * current search path. Visible means "would be found by searching
1039 * for the unqualified operator name with exact argument matches".
1042 OperatorIsVisible(Oid oprid)
1045 Form_pg_operator oprform;
1049 oprtup = SearchSysCache(OPEROID,
1050 ObjectIdGetDatum(oprid),
1052 if (!HeapTupleIsValid(oprtup))
1053 elog(ERROR, "cache lookup failed for operator %u", oprid);
1054 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
1056 recomputeNamespacePath();
1059 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1060 * the system namespace are surely in the path and so we needn't even do
1061 * list_member_oid() for them.
1063 oprnamespace = oprform->oprnamespace;
1064 if (oprnamespace != PG_CATALOG_NAMESPACE &&
1065 !list_member_oid(activeSearchPath, oprnamespace))
1070 * If it is in the path, it might still not be visible; it could be
1071 * hidden by another operator of the same name and arguments earlier
1072 * in the path. So we must do a slow check to see if this is the same
1073 * operator that would be found by OpernameGetOprId.
1075 char *oprname = NameStr(oprform->oprname);
1077 visible = (OpernameGetOprid(list_make1(makeString(oprname)),
1078 oprform->oprleft, oprform->oprright)
1082 ReleaseSysCache(oprtup);
1089 * OpclassnameGetOpcid
1090 * Try to resolve an unqualified index opclass name.
1091 * Returns OID if opclass found in search path, else InvalidOid.
1093 * This is essentially the same as TypenameGetTypid, but we have to have
1094 * an extra argument for the index AM OID.
1097 OpclassnameGetOpcid(Oid amid, const char *opcname)
1102 recomputeNamespacePath();
1104 foreach(l, activeSearchPath)
1106 Oid namespaceId = lfirst_oid(l);
1108 if (namespaceId == myTempNamespace)
1109 continue; /* do not look in temp namespace */
1111 opcid = GetSysCacheOid(CLAAMNAMENSP,
1112 ObjectIdGetDatum(amid),
1113 PointerGetDatum(opcname),
1114 ObjectIdGetDatum(namespaceId),
1116 if (OidIsValid(opcid))
1120 /* Not found in path */
1126 * Determine whether an opclass (identified by OID) is visible in the
1127 * current search path. Visible means "would be found by searching
1128 * for the unqualified opclass name".
1131 OpclassIsVisible(Oid opcid)
1134 Form_pg_opclass opcform;
1138 opctup = SearchSysCache(CLAOID,
1139 ObjectIdGetDatum(opcid),
1141 if (!HeapTupleIsValid(opctup))
1142 elog(ERROR, "cache lookup failed for opclass %u", opcid);
1143 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1145 recomputeNamespacePath();
1148 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1149 * the system namespace are surely in the path and so we needn't even do
1150 * list_member_oid() for them.
1152 opcnamespace = opcform->opcnamespace;
1153 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1154 !list_member_oid(activeSearchPath, opcnamespace))
1159 * If it is in the path, it might still not be visible; it could be
1160 * hidden by another opclass of the same name earlier in the path. So
1161 * we must do a slow check to see if this opclass would be found by
1162 * OpclassnameGetOpcid.
1164 char *opcname = NameStr(opcform->opcname);
1166 visible = (OpclassnameGetOpcid(opcform->opcmethod, opcname) == opcid);
1169 ReleaseSysCache(opctup);
1175 * OpfamilynameGetOpfid
1176 * Try to resolve an unqualified index opfamily name.
1177 * Returns OID if opfamily found in search path, else InvalidOid.
1179 * This is essentially the same as TypenameGetTypid, but we have to have
1180 * an extra argument for the index AM OID.
1183 OpfamilynameGetOpfid(Oid amid, const char *opfname)
1188 recomputeNamespacePath();
1190 foreach(l, activeSearchPath)
1192 Oid namespaceId = lfirst_oid(l);
1194 if (namespaceId == myTempNamespace)
1195 continue; /* do not look in temp namespace */
1197 opfid = GetSysCacheOid(OPFAMILYAMNAMENSP,
1198 ObjectIdGetDatum(amid),
1199 PointerGetDatum(opfname),
1200 ObjectIdGetDatum(namespaceId),
1202 if (OidIsValid(opfid))
1206 /* Not found in path */
1212 * Determine whether an opfamily (identified by OID) is visible in the
1213 * current search path. Visible means "would be found by searching
1214 * for the unqualified opfamily name".
1217 OpfamilyIsVisible(Oid opfid)
1220 Form_pg_opfamily opfform;
1224 opftup = SearchSysCache(OPFAMILYOID,
1225 ObjectIdGetDatum(opfid),
1227 if (!HeapTupleIsValid(opftup))
1228 elog(ERROR, "cache lookup failed for opfamily %u", opfid);
1229 opfform = (Form_pg_opfamily) GETSTRUCT(opftup);
1231 recomputeNamespacePath();
1234 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1235 * the system namespace are surely in the path and so we needn't even do
1236 * list_member_oid() for them.
1238 opfnamespace = opfform->opfnamespace;
1239 if (opfnamespace != PG_CATALOG_NAMESPACE &&
1240 !list_member_oid(activeSearchPath, opfnamespace))
1245 * If it is in the path, it might still not be visible; it could be
1246 * hidden by another opfamily of the same name earlier in the path. So
1247 * we must do a slow check to see if this opfamily would be found by
1248 * OpfamilynameGetOpfid.
1250 char *opfname = NameStr(opfform->opfname);
1252 visible = (OpfamilynameGetOpfid(opfform->opfmethod, opfname) == opfid);
1255 ReleaseSysCache(opftup);
1261 * ConversionGetConid
1262 * Try to resolve an unqualified conversion name.
1263 * Returns OID if conversion found in search path, else InvalidOid.
1265 * This is essentially the same as RelnameGetRelid.
1268 ConversionGetConid(const char *conname)
1273 recomputeNamespacePath();
1275 foreach(l, activeSearchPath)
1277 Oid namespaceId = lfirst_oid(l);
1279 if (namespaceId == myTempNamespace)
1280 continue; /* do not look in temp namespace */
1282 conid = GetSysCacheOid(CONNAMENSP,
1283 PointerGetDatum(conname),
1284 ObjectIdGetDatum(namespaceId),
1286 if (OidIsValid(conid))
1290 /* Not found in path */
1295 * ConversionIsVisible
1296 * Determine whether a conversion (identified by OID) is visible in the
1297 * current search path. Visible means "would be found by searching
1298 * for the unqualified conversion name".
1301 ConversionIsVisible(Oid conid)
1304 Form_pg_conversion conform;
1308 contup = SearchSysCache(CONVOID,
1309 ObjectIdGetDatum(conid),
1311 if (!HeapTupleIsValid(contup))
1312 elog(ERROR, "cache lookup failed for conversion %u", conid);
1313 conform = (Form_pg_conversion) GETSTRUCT(contup);
1315 recomputeNamespacePath();
1318 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1319 * the system namespace are surely in the path and so we needn't even do
1320 * list_member_oid() for them.
1322 connamespace = conform->connamespace;
1323 if (connamespace != PG_CATALOG_NAMESPACE &&
1324 !list_member_oid(activeSearchPath, connamespace))
1329 * If it is in the path, it might still not be visible; it could be
1330 * hidden by another conversion of the same name earlier in the path.
1331 * So we must do a slow check to see if this conversion would be found
1332 * by ConversionGetConid.
1334 char *conname = NameStr(conform->conname);
1336 visible = (ConversionGetConid(conname) == conid);
1339 ReleaseSysCache(contup);
1345 * TSParserGetPrsid - find a TS parser by possibly qualified name
1347 * If not found, returns InvalidOid if failOK, else throws error
1350 TSParserGetPrsid(List *names, bool failOK)
1355 Oid prsoid = InvalidOid;
1358 /* deconstruct the name list */
1359 DeconstructQualifiedName(names, &schemaname, &parser_name);
1363 /* use exact schema given */
1364 namespaceId = LookupExplicitNamespace(schemaname);
1365 prsoid = GetSysCacheOid(TSPARSERNAMENSP,
1366 PointerGetDatum(parser_name),
1367 ObjectIdGetDatum(namespaceId),
1372 /* search for it in search path */
1373 recomputeNamespacePath();
1375 foreach(l, activeSearchPath)
1377 namespaceId = lfirst_oid(l);
1379 if (namespaceId == myTempNamespace)
1380 continue; /* do not look in temp namespace */
1382 prsoid = GetSysCacheOid(TSPARSERNAMENSP,
1383 PointerGetDatum(parser_name),
1384 ObjectIdGetDatum(namespaceId),
1386 if (OidIsValid(prsoid))
1391 if (!OidIsValid(prsoid) && !failOK)
1393 (errcode(ERRCODE_UNDEFINED_OBJECT),
1394 errmsg("text search parser \"%s\" does not exist",
1395 NameListToString(names))));
1402 * Determine whether a parser (identified by OID) is visible in the
1403 * current search path. Visible means "would be found by searching
1404 * for the unqualified parser name".
1407 TSParserIsVisible(Oid prsId)
1410 Form_pg_ts_parser form;
1414 tup = SearchSysCache(TSPARSEROID,
1415 ObjectIdGetDatum(prsId),
1417 if (!HeapTupleIsValid(tup))
1418 elog(ERROR, "cache lookup failed for text search parser %u", prsId);
1419 form = (Form_pg_ts_parser) GETSTRUCT(tup);
1421 recomputeNamespacePath();
1424 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1425 * the system namespace are surely in the path and so we needn't even do
1426 * list_member_oid() for them.
1428 namespace = form->prsnamespace;
1429 if (namespace != PG_CATALOG_NAMESPACE &&
1430 !list_member_oid(activeSearchPath, namespace))
1435 * If it is in the path, it might still not be visible; it could be
1436 * hidden by another parser of the same name earlier in the path. So we
1437 * must do a slow check for conflicting parsers.
1439 char *name = NameStr(form->prsname);
1443 foreach(l, activeSearchPath)
1445 Oid namespaceId = lfirst_oid(l);
1447 if (namespaceId == myTempNamespace)
1448 continue; /* do not look in temp namespace */
1450 if (namespaceId == namespace)
1452 /* Found it first in path */
1456 if (SearchSysCacheExists(TSPARSERNAMENSP,
1457 PointerGetDatum(name),
1458 ObjectIdGetDatum(namespaceId),
1461 /* Found something else first in path */
1467 ReleaseSysCache(tup);
1473 * TSDictionaryGetDictid - find a TS dictionary by possibly qualified name
1475 * If not found, returns InvalidOid if failOK, else throws error
1478 TSDictionaryGetDictid(List *names, bool failOK)
1483 Oid dictoid = InvalidOid;
1486 /* deconstruct the name list */
1487 DeconstructQualifiedName(names, &schemaname, &dict_name);
1491 /* use exact schema given */
1492 namespaceId = LookupExplicitNamespace(schemaname);
1493 dictoid = GetSysCacheOid(TSDICTNAMENSP,
1494 PointerGetDatum(dict_name),
1495 ObjectIdGetDatum(namespaceId),
1500 /* search for it in search path */
1501 recomputeNamespacePath();
1503 foreach(l, activeSearchPath)
1505 namespaceId = lfirst_oid(l);
1507 if (namespaceId == myTempNamespace)
1508 continue; /* do not look in temp namespace */
1510 dictoid = GetSysCacheOid(TSDICTNAMENSP,
1511 PointerGetDatum(dict_name),
1512 ObjectIdGetDatum(namespaceId),
1514 if (OidIsValid(dictoid))
1519 if (!OidIsValid(dictoid) && !failOK)
1521 (errcode(ERRCODE_UNDEFINED_OBJECT),
1522 errmsg("text search dictionary \"%s\" does not exist",
1523 NameListToString(names))));
1529 * TSDictionaryIsVisible
1530 * Determine whether a dictionary (identified by OID) is visible in the
1531 * current search path. Visible means "would be found by searching
1532 * for the unqualified dictionary name".
1535 TSDictionaryIsVisible(Oid dictId)
1538 Form_pg_ts_dict form;
1542 tup = SearchSysCache(TSDICTOID,
1543 ObjectIdGetDatum(dictId),
1545 if (!HeapTupleIsValid(tup))
1546 elog(ERROR, "cache lookup failed for text search dictionary %u",
1548 form = (Form_pg_ts_dict) GETSTRUCT(tup);
1550 recomputeNamespacePath();
1553 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1554 * the system namespace are surely in the path and so we needn't even do
1555 * list_member_oid() for them.
1557 namespace = form->dictnamespace;
1558 if (namespace != PG_CATALOG_NAMESPACE &&
1559 !list_member_oid(activeSearchPath, namespace))
1564 * If it is in the path, it might still not be visible; it could be
1565 * hidden by another dictionary of the same name earlier in the
1566 * path. So we must do a slow check for conflicting dictionaries.
1568 char *name = NameStr(form->dictname);
1572 foreach(l, activeSearchPath)
1574 Oid namespaceId = lfirst_oid(l);
1576 if (namespaceId == myTempNamespace)
1577 continue; /* do not look in temp namespace */
1579 if (namespaceId == namespace)
1581 /* Found it first in path */
1585 if (SearchSysCacheExists(TSDICTNAMENSP,
1586 PointerGetDatum(name),
1587 ObjectIdGetDatum(namespaceId),
1590 /* Found something else first in path */
1596 ReleaseSysCache(tup);
1602 * TSTemplateGetTmplid - find a TS template by possibly qualified name
1604 * If not found, returns InvalidOid if failOK, else throws error
1607 TSTemplateGetTmplid(List *names, bool failOK)
1610 char *template_name;
1612 Oid tmploid = InvalidOid;
1615 /* deconstruct the name list */
1616 DeconstructQualifiedName(names, &schemaname, &template_name);
1620 /* use exact schema given */
1621 namespaceId = LookupExplicitNamespace(schemaname);
1622 tmploid = GetSysCacheOid(TSTEMPLATENAMENSP,
1623 PointerGetDatum(template_name),
1624 ObjectIdGetDatum(namespaceId),
1629 /* search for it in search path */
1630 recomputeNamespacePath();
1632 foreach(l, activeSearchPath)
1634 namespaceId = lfirst_oid(l);
1636 if (namespaceId == myTempNamespace)
1637 continue; /* do not look in temp namespace */
1639 tmploid = GetSysCacheOid(TSTEMPLATENAMENSP,
1640 PointerGetDatum(template_name),
1641 ObjectIdGetDatum(namespaceId),
1643 if (OidIsValid(tmploid))
1648 if (!OidIsValid(tmploid) && !failOK)
1650 (errcode(ERRCODE_UNDEFINED_OBJECT),
1651 errmsg("text search template \"%s\" does not exist",
1652 NameListToString(names))));
1658 * TSTemplateIsVisible
1659 * Determine whether a template (identified by OID) is visible in the
1660 * current search path. Visible means "would be found by searching
1661 * for the unqualified template name".
1664 TSTemplateIsVisible(Oid tmplId)
1667 Form_pg_ts_template form;
1671 tup = SearchSysCache(TSTEMPLATEOID,
1672 ObjectIdGetDatum(tmplId),
1674 if (!HeapTupleIsValid(tup))
1675 elog(ERROR, "cache lookup failed for text search template %u", tmplId);
1676 form = (Form_pg_ts_template) GETSTRUCT(tup);
1678 recomputeNamespacePath();
1681 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1682 * the system namespace are surely in the path and so we needn't even do
1683 * list_member_oid() for them.
1685 namespace = form->tmplnamespace;
1686 if (namespace != PG_CATALOG_NAMESPACE &&
1687 !list_member_oid(activeSearchPath, namespace))
1692 * If it is in the path, it might still not be visible; it could be
1693 * hidden by another template of the same name earlier in the path.
1694 * So we must do a slow check for conflicting templates.
1696 char *name = NameStr(form->tmplname);
1700 foreach(l, activeSearchPath)
1702 Oid namespaceId = lfirst_oid(l);
1704 if (namespaceId == myTempNamespace)
1705 continue; /* do not look in temp namespace */
1707 if (namespaceId == namespace)
1709 /* Found it first in path */
1713 if (SearchSysCacheExists(TSTEMPLATENAMENSP,
1714 PointerGetDatum(name),
1715 ObjectIdGetDatum(namespaceId),
1718 /* Found something else first in path */
1724 ReleaseSysCache(tup);
1730 * TSConfigGetCfgid - find a TS config by possibly qualified name
1732 * If not found, returns InvalidOid if failOK, else throws error
1735 TSConfigGetCfgid(List *names, bool failOK)
1740 Oid cfgoid = InvalidOid;
1743 /* deconstruct the name list */
1744 DeconstructQualifiedName(names, &schemaname, &config_name);
1748 /* use exact schema given */
1749 namespaceId = LookupExplicitNamespace(schemaname);
1750 cfgoid = GetSysCacheOid(TSCONFIGNAMENSP,
1751 PointerGetDatum(config_name),
1752 ObjectIdGetDatum(namespaceId),
1757 /* search for it in search path */
1758 recomputeNamespacePath();
1760 foreach(l, activeSearchPath)
1762 namespaceId = lfirst_oid(l);
1764 if (namespaceId == myTempNamespace)
1765 continue; /* do not look in temp namespace */
1767 cfgoid = GetSysCacheOid(TSCONFIGNAMENSP,
1768 PointerGetDatum(config_name),
1769 ObjectIdGetDatum(namespaceId),
1771 if (OidIsValid(cfgoid))
1776 if (!OidIsValid(cfgoid) && !failOK)
1778 (errcode(ERRCODE_UNDEFINED_OBJECT),
1779 errmsg("text search configuration \"%s\" does not exist",
1780 NameListToString(names))));
1787 * Determine whether a text search configuration (identified by OID)
1788 * is visible in the current search path. Visible means "would be found
1789 * by searching for the unqualified text search configuration name".
1792 TSConfigIsVisible(Oid cfgid)
1795 Form_pg_ts_config form;
1799 tup = SearchSysCache(TSCONFIGOID,
1800 ObjectIdGetDatum(cfgid),
1802 if (!HeapTupleIsValid(tup))
1803 elog(ERROR, "cache lookup failed for text search configuration %u",
1805 form = (Form_pg_ts_config) GETSTRUCT(tup);
1807 recomputeNamespacePath();
1810 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1811 * the system namespace are surely in the path and so we needn't even do
1812 * list_member_oid() for them.
1814 namespace = form->cfgnamespace;
1815 if (namespace != PG_CATALOG_NAMESPACE &&
1816 !list_member_oid(activeSearchPath, namespace))
1821 * If it is in the path, it might still not be visible; it could be
1822 * hidden by another configuration of the same name earlier in the
1823 * path. So we must do a slow check for conflicting configurations.
1825 char *name = NameStr(form->cfgname);
1829 foreach(l, activeSearchPath)
1831 Oid namespaceId = lfirst_oid(l);
1833 if (namespaceId == myTempNamespace)
1834 continue; /* do not look in temp namespace */
1836 if (namespaceId == namespace)
1838 /* Found it first in path */
1842 if (SearchSysCacheExists(TSCONFIGNAMENSP,
1843 PointerGetDatum(name),
1844 ObjectIdGetDatum(namespaceId),
1847 /* Found something else first in path */
1853 ReleaseSysCache(tup);
1860 * DeconstructQualifiedName
1861 * Given a possibly-qualified name expressed as a list of String nodes,
1862 * extract the schema name and object name.
1864 * *nspname_p is set to NULL if there is no explicit schema name.
1867 DeconstructQualifiedName(List *names,
1872 char *schemaname = NULL;
1873 char *objname = NULL;
1875 switch (list_length(names))
1878 objname = strVal(linitial(names));
1881 schemaname = strVal(linitial(names));
1882 objname = strVal(lsecond(names));
1885 catalogname = strVal(linitial(names));
1886 schemaname = strVal(lsecond(names));
1887 objname = strVal(lthird(names));
1890 * We check the catalog name and then ignore it.
1892 if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
1894 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1895 errmsg("cross-database references are not implemented: %s",
1896 NameListToString(names))));
1900 (errcode(ERRCODE_SYNTAX_ERROR),
1901 errmsg("improper qualified name (too many dotted names): %s",
1902 NameListToString(names))));
1906 *nspname_p = schemaname;
1907 *objname_p = objname;
1911 * LookupExplicitNamespace
1912 * Process an explicitly-specified schema name: look up the schema
1913 * and verify we have USAGE (lookup) rights in it.
1915 * Returns the namespace OID. Raises ereport if any problem.
1918 LookupExplicitNamespace(const char *nspname)
1921 AclResult aclresult;
1923 /* check for pg_temp alias */
1924 if (strcmp(nspname, "pg_temp") == 0)
1926 if (OidIsValid(myTempNamespace))
1927 return myTempNamespace;
1929 * Since this is used only for looking up existing objects, there
1930 * is no point in trying to initialize the temp namespace here;
1931 * and doing so might create problems for some callers.
1932 * Just fall through and give the "does not exist" error.
1936 namespaceId = GetSysCacheOid(NAMESPACENAME,
1937 CStringGetDatum(nspname),
1939 if (!OidIsValid(namespaceId))
1941 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1942 errmsg("schema \"%s\" does not exist", nspname)));
1944 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
1945 if (aclresult != ACLCHECK_OK)
1946 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
1953 * LookupCreationNamespace
1954 * Look up the schema and verify we have CREATE rights on it.
1956 * This is just like LookupExplicitNamespace except for the permission check,
1957 * and that we are willing to create pg_temp if needed.
1959 * Note: calling this may result in a CommandCounterIncrement operation,
1960 * if we have to create or clean out the temp namespace.
1963 LookupCreationNamespace(const char *nspname)
1966 AclResult aclresult;
1968 /* check for pg_temp alias */
1969 if (strcmp(nspname, "pg_temp") == 0)
1971 /* Initialize temp namespace if first time through */
1972 if (!OidIsValid(myTempNamespace))
1973 InitTempTableNamespace();
1974 return myTempNamespace;
1977 namespaceId = GetSysCacheOid(NAMESPACENAME,
1978 CStringGetDatum(nspname),
1980 if (!OidIsValid(namespaceId))
1982 (errcode(ERRCODE_UNDEFINED_SCHEMA),
1983 errmsg("schema \"%s\" does not exist", nspname)));
1985 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
1986 if (aclresult != ACLCHECK_OK)
1987 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
1994 * QualifiedNameGetCreationNamespace
1995 * Given a possibly-qualified name for an object (in List-of-Values
1996 * format), determine what namespace the object should be created in.
1997 * Also extract and return the object name (last component of list).
1999 * Note: this does not apply any permissions check. Callers must check
2000 * for CREATE rights on the selected namespace when appropriate.
2002 * Note: calling this may result in a CommandCounterIncrement operation,
2003 * if we have to create or clean out the temp namespace.
2006 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
2011 /* deconstruct the name list */
2012 DeconstructQualifiedName(names, &schemaname, objname_p);
2016 /* check for pg_temp alias */
2017 if (strcmp(schemaname, "pg_temp") == 0)
2019 /* Initialize temp namespace if first time through */
2020 if (!OidIsValid(myTempNamespace))
2021 InitTempTableNamespace();
2022 return myTempNamespace;
2024 /* use exact schema given */
2025 namespaceId = GetSysCacheOid(NAMESPACENAME,
2026 CStringGetDatum(schemaname),
2028 if (!OidIsValid(namespaceId))
2030 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2031 errmsg("schema \"%s\" does not exist", schemaname)));
2032 /* we do not check for USAGE rights here! */
2036 /* use the default creation namespace */
2037 recomputeNamespacePath();
2038 if (activeTempCreationPending)
2040 /* Need to initialize temp namespace */
2041 InitTempTableNamespace();
2042 return myTempNamespace;
2044 namespaceId = activeCreationNamespace;
2045 if (!OidIsValid(namespaceId))
2047 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2048 errmsg("no schema has been selected to create in")));
2055 * makeRangeVarFromNameList
2056 * Utility routine to convert a qualified-name list into RangeVar form.
2059 makeRangeVarFromNameList(List *names)
2061 RangeVar *rel = makeRangeVar(NULL, NULL);
2063 switch (list_length(names))
2066 rel->relname = strVal(linitial(names));
2069 rel->schemaname = strVal(linitial(names));
2070 rel->relname = strVal(lsecond(names));
2073 rel->catalogname = strVal(linitial(names));
2074 rel->schemaname = strVal(lsecond(names));
2075 rel->relname = strVal(lthird(names));
2079 (errcode(ERRCODE_SYNTAX_ERROR),
2080 errmsg("improper relation name (too many dotted names): %s",
2081 NameListToString(names))));
2090 * Utility routine to convert a qualified-name list into a string.
2092 * This is used primarily to form error messages, and so we do not quote
2093 * the list elements, for the sake of legibility.
2096 NameListToString(List *names)
2098 StringInfoData string;
2101 initStringInfo(&string);
2105 if (l != list_head(names))
2106 appendStringInfoChar(&string, '.');
2107 appendStringInfoString(&string, strVal(lfirst(l)));
2114 * NameListToQuotedString
2115 * Utility routine to convert a qualified-name list into a string.
2117 * Same as above except that names will be double-quoted where necessary,
2118 * so the string could be re-parsed (eg, by textToQualifiedNameList).
2121 NameListToQuotedString(List *names)
2123 StringInfoData string;
2126 initStringInfo(&string);
2130 if (l != list_head(names))
2131 appendStringInfoChar(&string, '.');
2132 appendStringInfoString(&string, quote_identifier(strVal(lfirst(l))));
2139 * isTempNamespace - is the given namespace my temporary-table namespace?
2142 isTempNamespace(Oid namespaceId)
2144 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
2150 * isTempToastNamespace - is the given namespace my temporary-toast-table
2154 isTempToastNamespace(Oid namespaceId)
2156 if (OidIsValid(myTempToastNamespace) && myTempToastNamespace == namespaceId)
2162 * isTempOrToastNamespace - is the given namespace my temporary-table
2163 * namespace or my temporary-toast-table namespace?
2166 isTempOrToastNamespace(Oid namespaceId)
2168 if (OidIsValid(myTempNamespace) &&
2169 (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId))
2175 * isAnyTempNamespace - is the given namespace a temporary-table namespace
2176 * (either my own, or another backend's)? Temporary-toast-table namespaces
2177 * are included, too.
2180 isAnyTempNamespace(Oid namespaceId)
2185 /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
2186 nspname = get_namespace_name(namespaceId);
2188 return false; /* no such namespace? */
2189 result = (strncmp(nspname, "pg_temp_", 8) == 0) ||
2190 (strncmp(nspname, "pg_toast_temp_", 14) == 0);
2196 * isOtherTempNamespace - is the given namespace some other backend's
2197 * temporary-table namespace (including temporary-toast-table namespaces)?
2200 isOtherTempNamespace(Oid namespaceId)
2202 /* If it's my own temp namespace, say "false" */
2203 if (isTempOrToastNamespace(namespaceId))
2205 /* Else, if it's any temp namespace, say "true" */
2206 return isAnyTempNamespace(namespaceId);
2210 * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
2211 * which must already be assigned. (This is only used when creating a toast
2212 * table for a temp table, so we must have already done InitTempTableNamespace)
2215 GetTempToastNamespace(void)
2217 Assert(OidIsValid(myTempToastNamespace));
2218 return myTempToastNamespace;
2223 * GetOverrideSearchPath - fetch current search path definition in form
2224 * used by PushOverrideSearchPath.
2226 * The result structure is allocated in the specified memory context
2227 * (which might or might not be equal to CurrentMemoryContext); but any
2228 * junk created by revalidation calculations will be in CurrentMemoryContext.
2230 OverrideSearchPath *
2231 GetOverrideSearchPath(MemoryContext context)
2233 OverrideSearchPath *result;
2235 MemoryContext oldcxt;
2237 recomputeNamespacePath();
2239 oldcxt = MemoryContextSwitchTo(context);
2241 result = (OverrideSearchPath *) palloc0(sizeof(OverrideSearchPath));
2242 schemas = list_copy(activeSearchPath);
2243 while (schemas && linitial_oid(schemas) != activeCreationNamespace)
2245 if (linitial_oid(schemas) == myTempNamespace)
2246 result->addTemp = true;
2249 Assert(linitial_oid(schemas) == PG_CATALOG_NAMESPACE);
2250 result->addCatalog = true;
2252 schemas = list_delete_first(schemas);
2254 result->schemas = schemas;
2256 MemoryContextSwitchTo(oldcxt);
2262 * PushOverrideSearchPath - temporarily override the search path
2264 * We allow nested overrides, hence the push/pop terminology. The GUC
2265 * search_path variable is ignored while an override is active.
2268 PushOverrideSearchPath(OverrideSearchPath *newpath)
2270 OverrideStackEntry *entry;
2273 MemoryContext oldcxt;
2276 * Copy the list for safekeeping, and insert implicitly-searched
2277 * namespaces as needed. This code should track recomputeNamespacePath.
2279 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
2281 oidlist = list_copy(newpath->schemas);
2284 * Remember the first member of the explicit list.
2287 firstNS = InvalidOid;
2289 firstNS = linitial_oid(oidlist);
2292 * Add any implicitly-searched namespaces to the list. Note these go on
2293 * the front, not the back; also notice that we do not check USAGE
2294 * permissions for these.
2296 if (newpath->addCatalog)
2297 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
2299 if (newpath->addTemp)
2301 Assert(OidIsValid(myTempNamespace));
2302 oidlist = lcons_oid(myTempNamespace, oidlist);
2306 * Build the new stack entry, then insert it at the head of the list.
2308 entry = (OverrideStackEntry *) palloc(sizeof(OverrideStackEntry));
2309 entry->searchPath = oidlist;
2310 entry->creationNamespace = firstNS;
2311 entry->nestLevel = GetCurrentTransactionNestLevel();
2313 overrideStack = lcons(entry, overrideStack);
2315 /* And make it active. */
2316 activeSearchPath = entry->searchPath;
2317 activeCreationNamespace = entry->creationNamespace;
2318 activeTempCreationPending = false; /* XXX is this OK? */
2320 MemoryContextSwitchTo(oldcxt);
2324 * PopOverrideSearchPath - undo a previous PushOverrideSearchPath
2326 * Any push during a (sub)transaction will be popped automatically at abort.
2327 * But it's caller error if a push isn't popped in normal control flow.
2330 PopOverrideSearchPath(void)
2332 OverrideStackEntry *entry;
2334 /* Sanity checks. */
2335 if (overrideStack == NIL)
2336 elog(ERROR, "bogus PopOverrideSearchPath call");
2337 entry = (OverrideStackEntry *) linitial(overrideStack);
2338 if (entry->nestLevel != GetCurrentTransactionNestLevel())
2339 elog(ERROR, "bogus PopOverrideSearchPath call");
2341 /* Pop the stack and free storage. */
2342 overrideStack = list_delete_first(overrideStack);
2343 list_free(entry->searchPath);
2346 /* Activate the next level down. */
2349 entry = (OverrideStackEntry *) linitial(overrideStack);
2350 activeSearchPath = entry->searchPath;
2351 activeCreationNamespace = entry->creationNamespace;
2352 activeTempCreationPending = false; /* XXX is this OK? */
2356 /* If not baseSearchPathValid, this is useless but harmless */
2357 activeSearchPath = baseSearchPath;
2358 activeCreationNamespace = baseCreationNamespace;
2359 activeTempCreationPending = baseTempCreationPending;
2365 * FindConversionByName - find a conversion by possibly qualified name
2368 FindConversionByName(List *name)
2371 char *conversion_name;
2376 /* deconstruct the name list */
2377 DeconstructQualifiedName(name, &schemaname, &conversion_name);
2381 /* use exact schema given */
2382 namespaceId = LookupExplicitNamespace(schemaname);
2383 return FindConversion(conversion_name, namespaceId);
2387 /* search for it in search path */
2388 recomputeNamespacePath();
2390 foreach(l, activeSearchPath)
2392 namespaceId = lfirst_oid(l);
2394 if (namespaceId == myTempNamespace)
2395 continue; /* do not look in temp namespace */
2397 conoid = FindConversion(conversion_name, namespaceId);
2398 if (OidIsValid(conoid))
2403 /* Not found in path */
2408 * FindDefaultConversionProc - find default encoding conversion proc
2411 FindDefaultConversionProc(int4 for_encoding, int4 to_encoding)
2416 recomputeNamespacePath();
2418 foreach(l, activeSearchPath)
2420 Oid namespaceId = lfirst_oid(l);
2422 if (namespaceId == myTempNamespace)
2423 continue; /* do not look in temp namespace */
2425 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
2426 if (OidIsValid(proc))
2430 /* Not found in path */
2435 * recomputeNamespacePath - recompute path derived variables if needed.
2438 recomputeNamespacePath(void)
2440 Oid roleid = GetUserId();
2448 MemoryContext oldcxt;
2450 /* Do nothing if an override search spec is active. */
2454 /* Do nothing if path is already valid. */
2455 if (baseSearchPathValid && namespaceUser == roleid)
2458 /* Need a modifiable copy of namespace_search_path string */
2459 rawname = pstrdup(namespace_search_path);
2461 /* Parse string into list of identifiers */
2462 if (!SplitIdentifierString(rawname, ',', &namelist))
2464 /* syntax error in name list */
2465 /* this should not happen if GUC checked check_search_path */
2466 elog(ERROR, "invalid list syntax");
2470 * Convert the list of names to a list of OIDs. If any names are not
2471 * recognizable or we don't have read access, just leave them out of the
2472 * list. (We can't raise an error, since the search_path setting has
2473 * already been accepted.) Don't make duplicate entries, either.
2476 temp_missing = false;
2477 foreach(l, namelist)
2479 char *curname = (char *) lfirst(l);
2482 if (strcmp(curname, "$user") == 0)
2484 /* $user --- substitute namespace matching user name, if any */
2487 tuple = SearchSysCache(AUTHOID,
2488 ObjectIdGetDatum(roleid),
2490 if (HeapTupleIsValid(tuple))
2494 rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
2495 namespaceId = GetSysCacheOid(NAMESPACENAME,
2496 CStringGetDatum(rname),
2498 ReleaseSysCache(tuple);
2499 if (OidIsValid(namespaceId) &&
2500 !list_member_oid(oidlist, namespaceId) &&
2501 pg_namespace_aclcheck(namespaceId, roleid,
2502 ACL_USAGE) == ACLCHECK_OK)
2503 oidlist = lappend_oid(oidlist, namespaceId);
2506 else if (strcmp(curname, "pg_temp") == 0)
2508 /* pg_temp --- substitute temp namespace, if any */
2509 if (OidIsValid(myTempNamespace))
2511 if (!list_member_oid(oidlist, myTempNamespace))
2512 oidlist = lappend_oid(oidlist, myTempNamespace);
2516 /* If it ought to be the creation namespace, set flag */
2518 temp_missing = true;
2523 /* normal namespace reference */
2524 namespaceId = GetSysCacheOid(NAMESPACENAME,
2525 CStringGetDatum(curname),
2527 if (OidIsValid(namespaceId) &&
2528 !list_member_oid(oidlist, namespaceId) &&
2529 pg_namespace_aclcheck(namespaceId, roleid,
2530 ACL_USAGE) == ACLCHECK_OK)
2531 oidlist = lappend_oid(oidlist, namespaceId);
2536 * Remember the first member of the explicit list. (Note: this is
2537 * nominally wrong if temp_missing, but we need it anyway to distinguish
2538 * explicit from implicit mention of pg_catalog.)
2541 firstNS = InvalidOid;
2543 firstNS = linitial_oid(oidlist);
2546 * Add any implicitly-searched namespaces to the list. Note these go on
2547 * the front, not the back; also notice that we do not check USAGE
2548 * permissions for these.
2550 if (!list_member_oid(oidlist, PG_CATALOG_NAMESPACE))
2551 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
2553 if (OidIsValid(myTempNamespace) &&
2554 !list_member_oid(oidlist, myTempNamespace))
2555 oidlist = lcons_oid(myTempNamespace, oidlist);
2558 * Now that we've successfully built the new list of namespace OIDs, save
2559 * it in permanent storage.
2561 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
2562 newpath = list_copy(oidlist);
2563 MemoryContextSwitchTo(oldcxt);
2565 /* Now safe to assign to state variables. */
2566 list_free(baseSearchPath);
2567 baseSearchPath = newpath;
2568 baseCreationNamespace = firstNS;
2569 baseTempCreationPending = temp_missing;
2571 /* Mark the path valid. */
2572 baseSearchPathValid = true;
2573 namespaceUser = roleid;
2575 /* And make it active. */
2576 activeSearchPath = baseSearchPath;
2577 activeCreationNamespace = baseCreationNamespace;
2578 activeTempCreationPending = baseTempCreationPending;
2582 list_free(namelist);
2587 * InitTempTableNamespace
2588 * Initialize temp table namespace on first use in a particular backend
2591 InitTempTableNamespace(void)
2593 char namespaceName[NAMEDATALEN];
2597 Assert(!OidIsValid(myTempNamespace));
2600 * First, do permission check to see if we are authorized to make temp
2601 * tables. We use a nonstandard error message here since "databasename:
2602 * permission denied" might be a tad cryptic.
2604 * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
2605 * that's necessary since current user ID could change during the session.
2606 * But there's no need to make the namespace in the first place until a
2607 * temp table creation request is made by someone with appropriate rights.
2609 if (pg_database_aclcheck(MyDatabaseId, GetUserId(),
2610 ACL_CREATE_TEMP) != ACLCHECK_OK)
2612 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2613 errmsg("permission denied to create temporary tables in database \"%s\"",
2614 get_database_name(MyDatabaseId))));
2616 snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
2618 namespaceId = GetSysCacheOid(NAMESPACENAME,
2619 CStringGetDatum(namespaceName),
2621 if (!OidIsValid(namespaceId))
2624 * First use of this temp namespace in this database; create it. The
2625 * temp namespaces are always owned by the superuser. We leave their
2626 * permissions at default --- i.e., no access except to superuser ---
2627 * to ensure that unprivileged users can't peek at other backends'
2628 * temp tables. This works because the places that access the temp
2629 * namespace for my own backend skip permissions checks on it.
2631 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID);
2632 /* Advance command counter to make namespace visible */
2633 CommandCounterIncrement();
2638 * If the namespace already exists, clean it out (in case the former
2639 * owner crashed without doing so).
2641 RemoveTempRelations(namespaceId);
2645 * If the corresponding temp-table namespace doesn't exist yet, create it.
2646 * (We assume there is no need to clean it out if it does exist, since
2647 * dropping a parent table should make its toast table go away.)
2649 snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
2652 toastspaceId = GetSysCacheOid(NAMESPACENAME,
2653 CStringGetDatum(namespaceName),
2655 if (!OidIsValid(toastspaceId))
2657 toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID);
2658 /* Advance command counter to make namespace visible */
2659 CommandCounterIncrement();
2663 * Okay, we've prepared the temp namespace ... but it's not committed yet,
2664 * so all our work could be undone by transaction rollback. Set flag for
2665 * AtEOXact_Namespace to know what to do.
2667 myTempNamespace = namespaceId;
2668 myTempToastNamespace = toastspaceId;
2670 /* It should not be done already. */
2671 AssertState(myTempNamespaceSubID == InvalidSubTransactionId);
2672 myTempNamespaceSubID = GetCurrentSubTransactionId();
2674 baseSearchPathValid = false; /* need to rebuild list */
2678 * End-of-transaction cleanup for namespaces.
2681 AtEOXact_Namespace(bool isCommit)
2684 * If we abort the transaction in which a temp namespace was selected,
2685 * we'll have to do any creation or cleanout work over again. So, just
2686 * forget the namespace entirely until next time. On the other hand, if
2687 * we commit then register an exit callback to clean out the temp tables
2688 * at backend shutdown. (We only want to register the callback once per
2689 * session, so this is a good place to do it.)
2691 if (myTempNamespaceSubID != InvalidSubTransactionId)
2694 on_shmem_exit(RemoveTempRelationsCallback, 0);
2697 myTempNamespace = InvalidOid;
2698 myTempToastNamespace = InvalidOid;
2699 baseSearchPathValid = false; /* need to rebuild list */
2701 myTempNamespaceSubID = InvalidSubTransactionId;
2705 * Clean up if someone failed to do PopOverrideSearchPath
2710 elog(WARNING, "leaked override search path");
2711 while (overrideStack)
2713 OverrideStackEntry *entry;
2715 entry = (OverrideStackEntry *) linitial(overrideStack);
2716 overrideStack = list_delete_first(overrideStack);
2717 list_free(entry->searchPath);
2720 /* If not baseSearchPathValid, this is useless but harmless */
2721 activeSearchPath = baseSearchPath;
2722 activeCreationNamespace = baseCreationNamespace;
2723 activeTempCreationPending = baseTempCreationPending;
2728 * AtEOSubXact_Namespace
2730 * At subtransaction commit, propagate the temp-namespace-creation
2731 * flag to the parent subtransaction.
2733 * At subtransaction abort, forget the flag if we set it up.
2736 AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid,
2737 SubTransactionId parentSubid)
2739 OverrideStackEntry *entry;
2741 if (myTempNamespaceSubID == mySubid)
2744 myTempNamespaceSubID = parentSubid;
2747 myTempNamespaceSubID = InvalidSubTransactionId;
2748 /* TEMP namespace creation failed, so reset state */
2749 myTempNamespace = InvalidOid;
2750 myTempToastNamespace = InvalidOid;
2751 baseSearchPathValid = false; /* need to rebuild list */
2756 * Clean up if someone failed to do PopOverrideSearchPath
2758 while (overrideStack)
2760 entry = (OverrideStackEntry *) linitial(overrideStack);
2761 if (entry->nestLevel < GetCurrentTransactionNestLevel())
2764 elog(WARNING, "leaked override search path");
2765 overrideStack = list_delete_first(overrideStack);
2766 list_free(entry->searchPath);
2770 /* Activate the next level down. */
2773 entry = (OverrideStackEntry *) linitial(overrideStack);
2774 activeSearchPath = entry->searchPath;
2775 activeCreationNamespace = entry->creationNamespace;
2776 activeTempCreationPending = false; /* XXX is this OK? */
2780 /* If not baseSearchPathValid, this is useless but harmless */
2781 activeSearchPath = baseSearchPath;
2782 activeCreationNamespace = baseCreationNamespace;
2783 activeTempCreationPending = baseTempCreationPending;
2788 * Remove all relations in the specified temp namespace.
2790 * This is called at backend shutdown (if we made any temp relations).
2791 * It is also called when we begin using a pre-existing temp namespace,
2792 * in order to clean out any relations that might have been created by
2793 * a crashed backend.
2796 RemoveTempRelations(Oid tempNamespaceId)
2798 ObjectAddress object;
2801 * We want to get rid of everything in the target namespace, but not the
2802 * namespace itself (deleting it only to recreate it later would be a
2803 * waste of cycles). We do this by finding everything that has a
2804 * dependency on the namespace.
2806 object.classId = NamespaceRelationId;
2807 object.objectId = tempNamespaceId;
2808 object.objectSubId = 0;
2810 deleteWhatDependsOn(&object, false);
2814 * Callback to remove temp relations at backend exit.
2817 RemoveTempRelationsCallback(int code, Datum arg)
2819 if (OidIsValid(myTempNamespace)) /* should always be true */
2821 /* Need to ensure we have a usable transaction. */
2822 AbortOutOfAnyTransaction();
2823 StartTransactionCommand();
2825 RemoveTempRelations(myTempNamespace);
2827 CommitTransactionCommand();
2832 * Remove all temp tables from the temporary namespace.
2835 ResetTempTableNamespace(void)
2837 if (OidIsValid(myTempNamespace))
2838 RemoveTempRelations(myTempNamespace);
2843 * Routines for handling the GUC variable 'search_path'.
2846 /* assign_hook: validate new search_path, do extra actions as needed */
2848 assign_search_path(const char *newval, bool doit, GucSource source)
2854 /* Need a modifiable copy of string */
2855 rawname = pstrdup(newval);
2857 /* Parse string into list of identifiers */
2858 if (!SplitIdentifierString(rawname, ',', &namelist))
2860 /* syntax error in name list */
2862 list_free(namelist);
2867 * If we aren't inside a transaction, we cannot do database access so
2868 * cannot verify the individual names. Must accept the list on faith.
2870 if (source >= PGC_S_INTERACTIVE && IsTransactionState())
2873 * Verify that all the names are either valid namespace names or
2874 * "$user" or "pg_temp". We do not require $user to correspond to a
2875 * valid namespace, and pg_temp might not exist yet. We do not check
2876 * for USAGE rights, either; should we?
2878 * When source == PGC_S_TEST, we are checking the argument of an ALTER
2879 * DATABASE SET or ALTER USER SET command. It could be that the
2880 * intended use of the search path is for some other database, so we
2881 * should not error out if it mentions schemas not present in the
2882 * current database. We reduce the message to NOTICE instead.
2884 foreach(l, namelist)
2886 char *curname = (char *) lfirst(l);
2888 if (strcmp(curname, "$user") == 0)
2890 if (strcmp(curname, "pg_temp") == 0)
2892 if (!SearchSysCacheExists(NAMESPACENAME,
2893 CStringGetDatum(curname),
2895 ereport((source == PGC_S_TEST) ? NOTICE : ERROR,
2896 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2897 errmsg("schema \"%s\" does not exist", curname)));
2902 list_free(namelist);
2905 * We mark the path as needing recomputation, but don't do anything until
2906 * it's needed. This avoids trying to do database access during GUC
2910 baseSearchPathValid = false;
2916 * InitializeSearchPath: initialize module during InitPostgres.
2918 * This is called after we are up enough to be able to do catalog lookups.
2921 InitializeSearchPath(void)
2923 if (IsBootstrapProcessingMode())
2926 * In bootstrap mode, the search path must be 'pg_catalog' so that
2927 * tables are created in the proper namespace; ignore the GUC setting.
2929 MemoryContext oldcxt;
2931 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
2932 baseSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
2933 MemoryContextSwitchTo(oldcxt);
2934 baseCreationNamespace = PG_CATALOG_NAMESPACE;
2935 baseTempCreationPending = false;
2936 baseSearchPathValid = true;
2937 namespaceUser = GetUserId();
2938 activeSearchPath = baseSearchPath;
2939 activeCreationNamespace = baseCreationNamespace;
2940 activeTempCreationPending = baseTempCreationPending;
2945 * In normal mode, arrange for a callback on any syscache invalidation
2946 * of pg_namespace rows.
2948 CacheRegisterSyscacheCallback(NAMESPACEOID,
2951 /* Force search path to be recomputed on next use */
2952 baseSearchPathValid = false;
2958 * Syscache inval callback function
2961 NamespaceCallback(Datum arg, Oid relid)
2963 /* Force search path to be recomputed on next use */
2964 baseSearchPathValid = false;
2968 * Fetch the active search path. The return value is a palloc'ed list
2969 * of OIDs; the caller is responsible for freeing this storage as
2972 * The returned list includes the implicitly-prepended namespaces only if
2973 * includeImplicit is true.
2975 * Note: calling this may result in a CommandCounterIncrement operation,
2976 * if we have to create or clean out the temp namespace.
2979 fetch_search_path(bool includeImplicit)
2983 recomputeNamespacePath();
2986 * If the temp namespace should be first, force it to exist. This is
2987 * so that callers can trust the result to reflect the actual default
2988 * creation namespace. It's a bit bogus to do this here, since
2989 * current_schema() is supposedly a stable function without side-effects,
2990 * but the alternatives seem worse.
2992 if (activeTempCreationPending)
2994 InitTempTableNamespace();
2995 recomputeNamespacePath();
2998 result = list_copy(activeSearchPath);
2999 if (!includeImplicit)
3001 while (result && linitial_oid(result) != activeCreationNamespace)
3002 result = list_delete_first(result);
3009 * Export the FooIsVisible functions as SQL-callable functions.
3013 pg_table_is_visible(PG_FUNCTION_ARGS)
3015 Oid oid = PG_GETARG_OID(0);
3017 PG_RETURN_BOOL(RelationIsVisible(oid));
3021 pg_type_is_visible(PG_FUNCTION_ARGS)
3023 Oid oid = PG_GETARG_OID(0);
3025 PG_RETURN_BOOL(TypeIsVisible(oid));
3029 pg_function_is_visible(PG_FUNCTION_ARGS)
3031 Oid oid = PG_GETARG_OID(0);
3033 PG_RETURN_BOOL(FunctionIsVisible(oid));
3037 pg_operator_is_visible(PG_FUNCTION_ARGS)
3039 Oid oid = PG_GETARG_OID(0);
3041 PG_RETURN_BOOL(OperatorIsVisible(oid));
3045 pg_opclass_is_visible(PG_FUNCTION_ARGS)
3047 Oid oid = PG_GETARG_OID(0);
3049 PG_RETURN_BOOL(OpclassIsVisible(oid));
3053 pg_conversion_is_visible(PG_FUNCTION_ARGS)
3055 Oid oid = PG_GETARG_OID(0);
3057 PG_RETURN_BOOL(ConversionIsVisible(oid));
3061 pg_ts_parser_is_visible(PG_FUNCTION_ARGS)
3063 Oid oid = PG_GETARG_OID(0);
3065 PG_RETURN_BOOL(TSParserIsVisible(oid));
3069 pg_ts_dict_is_visible(PG_FUNCTION_ARGS)
3071 Oid oid = PG_GETARG_OID(0);
3073 PG_RETURN_BOOL(TSDictionaryIsVisible(oid));
3077 pg_ts_template_is_visible(PG_FUNCTION_ARGS)
3079 Oid oid = PG_GETARG_OID(0);
3081 PG_RETURN_BOOL(TSTemplateIsVisible(oid));
3085 pg_ts_config_is_visible(PG_FUNCTION_ARGS)
3087 Oid oid = PG_GETARG_OID(0);
3089 PG_RETURN_BOOL(TSConfigIsVisible(oid));
3093 pg_my_temp_schema(PG_FUNCTION_ARGS)
3095 PG_RETURN_OID(myTempNamespace);
3099 pg_is_other_temp_schema(PG_FUNCTION_ARGS)
3101 Oid oid = PG_GETARG_OID(0);
3103 PG_RETURN_BOOL(isOtherTempNamespace(oid));