9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.263 2007/07/17 05:02:02 neilc Exp $
12
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.264 2007/10/13 15:55:40 tgl Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
28
28
#include "catalog/pg_operator.h"
29
29
#include "catalog/pg_trigger.h"
30
30
#include "commands/defrem.h"
31
+ #include "commands/tablespace.h"
31
32
#include "executor/spi.h"
32
33
#include "funcapi.h"
33
34
#include "nodes/makefuncs.h"
@@ -126,12 +127,13 @@ static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags);
126
127
static void decompile_column_index_array (Datum column_index_array , Oid relId ,
127
128
StringInfo buf );
128
129
static char * pg_get_ruledef_worker (Oid ruleoid , int prettyFlags );
129
- static char * pg_get_indexdef_worker (Oid indexrelid , int colno ,
130
+ static char * pg_get_indexdef_worker (Oid indexrelid , int colno , bool showTblSpc ,
130
131
int prettyFlags );
131
132
static char * pg_get_constraintdef_worker (Oid constraintId , bool fullCommand ,
132
133
int prettyFlags );
133
134
static char * pg_get_expr_worker (text * expr , Oid relid , char * relname ,
134
135
int prettyFlags );
136
+ static Oid get_constraint_index (Oid constraintId );
135
137
static void make_ruledef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
136
138
int prettyFlags );
137
139
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
@@ -577,14 +579,19 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
577
579
* In the extended version, there is a colno argument as well as pretty bool.
578
580
* if colno == 0, we want a complete index definition.
579
581
* if colno > 0, we only want the Nth index key's variable or expression.
582
+ *
583
+ * Note that the SQL-function versions of this omit any info about the
584
+ * index tablespace; this is intentional because pg_dump wants it that way.
585
+ * However pg_get_indexdef_string() includes index tablespace if not default.
580
586
* ----------
581
587
*/
582
588
Datum
583
589
pg_get_indexdef (PG_FUNCTION_ARGS )
584
590
{
585
591
Oid indexrelid = PG_GETARG_OID (0 );
586
592
587
- PG_RETURN_TEXT_P (string_to_text (pg_get_indexdef_worker (indexrelid , 0 , 0 )));
593
+ PG_RETURN_TEXT_P (string_to_text (pg_get_indexdef_worker (indexrelid , 0 ,
594
+ false, 0 )));
588
595
}
589
596
590
597
Datum
@@ -596,18 +603,20 @@ pg_get_indexdef_ext(PG_FUNCTION_ARGS)
596
603
int prettyFlags ;
597
604
598
605
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0 ;
599
- PG_RETURN_TEXT_P (string_to_text (pg_get_indexdef_worker (indexrelid , colno , prettyFlags )));
606
+ PG_RETURN_TEXT_P (string_to_text (pg_get_indexdef_worker (indexrelid , colno ,
607
+ false, prettyFlags )));
600
608
}
601
609
602
610
/* Internal version that returns a palloc'd C string */
603
611
char *
604
612
pg_get_indexdef_string (Oid indexrelid )
605
613
{
606
- return pg_get_indexdef_worker (indexrelid , 0 , 0 );
614
+ return pg_get_indexdef_worker (indexrelid , 0 , true, 0 );
607
615
}
608
616
609
617
static char *
610
- pg_get_indexdef_worker (Oid indexrelid , int colno , int prettyFlags )
618
+ pg_get_indexdef_worker (Oid indexrelid , int colno , bool showTblSpc ,
619
+ int prettyFlags )
611
620
{
612
621
HeapTuple ht_idx ;
613
622
HeapTuple ht_idxrel ;
@@ -798,8 +807,17 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, int prettyFlags)
798
807
}
799
808
800
809
/*
801
- * XXX we don't include the tablespace ... this is for pg_dump
810
+ * If it's in a nondefault tablespace, say so, but only if requested
802
811
*/
812
+ if (showTblSpc )
813
+ {
814
+ Oid tblspc ;
815
+
816
+ tblspc = get_rel_tablespace (indexrelid );
817
+ if (OidIsValid (tblspc ))
818
+ appendStringInfo (& buf , " TABLESPACE %s" ,
819
+ quote_identifier (get_tablespace_name (tblspc )));
820
+ }
803
821
804
822
/*
805
823
* If it's a partial index, decompile and append the predicate
@@ -1014,6 +1032,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
1014
1032
{
1015
1033
Datum val ;
1016
1034
bool isnull ;
1035
+ Oid indexId ;
1017
1036
1018
1037
/* Start off the constraint definition */
1019
1038
if (conForm -> contype == CONSTRAINT_PRIMARY )
@@ -1032,15 +1051,24 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
1032
1051
1033
1052
appendStringInfo (& buf , ")" );
1034
1053
1035
- if (fullCommand && OidIsValid (conForm -> conrelid ))
1054
+ indexId = get_constraint_index (constraintId );
1055
+
1056
+ /* XXX why do we only print these bits if fullCommand? */
1057
+ if (fullCommand && OidIsValid (indexId ))
1036
1058
{
1037
- char * options = flatten_reloptions (conForm -> conrelid );
1059
+ char * options = flatten_reloptions (indexId );
1060
+ Oid tblspc ;
1038
1061
1039
1062
if (options )
1040
1063
{
1041
1064
appendStringInfo (& buf , " WITH (%s)" , options );
1042
1065
pfree (options );
1043
1066
}
1067
+
1068
+ tblspc = get_rel_tablespace (indexId );
1069
+ if (OidIsValid (tblspc ))
1070
+ appendStringInfo (& buf , " USING INDEX TABLESPACE %s" ,
1071
+ quote_identifier (get_tablespace_name (tblspc )));
1044
1072
}
1045
1073
1046
1074
break ;
@@ -1356,7 +1384,69 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS)
1356
1384
}
1357
1385
1358
1386
1359
- /* ----------
1387
+ /*
1388
+ * get_constraint_index
1389
+ * Given the OID of a unique or primary-key constraint, return the
1390
+ * OID of the underlying unique index.
1391
+ *
1392
+ * Return InvalidOid if the index couldn't be found; this suggests the
1393
+ * given OID is bogus, but we leave it to caller to decide what to do.
1394
+ */
1395
+ static Oid
1396
+ get_constraint_index (Oid constraintId )
1397
+ {
1398
+ Oid indexId = InvalidOid ;
1399
+ Relation depRel ;
1400
+ ScanKeyData key [3 ];
1401
+ SysScanDesc scan ;
1402
+ HeapTuple tup ;
1403
+
1404
+ /* Search the dependency table for the dependent index */
1405
+ depRel = heap_open (DependRelationId , AccessShareLock );
1406
+
1407
+ ScanKeyInit (& key [0 ],
1408
+ Anum_pg_depend_refclassid ,
1409
+ BTEqualStrategyNumber , F_OIDEQ ,
1410
+ ObjectIdGetDatum (ConstraintRelationId ));
1411
+ ScanKeyInit (& key [1 ],
1412
+ Anum_pg_depend_refobjid ,
1413
+ BTEqualStrategyNumber , F_OIDEQ ,
1414
+ ObjectIdGetDatum (constraintId ));
1415
+ ScanKeyInit (& key [2 ],
1416
+ Anum_pg_depend_refobjsubid ,
1417
+ BTEqualStrategyNumber , F_INT4EQ ,
1418
+ Int32GetDatum (0 ));
1419
+
1420
+ scan = systable_beginscan (depRel , DependReferenceIndexId , true,
1421
+ SnapshotNow , 3 , key );
1422
+
1423
+ while (HeapTupleIsValid (tup = systable_getnext (scan )))
1424
+ {
1425
+ Form_pg_depend deprec = (Form_pg_depend ) GETSTRUCT (tup );
1426
+
1427
+ /*
1428
+ * We assume any internal dependency of an index on the constraint
1429
+ * must be what we are looking for. (The relkind test is just
1430
+ * paranoia; there shouldn't be any such dependencies otherwise.)
1431
+ */
1432
+ if (deprec -> classid == RelationRelationId &&
1433
+ deprec -> objsubid == 0 &&
1434
+ deprec -> deptype == DEPENDENCY_INTERNAL &&
1435
+ get_rel_relkind (deprec -> objid ) == RELKIND_INDEX )
1436
+ {
1437
+ indexId = deprec -> objid ;
1438
+ break ;
1439
+ }
1440
+ }
1441
+
1442
+ systable_endscan (scan );
1443
+ heap_close (depRel , AccessShareLock );
1444
+
1445
+ return indexId ;
1446
+ }
1447
+
1448
+
1449
+ /*
1360
1450
* deparse_expression - General utility for deparsing expressions
1361
1451
*
1362
1452
* calls deparse_expression_pretty with all prettyPrinting disabled
0 commit comments