misc.c 4.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * misc.c
4
 *
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
J
Joe Conway 已提交
11
 *	  $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.35 2004/07/02 18:59:22 joe Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16
#include "postgres.h"

17
#include <sys/file.h>
18
#include <signal.h>
J
Joe Conway 已提交
19
#include <dirent.h>
20

T
Tom Lane 已提交
21
#include "commands/dbcommands.h"
B
 
Bruce Momjian 已提交
22
#include "miscadmin.h"
23
#include "storage/sinval.h"
J
Joe Conway 已提交
24
#include "storage/fd.h"
25
#include "utils/builtins.h"
J
Joe Conway 已提交
26 27 28
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "catalog/pg_tablespace.h"
29

30 31

/*
32
 * Check if data is Null
33
 */
34 35
Datum
nullvalue(PG_FUNCTION_ARGS)
36
{
37 38 39
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(true);
	PG_RETURN_BOOL(false);
40 41
}

42 43 44 45 46
/*
 * Check if data is not Null
 */
Datum
nonnullvalue(PG_FUNCTION_ARGS)
47
{
48 49 50
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(false);
	PG_RETURN_BOOL(true);
51 52
}

B
 
Bruce Momjian 已提交
53 54 55 56 57 58 59
/*
 * current_database()
 *	Expose the current database to the user
 */
Datum
current_database(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
60
	Name		db;
B
 
Bruce Momjian 已提交
61 62 63

	db = (Name) palloc(NAMEDATALEN);

64
	namestrcpy(db, get_database_name(MyDatabaseId));
B
 
Bruce Momjian 已提交
65 66
	PG_RETURN_NAME(db);
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


/*
 * 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));
}
J
Joe Conway 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204


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);
}