PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
misc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * misc.c
4  *
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/misc.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <sys/file.h>
18 #include <signal.h>
19 #include <dirent.h>
20 #include <math.h>
21 #include <unistd.h>
22 
23 #include "access/sysattr.h"
24 #include "catalog/pg_authid.h"
25 #include "catalog/catalog.h"
26 #include "catalog/pg_tablespace.h"
27 #include "catalog/pg_type.h"
28 #include "commands/dbcommands.h"
29 #include "common/keywords.h"
30 #include "funcapi.h"
31 #include "miscadmin.h"
32 #include "pgstat.h"
33 #include "parser/scansup.h"
34 #include "postmaster/syslogger.h"
35 #include "rewrite/rewriteHandler.h"
36 #include "storage/fd.h"
37 #include "storage/pmsignal.h"
38 #include "storage/proc.h"
39 #include "storage/procarray.h"
40 #include "utils/lsyscache.h"
41 #include "utils/ruleutils.h"
42 #include "tcop/tcopprot.h"
43 #include "utils/acl.h"
44 #include "utils/builtins.h"
45 #include "utils/timestamp.h"
46 
47 
48 /*
49  * Common subroutine for num_nulls() and num_nonnulls().
50  * Returns TRUE if successful, FALSE if function should return NULL.
51  * If successful, total argument count and number of nulls are
52  * returned into *nargs and *nulls.
53  */
54 static bool
56  int32 *nargs, int32 *nulls)
57 {
58  int32 count = 0;
59  int i;
60 
61  /* Did we get a VARIADIC array argument, or separate arguments? */
62  if (get_fn_expr_variadic(fcinfo->flinfo))
63  {
64  ArrayType *arr;
65  int ndims,
66  nitems,
67  *dims;
68  bits8 *bitmap;
69 
70  Assert(PG_NARGS() == 1);
71 
72  /*
73  * If we get a null as VARIADIC array argument, we can't say anything
74  * useful about the number of elements, so return NULL. This behavior
75  * is consistent with other variadic functions - see concat_internal.
76  */
77  if (PG_ARGISNULL(0))
78  return false;
79 
80  /*
81  * Non-null argument had better be an array. We assume that any call
82  * context that could let get_fn_expr_variadic return true will have
83  * checked that a VARIADIC-labeled parameter actually is an array. So
84  * it should be okay to just Assert that it's an array rather than
85  * doing a full-fledged error check.
86  */
88 
89  /* OK, safe to fetch the array value */
90  arr = PG_GETARG_ARRAYTYPE_P(0);
91 
92  /* Count the array elements */
93  ndims = ARR_NDIM(arr);
94  dims = ARR_DIMS(arr);
95  nitems = ArrayGetNItems(ndims, dims);
96 
97  /* Count those that are NULL */
98  bitmap = ARR_NULLBITMAP(arr);
99  if (bitmap)
100  {
101  int bitmask = 1;
102 
103  for (i = 0; i < nitems; i++)
104  {
105  if ((*bitmap & bitmask) == 0)
106  count++;
107 
108  bitmask <<= 1;
109  if (bitmask == 0x100)
110  {
111  bitmap++;
112  bitmask = 1;
113  }
114  }
115  }
116 
117  *nargs = nitems;
118  *nulls = count;
119  }
120  else
121  {
122  /* Separate arguments, so just count 'em */
123  for (i = 0; i < PG_NARGS(); i++)
124  {
125  if (PG_ARGISNULL(i))
126  count++;
127  }
128 
129  *nargs = PG_NARGS();
130  *nulls = count;
131  }
132 
133  return true;
134 }
135 
136 /*
137  * num_nulls()
138  * Count the number of NULL arguments
139  */
140 Datum
142 {
143  int32 nargs,
144  nulls;
145 
146  if (!count_nulls(fcinfo, &nargs, &nulls))
147  PG_RETURN_NULL();
148 
149  PG_RETURN_INT32(nulls);
150 }
151 
152 /*
153  * num_nonnulls()
154  * Count the number of non-NULL arguments
155  */
156 Datum
158 {
159  int32 nargs,
160  nulls;
161 
162  if (!count_nulls(fcinfo, &nargs, &nulls))
163  PG_RETURN_NULL();
164 
165  PG_RETURN_INT32(nargs - nulls);
166 }
167 
168 
169 /*
170  * current_database()
171  * Expose the current database to the user
172  */
173 Datum
175 {
176  Name db;
177 
178  db = (Name) palloc(NAMEDATALEN);
179 
181  PG_RETURN_NAME(db);
182 }
183 
184 
185 /*
186  * current_query()
187  * Expose the current query to the user (useful in stored procedures)
188  * We might want to use ActivePortal->sourceText someday.
189  */
190 Datum
192 {
193  /* there is no easy way to access the more concise 'query_string' */
194  if (debug_query_string)
196  else
197  PG_RETURN_NULL();
198 }
199 
200 /*
201  * Send a signal to another backend.
202  *
203  * The signal is delivered if the user is either a superuser or the same
204  * role as the backend being signaled. For "dangerous" signals, an explicit
205  * check for superuser needs to be done prior to calling this function.
206  *
207  * Returns 0 on success, 1 on general failure, 2 on normal permission error
208  * and 3 if the caller needs to be a superuser.
209  *
210  * In the event of a general failure (return code 1), a warning message will
211  * be emitted. For permission errors, doing that is the responsibility of
212  * the caller.
213  */
214 #define SIGNAL_BACKEND_SUCCESS 0
215 #define SIGNAL_BACKEND_ERROR 1
216 #define SIGNAL_BACKEND_NOPERMISSION 2
217 #define SIGNAL_BACKEND_NOSUPERUSER 3
218 static int
219 pg_signal_backend(int pid, int sig)
220 {
221  PGPROC *proc = BackendPidGetProc(pid);
222 
223  /*
224  * BackendPidGetProc returns NULL if the pid isn't valid; but by the time
225  * we reach kill(), a process for which we get a valid proc here might
226  * have terminated on its own. There's no way to acquire a lock on an
227  * arbitrary process to prevent that. But since so far all the callers of
228  * this mechanism involve some request for ending the process anyway, that
229  * it might end on its own first is not a problem.
230  */
231  if (proc == NULL)
232  {
233  /*
234  * This is just a warning so a loop-through-resultset will not abort
235  * if one backend terminated on its own during the run.
236  */
238  (errmsg("PID %d is not a PostgreSQL server process", pid)));
239  return SIGNAL_BACKEND_ERROR;
240  }
241 
242  /* Only allow superusers to signal superuser-owned backends. */
243  if (superuser_arg(proc->roleId) && !superuser())
245 
246  /* Users can signal backends they have role membership in. */
247  if (!has_privs_of_role(GetUserId(), proc->roleId) &&
250 
251  /*
252  * Can the process we just validated above end, followed by the pid being
253  * recycled for a new process, before reaching here? Then we'd be trying
254  * to kill the wrong thing. Seems near impossible when sequential pid
255  * assignment and wraparound is used. Perhaps it could happen on a system
256  * where pid re-use is randomized. That race condition possibility seems
257  * too unlikely to worry about.
258  */
259 
260  /* If we have setsid(), signal the backend's whole process group */
261 #ifdef HAVE_SETSID
262  if (kill(-pid, sig))
263 #else
264  if (kill(pid, sig))
265 #endif
266  {
267  /* Again, just a warning to allow loops */
269  (errmsg("could not send signal to process %d: %m", pid)));
270  return SIGNAL_BACKEND_ERROR;
271  }
272  return SIGNAL_BACKEND_SUCCESS;
273 }
274 
275 /*
276  * Signal to cancel a backend process. This is allowed if you are a member of
277  * the role whose process is being canceled.
278  *
279  * Note that only superusers can signal superuser-owned processes.
280  */
281 Datum
283 {
284  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
285 
287  ereport(ERROR,
288  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
289  (errmsg("must be a superuser to cancel superuser query"))));
290 
292  ereport(ERROR,
293  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
294  (errmsg("must be a member of the role whose query is being canceled or member of pg_signal_backend"))));
295 
297 }
298 
299 /*
300  * Signal to terminate a backend process. This is allowed if you are a member
301  * of the role whose process is being terminated.
302  *
303  * Note that only superusers can signal superuser-owned processes.
304  */
305 Datum
307 {
308  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGTERM);
309 
311  ereport(ERROR,
312  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
313  (errmsg("must be a superuser to terminate superuser process"))));
314 
316  ereport(ERROR,
317  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
318  (errmsg("must be a member of the role whose process is being terminated or member of pg_signal_backend"))));
319 
321 }
322 
323 /*
324  * Signal to reload the database configuration
325  *
326  * Permission checking for this function is managed through the normal
327  * GRANT system.
328  */
329 Datum
331 {
332  if (kill(PostmasterPid, SIGHUP))
333  {
335  (errmsg("failed to send signal to postmaster: %m")));
336  PG_RETURN_BOOL(false);
337  }
338 
339  PG_RETURN_BOOL(true);
340 }
341 
342 
343 /*
344  * Rotate log file
345  *
346  * Permission checking for this function is managed through the normal
347  * GRANT system.
348  */
349 Datum
351 {
352  if (!Logging_collector)
353  {
355  (errmsg("rotation not possible because log collection not active")));
356  PG_RETURN_BOOL(false);
357  }
358 
360  PG_RETURN_BOOL(true);
361 }
362 
363 /* Function to find out which databases make use of a tablespace */
364 
365 typedef struct
366 {
367  char *location;
369 } ts_db_fctx;
370 
371 Datum
373 {
374  FuncCallContext *funcctx;
375  struct dirent *de;
376  ts_db_fctx *fctx;
377 
378  if (SRF_IS_FIRSTCALL())
379  {
380  MemoryContext oldcontext;
381  Oid tablespaceOid = PG_GETARG_OID(0);
382 
383  funcctx = SRF_FIRSTCALL_INIT();
384  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
385 
386  fctx = palloc(sizeof(ts_db_fctx));
387 
388  if (tablespaceOid == GLOBALTABLESPACE_OID)
389  {
390  fctx->dirdesc = NULL;
392  (errmsg("global tablespace never has databases")));
393  }
394  else
395  {
396  if (tablespaceOid == DEFAULTTABLESPACE_OID)
397  fctx->location = psprintf("base");
398  else
399  fctx->location = psprintf("pg_tblspc/%u/%s", tablespaceOid,
401 
402  fctx->dirdesc = AllocateDir(fctx->location);
403 
404  if (!fctx->dirdesc)
405  {
406  /* the only expected error is ENOENT */
407  if (errno != ENOENT)
408  ereport(ERROR,
410  errmsg("could not open directory \"%s\": %m",
411  fctx->location)));
413  (errmsg("%u is not a tablespace OID", tablespaceOid)));
414  }
415  }
416  funcctx->user_fctx = fctx;
417  MemoryContextSwitchTo(oldcontext);
418  }
419 
420  funcctx = SRF_PERCALL_SETUP();
421  fctx = (ts_db_fctx *) funcctx->user_fctx;
422 
423  if (!fctx->dirdesc) /* not a tablespace */
424  SRF_RETURN_DONE(funcctx);
425 
426  while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
427  {
428  char *subdir;
429  DIR *dirdesc;
430  Oid datOid = atooid(de->d_name);
431 
432  /* this test skips . and .., but is awfully weak */
433  if (!datOid)
434  continue;
435 
436  /* if database subdir is empty, don't report tablespace as used */
437 
438  subdir = psprintf("%s/%s", fctx->location, de->d_name);
439  dirdesc = AllocateDir(subdir);
440  while ((de = ReadDir(dirdesc, subdir)) != NULL)
441  {
442  if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
443  break;
444  }
445  FreeDir(dirdesc);
446  pfree(subdir);
447 
448  if (!de)
449  continue; /* indeed, nothing in it */
450 
451  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
452  }
453 
454  FreeDir(fctx->dirdesc);
455  SRF_RETURN_DONE(funcctx);
456 }
457 
458 
459 /*
460  * pg_tablespace_location - get location for a tablespace
461  */
462 Datum
464 {
465  Oid tablespaceOid = PG_GETARG_OID(0);
466  char sourcepath[MAXPGPATH];
467  char targetpath[MAXPGPATH];
468  int rllen;
469 
470  /*
471  * It's useful to apply this function to pg_class.reltablespace, wherein
472  * zero means "the database's default tablespace". So, rather than
473  * throwing an error for zero, we choose to assume that's what is meant.
474  */
475  if (tablespaceOid == InvalidOid)
476  tablespaceOid = MyDatabaseTableSpace;
477 
478  /*
479  * Return empty string for the cluster's default tablespaces
480  */
481  if (tablespaceOid == DEFAULTTABLESPACE_OID ||
482  tablespaceOid == GLOBALTABLESPACE_OID)
484 
485 #if defined(HAVE_READLINK) || defined(WIN32)
486 
487  /*
488  * Find the location of the tablespace by reading the symbolic link that
489  * is in pg_tblspc/<oid>.
490  */
491  snprintf(sourcepath, sizeof(sourcepath), "pg_tblspc/%u", tablespaceOid);
492 
493  rllen = readlink(sourcepath, targetpath, sizeof(targetpath));
494  if (rllen < 0)
495  ereport(ERROR,
497  errmsg("could not read symbolic link \"%s\": %m",
498  sourcepath)));
499  if (rllen >= sizeof(targetpath))
500  ereport(ERROR,
501  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
502  errmsg("symbolic link \"%s\" target is too long",
503  sourcepath)));
504  targetpath[rllen] = '\0';
505 
506  PG_RETURN_TEXT_P(cstring_to_text(targetpath));
507 #else
508  ereport(ERROR,
509  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
510  errmsg("tablespaces are not supported on this platform")));
511  PG_RETURN_NULL();
512 #endif
513 }
514 
515 /*
516  * pg_sleep - delay for N seconds
517  */
518 Datum
520 {
521  float8 secs = PG_GETARG_FLOAT8(0);
522  float8 endtime;
523 
524  /*
525  * We sleep using WaitLatch, to ensure that we'll wake up promptly if an
526  * important signal (such as SIGALRM or SIGINT) arrives. Because
527  * WaitLatch's upper limit of delay is INT_MAX milliseconds, and the user
528  * might ask for more than that, we sleep for at most 10 minutes and then
529  * loop.
530  *
531  * By computing the intended stop time initially, we avoid accumulation of
532  * extra delay across multiple sleeps. This also ensures we won't delay
533  * less than the specified time when WaitLatch is terminated early by a
534  * non-query-canceling signal such as SIGHUP.
535  */
536 #define GetNowFloat() ((float8) GetCurrentTimestamp() / 1000000.0)
537 
538  endtime = GetNowFloat() + secs;
539 
540  for (;;)
541  {
542  float8 delay;
543  long delay_ms;
544 
546 
547  delay = endtime - GetNowFloat();
548  if (delay >= 600.0)
549  delay_ms = 600000;
550  else if (delay > 0.0)
551  delay_ms = (long) ceil(delay * 1000.0);
552  else
553  break;
554 
555  (void) WaitLatch(MyLatch,
557  delay_ms,
560  }
561 
562  PG_RETURN_VOID();
563 }
564 
565 /* Function to return the list of grammar keywords */
566 Datum
568 {
569  FuncCallContext *funcctx;
570 
571  if (SRF_IS_FIRSTCALL())
572  {
573  MemoryContext oldcontext;
574  TupleDesc tupdesc;
575 
576  funcctx = SRF_FIRSTCALL_INIT();
577  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
578 
579  tupdesc = CreateTemplateTupleDesc(3, false);
580  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
581  TEXTOID, -1, 0);
582  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catcode",
583  CHAROID, -1, 0);
584  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "catdesc",
585  TEXTOID, -1, 0);
586 
587  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
588 
589  MemoryContextSwitchTo(oldcontext);
590  }
591 
592  funcctx = SRF_PERCALL_SETUP();
593 
594  if (funcctx->call_cntr < NumScanKeywords)
595  {
596  char *values[3];
597  HeapTuple tuple;
598 
599  /* cast-away-const is ugly but alternatives aren't much better */
600  values[0] = (char *) ScanKeywords[funcctx->call_cntr].name;
601 
602  switch (ScanKeywords[funcctx->call_cntr].category)
603  {
604  case UNRESERVED_KEYWORD:
605  values[1] = "U";
606  values[2] = _("unreserved");
607  break;
608  case COL_NAME_KEYWORD:
609  values[1] = "C";
610  values[2] = _("unreserved (cannot be function or type name)");
611  break;
613  values[1] = "T";
614  values[2] = _("reserved (can be function or type name)");
615  break;
616  case RESERVED_KEYWORD:
617  values[1] = "R";
618  values[2] = _("reserved");
619  break;
620  default: /* shouldn't be possible */
621  values[1] = NULL;
622  values[2] = NULL;
623  break;
624  }
625 
626  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
627 
628  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
629  }
630 
631  SRF_RETURN_DONE(funcctx);
632 }
633 
634 
635 /*
636  * Return the type of the argument.
637  */
638 Datum
640 {
641  PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
642 }
643 
644 
645 /*
646  * Implementation of the COLLATE FOR expression; returns the collation
647  * of the argument.
648  */
649 Datum
651 {
652  Oid typeid;
653  Oid collid;
654 
655  typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
656  if (!typeid)
657  PG_RETURN_NULL();
658  if (!type_is_collatable(typeid) && typeid != UNKNOWNOID)
659  ereport(ERROR,
660  (errcode(ERRCODE_DATATYPE_MISMATCH),
661  errmsg("collations are not supported by type %s",
662  format_type_be(typeid))));
663 
664  collid = PG_GET_COLLATION();
665  if (!collid)
666  PG_RETURN_NULL();
668 }
669 
670 
671 /*
672  * pg_relation_is_updatable - determine which update events the specified
673  * relation supports.
674  *
675  * This relies on relation_is_updatable() in rewriteHandler.c, which see
676  * for additional information.
677  */
678 Datum
680 {
681  Oid reloid = PG_GETARG_OID(0);
682  bool include_triggers = PG_GETARG_BOOL(1);
683 
684  PG_RETURN_INT32(relation_is_updatable(reloid, include_triggers, NULL));
685 }
686 
687 /*
688  * pg_column_is_updatable - determine whether a column is updatable
689  *
690  * This function encapsulates the decision about just what
691  * information_schema.columns.is_updatable actually means. It's not clear
692  * whether deletability of the column's relation should be required, so
693  * we want that decision in C code where we could change it without initdb.
694  */
695 Datum
697 {
698  Oid reloid = PG_GETARG_OID(0);
699  AttrNumber attnum = PG_GETARG_INT16(1);
701  bool include_triggers = PG_GETARG_BOOL(2);
702  int events;
703 
704  /* System columns are never updatable */
705  if (attnum <= 0)
706  PG_RETURN_BOOL(false);
707 
708  events = relation_is_updatable(reloid, include_triggers,
709  bms_make_singleton(col));
710 
711  /* We require both updatability and deletability of the relation */
712 #define REQ_EVENTS ((1 << CMD_UPDATE) | (1 << CMD_DELETE))
713 
714  PG_RETURN_BOOL((events & REQ_EVENTS) == REQ_EVENTS);
715 }
716 
717 
718 /*
719  * Is character a valid identifier start?
720  * Must match scan.l's {ident_start} character class.
721  */
722 static bool
723 is_ident_start(unsigned char c)
724 {
725  /* Underscores and ASCII letters are OK */
726  if (c == '_')
727  return true;
728  if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
729  return true;
730  /* Any high-bit-set character is OK (might be part of a multibyte char) */
731  if (IS_HIGHBIT_SET(c))
732  return true;
733  return false;
734 }
735 
736 /*
737  * Is character a valid identifier continuation?
738  * Must match scan.l's {ident_cont} character class.
739  */
740 static bool
741 is_ident_cont(unsigned char c)
742 {
743  /* Can be digit or dollar sign ... */
744  if ((c >= '0' && c <= '9') || c == '$')
745  return true;
746  /* ... or an identifier start character */
747  return is_ident_start(c);
748 }
749 
750 /*
751  * parse_ident - parse a SQL qualified identifier into separate identifiers.
752  * When strict mode is active (second parameter), then any chars after
753  * the last identifier are disallowed.
754  */
755 Datum
757 {
758  text *qualname = PG_GETARG_TEXT_PP(0);
759  bool strict = PG_GETARG_BOOL(1);
760  char *qualname_str = text_to_cstring(qualname);
761  ArrayBuildState *astate = NULL;
762  char *nextp;
763  bool after_dot = false;
764 
765  /*
766  * The code below scribbles on qualname_str in some cases, so we should
767  * reconvert qualname if we need to show the original string in error
768  * messages.
769  */
770  nextp = qualname_str;
771 
772  /* skip leading whitespace */
773  while (scanner_isspace(*nextp))
774  nextp++;
775 
776  for (;;)
777  {
778  char *curname;
779  bool missing_ident = true;
780 
781  if (*nextp == '"')
782  {
783  char *endp;
784 
785  curname = nextp + 1;
786  for (;;)
787  {
788  endp = strchr(nextp + 1, '"');
789  if (endp == NULL)
790  ereport(ERROR,
791  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
792  errmsg("string is not a valid identifier: \"%s\"",
793  text_to_cstring(qualname)),
794  errdetail("String has unclosed double quotes.")));
795  if (endp[1] != '"')
796  break;
797  memmove(endp, endp + 1, strlen(endp));
798  nextp = endp;
799  }
800  nextp = endp + 1;
801  *endp = '\0';
802 
803  if (endp - curname == 0)
804  ereport(ERROR,
805  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
806  errmsg("string is not a valid identifier: \"%s\"",
807  text_to_cstring(qualname)),
808  errdetail("Quoted identifier must not be empty.")));
809 
810  astate = accumArrayResult(astate, CStringGetTextDatum(curname),
811  false, TEXTOID, CurrentMemoryContext);
812  missing_ident = false;
813  }
814  else if (is_ident_start((unsigned char) *nextp))
815  {
816  char *downname;
817  int len;
818  text *part;
819 
820  curname = nextp++;
821  while (is_ident_cont((unsigned char) *nextp))
822  nextp++;
823 
824  len = nextp - curname;
825 
826  /*
827  * We don't implicitly truncate identifiers. This is useful for
828  * allowing the user to check for specific parts of the identifier
829  * being too long. It's easy enough for the user to get the
830  * truncated names by casting our output to name[].
831  */
832  downname = downcase_identifier(curname, len, false, false);
833  part = cstring_to_text_with_len(downname, len);
834  astate = accumArrayResult(astate, PointerGetDatum(part), false,
836  missing_ident = false;
837  }
838 
839  if (missing_ident)
840  {
841  /* Different error messages based on where we failed. */
842  if (*nextp == '.')
843  ereport(ERROR,
844  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
845  errmsg("string is not a valid identifier: \"%s\"",
846  text_to_cstring(qualname)),
847  errdetail("No valid identifier before \".\".")));
848  else if (after_dot)
849  ereport(ERROR,
850  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
851  errmsg("string is not a valid identifier: \"%s\"",
852  text_to_cstring(qualname)),
853  errdetail("No valid identifier after \".\".")));
854  else
855  ereport(ERROR,
856  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
857  errmsg("string is not a valid identifier: \"%s\"",
858  text_to_cstring(qualname))));
859  }
860 
861  while (scanner_isspace(*nextp))
862  nextp++;
863 
864  if (*nextp == '.')
865  {
866  after_dot = true;
867  nextp++;
868  while (scanner_isspace(*nextp))
869  nextp++;
870  }
871  else if (*nextp == '\0')
872  {
873  break;
874  }
875  else
876  {
877  if (strict)
878  ereport(ERROR,
879  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
880  errmsg("string is not a valid identifier: \"%s\"",
881  text_to_cstring(qualname))));
882  break;
883  }
884  }
885 
887 }
888 
889 /*
890  * pg_current_logfile
891  *
892  * Report current log file used by log collector by scanning current_logfiles.
893  */
894 Datum
896 {
897  FILE *fd;
898  char lbuffer[MAXPGPATH];
899  char *logfmt;
900  char *log_filepath;
901  char *log_format = lbuffer;
902  char *nlpos;
903 
904  /* The log format parameter is optional */
905  if (PG_NARGS() == 0 || PG_ARGISNULL(0))
906  logfmt = NULL;
907  else
908  {
909  logfmt = text_to_cstring(PG_GETARG_TEXT_PP(0));
910 
911  if (strcmp(logfmt, "stderr") != 0 && strcmp(logfmt, "csvlog") != 0)
912  ereport(ERROR,
913  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
914  errmsg("log format \"%s\" is not supported", logfmt),
915  errhint("The supported log formats are \"stderr\" and \"csvlog\".")));
916  }
917 
919  if (fd == NULL)
920  {
921  if (errno != ENOENT)
922  ereport(ERROR,
924  errmsg("could not read file \"%s\": %m",
926  PG_RETURN_NULL();
927  }
928 
929  /*
930  * Read the file to gather current log filename(s) registered by the
931  * syslogger.
932  */
933  while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
934  {
935  /*
936  * Extract log format and log file path from the line; lbuffer ==
937  * log_format, they share storage.
938  */
939  log_filepath = strchr(lbuffer, ' ');
940  if (log_filepath == NULL)
941  {
942  /* Uh oh. No space found, so file content is corrupted. */
943  elog(ERROR,
944  "missing space character in \"%s\"", LOG_METAINFO_DATAFILE);
945  break;
946  }
947 
948  *log_filepath = '\0';
949  log_filepath++;
950  nlpos = strchr(log_filepath, '\n');
951  if (nlpos == NULL)
952  {
953  /* Uh oh. No newline found, so file content is corrupted. */
954  elog(ERROR,
955  "missing newline character in \"%s\"", LOG_METAINFO_DATAFILE);
956  break;
957  }
958  *nlpos = '\0';
959 
960  if (logfmt == NULL || strcmp(logfmt, log_format) == 0)
961  {
962  FreeFile(fd);
963  PG_RETURN_TEXT_P(cstring_to_text(log_filepath));
964  }
965  }
966 
967  /* Close the current log filename file. */
968  FreeFile(fd);
969 
970  PG_RETURN_NULL();
971 }
972 
973 /*
974  * Report current log file used by log collector (1 argument version)
975  *
976  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
977  * which checks that all built-in functions that share the implementing C
978  * function take the same number of arguments
979  */
980 Datum
982 {
983  return pg_current_logfile(fcinfo);
984 }
985 
986 /*
987  * SQL wrapper around RelationGetReplicaIndex().
988  */
989 Datum
991 {
992  Oid reloid = PG_GETARG_OID(0);
993  Oid idxoid;
994  Relation rel;
995 
996  rel = heap_open(reloid, AccessShareLock);
997  idxoid = RelationGetReplicaIndex(rel);
999 
1000  if (OidIsValid(idxoid))
1001  PG_RETURN_OID(idxoid);
1002  else
1003  PG_RETURN_NULL();
1004 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define DEFAULT_ROLE_SIGNAL_BACKENDID
Definition: pg_authid.h:112
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static bool is_ident_start(unsigned char c)
Definition: misc.c:723
Datum pg_get_replica_identity_index(PG_FUNCTION_ARGS)
Definition: misc.c:990
Datum pg_rotate_logfile(PG_FUNCTION_ARGS)
Definition: misc.c:350
int errhint(const char *fmt,...)
Definition: elog.c:987
Datum pg_relation_is_updatable(PG_FUNCTION_ARGS)
Definition: misc.c:679
Datum parse_ident(PG_FUNCTION_ARGS)
Definition: misc.c:756
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4688
#define WL_TIMEOUT
Definition: latch.h:127
static bool count_nulls(FunctionCallInfo fcinfo, int32 *nargs, int32 *nulls)
Definition: misc.c:55
Oid GetUserId(void)
Definition: miscinit.c:284
#define TEXTOID
Definition: pg_type.h:324
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2346
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4830
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
const int NumScanKeywords
Definition: keywords.c:45
#define AccessShareLock
Definition: lockdefs.h:36
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
Datum pg_collation_for(PG_FUNCTION_ARGS)
Definition: misc.c:650
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2038
Oid roleId
Definition: proc.h:115
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define LOG_METAINFO_DATAFILE
Definition: syslogger.h:94
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
#define UNRESERVED_KEYWORD
Definition: keywords.h:18
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:216
Definition: dirent.h:9
#define OidIsValid(objectId)
Definition: c.h:532
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_GET_COLLATION()
Definition: fmgr.h:163
Datum pg_typeof(PG_FUNCTION_ARGS)
Definition: misc.c:639
char * generate_collation_name(Oid collid)
Definition: ruleutils.c:10798
signed int int32
Definition: c.h:246
Oid MyDatabaseTableSpace
Definition: globals.c:79
bool Logging_collector
Definition: syslogger.c:63
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
Datum current_database(PG_FUNCTION_ARGS)
Definition: misc.c:174
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Datum current_query(PG_FUNCTION_ARGS)
Definition: misc.c:191
#define NAMEDATALEN
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
FmgrInfo * flinfo
Definition: fmgr.h:79
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define GetNowFloat()
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
void pfree(void *pointer)
Definition: mcxt.c:949
Datum pg_current_logfile(PG_FUNCTION_ARGS)
Definition: misc.c:895
#define IS_HIGHBIT_SET(ch)
Definition: c.h:979
Datum pg_tablespace_location(PG_FUNCTION_ARGS)
Definition: misc.c:463
Definition: dirent.c:25
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:375
Datum pg_num_nulls(PG_FUNCTION_ARGS)
Definition: misc.c:141
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
#define SIGNAL_BACKEND_SUCCESS
Definition: misc.c:214
#define ARR_DIMS(a)
Definition: array.h:279
#define MAXPGPATH
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
Definition: c.h:487
#define TYPE_FUNC_NAME_KEYWORD
Definition: keywords.h:20
char * c
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:179
int relation_is_updatable(Oid reloid, bool include_triggers, Bitmapset *include_cols)
const ScanKeyword ScanKeywords[]
Definition: keywords.c:41
#define memmove(d, s, c)
Definition: c.h:1064
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
AttInMetadata * attinmeta
Definition: funcapi.h:99
#define DEFAULTTABLESPACE_OID
Definition: pg_tablespace.h:63
const char * name
Definition: keywords.h:26
int errdetail(const char *fmt,...)
Definition: elog.c:873
int errcode_for_file_access(void)
Definition: elog.c:598
Datum pg_terminate_backend(PG_FUNCTION_ARGS)
Definition: misc.c:306
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2117
Datum pg_current_logfile_1arg(PG_FUNCTION_ARGS)
Definition: misc.c:981
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2367
Datum pg_sleep(PG_FUNCTION_ARGS)
Definition: misc.c:519
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define SIGNAL_BACKEND_NOPERMISSION
Definition: misc.c:216
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define atooid(x)
Definition: postgres_ext.h:42
Datum pg_tablespace_databases(PG_FUNCTION_ARGS)
Definition: misc.c:372
#define ereport(elevel, rest)
Definition: elog.h:122
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5106
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
pid_t PostmasterPid
Definition: globals.c:87
#define WARNING
Definition: elog.h:40
const char * debug_query_string
Definition: postgres.c:85
Datum pg_num_nonnulls(PG_FUNCTION_ARGS)
Definition: misc.c:157
uint8 bits8
Definition: c.h:265
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum pg_reload_conf(PG_FUNCTION_ARGS)
Definition: misc.c:330
Oid MyDatabaseId
Definition: globals.c:77
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
bool scanner_isspace(char ch)
Definition: scansup.c:221
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define REQ_EVENTS
static int sig
Definition: pg_ctl.c:88
#define RESERVED_KEYWORD
Definition: keywords.h:21
#define CHAROID
Definition: pg_type.h:296
#define SIGHUP
Definition: win32.h:188
#define InvalidOid
Definition: postgres_ext.h:36
char * downcase_identifier(const char *ident, int len, bool warn, bool truncate)
Definition: scansup.c:140
Datum pg_cancel_backend(PG_FUNCTION_ARGS)
Definition: misc.c:282
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: misc.c:217
#define PG_RETURN_VOID()
Definition: fmgr.h:309
static int pg_signal_backend(int pid, int sig)
Definition: misc.c:219
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:681
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2433
int16 category
Definition: keywords.h:28
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
static bool is_ident_cont(unsigned char c)
Definition: misc.c:741
#define PG_NARGS()
Definition: fmgr.h:168
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
#define TABLESPACE_VERSION_DIRECTORY
Definition: catalog.h:26
#define ARR_NDIM(a)
Definition: array.h:275
#define UNKNOWNOID
Definition: pg_type.h:431
#define COL_NAME_KEYWORD
Definition: keywords.h:19
int FreeFile(FILE *file)
Definition: fd.c:2309
static Datum values[MAXATTR]
Definition: bootstrap.c:164
char * text_to_cstring(const text *t)
Definition: varlena.c:182
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5042
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2557
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:91
struct Latch * MyLatch
Definition: globals.c:52
Definition: c.h:433
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
Datum pg_get_keywords(PG_FUNCTION_ARGS)
Definition: misc.c:567
NameData * Name
Definition: c.h:491
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2806
Definition: proc.h:95
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
#define SIGNAL_BACKEND_ERROR
Definition: misc.c:215
char * location
Definition: misc.c:367
#define WL_LATCH_SET
Definition: latch.h:124
int16 AttrNumber
Definition: attnum.h:21
#define _(x)
Definition: elog.c:84
#define ARR_NULLBITMAP(a)
Definition: array.h:285
DIR * dirdesc
Definition: misc.c:368
#define PG_RETURN_NULL()
Definition: fmgr.h:305
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
int FreeDir(DIR *dir)
Definition: fd.c:2476
Datum pg_column_is_updatable(PG_FUNCTION_ARGS)
Definition: misc.c:696
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287