1
/*-------------------------------------------------------------------------
6
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
11
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.40 2004-12-31 22:01:22 pgsql Exp $
13
*-------------------------------------------------------------------------
21
#include "commands/dbcommands.h"
22
#include "miscadmin.h"
23
#include "storage/sinval.h"
24
#include "storage/fd.h"
25
#include "utils/builtins.h"
27
#include "catalog/pg_type.h"
28
#include "catalog/pg_tablespace.h"
30
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
34
* Check if data is Null
37
nullvalue(PG_FUNCTION_ARGS)
41
PG_RETURN_BOOL(false);
45
* Check if data is not Null
48
nonnullvalue(PG_FUNCTION_ARGS)
51
PG_RETURN_BOOL(false);
57
* Expose the current database to the user
60
current_database(PG_FUNCTION_ARGS)
64
db = (Name) palloc(NAMEDATALEN);
66
namestrcpy(db, get_database_name(MyDatabaseId));
72
* Functions to send signals to other backends.
76
pg_signal_backend(int pid, int sig)
80
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
81
(errmsg("must be superuser to signal other server processes"))));
83
if (!IsBackendPid(pid))
86
* This is just a warning so a loop-through-resultset will not
87
* abort if one backend terminated on it's own during the run
90
(errmsg("PID %d is not a PostgreSQL server process", pid)));
96
/* Again, just a warning to allow loops */
98
(errmsg("could not send signal to process %d: %m", pid)));
105
pg_cancel_backend(PG_FUNCTION_ARGS)
107
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0), SIGINT));
112
/* Disabled in 8.0 due to reliability concerns; FIXME someday */
115
pg_terminate_backend(PG_FUNCTION_ARGS)
117
PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0), SIGTERM));
122
/* Function to find out which databases make use of a tablespace */
131
pg_tablespace_databases(PG_FUNCTION_ARGS)
133
FuncCallContext *funcctx;
137
if (SRF_IS_FIRSTCALL())
139
MemoryContext oldcontext;
140
Oid tablespaceOid = PG_GETARG_OID(0);
142
funcctx = SRF_FIRSTCALL_INIT();
143
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
145
fctx = palloc(sizeof(ts_db_fctx));
148
* size = path length + tablespace dirname length + 2 dir sep
149
* chars + oid + terminator
151
fctx->location = (char *) palloc(strlen(DataDir) + 11 + 10 + 1);
152
if (tablespaceOid == GLOBALTABLESPACE_OID)
154
fctx->dirdesc = NULL;
156
(errmsg("global tablespace never has databases")));
160
if (tablespaceOid == DEFAULTTABLESPACE_OID)
161
sprintf(fctx->location, "%s/base", DataDir);
163
sprintf(fctx->location, "%s/pg_tblspc/%u", DataDir,
166
fctx->dirdesc = AllocateDir(fctx->location);
170
/* the only expected error is ENOENT */
173
(errcode_for_file_access(),
174
errmsg("could not open directory \"%s\": %m",
177
(errmsg("%u is not a tablespace OID", tablespaceOid)));
180
funcctx->user_fctx = fctx;
181
MemoryContextSwitchTo(oldcontext);
184
funcctx = SRF_PERCALL_SETUP();
185
fctx = (ts_db_fctx *) funcctx->user_fctx;
187
if (!fctx->dirdesc) /* not a tablespace */
188
SRF_RETURN_DONE(funcctx);
190
while ((de = readdir(fctx->dirdesc)) != NULL)
195
Oid datOid = atooid(de->d_name);
197
/* this test skips . and .., but is awfully weak */
201
/* if database subdir is empty, don't report tablespace as used */
203
/* size = path length + dir sep char + file name + terminator */
204
subdir = palloc(strlen(fctx->location) + 1 + strlen(de->d_name) + 1);
205
sprintf(subdir, "%s/%s", fctx->location, de->d_name);
206
dirdesc = AllocateDir(subdir);
209
continue; /* XXX more sloppiness */
211
while ((de = readdir(dirdesc)) != 0)
213
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
219
continue; /* indeed, nothing in it */
221
SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
224
FreeDir(fctx->dirdesc);
225
SRF_RETURN_DONE(funcctx);