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);
6060static void _becomeUser (ArchiveHandle * AH ,const char * user );
6161static void _becomeOwner (ArchiveHandle * AH ,TocEntry * te );
6262static void _selectOutputSchema (ArchiveHandle * AH ,const char * schemaName );
63+ static void _selectTablespace (ArchiveHandle * AH ,const char * tablespace );
6364
6465static teReqs _tocEntryRequired (TocEntry * te ,RestoreOptions * ropt ,bool acl_pass );
6566static void _disableTriggersIfNecessary (ArchiveHandle * AH ,TocEntry * te ,RestoreOptions * ropt );
602603ArchiveEntry (Archive * AHX ,
603604CatalogId catalogId ,DumpId dumpId ,
604605const 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 ,
606609const char * desc ,const char * defn ,
607610const char * dropStmt ,const char * copyStmt ,
608611const DumpId * deps ,int nDeps ,
@@ -629,6 +632,7 @@ ArchiveEntry(Archive *AHX,
629632
630633newToc -> tag = strdup (tag );
631634newToc -> namespace = namespace ?strdup (namespace ) :NULL ;
635+ newToc -> tablespace = tablespace ?strdup (tablespace ) :NULL ;
632636newToc -> owner = strdup (owner );
633637newToc -> withOids = withOids ;
634638newToc -> desc = strdup (desc );
@@ -693,6 +697,12 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
693697ahprintf (AH ,"; Format: %s\n" ,fmtName );
694698ahprintf (AH ,"; Integer: %d bytes\n" , (int )AH -> intSize );
695699ahprintf (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
697707ahprintf (AH ,";\n;\n; Selected TOC Entries:\n;\n" );
698708
@@ -1822,6 +1832,7 @@ WriteToc(ArchiveHandle *AH)
18221832WriteStr (AH ,te -> dropStmt );
18231833WriteStr (AH ,te -> copyStmt );
18241834WriteStr (AH ,te -> namespace );
1835+ WriteStr (AH ,te -> tablespace );
18251836WriteStr (AH ,te -> owner );
18261837WriteStr (AH ,te -> withOids ?"true" :"false" );
18271838
@@ -1891,6 +1902,9 @@ ReadToc(ArchiveHandle *AH)
18911902if (AH -> version >=K_VERS_1_6 )
18921903te -> namespace = ReadStr (AH );
18931904
1905+ if (AH -> version >=K_VERS_1_10 )
1906+ te -> tablespace = ReadStr (AH );
1907+
18941908te -> owner = ReadStr (AH );
18951909if (AH -> version >=K_VERS_1_9 )
18961910{
@@ -2293,6 +2307,61 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
22932307destroyPQExpBuffer (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
23782447strcmp (te -> desc ,"SCHEMA" )== 0 && strcmp (te -> tag ,"public" )== 0 )
23792448return ;
23802449
2381- /* Select owner andschema as necessary */
2450+ /* Select owner, schema, andtablespace as necessary */
23822451_becomeOwner (AH ,te );
23832452_selectOutputSchema (AH ,te -> namespace );
2453+ _selectTablespace (AH ,te -> tablespace );
23842454
23852455/* Set up OID mode too */
23862456if (strcmp (te -> desc ,"TABLE" )== 0 )
@@ -2411,10 +2481,14 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
24112481ahprintf (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" ,
24152485pfx ,te -> tag ,te -> desc ,
24162486te -> namespace ?te -> namespace :"-" ,
24172487te -> owner );
2488+ if (te -> tablespace )
2489+ ahprintf (AH ,"; Tablespace: %s" ,te -> tablespace );
2490+ ahprintf (AH ,"\n" );
2491+
24182492if (AH -> PrintExtraTocPtr != NULL )
24192493(* AH -> PrintExtraTocPtr ) (AH ,te );
24202494ahprintf (AH ,"--\n\n" );
@@ -2509,6 +2583,8 @@ WriteHead(ArchiveHandle *AH)
25092583WriteInt (AH ,crtm .tm_year );
25102584WriteInt (AH ,crtm .tm_isdst );
25112585WriteStr (AH ,PQdb (AH -> connection ));
2586+ WriteStr (AH ,AH -> public .remoteVersionStr );
2587+ WriteStr (AH ,PG_VERSION );
25122588}
25132589
25142590void
@@ -2595,6 +2671,12 @@ ReadHead(ArchiveHandle *AH)
25952671write_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