Age Owner Branch data TLA Line data Source code
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-2025, 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/namespace.h"
28 : : #include "catalog/objectaccess.h"
29 : : #include "catalog/pg_authid.h"
30 : : #include "catalog/pg_collation.h"
31 : : #include "catalog/pg_conversion.h"
32 : : #include "catalog/pg_database.h"
33 : : #include "catalog/pg_namespace.h"
34 : : #include "catalog/pg_opclass.h"
35 : : #include "catalog/pg_operator.h"
36 : : #include "catalog/pg_opfamily.h"
37 : : #include "catalog/pg_proc.h"
38 : : #include "catalog/pg_statistic_ext.h"
39 : : #include "catalog/pg_ts_config.h"
40 : : #include "catalog/pg_ts_dict.h"
41 : : #include "catalog/pg_ts_parser.h"
42 : : #include "catalog/pg_ts_template.h"
43 : : #include "catalog/pg_type.h"
44 : : #include "common/hashfn_unstable.h"
45 : : #include "funcapi.h"
46 : : #include "mb/pg_wchar.h"
47 : : #include "miscadmin.h"
48 : : #include "nodes/makefuncs.h"
49 : : #include "storage/ipc.h"
50 : : #include "storage/lmgr.h"
51 : : #include "storage/procarray.h"
52 : : #include "utils/acl.h"
53 : : #include "utils/builtins.h"
54 : : #include "utils/catcache.h"
55 : : #include "utils/guc_hooks.h"
56 : : #include "utils/inval.h"
57 : : #include "utils/lsyscache.h"
58 : : #include "utils/memutils.h"
59 : : #include "utils/snapmgr.h"
60 : : #include "utils/syscache.h"
61 : : #include "utils/varlena.h"
62 : :
63 : :
64 : : /*
65 : : * The namespace search path is a possibly-empty list of namespace OIDs.
66 : : * In addition to the explicit list, implicitly-searched namespaces
67 : : * may be included:
68 : : *
69 : : * 1. If a TEMP table namespace has been initialized in this session, it
70 : : * is implicitly searched first.
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 : : * activeSearchPath is always the actually active path; it points to
110 : : * baseSearchPath which is the list derived from namespace_search_path.
111 : : *
112 : : * If baseSearchPathValid is false, then baseSearchPath (and other derived
113 : : * variables) need to be recomputed from namespace_search_path, or retrieved
114 : : * from the search path cache if there haven't been any syscache
115 : : * invalidations. We mark it invalid upon an assignment to
116 : : * namespace_search_path or receipt of a syscache invalidation event for
117 : : * pg_namespace or pg_authid. The recomputation is done during the next
118 : : * lookup attempt.
119 : : *
120 : : * Any namespaces mentioned in namespace_search_path that are not readable
121 : : * by the current user ID are simply left out of baseSearchPath; so
122 : : * we have to be willing to recompute the path when current userid changes.
123 : : * namespaceUser is the userid the path has been computed for.
124 : : *
125 : : * Note: all data pointed to by these List variables is in TopMemoryContext.
126 : : *
127 : : * activePathGeneration is incremented whenever the effective values of
128 : : * activeSearchPath/activeCreationNamespace/activeTempCreationPending change.
129 : : * This can be used to quickly detect whether any change has happened since
130 : : * a previous examination of the search path state.
131 : : */
132 : :
133 : : /* These variables define the actually active state: */
134 : :
135 : : static List *activeSearchPath = NIL;
136 : :
137 : : /* default place to create stuff; if InvalidOid, no default */
138 : : static Oid activeCreationNamespace = InvalidOid;
139 : :
140 : : /* if true, activeCreationNamespace is wrong, it should be temp namespace */
141 : : static bool activeTempCreationPending = false;
142 : :
143 : : /* current generation counter; make sure this is never zero */
144 : : static uint64 activePathGeneration = 1;
145 : :
146 : : /* These variables are the values last derived from namespace_search_path: */
147 : :
148 : : static List *baseSearchPath = NIL;
149 : :
150 : : static Oid baseCreationNamespace = InvalidOid;
151 : :
152 : : static bool baseTempCreationPending = false;
153 : :
154 : : static Oid namespaceUser = InvalidOid;
155 : :
156 : : /* The above four values are valid only if baseSearchPathValid */
157 : : static bool baseSearchPathValid = true;
158 : :
159 : : /*
160 : : * Storage for search path cache. Clear searchPathCacheValid as a simple
161 : : * way to invalidate *all* the cache entries, not just the active one.
162 : : */
163 : : static bool searchPathCacheValid = false;
164 : : static MemoryContext SearchPathCacheContext = NULL;
165 : :
166 : : typedef struct SearchPathCacheKey
167 : : {
168 : : const char *searchPath;
169 : : Oid roleid;
170 : : } SearchPathCacheKey;
171 : :
172 : : typedef struct SearchPathCacheEntry
173 : : {
174 : : SearchPathCacheKey key;
175 : : List *oidlist; /* namespace OIDs that pass ACL checks */
176 : : List *finalPath; /* cached final computed search path */
177 : : Oid firstNS; /* first explicitly-listed namespace */
178 : : bool temp_missing;
179 : : bool forceRecompute; /* force recompute of finalPath */
180 : :
181 : : /* needed for simplehash */
182 : : char status;
183 : : } SearchPathCacheEntry;
184 : :
185 : : /*
186 : : * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
187 : : * in a particular backend session (this happens when a CREATE TEMP TABLE
188 : : * command is first executed). Thereafter it's the OID of the temp namespace.
189 : : *
190 : : * myTempToastNamespace is the OID of the namespace for my temp tables' toast
191 : : * tables. It is set when myTempNamespace is, and is InvalidOid before that.
192 : : *
193 : : * myTempNamespaceSubID shows whether we've created the TEMP namespace in the
194 : : * current subtransaction. The flag propagates up the subtransaction tree,
195 : : * so the main transaction will correctly recognize the flag if all
196 : : * intermediate subtransactions commit. When it is InvalidSubTransactionId,
197 : : * we either haven't made the TEMP namespace yet, or have successfully
198 : : * committed its creation, depending on whether myTempNamespace is valid.
199 : : */
200 : : static Oid myTempNamespace = InvalidOid;
201 : :
202 : : static Oid myTempToastNamespace = InvalidOid;
203 : :
204 : : static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId;
205 : :
206 : : /*
207 : : * This is the user's textual search path specification --- it's the value
208 : : * of the GUC variable 'search_path'.
209 : : */
210 : : char *namespace_search_path = NULL;
211 : :
212 : :
213 : : /* Local functions */
214 : : static bool RelationIsVisibleExt(Oid relid, bool *is_missing);
215 : : static bool TypeIsVisibleExt(Oid typid, bool *is_missing);
216 : : static bool FunctionIsVisibleExt(Oid funcid, bool *is_missing);
217 : : static bool OperatorIsVisibleExt(Oid oprid, bool *is_missing);
218 : : static bool OpclassIsVisibleExt(Oid opcid, bool *is_missing);
219 : : static bool OpfamilyIsVisibleExt(Oid opfid, bool *is_missing);
220 : : static bool CollationIsVisibleExt(Oid collid, bool *is_missing);
221 : : static bool ConversionIsVisibleExt(Oid conid, bool *is_missing);
222 : : static bool StatisticsObjIsVisibleExt(Oid stxid, bool *is_missing);
223 : : static bool TSParserIsVisibleExt(Oid prsId, bool *is_missing);
224 : : static bool TSDictionaryIsVisibleExt(Oid dictId, bool *is_missing);
225 : : static bool TSTemplateIsVisibleExt(Oid tmplId, bool *is_missing);
226 : : static bool TSConfigIsVisibleExt(Oid cfgid, bool *is_missing);
227 : : static void recomputeNamespacePath(void);
228 : : static void AccessTempTableNamespace(bool force);
229 : : static void InitTempTableNamespace(void);
230 : : static void RemoveTempRelations(Oid tempNamespaceId);
231 : : static void RemoveTempRelationsCallback(int code, Datum arg);
232 : : static void InvalidationCallback(Datum arg, int cacheid, uint32 hashvalue);
233 : : static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
234 : : bool include_out_arguments, int pronargs,
235 : : int **argnumbers, int *fgc_flags);
236 : :
237 : : /*
238 : : * Recomputing the namespace path can be costly when done frequently, such as
239 : : * when a function has search_path set in proconfig. Add a search path cache
240 : : * that can be used by recomputeNamespacePath().
241 : : *
242 : : * The cache is also used to remember already-validated strings in
243 : : * check_search_path() to avoid the need to call SplitIdentifierString()
244 : : * repeatedly.
245 : : *
246 : : * The search path cache is based on a wrapper around a simplehash hash table
247 : : * (nsphash, defined below). The spcache wrapper deals with OOM while trying
248 : : * to initialize a key, optimizes repeated lookups of the same key, and also
249 : : * offers a more convenient API.
250 : : */
251 : :
252 : : static inline uint32
713 jdavis@postgresql.or 253 :CBC 80974 : spcachekey_hash(SearchPathCacheKey key)
254 : : {
255 : : fasthash_state hs;
256 : : int sp_len;
257 : :
645 john.naylor@postgres 258 : 80974 : fasthash_init(&hs, 0);
259 : :
650 260 : 80974 : hs.accum = key.roleid;
261 : 80974 : fasthash_combine(&hs);
262 : :
263 : : /*
264 : : * Combine search path into the hash and save the length for tweaking the
265 : : * final mix.
266 : : */
267 : 80974 : sp_len = fasthash_accum_cstring(&hs, key.searchPath);
268 : :
269 : 80974 : return fasthash_final32(&hs, sp_len);
270 : : }
271 : :
272 : : static inline bool
713 jdavis@postgresql.or 273 : 31220 : spcachekey_equal(SearchPathCacheKey a, SearchPathCacheKey b)
274 : : {
275 [ + + ]: 62179 : return a.roleid == b.roleid &&
276 [ + + ]: 30959 : strcmp(a.searchPath, b.searchPath) == 0;
277 : : }
278 : :
279 : : #define SH_PREFIX nsphash
280 : : #define SH_ELEMENT_TYPE SearchPathCacheEntry
281 : : #define SH_KEY_TYPE SearchPathCacheKey
282 : : #define SH_KEY key
283 : : #define SH_HASH_KEY(tb, key) spcachekey_hash(key)
284 : : #define SH_EQUAL(tb, a, b) spcachekey_equal(a, b)
285 : : #define SH_SCOPE static inline
286 : : #define SH_DECLARE
287 : : #define SH_DEFINE
288 : : #include "lib/simplehash.h"
289 : :
290 : : /*
291 : : * We only expect a small number of unique search_path strings to be used. If
292 : : * this cache grows to an unreasonable size, reset it to avoid steady-state
293 : : * memory growth. Most likely, only a few of those entries will benefit from
294 : : * the cache, and the cache will be quickly repopulated with such entries.
295 : : */
296 : : #define SPCACHE_RESET_THRESHOLD 256
297 : :
298 : : static nsphash_hash *SearchPathCache = NULL;
299 : : static SearchPathCacheEntry *LastSearchPathCacheEntry = NULL;
300 : :
301 : : /*
302 : : * Create or reset search_path cache as necessary.
303 : : */
304 : : static void
305 : 112258 : spcache_init(void)
306 : : {
707 307 [ + + + + ]: 112258 : if (SearchPathCache && searchPathCacheValid &&
308 [ + - ]: 93733 : SearchPathCache->members < SPCACHE_RESET_THRESHOLD)
713 309 : 93733 : return;
310 : :
385 311 : 18525 : searchPathCacheValid = false;
312 : 18525 : baseSearchPathValid = false;
313 : :
314 : : /*
315 : : * Make sure we don't leave dangling pointers if a failure happens during
316 : : * initialization.
317 : : */
664 tgl@sss.pgh.pa.us 318 : 18525 : SearchPathCache = NULL;
693 jdavis@postgresql.or 319 : 18525 : LastSearchPathCacheEntry = NULL;
320 : :
385 321 [ + + ]: 18525 : if (SearchPathCacheContext == NULL)
322 : : {
323 : : /* Make the context we'll keep search path cache hashtable in */
324 : 9486 : SearchPathCacheContext = AllocSetContextCreate(TopMemoryContext,
325 : : "search_path processing cache",
326 : : ALLOCSET_DEFAULT_SIZES);
327 : : }
328 : : else
329 : : {
330 : 9039 : MemoryContextReset(SearchPathCacheContext);
331 : : }
332 : :
333 : : /* arbitrary initial starting size of 16 elements */
713 334 : 18525 : SearchPathCache = nsphash_create(SearchPathCacheContext, 16, NULL);
335 : 18525 : searchPathCacheValid = true;
336 : : }
337 : :
338 : : /*
339 : : * Look up entry in search path cache without inserting. Returns NULL if not
340 : : * present.
341 : : */
342 : : static SearchPathCacheEntry *
707 343 : 75791 : spcache_lookup(const char *searchPath, Oid roleid)
344 : : {
693 345 [ + + ]: 75791 : if (LastSearchPathCacheEntry &&
346 [ + + ]: 74476 : LastSearchPathCacheEntry->key.roleid == roleid &&
347 [ + + ]: 74375 : strcmp(LastSearchPathCacheEntry->key.searchPath, searchPath) == 0)
348 : : {
349 : 57253 : return LastSearchPathCacheEntry;
350 : : }
351 : : else
352 : : {
353 : : SearchPathCacheEntry *entry;
354 : 18538 : SearchPathCacheKey cachekey = {
355 : : .searchPath = searchPath,
356 : : .roleid = roleid
357 : : };
358 : :
359 : 18538 : entry = nsphash_lookup(SearchPathCache, cachekey);
664 tgl@sss.pgh.pa.us 360 [ + + ]: 18538 : if (entry)
361 : 13970 : LastSearchPathCacheEntry = entry;
693 jdavis@postgresql.or 362 : 18538 : return entry;
363 : : }
364 : : }
365 : :
366 : : /*
367 : : * Look up or insert entry in search path cache.
368 : : *
369 : : * Initialize key safely, so that OOM does not leave an entry without a valid
370 : : * key. Caller must ensure that non-key contents are properly initialized.
371 : : */
372 : : static SearchPathCacheEntry *
713 373 : 41035 : spcache_insert(const char *searchPath, Oid roleid)
374 : : {
693 375 [ + + ]: 41035 : if (LastSearchPathCacheEntry &&
376 [ + + ]: 22510 : LastSearchPathCacheEntry->key.roleid == roleid &&
377 [ + + ]: 20799 : strcmp(LastSearchPathCacheEntry->key.searchPath, searchPath) == 0)
378 : : {
379 : 1530 : return LastSearchPathCacheEntry;
380 : : }
381 : : else
382 : : {
383 : : SearchPathCacheEntry *entry;
384 : 39505 : SearchPathCacheKey cachekey = {
385 : : .searchPath = searchPath,
386 : : .roleid = roleid
387 : : };
388 : :
389 : : /*
390 : : * searchPath is not saved in SearchPathCacheContext. First perform a
391 : : * lookup, and copy searchPath only if we need to create a new entry.
392 : : */
393 : 39505 : entry = nsphash_lookup(SearchPathCache, cachekey);
394 : :
395 [ + + ]: 39505 : if (!entry)
396 : : {
397 : : bool found;
398 : :
399 : 22834 : cachekey.searchPath = MemoryContextStrdup(SearchPathCacheContext, searchPath);
400 : 22834 : entry = nsphash_insert(SearchPathCache, cachekey, &found);
401 [ - + ]: 22834 : Assert(!found);
402 : :
403 : 22834 : entry->oidlist = NIL;
404 : 22834 : entry->finalPath = NIL;
405 : 22834 : entry->firstNS = InvalidOid;
406 : 22834 : entry->temp_missing = false;
407 : 22834 : entry->forceRecompute = false;
408 : : /* do not touch entry->status, used by simplehash */
409 : : }
410 : :
411 : 39505 : LastSearchPathCacheEntry = entry;
412 : 39505 : return entry;
413 : : }
414 : : }
415 : :
416 : : /*
417 : : * RangeVarGetRelidExtended
418 : : * Given a RangeVar describing an existing relation,
419 : : * select the proper namespace and look up the relation OID.
420 : : *
421 : : * If the schema or relation is not found, return InvalidOid if flags contains
422 : : * RVR_MISSING_OK, otherwise raise an error.
423 : : *
424 : : * If flags contains RVR_NOWAIT, throw an error if we'd have to wait for a
425 : : * lock.
426 : : *
427 : : * If flags contains RVR_SKIP_LOCKED, return InvalidOid if we'd have to wait
428 : : * for a lock.
429 : : *
430 : : * flags cannot contain both RVR_NOWAIT and RVR_SKIP_LOCKED.
431 : : *
432 : : * Note that if RVR_MISSING_OK and RVR_SKIP_LOCKED are both specified, a
433 : : * return value of InvalidOid could either mean the relation is missing or it
434 : : * could not be locked.
435 : : *
436 : : * Callback allows caller to check permissions or acquire additional locks
437 : : * prior to grabbing the relation lock.
438 : : */
439 : : Oid
5080 rhaas@postgresql.org 440 : 349564 : RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
441 : : uint32 flags,
442 : : RangeVarGetRelidCallback callback, void *callback_arg)
443 : : {
444 : : uint64 inval_count;
445 : : Oid relId;
5225 446 : 349564 : Oid oldRelId = InvalidOid;
447 : 349564 : bool retry = false;
2768 andres@anarazel.de 448 : 349564 : bool missing_ok = (flags & RVR_MISSING_OK) != 0;
449 : :
450 : : /* verify that flags do no conflict */
451 [ + + - + ]: 349564 : Assert(!((flags & RVR_NOWAIT) && (flags & RVR_SKIP_LOCKED)));
452 : :
453 : : /*
454 : : * We check the catalog name and then ignore it.
455 : : */
8616 tgl@sss.pgh.pa.us 456 [ + + ]: 349564 : if (relation->catalogname)
457 : : {
8158 peter_e@gmx.net 458 [ - + ]: 40 : if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
8134 tgl@sss.pgh.pa.us 459 [ + - ]: 40 : ereport(ERROR,
460 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
461 : : errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
462 : : relation->catalogname, relation->schemaname,
463 : : relation->relname)));
464 : : }
465 : :
466 : : /*
467 : : * DDL operations can change the results of a name lookup. Since all such
468 : : * operations will generate invalidation messages, we keep track of
469 : : * whether any such messages show up while we're performing the operation,
470 : : * and retry until either (1) no more invalidation messages show up or (2)
471 : : * the answer doesn't change.
472 : : *
473 : : * But if lockmode = NoLock, then we assume that either the caller is OK
474 : : * with the answer changing under them, or that they already hold some
475 : : * appropriate lock, and therefore return the first answer we get without
476 : : * checking for invalidation messages. Also, if the requested lock is
477 : : * already held, LockRelationOid will not AcceptInvalidationMessages, so
478 : : * we may fail to notice a change. We could protect against that case by
479 : : * calling AcceptInvalidationMessages() before beginning this loop, but
480 : : * that would add a significant amount overhead, so for now we don't.
481 : : */
482 : : for (;;)
483 : : {
484 : : /*
485 : : * Remember this value, so that, after looking up the relation name
486 : : * and locking its OID, we can check whether any invalidation messages
487 : : * have been processed that might require a do-over.
488 : : */
5225 rhaas@postgresql.org 489 : 351220 : inval_count = SharedInvalidMessageCounter;
490 : :
491 : : /*
492 : : * Some non-default relpersistence value may have been specified. The
493 : : * parser never generates such a RangeVar in simple DML, but it can
494 : : * happen in contexts such as "CREATE TEMP TABLE foo (f1 int PRIMARY
495 : : * KEY)". Such a command will generate an added CREATE INDEX
496 : : * operation, which must be careful to find the temp table, even when
497 : : * pg_temp is not first in the search path.
498 : : */
499 [ + + ]: 351220 : if (relation->relpersistence == RELPERSISTENCE_TEMP)
500 : : {
5150 501 [ - + ]: 347 : if (!OidIsValid(myTempNamespace))
3050 tgl@sss.pgh.pa.us 502 :UBC 0 : relId = InvalidOid; /* this probably can't happen? */
503 : : else
504 : : {
5150 rhaas@postgresql.org 505 [ + + ]:CBC 347 : if (relation->schemaname)
506 : : {
507 : : Oid namespaceId;
508 : :
4657 bruce@momjian.us 509 : 6 : namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
510 : :
511 : : /*
512 : : * For missing_ok, allow a non-existent schema name to
513 : : * return InvalidOid.
514 : : */
5150 rhaas@postgresql.org 515 [ - + ]: 6 : if (namespaceId != myTempNamespace)
5150 rhaas@postgresql.org 516 [ # # ]:UBC 0 : ereport(ERROR,
517 : : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
518 : : errmsg("temporary tables cannot specify a schema name")));
519 : : }
520 : :
5225 rhaas@postgresql.org 521 :CBC 347 : relId = get_relname_relid(relation->relname, myTempNamespace);
522 : : }
523 : : }
524 [ + + ]: 350873 : else if (relation->schemaname)
525 : : {
526 : : Oid namespaceId;
527 : :
528 : : /* use exact schema given */
4657 bruce@momjian.us 529 : 139581 : namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
530 [ + + + + ]: 139524 : if (missing_ok && !OidIsValid(namespaceId))
531 : 42 : relId = InvalidOid;
532 : : else
533 : 139482 : relId = get_relname_relid(relation->relname, namespaceId);
534 : : }
535 : : else
536 : : {
537 : : /* search the namespace path */
5225 rhaas@postgresql.org 538 : 211292 : relId = RelnameGetRelid(relation->relname);
539 : : }
540 : :
541 : : /*
542 : : * Invoke caller-supplied callback, if any.
543 : : *
544 : : * This callback is a good place to check permissions: we haven't
545 : : * taken the table lock yet (and it's really best to check permissions
546 : : * before locking anything!), but we've gotten far enough to know what
547 : : * OID we think we should lock. Of course, concurrent DDL might
548 : : * change things while we're waiting for the lock, but in that case
549 : : * the callback will be invoked again for the new OID.
550 : : */
5080 551 [ + + ]: 351163 : if (callback)
552 : 46135 : callback(relation, relId, oldRelId, callback_arg);
553 : :
554 : : /*
555 : : * If no lock requested, we assume the caller knows what they're
556 : : * doing. They should have already acquired a heavyweight lock on
557 : : * this relation earlier in the processing of this same statement, so
558 : : * it wouldn't be appropriate to AcceptInvalidationMessages() here, as
559 : : * that might pull the rug out from under them.
560 : : */
5225 561 [ + + ]: 350986 : if (lockmode == NoLock)
562 : 26070 : break;
563 : :
564 : : /*
565 : : * If, upon retry, we get back the same OID we did last time, then the
566 : : * invalidation messages we processed did not change the final answer.
567 : : * So we're done.
568 : : *
569 : : * If we got a different OID, we've locked the relation that used to
570 : : * have this name rather than the one that does now. So release the
571 : : * lock.
572 : : */
5098 573 [ + + ]: 324916 : if (retry)
574 : : {
575 [ + + ]: 1696 : if (relId == oldRelId)
576 : 1690 : break;
577 [ + - ]: 6 : if (OidIsValid(oldRelId))
578 : 6 : UnlockRelationOid(oldRelId, lockmode);
579 : : }
580 : :
581 : : /*
582 : : * Lock relation. This will also accept any pending invalidation
583 : : * messages. If we got back InvalidOid, indicating not found, then
584 : : * there's nothing to lock, but we accept invalidation messages
585 : : * anyway, to flush any negative catcache entries that may be
586 : : * lingering.
587 : : */
5225 588 [ + + ]: 323226 : if (!OidIsValid(relId))
589 : 990 : AcceptInvalidationMessages();
2768 andres@anarazel.de 590 [ + + ]: 322236 : else if (!(flags & (RVR_NOWAIT | RVR_SKIP_LOCKED)))
5225 rhaas@postgresql.org 591 : 321972 : LockRelationOid(relId, lockmode);
592 [ + + ]: 264 : else if (!ConditionalLockRelationOid(relId, lockmode))
593 : : {
2768 andres@anarazel.de 594 [ + + ]: 8 : int elevel = (flags & RVR_SKIP_LOCKED) ? DEBUG1 : ERROR;
595 : :
5225 rhaas@postgresql.org 596 [ - + ]: 8 : if (relation->schemaname)
2768 andres@anarazel.de 597 [ # # ]:UBC 0 : ereport(elevel,
598 : : (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
599 : : errmsg("could not obtain lock on relation \"%s.%s\"",
600 : : relation->schemaname, relation->relname)));
601 : : else
2768 andres@anarazel.de 602 [ + + ]:CBC 8 : ereport(elevel,
603 : : (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
604 : : errmsg("could not obtain lock on relation \"%s\"",
605 : : relation->relname)));
606 : :
607 : 4 : return InvalidOid;
608 : : }
609 : :
610 : : /*
611 : : * If no invalidation message were processed, we're done!
612 : : */
5225 rhaas@postgresql.org 613 [ + + ]: 323210 : if (inval_count == SharedInvalidMessageCounter)
614 : 321514 : break;
615 : :
616 : : /*
617 : : * Something may have changed. Let's repeat the name lookup, to make
618 : : * sure this name still references the same relation it did
619 : : * previously.
620 : : */
621 : 1696 : retry = true;
622 : 1696 : oldRelId = relId;
623 : : }
624 : :
2768 andres@anarazel.de 625 [ + + ]: 349274 : if (!OidIsValid(relId))
626 : : {
627 [ + + ]: 1034 : int elevel = missing_ok ? DEBUG1 : ERROR;
628 : :
8616 tgl@sss.pgh.pa.us 629 [ + + ]: 1034 : if (relation->schemaname)
2768 andres@anarazel.de 630 [ + + ]: 144 : ereport(elevel,
631 : : (errcode(ERRCODE_UNDEFINED_TABLE),
632 : : errmsg("relation \"%s.%s\" does not exist",
633 : : relation->schemaname, relation->relname)));
634 : : else
635 [ + + ]: 890 : ereport(elevel,
636 : : (errcode(ERRCODE_UNDEFINED_TABLE),
637 : : errmsg("relation \"%s\" does not exist",
638 : : relation->relname)));
639 : : }
8616 tgl@sss.pgh.pa.us 640 : 349040 : return relId;
641 : : }
642 : :
643 : : /*
644 : : * RangeVarGetCreationNamespace
645 : : * Given a RangeVar describing a to-be-created relation,
646 : : * choose which namespace to create it in.
647 : : *
648 : : * Note: calling this may result in a CommandCounterIncrement operation.
649 : : * That will happen on the first request for a temp table in any particular
650 : : * backend run; we will need to either create or clean out the temp schema.
651 : : */
652 : : Oid
653 : 59823 : RangeVarGetCreationNamespace(const RangeVar *newRelation)
654 : : {
655 : : Oid namespaceId;
656 : :
657 : : /*
658 : : * We check the catalog name and then ignore it.
659 : : */
660 [ - + ]: 59823 : if (newRelation->catalogname)
661 : : {
8158 peter_e@gmx.net 662 [ # # ]:UBC 0 : if (strcmp(newRelation->catalogname, get_database_name(MyDatabaseId)) != 0)
8134 tgl@sss.pgh.pa.us 663 [ # # ]: 0 : ereport(ERROR,
664 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
665 : : errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
666 : : newRelation->catalogname, newRelation->schemaname,
667 : : newRelation->relname)));
668 : : }
669 : :
8616 tgl@sss.pgh.pa.us 670 [ + + ]:CBC 59823 : if (newRelation->schemaname)
671 : : {
672 : : /* check for pg_temp alias */
6765 673 [ + + ]: 21345 : if (strcmp(newRelation->schemaname, "pg_temp") == 0)
674 : : {
675 : : /* Initialize temp namespace */
2474 michael@paquier.xyz 676 : 35 : AccessTempTableNamespace(false);
6765 tgl@sss.pgh.pa.us 677 : 35 : return myTempNamespace;
678 : : }
679 : : /* use exact schema given */
5562 rhaas@postgresql.org 680 : 21310 : namespaceId = get_namespace_oid(newRelation->schemaname, false);
681 : : /* we do not check for USAGE rights here! */
682 : : }
5230 683 [ + + ]: 38478 : else if (newRelation->relpersistence == RELPERSISTENCE_TEMP)
684 : : {
685 : : /* Initialize temp namespace */
2474 michael@paquier.xyz 686 : 3215 : AccessTempTableNamespace(false);
5230 rhaas@postgresql.org 687 : 3215 : return myTempNamespace;
688 : : }
689 : : else
690 : : {
691 : : /* use the default creation namespace */
8582 tgl@sss.pgh.pa.us 692 : 35263 : recomputeNamespacePath();
6765 693 [ - + ]: 35263 : if (activeTempCreationPending)
694 : : {
695 : : /* Need to initialize temp namespace */
2474 michael@paquier.xyz 696 :UBC 0 : AccessTempTableNamespace(true);
6765 tgl@sss.pgh.pa.us 697 : 0 : return myTempNamespace;
698 : : }
6793 tgl@sss.pgh.pa.us 699 :CBC 35263 : namespaceId = activeCreationNamespace;
8596 700 [ - + ]: 35263 : if (!OidIsValid(namespaceId))
8134 tgl@sss.pgh.pa.us 701 [ # # ]:UBC 0 : ereport(ERROR,
702 : : (errcode(ERRCODE_UNDEFINED_SCHEMA),
703 : : errmsg("no schema has been selected to create in")));
704 : : }
705 : :
706 : : /* Note: callers will check for CREATE rights when appropriate */
707 : :
8616 tgl@sss.pgh.pa.us 708 :CBC 56573 : return namespaceId;
709 : : }
710 : :
711 : : /*
712 : : * RangeVarGetAndCheckCreationNamespace
713 : : *
714 : : * This function returns the OID of the namespace in which a new relation
715 : : * with a given name should be created. If the user does not have CREATE
716 : : * permission on the target namespace, this function will instead signal
717 : : * an ERROR.
718 : : *
719 : : * If non-NULL, *existing_relation_id is set to the OID of any existing relation
720 : : * with the same name which already exists in that namespace, or to InvalidOid
721 : : * if no such relation exists.
722 : : *
723 : : * If lockmode != NoLock, the specified lock mode is acquired on the existing
724 : : * relation, if any, provided that the current user owns the target relation.
725 : : * However, if lockmode != NoLock and the user does not own the target
726 : : * relation, we throw an ERROR, as we must not try to lock relations the
727 : : * user does not have permissions on.
728 : : *
729 : : * As a side effect, this function acquires AccessShareLock on the target
730 : : * namespace. Without this, the namespace could be dropped before our
731 : : * transaction commits, leaving behind relations with relnamespace pointing
732 : : * to a no-longer-existent namespace.
733 : : *
734 : : * As a further side-effect, if the selected namespace is a temporary namespace,
735 : : * we mark the RangeVar as RELPERSISTENCE_TEMP.
736 : : */
737 : : Oid
5033 rhaas@postgresql.org 738 : 57819 : RangeVarGetAndCheckCreationNamespace(RangeVar *relation,
739 : : LOCKMODE lockmode,
740 : : Oid *existing_relation_id)
741 : : {
742 : : uint64 inval_count;
743 : : Oid relid;
744 : 57819 : Oid oldrelid = InvalidOid;
745 : : Oid nspid;
746 : 57819 : Oid oldnspid = InvalidOid;
747 : 57819 : bool retry = false;
748 : :
749 : : /*
750 : : * We check the catalog name and then ignore it.
751 : : */
752 [ + - ]: 57819 : if (relation->catalogname)
753 : : {
5033 rhaas@postgresql.org 754 [ # # ]:UBC 0 : if (strcmp(relation->catalogname, get_database_name(MyDatabaseId)) != 0)
755 [ # # ]: 0 : ereport(ERROR,
756 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
757 : : errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
758 : : relation->catalogname, relation->schemaname,
759 : : relation->relname)));
760 : : }
761 : :
762 : : /*
763 : : * As in RangeVarGetRelidExtended(), we guard against concurrent DDL
764 : : * operations by tracking whether any invalidation messages are processed
765 : : * while we're doing the name lookups and acquiring locks. See comments
766 : : * in that function for a more detailed explanation of this logic.
767 : : */
768 : : for (;;)
5299 rhaas@postgresql.org 769 :CBC 492 : {
770 : : AclResult aclresult;
771 : :
5033 772 : 58311 : inval_count = SharedInvalidMessageCounter;
773 : :
774 : : /* Look up creation namespace and check for existing relation. */
775 : 58311 : nspid = RangeVarGetCreationNamespace(relation);
776 [ - + ]: 58311 : Assert(OidIsValid(nspid));
777 [ + + ]: 58311 : if (existing_relation_id != NULL)
778 : 28186 : relid = get_relname_relid(relation->relname, nspid);
779 : : else
780 : 30125 : relid = InvalidOid;
781 : :
782 : : /*
783 : : * In bootstrap processing mode, we don't bother with permissions or
784 : : * locking. Permissions might not be working yet, and locking is
785 : : * unnecessary.
786 : : */
787 [ - + ]: 58311 : if (IsBootstrapProcessingMode())
5033 rhaas@postgresql.org 788 :UBC 0 : break;
789 : :
790 : : /* Check namespace permissions. */
1079 peter@eisentraut.org 791 :CBC 58311 : aclresult = object_aclcheck(NamespaceRelationId, nspid, GetUserId(), ACL_CREATE);
5299 rhaas@postgresql.org 792 [ - + ]: 58311 : if (aclresult != ACLCHECK_OK)
2886 peter_e@gmx.net 793 :UBC 0 : aclcheck_error(aclresult, OBJECT_SCHEMA,
5033 rhaas@postgresql.org 794 : 0 : get_namespace_name(nspid));
795 : :
5033 rhaas@postgresql.org 796 [ + + ]:CBC 58311 : if (retry)
797 : : {
798 : : /* If nothing changed, we're done. */
799 [ + - + - ]: 492 : if (relid == oldrelid && nspid == oldnspid)
800 : 492 : break;
801 : : /* If creation namespace has changed, give up old lock. */
5033 rhaas@postgresql.org 802 [ # # ]:UBC 0 : if (nspid != oldnspid)
803 : 0 : UnlockDatabaseObject(NamespaceRelationId, oldnspid, 0,
804 : : AccessShareLock);
805 : : /* If name points to something different, give up old lock. */
806 [ # # # # : 0 : if (relid != oldrelid && OidIsValid(oldrelid) && lockmode != NoLock)
# # ]
807 : 0 : UnlockRelationOid(oldrelid, lockmode);
808 : : }
809 : :
810 : : /* Lock namespace. */
5033 rhaas@postgresql.org 811 [ + - ]:CBC 57819 : if (nspid != oldnspid)
812 : 57819 : LockDatabaseObject(NamespaceRelationId, nspid, 0, AccessShareLock);
813 : :
814 : : /* Lock relation, if required if and we have permission. */
815 [ + + + + ]: 57819 : if (lockmode != NoLock && OidIsValid(relid))
816 : : {
1079 peter@eisentraut.org 817 [ - + ]: 112 : if (!object_ownercheck(RelationRelationId, relid, GetUserId()))
2886 peter_e@gmx.net 818 :UBC 0 : aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)),
5033 rhaas@postgresql.org 819 : 0 : relation->relname);
5033 rhaas@postgresql.org 820 [ + - ]:CBC 112 : if (relid != oldrelid)
821 : 112 : LockRelationOid(relid, lockmode);
822 : : }
823 : :
824 : : /* If no invalidation message were processed, we're done! */
825 [ + + ]: 57819 : if (inval_count == SharedInvalidMessageCounter)
826 : 57327 : break;
827 : :
828 : : /* Something may have changed, so recheck our work. */
829 : 492 : retry = true;
830 : 492 : oldrelid = relid;
831 : 492 : oldnspid = nspid;
832 : : }
833 : :
834 : 57819 : RangeVarAdjustRelationPersistence(relation, nspid);
835 [ + + ]: 57807 : if (existing_relation_id != NULL)
836 : 27781 : *existing_relation_id = relid;
837 : 57807 : return nspid;
838 : : }
839 : :
840 : : /*
841 : : * Adjust the relpersistence for an about-to-be-created relation based on the
842 : : * creation namespace, and throw an error for invalid combinations.
843 : : */
844 : : void
5230 845 : 58709 : RangeVarAdjustRelationPersistence(RangeVar *newRelation, Oid nspid)
846 : : {
847 [ + + + ]: 58709 : switch (newRelation->relpersistence)
848 : : {
849 : 3081 : case RELPERSISTENCE_TEMP:
4081 bruce@momjian.us 850 [ + + ]: 3081 : if (!isTempOrTempToastNamespace(nspid))
851 : : {
5230 rhaas@postgresql.org 852 [ - + ]: 9 : if (isAnyTempNamespace(nspid))
5230 rhaas@postgresql.org 853 [ # # ]:UBC 0 : ereport(ERROR,
854 : : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
855 : : errmsg("cannot create relations in temporary schemas of other sessions")));
856 : : else
5230 rhaas@postgresql.org 857 [ + - ]:CBC 9 : ereport(ERROR,
858 : : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
859 : : errmsg("cannot create temporary relation in non-temporary schema")));
860 : : }
861 : 3072 : break;
862 : 55429 : case RELPERSISTENCE_PERMANENT:
4081 bruce@momjian.us 863 [ + + ]: 55429 : if (isTempOrTempToastNamespace(nspid))
5230 rhaas@postgresql.org 864 : 13 : newRelation->relpersistence = RELPERSISTENCE_TEMP;
865 [ - + ]: 55416 : else if (isAnyTempNamespace(nspid))
5230 rhaas@postgresql.org 866 [ # # ]:UBC 0 : ereport(ERROR,
867 : : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
868 : : errmsg("cannot create relations in temporary schemas of other sessions")));
5230 rhaas@postgresql.org 869 :CBC 55429 : break;
870 : 199 : default:
5211 871 [ + + ]: 199 : if (isAnyTempNamespace(nspid))
872 [ + - ]: 3 : ereport(ERROR,
873 : : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
874 : : errmsg("only temporary relations may be created in temporary schemas")));
875 : : }
5230 876 : 58697 : }
877 : :
878 : : /*
879 : : * RelnameGetRelid
880 : : * Try to resolve an unqualified relation name.
881 : : * Returns OID if relation found in search path, else InvalidOid.
882 : : */
883 : : Oid
8616 tgl@sss.pgh.pa.us 884 : 211322 : RelnameGetRelid(const char *relname)
885 : : {
886 : : Oid relid;
887 : : ListCell *l;
888 : :
8582 889 : 211322 : recomputeNamespacePath();
890 : :
6793 891 [ + - + + : 378383 : foreach(l, activeSearchPath)
+ + ]
892 : : {
7824 neilc@samurai.com 893 : 377485 : Oid namespaceId = lfirst_oid(l);
894 : :
8611 tgl@sss.pgh.pa.us 895 : 377485 : relid = get_relname_relid(relname, namespaceId);
896 [ + + ]: 377485 : if (OidIsValid(relid))
897 : 210424 : return relid;
898 : : }
899 : :
900 : : /* Not found in path */
901 : 898 : return InvalidOid;
902 : : }
903 : :
904 : :
905 : : /*
906 : : * RelationIsVisible
907 : : * Determine whether a relation (identified by OID) is visible in the
908 : : * current search path. Visible means "would be found by searching
909 : : * for the unqualified relation name".
910 : : */
911 : : bool
8580 912 : 152753 : RelationIsVisible(Oid relid)
913 : : {
744 914 : 152753 : return RelationIsVisibleExt(relid, NULL);
915 : : }
916 : :
917 : : /*
918 : : * RelationIsVisibleExt
919 : : * As above, but if the relation isn't found and is_missing is not NULL,
920 : : * then set *is_missing = true and return false instead of throwing
921 : : * an error. (Caller must initialize *is_missing = false.)
922 : : */
923 : : static bool
924 : 164609 : RelationIsVisibleExt(Oid relid, bool *is_missing)
925 : : {
926 : : HeapTuple reltup;
927 : : Form_pg_class relform;
928 : : Oid relnamespace;
929 : : bool visible;
930 : :
5734 rhaas@postgresql.org 931 : 164609 : reltup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
8580 tgl@sss.pgh.pa.us 932 [ + + ]: 164609 : if (!HeapTupleIsValid(reltup))
933 : : {
744 934 [ + - ]: 1 : if (is_missing != NULL)
935 : : {
936 : 1 : *is_missing = true;
937 : 1 : return false;
938 : : }
8134 tgl@sss.pgh.pa.us 939 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relid);
940 : : }
8580 tgl@sss.pgh.pa.us 941 :CBC 164608 : relform = (Form_pg_class) GETSTRUCT(reltup);
942 : :
8564 943 : 164608 : recomputeNamespacePath();
944 : :
945 : : /*
946 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
947 : : * the system namespace are surely in the path and so we needn't even do
948 : : * list_member_oid() for them.
949 : : */
8580 950 : 164608 : relnamespace = relform->relnamespace;
8564 951 [ + + ]: 164608 : if (relnamespace != PG_CATALOG_NAMESPACE &&
6793 952 [ + + ]: 135985 : !list_member_oid(activeSearchPath, relnamespace))
8580 953 : 56763 : visible = false;
954 : : else
955 : : {
956 : : /*
957 : : * If it is in the path, it might still not be visible; it could be
958 : : * hidden by another relation of the same name earlier in the path. So
959 : : * we must do a slow check for conflicting relations.
960 : : */
961 : 107845 : char *relname = NameStr(relform->relname);
962 : : ListCell *l;
963 : :
7326 964 : 107845 : visible = false;
6793 965 [ + - + - : 262782 : foreach(l, activeSearchPath)
+ - ]
966 : : {
7326 967 : 262782 : Oid namespaceId = lfirst_oid(l);
968 : :
969 [ + + ]: 262782 : if (namespaceId == relnamespace)
970 : : {
971 : : /* Found it first in path */
972 : 107812 : visible = true;
973 : 107812 : break;
974 : : }
975 [ + + ]: 154970 : if (OidIsValid(get_relname_relid(relname, namespaceId)))
976 : : {
977 : : /* Found something else first in path */
978 : 33 : break;
979 : : }
980 : : }
981 : : }
982 : :
8580 983 : 164608 : ReleaseSysCache(reltup);
984 : :
985 : 164608 : return visible;
986 : : }
987 : :
988 : :
989 : : /*
990 : : * TypenameGetTypid
991 : : * Wrapper for binary compatibility.
992 : : */
993 : : Oid
2275 noah@leadboat.com 994 :UBC 0 : TypenameGetTypid(const char *typname)
995 : : {
996 : 0 : return TypenameGetTypidExtended(typname, true);
997 : : }
998 : :
999 : : /*
1000 : : * TypenameGetTypidExtended
1001 : : * Try to resolve an unqualified datatype name.
1002 : : * Returns OID if type found in search path, else InvalidOid.
1003 : : *
1004 : : * This is essentially the same as RelnameGetRelid.
1005 : : */
1006 : : Oid
2275 noah@leadboat.com 1007 :CBC 180738 : TypenameGetTypidExtended(const char *typname, bool temp_ok)
1008 : : {
1009 : : Oid typid;
1010 : : ListCell *l;
1011 : :
8582 tgl@sss.pgh.pa.us 1012 : 180738 : recomputeNamespacePath();
1013 : :
6793 1014 [ + - + + : 295655 : foreach(l, activeSearchPath)
+ + ]
1015 : : {
7824 neilc@samurai.com 1016 : 272365 : Oid namespaceId = lfirst_oid(l);
1017 : :
2275 noah@leadboat.com 1018 [ + + + + ]: 272365 : if (!temp_ok && namespaceId == myTempNamespace)
1019 : 4752 : continue; /* do not look in temp namespace */
1020 : :
2533 andres@anarazel.de 1021 : 267613 : typid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid,
1022 : : PointerGetDatum(typname),
1023 : : ObjectIdGetDatum(namespaceId));
8611 tgl@sss.pgh.pa.us 1024 [ + + ]: 267613 : if (OidIsValid(typid))
1025 : 157448 : return typid;
1026 : : }
1027 : :
1028 : : /* Not found in path */
1029 : 23290 : return InvalidOid;
1030 : : }
1031 : :
1032 : : /*
1033 : : * TypeIsVisible
1034 : : * Determine whether a type (identified by OID) is visible in the
1035 : : * current search path. Visible means "would be found by searching
1036 : : * for the unqualified type name".
1037 : : */
1038 : : bool
8580 1039 : 238974 : TypeIsVisible(Oid typid)
1040 : : {
744 1041 : 238974 : return TypeIsVisibleExt(typid, NULL);
1042 : : }
1043 : :
1044 : : /*
1045 : : * TypeIsVisibleExt
1046 : : * As above, but if the type isn't found and is_missing is not NULL,
1047 : : * then set *is_missing = true and return false instead of throwing
1048 : : * an error. (Caller must initialize *is_missing = false.)
1049 : : */
1050 : : static bool
1051 : 240975 : TypeIsVisibleExt(Oid typid, bool *is_missing)
1052 : : {
1053 : : HeapTuple typtup;
1054 : : Form_pg_type typform;
1055 : : Oid typnamespace;
1056 : : bool visible;
1057 : :
5734 rhaas@postgresql.org 1058 : 240975 : typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
8580 tgl@sss.pgh.pa.us 1059 [ - + ]: 240975 : if (!HeapTupleIsValid(typtup))
1060 : : {
744 tgl@sss.pgh.pa.us 1061 [ # # ]:UBC 0 : if (is_missing != NULL)
1062 : : {
1063 : 0 : *is_missing = true;
1064 : 0 : return false;
1065 : : }
8134 1066 [ # # ]: 0 : elog(ERROR, "cache lookup failed for type %u", typid);
1067 : : }
8580 tgl@sss.pgh.pa.us 1068 :CBC 240975 : typform = (Form_pg_type) GETSTRUCT(typtup);
1069 : :
8564 1070 : 240975 : recomputeNamespacePath();
1071 : :
1072 : : /*
1073 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
1074 : : * the system namespace are surely in the path and so we needn't even do
1075 : : * list_member_oid() for them.
1076 : : */
8580 1077 : 240975 : typnamespace = typform->typnamespace;
1078 [ + + ]: 240975 : if (typnamespace != PG_CATALOG_NAMESPACE &&
6793 1079 [ + + ]: 48435 : !list_member_oid(activeSearchPath, typnamespace))
8580 1080 : 7623 : visible = false;
1081 : : else
1082 : : {
1083 : : /*
1084 : : * If it is in the path, it might still not be visible; it could be
1085 : : * hidden by another type of the same name earlier in the path. So we
1086 : : * must do a slow check for conflicting types.
1087 : : */
1088 : 233352 : char *typname = NameStr(typform->typname);
1089 : : ListCell *l;
1090 : :
7326 1091 : 233352 : visible = false;
6793 1092 [ + - + - : 303827 : foreach(l, activeSearchPath)
+ - ]
1093 : : {
7326 1094 : 303827 : Oid namespaceId = lfirst_oid(l);
1095 : :
1096 [ + + ]: 303827 : if (namespaceId == typnamespace)
1097 : : {
1098 : : /* Found it first in path */
1099 : 233346 : visible = true;
1100 : 233346 : break;
1101 : : }
5734 rhaas@postgresql.org 1102 [ + + ]: 70481 : if (SearchSysCacheExists2(TYPENAMENSP,
1103 : : PointerGetDatum(typname),
1104 : : ObjectIdGetDatum(namespaceId)))
1105 : : {
1106 : : /* Found something else first in path */
7326 tgl@sss.pgh.pa.us 1107 : 6 : break;
1108 : : }
1109 : : }
1110 : : }
1111 : :
8580 1112 : 240975 : ReleaseSysCache(typtup);
1113 : :
1114 : 240975 : return visible;
1115 : : }
1116 : :
1117 : :
1118 : : /*
1119 : : * FuncnameGetCandidates
1120 : : * Given a possibly-qualified routine name, argument count, and arg names,
1121 : : * retrieve a list of the possible matches.
1122 : : *
1123 : : * If nargs is -1, we return all routines matching the given name,
1124 : : * regardless of argument count. (argnames must be NIL, and expand_variadic
1125 : : * and expand_defaults must be false, in this case.)
1126 : : *
1127 : : * If argnames isn't NIL, we are considering a named- or mixed-notation call,
1128 : : * and only routines having all the listed argument names will be returned.
1129 : : * (We assume that length(argnames) <= nargs and all the passed-in names are
1130 : : * distinct.) The returned structs will include an argnumbers array showing
1131 : : * the actual argument index for each logical argument position.
1132 : : *
1133 : : * If expand_variadic is true, then variadic functions having the same number
1134 : : * or fewer arguments will be retrieved, with the variadic argument and any
1135 : : * additional argument positions filled with the variadic element type.
1136 : : * nvargs in the returned struct is set to the number of such arguments.
1137 : : * If expand_variadic is false, variadic arguments are not treated specially,
1138 : : * and the returned nvargs will always be zero.
1139 : : *
1140 : : * If expand_defaults is true, functions that could match after insertion of
1141 : : * default argument values will also be retrieved. In this case the returned
1142 : : * structs could have nargs > passed-in nargs, and ndargs is set to the number
1143 : : * of additional args (which can be retrieved from the function's
1144 : : * proargdefaults entry).
1145 : : *
1146 : : * If include_out_arguments is true, then OUT-mode arguments are considered to
1147 : : * be included in the argument list. Their types are included in the returned
1148 : : * arrays, and argnumbers are indexes in proallargtypes not proargtypes.
1149 : : * We also set nominalnargs to be the length of proallargtypes not proargtypes.
1150 : : * Otherwise OUT-mode arguments are ignored.
1151 : : *
1152 : : * It is not possible for nvargs and ndargs to both be nonzero in the same
1153 : : * list entry, since default insertion allows matches to functions with more
1154 : : * than nargs arguments while the variadic transformation requires the same
1155 : : * number or less.
1156 : : *
1157 : : * When argnames isn't NIL, the returned args[] type arrays are not ordered
1158 : : * according to the functions' declarations, but rather according to the call:
1159 : : * first any positional arguments, then the named arguments, then defaulted
1160 : : * arguments (if needed and allowed by expand_defaults). The argnumbers[]
1161 : : * array can be used to map this back to the catalog information.
1162 : : * argnumbers[k] is set to the proargtypes or proallargtypes index of the
1163 : : * k'th call argument.
1164 : : *
1165 : : * We search a single namespace if the function name is qualified, else
1166 : : * all namespaces in the search path. In the multiple-namespace case,
1167 : : * we arrange for entries in earlier namespaces to mask identical entries in
1168 : : * later namespaces.
1169 : : *
1170 : : * When expanding variadics, we arrange for non-variadic functions to mask
1171 : : * variadic ones if the expanded argument list is the same. It is still
1172 : : * possible for there to be conflicts between different variadic functions,
1173 : : * however.
1174 : : *
1175 : : * It is guaranteed that the return list will never contain multiple entries
1176 : : * with identical argument lists. When expand_defaults is true, the entries
1177 : : * could have more than nargs positions, but we still guarantee that they are
1178 : : * distinct in the first nargs positions. However, if argnames isn't NIL or
1179 : : * either expand_variadic or expand_defaults is true, there might be multiple
1180 : : * candidate functions that expand to identical argument lists. Rather than
1181 : : * throw error here, we report such situations by returning a single entry
1182 : : * with oid = 0 that represents a set of such conflicting candidates.
1183 : : * The caller might end up discarding such an entry anyway, but if it selects
1184 : : * such an entry it should react as though the call were ambiguous.
1185 : : *
1186 : : * We return an empty list (NULL) if no suitable matches can be found.
1187 : : * If the function name was schema-qualified with a schema that does not
1188 : : * exist, then we return an empty list if missing_ok is true and otherwise
1189 : : * throw an error. (missing_ok does not affect the behavior otherwise.)
1190 : : *
1191 : : * The output argument *fgc_flags is filled with a bitmask indicating how
1192 : : * far we were able to match the supplied information. This is not of much
1193 : : * interest if any candidates were found, but if not, it can help callers
1194 : : * produce an on-point error message.
1195 : : */
1196 : : FuncCandidateList
5863 1197 : 277962 : FuncnameGetCandidates(List *names, int nargs, List *argnames,
1198 : : bool expand_variadic, bool expand_defaults,
1199 : : bool include_out_arguments, bool missing_ok,
1200 : : int *fgc_flags)
1201 : : {
8605 1202 : 277962 : FuncCandidateList resultList = NULL;
6157 1203 : 277962 : bool any_special = false;
1204 : : char *schemaname;
1205 : : char *funcname;
1206 : : Oid namespaceId;
1207 : : CatCList *catlist;
1208 : : int i;
1209 : :
1210 : : /* check for caller error */
1211 [ + + - + ]: 277962 : Assert(nargs >= 0 || !(expand_variadic | expand_defaults));
1212 : :
1213 : : /* initialize output fgc_flags to empty */
41 tgl@sss.pgh.pa.us 1214 :GNC 277962 : *fgc_flags = 0;
1215 : :
1216 : : /* deconstruct the name list */
8491 tgl@sss.pgh.pa.us 1217 :CBC 277962 : DeconstructQualifiedName(names, &schemaname, &funcname);
1218 : :
8605 1219 [ + + ]: 277944 : if (schemaname)
1220 : : {
1221 : : /* use exact schema given */
41 tgl@sss.pgh.pa.us 1222 :GNC 72924 : *fgc_flags |= FGC_SCHEMA_GIVEN; /* report that a schema is given */
4295 alvherre@alvh.no-ip. 1223 :CBC 72924 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
1224 [ + + ]: 72924 : if (!OidIsValid(namespaceId))
1225 : 24 : return NULL;
41 tgl@sss.pgh.pa.us 1226 :GNC 72900 : *fgc_flags |= FGC_SCHEMA_EXISTS; /* report that the schema exists */
1227 : : }
1228 : : else
1229 : : {
1230 : : /* flag to indicate we need namespace search */
8605 tgl@sss.pgh.pa.us 1231 :CBC 205020 : namespaceId = InvalidOid;
8582 1232 : 205020 : recomputeNamespacePath();
1233 : : }
1234 : :
1235 : : /* Search syscache by name only */
5734 rhaas@postgresql.org 1236 : 277920 : catlist = SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(funcname));
1237 : :
8605 tgl@sss.pgh.pa.us 1238 [ + + ]: 864459 : for (i = 0; i < catlist->n_members; i++)
1239 : : {
1240 : 586539 : HeapTuple proctup = &catlist->members[i]->tuple;
1241 : 586539 : Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
1600 1242 : 586539 : Oid *proargtypes = procform->proargtypes.values;
7517 1243 : 586539 : int pronargs = procform->pronargs;
1244 : : int effective_nargs;
8605 1245 : 586539 : int pathpos = 0;
1246 : : bool variadic;
1247 : : bool use_defaults;
1248 : : Oid va_elem_type;
5863 1249 : 586539 : int *argnumbers = NULL;
1250 : : FuncCandidateList newResult;
1251 : :
41 tgl@sss.pgh.pa.us 1252 :GNC 586539 : *fgc_flags |= FGC_NAME_EXISTS; /* the name is present in pg_proc */
1253 : :
8605 tgl@sss.pgh.pa.us 1254 [ + + ]:CBC 586539 : if (OidIsValid(namespaceId))
1255 : : {
1256 : : /* Consider only procs in specified namespace */
1257 [ + + ]: 143317 : if (procform->pronamespace != namespaceId)
1258 : 147615 : continue;
1259 : : }
1260 : : else
1261 : : {
1262 : : /*
1263 : : * Consider only procs that are in the search path and are not in
1264 : : * the temp namespace.
1265 : : */
1266 : : ListCell *nsp;
1267 : :
6793 1268 [ + - + + : 577129 : foreach(nsp, activeSearchPath)
+ + ]
1269 : : {
6765 1270 [ + + ]: 575511 : if (procform->pronamespace == lfirst_oid(nsp) &&
1271 [ + + ]: 441622 : procform->pronamespace != myTempNamespace)
8564 1272 : 441604 : break;
1273 : 133907 : pathpos++;
1274 : : }
7824 neilc@samurai.com 1275 [ + + ]: 443222 : if (nsp == NULL)
8564 tgl@sss.pgh.pa.us 1276 : 1618 : continue; /* proc is not in search path */
1277 : : }
1278 : :
41 tgl@sss.pgh.pa.us 1279 :GNC 580052 : *fgc_flags |= FGC_NAME_VISIBLE; /* routine is in the right schema */
1280 : :
1281 : : /*
1282 : : * If we are asked to match to OUT arguments, then use the
1283 : : * proallargtypes array (which includes those); otherwise use
1284 : : * proargtypes (which doesn't). Of course, if proallargtypes is null,
1285 : : * we always use proargtypes.
1286 : : */
1600 tgl@sss.pgh.pa.us 1287 [ + + ]:CBC 580052 : if (include_out_arguments)
1288 : : {
1289 : : Datum proallargtypes;
1290 : : bool isNull;
1291 : :
1292 : 358 : proallargtypes = SysCacheGetAttr(PROCNAMEARGSNSP, proctup,
1293 : : Anum_pg_proc_proallargtypes,
1294 : : &isNull);
1295 [ + + ]: 358 : if (!isNull)
1296 : : {
1297 : 111 : ArrayType *arr = DatumGetArrayTypeP(proallargtypes);
1298 : :
1299 : 111 : pronargs = ARR_DIMS(arr)[0];
1300 [ + - + - ]: 111 : if (ARR_NDIM(arr) != 1 ||
1301 : 111 : pronargs < 0 ||
1302 [ + - ]: 111 : ARR_HASNULL(arr) ||
1303 [ - + ]: 111 : ARR_ELEMTYPE(arr) != OIDOID)
1600 tgl@sss.pgh.pa.us 1304 [ # # ]:UBC 0 : elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1600 tgl@sss.pgh.pa.us 1305 [ - + ]:CBC 111 : Assert(pronargs >= procform->pronargs);
1306 [ - + ]: 111 : proargtypes = (Oid *) ARR_DATA_PTR(arr);
1307 : : }
1308 : : }
1309 : :
5863 1310 [ + + ]: 580052 : if (argnames != NIL)
1311 : : {
1312 : : /*
1313 : : * Call uses named or mixed notation
1314 : : *
1315 : : * Check argument count.
1316 : : */
5722 bruce@momjian.us 1317 [ - + ]: 16145 : Assert(nargs >= 0); /* -1 not supported with argnames */
1318 : :
5863 tgl@sss.pgh.pa.us 1319 [ + + + - ]: 16145 : if (pronargs > nargs && expand_defaults)
1320 : : {
1321 : : /* Ignore if not enough default expressions */
1322 [ + + ]: 7066 : if (nargs + procform->pronargdefaults < pronargs)
1323 : 3236 : continue;
1324 : 3830 : use_defaults = true;
1325 : : }
1326 : : else
1327 : 9079 : use_defaults = false;
1328 : :
1329 : : /* Ignore if it doesn't match requested argument count */
1330 [ + + + + ]: 12909 : if (pronargs != nargs && !use_defaults)
1331 : 4696 : continue;
1332 : :
1333 : : /* We found a routine with a suitable number of arguments */
41 tgl@sss.pgh.pa.us 1334 :GNC 8213 : *fgc_flags |= FGC_ARGCOUNT_MATCH;
1335 : :
1336 : : /* Check for argument name match, generate positional mapping */
5863 tgl@sss.pgh.pa.us 1337 [ + + ]:CBC 8213 : if (!MatchNamedCall(proctup, nargs, argnames,
1338 : : include_out_arguments, pronargs,
1339 : : &argnumbers, fgc_flags))
41 tgl@sss.pgh.pa.us 1340 :GNC 15 : continue;
1341 : :
1342 : : /*
1343 : : * Named or mixed notation can match a variadic function only if
1344 : : * expand_variadic is off; otherwise there is no way to match the
1345 : : * presumed-nameless parameters expanded from the variadic array.
1346 : : * However, we postpone the check until here because we want to
1347 : : * perform argument name matching anyway (using the variadic array
1348 : : * argument's name). This allows us to give an on-point error
1349 : : * message if the user forgets to say VARIADIC in what would have
1350 : : * been a valid call with it.
1351 : : */
1352 [ + + + + ]: 8198 : if (OidIsValid(procform->provariadic) && expand_variadic)
5863 tgl@sss.pgh.pa.us 1353 :CBC 3 : continue;
41 tgl@sss.pgh.pa.us 1354 :GNC 8195 : va_elem_type = InvalidOid;
1355 : 8195 : variadic = false;
1356 : :
1357 : : /* We found a fully-valid call using argument names */
1358 : 8195 : *fgc_flags |= FGC_ARGNAMES_VALID;
1359 : :
1360 : : /* Named argument matching is always "special" */
5863 tgl@sss.pgh.pa.us 1361 :CBC 8195 : any_special = true;
1362 : : }
1363 : : else
1364 : : {
1365 : : /*
1366 : : * Call uses positional notation
1367 : : *
1368 : : * Check if function is variadic, and get variadic element type if
1369 : : * so. If expand_variadic is false, we should just ignore
1370 : : * variadic-ness.
1371 : : */
1372 [ + + + + ]: 563907 : if (pronargs <= nargs && expand_variadic)
1373 : : {
1374 : 350003 : va_elem_type = procform->provariadic;
1375 : 350003 : variadic = OidIsValid(va_elem_type);
1376 : 350003 : any_special |= variadic;
1377 : : }
1378 : : else
1379 : : {
1380 : 213904 : va_elem_type = InvalidOid;
1381 : 213904 : variadic = false;
1382 : : }
1383 : :
1384 : : /*
1385 : : * Check if function can match by using parameter defaults.
1386 : : */
1387 [ + + + + ]: 563907 : if (pronargs > nargs && expand_defaults)
1388 : : {
1389 : : /* Ignore if not enough default expressions */
1390 [ + + ]: 102441 : if (nargs + procform->pronargdefaults < pronargs)
1391 : 99256 : continue;
1392 : 3185 : use_defaults = true;
1393 : 3185 : any_special = true;
1394 : : }
1395 : : else
1396 : 461466 : use_defaults = false;
1397 : :
1398 : : /* Ignore if it doesn't match requested argument count */
1399 [ + + + + : 464651 : if (nargs >= 0 && pronargs != nargs && !variadic && !use_defaults)
+ + + + ]
1400 : 32868 : continue;
1401 : :
1402 : : /* We found a routine with a suitable number of arguments */
41 tgl@sss.pgh.pa.us 1403 :GNC 431783 : *fgc_flags |= FGC_ARGCOUNT_MATCH;
1404 : : }
1405 : :
1406 : : /*
1407 : : * We must compute the effective argument list so that we can easily
1408 : : * compare it to earlier results. We waste a palloc cycle if it gets
1409 : : * masked by an earlier result, but really that's a pretty infrequent
1410 : : * case so it's not worth worrying about.
1411 : : */
6312 tgl@sss.pgh.pa.us 1412 :CBC 439978 : effective_nargs = Max(pronargs, nargs);
1413 : : newResult = (FuncCandidateList)
3902 1414 : 439978 : palloc(offsetof(struct _FuncCandidateList, args) +
1415 : : effective_nargs * sizeof(Oid));
6312 1416 : 439978 : newResult->pathpos = pathpos;
2533 andres@anarazel.de 1417 : 439978 : newResult->oid = procform->oid;
1600 tgl@sss.pgh.pa.us 1418 : 439978 : newResult->nominalnargs = pronargs;
6312 1419 : 439978 : newResult->nargs = effective_nargs;
5863 1420 : 439978 : newResult->argnumbers = argnumbers;
1421 [ + + ]: 439978 : if (argnumbers)
1422 : : {
1423 : : /* Re-order the argument types into call's logical order */
1118 drowley@postgresql.o 1424 [ + + ]: 41134 : for (int j = 0; j < pronargs; j++)
1425 : 32939 : newResult->args[j] = proargtypes[argnumbers[j]];
1426 : : }
1427 : : else
1428 : : {
1429 : : /* Simple positional case, just copy proargtypes as-is */
1600 tgl@sss.pgh.pa.us 1430 : 431783 : memcpy(newResult->args, proargtypes, pronargs * sizeof(Oid));
1431 : : }
6312 1432 [ + + ]: 439978 : if (variadic)
1433 : : {
1434 : 5666 : newResult->nvargs = effective_nargs - pronargs + 1;
1435 : : /* Expand variadic argument into N copies of element type */
1118 drowley@postgresql.o 1436 [ + + ]: 42722 : for (int j = pronargs - 1; j < effective_nargs; j++)
1437 : 37056 : newResult->args[j] = va_elem_type;
1438 : : }
1439 : : else
6312 tgl@sss.pgh.pa.us 1440 : 434312 : newResult->nvargs = 0;
6157 1441 [ + + ]: 439978 : newResult->ndargs = use_defaults ? pronargs - nargs : 0;
1442 : :
1443 : : /*
1444 : : * Does it have the same arguments as something we already accepted?
1445 : : * If so, decide what to do to avoid returning duplicate argument
1446 : : * lists. We can skip this check for the single-namespace case if no
1447 : : * special (named, variadic or defaults) match has been made, since
1448 : : * then the unique index on pg_proc guarantees all the matches have
1449 : : * different argument lists.
1450 : : */
1451 [ + + + + ]: 439978 : if (resultList != NULL &&
1452 [ + + ]: 163515 : (any_special || !OidIsValid(namespaceId)))
1453 : : {
1454 : : /*
1455 : : * If we have an ordered list from SearchSysCacheList (the normal
1456 : : * case), then any conflicting proc must immediately adjoin this
1457 : : * one in the list, so we only need to look at the newest result
1458 : : * item. If we have an unordered list, we have to scan the whole
1459 : : * result list. Also, if either the current candidate or any
1460 : : * previous candidate is a special match, we can't assume that
1461 : : * conflicts are adjacent.
1462 : : *
1463 : : * We ignore defaulted arguments in deciding what is a match.
1464 : : */
1465 : : FuncCandidateList prevResult;
1466 : :
1467 [ + - + + ]: 143534 : if (catlist->ordered && !any_special)
1468 : : {
1469 : : /* ndargs must be 0 if !any_special */
1470 [ + + ]: 142291 : if (effective_nargs == resultList->nargs &&
1471 : 142231 : memcmp(newResult->args,
1472 [ + + ]: 142231 : resultList->args,
1473 : : effective_nargs * sizeof(Oid)) == 0)
1474 : 4 : prevResult = resultList;
1475 : : else
1476 : 142287 : prevResult = NULL;
1477 : : }
1478 : : else
1479 : : {
5982 bruce@momjian.us 1480 : 1243 : int cmp_nargs = newResult->nargs - newResult->ndargs;
1481 : :
6157 tgl@sss.pgh.pa.us 1482 : 1243 : for (prevResult = resultList;
1483 [ + + ]: 1457 : prevResult;
1484 : 214 : prevResult = prevResult->next)
1485 : : {
1486 [ + - ]: 1303 : if (cmp_nargs == prevResult->nargs - prevResult->ndargs &&
6312 1487 : 1303 : memcmp(newResult->args,
6157 1488 [ + + ]: 1303 : prevResult->args,
1489 : : cmp_nargs * sizeof(Oid)) == 0)
1490 : 1089 : break;
1491 : : }
1492 : : }
1493 : :
1494 [ + + ]: 143534 : if (prevResult)
1495 : : {
1496 : : /*
1497 : : * We have a match with a previous result. Decide which one
1498 : : * to keep, or mark it ambiguous if we can't decide. The
1499 : : * logic here is preference > 0 means prefer the old result,
1500 : : * preference < 0 means prefer the new, preference = 0 means
1501 : : * ambiguous.
1502 : : */
1503 : : int preference;
1504 : :
1505 [ + + ]: 1093 : if (pathpos != prevResult->pathpos)
1506 : : {
1507 : : /*
1508 : : * Prefer the one that's earlier in the search path.
1509 : : */
1510 : 1 : preference = pathpos - prevResult->pathpos;
1511 : : }
1512 [ + + + - ]: 1092 : else if (variadic && prevResult->nvargs == 0)
1513 : : {
1514 : : /*
1515 : : * With variadic functions we could have, for example,
1516 : : * both foo(numeric) and foo(variadic numeric[]) in the
1517 : : * same namespace; if so we prefer the non-variadic match
1518 : : * on efficiency grounds.
1519 : : */
1520 : 1032 : preference = 1;
1521 : : }
1522 [ + - + + ]: 60 : else if (!variadic && prevResult->nvargs > 0)
1523 : : {
1524 : 39 : preference = -1;
1525 : : }
1526 : : else
1527 : : {
1528 : : /*----------
1529 : : * We can't decide. This can happen with, for example,
1530 : : * both foo(numeric, variadic numeric[]) and
1531 : : * foo(variadic numeric[]) in the same namespace, or
1532 : : * both foo(int) and foo (int, int default something)
1533 : : * in the same namespace, or both foo(a int, b text)
1534 : : * and foo(b text, a int) in the same namespace.
1535 : : *----------
1536 : : */
1537 : 21 : preference = 0;
1538 : : }
1539 : :
1540 [ + + ]: 1093 : if (preference > 0)
1541 : : {
1542 : : /* keep previous result */
1543 : 1033 : pfree(newResult);
1544 : 1033 : continue;
1545 : : }
1546 [ + + ]: 60 : else if (preference < 0)
1547 : : {
1548 : : /* remove previous result from the list */
1549 [ + - ]: 39 : if (prevResult == resultList)
1550 : 39 : resultList = prevResult->next;
1551 : : else
1552 : : {
1553 : : FuncCandidateList prevPrevResult;
1554 : :
6157 tgl@sss.pgh.pa.us 1555 :UBC 0 : for (prevPrevResult = resultList;
1556 [ # # ]: 0 : prevPrevResult;
1557 : 0 : prevPrevResult = prevPrevResult->next)
1558 : : {
1559 [ # # ]: 0 : if (prevResult == prevPrevResult->next)
1560 : : {
1561 : 0 : prevPrevResult->next = prevResult->next;
1562 : 0 : break;
1563 : : }
1564 : : }
5982 bruce@momjian.us 1565 [ # # ]: 0 : Assert(prevPrevResult); /* assert we found it */
1566 : : }
6157 tgl@sss.pgh.pa.us 1567 :CBC 39 : pfree(prevResult);
1568 : : /* fall through to add newResult to list */
1569 : : }
1570 : : else
1571 : : {
1572 : : /* mark old result as ambiguous, discard new */
1573 : 21 : prevResult->oid = InvalidOid;
6312 1574 : 21 : pfree(newResult);
6157 1575 : 21 : continue;
1576 : : }
1577 : : }
1578 : : }
1579 : :
1580 : : /*
1581 : : * Okay to add it to result list
1582 : : */
8605 1583 : 438924 : newResult->next = resultList;
1584 : 438924 : resultList = newResult;
1585 : : }
1586 : :
1587 : 277920 : ReleaseSysCacheList(catlist);
1588 : :
8595 1589 : 277920 : return resultList;
1590 : : }
1591 : :
1592 : : /*
1593 : : * MatchNamedCall
1594 : : * Given a pg_proc heap tuple and a call's list of argument names,
1595 : : * check whether the function could match the call.
1596 : : *
1597 : : * The call could match if all supplied argument names are accepted by
1598 : : * the function, in positions after the last positional argument, and there
1599 : : * are defaults for all unsupplied arguments.
1600 : : *
1601 : : * If include_out_arguments is true, we are treating OUT arguments as
1602 : : * included in the argument list. pronargs is the number of arguments
1603 : : * we're considering (the length of either proargtypes or proallargtypes).
1604 : : *
1605 : : * The number of positional arguments is nargs - list_length(argnames).
1606 : : * Note caller has already done basic checks on argument count.
1607 : : *
1608 : : * On match, return true and fill *argnumbers with a palloc'd array showing
1609 : : * the mapping from call argument positions to actual function argument
1610 : : * numbers. Defaulted arguments are included in this map, at positions
1611 : : * after the last supplied argument.
1612 : : *
1613 : : * We also add flag bits to *fgc_flags reporting on how far the match got.
1614 : : */
1615 : : static bool
5863 1616 : 8213 : MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
1617 : : bool include_out_arguments, int pronargs,
1618 : : int **argnumbers, int *fgc_flags)
1619 : : {
1620 : 8213 : Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
1621 : 8213 : int numposargs = nargs - list_length(argnames);
1622 : : int pronallargs;
1623 : : Oid *p_argtypes;
1624 : : char **p_argnames;
1625 : : char *p_argmodes;
1626 : : bool arggiven[FUNC_MAX_ARGS];
41 tgl@sss.pgh.pa.us 1627 :GNC 8213 : bool arg_filled_twice = false;
1628 : : bool isnull;
1629 : : int ap; /* call args position */
1630 : : int pp; /* proargs position */
1631 : : ListCell *lc;
1632 : :
5863 tgl@sss.pgh.pa.us 1633 [ - + ]:CBC 8213 : Assert(argnames != NIL);
1634 [ - + ]: 8213 : Assert(numposargs >= 0);
1635 [ - + ]: 8213 : Assert(nargs <= pronargs);
1636 : :
1637 : : /* Ignore this function if its proargnames is null */
1638 : 8213 : (void) SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_proargnames,
1639 : : &isnull);
1640 [ - + ]: 8213 : if (isnull)
5863 tgl@sss.pgh.pa.us 1641 :UBC 0 : return false;
1642 : :
1643 : : /* OK, let's extract the argument names and types */
5863 tgl@sss.pgh.pa.us 1644 :CBC 8213 : pronallargs = get_func_arg_info(proctup,
1645 : : &p_argtypes, &p_argnames, &p_argmodes);
1646 [ - + ]: 8213 : Assert(p_argnames != NULL);
1647 : :
1600 1648 [ + + - + ]: 8213 : Assert(include_out_arguments ? (pronargs == pronallargs) : (pronargs <= pronallargs));
1649 : :
1650 : : /* initialize state for matching */
5863 1651 : 8213 : *argnumbers = (int *) palloc(pronargs * sizeof(int));
1652 : 8213 : memset(arggiven, false, pronargs * sizeof(bool));
1653 : :
1654 : : /* there are numposargs positional args before the named args */
1655 [ + + ]: 9623 : for (ap = 0; ap < numposargs; ap++)
1656 : : {
1657 : 1410 : (*argnumbers)[ap] = ap;
1658 : 1410 : arggiven[ap] = true;
1659 : : }
1660 : :
1661 : : /* now examine the named args */
1662 [ + - + + : 31946 : foreach(lc, argnames)
+ + ]
1663 : : {
5722 bruce@momjian.us 1664 : 23739 : char *argname = (char *) lfirst(lc);
1665 : : bool found;
1666 : : int i;
1667 : :
5863 tgl@sss.pgh.pa.us 1668 : 23739 : pp = 0;
1669 : 23739 : found = false;
1670 [ + + ]: 54920 : for (i = 0; i < pronallargs; i++)
1671 : : {
1672 : : /* consider only input params, except with include_out_arguments */
1600 1673 [ + + + + ]: 54914 : if (!include_out_arguments &&
1674 : 36702 : p_argmodes &&
5863 1675 [ + + ]: 36702 : (p_argmodes[i] != FUNC_PARAM_IN &&
1676 [ + - ]: 42 : p_argmodes[i] != FUNC_PARAM_INOUT &&
1677 [ + + ]: 42 : p_argmodes[i] != FUNC_PARAM_VARIADIC))
1678 : 24 : continue;
1679 [ + - + + ]: 54890 : if (p_argnames[i] && strcmp(p_argnames[i], argname) == 0)
1680 : : {
1681 : : /* note if argname matches a positional argument */
1682 [ + + ]: 23733 : if (arggiven[pp])
41 tgl@sss.pgh.pa.us 1683 :GNC 6 : arg_filled_twice = true;
5863 tgl@sss.pgh.pa.us 1684 :CBC 23733 : arggiven[pp] = true;
1685 : 23733 : (*argnumbers)[ap] = pp;
1686 : 23733 : found = true;
1687 : 23733 : break;
1688 : : }
1689 : : /* increase pp only for considered parameters */
1690 : 31157 : pp++;
1691 : : }
1692 : : /* if name isn't in proargnames, fail */
1693 [ + + ]: 23739 : if (!found)
1694 : 6 : return false;
1695 : 23733 : ap++;
1696 : : }
1697 : :
1698 [ - + ]: 8207 : Assert(ap == nargs); /* processed all actual parameters */
1699 : :
1700 : : /* If we get here, the function did match all the supplied argnames */
41 tgl@sss.pgh.pa.us 1701 :GNC 8207 : *fgc_flags |= FGC_ARGNAMES_MATCH;
1702 : :
1703 : : /* ... however, some of them might have been placed wrong */
1704 [ + + ]: 8207 : if (arg_filled_twice)
1705 : 6 : return false; /* some argname matched a positional argument */
1706 : :
1707 : : /* If we get here, the call doesn't have invalid mixed notation */
1708 : 8201 : *fgc_flags |= FGC_ARGNAMES_NONDUP;
1709 : :
1710 : : /* Check for default arguments */
5863 tgl@sss.pgh.pa.us 1711 [ + + ]:CBC 8201 : if (nargs < pronargs)
1712 : : {
5722 bruce@momjian.us 1713 : 3824 : int first_arg_with_default = pronargs - procform->pronargdefaults;
1714 : :
5863 tgl@sss.pgh.pa.us 1715 [ + + ]: 25114 : for (pp = numposargs; pp < pronargs; pp++)
1716 : : {
1717 [ + + ]: 21293 : if (arggiven[pp])
1718 : 13464 : continue;
1719 : : /* fail if arg not given and no default available */
1720 [ + + ]: 7829 : if (pp < first_arg_with_default)
1721 : 3 : return false;
1722 : 7826 : (*argnumbers)[ap++] = pp;
1723 : : }
1724 : : }
1725 : :
1726 [ - + ]: 8198 : Assert(ap == pronargs); /* processed all function parameters */
1727 : :
1728 : : /* If we get here, the call supplies all the required arguments */
41 tgl@sss.pgh.pa.us 1729 :GNC 8198 : *fgc_flags |= FGC_ARGNAMES_ALL;
1730 : :
5863 tgl@sss.pgh.pa.us 1731 :CBC 8198 : return true;
1732 : : }
1733 : :
1734 : : /*
1735 : : * FunctionIsVisible
1736 : : * Determine whether a function (identified by OID) is visible in the
1737 : : * current search path. Visible means "would be found by searching
1738 : : * for the unqualified function name with exact argument matches".
1739 : : */
1740 : : bool
8580 1741 : 13992 : FunctionIsVisible(Oid funcid)
1742 : : {
744 1743 : 13992 : return FunctionIsVisibleExt(funcid, NULL);
1744 : : }
1745 : :
1746 : : /*
1747 : : * FunctionIsVisibleExt
1748 : : * As above, but if the function isn't found and is_missing is not NULL,
1749 : : * then set *is_missing = true and return false instead of throwing
1750 : : * an error. (Caller must initialize *is_missing = false.)
1751 : : */
1752 : : static bool
1753 : 17790 : FunctionIsVisibleExt(Oid funcid, bool *is_missing)
1754 : : {
1755 : : HeapTuple proctup;
1756 : : Form_pg_proc procform;
1757 : : Oid pronamespace;
1758 : : bool visible;
1759 : :
5734 rhaas@postgresql.org 1760 : 17790 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
8580 tgl@sss.pgh.pa.us 1761 [ - + ]: 17790 : if (!HeapTupleIsValid(proctup))
1762 : : {
744 tgl@sss.pgh.pa.us 1763 [ # # ]:UBC 0 : if (is_missing != NULL)
1764 : : {
1765 : 0 : *is_missing = true;
1766 : 0 : return false;
1767 : : }
8134 1768 [ # # ]: 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
1769 : : }
8580 tgl@sss.pgh.pa.us 1770 :CBC 17790 : procform = (Form_pg_proc) GETSTRUCT(proctup);
1771 : :
8564 1772 : 17790 : recomputeNamespacePath();
1773 : :
1774 : : /*
1775 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
1776 : : * the system namespace are surely in the path and so we needn't even do
1777 : : * list_member_oid() for them.
1778 : : */
8580 1779 : 17790 : pronamespace = procform->pronamespace;
1780 [ + + ]: 17790 : if (pronamespace != PG_CATALOG_NAMESPACE &&
6793 1781 [ + + ]: 7199 : !list_member_oid(activeSearchPath, pronamespace))
8580 1782 : 393 : visible = false;
1783 : : else
1784 : : {
1785 : : /*
1786 : : * If it is in the path, it might still not be visible; it could be
1787 : : * hidden by another proc of the same name and arguments earlier in
1788 : : * the path. So we must do a slow check to see if this is the same
1789 : : * proc that would be found by FuncnameGetCandidates.
1790 : : */
1791 : 17397 : char *proname = NameStr(procform->proname);
1792 : 17397 : int nargs = procform->pronargs;
1793 : : FuncCandidateList clist;
1794 : : int fgc_flags;
1795 : :
1796 : 17397 : visible = false;
1797 : :
6312 1798 : 17397 : clist = FuncnameGetCandidates(list_make1(makeString(proname)),
1799 : : nargs, NIL, false, false, false, false,
1800 : : &fgc_flags);
1801 : :
8580 1802 [ + + ]: 21988 : for (; clist; clist = clist->next)
1803 : : {
7517 1804 [ + + ]: 21982 : if (memcmp(clist->args, procform->proargtypes.values,
1805 : : nargs * sizeof(Oid)) == 0)
1806 : : {
1807 : : /* Found the expected entry; is it the right proc? */
8580 1808 : 17391 : visible = (clist->oid == funcid);
1809 : 17391 : break;
1810 : : }
1811 : : }
1812 : : }
1813 : :
1814 : 17790 : ReleaseSysCache(proctup);
1815 : :
1816 : 17790 : return visible;
1817 : : }
1818 : :
1819 : :
1820 : : /*
1821 : : * OpernameGetOprid
1822 : : * Given a possibly-qualified operator name and exact input datatypes,
1823 : : * look up the operator. Returns InvalidOid if not found.
1824 : : *
1825 : : * Pass oprleft = InvalidOid for a prefix op.
1826 : : *
1827 : : * If the operator name is not schema-qualified, it is sought in the current
1828 : : * namespace search path. If the name is schema-qualified and the given
1829 : : * schema does not exist, InvalidOid is returned.
1830 : : */
1831 : : Oid
7119 1832 : 46571 : OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
1833 : : {
1834 : : char *schemaname;
1835 : : char *opername;
1836 : : CatCList *catlist;
1837 : : ListCell *l;
1838 : :
1839 : : /* deconstruct the name list */
1840 : 46571 : DeconstructQualifiedName(names, &schemaname, &opername);
1841 : :
1842 [ + + ]: 46571 : if (schemaname)
1843 : : {
1844 : : /* search only in exact schema given */
1845 : : Oid namespaceId;
1846 : :
4295 alvherre@alvh.no-ip. 1847 : 1465 : namespaceId = LookupExplicitNamespace(schemaname, true);
1848 [ + + ]: 1465 : if (OidIsValid(namespaceId))
1849 : : {
1850 : : HeapTuple opertup;
1851 : :
1852 : 1453 : opertup = SearchSysCache4(OPERNAMENSP,
1853 : : CStringGetDatum(opername),
1854 : : ObjectIdGetDatum(oprleft),
1855 : : ObjectIdGetDatum(oprright),
1856 : : ObjectIdGetDatum(namespaceId));
1857 [ + + ]: 1453 : if (HeapTupleIsValid(opertup))
1858 : : {
2533 andres@anarazel.de 1859 : 852 : Form_pg_operator operclass = (Form_pg_operator) GETSTRUCT(opertup);
1860 : 852 : Oid result = operclass->oid;
1861 : :
4295 alvherre@alvh.no-ip. 1862 : 852 : ReleaseSysCache(opertup);
1863 : 852 : return result;
1864 : : }
1865 : : }
1866 : :
7119 tgl@sss.pgh.pa.us 1867 : 613 : return InvalidOid;
1868 : : }
1869 : :
1870 : : /* Search syscache by name and argument types */
5734 rhaas@postgresql.org 1871 : 45106 : catlist = SearchSysCacheList3(OPERNAMENSP,
1872 : : CStringGetDatum(opername),
1873 : : ObjectIdGetDatum(oprleft),
1874 : : ObjectIdGetDatum(oprright));
1875 : :
7119 tgl@sss.pgh.pa.us 1876 [ + + ]: 45106 : if (catlist->n_members == 0)
1877 : : {
1878 : : /* no hope, fall out early */
1879 : 9246 : ReleaseSysCacheList(catlist);
1880 : 9246 : return InvalidOid;
1881 : : }
1882 : :
1883 : : /*
1884 : : * We have to find the list member that is first in the search path, if
1885 : : * there's more than one. This doubly-nested loop looks ugly, but in
1886 : : * practice there should usually be few catlist members.
1887 : : */
1888 : 35860 : recomputeNamespacePath();
1889 : :
6793 1890 [ + - + + : 42576 : foreach(l, activeSearchPath)
+ + ]
1891 : : {
7119 1892 : 42570 : Oid namespaceId = lfirst_oid(l);
1893 : : int i;
1894 : :
6765 1895 [ + + ]: 42570 : if (namespaceId == myTempNamespace)
1896 : 4484 : continue; /* do not look in temp namespace */
1897 : :
7119 1898 [ + + ]: 40336 : for (i = 0; i < catlist->n_members; i++)
1899 : : {
1900 : 38104 : HeapTuple opertup = &catlist->members[i]->tuple;
1901 : 38104 : Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
1902 : :
1903 [ + + ]: 38104 : if (operform->oprnamespace == namespaceId)
1904 : : {
2533 andres@anarazel.de 1905 : 35854 : Oid result = operform->oid;
1906 : :
7119 tgl@sss.pgh.pa.us 1907 : 35854 : ReleaseSysCacheList(catlist);
1908 : 35854 : return result;
1909 : : }
1910 : : }
1911 : : }
1912 : :
1913 : 6 : ReleaseSysCacheList(catlist);
1914 : 6 : return InvalidOid;
1915 : : }
1916 : :
1917 : : /*
1918 : : * OpernameGetCandidates
1919 : : * Given a possibly-qualified operator name and operator kind,
1920 : : * retrieve a list of the possible matches.
1921 : : *
1922 : : * If oprkind is '\0', we return all operators matching the given name,
1923 : : * regardless of arguments.
1924 : : *
1925 : : * We search a single namespace if the operator name is qualified, else
1926 : : * all namespaces in the search path. The return list will never contain
1927 : : * multiple entries with identical argument lists --- in the multiple-
1928 : : * namespace case, we arrange for entries in earlier namespaces to mask
1929 : : * identical entries in later namespaces.
1930 : : *
1931 : : * The returned items always have two args[] entries --- the first will be
1932 : : * InvalidOid for a prefix oprkind. nargs is always 2, too.
1933 : : *
1934 : : * We return an empty list (NULL) if no suitable matches can be found. If the
1935 : : * operator name was schema-qualified with a schema that does not exist, then
1936 : : * we return an empty list if missing_schema_ok is true and otherwise throw an
1937 : : * error. (missing_schema_ok does not affect the behavior otherwise.)
1938 : : *
1939 : : * The output argument *fgc_flags is filled with a bitmask indicating how
1940 : : * far we were able to match the supplied information. This is not of much
1941 : : * interest if any candidates were found, but if not, it can help callers
1942 : : * produce an on-point error message.
1943 : : */
1944 : : FuncCandidateList
41 tgl@sss.pgh.pa.us 1945 :GNC 9299 : OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok,
1946 : : int *fgc_flags)
1947 : : {
8595 tgl@sss.pgh.pa.us 1948 :CBC 9299 : FuncCandidateList resultList = NULL;
7973 1949 : 9299 : char *resultSpace = NULL;
1950 : 9299 : int nextResult = 0;
1951 : : char *schemaname;
1952 : : char *opername;
1953 : : Oid namespaceId;
1954 : : CatCList *catlist;
1955 : : int i;
1956 : :
1957 : : /* initialize output fgc_flags to empty */
41 tgl@sss.pgh.pa.us 1958 :GNC 9299 : *fgc_flags = 0;
1959 : :
1960 : : /* deconstruct the name list */
8491 tgl@sss.pgh.pa.us 1961 :CBC 9299 : DeconstructQualifiedName(names, &schemaname, &opername);
1962 : :
8595 1963 [ + + ]: 9299 : if (schemaname)
1964 : : {
1965 : : /* use exact schema given */
41 tgl@sss.pgh.pa.us 1966 :GNC 611 : *fgc_flags |= FGC_SCHEMA_GIVEN; /* report that a schema is given */
4220 rhaas@postgresql.org 1967 :CBC 611 : namespaceId = LookupExplicitNamespace(schemaname, missing_schema_ok);
41 tgl@sss.pgh.pa.us 1968 [ + + ]:GNC 611 : if (!OidIsValid(namespaceId))
4220 rhaas@postgresql.org 1969 :CBC 9 : return NULL;
41 tgl@sss.pgh.pa.us 1970 :GNC 602 : *fgc_flags |= FGC_SCHEMA_EXISTS; /* report that the schema exists */
1971 : : }
1972 : : else
1973 : : {
1974 : : /* flag to indicate we need namespace search */
8595 tgl@sss.pgh.pa.us 1975 :CBC 8688 : namespaceId = InvalidOid;
8582 1976 : 8688 : recomputeNamespacePath();
1977 : : }
1978 : :
1979 : : /* Search syscache by name only */
5734 rhaas@postgresql.org 1980 : 9290 : catlist = SearchSysCacheList1(OPERNAMENSP, CStringGetDatum(opername));
1981 : :
1982 : : /*
1983 : : * In typical scenarios, most if not all of the operators found by the
1984 : : * catcache search will end up getting returned; and there can be quite a
1985 : : * few, for common operator names such as '=' or '+'. To reduce the time
1986 : : * spent in palloc, we allocate the result space as an array large enough
1987 : : * to hold all the operators. The original coding of this routine did a
1988 : : * separate palloc for each operator, but profiling revealed that the
1989 : : * pallocs used an unreasonably large fraction of parsing time.
1990 : : */
1991 : : #define SPACE_PER_OP MAXALIGN(offsetof(struct _FuncCandidateList, args) + \
1992 : : 2 * sizeof(Oid))
1993 : :
7973 tgl@sss.pgh.pa.us 1994 [ + + ]: 9290 : if (catlist->n_members > 0)
1995 : 9281 : resultSpace = palloc(catlist->n_members * SPACE_PER_OP);
1996 : :
8595 1997 [ + + ]: 431771 : for (i = 0; i < catlist->n_members; i++)
1998 : : {
1999 : 422481 : HeapTuple opertup = &catlist->members[i]->tuple;
2000 : 422481 : Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
2001 : 422481 : int pathpos = 0;
2002 : : FuncCandidateList newResult;
2003 : :
2004 : : /* Ignore operators of wrong kind, if specific kind requested */
8586 2005 [ + + + + ]: 422481 : if (oprkind && operform->oprkind != oprkind)
8595 2006 : 5034 : continue;
2007 : :
41 tgl@sss.pgh.pa.us 2008 :GNC 417447 : *fgc_flags |= FGC_NAME_EXISTS; /* the name is present in pg_operator */
2009 : :
8595 tgl@sss.pgh.pa.us 2010 [ + + ]:CBC 417447 : if (OidIsValid(namespaceId))
2011 : : {
2012 : : /* Consider only opers in specified namespace */
2013 [ - + ]: 11808 : if (operform->oprnamespace != namespaceId)
8595 tgl@sss.pgh.pa.us 2014 :UBC 0 : continue;
2015 : : /* No need to check args, they must all be different */
2016 : : }
2017 : : else
2018 : : {
2019 : : /*
2020 : : * Consider only opers that are in the search path and are not in
2021 : : * the temp namespace.
2022 : : */
2023 : : ListCell *nsp;
2024 : :
6793 tgl@sss.pgh.pa.us 2025 [ + - + + :CBC 452914 : foreach(nsp, activeSearchPath)
+ + ]
2026 : : {
6765 2027 [ + + ]: 452473 : if (operform->oprnamespace == lfirst_oid(nsp) &&
2028 [ + - ]: 405198 : operform->oprnamespace != myTempNamespace)
8564 2029 : 405198 : break;
2030 : 47275 : pathpos++;
2031 : : }
7824 neilc@samurai.com 2032 [ + + ]: 405639 : if (nsp == NULL)
8564 tgl@sss.pgh.pa.us 2033 : 441 : continue; /* oper is not in search path */
2034 : :
2035 : : /*
2036 : : * Okay, it's in the search path, but does it have the same
2037 : : * arguments as something we already accepted? If so, keep only
2038 : : * the one that appears earlier in the search path.
2039 : : *
2040 : : * If we have an ordered list from SearchSysCacheList (the normal
2041 : : * case), then any conflicting oper must immediately adjoin this
2042 : : * one in the list, so we only need to look at the newest result
2043 : : * item. If we have an unordered list, we have to scan the whole
2044 : : * result list.
2045 : : */
8595 2046 [ + + ]: 405198 : if (resultList)
2047 : : {
2048 : : FuncCandidateList prevResult;
2049 : :
2050 [ + - ]: 396522 : if (catlist->ordered)
2051 : : {
2052 [ + + ]: 396522 : if (operform->oprleft == resultList->args[0] &&
2053 [ - + ]: 114328 : operform->oprright == resultList->args[1])
8595 tgl@sss.pgh.pa.us 2054 :UBC 0 : prevResult = resultList;
2055 : : else
8595 tgl@sss.pgh.pa.us 2056 :CBC 396522 : prevResult = NULL;
2057 : : }
2058 : : else
2059 : : {
8595 tgl@sss.pgh.pa.us 2060 :UBC 0 : for (prevResult = resultList;
2061 [ # # ]: 0 : prevResult;
2062 : 0 : prevResult = prevResult->next)
2063 : : {
2064 [ # # ]: 0 : if (operform->oprleft == prevResult->args[0] &&
2065 [ # # ]: 0 : operform->oprright == prevResult->args[1])
2066 : 0 : break;
2067 : : }
2068 : : }
8595 tgl@sss.pgh.pa.us 2069 [ - + ]:CBC 396522 : if (prevResult)
2070 : : {
2071 : : /* We have a match with a previous result */
8595 tgl@sss.pgh.pa.us 2072 [ # # ]:UBC 0 : Assert(pathpos != prevResult->pathpos);
2073 [ # # ]: 0 : if (pathpos > prevResult->pathpos)
3050 2074 : 0 : continue; /* keep previous result */
2075 : : /* replace previous result */
8595 2076 : 0 : prevResult->pathpos = pathpos;
2533 andres@anarazel.de 2077 : 0 : prevResult->oid = operform->oid;
8595 tgl@sss.pgh.pa.us 2078 : 0 : continue; /* args are same, of course */
2079 : : }
2080 : : }
2081 : : }
2082 : :
41 tgl@sss.pgh.pa.us 2083 :GNC 417006 : *fgc_flags |= FGC_NAME_VISIBLE; /* operator is in the right schema */
2084 : :
2085 : : /*
2086 : : * Okay to add it to result list
2087 : : */
7973 tgl@sss.pgh.pa.us 2088 :CBC 417006 : newResult = (FuncCandidateList) (resultSpace + nextResult);
2089 : 417006 : nextResult += SPACE_PER_OP;
2090 : :
8595 2091 : 417006 : newResult->pathpos = pathpos;
2533 andres@anarazel.de 2092 : 417006 : newResult->oid = operform->oid;
1600 tgl@sss.pgh.pa.us 2093 : 417006 : newResult->nominalnargs = 2;
8586 2094 : 417006 : newResult->nargs = 2;
6312 2095 : 417006 : newResult->nvargs = 0;
6157 2096 : 417006 : newResult->ndargs = 0;
5863 2097 : 417006 : newResult->argnumbers = NULL;
8595 2098 : 417006 : newResult->args[0] = operform->oprleft;
2099 : 417006 : newResult->args[1] = operform->oprright;
2100 : 417006 : newResult->next = resultList;
2101 : 417006 : resultList = newResult;
2102 : : }
2103 : :
2104 : 9290 : ReleaseSysCacheList(catlist);
2105 : :
8605 2106 : 9290 : return resultList;
2107 : : }
2108 : :
2109 : : /*
2110 : : * OperatorIsVisible
2111 : : * Determine whether an operator (identified by OID) is visible in the
2112 : : * current search path. Visible means "would be found by searching
2113 : : * for the unqualified operator name with exact argument matches".
2114 : : */
2115 : : bool
8580 2116 : 1877 : OperatorIsVisible(Oid oprid)
2117 : : {
744 2118 : 1877 : return OperatorIsVisibleExt(oprid, NULL);
2119 : : }
2120 : :
2121 : : /*
2122 : : * OperatorIsVisibleExt
2123 : : * As above, but if the operator isn't found and is_missing is not NULL,
2124 : : * then set *is_missing = true and return false instead of throwing
2125 : : * an error. (Caller must initialize *is_missing = false.)
2126 : : */
2127 : : static bool
2128 : 2727 : OperatorIsVisibleExt(Oid oprid, bool *is_missing)
2129 : : {
2130 : : HeapTuple oprtup;
2131 : : Form_pg_operator oprform;
2132 : : Oid oprnamespace;
2133 : : bool visible;
2134 : :
5734 rhaas@postgresql.org 2135 : 2727 : oprtup = SearchSysCache1(OPEROID, ObjectIdGetDatum(oprid));
8580 tgl@sss.pgh.pa.us 2136 [ - + ]: 2727 : if (!HeapTupleIsValid(oprtup))
2137 : : {
744 tgl@sss.pgh.pa.us 2138 [ # # ]:UBC 0 : if (is_missing != NULL)
2139 : : {
2140 : 0 : *is_missing = true;
2141 : 0 : return false;
2142 : : }
8134 2143 [ # # ]: 0 : elog(ERROR, "cache lookup failed for operator %u", oprid);
2144 : : }
8580 tgl@sss.pgh.pa.us 2145 :CBC 2727 : oprform = (Form_pg_operator) GETSTRUCT(oprtup);
2146 : :
8564 2147 : 2727 : recomputeNamespacePath();
2148 : :
2149 : : /*
2150 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2151 : : * the system namespace are surely in the path and so we needn't even do
2152 : : * list_member_oid() for them.
2153 : : */
8580 2154 : 2727 : oprnamespace = oprform->oprnamespace;
2155 [ + + ]: 2727 : if (oprnamespace != PG_CATALOG_NAMESPACE &&
6793 2156 [ + + ]: 770 : !list_member_oid(activeSearchPath, oprnamespace))
8580 2157 : 153 : visible = false;
2158 : : else
2159 : : {
2160 : : /*
2161 : : * If it is in the path, it might still not be visible; it could be
2162 : : * hidden by another operator of the same name and arguments earlier
2163 : : * in the path. So we must do a slow check to see if this is the same
2164 : : * operator that would be found by OpernameGetOprid.
2165 : : */
2166 : 2574 : char *oprname = NameStr(oprform->oprname);
2167 : :
7119 2168 : 2574 : visible = (OpernameGetOprid(list_make1(makeString(oprname)),
2169 : : oprform->oprleft, oprform->oprright)
2170 : : == oprid);
2171 : : }
2172 : :
8580 2173 : 2727 : ReleaseSysCache(oprtup);
2174 : :
2175 : 2727 : return visible;
2176 : : }
2177 : :
2178 : :
2179 : : /*
2180 : : * OpclassnameGetOpcid
2181 : : * Try to resolve an unqualified index opclass name.
2182 : : * Returns OID if opclass found in search path, else InvalidOid.
2183 : : *
2184 : : * This is essentially the same as TypenameGetTypid, but we have to have
2185 : : * an extra argument for the index AM OID.
2186 : : */
2187 : : Oid
2188 : 11392 : OpclassnameGetOpcid(Oid amid, const char *opcname)
2189 : : {
2190 : : Oid opcid;
2191 : : ListCell *l;
2192 : :
2193 : 11392 : recomputeNamespacePath();
2194 : :
6793 2195 [ + - + + : 11945 : foreach(l, activeSearchPath)
+ + ]
2196 : : {
7824 neilc@samurai.com 2197 : 11933 : Oid namespaceId = lfirst_oid(l);
2198 : :
6765 tgl@sss.pgh.pa.us 2199 [ + + ]: 11933 : if (namespaceId == myTempNamespace)
2200 : 226 : continue; /* do not look in temp namespace */
2201 : :
2533 andres@anarazel.de 2202 : 11707 : opcid = GetSysCacheOid3(CLAAMNAMENSP, Anum_pg_opclass_oid,
2203 : : ObjectIdGetDatum(amid),
2204 : : PointerGetDatum(opcname),
2205 : : ObjectIdGetDatum(namespaceId));
8580 tgl@sss.pgh.pa.us 2206 [ + + ]: 11707 : if (OidIsValid(opcid))
2207 : 11380 : return opcid;
2208 : : }
2209 : :
2210 : : /* Not found in path */
2211 : 12 : return InvalidOid;
2212 : : }
2213 : :
2214 : : /*
2215 : : * OpclassIsVisible
2216 : : * Determine whether an opclass (identified by OID) is visible in the
2217 : : * current search path. Visible means "would be found by searching
2218 : : * for the unqualified opclass name".
2219 : : */
2220 : : bool
2221 : 429 : OpclassIsVisible(Oid opcid)
2222 : : {
744 2223 : 429 : return OpclassIsVisibleExt(opcid, NULL);
2224 : : }
2225 : :
2226 : : /*
2227 : : * OpclassIsVisibleExt
2228 : : * As above, but if the opclass isn't found and is_missing is not NULL,
2229 : : * then set *is_missing = true and return false instead of throwing
2230 : : * an error. (Caller must initialize *is_missing = false.)
2231 : : */
2232 : : static bool
2233 : 438 : OpclassIsVisibleExt(Oid opcid, bool *is_missing)
2234 : : {
2235 : : HeapTuple opctup;
2236 : : Form_pg_opclass opcform;
2237 : : Oid opcnamespace;
2238 : : bool visible;
2239 : :
5734 rhaas@postgresql.org 2240 : 438 : opctup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opcid));
8580 tgl@sss.pgh.pa.us 2241 [ - + ]: 438 : if (!HeapTupleIsValid(opctup))
2242 : : {
744 tgl@sss.pgh.pa.us 2243 [ # # ]:UBC 0 : if (is_missing != NULL)
2244 : : {
2245 : 0 : *is_missing = true;
2246 : 0 : return false;
2247 : : }
8134 2248 [ # # ]: 0 : elog(ERROR, "cache lookup failed for opclass %u", opcid);
2249 : : }
8580 tgl@sss.pgh.pa.us 2250 :CBC 438 : opcform = (Form_pg_opclass) GETSTRUCT(opctup);
2251 : :
8564 2252 : 438 : recomputeNamespacePath();
2253 : :
2254 : : /*
2255 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2256 : : * the system namespace are surely in the path and so we needn't even do
2257 : : * list_member_oid() for them.
2258 : : */
8580 2259 : 438 : opcnamespace = opcform->opcnamespace;
2260 [ + + ]: 438 : if (opcnamespace != PG_CATALOG_NAMESPACE &&
6793 2261 [ + + ]: 152 : !list_member_oid(activeSearchPath, opcnamespace))
8580 2262 : 14 : visible = false;
2263 : : else
2264 : : {
2265 : : /*
2266 : : * If it is in the path, it might still not be visible; it could be
2267 : : * hidden by another opclass of the same name earlier in the path. So
2268 : : * we must do a slow check to see if this opclass would be found by
2269 : : * OpclassnameGetOpcid.
2270 : : */
2271 : 424 : char *opcname = NameStr(opcform->opcname);
2272 : :
6883 2273 : 424 : visible = (OpclassnameGetOpcid(opcform->opcmethod, opcname) == opcid);
2274 : : }
2275 : :
8580 2276 : 438 : ReleaseSysCache(opctup);
2277 : :
2278 : 438 : return visible;
2279 : : }
2280 : :
2281 : : /*
2282 : : * OpfamilynameGetOpfid
2283 : : * Try to resolve an unqualified index opfamily name.
2284 : : * Returns OID if opfamily found in search path, else InvalidOid.
2285 : : *
2286 : : * This is essentially the same as TypenameGetTypid, but we have to have
2287 : : * an extra argument for the index AM OID.
2288 : : */
2289 : : Oid
6883 2290 : 2710 : OpfamilynameGetOpfid(Oid amid, const char *opfname)
2291 : : {
2292 : : Oid opfid;
2293 : : ListCell *l;
2294 : :
2295 : 2710 : recomputeNamespacePath();
2296 : :
6793 2297 [ + - + + : 6723 : foreach(l, activeSearchPath)
+ + ]
2298 : : {
6883 2299 : 6717 : Oid namespaceId = lfirst_oid(l);
2300 : :
6765 2301 [ + + ]: 6717 : if (namespaceId == myTempNamespace)
2302 : 1535 : continue; /* do not look in temp namespace */
2303 : :
2533 andres@anarazel.de 2304 : 5182 : opfid = GetSysCacheOid3(OPFAMILYAMNAMENSP, Anum_pg_opfamily_oid,
2305 : : ObjectIdGetDatum(amid),
2306 : : PointerGetDatum(opfname),
2307 : : ObjectIdGetDatum(namespaceId));
6883 tgl@sss.pgh.pa.us 2308 [ + + ]: 5182 : if (OidIsValid(opfid))
2309 : 2704 : return opfid;
2310 : : }
2311 : :
2312 : : /* Not found in path */
2313 : 6 : return InvalidOid;
2314 : : }
2315 : :
2316 : : /*
2317 : : * OpfamilyIsVisible
2318 : : * Determine whether an opfamily (identified by OID) is visible in the
2319 : : * current search path. Visible means "would be found by searching
2320 : : * for the unqualified opfamily name".
2321 : : */
2322 : : bool
2323 : 2051 : OpfamilyIsVisible(Oid opfid)
2324 : : {
744 2325 : 2051 : return OpfamilyIsVisibleExt(opfid, NULL);
2326 : : }
2327 : :
2328 : : /*
2329 : : * OpfamilyIsVisibleExt
2330 : : * As above, but if the opfamily isn't found and is_missing is not NULL,
2331 : : * then set *is_missing = true and return false instead of throwing
2332 : : * an error. (Caller must initialize *is_missing = false.)
2333 : : */
2334 : : static bool
2335 : 2210 : OpfamilyIsVisibleExt(Oid opfid, bool *is_missing)
2336 : : {
2337 : : HeapTuple opftup;
2338 : : Form_pg_opfamily opfform;
2339 : : Oid opfnamespace;
2340 : : bool visible;
2341 : :
5734 rhaas@postgresql.org 2342 : 2210 : opftup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
6883 tgl@sss.pgh.pa.us 2343 [ - + ]: 2210 : if (!HeapTupleIsValid(opftup))
2344 : : {
744 tgl@sss.pgh.pa.us 2345 [ # # ]:UBC 0 : if (is_missing != NULL)
2346 : : {
2347 : 0 : *is_missing = true;
2348 : 0 : return false;
2349 : : }
6883 2350 [ # # ]: 0 : elog(ERROR, "cache lookup failed for opfamily %u", opfid);
2351 : : }
6883 tgl@sss.pgh.pa.us 2352 :CBC 2210 : opfform = (Form_pg_opfamily) GETSTRUCT(opftup);
2353 : :
2354 : 2210 : recomputeNamespacePath();
2355 : :
2356 : : /*
2357 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2358 : : * the system namespace are surely in the path and so we needn't even do
2359 : : * list_member_oid() for them.
2360 : : */
2361 : 2210 : opfnamespace = opfform->opfnamespace;
2362 [ + + ]: 2210 : if (opfnamespace != PG_CATALOG_NAMESPACE &&
6793 2363 [ + + ]: 2039 : !list_member_oid(activeSearchPath, opfnamespace))
6883 2364 : 98 : visible = false;
2365 : : else
2366 : : {
2367 : : /*
2368 : : * If it is in the path, it might still not be visible; it could be
2369 : : * hidden by another opfamily of the same name earlier in the path. So
2370 : : * we must do a slow check to see if this opfamily would be found by
2371 : : * OpfamilynameGetOpfid.
2372 : : */
2373 : 2112 : char *opfname = NameStr(opfform->opfname);
2374 : :
2375 : 2112 : visible = (OpfamilynameGetOpfid(opfform->opfmethod, opfname) == opfid);
2376 : : }
2377 : :
2378 : 2210 : ReleaseSysCache(opftup);
2379 : :
2380 : 2210 : return visible;
2381 : : }
2382 : :
2383 : : /*
2384 : : * lookup_collation
2385 : : * If there's a collation of the given name/namespace, and it works
2386 : : * with the given encoding, return its OID. Else return InvalidOid.
2387 : : */
2388 : : static Oid
3047 2389 : 6292 : lookup_collation(const char *collname, Oid collnamespace, int32 encoding)
2390 : : {
2391 : : Oid collid;
2392 : : HeapTuple colltup;
2393 : : Form_pg_collation collform;
2394 : :
2395 : : /* Check for encoding-specific entry (exact match) */
2533 andres@anarazel.de 2396 : 6292 : collid = GetSysCacheOid3(COLLNAMEENCNSP, Anum_pg_collation_oid,
2397 : : PointerGetDatum(collname),
2398 : : Int32GetDatum(encoding),
2399 : : ObjectIdGetDatum(collnamespace));
3047 tgl@sss.pgh.pa.us 2400 [ + + ]: 6292 : if (OidIsValid(collid))
2401 : 154 : return collid;
2402 : :
2403 : : /*
2404 : : * Check for any-encoding entry. This takes a bit more work: while libc
2405 : : * collations with collencoding = -1 do work with all encodings, ICU
2406 : : * collations only work with certain encodings, so we have to check that
2407 : : * aspect before deciding it's a match.
2408 : : */
2409 : 6138 : colltup = SearchSysCache3(COLLNAMEENCNSP,
2410 : : PointerGetDatum(collname),
2411 : : Int32GetDatum(-1),
2412 : : ObjectIdGetDatum(collnamespace));
2413 [ + + ]: 6138 : if (!HeapTupleIsValid(colltup))
2414 : 687 : return InvalidOid;
2415 : 5451 : collform = (Form_pg_collation) GETSTRUCT(colltup);
2416 [ + + ]: 5451 : if (collform->collprovider == COLLPROVIDER_ICU)
2417 : : {
2418 [ + - ]: 680 : if (is_encoding_supported_by_icu(encoding))
2533 andres@anarazel.de 2419 : 680 : collid = collform->oid;
2420 : : else
3047 tgl@sss.pgh.pa.us 2421 :UBC 0 : collid = InvalidOid;
2422 : : }
2423 : : else
2424 : : {
2533 andres@anarazel.de 2425 :CBC 4771 : collid = collform->oid;
2426 : : }
3047 tgl@sss.pgh.pa.us 2427 : 5451 : ReleaseSysCache(colltup);
2428 : 5451 : return collid;
2429 : : }
2430 : :
2431 : : /*
2432 : : * CollationGetCollid
2433 : : * Try to resolve an unqualified collation name.
2434 : : * Returns OID if collation found in search path, else InvalidOid.
2435 : : *
2436 : : * Note that this will only find collations that work with the current
2437 : : * database's encoding.
2438 : : */
2439 : : Oid
5375 peter_e@gmx.net 2440 : 192 : CollationGetCollid(const char *collname)
2441 : : {
5344 tgl@sss.pgh.pa.us 2442 : 192 : int32 dbencoding = GetDatabaseEncoding();
2443 : : ListCell *l;
2444 : :
5375 peter_e@gmx.net 2445 : 192 : recomputeNamespacePath();
2446 : :
2447 [ + - + - : 301 : foreach(l, activeSearchPath)
+ - ]
2448 : : {
2449 : 301 : Oid namespaceId = lfirst_oid(l);
2450 : : Oid collid;
2451 : :
2452 [ + + ]: 301 : if (namespaceId == myTempNamespace)
2453 : 74 : continue; /* do not look in temp namespace */
2454 : :
3047 tgl@sss.pgh.pa.us 2455 : 227 : collid = lookup_collation(collname, namespaceId, dbencoding);
5375 peter_e@gmx.net 2456 [ + + ]: 227 : if (OidIsValid(collid))
2457 : 192 : return collid;
2458 : : }
2459 : :
2460 : : /* Not found in path */
5375 peter_e@gmx.net 2461 :UBC 0 : return InvalidOid;
2462 : : }
2463 : :
2464 : : /*
2465 : : * CollationIsVisible
2466 : : * Determine whether a collation (identified by OID) is visible in the
2467 : : * current search path. Visible means "would be found by searching
2468 : : * for the unqualified collation name".
2469 : : *
2470 : : * Note that only collations that work with the current database's encoding
2471 : : * will be considered visible.
2472 : : */
2473 : : bool
5375 peter_e@gmx.net 2474 :CBC 192 : CollationIsVisible(Oid collid)
2475 : : {
744 tgl@sss.pgh.pa.us 2476 : 192 : return CollationIsVisibleExt(collid, NULL);
2477 : : }
2478 : :
2479 : : /*
2480 : : * CollationIsVisibleExt
2481 : : * As above, but if the collation isn't found and is_missing is not NULL,
2482 : : * then set *is_missing = true and return false instead of throwing
2483 : : * an error. (Caller must initialize *is_missing = false.)
2484 : : */
2485 : : static bool
2486 : 192 : CollationIsVisibleExt(Oid collid, bool *is_missing)
2487 : : {
2488 : : HeapTuple colltup;
2489 : : Form_pg_collation collform;
2490 : : Oid collnamespace;
2491 : : bool visible;
2492 : :
5375 peter_e@gmx.net 2493 : 192 : colltup = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
2494 [ - + ]: 192 : if (!HeapTupleIsValid(colltup))
2495 : : {
744 tgl@sss.pgh.pa.us 2496 [ # # ]:UBC 0 : if (is_missing != NULL)
2497 : : {
2498 : 0 : *is_missing = true;
2499 : 0 : return false;
2500 : : }
5375 peter_e@gmx.net 2501 [ # # ]: 0 : elog(ERROR, "cache lookup failed for collation %u", collid);
2502 : : }
5375 peter_e@gmx.net 2503 :CBC 192 : collform = (Form_pg_collation) GETSTRUCT(colltup);
2504 : :
2505 : 192 : recomputeNamespacePath();
2506 : :
2507 : : /*
2508 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2509 : : * the system namespace are surely in the path and so we needn't even do
2510 : : * list_member_oid() for them.
2511 : : */
2512 : 192 : collnamespace = collform->collnamespace;
2513 [ + + ]: 192 : if (collnamespace != PG_CATALOG_NAMESPACE &&
2514 [ - + ]: 35 : !list_member_oid(activeSearchPath, collnamespace))
5375 peter_e@gmx.net 2515 :UBC 0 : visible = false;
2516 : : else
2517 : : {
2518 : : /*
2519 : : * If it is in the path, it might still not be visible; it could be
2520 : : * hidden by another collation of the same name earlier in the path,
2521 : : * or it might not work with the current DB encoding. So we must do a
2522 : : * slow check to see if this collation would be found by
2523 : : * CollationGetCollid.
2524 : : */
5375 peter_e@gmx.net 2525 :CBC 192 : char *collname = NameStr(collform->collname);
2526 : :
2527 : 192 : visible = (CollationGetCollid(collname) == collid);
2528 : : }
2529 : :
2530 : 192 : ReleaseSysCache(colltup);
2531 : :
2532 : 192 : return visible;
2533 : : }
2534 : :
2535 : :
2536 : : /*
2537 : : * ConversionGetConid
2538 : : * Try to resolve an unqualified conversion name.
2539 : : * Returns OID if conversion found in search path, else InvalidOid.
2540 : : *
2541 : : * This is essentially the same as RelnameGetRelid.
2542 : : */
2543 : : Oid
8355 bruce@momjian.us 2544 : 9 : ConversionGetConid(const char *conname)
2545 : : {
2546 : : Oid conid;
2547 : : ListCell *l;
2548 : :
2549 : 9 : recomputeNamespacePath();
2550 : :
6793 tgl@sss.pgh.pa.us 2551 [ + - + - : 18 : foreach(l, activeSearchPath)
+ - ]
2552 : : {
7824 neilc@samurai.com 2553 : 18 : Oid namespaceId = lfirst_oid(l);
2554 : :
6765 tgl@sss.pgh.pa.us 2555 [ - + ]: 18 : if (namespaceId == myTempNamespace)
6765 tgl@sss.pgh.pa.us 2556 :UBC 0 : continue; /* do not look in temp namespace */
2557 : :
2533 andres@anarazel.de 2558 :CBC 18 : conid = GetSysCacheOid2(CONNAMENSP, Anum_pg_conversion_oid,
2559 : : PointerGetDatum(conname),
2560 : : ObjectIdGetDatum(namespaceId));
8355 bruce@momjian.us 2561 [ + + ]: 18 : if (OidIsValid(conid))
2562 : 9 : return conid;
2563 : : }
2564 : :
2565 : : /* Not found in path */
8355 bruce@momjian.us 2566 :UBC 0 : return InvalidOid;
2567 : : }
2568 : :
2569 : : /*
2570 : : * ConversionIsVisible
2571 : : * Determine whether a conversion (identified by OID) is visible in the
2572 : : * current search path. Visible means "would be found by searching
2573 : : * for the unqualified conversion name".
2574 : : */
2575 : : bool
8355 bruce@momjian.us 2576 :CBC 15 : ConversionIsVisible(Oid conid)
2577 : : {
744 tgl@sss.pgh.pa.us 2578 : 15 : return ConversionIsVisibleExt(conid, NULL);
2579 : : }
2580 : :
2581 : : /*
2582 : : * ConversionIsVisibleExt
2583 : : * As above, but if the conversion isn't found and is_missing is not NULL,
2584 : : * then set *is_missing = true and return false instead of throwing
2585 : : * an error. (Caller must initialize *is_missing = false.)
2586 : : */
2587 : : static bool
2588 : 15 : ConversionIsVisibleExt(Oid conid, bool *is_missing)
2589 : : {
2590 : : HeapTuple contup;
2591 : : Form_pg_conversion conform;
2592 : : Oid connamespace;
2593 : : bool visible;
2594 : :
5734 rhaas@postgresql.org 2595 : 15 : contup = SearchSysCache1(CONVOID, ObjectIdGetDatum(conid));
8355 bruce@momjian.us 2596 [ - + ]: 15 : if (!HeapTupleIsValid(contup))
2597 : : {
744 tgl@sss.pgh.pa.us 2598 [ # # ]:UBC 0 : if (is_missing != NULL)
2599 : : {
2600 : 0 : *is_missing = true;
2601 : 0 : return false;
2602 : : }
8134 2603 [ # # ]: 0 : elog(ERROR, "cache lookup failed for conversion %u", conid);
2604 : : }
8355 bruce@momjian.us 2605 :CBC 15 : conform = (Form_pg_conversion) GETSTRUCT(contup);
2606 : :
2607 : 15 : recomputeNamespacePath();
2608 : :
2609 : : /*
2610 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2611 : : * the system namespace are surely in the path and so we needn't even do
2612 : : * list_member_oid() for them.
2613 : : */
2614 : 15 : connamespace = conform->connamespace;
2615 [ + - ]: 15 : if (connamespace != PG_CATALOG_NAMESPACE &&
6793 tgl@sss.pgh.pa.us 2616 [ + + ]: 15 : !list_member_oid(activeSearchPath, connamespace))
8355 bruce@momjian.us 2617 : 6 : visible = false;
2618 : : else
2619 : : {
2620 : : /*
2621 : : * If it is in the path, it might still not be visible; it could be
2622 : : * hidden by another conversion of the same name earlier in the path.
2623 : : * So we must do a slow check to see if this conversion would be found
2624 : : * by ConversionGetConid.
2625 : : */
2626 : 9 : char *conname = NameStr(conform->conname);
2627 : :
2628 : 9 : visible = (ConversionGetConid(conname) == conid);
2629 : : }
2630 : :
2631 : 15 : ReleaseSysCache(contup);
2632 : :
2633 : 15 : return visible;
2634 : : }
2635 : :
2636 : : /*
2637 : : * get_statistics_object_oid - find a statistics object by possibly qualified name
2638 : : *
2639 : : * If not found, returns InvalidOid if missing_ok, else throws error
2640 : : */
2641 : : Oid
3088 tgl@sss.pgh.pa.us 2642 : 172 : get_statistics_object_oid(List *names, bool missing_ok)
2643 : : {
2644 : : char *schemaname;
2645 : : char *stats_name;
2646 : : Oid namespaceId;
3139 alvherre@alvh.no-ip. 2647 : 172 : Oid stats_oid = InvalidOid;
2648 : : ListCell *l;
2649 : :
2650 : : /* deconstruct the name list */
2651 : 172 : DeconstructQualifiedName(names, &schemaname, &stats_name);
2652 : :
2653 [ + + ]: 172 : if (schemaname)
2654 : : {
2655 : : /* use exact schema given */
2656 : 17 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2657 [ - + - - ]: 17 : if (missing_ok && !OidIsValid(namespaceId))
3139 alvherre@alvh.no-ip. 2658 :UBC 0 : stats_oid = InvalidOid;
2659 : : else
2533 andres@anarazel.de 2660 :CBC 17 : stats_oid = GetSysCacheOid2(STATEXTNAMENSP, Anum_pg_statistic_ext_oid,
2661 : : PointerGetDatum(stats_name),
2662 : : ObjectIdGetDatum(namespaceId));
2663 : : }
2664 : : else
2665 : : {
2666 : : /* search for it in search path */
3139 alvherre@alvh.no-ip. 2667 : 155 : recomputeNamespacePath();
2668 : :
2669 [ + - + + : 416 : foreach(l, activeSearchPath)
+ + ]
2670 : : {
2671 : 407 : namespaceId = lfirst_oid(l);
2672 : :
2673 [ + + ]: 407 : if (namespaceId == myTempNamespace)
2674 : 97 : continue; /* do not look in temp namespace */
2533 andres@anarazel.de 2675 : 310 : stats_oid = GetSysCacheOid2(STATEXTNAMENSP, Anum_pg_statistic_ext_oid,
2676 : : PointerGetDatum(stats_name),
2677 : : ObjectIdGetDatum(namespaceId));
3139 alvherre@alvh.no-ip. 2678 [ + + ]: 310 : if (OidIsValid(stats_oid))
2679 : 146 : break;
2680 : : }
2681 : : }
2682 : :
2683 [ + + + + ]: 172 : if (!OidIsValid(stats_oid) && !missing_ok)
2684 [ + - ]: 6 : ereport(ERROR,
2685 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2686 : : errmsg("statistics object \"%s\" does not exist",
2687 : : NameListToString(names))));
2688 : :
2689 : 166 : return stats_oid;
2690 : : }
2691 : :
2692 : : /*
2693 : : * StatisticsObjIsVisible
2694 : : * Determine whether a statistics object (identified by OID) is visible in
2695 : : * the current search path. Visible means "would be found by searching
2696 : : * for the unqualified statistics object name".
2697 : : */
2698 : : bool
744 tgl@sss.pgh.pa.us 2699 : 176 : StatisticsObjIsVisible(Oid stxid)
2700 : : {
2701 : 176 : return StatisticsObjIsVisibleExt(stxid, NULL);
2702 : : }
2703 : :
2704 : : /*
2705 : : * StatisticsObjIsVisibleExt
2706 : : * As above, but if the statistics object isn't found and is_missing is
2707 : : * not NULL, then set *is_missing = true and return false instead of
2708 : : * throwing an error. (Caller must initialize *is_missing = false.)
2709 : : */
2710 : : static bool
2711 : 362 : StatisticsObjIsVisibleExt(Oid stxid, bool *is_missing)
2712 : : {
2713 : : HeapTuple stxtup;
2714 : : Form_pg_statistic_ext stxform;
2715 : : Oid stxnamespace;
2716 : : bool visible;
2717 : :
2718 : 362 : stxtup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(stxid));
3089 alvherre@alvh.no-ip. 2719 [ - + ]: 362 : if (!HeapTupleIsValid(stxtup))
2720 : : {
744 tgl@sss.pgh.pa.us 2721 [ # # ]:UBC 0 : if (is_missing != NULL)
2722 : : {
2723 : 0 : *is_missing = true;
2724 : 0 : return false;
2725 : : }
2726 [ # # ]: 0 : elog(ERROR, "cache lookup failed for statistics object %u", stxid);
2727 : : }
3089 alvherre@alvh.no-ip. 2728 :CBC 362 : stxform = (Form_pg_statistic_ext) GETSTRUCT(stxtup);
2729 : :
2730 : 362 : recomputeNamespacePath();
2731 : :
2732 : : /*
2733 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2734 : : * the system namespace are surely in the path and so we needn't even do
2735 : : * list_member_oid() for them.
2736 : : */
2737 : 362 : stxnamespace = stxform->stxnamespace;
2738 [ + - ]: 362 : if (stxnamespace != PG_CATALOG_NAMESPACE &&
2739 [ + + ]: 362 : !list_member_oid(activeSearchPath, stxnamespace))
2740 : 39 : visible = false;
2741 : : else
2742 : : {
2743 : : /*
2744 : : * If it is in the path, it might still not be visible; it could be
2745 : : * hidden by another statistics object of the same name earlier in the
2746 : : * path. So we must do a slow check for conflicting objects.
2747 : : */
2748 : 323 : char *stxname = NameStr(stxform->stxname);
2749 : : ListCell *l;
2750 : :
2751 : 323 : visible = false;
2752 [ + - + + : 974 : foreach(l, activeSearchPath)
+ + ]
2753 : : {
2754 : 968 : Oid namespaceId = lfirst_oid(l);
2755 : :
28 noah@leadboat.com 2756 [ + + ]: 968 : if (namespaceId == myTempNamespace)
2757 : 280 : continue; /* do not look in temp namespace */
2758 : :
3089 alvherre@alvh.no-ip. 2759 [ + + ]: 688 : if (namespaceId == stxnamespace)
2760 : : {
2761 : : /* Found it first in path */
2762 : 317 : visible = true;
2763 : 317 : break;
2764 : : }
2765 [ - + ]: 371 : if (SearchSysCacheExists2(STATEXTNAMENSP,
2766 : : PointerGetDatum(stxname),
2767 : : ObjectIdGetDatum(namespaceId)))
2768 : : {
2769 : : /* Found something else first in path */
3089 alvherre@alvh.no-ip. 2770 :UBC 0 : break;
2771 : : }
2772 : : }
2773 : : }
2774 : :
3089 alvherre@alvh.no-ip. 2775 :CBC 362 : ReleaseSysCache(stxtup);
2776 : :
2777 : 362 : return visible;
2778 : : }
2779 : :
2780 : : /*
2781 : : * get_ts_parser_oid - find a TS parser by possibly qualified name
2782 : : *
2783 : : * If not found, returns InvalidOid if missing_ok, else throws error
2784 : : */
2785 : : Oid
5562 rhaas@postgresql.org 2786 : 1456 : get_ts_parser_oid(List *names, bool missing_ok)
2787 : : {
2788 : : char *schemaname;
2789 : : char *parser_name;
2790 : : Oid namespaceId;
6642 tgl@sss.pgh.pa.us 2791 : 1456 : Oid prsoid = InvalidOid;
2792 : : ListCell *l;
2793 : :
2794 : : /* deconstruct the name list */
2795 : 1456 : DeconstructQualifiedName(names, &schemaname, &parser_name);
2796 : :
2797 [ + + ]: 1450 : if (schemaname)
2798 : : {
2799 : : /* use exact schema given */
4657 bruce@momjian.us 2800 : 23 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2801 [ + + + - ]: 23 : if (missing_ok && !OidIsValid(namespaceId))
2802 : 3 : prsoid = InvalidOid;
2803 : : else
2533 andres@anarazel.de 2804 : 20 : prsoid = GetSysCacheOid2(TSPARSERNAMENSP, Anum_pg_ts_parser_oid,
2805 : : PointerGetDatum(parser_name),
2806 : : ObjectIdGetDatum(namespaceId));
2807 : : }
2808 : : else
2809 : : {
2810 : : /* search for it in search path */
6642 tgl@sss.pgh.pa.us 2811 : 1427 : recomputeNamespacePath();
2812 : :
2813 [ + - + + : 1468 : foreach(l, activeSearchPath)
+ + ]
2814 : : {
2815 : 1456 : namespaceId = lfirst_oid(l);
2816 : :
2817 [ - + ]: 1456 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 2818 :UBC 0 : continue; /* do not look in temp namespace */
2819 : :
2533 andres@anarazel.de 2820 :CBC 1456 : prsoid = GetSysCacheOid2(TSPARSERNAMENSP, Anum_pg_ts_parser_oid,
2821 : : PointerGetDatum(parser_name),
2822 : : ObjectIdGetDatum(namespaceId));
6642 tgl@sss.pgh.pa.us 2823 [ + + ]: 1456 : if (OidIsValid(prsoid))
2824 : 1415 : break;
2825 : : }
2826 : : }
2827 : :
5562 rhaas@postgresql.org 2828 [ + + + + ]: 1450 : if (!OidIsValid(prsoid) && !missing_ok)
6642 tgl@sss.pgh.pa.us 2829 [ + - ]: 15 : ereport(ERROR,
2830 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2831 : : errmsg("text search parser \"%s\" does not exist",
2832 : : NameListToString(names))));
2833 : :
2834 : 1435 : return prsoid;
2835 : : }
2836 : :
2837 : : /*
2838 : : * TSParserIsVisible
2839 : : * Determine whether a parser (identified by OID) is visible in the
2840 : : * current search path. Visible means "would be found by searching
2841 : : * for the unqualified parser name".
2842 : : */
2843 : : bool
2844 : 15 : TSParserIsVisible(Oid prsId)
2845 : : {
744 2846 : 15 : return TSParserIsVisibleExt(prsId, NULL);
2847 : : }
2848 : :
2849 : : /*
2850 : : * TSParserIsVisibleExt
2851 : : * As above, but if the parser isn't found and is_missing is not NULL,
2852 : : * then set *is_missing = true and return false instead of throwing
2853 : : * an error. (Caller must initialize *is_missing = false.)
2854 : : */
2855 : : static bool
2856 : 15 : TSParserIsVisibleExt(Oid prsId, bool *is_missing)
2857 : : {
2858 : : HeapTuple tup;
2859 : : Form_pg_ts_parser form;
2860 : : Oid namespace;
2861 : : bool visible;
2862 : :
5734 rhaas@postgresql.org 2863 : 15 : tup = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
6642 tgl@sss.pgh.pa.us 2864 [ - + ]: 15 : if (!HeapTupleIsValid(tup))
2865 : : {
744 tgl@sss.pgh.pa.us 2866 [ # # ]:UBC 0 : if (is_missing != NULL)
2867 : : {
2868 : 0 : *is_missing = true;
2869 : 0 : return false;
2870 : : }
6642 2871 [ # # ]: 0 : elog(ERROR, "cache lookup failed for text search parser %u", prsId);
2872 : : }
6642 tgl@sss.pgh.pa.us 2873 :CBC 15 : form = (Form_pg_ts_parser) GETSTRUCT(tup);
2874 : :
2875 : 15 : recomputeNamespacePath();
2876 : :
2877 : : /*
2878 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
2879 : : * the system namespace are surely in the path and so we needn't even do
2880 : : * list_member_oid() for them.
2881 : : */
2882 : 15 : namespace = form->prsnamespace;
2883 [ + - ]: 15 : if (namespace != PG_CATALOG_NAMESPACE &&
2884 [ + + ]: 15 : !list_member_oid(activeSearchPath, namespace))
2885 : 6 : visible = false;
2886 : : else
2887 : : {
2888 : : /*
2889 : : * If it is in the path, it might still not be visible; it could be
2890 : : * hidden by another parser of the same name earlier in the path. So
2891 : : * we must do a slow check for conflicting parsers.
2892 : : */
2893 : 9 : char *name = NameStr(form->prsname);
2894 : : ListCell *l;
2895 : :
2896 : 9 : visible = false;
2897 [ + - + - : 18 : foreach(l, activeSearchPath)
+ - ]
2898 : : {
2899 : 18 : Oid namespaceId = lfirst_oid(l);
2900 : :
2901 [ - + ]: 18 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 2902 :UBC 0 : continue; /* do not look in temp namespace */
2903 : :
6642 tgl@sss.pgh.pa.us 2904 [ + + ]:CBC 18 : if (namespaceId == namespace)
2905 : : {
2906 : : /* Found it first in path */
2907 : 9 : visible = true;
2908 : 9 : break;
2909 : : }
5734 rhaas@postgresql.org 2910 [ - + ]: 9 : if (SearchSysCacheExists2(TSPARSERNAMENSP,
2911 : : PointerGetDatum(name),
2912 : : ObjectIdGetDatum(namespaceId)))
2913 : : {
2914 : : /* Found something else first in path */
6642 tgl@sss.pgh.pa.us 2915 :UBC 0 : break;
2916 : : }
2917 : : }
2918 : : }
2919 : :
6642 tgl@sss.pgh.pa.us 2920 :CBC 15 : ReleaseSysCache(tup);
2921 : :
2922 : 15 : return visible;
2923 : : }
2924 : :
2925 : : /*
2926 : : * get_ts_dict_oid - find a TS dictionary by possibly qualified name
2927 : : *
2928 : : * If not found, returns InvalidOid if missing_ok, else throws error
2929 : : */
2930 : : Oid
5562 rhaas@postgresql.org 2931 : 6147 : get_ts_dict_oid(List *names, bool missing_ok)
2932 : : {
2933 : : char *schemaname;
2934 : : char *dict_name;
2935 : : Oid namespaceId;
6642 tgl@sss.pgh.pa.us 2936 : 6147 : Oid dictoid = InvalidOid;
2937 : : ListCell *l;
2938 : :
2939 : : /* deconstruct the name list */
2940 : 6147 : DeconstructQualifiedName(names, &schemaname, &dict_name);
2941 : :
2942 [ + + ]: 6141 : if (schemaname)
2943 : : {
2944 : : /* use exact schema given */
4657 bruce@momjian.us 2945 : 52 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
2946 [ + + + - ]: 52 : if (missing_ok && !OidIsValid(namespaceId))
2947 : 3 : dictoid = InvalidOid;
2948 : : else
2533 andres@anarazel.de 2949 : 49 : dictoid = GetSysCacheOid2(TSDICTNAMENSP, Anum_pg_ts_dict_oid,
2950 : : PointerGetDatum(dict_name),
2951 : : ObjectIdGetDatum(namespaceId));
2952 : : }
2953 : : else
2954 : : {
2955 : : /* search for it in search path */
6642 tgl@sss.pgh.pa.us 2956 : 6089 : recomputeNamespacePath();
2957 : :
2958 [ + - + + : 6525 : foreach(l, activeSearchPath)
+ + ]
2959 : : {
2960 : 6510 : namespaceId = lfirst_oid(l);
2961 : :
2962 [ - + ]: 6510 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 2963 :UBC 0 : continue; /* do not look in temp namespace */
2964 : :
2533 andres@anarazel.de 2965 :CBC 6510 : dictoid = GetSysCacheOid2(TSDICTNAMENSP, Anum_pg_ts_dict_oid,
2966 : : PointerGetDatum(dict_name),
2967 : : ObjectIdGetDatum(namespaceId));
6642 tgl@sss.pgh.pa.us 2968 [ + + ]: 6510 : if (OidIsValid(dictoid))
2969 : 6074 : break;
2970 : : }
2971 : : }
2972 : :
5562 rhaas@postgresql.org 2973 [ + + + + ]: 6141 : if (!OidIsValid(dictoid) && !missing_ok)
6642 tgl@sss.pgh.pa.us 2974 [ + - ]: 15 : ereport(ERROR,
2975 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
2976 : : errmsg("text search dictionary \"%s\" does not exist",
2977 : : NameListToString(names))));
2978 : :
2979 : 6126 : return dictoid;
2980 : : }
2981 : :
2982 : : /*
2983 : : * TSDictionaryIsVisible
2984 : : * Determine whether a dictionary (identified by OID) is visible in the
2985 : : * current search path. Visible means "would be found by searching
2986 : : * for the unqualified dictionary name".
2987 : : */
2988 : : bool
2989 : 2953 : TSDictionaryIsVisible(Oid dictId)
2990 : : {
744 2991 : 2953 : return TSDictionaryIsVisibleExt(dictId, NULL);
2992 : : }
2993 : :
2994 : : /*
2995 : : * TSDictionaryIsVisibleExt
2996 : : * As above, but if the dictionary isn't found and is_missing is not NULL,
2997 : : * then set *is_missing = true and return false instead of throwing
2998 : : * an error. (Caller must initialize *is_missing = false.)
2999 : : */
3000 : : static bool
3001 : 2953 : TSDictionaryIsVisibleExt(Oid dictId, bool *is_missing)
3002 : : {
3003 : : HeapTuple tup;
3004 : : Form_pg_ts_dict form;
3005 : : Oid namespace;
3006 : : bool visible;
3007 : :
5734 rhaas@postgresql.org 3008 : 2953 : tup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
6642 tgl@sss.pgh.pa.us 3009 [ - + ]: 2953 : if (!HeapTupleIsValid(tup))
3010 : : {
744 tgl@sss.pgh.pa.us 3011 [ # # ]:UBC 0 : if (is_missing != NULL)
3012 : : {
3013 : 0 : *is_missing = true;
3014 : 0 : return false;
3015 : : }
6642 3016 [ # # ]: 0 : elog(ERROR, "cache lookup failed for text search dictionary %u",
3017 : : dictId);
3018 : : }
6642 tgl@sss.pgh.pa.us 3019 :CBC 2953 : form = (Form_pg_ts_dict) GETSTRUCT(tup);
3020 : :
3021 : 2953 : recomputeNamespacePath();
3022 : :
3023 : : /*
3024 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
3025 : : * the system namespace are surely in the path and so we needn't even do
3026 : : * list_member_oid() for them.
3027 : : */
3028 : 2953 : namespace = form->dictnamespace;
3029 [ + + ]: 2953 : if (namespace != PG_CATALOG_NAMESPACE &&
3030 [ + + ]: 153 : !list_member_oid(activeSearchPath, namespace))
3031 : 141 : visible = false;
3032 : : else
3033 : : {
3034 : : /*
3035 : : * If it is in the path, it might still not be visible; it could be
3036 : : * hidden by another dictionary of the same name earlier in the path.
3037 : : * So we must do a slow check for conflicting dictionaries.
3038 : : */
3039 : 2812 : char *name = NameStr(form->dictname);
3040 : : ListCell *l;
3041 : :
3042 : 2812 : visible = false;
3043 [ + - + - : 2824 : foreach(l, activeSearchPath)
+ - ]
3044 : : {
3045 : 2824 : Oid namespaceId = lfirst_oid(l);
3046 : :
3047 [ - + ]: 2824 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 3048 :UBC 0 : continue; /* do not look in temp namespace */
3049 : :
6642 tgl@sss.pgh.pa.us 3050 [ + + ]:CBC 2824 : if (namespaceId == namespace)
3051 : : {
3052 : : /* Found it first in path */
3053 : 2812 : visible = true;
3054 : 2812 : break;
3055 : : }
5734 rhaas@postgresql.org 3056 [ - + ]: 12 : if (SearchSysCacheExists2(TSDICTNAMENSP,
3057 : : PointerGetDatum(name),
3058 : : ObjectIdGetDatum(namespaceId)))
3059 : : {
3060 : : /* Found something else first in path */
6642 tgl@sss.pgh.pa.us 3061 :UBC 0 : break;
3062 : : }
3063 : : }
3064 : : }
3065 : :
6642 tgl@sss.pgh.pa.us 3066 :CBC 2953 : ReleaseSysCache(tup);
3067 : :
3068 : 2953 : return visible;
3069 : : }
3070 : :
3071 : : /*
3072 : : * get_ts_template_oid - find a TS template by possibly qualified name
3073 : : *
3074 : : * If not found, returns InvalidOid if missing_ok, else throws error
3075 : : */
3076 : : Oid
5562 rhaas@postgresql.org 3077 : 1562 : get_ts_template_oid(List *names, bool missing_ok)
3078 : : {
3079 : : char *schemaname;
3080 : : char *template_name;
3081 : : Oid namespaceId;
6642 tgl@sss.pgh.pa.us 3082 : 1562 : Oid tmploid = InvalidOid;
3083 : : ListCell *l;
3084 : :
3085 : : /* deconstruct the name list */
3086 : 1562 : DeconstructQualifiedName(names, &schemaname, &template_name);
3087 : :
3088 [ + + ]: 1556 : if (schemaname)
3089 : : {
3090 : : /* use exact schema given */
4657 bruce@momjian.us 3091 : 28 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3092 [ + + + - ]: 28 : if (missing_ok && !OidIsValid(namespaceId))
3093 : 3 : tmploid = InvalidOid;
3094 : : else
2533 andres@anarazel.de 3095 : 25 : tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP, Anum_pg_ts_template_oid,
3096 : : PointerGetDatum(template_name),
3097 : : ObjectIdGetDatum(namespaceId));
3098 : : }
3099 : : else
3100 : : {
3101 : : /* search for it in search path */
6642 tgl@sss.pgh.pa.us 3102 : 1528 : recomputeNamespacePath();
3103 : :
3104 [ + - + + : 1570 : foreach(l, activeSearchPath)
+ + ]
3105 : : {
3106 : 1558 : namespaceId = lfirst_oid(l);
3107 : :
3108 [ - + ]: 1558 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 3109 :UBC 0 : continue; /* do not look in temp namespace */
3110 : :
2533 andres@anarazel.de 3111 :CBC 1558 : tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP, Anum_pg_ts_template_oid,
3112 : : PointerGetDatum(template_name),
3113 : : ObjectIdGetDatum(namespaceId));
6642 tgl@sss.pgh.pa.us 3114 [ + + ]: 1558 : if (OidIsValid(tmploid))
3115 : 1516 : break;
3116 : : }
3117 : : }
3118 : :
5562 rhaas@postgresql.org 3119 [ + + + + ]: 1556 : if (!OidIsValid(tmploid) && !missing_ok)
6642 tgl@sss.pgh.pa.us 3120 [ + - ]: 15 : ereport(ERROR,
3121 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3122 : : errmsg("text search template \"%s\" does not exist",
3123 : : NameListToString(names))));
3124 : :
3125 : 1541 : return tmploid;
3126 : : }
3127 : :
3128 : : /*
3129 : : * TSTemplateIsVisible
3130 : : * Determine whether a template (identified by OID) is visible in the
3131 : : * current search path. Visible means "would be found by searching
3132 : : * for the unqualified template name".
3133 : : */
3134 : : bool
3135 : 15 : TSTemplateIsVisible(Oid tmplId)
3136 : : {
744 3137 : 15 : return TSTemplateIsVisibleExt(tmplId, NULL);
3138 : : }
3139 : :
3140 : : /*
3141 : : * TSTemplateIsVisibleExt
3142 : : * As above, but if the template isn't found and is_missing is not NULL,
3143 : : * then set *is_missing = true and return false instead of throwing
3144 : : * an error. (Caller must initialize *is_missing = false.)
3145 : : */
3146 : : static bool
3147 : 15 : TSTemplateIsVisibleExt(Oid tmplId, bool *is_missing)
3148 : : {
3149 : : HeapTuple tup;
3150 : : Form_pg_ts_template form;
3151 : : Oid namespace;
3152 : : bool visible;
3153 : :
5734 rhaas@postgresql.org 3154 : 15 : tup = SearchSysCache1(TSTEMPLATEOID, ObjectIdGetDatum(tmplId));
6642 tgl@sss.pgh.pa.us 3155 [ - + ]: 15 : if (!HeapTupleIsValid(tup))
3156 : : {
744 tgl@sss.pgh.pa.us 3157 [ # # ]:UBC 0 : if (is_missing != NULL)
3158 : : {
3159 : 0 : *is_missing = true;
3160 : 0 : return false;
3161 : : }
6642 3162 [ # # ]: 0 : elog(ERROR, "cache lookup failed for text search template %u", tmplId);
3163 : : }
6642 tgl@sss.pgh.pa.us 3164 :CBC 15 : form = (Form_pg_ts_template) GETSTRUCT(tup);
3165 : :
3166 : 15 : recomputeNamespacePath();
3167 : :
3168 : : /*
3169 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
3170 : : * the system namespace are surely in the path and so we needn't even do
3171 : : * list_member_oid() for them.
3172 : : */
3173 : 15 : namespace = form->tmplnamespace;
3174 [ + - ]: 15 : if (namespace != PG_CATALOG_NAMESPACE &&
3175 [ + + ]: 15 : !list_member_oid(activeSearchPath, namespace))
3176 : 6 : visible = false;
3177 : : else
3178 : : {
3179 : : /*
3180 : : * If it is in the path, it might still not be visible; it could be
3181 : : * hidden by another template of the same name earlier in the path. So
3182 : : * we must do a slow check for conflicting templates.
3183 : : */
3184 : 9 : char *name = NameStr(form->tmplname);
3185 : : ListCell *l;
3186 : :
3187 : 9 : visible = false;
3188 [ + - + - : 18 : foreach(l, activeSearchPath)
+ - ]
3189 : : {
3190 : 18 : Oid namespaceId = lfirst_oid(l);
3191 : :
3192 [ - + ]: 18 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 3193 :UBC 0 : continue; /* do not look in temp namespace */
3194 : :
6642 tgl@sss.pgh.pa.us 3195 [ + + ]:CBC 18 : if (namespaceId == namespace)
3196 : : {
3197 : : /* Found it first in path */
3198 : 9 : visible = true;
3199 : 9 : break;
3200 : : }
5734 rhaas@postgresql.org 3201 [ - + ]: 9 : if (SearchSysCacheExists2(TSTEMPLATENAMENSP,
3202 : : PointerGetDatum(name),
3203 : : ObjectIdGetDatum(namespaceId)))
3204 : : {
3205 : : /* Found something else first in path */
6642 tgl@sss.pgh.pa.us 3206 :UBC 0 : break;
3207 : : }
3208 : : }
3209 : : }
3210 : :
6642 tgl@sss.pgh.pa.us 3211 :CBC 15 : ReleaseSysCache(tup);
3212 : :
3213 : 15 : return visible;
3214 : : }
3215 : :
3216 : : /*
3217 : : * get_ts_config_oid - find a TS config by possibly qualified name
3218 : : *
3219 : : * If not found, returns InvalidOid if missing_ok, else throws error
3220 : : */
3221 : : Oid
5562 rhaas@postgresql.org 3222 : 9577 : get_ts_config_oid(List *names, bool missing_ok)
3223 : : {
3224 : : char *schemaname;
3225 : : char *config_name;
3226 : : Oid namespaceId;
6642 tgl@sss.pgh.pa.us 3227 : 9577 : Oid cfgoid = InvalidOid;
3228 : : ListCell *l;
3229 : :
3230 : : /* deconstruct the name list */
3231 : 9577 : DeconstructQualifiedName(names, &schemaname, &config_name);
3232 : :
3233 [ + + ]: 9571 : if (schemaname)
3234 : : {
3235 : : /* use exact schema given */
4657 bruce@momjian.us 3236 : 2883 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
3237 [ + + + + ]: 2883 : if (missing_ok && !OidIsValid(namespaceId))
3238 : 3 : cfgoid = InvalidOid;
3239 : : else
2533 andres@anarazel.de 3240 : 2880 : cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP, Anum_pg_ts_config_oid,
3241 : : PointerGetDatum(config_name),
3242 : : ObjectIdGetDatum(namespaceId));
3243 : : }
3244 : : else
3245 : : {
3246 : : /* search for it in search path */
6642 tgl@sss.pgh.pa.us 3247 : 6688 : recomputeNamespacePath();
3248 : :
3249 [ + - + + : 7263 : foreach(l, activeSearchPath)
+ + ]
3250 : : {
3251 : 7235 : namespaceId = lfirst_oid(l);
3252 : :
3253 [ + + ]: 7235 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 3254 : 351 : continue; /* do not look in temp namespace */
3255 : :
2533 andres@anarazel.de 3256 : 6884 : cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP, Anum_pg_ts_config_oid,
3257 : : PointerGetDatum(config_name),
3258 : : ObjectIdGetDatum(namespaceId));
6642 tgl@sss.pgh.pa.us 3259 [ + + ]: 6884 : if (OidIsValid(cfgoid))
3260 : 6660 : break;
3261 : : }
3262 : : }
3263 : :
5562 rhaas@postgresql.org 3264 [ + + + + ]: 9571 : if (!OidIsValid(cfgoid) && !missing_ok)
6642 tgl@sss.pgh.pa.us 3265 [ + - ]: 15 : ereport(ERROR,
3266 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3267 : : errmsg("text search configuration \"%s\" does not exist",
3268 : : NameListToString(names))));
3269 : :
3270 : 9556 : return cfgoid;
3271 : : }
3272 : :
3273 : : /*
3274 : : * TSConfigIsVisible
3275 : : * Determine whether a text search configuration (identified by OID)
3276 : : * is visible in the current search path. Visible means "would be found
3277 : : * by searching for the unqualified text search configuration name".
3278 : : */
3279 : : bool
3280 : 23 : TSConfigIsVisible(Oid cfgid)
3281 : : {
744 3282 : 23 : return TSConfigIsVisibleExt(cfgid, NULL);
3283 : : }
3284 : :
3285 : : /*
3286 : : * TSConfigIsVisibleExt
3287 : : * As above, but if the configuration isn't found and is_missing is not
3288 : : * NULL, then set *is_missing = true and return false instead of throwing
3289 : : * an error. (Caller must initialize *is_missing = false.)
3290 : : */
3291 : : static bool
3292 : 23 : TSConfigIsVisibleExt(Oid cfgid, bool *is_missing)
3293 : : {
3294 : : HeapTuple tup;
3295 : : Form_pg_ts_config form;
3296 : : Oid namespace;
3297 : : bool visible;
3298 : :
5734 rhaas@postgresql.org 3299 : 23 : tup = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgid));
6642 tgl@sss.pgh.pa.us 3300 [ - + ]: 23 : if (!HeapTupleIsValid(tup))
3301 : : {
744 tgl@sss.pgh.pa.us 3302 [ # # ]:UBC 0 : if (is_missing != NULL)
3303 : : {
3304 : 0 : *is_missing = true;
3305 : 0 : return false;
3306 : : }
6642 3307 [ # # ]: 0 : elog(ERROR, "cache lookup failed for text search configuration %u",
3308 : : cfgid);
3309 : : }
6642 tgl@sss.pgh.pa.us 3310 :CBC 23 : form = (Form_pg_ts_config) GETSTRUCT(tup);
3311 : :
3312 : 23 : recomputeNamespacePath();
3313 : :
3314 : : /*
3315 : : * Quick check: if it ain't in the path at all, it ain't visible. Items in
3316 : : * the system namespace are surely in the path and so we needn't even do
3317 : : * list_member_oid() for them.
3318 : : */
3319 : 23 : namespace = form->cfgnamespace;
3320 [ + - ]: 23 : if (namespace != PG_CATALOG_NAMESPACE &&
3321 [ + + ]: 23 : !list_member_oid(activeSearchPath, namespace))
3322 : 8 : visible = false;
3323 : : else
3324 : : {
3325 : : /*
3326 : : * If it is in the path, it might still not be visible; it could be
3327 : : * hidden by another configuration of the same name earlier in the
3328 : : * path. So we must do a slow check for conflicting configurations.
3329 : : */
3330 : 15 : char *name = NameStr(form->cfgname);
3331 : : ListCell *l;
3332 : :
3333 : 15 : visible = false;
3334 [ + - + - : 30 : foreach(l, activeSearchPath)
+ - ]
3335 : : {
3336 : 30 : Oid namespaceId = lfirst_oid(l);
3337 : :
3338 [ - + ]: 30 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 3339 :UBC 0 : continue; /* do not look in temp namespace */
3340 : :
6642 tgl@sss.pgh.pa.us 3341 [ + + ]:CBC 30 : if (namespaceId == namespace)
3342 : : {
3343 : : /* Found it first in path */
3344 : 15 : visible = true;
3345 : 15 : break;
3346 : : }
5734 rhaas@postgresql.org 3347 [ - + ]: 15 : if (SearchSysCacheExists2(TSCONFIGNAMENSP,
3348 : : PointerGetDatum(name),
3349 : : ObjectIdGetDatum(namespaceId)))
3350 : : {
3351 : : /* Found something else first in path */
6642 tgl@sss.pgh.pa.us 3352 :UBC 0 : break;
3353 : : }
3354 : : }
3355 : : }
3356 : :
6642 tgl@sss.pgh.pa.us 3357 :CBC 23 : ReleaseSysCache(tup);
3358 : :
3359 : 23 : return visible;
3360 : : }
3361 : :
3362 : :
3363 : : /*
3364 : : * DeconstructQualifiedName
3365 : : * Given a possibly-qualified name expressed as a list of String nodes,
3366 : : * extract the schema name and object name.
3367 : : *
3368 : : * *nspname_p is set to NULL if there is no explicit schema name.
3369 : : */
3370 : : void
796 peter@eisentraut.org 3371 : 1071887 : DeconstructQualifiedName(const List *names,
3372 : : char **nspname_p,
3373 : : char **objname_p)
3374 : : {
3375 : : char *catalogname;
8613 tgl@sss.pgh.pa.us 3376 : 1071887 : char *schemaname = NULL;
3377 : 1071887 : char *objname = NULL;
3378 : :
7824 neilc@samurai.com 3379 [ + + + + ]: 1071887 : switch (list_length(names))
3380 : : {
8613 tgl@sss.pgh.pa.us 3381 : 831810 : case 1:
7824 neilc@samurai.com 3382 : 831810 : objname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3383 : 831810 : break;
3384 : 240023 : case 2:
7824 neilc@samurai.com 3385 : 240023 : schemaname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3386 : 240023 : objname = strVal(lsecond(names));
3387 : 240023 : break;
3388 : 51 : case 3:
7824 neilc@samurai.com 3389 : 51 : catalogname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3390 : 51 : schemaname = strVal(lsecond(names));
8296 3391 : 51 : objname = strVal(lthird(names));
3392 : :
3393 : : /*
3394 : : * We check the catalog name and then ignore it.
3395 : : */
8158 peter_e@gmx.net 3396 [ + - ]: 51 : if (strcmp(catalogname, get_database_name(MyDatabaseId)) != 0)
8134 tgl@sss.pgh.pa.us 3397 [ + - ]: 51 : ereport(ERROR,
3398 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3399 : : errmsg("cross-database references are not implemented: %s",
3400 : : NameListToString(names))));
8613 tgl@sss.pgh.pa.us 3401 :UBC 0 : break;
8613 tgl@sss.pgh.pa.us 3402 :CBC 3 : default:
8134 3403 [ + - ]: 3 : ereport(ERROR,
3404 : : (errcode(ERRCODE_SYNTAX_ERROR),
3405 : : errmsg("improper qualified name (too many dotted names): %s",
3406 : : NameListToString(names))));
3407 : : break;
3408 : : }
3409 : :
8491 3410 : 1071833 : *nspname_p = schemaname;
3411 : 1071833 : *objname_p = objname;
3412 : 1071833 : }
3413 : :
3414 : : /*
3415 : : * LookupNamespaceNoError
3416 : : * Look up a schema name.
3417 : : *
3418 : : * Returns the namespace OID, or InvalidOid if not found.
3419 : : *
3420 : : * Note this does NOT perform any permissions check --- callers are
3421 : : * responsible for being sure that an appropriate check is made.
3422 : : * In the majority of cases LookupExplicitNamespace is preferable.
3423 : : */
3424 : : Oid
5840 3425 : 171 : LookupNamespaceNoError(const char *nspname)
3426 : : {
3427 : : /* check for pg_temp alias */
3428 [ - + ]: 171 : if (strcmp(nspname, "pg_temp") == 0)
3429 : : {
5840 tgl@sss.pgh.pa.us 3430 [ # # ]:UBC 0 : if (OidIsValid(myTempNamespace))
3431 : : {
4588 rhaas@postgresql.org 3432 [ # # ]: 0 : InvokeNamespaceSearchHook(myTempNamespace, true);
5840 tgl@sss.pgh.pa.us 3433 : 0 : return myTempNamespace;
3434 : : }
3435 : :
3436 : : /*
3437 : : * Since this is used only for looking up existing objects, there is
3438 : : * no point in trying to initialize the temp namespace here; and doing
3439 : : * so might create problems for some callers. Just report "not found".
3440 : : */
3441 : 0 : return InvalidOid;
3442 : : }
3443 : :
5562 rhaas@postgresql.org 3444 :CBC 171 : return get_namespace_oid(nspname, true);
3445 : : }
3446 : :
3447 : : /*
3448 : : * LookupExplicitNamespace
3449 : : * Process an explicitly-specified schema name: look up the schema
3450 : : * and verify we have USAGE (lookup) rights in it.
3451 : : *
3452 : : * Returns the namespace OID
3453 : : */
3454 : : Oid
4657 bruce@momjian.us 3455 : 378847 : LookupExplicitNamespace(const char *nspname, bool missing_ok)
3456 : : {
3457 : : Oid namespaceId;
3458 : : AclResult aclresult;
3459 : :
3460 : : /* check for pg_temp alias */
6765 tgl@sss.pgh.pa.us 3461 [ + + ]: 378847 : if (strcmp(nspname, "pg_temp") == 0)
3462 : : {
3463 [ + - ]: 172 : if (OidIsValid(myTempNamespace))
3464 : 172 : return myTempNamespace;
3465 : :
3466 : : /*
3467 : : * Since this is used only for looking up existing objects, there is
3468 : : * no point in trying to initialize the temp namespace here; and doing
3469 : : * so might create problems for some callers --- just fall through.
3470 : : */
3471 : : }
3472 : :
4657 bruce@momjian.us 3473 : 378675 : namespaceId = get_namespace_oid(nspname, missing_ok);
3474 [ + + + + ]: 378616 : if (missing_ok && !OidIsValid(namespaceId))
3475 : 168 : return InvalidOid;
3476 : :
1079 peter@eisentraut.org 3477 : 378448 : aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(), ACL_USAGE);
8491 tgl@sss.pgh.pa.us 3478 [ + + ]: 378448 : if (aclresult != ACLCHECK_OK)
2886 peter_e@gmx.net 3479 : 4 : aclcheck_error(aclresult, OBJECT_SCHEMA,
3480 : : nspname);
3481 : : /* Schema search hook for this lookup */
4588 rhaas@postgresql.org 3482 [ + + ]: 378444 : InvokeNamespaceSearchHook(namespaceId, true);
3483 : :
8491 tgl@sss.pgh.pa.us 3484 : 378444 : return namespaceId;
3485 : : }
3486 : :
3487 : : /*
3488 : : * LookupCreationNamespace
3489 : : * Look up the schema and verify we have CREATE rights on it.
3490 : : *
3491 : : * This is just like LookupExplicitNamespace except for the different
3492 : : * permission check, and that we are willing to create pg_temp if needed.
3493 : : *
3494 : : * Note: calling this may result in a CommandCounterIncrement operation,
3495 : : * if we have to create or clean out the temp namespace.
3496 : : */
3497 : : Oid
7392 3498 : 243 : LookupCreationNamespace(const char *nspname)
3499 : : {
3500 : : Oid namespaceId;
3501 : : AclResult aclresult;
3502 : :
3503 : : /* check for pg_temp alias */
6765 3504 [ + + ]: 243 : if (strcmp(nspname, "pg_temp") == 0)
3505 : : {
3506 : : /* Initialize temp namespace */
2474 michael@paquier.xyz 3507 : 76 : AccessTempTableNamespace(false);
6765 tgl@sss.pgh.pa.us 3508 : 76 : return myTempNamespace;
3509 : : }
3510 : :
5562 rhaas@postgresql.org 3511 : 167 : namespaceId = get_namespace_oid(nspname, false);
3512 : :
1079 peter@eisentraut.org 3513 : 166 : aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(), ACL_CREATE);
7392 tgl@sss.pgh.pa.us 3514 [ - + ]: 166 : if (aclresult != ACLCHECK_OK)
2886 peter_e@gmx.net 3515 :UBC 0 : aclcheck_error(aclresult, OBJECT_SCHEMA,
3516 : : nspname);
3517 : :
7392 tgl@sss.pgh.pa.us 3518 :CBC 166 : return namespaceId;
3519 : : }
3520 : :
3521 : : /*
3522 : : * Common checks on switching namespaces.
3523 : : *
3524 : : * We complain if either the old or new namespaces is a temporary schema
3525 : : * (or temporary toast schema), or if either the old or new namespaces is the
3526 : : * TOAST schema.
3527 : : */
3528 : : void
3630 rhaas@postgresql.org 3529 : 270 : CheckSetNamespace(Oid oldNspOid, Oid nspOid)
3530 : : {
3531 : : /* disallow renaming into or out of temp schemas */
5453 3532 [ + - - + ]: 270 : if (isAnyTempNamespace(nspOid) || isAnyTempNamespace(oldNspOid))
5453 rhaas@postgresql.org 3533 [ # # ]:UBC 0 : ereport(ERROR,
3534 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3535 : : errmsg("cannot move objects into or out of temporary schemas")));
3536 : :
3537 : : /* same for TOAST schema */
5453 rhaas@postgresql.org 3538 [ + - - + ]:CBC 270 : if (nspOid == PG_TOAST_NAMESPACE || oldNspOid == PG_TOAST_NAMESPACE)
5453 rhaas@postgresql.org 3539 [ # # ]:UBC 0 : ereport(ERROR,
3540 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3541 : : errmsg("cannot move objects into or out of TOAST schema")));
5453 rhaas@postgresql.org 3542 :CBC 270 : }
3543 : :
3544 : : /*
3545 : : * QualifiedNameGetCreationNamespace
3546 : : * Given a possibly-qualified name for an object (in List-of-Strings
3547 : : * format), determine what namespace the object should be created in.
3548 : : * Also extract and return the object name (last component of list).
3549 : : *
3550 : : * Note: this does not apply any permissions check. Callers must check
3551 : : * for CREATE rights on the selected namespace when appropriate.
3552 : : *
3553 : : * Note: calling this may result in a CommandCounterIncrement operation,
3554 : : * if we have to create or clean out the temp namespace.
3555 : : */
3556 : : Oid
796 peter@eisentraut.org 3557 : 19317 : QualifiedNameGetCreationNamespace(const List *names, char **objname_p)
3558 : : {
3559 : : char *schemaname;
3560 : : Oid namespaceId;
3561 : :
3562 : : /* deconstruct the name list */
6765 tgl@sss.pgh.pa.us 3563 : 19317 : DeconstructQualifiedName(names, &schemaname, objname_p);
3564 : :
8613 3565 [ + + ]: 19317 : if (schemaname)
3566 : : {
3567 : : /* check for pg_temp alias */
6765 3568 [ + + ]: 802 : if (strcmp(schemaname, "pg_temp") == 0)
3569 : : {
3570 : : /* Initialize temp namespace */
2474 michael@paquier.xyz 3571 : 153 : AccessTempTableNamespace(false);
6765 tgl@sss.pgh.pa.us 3572 : 153 : return myTempNamespace;
3573 : : }
3574 : : /* use exact schema given */
5562 rhaas@postgresql.org 3575 : 649 : namespaceId = get_namespace_oid(schemaname, false);
3576 : : /* we do not check for USAGE rights here! */
3577 : : }
3578 : : else
3579 : : {
3580 : : /* use the default creation namespace */
8582 tgl@sss.pgh.pa.us 3581 : 18515 : recomputeNamespacePath();
6765 3582 [ - + ]: 18515 : if (activeTempCreationPending)
3583 : : {
3584 : : /* Need to initialize temp namespace */
2474 michael@paquier.xyz 3585 :UBC 0 : AccessTempTableNamespace(true);
6765 tgl@sss.pgh.pa.us 3586 : 0 : return myTempNamespace;
3587 : : }
6793 tgl@sss.pgh.pa.us 3588 :CBC 18515 : namespaceId = activeCreationNamespace;
8596 3589 [ - + ]: 18515 : if (!OidIsValid(namespaceId))
8134 tgl@sss.pgh.pa.us 3590 [ # # ]:UBC 0 : ereport(ERROR,
3591 : : (errcode(ERRCODE_UNDEFINED_SCHEMA),
3592 : : errmsg("no schema has been selected to create in")));
3593 : : }
3594 : :
8613 tgl@sss.pgh.pa.us 3595 :CBC 19164 : return namespaceId;
3596 : : }
3597 : :
3598 : : /*
3599 : : * get_namespace_oid - given a namespace name, look up the OID
3600 : : *
3601 : : * If missing_ok is false, throw an error if namespace name not found. If
3602 : : * true, just return InvalidOid.
3603 : : */
3604 : : Oid
5562 rhaas@postgresql.org 3605 : 436243 : get_namespace_oid(const char *nspname, bool missing_ok)
3606 : : {
3607 : : Oid oid;
3608 : :
2533 andres@anarazel.de 3609 : 436243 : oid = GetSysCacheOid1(NAMESPACENAME, Anum_pg_namespace_oid,
3610 : : CStringGetDatum(nspname));
5562 rhaas@postgresql.org 3611 [ + + + + ]: 436243 : if (!OidIsValid(oid) && !missing_ok)
5314 bruce@momjian.us 3612 [ + - ]: 92 : ereport(ERROR,
3613 : : (errcode(ERRCODE_UNDEFINED_SCHEMA),
3614 : : errmsg("schema \"%s\" does not exist", nspname)));
3615 : :
5562 rhaas@postgresql.org 3616 : 436151 : return oid;
3617 : : }
3618 : :
3619 : : /*
3620 : : * makeRangeVarFromNameList
3621 : : * Utility routine to convert a qualified-name list into RangeVar form.
3622 : : */
3623 : : RangeVar *
796 peter@eisentraut.org 3624 : 29633 : makeRangeVarFromNameList(const List *names)
3625 : : {
6265 tgl@sss.pgh.pa.us 3626 : 29633 : RangeVar *rel = makeRangeVar(NULL, NULL, -1);
3627 : :
7824 neilc@samurai.com 3628 [ + + + - ]: 29633 : switch (list_length(names))
3629 : : {
8613 tgl@sss.pgh.pa.us 3630 : 19700 : case 1:
7824 neilc@samurai.com 3631 : 19700 : rel->relname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3632 : 19700 : break;
3633 : 9893 : case 2:
7824 neilc@samurai.com 3634 : 9893 : rel->schemaname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3635 : 9893 : rel->relname = strVal(lsecond(names));
3636 : 9893 : break;
3637 : 40 : case 3:
7824 neilc@samurai.com 3638 : 40 : rel->catalogname = strVal(linitial(names));
8613 tgl@sss.pgh.pa.us 3639 : 40 : rel->schemaname = strVal(lsecond(names));
8296 3640 : 40 : rel->relname = strVal(lthird(names));
8613 3641 : 40 : break;
8613 tgl@sss.pgh.pa.us 3642 :UBC 0 : default:
8134 3643 [ # # ]: 0 : ereport(ERROR,
3644 : : (errcode(ERRCODE_SYNTAX_ERROR),
3645 : : errmsg("improper relation name (too many dotted names): %s",
3646 : : NameListToString(names))));
3647 : : break;
3648 : : }
3649 : :
8613 tgl@sss.pgh.pa.us 3650 :CBC 29633 : return rel;
3651 : : }
3652 : :
3653 : : /*
3654 : : * NameListToString
3655 : : * Utility routine to convert a qualified-name list into a string.
3656 : : *
3657 : : * This is used primarily to form error messages, and so we do not quote
3658 : : * the list elements, for the sake of legibility.
3659 : : *
3660 : : * In most scenarios the list elements should always be String values,
3661 : : * but we also allow A_Star for the convenience of ColumnRef processing.
3662 : : */
3663 : : char *
796 peter@eisentraut.org 3664 : 897 : NameListToString(const List *names)
3665 : : {
3666 : : StringInfoData string;
3667 : : ListCell *l;
3668 : :
8602 tgl@sss.pgh.pa.us 3669 : 897 : initStringInfo(&string);
3670 : :
3671 [ + - + + : 2016 : foreach(l, names)
+ + ]
3672 : : {
6267 3673 : 1119 : Node *name = (Node *) lfirst(l);
3674 : :
7824 neilc@samurai.com 3675 [ + + ]: 1119 : if (l != list_head(names))
8602 tgl@sss.pgh.pa.us 3676 : 222 : appendStringInfoChar(&string, '.');
3677 : :
6267 3678 [ + - ]: 1119 : if (IsA(name, String))
3679 : 1119 : appendStringInfoString(&string, strVal(name));
6267 tgl@sss.pgh.pa.us 3680 [ # # ]:UBC 0 : else if (IsA(name, A_Star))
3822 peter_e@gmx.net 3681 : 0 : appendStringInfoChar(&string, '*');
3682 : : else
6267 tgl@sss.pgh.pa.us 3683 [ # # ]: 0 : elog(ERROR, "unexpected node type in name list: %d",
3684 : : (int) nodeTag(name));
3685 : : }
3686 : :
8602 tgl@sss.pgh.pa.us 3687 :CBC 897 : return string.data;
3688 : : }
3689 : :
3690 : : /*
3691 : : * NameListToQuotedString
3692 : : * Utility routine to convert a qualified-name list into a string.
3693 : : *
3694 : : * Same as above except that names will be double-quoted where necessary,
3695 : : * so the string could be re-parsed (eg, by textToQualifiedNameList).
3696 : : */
3697 : : char *
796 peter@eisentraut.org 3698 :UBC 0 : NameListToQuotedString(const List *names)
3699 : : {
3700 : : StringInfoData string;
3701 : : ListCell *l;
3702 : :
8395 tgl@sss.pgh.pa.us 3703 : 0 : initStringInfo(&string);
3704 : :
3705 [ # # # # : 0 : foreach(l, names)
# # ]
3706 : : {
7824 neilc@samurai.com 3707 [ # # ]: 0 : if (l != list_head(names))
8395 tgl@sss.pgh.pa.us 3708 : 0 : appendStringInfoChar(&string, '.');
8222 3709 : 0 : appendStringInfoString(&string, quote_identifier(strVal(lfirst(l))));
3710 : : }
3711 : :
8395 3712 : 0 : return string.data;
3713 : : }
3714 : :
3715 : : /*
3716 : : * isTempNamespace - is the given namespace my temporary-table namespace?
3717 : : */
3718 : : bool
8611 tgl@sss.pgh.pa.us 3719 :CBC 33721 : isTempNamespace(Oid namespaceId)
3720 : : {
3721 [ + + + + ]: 33721 : if (OidIsValid(myTempNamespace) && myTempNamespace == namespaceId)
3722 : 580 : return true;
3723 : 33141 : return false;
3724 : : }
3725 : :
3726 : : /*
3727 : : * isTempToastNamespace - is the given namespace my temporary-toast-table
3728 : : * namespace?
3729 : : */
3730 : : bool
6669 3731 : 3131064 : isTempToastNamespace(Oid namespaceId)
3732 : : {
3733 [ + + + + ]: 3131064 : if (OidIsValid(myTempToastNamespace) && myTempToastNamespace == namespaceId)
3734 : 1874 : return true;
3735 : 3129190 : return false;
3736 : : }
3737 : :
3738 : : /*
3739 : : * isTempOrTempToastNamespace - is the given namespace my temporary-table
3740 : : * namespace or my temporary-toast-table namespace?
3741 : : */
3742 : : bool
4081 bruce@momjian.us 3743 : 95956 : isTempOrTempToastNamespace(Oid namespaceId)
3744 : : {
6669 tgl@sss.pgh.pa.us 3745 [ + + ]: 95956 : if (OidIsValid(myTempNamespace) &&
3050 3746 [ + + + + ]: 43686 : (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId))
6669 3747 : 21844 : return true;
3748 : 74112 : return false;
3749 : : }
3750 : :
3751 : : /*
3752 : : * isAnyTempNamespace - is the given namespace a temporary-table namespace
3753 : : * (either my own, or another backend's)? Temporary-toast-table namespaces
3754 : : * are included, too.
3755 : : */
3756 : : bool
7392 3757 : 68526 : isAnyTempNamespace(Oid namespaceId)
3758 : : {
3759 : : bool result;
3760 : : char *nspname;
3761 : :
3762 : : /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
8435 3763 : 68526 : nspname = get_namespace_name(namespaceId);
3764 [ - + ]: 68526 : if (!nspname)
8435 tgl@sss.pgh.pa.us 3765 :UBC 0 : return false; /* no such namespace? */
6669 tgl@sss.pgh.pa.us 3766 [ + + ]:CBC 132998 : result = (strncmp(nspname, "pg_temp_", 8) == 0) ||
3767 [ + + ]: 64472 : (strncmp(nspname, "pg_toast_temp_", 14) == 0);
8435 3768 : 68526 : pfree(nspname);
3769 : 68526 : return result;
3770 : : }
3771 : :
3772 : : /*
3773 : : * isOtherTempNamespace - is the given namespace some other backend's
3774 : : * temporary-table namespace (including temporary-toast-table namespaces)?
3775 : : *
3776 : : * Note: for most purposes in the C code, this function is obsolete. Use
3777 : : * RELATION_IS_OTHER_TEMP() instead to detect non-local temp relations.
3778 : : */
3779 : : bool
7392 3780 : 10637 : isOtherTempNamespace(Oid namespaceId)
3781 : : {
3782 : : /* If it's my own temp namespace, say "false" */
4081 bruce@momjian.us 3783 [ + + ]: 10637 : if (isTempOrTempToastNamespace(namespaceId))
7392 tgl@sss.pgh.pa.us 3784 : 258 : return false;
3785 : : /* Else, if it's any temp namespace, say "true" */
3786 : 10379 : return isAnyTempNamespace(namespaceId);
3787 : : }
3788 : :
3789 : : /*
3790 : : * checkTempNamespaceStatus - is the given namespace owned and actively used
3791 : : * by a backend?
3792 : : *
3793 : : * Note: this can be used while scanning relations in pg_class to detect
3794 : : * orphaned temporary tables or namespaces with a backend connected to a
3795 : : * given database. The result may be out of date quickly, so the caller
3796 : : * must be careful how to handle this information.
3797 : : */
3798 : : TempNamespaceStatus
2068 3799 : 5 : checkTempNamespaceStatus(Oid namespaceId)
3800 : : {
3801 : : PGPROC *proc;
3802 : : ProcNumber procNumber;
3803 : :
2632 michael@paquier.xyz 3804 [ - + ]: 5 : Assert(OidIsValid(MyDatabaseId));
3805 : :
603 heikki.linnakangas@i 3806 : 5 : procNumber = GetTempNamespaceProcNumber(namespaceId);
3807 : :
3808 : : /* No such namespace, or its name shows it's not temp? */
3809 [ - + ]: 5 : if (procNumber == INVALID_PROC_NUMBER)
2068 tgl@sss.pgh.pa.us 3810 :UBC 0 : return TEMP_NAMESPACE_NOT_TEMP;
3811 : :
3812 : : /* Is the backend alive? */
603 heikki.linnakangas@i 3813 :CBC 5 : proc = ProcNumberGetProc(procNumber);
2632 michael@paquier.xyz 3814 [ - + ]: 5 : if (proc == NULL)
2068 tgl@sss.pgh.pa.us 3815 :UBC 0 : return TEMP_NAMESPACE_IDLE;
3816 : :
3817 : : /* Is the backend connected to the same database we are looking at? */
2632 michael@paquier.xyz 3818 [ - + ]:CBC 5 : if (proc->databaseId != MyDatabaseId)
2068 tgl@sss.pgh.pa.us 3819 :UBC 0 : return TEMP_NAMESPACE_IDLE;
3820 : :
3821 : : /* Does the backend own the temporary namespace? */
2632 michael@paquier.xyz 3822 [ - + ]:CBC 5 : if (proc->tempNamespaceId != namespaceId)
2068 tgl@sss.pgh.pa.us 3823 :UBC 0 : return TEMP_NAMESPACE_IDLE;
3824 : :
3825 : : /* Yup, so namespace is busy */
2068 tgl@sss.pgh.pa.us 3826 :CBC 5 : return TEMP_NAMESPACE_IN_USE;
3827 : : }
3828 : :
3829 : : /*
3830 : : * GetTempNamespaceProcNumber - if the given namespace is a temporary-table
3831 : : * namespace (either my own, or another backend's), return the proc number
3832 : : * that owns it. Temporary-toast-table namespaces are included, too.
3833 : : * If it isn't a temp namespace, return INVALID_PROC_NUMBER.
3834 : : */
3835 : : ProcNumber
603 heikki.linnakangas@i 3836 : 23 : GetTempNamespaceProcNumber(Oid namespaceId)
3837 : : {
3838 : : int result;
3839 : : char *nspname;
3840 : :
3841 : : /* See if the namespace name starts with "pg_temp_" or "pg_toast_temp_" */
6327 tgl@sss.pgh.pa.us 3842 : 23 : nspname = get_namespace_name(namespaceId);
3843 [ - + ]: 23 : if (!nspname)
603 heikki.linnakangas@i 3844 :UBC 0 : return INVALID_PROC_NUMBER; /* no such namespace? */
6327 tgl@sss.pgh.pa.us 3845 [ + - ]:CBC 23 : if (strncmp(nspname, "pg_temp_", 8) == 0)
3846 : 23 : result = atoi(nspname + 8);
6327 tgl@sss.pgh.pa.us 3847 [ # # ]:UBC 0 : else if (strncmp(nspname, "pg_toast_temp_", 14) == 0)
3848 : 0 : result = atoi(nspname + 14);
3849 : : else
603 heikki.linnakangas@i 3850 : 0 : result = INVALID_PROC_NUMBER;
6327 tgl@sss.pgh.pa.us 3851 :CBC 23 : pfree(nspname);
3852 : 23 : return result;
3853 : : }
3854 : :
3855 : : /*
3856 : : * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
3857 : : * which must already be assigned. (This is only used when creating a toast
3858 : : * table for a temp table, so we must have already done InitTempTableNamespace)
3859 : : */
3860 : : Oid
6669 3861 : 475 : GetTempToastNamespace(void)
3862 : : {
3863 [ - + ]: 475 : Assert(OidIsValid(myTempToastNamespace));
3864 : 475 : return myTempToastNamespace;
3865 : : }
3866 : :
3867 : :
3868 : : /*
3869 : : * GetTempNamespaceState - fetch status of session's temporary namespace
3870 : : *
3871 : : * This is used for conveying state to a parallel worker, and is not meant
3872 : : * for general-purpose access.
3873 : : */
3874 : : void
3427 3875 : 459 : GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
3876 : : {
3877 : : /* Return namespace OIDs, or 0 if session has not created temp namespace */
3878 : 459 : *tempNamespaceId = myTempNamespace;
3879 : 459 : *tempToastNamespaceId = myTempToastNamespace;
3880 : 459 : }
3881 : :
3882 : : /*
3883 : : * SetTempNamespaceState - set status of session's temporary namespace
3884 : : *
3885 : : * This is used for conveying state to a parallel worker, and is not meant for
3886 : : * general-purpose access. By transferring these namespace OIDs to workers,
3887 : : * we ensure they will have the same notion of the search path as their leader
3888 : : * does.
3889 : : */
3890 : : void
3891 : 1378 : SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
3892 : : {
3893 : : /* Worker should not have created its own namespaces ... */
3894 [ - + ]: 1378 : Assert(myTempNamespace == InvalidOid);
3895 [ - + ]: 1378 : Assert(myTempToastNamespace == InvalidOid);
3896 [ - + ]: 1378 : Assert(myTempNamespaceSubID == InvalidSubTransactionId);
3897 : :
3898 : : /* Assign same namespace OIDs that leader has */
3899 : 1378 : myTempNamespace = tempNamespaceId;
3900 : 1378 : myTempToastNamespace = tempToastNamespaceId;
3901 : :
3902 : : /*
3903 : : * It's fine to leave myTempNamespaceSubID == InvalidSubTransactionId.
3904 : : * Even if the namespace is new so far as the leader is concerned, it's
3905 : : * not new to the worker, and we certainly wouldn't want the worker trying
3906 : : * to destroy it.
3907 : : */
3908 : :
3909 : 1378 : baseSearchPathValid = false; /* may need to rebuild list */
713 jdavis@postgresql.or 3910 : 1378 : searchPathCacheValid = false;
3427 tgl@sss.pgh.pa.us 3911 : 1378 : }
3912 : :
3913 : :
3914 : : /*
3915 : : * GetSearchPathMatcher - fetch current search path definition.
3916 : : *
3917 : : * The result structure is allocated in the specified memory context
3918 : : * (which might or might not be equal to CurrentMemoryContext); but any
3919 : : * junk created by revalidation calculations will be in CurrentMemoryContext.
3920 : : */
3921 : : SearchPathMatcher *
819 noah@leadboat.com 3922 : 30532 : GetSearchPathMatcher(MemoryContext context)
3923 : : {
3924 : : SearchPathMatcher *result;
3925 : : List *schemas;
3926 : : MemoryContext oldcxt;
3927 : :
6793 tgl@sss.pgh.pa.us 3928 : 30532 : recomputeNamespacePath();
3929 : :
3930 : 30532 : oldcxt = MemoryContextSwitchTo(context);
3931 : :
819 noah@leadboat.com 3932 : 30532 : result = (SearchPathMatcher *) palloc0(sizeof(SearchPathMatcher));
6793 tgl@sss.pgh.pa.us 3933 : 30532 : schemas = list_copy(activeSearchPath);
3934 [ + + + + ]: 67321 : while (schemas && linitial_oid(schemas) != activeCreationNamespace)
3935 : : {
3936 [ + + ]: 36789 : if (linitial_oid(schemas) == myTempNamespace)
3937 : 7473 : result->addTemp = true;
3938 : : else
3939 : : {
3940 [ - + ]: 29316 : Assert(linitial_oid(schemas) == PG_CATALOG_NAMESPACE);
3941 : 29316 : result->addCatalog = true;
3942 : : }
3943 : 36789 : schemas = list_delete_first(schemas);
3944 : : }
3945 : 30532 : result->schemas = schemas;
2041 3946 : 30532 : result->generation = activePathGeneration;
3947 : :
6793 3948 : 30532 : MemoryContextSwitchTo(oldcxt);
3949 : :
3950 : 30532 : return result;
3951 : : }
3952 : :
3953 : : /*
3954 : : * CopySearchPathMatcher - copy the specified SearchPathMatcher.
3955 : : *
3956 : : * The result structure is allocated in CurrentMemoryContext.
3957 : : */
3958 : : SearchPathMatcher *
819 noah@leadboat.com 3959 :UBC 0 : CopySearchPathMatcher(SearchPathMatcher *path)
3960 : : {
3961 : : SearchPathMatcher *result;
3962 : :
3963 : 0 : result = (SearchPathMatcher *) palloc(sizeof(SearchPathMatcher));
5155 tgl@sss.pgh.pa.us 3964 : 0 : result->schemas = list_copy(path->schemas);
3965 : 0 : result->addCatalog = path->addCatalog;
3966 : 0 : result->addTemp = path->addTemp;
2041 3967 : 0 : result->generation = path->generation;
3968 : :
5155 3969 : 0 : return result;
3970 : : }
3971 : :
3972 : : /*
3973 : : * SearchPathMatchesCurrentEnvironment - does path match current environment?
3974 : : *
3975 : : * This is tested over and over in some common code paths, and in the typical
3976 : : * scenario where the active search path seldom changes, it'll always succeed.
3977 : : * We make that case fast by keeping a generation counter that is advanced
3978 : : * whenever the active search path changes.
3979 : : */
3980 : : bool
819 noah@leadboat.com 3981 :CBC 396091 : SearchPathMatchesCurrentEnvironment(SearchPathMatcher *path)
3982 : : {
3983 : : ListCell *lc,
3984 : : *lcp;
3985 : :
3986 tgl@sss.pgh.pa.us 3986 : 396091 : recomputeNamespacePath();
3987 : :
3988 : : /* Quick out if already known equal to active path. */
2041 3989 [ + + ]: 396091 : if (path->generation == activePathGeneration)
3990 : 395894 : return true;
3991 : :
3992 : : /* We scan down the activeSearchPath to see if it matches the input. */
3986 3993 : 197 : lc = list_head(activeSearchPath);
3994 : :
3995 : : /* If path->addTemp, first item should be my temp namespace. */
3996 [ + + ]: 197 : if (path->addTemp)
3997 : : {
3998 [ + - + + ]: 39 : if (lc && lfirst_oid(lc) == myTempNamespace)
2296 3999 : 33 : lc = lnext(activeSearchPath, lc);
4000 : : else
3986 tgl@sss.pgh.pa.us 4001 :GBC 6 : return false;
4002 : : }
4003 : : /* If path->addCatalog, next item should be pg_catalog. */
3986 tgl@sss.pgh.pa.us 4004 [ + + ]:CBC 191 : if (path->addCatalog)
4005 : : {
4006 [ + - + + ]: 110 : if (lc && lfirst_oid(lc) == PG_CATALOG_NAMESPACE)
2296 4007 : 93 : lc = lnext(activeSearchPath, lc);
4008 : : else
3986 4009 : 17 : return false;
4010 : : }
4011 : : /* We should now be looking at the activeCreationNamespace. */
4012 [ + + + + ]: 174 : if (activeCreationNamespace != (lc ? lfirst_oid(lc) : InvalidOid))
4013 : 18 : return false;
4014 : : /* The remainder of activeSearchPath should match path->schemas. */
4015 [ + + + + : 292 : foreach(lcp, path->schemas)
+ + ]
4016 : : {
4017 [ + - + + ]: 159 : if (lc && lfirst_oid(lc) == lfirst_oid(lcp))
2296 4018 : 136 : lc = lnext(activeSearchPath, lc);
4019 : : else
3986 4020 : 23 : return false;
4021 : : }
4022 [ + + ]: 133 : if (lc)
4023 : 9 : return false;
4024 : :
4025 : : /*
4026 : : * Update path->generation so that future tests will return quickly, so
4027 : : * long as the active search path doesn't change.
4028 : : */
2041 4029 : 124 : path->generation = activePathGeneration;
4030 : :
3986 4031 : 124 : return true;
4032 : : }
4033 : :
4034 : : /*
4035 : : * get_collation_oid - find a collation by possibly qualified name
4036 : : *
4037 : : * Note that this will only find collations that work with the current
4038 : : * database's encoding.
4039 : : */
4040 : : Oid
1134 pg@bowt.ie 4041 : 5450 : get_collation_oid(List *collname, bool missing_ok)
4042 : : {
4043 : : char *schemaname;
4044 : : char *collation_name;
5344 tgl@sss.pgh.pa.us 4045 : 5450 : int32 dbencoding = GetDatabaseEncoding();
4046 : : Oid namespaceId;
4047 : : Oid colloid;
4048 : : ListCell *l;
4049 : :
4050 : : /* deconstruct the name list */
1134 pg@bowt.ie 4051 : 5450 : DeconstructQualifiedName(collname, &schemaname, &collation_name);
4052 : :
5375 peter_e@gmx.net 4053 [ + + ]: 5450 : if (schemaname)
4054 : : {
4055 : : /* use exact schema given */
4657 bruce@momjian.us 4056 : 3459 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
4057 [ + + + + ]: 3459 : if (missing_ok && !OidIsValid(namespaceId))
4058 : 12 : return InvalidOid;
4059 : :
3047 tgl@sss.pgh.pa.us 4060 : 3447 : colloid = lookup_collation(collation_name, namespaceId, dbencoding);
5344 4061 [ + - ]: 3447 : if (OidIsValid(colloid))
4062 : 3447 : return colloid;
4063 : : }
4064 : : else
4065 : : {
4066 : : /* search for it in search path */
5375 peter_e@gmx.net 4067 : 1991 : recomputeNamespacePath();
4068 : :
4069 [ + - + + : 3312 : foreach(l, activeSearchPath)
+ + ]
4070 : : {
4071 : 3287 : namespaceId = lfirst_oid(l);
4072 : :
4073 [ + + ]: 3287 : if (namespaceId == myTempNamespace)
4074 : 669 : continue; /* do not look in temp namespace */
4075 : :
3047 tgl@sss.pgh.pa.us 4076 : 2618 : colloid = lookup_collation(collation_name, namespaceId, dbencoding);
5375 peter_e@gmx.net 4077 [ + + ]: 2618 : if (OidIsValid(colloid))
4078 : 1966 : return colloid;
4079 : : }
4080 : : }
4081 : :
4082 : : /* Not found in path */
5344 tgl@sss.pgh.pa.us 4083 [ + + ]: 25 : if (!missing_ok)
5375 peter_e@gmx.net 4084 [ + - ]: 16 : ereport(ERROR,
4085 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
4086 : : errmsg("collation \"%s\" for encoding \"%s\" does not exist",
4087 : : NameListToString(collname), GetDatabaseEncodingName())));
5344 tgl@sss.pgh.pa.us 4088 : 9 : return InvalidOid;
4089 : : }
4090 : :
4091 : : /*
4092 : : * get_conversion_oid - find a conversion by possibly qualified name
4093 : : */
4094 : : Oid
1134 pg@bowt.ie 4095 : 91 : get_conversion_oid(List *conname, bool missing_ok)
4096 : : {
4097 : : char *schemaname;
4098 : : char *conversion_name;
4099 : : Oid namespaceId;
5562 rhaas@postgresql.org 4100 : 91 : Oid conoid = InvalidOid;
4101 : : ListCell *l;
4102 : :
4103 : : /* deconstruct the name list */
1134 pg@bowt.ie 4104 : 91 : DeconstructQualifiedName(conname, &schemaname, &conversion_name);
4105 : :
8395 tgl@sss.pgh.pa.us 4106 [ + + ]: 85 : if (schemaname)
4107 : : {
4108 : : /* use exact schema given */
4657 bruce@momjian.us 4109 : 19 : namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
4110 [ + + + - ]: 19 : if (missing_ok && !OidIsValid(namespaceId))
4111 : 3 : conoid = InvalidOid;
4112 : : else
2533 andres@anarazel.de 4113 : 16 : conoid = GetSysCacheOid2(CONNAMENSP, Anum_pg_conversion_oid,
4114 : : PointerGetDatum(conversion_name),
4115 : : ObjectIdGetDatum(namespaceId));
4116 : : }
4117 : : else
4118 : : {
4119 : : /* search for it in search path */
8395 tgl@sss.pgh.pa.us 4120 : 66 : recomputeNamespacePath();
4121 : :
6793 4122 [ + - + + : 147 : foreach(l, activeSearchPath)
+ + ]
4123 : : {
7824 neilc@samurai.com 4124 : 132 : namespaceId = lfirst_oid(l);
4125 : :
6765 tgl@sss.pgh.pa.us 4126 [ - + ]: 132 : if (namespaceId == myTempNamespace)
6556 bruce@momjian.us 4127 :UBC 0 : continue; /* do not look in temp namespace */
4128 : :
2533 andres@anarazel.de 4129 :CBC 132 : conoid = GetSysCacheOid2(CONNAMENSP, Anum_pg_conversion_oid,
4130 : : PointerGetDatum(conversion_name),
4131 : : ObjectIdGetDatum(namespaceId));
8395 tgl@sss.pgh.pa.us 4132 [ + + ]: 132 : if (OidIsValid(conoid))
4133 : 51 : return conoid;
4134 : : }
4135 : : }
4136 : :
4137 : : /* Not found in path */
5562 rhaas@postgresql.org 4138 [ + + + + ]: 34 : if (!OidIsValid(conoid) && !missing_ok)
4139 [ + - ]: 18 : ereport(ERROR,
4140 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
4141 : : errmsg("conversion \"%s\" does not exist",
4142 : : NameListToString(conname))));
4143 : 16 : return conoid;
4144 : : }
4145 : :
4146 : : /*
4147 : : * FindDefaultConversionProc - find default encoding conversion proc
4148 : : */
4149 : : Oid
4872 peter_e@gmx.net 4150 : 3391 : FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
4151 : : {
4152 : : Oid proc;
4153 : : ListCell *l;
4154 : :
8504 ishii@postgresql.org 4155 : 3391 : recomputeNamespacePath();
4156 : :
6793 tgl@sss.pgh.pa.us 4157 [ + - + - : 3391 : foreach(l, activeSearchPath)
+ - ]
4158 : : {
7824 neilc@samurai.com 4159 : 3391 : Oid namespaceId = lfirst_oid(l);
4160 : :
6765 tgl@sss.pgh.pa.us 4161 [ - + ]: 3391 : if (namespaceId == myTempNamespace)
6765 tgl@sss.pgh.pa.us 4162 :UBC 0 : continue; /* do not look in temp namespace */
4163 : :
8504 ishii@postgresql.org 4164 :CBC 3391 : proc = FindDefaultConversion(namespaceId, for_encoding, to_encoding);
4165 [ + - ]: 3391 : if (OidIsValid(proc))
4166 : 3391 : return proc;
4167 : : }
4168 : :
4169 : : /* Not found in path */
8504 ishii@postgresql.org 4170 :UBC 0 : return InvalidOid;
4171 : : }
4172 : :
4173 : : /*
4174 : : * Look up namespace IDs and perform ACL checks. Return newly-allocated list.
4175 : : */
4176 : : static List *
713 jdavis@postgresql.or 4177 :CBC 19310 : preprocessNamespacePath(const char *searchPath, Oid roleid,
4178 : : bool *temp_missing)
4179 : : {
4180 : : char *rawname;
4181 : : List *namelist;
4182 : : List *oidlist;
4183 : : ListCell *l;
4184 : :
4185 : : /* Need a modifiable copy */
4186 : 19310 : rawname = pstrdup(searchPath);
4187 : :
4188 : : /* Parse string into list of identifiers */
8582 tgl@sss.pgh.pa.us 4189 [ - + ]: 19310 : if (!SplitIdentifierString(rawname, ',', &namelist))
4190 : : {
4191 : : /* syntax error in name list */
4192 : : /* this should not happen if GUC checked check_search_path */
8134 tgl@sss.pgh.pa.us 4193 [ # # ]:UBC 0 : elog(ERROR, "invalid list syntax");
4194 : : }
4195 : :
4196 : : /*
4197 : : * Convert the list of names to a list of OIDs. If any names are not
4198 : : * recognizable or we don't have read access, just leave them out of the
4199 : : * list. (We can't raise an error, since the search_path setting has
4200 : : * already been accepted.) Don't make duplicate entries, either.
4201 : : */
8582 tgl@sss.pgh.pa.us 4202 :CBC 19310 : oidlist = NIL;
713 jdavis@postgresql.or 4203 : 19310 : *temp_missing = false;
8582 tgl@sss.pgh.pa.us 4204 [ + + + + : 53119 : foreach(l, namelist)
+ + ]
4205 : : {
8454 bruce@momjian.us 4206 : 33809 : char *curname = (char *) lfirst(l);
4207 : : Oid namespaceId;
4208 : :
8582 tgl@sss.pgh.pa.us 4209 [ + + ]: 33809 : if (strcmp(curname, "$user") == 0)
4210 : : {
4211 : : /* $user --- substitute namespace matching user name, if any */
4212 : : HeapTuple tuple;
4213 : :
5734 rhaas@postgresql.org 4214 : 15229 : tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
8582 tgl@sss.pgh.pa.us 4215 [ + - ]: 15229 : if (HeapTupleIsValid(tuple))
4216 : : {
4217 : : char *rname;
4218 : :
7426 4219 : 15229 : rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
5562 rhaas@postgresql.org 4220 : 15229 : namespaceId = get_namespace_oid(rname, true);
8582 tgl@sss.pgh.pa.us 4221 : 15229 : ReleaseSysCache(tuple);
4222 [ + + + - ]: 15232 : if (OidIsValid(namespaceId) &&
1079 peter@eisentraut.org 4223 : 3 : object_aclcheck(NamespaceRelationId, namespaceId, roleid,
4224 : : ACL_USAGE) == ACLCHECK_OK)
7824 neilc@samurai.com 4225 : 3 : oidlist = lappend_oid(oidlist, namespaceId);
4226 : : }
4227 : : }
6765 tgl@sss.pgh.pa.us 4228 [ + + ]: 18580 : else if (strcmp(curname, "pg_temp") == 0)
4229 : : {
4230 : : /* pg_temp --- substitute temp namespace, if any */
4231 [ + + ]: 545 : if (OidIsValid(myTempNamespace))
713 jdavis@postgresql.or 4232 : 142 : oidlist = lappend_oid(oidlist, myTempNamespace);
4233 : : else
4234 : : {
4235 : : /* If it ought to be the creation namespace, set flag */
6765 tgl@sss.pgh.pa.us 4236 [ + + ]: 403 : if (oidlist == NIL)
713 jdavis@postgresql.or 4237 : 3 : *temp_missing = true;
4238 : : }
4239 : : }
4240 : : else
4241 : : {
4242 : : /* normal namespace reference */
5562 rhaas@postgresql.org 4243 : 18035 : namespaceId = get_namespace_oid(curname, true);
8582 tgl@sss.pgh.pa.us 4244 [ + + + + ]: 36036 : if (OidIsValid(namespaceId) &&
1079 peter@eisentraut.org 4245 : 18001 : object_aclcheck(NamespaceRelationId, namespaceId, roleid,
4246 : : ACL_USAGE) == ACLCHECK_OK)
7824 neilc@samurai.com 4247 : 17986 : oidlist = lappend_oid(oidlist, namespaceId);
4248 : : }
4249 : : }
4250 : :
713 jdavis@postgresql.or 4251 : 19310 : pfree(rawname);
4252 : 19310 : list_free(namelist);
4253 : :
4254 : 19310 : return oidlist;
4255 : : }
4256 : :
4257 : : /*
4258 : : * Remove duplicates, run namespace search hooks, and prepend
4259 : : * implicitly-searched namespaces. Return newly-allocated list.
4260 : : *
4261 : : * If an object_access_hook is present, this must always be recalculated. It
4262 : : * may seem that duplicate elimination is not dependent on the result of the
4263 : : * hook, but if a hook returns different results on different calls for the
4264 : : * same namespace ID, then it could affect the order in which that namespace
4265 : : * appears in the final list.
4266 : : */
4267 : : static List *
4268 : 18912 : finalNamespacePath(List *oidlist, Oid *firstNS)
4269 : : {
4270 : 18912 : List *finalPath = NIL;
4271 : : ListCell *lc;
4272 : :
4273 [ + + + + : 37047 : foreach(lc, oidlist)
+ + ]
4274 : : {
4275 : 18135 : Oid namespaceId = lfirst_oid(lc);
4276 : :
4277 [ + + ]: 18135 : if (!list_member_oid(finalPath, namespaceId))
4278 : : {
4279 [ + + + - ]: 18128 : if (InvokeNamespaceSearchHook(namespaceId, false))
4280 : 18128 : finalPath = lappend_oid(finalPath, namespaceId);
4281 : : }
4282 : : }
4283 : :
4284 : : /*
4285 : : * Remember the first member of the explicit list. (Note: this is
4286 : : * nominally wrong if temp_missing, but we need it anyway to distinguish
4287 : : * explicit from implicit mention of pg_catalog.)
4288 : : */
4289 [ + + ]: 18912 : if (finalPath == NIL)
4290 : 1337 : *firstNS = InvalidOid;
4291 : : else
4292 : 17575 : *firstNS = linitial_oid(finalPath);
4293 : :
4294 : : /*
4295 : : * Add any implicitly-searched namespaces to the list. Note these go on
4296 : : * the front, not the back; also notice that we do not check USAGE
4297 : : * permissions for these.
4298 : : */
4299 [ + + ]: 18912 : if (!list_member_oid(finalPath, PG_CATALOG_NAMESPACE))
4300 : 18313 : finalPath = lcons_oid(PG_CATALOG_NAMESPACE, finalPath);
4301 : :
8564 tgl@sss.pgh.pa.us 4302 [ + + ]: 18912 : if (OidIsValid(myTempNamespace) &&
713 jdavis@postgresql.or 4303 [ + + ]: 2135 : !list_member_oid(finalPath, myTempNamespace))
4304 : 1993 : finalPath = lcons_oid(myTempNamespace, finalPath);
4305 : :
4306 : 18912 : return finalPath;
4307 : : }
4308 : :
4309 : : /*
4310 : : * Retrieve search path information from the cache; or if not there, fill
4311 : : * it. The returned entry is valid only until the next call to this function.
4312 : : */
4313 : : static const SearchPathCacheEntry *
707 4314 : 36468 : cachedNamespacePath(const char *searchPath, Oid roleid)
4315 : : {
4316 : : MemoryContext oldcxt;
4317 : : SearchPathCacheEntry *entry;
4318 : :
713 4319 : 36468 : spcache_init();
4320 : :
4321 : 36468 : entry = spcache_insert(searchPath, roleid);
4322 : :
4323 : : /*
4324 : : * An OOM may have resulted in a cache entry with missing 'oidlist' or
4325 : : * 'finalPath', so just compute whatever is missing.
4326 : : */
4327 : :
4328 [ + + ]: 36468 : if (entry->oidlist == NIL)
4329 : : {
4330 : 19310 : oldcxt = MemoryContextSwitchTo(SearchPathCacheContext);
4331 : 19310 : entry->oidlist = preprocessNamespacePath(searchPath, roleid,
4332 : : &entry->temp_missing);
4333 : 19310 : MemoryContextSwitchTo(oldcxt);
4334 : : }
4335 : :
4336 : : /*
4337 : : * If a hook is set, we must recompute finalPath from the oidlist each
4338 : : * time, because the hook may affect the result. This is still much faster
4339 : : * than recomputing from the string (and doing catalog lookups and ACL
4340 : : * checks).
4341 : : */
4342 [ + + + + ]: 36468 : if (entry->finalPath == NIL || object_access_hook ||
4343 [ - + ]: 17556 : entry->forceRecompute)
4344 : : {
707 4345 : 18912 : list_free(entry->finalPath);
4346 : 18912 : entry->finalPath = NIL;
4347 : :
713 4348 : 18912 : oldcxt = MemoryContextSwitchTo(SearchPathCacheContext);
707 4349 : 18912 : entry->finalPath = finalNamespacePath(entry->oidlist,
4350 : : &entry->firstNS);
2041 tgl@sss.pgh.pa.us 4351 : 18912 : MemoryContextSwitchTo(oldcxt);
4352 : :
4353 : : /*
4354 : : * If an object_access_hook is set when finalPath is calculated, the
4355 : : * result may be affected by the hook. Force recomputation of
4356 : : * finalPath the next time this cache entry is used, even if the
4357 : : * object_access_hook is not set at that time.
4358 : : */
713 jdavis@postgresql.or 4359 : 18912 : entry->forceRecompute = object_access_hook ? true : false;
4360 : : }
4361 : :
4362 : 36468 : return entry;
4363 : : }
4364 : :
4365 : : /*
4366 : : * recomputeNamespacePath - recompute path derived variables if needed.
4367 : : */
4368 : : static void
4369 : 1934706 : recomputeNamespacePath(void)
4370 : : {
4371 : 1934706 : Oid roleid = GetUserId();
4372 : : bool pathChanged;
4373 : : const SearchPathCacheEntry *entry;
4374 : :
4375 : : /* Do nothing if path is already valid. */
4376 [ + + + + ]: 1934706 : if (baseSearchPathValid && namespaceUser == roleid)
4377 : 1898238 : return;
4378 : :
707 4379 : 36468 : entry = cachedNamespacePath(namespace_search_path, roleid);
4380 : :
713 4381 [ + + ]: 36468 : if (baseCreationNamespace == entry->firstNS &&
4382 [ + + + + ]: 53393 : baseTempCreationPending == entry->temp_missing &&
707 4383 : 26695 : equal(entry->finalPath, baseSearchPath))
4384 : : {
713 4385 : 25065 : pathChanged = false;
4386 : : }
4387 : : else
4388 : : {
4389 : : MemoryContext oldcxt;
4390 : : List *newpath;
4391 : :
4392 : 11403 : pathChanged = true;
4393 : :
4394 : : /* Must save OID list in permanent storage. */
707 4395 : 11403 : oldcxt = MemoryContextSwitchTo(TopMemoryContext);
4396 : 11403 : newpath = list_copy(entry->finalPath);
4397 : 11403 : MemoryContextSwitchTo(oldcxt);
4398 : :
4399 : : /* Now safe to assign to state variables. */
4400 : 11403 : list_free(baseSearchPath);
4401 : 11403 : baseSearchPath = newpath;
4402 : 11403 : baseCreationNamespace = entry->firstNS;
4403 : 11403 : baseTempCreationPending = entry->temp_missing;
4404 : : }
4405 : :
4406 : : /* Mark the path valid. */
6793 tgl@sss.pgh.pa.us 4407 : 36468 : baseSearchPathValid = true;
7426 4408 : 36468 : namespaceUser = roleid;
4409 : :
4410 : : /* And make it active. */
6793 4411 : 36468 : activeSearchPath = baseSearchPath;
4412 : 36468 : activeCreationNamespace = baseCreationNamespace;
6765 4413 : 36468 : activeTempCreationPending = baseTempCreationPending;
4414 : :
4415 : : /*
4416 : : * Bump the generation only if something actually changed. (Notice that
4417 : : * what we compared to was the old state of the base path variables.)
4418 : : */
2041 4419 [ + + ]: 36468 : if (pathChanged)
4420 : 11403 : activePathGeneration++;
4421 : : }
4422 : :
4423 : : /*
4424 : : * AccessTempTableNamespace
4425 : : * Provide access to a temporary namespace, potentially creating it
4426 : : * if not present yet. This routine registers if the namespace gets
4427 : : * in use in this transaction. 'force' can be set to true to allow
4428 : : * the caller to enforce the creation of the temporary namespace for
4429 : : * use in this backend, which happens if its creation is pending.
4430 : : */
4431 : : static void
2474 michael@paquier.xyz 4432 : 3482 : AccessTempTableNamespace(bool force)
4433 : : {
4434 : : /*
4435 : : * Make note that this temporary namespace has been accessed in this
4436 : : * transaction.
4437 : : */
4438 : 3482 : MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
4439 : :
4440 : : /*
4441 : : * If the caller attempting to access a temporary schema expects the
4442 : : * creation of the namespace to be pending and should be enforced, then go
4443 : : * through the creation.
4444 : : */
4445 [ + + + + ]: 3482 : if (!force && OidIsValid(myTempNamespace))
4446 : 3149 : return;
4447 : :
4448 : : /*
4449 : : * The temporary tablespace does not exist yet and is wanted, so
4450 : : * initialize it.
4451 : : */
4452 : 333 : InitTempTableNamespace();
4453 : : }
4454 : :
4455 : : /*
4456 : : * InitTempTableNamespace
4457 : : * Initialize temp table namespace on first use in a particular backend
4458 : : */
4459 : : static void
8564 tgl@sss.pgh.pa.us 4460 : 333 : InitTempTableNamespace(void)
4461 : : {
4462 : : char namespaceName[NAMEDATALEN];
4463 : : Oid namespaceId;
4464 : : Oid toastspaceId;
4465 : :
6765 4466 [ - + ]: 333 : Assert(!OidIsValid(myTempNamespace));
4467 : :
4468 : : /*
4469 : : * First, do permission check to see if we are authorized to make temp
4470 : : * tables. We use a nonstandard error message here since "databasename:
4471 : : * permission denied" might be a tad cryptic.
4472 : : *
4473 : : * Note that ACL_CREATE_TEMP rights are rechecked in pg_namespace_aclmask;
4474 : : * that's necessary since current user ID could change during the session.
4475 : : * But there's no need to make the namespace in the first place until a
4476 : : * temp table creation request is made by someone with appropriate rights.
4477 : : */
1079 peter@eisentraut.org 4478 [ - + ]: 333 : if (object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(),
4479 : : ACL_CREATE_TEMP) != ACLCHECK_OK)
8134 tgl@sss.pgh.pa.us 4480 [ # # ]:UBC 0 : ereport(ERROR,
4481 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4482 : : errmsg("permission denied to create temporary tables in database \"%s\"",
4483 : : get_database_name(MyDatabaseId))));
4484 : :
4485 : : /*
4486 : : * Do not allow a Hot Standby session to make temp tables. Aside from
4487 : : * problems with modifying the system catalogs, there is a naming
4488 : : * conflict: pg_temp_N belongs to the session with proc number N on the
4489 : : * primary, not to a hot standby session with the same proc number. We
4490 : : * should not be able to get here anyway due to XactReadOnly checks, but
4491 : : * let's just make real sure. Note that this also backstops various
4492 : : * operations that allow XactReadOnly transactions to modify temp tables;
4493 : : * they'd need RecoveryInProgress checks if not for this.
4494 : : */
5728 tgl@sss.pgh.pa.us 4495 [ - + ]:CBC 333 : if (RecoveryInProgress())
5728 tgl@sss.pgh.pa.us 4496 [ # # ]:UBC 0 : ereport(ERROR,
4497 : : (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
4498 : : errmsg("cannot create temporary tables during recovery")));
4499 : :
4500 : : /* Parallel workers can't create temporary tables, either. */
3833 rhaas@postgresql.org 4501 [ - + ]:CBC 333 : if (IsParallelWorker())
3833 rhaas@postgresql.org 4502 [ # # ]:UBC 0 : ereport(ERROR,
4503 : : (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
4504 : : errmsg("cannot create temporary tables during a parallel operation")));
4505 : :
603 heikki.linnakangas@i 4506 :CBC 333 : snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyProcNumber);
4507 : :
5562 rhaas@postgresql.org 4508 : 333 : namespaceId = get_namespace_oid(namespaceName, true);
8611 tgl@sss.pgh.pa.us 4509 [ + + ]: 333 : if (!OidIsValid(namespaceId))
4510 : : {
4511 : : /*
4512 : : * First use of this temp namespace in this database; create it. The
4513 : : * temp namespaces are always owned by the superuser. We leave their
4514 : : * permissions at default --- i.e., no access except to superuser ---
4515 : : * to ensure that unprivileged users can't peek at other backends'
4516 : : * temp tables. This works because the places that access the temp
4517 : : * namespace for my own backend skip permissions checks on it.
4518 : : */
4981 4519 : 214 : namespaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
4520 : : true);
4521 : : /* Advance command counter to make namespace visible */
8611 4522 : 214 : CommandCounterIncrement();
4523 : : }
4524 : : else
4525 : : {
4526 : : /*
4527 : : * If the namespace already exists, clean it out (in case the former
4528 : : * owner crashed without doing so).
4529 : : */
4530 : 119 : RemoveTempRelations(namespaceId);
4531 : : }
4532 : :
4533 : : /*
4534 : : * If the corresponding toast-table namespace doesn't exist yet, create
4535 : : * it. (We assume there is no need to clean it out if it does exist, since
4536 : : * dropping a parent table should make its toast table go away.)
4537 : : */
6669 4538 : 333 : snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
4539 : : MyProcNumber);
4540 : :
5562 rhaas@postgresql.org 4541 : 333 : toastspaceId = get_namespace_oid(namespaceName, true);
6669 tgl@sss.pgh.pa.us 4542 [ + + ]: 333 : if (!OidIsValid(toastspaceId))
4543 : : {
4981 4544 : 214 : toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID,
4545 : : true);
4546 : : /* Advance command counter to make namespace visible */
6669 4547 : 214 : CommandCounterIncrement();
4548 : : }
4549 : :
4550 : : /*
4551 : : * Okay, we've prepared the temp namespace ... but it's not committed yet,
4552 : : * so all our work could be undone by transaction rollback. Set flag for
4553 : : * AtEOXact_Namespace to know what to do.
4554 : : */
8564 4555 : 333 : myTempNamespace = namespaceId;
6669 4556 : 333 : myTempToastNamespace = toastspaceId;
4557 : :
4558 : : /*
4559 : : * Mark MyProc as owning this namespace which other processes can use to
4560 : : * decide if a temporary namespace is in use or not. We assume that
4561 : : * assignment of namespaceId is an atomic operation. Even if it is not,
4562 : : * the temporary relation which resulted in the creation of this temporary
4563 : : * namespace is still locked until the current transaction commits, and
4564 : : * its pg_namespace row is not visible yet. However it does not matter:
4565 : : * this flag makes the namespace as being in use, so no objects created on
4566 : : * it would be removed concurrently.
4567 : : */
2632 michael@paquier.xyz 4568 : 333 : MyProc->tempNamespaceId = namespaceId;
4569 : :
4570 : : /* It should not be done already. */
1095 peter@eisentraut.org 4571 [ - + ]: 333 : Assert(myTempNamespaceSubID == InvalidSubTransactionId);
7711 tgl@sss.pgh.pa.us 4572 : 333 : myTempNamespaceSubID = GetCurrentSubTransactionId();
4573 : :
6793 4574 : 333 : baseSearchPathValid = false; /* need to rebuild list */
713 jdavis@postgresql.or 4575 : 333 : searchPathCacheValid = false;
8564 tgl@sss.pgh.pa.us 4576 : 333 : }
4577 : :
4578 : : /*
4579 : : * End-of-transaction cleanup for namespaces.
4580 : : */
4581 : : void
3833 rhaas@postgresql.org 4582 : 319678 : AtEOXact_Namespace(bool isCommit, bool parallel)
4583 : : {
4584 : : /*
4585 : : * If we abort the transaction in which a temp namespace was selected,
4586 : : * we'll have to do any creation or cleanout work over again. So, just
4587 : : * forget the namespace entirely until next time. On the other hand, if
4588 : : * we commit then register an exit callback to clean out the temp tables
4589 : : * at backend shutdown. (We only want to register the callback once per
4590 : : * session, so this is a good place to do it.)
4591 : : */
4592 [ + + + - ]: 319678 : if (myTempNamespaceSubID != InvalidSubTransactionId && !parallel)
4593 : : {
8564 tgl@sss.pgh.pa.us 4594 [ + + ]: 332 : if (isCommit)
4331 rhaas@postgresql.org 4595 : 319 : before_shmem_exit(RemoveTempRelationsCallback, 0);
4596 : : else
4597 : : {
8564 tgl@sss.pgh.pa.us 4598 : 13 : myTempNamespace = InvalidOid;
6669 4599 : 13 : myTempToastNamespace = InvalidOid;
3050 4600 : 13 : baseSearchPathValid = false; /* need to rebuild list */
713 jdavis@postgresql.or 4601 : 13 : searchPathCacheValid = false;
4602 : :
4603 : : /*
4604 : : * Reset the temporary namespace flag in MyProc. We assume that
4605 : : * this operation is atomic.
4606 : : *
4607 : : * Because this transaction is aborting, the pg_namespace row is
4608 : : * not visible to anyone else anyway, but that doesn't matter:
4609 : : * it's not a problem if objects contained in this namespace are
4610 : : * removed concurrently.
4611 : : */
2632 michael@paquier.xyz 4612 : 13 : MyProc->tempNamespaceId = InvalidOid;
4613 : : }
7711 tgl@sss.pgh.pa.us 4614 : 332 : myTempNamespaceSubID = InvalidSubTransactionId;
4615 : : }
4616 : :
8611 4617 : 319678 : }
4618 : :
4619 : : /*
4620 : : * AtEOSubXact_Namespace
4621 : : *
4622 : : * At subtransaction commit, propagate the temp-namespace-creation
4623 : : * flag to the parent subtransaction.
4624 : : *
4625 : : * At subtransaction abort, forget the flag if we set it up.
4626 : : */
4627 : : void
7711 4628 : 9093 : AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid,
4629 : : SubTransactionId parentSubid)
4630 : : {
4631 : :
4632 [ + + ]: 9093 : if (myTempNamespaceSubID == mySubid)
4633 : : {
7761 4634 [ + + ]: 4 : if (isCommit)
7711 4635 : 3 : myTempNamespaceSubID = parentSubid;
4636 : : else
4637 : : {
4638 : 1 : myTempNamespaceSubID = InvalidSubTransactionId;
4639 : : /* TEMP namespace creation failed, so reset state */
7761 4640 : 1 : myTempNamespace = InvalidOid;
6669 4641 : 1 : myTempToastNamespace = InvalidOid;
3050 4642 : 1 : baseSearchPathValid = false; /* need to rebuild list */
713 jdavis@postgresql.or 4643 : 1 : searchPathCacheValid = false;
4644 : :
4645 : : /*
4646 : : * Reset the temporary namespace flag in MyProc. We assume that
4647 : : * this operation is atomic.
4648 : : *
4649 : : * Because this subtransaction is aborting, the pg_namespace row
4650 : : * is not visible to anyone else anyway, but that doesn't matter:
4651 : : * it's not a problem if objects contained in this namespace are
4652 : : * removed concurrently.
4653 : : */
2632 michael@paquier.xyz 4654 : 1 : MyProc->tempNamespaceId = InvalidOid;
4655 : : }
4656 : : }
7761 tgl@sss.pgh.pa.us 4657 : 9093 : }
4658 : :
4659 : : /*
4660 : : * Remove all relations in the specified temp namespace.
4661 : : *
4662 : : * This is called at backend shutdown (if we made any temp relations).
4663 : : * It is also called when we begin using a pre-existing temp namespace,
4664 : : * in order to clean out any relations that might have been created by
4665 : : * a crashed backend.
4666 : : */
4667 : : static void
8611 4668 : 445 : RemoveTempRelations(Oid tempNamespaceId)
4669 : : {
4670 : : ObjectAddress object;
4671 : :
4672 : : /*
4673 : : * We want to get rid of everything in the target namespace, but not the
4674 : : * namespace itself (deleting it only to recreate it later would be a
4675 : : * waste of cycles). Hence, specify SKIP_ORIGINAL. It's also an INTERNAL
4676 : : * deletion, and we want to not drop any extensions that might happen to
4677 : : * own temp objects.
4678 : : */
7501 4679 : 445 : object.classId = NamespaceRelationId;
8298 4680 : 445 : object.objectId = tempNamespaceId;
4681 : 445 : object.objectSubId = 0;
4682 : :
3251 4683 : 445 : performDeletion(&object, DROP_CASCADE,
4684 : : PERFORM_DELETION_INTERNAL |
4685 : : PERFORM_DELETION_QUIETLY |
4686 : : PERFORM_DELETION_SKIP_ORIGINAL |
4687 : : PERFORM_DELETION_SKIP_EXTENSIONS);
8611 4688 : 445 : }
4689 : :
4690 : : /*
4691 : : * Callback to remove temp relations at backend exit.
4692 : : */
4693 : : static void
7990 peter_e@gmx.net 4694 : 319 : RemoveTempRelationsCallback(int code, Datum arg)
4695 : : {
8454 bruce@momjian.us 4696 [ + - ]: 319 : if (OidIsValid(myTempNamespace)) /* should always be true */
4697 : : {
4698 : : /* Need to ensure we have a usable transaction. */
8611 tgl@sss.pgh.pa.us 4699 : 319 : AbortOutOfAnyTransaction();
8202 4700 : 319 : StartTransactionCommand();
1344 andres@anarazel.de 4701 : 319 : PushActiveSnapshot(GetTransactionSnapshot());
4702 : :
8611 tgl@sss.pgh.pa.us 4703 : 319 : RemoveTempRelations(myTempNamespace);
4704 : :
1344 andres@anarazel.de 4705 : 319 : PopActiveSnapshot();
8202 tgl@sss.pgh.pa.us 4706 : 319 : CommitTransactionCommand();
4707 : : }
8611 4708 : 319 : }
4709 : :
4710 : : /*
4711 : : * Remove all temp tables from the temporary namespace.
4712 : : */
4713 : : void
6765 4714 : 7 : ResetTempTableNamespace(void)
4715 : : {
4716 [ + - ]: 7 : if (OidIsValid(myTempNamespace))
4717 : 7 : RemoveTempRelations(myTempNamespace);
4718 : 7 : }
4719 : :
4720 : :
4721 : : /*
4722 : : * Routines for handling the GUC variable 'search_path'.
4723 : : */
4724 : :
4725 : : /* check_hook: validate new search_path value */
4726 : : bool
5317 4727 : 85364 : check_search_path(char **newval, void **extra, GucSource source)
4728 : : {
707 jdavis@postgresql.or 4729 : 85364 : Oid roleid = InvalidOid;
4730 : 85364 : const char *searchPath = *newval;
4731 : : char *rawname;
4732 : : List *namelist;
4733 : 85364 : bool use_cache = (SearchPathCacheContext != NULL);
4734 : :
4735 : : /*
4736 : : * We used to try to check that the named schemas exist, but there are
4737 : : * many valid use-cases for having search_path settings that include
4738 : : * schemas that don't exist; and often, we are not inside a transaction
4739 : : * here and so can't consult the system catalogs anyway. So now, the only
4740 : : * requirement is syntactic validity of the identifier list.
4741 : : *
4742 : : * Checking only the syntactic validity also allows us to use the search
4743 : : * path cache (if available) to avoid calling SplitIdentifierString() on
4744 : : * the same string repeatedly.
4745 : : */
4746 [ + + ]: 85364 : if (use_cache)
4747 : : {
4748 : 75791 : spcache_init();
4749 : :
4750 : 75791 : roleid = GetUserId();
4751 : :
4752 [ + + ]: 75791 : if (spcache_lookup(searchPath, roleid) != NULL)
4753 : 71223 : return true;
4754 : : }
4755 : :
4756 : : /*
4757 : : * Ensure validity check succeeds before creating cache entry.
4758 : : */
4759 : :
4760 : 14141 : rawname = pstrdup(searchPath); /* need a modifiable copy */
4761 : :
4762 : : /* Parse string into list of identifiers */
8610 tgl@sss.pgh.pa.us 4763 [ - + ]: 14141 : if (!SplitIdentifierString(rawname, ',', &namelist))
4764 : : {
4765 : : /* syntax error in name list */
5317 tgl@sss.pgh.pa.us 4766 :UBC 0 : GUC_check_errdetail("List syntax is invalid.");
8610 4767 : 0 : pfree(rawname);
7824 neilc@samurai.com 4768 : 0 : list_free(namelist);
5317 tgl@sss.pgh.pa.us 4769 : 0 : return false;
4770 : : }
8610 tgl@sss.pgh.pa.us 4771 :CBC 14141 : pfree(rawname);
7824 neilc@samurai.com 4772 : 14141 : list_free(namelist);
4773 : :
4774 : : /* OK to create empty cache entry */
707 jdavis@postgresql.or 4775 [ + + ]: 14141 : if (use_cache)
4776 : 4568 : (void) spcache_insert(searchPath, roleid);
4777 : :
4947 tgl@sss.pgh.pa.us 4778 : 14141 : return true;
4779 : : }
4780 : :
4781 : : /* assign_hook: do extra actions as needed */
4782 : : void
5317 4783 : 165613 : assign_search_path(const char *newval, void *extra)
4784 : : {
4785 : : /* don't access search_path during bootstrap */
602 jdavis@postgresql.or 4786 [ - + ]: 165613 : Assert(!IsBootstrapProcessingMode());
4787 : :
4788 : : /*
4789 : : * We mark the path as needing recomputation, but don't do anything until
4790 : : * it's needed. This avoids trying to do database access during GUC
4791 : : * initialization, or outside a transaction.
4792 : : *
4793 : : * This does not invalidate the search path cache, so if this value had
4794 : : * been previously set and no syscache invalidations happened,
4795 : : * recomputation may not be necessary.
4796 : : */
5317 tgl@sss.pgh.pa.us 4797 : 165613 : baseSearchPathValid = false;
8610 4798 : 165613 : }
4799 : :
4800 : : /*
4801 : : * InitializeSearchPath: initialize module during InitPostgres.
4802 : : *
4803 : : * This is called after we are up enough to be able to do catalog lookups.
4804 : : */
4805 : : void
4806 : 13487 : InitializeSearchPath(void)
4807 : : {
8596 4808 [ + + ]: 13487 : if (IsBootstrapProcessingMode())
4809 : : {
4810 : : /*
4811 : : * In bootstrap mode, the search path must be 'pg_catalog' so that
4812 : : * tables are created in the proper namespace; ignore the GUC setting.
4813 : : */
4814 : : MemoryContext oldcxt;
4815 : :
4816 : 50 : oldcxt = MemoryContextSwitchTo(TopMemoryContext);
6793 4817 : 50 : baseSearchPath = list_make1_oid(PG_CATALOG_NAMESPACE);
8596 4818 : 50 : MemoryContextSwitchTo(oldcxt);
6793 4819 : 50 : baseCreationNamespace = PG_CATALOG_NAMESPACE;
6765 4820 : 50 : baseTempCreationPending = false;
6793 4821 : 50 : baseSearchPathValid = true;
8582 4822 : 50 : namespaceUser = GetUserId();
6793 4823 : 50 : activeSearchPath = baseSearchPath;
4824 : 50 : activeCreationNamespace = baseCreationNamespace;
6765 4825 : 50 : activeTempCreationPending = baseTempCreationPending;
2041 4826 : 50 : activePathGeneration++; /* pro forma */
4827 : : }
4828 : : else
4829 : : {
4830 : : /*
4831 : : * In normal mode, arrange for a callback on any syscache invalidation
4832 : : * that will affect the search_path cache.
4833 : : */
4834 : :
4835 : : /* namespace name or ACLs may have changed */
8582 4836 : 13437 : CacheRegisterSyscacheCallback(NAMESPACEOID,
4837 : : InvalidationCallback,
4838 : : (Datum) 0);
4839 : :
4840 : : /* role name may affect the meaning of "$user" */
812 jdavis@postgresql.or 4841 : 13437 : CacheRegisterSyscacheCallback(AUTHOID,
4842 : : InvalidationCallback,
4843 : : (Datum) 0);
4844 : :
4845 : : /* role membership may affect ACLs */
475 4846 : 13437 : CacheRegisterSyscacheCallback(AUTHMEMROLEMEM,
4847 : : InvalidationCallback,
4848 : : (Datum) 0);
4849 : :
4850 : : /* database owner may affect ACLs */
4851 : 13437 : CacheRegisterSyscacheCallback(DATABASEOID,
4852 : : InvalidationCallback,
4853 : : (Datum) 0);
4854 : :
4855 : : /* Force search path to be recomputed on next use */
6793 tgl@sss.pgh.pa.us 4856 : 13437 : baseSearchPathValid = false;
713 jdavis@postgresql.or 4857 : 13437 : searchPathCacheValid = false;
4858 : : }
8610 tgl@sss.pgh.pa.us 4859 : 13487 : }
4860 : :
4861 : : /*
4862 : : * InvalidationCallback
4863 : : * Syscache inval callback function
4864 : : */
4865 : : static void
475 jdavis@postgresql.or 4866 : 38099 : InvalidationCallback(Datum arg, int cacheid, uint32 hashvalue)
4867 : : {
4868 : : /*
4869 : : * Force search path to be recomputed on next use, also invalidating the
4870 : : * search path cache (because namespace names, ACLs, or role names may
4871 : : * have changed).
4872 : : */
6793 tgl@sss.pgh.pa.us 4873 : 38099 : baseSearchPathValid = false;
713 jdavis@postgresql.or 4874 : 38099 : searchPathCacheValid = false;
8582 tgl@sss.pgh.pa.us 4875 : 38099 : }
4876 : :
4877 : : /*
4878 : : * Fetch the active search path. The return value is a palloc'ed list
4879 : : * of OIDs; the caller is responsible for freeing this storage as
4880 : : * appropriate.
4881 : : *
4882 : : * The returned list includes the implicitly-prepended namespaces only if
4883 : : * includeImplicit is true.
4884 : : *
4885 : : * Note: calling this may result in a CommandCounterIncrement operation,
4886 : : * if we have to create or clean out the temp namespace.
4887 : : */
4888 : : List *
8564 4889 : 482 : fetch_search_path(bool includeImplicit)
4890 : : {
4891 : : List *result;
4892 : :
8582 4893 : 482 : recomputeNamespacePath();
4894 : :
4895 : : /*
4896 : : * If the temp namespace should be first, force it to exist. This is so
4897 : : * that callers can trust the result to reflect the actual default
4898 : : * creation namespace. It's a bit bogus to do this here, since
4899 : : * current_schema() is supposedly a stable function without side-effects,
4900 : : * but the alternatives seem worse.
4901 : : */
6765 4902 [ + + ]: 482 : if (activeTempCreationPending)
4903 : : {
2474 michael@paquier.xyz 4904 : 3 : AccessTempTableNamespace(true);
6765 tgl@sss.pgh.pa.us 4905 : 3 : recomputeNamespacePath();
4906 : : }
4907 : :
6793 4908 : 482 : result = list_copy(activeSearchPath);
8564 4909 [ + + ]: 482 : if (!includeImplicit)
4910 : : {
6793 4911 [ + + + + ]: 817 : while (result && linitial_oid(result) != activeCreationNamespace)
7824 neilc@samurai.com 4912 : 428 : result = list_delete_first(result);
4913 : : }
4914 : :
8564 tgl@sss.pgh.pa.us 4915 : 482 : return result;
4916 : : }
4917 : :
4918 : : /*
4919 : : * Fetch the active search path into a caller-allocated array of OIDs.
4920 : : * Returns the number of path entries. (If this is more than sarray_len,
4921 : : * then the data didn't fit and is not all stored.)
4922 : : *
4923 : : * The returned list always includes the implicitly-prepended namespaces,
4924 : : * but never includes the temp namespace. (This is suitable for existing
4925 : : * users, which would want to ignore the temp namespace anyway.) This
4926 : : * definition allows us to not worry about initializing the temp namespace.
4927 : : */
4928 : : int
6543 4929 : 344230 : fetch_search_path_array(Oid *sarray, int sarray_len)
4930 : : {
4931 : 344230 : int count = 0;
4932 : : ListCell *l;
4933 : :
4934 : 344230 : recomputeNamespacePath();
4935 : :
4936 [ + - + + : 1019964 : foreach(l, activeSearchPath)
+ + ]
4937 : : {
4938 : 675734 : Oid namespaceId = lfirst_oid(l);
4939 : :
4940 [ + + ]: 675734 : if (namespaceId == myTempNamespace)
4941 : 92194 : continue; /* do not include temp namespace */
4942 : :
4943 [ + - ]: 583540 : if (count < sarray_len)
4944 : 583540 : sarray[count] = namespaceId;
4945 : 583540 : count++;
4946 : : }
4947 : :
4948 : 344230 : return count;
4949 : : }
4950 : :
4951 : :
4952 : : /*
4953 : : * Export the FooIsVisible functions as SQL-callable functions.
4954 : : *
4955 : : * Note: as of Postgres 8.4, these will silently return NULL if called on
4956 : : * a nonexistent object OID, rather than failing. This is to avoid race
4957 : : * condition errors when a query that's scanning a catalog using an MVCC
4958 : : * snapshot uses one of these functions. The underlying IsVisible functions
4959 : : * always use an up-to-date snapshot and so might see the object as already
4960 : : * gone when it's still visible to the transaction snapshot.
4961 : : */
4962 : :
4963 : : Datum
8480 4964 : 11856 : pg_table_is_visible(PG_FUNCTION_ARGS)
4965 : : {
4966 : 11856 : Oid oid = PG_GETARG_OID(0);
4967 : : bool result;
744 4968 : 11856 : bool is_missing = false;
4969 : :
4970 : 11856 : result = RelationIsVisibleExt(oid, &is_missing);
4971 : :
4972 [ + + ]: 11856 : if (is_missing)
4973 : 1 : PG_RETURN_NULL();
4974 : 11855 : PG_RETURN_BOOL(result);
4975 : : }
4976 : :
4977 : : Datum
8480 4978 : 2001 : pg_type_is_visible(PG_FUNCTION_ARGS)
4979 : : {
4980 : 2001 : Oid oid = PG_GETARG_OID(0);
4981 : : bool result;
744 4982 : 2001 : bool is_missing = false;
4983 : :
4984 : 2001 : result = TypeIsVisibleExt(oid, &is_missing);
4985 : :
4986 [ - + ]: 2001 : if (is_missing)
744 tgl@sss.pgh.pa.us 4987 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 4988 :CBC 2001 : PG_RETURN_BOOL(result);
4989 : : }
4990 : :
4991 : : Datum
8480 4992 : 3798 : pg_function_is_visible(PG_FUNCTION_ARGS)
4993 : : {
4994 : 3798 : Oid oid = PG_GETARG_OID(0);
4995 : : bool result;
744 4996 : 3798 : bool is_missing = false;
4997 : :
4998 : 3798 : result = FunctionIsVisibleExt(oid, &is_missing);
4999 : :
5000 [ - + ]: 3798 : if (is_missing)
744 tgl@sss.pgh.pa.us 5001 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 5002 :CBC 3798 : PG_RETURN_BOOL(result);
5003 : : }
5004 : :
5005 : : Datum
8480 5006 : 850 : pg_operator_is_visible(PG_FUNCTION_ARGS)
5007 : : {
5008 : 850 : Oid oid = PG_GETARG_OID(0);
5009 : : bool result;
744 5010 : 850 : bool is_missing = false;
5011 : :
5012 : 850 : result = OperatorIsVisibleExt(oid, &is_missing);
5013 : :
5014 [ - + ]: 850 : if (is_missing)
744 tgl@sss.pgh.pa.us 5015 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 5016 :CBC 850 : PG_RETURN_BOOL(result);
5017 : : }
5018 : :
5019 : : Datum
8480 5020 : 9 : pg_opclass_is_visible(PG_FUNCTION_ARGS)
5021 : : {
5022 : 9 : Oid oid = PG_GETARG_OID(0);
5023 : : bool result;
744 5024 : 9 : bool is_missing = false;
5025 : :
5026 : 9 : result = OpclassIsVisibleExt(oid, &is_missing);
5027 : :
5028 [ - + ]: 9 : if (is_missing)
744 tgl@sss.pgh.pa.us 5029 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 5030 :CBC 9 : PG_RETURN_BOOL(result);
5031 : : }
5032 : :
5033 : : Datum
5216 rhaas@postgresql.org 5034 : 159 : pg_opfamily_is_visible(PG_FUNCTION_ARGS)
5035 : : {
5036 : 159 : Oid oid = PG_GETARG_OID(0);
5037 : : bool result;
744 tgl@sss.pgh.pa.us 5038 : 159 : bool is_missing = false;
5039 : :
5040 : 159 : result = OpfamilyIsVisibleExt(oid, &is_missing);
5041 : :
5042 [ - + ]: 159 : if (is_missing)
744 tgl@sss.pgh.pa.us 5043 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 5044 :CBC 159 : PG_RETURN_BOOL(result);
5045 : : }
5046 : :
5047 : : Datum
5375 peter_e@gmx.net 5048 :UBC 0 : pg_collation_is_visible(PG_FUNCTION_ARGS)
5049 : : {
5050 : 0 : Oid oid = PG_GETARG_OID(0);
5051 : : bool result;
744 tgl@sss.pgh.pa.us 5052 : 0 : bool is_missing = false;
5053 : :
5054 : 0 : result = CollationIsVisibleExt(oid, &is_missing);
5055 : :
5056 [ # # ]: 0 : if (is_missing)
5057 : 0 : PG_RETURN_NULL();
5058 : 0 : PG_RETURN_BOOL(result);
5059 : : }
5060 : :
5061 : : Datum
8355 bruce@momjian.us 5062 : 0 : pg_conversion_is_visible(PG_FUNCTION_ARGS)
5063 : : {
5064 : 0 : Oid oid = PG_GETARG_OID(0);
5065 : : bool result;
744 tgl@sss.pgh.pa.us 5066 : 0 : bool is_missing = false;
5067 : :
5068 : 0 : result = ConversionIsVisibleExt(oid, &is_missing);
5069 : :
5070 [ # # ]: 0 : if (is_missing)
5071 : 0 : PG_RETURN_NULL();
5072 : 0 : PG_RETURN_BOOL(result);
5073 : : }
5074 : :
5075 : : Datum
3088 tgl@sss.pgh.pa.us 5076 :CBC 186 : pg_statistics_obj_is_visible(PG_FUNCTION_ARGS)
5077 : : {
3089 alvherre@alvh.no-ip. 5078 : 186 : Oid oid = PG_GETARG_OID(0);
5079 : : bool result;
744 tgl@sss.pgh.pa.us 5080 : 186 : bool is_missing = false;
5081 : :
5082 : 186 : result = StatisticsObjIsVisibleExt(oid, &is_missing);
5083 : :
5084 [ - + ]: 186 : if (is_missing)
744 tgl@sss.pgh.pa.us 5085 :UBC 0 : PG_RETURN_NULL();
744 tgl@sss.pgh.pa.us 5086 :CBC 186 : PG_RETURN_BOOL(result);
5087 : : }
5088 : :
5089 : : Datum
6642 tgl@sss.pgh.pa.us 5090 :UBC 0 : pg_ts_parser_is_visible(PG_FUNCTION_ARGS)
5091 : : {
5092 : 0 : Oid oid = PG_GETARG_OID(0);
5093 : : bool result;
744 5094 : 0 : bool is_missing = false;
5095 : :
5096 : 0 : result = TSParserIsVisibleExt(oid, &is_missing);
5097 : :
5098 [ # # ]: 0 : if (is_missing)
5099 : 0 : PG_RETURN_NULL();
5100 : 0 : PG_RETURN_BOOL(result);
5101 : : }
5102 : :
5103 : : Datum
6642 5104 : 0 : pg_ts_dict_is_visible(PG_FUNCTION_ARGS)
5105 : : {
5106 : 0 : Oid oid = PG_GETARG_OID(0);
5107 : : bool result;
744 5108 : 0 : bool is_missing = false;
5109 : :
5110 : 0 : result = TSDictionaryIsVisibleExt(oid, &is_missing);
5111 : :
5112 [ # # ]: 0 : if (is_missing)
5113 : 0 : PG_RETURN_NULL();
5114 : 0 : PG_RETURN_BOOL(result);
5115 : : }
5116 : :
5117 : : Datum
6642 5118 : 0 : pg_ts_template_is_visible(PG_FUNCTION_ARGS)
5119 : : {
5120 : 0 : Oid oid = PG_GETARG_OID(0);
5121 : : bool result;
744 5122 : 0 : bool is_missing = false;
5123 : :
5124 : 0 : result = TSTemplateIsVisibleExt(oid, &is_missing);
5125 : :
5126 [ # # ]: 0 : if (is_missing)
5127 : 0 : PG_RETURN_NULL();
5128 : 0 : PG_RETURN_BOOL(result);
5129 : : }
5130 : :
5131 : : Datum
6642 5132 : 0 : pg_ts_config_is_visible(PG_FUNCTION_ARGS)
5133 : : {
5134 : 0 : Oid oid = PG_GETARG_OID(0);
5135 : : bool result;
744 5136 : 0 : bool is_missing = false;
5137 : :
5138 : 0 : result = TSConfigIsVisibleExt(oid, &is_missing);
5139 : :
5140 [ # # ]: 0 : if (is_missing)
5141 : 0 : PG_RETURN_NULL();
5142 : 0 : PG_RETURN_BOOL(result);
5143 : : }
5144 : :
5145 : : Datum
6983 tgl@sss.pgh.pa.us 5146 :CBC 1634 : pg_my_temp_schema(PG_FUNCTION_ARGS)
5147 : : {
5148 : 1634 : PG_RETURN_OID(myTempNamespace);
5149 : : }
5150 : :
5151 : : Datum
5152 : 10637 : pg_is_other_temp_schema(PG_FUNCTION_ARGS)
5153 : : {
5154 : 10637 : Oid oid = PG_GETARG_OID(0);
5155 : :
5156 : 10637 : PG_RETURN_BOOL(isOtherTempNamespace(oid));
5157 : : }
|