PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
namespace.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * namespace.c
4  * code to support accessing and searching namespaces
5  *
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.
10  *
11  *
12  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * IDENTIFICATION
16  * src/backend/catalog/namespace.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include "access/htup_details.h"
23 #include "access/parallel.h"
24 #include "access/xact.h"
25 #include "access/xlog.h"
26 #include "catalog/dependency.h"
27 #include "catalog/objectaccess.h"
28 #include "catalog/pg_authid.h"
29 #include "catalog/pg_collation.h"
30 #include "catalog/pg_conversion.h"
32 #include "catalog/pg_namespace.h"
33 #include "catalog/pg_opclass.h"
34 #include "catalog/pg_operator.h"
35 #include "catalog/pg_opfamily.h"
36 #include "catalog/pg_proc.h"
37 #include "catalog/pg_ts_config.h"
38 #include "catalog/pg_ts_dict.h"
39 #include "catalog/pg_ts_parser.h"
40 #include "catalog/pg_ts_template.h"
41 #include "catalog/pg_type.h"
42 #include "commands/dbcommands.h"
43 #include "funcapi.h"
44 #include "mb/pg_wchar.h"
45 #include "miscadmin.h"
46 #include "nodes/makefuncs.h"
47 #include "parser/parse_func.h"
48 #include "storage/ipc.h"
49 #include "storage/lmgr.h"
50 #include "storage/sinval.h"
51 #include "utils/acl.h"
52 #include "utils/builtins.h"
53 #include "utils/catcache.h"
54 #include "utils/guc.h"
55 #include "utils/inval.h"
56 #include "utils/lsyscache.h"
57 #include "utils/memutils.h"
58 #include "utils/syscache.h"
59 #include "utils/varlena.h"
60 
61 
62 /*
63  * The namespace search path is a possibly-empty list of namespace OIDs.
64  * In addition to the explicit list, implicitly-searched namespaces
65  * may be included:
66  *
67  * 1. If a TEMP table namespace has been initialized in this session, it
68  * is implicitly searched first. (The only time this doesn't happen is
69  * when we are obeying an override search path spec that says not to use the
70  * temp namespace, or the temp namespace is included in the explicit list.)
71  *
72  * 2. The system catalog namespace is always searched. If the system
73  * namespace is present in the explicit path then it will be searched in
74  * the specified order; otherwise it will be searched after TEMP tables and
75  * *before* the explicit list. (It might seem that the system namespace
76  * should be implicitly last, but this behavior appears to be required by
77  * SQL99. Also, this provides a way to search the system namespace first
78  * without thereby making it the default creation target namespace.)
79  *
80  * For security reasons, searches using the search path will ignore the temp
81  * namespace when searching for any object type other than relations and
82  * types. (We must allow types since temp tables have rowtypes.)
83  *
84  * The default creation target namespace is always the first element of the
85  * explicit list. If the explicit list is empty, there is no default target.
86  *
87  * The textual specification of search_path can include "$user" to refer to
88  * the namespace named the same as the current user, if any. (This is just
89  * ignored if there is no such namespace.) Also, it can include "pg_temp"
90  * to refer to the current backend's temp namespace. This is usually also
91  * ignorable if the temp namespace hasn't been set up, but there's a special
92  * case: if "pg_temp" appears first then it should be the default creation
93  * target. We kluge this case a little bit so that the temp namespace isn't
94  * set up until the first attempt to create something in it. (The reason for
95  * klugery is that we can't create the temp namespace outside a transaction,
96  * but initial GUC processing of search_path happens outside a transaction.)
97  * activeTempCreationPending is TRUE if "pg_temp" appears first in the string
98  * but is not reflected in activeCreationNamespace because the namespace isn't
99  * set up yet.
100  *
101  * In bootstrap mode, the search path is set equal to "pg_catalog", so that
102  * the system namespace is the only one searched or inserted into.
103  * initdb is also careful to set search_path to "pg_catalog" for its
104  * post-bootstrap standalone backend runs. Otherwise the default search
105  * path is determined by GUC. The factory default path contains the PUBLIC
106  * namespace (if it exists), preceded by the user's personal namespace
107  * (if one exists).
108  *
109  * We support a stack of "override" search path settings for use within
110  * specific sections of backend code. namespace_search_path is ignored
111  * whenever the override stack is nonempty. activeSearchPath is always
112  * the actually active path; it points either to the search list of the
113  * topmost stack entry, or to baseSearchPath which is the list derived
114  * from namespace_search_path.
115  *
116  * If baseSearchPathValid is false, then baseSearchPath (and other
117  * derived variables) need to be recomputed from namespace_search_path.
118  * We mark it invalid upon an assignment to namespace_search_path or receipt
119  * of a syscache invalidation event for pg_namespace. The recomputation
120  * is done during the next non-overridden lookup attempt. Note that an
121  * override spec is never subject to recomputation.
122  *
123  * Any namespaces mentioned in namespace_search_path that are not readable
124  * by the current user ID are simply left out of baseSearchPath; so
125  * we have to be willing to recompute the path when current userid changes.
126  * namespaceUser is the userid the path has been computed for.
127  *
128  * Note: all data pointed to by these List variables is in TopMemoryContext.
129  */
130 
131 /* These variables define the actually active state: */
132 
134 
135 /* default place to create stuff; if InvalidOid, no default */
137 
138 /* if TRUE, activeCreationNamespace is wrong, it should be temp namespace */
139 static bool activeTempCreationPending = false;
140 
141 /* These variables are the values last derived from namespace_search_path: */
142 
144 
146 
147 static bool baseTempCreationPending = false;
148 
150 
151 /* The above four values are valid only if baseSearchPathValid */
152 static bool baseSearchPathValid = true;
153 
154 /* Override requests are remembered in a stack of OverrideStackEntry structs */
155 
156 typedef struct
157 {
158  List *searchPath; /* the desired search path */
159  Oid creationNamespace; /* the desired creation namespace */
160  int nestLevel; /* subtransaction nesting level */
162 
164 
165 /*
166  * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
167  * in a particular backend session (this happens when a CREATE TEMP TABLE
168  * command is first executed). Thereafter it's the OID of the temp namespace.
169  *
170  * myTempToastNamespace is the OID of the namespace for my temp tables' toast
171  * tables. It is set when myTempNamespace is, and is InvalidOid before that.
172  *
173  * myTempNamespaceSubID shows whether we've created the TEMP namespace in the
174  * current subtransaction. The flag propagates up the subtransaction tree,
175  * so the main transaction will correctly recognize the flag if all
176  * intermediate subtransactions commit. When it is InvalidSubTransactionId,
177  * we either haven't made the TEMP namespace yet, or have successfully
178  * committed its creation, depending on whether myTempNamespace is valid.
179  */
181 
183 
185 
186 /*
187  * This is the user's textual search path specification --- it's the value
188  * of the GUC variable 'search_path'.
189  */
191 
192 
193 /* Local functions */
194 static void recomputeNamespacePath(void);
195 static void InitTempTableNamespace(void);
196 static void RemoveTempRelations(Oid tempNamespaceId);
197 static void RemoveTempRelationsCallback(int code, Datum arg);
198 static void NamespaceCallback(Datum arg, int cacheid, uint32 hashvalue);
199 static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
200  int **argnumbers);
201 
202 
203 /*
204  * RangeVarGetRelid
205  * Given a RangeVar describing an existing relation,
206  * select the proper namespace and look up the relation OID.
207  *
208  * If the schema or relation is not found, return InvalidOid if missing_ok
209  * = true, otherwise raise an error.
210  *
211  * If nowait = true, throw an error if we'd have to wait for a lock.
212  *
213  * Callback allows caller to check permissions or acquire additional locks
214  * prior to grabbing the relation lock.
215  */
216 Oid
217 RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
218  bool missing_ok, bool nowait,
219  RangeVarGetRelidCallback callback, void *callback_arg)
220 {
221  uint64 inval_count;
222  Oid relId;
223  Oid oldRelId = InvalidOid;
224  bool retry = false;
225 
226  /*
227  * We check the catalog name and then ignore it.
228  */
229  if (relation->catalogname)
230  {
231  if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
232  ereport(ERROR,
233  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
234  errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
235  relation->catalogname, relation->schemaname,
236  relation->relname)));
237  }
238 
239  /*
240  * DDL operations can change the results of a name lookup. Since all such
241  * operations will generate invalidation messages, we keep track of
242  * whether any such messages show up while we're performing the operation,
243  * and retry until either (1) no more invalidation messages show up or (2)
244  * the answer doesn't change.
245  *
246  * But if lockmode = NoLock, then we assume that either the caller is OK
247  * with the answer changing under them, or that they already hold some
248  * appropriate lock, and therefore return the first answer we get without
249  * checking for invalidation messages. Also, if the requested lock is
250  * already held, LockRelationOid will not AcceptInvalidationMessages, so
251  * we may fail to notice a change. We could protect against that case by
252  * calling AcceptInvalidationMessages() before beginning this loop, but
253  * that would add a significant amount overhead, so for now we don't.
254  */
255  for (;;)
256  {
257  /*
258  * Remember this value, so that, after looking up the relation name
259  * and locking its OID, we can check whether any invalidation messages
260  * have been processed that might require a do-over.
261  */
262  inval_count = SharedInvalidMessageCounter;
263 
264  /*
265  * Some non-default relpersistence value may have been specified. The
266  * parser never generates such a RangeVar in simple DML, but it can
267  * happen in contexts such as "CREATE TEMP TABLE foo (f1 int PRIMARY
268  * KEY)". Such a command will generate an added CREATE INDEX
269  * operation, which must be careful to find the temp table, even when
270  * pg_temp is not first in the search path.
271  */
272  if (relation->relpersistence == RELPERSISTENCE_TEMP)
273  {
275  relId = InvalidOid; /* this probably can't happen? */
276  else
277  {
278  if (relation->schemaname)
279  {
280  Oid namespaceId;
281 
282  namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
283 
284  /*
285  * For missing_ok, allow a non-existent schema name to
286  * return InvalidOid.
287  */
288  if (namespaceId != myTempNamespace)
289  ereport(ERROR,
290  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
291  errmsg("temporary tables cannot specify a schema name")));
292  }
293 
294  relId = get_relname_relid(relation->relname, myTempNamespace);
295  }
296  }
297  else if (relation->schemaname)
298  {
299  Oid namespaceId;
300 
301  /* use exact schema given */
302  namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
303  if (missing_ok && !OidIsValid(namespaceId))
304  relId = InvalidOid;
305  else
306  relId = get_relname_relid(relation->relname, namespaceId);
307  }
308  else
309  {
310  /* search the namespace path */
311  relId = RelnameGetRelid(relation->relname);
312  }
313 
314  /*
315  * Invoke caller-supplied callback, if any.
316  *
317  * This callback is a good place to check permissions: we haven't
318  * taken the table lock yet (and it's really best to check permissions
319  * before locking anything!), but we've gotten far enough to know what
320  * OID we think we should lock. Of course, concurrent DDL might
321  * change things while we're waiting for the lock, but in that case
322  * the callback will be invoked again for the new OID.
323  */
324  if (callback)
325  callback(relation, relId, oldRelId, callback_arg);
326 
327  /*
328  * If no lock requested, we assume the caller knows what they're
329  * doing. They should have already acquired a heavyweight lock on
330  * this relation earlier in the processing of this same statement, so
331  * it wouldn't be appropriate to AcceptInvalidationMessages() here, as
332  * that might pull the rug out from under them.
333  */
334  if (lockmode == NoLock)
335  break;
336 
337  /*
338  * If, upon retry, we get back the same OID we did last time, then the
339  * invalidation messages we processed did not change the final answer.
340  * So we're done.
341  *
342  * If we got a different OID, we've locked the relation that used to
343  * have this name rather than the one that does now. So release the
344  * lock.
345  */
346  if (retry)
347  {
348  if (relId == oldRelId)
349  break;
350  if (OidIsValid(oldRelId))
351  UnlockRelationOid(oldRelId, lockmode);
352  }
353 
354  /*
355  * Lock relation. This will also accept any pending invalidation
356  * messages. If we got back InvalidOid, indicating not found, then
357  * there's nothing to lock, but we accept invalidation messages
358  * anyway, to flush any negative catcache entries that may be
359  * lingering.
360  */
361  if (!OidIsValid(relId))
363  else if (!nowait)
364  LockRelationOid(relId, lockmode);
365  else if (!ConditionalLockRelationOid(relId, lockmode))
366  {
367  if (relation->schemaname)
368  ereport(ERROR,
369  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
370  errmsg("could not obtain lock on relation \"%s.%s\"",
371  relation->schemaname, relation->relname)));
372  else
373  ereport(ERROR,
374  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
375  errmsg("could not obtain lock on relation \"%s\"",
376  relation->relname)));
377  }
378 
379  /*
380  * If no invalidation message were processed, we're done!
381  */
382  if (inval_count == SharedInvalidMessageCounter)
383  break;
384 
385  /*
386  * Something may have changed. Let's repeat the name lookup, to make
387  * sure this name still references the same relation it did
388  * previously.
389  */
390  retry = true;
391  oldRelId = relId;
392  }
393 
394  if (!OidIsValid(relId) && !missing_ok)
395  {
396  if (relation->schemaname)
397  ereport(ERROR,
399  errmsg("relation \"%s.%s\" does not exist",
400  relation->schemaname, relation->relname)));
401  else
402  ereport(ERROR,
404  errmsg("relation \"%s\" does not exist",
405  relation->relname)));
406  }
407  return relId;
408 }
409 
410 /*
411  * RangeVarGetCreationNamespace
412  * Given a RangeVar describing a to-be-created relation,
413  * choose which namespace to create it in.
414  *
415  * Note: calling this may result in a CommandCounterIncrement operation.
416  * That will happen on the first request for a temp table in any particular
417  * backend run; we will need to either create or clean out the temp schema.
418  */
419 Oid
421 {
422  Oid namespaceId;
423 
424  /*
425  * We check the catalog name and then ignore it.
426  */
427  if (newRelation->catalogname)
428  {
429  if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
430  ereport(ERROR,
431  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
432  errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
433  newRelation->catalogname, newRelation->schemaname,
434  newRelation->relname)));
435  }
436 
437  if (newRelation->schemaname)
438  {
439  /* check for pg_temp alias */
440  if (strcmp(newRelation->schemaname, "pg_temp") == 0)
441  {
442  /* Initialize temp namespace if first time through */
445  return myTempNamespace;
446  }
447  /* use exact schema given */
448  namespaceId = get_namespace_oid(newRelation->schemaname, false);
449  /* we do not check for USAGE rights here! */
450  }
451  else if (newRelation->relpersistence == RELPERSISTENCE_TEMP)
452  {
453  /* Initialize temp namespace if first time through */
456  return myTempNamespace;
457  }
458  else
459  {
460  /* use the default creation namespace */
463  {
464  /* Need to initialize temp namespace */
466  return myTempNamespace;
467  }
468  namespaceId = activeCreationNamespace;
469  if (!OidIsValid(namespaceId))
470  ereport(ERROR,
471  (errcode(ERRCODE_UNDEFINED_SCHEMA),
472  errmsg("no schema has been selected to create in")));
473  }
474 
475  /* Note: callers will check for CREATE rights when appropriate */
476 
477  return namespaceId;
478 }
479 
480 /*
481  * RangeVarGetAndCheckCreationNamespace
482  *
483  * This function returns the OID of the namespace in which a new relation
484  * with a given name should be created. If the user does not have CREATE
485  * permission on the target namespace, this function will instead signal
486  * an ERROR.
487  *
488  * If non-NULL, *existing_oid is set to the OID of any existing relation with
489  * the same name which already exists in that namespace, or to InvalidOid if
490  * no such relation exists.
491  *
492  * If lockmode != NoLock, the specified lock mode is acquired on the existing
493  * relation, if any, provided that the current user owns the target relation.
494  * However, if lockmode != NoLock and the user does not own the target
495  * relation, we throw an ERROR, as we must not try to lock relations the
496  * user does not have permissions on.
497  *
498  * As a side effect, this function acquires AccessShareLock on the target
499  * namespace. Without this, the namespace could be dropped before our
500  * transaction commits, leaving behind relations with relnamespace pointing
501  * to a no-longer-existent namespace.
502  *
503  * As a further side-effect, if the selected namespace is a temporary namespace,
504  * we mark the RangeVar as RELPERSISTENCE_TEMP.
505  */
506 Oid
508  LOCKMODE lockmode,
509  Oid *existing_relation_id)
510 {
511  uint64 inval_count;
512  Oid relid;
513  Oid oldrelid = InvalidOid;
514  Oid nspid;
515  Oid oldnspid = InvalidOid;
516  bool retry = false;
517 
518  /*
519  * We check the catalog name and then ignore it.
520  */
521  if (relation->catalogname)
522  {
523  if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
524  ereport(ERROR,
525  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
526  errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
527  relation->catalogname, relation->schemaname,
528  relation->relname)));
529  }
530 
531  /*
532  * As in RangeVarGetRelidExtended(), we guard against concurrent DDL
533  * operations by tracking whether any invalidation messages are processed
534  * while we're doing the name lookups and acquiring locks. See comments
535  * in that function for a more detailed explanation of this logic.
536  */
537  for (;;)
538  {
539  AclResult aclresult;
540 
541  inval_count = SharedInvalidMessageCounter;
542 
543  /* Look up creation namespace and check for existing relation. */
544  nspid = RangeVarGetCreationNamespace(relation);
545  Assert(OidIsValid(nspid));
546  if (existing_relation_id != NULL)
547  relid = get_relname_relid(relation->relname, nspid);
548  else
549  relid = InvalidOid;
550 
551  /*
552  * In bootstrap processing mode, we don't bother with permissions or
553  * locking. Permissions might not be working yet, and locking is
554  * unnecessary.
555  */
557  break;
558 
559  /* Check namespace permissions. */
560  aclresult = pg_namespace_aclcheck(nspid, GetUserId(), ACL_CREATE);
561  if (aclresult != ACLCHECK_OK)
563  get_namespace_name(nspid));
564 
565  if (retry)
566  {
567  /* If nothing changed, we're done. */
568  if (relid == oldrelid && nspid == oldnspid)
569  break;
570  /* If creation namespace has changed, give up old lock. */
571  if (nspid != oldnspid)
574  /* If name points to something different, give up old lock. */
575  if (relid != oldrelid && OidIsValid(oldrelid) && lockmode != NoLock)
576  UnlockRelationOid(oldrelid, lockmode);
577  }
578 
579  /* Lock namespace. */
580  if (nspid != oldnspid)
582 
583  /* Lock relation, if required if and we have permission. */
584  if (lockmode != NoLock && OidIsValid(relid))
585  {
586  if (!pg_class_ownercheck(relid, GetUserId()))
588  relation->relname);
589  if (relid != oldrelid)
590  LockRelationOid(relid, lockmode);
591  }
592 
593  /* If no invalidation message were processed, we're done! */
594  if (inval_count == SharedInvalidMessageCounter)
595  break;
596 
597  /* Something may have changed, so recheck our work. */
598  retry = true;
599  oldrelid = relid;
600  oldnspid = nspid;
601  }
602 
603  RangeVarAdjustRelationPersistence(relation, nspid);
604  if (existing_relation_id != NULL)
605  *existing_relation_id = relid;
606  return nspid;
607 }
608 
609 /*
610  * Adjust the relpersistence for an about-to-be-created relation based on the
611  * creation namespace, and throw an error for invalid combinations.
612  */
613 void
615 {
616  switch (newRelation->relpersistence)
617  {
618  case RELPERSISTENCE_TEMP:
619  if (!isTempOrTempToastNamespace(nspid))
620  {
621  if (isAnyTempNamespace(nspid))
622  ereport(ERROR,
623  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
624  errmsg("cannot create relations in temporary schemas of other sessions")));
625  else
626  ereport(ERROR,
627  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
628  errmsg("cannot create temporary relation in non-temporary schema")));
629  }
630  break;
632  if (isTempOrTempToastNamespace(nspid))
633  newRelation->relpersistence = RELPERSISTENCE_TEMP;
634  else if (isAnyTempNamespace(nspid))
635  ereport(ERROR,
636  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
637  errmsg("cannot create relations in temporary schemas of other sessions")));
638  break;
639  default:
640  if (isAnyTempNamespace(nspid))
641  ereport(ERROR,
642  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
643  errmsg("only temporary relations may be created in temporary schemas")));
644  }
645 }
646 
647 /*
648  * RelnameGetRelid
649  * Try to resolve an unqualified relation name.
650  * Returns OID if relation found in search path, else InvalidOid.
651  */
652 Oid
653 RelnameGetRelid(const char *relname)
654 {
655  Oid relid;
656  ListCell *l;
657 
659 
660  foreach(l, activeSearchPath)
661  {
662  Oid namespaceId = lfirst_oid(l);
663 
664  relid = get_relname_relid(relname, namespaceId);
665  if (OidIsValid(relid))
666  return relid;
667  }
668 
669  /* Not found in path */
670  return InvalidOid;
671 }
672 
673 
674 /*
675  * RelationIsVisible
676  * Determine whether a relation (identified by OID) is visible in the
677  * current search path. Visible means "would be found by searching
678  * for the unqualified relation name".
679  */
680 bool
682 {
683  HeapTuple reltup;
684  Form_pg_class relform;
685  Oid relnamespace;
686  bool visible;
687 
688  reltup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
689  if (!HeapTupleIsValid(reltup))
690  elog(ERROR, "cache lookup failed for relation %u", relid);
691  relform = (Form_pg_class) GETSTRUCT(reltup);
692 
694 
695  /*
696  * Quick check: if it ain't in the path at all, it ain't visible. Items in
697  * the system namespace are surely in the path and so we needn't even do
698  * list_member_oid() for them.
699  */
700  relnamespace = relform->relnamespace;
701  if (relnamespace != PG_CATALOG_NAMESPACE &&
702  !list_member_oid(activeSearchPath, relnamespace))
703  visible = false;
704  else
705  {
706  /*
707  * If it is in the path, it might still not be visible; it could be
708  * hidden by another relation of the same name earlier in the path. So
709  * we must do a slow check for conflicting relations.
710  */
711  char *relname = NameStr(relform->relname);
712  ListCell *l;
713 
714  visible = false;
715  foreach(l, activeSearchPath)
716  {
717  Oid namespaceId = lfirst_oid(l);
718 
719  if (namespaceId == relnamespace)
720  {
721  /* Found it first in path */
722  visible = true;
723  break;
724  }
725  if (OidIsValid(get_relname_relid(relname, namespaceId)))
726  {
727  /* Found something else first in path */
728  break;
729  }
730  }
731  }
732 
733  ReleaseSysCache(reltup);
734 
735  return visible;
736 }
737 
738 
739 /*
740  * TypenameGetTypid
741  * Try to resolve an unqualified datatype name.
742  * Returns OID if type found in search path, else InvalidOid.
743  *
744  * This is essentially the same as RelnameGetRelid.
745  */
746 Oid
747 TypenameGetTypid(const char *typname)
748 {
749  Oid typid;
750  ListCell *l;
751 
753 
754  foreach(l, activeSearchPath)
755  {
756  Oid namespaceId = lfirst_oid(l);
757 
759  PointerGetDatum(typname),
760  ObjectIdGetDatum(namespaceId));
761  if (OidIsValid(typid))
762  return typid;
763  }
764 
765  /* Not found in path */
766  return InvalidOid;
767 }
768 
769 /*
770  * TypeIsVisible
771  * Determine whether a type (identified by OID) is visible in the
772  * current search path. Visible means "would be found by searching
773  * for the unqualified type name".
774  */
775 bool
777 {
778  HeapTuple typtup;
779  Form_pg_type typform;
780  Oid typnamespace;
781  bool visible;
782 
783  typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
784  if (!HeapTupleIsValid(typtup))
785  elog(ERROR, "cache lookup failed for type %u", typid);
786  typform = (Form_pg_type) GETSTRUCT(typtup);
787 
789 
790  /*
791  * Quick check: if it ain't in the path at all, it ain't visible. Items in
792  * the system namespace are surely in the path and so we needn't even do
793  * list_member_oid() for them.
794  */
795  typnamespace = typform->typnamespace;
796  if (typnamespace != PG_CATALOG_NAMESPACE &&
797  !list_member_oid(activeSearchPath, typnamespace))
798  visible = false;
799  else
800  {
801  /*
802  * If it is in the path, it might still not be visible; it could be
803  * hidden by another type of the same name earlier in the path. So we
804  * must do a slow check for conflicting types.
805  */
806  char *typname = NameStr(typform->typname);
807  ListCell *l;
808 
809  visible = false;
810  foreach(l, activeSearchPath)
811  {
812  Oid namespaceId = lfirst_oid(l);
813 
814  if (namespaceId == typnamespace)
815  {
816  /* Found it first in path */
817  visible = true;
818  break;
819  }
821  PointerGetDatum(typname),
822  ObjectIdGetDatum(namespaceId)))
823  {
824  /* Found something else first in path */
825  break;
826  }
827  }
828  }
829 
830  ReleaseSysCache(typtup);
831 
832  return visible;
833 }
834 
835 
836 /*
837  * FuncnameGetCandidates
838  * Given a possibly-qualified function name and argument count,
839  * retrieve a list of the possible matches.
840  *
841  * If nargs is -1, we return all functions matching the given name,
842  * regardless of argument count. (argnames must be NIL, and expand_variadic
843  * and expand_defaults must be false, in this case.)
844  *
845  * If argnames isn't NIL, we are considering a named- or mixed-notation call,
846  * and only functions having all the listed argument names will be returned.
847  * (We assume that length(argnames) <= nargs and all the passed-in names are
848  * distinct.) The returned structs will include an argnumbers array showing
849  * the actual argument index for each logical argument position.
850  *
851  * If expand_variadic is true, then variadic functions having the same number
852  * or fewer arguments will be retrieved, with the variadic argument and any
853  * additional argument positions filled with the variadic element type.
854  * nvargs in the returned struct is set to the number of such arguments.
855  * If expand_variadic is false, variadic arguments are not treated specially,
856  * and the returned nvargs will always be zero.
857  *
858  * If expand_defaults is true, functions that could match after insertion of
859  * default argument values will also be retrieved. In this case the returned
860  * structs could have nargs > passed-in nargs, and ndargs is set to the number
861  * of additional args (which can be retrieved from the function's
862  * proargdefaults entry).
863  *
864  * It is not possible for nvargs and ndargs to both be nonzero in the same
865  * list entry, since default insertion allows matches to functions with more
866  * than nargs arguments while the variadic transformation requires the same
867  * number or less.
868  *
869  * When argnames isn't NIL, the returned args[] type arrays are not ordered
870  * according to the functions' declarations, but rather according to the call:
871  * first any positional arguments, then the named arguments, then defaulted
872  * arguments (if needed and allowed by expand_defaults). The argnumbers[]
873  * array can be used to map this back to the catalog information.
874  * argnumbers[k] is set to the proargtypes index of the k'th call argument.
875  *
876  * We search a single namespace if the function name is qualified, else
877  * all namespaces in the search path. In the multiple-namespace case,
878  * we arrange for entries in earlier namespaces to mask identical entries in
879  * later namespaces.
880  *
881  * When expanding variadics, we arrange for non-variadic functions to mask
882  * variadic ones if the expanded argument list is the same. It is still
883  * possible for there to be conflicts between different variadic functions,
884  * however.
885  *
886  * It is guaranteed that the return list will never contain multiple entries
887  * with identical argument lists. When expand_defaults is true, the entries
888  * could have more than nargs positions, but we still guarantee that they are
889  * distinct in the first nargs positions. However, if argnames isn't NIL or
890  * either expand_variadic or expand_defaults is true, there might be multiple
891  * candidate functions that expand to identical argument lists. Rather than
892  * throw error here, we report such situations by returning a single entry
893  * with oid = 0 that represents a set of such conflicting candidates.
894  * The caller might end up discarding such an entry anyway, but if it selects
895  * such an entry it should react as though the call were ambiguous.
896  *
897  * If missing_ok is true, an empty list (NULL) is returned if the name was
898  * schema- qualified with a schema that does not exist. Likewise if no
899  * candidate is found for other reasons.
900  */
902 FuncnameGetCandidates(List *names, int nargs, List *argnames,
903  bool expand_variadic, bool expand_defaults,
904  bool missing_ok)
905 {
906  FuncCandidateList resultList = NULL;
907  bool any_special = false;
908  char *schemaname;
909  char *funcname;
910  Oid namespaceId;
911  CatCList *catlist;
912  int i;
913 
914  /* check for caller error */
915  Assert(nargs >= 0 || !(expand_variadic | expand_defaults));
916 
917  /* deconstruct the name list */
918  DeconstructQualifiedName(names, &schemaname, &funcname);
919 
920  if (schemaname)
921  {
922  /* use exact schema given */
923  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
924  if (!OidIsValid(namespaceId))
925  return NULL;
926  }
927  else
928  {
929  /* flag to indicate we need namespace search */
930  namespaceId = InvalidOid;
932  }
933 
934  /* Search syscache by name only */
936 
937  for (i = 0; i < catlist->n_members; i++)
938  {
939  HeapTuple proctup = &catlist->members[i]->tuple;
940  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
941  int pronargs = procform->pronargs;
942  int effective_nargs;
943  int pathpos = 0;
944  bool variadic;
945  bool use_defaults;
946  Oid va_elem_type;
947  int *argnumbers = NULL;
948  FuncCandidateList newResult;
949 
950  if (OidIsValid(namespaceId))
951  {
952  /* Consider only procs in specified namespace */
953  if (procform->pronamespace != namespaceId)
954  continue;
955  }
956  else
957  {
958  /*
959  * Consider only procs that are in the search path and are not in
960  * the temp namespace.
961  */
962  ListCell *nsp;
963 
964  foreach(nsp, activeSearchPath)
965  {
966  if (procform->pronamespace == lfirst_oid(nsp) &&
967  procform->pronamespace != myTempNamespace)
968  break;
969  pathpos++;
970  }
971  if (nsp == NULL)
972  continue; /* proc is not in search path */
973  }
974 
975  if (argnames != NIL)
976  {
977  /*
978  * Call uses named or mixed notation
979  *
980  * Named or mixed notation can match a variadic function only if
981  * expand_variadic is off; otherwise there is no way to match the
982  * presumed-nameless parameters expanded from the variadic array.
983  */
984  if (OidIsValid(procform->provariadic) && expand_variadic)
985  continue;
986  va_elem_type = InvalidOid;
987  variadic = false;
988 
989  /*
990  * Check argument count.
991  */
992  Assert(nargs >= 0); /* -1 not supported with argnames */
993 
994  if (pronargs > nargs && expand_defaults)
995  {
996  /* Ignore if not enough default expressions */
997  if (nargs + procform->pronargdefaults < pronargs)
998  continue;
999  use_defaults = true;
1000  }
1001  else
1002  use_defaults = false;
1003 
1004  /* Ignore if it doesn't match requested argument count */
1005  if (pronargs != nargs && !use_defaults)
1006  continue;
1007 
1008  /* Check for argument name match, generate positional mapping */
1009  if (!MatchNamedCall(proctup, nargs, argnames,
1010  &argnumbers))
1011  continue;
1012 
1013  /* Named argument matching is always "special" */
1014  any_special = true;
1015  }
1016  else
1017  {
1018  /*
1019  * Call uses positional notation
1020  *
1021  * Check if function is variadic, and get variadic element type if
1022  * so. If expand_variadic is false, we should just ignore
1023  * variadic-ness.
1024  */
1025  if (pronargs <= nargs && expand_variadic)
1026  {
1027  va_elem_type = procform->provariadic;
1028  variadic = OidIsValid(va_elem_type);
1029  any_special |= variadic;
1030  }
1031  else
1032  {
1033  va_elem_type = InvalidOid;
1034  variadic = false;
1035  }
1036 
1037  /*
1038  * Check if function can match by using parameter defaults.
1039  */
1040  if (pronargs > nargs && expand_defaults)
1041  {
1042  /* Ignore if not enough default expressions */
1043  if (nargs + procform->pronargdefaults < pronargs)
1044  continue;
1045  use_defaults = true;
1046  any_special = true;
1047  }
1048  else
1049  use_defaults = false;
1050 
1051  /* Ignore if it doesn't match requested argument count */
1052  if (nargs >= 0 && pronargs != nargs && !variadic && !use_defaults)
1053  continue;
1054  }
1055 
1056  /*
1057  * We must compute the effective argument list so that we can easily
1058  * compare it to earlier results. We waste a palloc cycle if it gets
1059  * masked by an earlier result, but really that's a pretty infrequent
1060  * case so it's not worth worrying about.
1061  */
1062  effective_nargs = Max(pronargs, nargs);
1063  newResult = (FuncCandidateList)
1065  effective_nargs * sizeof(Oid));
1066  newResult->pathpos = pathpos;
1067  newResult->oid = HeapTupleGetOid(proctup);
1068  newResult->nargs = effective_nargs;
1069  newResult->argnumbers = argnumbers;
1070  if (argnumbers)
1071  {
1072  /* Re-order the argument types into call's logical order */
1073  Oid *proargtypes = procform->proargtypes.values;
1074  int i;
1075 
1076  for (i = 0; i < pronargs; i++)
1077  newResult->args[i] = proargtypes[argnumbers[i]];
1078  }
1079  else
1080  {
1081  /* Simple positional case, just copy proargtypes as-is */
1082  memcpy(newResult->args, procform->proargtypes.values,
1083  pronargs * sizeof(Oid));
1084  }
1085  if (variadic)
1086  {
1087  int i;
1088 
1089  newResult->nvargs = effective_nargs - pronargs + 1;
1090  /* Expand variadic argument into N copies of element type */
1091  for (i = pronargs - 1; i < effective_nargs; i++)
1092  newResult->args[i] = va_elem_type;
1093  }
1094  else
1095  newResult->nvargs = 0;
1096  newResult->ndargs = use_defaults ? pronargs - nargs : 0;
1097 
1098  /*
1099  * Does it have the same arguments as something we already accepted?
1100  * If so, decide what to do to avoid returning duplicate argument
1101  * lists. We can skip this check for the single-namespace case if no
1102  * special (named, variadic or defaults) match has been made, since
1103  * then the unique index on pg_proc guarantees all the matches have
1104  * different argument lists.
1105  */
1106  if (resultList != NULL &&
1107  (any_special || !OidIsValid(namespaceId)))
1108  {
1109  /*
1110  * If we have an ordered list from SearchSysCacheList (the normal
1111  * case), then any conflicting proc must immediately adjoin this
1112  * one in the list, so we only need to look at the newest result
1113  * item. If we have an unordered list, we have to scan the whole
1114  * result list. Also, if either the current candidate or any
1115  * previous candidate is a special match, we can't assume that
1116  * conflicts are adjacent.
1117  *
1118  * We ignore defaulted arguments in deciding what is a match.
1119  */
1120  FuncCandidateList prevResult;
1121 
1122  if (catlist->ordered && !any_special)
1123  {
1124  /* ndargs must be 0 if !any_special */
1125  if (effective_nargs == resultList->nargs &&
1126  memcmp(newResult->args,
1127  resultList->args,
1128  effective_nargs * sizeof(Oid)) == 0)
1129  prevResult = resultList;
1130  else
1131  prevResult = NULL;
1132  }
1133  else
1134  {
1135  int cmp_nargs = newResult->nargs - newResult->ndargs;
1136 
1137  for (prevResult = resultList;
1138  prevResult;
1139  prevResult = prevResult->next)
1140  {
1141  if (cmp_nargs == prevResult->nargs - prevResult->ndargs &&
1142  memcmp(newResult->args,
1143  prevResult->args,
1144  cmp_nargs * sizeof(Oid)) == 0)
1145  break;
1146  }
1147  }
1148 
1149  if (prevResult)
1150  {
1151  /*
1152  * We have a match with a previous result. Decide which one
1153  * to keep, or mark it ambiguous if we can't decide. The
1154  * logic here is preference > 0 means prefer the old result,
1155  * preference < 0 means prefer the new, preference = 0 means
1156  * ambiguous.
1157  */
1158  int preference;
1159 
1160  if (pathpos != prevResult->pathpos)
1161  {
1162  /*
1163  * Prefer the one that's earlier in the search path.
1164  */
1165  preference = pathpos - prevResult->pathpos;
1166  }
1167  else if (variadic && prevResult->nvargs == 0)
1168  {
1169  /*
1170  * With variadic functions we could have, for example,
1171  * both foo(numeric) and foo(variadic numeric[]) in the
1172  * same namespace; if so we prefer the non-variadic match
1173  * on efficiency grounds.
1174  */
1175  preference = 1;
1176  }
1177  else if (!variadic && prevResult->nvargs > 0)
1178  {
1179  preference = -1;
1180  }
1181  else
1182  {
1183  /*----------
1184  * We can't decide. This can happen with, for example,
1185  * both foo(numeric, variadic numeric[]) and
1186  * foo(variadic numeric[]) in the same namespace, or
1187  * both foo(int) and foo (int, int default something)
1188  * in the same namespace, or both foo(a int, b text)
1189  * and foo(b text, a int) in the same namespace.
1190  *----------
1191  */
1192  preference = 0;
1193  }
1194 
1195  if (preference > 0)
1196  {
1197  /* keep previous result */
1198  pfree(newResult);
1199  continue;
1200  }
1201  else if (preference < 0)
1202  {
1203  /* remove previous result from the list */
1204  if (prevResult == resultList)
1205  resultList = prevResult->next;
1206  else
1207  {
1208  FuncCandidateList prevPrevResult;
1209 
1210  for (prevPrevResult = resultList;
1211  prevPrevResult;
1212  prevPrevResult = prevPrevResult->next)
1213  {
1214  if (prevResult == prevPrevResult->next)
1215  {
1216  prevPrevResult->next = prevResult->next;
1217  break;
1218  }
1219  }
1220  Assert(prevPrevResult); /* assert we found it */
1221  }
1222  pfree(prevResult);
1223  /* fall through to add newResult to list */
1224  }
1225  else
1226  {
1227  /* mark old result as ambiguous, discard new */
1228  prevResult->oid = InvalidOid;
1229  pfree(newResult);
1230  continue;
1231  }
1232  }
1233  }
1234 
1235  /*
1236  * Okay to add it to result list
1237  */
1238  newResult->next = resultList;
1239  resultList = newResult;
1240  }
1241 
1242  ReleaseSysCacheList(catlist);
1243 
1244  return resultList;
1245 }
1246 
1247 /*
1248  * MatchNamedCall
1249  * Given a pg_proc heap tuple and a call's list of argument names,
1250  * check whether the function could match the call.
1251  *
1252  * The call could match if all supplied argument names are accepted by
1253  * the function, in positions after the last positional argument, and there
1254  * are defaults for all unsupplied arguments.
1255  *
1256  * The number of positional arguments is nargs - list_length(argnames).
1257  * Note caller has already done basic checks on argument count.
1258  *
1259  * On match, return true and fill *argnumbers with a palloc'd array showing
1260  * the mapping from call argument positions to actual function argument
1261  * numbers. Defaulted arguments are included in this map, at positions
1262  * after the last supplied argument.
1263  */
1264 static bool
1265 MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
1266  int **argnumbers)
1267 {
1268  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
1269  int pronargs = procform->pronargs;
1270  int numposargs = nargs - list_length(argnames);
1271  int pronallargs;
1272  Oid *p_argtypes;
1273  char **p_argnames;
1274  char *p_argmodes;
1275  bool arggiven[FUNC_MAX_ARGS];
1276  bool isnull;
1277  int ap; /* call args position */
1278  int pp; /* proargs position */
1279  ListCell *lc;
1280 
1281  Assert(argnames != NIL);
1282  Assert(numposargs >= 0);
1283  Assert(nargs <= pronargs);
1284 
1285  /* Ignore this function if its proargnames is null */
1287  &isnull);
1288  if (isnull)
1289  return false;
1290 
1291  /* OK, let's extract the argument names and types */
1292  pronallargs = get_func_arg_info(proctup,
1293  &p_argtypes, &p_argnames, &p_argmodes);
1294  Assert(p_argnames != NULL);
1295 
1296  /* initialize state for matching */
1297  *argnumbers = (int *) palloc(pronargs * sizeof(int));
1298  memset(arggiven, false, pronargs * sizeof(bool));
1299 
1300  /* there are numposargs positional args before the named args */
1301  for (ap = 0; ap < numposargs; ap++)
1302  {
1303  (*argnumbers)[ap] = ap;
1304  arggiven[ap] = true;
1305  }
1306 
1307  /* now examine the named args */
1308  foreach(lc, argnames)
1309  {
1310  char *argname = (char *) lfirst(lc);
1311  bool found;
1312  int i;
1313 
1314  pp = 0;
1315  found = false;
1316  for (i = 0; i < pronallargs; i++)
1317  {
1318  /* consider only input parameters */
1319  if (p_argmodes &&
1320  (p_argmodes[i] != FUNC_PARAM_IN &&
1321  p_argmodes[i] != FUNC_PARAM_INOUT &&
1322  p_argmodes[i] != FUNC_PARAM_VARIADIC))
1323  continue;
1324  if (p_argnames[i] && strcmp(p_argnames[i], argname) == 0)
1325  {
1326  /* fail if argname matches a positional argument */
1327  if (arggiven[pp])
1328  return false;
1329  arggiven[pp] = true;
1330  (*argnumbers)[ap] = pp;
1331  found = true;
1332  break;
1333  }
1334  /* increase pp only for input parameters */
1335  pp++;
1336  }
1337  /* if name isn't in proargnames, fail */
1338  if (!found)
1339  return false;
1340  ap++;
1341  }
1342 
1343  Assert(ap == nargs); /* processed all actual parameters */
1344 
1345  /* Check for default arguments */
1346  if (nargs < pronargs)
1347  {
1348  int first_arg_with_default = pronargs - procform->pronargdefaults;
1349 
1350  for (pp = numposargs; pp < pronargs; pp++)
1351  {
1352  if (arggiven[pp])
1353  continue;
1354  /* fail if arg not given and no default available */
1355  if (pp < first_arg_with_default)
1356  return false;
1357  (*argnumbers)[ap++] = pp;
1358  }
1359  }
1360 
1361  Assert(ap == pronargs); /* processed all function parameters */
1362 
1363  return true;
1364 }
1365 
1366 /*
1367  * FunctionIsVisible
1368  * Determine whether a function (identified by OID) is visible in the
1369  * current search path. Visible means "would be found by searching
1370  * for the unqualified function name with exact argument matches".
1371  */
1372 bool
1374 {
1375  HeapTuple proctup;
1376  Form_pg_proc procform;
1377  Oid pronamespace;
1378  bool visible;
1379 
1380  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1381  if (!HeapTupleIsValid(proctup))
1382  elog(ERROR, "cache lookup failed for function %u", funcid);
1383  procform = (Form_pg_proc) GETSTRUCT(proctup);
1384 
1386 
1387  /*
1388  * Quick check: if it ain't in the path at all, it ain't visible. Items in
1389  * the system namespace are surely in the path and so we needn't even do
1390  * list_member_oid() for them.
1391  */
1392  pronamespace = procform->pronamespace;
1393  if (pronamespace != PG_CATALOG_NAMESPACE &&
1394  !list_member_oid(activeSearchPath, pronamespace))
1395  visible = false;
1396  else
1397  {
1398  /*
1399  * If it is in the path, it might still not be visible; it could be
1400  * hidden by another proc of the same name and arguments earlier in
1401  * the path. So we must do a slow check to see if this is the same
1402  * proc that would be found by FuncnameGetCandidates.
1403  */
1404  char *proname = NameStr(procform->proname);
1405  int nargs = procform->pronargs;
1406  FuncCandidateList clist;
1407 
1408  visible = false;
1409 
1410  clist = FuncnameGetCandidates(list_make1(makeString(proname)),
1411  nargs, NIL, false, false, false);
1412 
1413  for (; clist; clist = clist->next)
1414  {
1415  if (memcmp(clist->args, procform->proargtypes.values,
1416  nargs * sizeof(Oid)) == 0)
1417  {
1418  /* Found the expected entry; is it the right proc? */
1419  visible = (clist->oid == funcid);
1420  break;
1421  }
1422  }
1423  }
1424 
1425  ReleaseSysCache(proctup);
1426 
1427  return visible;
1428 }
1429 
1430 
1431 /*
1432  * OpernameGetOprid
1433  * Given a possibly-qualified operator name and exact input datatypes,
1434  * look up the operator. Returns InvalidOid if not found.
1435  *
1436  * Pass oprleft = InvalidOid for a prefix op, oprright = InvalidOid for
1437  * a postfix op.
1438  *
1439  * If the operator name is not schema-qualified, it is sought in the current
1440  * namespace search path. If the name is schema-qualified and the given
1441  * schema does not exist, InvalidOid is returned.
1442  */
1443 Oid
1444 OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
1445 {
1446  char *schemaname;
1447  char *opername;
1448  CatCList *catlist;
1449  ListCell *l;
1450 
1451  /* deconstruct the name list */
1452  DeconstructQualifiedName(names, &schemaname, &opername);
1453 
1454  if (schemaname)
1455  {
1456  /* search only in exact schema given */
1457  Oid namespaceId;
1458 
1459  namespaceId = LookupExplicitNamespace(schemaname, true);
1460  if (OidIsValid(namespaceId))
1461  {
1462  HeapTuple opertup;
1463 
1464  opertup = SearchSysCache4(OPERNAMENSP,
1465  CStringGetDatum(opername),
1466  ObjectIdGetDatum(oprleft),
1467  ObjectIdGetDatum(oprright),
1468  ObjectIdGetDatum(namespaceId));
1469  if (HeapTupleIsValid(opertup))
1470  {
1471  Oid result = HeapTupleGetOid(opertup);
1472 
1473  ReleaseSysCache(opertup);
1474  return result;
1475  }
1476  }
1477 
1478  return InvalidOid;
1479  }
1480 
1481  /* Search syscache by name and argument types */
1482  catlist = SearchSysCacheList3(OPERNAMENSP,
1483  CStringGetDatum(opername),
1484  ObjectIdGetDatum(oprleft),
1485  ObjectIdGetDatum(oprright));
1486 
1487  if (catlist->n_members == 0)
1488  {
1489  /* no hope, fall out early */
1490  ReleaseSysCacheList(catlist);
1491  return InvalidOid;
1492  }
1493 
1494  /*
1495  * We have to find the list member that is first in the search path, if
1496  * there's more than one. This doubly-nested loop looks ugly, but in
1497  * practice there should usually be few catlist members.
1498  */
1500 
1501  foreach(l, activeSearchPath)
1502  {
1503  Oid namespaceId = lfirst_oid(l);
1504  int i;
1505 
1506  if (namespaceId == myTempNamespace)
1507  continue; /* do not look in temp namespace */
1508 
1509  for (i = 0; i < catlist->n_members; i++)
1510  {
1511  HeapTuple opertup = &catlist->members[i]->tuple;
1512  Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1513 
1514  if (operform->oprnamespace == namespaceId)
1515  {
1516  Oid result = HeapTupleGetOid(opertup);
1517 
1518  ReleaseSysCacheList(catlist);
1519  return result;
1520  }
1521  }
1522  }
1523 
1524  ReleaseSysCacheList(catlist);
1525  return InvalidOid;
1526 }
1527 
1528 /*
1529  * OpernameGetCandidates
1530  * Given a possibly-qualified operator name and operator kind,
1531  * retrieve a list of the possible matches.
1532  *
1533  * If oprkind is '\0', we return all operators matching the given name,
1534  * regardless of arguments.
1535  *
1536  * We search a single namespace if the operator name is qualified, else
1537  * all namespaces in the search path. The return list will never contain
1538  * multiple entries with identical argument lists --- in the multiple-
1539  * namespace case, we arrange for entries in earlier namespaces to mask
1540  * identical entries in later namespaces.
1541  *
1542  * The returned items always have two args[] entries --- one or the other
1543  * will be InvalidOid for a prefix or postfix oprkind. nargs is 2, too.
1544  */
1546 OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok)
1547 {
1548  FuncCandidateList resultList = NULL;
1549  char *resultSpace = NULL;
1550  int nextResult = 0;
1551  char *schemaname;
1552  char *opername;
1553  Oid namespaceId;
1554  CatCList *catlist;
1555  int i;
1556 
1557  /* deconstruct the name list */
1558  DeconstructQualifiedName(names, &schemaname, &opername);
1559 
1560  if (schemaname)
1561  {
1562  /* use exact schema given */
1563  namespaceId = LookupExplicitNamespace(schemaname, missing_schema_ok);
1564  if (missing_schema_ok && !OidIsValid(namespaceId))
1565  return NULL;
1566  }
1567  else
1568  {
1569  /* flag to indicate we need namespace search */
1570  namespaceId = InvalidOid;
1572  }
1573 
1574  /* Search syscache by name only */
1575  catlist = SearchSysCacheList1(OPERNAMENSP, CStringGetDatum(opername));
1576 
1577  /*
1578  * In typical scenarios, most if not all of the operators found by the
1579  * catcache search will end up getting returned; and there can be quite a
1580  * few, for common operator names such as '=' or '+'. To reduce the time
1581  * spent in palloc, we allocate the result space as an array large enough
1582  * to hold all the operators. The original coding of this routine did a
1583  * separate palloc for each operator, but profiling revealed that the
1584  * pallocs used an unreasonably large fraction of parsing time.
1585  */
1586 #define SPACE_PER_OP MAXALIGN(offsetof(struct _FuncCandidateList, args) + \
1587  2 * sizeof(Oid))
1588 
1589  if (catlist->n_members > 0)
1590  resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
1591 
1592  for (i = 0; i < catlist->n_members; i++)
1593  {
1594  HeapTuple opertup = &catlist->members[i]->tuple;
1595  Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1596  int pathpos = 0;
1597  FuncCandidateList newResult;
1598 
1599  /* Ignore operators of wrong kind, if specific kind requested */
1600  if (oprkind && operform->oprkind != oprkind)
1601  continue;
1602 
1603  if (OidIsValid(namespaceId))
1604  {
1605  /* Consider only opers in specified namespace */
1606  if (operform->oprnamespace != namespaceId)
1607  continue;
1608  /* No need to check args, they must all be different */
1609  }
1610  else
1611  {
1612  /*
1613  * Consider only opers that are in the search path and are not in
1614  * the temp namespace.
1615  */
1616  ListCell *nsp;
1617 
1618  foreach(nsp, activeSearchPath)
1619  {
1620  if (operform->oprnamespace == lfirst_oid(nsp) &&
1621  operform->oprnamespace != myTempNamespace)
1622  break;
1623  pathpos++;
1624  }
1625  if (nsp == NULL)
1626  continue; /* oper is not in search path */
1627 
1628  /*
1629  * Okay, it's in the search path, but does it have the same
1630  * arguments as something we already accepted? If so, keep only
1631  * the one that appears earlier in the search path.
1632  *
1633  * If we have an ordered list from SearchSysCacheList (the normal
1634  * case), then any conflicting oper must immediately adjoin this
1635  * one in the list, so we only need to look at the newest result
1636  * item. If we have an unordered list, we have to scan the whole
1637  * result list.
1638  */
1639  if (resultList)
1640  {
1641  FuncCandidateList prevResult;
1642 
1643  if (catlist->ordered)
1644  {
1645  if (operform->oprleft == resultList->args[0] &&
1646  operform->oprright == resultList->args[1])
1647  prevResult = resultList;
1648  else
1649  prevResult = NULL;
1650  }
1651  else
1652  {
1653  for (prevResult = resultList;
1654  prevResult;
1655  prevResult = prevResult->next)
1656  {
1657  if (operform->oprleft == prevResult->args[0] &&
1658  operform->oprright == prevResult->args[1])
1659  break;
1660  }
1661  }
1662  if (prevResult)
1663  {
1664  /* We have a match with a previous result */
1665  Assert(pathpos != prevResult->pathpos);
1666  if (pathpos > prevResult->pathpos)
1667  continue; /* keep previous result */
1668  /* replace previous result */
1669  prevResult->pathpos = pathpos;
1670  prevResult->oid = HeapTupleGetOid(opertup);
1671  continue; /* args are same, of course */
1672  }
1673  }
1674  }
1675 
1676  /*
1677  * Okay to add it to result list
1678  */
1679  newResult = (FuncCandidateList) (resultSpace + nextResult);
1680  nextResult += SPACE_PER_OP;
1681 
1682  newResult->pathpos = pathpos;
1683  newResult->oid = HeapTupleGetOid(opertup);
1684  newResult->nargs = 2;
1685  newResult->nvargs = 0;
1686  newResult->ndargs = 0;
1687  newResult->argnumbers = NULL;
1688  newResult->args[0] = operform->oprleft;
1689  newResult->args[1] = operform->oprright;
1690  newResult->next = resultList;
1691  resultList = newResult;
1692  }
1693 
1694  ReleaseSysCacheList(catlist);
1695 
1696  return resultList;
1697 }
1698 
1699 /*
1700  * OperatorIsVisible
1701  * Determine whether an operator (identified by OID) is visible in the
1702  * current search path. Visible means "would be found by searching
1703  * for the unqualified operator name with exact argument matches".
1704  */
1705 bool
1707 {
1708  HeapTuple oprtup;
1709  Form_pg_operator oprform;
1710  Oid oprnamespace;
1711  bool visible;
1712 
1713  oprtup = SearchSysCache1(OPEROID, ObjectIdGetDatum(oprid));
1714  if (!HeapTupleIsValid(oprtup))
1715  elog(ERROR, "cache lookup failed for operator %u", oprid);
1716  oprform = (Form_pg_operator) GETSTRUCT(oprtup);
1717 
1719 
1720  /*
1721  * Quick check: if it ain't in the path at all, it ain't visible. Items in
1722  * the system namespace are surely in the path and so we needn't even do
1723  * list_member_oid() for them.
1724  */
1725  oprnamespace = oprform->oprnamespace;
1726  if (oprnamespace != PG_CATALOG_NAMESPACE &&
1727  !list_member_oid(activeSearchPath, oprnamespace))
1728  visible = false;
1729  else
1730  {
1731  /*
1732  * If it is in the path, it might still not be visible; it could be
1733  * hidden by another operator of the same name and arguments earlier
1734  * in the path. So we must do a slow check to see if this is the same
1735  * operator that would be found by OpernameGetOprid.
1736  */
1737  char *oprname = NameStr(oprform->oprname);
1738 
1739  visible = (OpernameGetOprid(list_make1(makeString(oprname)),
1740  oprform->oprleft, oprform->oprright)
1741  == oprid);
1742  }
1743 
1744  ReleaseSysCache(oprtup);
1745 
1746  return visible;
1747 }
1748 
1749 
1750 /*
1751  * OpclassnameGetOpcid
1752  * Try to resolve an unqualified index opclass name.
1753  * Returns OID if opclass found in search path, else InvalidOid.
1754  *
1755  * This is essentially the same as TypenameGetTypid, but we have to have
1756  * an extra argument for the index AM OID.
1757  */
1758 Oid
1759 OpclassnameGetOpcid(Oid amid, const char *opcname)
1760 {
1761  Oid opcid;
1762  ListCell *l;
1763 
1765 
1766  foreach(l, activeSearchPath)
1767  {
1768  Oid namespaceId = lfirst_oid(l);
1769 
1770  if (namespaceId == myTempNamespace)
1771  continue; /* do not look in temp namespace */
1772 
1773  opcid = GetSysCacheOid3(CLAAMNAMENSP,
1774  ObjectIdGetDatum(amid),
1775  PointerGetDatum(opcname),
1776  ObjectIdGetDatum(namespaceId));
1777  if (OidIsValid(opcid))
1778  return opcid;
1779  }
1780 
1781  /* Not found in path */
1782  return InvalidOid;
1783 }
1784 
1785 /*
1786  * OpclassIsVisible
1787  * Determine whether an opclass (identified by OID) is visible in the
1788  * current search path. Visible means "would be found by searching
1789  * for the unqualified opclass name".
1790  */
1791 bool
1793 {
1794  HeapTuple opctup;
1795  Form_pg_opclass opcform;
1796  Oid opcnamespace;
1797  bool visible;
1798 
1799  opctup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opcid));
1800  if (!HeapTupleIsValid(opctup))
1801  elog(ERROR, "cache lookup failed for opclass %u", opcid);
1802  opcform = (Form_pg_opclass) GETSTRUCT(opctup);
1803 
1805 
1806  /*
1807  * Quick check: if it ain't in the path at all, it ain't visible. Items in
1808  * the system namespace are surely in the path and so we needn't even do
1809  * list_member_oid() for them.
1810  */
1811  opcnamespace = opcform->opcnamespace;
1812  if (opcnamespace != PG_CATALOG_NAMESPACE &&
1813  !list_member_oid(activeSearchPath, opcnamespace))
1814  visible = false;
1815  else
1816  {
1817  /*
1818  * If it is in the path, it might still not be visible; it could be
1819  * hidden by another opclass of the same name earlier in the path. So
1820  * we must do a slow check to see if this opclass would be found by
1821  * OpclassnameGetOpcid.
1822  */
1823  char *opcname = NameStr(opcform->opcname);
1824 
1825  visible = (OpclassnameGetOpcid(opcform->opcmethod, opcname) == opcid);
1826  }
1827 
1828  ReleaseSysCache(opctup);
1829 
1830  return visible;
1831 }
1832 
1833 /*
1834  * OpfamilynameGetOpfid
1835  * Try to resolve an unqualified index opfamily name.
1836  * Returns OID if opfamily found in search path, else InvalidOid.
1837  *
1838  * This is essentially the same as TypenameGetTypid, but we have to have
1839  * an extra argument for the index AM OID.
1840  */
1841 Oid
1842 OpfamilynameGetOpfid(Oid amid, const char *opfname)
1843 {
1844  Oid opfid;
1845  ListCell *l;
1846 
1848 
1849  foreach(l, activeSearchPath)
1850  {
1851  Oid namespaceId = lfirst_oid(l);
1852 
1853  if (namespaceId == myTempNamespace)
1854  continue; /* do not look in temp namespace */
1855 
1857  ObjectIdGetDatum(amid),
1858  PointerGetDatum(opfname),
1859  ObjectIdGetDatum(namespaceId));
1860  if (OidIsValid(opfid))
1861  return opfid;
1862  }
1863 
1864  /* Not found in path */
1865  return InvalidOid;
1866 }
1867 
1868 /*
1869  * OpfamilyIsVisible
1870  * Determine whether an opfamily (identified by OID) is visible in the
1871  * current search path. Visible means "would be found by searching
1872  * for the unqualified opfamily name".
1873  */
1874 bool
1876 {
1877  HeapTuple opftup;
1878  Form_pg_opfamily opfform;
1879  Oid opfnamespace;
1880  bool visible;
1881 
1882  opftup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
1883  if (!HeapTupleIsValid(opftup))
1884  elog(ERROR, "cache lookup failed for opfamily %u", opfid);
1885  opfform = (Form_pg_opfamily) GETSTRUCT(opftup);
1886 
1888 
1889  /*
1890  * Quick check: if it ain't in the path at all, it ain't visible. Items in
1891  * the system namespace are surely in the path and so we needn't even do
1892  * list_member_oid() for them.
1893  */
1894  opfnamespace = opfform->opfnamespace;
1895  if (opfnamespace != PG_CATALOG_NAMESPACE &&
1896  !list_member_oid(activeSearchPath, opfnamespace))
1897  visible = false;
1898  else
1899  {
1900  /*
1901  * If it is in the path, it might still not be visible; it could be
1902  * hidden by another opfamily of the same name earlier in the path. So
1903  * we must do a slow check to see if this opfamily would be found by
1904  * OpfamilynameGetOpfid.
1905  */
1906  char *opfname = NameStr(opfform->opfname);
1907 
1908  visible = (OpfamilynameGetOpfid(opfform->opfmethod, opfname) == opfid);
1909  }
1910 
1911  ReleaseSysCache(opftup);
1912 
1913  return visible;
1914 }
1915 
1916 /*
1917  * CollationGetCollid
1918  * Try to resolve an unqualified collation name.
1919  * Returns OID if collation found in search path, else InvalidOid.
1920  */
1921 Oid
1922 CollationGetCollid(const char *collname)
1923 {
1924  int32 dbencoding = GetDatabaseEncoding();
1925  ListCell *l;
1926 
1928 
1929  foreach(l, activeSearchPath)
1930  {
1931  Oid namespaceId = lfirst_oid(l);
1932  Oid collid;
1933 
1934  if (namespaceId == myTempNamespace)
1935  continue; /* do not look in temp namespace */
1936 
1937  /* Check for database-encoding-specific entry */
1939  PointerGetDatum(collname),
1940  Int32GetDatum(dbencoding),
1941  ObjectIdGetDatum(namespaceId));
1942  if (OidIsValid(collid))
1943  return collid;
1944 
1945  /* Check for any-encoding entry */
1947  PointerGetDatum(collname),
1948  Int32GetDatum(-1),
1949  ObjectIdGetDatum(namespaceId));
1950  if (OidIsValid(collid))
1951  return collid;
1952  }
1953 
1954  /* Not found in path */
1955  return InvalidOid;
1956 }
1957 
1958 /*
1959  * CollationIsVisible
1960  * Determine whether a collation (identified by OID) is visible in the
1961  * current search path. Visible means "would be found by searching
1962  * for the unqualified collation name".
1963  */
1964 bool
1966 {
1967  HeapTuple colltup;
1968  Form_pg_collation collform;
1969  Oid collnamespace;
1970  bool visible;
1971 
1972  colltup = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1973  if (!HeapTupleIsValid(colltup))
1974  elog(ERROR, "cache lookup failed for collation %u", collid);
1975  collform = (Form_pg_collation) GETSTRUCT(colltup);
1976 
1978 
1979  /*
1980  * Quick check: if it ain't in the path at all, it ain't visible. Items in
1981  * the system namespace are surely in the path and so we needn't even do
1982  * list_member_oid() for them.
1983  */
1984  collnamespace = collform->collnamespace;
1985  if (collnamespace != PG_CATALOG_NAMESPACE &&
1986  !list_member_oid(activeSearchPath, collnamespace))
1987  visible = false;
1988  else
1989  {
1990  /*
1991  * If it is in the path, it might still not be visible; it could be
1992  * hidden by another conversion of the same name earlier in the path.
1993  * So we must do a slow check to see if this conversion would be found
1994  * by CollationGetCollid.
1995  */
1996  char *collname = NameStr(collform->collname);
1997 
1998  visible = (CollationGetCollid(collname) == collid);
1999  }
2000 
2001  ReleaseSysCache(colltup);
2002 
2003  return visible;
2004 }
2005 
2006 
2007 /*
2008  * ConversionGetConid
2009  * Try to resolve an unqualified conversion name.
2010  * Returns OID if conversion found in search path, else InvalidOid.
2011  *
2012  * This is essentially the same as RelnameGetRelid.
2013  */
2014 Oid
2015 ConversionGetConid(const char *conname)
2016 {
2017  Oid conid;
2018  ListCell *l;
2019 
2021 
2022  foreach(l, activeSearchPath)
2023  {
2024  Oid namespaceId = lfirst_oid(l);
2025 
2026  if (namespaceId == myTempNamespace)
2027  continue; /* do not look in temp namespace */
2028 
2029  conid = GetSysCacheOid2(CONNAMENSP,
2030  PointerGetDatum(conname),
2031  ObjectIdGetDatum(namespaceId));
2032  if (OidIsValid(conid))
2033  return conid;
2034  }
2035 
2036  /* Not found in path */
2037  return InvalidOid;
2038 }
2039 
2040 /*
2041  * ConversionIsVisible
2042  * Determine whether a conversion (identified by OID) is visible in the
2043  * current search path. Visible means "would be found by searching
2044  * for the unqualified conversion name".
2045  */
2046 bool
2048 {
2049  HeapTuple contup;
2050  Form_pg_conversion conform;
2051  Oid connamespace;
2052  bool visible;
2053 
2054  contup = SearchSysCache1(CONVOID, ObjectIdGetDatum(conid));
2055  if (!HeapTupleIsValid(contup))
2056  elog(ERROR, "cache lookup failed for conversion %u", conid);
2057  conform = (Form_pg_conversion) GETSTRUCT(contup);
2058 
2060 
2061  /*
2062  * Quick check: if it ain't in the path at all, it ain't visible. Items in
2063  * the system namespace are surely in the path and so we needn't even do
2064  * list_member_oid() for them.
2065  */
2066  connamespace = conform->connamespace;
2067  if (connamespace != PG_CATALOG_NAMESPACE &&
2068  !list_member_oid(activeSearchPath, connamespace))
2069  visible = false;
2070  else
2071  {
2072  /*
2073  * If it is in the path, it might still not be visible; it could be
2074  * hidden by another conversion of the same name earlier in the path.
2075  * So we must do a slow check to see if this conversion would be found
2076  * by ConversionGetConid.
2077  */
2078  char *conname = NameStr(conform->conname);
2079 
2080  visible = (ConversionGetConid(conname) == conid);
2081  }
2082 
2083  ReleaseSysCache(contup);
2084 
2085  return visible;
2086 }
2087 
2088 /*
2089  * get_ts_parser_oid - find a TS parser by possibly qualified name
2090  *
2091  * If not found, returns InvalidOid if missing_ok, else throws error
2092  */
2093 Oid
2094 get_ts_parser_oid(List *names, bool missing_ok)
2095 {
2096  char *schemaname;
2097  char *parser_name;
2098  Oid namespaceId;
2099  Oid prsoid = InvalidOid;
2100  ListCell *l;
2101 
2102  /* deconstruct the name list */
2103  DeconstructQualifiedName(names, &schemaname, &parser_name);
2104 
2105  if (schemaname)
2106  {
2107  /* use exact schema given */
2108  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2109  if (missing_ok && !OidIsValid(namespaceId))
2110  prsoid = InvalidOid;
2111  else
2113  PointerGetDatum(parser_name),
2114  ObjectIdGetDatum(namespaceId));
2115  }
2116  else
2117  {
2118  /* search for it in search path */
2120 
2121  foreach(l, activeSearchPath)
2122  {
2123  namespaceId = lfirst_oid(l);
2124 
2125  if (namespaceId == myTempNamespace)
2126  continue; /* do not look in temp namespace */
2127 
2129  PointerGetDatum(parser_name),
2130  ObjectIdGetDatum(namespaceId));
2131  if (OidIsValid(prsoid))
2132  break;
2133  }
2134  }
2135 
2136  if (!OidIsValid(prsoid) && !missing_ok)
2137  ereport(ERROR,
2138  (errcode(ERRCODE_UNDEFINED_OBJECT),
2139  errmsg("text search parser \"%s\" does not exist",
2140  NameListToString(names))));
2141 
2142  return prsoid;
2143 }
2144 
2145 /*
2146  * TSParserIsVisible
2147  * Determine whether a parser (identified by OID) is visible in the
2148  * current search path. Visible means "would be found by searching
2149  * for the unqualified parser name".
2150  */
2151 bool
2153 {
2154  HeapTuple tup;
2155  Form_pg_ts_parser form;
2156  Oid namespace;
2157  bool visible;
2158 
2160  if (!HeapTupleIsValid(tup))
2161  elog(ERROR, "cache lookup failed for text search parser %u", prsId);
2162  form = (Form_pg_ts_parser) GETSTRUCT(tup);
2163 
2165 
2166  /*
2167  * Quick check: if it ain't in the path at all, it ain't visible. Items in
2168  * the system namespace are surely in the path and so we needn't even do
2169  * list_member_oid() for them.
2170  */
2171  namespace = form->prsnamespace;
2172  if (namespace != PG_CATALOG_NAMESPACE &&
2173  !list_member_oid(activeSearchPath, namespace))
2174  visible = false;
2175  else
2176  {
2177  /*
2178  * If it is in the path, it might still not be visible; it could be
2179  * hidden by another parser of the same name earlier in the path. So
2180  * we must do a slow check for conflicting parsers.
2181  */
2182  char *name = NameStr(form->prsname);
2183  ListCell *l;
2184 
2185  visible = false;
2186  foreach(l, activeSearchPath)
2187  {
2188  Oid namespaceId = lfirst_oid(l);
2189 
2190  if (namespaceId == myTempNamespace)
2191  continue; /* do not look in temp namespace */
2192 
2193  if (namespaceId == namespace)
2194  {
2195  /* Found it first in path */
2196  visible = true;
2197  break;
2198  }
2200  PointerGetDatum(name),
2201  ObjectIdGetDatum(namespaceId)))
2202  {
2203  /* Found something else first in path */
2204  break;
2205  }
2206  }
2207  }
2208 
2209  ReleaseSysCache(tup);
2210 
2211  return visible;
2212 }
2213 
2214 /*
2215  * get_ts_dict_oid - find a TS dictionary by possibly qualified name
2216  *
2217  * If not found, returns InvalidOid if failOK, else throws error
2218  */
2219 Oid
2220 get_ts_dict_oid(List *names, bool missing_ok)
2221 {
2222  char *schemaname;
2223  char *dict_name;
2224  Oid namespaceId;
2225  Oid dictoid = InvalidOid;
2226  ListCell *l;
2227 
2228  /* deconstruct the name list */
2229  DeconstructQualifiedName(names, &schemaname, &dict_name);
2230 
2231  if (schemaname)
2232  {
2233  /* use exact schema given */
2234  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2235  if (missing_ok && !OidIsValid(namespaceId))
2236  dictoid = InvalidOid;
2237  else
2238  dictoid = GetSysCacheOid2(TSDICTNAMENSP,
2239  PointerGetDatum(dict_name),
2240  ObjectIdGetDatum(namespaceId));
2241  }
2242  else
2243  {
2244  /* search for it in search path */
2246 
2247  foreach(l, activeSearchPath)
2248  {
2249  namespaceId = lfirst_oid(l);
2250 
2251  if (namespaceId == myTempNamespace)
2252  continue; /* do not look in temp namespace */
2253 
2254  dictoid = GetSysCacheOid2(TSDICTNAMENSP,
2255  PointerGetDatum(dict_name),
2256  ObjectIdGetDatum(namespaceId));
2257  if (OidIsValid(dictoid))
2258  break;
2259  }
2260  }
2261 
2262  if (!OidIsValid(dictoid) && !missing_ok)
2263  ereport(ERROR,
2264  (errcode(ERRCODE_UNDEFINED_OBJECT),
2265  errmsg("text search dictionary \"%s\" does not exist",
2266  NameListToString(names))));
2267 
2268  return dictoid;
2269 }
2270 
2271 /*
2272  * TSDictionaryIsVisible
2273  * Determine whether a dictionary (identified by OID) is visible in the
2274  * current search path. Visible means "would be found by searching
2275  * for the unqualified dictionary name".
2276  */
2277 bool
2279 {
2280  HeapTuple tup;
2281  Form_pg_ts_dict form;
2282  Oid namespace;
2283  bool visible;
2284 
2285  tup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
2286  if (!HeapTupleIsValid(tup))
2287  elog(ERROR, "cache lookup failed for text search dictionary %u",
2288  dictId);
2289  form = (Form_pg_ts_dict) GETSTRUCT(tup);
2290 
2292 
2293  /*
2294  * Quick check: if it ain't in the path at all, it ain't visible. Items in
2295  * the system namespace are surely in the path and so we needn't even do
2296  * list_member_oid() for them.
2297  */
2298  namespace = form->dictnamespace;
2299  if (namespace != PG_CATALOG_NAMESPACE &&
2300  !list_member_oid(activeSearchPath, namespace))
2301  visible = false;
2302  else
2303  {
2304  /*
2305  * If it is in the path, it might still not be visible; it could be
2306  * hidden by another dictionary of the same name earlier in the path.
2307  * So we must do a slow check for conflicting dictionaries.
2308  */
2309  char *name = NameStr(form->dictname);
2310  ListCell *l;
2311 
2312  visible = false;
2313  foreach(l, activeSearchPath)
2314  {
2315  Oid namespaceId = lfirst_oid(l);
2316 
2317  if (namespaceId == myTempNamespace)
2318  continue; /* do not look in temp namespace */
2319 
2320  if (namespaceId == namespace)
2321  {
2322  /* Found it first in path */
2323  visible = true;
2324  break;
2325  }
2327  PointerGetDatum(name),
2328  ObjectIdGetDatum(namespaceId)))
2329  {
2330  /* Found something else first in path */
2331  break;
2332  }
2333  }
2334  }
2335 
2336  ReleaseSysCache(tup);
2337 
2338  return visible;
2339 }
2340 
2341 /*
2342  * get_ts_template_oid - find a TS template by possibly qualified name
2343  *
2344  * If not found, returns InvalidOid if missing_ok, else throws error
2345  */
2346 Oid
2347 get_ts_template_oid(List *names, bool missing_ok)
2348 {
2349  char *schemaname;
2350  char *template_name;
2351  Oid namespaceId;
2352  Oid tmploid = InvalidOid;
2353  ListCell *l;
2354 
2355  /* deconstruct the name list */
2356  DeconstructQualifiedName(names, &schemaname, &template_name);
2357 
2358  if (schemaname)
2359  {
2360  /* use exact schema given */
2361  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2362  if (missing_ok && !OidIsValid(namespaceId))
2363  tmploid = InvalidOid;
2364  else
2366  PointerGetDatum(template_name),
2367  ObjectIdGetDatum(namespaceId));
2368  }
2369  else
2370  {
2371  /* search for it in search path */
2373 
2374  foreach(l, activeSearchPath)
2375  {
2376  namespaceId = lfirst_oid(l);
2377 
2378  if (namespaceId == myTempNamespace)
2379  continue; /* do not look in temp namespace */
2380 
2382  PointerGetDatum(template_name),
2383  ObjectIdGetDatum(namespaceId));
2384  if (OidIsValid(tmploid))
2385  break;
2386  }
2387  }
2388 
2389  if (!OidIsValid(tmploid) && !missing_ok)
2390  ereport(ERROR,
2391  (errcode(ERRCODE_UNDEFINED_OBJECT),
2392  errmsg("text search template \"%s\" does not exist",
2393  NameListToString(names))));
2394 
2395  return tmploid;
2396 }
2397 
2398 /*
2399  * TSTemplateIsVisible
2400  * Determine whether a template (identified by OID) is visible in the
2401  * current search path. Visible means "would be found by searching
2402  * for the unqualified template name".
2403  */
2404 bool
2406 {
2407  HeapTuple tup;
2408  Form_pg_ts_template form;
2409  Oid namespace;
2410  bool visible;
2411 
2413  if (!HeapTupleIsValid(tup))
2414  elog(ERROR, "cache lookup failed for text search template %u", tmplId);
2415  form = (Form_pg_ts_template) GETSTRUCT(tup);
2416 
2418 
2419  /*
2420  * Quick check: if it ain't in the path at all, it ain't visible. Items in
2421  * the system namespace are surely in the path and so we needn't even do
2422  * list_member_oid() for them.
2423  */
2424  namespace = form->tmplnamespace;
2425  if (namespace != PG_CATALOG_NAMESPACE &&
2426  !list_member_oid(activeSearchPath, namespace))
2427  visible = false;
2428  else
2429  {
2430  /*
2431  * If it is in the path, it might still not be visible; it could be
2432  * hidden by another template of the same name earlier in the path. So
2433  * we must do a slow check for conflicting templates.
2434  */
2435  char *name = NameStr(form->tmplname);
2436  ListCell *l;
2437 
2438  visible = false;
2439  foreach(l, activeSearchPath)
2440  {
2441  Oid namespaceId = lfirst_oid(l);
2442 
2443  if (namespaceId == myTempNamespace)
2444  continue; /* do not look in temp namespace */
2445 
2446  if (namespaceId == namespace)
2447  {
2448  /* Found it first in path */
2449  visible = true;
2450  break;
2451  }
2453  PointerGetDatum(name),
2454  ObjectIdGetDatum(namespaceId)))
2455  {
2456  /* Found something else first in path */
2457  break;
2458  }
2459  }
2460  }
2461 
2462  ReleaseSysCache(tup);
2463 
2464  return visible;
2465 }
2466 
2467 /*
2468  * get_ts_config_oid - find a TS config by possibly qualified name
2469  *
2470  * If not found, returns InvalidOid if missing_ok, else throws error
2471  */
2472 Oid
2473 get_ts_config_oid(List *names, bool missing_ok)
2474 {
2475  char *schemaname;
2476  char *config_name;
2477  Oid namespaceId;
2478  Oid cfgoid = InvalidOid;
2479  ListCell *l;
2480 
2481  /* deconstruct the name list */
2482  DeconstructQualifiedName(names, &schemaname, &config_name);
2483 
2484  if (schemaname)
2485  {
2486  /* use exact schema given */
2487  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2488  if (missing_ok && !OidIsValid(namespaceId))
2489  cfgoid = InvalidOid;
2490  else
2492  PointerGetDatum(config_name),
2493  ObjectIdGetDatum(namespaceId));
2494  }
2495  else
2496  {
2497  /* search for it in search path */
2499 
2500  foreach(l, activeSearchPath)
2501  {
2502  namespaceId = lfirst_oid(l);
2503 
2504  if (namespaceId == myTempNamespace)
2505  continue; /* do not look in temp namespace */
2506 
2508  PointerGetDatum(config_name),
2509  ObjectIdGetDatum(namespaceId));
2510  if (OidIsValid(cfgoid))
2511  break;
2512  }
2513  }
2514 
2515  if (!OidIsValid(cfgoid) && !missing_ok)
2516  ereport(ERROR,
2517  (errcode(ERRCODE_UNDEFINED_OBJECT),
2518  errmsg("text search configuration \"%s\" does not exist",
2519  NameListToString(names))));
2520 
2521  return cfgoid;
2522 }
2523 
2524 /*
2525  * TSConfigIsVisible
2526  * Determine whether a text search configuration (identified by OID)
2527  * is visible in the current search path. Visible means "would be found
2528  * by searching for the unqualified text search configuration name".
2529  */
2530 bool
2532 {
2533  HeapTuple tup;
2534  Form_pg_ts_config form;
2535  Oid namespace;
2536  bool visible;
2537 
2539  if (!HeapTupleIsValid(tup))
2540  elog(ERROR, "cache lookup failed for text search configuration %u",
2541  cfgid);
2542  form = (Form_pg_ts_config) GETSTRUCT(tup);
2543 
2545 
2546  /*
2547  * Quick check: if it ain't in the path at all, it ain't visible. Items in
2548  * the system namespace are surely in the path and so we needn't even do
2549  * list_member_oid() for them.
2550  */
2551  namespace = form->cfgnamespace;
2552  if (namespace != PG_CATALOG_NAMESPACE &&
2553  !list_member_oid(activeSearchPath, namespace))
2554  visible = false;
2555  else
2556  {
2557  /*
2558  * If it is in the path, it might still not be visible; it could be
2559  * hidden by another configuration of the same name earlier in the
2560  * path. So we must do a slow check for conflicting configurations.
2561  */
2562  char *name = NameStr(form->cfgname);
2563  ListCell *l;
2564 
2565  visible = false;
2566  foreach(l, activeSearchPath)
2567  {
2568  Oid namespaceId = lfirst_oid(l);
2569 
2570  if (namespaceId == myTempNamespace)
2571  continue; /* do not look in temp namespace */
2572 
2573  if (namespaceId == namespace)
2574  {
2575  /* Found it first in path */
2576  visible = true;
2577  break;
2578  }
2580  PointerGetDatum(name),
2581  ObjectIdGetDatum(namespaceId)))
2582  {
2583  /* Found something else first in path */
2584  break;
2585  }
2586  }
2587  }
2588 
2589  ReleaseSysCache(tup);
2590 
2591  return visible;
2592 }
2593 
2594 
2595 /*
2596  * DeconstructQualifiedName
2597  * Given a possibly-qualified name expressed as a list of String nodes,
2598  * extract the schema name and object name.
2599  *
2600  * *nspname_p is set to NULL if there is no explicit schema name.
2601  */
2602 void
2604  char **nspname_p,
2605  char **objname_p)
2606 {
2607  char *catalogname;
2608  char *schemaname = NULL;
2609  char *objname = NULL;
2610 
2611  switch (list_length(names))
2612  {
2613  case 1:
2614  objname = strVal(linitial(names));
2615  break;
2616  case 2:
2617  schemaname = strVal(linitial(names));
2618  objname = strVal(lsecond(names));
2619  break;
2620  case 3:
2621  catalogname = strVal(linitial(names));
2622  schemaname = strVal(lsecond(names));
2623  objname = strVal(lthird(names));
2624 
2625  /*
2626  * We check the catalog name and then ignore it.
2627  */
2628  if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
2629  ereport(ERROR,
2630  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2631  errmsg("cross-database references are not implemented: %s",
2632  NameListToString(names))));
2633  break;
2634  default:
2635  ereport(ERROR,
2636  (errcode(ERRCODE_SYNTAX_ERROR),
2637  errmsg("improper qualified name (too many dotted names): %s",
2638  NameListToString(names))));
2639  break;
2640  }
2641 
2642  *nspname_p = schemaname;
2643  *objname_p = objname;
2644 }
2645 
2646 /*
2647  * LookupNamespaceNoError
2648  * Look up a schema name.
2649  *
2650  * Returns the namespace OID, or InvalidOid if not found.
2651  *
2652  * Note this does NOT perform any permissions check --- callers are
2653  * responsible for being sure that an appropriate check is made.
2654  * In the majority of cases LookupExplicitNamespace is preferable.
2655  */
2656 Oid
2657 LookupNamespaceNoError(const char *nspname)
2658 {
2659  /* check for pg_temp alias */
2660  if (strcmp(nspname, "pg_temp") == 0)
2661  {
2663  {
2665  return myTempNamespace;
2666  }
2667 
2668  /*
2669  * Since this is used only for looking up existing objects, there is
2670  * no point in trying to initialize the temp namespace here; and doing
2671  * so might create problems for some callers. Just report "not found".
2672  */
2673  return InvalidOid;
2674  }
2675 
2676  return get_namespace_oid(nspname, true);
2677 }
2678 
2679 /*
2680  * LookupExplicitNamespace
2681  * Process an explicitly-specified schema name: look up the schema
2682  * and verify we have USAGE (lookup) rights in it.
2683  *
2684  * Returns the namespace OID
2685  */
2686 Oid
2687 LookupExplicitNamespace(const char *nspname, bool missing_ok)
2688 {
2689  Oid namespaceId;
2690  AclResult aclresult;
2691 
2692  /* check for pg_temp alias */
2693  if (strcmp(nspname, "pg_temp") == 0)
2694  {
2696  return myTempNamespace;
2697 
2698  /*
2699  * Since this is used only for looking up existing objects, there is
2700  * no point in trying to initialize the temp namespace here; and doing
2701  * so might create problems for some callers --- just fall through.
2702  */
2703  }
2704 
2705  namespaceId = get_namespace_oid(nspname, missing_ok);
2706  if (missing_ok && !OidIsValid(namespaceId))
2707  return InvalidOid;
2708 
2709  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
2710  if (aclresult != ACLCHECK_OK)
2712  nspname);
2713  /* Schema search hook for this lookup */
2714  InvokeNamespaceSearchHook(namespaceId, true);
2715 
2716  return namespaceId;
2717 }
2718 
2719 /*
2720  * LookupCreationNamespace
2721  * Look up the schema and verify we have CREATE rights on it.
2722  *
2723  * This is just like LookupExplicitNamespace except for the different
2724  * permission check, and that we are willing to create pg_temp if needed.
2725  *
2726  * Note: calling this may result in a CommandCounterIncrement operation,
2727  * if we have to create or clean out the temp namespace.
2728  */
2729 Oid
2730 LookupCreationNamespace(const char *nspname)
2731 {
2732  Oid namespaceId;
2733  AclResult aclresult;
2734 
2735  /* check for pg_temp alias */
2736  if (strcmp(nspname, "pg_temp") == 0)
2737  {
2738  /* Initialize temp namespace if first time through */
2741  return myTempNamespace;
2742  }
2743 
2744  namespaceId = get_namespace_oid(nspname, false);
2745 
2746  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
2747  if (aclresult != ACLCHECK_OK)
2749  nspname);
2750 
2751  return namespaceId;
2752 }
2753 
2754 /*
2755  * Common checks on switching namespaces.
2756  *
2757  * We complain if either the old or new namespaces is a temporary schema
2758  * (or temporary toast schema), or if either the old or new namespaces is the
2759  * TOAST schema.
2760  */
2761 void
2762 CheckSetNamespace(Oid oldNspOid, Oid nspOid)
2763 {
2764  /* disallow renaming into or out of temp schemas */
2765  if (isAnyTempNamespace(nspOid) || isAnyTempNamespace(oldNspOid))
2766  ereport(ERROR,
2767  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2768  errmsg("cannot move objects into or out of temporary schemas")));
2769 
2770  /* same for TOAST schema */
2771  if (nspOid == PG_TOAST_NAMESPACE || oldNspOid == PG_TOAST_NAMESPACE)
2772  ereport(ERROR,
2773  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2774  errmsg("cannot move objects into or out of TOAST schema")));
2775 }
2776 
2777 /*
2778  * QualifiedNameGetCreationNamespace
2779  * Given a possibly-qualified name for an object (in List-of-Values
2780  * format), determine what namespace the object should be created in.
2781  * Also extract and return the object name (last component of list).
2782  *
2783  * Note: this does not apply any permissions check. Callers must check
2784  * for CREATE rights on the selected namespace when appropriate.
2785  *
2786  * Note: calling this may result in a CommandCounterIncrement operation,
2787  * if we have to create or clean out the temp namespace.
2788  */
2789 Oid
2790 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
2791 {
2792  char *schemaname;
2793  Oid namespaceId;
2794 
2795  /* deconstruct the name list */
2796  DeconstructQualifiedName(names, &schemaname, objname_p);
2797 
2798  if (schemaname)
2799  {
2800  /* check for pg_temp alias */
2801  if (strcmp(schemaname, "pg_temp") == 0)
2802  {
2803  /* Initialize temp namespace if first time through */
2806  return myTempNamespace;
2807  }
2808  /* use exact schema given */
2809  namespaceId = get_namespace_oid(schemaname, false);
2810  /* we do not check for USAGE rights here! */
2811  }
2812  else
2813  {
2814  /* use the default creation namespace */
2817  {
2818  /* Need to initialize temp namespace */
2820  return myTempNamespace;
2821  }
2822  namespaceId = activeCreationNamespace;
2823  if (!OidIsValid(namespaceId))
2824  ereport(ERROR,
2825  (errcode(ERRCODE_UNDEFINED_SCHEMA),
2826  errmsg("no schema has been selected to create in")));
2827  }
2828 
2829  return namespaceId;
2830 }
2831 
2832 /*
2833  * get_namespace_oid - given a namespace name, look up the OID
2834  *
2835  * If missing_ok is false, throw an error if namespace name not found. If
2836  * true, just return InvalidOid.
2837  */
2838 Oid
2839 get_namespace_oid(const char *nspname, bool missing_ok)
2840 {
2841  Oid oid;
2842 
2844  if (!OidIsValid(oid) && !missing_ok)
2845  ereport(ERROR,
2846  (errcode(ERRCODE_UNDEFINED_SCHEMA),
2847  errmsg("schema \"%s\" does not exist", nspname)));
2848 
2849  return oid;
2850 }
2851 
2852 /*
2853  * makeRangeVarFromNameList
2854  * Utility routine to convert a qualified-name list into RangeVar form.
2855  */
2856 RangeVar *
2858 {
2859  RangeVar *rel = makeRangeVar(NULL, NULL, -1);
2860 
2861  switch (list_length(names))
2862  {
2863  case 1:
2864  rel->relname = strVal(linitial(names));
2865  break;
2866  case 2:
2867  rel->schemaname = strVal(linitial(names));
2868  rel->relname = strVal(lsecond(names));
2869  break;
2870  case 3:
2871  rel->catalogname = strVal(linitial(names));
2872  rel->schemaname = strVal(lsecond(names));
2873  rel->relname = strVal(lthird(names));
2874  break;
2875  default:
2876  ereport(ERROR,
2877  (errcode(ERRCODE_SYNTAX_ERROR),
2878  errmsg("improper relation name (too many dotted names): %s",
2879  NameListToString(names))));
2880  break;
2881  }
2882 
2883  return rel;
2884 }
2885 
2886 /*
2887  * NameListToString
2888  * Utility routine to convert a qualified-name list into a string.
2889  *
2890  * This is used primarily to form error messages, and so we do not quote
2891  * the list elements, for the sake of legibility.
2892  *
2893  * In most scenarios the list elements should always be Value strings,
2894  * but we also allow A_Star for the convenience of ColumnRef processing.
2895  */
2896 char *
2898 {
2900  ListCell *l;
2901 
2902  initStringInfo(&string);
2903 
2904  foreach(l, names)
2905  {
2906  Node *name = (Node *) lfirst(l);
2907 
2908  if (l != list_head(names))
2909  appendStringInfoChar(&string, '.');
2910 
2911  if (IsA(name, String))
2912  appendStringInfoString(&string, strVal(name));
2913  else if (IsA(name, A_Star))
2914  appendStringInfoChar(&string, '*');
2915  else
2916  elog(ERROR, "unexpected node type in name list: %d",
2917  (int) nodeTag(name));
2918  }
2919 
2920  return string.data;
2921 }
2922 
2923 /*
2924  * NameListToQuotedString
2925  * Utility routine to convert a qualified-name list into a string.
2926  *
2927  * Same as above except that names will be double-quoted where necessary,
2928  * so the string could be re-parsed (eg, by textToQualifiedNameList).
2929  */
2930 char *
2932 {
2934  ListCell *l;
2935 
2936  initStringInfo(&string);
2937 
2938  foreach(l, names)
2939  {
2940  if (l != list_head(names))
2941  appendStringInfoChar(&string, '.');
2943  }
2944 
2945  return string.data;
2946 }
2947 
2948 /*
2949  * isTempNamespace - is the given namespace my temporary-table namespace?
2950  */
2951 bool
2952 isTempNamespace(Oid namespaceId)
2953 {
2954  if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
2955  return true;
2956  return false;
2957 }
2958 
2959 /*
2960  * isTempToastNamespace - is the given namespace my temporary-toast-table
2961  * namespace?
2962  */
2963 bool
2965 {
2966  if (OidIsValid(myTempToastNamespace) && myTempToastNamespace == namespaceId)
2967  return true;
2968  return false;
2969 }
2970 
2971 /*
2972  * isTempOrTempToastNamespace - is the given namespace my temporary-table
2973  * namespace or my temporary-toast-table namespace?
2974  */
2975 bool
2977 {
2978  if (OidIsValid(myTempNamespace) &&
2979  (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId))
2980  return true;
2981  return false;
2982 }
2983 
2984 /*
2985  * isAnyTempNamespace - is the given namespace a temporary-table namespace
2986  * (either my own, or another backend's)? Temporary-toast-table namespaces
2987  * are included, too.
2988  */
2989 bool
2991 {
2992  bool result;
2993  char *nspname;
2994 
2995  /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
2996  nspname = get_namespace_name(namespaceId);
2997  if (!nspname)
2998  return false; /* no such namespace? */
2999  result = (strncmp(nspname, "pg_temp_", 8) == 0) ||
3000  (strncmp(nspname, "pg_toast_temp_", 14) == 0);
3001  pfree(nspname);
3002  return result;
3003 }
3004 
3005 /*
3006  * isOtherTempNamespace - is the given namespace some other backend's
3007  * temporary-table namespace (including temporary-toast-table namespaces)?
3008  *
3009  * Note: for most purposes in the C code, this function is obsolete. Use
3010  * RELATION_IS_OTHER_TEMP() instead to detect non-local temp relations.
3011  */
3012 bool
3014 {
3015  /* If it's my own temp namespace, say "false" */
3016  if (isTempOrTempToastNamespace(namespaceId))
3017  return false;
3018  /* Else, if it's any temp namespace, say "true" */
3019  return isAnyTempNamespace(namespaceId);
3020 }
3021 
3022 /*
3023  * GetTempNamespaceBackendId - if the given namespace is a temporary-table
3024  * namespace (either my own, or another backend's), return the BackendId
3025  * that owns it. Temporary-toast-table namespaces are included, too.
3026  * If it isn't a temp namespace, return InvalidBackendId.
3027  */
3028 int
3030 {
3031  int result;
3032  char *nspname;
3033 
3034  /* See if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
3035  nspname = get_namespace_name(namespaceId);
3036  if (!nspname)
3037  return InvalidBackendId; /* no such namespace? */
3038  if (strncmp(nspname, "pg_temp_", 8) == 0)
3039  result = atoi(nspname + 8);
3040  else if (strncmp(nspname, "pg_toast_temp_", 14) == 0)
3041  result = atoi(nspname + 14);
3042  else
3043  result = InvalidBackendId;
3044  pfree(nspname);
3045  return result;
3046 }
3047 
3048 /*
3049  * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
3050  * which must already be assigned. (This is only used when creating a toast
3051  * table for a temp table, so we must have already done InitTempTableNamespace)
3052  */
3053 Oid
3055 {
3057  return myTempToastNamespace;
3058 }
3059 
3060 
3061 /*
3062  * GetTempNamespaceState - fetch status of session's temporary namespace
3063  *
3064  * This is used for conveying state to a parallel worker, and is not meant
3065  * for general-purpose access.
3066  */
3067 void
3068 GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
3069 {
3070  /* Return namespace OIDs, or 0 if session has not created temp namespace */
3071  *tempNamespaceId = myTempNamespace;
3072  *tempToastNamespaceId = myTempToastNamespace;
3073 }
3074 
3075 /*
3076  * SetTempNamespaceState - set status of session's temporary namespace
3077  *
3078  * This is used for conveying state to a parallel worker, and is not meant for
3079  * general-purpose access. By transferring these namespace OIDs to workers,
3080  * we ensure they will have the same notion of the search path as their leader
3081  * does.
3082  */
3083 void
3084 SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
3085 {
3086  /* Worker should not have created its own namespaces ... */
3090 
3091  /* Assign same namespace OIDs that leader has */
3092  myTempNamespace = tempNamespaceId;
3093  myTempToastNamespace = tempToastNamespaceId;
3094 
3095  /*
3096  * It's fine to leave myTempNamespaceSubID == InvalidSubTransactionId.
3097  * Even if the namespace is new so far as the leader is concerned, it's
3098  * not new to the worker, and we certainly wouldn't want the worker trying
3099  * to destroy it.
3100  */
3101 
3102  baseSearchPathValid = false; /* may need to rebuild list */
3103 }
3104 
3105 
3106 /*
3107  * GetOverrideSearchPath - fetch current search path definition in form
3108  * used by PushOverrideSearchPath.
3109  *
3110  * The result structure is allocated in the specified memory context
3111  * (which might or might not be equal to CurrentMemoryContext); but any
3112  * junk created by revalidation calculations will be in CurrentMemoryContext.
3113  */
3116 {
3117  OverrideSearchPath *result;
3118  List *schemas;
3119  MemoryContext oldcxt;
3120 
3122 
3123  oldcxt = MemoryContextSwitchTo(context);
3124 
3125  result = (OverrideSearchPath *) palloc0(sizeof(OverrideSearchPath));
3126  schemas = list_copy(activeSearchPath);
3127  while (schemas && linitial_oid(schemas) != activeCreationNamespace)
3128  {
3129  if (linitial_oid(schemas) == myTempNamespace)
3130  result->addTemp = true;
3131  else
3132  {
3134  result->addCatalog = true;
3135  }
3136  schemas = list_delete_first(schemas);
3137  }
3138  result->schemas = schemas;
3139 
3140  MemoryContextSwitchTo(oldcxt);
3141 
3142  return result;
3143 }
3144 
3145 /*
3146  * CopyOverrideSearchPath - copy the specified OverrideSearchPath.
3147  *
3148  * The result structure is allocated in CurrentMemoryContext.
3149  */
3152 {
3153  OverrideSearchPath *result;
3154 
3155  result = (OverrideSearchPath *) palloc(sizeof(OverrideSearchPath));
3156  result->schemas = list_copy(path->schemas);
3157  result->addCatalog = path->addCatalog;
3158  result->addTemp = path->addTemp;
3159 
3160  return result;
3161 }
3162 
3163 /*
3164  * OverrideSearchPathMatchesCurrent - does path match current setting?
3165  */
3166 bool
3168 {
3169  ListCell *lc,
3170  *lcp;
3171 
3173 
3174  /* We scan down the activeSearchPath to see if it matches the input. */
3175  lc = list_head(activeSearchPath);
3176 
3177  /* If path->addTemp, first item should be my temp namespace. */
3178  if (path->addTemp)
3179  {
3180  if (lc && lfirst_oid(lc) == myTempNamespace)
3181  lc = lnext(lc);
3182  else
3183  return false;
3184  }
3185  /* If path->addCatalog, next item should be pg_catalog. */
3186  if (path->addCatalog)
3187  {
3188  if (lc && lfirst_oid(lc) == PG_CATALOG_NAMESPACE)
3189  lc = lnext(lc);
3190  else
3191  return false;
3192  }
3193  /* We should now be looking at the activeCreationNamespace. */
3194  if (activeCreationNamespace != (lc ? lfirst_oid(lc) : InvalidOid))
3195  return false;
3196  /* The remainder of activeSearchPath should match path->schemas. */
3197  foreach(lcp, path->schemas)
3198  {
3199  if (lc && lfirst_oid(lc) == lfirst_oid(lcp))
3200  lc = lnext(lc);
3201  else
3202  return false;
3203  }
3204  if (lc)
3205  return false;
3206  return true;
3207 }
3208 
3209 /*
3210  * PushOverrideSearchPath - temporarily override the search path
3211  *
3212  * We allow nested overrides, hence the push/pop terminology. The GUC
3213  * search_path variable is ignored while an override is active.
3214  *
3215  * It's possible that newpath->useTemp is set but there is no longer any
3216  * active temp namespace, if the path was saved during a transaction that
3217  * created a temp namespace and was later rolled back. In that case we just
3218  * ignore useTemp. A plausible alternative would be to create a new temp
3219  * namespace, but for existing callers that's not necessary because an empty
3220  * temp namespace wouldn't affect their results anyway.
3221  *
3222  * It's also worth noting that other schemas listed in newpath might not
3223  * exist anymore either. We don't worry about this because OIDs that match
3224  * no existing namespace will simply not produce any hits during searches.
3225  */
3226 void
3228 {
3229  OverrideStackEntry *entry;
3230  List *oidlist;
3231  Oid firstNS;
3232  MemoryContext oldcxt;
3233 
3234  /*
3235  * Copy the list for safekeeping, and insert implicitly-searched
3236  * namespaces as needed. This code should track recomputeNamespacePath.
3237  */
3239 
3240  oidlist = list_copy(newpath->schemas);
3241 
3242  /*
3243  * Remember the first member of the explicit list.
3244  */
3245  if (oidlist == NIL)
3246  firstNS = InvalidOid;
3247  else
3248  firstNS = linitial_oid(oidlist);
3249 
3250  /*
3251  * Add any implicitly-searched namespaces to the list. Note these go on
3252  * the front, not the back; also notice that we do not check USAGE
3253  * permissions for these.
3254  */
3255  if (newpath->addCatalog)
3256  oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
3257 
3258  if (newpath->addTemp && OidIsValid(myTempNamespace))
3259  oidlist = lcons_oid(myTempNamespace, oidlist);
3260 
3261  /*
3262  * Build the new stack entry, then insert it at the head of the list.
3263  */
3264  entry = (OverrideStackEntry *) palloc(sizeof(OverrideStackEntry));
3265  entry->searchPath = oidlist;
3266  entry->creationNamespace = firstNS;
3268 
3269  overrideStack = lcons(entry, overrideStack);
3270 
3271  /* And make it active. */
3272  activeSearchPath = entry->searchPath;
3274  activeTempCreationPending = false; /* XXX is this OK? */
3275 
3276  MemoryContextSwitchTo(oldcxt);
3277 }
3278 
3279 /*
3280  * PopOverrideSearchPath - undo a previous PushOverrideSearchPath
3281  *
3282  * Any push during a (sub)transaction will be popped automatically at abort.
3283  * But it's caller error if a push isn't popped in normal control flow.
3284  */
3285 void
3287 {
3288  OverrideStackEntry *entry;
3289 
3290  /* Sanity checks. */
3291  if (overrideStack == NIL)
3292  elog(ERROR, "bogus PopOverrideSearchPath call");
3293  entry = (OverrideStackEntry *) linitial(overrideStack);
3294  if (entry->nestLevel != GetCurrentTransactionNestLevel())
3295  elog(ERROR, "bogus PopOverrideSearchPath call");
3296 
3297  /* Pop the stack and free storage. */
3298  overrideStack = list_delete_first(overrideStack);
3299  list_free(entry->searchPath);
3300  pfree(entry);
3301 
3302  /* Activate the next level down. */
3303  if (overrideStack)
3304  {
3305  entry = (OverrideStackEntry *) linitial(overrideStack);
3306  activeSearchPath = entry->searchPath;
3308  activeTempCreationPending = false; /* XXX is this OK? */
3309  }
3310  else
3311  {
3312  /* If not baseSearchPathValid, this is useless but harmless */
3313  activeSearchPath = baseSearchPath;
3316  }
3317 }
3318 
3319 
3320 /*
3321  * get_collation_oid - find a collation by possibly qualified name
3322  */
3323 Oid
3324 get_collation_oid(List *name, bool missing_ok)
3325 {
3326  char *schemaname;
3327  char *collation_name;
3328  int32 dbencoding = GetDatabaseEncoding();
3329  Oid namespaceId;
3330  Oid colloid;
3331  ListCell *l;
3332 
3333  /* deconstruct the name list */
3334  DeconstructQualifiedName(name, &schemaname, &collation_name);
3335 
3336  if (schemaname)
3337  {
3338  /* use exact schema given */
3339  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3340  if (missing_ok && !OidIsValid(namespaceId))
3341  return InvalidOid;
3342 
3343  /* first try for encoding-specific entry, then any-encoding */
3344  colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3345  PointerGetDatum(collation_name),
3346  Int32GetDatum(dbencoding),
3347  ObjectIdGetDatum(namespaceId));
3348  if (OidIsValid(colloid))
3349  return colloid;
3350  colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3351  PointerGetDatum(collation_name),
3352  Int32GetDatum(-1),
3353  ObjectIdGetDatum(namespaceId));
3354  if (OidIsValid(colloid))
3355  return colloid;
3356  }
3357  else
3358  {
3359  /* search for it in search path */
3361 
3362  foreach(l, activeSearchPath)
3363  {
3364  namespaceId = lfirst_oid(l);
3365 
3366  if (namespaceId == myTempNamespace)
3367  continue; /* do not look in temp namespace */
3368 
3369  colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3370  PointerGetDatum(collation_name),
3371  Int32GetDatum(dbencoding),
3372  ObjectIdGetDatum(namespaceId));
3373  if (OidIsValid(colloid))
3374  return colloid;
3375  colloid = GetSysCacheOid3(COLLNAMEENCNSP,
3376  PointerGetDatum(collation_name),
3377  Int32GetDatum(-1),
3378  ObjectIdGetDatum(namespaceId));
3379  if (OidIsValid(colloid))
3380  return colloid;
3381  }
3382  }
3383 
3384  /* Not found in path */
3385  if (!missing_ok)
3386  ereport(ERROR,
3387  (errcode(ERRCODE_UNDEFINED_OBJECT),
3388  errmsg("collation \"%s\" for encoding \"%s\" does not exist",
3390  return InvalidOid;
3391 }
3392 
3393 /*
3394  * get_conversion_oid - find a conversion by possibly qualified name
3395  */
3396 Oid
3397 get_conversion_oid(List *name, bool missing_ok)
3398 {
3399  char *schemaname;
3400  char *conversion_name;
3401  Oid namespaceId;
3402  Oid conoid = InvalidOid;
3403  ListCell *l;
3404 
3405  /* deconstruct the name list */
3406  DeconstructQualifiedName(name, &schemaname, &conversion_name);
3407 
3408  if (schemaname)
3409  {
3410  /* use exact schema given */
3411  namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3412  if (missing_ok && !OidIsValid(namespaceId))
3413  conoid = InvalidOid;
3414  else
3415  conoid = GetSysCacheOid2(CONNAMENSP,
3416  PointerGetDatum(conversion_name),
3417  ObjectIdGetDatum(namespaceId));
3418  }
3419  else
3420  {
3421  /* search for it in search path */
3423 
3424  foreach(l, activeSearchPath)
3425  {
3426  namespaceId = lfirst_oid(l);
3427 
3428  if (namespaceId == myTempNamespace)
3429  continue; /* do not look in temp namespace */
3430 
3431  conoid = GetSysCacheOid2(CONNAMENSP,
3432  PointerGetDatum(conversion_name),
3433  ObjectIdGetDatum(namespaceId));
3434  if (OidIsValid(conoid))
3435  return conoid;
3436  }
3437  }
3438 
3439  /* Not found in path */
3440  if (!OidIsValid(conoid) && !missing_ok)
3441  ereport(ERROR,
3442  (errcode(ERRCODE_UNDEFINED_OBJECT),
3443  errmsg("conversion \"%s\" does not exist",
3444  NameListToString(name))));
3445  return conoid;
3446 }
3447 
3448 /*
3449  * FindDefaultConversionProc - find default encoding conversion proc
3450  */
3451 Oid
3452 FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
3453 {
3454  Oid proc;
3455  ListCell *l;
3456 
3458 
3459  foreach(l, activeSearchPath)
3460  {
3461  Oid namespaceId = lfirst_oid(l);
3462 
3463  if (namespaceId == myTempNamespace)
3464  continue; /* do not look in temp namespace */
3465 
3466  proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
3467  if (OidIsValid(proc))
3468  return proc;
3469  }
3470 
3471  /* Not found in path */
3472  return InvalidOid;
3473 }
3474 
3475 /*
3476  * recomputeNamespacePath - recompute path derived variables if needed.
3477  */
3478 static void
3480 {
3481  Oid roleid = GetUserId();
3482  char *rawname;
3483  List *namelist;
3484  List *oidlist;
3485  List *newpath;
3486  ListCell *l;
3487  bool temp_missing;
3488  Oid firstNS;
3489  MemoryContext oldcxt;
3490 
3491  /* Do nothing if an override search spec is active. */
3492  if (overrideStack)
3493  return;
3494 
3495  /* Do nothing if path is already valid. */
3496  if (baseSearchPathValid && namespaceUser == roleid)
3497  return;
3498 
3499  /* Need a modifiable copy of namespace_search_path string */
3500  rawname = pstrdup(namespace_search_path);
3501 
3502  /* Parse string into list of identifiers */
3503  if (!SplitIdentifierString(rawname, ',', &namelist))
3504  {
3505  /* syntax error in name list */
3506  /* this should not happen if GUC checked check_search_path */
3507  elog(ERROR, "invalid list syntax");
3508  }
3509 
3510  /*
3511  * Convert the list of names to a list of OIDs. If any names are not
3512  * recognizable or we don't have read access, just leave them out of the
3513  * list. (We can't raise an error, since the search_path setting has
3514  * already been accepted.) Don't make duplicate entries, either.
3515  */
3516  oidlist = NIL;
3517  temp_missing = false;
3518  foreach(l, namelist)
3519  {
3520  char *curname = (char *) lfirst(l);
3521  Oid namespaceId;
3522 
3523  if (strcmp(curname, "$user") == 0)
3524  {
3525  /* $user --- substitute namespace matching user name, if any */
3526  HeapTuple tuple;
3527 
3528  tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
3529  if (HeapTupleIsValid(tuple))
3530  {
3531  char *rname;
3532 
3533  rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
3534  namespaceId = get_namespace_oid(rname, true);
3535  ReleaseSysCache(tuple);
3536  if (OidIsValid(namespaceId) &&
3537  !list_member_oid(oidlist, namespaceId) &&
3538  pg_namespace_aclcheck(namespaceId, roleid,
3539  ACL_USAGE) == ACLCHECK_OK &&
3540  InvokeNamespaceSearchHook(namespaceId, false))
3541  oidlist = lappend_oid(oidlist, namespaceId);
3542  }
3543  }
3544  else if (strcmp(curname, "pg_temp") == 0)
3545  {
3546  /* pg_temp --- substitute temp namespace, if any */
3548  {
3549  if (!list_member_oid(oidlist, myTempNamespace) &&
3551  oidlist = lappend_oid(oidlist, myTempNamespace);
3552  }
3553  else
3554  {
3555  /* If it ought to be the creation namespace, set flag */
3556  if (oidlist == NIL)
3557  temp_missing = true;
3558  }
3559  }
3560  else
3561  {
3562  /* normal namespace reference */
3563  namespaceId = get_namespace_oid(curname, true);
3564  if (OidIsValid(namespaceId) &&
3565  !list_member_oid(oidlist, namespaceId) &&
3566  pg_namespace_aclcheck(namespaceId, roleid,
3567  ACL_USAGE) == ACLCHECK_OK &&
3568  InvokeNamespaceSearchHook(namespaceId, false))
3569  oidlist = lappend_oid(oidlist, namespaceId);
3570  }
3571  }
3572 
3573  /*
3574  * Remember the first member of the explicit list. (Note: this is
3575  * nominally wrong if temp_missing, but we need it anyway to distinguish
3576  * explicit from implicit mention of pg_catalog.)
3577  */
3578  if (oidlist == NIL)
3579  firstNS = InvalidOid;
3580  else
3581  firstNS = linitial_oid(oidlist);
3582 
3583  /*
3584  * Add any implicitly-searched namespaces to the list. Note these go on
3585  * the front, not the back; also notice that we do not check USAGE
3586  * permissions for these.
3587  */
3588  if (!list_member_oid(oidlist, PG_CATALOG_NAMESPACE))
3589  oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist);
3590 
3591  if (OidIsValid(myTempNamespace) &&
3592  !list_member_oid(oidlist, myTempNamespace))
3593  oidlist = lcons_oid(myTempNamespace, oidlist);
3594 
3595  /*
3596  * Now that we've successfully built the new list of namespace OIDs, save
3597  * it in permanent storage.
3598  */
3600  newpath = list_copy(oidlist);
3601  MemoryContextSwitchTo(oldcxt);
3602 
3603  /* Now safe to assign to state variables. */
3604  list_free(baseSearchPath);
3605  baseSearchPath = newpath;
3606  baseCreationNamespace = firstNS;
3607  baseTempCreationPending = temp_missing;
3608 
3609  /* Mark the path valid. */
3610  baseSearchPathValid = true;
3611  namespaceUser = roleid;
3612 
3613  /* And make it active. */
3614  activeSearchPath = baseSearchPath;
3617 
3618  /* Clean up. */
3619  pfree(rawname);
3620  list_free(namelist);
3621  list_free(oidlist);
3622 }
3623 
3624 /*
3625  * InitTempTableNamespace
3626  * Initialize temp table namespace on first use in a particular backend
3627  */
3628 static void
3630 {
3631  char namespaceName[NAMEDATALEN];
3632  Oid namespaceId;
3633  Oid toastspaceId;
3634 
3636 
3637  /*
3638  * First, do permission check to see if we are authorized to make temp
3639  * tables. We use a nonstandard error message here since "databasename:
3640  * permission denied" might be a tad cryptic.
3641  *
3642  * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
3643  * that's necessary since current user ID could change during the session.
3644  * But there's no need to make the namespace in the first place until a
3645  * temp table creation request is made by someone with appropriate rights.
3646  */
3649  ereport(ERROR,
3650  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3651  errmsg("permission denied to create temporary tables in database \"%s\"",
3653 
3654  /*
3655  * Do not allow a Hot Standby slave session to make temp tables. Aside
3656  * from problems with modifying the system catalogs, there is a naming
3657  * conflict: pg_temp_N belongs to the session with BackendId N on the
3658  * master, not to a slave session with the same BackendId. We should not
3659  * be able to get here anyway due to XactReadOnly checks, but let's just
3660  * make real sure. Note that this also backstops various operations that
3661  * allow XactReadOnly transactions to modify temp tables; they'd need
3662  * RecoveryInProgress checks if not for this.
3663  */
3664  if (RecoveryInProgress())
3665  ereport(ERROR,
3666  (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
3667  errmsg("cannot create temporary tables during recovery")));
3668 
3669  /* Parallel workers can't create temporary tables, either. */
3670  if (IsParallelWorker())
3671  ereport(ERROR,
3672  (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
3673  errmsg("cannot create temporary tables in parallel mode")));
3674 
3675  snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
3676 
3677  namespaceId = get_namespace_oid(namespaceName, true);
3678  if (!OidIsValid(namespaceId))
3679  {
3680  /*
3681  * First use of this temp namespace in this database; create it. The
3682  * temp namespaces are always owned by the superuser. We leave their
3683  * permissions at default --- i.e., no access except to superuser ---
3684  * to ensure that unprivileged users can't peek at other backends'
3685  * temp tables. This works because the places that access the temp
3686  * namespace for my own backend skip permissions checks on it.
3687  */
3688  namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
3689  true);
3690  /* Advance command counter to make namespace visible */
3692  }
3693  else
3694  {
3695  /*
3696  * If the namespace already exists, clean it out (in case the former
3697  * owner crashed without doing so).
3698  */
3699  RemoveTempRelations(namespaceId);
3700  }
3701 
3702  /*
3703  * If the corresponding toast-table namespace doesn't exist yet, create
3704  * it. (We assume there is no need to clean it out if it does exist, since
3705  * dropping a parent table should make its toast table go away.)
3706  */
3707  snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
3708  MyBackendId);
3709 
3710  toastspaceId = get_namespace_oid(namespaceName, true);
3711  if (!OidIsValid(toastspaceId))
3712  {
3713  toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
3714  true);
3715  /* Advance command counter to make namespace visible */
3717  }
3718 
3719  /*
3720  * Okay, we've prepared the temp namespace ... but it's not committed yet,
3721  * so all our work could be undone by transaction rollback. Set flag for
3722  * AtEOXact_Namespace to know what to do.
3723  */
3724  myTempNamespace = namespaceId;
3725  myTempToastNamespace = toastspaceId;
3726 
3727  /* It should not be done already. */
3730 
3731  baseSearchPathValid = false; /* need to rebuild list */
3732 }
3733 
3734 /*
3735  * End-of-transaction cleanup for namespaces.
3736  */
3737 void
3738 AtEOXact_Namespace(bool isCommit, bool parallel)
3739 {
3740  /*
3741  * If we abort the transaction in which a temp namespace was selected,
3742  * we'll have to do any creation or cleanout work over again. So, just
3743  * forget the namespace entirely until next time. On the other hand, if
3744  * we commit then register an exit callback to clean out the temp tables
3745  * at backend shutdown. (We only want to register the callback once per
3746  * session, so this is a good place to do it.)
3747  */
3748  if (myTempNamespaceSubID != InvalidSubTransactionId && !parallel)
3749  {
3750  if (isCommit)
3752  else
3753  {
3756  baseSearchPathValid = false; /* need to rebuild list */
3757  }
3759  }
3760 
3761  /*
3762  * Clean up if someone failed to do PopOverrideSearchPath
3763  */
3764  if (overrideStack)
3765  {
3766  if (isCommit)
3767  elog(WARNING, "leaked override search path");
3768  while (overrideStack)
3769  {
3770  OverrideStackEntry *entry;
3771 
3772  entry = (OverrideStackEntry *) linitial(overrideStack);
3773  overrideStack = list_delete_first(overrideStack);
3774  list_free(entry->searchPath);
3775  pfree(entry);
3776  }
3777  /* If not baseSearchPathValid, this is useless but harmless */
3778  activeSearchPath = baseSearchPath;
3781  }
3782 }
3783 
3784 /*
3785  * AtEOSubXact_Namespace
3786  *
3787  * At subtransaction commit, propagate the temp-namespace-creation
3788  * flag to the parent subtransaction.
3789  *
3790  * At subtransaction abort, forget the flag if we set it up.
3791  */
3792 void
3794  SubTransactionId parentSubid)
3795 {
3796  OverrideStackEntry *entry;
3797 
3798  if (myTempNamespaceSubID == mySubid)
3799  {
3800  if (isCommit)
3801  myTempNamespaceSubID = parentSubid;
3802  else
3803  {
3805  /* TEMP namespace creation failed, so reset state */
3808  baseSearchPathValid = false; /* need to rebuild list */
3809  }
3810  }
3811 
3812  /*
3813  * Clean up if someone failed to do PopOverrideSearchPath
3814  */
3815  while (overrideStack)
3816  {
3817  entry = (OverrideStackEntry *) linitial(overrideStack);
3819  break;
3820  if (isCommit)
3821  elog(WARNING, "leaked override search path");
3822  overrideStack = list_delete_first(overrideStack);
3823  list_free(entry->searchPath);
3824  pfree(entry);
3825  }
3826 
3827  /* Activate the next level down. */
3828  if (overrideStack)
3829  {
3830  entry = (OverrideStackEntry *) linitial(overrideStack);
3831  activeSearchPath = entry->searchPath;
3833  activeTempCreationPending = false; /* XXX is this OK? */
3834  }
3835  else
3836  {
3837  /* If not baseSearchPathValid, this is useless but harmless */
3838  activeSearchPath = baseSearchPath;
3841  }
3842 }
3843 
3844 /*
3845  * Remove all relations in the specified temp namespace.
3846  *
3847  * This is called at backend shutdown (if we made any temp relations).
3848  * It is also called when we begin using a pre-existing temp namespace,
3849  * in order to clean out any relations that might have been created by
3850  * a crashed backend.
3851  */
3852 static void
3853 RemoveTempRelations(Oid tempNamespaceId)
3854 {
3855  ObjectAddress object;
3856 
3857  /*
3858  * We want to get rid of everything in the target namespace, but not the
3859  * namespace itself (deleting it only to recreate it later would be a
3860  * waste of cycles). Hence, specify SKIP_ORIGINAL. It's also an INTERNAL
3861  * deletion, and we want to not drop any extensions that might happen to
3862  * own temp objects.
3863  */
3864  object.classId = NamespaceRelationId;
3865  object.objectId = tempNamespaceId;
3866  object.objectSubId = 0;
3867 
3868  performDeletion(&object, DROP_CASCADE,
3873 }
3874 
3875 /*
3876  * Callback to remove temp relations at backend exit.
3877  */
3878 static void
3880 {
3881  if (OidIsValid(myTempNamespace)) /* should always be true */
3882  {
3883  /* Need to ensure we have a usable transaction. */
3886 
3888 
3890  }
3891 }
3892 
3893 /*
3894  * Remove all temp tables from the temporary namespace.
3895  */
3896 void
3898 {
3901 }
3902 
3903 
3904 /*
3905  * Routines for handling the GUC variable 'search_path'.
3906  */
3907 
3908 /* check_hook: validate new search_path value */
3909 bool
3910 check_search_path(char **newval, void **extra, GucSource source)
3911 {
3912  char *rawname;
3913  List *namelist;
3914 
3915  /* Need a modifiable copy of string */
3916  rawname = pstrdup(*newval);
3917 
3918  /* Parse string into list of identifiers */
3919  if (!SplitIdentifierString(rawname, ',', &namelist))
3920  {
3921  /* syntax error in name list */
3922  GUC_check_errdetail("List syntax is invalid.");
3923  pfree(rawname);
3924  list_free(namelist);
3925  return false;
3926  }
3927 
3928  /*
3929  * We used to try to check that the named schemas exist, but there are
3930  * many valid use-cases for having search_path settings that include
3931  * schemas that don't exist; and often, we are not inside a transaction
3932  * here and so can't consult the system catalogs anyway. So now, the only
3933  * requirement is syntactic validity of the identifier list.
3934  */
3935 
3936  pfree(rawname);
3937  list_free(namelist);
3938 
3939  return true;
3940 }
3941 
3942 /* assign_hook: do extra actions as needed */
3943 void
3944 assign_search_path(const char *newval, void *extra)
3945 {
3946  /*
3947  * We mark the path as needing recomputation, but don't do anything until
3948  * it's needed. This avoids trying to do database access during GUC
3949  * initialization, or outside a transaction.
3950  */
3951  baseSearchPathValid = false;
3952 }
3953 
3954 /*
3955  * InitializeSearchPath: initialize module during InitPostgres.
3956  *
3957  * This is called after we are up enough to be able to do catalog lookups.
3958  */
3959 void
3961 {
3963  {
3964  /*
3965  * In bootstrap mode, the search path must be 'pg_catalog' so that
3966  * tables are created in the proper namespace; ignore the GUC setting.
3967  */
3968  MemoryContext oldcxt;
3969 
3971  baseSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
3972  MemoryContextSwitchTo(oldcxt);
3974  baseTempCreationPending = false;
3975  baseSearchPathValid = true;
3977  activeSearchPath = baseSearchPath;
3980  }
3981  else
3982  {
3983  /*
3984  * In normal mode, arrange for a callback on any syscache invalidation
3985  * of pg_namespace rows.
3986  */
3989  (Datum) 0);
3990  /* Force search path to be recomputed on next use */
3991  baseSearchPathValid = false;
3992  }
3993 }
3994 
3995 /*
3996  * NamespaceCallback
3997  * Syscache inval callback function
3998  */
3999 static void
4000 NamespaceCallback(Datum arg, int cacheid, uint32 hashvalue)
4001 {
4002  /* Force search path to be recomputed on next use */
4003  baseSearchPathValid = false;
4004 }
4005 
4006 /*
4007  * Fetch the active search path. The return value is a palloc'ed list
4008  * of OIDs; the caller is responsible for freeing this storage as
4009  * appropriate.
4010  *
4011  * The returned list includes the implicitly-prepended namespaces only if
4012  * includeImplicit is true.
4013  *
4014  * Note: calling this may result in a CommandCounterIncrement operation,
4015  * if we have to create or clean out the temp namespace.
4016  */
4017 List *
4018 fetch_search_path(bool includeImplicit)
4019 {
4020  List *result;
4021 
4023 
4024  /*
4025  * If the temp namespace should be first, force it to exist. This is so
4026  * that callers can trust the result to reflect the actual default
4027  * creation namespace. It's a bit bogus to do this here, since
4028  * current_schema() is supposedly a stable function without side-effects,
4029  * but the alternatives seem worse.
4030  */
4032  {
4035  }
4036 
4037  result = list_copy(activeSearchPath);
4038  if (!includeImplicit)
4039  {
4040  while (result && linitial_oid(result) != activeCreationNamespace)
4041  result = list_delete_first(result);
4042  }
4043 
4044  return result;
4045 }
4046 
4047 /*
4048  * Fetch the active search path into a caller-allocated array of OIDs.
4049  * Returns the number of path entries. (If this is more than sarray_len,
4050  * then the data didn't fit and is not all stored.)
4051  *
4052  * The returned list always includes the implicitly-prepended namespaces,
4053  * but never includes the temp namespace. (This is suitable for existing
4054  * users, which would want to ignore the temp namespace anyway.) This
4055  * definition allows us to not worry about initializing the temp namespace.
4056  */
4057 int
4058 fetch_search_path_array(Oid *sarray, int sarray_len)
4059 {
4060  int count = 0;
4061  ListCell *l;
4062 
4064 
4065  foreach(l, activeSearchPath)
4066  {
4067  Oid namespaceId = lfirst_oid(l);
4068 
4069  if (namespaceId == myTempNamespace)
4070  continue; /* do not include temp namespace */
4071 
4072  if (count < sarray_len)
4073  sarray[count] = namespaceId;
4074  count++;
4075  }
4076 
4077  return count;
4078 }
4079 
4080 
4081 /*
4082  * Export the FooIsVisible functions as SQL-callable functions.
4083  *
4084  * Note: as of Postgres 8.4, these will silently return NULL if called on
4085  * a nonexistent object OID, rather than failing. This is to avoid race
4086  * condition errors when a query that's scanning a catalog using an MVCC
4087  * snapshot uses one of these functions. The underlying IsVisible functions
4088  * always use an up-to-date snapshot and so might see the object as already
4089  * gone when it's still visible to the transaction snapshot. (There is no race
4090  * condition in the current coding because we don't accept sinval messages
4091  * between the SearchSysCacheExists test and the subsequent lookup.)
4092  */
4093 
4094 Datum
4096 {
4097  Oid oid = PG_GETARG_OID(0);
4098 
4100  PG_RETURN_NULL();
4101 
4103 }
4104 
4105 Datum
4107 {
4108  Oid oid = PG_GETARG_OID(0);
4109 
4111  PG_RETURN_NULL();
4112 
4114 }
4115 
4116 Datum
4118 {
4119  Oid oid = PG_GETARG_OID(0);
4120 
4122  PG_RETURN_NULL();
4123 
4125 }
4126 
4127 Datum
4129 {
4130  Oid oid = PG_GETARG_OID(0);
4131 
4133  PG_RETURN_NULL();
4134 
4136 }
4137 
4138 Datum
4140 {
4141  Oid oid = PG_GETARG_OID(0);
4142 
4144  PG_RETURN_NULL();
4145 
4147 }
4148 
4149 Datum
4151 {
4152  Oid oid = PG_GETARG_OID(0);
4153 
4155  PG_RETURN_NULL();
4156 
4158 }
4159 
4160 Datum
4162 {
4163  Oid oid = PG_GETARG_OID(0);
4164 
4166  PG_RETURN_NULL();
4167 
4169 }
4170 
4171 Datum
4173 {
4174  Oid oid = PG_GETARG_OID(0);
4175 
4177  PG_RETURN_NULL();
4178 
4180 }
4181 
4182 Datum
4184 {
4185  Oid oid = PG_GETARG_OID(0);
4186 
4188  PG_RETURN_NULL();
4189 
4191 }
4192 
4193 Datum
4195 {
4196  Oid oid = PG_GETARG_OID(0);
4197 
4199  PG_RETURN_NULL();
4200 
4202 }
4203 
4204 Datum
4206 {
4207  Oid oid = PG_GETARG_OID(0);
4208 
4210  PG_RETURN_NULL();
4211 
4213 }
4214 
4215 Datum
4217 {
4218  Oid oid = PG_GETARG_OID(0);
4219 
4221  PG_RETURN_NULL();
4222 
4224 }
4225 
4226 Datum
4228 {
4230 }
4231 
4232 Datum
4234 {
4235  Oid oid = PG_GETARG_OID(0);
4236 
4238 }
Value * makeString(char *str)
Definition: value.c:53
void ResetTempTableNamespace(void)
Definition: namespace.c:3897
#define NIL
Definition: pg_list.h:69
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
static bool baseSearchPathValid
Definition: namespace.c:152
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
int n_members
Definition: catcache.h:154
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition: namespace.c:2839
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2687
BackendId MyBackendId
Definition: globals.c:72
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define AssertState(condition)
Definition: c.h:674
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
void AcceptInvalidationMessages(void)
Definition: inval.c:672
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:9968
FormData_pg_ts_config * Form_pg_ts_config
Definition: pg_ts_config.h:41
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:282
Oid TypenameGetTypid(const char *typname)
Definition: namespace.c:747
Datum pg_type_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4106
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
Oid oprid(Operator op)
Definition: parse_oper.c:243
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok, bool nowait, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:217
#define GUC_check_errdetail
Definition: guc.h:407
Oid RelnameGetRelid(const char *relname)
Definition: namespace.c:653
Datum pg_my_temp_schema(PG_FUNCTION_ARGS)
Definition: namespace.c:4227
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2790
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition: namespace.c:2976
#define PointerGetDatum(X)
Definition: postgres.h:564
static Oid myTempToastNamespace
Definition: namespace.c:182
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2730
#define SearchSysCache4(cacheId, key1, key2, key3, key4)
Definition: syscache.h:155
char * pstrdup(const char *in)
Definition: mcxt.c:1165
void CommitTransactionCommand(void)
Definition: xact.c:2745
Datum pg_opclass_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4139
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition: namespace.c:3068
void PushOverrideSearchPath(OverrideSearchPath *newpath)
Definition: namespace.c:3227
Datum pg_conversion_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4172
#define PERFORM_DELETION_SKIP_ORIGINAL
Definition: dependency.h:176
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:791
static Oid myTempNamespace
Definition: namespace.c:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
static List * overrideStack
Definition: namespace.c:163
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2473
bool OperatorIsVisible(Oid oprid)
Definition: namespace.c:1706
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int GetTempNamespaceBackendId(Oid namespaceId)
Definition: namespace.c:3029
void DeconstructQualifiedName(List *names, char **nspname_p, char **objname_p)
Definition: namespace.c:2603
bool FunctionIsVisible(Oid funcid)
Definition: namespace.c:1373
static Oid namespaceUser
Definition: namespace.c:149
void(* RangeVarGetRelidCallback)(const RangeVar *relation, Oid relId, Oid oldRelId, void *callback_arg)
Definition: namespace.h:50
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2857
Oid CollationGetCollid(const char *collname)
Definition: namespace.c:1922
uint32 SubTransactionId
Definition: c.h:398
List * lcons_oid(Oid datum, List *list)
Definition: list.c:295
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_TOAST_NAMESPACE
Definition: pg_namespace.h:74
bool RecoveryInProgress(void)
Definition: xlog.c:7805
Oid OpclassnameGetOpcid(Oid amid, const char *opcname)
Definition: namespace.c:1759
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:534
int fetch_search_path_array(Oid *sarray, int sarray_len)
Definition: namespace.c:4058
void AbortOutOfAnyTransaction(void)
Definition: xact.c:4220
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4459
#define GetSysCacheOid3(cacheId, key1, key2, key3)
Definition: syscache.h:180
void RangeVarAdjustRelationPersistence(RangeVar *newRelation, Oid nspid)
Definition: namespace.c:614
#define lsecond(l)
Definition: pg_list.h:114
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
signed int int32
Definition: c.h:253
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:178
FuncCandidateList OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok)
Definition: namespace.c:1546
GucSource
Definition: guc.h:105
static void recomputeNamespacePath(void)
Definition: namespace.c:3479
struct _FuncCandidateList * FuncCandidateList
char * schemaname
Definition: primnodes.h:66
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:176
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
#define FUNC_MAX_ARGS
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:174
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:155
#define list_make1(x1)
Definition: pg_list.h:133
#define NAMEDATALEN
Oid ConversionGetConid(const char *conname)
Definition: namespace.c:2015
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:72
char * relname
Definition: primnodes.h:67
Oid OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
Definition: namespace.c:1444
Oid get_ts_dict_oid(List *names, bool missing_ok)
Definition: namespace.c:2220
static Oid activeCreationNamespace
Definition: namespace.c:136
Oid OpfamilynameGetOpfid(Oid amid, const char *opfname)
Definition: namespace.c:1842
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:167
Oid args[FLEXIBLE_ARRAY_MEMBER]
Definition: namespace.h:37
void pfree(void *pointer)
Definition: mcxt.c:992
#define linitial(l)
Definition: pg_list.h:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
bool TSConfigIsVisible(Oid cfgid)
Definition: namespace.c:2531
static bool baseTempCreationPending
Definition: namespace.c:147
FuncCandidateList FuncnameGetCandidates(List *names, int nargs, List *argnames, bool expand_variadic, bool expand_defaults, bool missing_ok)
Definition: namespace.c:902
void InitializeSearchPath(void)
Definition: namespace.c:3960
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
OverrideSearchPath * CopyOverrideSearchPath(OverrideSearchPath *path)
Definition: namespace.c:3151
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1651
OverrideSearchPath * GetOverrideSearchPath(MemoryContext context)
Definition: namespace.c:3115
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2048
Datum pg_opfamily_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4150
FormData_pg_ts_dict * Form_pg_ts_dict
Definition: pg_ts_dict.h:45
Oid GetTempToastNamespace(void)
Definition: namespace.c:3054
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
Oid get_ts_template_oid(List *names, bool missing_ok)
Definition: namespace.c:2347
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition: namespace.c:3084
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3129
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3378
Datum pg_function_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4117
#define CStringGetDatum(X)
Definition: postgres.h:586
char string[11]
Definition: preproc-type.c:46
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:830
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:301
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
unsigned int uint32
Definition: c.h:265
static SubTransactionId myTempNamespaceSubID
Definition: namespace.c:184
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:2952
#define ACL_USAGE
Definition: parsenodes.h:73
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:194
bool CollationIsVisible(Oid collid)
Definition: namespace.c:1965
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
#define SPACE_PER_OP
struct _FuncCandidateList * next
Definition: namespace.h:30
static void NamespaceCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: namespace.c:4000
Datum pg_ts_config_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4216
#define lnext(lc)
Definition: pg_list.h:105
static bool activeTempCreationPending
Definition: namespace.c:139
#define ereport(elevel, rest)
Definition: elog.h:122
bool OpfamilyIsVisible(Oid opfid)
Definition: namespace.c:1875
#define IsParallelWorker()
Definition: parallel.h:53
MemoryContext TopMemoryContext
Definition: mcxt.c:43
Datum pg_ts_template_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4205
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition: namespace.c:2762
void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:851
static Oid baseCreationNamespace
Definition: namespace.c:145
static void InitTempTableNamespace(void)
Definition: namespace.c:3629
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:2964
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
bool TSDictionaryIsVisible(Oid dictId)
Definition: namespace.c:2278
#define WARNING
Definition: elog.h:40
char * NameListToString(List *names)
Definition: namespace.c:2897
char * NameListToQuotedString(List *names)
Definition: namespace.c:2931
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition: namespace.c:507
#define ReleaseSysCacheList(x)
Definition: syscache.h:203
void PopOverrideSearchPath(void)
Definition: namespace.c:3286
#define InvalidBackendId
Definition: backendid.h:23
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:44
char * namespace_search_path
Definition: namespace.c:190
static List * baseSearchPath
Definition: namespace.c:143
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1381
void * palloc0(Size size)
Definition: mcxt.c:920
AclResult
Definition: acl.h:170
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
void CommandCounterIncrement(void)
Definition: xact.c:921
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
FormData_pg_ts_parser * Form_pg_ts_parser
Definition: pg_ts_parser.h:44
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:56
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
#define list_make1_oid(x1)
Definition: pg_list.h:145
Datum pg_ts_parser_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4183
Oid MyDatabaseId
Definition: globals.c:76
bool OpclassIsVisible(Oid opcid)
Definition: namespace.c:1792
uint64 SharedInvalidMessageCounter
Definition: sinval.c:26
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
void assign_search_path(const char *newval, void *extra)
Definition: namespace.c:3944
Datum pg_ts_dict_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4194
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
List * lcons(void *datum, List *list)
Definition: list.c:259
bool ConversionIsVisible(Oid conid)
Definition: namespace.c:2047
bool OverrideSearchPathMatchesCurrent(OverrideSearchPath *path)
Definition: namespace.c:3167
#define Max(x, y)
Definition: c.h:796
Datum pg_is_other_temp_schema(PG_FUNCTION_ARGS)
Definition: namespace.c:4233
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4409
#define NULL
Definition: c.h:226
Oid get_ts_parser_oid(List *names, bool missing_ok)
Definition: namespace.c:2094
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1021
bool TypeIsVisible(Oid typid)
Definition: namespace.c:776
#define PERFORM_DELETION_QUIETLY
Definition: dependency.h:175
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:648
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4521
void StartTransactionCommand(void)
Definition: xact.c:2675
Datum pg_operator_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4128
static void RemoveTempRelations(Oid tempNamespaceId)
Definition: namespace.c:3853
#define linitial_oid(l)
Definition: pg_list.h:112
bool check_search_path(char **newval, void **extra, GucSource source)
Definition: namespace.c:3910
static int list_length(const List *l)
Definition: pg_list.h:89
#define newval
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3452
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:169
#define BOOTSTRAP_SUPERUSERID
Definition: pg_authid.h:104
#define InvalidSubTransactionId
Definition: c.h:400
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
const char * name
Definition: encode.c:521
static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames, int **argnumbers)
Definition: namespace.c:1265
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:47
Datum pg_collation_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4161
#define nodeTag(nodeptr)
Definition: nodes.h:513
char relpersistence
Definition: primnodes.h:70
Oid get_conversion_oid(List *name, bool missing_ok)
Definition: namespace.c:3397
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
static List * activeSearchPath
Definition: namespace.c:133
#define Int32GetDatum(X)
Definition: postgres.h:487
bool ordered
Definition: catcache.h:150
bool TSTemplateIsVisible(Oid tmplId)
Definition: namespace.c:2405
bool TSParserIsVisible(Oid prsId)
Definition: namespace.c:2152
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define SearchSysCacheList3(cacheId, key1, key2, key3)
Definition: syscache.h:198
bool RelationIsVisible(Oid relid)
Definition: namespace.c:681
bool isOtherTempNamespace(Oid namespaceId)
Definition: namespace.c:3013
void list_free(List *list)
Definition: list.c:1133
FormData_pg_ts_template * Form_pg_ts_template
int i
#define ACL_CREATE_TEMP
Definition: parsenodes.h:76
#define NameStr(name)
Definition: c.h:495
void * arg
#define lthird(l)
Definition: pg_list.h:118
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:2990
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
HeapTupleData tuple
Definition: catcache.h:116
#define elog
Definition: elog.h:219
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:3793
Oid FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
#define PERFORM_DELETION_SKIP_EXTENSIONS
Definition: dependency.h:177
Datum pg_table_is_visible(PG_FUNCTION_ARGS)
Definition: namespace.c:4095
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:68
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3738
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3324
Definition: pg_list.h:45
#define PG_RETURN_OID(x)
Definition: fmgr.h:304
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4018
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
#define PG_RETURN_NULL()
Definition: fmgr.h:289
char * catalogname
Definition: primnodes.h:65
static void RemoveTempRelationsCallback(int code, Datum arg)
Definition: namespace.c:3879
#define offsetof(type, field)
Definition: c.h:551
Oid RangeVarGetCreationNamespace(const RangeVar *newRelation)
Definition: namespace.c:420
#define lfirst_oid(lc)
Definition: pg_list.h:108
Oid NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
Definition: pg_namespace.c:41
List * list_delete_first(List *list)
Definition: list.c:666
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2657
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:173