1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* misc.c
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/misc.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2002-09-02 04:47:07 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/file.h>
|
2004-06-02 23:29:29 +02:00
|
|
|
#include <signal.h>
|
2004-07-02 20:59:25 +02:00
|
|
|
#include <dirent.h>
|
2006-01-11 21:12:43 +01:00
|
|
|
#include <math.h>
|
2011-12-07 10:35:00 +01:00
|
|
|
#include <unistd.h>
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2013-10-18 16:35:36 +02:00
|
|
|
#include "access/sysattr.h"
|
2010-01-12 03:42:52 +01:00
|
|
|
#include "catalog/catalog.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "catalog/pg_tablespace.h"
|
2011-09-09 19:23:41 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2003-06-27 19:07:03 +02:00
|
|
|
#include "commands/dbcommands.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "funcapi.h"
|
2002-08-20 06:46:00 +02:00
|
|
|
#include "miscadmin.h"
|
2016-03-18 16:16:14 +01:00
|
|
|
#include "parser/scansup.h"
|
2008-07-03 22:58:47 +02:00
|
|
|
#include "parser/keywords.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "postmaster/syslogger.h"
|
2012-12-09 00:25:48 +01:00
|
|
|
#include "rewrite/rewriteHandler.h"
|
2004-07-02 20:59:25 +02:00
|
|
|
#include "storage/fd.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "storage/pmsignal.h"
|
2012-01-15 15:34:40 +01:00
|
|
|
#include "storage/proc.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "storage/procarray.h"
|
2012-03-02 20:12:16 +01:00
|
|
|
#include "utils/lsyscache.h"
|
2014-10-08 23:10:47 +02:00
|
|
|
#include "utils/ruleutils.h"
|
2008-04-04 18:57:21 +02:00
|
|
|
#include "tcop/tcopprot.h"
|
2015-03-19 20:02:33 +01:00
|
|
|
#include "utils/acl.h"
|
2011-09-09 19:23:41 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/timestamp.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2016-02-05 05:03:10 +01:00
|
|
|
/*
|
|
|
|
* Common subroutine for num_nulls() and num_nonnulls().
|
|
|
|
* Returns TRUE if successful, FALSE if function should return NULL.
|
|
|
|
* If successful, total argument count and number of nulls are
|
|
|
|
* returned into *nargs and *nulls.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
count_nulls(FunctionCallInfo fcinfo,
|
|
|
|
int32 *nargs, int32 *nulls)
|
|
|
|
{
|
|
|
|
int32 count = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Did we get a VARIADIC array argument, or separate arguments? */
|
|
|
|
if (get_fn_expr_variadic(fcinfo->flinfo))
|
|
|
|
{
|
|
|
|
ArrayType *arr;
|
|
|
|
int ndims,
|
|
|
|
nitems,
|
|
|
|
*dims;
|
|
|
|
bits8 *bitmap;
|
|
|
|
|
|
|
|
Assert(PG_NARGS() == 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get a null as VARIADIC array argument, we can't say anything
|
|
|
|
* useful about the number of elements, so return NULL. This behavior
|
|
|
|
* is consistent with other variadic functions - see concat_internal.
|
|
|
|
*/
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-null argument had better be an array. We assume that any call
|
|
|
|
* context that could let get_fn_expr_variadic return true will have
|
|
|
|
* checked that a VARIADIC-labeled parameter actually is an array. So
|
|
|
|
* it should be okay to just Assert that it's an array rather than
|
|
|
|
* doing a full-fledged error check.
|
|
|
|
*/
|
|
|
|
Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 0))));
|
|
|
|
|
|
|
|
/* OK, safe to fetch the array value */
|
|
|
|
arr = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
|
|
|
|
/* Count the array elements */
|
|
|
|
ndims = ARR_NDIM(arr);
|
|
|
|
dims = ARR_DIMS(arr);
|
|
|
|
nitems = ArrayGetNItems(ndims, dims);
|
|
|
|
|
|
|
|
/* Count those that are NULL */
|
|
|
|
bitmap = ARR_NULLBITMAP(arr);
|
|
|
|
if (bitmap)
|
|
|
|
{
|
|
|
|
int bitmask = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++)
|
|
|
|
{
|
|
|
|
if ((*bitmap & bitmask) == 0)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
bitmask <<= 1;
|
|
|
|
if (bitmask == 0x100)
|
|
|
|
{
|
|
|
|
bitmap++;
|
|
|
|
bitmask = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*nargs = nitems;
|
|
|
|
*nulls = count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Separate arguments, so just count 'em */
|
|
|
|
for (i = 0; i < PG_NARGS(); i++)
|
|
|
|
{
|
|
|
|
if (PG_ARGISNULL(i))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nargs = PG_NARGS();
|
|
|
|
*nulls = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* num_nulls()
|
|
|
|
* Count the number of NULL arguments
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_num_nulls(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
int32 nargs,
|
|
|
|
nulls;
|
|
|
|
|
|
|
|
if (!count_nulls(fcinfo, &nargs, &nulls))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_INT32(nulls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* num_nonnulls()
|
|
|
|
* Count the number of non-NULL arguments
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_num_nonnulls(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
int32 nargs,
|
|
|
|
nulls;
|
|
|
|
|
|
|
|
if (!count_nulls(fcinfo, &nargs, &nulls))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_INT32(nargs - nulls);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-20 06:46:00 +02:00
|
|
|
/*
|
|
|
|
* current_database()
|
|
|
|
* Expose the current database to the user
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
current_database(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
Name db;
|
2002-08-20 06:46:00 +02:00
|
|
|
|
|
|
|
db = (Name) palloc(NAMEDATALEN);
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
namestrcpy(db, get_database_name(MyDatabaseId));
|
2002-08-20 06:46:00 +02:00
|
|
|
PG_RETURN_NAME(db);
|
|
|
|
}
|
2004-06-02 23:29:29 +02:00
|
|
|
|
|
|
|
|
2008-04-04 18:57:21 +02:00
|
|
|
/*
|
|
|
|
* current_query()
|
2009-06-11 16:49:15 +02:00
|
|
|
* Expose the current query to the user (useful in stored procedures)
|
|
|
|
* We might want to use ActivePortal->sourceText someday.
|
2008-04-04 18:57:21 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
current_query(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-01-08 01:13:22 +01:00
|
|
|
/* there is no easy way to access the more concise 'query_string' */
|
|
|
|
if (debug_query_string)
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
2008-04-04 18:57:21 +02:00
|
|
|
}
|
|
|
|
|
2004-06-02 23:29:29 +02:00
|
|
|
/*
|
2012-01-15 15:34:40 +01:00
|
|
|
* Send a signal to another backend.
|
2012-09-27 17:15:03 +02:00
|
|
|
*
|
2012-01-15 15:34:40 +01:00
|
|
|
* The signal is delivered if the user is either a superuser or the same
|
|
|
|
* role as the backend being signaled. For "dangerous" signals, an explicit
|
|
|
|
* check for superuser needs to be done prior to calling this function.
|
|
|
|
*
|
2015-03-19 20:02:33 +01:00
|
|
|
* Returns 0 on success, 1 on general failure, 2 on normal permission error
|
|
|
|
* and 3 if the caller needs to be a superuser.
|
|
|
|
*
|
2012-09-27 17:15:03 +02:00
|
|
|
* In the event of a general failure (return code 1), a warning message will
|
2012-01-15 15:34:40 +01:00
|
|
|
* be emitted. For permission errors, doing that is the responsibility of
|
|
|
|
* the caller.
|
2004-06-02 23:29:29 +02:00
|
|
|
*/
|
2012-01-15 15:34:40 +01:00
|
|
|
#define SIGNAL_BACKEND_SUCCESS 0
|
|
|
|
#define SIGNAL_BACKEND_ERROR 1
|
|
|
|
#define SIGNAL_BACKEND_NOPERMISSION 2
|
2015-03-19 20:02:33 +01:00
|
|
|
#define SIGNAL_BACKEND_NOSUPERUSER 3
|
2012-01-15 15:34:40 +01:00
|
|
|
static int
|
2008-04-15 22:28:47 +02:00
|
|
|
pg_signal_backend(int pid, int sig)
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
2012-09-27 17:15:03 +02:00
|
|
|
PGPROC *proc = BackendPidGetProc(pid);
|
2012-01-15 15:34:40 +01:00
|
|
|
|
2012-09-27 17:15:03 +02:00
|
|
|
/*
|
|
|
|
* BackendPidGetProc returns NULL if the pid isn't valid; but by the time
|
2013-05-29 22:58:43 +02:00
|
|
|
* we reach kill(), a process for which we get a valid proc here might
|
2014-05-06 18:12:18 +02:00
|
|
|
* have terminated on its own. There's no way to acquire a lock on an
|
2013-05-29 22:58:43 +02:00
|
|
|
* arbitrary process to prevent that. But since so far all the callers of
|
|
|
|
* this mechanism involve some request for ending the process anyway, that
|
|
|
|
* it might end on its own first is not a problem.
|
2012-09-27 17:15:03 +02:00
|
|
|
*/
|
|
|
|
if (proc == NULL)
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This is just a warning so a loop-through-resultset will not abort
|
2012-09-27 17:15:03 +02:00
|
|
|
* if one backend terminated on its own during the run.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
2004-06-02 23:29:29 +02:00
|
|
|
ereport(WARNING,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("PID %d is not a PostgreSQL server process", pid)));
|
2012-01-15 15:34:40 +01:00
|
|
|
return SIGNAL_BACKEND_ERROR;
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-19 20:02:33 +01:00
|
|
|
/* Only allow superusers to signal superuser-owned backends. */
|
|
|
|
if (superuser_arg(proc->roleId) && !superuser())
|
|
|
|
return SIGNAL_BACKEND_NOSUPERUSER;
|
|
|
|
|
|
|
|
/* Users can signal backends they have role membership in. */
|
|
|
|
if (!has_privs_of_role(GetUserId(), proc->roleId))
|
2012-09-27 17:15:03 +02:00
|
|
|
return SIGNAL_BACKEND_NOPERMISSION;
|
|
|
|
|
2012-01-15 15:34:40 +01:00
|
|
|
/*
|
|
|
|
* Can the process we just validated above end, followed by the pid being
|
|
|
|
* recycled for a new process, before reaching here? Then we'd be trying
|
|
|
|
* to kill the wrong thing. Seems near impossible when sequential pid
|
|
|
|
* assignment and wraparound is used. Perhaps it could happen on a system
|
2014-05-06 18:12:18 +02:00
|
|
|
* where pid re-use is randomized. That race condition possibility seems
|
2012-01-15 15:34:40 +01:00
|
|
|
* too unlikely to worry about.
|
|
|
|
*/
|
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
/* If we have setsid(), signal the backend's whole process group */
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (kill(-pid, sig))
|
|
|
|
#else
|
2004-08-29 07:07:03 +02:00
|
|
|
if (kill(pid, sig))
|
2006-11-21 21:59:53 +01:00
|
|
|
#endif
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
|
|
|
/* Again, just a warning to allow loops */
|
|
|
|
ereport(WARNING,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg("could not send signal to process %d: %m", pid)));
|
2012-01-15 15:34:40 +01:00
|
|
|
return SIGNAL_BACKEND_ERROR;
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
2012-01-15 15:34:40 +01:00
|
|
|
return SIGNAL_BACKEND_SUCCESS;
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2012-01-15 15:34:40 +01:00
|
|
|
/*
|
2015-03-19 20:02:33 +01:00
|
|
|
* Signal to cancel a backend process. This is allowed if you are a member of
|
|
|
|
* the role whose process is being canceled.
|
|
|
|
*
|
|
|
|
* Note that only superusers can signal superuser-owned processes.
|
2012-01-15 15:34:40 +01:00
|
|
|
*/
|
2004-06-02 23:29:29 +02:00
|
|
|
Datum
|
2004-08-03 22:32:36 +02:00
|
|
|
pg_cancel_backend(PG_FUNCTION_ARGS)
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
2012-01-15 15:34:40 +01:00
|
|
|
int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
|
|
|
|
|
2015-03-19 20:02:33 +01:00
|
|
|
if (r == SIGNAL_BACKEND_NOSUPERUSER)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be a superuser to cancel superuser query"))));
|
|
|
|
|
2012-01-15 15:34:40 +01:00
|
|
|
if (r == SIGNAL_BACKEND_NOPERMISSION)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2015-10-29 01:23:53 +01:00
|
|
|
(errmsg("must be a member of the role whose query is being canceled"))));
|
2012-01-15 15:34:40 +01:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(r == SIGNAL_BACKEND_SUCCESS);
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2012-01-15 15:34:40 +01:00
|
|
|
/*
|
2015-03-19 20:02:33 +01:00
|
|
|
* Signal to terminate a backend process. This is allowed if you are a member
|
|
|
|
* of the role whose process is being terminated.
|
|
|
|
*
|
|
|
|
* Note that only superusers can signal superuser-owned processes.
|
2012-01-15 15:34:40 +01:00
|
|
|
*/
|
2008-04-17 22:56:41 +02:00
|
|
|
Datum
|
|
|
|
pg_terminate_backend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2012-06-26 22:16:52 +02:00
|
|
|
int r = pg_signal_backend(PG_GETARG_INT32(0), SIGTERM);
|
|
|
|
|
2015-03-19 20:02:33 +01:00
|
|
|
if (r == SIGNAL_BACKEND_NOSUPERUSER)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2015-05-24 03:35:49 +02:00
|
|
|
(errmsg("must be a superuser to terminate superuser process"))));
|
2015-03-19 20:02:33 +01:00
|
|
|
|
2012-06-26 22:16:52 +02:00
|
|
|
if (r == SIGNAL_BACKEND_NOPERMISSION)
|
2012-01-15 15:34:40 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2015-03-19 20:02:33 +01:00
|
|
|
(errmsg("must be a member of the role whose process is being terminated"))));
|
2012-01-15 15:34:40 +01:00
|
|
|
|
2012-06-26 22:16:52 +02:00
|
|
|
PG_RETURN_BOOL(r == SIGNAL_BACKEND_SUCCESS);
|
2008-04-17 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
2012-01-15 15:34:40 +01:00
|
|
|
/*
|
|
|
|
* Signal to reload the database configuration
|
|
|
|
*/
|
2005-08-12 05:25:13 +02:00
|
|
|
Datum
|
|
|
|
pg_reload_conf(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
if (!superuser())
|
2005-08-12 05:25:13 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to signal the postmaster"))));
|
|
|
|
|
|
|
|
if (kill(PostmasterPid, SIGHUP))
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("failed to send signal to postmaster: %m")));
|
2005-09-16 07:35:41 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
2005-09-16 07:35:41 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rotate log file
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_rotate_logfile(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
if (!superuser())
|
2005-08-12 05:25:13 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to rotate log files"))));
|
|
|
|
|
2007-08-19 03:41:25 +02:00
|
|
|
if (!Logging_collector)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2005-08-12 20:23:56 +02:00
|
|
|
ereport(WARNING,
|
2007-08-19 03:41:25 +02:00
|
|
|
(errmsg("rotation not possible because log collection not active")));
|
2005-09-16 07:35:41 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SendPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE);
|
2005-09-16 07:35:41 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/* Function to find out which databases make use of a tablespace */
|
2004-07-02 20:59:25 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
typedef struct
|
2004-07-02 20:59:25 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char *location;
|
|
|
|
DIR *dirdesc;
|
2004-07-02 20:59:25 +02:00
|
|
|
} ts_db_fctx;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
Datum
|
|
|
|
pg_tablespace_databases(PG_FUNCTION_ARGS)
|
2004-07-02 20:59:25 +02:00
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
struct dirent *de;
|
|
|
|
ts_db_fctx *fctx;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
2004-08-29 07:07:03 +02:00
|
|
|
Oid tablespaceOid = PG_GETARG_OID(0);
|
2004-07-02 20:59:25 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2004-07-02 20:59:25 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
fctx = palloc(sizeof(ts_db_fctx));
|
|
|
|
|
|
|
|
if (tablespaceOid == GLOBALTABLESPACE_OID)
|
|
|
|
{
|
|
|
|
fctx->dirdesc = NULL;
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("global tablespace never has databases")));
|
2004-07-02 20:59:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-05-11 00:27:30 +02:00
|
|
|
if (tablespaceOid == DEFAULTTABLESPACE_OID)
|
2013-10-13 06:09:18 +02:00
|
|
|
fctx->location = psprintf("base");
|
2005-05-11 00:27:30 +02:00
|
|
|
else
|
2013-10-13 06:09:18 +02:00
|
|
|
fctx->location = psprintf("pg_tblspc/%u/%s", tablespaceOid,
|
2014-05-06 18:12:18 +02:00
|
|
|
TABLESPACE_VERSION_DIRECTORY);
|
2005-05-11 00:27:30 +02:00
|
|
|
|
2004-07-02 20:59:25 +02:00
|
|
|
fctx->dirdesc = AllocateDir(fctx->location);
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
if (!fctx->dirdesc)
|
|
|
|
{
|
|
|
|
/* the only expected error is ENOENT */
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open directory \"%s\": %m",
|
|
|
|
fctx->location)));
|
|
|
|
ereport(WARNING,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("%u is not a tablespace OID", tablespaceOid)));
|
2004-08-03 22:32:36 +02:00
|
|
|
}
|
2004-07-02 20:59:25 +02:00
|
|
|
}
|
|
|
|
funcctx->user_fctx = fctx;
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
|
|
|
fctx = (ts_db_fctx *) funcctx->user_fctx;
|
2004-07-02 20:59:25 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!fctx->dirdesc) /* not a tablespace */
|
2004-07-02 20:59:25 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
|
2005-06-19 23:34:03 +02:00
|
|
|
while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
|
2004-07-02 20:59:25 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char *subdir;
|
|
|
|
DIR *dirdesc;
|
|
|
|
Oid datOid = atooid(de->d_name);
|
2004-07-02 20:59:25 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/* this test skips . and .., but is awfully weak */
|
2004-07-02 20:59:25 +02:00
|
|
|
if (!datOid)
|
|
|
|
continue;
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/* if database subdir is empty, don't report tablespace as used */
|
|
|
|
|
2013-10-13 06:09:18 +02:00
|
|
|
subdir = psprintf("%s/%s", fctx->location, de->d_name);
|
2004-07-02 20:59:25 +02:00
|
|
|
dirdesc = AllocateDir(subdir);
|
2005-06-19 23:34:03 +02:00
|
|
|
while ((de = ReadDir(dirdesc, subdir)) != NULL)
|
2004-08-03 22:32:36 +02:00
|
|
|
{
|
|
|
|
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
|
|
|
|
break;
|
2004-07-02 20:59:25 +02:00
|
|
|
}
|
2004-08-03 22:32:36 +02:00
|
|
|
FreeDir(dirdesc);
|
2005-06-19 23:34:03 +02:00
|
|
|
pfree(subdir);
|
2004-08-03 22:32:36 +02:00
|
|
|
|
|
|
|
if (!de)
|
|
|
|
continue; /* indeed, nothing in it */
|
2004-07-02 20:59:25 +02:00
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(fctx->dirdesc);
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
2006-01-11 21:12:43 +01:00
|
|
|
|
|
|
|
|
2011-12-07 10:35:00 +01:00
|
|
|
/*
|
|
|
|
* pg_tablespace_location - get location for a tablespace
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_tablespace_location(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
Oid tablespaceOid = PG_GETARG_OID(0);
|
|
|
|
char sourcepath[MAXPGPATH];
|
|
|
|
char targetpath[MAXPGPATH];
|
|
|
|
int rllen;
|
2011-12-07 10:35:00 +01:00
|
|
|
|
|
|
|
/*
|
2012-04-11 03:42:46 +02:00
|
|
|
* It's useful to apply this function to pg_class.reltablespace, wherein
|
2014-05-06 18:12:18 +02:00
|
|
|
* zero means "the database's default tablespace". So, rather than
|
2012-04-11 03:42:46 +02:00
|
|
|
* throwing an error for zero, we choose to assume that's what is meant.
|
|
|
|
*/
|
|
|
|
if (tablespaceOid == InvalidOid)
|
|
|
|
tablespaceOid = MyDatabaseTableSpace;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return empty string for the cluster's default tablespaces
|
2011-12-07 10:35:00 +01:00
|
|
|
*/
|
|
|
|
if (tablespaceOid == DEFAULTTABLESPACE_OID ||
|
|
|
|
tablespaceOid == GLOBALTABLESPACE_OID)
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(""));
|
|
|
|
|
|
|
|
#if defined(HAVE_READLINK) || defined(WIN32)
|
2012-06-10 21:20:04 +02:00
|
|
|
|
2011-12-07 10:35:00 +01:00
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* Find the location of the tablespace by reading the symbolic link that
|
|
|
|
* is in pg_tblspc/<oid>.
|
2011-12-07 10:35:00 +01:00
|
|
|
*/
|
|
|
|
snprintf(sourcepath, sizeof(sourcepath), "pg_tblspc/%u", tablespaceOid);
|
2011-12-07 19:34:13 +01:00
|
|
|
|
|
|
|
rllen = readlink(sourcepath, targetpath, sizeof(targetpath));
|
2011-12-07 12:17:55 +01:00
|
|
|
if (rllen < 0)
|
2011-12-07 10:35:00 +01:00
|
|
|
ereport(ERROR,
|
2015-05-28 18:17:22 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read symbolic link \"%s\": %m",
|
2011-12-07 19:34:13 +01:00
|
|
|
sourcepath)));
|
2015-05-28 18:17:22 +02:00
|
|
|
if (rllen >= sizeof(targetpath))
|
2011-12-07 12:17:55 +01:00
|
|
|
ereport(ERROR,
|
2015-05-28 18:17:22 +02:00
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("symbolic link \"%s\" target is too long",
|
2011-12-07 19:34:13 +01:00
|
|
|
sourcepath)));
|
2011-12-07 10:35:00 +01:00
|
|
|
targetpath[rllen] = '\0';
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(targetpath));
|
|
|
|
#else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("tablespaces are not supported on this platform")));
|
2011-12-07 19:34:13 +01:00
|
|
|
PG_RETURN_NULL();
|
2011-12-07 10:35:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-01-11 21:12:43 +01:00
|
|
|
/*
|
|
|
|
* pg_sleep - delay for N seconds
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_sleep(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
float8 secs = PG_GETARG_FLOAT8(0);
|
|
|
|
float8 endtime;
|
|
|
|
|
|
|
|
/*
|
2013-06-15 22:22:29 +02:00
|
|
|
* We sleep using WaitLatch, to ensure that we'll wake up promptly if an
|
|
|
|
* important signal (such as SIGALRM or SIGINT) arrives. Because
|
|
|
|
* WaitLatch's upper limit of delay is INT_MAX milliseconds, and the user
|
|
|
|
* might ask for more than that, we sleep for at most 10 minutes and then
|
|
|
|
* loop.
|
2006-01-11 21:12:43 +01:00
|
|
|
*
|
2006-10-04 02:30:14 +02:00
|
|
|
* By computing the intended stop time initially, we avoid accumulation of
|
2014-05-06 18:12:18 +02:00
|
|
|
* extra delay across multiple sleeps. This also ensures we won't delay
|
2013-06-15 22:22:29 +02:00
|
|
|
* less than the specified time when WaitLatch is terminated early by a
|
|
|
|
* non-query-cancelling signal such as SIGHUP.
|
2006-01-11 21:12:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
#define GetNowFloat() ((float8) GetCurrentTimestamp() / 1000000.0)
|
|
|
|
#else
|
|
|
|
#define GetNowFloat() GetCurrentTimestamp()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
endtime = GetNowFloat() + secs;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
float8 delay;
|
2013-06-15 22:22:29 +02:00
|
|
|
long delay_ms;
|
2006-01-11 21:12:43 +01:00
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
2013-06-15 22:22:29 +02:00
|
|
|
|
2006-01-11 21:12:43 +01:00
|
|
|
delay = endtime - GetNowFloat();
|
2013-06-15 22:22:29 +02:00
|
|
|
if (delay >= 600.0)
|
|
|
|
delay_ms = 600000;
|
2006-01-11 21:12:43 +01:00
|
|
|
else if (delay > 0.0)
|
2013-06-15 22:22:29 +02:00
|
|
|
delay_ms = (long) ceil(delay * 1000.0);
|
2006-01-11 21:12:43 +01:00
|
|
|
else
|
|
|
|
break;
|
2013-06-15 22:22:29 +02:00
|
|
|
|
2015-01-14 18:45:22 +01:00
|
|
|
(void) WaitLatch(MyLatch,
|
2013-06-15 22:22:29 +02:00
|
|
|
WL_LATCH_SET | WL_TIMEOUT,
|
|
|
|
delay_ms);
|
2015-01-14 18:45:22 +01:00
|
|
|
ResetLatch(MyLatch);
|
2006-01-11 21:12:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
2008-07-03 22:58:47 +02:00
|
|
|
|
|
|
|
/* Function to return the list of grammar keywords */
|
|
|
|
Datum
|
|
|
|
pg_get_keywords(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
tupdesc = CreateTemplateTupleDesc(3, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
|
|
|
|
TEXTOID, -1, 0);
|
2009-06-11 16:49:15 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catcode",
|
2008-07-03 22:58:47 +02:00
|
|
|
CHAROID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "catdesc",
|
|
|
|
TEXTOID, -1, 0);
|
|
|
|
|
|
|
|
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
|
|
|
|
2009-07-14 22:24:10 +02:00
|
|
|
if (funcctx->call_cntr < NumScanKeywords)
|
2008-07-03 22:58:47 +02:00
|
|
|
{
|
|
|
|
char *values[3];
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
/* cast-away-const is ugly but alternatives aren't much better */
|
|
|
|
values[0] = (char *) ScanKeywords[funcctx->call_cntr].name;
|
|
|
|
|
|
|
|
switch (ScanKeywords[funcctx->call_cntr].category)
|
|
|
|
{
|
|
|
|
case UNRESERVED_KEYWORD:
|
|
|
|
values[1] = "U";
|
2008-12-05 14:41:20 +01:00
|
|
|
values[2] = _("unreserved");
|
2008-07-03 22:58:47 +02:00
|
|
|
break;
|
|
|
|
case COL_NAME_KEYWORD:
|
|
|
|
values[1] = "C";
|
2008-12-05 14:41:20 +01:00
|
|
|
values[2] = _("unreserved (cannot be function or type name)");
|
2008-07-03 22:58:47 +02:00
|
|
|
break;
|
|
|
|
case TYPE_FUNC_NAME_KEYWORD:
|
|
|
|
values[1] = "T";
|
2008-12-05 14:41:20 +01:00
|
|
|
values[2] = _("reserved (can be function or type name)");
|
2008-07-03 22:58:47 +02:00
|
|
|
break;
|
|
|
|
case RESERVED_KEYWORD:
|
|
|
|
values[1] = "R";
|
2008-12-05 14:41:20 +01:00
|
|
|
values[2] = _("reserved");
|
2008-07-03 22:58:47 +02:00
|
|
|
break;
|
|
|
|
default: /* shouldn't be possible */
|
|
|
|
values[1] = NULL;
|
|
|
|
values[2] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
|
|
|
|
}
|
|
|
|
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
2008-11-03 18:51:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the type of the argument.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_typeof(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
|
|
|
|
}
|
2012-03-02 20:12:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the COLLATE FOR expression; returns the collation
|
|
|
|
* of the argument.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_collation_for(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
Oid typeid;
|
|
|
|
Oid collid;
|
2012-03-02 20:12:16 +01:00
|
|
|
|
|
|
|
typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
|
|
|
|
if (!typeid)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (!type_is_collatable(typeid) && typeid != UNKNOWNOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
|
|
|
errmsg("collations are not supported by type %s",
|
|
|
|
format_type_be(typeid))));
|
|
|
|
|
|
|
|
collid = PG_GET_COLLATION();
|
|
|
|
if (!collid)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(generate_collation_name(collid)));
|
|
|
|
}
|
2012-12-09 00:25:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-06-12 23:52:54 +02:00
|
|
|
* pg_relation_is_updatable - determine which update events the specified
|
|
|
|
* relation supports.
|
2012-12-09 00:25:48 +01:00
|
|
|
*
|
2013-06-12 23:52:54 +02:00
|
|
|
* This relies on relation_is_updatable() in rewriteHandler.c, which see
|
|
|
|
* for additional information.
|
2012-12-09 00:25:48 +01:00
|
|
|
*/
|
|
|
|
Datum
|
2013-06-12 23:52:54 +02:00
|
|
|
pg_relation_is_updatable(PG_FUNCTION_ARGS)
|
2012-12-09 00:25:48 +01:00
|
|
|
{
|
2013-06-12 23:52:54 +02:00
|
|
|
Oid reloid = PG_GETARG_OID(0);
|
|
|
|
bool include_triggers = PG_GETARG_BOOL(1);
|
2012-12-09 00:25:48 +01:00
|
|
|
|
2013-10-18 16:35:36 +02:00
|
|
|
PG_RETURN_INT32(relation_is_updatable(reloid, include_triggers, NULL));
|
2012-12-09 00:25:48 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:52:54 +02:00
|
|
|
/*
|
|
|
|
* pg_column_is_updatable - determine whether a column is updatable
|
|
|
|
*
|
2013-10-18 16:35:36 +02:00
|
|
|
* This function encapsulates the decision about just what
|
2014-05-06 18:12:18 +02:00
|
|
|
* information_schema.columns.is_updatable actually means. It's not clear
|
2013-06-12 23:52:54 +02:00
|
|
|
* whether deletability of the column's relation should be required, so
|
|
|
|
* we want that decision in C code where we could change it without initdb.
|
|
|
|
*/
|
2012-12-09 00:25:48 +01:00
|
|
|
Datum
|
2013-06-12 23:52:54 +02:00
|
|
|
pg_column_is_updatable(PG_FUNCTION_ARGS)
|
2012-12-09 00:25:48 +01:00
|
|
|
{
|
2013-06-12 23:52:54 +02:00
|
|
|
Oid reloid = PG_GETARG_OID(0);
|
|
|
|
AttrNumber attnum = PG_GETARG_INT16(1);
|
2013-10-18 16:35:36 +02:00
|
|
|
AttrNumber col = attnum - FirstLowInvalidHeapAttributeNumber;
|
2013-06-12 23:52:54 +02:00
|
|
|
bool include_triggers = PG_GETARG_BOOL(2);
|
|
|
|
int events;
|
|
|
|
|
|
|
|
/* System columns are never updatable */
|
|
|
|
if (attnum <= 0)
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
2013-10-18 16:35:36 +02:00
|
|
|
events = relation_is_updatable(reloid, include_triggers,
|
|
|
|
bms_make_singleton(col));
|
2013-06-12 23:52:54 +02:00
|
|
|
|
|
|
|
/* We require both updatability and deletability of the relation */
|
|
|
|
#define REQ_EVENTS ((1 << CMD_UPDATE) | (1 << CMD_DELETE))
|
2012-12-09 00:25:48 +01:00
|
|
|
|
2013-06-12 23:52:54 +02:00
|
|
|
PG_RETURN_BOOL((events & REQ_EVENTS) == REQ_EVENTS);
|
2012-12-09 00:25:48 +01:00
|
|
|
}
|
2016-03-18 16:16:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This simple parser utility are compatible with lexer implementation,
|
|
|
|
* used only in parse_ident function
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_ident_start(unsigned char c)
|
|
|
|
{
|
|
|
|
if (c == '_')
|
|
|
|
return true;
|
|
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (c >= 0200 && c <= 0377)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_ident_cont(unsigned char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return is_ident_start(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanitize SQL string for using in error message.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
sanitize_text(text *t)
|
|
|
|
{
|
|
|
|
int len = VARSIZE_ANY_EXHDR(t);
|
|
|
|
const char *p = VARDATA_ANY(t);
|
|
|
|
StringInfo dstr;
|
|
|
|
|
|
|
|
dstr = makeStringInfo();
|
|
|
|
|
|
|
|
appendStringInfoChar(dstr, '"');
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '\b':
|
|
|
|
appendStringInfoString(dstr, "\\b");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
appendStringInfoString(dstr, "\\f");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
appendStringInfoString(dstr, "\\n");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
appendStringInfoString(dstr, "\\r");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
appendStringInfoString(dstr, "\\t");
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
appendStringInfoString(dstr, "''");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
appendStringInfoString(dstr, "\\\\");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((unsigned char) *p < ' ')
|
|
|
|
appendStringInfo(dstr, "\\u%04x", (int) *p);
|
|
|
|
else
|
|
|
|
appendStringInfoCharMacro(dstr, *p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfoChar(dstr, '"');
|
|
|
|
|
|
|
|
return dstr->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse_ident - parse SQL composed identifier to separate identifiers.
|
|
|
|
* When strict mode is active (second parameter), then any chars after
|
|
|
|
* last identifiers are disallowed.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
parse_ident(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *qualname;
|
|
|
|
char *qualname_str;
|
|
|
|
bool strict;
|
|
|
|
char *nextp;
|
|
|
|
bool after_dot = false;
|
|
|
|
ArrayBuildState *astate = NULL;
|
|
|
|
|
|
|
|
qualname = PG_GETARG_TEXT_PP(0);
|
|
|
|
qualname_str = text_to_cstring(qualname);
|
|
|
|
strict = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
nextp = qualname_str;
|
|
|
|
|
|
|
|
/* skip leading whitespace */
|
|
|
|
while (isspace((unsigned char) *nextp))
|
|
|
|
nextp++;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char *curname;
|
|
|
|
char *endp;
|
|
|
|
bool missing_ident;
|
|
|
|
|
|
|
|
missing_ident = true;
|
|
|
|
|
|
|
|
if (*nextp == '\"')
|
|
|
|
{
|
|
|
|
curname = nextp + 1;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
endp = strchr(nextp + 1, '\"');
|
|
|
|
if (endp == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unclosed double quotes"),
|
|
|
|
errdetail("string %s is not valid identifier",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
if (endp[1] != '\"')
|
|
|
|
break;
|
|
|
|
memmove(endp, endp + 1, strlen(endp));
|
|
|
|
nextp = endp;
|
|
|
|
}
|
|
|
|
nextp = endp + 1;
|
|
|
|
*endp = '\0';
|
|
|
|
|
|
|
|
/* Show complete input string in this case. */
|
|
|
|
if (endp - curname == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("identifier should not be empty: %s",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
|
|
|
|
astate = accumArrayResult(astate, CStringGetTextDatum(curname),
|
|
|
|
false, TEXTOID, CurrentMemoryContext);
|
|
|
|
missing_ident = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_ident_start((unsigned char) *nextp))
|
|
|
|
{
|
|
|
|
char *downname;
|
|
|
|
int len;
|
|
|
|
text *part;
|
|
|
|
|
|
|
|
curname = nextp++;
|
|
|
|
while (is_ident_cont((unsigned char) *nextp))
|
|
|
|
nextp++;
|
|
|
|
|
|
|
|
len = nextp - curname;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlike name, we don't implicitly truncate identifiers. This
|
|
|
|
* is useful for allowing the user to check for specific parts
|
|
|
|
* of the identifier being too long. It's easy enough for the
|
|
|
|
* user to get the truncated names by casting our output to
|
|
|
|
* name[].
|
|
|
|
*/
|
|
|
|
downname = downcase_identifier(curname, len, false, false);
|
|
|
|
part = cstring_to_text_with_len(downname, len);
|
|
|
|
astate = accumArrayResult(astate, PointerGetDatum(part), false,
|
|
|
|
TEXTOID, CurrentMemoryContext);
|
|
|
|
missing_ident = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (missing_ident)
|
|
|
|
{
|
|
|
|
/* Different error messages based on where we failed. */
|
|
|
|
if (*nextp == '.')
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("missing valid identifier before \".\" symbol: %s",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
else if (after_dot)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("missing valid identifier after \".\" symbol: %s",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("missing valid identifier: %s",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (isspace((unsigned char) *nextp))
|
|
|
|
nextp++;
|
|
|
|
|
|
|
|
if (*nextp == '.')
|
|
|
|
{
|
|
|
|
after_dot = true;
|
|
|
|
nextp++;
|
|
|
|
while (isspace((unsigned char) *nextp))
|
|
|
|
nextp++;
|
|
|
|
}
|
|
|
|
else if (*nextp == '\0')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strict)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("identifier contains disallowed characters: %s",
|
|
|
|
sanitize_text(qualname))));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
|
|
|
|
}
|