12
12
* by PostgreSQL
13
13
*
14
14
* IDENTIFICATION
15
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.499 2008/07/30 19:35:13 tgl Exp $
15
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.500 2008/09/08 15:26:23 tgl Exp $
16
16
*
17
17
*-------------------------------------------------------------------------
18
18
*/
@@ -166,6 +166,7 @@ static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
166
166
static void getDependencies (void );
167
167
static void getDomainConstraints (TypeInfo * tinfo );
168
168
static void getTableData (TableInfo * tblinfo , int numTables , bool oids );
169
+ static void getTableDataFKConstraints (void );
169
170
static char * format_function_arguments (FuncInfo * finfo , char * funcargs );
170
171
static char * format_function_arguments_old (FuncInfo * finfo , int nallargs ,
171
172
char * * allargtypes ,
@@ -659,7 +660,11 @@ main(int argc, char **argv)
659
660
guessConstraintInheritance (tblinfo , numTables );
660
661
661
662
if (!schemaOnly )
663
+ {
662
664
getTableData (tblinfo , numTables , oids );
665
+ if (dataOnly )
666
+ getTableDataFKConstraints ();
667
+ }
663
668
664
669
if (outputBlobs && hasBlobs (g_fout ))
665
670
{
@@ -1392,10 +1397,59 @@ getTableData(TableInfo *tblinfo, int numTables, bool oids)
1392
1397
tdinfo -> tdtable = & (tblinfo [i ]);
1393
1398
tdinfo -> oids = oids ;
1394
1399
addObjectDependency (& tdinfo -> dobj , tblinfo [i ].dobj .dumpId );
1400
+
1401
+ tblinfo [i ].dataObj = tdinfo ;
1395
1402
}
1396
1403
}
1397
1404
}
1398
1405
1406
+ /*
1407
+ * getTableDataFKConstraints -
1408
+ * add dump-order dependencies reflecting foreign key constraints
1409
+ *
1410
+ * This code is executed only in a data-only dump --- in schema+data dumps
1411
+ * we handle foreign key issues by not creating the FK constraints until
1412
+ * after the data is loaded. In a data-only dump, however, we want to
1413
+ * order the table data objects in such a way that a table's referenced
1414
+ * tables are restored first. (In the presence of circular references or
1415
+ * self-references this may be impossible; we'll detect and complain about
1416
+ * that during the dependency sorting step.)
1417
+ */
1418
+ static void
1419
+ getTableDataFKConstraints (void )
1420
+ {
1421
+ DumpableObject * * dobjs ;
1422
+ int numObjs ;
1423
+ int i ;
1424
+
1425
+ /* Search through all the dumpable objects for FK constraints */
1426
+ getDumpableObjects (& dobjs , & numObjs );
1427
+ for (i = 0 ; i < numObjs ; i ++ )
1428
+ {
1429
+ if (dobjs [i ]-> objType == DO_FK_CONSTRAINT )
1430
+ {
1431
+ ConstraintInfo * cinfo = (ConstraintInfo * ) dobjs [i ];
1432
+ TableInfo * ftable ;
1433
+
1434
+ /* Not interesting unless both tables are to be dumped */
1435
+ if (cinfo -> contable == NULL ||
1436
+ cinfo -> contable -> dataObj == NULL )
1437
+ continue ;
1438
+ ftable = findTableByOid (cinfo -> confrelid );
1439
+ if (ftable == NULL ||
1440
+ ftable -> dataObj == NULL )
1441
+ continue ;
1442
+ /*
1443
+ * Okay, make referencing table's TABLE_DATA object depend on
1444
+ * the referenced table's TABLE_DATA object.
1445
+ */
1446
+ addObjectDependency (& cinfo -> contable -> dataObj -> dobj ,
1447
+ ftable -> dataObj -> dobj .dumpId );
1448
+ }
1449
+ }
1450
+ free (dobjs );
1451
+ }
1452
+
1399
1453
1400
1454
/*
1401
1455
* guessConstraintInheritance:
@@ -3626,6 +3680,7 @@ getIndexes(TableInfo tblinfo[], int numTables)
3626
3680
constrinfo [j ].condomain = NULL ;
3627
3681
constrinfo [j ].contype = contype ;
3628
3682
constrinfo [j ].condef = NULL ;
3683
+ constrinfo [j ].confrelid = InvalidOid ;
3629
3684
constrinfo [j ].conindex = indxinfo [j ].dobj .dumpId ;
3630
3685
constrinfo [j ].conislocal = true;
3631
3686
constrinfo [j ].separate = true;
@@ -3666,10 +3721,11 @@ getConstraints(TableInfo tblinfo[], int numTables)
3666
3721
ConstraintInfo * constrinfo ;
3667
3722
PQExpBuffer query ;
3668
3723
PGresult * res ;
3669
- int i_condef ,
3670
- i_contableoid ,
3724
+ int i_contableoid ,
3671
3725
i_conoid ,
3672
- i_conname ;
3726
+ i_conname ,
3727
+ i_confrelid ,
3728
+ i_condef ;
3673
3729
int ntups ;
3674
3730
3675
3731
/* pg_constraint was created in 7.3, so nothing to do if older */
@@ -3697,7 +3753,7 @@ getConstraints(TableInfo tblinfo[], int numTables)
3697
3753
3698
3754
resetPQExpBuffer (query );
3699
3755
appendPQExpBuffer (query ,
3700
- "SELECT tableoid, oid, conname, "
3756
+ "SELECT tableoid, oid, conname, confrelid, "
3701
3757
"pg_catalog.pg_get_constraintdef(oid) as condef "
3702
3758
"FROM pg_catalog.pg_constraint "
3703
3759
"WHERE conrelid = '%u'::pg_catalog.oid "
@@ -3711,6 +3767,7 @@ getConstraints(TableInfo tblinfo[], int numTables)
3711
3767
i_contableoid = PQfnumber (res , "tableoid" );
3712
3768
i_conoid = PQfnumber (res , "oid" );
3713
3769
i_conname = PQfnumber (res , "conname" );
3770
+ i_confrelid = PQfnumber (res , "confrelid" );
3714
3771
i_condef = PQfnumber (res , "condef" );
3715
3772
3716
3773
constrinfo = (ConstraintInfo * ) malloc (ntups * sizeof (ConstraintInfo ));
@@ -3727,6 +3784,7 @@ getConstraints(TableInfo tblinfo[], int numTables)
3727
3784
constrinfo [j ].condomain = NULL ;
3728
3785
constrinfo [j ].contype = 'f' ;
3729
3786
constrinfo [j ].condef = strdup (PQgetvalue (res , j , i_condef ));
3787
+ constrinfo [j ].confrelid = atooid (PQgetvalue (res , j , i_confrelid ));
3730
3788
constrinfo [j ].conindex = 0 ;
3731
3789
constrinfo [j ].conislocal = true;
3732
3790
constrinfo [j ].separate = true;
@@ -3810,6 +3868,7 @@ getDomainConstraints(TypeInfo *tinfo)
3810
3868
constrinfo [i ].condomain = tinfo ;
3811
3869
constrinfo [i ].contype = 'c' ;
3812
3870
constrinfo [i ].condef = strdup (PQgetvalue (res , i , i_consrc ));
3871
+ constrinfo [i ].confrelid = InvalidOid ;
3813
3872
constrinfo [i ].conindex = 0 ;
3814
3873
constrinfo [i ].conislocal = true;
3815
3874
constrinfo [i ].separate = false;
@@ -4788,6 +4847,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
4788
4847
constrs [j ].condomain = NULL ;
4789
4848
constrs [j ].contype = 'c' ;
4790
4849
constrs [j ].condef = strdup (PQgetvalue (res , j , 3 ));
4850
+ constrs [j ].confrelid = InvalidOid ;
4791
4851
constrs [j ].conindex = 0 ;
4792
4852
constrs [j ].conislocal = (PQgetvalue (res , j , 4 )[0 ] == 't' );
4793
4853
constrs [j ].separate = false;
0 commit comments