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
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, 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
|
2006-11-21 21:59:53 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.55 2006/11/21 20:59:53 tgl Exp $
|
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>
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "catalog/pg_tablespace.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"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "postmaster/syslogger.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"
|
|
|
|
#include "storage/procarray.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Check if data is Null
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
nullvalue(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
PG_RETURN_BOOL(false);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
/*
|
|
|
|
* Check if data is not Null
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
nonnullvalue(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
PG_RETURN_BOOL(true);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-03 22:32:36 +02:00
|
|
|
* Functions to send signals to other backends.
|
2004-06-02 23:29:29 +02:00
|
|
|
*/
|
2005-09-16 07:35:41 +02:00
|
|
|
static bool
|
2004-08-29 07:07:03 +02:00
|
|
|
pg_signal_backend(int pid, int sig)
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!superuser())
|
2004-06-02 23:29:29 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("must be superuser to signal other server processes"))));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-06-02 23:29:29 +02:00
|
|
|
if (!IsBackendPid(pid))
|
|
|
|
{
|
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
|
|
|
|
* if one backend terminated on it's 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)));
|
2005-09-16 07:35:41 +02:00
|
|
|
return false;
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
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)));
|
2005-09-16 07:35:41 +02:00
|
|
|
return false;
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
2005-09-16 07:35:41 +02:00
|
|
|
return true;
|
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
|
|
|
{
|
2005-09-16 07:35:41 +02:00
|
|
|
PG_RETURN_BOOL(pg_signal_backend(PG_GETARG_INT32(0), SIGINT));
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
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"))));
|
|
|
|
|
|
|
|
if (!Redirect_stderr)
|
|
|
|
{
|
2005-08-12 20:23:56 +02:00
|
|
|
ereport(WARNING,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("rotation not possible because log redirection 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
|
|
|
#ifdef NOT_USED
|
|
|
|
|
|
|
|
/* Disabled in 8.0 due to reliability concerns; FIXME someday */
|
2004-06-02 23:29:29 +02:00
|
|
|
Datum
|
2004-08-03 22:32:36 +02:00
|
|
|
pg_terminate_backend(PG_FUNCTION_ARGS)
|
2004-06-02 23:29:29 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0), SIGTERM));
|
2004-06-02 23:29:29 +02:00
|
|
|
}
|
2004-08-03 22:32:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
2005-05-11 00:27:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* size = tablespace dirname length + dir sep char + oid + terminator
|
2005-05-11 00:27:30 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
fctx->location = (char *) palloc(10 + 10 + 1);
|
2004-07-02 20:59:25 +02:00
|
|
|
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)
|
2005-07-04 06:51:52 +02:00
|
|
|
sprintf(fctx->location, "base");
|
2005-05-11 00:27:30 +02:00
|
|
|
else
|
2005-07-04 06:51:52 +02:00
|
|
|
sprintf(fctx->location, "pg_tblspc/%u", tablespaceOid);
|
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 */
|
|
|
|
|
2004-07-02 20:59:25 +02:00
|
|
|
/* size = path length + dir sep char + file name + terminator */
|
|
|
|
subdir = palloc(strlen(fctx->location) + 1 + strlen(de->d_name) + 1);
|
|
|
|
sprintf(subdir, "%s/%s", fctx->location, de->d_name);
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_sleep - delay for N seconds
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_sleep(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
float8 secs = PG_GETARG_FLOAT8(0);
|
|
|
|
float8 endtime;
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* We break the requested sleep into segments of no more than 1 second, to
|
|
|
|
* put an upper bound on how long it will take us to respond to a cancel
|
|
|
|
* or die interrupt. (Note that pg_usleep is interruptible by signals on
|
|
|
|
* some platforms but not others.) Also, this method avoids exposing
|
|
|
|
* pg_usleep's upper bound on allowed delays.
|
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
|
|
|
|
* extra delay across multiple sleeps. This also ensures we won't delay
|
|
|
|
* less than the specified time if pg_usleep is interrupted by other
|
|
|
|
* signals 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;
|
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
delay = endtime - GetNowFloat();
|
|
|
|
if (delay >= 1.0)
|
|
|
|
pg_usleep(1000000L);
|
|
|
|
else if (delay > 0.0)
|
|
|
|
pg_usleep((long) ceil(delay * 1000000.0));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|