8
8
*
9
9
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
10
10
*
11
- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.240 2010/03/11 04:36:43 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.241 2010/03/11 21:29:32 tgl Exp $
12
12
*/
13
13
#include "postgres_fe.h"
14
14
@@ -1105,7 +1105,6 @@ describeOneTableDetails(const char *schemaname,
1105
1105
bool hasrules ;
1106
1106
bool hastriggers ;
1107
1107
bool hasoids ;
1108
- bool hasexclusion ;
1109
1108
Oid tablespace ;
1110
1109
char * reloptions ;
1111
1110
char * reloftype ;
@@ -1128,8 +1127,8 @@ describeOneTableDetails(const char *schemaname,
1128
1127
printfPQExpBuffer (& buf ,
1129
1128
"SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, "
1130
1129
"c.relhastriggers, c.relhasoids, "
1131
- "%s, c.reltablespace, c.relhasexclusion, "
1132
- "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::text END\n"
1130
+ "%s, c.reltablespace, "
1131
+ "CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog. text END\n"
1133
1132
"FROM pg_catalog.pg_class c\n "
1134
1133
"LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n"
1135
1134
"WHERE c.oid = '%s'\n" ,
@@ -1207,10 +1206,8 @@ describeOneTableDetails(const char *schemaname,
1207
1206
strdup (PQgetvalue (res , 0 , 6 )) : 0 ;
1208
1207
tableinfo .tablespace = (pset .sversion >= 80000 ) ?
1209
1208
atooid (PQgetvalue (res , 0 , 7 )) : 0 ;
1210
- tableinfo .hasexclusion = (pset .sversion >= 90000 ) ?
1211
- strcmp (PQgetvalue (res , 0 , 8 ), "t" ) == 0 : false;
1212
- tableinfo .reloftype = (pset .sversion >= 90000 && strcmp (PQgetvalue (res , 0 , 9 ), "" ) != 0 ) ?
1213
- strdup (PQgetvalue (res , 0 , 9 )) : 0 ;
1209
+ tableinfo .reloftype = (pset .sversion >= 90000 && strcmp (PQgetvalue (res , 0 , 8 ), "" ) != 0 ) ?
1210
+ strdup (PQgetvalue (res , 0 , 8 )) : 0 ;
1214
1211
PQclear (res );
1215
1212
res = NULL ;
1216
1213
@@ -1545,27 +1542,23 @@ describeOneTableDetails(const char *schemaname,
1545
1542
appendPQExpBuffer (& buf , "i.indisvalid, " );
1546
1543
else
1547
1544
appendPQExpBuffer (& buf , "true as indisvalid, " );
1548
- appendPQExpBuffer (& buf , "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)" );
1545
+ appendPQExpBuffer (& buf , "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n " );
1549
1546
if (pset .sversion >= 90000 )
1550
1547
appendPQExpBuffer (& buf ,
1551
- ",\n (NOT i.indimmediate) AND "
1552
- "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1553
- "WHERE conrelid = i.indrelid AND "
1554
- "conindid = i.indexrelid AND "
1555
- "contype IN ('p','u','x') AND "
1556
- "condeferrable) AS condeferrable"
1557
- ",\n (NOT i.indimmediate) AND "
1558
- "EXISTS (SELECT 1 FROM pg_catalog.pg_constraint "
1559
- "WHERE conrelid = i.indrelid AND "
1560
- "conindid = i.indexrelid AND "
1561
- "contype IN ('p','u','x') AND "
1562
- "condeferred) AS condeferred" );
1548
+ "pg_catalog.pg_get_constraintdef(con.oid, true), "
1549
+ "contype, condeferrable, condeferred" );
1563
1550
else
1564
- appendPQExpBuffer (& buf , ", false AS condeferrable, false AS condeferred" );
1551
+ appendPQExpBuffer (& buf ,
1552
+ "null AS constraintdef, null AS contype, "
1553
+ "false AS condeferrable, false AS condeferred" );
1565
1554
if (pset .sversion >= 80000 )
1566
1555
appendPQExpBuffer (& buf , ", c2.reltablespace" );
1567
1556
appendPQExpBuffer (& buf ,
1568
- "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1557
+ "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" );
1558
+ if (pset .sversion >= 90000 )
1559
+ appendPQExpBuffer (& buf ,
1560
+ " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n" );
1561
+ appendPQExpBuffer (& buf ,
1569
1562
"WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1570
1563
"ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname" ,
1571
1564
oid );
@@ -1580,46 +1573,55 @@ describeOneTableDetails(const char *schemaname,
1580
1573
printTableAddFooter (& cont , _ ("Indexes:" ));
1581
1574
for (i = 0 ; i < tuples ; i ++ )
1582
1575
{
1583
- const char * indexdef ;
1584
- const char * usingpos ;
1585
-
1586
1576
/* untranslated index name */
1587
1577
printfPQExpBuffer (& buf , " \"%s\"" ,
1588
1578
PQgetvalue (result , i , 0 ));
1589
1579
1590
- /* Label as primary key or unique (but not both) */
1591
- appendPQExpBuffer (& buf ,
1592
- strcmp (PQgetvalue (result , i , 1 ), "t" ) == 0
1593
- ? " PRIMARY KEY," :
1594
- (strcmp (PQgetvalue (result , i , 2 ), "t" ) == 0
1595
- ? " UNIQUE,"
1596
- : "" ));
1597
- /* Everything after "USING" is echoed verbatim */
1598
- indexdef = PQgetvalue (result , i , 5 );
1599
- usingpos = strstr (indexdef , " USING " );
1600
- if (usingpos )
1601
- indexdef = usingpos + 7 ;
1602
-
1603
- appendPQExpBuffer (& buf , " %s" , indexdef );
1580
+ /* If exclusion constraint, print the constraintdef */
1581
+ if (strcmp (PQgetvalue (result , i , 7 ), "x" ) == 0 )
1582
+ {
1583
+ appendPQExpBuffer (& buf , " %s" ,
1584
+ PQgetvalue (result , i , 6 ));
1585
+ }
1586
+ else
1587
+ {
1588
+ const char * indexdef ;
1589
+ const char * usingpos ;
1590
+
1591
+ /* Label as primary key or unique (but not both) */
1592
+ if (strcmp (PQgetvalue (result , i , 1 ), "t" ) == 0 )
1593
+ appendPQExpBuffer (& buf , " PRIMARY KEY," );
1594
+ else if (strcmp (PQgetvalue (result , i , 2 ), "t" ) == 0 )
1595
+ appendPQExpBuffer (& buf , " UNIQUE," );
1596
+
1597
+ /* Everything after "USING" is echoed verbatim */
1598
+ indexdef = PQgetvalue (result , i , 5 );
1599
+ usingpos = strstr (indexdef , " USING " );
1600
+ if (usingpos )
1601
+ indexdef = usingpos + 7 ;
1602
+ appendPQExpBuffer (& buf , " %s" , indexdef );
1603
+
1604
+ /* Need these for deferrable PK/UNIQUE indexes */
1605
+ if (strcmp (PQgetvalue (result , i , 8 ), "t" ) == 0 )
1606
+ appendPQExpBuffer (& buf , " DEFERRABLE" );
1607
+
1608
+ if (strcmp (PQgetvalue (result , i , 9 ), "t" ) == 0 )
1609
+ appendPQExpBuffer (& buf , " INITIALLY DEFERRED" );
1610
+ }
1604
1611
1612
+ /* Add these for all cases */
1605
1613
if (strcmp (PQgetvalue (result , i , 3 ), "t" ) == 0 )
1606
1614
appendPQExpBuffer (& buf , " CLUSTER" );
1607
1615
1608
1616
if (strcmp (PQgetvalue (result , i , 4 ), "t" ) != 0 )
1609
1617
appendPQExpBuffer (& buf , " INVALID" );
1610
1618
1611
- if (strcmp (PQgetvalue (result , i , 6 ), "t" ) == 0 )
1612
- appendPQExpBuffer (& buf , " DEFERRABLE" );
1613
-
1614
- if (strcmp (PQgetvalue (result , i , 7 ), "t" ) == 0 )
1615
- appendPQExpBuffer (& buf , " INITIALLY DEFERRED" );
1616
-
1617
1619
printTableAddFooter (& cont , buf .data );
1618
1620
1619
1621
/* Print tablespace of the index on the same line */
1620
1622
if (pset .sversion >= 80000 )
1621
1623
add_tablespace_footer (& cont , 'i' ,
1622
- atooid (PQgetvalue (result , i , 8 )),
1624
+ atooid (PQgetvalue (result , i , 10 )),
1623
1625
false);
1624
1626
}
1625
1627
}
@@ -1657,38 +1659,6 @@ describeOneTableDetails(const char *schemaname,
1657
1659
PQclear (result );
1658
1660
}
1659
1661
1660
- /* print exclusion constraints */
1661
- if (tableinfo .hasexclusion )
1662
- {
1663
- printfPQExpBuffer (& buf ,
1664
- "SELECT r.conname, "
1665
- "pg_catalog.pg_get_constraintdef(r.oid, true)\n"
1666
- "FROM pg_catalog.pg_constraint r\n"
1667
- "WHERE r.conrelid = '%s' AND r.contype = 'x'\n"
1668
- "ORDER BY 1" ,
1669
- oid );
1670
- result = PSQLexec (buf .data , false);
1671
- if (!result )
1672
- goto error_return ;
1673
- else
1674
- tuples = PQntuples (result );
1675
-
1676
- if (tuples > 0 )
1677
- {
1678
- printTableAddFooter (& cont , _ ("Exclusion constraints:" ));
1679
- for (i = 0 ; i < tuples ; i ++ )
1680
- {
1681
- /* untranslated contraint name and def */
1682
- printfPQExpBuffer (& buf , " \"%s\" %s" ,
1683
- PQgetvalue (result , i , 0 ),
1684
- PQgetvalue (result , i , 1 ));
1685
-
1686
- printTableAddFooter (& cont , buf .data );
1687
- }
1688
- }
1689
- PQclear (result );
1690
- }
1691
-
1692
1662
/* print foreign-key constraints (there are none if no triggers) */
1693
1663
if (tableinfo .hastriggers )
1694
1664
{
0 commit comments