]> granicus.if.org Git - postgresql/commitdiff
Add Pgadmin administration functions to /contrib/adminpack.
authorBruce Momjian <bruce@momjian.us>
Tue, 30 May 2006 12:07:31 +0000 (12:07 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 30 May 2006 12:07:31 +0000 (12:07 +0000)
Dave Page

contrib/README
contrib/adminpack/Makefile [new file with mode: 0644]
contrib/adminpack/README.adminpack [new file with mode: 0644]
contrib/adminpack/adminpack.c [new file with mode: 0644]
contrib/adminpack/adminpack.sql.in [new file with mode: 0644]

index d18a86f0a81bed29f97ccdf8c261bd08582fd998..ba7ebe158c6720bf72e17563ab06e2eba3fbd4e5 100644 (file)
@@ -28,9 +28,10 @@ adddepend -
        Add object dependency information to pre-7.3 objects.
        by Rod Taylor <rbt@rbt.ca>
 
-btree_gist -
-       Support for emulating BTREE indexing in GiST
-       by Oleg Bartunov <oleg@sai.msu.su> and Teodor Sigaev <teodor@sigaev.ru>
+adminpack -
+       File and log manipulation routines, used by Pgadmin
+       by From: Dave Page <dpage@vale-housing.co.uk>
+
 
 chkpass -
        An auto-encrypted password datatype
diff --git a/contrib/adminpack/Makefile b/contrib/adminpack/Makefile
new file mode 100644 (file)
index 0000000..808d0be
--- /dev/null
@@ -0,0 +1,15 @@
+MODULE_big = adminpack\r
+PG_CPPFLAGS = -I$(libpq_srcdir)\r
+DATA_built = adminpack.sql\r
+DOCS = README.adminpack\r
+OBJS = adminpack.o\r
+\r
+ifdef USE_PGXS\r
+PGXS := $(shell pg_config --pgxs)\r
+include $(PGXS)\r
+else\r
+subdir = contrib/adminpack\r
+top_builddir = ../..\r
+include $(top_builddir)/src/Makefile.global\r
+include $(top_srcdir)/contrib/contrib-global.mk\r
+endif\r
diff --git a/contrib/adminpack/README.adminpack b/contrib/adminpack/README.adminpack
new file mode 100644 (file)
index 0000000..c786f51
--- /dev/null
@@ -0,0 +1,47 @@
+PostgreSQL Administration Functions\r
+===================================\r
+\r
+This directory is a PostgreSQL 'contrib' module which implements a number of\r
+support functions which pgAdmin and other administration and management tools\r
+can use to provide additional functionality if installed on a server. \r
+\r
+Installation\r
+============\r
+\r
+This module is normally distributed as a PostgreSQL 'contrib' module. To\r
+install it from a pre-configured source tree run the following commands\r
+as a user with appropriate privileges from the adminpack source directory:\r
+\r
+make\r
+make install\r
+\r
+Alternatively, if you have a PostgreSQL 8.2 or higher installation but no \r
+source tree you can install using PGXS. Simply run the following commands the\r
+adminpack source directory:\r
\r
+make USE_PGXS=1 \r
+make USE_PGXS=1 install \r
+\r
+pgAdmin will look for the functions in the Maintenance Database (usually \r
+"postgres" for 8.2 servers) specified in the connection dialogue for the server. \r
+To install the functions in the database, either run the adminpack.sql script \r
+using the pgAdmin SQL tool (and then close and reopen the connection to the \r
+freshly instrumented server), or run the script using psql, eg:\r
+\r
+psql -U postgres postgres < adminpack.sql\r
+\r
+Other administration tools that use this module may have different requirements,\r
+please consult the tool's documentation for further details.\r
+\r
+Objects implemented (superuser only)\r
+====================================\r
+\r
+int8 pg_catalog.pg_file_write(fname text, data text, append bool)\r
+int8 pg_catalog.pg_file_read(fname text, data text, append bool)\r
+bool pg_catalog.pg_file_rename(oldname text, newname text)\r
+bool pg_catalog.pg_file_rename(oldname text, newname text, archivname text)\r
+bool pg_catalog.pg_file_unlink(fname text)\r
+bigint pg_catalog.pg_file_size(text)\r
+int4 pg_catalog.pg_logfile_rotate()\r
+setof record pg_catalog.pg_logdir_ls()\r
+\r
diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c
new file mode 100644 (file)
index 0000000..683a8e9
--- /dev/null
@@ -0,0 +1,390 @@
+/*-------------------------------------------------------------------------\r
+ *\r
+ * admin81.c\r
+ *\r
+ *\r
+ * Copyright (c) 2002 - 2006, PostgreSQL Global Development Group\r
+ * \r
+ * Author: Andreas Pflug <pgadmin@pse-consulting.de>\r
+ *\r
+ * IDENTIFICATION\r
+ *       $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.1 2006/05/30 12:07:31 momjian Exp $\r
+ *\r
+ *-------------------------------------------------------------------------\r
+ */\r
+#include "postgres.h"\r
+\r
+#include <sys/file.h>\r
+#include <sys/stat.h>\r
+#include <unistd.h>\r
+#include <dirent.h>\r
+\r
+#include "miscadmin.h"\r
+#include "storage/fd.h"\r
+#include "catalog/pg_type.h"\r
+#include "funcapi.h"\r
+#include "utils/datetime.h"\r
+\r
+\r
+#ifdef WIN32\r
+\r
+#ifdef rename\r
+#undef rename\r
+#endif\r
+\r
+#ifdef unlink\r
+#undef unlink\r
+#endif\r
+\r
+#endif\r
+\r
+extern DLLIMPORT char *DataDir;\r
+extern DLLIMPORT char *Log_directory;\r
+extern DLLIMPORT char *Log_filename;\r
+\r
+Datum pg_file_write(PG_FUNCTION_ARGS);\r
+Datum pg_file_rename(PG_FUNCTION_ARGS);\r
+Datum pg_file_unlink(PG_FUNCTION_ARGS);\r
+Datum pg_logdir_ls(PG_FUNCTION_ARGS);\r
+\r
+PG_FUNCTION_INFO_V1(pg_file_write);\r
+PG_FUNCTION_INFO_V1(pg_file_rename);\r
+PG_FUNCTION_INFO_V1(pg_file_unlink);\r
+PG_FUNCTION_INFO_V1(pg_logdir_ls);\r
+\r
+typedef struct \r
+{\r
+       char *location;\r
+       DIR *dirdesc;\r
+} directory_fctx;\r
+\r
+/*-----------------------\r
+ * some helper functions\r
+ */\r
+\r
+/*\r
+ * Return an absolute path. Argument may be absolute or \r
+ * relative to the DataDir.\r
+ */\r
+static char *absClusterPath(text *arg, bool logAllowed)\r
+{\r
+       char *filename;\r
+       int len=VARSIZE(arg) - VARHDRSZ;\r
+       int dlen = strlen(DataDir);\r
+\r
+       filename = palloc(len+1);\r
+       memcpy(filename, VARDATA(arg), len);\r
+       filename[len] = 0;\r
+\r
+       if (strstr(filename, "..") != NULL)\r
+         ereport(ERROR,\r
+                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+                          (errmsg("No .. allowed in filenames"))));\r
+       \r
+       if (is_absolute_path(filename))\r
+       {\r
+           if (logAllowed && !strncmp(filename, Log_directory, strlen(Log_directory)))\r
+                   return filename;\r
+               if (strncmp(filename, DataDir, dlen))\r
+                   ereport(ERROR,\r
+                                       (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+                                        (errmsg("Absolute path not allowed"))));\r
+\r
+               return filename;\r
+       }\r
+       else\r
+       {\r
+           char *absname = palloc(dlen+len+2);\r
+               sprintf(absname, "%s/%s", DataDir, filename);\r
+               pfree(filename);\r
+               return absname;\r
+       }\r
+}\r
+\r
+\r
+/*\r
+ * check for superuser, bark if not.\r
+ */\r
+static void\r
+requireSuperuser(void)\r
+{\r
+       if (!superuser())\r
+           ereport(ERROR,\r
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+                                (errmsg("only superuser may access generic file functions"))));\r
+}\r
+\r
+\r
+\r
+/* ------------------------------------\r
+ * generic file handling functions\r
+ */\r
+\r
+Datum pg_file_write(PG_FUNCTION_ARGS)\r
+{\r
+       FILE *f;\r
+       char *filename;\r
+       text *data;\r
+       int64 count = 0;\r
+\r
+       requireSuperuser();\r
+\r
+       filename = absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+       data = PG_GETARG_TEXT_P(1);\r
+\r
+       if (PG_ARGISNULL(2) || !PG_GETARG_BOOL(2))\r
+       {\r
+           struct stat fst;\r
+               if (stat(filename, &fst) >= 0)\r
+                   ereport(ERROR,\r
+                                       (ERRCODE_DUPLICATE_FILE,\r
+                                        errmsg("file %s exists", filename)));\r
+\r
+           f = fopen(filename, "wb");\r
+       }\r
+       else\r
+           f = fopen(filename, "ab");\r
+\r
+       if (!f)\r
+       {\r
+               ereport(ERROR,\r
+                               (errcode_for_file_access(),\r
+                                errmsg("could open file %s for writing: %m", filename)));\r
+       }\r
+\r
+       if (VARSIZE(data) != 0)\r
+       {\r
+               count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);\r
+\r
+               if (count != VARSIZE(data) - VARHDRSZ)\r
+                   ereport(ERROR,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("error writing file %s: %m", filename)));\r
+       }\r
+       fclose(f);\r
+\r
+       PG_RETURN_INT64(count);\r
+}\r
+\r
+\r
+Datum pg_file_rename(PG_FUNCTION_ARGS)\r
+{\r
+    char *fn1, *fn2, *fn3;\r
+       int rc;\r
+\r
+       requireSuperuser();\r
+\r
+       if (PG_ARGISNULL(0) || PG_ARGISNULL(1))\r
+               PG_RETURN_NULL();\r
+\r
+       fn1=absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+       fn2=absClusterPath(PG_GETARG_TEXT_P(1), false);\r
+       if (PG_ARGISNULL(2))\r
+           fn3=0;\r
+       else\r
+           fn3=absClusterPath(PG_GETARG_TEXT_P(2), false);\r
+\r
+       if (access(fn1, W_OK) < 0)\r
+       {\r
+               ereport(WARNING,\r
+                               (errcode_for_file_access(),\r
+                                errmsg("file %s not accessible: %m", fn1)));\r
+\r
+           PG_RETURN_BOOL(false);\r
+       }\r
+\r
+       if (fn3 && access(fn2, W_OK) < 0)\r
+       {\r
+               ereport(WARNING,\r
+                               (errcode_for_file_access(),\r
+                                errmsg("file %s not accessible: %m", fn2)));\r
+\r
+           PG_RETURN_BOOL(false);\r
+       }\r
+\r
+\r
+       rc = access(fn3 ? fn3 : fn2, 2);\r
+       if (rc >= 0 || errno != ENOENT)\r
+       {\r
+               ereport(ERROR,\r
+                               (ERRCODE_DUPLICATE_FILE,\r
+                                errmsg("cannot rename to target file %s", fn3 ? fn3 : fn2)));\r
+       }\r
+       \r
+       if (fn3)\r
+       {\r
+           if (rename(fn2, fn3) != 0)\r
+               {\r
+                       ereport(ERROR,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("could not rename %s to %s: %m", fn2, fn3)));\r
+               }\r
+               if (rename(fn1, fn2) != 0)\r
+               {\r
+                       ereport(WARNING,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("could not rename %s to %s: %m", fn1, fn2)));\r
+\r
+                       if (rename(fn3, fn2) != 0)\r
+                       {\r
+                               ereport(ERROR,\r
+                                               (errcode_for_file_access(),\r
+                                                errmsg("could not rename %s back to %s: %m", fn3, fn2)));\r
+                       }\r
+                       else\r
+                       {\r
+                               ereport(ERROR,\r
+                                               (ERRCODE_UNDEFINED_FILE,\r
+                                                errmsg("renaming %s to %s was reverted", fn2, fn3)));\r
+\r
+                       }\r
+               }\r
+       }\r
+       else if (rename(fn1, fn2) != 0)\r
+       {\r
+                       ereport(WARNING,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("renaming %s to %s %m", fn1, fn2)));\r
+               ereport(ERROR,\r
+                               (errcode_for_file_access(),\r
+                                errmsg("could not rename %s to %s: %m", fn1, fn2)));\r
+       }\r
+\r
+       PG_RETURN_BOOL(true);\r
+}\r
+\r
+\r
+Datum pg_file_unlink(PG_FUNCTION_ARGS)\r
+{\r
+    char *filename;\r
+\r
+       requireSuperuser();\r
+\r
+    filename = absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+\r
+       if (access(filename, W_OK) < 0)\r
+       {\r
+           if (errno == ENOENT)\r
+                   PG_RETURN_BOOL(false);\r
+               else\r
+                   ereport(ERROR,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("file %s not accessible: %m", filename)));\r
+\r
+       }\r
+\r
+       if (unlink(filename) < 0)\r
+       {\r
+               ereport(WARNING,\r
+                               (errcode_for_file_access(),\r
+                                errmsg("could not unlink file %s: %m", filename)));\r
+\r
+               PG_RETURN_BOOL(false);\r
+       }\r
+       PG_RETURN_BOOL(true);\r
+}\r
+\r
+\r
+Datum pg_logdir_ls(PG_FUNCTION_ARGS)\r
+{\r
+       FuncCallContext *funcctx;\r
+       struct dirent *de;\r
+       directory_fctx *fctx;\r
+\r
+       if (!superuser()) \r
+               ereport(ERROR,\r
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+                                (errmsg("only superuser can list the log directory"))));\r
+       \r
+       if (memcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log", 30) != 0)\r
+               ereport(ERROR,\r
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),\r
+                                (errmsg("the log_filename parameter must equal 'postgresql-%%Y-%%m-%%d_%%H%%M%%S.log'"))));\r
+\r
+       if (SRF_IS_FIRSTCALL())\r
+       {\r
+               MemoryContext oldcontext;\r
+               TupleDesc tupdesc;\r
+\r
+               funcctx=SRF_FIRSTCALL_INIT();\r
+               oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);\r
+\r
+               fctx = palloc(sizeof(directory_fctx));\r
+               if (is_absolute_path(Log_directory))\r
+                   fctx->location = Log_directory;\r
+               else\r
+               {\r
+                       fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) +2);\r
+                       sprintf(fctx->location, "%s/%s", DataDir, Log_directory);\r
+               }\r
+               tupdesc = CreateTemplateTupleDesc(2, false);\r
+               TupleDescInitEntry(tupdesc, (AttrNumber) 1, "starttime",\r
+                                                  TIMESTAMPOID, -1, 0);\r
+               TupleDescInitEntry(tupdesc, (AttrNumber) 2, "filename",\r
+                                                  TEXTOID, -1, 0);\r
+\r
+               funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);\r
+               \r
+               fctx->dirdesc = AllocateDir(fctx->location);\r
+\r
+               if (!fctx->dirdesc)\r
+                   ereport(ERROR,\r
+                                       (errcode_for_file_access(),\r
+                                        errmsg("%s is not browsable: %m", fctx->location)));\r
+\r
+               funcctx->user_fctx = fctx;\r
+               MemoryContextSwitchTo(oldcontext);\r
+       }\r
+\r
+       funcctx=SRF_PERCALL_SETUP();\r
+       fctx = (directory_fctx*) funcctx->user_fctx;\r
+\r
+       if (!fctx->dirdesc)  /* not a readable directory  */\r
+               SRF_RETURN_DONE(funcctx);\r
+\r
+       while ((de = readdir(fctx->dirdesc)) != NULL)\r
+       {\r
+               char *values[2];\r
+               HeapTuple tuple;\r
+            \r
+               char            *field[MAXDATEFIELDS];\r
+               char            lowstr[MAXDATELEN + 1];\r
+               int             dtype;\r
+               int             nf, ftype[MAXDATEFIELDS];\r
+               fsec_t          fsec;\r
+               int             tz = 0;\r
+               struct          pg_tm date;\r
+\r
+               /*\r
+                * Default format:\r
+                *        postgresql-YYYY-MM-DD_HHMMSS.log\r
+                */\r
+               if (strlen(de->d_name) != 32\r
+                   || memcmp(de->d_name, "postgresql-", 11)\r
+                       || de->d_name[21] != '_'\r
+                       || strcmp(de->d_name + 28, ".log"))\r
+                     continue;\r
+\r
+               values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);\r
+               sprintf(values[1], "%s/%s", fctx->location, de->d_name);\r
+\r
+               values[0] = de->d_name + 11;       /* timestamp */\r
+               values[0][17] = 0;\r
+\r
+                    /* parse and decode expected timestamp */\r
+               if (ParseDateTime(values[0], lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))\r
+                   continue;\r
+\r
+               if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))\r
+                   continue;\r
+\r
+               /* Seems the format fits the expected format; feed it into the tuple */\r
+\r
+               tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);\r
+\r
+               SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));\r
+       }\r
+\r
+       FreeDir(fctx->dirdesc);\r
+       SRF_RETURN_DONE(funcctx);\r
+}\r
diff --git a/contrib/adminpack/adminpack.sql.in b/contrib/adminpack/adminpack.sql.in
new file mode 100644 (file)
index 0000000..f1b2bd9
--- /dev/null
@@ -0,0 +1,41 @@
+/* ***********************************************\r
+ * Administrative functions for PostgreSQL \r
+ * *********************************************** */\r
+\r
+/* generic file access functions */\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_write(text, text, bool) RETURNS bigint\r
+   AS 'MODULE_PATHNAME', 'pg_file_write'\r
+       LANGUAGE C VOLATILE STRICT;\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_rename(text, text, text) RETURNS bool\r
+   AS 'MODULE_PATHNAME', 'pg_file_rename'\r
+       LANGUAGE C VOLATILE;\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_unlink(text) RETURNS bool\r
+   AS 'MODULE_PATHNAME', 'pg_file_unlink'\r
+       LANGUAGE C VOLATILE STRICT;\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_rename(text, text) RETURNS bool\r
+   AS 'SELECT pg_file_rename($1, $2, NULL); '\r
+       LANGUAGE SQL VOLATILE STRICT;\r
+\r
+CREATE FUNCTION pg_catalog.pg_logdir_ls() RETURNS setof record\r
+   AS 'MODULE_PATHNAME', 'pg_logdir_ls'\r
+       LANGUAGE C VOLATILE STRICT;\r
+\r
+\r
+/* compatibility redefines */\r
+\r
+CREATE FUNCTION pg_catalog.pg_logfile_rotate() RETURNS int4\r
+  AS 'pg_rotate_logfile'\r
+   LANGUAGE INTERNAL VOLATILE STRICT;\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_read(text, bigint, bigint) RETURNS text\r
+   AS 'pg_read_file'\r
+       LANGUAGE INTERNAL VOLATILE STRICT;\r
+\r
+CREATE FUNCTION pg_catalog.pg_file_length(text) RETURNS bigint\r
+   AS 'SELECT size FROM pg_stat_file($1)'\r
+    LANGUAGE SQL VOLATILE STRICT;\r
+\r