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
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, 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
|
2004-07-02 20:59:25 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.35 2004/07/02 18:59:22 joe 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"
|
2004-06-02 23:29:29 +02:00
|
|
|
#include "storage/sinval.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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions to terminate a backend or cancel a query running on
|
|
|
|
* a different backend.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int pg_signal_backend(int pid, int sig)
|
|
|
|
{
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("only superuser can signal other backends"))));
|
|
|
|
|
|
|
|
if (!IsBackendPid(pid))
|
|
|
|
{
|
|
|
|
/* This is just a warning so a loop-through-resultset will not abort
|
|
|
|
* if one backend terminated on it's own during the run */
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("pid %i is not a postgresql backend",pid)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kill(pid, sig))
|
|
|
|
{
|
|
|
|
/* Again, just a warning to allow loops */
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("failed to send signal to backend %i: %m",pid)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_terminate_backend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGTERM));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_cancel_backend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGINT));
|
|
|
|
}
|
2004-07-02 20:59:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *location;
|
|
|
|
DIR *dirdesc;
|
|
|
|
} ts_db_fctx;
|
|
|
|
|
|
|
|
Datum pg_tablespace_databases(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
struct dirent *de;
|
|
|
|
ts_db_fctx *fctx;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
Oid tablespaceOid=PG_GETARG_OID(0);
|
|
|
|
|
|
|
|
funcctx=SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
fctx = palloc(sizeof(ts_db_fctx));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* size = path length + tablespace dirname length
|
|
|
|
* + 2 dir sep chars + oid + terminator
|
|
|
|
*/
|
|
|
|
fctx->location = (char*) palloc(strlen(DataDir) + 11 + 10 + 1);
|
|
|
|
if (tablespaceOid == GLOBALTABLESPACE_OID)
|
|
|
|
{
|
|
|
|
fctx->dirdesc = NULL;
|
|
|
|
ereport(NOTICE,
|
|
|
|
(errcode(ERRCODE_WARNING),
|
|
|
|
errmsg("global tablespace never has databases.")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tablespaceOid == DEFAULTTABLESPACE_OID)
|
|
|
|
sprintf(fctx->location, "%s/base", DataDir);
|
|
|
|
else
|
|
|
|
sprintf(fctx->location, "%s/pg_tblspc/%u", DataDir,
|
|
|
|
tablespaceOid);
|
|
|
|
|
|
|
|
fctx->dirdesc = AllocateDir(fctx->location);
|
|
|
|
|
|
|
|
if (!fctx->dirdesc) /* not a tablespace */
|
|
|
|
ereport(NOTICE,
|
|
|
|
(errcode(ERRCODE_WARNING),
|
|
|
|
errmsg("%d is no tablespace oid.", tablespaceOid)));
|
|
|
|
}
|
|
|
|
funcctx->user_fctx = fctx;
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
funcctx=SRF_PERCALL_SETUP();
|
|
|
|
fctx = (ts_db_fctx*) funcctx->user_fctx;
|
|
|
|
|
|
|
|
if (!fctx->dirdesc) /* not a tablespace */
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
|
|
|
|
while ((de = readdir(fctx->dirdesc)) != NULL)
|
|
|
|
{
|
|
|
|
char *subdir;
|
|
|
|
DIR *dirdesc;
|
|
|
|
|
|
|
|
Oid datOid = atol(de->d_name);
|
|
|
|
if (!datOid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (dirdesc)
|
|
|
|
{
|
|
|
|
while ((de = readdir(dirdesc)) != 0)
|
|
|
|
{
|
|
|
|
if (strcmp(de->d_name, ".") && strcmp(de->d_name, ".."))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pfree(subdir);
|
|
|
|
FreeDir(dirdesc);
|
|
|
|
|
|
|
|
if (!de) /* database subdir is empty; don't report tablespace as used */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(fctx->dirdesc);
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|