1 /*-------------------------------------------------------------------------
4 * code to support accessing and searching namespaces
6 * This is separate from pg_namespace.c, which contains the routines that
7 * directly manipulate the pg_namespace system catalog. This module
8 * provides routines associated with defining a "namespace search path"
9 * and implementing search-path-controlled searches.
12 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.44 2003/01/10 22:03:27 petere Exp $
18 *-------------------------------------------------------------------------
22 #include "access/heapam.h"
23 #include "access/xact.h"
24 #include "catalog/catalog.h"
25 #include "catalog/catname.h"
26 #include "catalog/dependency.h"
27 #include "catalog/heap.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_conversion.h"
30 #include "catalog/pg_inherits.h"
31 #include "catalog/pg_namespace.h"
32 #include "catalog/pg_opclass.h"
33 #include "catalog/pg_operator.h"
34 #include "catalog/pg_proc.h"
35 #include "catalog/pg_shadow.h"
36 #include "catalog/pg_type.h"
37 #include "lib/stringinfo.h"
38 #include "miscadmin.h"
39 #include "nodes/makefuncs.h"
40 #include "storage/backendid.h"
41 #include "storage/ipc.h"
42 #include "utils/acl.h"
43 #include "utils/builtins.h"
44 #include "utils/catcache.h"
45 #include "utils/fmgroids.h"
46 #include "utils/guc.h"
47 #include "utils/inval.h"
48 #include "utils/lsyscache.h"
49 #include "utils/syscache.h"
53 * The namespace search path is a possibly-empty list of namespace OIDs.
54 * In addition to the explicit list, several implicitly-searched namespaces
57 * 1. If a "special" namespace has been set by PushSpecialNamespace, it is
58 * always searched first. (This is a hack for CREATE SCHEMA.)
60 * 2. If a TEMP table namespace has been initialized in this session, it
61 * is always searched just after any special namespace.
63 * 3. The system catalog namespace is always searched. If the system
64 * namespace is present in the explicit path then it will be searched in
65 * the specified order; otherwise it will be searched after TEMP tables and
66 * *before* the explicit list. (It might seem that the system namespace
67 * should be implicitly last, but this behavior appears to be required by
68 * SQL99. Also, this provides a way to search the system namespace first
69 * without thereby making it the default creation target namespace.)
71 * The default creation target namespace is normally equal to the first
72 * element of the explicit list, but is the "special" namespace when one
73 * has been set. If the explicit list is empty and there is no special
74 * namespace, there is no default target.
76 * In bootstrap mode, the search path is set equal to 'pg_catalog', so that
77 * the system namespace is the only one searched or inserted into.
78 * The initdb script is also careful to set search_path to 'pg_catalog' for
79 * its post-bootstrap standalone backend runs. Otherwise the default search
80 * path is determined by GUC. The factory default path contains the PUBLIC
81 * namespace (if it exists), preceded by the user's personal namespace
84 * If namespaceSearchPathValid is false, then namespaceSearchPath (and other
85 * derived variables) need to be recomputed from namespace_search_path.
86 * We mark it invalid upon an assignment to namespace_search_path or receipt
87 * of a syscache invalidation event for pg_namespace. The recomputation
88 * is done during the next lookup attempt.
90 * Any namespaces mentioned in namespace_search_path that are not readable
91 * by the current user ID are simply left out of namespaceSearchPath; so
92 * we have to be willing to recompute the path when current userid changes.
93 * namespaceUser is the userid the path has been computed for.
96 static List *namespaceSearchPath = NIL;
98 static Oid namespaceUser = InvalidOid;
100 /* default place to create stuff; if InvalidOid, no default */
101 static Oid defaultCreationNamespace = InvalidOid;
103 /* first explicit member of list; usually same as defaultCreationNamespace */
104 static Oid firstExplicitNamespace = InvalidOid;
106 /* The above four values are valid only if namespaceSearchPathValid */
107 static bool namespaceSearchPathValid = true;
110 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
111 * in a particular backend session (this happens when a CREATE TEMP TABLE
112 * command is first executed). Thereafter it's the OID of the temp namespace.
113 * firstTempTransaction flags whether we've committed creation of the TEMP
116 static Oid myTempNamespace = InvalidOid;
118 static bool firstTempTransaction = false;
121 * "Special" namespace for CREATE SCHEMA. If set, it's the first search
122 * path element, and also the default creation namespace.
124 static Oid mySpecialNamespace = InvalidOid;
127 * This is the text equivalent of the search path --- it's the value
128 * of the GUC variable 'search_path'.
130 char *namespace_search_path = NULL;
133 /* Local functions */
134 static void recomputeNamespacePath(void);
135 static void InitTempTableNamespace(void);
136 static void RemoveTempRelations(Oid tempNamespaceId);
137 static void RemoveTempRelationsCallback(void);
138 static void NamespaceCallback(Datum arg, Oid relid);
140 /* These don't really need to appear in any header file */
141 Datum pg_table_is_visible(PG_FUNCTION_ARGS);
142 Datum pg_type_is_visible(PG_FUNCTION_ARGS);
143 Datum pg_function_is_visible(PG_FUNCTION_ARGS);
144 Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
145 Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
146 Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
151 * Given a RangeVar describing an existing relation,
152 * select the proper namespace and look up the relation OID.
154 * If the relation is not found, return InvalidOid if failOK = true,
155 * otherwise raise an error.
158 RangeVarGetRelid(const RangeVar *relation, bool failOK)
164 * We check the catalog name and then ignore it.
166 if (relation->catalogname)
168 if (strcmp(relation->catalogname, DatabaseName) != 0)
169 elog(ERROR, "Cross-database references are not implemented");
172 if (relation->schemaname)
174 /* use exact schema given */
175 namespaceId = LookupExplicitNamespace(relation->schemaname);
176 relId = get_relname_relid(relation->relname, namespaceId);
180 /* search the namespace path */
181 relId = RelnameGetRelid(relation->relname);
184 if (!OidIsValid(relId) && !failOK)
186 if (relation->schemaname)
187 elog(ERROR, "Relation \"%s\".\"%s\" does not exist",
188 relation->schemaname, relation->relname);
190 elog(ERROR, "Relation \"%s\" does not exist",
197 * RangeVarGetCreationNamespace
198 * Given a RangeVar describing a to-be-created relation,
199 * choose which namespace to create it in.
201 * Note: calling this may result in a CommandCounterIncrement operation.
202 * That will happen on the first request for a temp table in any particular
203 * backend run; we will need to either create or clean out the temp schema.
206 RangeVarGetCreationNamespace(const RangeVar *newRelation)
211 * We check the catalog name and then ignore it.
213 if (newRelation->catalogname)
215 if (strcmp(newRelation->catalogname, DatabaseName) != 0)
216 elog(ERROR, "Cross-database references are not implemented");
219 if (newRelation->istemp)
221 /* TEMP tables are created in our backend-local temp namespace */
222 if (newRelation->schemaname)
223 elog(ERROR, "TEMP tables may not specify a namespace");
224 /* Initialize temp namespace if first time through */
225 if (!OidIsValid(myTempNamespace))
226 InitTempTableNamespace();
227 return myTempNamespace;
230 if (newRelation->schemaname)
232 /* use exact schema given */
233 namespaceId = GetSysCacheOid(NAMESPACENAME,
234 CStringGetDatum(newRelation->schemaname),
236 if (!OidIsValid(namespaceId))
237 elog(ERROR, "Namespace \"%s\" does not exist",
238 newRelation->schemaname);
239 /* we do not check for USAGE rights here! */
243 /* use the default creation namespace */
244 recomputeNamespacePath();
245 namespaceId = defaultCreationNamespace;
246 if (!OidIsValid(namespaceId))
247 elog(ERROR, "No namespace has been selected to create in");
250 /* Note: callers will check for CREATE rights when appropriate */
257 * Try to resolve an unqualified relation name.
258 * Returns OID if relation found in search path, else InvalidOid.
261 RelnameGetRelid(const char *relname)
266 recomputeNamespacePath();
268 foreach(lptr, namespaceSearchPath)
270 Oid namespaceId = (Oid) lfirsti(lptr);
272 relid = get_relname_relid(relname, namespaceId);
273 if (OidIsValid(relid))
277 /* Not found in path */
282 * RelidGetNamespaceId
283 * Given a relation OID, return the namespace OID.
286 RelidGetNamespaceId(Oid relid)
289 Form_pg_class pg_class_form;
292 tuple = SearchSysCache(RELOID,
293 ObjectIdGetDatum(relid),
295 if (!HeapTupleIsValid(tuple))
296 elog(ERROR, "cache lookup failed for relation %u", relid);
297 pg_class_form = (Form_pg_class) GETSTRUCT(tuple);
299 result = pg_class_form->relnamespace;
300 ReleaseSysCache(tuple);
307 * Determine whether a relation (identified by OID) is visible in the
308 * current search path. Visible means "would be found by searching
309 * for the unqualified relation name".
312 RelationIsVisible(Oid relid)
315 Form_pg_class relform;
319 reltup = SearchSysCache(RELOID,
320 ObjectIdGetDatum(relid),
322 if (!HeapTupleIsValid(reltup))
323 elog(ERROR, "Cache lookup failed for relation %u", relid);
324 relform = (Form_pg_class) GETSTRUCT(reltup);
326 recomputeNamespacePath();
329 * Quick check: if it ain't in the path at all, it ain't visible.
330 * Items in the system namespace are surely in the path and so we
331 * needn't even do intMember() for them.
333 relnamespace = relform->relnamespace;
334 if (relnamespace != PG_CATALOG_NAMESPACE &&
335 !intMember(relnamespace, namespaceSearchPath))
340 * If it is in the path, it might still not be visible; it could
341 * be hidden by another relation of the same name earlier in the
342 * path. So we must do a slow check to see if this rel would be
343 * found by RelnameGetRelid.
345 char *relname = NameStr(relform->relname);
347 visible = (RelnameGetRelid(relname) == relid);
350 ReleaseSysCache(reltup);
358 * Try to resolve an unqualified datatype name.
359 * Returns OID if type found in search path, else InvalidOid.
361 * This is essentially the same as RelnameGetRelid.
364 TypenameGetTypid(const char *typname)
369 recomputeNamespacePath();
371 foreach(lptr, namespaceSearchPath)
373 Oid namespaceId = (Oid) lfirsti(lptr);
375 typid = GetSysCacheOid(TYPENAMENSP,
376 PointerGetDatum(typname),
377 ObjectIdGetDatum(namespaceId),
379 if (OidIsValid(typid))
383 /* Not found in path */
389 * Determine whether a type (identified by OID) is visible in the
390 * current search path. Visible means "would be found by searching
391 * for the unqualified type name".
394 TypeIsVisible(Oid typid)
397 Form_pg_type typform;
401 typtup = SearchSysCache(TYPEOID,
402 ObjectIdGetDatum(typid),
404 if (!HeapTupleIsValid(typtup))
405 elog(ERROR, "Cache lookup failed for type %u", typid);
406 typform = (Form_pg_type) GETSTRUCT(typtup);
408 recomputeNamespacePath();
411 * Quick check: if it ain't in the path at all, it ain't visible.
412 * Items in the system namespace are surely in the path and so we
413 * needn't even do intMember() for them.
415 typnamespace = typform->typnamespace;
416 if (typnamespace != PG_CATALOG_NAMESPACE &&
417 !intMember(typnamespace, namespaceSearchPath))
422 * If it is in the path, it might still not be visible; it could
423 * be hidden by another type of the same name earlier in the path.
424 * So we must do a slow check to see if this type would be found
425 * by TypenameGetTypid.
427 char *typname = NameStr(typform->typname);
429 visible = (TypenameGetTypid(typname) == typid);
432 ReleaseSysCache(typtup);
439 * FuncnameGetCandidates
440 * Given a possibly-qualified function name and argument count,
441 * retrieve a list of the possible matches.
443 * If nargs is -1, we return all functions matching the given name,
444 * regardless of argument count.
446 * We search a single namespace if the function name is qualified, else
447 * all namespaces in the search path. The return list will never contain
448 * multiple entries with identical argument lists --- in the multiple-
449 * namespace case, we arrange for entries in earlier namespaces to mask
450 * identical entries in later namespaces.
453 FuncnameGetCandidates(List *names, int nargs)
455 FuncCandidateList resultList = NULL;
462 /* deconstruct the name list */
463 DeconstructQualifiedName(names, &schemaname, &funcname);
467 /* use exact schema given */
468 namespaceId = LookupExplicitNamespace(schemaname);
472 /* flag to indicate we need namespace search */
473 namespaceId = InvalidOid;
474 recomputeNamespacePath();
477 /* Search syscache by name and (optionally) nargs only */
479 catlist = SearchSysCacheList(PROCNAMENSP, 2,
480 CStringGetDatum(funcname),
481 Int16GetDatum(nargs),
484 catlist = SearchSysCacheList(PROCNAMENSP, 1,
485 CStringGetDatum(funcname),
488 for (i = 0; i < catlist->n_members; i++)
490 HeapTuple proctup = &catlist->members[i]->tuple;
491 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
493 FuncCandidateList newResult;
495 nargs = procform->pronargs;
497 if (OidIsValid(namespaceId))
499 /* Consider only procs in specified namespace */
500 if (procform->pronamespace != namespaceId)
502 /* No need to check args, they must all be different */
506 /* Consider only procs that are in the search path */
509 foreach(nsp, namespaceSearchPath)
511 if (procform->pronamespace == (Oid) lfirsti(nsp))
516 continue; /* proc is not in search path */
519 * Okay, it's in the search path, but does it have the same
520 * arguments as something we already accepted? If so, keep
521 * only the one that appears earlier in the search path.
523 * If we have an ordered list from SearchSysCacheList (the normal
524 * case), then any conflicting proc must immediately adjoin
525 * this one in the list, so we only need to look at the newest
526 * result item. If we have an unordered list, we have to scan
527 * the whole result list.
531 FuncCandidateList prevResult;
533 if (catlist->ordered)
535 if (nargs == resultList->nargs &&
536 memcmp(procform->proargtypes, resultList->args,
537 nargs * sizeof(Oid)) == 0)
538 prevResult = resultList;
544 for (prevResult = resultList;
546 prevResult = prevResult->next)
548 if (nargs == prevResult->nargs &&
549 memcmp(procform->proargtypes, prevResult->args,
550 nargs * sizeof(Oid)) == 0)
556 /* We have a match with a previous result */
557 Assert(pathpos != prevResult->pathpos);
558 if (pathpos > prevResult->pathpos)
559 continue; /* keep previous result */
560 /* replace previous result */
561 prevResult->pathpos = pathpos;
562 prevResult->oid = HeapTupleGetOid(proctup);
563 continue; /* args are same, of course */
569 * Okay to add it to result list
571 newResult = (FuncCandidateList)
572 palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
573 + nargs * sizeof(Oid));
574 newResult->pathpos = pathpos;
575 newResult->oid = HeapTupleGetOid(proctup);
576 newResult->nargs = nargs;
577 memcpy(newResult->args, procform->proargtypes, nargs * sizeof(Oid));
579 newResult->next = resultList;
580 resultList = newResult;
583 ReleaseSysCacheList(catlist);
590 * Determine whether a function (identified by OID) is visible in the
591 * current search path. Visible means "would be found by searching
592 * for the unqualified function name with exact argument matches".
595 FunctionIsVisible(Oid funcid)
598 Form_pg_proc procform;
602 proctup = SearchSysCache(PROCOID,
603 ObjectIdGetDatum(funcid),
605 if (!HeapTupleIsValid(proctup))
606 elog(ERROR, "Cache lookup failed for procedure %u", funcid);
607 procform = (Form_pg_proc) GETSTRUCT(proctup);
609 recomputeNamespacePath();
612 * Quick check: if it ain't in the path at all, it ain't visible.
613 * Items in the system namespace are surely in the path and so we
614 * needn't even do intMember() for them.
616 pronamespace = procform->pronamespace;
617 if (pronamespace != PG_CATALOG_NAMESPACE &&
618 !intMember(pronamespace, namespaceSearchPath))
623 * If it is in the path, it might still not be visible; it could
624 * be hidden by another proc of the same name and arguments
625 * earlier in the path. So we must do a slow check to see if this
626 * is the same proc that would be found by FuncnameGetCandidates.
628 char *proname = NameStr(procform->proname);
629 int nargs = procform->pronargs;
630 FuncCandidateList clist;
634 clist = FuncnameGetCandidates(makeList1(makeString(proname)), nargs);
636 for (; clist; clist = clist->next)
638 if (memcmp(clist->args, procform->proargtypes,
639 nargs * sizeof(Oid)) == 0)
641 /* Found the expected entry; is it the right proc? */
642 visible = (clist->oid == funcid);
648 ReleaseSysCache(proctup);
655 * OpernameGetCandidates
656 * Given a possibly-qualified operator name and operator kind,
657 * retrieve a list of the possible matches.
659 * If oprkind is '\0', we return all operators matching the given name,
660 * regardless of arguments.
662 * We search a single namespace if the operator name is qualified, else
663 * all namespaces in the search path. The return list will never contain
664 * multiple entries with identical argument lists --- in the multiple-
665 * namespace case, we arrange for entries in earlier namespaces to mask
666 * identical entries in later namespaces.
668 * The returned items always have two args[] entries --- one or the other
669 * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
672 OpernameGetCandidates(List *names, char oprkind)
674 FuncCandidateList resultList = NULL;
681 /* deconstruct the name list */
682 DeconstructQualifiedName(names, &schemaname, &opername);
686 /* use exact schema given */
687 namespaceId = LookupExplicitNamespace(schemaname);
691 /* flag to indicate we need namespace search */
692 namespaceId = InvalidOid;
693 recomputeNamespacePath();
696 /* Search syscache by name only */
697 catlist = SearchSysCacheList(OPERNAMENSP, 1,
698 CStringGetDatum(opername),
701 for (i = 0; i < catlist->n_members; i++)
703 HeapTuple opertup = &catlist->members[i]->tuple;
704 Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
706 FuncCandidateList newResult;
708 /* Ignore operators of wrong kind, if specific kind requested */
709 if (oprkind && operform->oprkind != oprkind)
712 if (OidIsValid(namespaceId))
714 /* Consider only opers in specified namespace */
715 if (operform->oprnamespace != namespaceId)
717 /* No need to check args, they must all be different */
721 /* Consider only opers that are in the search path */
724 foreach(nsp, namespaceSearchPath)
726 if (operform->oprnamespace == (Oid) lfirsti(nsp))
731 continue; /* oper is not in search path */
734 * Okay, it's in the search path, but does it have the same
735 * arguments as something we already accepted? If so, keep
736 * only the one that appears earlier in the search path.
738 * If we have an ordered list from SearchSysCacheList (the normal
739 * case), then any conflicting oper must immediately adjoin
740 * this one in the list, so we only need to look at the newest
741 * result item. If we have an unordered list, we have to scan
742 * the whole result list.
746 FuncCandidateList prevResult;
748 if (catlist->ordered)
750 if (operform->oprleft == resultList->args[0] &&
751 operform->oprright == resultList->args[1])
752 prevResult = resultList;
758 for (prevResult = resultList;
760 prevResult = prevResult->next)
762 if (operform->oprleft == prevResult->args[0] &&
763 operform->oprright == prevResult->args[1])
769 /* We have a match with a previous result */
770 Assert(pathpos != prevResult->pathpos);
771 if (pathpos > prevResult->pathpos)
772 continue; /* keep previous result */
773 /* replace previous result */
774 prevResult->pathpos = pathpos;
775 prevResult->oid = HeapTupleGetOid(opertup);
776 continue; /* args are same, of course */
782 * Okay to add it to result list
784 newResult = (FuncCandidateList)
785 palloc(sizeof(struct _FuncCandidateList) + sizeof(Oid));
786 newResult->pathpos = pathpos;
787 newResult->oid = HeapTupleGetOid(opertup);
788 newResult->nargs = 2;
789 newResult->args[0] = operform->oprleft;
790 newResult->args[1] = operform->oprright;
791 newResult->next = resultList;
792 resultList = newResult;
795 ReleaseSysCacheList(catlist);
802 * Determine whether an operator (identified by OID) is visible in the
803 * current search path. Visible means "would be found by searching
804 * for the unqualified operator name with exact argument matches".
807 OperatorIsVisible(Oid oprid)
810 Form_pg_operator oprform;
814 oprtup = SearchSysCache(OPEROID,
815 ObjectIdGetDatum(oprid),
817 if (!HeapTupleIsValid(oprtup))
818 elog(ERROR, "Cache lookup failed for operator %u", oprid);
819 oprform = (Form_pg_operator) GETSTRUCT(oprtup);
821 recomputeNamespacePath();
824 * Quick check: if it ain't in the path at all, it ain't visible.
825 * Items in the system namespace are surely in the path and so we
826 * needn't even do intMember() for them.
828 oprnamespace = oprform->oprnamespace;
829 if (oprnamespace != PG_CATALOG_NAMESPACE &&
830 !intMember(oprnamespace, namespaceSearchPath))
835 * If it is in the path, it might still not be visible; it could
836 * be hidden by another operator of the same name and arguments
837 * earlier in the path. So we must do a slow check to see if this
838 * is the same operator that would be found by
839 * OpernameGetCandidates.
841 char *oprname = NameStr(oprform->oprname);
842 FuncCandidateList clist;
846 clist = OpernameGetCandidates(makeList1(makeString(oprname)),
849 for (; clist; clist = clist->next)
851 if (clist->args[0] == oprform->oprleft &&
852 clist->args[1] == oprform->oprright)
854 /* Found the expected entry; is it the right op? */
855 visible = (clist->oid == oprid);
861 ReleaseSysCache(oprtup);
868 * OpclassGetCandidates
869 * Given an index access method OID, retrieve a list of all the
870 * opclasses for that AM that are visible in the search path.
872 * NOTE: the opcname_tmp field in the returned structs should not be used
873 * by callers, because it points at syscache entries that we release at
874 * the end of this routine. If any callers needed the name information,
875 * we could pstrdup() the names ... but at present it'd be wasteful.
878 OpclassGetCandidates(Oid amid)
880 OpclassCandidateList resultList = NULL;
884 /* Search syscache by AM OID only */
885 catlist = SearchSysCacheList(CLAAMNAMENSP, 1,
886 ObjectIdGetDatum(amid),
889 recomputeNamespacePath();
891 for (i = 0; i < catlist->n_members; i++)
893 HeapTuple opctup = &catlist->members[i]->tuple;
894 Form_pg_opclass opcform = (Form_pg_opclass) GETSTRUCT(opctup);
896 OpclassCandidateList newResult;
899 /* Consider only opclasses that are in the search path */
900 foreach(nsp, namespaceSearchPath)
902 if (opcform->opcnamespace == (Oid) lfirsti(nsp))
907 continue; /* opclass is not in search path */
910 * Okay, it's in the search path, but does it have the same name
911 * as something we already accepted? If so, keep only the one
912 * that appears earlier in the search path.
914 * If we have an ordered list from SearchSysCacheList (the normal
915 * case), then any conflicting opclass must immediately adjoin
916 * this one in the list, so we only need to look at the newest
917 * result item. If we have an unordered list, we have to scan the
922 OpclassCandidateList prevResult;
924 if (catlist->ordered)
926 if (strcmp(NameStr(opcform->opcname),
927 resultList->opcname_tmp) == 0)
928 prevResult = resultList;
934 for (prevResult = resultList;
936 prevResult = prevResult->next)
938 if (strcmp(NameStr(opcform->opcname),
939 prevResult->opcname_tmp) == 0)
945 /* We have a match with a previous result */
946 Assert(pathpos != prevResult->pathpos);
947 if (pathpos > prevResult->pathpos)
948 continue; /* keep previous result */
949 /* replace previous result */
950 prevResult->opcname_tmp = NameStr(opcform->opcname);
951 prevResult->pathpos = pathpos;
952 prevResult->oid = HeapTupleGetOid(opctup);
953 prevResult->opcintype = opcform->opcintype;
954 prevResult->opcdefault = opcform->opcdefault;
955 prevResult->opckeytype = opcform->opckeytype;
961 * Okay to add it to result list
963 newResult = (OpclassCandidateList)
964 palloc(sizeof(struct _OpclassCandidateList));
965 newResult->opcname_tmp = NameStr(opcform->opcname);
966 newResult->pathpos = pathpos;
967 newResult->oid = HeapTupleGetOid(opctup);
968 newResult->opcintype = opcform->opcintype;
969 newResult->opcdefault = opcform->opcdefault;
970 newResult->opckeytype = opcform->opckeytype;
971 newResult->next = resultList;
972 resultList = newResult;
975 ReleaseSysCacheList(catlist);
981 * OpclassnameGetOpcid
982 * Try to resolve an unqualified index opclass name.
983 * Returns OID if opclass found in search path, else InvalidOid.
985 * This is essentially the same as TypenameGetTypid, but we have to have
986 * an extra argument for the index AM OID.
989 OpclassnameGetOpcid(Oid amid, const char *opcname)
994 recomputeNamespacePath();
996 foreach(lptr, namespaceSearchPath)
998 Oid namespaceId = (Oid) lfirsti(lptr);
1000 opcid = GetSysCacheOid(CLAAMNAMENSP,
1001 ObjectIdGetDatum(amid),
1002 PointerGetDatum(opcname),
1003 ObjectIdGetDatum(namespaceId),
1005 if (OidIsValid(opcid))
1009 /* Not found in path */
1015 * Determine whether an opclass (identified by OID) is visible in the
1016 * current search path. Visible means "would be found by searching
1017 * for the unqualified opclass name".
1020 OpclassIsVisible(Oid opcid)
1023 Form_pg_opclass opcform;
1027 opctup = SearchSysCache(CLAOID,
1028 ObjectIdGetDatum(opcid),
1030 if (!HeapTupleIsValid(opctup))
1031 elog(ERROR, "Cache lookup failed for opclass %u", opcid);
1032 opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1034 recomputeNamespacePath();
1037 * Quick check: if it ain't in the path at all, it ain't visible.
1038 * Items in the system namespace are surely in the path and so we
1039 * needn't even do intMember() for them.
1041 opcnamespace = opcform->opcnamespace;
1042 if (opcnamespace != PG_CATALOG_NAMESPACE &&
1043 !intMember(opcnamespace, namespaceSearchPath))
1048 * If it is in the path, it might still not be visible; it could
1049 * be hidden by another opclass of the same name earlier in the
1050 * path. So we must do a slow check to see if this opclass would
1051 * be found by OpclassnameGetOpcid.
1053 char *opcname = NameStr(opcform->opcname);
1055 visible = (OpclassnameGetOpcid(opcform->opcamid, opcname) == opcid);
1058 ReleaseSysCache(opctup);
1064 * ConversionGetConid
1065 * Try to resolve an unqualified conversion name.
1066 * Returns OID if conversion found in search path, else InvalidOid.
1068 * This is essentially the same as RelnameGetRelid.
1071 ConversionGetConid(const char *conname)
1076 recomputeNamespacePath();
1078 foreach(lptr, namespaceSearchPath)
1080 Oid namespaceId = (Oid) lfirsti(lptr);
1082 conid = GetSysCacheOid(CONNAMENSP,
1083 PointerGetDatum(conname),
1084 ObjectIdGetDatum(namespaceId),
1086 if (OidIsValid(conid))
1090 /* Not found in path */
1095 * ConversionIsVisible
1096 * Determine whether a conversion (identified by OID) is visible in the
1097 * current search path. Visible means "would be found by searching
1098 * for the unqualified conversion name".
1101 ConversionIsVisible(Oid conid)
1104 Form_pg_conversion conform;
1108 contup = SearchSysCache(CONOID,
1109 ObjectIdGetDatum(conid),
1111 if (!HeapTupleIsValid(contup))
1112 elog(ERROR, "Cache lookup failed for conversion %u", conid);
1113 conform = (Form_pg_conversion) GETSTRUCT(contup);
1115 recomputeNamespacePath();
1118 * Quick check: if it ain't in the path at all, it ain't visible.
1119 * Items in the system namespace are surely in the path and so we
1120 * needn't even do intMember() for them.
1122 connamespace = conform->connamespace;
1123 if (connamespace != PG_CATALOG_NAMESPACE &&
1124 !intMember(connamespace, namespaceSearchPath))
1129 * If it is in the path, it might still not be visible; it could
1130 * be hidden by another conversion of the same name earlier in the
1131 * path. So we must do a slow check to see if this conversion would
1132 * be found by ConversionGetConid.
1134 char *conname = NameStr(conform->conname);
1136 visible = (ConversionGetConid(conname) == conid);
1139 ReleaseSysCache(contup);
1145 * DeconstructQualifiedName
1146 * Given a possibly-qualified name expressed as a list of String nodes,
1147 * extract the schema name and object name.
1149 * *nspname_p is set to NULL if there is no explicit schema name.
1152 DeconstructQualifiedName(List *names,
1157 char *schemaname = NULL;
1158 char *objname = NULL;
1160 switch (length(names))
1163 objname = strVal(lfirst(names));
1166 schemaname = strVal(lfirst(names));
1167 objname = strVal(lsecond(names));
1170 catalogname = strVal(lfirst(names));
1171 schemaname = strVal(lsecond(names));
1172 objname = strVal(lfirst(lnext(lnext(names))));
1175 * We check the catalog name and then ignore it.
1177 if (strcmp(catalogname, DatabaseName) != 0)
1178 elog(ERROR, "Cross-database references are not implemented");
1181 elog(ERROR, "Improper qualified name (too many dotted names): %s",
1182 NameListToString(names));
1186 *nspname_p = schemaname;
1187 *objname_p = objname;
1191 * LookupExplicitNamespace
1192 * Process an explicitly-specified schema name: look up the schema
1193 * and verify we have USAGE (lookup) rights in it.
1195 * Returns the namespace OID. Raises elog if any problem.
1198 LookupExplicitNamespace(const char *nspname)
1201 AclResult aclresult;
1203 namespaceId = GetSysCacheOid(NAMESPACENAME,
1204 CStringGetDatum(nspname),
1206 if (!OidIsValid(namespaceId))
1207 elog(ERROR, "Namespace \"%s\" does not exist", nspname);
1209 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
1210 if (aclresult != ACLCHECK_OK)
1211 aclcheck_error(aclresult, nspname);
1217 * QualifiedNameGetCreationNamespace
1218 * Given a possibly-qualified name for an object (in List-of-Values
1219 * format), determine what namespace the object should be created in.
1220 * Also extract and return the object name (last component of list).
1222 * This is *not* used for tables. Hence, the TEMP table namespace is
1223 * never selected as the creation target.
1226 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
1232 /* deconstruct the name list */
1233 DeconstructQualifiedName(names, &schemaname, &objname);
1237 /* use exact schema given */
1238 namespaceId = GetSysCacheOid(NAMESPACENAME,
1239 CStringGetDatum(schemaname),
1241 if (!OidIsValid(namespaceId))
1242 elog(ERROR, "Namespace \"%s\" does not exist",
1244 /* we do not check for USAGE rights here! */
1248 /* use the default creation namespace */
1249 recomputeNamespacePath();
1250 namespaceId = defaultCreationNamespace;
1251 if (!OidIsValid(namespaceId))
1252 elog(ERROR, "No namespace has been selected to create in");
1255 /* Note: callers will check for CREATE rights when appropriate */
1257 *objname_p = objname;
1262 * makeRangeVarFromNameList
1263 * Utility routine to convert a qualified-name list into RangeVar form.
1266 makeRangeVarFromNameList(List *names)
1268 RangeVar *rel = makeRangeVar(NULL, NULL);
1270 switch (length(names))
1273 rel->relname = strVal(lfirst(names));
1276 rel->schemaname = strVal(lfirst(names));
1277 rel->relname = strVal(lsecond(names));
1280 rel->catalogname = strVal(lfirst(names));
1281 rel->schemaname = strVal(lsecond(names));
1282 rel->relname = strVal(lfirst(lnext(lnext(names))));
1285 elog(ERROR, "Improper relation name (too many dotted names)");
1294 * Utility routine to convert a qualified-name list into a string.
1296 * This is used primarily to form error messages, and so we do not quote
1297 * the list elements, for the sake of legibility.
1300 NameListToString(List *names)
1302 StringInfoData string;
1305 initStringInfo(&string);
1310 appendStringInfoChar(&string, '.');
1311 appendStringInfo(&string, "%s", strVal(lfirst(l)));
1318 * NameListToQuotedString
1319 * Utility routine to convert a qualified-name list into a string.
1321 * Same as above except that names will be double-quoted where necessary,
1322 * so the string could be re-parsed (eg, by textToQualifiedNameList).
1325 NameListToQuotedString(List *names)
1327 StringInfoData string;
1330 initStringInfo(&string);
1335 appendStringInfoChar(&string, '.');
1336 appendStringInfo(&string, "%s", quote_identifier(strVal(lfirst(l))));
1343 * isTempNamespace - is the given namespace my temporary-table namespace?
1346 isTempNamespace(Oid namespaceId)
1348 if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
1354 * isOtherTempNamespace - is the given namespace some other backend's
1355 * temporary-table namespace?
1358 isOtherTempNamespace(Oid namespaceId)
1363 /* If it's my own temp namespace, say "false" */
1364 if (isTempNamespace(namespaceId))
1366 /* Else, if the namespace name starts with "pg_temp_", say "true" */
1367 nspname = get_namespace_name(namespaceId);
1369 return false; /* no such namespace? */
1370 result = (strncmp(nspname, "pg_temp_", 8) == 0);
1376 * PushSpecialNamespace - push a "special" namespace onto the front of the
1379 * This is a slightly messy hack intended only for support of CREATE SCHEMA.
1380 * Although the API is defined to allow a stack of pushed namespaces, we
1381 * presently only support one at a time.
1383 * The pushed namespace will be removed from the search path at end of
1384 * transaction, whether commit or abort.
1387 PushSpecialNamespace(Oid namespaceId)
1389 Assert(!OidIsValid(mySpecialNamespace));
1390 mySpecialNamespace = namespaceId;
1391 namespaceSearchPathValid = false;
1395 * PopSpecialNamespace - remove previously pushed special namespace.
1398 PopSpecialNamespace(Oid namespaceId)
1400 Assert(mySpecialNamespace == namespaceId);
1401 mySpecialNamespace = InvalidOid;
1402 namespaceSearchPathValid = false;
1406 * FindConversionByName - find a conversion by possibly qualified name
1409 FindConversionByName(List *name)
1412 char *conversion_name;
1417 /* deconstruct the name list */
1418 DeconstructQualifiedName(name, &schemaname, &conversion_name);
1422 /* use exact schema given */
1423 namespaceId = LookupExplicitNamespace(schemaname);
1424 return FindConversion(conversion_name, namespaceId);
1428 /* search for it in search path */
1429 recomputeNamespacePath();
1431 foreach(lptr, namespaceSearchPath)
1433 namespaceId = (Oid) lfirsti(lptr);
1434 conoid = FindConversion(conversion_name, namespaceId);
1435 if (OidIsValid(conoid))
1440 /* Not found in path */
1445 * FindDefaultConversionProc - find default encoding conversion proc
1448 FindDefaultConversionProc(int4 for_encoding, int4 to_encoding)
1453 recomputeNamespacePath();
1455 foreach(lptr, namespaceSearchPath)
1457 Oid namespaceId = (Oid) lfirsti(lptr);
1459 proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
1460 if (OidIsValid(proc))
1464 /* Not found in path */
1469 * recomputeNamespacePath - recompute path derived variables if needed.
1472 recomputeNamespacePath(void)
1474 AclId userId = GetUserId();
1481 MemoryContext oldcxt;
1484 * Do nothing if path is already valid.
1486 if (namespaceSearchPathValid && namespaceUser == userId)
1489 /* Need a modifiable copy of namespace_search_path string */
1490 rawname = pstrdup(namespace_search_path);
1492 /* Parse string into list of identifiers */
1493 if (!SplitIdentifierString(rawname, ',', &namelist))
1495 /* syntax error in name list */
1496 /* this should not happen if GUC checked check_search_path */
1497 elog(ERROR, "recomputeNamespacePath: invalid list syntax");
1501 * Convert the list of names to a list of OIDs. If any names are not
1502 * recognizable or we don't have read access, just leave them out of
1503 * the list. (We can't raise an error, since the search_path setting
1504 * has already been accepted.) Don't make duplicate entries, either.
1507 foreach(l, namelist)
1509 char *curname = (char *) lfirst(l);
1512 if (strcmp(curname, "$user") == 0)
1514 /* $user --- substitute namespace matching user name, if any */
1517 tuple = SearchSysCache(SHADOWSYSID,
1518 ObjectIdGetDatum(userId),
1520 if (HeapTupleIsValid(tuple))
1524 uname = NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename);
1525 namespaceId = GetSysCacheOid(NAMESPACENAME,
1526 CStringGetDatum(uname),
1528 ReleaseSysCache(tuple);
1529 if (OidIsValid(namespaceId) &&
1530 !intMember(namespaceId, oidlist) &&
1531 pg_namespace_aclcheck(namespaceId, userId,
1532 ACL_USAGE) == ACLCHECK_OK)
1533 oidlist = lappendi(oidlist, namespaceId);
1538 /* normal namespace reference */
1539 namespaceId = GetSysCacheOid(NAMESPACENAME,
1540 CStringGetDatum(curname),
1542 if (OidIsValid(namespaceId) &&
1543 !intMember(namespaceId, oidlist) &&
1544 pg_namespace_aclcheck(namespaceId, userId,
1545 ACL_USAGE) == ACLCHECK_OK)
1546 oidlist = lappendi(oidlist, namespaceId);
1551 * Remember the first member of the explicit list.
1554 firstNS = InvalidOid;
1556 firstNS = (Oid) lfirsti(oidlist);
1559 * Add any implicitly-searched namespaces to the list. Note these go
1560 * on the front, not the back; also notice that we do not check USAGE
1561 * permissions for these.
1563 if (!intMember(PG_CATALOG_NAMESPACE, oidlist))
1564 oidlist = lconsi(PG_CATALOG_NAMESPACE, oidlist);
1566 if (OidIsValid(myTempNamespace) &&
1567 !intMember(myTempNamespace, oidlist))
1568 oidlist = lconsi(myTempNamespace, oidlist);
1570 if (OidIsValid(mySpecialNamespace) &&
1571 !intMember(mySpecialNamespace, oidlist))
1572 oidlist = lconsi(mySpecialNamespace, oidlist);
1575 * Now that we've successfully built the new list of namespace OIDs,
1576 * save it in permanent storage.
1578 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1579 newpath = listCopy(oidlist);
1580 MemoryContextSwitchTo(oldcxt);
1582 /* Now safe to assign to state variable. */
1583 freeList(namespaceSearchPath);
1584 namespaceSearchPath = newpath;
1587 * Update info derived from search path.
1589 firstExplicitNamespace = firstNS;
1590 if (OidIsValid(mySpecialNamespace))
1591 defaultCreationNamespace = mySpecialNamespace;
1593 defaultCreationNamespace = firstNS;
1595 /* Mark the path valid. */
1596 namespaceSearchPathValid = true;
1597 namespaceUser = userId;
1606 * InitTempTableNamespace
1607 * Initialize temp table namespace on first use in a particular backend
1610 InitTempTableNamespace(void)
1612 char namespaceName[NAMEDATALEN];
1616 * First, do permission check to see if we are authorized to make temp
1617 * tables. We use a nonstandard error message here since
1618 * "databasename: permission denied" might be a tad cryptic.
1620 * Note we apply the check to the session user, not the currently active
1621 * userid, since we are not going to change our minds about temp table
1622 * availability during the session.
1624 if (pg_database_aclcheck(MyDatabaseId, GetSessionUserId(),
1625 ACL_CREATE_TEMP) != ACLCHECK_OK)
1626 elog(ERROR, "%s: not authorized to create temp tables",
1629 snprintf(namespaceName, NAMEDATALEN, "pg_temp_%d", MyBackendId);
1631 namespaceId = GetSysCacheOid(NAMESPACENAME,
1632 CStringGetDatum(namespaceName),
1634 if (!OidIsValid(namespaceId))
1637 * First use of this temp namespace in this database; create it.
1638 * The temp namespaces are always owned by the superuser. We
1639 * leave their permissions at default --- i.e., no access except
1640 * to superuser --- to ensure that unprivileged users can't peek
1641 * at other backends' temp tables. This works because the places
1642 * that access the temp namespace for my own backend skip
1643 * permissions checks on it.
1645 namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_USESYSID);
1646 /* Advance command counter to make namespace visible */
1647 CommandCounterIncrement();
1652 * If the namespace already exists, clean it out (in case the
1653 * former owner crashed without doing so).
1655 RemoveTempRelations(namespaceId);
1659 * Okay, we've prepared the temp namespace ... but it's not committed
1660 * yet, so all our work could be undone by transaction rollback. Set
1661 * flag for AtEOXact_Namespace to know what to do.
1663 myTempNamespace = namespaceId;
1665 firstTempTransaction = true;
1667 namespaceSearchPathValid = false; /* need to rebuild list */
1671 * End-of-transaction cleanup for namespaces.
1674 AtEOXact_Namespace(bool isCommit)
1677 * If we abort the transaction in which a temp namespace was selected,
1678 * we'll have to do any creation or cleanout work over again. So,
1679 * just forget the namespace entirely until next time. On the other
1680 * hand, if we commit then register an exit callback to clean out the
1681 * temp tables at backend shutdown. (We only want to register the
1682 * callback once per session, so this is a good place to do it.)
1684 if (firstTempTransaction)
1687 on_shmem_exit(RemoveTempRelationsCallback, 0);
1690 myTempNamespace = InvalidOid;
1691 namespaceSearchPathValid = false; /* need to rebuild list */
1693 firstTempTransaction = false;
1697 * Clean up if someone failed to do PopSpecialNamespace
1699 if (OidIsValid(mySpecialNamespace))
1701 mySpecialNamespace = InvalidOid;
1702 namespaceSearchPathValid = false; /* need to rebuild list */
1707 * Remove all relations in the specified temp namespace.
1709 * This is called at backend shutdown (if we made any temp relations).
1710 * It is also called when we begin using a pre-existing temp namespace,
1711 * in order to clean out any relations that might have been created by
1712 * a crashed backend.
1715 RemoveTempRelations(Oid tempNamespaceId)
1721 ObjectAddress object;
1724 * Scan pg_class to find all the relations in the target namespace.
1725 * Ignore indexes, though, on the assumption that they'll go away when
1726 * their tables are deleted.
1728 * NOTE: if there are deletion constraints between temp relations, then
1729 * our CASCADE delete call may cause as-yet-unvisited objects to go
1730 * away. This is okay because we are using SnapshotNow; when the scan
1731 * does reach those pg_class tuples, they'll be ignored as already
1734 ScanKeyEntryInitialize(&key, 0x0,
1735 Anum_pg_class_relnamespace,
1737 ObjectIdGetDatum(tempNamespaceId));
1739 pgclass = heap_openr(RelationRelationName, AccessShareLock);
1740 scan = heap_beginscan(pgclass, SnapshotNow, 1, &key);
1742 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1744 switch (((Form_pg_class) GETSTRUCT(tuple))->relkind)
1746 case RELKIND_RELATION:
1747 case RELKIND_SEQUENCE:
1749 object.classId = RelOid_pg_class;
1750 object.objectId = HeapTupleGetOid(tuple);
1751 object.objectSubId = 0;
1752 performDeletion(&object, DROP_CASCADE);
1760 heap_close(pgclass, AccessShareLock);
1764 * Callback to remove temp relations at backend exit.
1767 RemoveTempRelationsCallback(void)
1769 if (OidIsValid(myTempNamespace)) /* should always be true */
1771 /* Need to ensure we have a usable transaction. */
1772 AbortOutOfAnyTransaction();
1773 StartTransactionCommand(true);
1775 RemoveTempRelations(myTempNamespace);
1777 CommitTransactionCommand(true);
1783 * Routines for handling the GUC variable 'search_path'.
1786 /* assign_hook: validate new search_path, do extra actions as needed */
1788 assign_search_path(const char *newval, bool doit, bool interactive)
1794 /* Need a modifiable copy of string */
1795 rawname = pstrdup(newval);
1797 /* Parse string into list of identifiers */
1798 if (!SplitIdentifierString(rawname, ',', &namelist))
1800 /* syntax error in name list */
1807 * If we aren't inside a transaction, we cannot do database access so
1808 * cannot verify the individual names. Must accept the list on faith.
1810 if (interactive && IsTransactionState())
1813 * Verify that all the names are either valid namespace names or
1814 * "$user". We do not require $user to correspond to a valid
1815 * namespace. We do not check for USAGE rights, either; should
1818 foreach(l, namelist)
1820 char *curname = (char *) lfirst(l);
1822 if (strcmp(curname, "$user") == 0)
1824 if (!SearchSysCacheExists(NAMESPACENAME,
1825 CStringGetDatum(curname),
1827 elog(ERROR, "Namespace \"%s\" does not exist", curname);
1835 * We mark the path as needing recomputation, but don't do anything
1836 * until it's needed. This avoids trying to do database access during
1837 * GUC initialization.
1840 namespaceSearchPathValid = false;
1846 * InitializeSearchPath: initialize module during InitPostgres.
1848 * This is called after we are up enough to be able to do catalog lookups.
1851 InitializeSearchPath(void)
1853 if (IsBootstrapProcessingMode())
1856 * In bootstrap mode, the search path must be 'pg_catalog' so that
1857 * tables are created in the proper namespace; ignore the GUC
1860 MemoryContext oldcxt;
1862 oldcxt = MemoryContextSwitchTo(TopMemoryContext);
1863 namespaceSearchPath = makeListi1(PG_CATALOG_NAMESPACE);
1864 MemoryContextSwitchTo(oldcxt);
1865 defaultCreationNamespace = PG_CATALOG_NAMESPACE;
1866 firstExplicitNamespace = PG_CATALOG_NAMESPACE;
1867 namespaceSearchPathValid = true;
1868 namespaceUser = GetUserId();
1873 * In normal mode, arrange for a callback on any syscache
1874 * invalidation of pg_namespace rows.
1876 CacheRegisterSyscacheCallback(NAMESPACEOID,
1879 /* Force search path to be recomputed on next use */
1880 namespaceSearchPathValid = false;
1886 * Syscache inval callback function
1889 NamespaceCallback(Datum arg, Oid relid)
1891 /* Force search path to be recomputed on next use */
1892 namespaceSearchPathValid = false;
1896 * Fetch the active search path, expressed as a List of OIDs.
1898 * The returned list includes the implicitly-prepended namespaces only if
1899 * includeImplicit is true.
1901 * NB: caller must treat the list as read-only!
1904 fetch_search_path(bool includeImplicit)
1908 recomputeNamespacePath();
1910 result = namespaceSearchPath;
1911 if (!includeImplicit)
1913 while (result && (Oid) lfirsti(result) != firstExplicitNamespace)
1914 result = lnext(result);
1921 * Export the FooIsVisible functions as SQL-callable functions.
1925 pg_table_is_visible(PG_FUNCTION_ARGS)
1927 Oid oid = PG_GETARG_OID(0);
1929 PG_RETURN_BOOL(RelationIsVisible(oid));
1933 pg_type_is_visible(PG_FUNCTION_ARGS)
1935 Oid oid = PG_GETARG_OID(0);
1937 PG_RETURN_BOOL(TypeIsVisible(oid));
1941 pg_function_is_visible(PG_FUNCTION_ARGS)
1943 Oid oid = PG_GETARG_OID(0);
1945 PG_RETURN_BOOL(FunctionIsVisible(oid));
1949 pg_operator_is_visible(PG_FUNCTION_ARGS)
1951 Oid oid = PG_GETARG_OID(0);
1953 PG_RETURN_BOOL(OperatorIsVisible(oid));
1957 pg_opclass_is_visible(PG_FUNCTION_ARGS)
1959 Oid oid = PG_GETARG_OID(0);
1961 PG_RETURN_BOOL(OpclassIsVisible(oid));
1965 pg_conversion_is_visible(PG_FUNCTION_ARGS)
1967 Oid oid = PG_GETARG_OID(0);
1969 PG_RETURN_BOOL(ConversionIsVisible(oid));