Skip to content

Commit 04baa0e

Browse files
committed
Update pg_dump to use SET DEFAULT_TABLESPACE instead of explicit
tablespace clauses; this should improve compatibility of dump files. Philip Warner, some rework by Tom Lane.
1 parent 1d13bed commit 04baa0e

File tree

5 files changed

+164
-46
lines changed

5 files changed

+164
-46
lines changed

src/bin/pg_dump/pg_backup.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.33 2004/08/29 05:06:53 momjian Exp $
18+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.34 2004/11/06 19:36:01 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -54,8 +54,10 @@ typedef enum _archiveFormat
5454
typedef struct _Archive
5555
{
5656
int verbose;
57-
int remoteVersion;
58-
int minRemoteVersion;
57+
char *remoteVersionStr; /* server's version string */
58+
int remoteVersion; /* same in numeric form */
59+
60+
int minRemoteVersion; /* allowable range */
5961
int maxRemoteVersion;
6062

6163
/* error handling */
@@ -139,7 +141,8 @@ PGconn *ConnectDatabase(Archive *AH,
139141
extern void ArchiveEntry(Archive *AHX,
140142
CatalogId catalogId, DumpId dumpId,
141143
const char *tag,
142-
const char *namespace, const char *owner, bool withOids,
144+
const char *namespace, const char *tablespace,
145+
const char *owner, bool withOids,
143146
const char *desc, const char *defn,
144147
const char *dropStmt, const char *copyStmt,
145148
const DumpId *deps, int nDeps,

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.99 2004/10/22 16:04:35 petere Exp $
18+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.100 2004/11/06 19:36:01 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -60,6 +60,7 @@ static void _reconnectToDB(ArchiveHandle *AH, const char *dbname);
6060
static void _becomeUser(ArchiveHandle *AH, const char *user);
6161
static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
6262
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
63+
static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
6364

6465
static teReqs _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool acl_pass);
6566
static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
@@ -602,7 +603,9 @@ void
602603
ArchiveEntry(Archive *AHX,
603604
CatalogId catalogId, DumpId dumpId,
604605
const char *tag,
605-
const char *namespace, const char *owner, bool withOids,
606+
const char *namespace,
607+
const char *tablespace,
608+
const char *owner, bool withOids,
606609
const char *desc, const char *defn,
607610
const char *dropStmt, const char *copyStmt,
608611
const DumpId *deps, int nDeps,
@@ -629,6 +632,7 @@ ArchiveEntry(Archive *AHX,
629632

630633
newToc->tag = strdup(tag);
631634
newToc->namespace = namespace ? strdup(namespace) : NULL;
635+
newToc->tablespace = tablespace ? strdup(tablespace) : NULL;
632636
newToc->owner = strdup(owner);
633637
newToc->withOids = withOids;
634638
newToc->desc = strdup(desc);
@@ -693,6 +697,12 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
693697
ahprintf(AH, "; Format: %s\n", fmtName);
694698
ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
695699
ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
700+
if (AH->archiveRemoteVersion)
701+
ahprintf(AH, "; Dumped from database version: %s\n",
702+
AH->archiveRemoteVersion);
703+
if (AH->archiveDumpVersion)
704+
ahprintf(AH, "; Dumped by pg_dump version: %s\n",
705+
AH->archiveDumpVersion);
696706

697707
ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
698708

@@ -1822,6 +1832,7 @@ WriteToc(ArchiveHandle *AH)
18221832
WriteStr(AH, te->dropStmt);
18231833
WriteStr(AH, te->copyStmt);
18241834
WriteStr(AH, te->namespace);
1835+
WriteStr(AH, te->tablespace);
18251836
WriteStr(AH, te->owner);
18261837
WriteStr(AH, te->withOids ? "true" : "false");
18271838

@@ -1891,6 +1902,9 @@ ReadToc(ArchiveHandle *AH)
18911902
if (AH->version >= K_VERS_1_6)
18921903
te->namespace = ReadStr(AH);
18931904

1905+
if (AH->version >= K_VERS_1_10)
1906+
te->tablespace = ReadStr(AH);
1907+
18941908
te->owner = ReadStr(AH);
18951909
if (AH->version >= K_VERS_1_9)
18961910
{
@@ -2293,6 +2307,61 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
22932307
destroyPQExpBuffer(qry);
22942308
}
22952309

2310+
/*
2311+
* Issue the commands to select the specified tablespace as the current one
2312+
* in the target database.
2313+
*/
2314+
static void
2315+
_selectTablespace(ArchiveHandle *AH, const char *tablespace)
2316+
{
2317+
PQExpBuffer qry;
2318+
const char *want, *have;
2319+
2320+
have = AH->currTablespace;
2321+
want = tablespace;
2322+
2323+
/* no need to do anything for non-tablespace object */
2324+
if (!want)
2325+
return;
2326+
2327+
if (have && strcmp(want, have) == 0)
2328+
return; /* no need to do anything */
2329+
2330+
qry = createPQExpBuffer();
2331+
2332+
if (strcmp(want, "") == 0)
2333+
{
2334+
/* We want the tablespace to be the database's default */
2335+
appendPQExpBuffer(qry, "SET default_tablespace = ''");
2336+
}
2337+
else
2338+
{
2339+
/* We want an explicit tablespace */
2340+
appendPQExpBuffer(qry, "SET default_tablespace = %s", fmtId(want));
2341+
}
2342+
2343+
if (RestoringToDB(AH))
2344+
{
2345+
PGresult *res;
2346+
2347+
res = PQexec(AH->connection, qry->data);
2348+
2349+
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2350+
warn_or_die_horribly(AH, modulename,
2351+
"could not set default_tablespace to %s: %s",
2352+
fmtId(want), PQerrorMessage(AH->connection));
2353+
2354+
PQclear(res);
2355+
}
2356+
else
2357+
ahprintf(AH, "%s;\n\n", qry->data);
2358+
2359+
if (AH->currTablespace)
2360+
free(AH->currTablespace);
2361+
AH->currTablespace = strdup(want);
2362+
2363+
destroyPQExpBuffer(qry);
2364+
}
22962365

22972366
/**
22982367
* Parses the dropStmt part of a TOC entry and returns
@@ -2378,9 +2447,10 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
23782447
strcmp(te->desc, "SCHEMA") == 0 && strcmp(te->tag, "public") == 0)
23792448
return;
23802449

2381-
/* Select owner and schema as necessary */
2450+
/* Select owner, schema, and tablespace as necessary */
23822451
_becomeOwner(AH, te);
23832452
_selectOutputSchema(AH, te->namespace);
2453+
_selectTablespace(AH, te->tablespace);
23842454

23852455
/* Set up OID mode too */
23862456
if (strcmp(te->desc, "TABLE") == 0)
@@ -2411,10 +2481,14 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
24112481
ahprintf(AH, "\n");
24122482
}
24132483
}
2414-
ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s\n",
2484+
ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s",
24152485
pfx, te->tag, te->desc,
24162486
te->namespace ? te->namespace : "-",
24172487
te->owner);
2488+
if (te->tablespace)
2489+
ahprintf(AH, "; Tablespace: %s", te->tablespace);
2490+
ahprintf(AH, "\n");
2491+
24182492
if (AH->PrintExtraTocPtr != NULL)
24192493
(*AH->PrintExtraTocPtr) (AH, te);
24202494
ahprintf(AH, "--\n\n");
@@ -2509,6 +2583,8 @@ WriteHead(ArchiveHandle *AH)
25092583
WriteInt(AH, crtm.tm_year);
25102584
WriteInt(AH, crtm.tm_isdst);
25112585
WriteStr(AH, PQdb(AH->connection));
2586+
WriteStr(AH, AH->public.remoteVersionStr);
2587+
WriteStr(AH, PG_VERSION);
25122588
}
25132589

25142590
void
@@ -2595,6 +2671,12 @@ ReadHead(ArchiveHandle *AH)
25952671
write_msg(modulename, "WARNING: invalid creation date in header\n");
25962672
}
25972673

2674+
if (AH->version >= K_VERS_1_10)
2675+
{
2676+
AH->archiveRemoteVersion = ReadStr(AH);
2677+
AH->archiveDumpVersion = ReadStr(AH);
2678+
}
2679+
25982680
}
25992681

26002682

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*
1919
* IDENTIFICATION
20-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.61 2004/08/29 05:06:53 momjian Exp $
20+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.62 2004/11/06 19:36:01 tgl Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -62,7 +62,7 @@ typedef z_stream *z_streamp;
6262
#endif
6363

6464
#define K_VERS_MAJOR 1
65-
#define K_VERS_MINOR 9
65+
#define K_VERS_MINOR 10
6666
#define K_VERS_REV 0
6767

6868
/* Data block types */
@@ -84,8 +84,9 @@ typedef z_stream *z_streamp;
8484
* dependencies */
8585
#define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0) /* add default_with_oids
8686
* tracking */
87+
#define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0) /* add tablespace */
8788

88-
#define K_VERS_MAX (( (1 * 256 + 9) * 256 + 255) * 256 + 0)
89+
#define K_VERS_MAX (( (1 * 256 + 10) * 256 + 255) * 256 + 0)
8990

9091
/* No of BLOBs to restore in 1 TX */
9192
#define BLOB_BATCH_SIZE 100
@@ -171,6 +172,11 @@ typedef struct _archiveHandle
171172
char vrev;
172173
int version; /* Conveniently formatted version */
173174

175+
char *archiveRemoteVersion; /* When reading an archive,
176+
* the version of the dumped DB */
177+
char *archiveDumpVersion; /* When reading an archive,
178+
* the version of the dumper */
179+
174180
int debugLevel; /* Used for logging (currently only by
175181
* --verbose) */
176182
size_t intSize; /* Size of an integer in the archive */
@@ -260,6 +266,7 @@ typedef struct _archiveHandle
260266
/* these vars track state to avoid sending redundant SET commands */
261267
char *currUser; /* current username */
262268
char *currSchema; /* current schema */
269+
char *currTablespace; /* current tablespace */
263270
bool currWithOids; /* current default_with_oids setting */
264271

265272
void *lo_buf;
@@ -283,6 +290,8 @@ typedef struct _tocEntry
283290
* (used in restore) */
284291
char *tag; /* index tag */
285292
char *namespace; /* null or empty string if not in a schema */
293+
char *tablespace; /* null if not in a tablespace; empty string
294+
* means use database default */
286295
char *owner;
287296
bool withOids; /* Used only by "TABLE" tags */
288297
char *desc;

src/bin/pg_dump/pg_backup_db.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.60 2004/10/16 03:10:15 momjian Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.61 2004/11/06 19:36:01 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -69,6 +69,7 @@ _check_database_version(ArchiveHandle *AH, bool ignoreVersion)
6969

7070
remoteversion = _parse_version(AH, remoteversion_str);
7171

72+
AH->public.remoteVersionStr = strdup(remoteversion_str);
7273
AH->public.remoteVersion = remoteversion;
7374

7475
if (myversion != remoteversion

0 commit comments

Comments
 (0)