|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.34 2004/06/02 21:29:29 momjian Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.35 2004/07/02 18:59:22 joe Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
15 | 15 | #include "postgres.h"
|
16 | 16 |
|
17 | 17 | #include <sys/file.h>
|
18 | 18 | #include <signal.h>
|
| 19 | +#include <dirent.h> |
19 | 20 |
|
20 | 21 | #include "commands/dbcommands.h"
|
21 | 22 | #include "miscadmin.h"
|
22 | 23 | #include "storage/sinval.h"
|
| 24 | +#include "storage/fd.h" |
23 | 25 | #include "utils/builtins.h"
|
| 26 | +#include "funcapi.h" |
| 27 | +#include "catalog/pg_type.h" |
| 28 | +#include "catalog/pg_tablespace.h" |
24 | 29 |
|
25 | 30 |
|
26 | 31 | /*
|
@@ -103,3 +108,97 @@ pg_cancel_backend(PG_FUNCTION_ARGS)
|
103 | 108 | {
|
104 | 109 | PG_RETURN_INT32(pg_signal_backend(PG_GETARG_INT32(0),SIGINT));
|
105 | 110 | }
|
| 111 | + |
| 112 | + |
| 113 | +typedef struct |
| 114 | +{ |
| 115 | + char *location; |
| 116 | + DIR *dirdesc; |
| 117 | +} ts_db_fctx; |
| 118 | + |
| 119 | +Datum pg_tablespace_databases(PG_FUNCTION_ARGS) |
| 120 | +{ |
| 121 | + FuncCallContext *funcctx; |
| 122 | + struct dirent *de; |
| 123 | + ts_db_fctx *fctx; |
| 124 | + |
| 125 | + if (SRF_IS_FIRSTCALL()) |
| 126 | + { |
| 127 | + MemoryContext oldcontext; |
| 128 | + Oid tablespaceOid=PG_GETARG_OID(0); |
| 129 | + |
| 130 | + funcctx=SRF_FIRSTCALL_INIT(); |
| 131 | + oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
| 132 | + |
| 133 | + fctx = palloc(sizeof(ts_db_fctx)); |
| 134 | + |
| 135 | + /* |
| 136 | + * size = path length + tablespace dirname length |
| 137 | + * + 2 dir sep chars + oid + terminator |
| 138 | + */ |
| 139 | + fctx->location = (char*) palloc(strlen(DataDir) + 11 + 10 + 1); |
| 140 | + if (tablespaceOid == GLOBALTABLESPACE_OID) |
| 141 | + { |
| 142 | + fctx->dirdesc = NULL; |
| 143 | + ereport(NOTICE, |
| 144 | + (errcode(ERRCODE_WARNING), |
| 145 | + errmsg("global tablespace never has databases."))); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + if (tablespaceOid == DEFAULTTABLESPACE_OID) |
| 150 | + sprintf(fctx->location, "%s/base", DataDir); |
| 151 | + else |
| 152 | + sprintf(fctx->location, "%s/pg_tblspc/%u", DataDir, |
| 153 | + tablespaceOid); |
| 154 | + |
| 155 | + fctx->dirdesc = AllocateDir(fctx->location); |
| 156 | + |
| 157 | + if (!fctx->dirdesc) /* not a tablespace */ |
| 158 | + ereport(NOTICE, |
| 159 | + (errcode(ERRCODE_WARNING), |
| 160 | + errmsg("%d is no tablespace oid.", tablespaceOid))); |
| 161 | + } |
| 162 | + funcctx->user_fctx = fctx; |
| 163 | + MemoryContextSwitchTo(oldcontext); |
| 164 | + } |
| 165 | + |
| 166 | + funcctx=SRF_PERCALL_SETUP(); |
| 167 | + fctx = (ts_db_fctx*) funcctx->user_fctx; |
| 168 | + |
| 169 | + if (!fctx->dirdesc) /* not a tablespace */ |
| 170 | + SRF_RETURN_DONE(funcctx); |
| 171 | + |
| 172 | + while ((de = readdir(fctx->dirdesc)) != NULL) |
| 173 | + { |
| 174 | + char *subdir; |
| 175 | + DIR *dirdesc; |
| 176 | + |
| 177 | + Oid datOid = atol(de->d_name); |
| 178 | + if (!datOid) |
| 179 | + continue; |
| 180 | + |
| 181 | + /* size = path length + dir sep char + file name + terminator */ |
| 182 | + subdir = palloc(strlen(fctx->location) + 1 + strlen(de->d_name) + 1); |
| 183 | + sprintf(subdir, "%s/%s", fctx->location, de->d_name); |
| 184 | + dirdesc = AllocateDir(subdir); |
| 185 | + if (dirdesc) |
| 186 | + { |
| 187 | + while ((de = readdir(dirdesc)) != 0) |
| 188 | + { |
| 189 | + if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) |
| 190 | + break; |
| 191 | + } |
| 192 | + pfree(subdir); |
| 193 | + FreeDir(dirdesc); |
| 194 | + |
| 195 | + if (!de) /* database subdir is empty; don't report tablespace as used */ |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid)); |
| 200 | + } |
| 201 | + |
| 202 | + FreeDir(fctx->dirdesc); |
| 203 | + SRF_RETURN_DONE(funcctx); |
| 204 | +} |
0 commit comments