提交 4483b7d3 编写于 作者: D Daniel Gustafsson 提交者: Jimmy Yih

Remove spclocation field from pg_tablespace

This is a backport of the below commit from upstream 9.3:

  commit 16d8e594
  Author: Magnus Hagander <magnus@hagander.net>
  Date:   Wed Dec 7 10:35:00 2011 +0100

    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.
上级 451934d5
......@@ -277,15 +277,24 @@ get_db_infos(migratorContext *ctx, DbInfoArr *dbinfs_arr, Cluster whichCluster)
int i_datname;
int i_oid;
int i_spclocation;
char query[QUERY_ALLOC];
res = executeQueryOrDie(ctx, conn,
"SELECT d.oid, d.datname, t.spclocation "
"FROM pg_catalog.pg_database d "
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
snprintf(query, sizeof(query),
"SELECT d.oid, d.datname, %s "
"FROM pg_catalog.pg_database d "
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
/* we don't preserve pg_database.oid so we sort by name */
"ORDER BY 2");
"ORDER BY 2",
/*
* 9.2 removed the spclocation column in upstream postgres, in GPDB it was
* removed in 6.0.0 during then merge of postgres 8.4
*/
(GET_MAJOR_VERSION(ctx->old.major_version) <= 803) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
res = executeQueryOrDie(ctx, conn, "%s", query);
i_datname = PQfnumber(res, "datname");
i_oid = PQfnumber(res, "oid");
......@@ -495,7 +504,7 @@ get_rel_infos(migratorContext *ctx, const DbInfo *dbinfo,
snprintf(query, sizeof(query),
"SELECT DISTINCT c.oid, n.nspname, c.relname, c.relstorage, c.relkind, "
" c.relfilenode, c.reltoastrelid, c.reltablespace, t.spclocation "
" c.relfilenode, c.reltoastrelid, c.reltablespace, %s "
"FROM pg_catalog.pg_class c JOIN "
" pg_catalog.pg_namespace n "
" ON c.relnamespace = n.oid "
......@@ -523,10 +532,16 @@ get_rel_infos(migratorContext *ctx, const DbInfo *dbinfo,
* nor indisready. */
" %s "
"GROUP BY c.oid, n.nspname, c.relname, c.relfilenode, c.relstorage, c.relkind, "
" c.reltoastrelid, c.reltablespace, t.spclocation, "
" c.reltoastrelid, c.reltablespace, spclocation, "
" n.nspname "
/* we preserve pg_class.oid so we sort by it to match old/new */
"ORDER BY 1;",
/*
* 9.2 removed the spclocation column in upstream postgres, in GPDB it was
* removed in 6.0.0 during the 8.4 merge
*/
(GET_MAJOR_VERSION(ctx->old.major_version) <= 803) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation",
FirstNormalObjectId,
/* does pg_largeobject_metadata need to be migrated? */
(GET_MAJOR_VERSION(ctx->old.major_version) <= 804) ?
......
......@@ -44,11 +44,21 @@ get_tablespace_paths(migratorContext *ctx)
int tblnum;
int i_spclocation;
res = executeQueryOrDie(ctx, conn,
"SELECT spclocation "
"FROM pg_catalog.pg_tablespace "
"WHERE spcname != 'pg_default' AND "
" spcname != 'pg_global'");
char query[QUERY_ALLOC];
snprintf(query, sizeof(query),
"SELECT %s "
"FROM pg_catalog.pg_tablespace "
"WHERE spcname != 'pg_default' AND "
" spcname != 'pg_global'",
/*
* 9.2 removed the spclocation column in upstream postgres, in GPDB it was
* removed in 6.0.0 during the 8.4 merge
*/
(GET_MAJOR_VERSION(ctx->old.major_version) <= 803) ?
"spclocation" : "pg_catalog.pg_tablespace_location(oid) AS spclocation");
res = executeQueryOrDie(ctx, conn, "%s", query);
if ((ctx->num_tablespaces = PQntuples(res)) != 0)
ctx->tablespaces = (char **) pg_malloc(ctx,
......
......@@ -379,8 +379,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
values[Anum_pg_tablespace_spcowner - 1] =
ObjectIdGetDatum(ownerId);
values[Anum_pg_tablespace_spclocation - 1] =
CStringGetTextDatum(location);
nulls[Anum_pg_tablespace_spcacl - 1] = true;
nulls[Anum_pg_tablespace_spcoptions - 1] = true;
......
......@@ -18,6 +18,7 @@
#include <signal.h>
#include <dirent.h>
#include <math.h>
#include <unistd.h>
#include "access/xact.h"
#include "catalog/catalog.h"
......@@ -362,6 +363,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/<oid>.
*/
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
*/
......
......@@ -1364,7 +1364,7 @@ dumpTablespaces(PGconn *conn)
if (server_version >= 90000)
res = executeQuery(conn, "SELECT spcname, "
"pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
"spclocation, spcacl, "
"pg_catalog.pg_tablespace_location(oid), spcacl, "
"array_to_string(spcoptions, ', '),"
"pg_catalog.shobj_description(oid, 'pg_tablespace') "
"FROM pg_catalog.pg_tablespace "
......
......@@ -267,13 +267,22 @@ describeTablespaces(const char *pattern, bool verbose)
initPQExpBuffer(&buf);
printfPQExpBuffer(&buf,
"SELECT spcname AS \"%s\",\n"
" pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
" spclocation AS \"%s\"",
gettext_noop("Name"),
gettext_noop("Owner"),
gettext_noop("Location"));
if (pset.sversion >= 90200)
printfPQExpBuffer(&buf,
"SELECT spcname AS \"%s\",\n"
" pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
" pg_catalog.pg_tablespace_location(oid) AS \"%s\"",
gettext_noop("Name"),
gettext_noop("Owner"),
gettext_noop("Location"));
else
printfPQExpBuffer(&buf,
"SELECT spcname AS \"%s\",\n"
" pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
" spclocation AS \"%s\"",
gettext_noop("Name"),
gettext_noop("Owner"),
gettext_noop("Location"));
if (verbose)
{
......
......@@ -3174,6 +3174,9 @@ DESCR("statistics: reset collected statistics for a single table or index in the
DATA(insert OID = 3777 ( pg_stat_reset_single_function_counters PGNSP PGUID 12 1 0 0 f f f f f v 1 0 2278 "26" _null_ _null_ _null_ _null_ pg_stat_reset_single_function_counters _null_ _null_ _null_ ));
DESCR("statistics: reset collected statistics for a single function in the current database");
DATA(insert OID = 3778 ( pg_tablespace_location PGNSP PGUID 12 1 0 0 f f f t f s 1 0 25 "26" _null_ _null_ _null_ _null_ pg_tablespace_location _null_ _null_ _null_ ));
DESCR("tablespace location");
DATA(insert OID = 1946 ( encode PGNSP PGUID 12 1 0 0 f f f t f i 2 0 25 "17 25" _null_ _null_ _null_ _null_ binary_encode _null_ _null_ _null_ ));
DESCR("convert bytea value into some ascii-only text string");
DATA(insert OID = 1947 ( decode PGNSP PGUID 12 1 0 0 f f f t f i 2 0 17 "25 25" _null_ _null_ _null_ _null_ binary_decode _null_ _null_ _null_ ));
......
......@@ -32,7 +32,6 @@ CATALOG(pg_tablespace,1213) BKI_SHARED_RELATION
{
NameData spcname; /* tablespace name */
Oid spcowner; /* owner of tablespace */
text spclocation; /* physical location (VAR LENGTH) */
aclitem spcacl[1]; /* access permissions (VAR LENGTH) */
text spcoptions[1]; /* per-tablespace options */
} FormData_pg_tablespace;
......@@ -52,15 +51,14 @@ typedef FormData_pg_tablespace *Form_pg_tablespace;
* ----------------
*/
#define Natts_pg_tablespace 5
#define Natts_pg_tablespace 4
#define Anum_pg_tablespace_spcname 1
#define Anum_pg_tablespace_spcowner 2
#define Anum_pg_tablespace_spclocation 3
#define Anum_pg_tablespace_spcacl 4
#define Anum_pg_tablespace_spcoptions 5
#define Anum_pg_tablespace_spcacl 3
#define Anum_pg_tablespace_spcoptions 4
DATA(insert OID = 1663 ( pg_default PGUID "" _null_ _null_ ));
DATA(insert OID = 1664 ( pg_global PGUID "" _null_ _null_ ));
DATA(insert OID = 1663 ( pg_default PGUID _null_ _null_ ));
DATA(insert OID = 1664 ( pg_global PGUID _null_ _null_ ));
#define DEFAULTTABLESPACE_OID 1663
#define GLOBALTABLESPACE_OID 1664
......
......@@ -481,6 +481,7 @@ extern Datum pg_terminate_backend_msg(PG_FUNCTION_ARGS);
extern Datum gp_cancel_query(PG_FUNCTION_ARGS);
extern Datum pg_reload_conf(PG_FUNCTION_ARGS);
extern Datum pg_tablespace_databases(PG_FUNCTION_ARGS);
extern Datum pg_tablespace_location(PG_FUNCTION_ARGS);
extern Datum pg_rotate_logfile(PG_FUNCTION_ARGS);
extern Datum pg_sleep(PG_FUNCTION_ARGS);
extern Datum pg_get_keywords(PG_FUNCTION_ARGS);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册