From 16d8e594acd96661267cb7897834f9cba51a2ffd Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Wed, 7 Dec 2011 10:35:00 +0100 Subject: Remove spclocation field from pg_tablespace Instead, add a function pg_tablespace_location(oid) used to return the same information, and do this by reading the symbolic link. Doing it this way makes it possible to relocate a tablespace when the database is down by simply changing the symbolic link. --- src/backend/utils/adt/misc.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/backend/utils/adt/misc.c') diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index 63ec6fd9d4e..4453e818c00 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "catalog/catalog.h" #include "catalog/pg_tablespace.h" @@ -261,6 +262,44 @@ pg_tablespace_databases(PG_FUNCTION_ARGS) } +/* + * pg_tablespace_location - get location for a tablespace + */ +Datum +pg_tablespace_location(PG_FUNCTION_ARGS) +{ + Oid tablespaceOid = PG_GETARG_OID(0); + char sourcepath[MAXPGPATH]; + char targetpath[MAXPGPATH]; + int rllen; + + /* + * Return empty string for our two default tablespace + */ + if (tablespaceOid == DEFAULTTABLESPACE_OID || + tablespaceOid == GLOBALTABLESPACE_OID) + PG_RETURN_TEXT_P(cstring_to_text("")); + +#if defined(HAVE_READLINK) || defined(WIN32) + /* + * Find the location of the tablespace by reading the symbolic link that is + * in pg_tblspc/. + */ + snprintf(sourcepath, sizeof(sourcepath), "pg_tblspc/%u", tablespaceOid); + rllen =readlink(sourcepath, targetpath, sizeof(targetpath)); + if (rllen < 0 || rllen >= sizeof(targetpath)) + ereport(ERROR, + (errmsg("could not read symbolic link \"%s\": %m", sourcepath))); + targetpath[rllen] = '\0'; + + PG_RETURN_TEXT_P(cstring_to_text(targetpath)); +#else + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("tablespaces are not supported on this platform"))); +#endif +} + /* * pg_sleep - delay for N seconds */ -- cgit v1.2.3