PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regproc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * regproc.c
4  * Functions for the built-in types regproc, regclass, regtype, etc.
5  *
6  * These types are all binary-compatible with type Oid, and rely on Oid
7  * for comparison and so forth. Their only interesting behavior is in
8  * special I/O conversion routines.
9  *
10  *
11  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  *
15  * IDENTIFICATION
16  * src/backend/utils/adt/regproc.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include <ctype.h>
23 
24 #include "access/htup_details.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_class.h"
27 #include "catalog/pg_operator.h"
28 #include "catalog/pg_proc.h"
29 #include "catalog/pg_ts_config.h"
30 #include "catalog/pg_ts_dict.h"
31 #include "catalog/pg_type.h"
32 #include "lib/stringinfo.h"
33 #include "miscadmin.h"
34 #include "parser/parse_type.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/syscache.h"
38 #include "utils/acl.h"
39 #include "utils/regproc.h"
40 #include "utils/varlena.h"
41 
42 static char *format_operator_internal(Oid operator_oid, bool force_qualify);
43 static char *format_procedure_internal(Oid procedure_oid, bool force_qualify);
44 static void parseNameAndArgTypes(const char *string, bool allowNone,
45  List **names, int *nargs, Oid *argtypes);
46 
47 
48 /*****************************************************************************
49  * USER I/O ROUTINES *
50  *****************************************************************************/
51 
52 /*
53  * regprocin - converts "proname" to proc OID
54  *
55  * We also accept a numeric OID, for symmetry with the output routine.
56  *
57  * '-' signifies unknown (OID 0). In all other cases, the input must
58  * match an existing pg_proc entry.
59  */
60 Datum
62 {
63  char *pro_name_or_oid = PG_GETARG_CSTRING(0);
65  List *names;
66  FuncCandidateList clist;
67 
68  /* '-' ? */
69  if (strcmp(pro_name_or_oid, "-") == 0)
71 
72  /* Numeric OID? */
73  if (pro_name_or_oid[0] >= '0' &&
74  pro_name_or_oid[0] <= '9' &&
75  strspn(pro_name_or_oid, "0123456789") == strlen(pro_name_or_oid))
76  {
78  CStringGetDatum(pro_name_or_oid)));
79  PG_RETURN_OID(result);
80  }
81 
82  /* Else it's a name, possibly schema-qualified */
83 
84  /*
85  * We should never get here in bootstrap mode, as all references should
86  * have been resolved by genbki.pl.
87  */
89  elog(ERROR, "regproc values must be OIDs in bootstrap mode");
90 
91  /*
92  * Normal case: parse the name into components and see if it matches any
93  * pg_proc entries in the current search path.
94  */
95  names = stringToQualifiedNameList(pro_name_or_oid);
96  clist = FuncnameGetCandidates(names, -1, NIL, false, false, false);
97 
98  if (clist == NULL)
99  ereport(ERROR,
100  (errcode(ERRCODE_UNDEFINED_FUNCTION),
101  errmsg("function \"%s\" does not exist", pro_name_or_oid)));
102  else if (clist->next != NULL)
103  ereport(ERROR,
104  (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
105  errmsg("more than one function named \"%s\"",
106  pro_name_or_oid)));
107 
108  result = clist->oid;
109 
110  PG_RETURN_OID(result);
111 }
112 
113 /*
114  * to_regproc - converts "proname" to proc OID
115  *
116  * If the name is not found, we return NULL.
117  */
118 Datum
120 {
121  char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
122  List *names;
123  FuncCandidateList clist;
124 
125  /*
126  * Parse the name into components and see if it matches any pg_proc
127  * entries in the current search path.
128  */
129  names = stringToQualifiedNameList(pro_name);
130  clist = FuncnameGetCandidates(names, -1, NIL, false, false, true);
131 
132  if (clist == NULL || clist->next != NULL)
133  PG_RETURN_NULL();
134 
135  PG_RETURN_OID(clist->oid);
136 }
137 
138 /*
139  * regprocout - converts proc OID to "pro_name"
140  */
141 Datum
143 {
144  RegProcedure proid = PG_GETARG_OID(0);
145  char *result;
146  HeapTuple proctup;
147 
148  if (proid == InvalidOid)
149  {
150  result = pstrdup("-");
151  PG_RETURN_CSTRING(result);
152  }
153 
154  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(proid));
155 
156  if (HeapTupleIsValid(proctup))
157  {
158  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
159  char *proname = NameStr(procform->proname);
160 
161  /*
162  * In bootstrap mode, skip the fancy namespace stuff and just return
163  * the proc name. (This path is only needed for debugging output
164  * anyway.)
165  */
167  result = pstrdup(proname);
168  else
169  {
170  char *nspname;
171  FuncCandidateList clist;
172 
173  /*
174  * Would this proc be found (uniquely!) by regprocin? If not,
175  * qualify it.
176  */
177  clist = FuncnameGetCandidates(list_make1(makeString(proname)),
178  -1, NIL, false, false, false);
179  if (clist != NULL && clist->next == NULL &&
180  clist->oid == proid)
181  nspname = NULL;
182  else
183  nspname = get_namespace_name(procform->pronamespace);
184 
185  result = quote_qualified_identifier(nspname, proname);
186  }
187 
188  ReleaseSysCache(proctup);
189  }
190  else
191  {
192  /* If OID doesn't match any pg_proc entry, return it numerically */
193  result = (char *) palloc(NAMEDATALEN);
194  snprintf(result, NAMEDATALEN, "%u", proid);
195  }
196 
197  PG_RETURN_CSTRING(result);
198 }
199 
200 /*
201  * regprocrecv - converts external binary format to regproc
202  */
203 Datum
205 {
206  /* Exactly the same as oidrecv, so share code */
207  return oidrecv(fcinfo);
208 }
209 
210 /*
211  * regprocsend - converts regproc to binary format
212  */
213 Datum
215 {
216  /* Exactly the same as oidsend, so share code */
217  return oidsend(fcinfo);
218 }
219 
220 
221 /*
222  * regprocedurein - converts "proname(args)" to proc OID
223  *
224  * We also accept a numeric OID, for symmetry with the output routine.
225  *
226  * '-' signifies unknown (OID 0). In all other cases, the input must
227  * match an existing pg_proc entry.
228  */
229 Datum
231 {
232  char *pro_name_or_oid = PG_GETARG_CSTRING(0);
234  List *names;
235  int nargs;
236  Oid argtypes[FUNC_MAX_ARGS];
237  FuncCandidateList clist;
238 
239  /* '-' ? */
240  if (strcmp(pro_name_or_oid, "-") == 0)
242 
243  /* Numeric OID? */
244  if (pro_name_or_oid[0] >= '0' &&
245  pro_name_or_oid[0] <= '9' &&
246  strspn(pro_name_or_oid, "0123456789") == strlen(pro_name_or_oid))
247  {
249  CStringGetDatum(pro_name_or_oid)));
250  PG_RETURN_OID(result);
251  }
252 
253  /* The rest of this wouldn't work in bootstrap mode */
255  elog(ERROR, "regprocedure values must be OIDs in bootstrap mode");
256 
257  /*
258  * Else it's a name and arguments. Parse the name and arguments, look up
259  * potential matches in the current namespace search list, and scan to see
260  * which one exactly matches the given argument types. (There will not be
261  * more than one match.)
262  */
263  parseNameAndArgTypes(pro_name_or_oid, false, &names, &nargs, argtypes);
264 
265  clist = FuncnameGetCandidates(names, nargs, NIL, false, false, false);
266 
267  for (; clist; clist = clist->next)
268  {
269  if (memcmp(clist->args, argtypes, nargs * sizeof(Oid)) == 0)
270  break;
271  }
272 
273  if (clist == NULL)
274  ereport(ERROR,
275  (errcode(ERRCODE_UNDEFINED_FUNCTION),
276  errmsg("function \"%s\" does not exist", pro_name_or_oid)));
277 
278  result = clist->oid;
279 
280  PG_RETURN_OID(result);
281 }
282 
283 /*
284  * to_regprocedure - converts "proname(args)" to proc OID
285  *
286  * If the name is not found, we return NULL.
287  */
288 Datum
290 {
291  char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
292  List *names;
293  int nargs;
294  Oid argtypes[FUNC_MAX_ARGS];
295  FuncCandidateList clist;
296 
297  /*
298  * Parse the name and arguments, look up potential matches in the current
299  * namespace search list, and scan to see which one exactly matches the
300  * given argument types. (There will not be more than one match.)
301  */
302  parseNameAndArgTypes(pro_name, false, &names, &nargs, argtypes);
303 
304  clist = FuncnameGetCandidates(names, nargs, NIL, false, false, true);
305 
306  for (; clist; clist = clist->next)
307  {
308  if (memcmp(clist->args, argtypes, nargs * sizeof(Oid)) == 0)
309  PG_RETURN_OID(clist->oid);
310  }
311 
312  PG_RETURN_NULL();
313 }
314 
315 /*
316  * format_procedure - converts proc OID to "pro_name(args)"
317  *
318  * This exports the useful functionality of regprocedureout for use
319  * in other backend modules. The result is a palloc'd string.
320  */
321 char *
322 format_procedure(Oid procedure_oid)
323 {
324  return format_procedure_internal(procedure_oid, false);
325 }
326 
327 char *
329 {
330  return format_procedure_internal(procedure_oid, true);
331 }
332 
333 /*
334  * Routine to produce regprocedure names; see format_procedure above.
335  *
336  * force_qualify says whether to schema-qualify; if true, the name is always
337  * qualified regardless of search_path visibility. Otherwise the name is only
338  * qualified if the function is not in path.
339  */
340 static char *
341 format_procedure_internal(Oid procedure_oid, bool force_qualify)
342 {
343  char *result;
344  HeapTuple proctup;
345 
346  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procedure_oid));
347 
348  if (HeapTupleIsValid(proctup))
349  {
350  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(proctup);
351  char *proname = NameStr(procform->proname);
352  int nargs = procform->pronargs;
353  int i;
354  char *nspname;
356 
357  /* XXX no support here for bootstrap mode */
359 
360  initStringInfo(&buf);
361 
362  /*
363  * Would this proc be found (given the right args) by regprocedurein?
364  * If not, or if caller requests it, we need to qualify it.
365  */
366  if (!force_qualify && FunctionIsVisible(procedure_oid))
367  nspname = NULL;
368  else
369  nspname = get_namespace_name(procform->pronamespace);
370 
371  appendStringInfo(&buf, "%s(",
372  quote_qualified_identifier(nspname, proname));
373  for (i = 0; i < nargs; i++)
374  {
375  Oid thisargtype = procform->proargtypes.values[i];
376 
377  if (i > 0)
378  appendStringInfoChar(&buf, ',');
380  force_qualify ?
381  format_type_be_qualified(thisargtype) :
382  format_type_be(thisargtype));
383  }
384  appendStringInfoChar(&buf, ')');
385 
386  result = buf.data;
387 
388  ReleaseSysCache(proctup);
389  }
390  else
391  {
392  /* If OID doesn't match any pg_proc entry, return it numerically */
393  result = (char *) palloc(NAMEDATALEN);
394  snprintf(result, NAMEDATALEN, "%u", procedure_oid);
395  }
396 
397  return result;
398 }
399 
400 /*
401  * Output an objname/objargs representation for the procedure with the
402  * given OID. If it doesn't exist, an error is thrown.
403  *
404  * This can be used to feed get_object_address.
405  */
406 void
407 format_procedure_parts(Oid procedure_oid, List **objnames, List **objargs)
408 {
409  HeapTuple proctup;
410  Form_pg_proc procform;
411  int nargs;
412  int i;
413 
414  proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procedure_oid));
415 
416  if (!HeapTupleIsValid(proctup))
417  elog(ERROR, "cache lookup failed for procedure with OID %u", procedure_oid);
418 
419  procform = (Form_pg_proc) GETSTRUCT(proctup);
420  nargs = procform->pronargs;
421 
422  *objnames = list_make2(get_namespace_name_or_temp(procform->pronamespace),
423  pstrdup(NameStr(procform->proname)));
424  *objargs = NIL;
425  for (i = 0; i < nargs; i++)
426  {
427  Oid thisargtype = procform->proargtypes.values[i];
428 
429  *objargs = lappend(*objargs, format_type_be_qualified(thisargtype));
430  }
431 
432  ReleaseSysCache(proctup);
433 }
434 
435 /*
436  * regprocedureout - converts proc OID to "pro_name(args)"
437  */
438 Datum
440 {
441  RegProcedure proid = PG_GETARG_OID(0);
442  char *result;
443 
444  if (proid == InvalidOid)
445  result = pstrdup("-");
446  else
447  result = format_procedure(proid);
448 
449  PG_RETURN_CSTRING(result);
450 }
451 
452 /*
453  * regprocedurerecv - converts external binary format to regprocedure
454  */
455 Datum
457 {
458  /* Exactly the same as oidrecv, so share code */
459  return oidrecv(fcinfo);
460 }
461 
462 /*
463  * regproceduresend - converts regprocedure to binary format
464  */
465 Datum
467 {
468  /* Exactly the same as oidsend, so share code */
469  return oidsend(fcinfo);
470 }
471 
472 
473 /*
474  * regoperin - converts "oprname" to operator OID
475  *
476  * We also accept a numeric OID, for symmetry with the output routine.
477  *
478  * '0' signifies unknown (OID 0). In all other cases, the input must
479  * match an existing pg_operator entry.
480  */
481 Datum
483 {
484  char *opr_name_or_oid = PG_GETARG_CSTRING(0);
486  List *names;
487  FuncCandidateList clist;
488 
489  /* '0' ? */
490  if (strcmp(opr_name_or_oid, "0") == 0)
492 
493  /* Numeric OID? */
494  if (opr_name_or_oid[0] >= '0' &&
495  opr_name_or_oid[0] <= '9' &&
496  strspn(opr_name_or_oid, "0123456789") == strlen(opr_name_or_oid))
497  {
499  CStringGetDatum(opr_name_or_oid)));
500  PG_RETURN_OID(result);
501  }
502 
503  /* Else it's a name, possibly schema-qualified */
504 
505  /* The rest of this wouldn't work in bootstrap mode */
507  elog(ERROR, "regoper values must be OIDs in bootstrap mode");
508 
509  /*
510  * Normal case: parse the name into components and see if it matches any
511  * pg_operator entries in the current search path.
512  */
513  names = stringToQualifiedNameList(opr_name_or_oid);
514  clist = OpernameGetCandidates(names, '\0', false);
515 
516  if (clist == NULL)
517  ereport(ERROR,
518  (errcode(ERRCODE_UNDEFINED_FUNCTION),
519  errmsg("operator does not exist: %s", opr_name_or_oid)));
520  else if (clist->next != NULL)
521  ereport(ERROR,
522  (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
523  errmsg("more than one operator named %s",
524  opr_name_or_oid)));
525 
526  result = clist->oid;
527 
528  PG_RETURN_OID(result);
529 }
530 
531 /*
532  * to_regoper - converts "oprname" to operator OID
533  *
534  * If the name is not found, we return NULL.
535  */
536 Datum
538 {
539  char *opr_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
540  List *names;
541  FuncCandidateList clist;
542 
543  /*
544  * Parse the name into components and see if it matches any pg_operator
545  * entries in the current search path.
546  */
547  names = stringToQualifiedNameList(opr_name);
548  clist = OpernameGetCandidates(names, '\0', true);
549 
550  if (clist == NULL || clist->next != NULL)
551  PG_RETURN_NULL();
552 
553  PG_RETURN_OID(clist->oid);
554 }
555 
556 /*
557  * regoperout - converts operator OID to "opr_name"
558  */
559 Datum
561 {
562  Oid oprid = PG_GETARG_OID(0);
563  char *result;
564  HeapTuple opertup;
565 
566  if (oprid == InvalidOid)
567  {
568  result = pstrdup("0");
569  PG_RETURN_CSTRING(result);
570  }
571 
572  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(oprid));
573 
574  if (HeapTupleIsValid(opertup))
575  {
576  Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
577  char *oprname = NameStr(operform->oprname);
578 
579  /*
580  * In bootstrap mode, skip the fancy namespace stuff and just return
581  * the oper name. (This path is only needed for debugging output
582  * anyway.)
583  */
585  result = pstrdup(oprname);
586  else
587  {
588  FuncCandidateList clist;
589 
590  /*
591  * Would this oper be found (uniquely!) by regoperin? If not,
592  * qualify it.
593  */
594  clist = OpernameGetCandidates(list_make1(makeString(oprname)),
595  '\0', false);
596  if (clist != NULL && clist->next == NULL &&
597  clist->oid == oprid)
598  result = pstrdup(oprname);
599  else
600  {
601  const char *nspname;
602 
603  nspname = get_namespace_name(operform->oprnamespace);
604  nspname = quote_identifier(nspname);
605  result = (char *) palloc(strlen(nspname) + strlen(oprname) + 2);
606  sprintf(result, "%s.%s", nspname, oprname);
607  }
608  }
609 
610  ReleaseSysCache(opertup);
611  }
612  else
613  {
614  /*
615  * If OID doesn't match any pg_operator entry, return it numerically
616  */
617  result = (char *) palloc(NAMEDATALEN);
618  snprintf(result, NAMEDATALEN, "%u", oprid);
619  }
620 
621  PG_RETURN_CSTRING(result);
622 }
623 
624 /*
625  * regoperrecv - converts external binary format to regoper
626  */
627 Datum
629 {
630  /* Exactly the same as oidrecv, so share code */
631  return oidrecv(fcinfo);
632 }
633 
634 /*
635  * regopersend - converts regoper to binary format
636  */
637 Datum
639 {
640  /* Exactly the same as oidsend, so share code */
641  return oidsend(fcinfo);
642 }
643 
644 
645 /*
646  * regoperatorin - converts "oprname(args)" to operator OID
647  *
648  * We also accept a numeric OID, for symmetry with the output routine.
649  *
650  * '0' signifies unknown (OID 0). In all other cases, the input must
651  * match an existing pg_operator entry.
652  */
653 Datum
655 {
656  char *opr_name_or_oid = PG_GETARG_CSTRING(0);
657  Oid result;
658  List *names;
659  int nargs;
660  Oid argtypes[FUNC_MAX_ARGS];
661 
662  /* '0' ? */
663  if (strcmp(opr_name_or_oid, "0") == 0)
665 
666  /* Numeric OID? */
667  if (opr_name_or_oid[0] >= '0' &&
668  opr_name_or_oid[0] <= '9' &&
669  strspn(opr_name_or_oid, "0123456789") == strlen(opr_name_or_oid))
670  {
672  CStringGetDatum(opr_name_or_oid)));
673  PG_RETURN_OID(result);
674  }
675 
676  /* The rest of this wouldn't work in bootstrap mode */
678  elog(ERROR, "regoperator values must be OIDs in bootstrap mode");
679 
680  /*
681  * Else it's a name and arguments. Parse the name and arguments, look up
682  * potential matches in the current namespace search list, and scan to see
683  * which one exactly matches the given argument types. (There will not be
684  * more than one match.)
685  */
686  parseNameAndArgTypes(opr_name_or_oid, true, &names, &nargs, argtypes);
687  if (nargs == 1)
688  ereport(ERROR,
689  (errcode(ERRCODE_UNDEFINED_PARAMETER),
690  errmsg("missing argument"),
691  errhint("Use NONE to denote the missing argument of a unary operator.")));
692  if (nargs != 2)
693  ereport(ERROR,
694  (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
695  errmsg("too many arguments"),
696  errhint("Provide two argument types for operator.")));
697 
698  result = OpernameGetOprid(names, argtypes[0], argtypes[1]);
699 
700  if (!OidIsValid(result))
701  ereport(ERROR,
702  (errcode(ERRCODE_UNDEFINED_FUNCTION),
703  errmsg("operator does not exist: %s", opr_name_or_oid)));
704 
705  PG_RETURN_OID(result);
706 }
707 
708 /*
709  * to_regoperator - converts "oprname(args)" to operator OID
710  *
711  * If the name is not found, we return NULL.
712  */
713 Datum
715 {
716  char *opr_name_or_oid = text_to_cstring(PG_GETARG_TEXT_PP(0));
717  Oid result;
718  List *names;
719  int nargs;
720  Oid argtypes[FUNC_MAX_ARGS];
721 
722  /*
723  * Parse the name and arguments, look up potential matches in the current
724  * namespace search list, and scan to see which one exactly matches the
725  * given argument types. (There will not be more than one match.)
726  */
727  parseNameAndArgTypes(opr_name_or_oid, true, &names, &nargs, argtypes);
728  if (nargs == 1)
729  ereport(ERROR,
730  (errcode(ERRCODE_UNDEFINED_PARAMETER),
731  errmsg("missing argument"),
732  errhint("Use NONE to denote the missing argument of a unary operator.")));
733  if (nargs != 2)
734  ereport(ERROR,
735  (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
736  errmsg("too many arguments"),
737  errhint("Provide two argument types for operator.")));
738 
739  result = OpernameGetOprid(names, argtypes[0], argtypes[1]);
740 
741  if (!OidIsValid(result))
742  PG_RETURN_NULL();
743 
744  PG_RETURN_OID(result);
745 }
746 
747 /*
748  * format_operator - converts operator OID to "opr_name(args)"
749  *
750  * This exports the useful functionality of regoperatorout for use
751  * in other backend modules. The result is a palloc'd string.
752  */
753 static char *
754 format_operator_internal(Oid operator_oid, bool force_qualify)
755 {
756  char *result;
757  HeapTuple opertup;
758 
759  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operator_oid));
760 
761  if (HeapTupleIsValid(opertup))
762  {
763  Form_pg_operator operform = (Form_pg_operator) GETSTRUCT(opertup);
764  char *oprname = NameStr(operform->oprname);
765  char *nspname;
767 
768  /* XXX no support here for bootstrap mode */
770 
771  initStringInfo(&buf);
772 
773  /*
774  * Would this oper be found (given the right args) by regoperatorin?
775  * If not, or if caller explicitly requests it, we need to qualify it.
776  */
777  if (force_qualify || !OperatorIsVisible(operator_oid))
778  {
779  nspname = get_namespace_name(operform->oprnamespace);
780  appendStringInfo(&buf, "%s.",
781  quote_identifier(nspname));
782  }
783 
784  appendStringInfo(&buf, "%s(", oprname);
785 
786  if (operform->oprleft)
787  appendStringInfo(&buf, "%s,",
788  force_qualify ?
789  format_type_be_qualified(operform->oprleft) :
790  format_type_be(operform->oprleft));
791  else
792  appendStringInfoString(&buf, "NONE,");
793 
794  if (operform->oprright)
795  appendStringInfo(&buf, "%s)",
796  force_qualify ?
797  format_type_be_qualified(operform->oprright) :
798  format_type_be(operform->oprright));
799  else
800  appendStringInfoString(&buf, "NONE)");
801 
802  result = buf.data;
803 
804  ReleaseSysCache(opertup);
805  }
806  else
807  {
808  /*
809  * If OID doesn't match any pg_operator entry, return it numerically
810  */
811  result = (char *) palloc(NAMEDATALEN);
812  snprintf(result, NAMEDATALEN, "%u", operator_oid);
813  }
814 
815  return result;
816 }
817 
818 char *
819 format_operator(Oid operator_oid)
820 {
821  return format_operator_internal(operator_oid, false);
822 }
823 
824 char *
826 {
827  return format_operator_internal(operator_oid, true);
828 }
829 
830 void
831 format_operator_parts(Oid operator_oid, List **objnames, List **objargs)
832 {
833  HeapTuple opertup;
834  Form_pg_operator oprForm;
835 
836  opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operator_oid));
837  if (!HeapTupleIsValid(opertup))
838  elog(ERROR, "cache lookup failed for operator with OID %u",
839  operator_oid);
840 
841  oprForm = (Form_pg_operator) GETSTRUCT(opertup);
842  *objnames = list_make2(get_namespace_name_or_temp(oprForm->oprnamespace),
843  pstrdup(NameStr(oprForm->oprname)));
844  *objargs = NIL;
845  if (oprForm->oprleft)
846  *objargs = lappend(*objargs,
847  format_type_be_qualified(oprForm->oprleft));
848  if (oprForm->oprright)
849  *objargs = lappend(*objargs,
850  format_type_be_qualified(oprForm->oprright));
851 
852  ReleaseSysCache(opertup);
853 }
854 
855 /*
856  * regoperatorout - converts operator OID to "opr_name(args)"
857  */
858 Datum
860 {
861  Oid oprid = PG_GETARG_OID(0);
862  char *result;
863 
864  if (oprid == InvalidOid)
865  result = pstrdup("0");
866  else
867  result = format_operator(oprid);
868 
869  PG_RETURN_CSTRING(result);
870 }
871 
872 /*
873  * regoperatorrecv - converts external binary format to regoperator
874  */
875 Datum
877 {
878  /* Exactly the same as oidrecv, so share code */
879  return oidrecv(fcinfo);
880 }
881 
882 /*
883  * regoperatorsend - converts regoperator to binary format
884  */
885 Datum
887 {
888  /* Exactly the same as oidsend, so share code */
889  return oidsend(fcinfo);
890 }
891 
892 
893 /*
894  * regclassin - converts "classname" to class OID
895  *
896  * We also accept a numeric OID, for symmetry with the output routine.
897  *
898  * '-' signifies unknown (OID 0). In all other cases, the input must
899  * match an existing pg_class entry.
900  */
901 Datum
903 {
904  char *class_name_or_oid = PG_GETARG_CSTRING(0);
906  List *names;
907 
908  /* '-' ? */
909  if (strcmp(class_name_or_oid, "-") == 0)
911 
912  /* Numeric OID? */
913  if (class_name_or_oid[0] >= '0' &&
914  class_name_or_oid[0] <= '9' &&
915  strspn(class_name_or_oid, "0123456789") == strlen(class_name_or_oid))
916  {
918  CStringGetDatum(class_name_or_oid)));
919  PG_RETURN_OID(result);
920  }
921 
922  /* Else it's a name, possibly schema-qualified */
923 
924  /* The rest of this wouldn't work in bootstrap mode */
926  elog(ERROR, "regclass values must be OIDs in bootstrap mode");
927 
928  /*
929  * Normal case: parse the name into components and see if it matches any
930  * pg_class entries in the current search path.
931  */
932  names = stringToQualifiedNameList(class_name_or_oid);
933 
934  /* We might not even have permissions on this relation; don't lock it. */
935  result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, false);
936 
937  PG_RETURN_OID(result);
938 }
939 
940 /*
941  * to_regclass - converts "classname" to class OID
942  *
943  * If the name is not found, we return NULL.
944  */
945 Datum
947 {
949  Oid result;
950  List *names;
951 
952  /*
953  * Parse the name into components and see if it matches any pg_class
954  * entries in the current search path.
955  */
956  names = stringToQualifiedNameList(class_name);
957 
958  /* We might not even have permissions on this relation; don't lock it. */
959  result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, true);
960 
961  if (OidIsValid(result))
962  PG_RETURN_OID(result);
963  else
964  PG_RETURN_NULL();
965 }
966 
967 /*
968  * regclassout - converts class OID to "class_name"
969  */
970 Datum
972 {
973  Oid classid = PG_GETARG_OID(0);
974  char *result;
975  HeapTuple classtup;
976 
977  if (classid == InvalidOid)
978  {
979  result = pstrdup("-");
980  PG_RETURN_CSTRING(result);
981  }
982 
983  classtup = SearchSysCache1(RELOID, ObjectIdGetDatum(classid));
984 
985  if (HeapTupleIsValid(classtup))
986  {
987  Form_pg_class classform = (Form_pg_class) GETSTRUCT(classtup);
988  char *classname = NameStr(classform->relname);
989 
990  /*
991  * In bootstrap mode, skip the fancy namespace stuff and just return
992  * the class name. (This path is only needed for debugging output
993  * anyway.)
994  */
996  result = pstrdup(classname);
997  else
998  {
999  char *nspname;
1000 
1001  /*
1002  * Would this class be found by regclassin? If not, qualify it.
1003  */
1004  if (RelationIsVisible(classid))
1005  nspname = NULL;
1006  else
1007  nspname = get_namespace_name(classform->relnamespace);
1008 
1009  result = quote_qualified_identifier(nspname, classname);
1010  }
1011 
1012  ReleaseSysCache(classtup);
1013  }
1014  else
1015  {
1016  /* If OID doesn't match any pg_class entry, return it numerically */
1017  result = (char *) palloc(NAMEDATALEN);
1018  snprintf(result, NAMEDATALEN, "%u", classid);
1019  }
1020 
1021  PG_RETURN_CSTRING(result);
1022 }
1023 
1024 /*
1025  * regclassrecv - converts external binary format to regclass
1026  */
1027 Datum
1029 {
1030  /* Exactly the same as oidrecv, so share code */
1031  return oidrecv(fcinfo);
1032 }
1033 
1034 /*
1035  * regclasssend - converts regclass to binary format
1036  */
1037 Datum
1039 {
1040  /* Exactly the same as oidsend, so share code */
1041  return oidsend(fcinfo);
1042 }
1043 
1044 
1045 /*
1046  * regtypein - converts "typename" to type OID
1047  *
1048  * The type name can be specified using the full type syntax recognized by
1049  * the parser; for example, DOUBLE PRECISION and INTEGER[] will work and be
1050  * translated to the correct type names. (We ignore any typmod info
1051  * generated by the parser, however.)
1052  *
1053  * We also accept a numeric OID, for symmetry with the output routine,
1054  * and for possible use in bootstrap mode.
1055  *
1056  * '-' signifies unknown (OID 0). In all other cases, the input must
1057  * match an existing pg_type entry.
1058  */
1059 Datum
1061 {
1062  char *typ_name_or_oid = PG_GETARG_CSTRING(0);
1063  Oid result = InvalidOid;
1064  int32 typmod;
1065 
1066  /* '-' ? */
1067  if (strcmp(typ_name_or_oid, "-") == 0)
1069 
1070  /* Numeric OID? */
1071  if (typ_name_or_oid[0] >= '0' &&
1072  typ_name_or_oid[0] <= '9' &&
1073  strspn(typ_name_or_oid, "0123456789") == strlen(typ_name_or_oid))
1074  {
1076  CStringGetDatum(typ_name_or_oid)));
1077  PG_RETURN_OID(result);
1078  }
1079 
1080  /* Else it's a type name, possibly schema-qualified or decorated */
1081 
1082  /* The rest of this wouldn't work in bootstrap mode */
1084  elog(ERROR, "regtype values must be OIDs in bootstrap mode");
1085 
1086  /*
1087  * Normal case: invoke the full parser to deal with special cases such as
1088  * array syntax.
1089  */
1090  parseTypeString(typ_name_or_oid, &result, &typmod, false);
1091 
1092  PG_RETURN_OID(result);
1093 }
1094 
1095 /*
1096  * to_regtype - converts "typename" to type OID
1097  *
1098  * If the name is not found, we return NULL.
1099  */
1100 Datum
1102 {
1103  char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1104  Oid result;
1105  int32 typmod;
1106 
1107  /*
1108  * Invoke the full parser to deal with special cases such as array syntax.
1109  */
1110  parseTypeString(typ_name, &result, &typmod, true);
1111 
1112  if (OidIsValid(result))
1113  PG_RETURN_OID(result);
1114  else
1115  PG_RETURN_NULL();
1116 }
1117 
1118 /*
1119  * regtypeout - converts type OID to "typ_name"
1120  */
1121 Datum
1123 {
1124  Oid typid = PG_GETARG_OID(0);
1125  char *result;
1126  HeapTuple typetup;
1127 
1128  if (typid == InvalidOid)
1129  {
1130  result = pstrdup("-");
1131  PG_RETURN_CSTRING(result);
1132  }
1133 
1134  typetup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
1135 
1136  if (HeapTupleIsValid(typetup))
1137  {
1138  Form_pg_type typeform = (Form_pg_type) GETSTRUCT(typetup);
1139 
1140  /*
1141  * In bootstrap mode, skip the fancy namespace stuff and just return
1142  * the type name. (This path is only needed for debugging output
1143  * anyway.)
1144  */
1146  {
1147  char *typname = NameStr(typeform->typname);
1148 
1149  result = pstrdup(typname);
1150  }
1151  else
1152  result = format_type_be(typid);
1153 
1154  ReleaseSysCache(typetup);
1155  }
1156  else
1157  {
1158  /* If OID doesn't match any pg_type entry, return it numerically */
1159  result = (char *) palloc(NAMEDATALEN);
1160  snprintf(result, NAMEDATALEN, "%u", typid);
1161  }
1162 
1163  PG_RETURN_CSTRING(result);
1164 }
1165 
1166 /*
1167  * regtyperecv - converts external binary format to regtype
1168  */
1169 Datum
1171 {
1172  /* Exactly the same as oidrecv, so share code */
1173  return oidrecv(fcinfo);
1174 }
1175 
1176 /*
1177  * regtypesend - converts regtype to binary format
1178  */
1179 Datum
1181 {
1182  /* Exactly the same as oidsend, so share code */
1183  return oidsend(fcinfo);
1184 }
1185 
1186 
1187 /*
1188  * regconfigin - converts "tsconfigname" to tsconfig OID
1189  *
1190  * We also accept a numeric OID, for symmetry with the output routine.
1191  *
1192  * '-' signifies unknown (OID 0). In all other cases, the input must
1193  * match an existing pg_ts_config entry.
1194  */
1195 Datum
1197 {
1198  char *cfg_name_or_oid = PG_GETARG_CSTRING(0);
1199  Oid result;
1200  List *names;
1201 
1202  /* '-' ? */
1203  if (strcmp(cfg_name_or_oid, "-") == 0)
1205 
1206  /* Numeric OID? */
1207  if (cfg_name_or_oid[0] >= '0' &&
1208  cfg_name_or_oid[0] <= '9' &&
1209  strspn(cfg_name_or_oid, "0123456789") == strlen(cfg_name_or_oid))
1210  {
1212  CStringGetDatum(cfg_name_or_oid)));
1213  PG_RETURN_OID(result);
1214  }
1215 
1216  /* The rest of this wouldn't work in bootstrap mode */
1218  elog(ERROR, "regconfig values must be OIDs in bootstrap mode");
1219 
1220  /*
1221  * Normal case: parse the name into components and see if it matches any
1222  * pg_ts_config entries in the current search path.
1223  */
1224  names = stringToQualifiedNameList(cfg_name_or_oid);
1225 
1226  result = get_ts_config_oid(names, false);
1227 
1228  PG_RETURN_OID(result);
1229 }
1230 
1231 /*
1232  * regconfigout - converts tsconfig OID to "tsconfigname"
1233  */
1234 Datum
1236 {
1237  Oid cfgid = PG_GETARG_OID(0);
1238  char *result;
1239  HeapTuple cfgtup;
1240 
1241  if (cfgid == InvalidOid)
1242  {
1243  result = pstrdup("-");
1244  PG_RETURN_CSTRING(result);
1245  }
1246 
1247  cfgtup = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgid));
1248 
1249  if (HeapTupleIsValid(cfgtup))
1250  {
1251  Form_pg_ts_config cfgform = (Form_pg_ts_config) GETSTRUCT(cfgtup);
1252  char *cfgname = NameStr(cfgform->cfgname);
1253  char *nspname;
1254 
1255  /*
1256  * Would this config be found by regconfigin? If not, qualify it.
1257  */
1258  if (TSConfigIsVisible(cfgid))
1259  nspname = NULL;
1260  else
1261  nspname = get_namespace_name(cfgform->cfgnamespace);
1262 
1263  result = quote_qualified_identifier(nspname, cfgname);
1264 
1265  ReleaseSysCache(cfgtup);
1266  }
1267  else
1268  {
1269  /* If OID doesn't match any pg_ts_config row, return it numerically */
1270  result = (char *) palloc(NAMEDATALEN);
1271  snprintf(result, NAMEDATALEN, "%u", cfgid);
1272  }
1273 
1274  PG_RETURN_CSTRING(result);
1275 }
1276 
1277 /*
1278  * regconfigrecv - converts external binary format to regconfig
1279  */
1280 Datum
1282 {
1283  /* Exactly the same as oidrecv, so share code */
1284  return oidrecv(fcinfo);
1285 }
1286 
1287 /*
1288  * regconfigsend - converts regconfig to binary format
1289  */
1290 Datum
1292 {
1293  /* Exactly the same as oidsend, so share code */
1294  return oidsend(fcinfo);
1295 }
1296 
1297 
1298 /*
1299  * regdictionaryin - converts "tsdictionaryname" to tsdictionary OID
1300  *
1301  * We also accept a numeric OID, for symmetry with the output routine.
1302  *
1303  * '-' signifies unknown (OID 0). In all other cases, the input must
1304  * match an existing pg_ts_dict entry.
1305  */
1306 Datum
1308 {
1309  char *dict_name_or_oid = PG_GETARG_CSTRING(0);
1310  Oid result;
1311  List *names;
1312 
1313  /* '-' ? */
1314  if (strcmp(dict_name_or_oid, "-") == 0)
1316 
1317  /* Numeric OID? */
1318  if (dict_name_or_oid[0] >= '0' &&
1319  dict_name_or_oid[0] <= '9' &&
1320  strspn(dict_name_or_oid, "0123456789") == strlen(dict_name_or_oid))
1321  {
1323  CStringGetDatum(dict_name_or_oid)));
1324  PG_RETURN_OID(result);
1325  }
1326 
1327  /* The rest of this wouldn't work in bootstrap mode */
1329  elog(ERROR, "regdictionary values must be OIDs in bootstrap mode");
1330 
1331  /*
1332  * Normal case: parse the name into components and see if it matches any
1333  * pg_ts_dict entries in the current search path.
1334  */
1335  names = stringToQualifiedNameList(dict_name_or_oid);
1336 
1337  result = get_ts_dict_oid(names, false);
1338 
1339  PG_RETURN_OID(result);
1340 }
1341 
1342 /*
1343  * regdictionaryout - converts tsdictionary OID to "tsdictionaryname"
1344  */
1345 Datum
1347 {
1348  Oid dictid = PG_GETARG_OID(0);
1349  char *result;
1350  HeapTuple dicttup;
1351 
1352  if (dictid == InvalidOid)
1353  {
1354  result = pstrdup("-");
1355  PG_RETURN_CSTRING(result);
1356  }
1357 
1358  dicttup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictid));
1359 
1360  if (HeapTupleIsValid(dicttup))
1361  {
1362  Form_pg_ts_dict dictform = (Form_pg_ts_dict) GETSTRUCT(dicttup);
1363  char *dictname = NameStr(dictform->dictname);
1364  char *nspname;
1365 
1366  /*
1367  * Would this dictionary be found by regdictionaryin? If not, qualify
1368  * it.
1369  */
1370  if (TSDictionaryIsVisible(dictid))
1371  nspname = NULL;
1372  else
1373  nspname = get_namespace_name(dictform->dictnamespace);
1374 
1375  result = quote_qualified_identifier(nspname, dictname);
1376 
1377  ReleaseSysCache(dicttup);
1378  }
1379  else
1380  {
1381  /* If OID doesn't match any pg_ts_dict row, return it numerically */
1382  result = (char *) palloc(NAMEDATALEN);
1383  snprintf(result, NAMEDATALEN, "%u", dictid);
1384  }
1385 
1386  PG_RETURN_CSTRING(result);
1387 }
1388 
1389 /*
1390  * regdictionaryrecv - converts external binary format to regdictionary
1391  */
1392 Datum
1394 {
1395  /* Exactly the same as oidrecv, so share code */
1396  return oidrecv(fcinfo);
1397 }
1398 
1399 /*
1400  * regdictionarysend - converts regdictionary to binary format
1401  */
1402 Datum
1404 {
1405  /* Exactly the same as oidsend, so share code */
1406  return oidsend(fcinfo);
1407 }
1408 
1409 /*
1410  * regrolein - converts "rolename" to role OID
1411  *
1412  * We also accept a numeric OID, for symmetry with the output routine.
1413  *
1414  * '-' signifies unknown (OID 0). In all other cases, the input must
1415  * match an existing pg_authid entry.
1416  */
1417 Datum
1419 {
1420  char *role_name_or_oid = PG_GETARG_CSTRING(0);
1421  Oid result;
1422  List *names;
1423 
1424  /* '-' ? */
1425  if (strcmp(role_name_or_oid, "-") == 0)
1427 
1428  /* Numeric OID? */
1429  if (role_name_or_oid[0] >= '0' &&
1430  role_name_or_oid[0] <= '9' &&
1431  strspn(role_name_or_oid, "0123456789") == strlen(role_name_or_oid))
1432  {
1434  CStringGetDatum(role_name_or_oid)));
1435  PG_RETURN_OID(result);
1436  }
1437 
1438  /* The rest of this wouldn't work in bootstrap mode */
1440  elog(ERROR, "regrole values must be OIDs in bootstrap mode");
1441 
1442  /* Normal case: see if the name matches any pg_authid entry. */
1443  names = stringToQualifiedNameList(role_name_or_oid);
1444 
1445  if (list_length(names) != 1)
1446  ereport(ERROR,
1447  (errcode(ERRCODE_INVALID_NAME),
1448  errmsg("invalid name syntax")));
1449 
1450  result = get_role_oid(strVal(linitial(names)), false);
1451 
1452  PG_RETURN_OID(result);
1453 }
1454 
1455 /*
1456  * to_regrole - converts "rolename" to role OID
1457  *
1458  * If the name is not found, we return NULL.
1459  */
1460 Datum
1462 {
1463  char *role_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1464  Oid result;
1465  List *names;
1466 
1467  names = stringToQualifiedNameList(role_name);
1468 
1469  if (list_length(names) != 1)
1470  ereport(ERROR,
1471  (errcode(ERRCODE_INVALID_NAME),
1472  errmsg("invalid name syntax")));
1473 
1474  result = get_role_oid(strVal(linitial(names)), true);
1475 
1476  if (OidIsValid(result))
1477  PG_RETURN_OID(result);
1478  else
1479  PG_RETURN_NULL();
1480 }
1481 
1482 /*
1483  * regroleout - converts role OID to "role_name"
1484  */
1485 Datum
1487 {
1488  Oid roleoid = PG_GETARG_OID(0);
1489  char *result;
1490 
1491  if (roleoid == InvalidOid)
1492  {
1493  result = pstrdup("-");
1494  PG_RETURN_CSTRING(result);
1495  }
1496 
1497  result = GetUserNameFromId(roleoid, true);
1498 
1499  if (result)
1500  {
1501  /* pstrdup is not really necessary, but it avoids a compiler warning */
1502  result = pstrdup(quote_identifier(result));
1503  }
1504  else
1505  {
1506  /* If OID doesn't match any role, return it numerically */
1507  result = (char *) palloc(NAMEDATALEN);
1508  snprintf(result, NAMEDATALEN, "%u", roleoid);
1509  }
1510 
1511  PG_RETURN_CSTRING(result);
1512 }
1513 
1514 /*
1515  * regrolerecv - converts external binary format to regrole
1516  */
1517 Datum
1519 {
1520  /* Exactly the same as oidrecv, so share code */
1521  return oidrecv(fcinfo);
1522 }
1523 
1524 /*
1525  * regrolesend - converts regrole to binary format
1526  */
1527 Datum
1529 {
1530  /* Exactly the same as oidsend, so share code */
1531  return oidsend(fcinfo);
1532 }
1533 
1534 /*
1535  * regnamespacein - converts "nspname" to namespace OID
1536  *
1537  * We also accept a numeric OID, for symmetry with the output routine.
1538  *
1539  * '-' signifies unknown (OID 0). In all other cases, the input must
1540  * match an existing pg_namespace entry.
1541  */
1542 Datum
1544 {
1545  char *nsp_name_or_oid = PG_GETARG_CSTRING(0);
1546  Oid result;
1547  List *names;
1548 
1549  /* '-' ? */
1550  if (strcmp(nsp_name_or_oid, "-") == 0)
1552 
1553  /* Numeric OID? */
1554  if (nsp_name_or_oid[0] >= '0' &&
1555  nsp_name_or_oid[0] <= '9' &&
1556  strspn(nsp_name_or_oid, "0123456789") == strlen(nsp_name_or_oid))
1557  {
1559  CStringGetDatum(nsp_name_or_oid)));
1560  PG_RETURN_OID(result);
1561  }
1562 
1563  /* The rest of this wouldn't work in bootstrap mode */
1565  elog(ERROR, "regnamespace values must be OIDs in bootstrap mode");
1566 
1567  /* Normal case: see if the name matches any pg_namespace entry. */
1568  names = stringToQualifiedNameList(nsp_name_or_oid);
1569 
1570  if (list_length(names) != 1)
1571  ereport(ERROR,
1572  (errcode(ERRCODE_INVALID_NAME),
1573  errmsg("invalid name syntax")));
1574 
1575  result = get_namespace_oid(strVal(linitial(names)), false);
1576 
1577  PG_RETURN_OID(result);
1578 }
1579 
1580 /*
1581  * to_regnamespace - converts "nspname" to namespace OID
1582  *
1583  * If the name is not found, we return NULL.
1584  */
1585 Datum
1587 {
1588  char *nsp_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1589  Oid result;
1590  List *names;
1591 
1592  names = stringToQualifiedNameList(nsp_name);
1593 
1594  if (list_length(names) != 1)
1595  ereport(ERROR,
1596  (errcode(ERRCODE_INVALID_NAME),
1597  errmsg("invalid name syntax")));
1598 
1599  result = get_namespace_oid(strVal(linitial(names)), true);
1600 
1601  if (OidIsValid(result))
1602  PG_RETURN_OID(result);
1603  else
1604  PG_RETURN_NULL();
1605 }
1606 
1607 /*
1608  * regnamespaceout - converts namespace OID to "nsp_name"
1609  */
1610 Datum
1612 {
1613  Oid nspid = PG_GETARG_OID(0);
1614  char *result;
1615 
1616  if (nspid == InvalidOid)
1617  {
1618  result = pstrdup("-");
1619  PG_RETURN_CSTRING(result);
1620  }
1621 
1622  result = get_namespace_name(nspid);
1623 
1624  if (result)
1625  {
1626  /* pstrdup is not really necessary, but it avoids a compiler warning */
1627  result = pstrdup(quote_identifier(result));
1628  }
1629  else
1630  {
1631  /* If OID doesn't match any namespace, return it numerically */
1632  result = (char *) palloc(NAMEDATALEN);
1633  snprintf(result, NAMEDATALEN, "%u", nspid);
1634  }
1635 
1636  PG_RETURN_CSTRING(result);
1637 }
1638 
1639 /*
1640  * regnamespacerecv - converts external binary format to regnamespace
1641  */
1642 Datum
1644 {
1645  /* Exactly the same as oidrecv, so share code */
1646  return oidrecv(fcinfo);
1647 }
1648 
1649 /*
1650  * regnamespacesend - converts regnamespace to binary format
1651  */
1652 Datum
1654 {
1655  /* Exactly the same as oidsend, so share code */
1656  return oidsend(fcinfo);
1657 }
1658 
1659 /*
1660  * text_regclass: convert text to regclass
1661  *
1662  * This could be replaced by CoerceViaIO, except that we need to treat
1663  * text-to-regclass as an implicit cast to support legacy forms of nextval()
1664  * and related functions.
1665  */
1666 Datum
1668 {
1669  text *relname = PG_GETARG_TEXT_PP(0);
1670  Oid result;
1671  RangeVar *rv;
1672 
1674 
1675  /* We might not even have permissions on this relation; don't lock it. */
1676  result = RangeVarGetRelid(rv, NoLock, false);
1677 
1678  PG_RETURN_OID(result);
1679 }
1680 
1681 
1682 /*
1683  * Given a C string, parse it into a qualified-name list.
1684  */
1685 List *
1686 stringToQualifiedNameList(const char *string)
1687 {
1688  char *rawname;
1689  List *result = NIL;
1690  List *namelist;
1691  ListCell *l;
1692 
1693  /* We need a modifiable copy of the input string. */
1694  rawname = pstrdup(string);
1695 
1696  if (!SplitIdentifierString(rawname, '.', &namelist))
1697  ereport(ERROR,
1698  (errcode(ERRCODE_INVALID_NAME),
1699  errmsg("invalid name syntax")));
1700 
1701  if (namelist == NIL)
1702  ereport(ERROR,
1703  (errcode(ERRCODE_INVALID_NAME),
1704  errmsg("invalid name syntax")));
1705 
1706  foreach(l, namelist)
1707  {
1708  char *curname = (char *) lfirst(l);
1709 
1710  result = lappend(result, makeString(pstrdup(curname)));
1711  }
1712 
1713  pfree(rawname);
1714  list_free(namelist);
1715 
1716  return result;
1717 }
1718 
1719 /*****************************************************************************
1720  * SUPPORT ROUTINES *
1721  *****************************************************************************/
1722 
1723 /*
1724  * Given a C string, parse it into a qualified function or operator name
1725  * followed by a parenthesized list of type names. Reduce the
1726  * type names to an array of OIDs (returned into *nargs and *argtypes;
1727  * the argtypes array should be of size FUNC_MAX_ARGS). The function or
1728  * operator name is returned to *names as a List of Strings.
1729  *
1730  * If allowNone is TRUE, accept "NONE" and return it as InvalidOid (this is
1731  * for unary operators).
1732  */
1733 static void
1734 parseNameAndArgTypes(const char *string, bool allowNone, List **names,
1735  int *nargs, Oid *argtypes)
1736 {
1737  char *rawname;
1738  char *ptr;
1739  char *ptr2;
1740  char *typename;
1741  bool in_quote;
1742  bool had_comma;
1743  int paren_count;
1744  Oid typeid;
1745  int32 typmod;
1746 
1747  /* We need a modifiable copy of the input string. */
1748  rawname = pstrdup(string);
1749 
1750  /* Scan to find the expected left paren; mustn't be quoted */
1751  in_quote = false;
1752  for (ptr = rawname; *ptr; ptr++)
1753  {
1754  if (*ptr == '"')
1755  in_quote = !in_quote;
1756  else if (*ptr == '(' && !in_quote)
1757  break;
1758  }
1759  if (*ptr == '\0')
1760  ereport(ERROR,
1761  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1762  errmsg("expected a left parenthesis")));
1763 
1764  /* Separate the name and parse it into a list */
1765  *ptr++ = '\0';
1766  *names = stringToQualifiedNameList(rawname);
1767 
1768  /* Check for the trailing right parenthesis and remove it */
1769  ptr2 = ptr + strlen(ptr);
1770  while (--ptr2 > ptr)
1771  {
1772  if (!isspace((unsigned char) *ptr2))
1773  break;
1774  }
1775  if (*ptr2 != ')')
1776  ereport(ERROR,
1777  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1778  errmsg("expected a right parenthesis")));
1779 
1780  *ptr2 = '\0';
1781 
1782  /* Separate the remaining string into comma-separated type names */
1783  *nargs = 0;
1784  had_comma = false;
1785 
1786  for (;;)
1787  {
1788  /* allow leading whitespace */
1789  while (isspace((unsigned char) *ptr))
1790  ptr++;
1791  if (*ptr == '\0')
1792  {
1793  /* End of string. Okay unless we had a comma before. */
1794  if (had_comma)
1795  ereport(ERROR,
1796  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1797  errmsg("expected a type name")));
1798  break;
1799  }
1800  typename = ptr;
1801  /* Find end of type name --- end of string or comma */
1802  /* ... but not a quoted or parenthesized comma */
1803  in_quote = false;
1804  paren_count = 0;
1805  for (; *ptr; ptr++)
1806  {
1807  if (*ptr == '"')
1808  in_quote = !in_quote;
1809  else if (*ptr == ',' && !in_quote && paren_count == 0)
1810  break;
1811  else if (!in_quote)
1812  {
1813  switch (*ptr)
1814  {
1815  case '(':
1816  case '[':
1817  paren_count++;
1818  break;
1819  case ')':
1820  case ']':
1821  paren_count--;
1822  break;
1823  }
1824  }
1825  }
1826  if (in_quote || paren_count != 0)
1827  ereport(ERROR,
1828  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1829  errmsg("improper type name")));
1830 
1831  ptr2 = ptr;
1832  if (*ptr == ',')
1833  {
1834  had_comma = true;
1835  *ptr++ = '\0';
1836  }
1837  else
1838  {
1839  had_comma = false;
1840  Assert(*ptr == '\0');
1841  }
1842  /* Lop off trailing whitespace */
1843  while (--ptr2 >= typename)
1844  {
1845  if (!isspace((unsigned char) *ptr2))
1846  break;
1847  *ptr2 = '\0';
1848  }
1849 
1850  if (allowNone && pg_strcasecmp(typename, "none") == 0)
1851  {
1852  /* Special case for NONE */
1853  typeid = InvalidOid;
1854  typmod = -1;
1855  }
1856  else
1857  {
1858  /* Use full parser to resolve the type name */
1859  parseTypeString(typename, &typeid, &typmod, false);
1860  }
1861  if (*nargs >= FUNC_MAX_ARGS)
1862  ereport(ERROR,
1863  (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1864  errmsg("too many arguments")));
1865 
1866  argtypes[*nargs] = typeid;
1867  (*nargs)++;
1868  }
1869 
1870  pfree(rawname);
1871 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
Datum regoperatorrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:876
Datum regnamespaceout(PG_FUNCTION_ARGS)
Definition: regproc.c:1611
Datum regoperatorin(PG_FUNCTION_ARGS)
Definition: regproc.c:654
Datum regconfigsend(PG_FUNCTION_ARGS)
Definition: regproc.c:1291
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition: namespace.c:2895
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Datum regclassin(PG_FUNCTION_ARGS)
Definition: regproc.c:902
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10254
FormData_pg_ts_config * Form_pg_ts_config
Definition: pg_ts_config.h:41
Oid oprid(Operator op)
Definition: parse_oper.c:245
Datum regroleout(PG_FUNCTION_ARGS)
Definition: regproc.c:1486
Datum regrolesend(PG_FUNCTION_ARGS)
Definition: regproc.c:1528
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
Datum regoperout(PG_FUNCTION_ARGS)
Definition: regproc.c:560
Datum regdictionaryout(PG_FUNCTION_ARGS)
Definition: regproc.c:1346
#define DatumGetObjectId(X)
Definition: postgres.h:506
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Datum to_regrole(PG_FUNCTION_ARGS)
Definition: regproc.c:1461
regproc RegProcedure
Definition: c.h:395
Datum regoperin(PG_FUNCTION_ARGS)
Definition: regproc.c:482
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2529
Datum regrolerecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1518
bool OperatorIsVisible(Oid oprid)
Definition: namespace.c:1706
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * format_operator(Oid operator_oid)
Definition: regproc.c:819
Datum to_regoper(PG_FUNCTION_ARGS)
Definition: regproc.c:537
bool FunctionIsVisible(Oid funcid)
Definition: namespace.c:1373
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2913
Datum regtypeout(PG_FUNCTION_ARGS)
Definition: regproc.c:1122
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
Datum regopersend(PG_FUNCTION_ARGS)
Definition: regproc.c:638
#define OidIsValid(objectId)
Definition: c.h:538
Datum regtyperecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1170
Datum regprocin(PG_FUNCTION_ARGS)
Definition: regproc.c:61
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
signed int int32
Definition: c.h:256
FuncCandidateList OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok)
Definition: namespace.c:1546
Datum oidsend(PG_FUNCTION_ARGS)
Definition: oid.c:151
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition: acl.c:5096
Datum regconfigout(PG_FUNCTION_ARGS)
Definition: regproc.c:1235
#define FUNC_MAX_ARGS
#define list_make1(x1)
Definition: pg_list.h:139
static char * format_operator_internal(Oid operator_oid, bool force_qualify)
Definition: regproc.c:754
#define NAMEDATALEN
Datum text_regclass(PG_FUNCTION_ARGS)
Definition: regproc.c:1667
Datum regprocedureout(PG_FUNCTION_ARGS)
Definition: regproc.c:439
Datum oidrecv(PG_FUNCTION_ARGS)
Definition: oid.c:140
Oid OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
Definition: namespace.c:1444
Oid get_ts_dict_oid(List *names, bool missing_ok)
Definition: namespace.c:2276
Oid args[FLEXIBLE_ARRAY_MEMBER]
Definition: namespace.h:37
void pfree(void *pointer)
Definition: mcxt.c:950
void format_operator_parts(Oid operator_oid, List **objnames, List **objargs)
Definition: regproc.c:831
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define linitial(l)
Definition: pg_list.h:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Datum regprocedurein(PG_FUNCTION_ARGS)
Definition: regproc.c:230
bool TSConfigIsVisible(Oid cfgid)
Definition: namespace.c:2587
FuncCandidateList FuncnameGetCandidates(List *names, int nargs, List *argnames, bool expand_variadic, bool expand_defaults, bool missing_ok)
Definition: namespace.c:902
Datum regproceduresend(PG_FUNCTION_ARGS)
Definition: regproc.c:466
char * format_operator_qualified(Oid operator_oid)
Definition: regproc.c:825
Datum to_regprocedure(PG_FUNCTION_ARGS)
Definition: regproc.c:289
Datum regprocout(PG_FUNCTION_ARGS)
Definition: regproc.c:142
FormData_pg_ts_dict * Form_pg_ts_dict
Definition: pg_ts_dict.h:45
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
Datum regtypesend(PG_FUNCTION_ARGS)
Definition: regproc.c:1180
Datum to_regproc(PG_FUNCTION_ARGS)
Definition: regproc.c:119
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3240
Datum regclasssend(PG_FUNCTION_ARGS)
Definition: regproc.c:1038
#define CStringGetDatum(X)
Definition: postgres.h:584
Datum regrolein(PG_FUNCTION_ARGS)
Definition: regproc.c:1418
Datum regprocrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:204
void format_procedure_parts(Oid procedure_oid, List **objnames, List **objargs)
Definition: regproc.c:407
Datum regdictionarysend(PG_FUNCTION_ARGS)
Definition: regproc.c:1403
Datum regprocsend(PG_FUNCTION_ARGS)
Definition: regproc.c:214
struct _FuncCandidateList * next
Definition: namespace.h:30
char * format_procedure_qualified(Oid procedure_oid)
Definition: regproc.c:328
#define ereport(elevel, rest)
Definition: elog.h:122
Datum to_regtype(PG_FUNCTION_ARGS)
Definition: regproc.c:1101
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3182
List * lappend(List *list, void *datum)
Definition: list.c:128
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
bool TSDictionaryIsVisible(Oid dictId)
Definition: namespace.c:2334
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10340
Datum regtypein(PG_FUNCTION_ARGS)
Definition: regproc.c:1060
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
void parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:781
#define InvalidOid
Definition: postgres_ext.h:36
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:322
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
static char * format_procedure_internal(Oid procedure_oid, bool force_qualify)
Definition: regproc.c:341
#define NULL
Definition: c.h:229
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:691
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static void parseNameAndArgTypes(const char *string, bool allowNone, List **names, int *nargs, Oid *argtypes)
Definition: regproc.c:1734
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:971
char * format_type_be_qualified(Oid type_oid)
Definition: format_type.c:104
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
const char * class_name
Definition: selinux.c:32
static int list_length(const List *l)
Definition: pg_list.h:89
Datum regnamespacesend(PG_FUNCTION_ARGS)
Definition: regproc.c:1653
Datum to_regnamespace(PG_FUNCTION_ARGS)
Definition: regproc.c:1586
FormData_pg_operator* Form_pg_operator
Definition: pg_operator.h:57
Datum regprocedurerecv(PG_FUNCTION_ARGS)
Definition: regproc.c:456
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
char * text_to_cstring(const text *t)
Definition: varlena.c:182
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1686
Datum regdictionaryin(PG_FUNCTION_ARGS)
Definition: regproc.c:1307
Datum regnamespacerecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1643
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool RelationIsVisible(Oid relid)
Definition: namespace.c:681
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3062
void list_free(List *list)
Definition: list.c:1133
Datum regoperatorout(PG_FUNCTION_ARGS)
Definition: regproc.c:859
int i
#define NameStr(name)
Definition: c.h:499
Datum to_regoperator(PG_FUNCTION_ARGS)
Definition: regproc.c:714
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum oidin(PG_FUNCTION_ARGS)
Definition: oid.c:117
Datum regconfigrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1281
#define elog
Definition: elog.h:219
Datum regdictionaryrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1393
Datum regoperrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:628
Datum regnamespacein(PG_FUNCTION_ARGS)
Definition: regproc.c:1543
Datum regconfigin(PG_FUNCTION_ARGS)
Definition: regproc.c:1196
Definition: pg_list.h:45
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum regoperatorsend(PG_FUNCTION_ARGS)
Definition: regproc.c:886
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum to_regclass(PG_FUNCTION_ARGS)
Definition: regproc.c:946
Datum regclassrecv(PG_FUNCTION_ARGS)
Definition: regproc.c:1028