20
20
*
21
21
*
22
22
* IDENTIFICATION
23
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.25 1997/03/01 15:24:51 momjian Exp $
23
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.26 1997/04/02 04:17:21 vadim Exp $
24
24
*
25
25
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
26
26
*
61
61
62
62
#include "pg_dump.h"
63
63
64
+ static void dumpSequence (FILE * fout , TableInfo tbinfo );
65
+
64
66
extern char * optarg ;
65
67
extern int optind , opterr ;
66
68
@@ -71,6 +73,8 @@ FILE *g_fout; /* the script file */
71
73
PGconn * g_conn ; /* the database connection */
72
74
int dumpData ; /* dump data using proper insert strings */
73
75
int attrNames ; /* put attr names into insert strings */
76
+ int schemaOnly ;
77
+ int dataOnly ;
74
78
75
79
char g_opaque_type [10 ]; /* name for the opaque type */
76
80
@@ -346,7 +350,23 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout,
346
350
if (isViewRule (tblinfo [i ].relname ))
347
351
continue ;
348
352
349
- if (!onlytable || (!strcmp (classname ,onlytable ))) {
353
+ if (!onlytable || (!strcmp (classname ,onlytable )))
354
+ {
355
+ if ( tblinfo [i ].sequence )
356
+ {
357
+ if ( dataOnly ) /* i.e. SCHEMA didn't dumped */
358
+ {
359
+ if ( g_verbose )
360
+ fprintf (stderr , "%s dumping out schema of sequence %s %s\n" ,
361
+ g_comment_start , classname , g_comment_end );
362
+ dumpSequence (fout , tblinfo [i ]);
363
+ }
364
+ else if ( g_verbose )
365
+ fprintf (stderr , "%s contents of sequence '%s' dumped in schema %s\n" ,
366
+ g_comment_start , classname , g_comment_end );
367
+ continue ;
368
+ }
369
+
350
370
if (g_verbose )
351
371
fprintf (stderr , "%s dumping out the contents of Table %s %s\n" ,
352
372
g_comment_start , classname , g_comment_end );
@@ -372,8 +392,6 @@ main(int argc, char** argv)
372
392
const char * progname ;
373
393
const char * filename ;
374
394
const char * dbname ;
375
- int schemaOnly ;
376
- int dataOnly ;
377
395
const char * pghost = NULL ;
378
396
const char * pgport = NULL ;
379
397
const char * tablename ;
@@ -905,6 +923,7 @@ getTables(int *numTables)
905
923
int i_oid ;
906
924
int i_relname ;
907
925
int i_relarch ;
926
+ int i_relkind ;
908
927
909
928
/* find all the user-defined tables (no indices and no catalogs),
910
929
ordering by oid is important so that we always process the parent
@@ -921,8 +940,8 @@ getTables(int *numTables)
921
940
PQclear (res );
922
941
923
942
sprintf (query ,
924
- "SELECT oid, relname, relarch from pg_class "
925
- "where relkind = 'r' and relname !~ '^pg_' "
943
+ "SELECT oid, relname, relarch, relkind from pg_class "
944
+ "where ( relkind = 'r' or relkind = 'S') and relname !~ '^pg_' "
926
945
"and relname !~ '^Xinv' order by oid;" );
927
946
928
947
res = PQexec (g_conn , query );
@@ -941,11 +960,13 @@ getTables(int *numTables)
941
960
i_oid = PQfnumber (res ,"oid" );
942
961
i_relname = PQfnumber (res ,"relname" );
943
962
i_relarch = PQfnumber (res ,"relarch" );
963
+ i_relkind = PQfnumber (res ,"relkind" );
944
964
945
965
for (i = 0 ;i < ntups ;i ++ ) {
946
966
tblinfo [i ].oid = strdup (PQgetvalue (res ,i ,i_oid ));
947
967
tblinfo [i ].relname = strdup (PQgetvalue (res ,i ,i_relname ));
948
968
tblinfo [i ].relarch = strdup (PQgetvalue (res ,i ,i_relarch ));
969
+ tblinfo [i ].sequence = (strcmp (PQgetvalue (res ,i ,i_relkind ), "S" ) == 0 );
949
970
}
950
971
951
972
PQclear (res );
@@ -1041,6 +1062,9 @@ getTableAttrs(TableInfo* tblinfo, int numTables)
1041
1062
/* skip archive tables */
1042
1063
if (isArchiveName (tblinfo [i ].relname ))
1043
1064
continue ;
1065
+
1066
+ if ( tblinfo [i ].sequence )
1067
+ continue ;
1044
1068
1045
1069
/* find all the user attributes and their types*/
1046
1070
/* we must read the attribute names in attribute number order! */
@@ -1500,6 +1524,12 @@ void dumpTables(FILE* fout, TableInfo *tblinfo, int numTables,
1500
1524
/* skip archive names*/
1501
1525
if (isArchiveName (tblinfo [i ].relname ))
1502
1526
continue ;
1527
+
1528
+ if ( tblinfo [i ].sequence )
1529
+ {
1530
+ dumpSequence (fout , tblinfo [i ]);
1531
+ continue ;
1532
+ }
1503
1533
1504
1534
parentRels = tblinfo [i ].parentRels ;
1505
1535
numParents = tblinfo [i ].numParents ;
@@ -1814,3 +1844,68 @@ checkForQuote(const char* s)
1814
1844
return result ;
1815
1845
1816
1846
}
1847
+
1848
+
1849
+ static void dumpSequence (FILE * fout , TableInfo tbinfo )
1850
+ {
1851
+ PGresult * res ;
1852
+ int4 last , incby , maxv , minv , cache ;
1853
+ char cycled , called , * t ;
1854
+ char query [MAXQUERYLEN ];
1855
+
1856
+ sprintf (query ,
1857
+ "SELECT sequence_name, last_value, increment_by, max_value, "
1858
+ "min_value, cache_value, is_cycled, is_called from %s;" ,
1859
+ tbinfo .relname );
1860
+
1861
+ res = PQexec (g_conn , query );
1862
+ if ( !res || PQresultStatus (res ) != PGRES_TUPLES_OK )
1863
+ {
1864
+ fprintf (stderr ,"dumpSequence(%s): SELECT failed\n" , tbinfo .relname );
1865
+ exit_nicely (g_conn );
1866
+ }
1867
+
1868
+ if ( PQntuples (res ) != 1 )
1869
+ {
1870
+ fprintf (stderr ,"dumpSequence(%s): %d (!= 1) tuples returned by SELECT\n" ,
1871
+ tbinfo .relname , PQntuples (res ));
1872
+ exit_nicely (g_conn );
1873
+ }
1874
+
1875
+ if ( strcmp (PQgetvalue (res ,0 ,0 ), tbinfo .relname ) != 0 )
1876
+ {
1877
+ fprintf (stderr , "dumpSequence(%s): different sequence name "
1878
+ "returned by SELECT: %s\n" ,
1879
+ tbinfo .relname , PQgetvalue (res ,0 ,0 ));
1880
+ exit_nicely (g_conn );
1881
+ }
1882
+
1883
+
1884
+ last = atoi (PQgetvalue (res ,0 ,1 ));
1885
+ incby = atoi (PQgetvalue (res ,0 ,2 ));
1886
+ maxv = atoi (PQgetvalue (res ,0 ,3 ));
1887
+ minv = atoi (PQgetvalue (res ,0 ,4 ));
1888
+ cache = atoi (PQgetvalue (res ,0 ,5 ));
1889
+ t = PQgetvalue (res ,0 ,6 );
1890
+ cycled = * t ;
1891
+ t = PQgetvalue (res ,0 ,7 );
1892
+ called = * t ;
1893
+
1894
+ PQclear (res );
1895
+
1896
+ sprintf (query ,
1897
+ "CREATE SEQUENCE %s start %d increment %d maxvalue %d "
1898
+ "minvalue %d cache %d %s;\n" ,
1899
+ tbinfo .relname , last , incby , maxv , minv , cache ,
1900
+ (cycled == 't' ) ? "cycle" : "" );
1901
+
1902
+ fputs (query , fout );
1903
+
1904
+ if ( called == 'f' )
1905
+ return ; /* nothing to do more */
1906
+
1907
+ sprintf (query , "SELECT nextval ('%s');\n" , tbinfo .relname );
1908
+ fputs (query , fout );
1909
+
1910
+ }
1911
+
0 commit comments