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
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, 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
|
2005-05-19 23:35:48 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.43 2005/05/19 21:35:47 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>
|
2000-06-05 09:29:25 +02:00
|
|
|
|
2003-06-27 19:07:03 +02:00
|
|
|
#include "commands/dbcommands.h"
|
2002-08-20 06:46:00 +02:00
|
|
|
#include "miscadmin.h"
|
2005-05-19 23:35:48 +02:00
|
|
|
#include "storage/procarray.h"
|
2004-07-02 20:59:25 +02:00
|
|
|
#include "storage/fd.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/builtins.h"
|
2004-07-02 20:59:25 +02:00
|
|
|
#include "funcapi.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "catalog/pg_tablespace.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
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
static int
|
|
|
|
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),
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg("must be superuser to signal other server processes"))));
|
|
|
|
|
2004-06-02 23:29:29 +02:00
|
|
|
if (!IsBackendPid(pid))
|
|
|
|
{
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +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,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg("PID %d is not a PostgreSQL server process", pid)));
|
2004-06-02 23:29:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (kill(pid, sig))
|
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)));
|
2004-06-02 23:29:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2004-08-03 22:32:36 +02:00
|
|
|
pg_cancel_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), SIGINT));
|
2004-06-02 23:29:29 +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
|
|
|
/*
|
|
|
|
* size = path length + tablespace dirname length + 2 dir sep
|
|
|
|
* chars + oid + terminator
|
|
|
|
*/
|
|
|
|
fctx->location = (char *) palloc(strlen(DataDir) + 11 + 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)
|
|
|
|
sprintf(fctx->location, "%s/base", DataDir);
|
|
|
|
else
|
|
|
|
sprintf(fctx->location, "%s/pg_tblspc/%u", DataDir,
|
|
|
|
tablespaceOid);
|
|
|
|
|
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,
|
2004-10-12 23:54:45 +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);
|
|
|
|
|
|
|
|
while ((de = readdir(fctx->dirdesc)) != NULL)
|
|
|
|
{
|
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);
|
2004-08-03 22:32:36 +02:00
|
|
|
pfree(subdir);
|
|
|
|
if (!dirdesc)
|
|
|
|
continue; /* XXX more sloppiness */
|
2004-07-02 20:59:25 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
while ((de = readdir(dirdesc)) != 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|