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-2009, 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.117 2009/03/31 22:12:46 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_conversion_fn.h"
28 #include "catalog/pg_namespace.h"
29 #include "catalog/pg_opclass.h"
30 #include "catalog/pg_operator.h"
31 #include "catalog/pg_opfamily.h"
32 #include "catalog/pg_proc.h"
33 #include "catalog/pg_ts_config.h"
34 #include "catalog/pg_ts_dict.h"
35 #include "catalog/pg_ts_parser.h"
36 #include "catalog/pg_ts_template.h"
37 #include "catalog/pg_type.h"
38 #include "commands/dbcommands.h"
39 #include "miscadmin.h"
40 #include "nodes/makefuncs.h"
41 #include "parser/parse_func.h"
42 #include "storage/backendid.h"
43 #include "storage/ipc.h"
44 #include "utils/acl.h"
45 #include "utils/builtins.h"
46 #include "utils/guc.h"
47 #include "utils/inval.h"
48 #include "utils/lsyscache.h"
49 #include "utils/memutils.h"
50 #include "utils/rel.h"
51 #include "utils/syscache.h"
55 * The namespace search path is a possibly-empty list of namespace OIDs.
56 * In addition to the explicit list, implicitly-searched namespaces
59 * 1. If a TEMP table namespace has been initialized in this session, it
60 * is implicitly searched first. (The only time this doesn't happen is
61 * when we are obeying an override search path spec that says not to use the
62 * temp namespace, or the temp namespace is included in the explicit list.)
64 * 2. The system catalog namespace is always searched. If the system
65 * namespace is present in the explicit path then it will be searched in
66 * the specified order; otherwise it will be searched after TEMP tables and
67 * *before* the explicit list. (It might seem that the system namespace
68 * should be implicitly last, but this behavior appears to be required by
69 * SQL99. Also, this provides a way to search the system namespace first
70 * without thereby making it the default creation target namespace.)
72 * For security reasons, searches using the search path will ignore the temp
73 * namespace when searching for any object type other than relations and
74 * types. (We must allow types since temp tables have rowtypes.)
76 * The default creation target namespace is always the first element of the
77 * explicit list. If the explicit list is empty, there is no default target.
79 * The textual specification of search_path can include "$user" to refer to
80 * the namespace named the same as the current user, if any. (This is just
81 * ignored if there is no such namespace.) Also, it can include "pg_temp"
82 * to refer to the current backend's temp namespace. This is usually also
83 * ignorable if the temp namespace hasn't been set up, but there's a special
84 * case: if "pg_temp" appears first then it should be the default creation
85 * target. We kluge this case a little bit so that the temp namespace isn't
86 * set up until the first attempt to create something in it. (The reason for
87 * klugery is that we can't create the temp namespace outside a transaction,
88 * but initial GUC processing of search_path happens outside a transaction.)
89 * activeTempCreationPending is TRUE if "pg_temp" appears first in the string
90 * but is not reflected in activeCreationNamespace because the namespace isn't
93 * In bootstrap mode, the search path is set equal to "pg_catalog", so that
94 * the system namespace is the only one searched or inserted into.
95 * initdb is also careful to set search_path to "pg_catalog" for its
96 * post-bootstrap standalone backend runs. Otherwise the default search
97 * path is determined by GUC. The factory default path contains the PUBLIC
98 * namespace (if it exists), preceded by the user's personal namespace
101 * We support a stack of "override" search path settings for use within
102 * specific sections of backend code. namespace_search_path is ignored
103 * whenever the override stack is nonempty. activeSearchPath is always
104 * the actually active path; it points either to the search list of the
105 * topmost stack entry, or to baseSearchPath which is the list derived
106 * from namespace_search_path.
108 * If baseSearchPathValid is false, then baseSearchPath (and other
109 * derived variables) need to be recomputed from namespace_search_path.
110 * We mark it invalid upon an assignment to namespace_search_path or receipt
111 * of a syscache invalidation event for pg_namespace. The recomputation
112 * is done during the next non-overridden lookup attempt. Note that an
113 * override spec is never subject to recomputation.
115 * Any namespaces mentioned in namespace_search_path that are not readable
116 * by the current user ID are simply left out of baseSearchPath; so
117 * we have to be willing to recompute the path when current userid changes.
118 * namespaceUser is the userid the path has been computed for.
120 * Note: all data pointed to by these List variables is in TopMemoryContext.
123 /* These variables define the actually active state: */
125 static List *activeSearchPath = NIL;
127 /* default place to create stuff; if InvalidOid, no default */
128 static Oid activeCreationNamespace = InvalidOid;
130 /* if TRUE, activeCreationNamespace is wrong, it should be temp namespace */
131 static bool activeTempCreationPending = false;
133 /* These variables are the values last derived from namespace_search_path: */
135 static List *baseSearchPath = NIL;
137 static Oid baseCreationNamespace = InvalidOid;
139 static bool baseTempCreationPending = false;
141 static Oid namespaceUser = InvalidOid;
143 /* The above four values are valid only if baseSearchPathValid */
144 static bool baseSearchPathValid = true;
146 /* Override requests are remembered in a stack of OverrideStackEntry structs */
150 List *searchPath; /* the desired search path */
151 Oid creationNamespace; /* the desired creation namespace */
152 int nestLevel; /* subtransaction nesting level */
153 } OverrideStackEntry;
155 static List *overrideStack = NIL;
158 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
159 * in a particular backend session (this happens when a CREATE TEMP TABLE
160 * command is first executed). Thereafter it's the OID of the temp namespace.
162 * myTempToastNamespace is the OID of the namespace for my temp tables' toast
163 * tables. It is set when myTempNamespace is, and is InvalidOid before that.
165 * myTempNamespaceSubID shows whether we've created the TEMP namespace in the
166 * current subtransaction. The flag propagates up the subtransaction tree,
167 * so the main transaction will correctly recognize the flag if all
168 * intermediate subtransactions commit. When it is InvalidSubTransactionId,
169 * we either haven't made the TEMP namespace yet, or have successfully
170 * committed its creation, depending on whether myTempNamespace is valid.
172 static Oid myTempNamespace = InvalidOid;
174 static Oid myTempToastNamespace = InvalidOid;
176 static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId;
179 * This is the user's textual search path specification --- it's the value
180 * of the GUC variable 'search_path'.
182 char *namespace_search_path = NULL;
185 /* Local functions */
186 static void recomputeNamespacePath(void);
187 static void InitTempTableNamespace(void);
188 static void RemoveTempRelations(Oid tempNamespaceId);
189 static void RemoveTempRelationsCallback(int code, Datum arg);
190 static void NamespaceCallback(Datum arg, int cacheid, ItemPointer tuplePtr);
192 /* These don't really need to appear in any header file */
193 Datum pg_table_is_visible(PG_FUNCTION_ARGS);
194 Datum pg_type_is_visible(PG_FUNCTION_ARGS);
195 Datum pg_function_is_visible(PG_FUNCTION_ARGS);
196 Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
197 Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
198 Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
199 Datum pg_ts_parser_is_visible(PG_FUNCTION_ARGS);
200 Datum pg_ts_dict_is_visible(PG_FUNCTION_ARGS);
201 Datum pg_ts_template_is_visible(PG_FUNCTION_ARGS);
202 Datum pg_ts_config_is_visible(PG_FUNCTION_ARGS);
203 Datum pg_my_temp_schema(PG_FUNCTION_ARGS);
204 Datum pg_is_other_temp_schema(PG_FUNCTION_ARGS);
209 * Given a RangeVar describing an existing relation,
210 * select the proper namespace and look up the relation OID.
212 * If the relation is not found, return InvalidOid if failOK = true,
213 * otherwise raise an error.
216 RangeVarGetRelid(const RangeVar *relation, bool failOK)
222 * We check the catalog name and then ignore it.
224 if (relation->catalogname)
226 if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
228 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
229 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
230 relation->catalogname, relation->schemaname,
231 relation->relname)));
235 * If istemp is set, this is a reference to a temp relation. The parser
236 * never generates such a RangeVar in simple DML, but it can happen in
237 * contexts such as "CREATE TEMP TABLE foo (f1 int PRIMARY KEY)". Such a
238 * command will generate an added CREATE INDEX operation, which must be
239 * careful to find the temp table, even when pg_temp is not first in the
242 if (relation->istemp)
244 if (relation->schemaname)
246 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
247 errmsg("temporary tables cannot specify a schema name")));
248 if (OidIsValid(myTempNamespace))
249 relId = get_relname_relid(relation->relname, myTempNamespace);
250 else /* this probably can't happen? */
253 else if (relation->schemaname)
255 /* use exact schema given */
256 namespaceId = LookupExplicitNamespace(relation->schemaname);
257 relId = get_relname_relid(relation->relname, namespaceId);
261 /* search the namespace path */
262 relId = RelnameGetRelid(relation->relname);
265 if (!OidIsValid(relId) && !failOK)
267 if (relation->schemaname)
269 (errcode(ERRCODE_UNDEFINED_TABLE),
270 errmsg("relation \"%s.%s\" does not exist",
271 relation->schemaname, relation->relname)));
274 (errcode(ERRCODE_UNDEFINED_TABLE),
275 errmsg("relation \"%s\" does not exist",
276 relation->relname)));
282 * RangeVarGetCreationNamespace
283 * Given a RangeVar describing a to-be-created relation,
284 * choose which namespace to create it in.
286 * Note: calling this may result in a CommandCounterIncrement operation.
287 * That will happen on the first request for a temp table in any particular
288 * backend run; we will need to either create or clean out the temp schema.
291 RangeVarGetCreationNamespace(const RangeVar *newRelation)
296 * We check the catalog name and then ignore it.
298 if (newRelation->catalogname)
300 if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
302 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
303 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
304 newRelation->catalogname, newRelation->schemaname,
305 newRelation->relname)));
308 if (newRelation->istemp)
310 /* TEMP tables are created in our backend-local temp namespace */
311 if (newRelation->schemaname)
313 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
314 errmsg("temporary tables cannot specify a schema name")));
315 /* Initialize temp namespace if first time through */
316 if (!OidIsValid(myTempNamespace))
317 InitTempTableNamespace();
318 return myTempNamespace;
321 if (newRelation->schemaname)
323 /* check for pg_temp alias */
324 if (strcmp(newRelation->schemaname, "pg_temp") == 0)
326 /* Initialize temp namespace if first time through */
327 if (!OidIsValid(myTempNamespace))
328 InitTempTableNamespace();
329 return myTempNamespace;
331 /* use exact schema given */
332 namespaceId = GetSysCacheOid(NAMESPACENAME,
333 CStringGetDatum(newRelation->schemaname),
335 if (!OidIsValid(namespaceId))
337 (errcode(ERRCODE_UNDEFINED_SCHEMA),
338 errmsg("schema \"%s\" does not exist",
339 newRelation->schemaname)));
340 /* we do not check for USAGE rights here! */
344 /* use the default creation namespace */
345 recomputeNamespacePath();
346 if (activeTempCreationPending)
348 /* Need to initialize temp namespace */
349 InitTempTableNamespace();
350 return myTempNamespace;
352 namespaceId = activeCreationNamespace;
353 if (!OidIsValid(namespaceId))
355 (errcode(ERRCODE_UNDEFINED_SCHEMA),
356 errmsg("no schema has been selected to create in")));
359 /* Note: callers will check for CREATE rights when appropriate */
366 * Try to resolve an unqualified relation name.
367 * Returns OID if relation found in search path, else InvalidOid.
370 RelnameGetRelid(const char *relname)
375 recomputeNamespacePath();
377 foreach(l, activeSearchPath)
379 Oid namespaceId = lfirst_oid(l);
381 relid = get_relname_relid(relname, namespaceId);
382 if (OidIsValid(relid))
386 /* Not found in path */
393 * Determine whether a relation (identified by OID) is visible in the
394 * current search path. Visible means "would be found by searching
395 * for the unqualified relation name".
398 RelationIsVisible(Oid relid)
401 Form_pg_class relform;
405 reltup = SearchSysCache(RELOID,
406 ObjectIdGetDatum(relid),
408 if (!HeapTupleIsValid(reltup))
409 elog(ERROR, "cache lookup failed for relation %u", relid);
410 relform = (Form_pg_class) GETSTRUCT(reltup);
412 recomputeNamespacePath();
415 * Quick check: if it ain't in the path at all, it ain't visible. Items in
416 * the system namespace are surely in the path and so we needn't even do
417 * list_member_oid() for them.
419 relnamespace = relform->relnamespace;
420 if (relnamespace != PG_CATALOG_NAMESPACE &&
421 !list_member_oid(activeSearchPath, relnamespace))
426 * If it is in the path, it might still not be visible; it could be
427 * hidden by another relation of the same name earlier in the path. So
428 * we must do a slow check for conflicting relations.
430 char *relname = NameStr(relform->relname);
434 foreach(l, activeSearchPath)
436 Oid namespaceId = lfirst_oid(l);
438 if (namespaceId == relnamespace)
440 /* Found it first in path */
444 if (OidIsValid(get_relname_relid(relname, namespaceId)))
446 /* Found something else first in path */
452 ReleaseSysCache(reltup);
460 * Try to resolve an unqualified datatype name.
461 * Returns OID if type found in search path, else InvalidOid.
463 * This is essentially the same as RelnameGetRelid.
466 TypenameGetTypid(const char *typname)
471 recomputeNamespacePath();
473 foreach(l, activeSearchPath)
475 Oid namespaceId = lfirst_oid(l);
477 typid = GetSysCacheOid(TYPENAMENSP,
478 PointerGetDatum(typname),
479 ObjectIdGetDatum(namespaceId),
481 if (OidIsValid(typid))
485 /* Not found in path */
491 * Determine whether a type (identified by OID) is visible in the
492 * current search path. Visible means "would be found by searching
493 * for the unqualified type name".
496 TypeIsVisible(Oid typid)
499 Form_pg_type typform;
503 typtup = SearchSysCache(TYPEOID,
504 ObjectIdGetDatum(typid),
506 if (!HeapTupleIsValid(typtup))
507 elog(ERROR, "cache lookup failed for type %u", typid);
508 typform = (Form_pg_type) GETSTRUCT(typtup);
510 recomputeNamespacePath();
513 * Quick check: if it ain't in the path at all, it ain't visible. Items in
514 * the system namespace are surely in the path and so we needn't even do
515 * list_member_oid() for them.
517 typnamespace = typform->typnamespace;
518 if (typnamespace != PG_CATALOG_NAMESPACE &&
519 !list_member_oid(activeSearchPath, typnamespace))
524 * If it is in the path, it might still not be visible; it could be
525 * hidden by another type of the same name earlier in the path. So we
526 * must do a slow check for conflicting types.
528 char *typname = NameStr(typform->typname);
532 foreach(l, activeSearchPath)
534 Oid namespaceId = lfirst_oid(l);
536 if (namespaceId == typnamespace)
538 /* Found it first in path */
542 if (SearchSysCacheExists(TYPENAMENSP,
543 PointerGetDatum(typname),
544 ObjectIdGetDatum(namespaceId),
547 /* Found something else first in path */
553 ReleaseSysCache(typtup);
560 * FuncnameGetCandidates
561 * Given a possibly-qualified function name and argument count,
562 * retrieve a list of the possible matches.
564 * If nargs is -1, we return all functions matching the given name,
565 * regardless of argument count. (expand_variadic and expand_defaults must be
566 * false in this case.)
568 * If expand_variadic is true, then variadic functions having the same number
569 * or fewer arguments will be retrieved, with the variadic argument and any
570 * additional argument positions filled with the variadic element type.
571 * nvargs in the returned struct is set to the number of such arguments.
572 * If expand_variadic is false, variadic arguments are not treated specially,
573 * and the returned nvargs will always be zero.
575 * If expand_defaults is true, functions that could match after insertion of
576 * default argument values will also be retrieved. In this case the returned
577 * structs could have nargs > passed-in nargs, and ndargs is set to the number
578 * of additional args (which can be retrieved from the function's
579 * proargdefaults entry).
581 * It is not possible for nvargs and ndargs to both be nonzero in the same
582 * list entry, since default insertion allows matches to functions with more
583 * than nargs arguments while the variadic transformation requires the same
586 * We search a single namespace if the function name is qualified, else
587 * all namespaces in the search path. In the multiple-namespace case,
588 * we arrange for entries in earlier namespaces to mask identical entries in
591 * When expanding variadics, we arrange for non-variadic functions to mask
592 * variadic ones if the expanded argument list is the same. It is still
593 * possible for there to be conflicts between different variadic functions,
596 * It is guaranteed that the return list will never contain multiple entries
597 * with identical argument lists. When expand_defaults is true, the entries
598 * could have more than nargs positions, but we still guarantee that they are
599 * distinct in the first nargs positions. However, if either expand_variadic
600 * or expand_defaults is true, there might be multiple candidate functions
601 * that expand to identical argument lists. Rather than throw error here,
602 * we report such situations by setting oid = 0 in the ambiguous entries.
603 * The caller might end up discarding such an entry anyway, but if it selects
604 * such an entry it should react as though the call were ambiguous.
607 FuncnameGetCandidates(List *names, int nargs,
608 bool expand_variadic, bool expand_defaults)
610 FuncCandidateList resultList = NULL;
611 bool any_special = false;
618 /* check for caller error */
619 Assert(nargs >= 0 || !(expand_variadic | expand_defaults));
621 /* deconstruct the name list */
622 DeconstructQualifiedName(names, &schemaname, &funcname);
626 /* use exact schema given */
627 namespaceId = LookupExplicitNamespace(schemaname);
631 /* flag to indicate we need namespace search */
632 namespaceId = InvalidOid;
633 recomputeNamespacePath();
636 /* Search syscache by name only */
637 catlist = SearchSysCacheList(PROCNAMEARGSNSP, 1,
638 CStringGetDatum(funcname),
641 for (i = 0; i < catlist->n_members; i++)
643 HeapTuple proctup = &catlist->members[i]->tuple;
644 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
645 int pronargs = procform->pronargs;
651 FuncCandidateList newResult;
654 * Check if function is variadic, and get variadic element type if so.
655 * If expand_variadic is false, we should just ignore variadic-ness.
657 if (pronargs <= nargs && expand_variadic)
659 va_elem_type = procform->provariadic;
660 variadic = OidIsValid(va_elem_type);
661 any_special |= variadic;
665 va_elem_type = InvalidOid;
670 * Check if function can match by using parameter defaults.
672 if (pronargs > nargs && expand_defaults)
674 /* Ignore if not enough default expressions */
675 if (nargs + procform->pronargdefaults < pronargs)
681 use_defaults = false;
683 /* Ignore if it doesn't match requested argument count */
684 if (nargs >= 0 && pronargs != nargs && !variadic && !use_defaults)
687 if (OidIsValid(namespaceId))
689 /* Consider only procs in specified namespace */
690 if (procform->pronamespace != namespaceId)
696 * Consider only procs that are in the search path and are not in
697 * the temp namespace.
701 foreach(nsp, activeSearchPath)
703 if (procform->pronamespace == lfirst_oid(nsp) &&
704 procform->pronamespace != myTempNamespace)
709 continue; /* proc is not in search path */
713 * We must compute the effective argument list so that we can easily
714 * compare it to earlier results. We waste a palloc cycle if it gets
715 * masked by an earlier result, but really that's a pretty infrequent
716 * case so it's not worth worrying about.
718 effective_nargs = Max(pronargs, nargs);
719 newResult = (FuncCandidateList)
720 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
721 + effective_nargs * sizeof(Oid));
722 newResult->pathpos = pathpos;
723 newResult->oid = HeapTupleGetOid(proctup);
724 newResult->nargs = effective_nargs;
725 memcpy(newResult->args, procform->proargtypes.values,
726 pronargs * sizeof(Oid));
731 newResult->nvargs = effective_nargs - pronargs + 1;
732 /* Expand variadic argument into N copies of element type */
733 for (i = pronargs - 1; i < effective_nargs; i++)
734 newResult->args[i] = va_elem_type;
737 newResult->nvargs = 0;
738 newResult->ndargs = use_defaults ? pronargs - nargs : 0;
741 * Does it have the same arguments as something we already accepted?
742 * If so, decide what to do to avoid returning duplicate argument
743 * lists. We can skip this check for the single-namespace case if no
744 * special (variadic or defaults) match has been made, since then the
745 * unique index on pg_proc guarantees all the matches have different
748 if (resultList != NULL &&
749 (any_special || !OidIsValid(namespaceId)))
752 * If we have an ordered list from SearchSysCacheList (the normal
753 * case), then any conflicting proc must immediately adjoin this
754 * one in the list, so we only need to look at the newest result
755 * item. If we have an unordered list, we have to scan the whole
756 * result list. Also, if either the current candidate or any
757 * previous candidate is a special match, we can't assume that
758 * conflicts are adjacent.
760 * We ignore defaulted arguments in deciding what is a match.
762 FuncCandidateList prevResult;
764 if (catlist->ordered && !any_special)
766 /* ndargs must be 0 if !any_special */
767 if (effective_nargs == resultList->nargs &&
768 memcmp(newResult->args,
770 effective_nargs * sizeof(Oid)) == 0)
771 prevResult = resultList;
777 int cmp_nargs = newResult->nargs - newResult->ndargs;
779 for (prevResult = resultList;
781 prevResult = prevResult->next)
783 if (cmp_nargs == prevResult->nargs - prevResult->ndargs &&
784 memcmp(newResult->args,
786 cmp_nargs * sizeof(Oid)) == 0)
794 * We have a match with a previous result. Decide which one
795 * to keep, or mark it ambiguous if we can't decide. The
796 * logic here is preference > 0 means prefer the old result,
797 * preference < 0 means prefer the new, preference = 0 means
802 if (pathpos != prevResult->pathpos)
805 * Prefer the one that's earlier in the search path.
807 preference = pathpos - prevResult->pathpos;
809 else if (variadic && prevResult->nvargs == 0)
812 * With variadic functions we could have, for example,
813 * both foo(numeric) and foo(variadic numeric[]) in
814 * the same namespace; if so we prefer the
815 * non-variadic match on efficiency grounds.
819 else if (!variadic && prevResult->nvargs > 0)
826 * We can't decide. This can happen with, for example,
827 * both foo(numeric, variadic numeric[]) and
828 * foo(variadic numeric[]) in the same namespace, or
829 * both foo(int) and foo (int, int default something)
830 * in the same namespace.
838 /* keep previous result */
842 else if (preference < 0)
844 /* remove previous result from the list */
845 if (prevResult == resultList)
846 resultList = prevResult->next;
849 FuncCandidateList prevPrevResult;
851 for (prevPrevResult = resultList;
853 prevPrevResult = prevPrevResult->next)
855 if (prevResult == prevPrevResult->next)
857 prevPrevResult->next = prevResult->next;
861 Assert(prevPrevResult); /* assert we found it */
864 /* fall through to add newResult to list */
868 /* mark old result as ambiguous, discard new */
869 prevResult->oid = InvalidOid;
877 * Okay to add it to result list
879 newResult->next = resultList;
880 resultList = newResult;
883 ReleaseSysCacheList(catlist);
890 * Determine whether a function (identified by OID) is visible in the
891 * current search path. Visible means "would be found by searching
892 * for the unqualified function name with exact argument matches".
895 FunctionIsVisible(Oid funcid)
898 Form_pg_proc procform;
902 proctup = SearchSysCache(PROCOID,
903 ObjectIdGetDatum(funcid),
905 if (!HeapTupleIsValid(proctup))
906 elog(ERROR, "cache lookup failed for function %u", funcid);
907 procform = (Form_pg_proc) GETSTRUCT(proctup);
909 recomputeNamespacePath();
912 * Quick check: if it ain't in the path at all, it ain't visible. Items in
913 * the system namespace are surely in the path and so we needn't even do
914 * list_member_oid() for them.
916 pronamespace = procform->pronamespace;
917 if (pronamespace != PG_CATALOG_NAMESPACE &&
918 !list_member_oid(activeSearchPath, pronamespace))
923 * If it is in the path, it might still not be visible; it could be
924 * hidden by another proc of the same name and arguments earlier in
925 * the path. So we must do a slow check to see if this is the same
926 * proc that would be found by FuncnameGetCandidates.
928 char *proname = NameStr(procform->proname);
929 int nargs = procform->pronargs;
930 FuncCandidateList clist;
934 clist = FuncnameGetCandidates(list_make1(makeString(proname)),
935 nargs, false, false);
937 for (; clist; clist = clist->next)
939 if (memcmp(clist->args, procform->proargtypes.values,
940 nargs * sizeof(Oid)) == 0)
942 /* Found the expected entry; is it the right proc? */
943 visible = (clist->oid == funcid);
949 ReleaseSysCache(proctup);
957 * Given a possibly-qualified operator name and exact input datatypes,
958 * look up the operator. Returns InvalidOid if not found.
960 * Pass oprleft = InvalidOid for a prefix op, oprright = InvalidOid for
963 * If the operator name is not schema-qualified, it is sought in the current
964 * namespace search path.
967 OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
974 /* deconstruct the name list */
975 DeconstructQualifiedName(names, &schemaname, &opername);
979 /* search only in exact schema given */
983 namespaceId = LookupExplicitNamespace(schemaname);
984 opertup = SearchSysCache(OPERNAMENSP,
985 CStringGetDatum(opername),
986 ObjectIdGetDatum(oprleft),
987 ObjectIdGetDatum(oprright),
988 ObjectIdGetDatum(namespaceId));
989 if (HeapTupleIsValid(opertup))
991 Oid result = HeapTupleGetOid(opertup);
993 ReleaseSysCache(opertup);
999 /* Search syscache by name and argument types */
1000 catlist = SearchSysCacheList(OPERNAMENSP, 3,
1001 CStringGetDatum(opername),
1002 ObjectIdGetDatum(oprleft),
1003 ObjectIdGetDatum(oprright),
1006 if (catlist->n_members == 0)
1008 /* no hope, fall out early */
1009 ReleaseSysCacheList(catlist);
1014 * We have to find the list member that is first in the search path, if
1015 * there's more than one. This doubly-nested loop looks ugly, but in
1016 * practice there should usually be few catlist members.
1018 recomputeNamespacePath();
1020 foreach(l, activeSearchPath)
1022 Oid namespaceId = lfirst_oid(l);
1025 if (namespaceId == myTempNamespace)
1026 continue; /* do not look in temp namespace */
1028 for (i = 0; i < catlist->n_members; i++)
1030 HeapTuple opertup = &catlist->members[i]->tuple;
1031 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1033 if (operform->oprnamespace == namespaceId)
1035 Oid result = HeapTupleGetOid(opertup);
1037 ReleaseSysCacheList(catlist);
1043 ReleaseSysCacheList(catlist);
1048 * OpernameGetCandidates
1049 * Given a possibly-qualified operator name and operator kind,
1050 * retrieve a list of the possible matches.
1052 * If oprkind is '\0', we return all operators matching the given name,
1053 * regardless of arguments.
1055 * We search a single namespace if the operator name is qualified, else
1056 * all namespaces in the search path. The return list will never contain
1057 * multiple entries with identical argument lists --- in the multiple-
1058 * namespace case, we arrange for entries in earlier namespaces to mask
1059 * identical entries in later namespaces.
1061 * The returned items always have two args[] entries --- one or the other
1062 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
1065 OpernameGetCandidates(List *names, char oprkind)
1067 FuncCandidateList resultList = NULL;
1068 char *resultSpace = NULL;
1076 /* deconstruct the name list */
1077 DeconstructQualifiedName(names, &schemaname, &opername);
1081 /* use exact schema given */
1082 namespaceId = LookupExplicitNamespace(schemaname);
1086 /* flag to indicate we need namespace search */
1087 namespaceId = InvalidOid;
1088 recomputeNamespacePath();
1091 /* Search syscache by name only */
1092 catlist = SearchSysCacheList(OPERNAMENSP, 1,
1093 CStringGetDatum(opername),
1097 * In typical scenarios, most if not all of the operators found by the
1098 * catcache search will end up getting returned; and there can be quite a
1099 * few, for common operator names such as '=' or '+'. To reduce the time
1100 * spent in palloc, we allocate the result space as an array large enough
1101 * to hold all the operators. The original coding of this routine did a
1102 * separate palloc for each operator, but profiling revealed that the
1103 * pallocs used an unreasonably large fraction of parsing time.
1105 #define SPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
1107 if (catlist->n_members > 0)
1108 resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
1110 for (i = 0; i < catlist->n_members; i++)
1112 HeapTuple opertup = &catlist->members[i]->tuple;
1113 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1115 FuncCandidateList newResult;
1117 /* Ignore operators of wrong kind, if specific kind requested */
1118 if (oprkind && operform->oprkind != oprkind)
1121 if (OidIsValid(namespaceId))
1123 /* Consider only opers in specified namespace */
1124 if (operform->oprnamespace != namespaceId)
1126 /* No need to check args, they must all be different */
1131 * Consider only opers that are in the search path and are not in
1132 * the temp namespace.
1136 foreach(nsp, activeSearchPath)
1138 if (operform->oprnamespace == lfirst_oid(nsp) &&
1139 operform->oprnamespace != myTempNamespace)
1144 continue; /* oper is not in search path */
1147 * Okay, it's in the search path, but does it have the same
1148 * arguments as something we already accepted? If so, keep only
1149 * the one that appears earlier in the search path.
1151 * If we have an ordered list from SearchSysCacheList (the normal
1152 * case), then any conflicting oper must immediately adjoin this
1153 * one in the list, so we only need to look at the newest result
1154 * item. If we have an unordered list, we have to scan the whole
1159 FuncCandidateList prevResult;
1161 if (catlist->ordered)
1163 if (operform->oprleft == resultList->args[0] &&
1164 operform->oprright == resultList->args[1])
1165 prevResult = resultList;
1171 for (prevResult = resultList;
1173 prevResult = prevResult->next)
1175 if (operform->oprleft == prevResult->args[0] &&
1176 operform->oprright == prevResult->args[1])
1182 /* We have a match with a previous result */
1183 Assert(pathpos != prevResult->pathpos);
1184 if (pathpos > prevResult->pathpos)
1185 continue; /* keep previous result */
1186 /* replace previous result */
1187 prevResult->pathpos = pathpos;
1188 prevResult->oid = HeapTupleGetOid(opertup);
1189 continue; /* args are same, of course */
1195 * Okay to add it to result list
1197 newResult = (FuncCandidateList) (resultSpace + nextResult);
1198 nextResult += SPACE_PER_OP;
1200 newResult->pathpos = pathpos;
1201 newResult->oid = HeapTupleGetOid(opertup);
1202 newResult->nargs = 2;
1203 newResult->nvargs = 0;
1204 newResult->ndargs = 0;
1205 newResult->args[0] = operform->oprleft;
1206 newResult->args[1] = operform->oprright;
1207 newResult->next = resultList;
1208 resultList = newResult;
1211 ReleaseSysCacheList(catlist);
1218 * Determine whether an operator (identified by OID) is visible in the
1219 * current search path. Visible means "would be found by searching
1220 * for the unqualified operator name with exact argument matches".
1223 OperatorIsVisible(Oid oprid)
1226 Form_pg_operator oprform;
1230 oprtup = SearchSysCache(OPEROID,
1231 ObjectIdGetDatum(oprid),
1233 if (!HeapTupleIsValid(oprtup))
1234 elog(ERROR, "cache lookup failed for operator %u", oprid);
1235 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
1237 recomputeNamespacePath();
1240 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1241 * the system namespace are surely in the path and so we needn't even do
1242 * list_member_oid() for them.
1244 oprnamespace = oprform->oprnamespace;
1245 if (oprnamespace != PG_CATALOG_NAMESPACE &&
1246 !list_member_oid(activeSearchPath, oprnamespace))
1251 * If it is in the path, it might still not be visible; it could be
1252 * hidden by another operator of the same name and arguments earlier
1253 * in the path. So we must do a slow check to see if this is the same
1254 * operator that would be found by OpernameGetOprId.
1256 char *oprname = NameStr(oprform->oprname);
1258 visible = (OpernameGetOprid(list_make1(makeString(oprname)),
1259 oprform->oprleft, oprform->oprright)
1263 ReleaseSysCache(oprtup);
1270 * OpclassnameGetOpcid
1271 * Try to resolve an unqualified index opclass name.
1272 * Returns OID if opclass found in search path, else InvalidOid.
1274 * This is essentially the same as TypenameGetTypid, but we have to have
1275 * an extra argument for the index AM OID.
1278 OpclassnameGetOpcid(Oid amid, const char *opcname)
1283 recomputeNamespacePath();
1285 foreach(l, activeSearchPath)
1287 Oid namespaceId = lfirst_oid(l);
1289 if (namespaceId == myTempNamespace)
1290 continue; /* do not look in temp namespace */
1292 opcid = GetSysCacheOid(CLAAMNAMENSP,
1293 ObjectIdGetDatum(amid),
1294 PointerGetDatum(opcname),
1295 ObjectIdGetDatum(namespaceId),
1297 if (OidIsValid(opcid))
1301 /* Not found in path */
1307 * Determine whether an opclass (identified by OID) is visible in the
1308 * current search path. Visible means "would be found by searching
1309 * for the unqualified opclass name".
1312 OpclassIsVisible(Oid opcid)
1315 Form_pg_opclass opcform;
1319 opctup = SearchSysCache(CLAOID,
1320 ObjectIdGetDatum(opcid),
1322 if (!HeapTupleIsValid(opctup))
1323 elog(ERROR, "cache lookup failed for opclass %u", opcid);
1324 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1326 recomputeNamespacePath();
1329 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1330 * the system namespace are surely in the path and so we needn't even do
1331 * list_member_oid() for them.
1333 opcnamespace = opcform->opcnamespace;
1334 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1335 !list_member_oid(activeSearchPath, opcnamespace))
1340 * If it is in the path, it might still not be visible; it could be
1341 * hidden by another opclass of the same name earlier in the path. So
1342 * we must do a slow check to see if this opclass would be found by
1343 * OpclassnameGetOpcid.
1345 char *opcname = NameStr(opcform->opcname);
1347 visible = (OpclassnameGetOpcid(opcform->opcmethod, opcname) == opcid);
1350 ReleaseSysCache(opctup);
1356 * OpfamilynameGetOpfid
1357 * Try to resolve an unqualified index opfamily name.
1358 * Returns OID if opfamily found in search path, else InvalidOid.
1360 * This is essentially the same as TypenameGetTypid, but we have to have
1361 * an extra argument for the index AM OID.
1364 OpfamilynameGetOpfid(Oid amid, const char *opfname)
1369 recomputeNamespacePath();
1371 foreach(l, activeSearchPath)
1373 Oid namespaceId = lfirst_oid(l);
1375 if (namespaceId == myTempNamespace)
1376 continue; /* do not look in temp namespace */
1378 opfid = GetSysCacheOid(OPFAMILYAMNAMENSP,
1379 ObjectIdGetDatum(amid),
1380 PointerGetDatum(opfname),
1381 ObjectIdGetDatum(namespaceId),
1383 if (OidIsValid(opfid))
1387 /* Not found in path */
1393 * Determine whether an opfamily (identified by OID) is visible in the
1394 * current search path. Visible means "would be found by searching
1395 * for the unqualified opfamily name".
1398 OpfamilyIsVisible(Oid opfid)
1401 Form_pg_opfamily opfform;
1405 opftup = SearchSysCache(OPFAMILYOID,
1406 ObjectIdGetDatum(opfid),
1408 if (!HeapTupleIsValid(opftup))
1409 elog(ERROR, "cache lookup failed for opfamily %u", opfid);
1410 opfform = (Form_pg_opfamily) GETSTRUCT(opftup);
1412 recomputeNamespacePath();
1415 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1416 * the system namespace are surely in the path and so we needn't even do
1417 * list_member_oid() for them.
1419 opfnamespace = opfform->opfnamespace;
1420 if (opfnamespace != PG_CATALOG_NAMESPACE &&
1421 !list_member_oid(activeSearchPath, opfnamespace))
1426 * If it is in the path, it might still not be visible; it could be
1427 * hidden by another opfamily of the same name earlier in the path. So
1428 * we must do a slow check to see if this opfamily would be found by
1429 * OpfamilynameGetOpfid.
1431 char *opfname = NameStr(opfform->opfname);
1433 visible = (OpfamilynameGetOpfid(opfform->opfmethod, opfname) == opfid);
1436 ReleaseSysCache(opftup);
1442 * ConversionGetConid
1443 * Try to resolve an unqualified conversion name.
1444 * Returns OID if conversion found in search path, else InvalidOid.
1446 * This is essentially the same as RelnameGetRelid.
1449 ConversionGetConid(const char *conname)
1454 recomputeNamespacePath();
1456 foreach(l, activeSearchPath)
1458 Oid namespaceId = lfirst_oid(l);
1460 if (namespaceId == myTempNamespace)
1461 continue; /* do not look in temp namespace */
1463 conid = GetSysCacheOid(CONNAMENSP,
1464 PointerGetDatum(conname),
1465 ObjectIdGetDatum(namespaceId),
1467 if (OidIsValid(conid))
1471 /* Not found in path */
1476 * ConversionIsVisible
1477 * Determine whether a conversion (identified by OID) is visible in the
1478 * current search path. Visible means "would be found by searching
1479 * for the unqualified conversion name".
1482 ConversionIsVisible(Oid conid)
1485 Form_pg_conversion conform;
1489 contup = SearchSysCache(CONVOID,
1490 ObjectIdGetDatum(conid),
1492 if (!HeapTupleIsValid(contup))
1493 elog(ERROR, "cache lookup failed for conversion %u", conid);
1494 conform = (Form_pg_conversion) GETSTRUCT(contup);
1496 recomputeNamespacePath();
1499 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1500 * the system namespace are surely in the path and so we needn't even do
1501 * list_member_oid() for them.
1503 connamespace = conform->connamespace;
1504 if (connamespace != PG_CATALOG_NAMESPACE &&
1505 !list_member_oid(activeSearchPath, connamespace))
1510 * If it is in the path, it might still not be visible; it could be
1511 * hidden by another conversion of the same name earlier in the path.
1512 * So we must do a slow check to see if this conversion would be found
1513 * by ConversionGetConid.
1515 char *conname = NameStr(conform->conname);
1517 visible = (ConversionGetConid(conname) == conid);
1520 ReleaseSysCache(contup);
1526 * TSParserGetPrsid - find a TS parser by possibly qualified name
1528 * If not found, returns InvalidOid if failOK, else throws error
1531 TSParserGetPrsid(List *names, bool failOK)
1536 Oid prsoid = InvalidOid;
1539 /* deconstruct the name list */
1540 DeconstructQualifiedName(names, &schemaname, &parser_name);
1544 /* use exact schema given */
1545 namespaceId = LookupExplicitNamespace(schemaname);
1546 prsoid = GetSysCacheOid(TSPARSERNAMENSP,
1547 PointerGetDatum(parser_name),
1548 ObjectIdGetDatum(namespaceId),
1553 /* search for it in search path */
1554 recomputeNamespacePath();
1556 foreach(l, activeSearchPath)
1558 namespaceId = lfirst_oid(l);
1560 if (namespaceId == myTempNamespace)
1561 continue; /* do not look in temp namespace */
1563 prsoid = GetSysCacheOid(TSPARSERNAMENSP,
1564 PointerGetDatum(parser_name),
1565 ObjectIdGetDatum(namespaceId),
1567 if (OidIsValid(prsoid))
1572 if (!OidIsValid(prsoid) && !failOK)
1574 (errcode(ERRCODE_UNDEFINED_OBJECT),
1575 errmsg("text search parser \"%s\" does not exist",
1576 NameListToString(names))));
1583 * Determine whether a parser (identified by OID) is visible in the
1584 * current search path. Visible means "would be found by searching
1585 * for the unqualified parser name".
1588 TSParserIsVisible(Oid prsId)
1591 Form_pg_ts_parser form;
1595 tup = SearchSysCache(TSPARSEROID,
1596 ObjectIdGetDatum(prsId),
1598 if (!HeapTupleIsValid(tup))
1599 elog(ERROR, "cache lookup failed for text search parser %u", prsId);
1600 form = (Form_pg_ts_parser) GETSTRUCT(tup);
1602 recomputeNamespacePath();
1605 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1606 * the system namespace are surely in the path and so we needn't even do
1607 * list_member_oid() for them.
1609 namespace = form->prsnamespace;
1610 if (namespace != PG_CATALOG_NAMESPACE &&
1611 !list_member_oid(activeSearchPath, namespace))
1616 * If it is in the path, it might still not be visible; it could be
1617 * hidden by another parser of the same name earlier in the path. So
1618 * we must do a slow check for conflicting parsers.
1620 char *name = NameStr(form->prsname);
1624 foreach(l, activeSearchPath)
1626 Oid namespaceId = lfirst_oid(l);
1628 if (namespaceId == myTempNamespace)
1629 continue; /* do not look in temp namespace */
1631 if (namespaceId == namespace)
1633 /* Found it first in path */
1637 if (SearchSysCacheExists(TSPARSERNAMENSP,
1638 PointerGetDatum(name),
1639 ObjectIdGetDatum(namespaceId),
1642 /* Found something else first in path */
1648 ReleaseSysCache(tup);
1654 * TSDictionaryGetDictid - find a TS dictionary by possibly qualified name
1656 * If not found, returns InvalidOid if failOK, else throws error
1659 TSDictionaryGetDictid(List *names, bool failOK)
1664 Oid dictoid = InvalidOid;
1667 /* deconstruct the name list */
1668 DeconstructQualifiedName(names, &schemaname, &dict_name);
1672 /* use exact schema given */
1673 namespaceId = LookupExplicitNamespace(schemaname);
1674 dictoid = GetSysCacheOid(TSDICTNAMENSP,
1675 PointerGetDatum(dict_name),
1676 ObjectIdGetDatum(namespaceId),
1681 /* search for it in search path */
1682 recomputeNamespacePath();
1684 foreach(l, activeSearchPath)
1686 namespaceId = lfirst_oid(l);
1688 if (namespaceId == myTempNamespace)
1689 continue; /* do not look in temp namespace */
1691 dictoid = GetSysCacheOid(TSDICTNAMENSP,
1692 PointerGetDatum(dict_name),
1693 ObjectIdGetDatum(namespaceId),
1695 if (OidIsValid(dictoid))
1700 if (!OidIsValid(dictoid) && !failOK)
1702 (errcode(ERRCODE_UNDEFINED_OBJECT),
1703 errmsg("text search dictionary \"%s\" does not exist",
1704 NameListToString(names))));
1710 * TSDictionaryIsVisible
1711 * Determine whether a dictionary (identified by OID) is visible in the
1712 * current search path. Visible means "would be found by searching
1713 * for the unqualified dictionary name".
1716 TSDictionaryIsVisible(Oid dictId)
1719 Form_pg_ts_dict form;
1723 tup = SearchSysCache(TSDICTOID,
1724 ObjectIdGetDatum(dictId),
1726 if (!HeapTupleIsValid(tup))
1727 elog(ERROR, "cache lookup failed for text search dictionary %u",
1729 form = (Form_pg_ts_dict) GETSTRUCT(tup);
1731 recomputeNamespacePath();
1734 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1735 * the system namespace are surely in the path and so we needn't even do
1736 * list_member_oid() for them.
1738 namespace = form->dictnamespace;
1739 if (namespace != PG_CATALOG_NAMESPACE &&
1740 !list_member_oid(activeSearchPath, namespace))
1745 * If it is in the path, it might still not be visible; it could be
1746 * hidden by another dictionary of the same name earlier in the path.
1747 * So we must do a slow check for conflicting dictionaries.
1749 char *name = NameStr(form->dictname);
1753 foreach(l, activeSearchPath)
1755 Oid namespaceId = lfirst_oid(l);
1757 if (namespaceId == myTempNamespace)
1758 continue; /* do not look in temp namespace */
1760 if (namespaceId == namespace)
1762 /* Found it first in path */
1766 if (SearchSysCacheExists(TSDICTNAMENSP,
1767 PointerGetDatum(name),
1768 ObjectIdGetDatum(namespaceId),
1771 /* Found something else first in path */
1777 ReleaseSysCache(tup);
1783 * TSTemplateGetTmplid - find a TS template by possibly qualified name
1785 * If not found, returns InvalidOid if failOK, else throws error
1788 TSTemplateGetTmplid(List *names, bool failOK)
1791 char *template_name;
1793 Oid tmploid = InvalidOid;
1796 /* deconstruct the name list */
1797 DeconstructQualifiedName(names, &schemaname, &template_name);
1801 /* use exact schema given */
1802 namespaceId = LookupExplicitNamespace(schemaname);
1803 tmploid = GetSysCacheOid(TSTEMPLATENAMENSP,
1804 PointerGetDatum(template_name),
1805 ObjectIdGetDatum(namespaceId),
1810 /* search for it in search path */
1811 recomputeNamespacePath();
1813 foreach(l, activeSearchPath)
1815 namespaceId = lfirst_oid(l);
1817 if (namespaceId == myTempNamespace)
1818 continue; /* do not look in temp namespace */
1820 tmploid = GetSysCacheOid(TSTEMPLATENAMENSP,
1821 PointerGetDatum(template_name),
1822 ObjectIdGetDatum(namespaceId),
1824 if (OidIsValid(tmploid))
1829 if (!OidIsValid(tmploid) && !failOK)
1831 (errcode(ERRCODE_UNDEFINED_OBJECT),
1832 errmsg("text search template \"%s\" does not exist",
1833 NameListToString(names))));
1839 * TSTemplateIsVisible
1840 * Determine whether a template (identified by OID) is visible in the
1841 * current search path. Visible means "would be found by searching
1842 * for the unqualified template name".
1845 TSTemplateIsVisible(Oid tmplId)
1848 Form_pg_ts_template form;
1852 tup = SearchSysCache(TSTEMPLATEOID,
1853 ObjectIdGetDatum(tmplId),
1855 if (!HeapTupleIsValid(tup))
1856 elog(ERROR, "cache lookup failed for text search template %u", tmplId);
1857 form = (Form_pg_ts_template) GETSTRUCT(tup);
1859 recomputeNamespacePath();
1862 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1863 * the system namespace are surely in the path and so we needn't even do
1864 * list_member_oid() for them.
1866 namespace = form->tmplnamespace;
1867 if (namespace != PG_CATALOG_NAMESPACE &&
1868 !list_member_oid(activeSearchPath, namespace))
1873 * If it is in the path, it might still not be visible; it could be
1874 * hidden by another template of the same name earlier in the path. So
1875 * we must do a slow check for conflicting templates.
1877 char *name = NameStr(form->tmplname);
1881 foreach(l, activeSearchPath)
1883 Oid namespaceId = lfirst_oid(l);
1885 if (namespaceId == myTempNamespace)
1886 continue; /* do not look in temp namespace */
1888 if (namespaceId == namespace)
1890 /* Found it first in path */
1894 if (SearchSysCacheExists(TSTEMPLATENAMENSP,
1895 PointerGetDatum(name),
1896 ObjectIdGetDatum(namespaceId),
1899 /* Found something else first in path */
1905 ReleaseSysCache(tup);
1911 * TSConfigGetCfgid - find a TS config by possibly qualified name
1913 * If not found, returns InvalidOid if failOK, else throws error
1916 TSConfigGetCfgid(List *names, bool failOK)
1921 Oid cfgoid = InvalidOid;
1924 /* deconstruct the name list */
1925 DeconstructQualifiedName(names, &schemaname, &config_name);
1929 /* use exact schema given */
1930 namespaceId = LookupExplicitNamespace(schemaname);
1931 cfgoid = GetSysCacheOid(TSCONFIGNAMENSP,
1932 PointerGetDatum(config_name),
1933 ObjectIdGetDatum(namespaceId),
1938 /* search for it in search path */
1939 recomputeNamespacePath();
1941 foreach(l, activeSearchPath)
1943 namespaceId = lfirst_oid(l);
1945 if (namespaceId == myTempNamespace)
1946 continue; /* do not look in temp namespace */
1948 cfgoid = GetSysCacheOid(TSCONFIGNAMENSP,
1949 PointerGetDatum(config_name),
1950 ObjectIdGetDatum(namespaceId),
1952 if (OidIsValid(cfgoid))
1957 if (!OidIsValid(cfgoid) && !failOK)
1959 (errcode(ERRCODE_UNDEFINED_OBJECT),
1960 errmsg("text search configuration \"%s\" does not exist",
1961 NameListToString(names))));
1968 * Determine whether a text search configuration (identified by OID)
1969 * is visible in the current search path. Visible means "would be found
1970 * by searching for the unqualified text search configuration name".
1973 TSConfigIsVisible(Oid cfgid)
1976 Form_pg_ts_config form;
1980 tup = SearchSysCache(TSCONFIGOID,
1981 ObjectIdGetDatum(cfgid),
1983 if (!HeapTupleIsValid(tup))
1984 elog(ERROR, "cache lookup failed for text search configuration %u",
1986 form = (Form_pg_ts_config) GETSTRUCT(tup);
1988 recomputeNamespacePath();
1991 * Quick check: if it ain't in the path at all, it ain't visible. Items in
1992 * the system namespace are surely in the path and so we needn't even do
1993 * list_member_oid() for them.
1995 namespace = form->cfgnamespace;
1996 if (namespace != PG_CATALOG_NAMESPACE &&
1997 !list_member_oid(activeSearchPath, namespace))
2002 * If it is in the path, it might still not be visible; it could be
2003 * hidden by another configuration of the same name earlier in the
2004 * path. So we must do a slow check for conflicting configurations.
2006 char *name = NameStr(form->cfgname);
2010 foreach(l, activeSearchPath)
2012 Oid namespaceId = lfirst_oid(l);
2014 if (namespaceId == myTempNamespace)
2015 continue; /* do not look in temp namespace */
2017 if (namespaceId == namespace)
2019 /* Found it first in path */
2023 if (SearchSysCacheExists(TSCONFIGNAMENSP,
2024 PointerGetDatum(name),
2025 ObjectIdGetDatum(namespaceId),
2028 /* Found something else first in path */
2034 ReleaseSysCache(tup);
2041 * DeconstructQualifiedName
2042 * Given a possibly-qualified name expressed as a list of String nodes,
2043 * extract the schema name and object name.
2045 * *nspname_p is set to NULL if there is no explicit schema name.
2048 DeconstructQualifiedName(List *names,
2053 char *schemaname = NULL;
2054 char *objname = NULL;
2056 switch (list_length(names))
2059 objname = strVal(linitial(names));
2062 schemaname = strVal(linitial(names));
2063 objname = strVal(lsecond(names));
2066 catalogname = strVal(linitial(names));
2067 schemaname = strVal(lsecond(names));
2068 objname = strVal(lthird(names));
2071 * We check the catalog name and then ignore it.
2073 if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
2075 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2076 errmsg("cross-database references are not implemented: %s",
2077 NameListToString(names))));
2081 (errcode(ERRCODE_SYNTAX_ERROR),
2082 errmsg("improper qualified name (too many dotted names): %s",
2083 NameListToString(names))));
2087 *nspname_p = schemaname;
2088 *objname_p = objname;
2092 * LookupExplicitNamespace
2093 * Process an explicitly-specified schema name: look up the schema
2094 * and verify we have USAGE (lookup) rights in it.
2096 * Returns the namespace OID. Raises ereport if any problem.
2099 LookupExplicitNamespace(const char *nspname)
2102 AclResult aclresult;
2104 /* check for pg_temp alias */
2105 if (strcmp(nspname, "pg_temp") == 0)
2107 if (OidIsValid(myTempNamespace))
2108 return myTempNamespace;
2111 * Since this is used only for looking up existing objects, there is
2112 * no point in trying to initialize the temp namespace here; and doing
2113 * so might create problems for some callers. Just fall through and
2114 * give the "does not exist" error.
2118 namespaceId = GetSysCacheOid(NAMESPACENAME,
2119 CStringGetDatum(nspname),
2121 if (!OidIsValid(namespaceId))
2123 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2124 errmsg("schema \"%s\" does not exist", nspname)));
2126 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
2127 if (aclresult != ACLCHECK_OK)
2128 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2135 * LookupCreationNamespace
2136 * Look up the schema and verify we have CREATE rights on it.
2138 * This is just like LookupExplicitNamespace except for the permission check,
2139 * and that we are willing to create pg_temp if needed.
2141 * Note: calling this may result in a CommandCounterIncrement operation,
2142 * if we have to create or clean out the temp namespace.
2145 LookupCreationNamespace(const char *nspname)
2148 AclResult aclresult;
2150 /* check for pg_temp alias */
2151 if (strcmp(nspname, "pg_temp") == 0)
2153 /* Initialize temp namespace if first time through */
2154 if (!OidIsValid(myTempNamespace))
2155 InitTempTableNamespace();
2156 return myTempNamespace;
2159 namespaceId = GetSysCacheOid(NAMESPACENAME,
2160 CStringGetDatum(nspname),
2162 if (!OidIsValid(namespaceId))
2164 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2165 errmsg("schema \"%s\" does not exist", nspname)));
2167 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
2168 if (aclresult != ACLCHECK_OK)
2169 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2176 * QualifiedNameGetCreationNamespace
2177 * Given a possibly-qualified name for an object (in List-of-Values
2178 * format), determine what namespace the object should be created in.
2179 * Also extract and return the object name (last component of list).
2181 * Note: this does not apply any permissions check. Callers must check
2182 * for CREATE rights on the selected namespace when appropriate.
2184 * Note: calling this may result in a CommandCounterIncrement operation,
2185 * if we have to create or clean out the temp namespace.
2188 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
2193 /* deconstruct the name list */
2194 DeconstructQualifiedName(names, &schemaname, objname_p);
2198 /* check for pg_temp alias */
2199 if (strcmp(schemaname, "pg_temp") == 0)
2201 /* Initialize temp namespace if first time through */
2202 if (!OidIsValid(myTempNamespace))
2203 InitTempTableNamespace();
2204 return myTempNamespace;
2206 /* use exact schema given */
2207 namespaceId = GetSysCacheOid(NAMESPACENAME,
2208 CStringGetDatum(schemaname),
2210 if (!OidIsValid(namespaceId))
2212 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2213 errmsg("schema \"%s\" does not exist", schemaname)));
2214 /* we do not check for USAGE rights here! */
2218 /* use the default creation namespace */
2219 recomputeNamespacePath();
2220 if (activeTempCreationPending)
2222 /* Need to initialize temp namespace */
2223 InitTempTableNamespace();
2224 return myTempNamespace;
2226 namespaceId = activeCreationNamespace;
2227 if (!OidIsValid(namespaceId))
2229 (errcode(ERRCODE_UNDEFINED_SCHEMA),
2230 errmsg("no schema has been selected to create in")));
2237 * makeRangeVarFromNameList
2238 * Utility routine to convert a qualified-name list into RangeVar form.
2241 makeRangeVarFromNameList(List *names)
2243 RangeVar *rel = makeRangeVar(NULL, NULL, -1);
2245 switch (list_length(names))
2248 rel->relname = strVal(linitial(names));
2251 rel->schemaname = strVal(linitial(names));
2252 rel->relname = strVal(lsecond(names));
2255 rel->catalogname = strVal(linitial(names));
2256 rel->schemaname = strVal(lsecond(names));
2257 rel->relname = strVal(lthird(names));
2261 (errcode(ERRCODE_SYNTAX_ERROR),
2262 errmsg("improper relation name (too many dotted names): %s",
2263 NameListToString(names))));
2272 * Utility routine to convert a qualified-name list into a string.
2274 * This is used primarily to form error messages, and so we do not quote
2275 * the list elements, for the sake of legibility.
2277 * In most scenarios the list elements should always be Value strings,
2278 * but we also allow A_Star for the convenience of ColumnRef processing.
2281 NameListToString(List *names)
2283 StringInfoData string;
2286 initStringInfo(&string);
2290 Node *name = (Node *) lfirst(l);
2292 if (l != list_head(names))
2293 appendStringInfoChar(&string, '.');
2295 if (IsA(name, String))
2296 appendStringInfoString(&string, strVal(name));
2297 else if (IsA(name, A_Star))
2298 appendStringInfoString(&string, "*");
2300 elog(ERROR, "unexpected node type in name list: %d",
2301 (int) nodeTag(name));
2308 * NameListToQuotedString
2309 * Utility routine to convert a qualified-name list into a string.
2311 * Same as above except that names will be double-quoted where necessary,
2312 * so the string could be re-parsed (eg, by textToQualifiedNameList).
2315 NameListToQuotedString(List *names)
2317 StringInfoData string;
2320 initStringInfo(&string);
2324 if (l != list_head(names))
2325 appendStringInfoChar(&string, '.');
2326 appendStringInfoString(&string, quote_identifier(strVal(lfirst(l))));
2333 * isTempNamespace - is the given namespace my temporary-table namespace?
2336 isTempNamespace(Oid namespaceId)
2338 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
2344 * isTempToastNamespace - is the given namespace my temporary-toast-table
2348 isTempToastNamespace(Oid namespaceId)
2350 if (OidIsValid(myTempToastNamespace) && myTempToastNamespace == namespaceId)
2356 * isTempOrToastNamespace - is the given namespace my temporary-table
2357 * namespace or my temporary-toast-table namespace?
2360 isTempOrToastNamespace(Oid namespaceId)
2362 if (OidIsValid(myTempNamespace) &&
2363 (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId))
2369 * isAnyTempNamespace - is the given namespace a temporary-table namespace
2370 * (either my own, or another backend's)? Temporary-toast-table namespaces
2371 * are included, too.
2374 isAnyTempNamespace(Oid namespaceId)
2379 /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
2380 nspname = get_namespace_name(namespaceId);
2382 return false; /* no such namespace? */
2383 result = (strncmp(nspname, "pg_temp_", 8) == 0) ||
2384 (strncmp(nspname, "pg_toast_temp_", 14) == 0);
2390 * isOtherTempNamespace - is the given namespace some other backend's
2391 * temporary-table namespace (including temporary-toast-table namespaces)?
2393 * Note: for most purposes in the C code, this function is obsolete. Use
2394 * RELATION_IS_OTHER_TEMP() instead to detect non-local temp relations.
2397 isOtherTempNamespace(Oid namespaceId)
2399 /* If it's my own temp namespace, say "false" */
2400 if (isTempOrToastNamespace(namespaceId))
2402 /* Else, if it's any temp namespace, say "true" */
2403 return isAnyTempNamespace(namespaceId);
2407 * GetTempNamespaceBackendId - if the given namespace is a temporary-table
2408 * namespace (either my own, or another backend's), return the BackendId
2409 * that owns it. Temporary-toast-table namespaces are included, too.
2410 * If it isn't a temp namespace, return -1.
2413 GetTempNamespaceBackendId(Oid namespaceId)
2418 /* See if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
2419 nspname = get_namespace_name(namespaceId);
2421 return -1; /* no such namespace? */
2422 if (strncmp(nspname, "pg_temp_", 8) == 0)
2423 result = atoi(nspname + 8);
2424 else if (strncmp(nspname, "pg_toast_temp_", 14) == 0)
2425 result = atoi(nspname + 14);
2433 * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
2434 * which must already be assigned. (This is only used when creating a toast
2435 * table for a temp table, so we must have already done InitTempTableNamespace)
2438 GetTempToastNamespace(void)
2440 Assert(OidIsValid(myTempToastNamespace));
2441 return myTempToastNamespace;
2446 * GetOverrideSearchPath - fetch current search path definition in form
2447 * used by PushOverrideSearchPath.
2449 * The result structure is allocated in the specified memory context
2450 * (which might or might not be equal to CurrentMemoryContext); but any
2451 * junk created by revalidation calculations will be in CurrentMemoryContext.
2453 OverrideSearchPath *
2454 GetOverrideSearchPath(MemoryContext context)
2456 OverrideSearchPath *result;
2458 MemoryContext oldcxt;
2460 recomputeNamespacePath();
2462 oldcxt = MemoryContextSwitchTo(context);
2464 result = (OverrideSearchPath *) palloc0(sizeof(OverrideSearchPath));
2465 schemas = list_copy(activeSearchPath);
2466 while (schemas && linitial_oid(schemas) != activeCreationNamespace)
2468 if (linitial_oid(schemas) == myTempNamespace)
2469 result->addTemp = true;
2472 Assert(linitial_oid(schemas) == PG_CATALOG_NAMESPACE);
2473 result->addCatalog = true;
2475 schemas = list_delete_first(schemas);
2477 result->schemas = schemas;
2479 MemoryContextSwitchTo(oldcxt);
2485 * PushOverrideSearchPath - temporarily override the search path
2487 * We allow nested overrides, hence the push/pop terminology. The GUC
2488 * search_path variable is ignored while an override is active.
2491 PushOverrideSearchPath(OverrideSearchPath *newpath)
2493 OverrideStackEntry *entry;
2496 MemoryContext oldcxt;
2499 * Copy the list for safekeeping, and insert implicitly-searched
2500 * namespaces as needed. This code should track recomputeNamespacePath.
2502 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
2504 oidlist = list_copy(newpath->schemas);
2507 * Remember the first member of the explicit list.
2510 firstNS = InvalidOid;
2512 firstNS = linitial_oid(oidlist);
2515 * Add any implicitly-searched namespaces to the list. Note these go on
2516 * the front, not the back; also notice that we do not check USAGE
2517 * permissions for these.
2519 if (newpath->addCatalog)
2520 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
2522 if (newpath->addTemp)
2524 Assert(OidIsValid(myTempNamespace));
2525 oidlist = lcons_oid(myTempNamespace, oidlist);
2529 * Build the new stack entry, then insert it at the head of the list.
2531 entry = (OverrideStackEntry *) palloc(sizeof(OverrideStackEntry));
2532 entry->searchPath = oidlist;
2533 entry->creationNamespace = firstNS;
2534 entry->nestLevel = GetCurrentTransactionNestLevel();
2536 overrideStack = lcons(entry, overrideStack);
2538 /* And make it active. */
2539 activeSearchPath = entry->searchPath;
2540 activeCreationNamespace = entry->creationNamespace;
2541 activeTempCreationPending = false; /* XXX is this OK? */
2543 MemoryContextSwitchTo(oldcxt);
2547 * PopOverrideSearchPath - undo a previous PushOverrideSearchPath
2549 * Any push during a (sub)transaction will be popped automatically at abort.
2550 * But it's caller error if a push isn't popped in normal control flow.
2553 PopOverrideSearchPath(void)
2555 OverrideStackEntry *entry;
2557 /* Sanity checks. */
2558 if (overrideStack == NIL)
2559 elog(ERROR, "bogus PopOverrideSearchPath call");
2560 entry = (OverrideStackEntry *) linitial(overrideStack);
2561 if (entry->nestLevel != GetCurrentTransactionNestLevel())
2562 elog(ERROR, "bogus PopOverrideSearchPath call");
2564 /* Pop the stack and free storage. */
2565 overrideStack = list_delete_first(overrideStack);
2566 list_free(entry->searchPath);
2569 /* Activate the next level down. */
2572 entry = (OverrideStackEntry *) linitial(overrideStack);
2573 activeSearchPath = entry->searchPath;
2574 activeCreationNamespace = entry->creationNamespace;
2575 activeTempCreationPending = false; /* XXX is this OK? */
2579 /* If not baseSearchPathValid, this is useless but harmless */
2580 activeSearchPath = baseSearchPath;
2581 activeCreationNamespace = baseCreationNamespace;
2582 activeTempCreationPending = baseTempCreationPending;
2588 * FindConversionByName - find a conversion by possibly qualified name
2591 FindConversionByName(List *name)
2594 char *conversion_name;
2599 /* deconstruct the name list */
2600 DeconstructQualifiedName(name, &schemaname, &conversion_name);
2604 /* use exact schema given */
2605 namespaceId = LookupExplicitNamespace(schemaname);
2606 return FindConversion(conversion_name, namespaceId);
2610 /* search for it in search path */
2611 recomputeNamespacePath();
2613 foreach(l, activeSearchPath)
2615 namespaceId = lfirst_oid(l);
2617 if (namespaceId == myTempNamespace)
2618 continue; /* do not look in temp namespace */
2620 conoid = FindConversion(conversion_name, namespaceId);
2621 if (OidIsValid(conoid))
2626 /* Not found in path */
2631 * FindDefaultConversionProc - find default encoding conversion proc
2634 FindDefaultConversionProc(int4 for_encoding, int4 to_encoding)
2639 recomputeNamespacePath();
2641 foreach(l, activeSearchPath)
2643 Oid namespaceId = lfirst_oid(l);
2645 if (namespaceId == myTempNamespace)
2646 continue; /* do not look in temp namespace */
2648 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
2649 if (OidIsValid(proc))
2653 /* Not found in path */
2658 * recomputeNamespacePath - recompute path derived variables if needed.
2661 recomputeNamespacePath(void)
2663 Oid roleid = GetUserId();
2671 MemoryContext oldcxt;
2673 /* Do nothing if an override search spec is active. */
2677 /* Do nothing if path is already valid. */
2678 if (baseSearchPathValid && namespaceUser == roleid)
2681 /* Need a modifiable copy of namespace_search_path string */
2682 rawname = pstrdup(namespace_search_path);
2684 /* Parse string into list of identifiers */
2685 if (!SplitIdentifierString(rawname, ',', &namelist))
2687 /* syntax error in name list */
2688 /* this should not happen if GUC checked check_search_path */
2689 elog(ERROR, "invalid list syntax");
2693 * Convert the list of names to a list of OIDs. If any names are not
2694 * recognizable or we don't have read access, just leave them out of the
2695 * list. (We can't raise an error, since the search_path setting has
2696 * already been accepted.) Don't make duplicate entries, either.
2699 temp_missing = false;
2700 foreach(l, namelist)
2702 char *curname = (char *) lfirst(l);
2705 if (strcmp(curname, "$user") == 0)
2707 /* $user --- substitute namespace matching user name, if any */
2710 tuple = SearchSysCache(AUTHOID,
2711 ObjectIdGetDatum(roleid),
2713 if (HeapTupleIsValid(tuple))
2717 rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
2718 namespaceId = GetSysCacheOid(NAMESPACENAME,
2719 CStringGetDatum(rname),
2721 ReleaseSysCache(tuple);
2722 if (OidIsValid(namespaceId) &&
2723 !list_member_oid(oidlist, namespaceId) &&
2724 pg_namespace_aclcheck(namespaceId, roleid,
2725 ACL_USAGE) == ACLCHECK_OK)
2726 oidlist = lappend_oid(oidlist, namespaceId);
2729 else if (strcmp(curname, "pg_temp") == 0)
2731 /* pg_temp --- substitute temp namespace, if any */
2732 if (OidIsValid(myTempNamespace))
2734 if (!list_member_oid(oidlist, myTempNamespace))
2735 oidlist = lappend_oid(oidlist, myTempNamespace);
2739 /* If it ought to be the creation namespace, set flag */
2741 temp_missing = true;
2746 /* normal namespace reference */
2747 namespaceId = GetSysCacheOid(NAMESPACENAME,
2748 CStringGetDatum(curname),
2750 if (OidIsValid(namespaceId) &&
2751 !list_member_oid(oidlist, namespaceId) &&
2752 pg_namespace_aclcheck(namespaceId, roleid,
2753 ACL_USAGE) == ACLCHECK_OK)
2754 oidlist = lappend_oid(oidlist, namespaceId);
2759 * Remember the first member of the explicit list. (Note: this is
2760 * nominally wrong if temp_missing, but we need it anyway to distinguish
2761 * explicit from implicit mention of pg_catalog.)
2764 firstNS = InvalidOid;
2766 firstNS = linitial_oid(oidlist);
2769 * Add any implicitly-searched namespaces to the list. Note these go on
2770 * the front, not the back; also notice that we do not check USAGE
2771 * permissions for these.
2773 if (!list_member_oid(oidlist, PG_CATALOG_NAMESPACE))
2774 oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
2776 if (OidIsValid(myTempNamespace) &&
2777 !list_member_oid(oidlist, myTempNamespace))
2778 oidlist = lcons_oid(myTempNamespace, oidlist);
2781 * Now that we've successfully built the new list of namespace OIDs, save
2782 * it in permanent storage.
2784 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
2785 newpath = list_copy(oidlist);
2786 MemoryContextSwitchTo(oldcxt);
2788 /* Now safe to assign to state variables. */
2789 list_free(baseSearchPath);
2790 baseSearchPath = newpath;
2791 baseCreationNamespace = firstNS;
2792 baseTempCreationPending = temp_missing;
2794 /* Mark the path valid. */
2795 baseSearchPathValid = true;
2796 namespaceUser = roleid;
2798 /* And make it active. */
2799 activeSearchPath = baseSearchPath;
2800 activeCreationNamespace = baseCreationNamespace;
2801 activeTempCreationPending = baseTempCreationPending;
2805 list_free(namelist);
2810 * InitTempTableNamespace
2811 * Initialize temp table namespace on first use in a particular backend
2814 InitTempTableNamespace(void)
2816 char namespaceName[NAMEDATALEN];
2820 Assert(!OidIsValid(myTempNamespace));
2823 * First, do permission check to see if we are authorized to make temp
2824 * tables. We use a nonstandard error message here since "databasename:
2825 * permission denied" might be a tad cryptic.
2827 * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
2828 * that's necessary since current user ID could change during the session.
2829 * But there's no need to make the namespace in the first place until a
2830 * temp table creation request is made by someone with appropriate rights.
2832 if (pg_database_aclcheck(MyDatabaseId, GetUserId(),
2833 ACL_CREATE_TEMP) != ACLCHECK_OK)
2835 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2836 errmsg("permission denied to create temporary tables in database \"%s\"",
2837 get_database_name(MyDatabaseId))));
2839 snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
2841 namespaceId = GetSysCacheOid(NAMESPACENAME,
2842 CStringGetDatum(namespaceName),
2844 if (!OidIsValid(namespaceId))
2847 * First use of this temp namespace in this database; create it. The
2848 * temp namespaces are always owned by the superuser. We leave their
2849 * permissions at default --- i.e., no access except to superuser ---
2850 * to ensure that unprivileged users can't peek at other backends'
2851 * temp tables. This works because the places that access the temp
2852 * namespace for my own backend skip permissions checks on it.
2854 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID);
2855 /* Advance command counter to make namespace visible */
2856 CommandCounterIncrement();
2861 * If the namespace already exists, clean it out (in case the former
2862 * owner crashed without doing so).
2864 RemoveTempRelations(namespaceId);
2868 * If the corresponding toast-table namespace doesn't exist yet, create it.
2869 * (We assume there is no need to clean it out if it does exist, since
2870 * dropping a parent table should make its toast table go away.)
2872 snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
2875 toastspaceId = GetSysCacheOid(NAMESPACENAME,
2876 CStringGetDatum(namespaceName),
2878 if (!OidIsValid(toastspaceId))
2880 toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID);
2881 /* Advance command counter to make namespace visible */
2882 CommandCounterIncrement();
2886 * Okay, we've prepared the temp namespace ... but it's not committed yet,
2887 * so all our work could be undone by transaction rollback. Set flag for
2888 * AtEOXact_Namespace to know what to do.
2890 myTempNamespace = namespaceId;
2891 myTempToastNamespace = toastspaceId;
2893 /* It should not be done already. */
2894 AssertState(myTempNamespaceSubID == InvalidSubTransactionId);
2895 myTempNamespaceSubID = GetCurrentSubTransactionId();
2897 baseSearchPathValid = false; /* need to rebuild list */
2901 * End-of-transaction cleanup for namespaces.
2904 AtEOXact_Namespace(bool isCommit)
2907 * If we abort the transaction in which a temp namespace was selected,
2908 * we'll have to do any creation or cleanout work over again. So, just
2909 * forget the namespace entirely until next time. On the other hand, if
2910 * we commit then register an exit callback to clean out the temp tables
2911 * at backend shutdown. (We only want to register the callback once per
2912 * session, so this is a good place to do it.)
2914 if (myTempNamespaceSubID != InvalidSubTransactionId)
2917 on_shmem_exit(RemoveTempRelationsCallback, 0);
2920 myTempNamespace = InvalidOid;
2921 myTempToastNamespace = InvalidOid;
2922 baseSearchPathValid = false; /* need to rebuild list */
2924 myTempNamespaceSubID = InvalidSubTransactionId;
2928 * Clean up if someone failed to do PopOverrideSearchPath
2933 elog(WARNING, "leaked override search path");
2934 while (overrideStack)
2936 OverrideStackEntry *entry;
2938 entry = (OverrideStackEntry *) linitial(overrideStack);
2939 overrideStack = list_delete_first(overrideStack);
2940 list_free(entry->searchPath);
2943 /* If not baseSearchPathValid, this is useless but harmless */
2944 activeSearchPath = baseSearchPath;
2945 activeCreationNamespace = baseCreationNamespace;
2946 activeTempCreationPending = baseTempCreationPending;
2951 * AtEOSubXact_Namespace
2953 * At subtransaction commit, propagate the temp-namespace-creation
2954 * flag to the parent subtransaction.
2956 * At subtransaction abort, forget the flag if we set it up.
2959 AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid,
2960 SubTransactionId parentSubid)
2962 OverrideStackEntry *entry;
2964 if (myTempNamespaceSubID == mySubid)
2967 myTempNamespaceSubID = parentSubid;
2970 myTempNamespaceSubID = InvalidSubTransactionId;
2971 /* TEMP namespace creation failed, so reset state */
2972 myTempNamespace = InvalidOid;
2973 myTempToastNamespace = InvalidOid;
2974 baseSearchPathValid = false; /* need to rebuild list */
2979 * Clean up if someone failed to do PopOverrideSearchPath
2981 while (overrideStack)
2983 entry = (OverrideStackEntry *) linitial(overrideStack);
2984 if (entry->nestLevel < GetCurrentTransactionNestLevel())
2987 elog(WARNING, "leaked override search path");
2988 overrideStack = list_delete_first(overrideStack);
2989 list_free(entry->searchPath);
2993 /* Activate the next level down. */
2996 entry = (OverrideStackEntry *) linitial(overrideStack);
2997 activeSearchPath = entry->searchPath;
2998 activeCreationNamespace = entry->creationNamespace;
2999 activeTempCreationPending = false; /* XXX is this OK? */
3003 /* If not baseSearchPathValid, this is useless but harmless */
3004 activeSearchPath = baseSearchPath;
3005 activeCreationNamespace = baseCreationNamespace;
3006 activeTempCreationPending = baseTempCreationPending;
3011 * Remove all relations in the specified temp namespace.
3013 * This is called at backend shutdown (if we made any temp relations).
3014 * It is also called when we begin using a pre-existing temp namespace,
3015 * in order to clean out any relations that might have been created by
3016 * a crashed backend.
3019 RemoveTempRelations(Oid tempNamespaceId)
3021 ObjectAddress object;
3024 * We want to get rid of everything in the target namespace, but not the
3025 * namespace itself (deleting it only to recreate it later would be a
3026 * waste of cycles). We do this by finding everything that has a
3027 * dependency on the namespace.
3029 object.classId = NamespaceRelationId;
3030 object.objectId = tempNamespaceId;
3031 object.objectSubId = 0;
3033 deleteWhatDependsOn(&object, false);
3037 * Callback to remove temp relations at backend exit.
3040 RemoveTempRelationsCallback(int code, Datum arg)
3042 if (OidIsValid(myTempNamespace)) /* should always be true */
3044 /* Need to ensure we have a usable transaction. */
3045 AbortOutOfAnyTransaction();
3046 StartTransactionCommand();
3048 RemoveTempRelations(myTempNamespace);
3050 CommitTransactionCommand();
3055 * Remove all temp tables from the temporary namespace.
3058 ResetTempTableNamespace(void)
3060 if (OidIsValid(myTempNamespace))
3061 RemoveTempRelations(myTempNamespace);
3066 * Routines for handling the GUC variable 'search_path'.
3069 /* assign_hook: validate new search_path, do extra actions as needed */
3071 assign_search_path(const char *newval, bool doit, GucSource source)
3077 /* Need a modifiable copy of string */
3078 rawname = pstrdup(newval);
3080 /* Parse string into list of identifiers */
3081 if (!SplitIdentifierString(rawname, ',', &namelist))
3083 /* syntax error in name list */
3085 list_free(namelist);
3090 * If we aren't inside a transaction, we cannot do database access so
3091 * cannot verify the individual names. Must accept the list on faith.
3093 if (source >= PGC_S_INTERACTIVE && IsTransactionState())
3096 * Verify that all the names are either valid namespace names or
3097 * "$user" or "pg_temp". We do not require $user to correspond to a
3098 * valid namespace, and pg_temp might not exist yet. We do not check
3099 * for USAGE rights, either; should we?
3101 * When source == PGC_S_TEST, we are checking the argument of an ALTER
3102 * DATABASE SET or ALTER USER SET command. It could be that the
3103 * intended use of the search path is for some other database, so we
3104 * should not error out if it mentions schemas not present in the
3105 * current database. We reduce the message to NOTICE instead.
3107 foreach(l, namelist)
3109 char *curname = (char *) lfirst(l);
3111 if (strcmp(curname, "$user") == 0)
3113 if (strcmp(curname, "pg_temp") == 0)
3115 if (!SearchSysCacheExists(NAMESPACENAME,
3116 CStringGetDatum(curname),
3118 ereport((source == PGC_S_TEST) ? NOTICE : ERROR,
3119 (errcode(ERRCODE_UNDEFINED_SCHEMA),
3120 errmsg("schema \"%s\" does not exist", curname)));
3125 list_free(namelist);
3128 * We mark the path as needing recomputation, but don't do anything until
3129 * it's needed. This avoids trying to do database access during GUC
3133 baseSearchPathValid = false;
3139 * InitializeSearchPath: initialize module during InitPostgres.
3141 * This is called after we are up enough to be able to do catalog lookups.
3144 InitializeSearchPath(void)
3146 if (IsBootstrapProcessingMode())
3149 * In bootstrap mode, the search path must be 'pg_catalog' so that
3150 * tables are created in the proper namespace; ignore the GUC setting.
3152 MemoryContext oldcxt;
3154 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
3155 baseSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
3156 MemoryContextSwitchTo(oldcxt);
3157 baseCreationNamespace = PG_CATALOG_NAMESPACE;
3158 baseTempCreationPending = false;
3159 baseSearchPathValid = true;
3160 namespaceUser = GetUserId();
3161 activeSearchPath = baseSearchPath;
3162 activeCreationNamespace = baseCreationNamespace;
3163 activeTempCreationPending = baseTempCreationPending;
3168 * In normal mode, arrange for a callback on any syscache invalidation
3169 * of pg_namespace rows.
3171 CacheRegisterSyscacheCallback(NAMESPACEOID,
3174 /* Force search path to be recomputed on next use */
3175 baseSearchPathValid = false;
3181 * Syscache inval callback function
3184 NamespaceCallback(Datum arg, int cacheid, ItemPointer tuplePtr)
3186 /* Force search path to be recomputed on next use */
3187 baseSearchPathValid = false;
3191 * Fetch the active search path. The return value is a palloc'ed list
3192 * of OIDs; the caller is responsible for freeing this storage as
3195 * The returned list includes the implicitly-prepended namespaces only if
3196 * includeImplicit is true.
3198 * Note: calling this may result in a CommandCounterIncrement operation,
3199 * if we have to create or clean out the temp namespace.
3202 fetch_search_path(bool includeImplicit)
3206 recomputeNamespacePath();
3209 * If the temp namespace should be first, force it to exist. This is so
3210 * that callers can trust the result to reflect the actual default
3211 * creation namespace. It's a bit bogus to do this here, since
3212 * current_schema() is supposedly a stable function without side-effects,
3213 * but the alternatives seem worse.
3215 if (activeTempCreationPending)
3217 InitTempTableNamespace();
3218 recomputeNamespacePath();
3221 result = list_copy(activeSearchPath);
3222 if (!includeImplicit)
3224 while (result && linitial_oid(result) != activeCreationNamespace)
3225 result = list_delete_first(result);
3232 * Fetch the active search path into a caller-allocated array of OIDs.
3233 * Returns the number of path entries. (If this is more than sarray_len,
3234 * then the data didn't fit and is not all stored.)
3236 * The returned list always includes the implicitly-prepended namespaces,
3237 * but never includes the temp namespace. (This is suitable for existing
3238 * users, which would want to ignore the temp namespace anyway.) This
3239 * definition allows us to not worry about initializing the temp namespace.
3242 fetch_search_path_array(Oid *sarray, int sarray_len)
3247 recomputeNamespacePath();
3249 foreach(l, activeSearchPath)
3251 Oid namespaceId = lfirst_oid(l);
3253 if (namespaceId == myTempNamespace)
3254 continue; /* do not include temp namespace */
3256 if (count < sarray_len)
3257 sarray[count] = namespaceId;
3266 * Export the FooIsVisible functions as SQL-callable functions.
3268 * Note: as of Postgres 8.4, these will silently return NULL if called on
3269 * a nonexistent object OID, rather than failing. This is to avoid race
3270 * condition errors when a query that's scanning a catalog using an MVCC
3271 * snapshot uses one of these functions. The underlying IsVisible functions
3272 * operate on SnapshotNow semantics and so might see the object as already
3273 * gone when it's still visible to the MVCC snapshot. (There is no race
3274 * condition in the current coding because we don't accept sinval messages
3275 * between the SearchSysCacheExists test and the subsequent lookup.)
3279 pg_table_is_visible(PG_FUNCTION_ARGS)
3281 Oid oid = PG_GETARG_OID(0);
3283 if (!SearchSysCacheExists(RELOID,
3284 ObjectIdGetDatum(oid),
3288 PG_RETURN_BOOL(RelationIsVisible(oid));
3292 pg_type_is_visible(PG_FUNCTION_ARGS)
3294 Oid oid = PG_GETARG_OID(0);
3296 if (!SearchSysCacheExists(TYPEOID,
3297 ObjectIdGetDatum(oid),
3301 PG_RETURN_BOOL(TypeIsVisible(oid));
3305 pg_function_is_visible(PG_FUNCTION_ARGS)
3307 Oid oid = PG_GETARG_OID(0);
3309 if (!SearchSysCacheExists(PROCOID,
3310 ObjectIdGetDatum(oid),
3314 PG_RETURN_BOOL(FunctionIsVisible(oid));
3318 pg_operator_is_visible(PG_FUNCTION_ARGS)
3320 Oid oid = PG_GETARG_OID(0);
3322 if (!SearchSysCacheExists(OPEROID,
3323 ObjectIdGetDatum(oid),
3327 PG_RETURN_BOOL(OperatorIsVisible(oid));
3331 pg_opclass_is_visible(PG_FUNCTION_ARGS)
3333 Oid oid = PG_GETARG_OID(0);
3335 if (!SearchSysCacheExists(CLAOID,
3336 ObjectIdGetDatum(oid),
3340 PG_RETURN_BOOL(OpclassIsVisible(oid));
3344 pg_conversion_is_visible(PG_FUNCTION_ARGS)
3346 Oid oid = PG_GETARG_OID(0);
3348 if (!SearchSysCacheExists(CONVOID,
3349 ObjectIdGetDatum(oid),
3353 PG_RETURN_BOOL(ConversionIsVisible(oid));
3357 pg_ts_parser_is_visible(PG_FUNCTION_ARGS)
3359 Oid oid = PG_GETARG_OID(0);
3361 if (!SearchSysCacheExists(TSPARSEROID,
3362 ObjectIdGetDatum(oid),
3366 PG_RETURN_BOOL(TSParserIsVisible(oid));
3370 pg_ts_dict_is_visible(PG_FUNCTION_ARGS)
3372 Oid oid = PG_GETARG_OID(0);
3374 if (!SearchSysCacheExists(TSDICTOID,
3375 ObjectIdGetDatum(oid),
3379 PG_RETURN_BOOL(TSDictionaryIsVisible(oid));
3383 pg_ts_template_is_visible(PG_FUNCTION_ARGS)
3385 Oid oid = PG_GETARG_OID(0);
3387 if (!SearchSysCacheExists(TSTEMPLATEOID,
3388 ObjectIdGetDatum(oid),
3392 PG_RETURN_BOOL(TSTemplateIsVisible(oid));
3396 pg_ts_config_is_visible(PG_FUNCTION_ARGS)
3398 Oid oid = PG_GETARG_OID(0);
3400 if (!SearchSysCacheExists(TSCONFIGOID,
3401 ObjectIdGetDatum(oid),
3405 PG_RETURN_BOOL(TSConfigIsVisible(oid));
3409 pg_my_temp_schema(PG_FUNCTION_ARGS)
3411 PG_RETURN_OID(myTempNamespace);
3415 pg_is_other_temp_schema(PG_FUNCTION_ARGS)
3417 Oid oid = PG_GETARG_OID(0);
3419 PG_RETURN_BOOL(isOtherTempNamespace(oid));