15
15
*
16
16
*
17
17
* 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 $
19
19
*
20
20
*-------------------------------------------------------------------------
21
21
*/
@@ -60,6 +60,7 @@ static void _reconnectToDB(ArchiveHandle *AH, const char *dbname);
60
60
static void _becomeUser (ArchiveHandle * AH ,const char * user );
61
61
static void _becomeOwner (ArchiveHandle * AH ,TocEntry * te );
62
62
static void _selectOutputSchema (ArchiveHandle * AH ,const char * schemaName );
63
+ static void _selectTablespace (ArchiveHandle * AH ,const char * tablespace );
63
64
64
65
static teReqs _tocEntryRequired (TocEntry * te ,RestoreOptions * ropt ,bool acl_pass );
65
66
static void _disableTriggersIfNecessary (ArchiveHandle * AH ,TocEntry * te ,RestoreOptions * ropt );
602
603
ArchiveEntry (Archive * AHX ,
603
604
CatalogId catalogId ,DumpId dumpId ,
604
605
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 ,
606
609
const char * desc ,const char * defn ,
607
610
const char * dropStmt ,const char * copyStmt ,
608
611
const DumpId * deps ,int nDeps ,
@@ -629,6 +632,7 @@ ArchiveEntry(Archive *AHX,
629
632
630
633
newToc -> tag = strdup (tag );
631
634
newToc -> namespace = namespace ?strdup (namespace ) :NULL ;
635
+ newToc -> tablespace = tablespace ?strdup (tablespace ) :NULL ;
632
636
newToc -> owner = strdup (owner );
633
637
newToc -> withOids = withOids ;
634
638
newToc -> desc = strdup (desc );
@@ -693,6 +697,12 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
693
697
ahprintf (AH ,"; Format: %s\n" ,fmtName );
694
698
ahprintf (AH ,"; Integer: %d bytes\n" , (int )AH -> intSize );
695
699
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 );
696
706
697
707
ahprintf (AH ,";\n;\n; Selected TOC Entries:\n;\n" );
698
708
@@ -1822,6 +1832,7 @@ WriteToc(ArchiveHandle *AH)
1822
1832
WriteStr (AH ,te -> dropStmt );
1823
1833
WriteStr (AH ,te -> copyStmt );
1824
1834
WriteStr (AH ,te -> namespace );
1835
+ WriteStr (AH ,te -> tablespace );
1825
1836
WriteStr (AH ,te -> owner );
1826
1837
WriteStr (AH ,te -> withOids ?"true" :"false" );
1827
1838
@@ -1891,6 +1902,9 @@ ReadToc(ArchiveHandle *AH)
1891
1902
if (AH -> version >=K_VERS_1_6 )
1892
1903
te -> namespace = ReadStr (AH );
1893
1904
1905
+ if (AH -> version >=K_VERS_1_10 )
1906
+ te -> tablespace = ReadStr (AH );
1907
+
1894
1908
te -> owner = ReadStr (AH );
1895
1909
if (AH -> version >=K_VERS_1_9 )
1896
1910
{
@@ -2293,6 +2307,61 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
2293
2307
destroyPQExpBuffer (qry );
2294
2308
}
2295
2309
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
+ }
2296
2365
2297
2366
/**
2298
2367
* Parses the dropStmt part of a TOC entry and returns
@@ -2378,9 +2447,10 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
2378
2447
strcmp (te -> desc ,"SCHEMA" )== 0 && strcmp (te -> tag ,"public" )== 0 )
2379
2448
return ;
2380
2449
2381
- /* Select owner andschema as necessary */
2450
+ /* Select owner, schema, andtablespace as necessary */
2382
2451
_becomeOwner (AH ,te );
2383
2452
_selectOutputSchema (AH ,te -> namespace );
2453
+ _selectTablespace (AH ,te -> tablespace );
2384
2454
2385
2455
/* Set up OID mode too */
2386
2456
if (strcmp (te -> desc ,"TABLE" )== 0 )
@@ -2411,10 +2481,14 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
2411
2481
ahprintf (AH ,"\n" );
2412
2482
}
2413
2483
}
2414
- ahprintf (AH ,"-- %sName: %s; Type: %s; Schema: %s; Owner: %s\n " ,
2484
+ ahprintf (AH ,"-- %sName: %s; Type: %s; Schema: %s; Owner: %s" ,
2415
2485
pfx ,te -> tag ,te -> desc ,
2416
2486
te -> namespace ?te -> namespace :"-" ,
2417
2487
te -> owner );
2488
+ if (te -> tablespace )
2489
+ ahprintf (AH ,"; Tablespace: %s" ,te -> tablespace );
2490
+ ahprintf (AH ,"\n" );
2491
+
2418
2492
if (AH -> PrintExtraTocPtr != NULL )
2419
2493
(* AH -> PrintExtraTocPtr ) (AH ,te );
2420
2494
ahprintf (AH ,"--\n\n" );
@@ -2509,6 +2583,8 @@ WriteHead(ArchiveHandle *AH)
2509
2583
WriteInt (AH ,crtm .tm_year );
2510
2584
WriteInt (AH ,crtm .tm_isdst );
2511
2585
WriteStr (AH ,PQdb (AH -> connection ));
2586
+ WriteStr (AH ,AH -> public .remoteVersionStr );
2587
+ WriteStr (AH ,PG_VERSION );
2512
2588
}
2513
2589
2514
2590
void
@@ -2595,6 +2671,12 @@ ReadHead(ArchiveHandle *AH)
2595
2671
write_msg (modulename ,"WARNING: invalid creation date in header\n" );
2596
2672
}
2597
2673
2674
+ if (AH -> version >=K_VERS_1_10 )
2675
+ {
2676
+ AH -> archiveRemoteVersion = ReadStr (AH );
2677
+ AH -> archiveDumpVersion = ReadStr (AH );
2678
+ }
2679
+
2598
2680
}
2599
2681
2600
2682