21
21
*
22
22
*
23
23
* IDENTIFICATION
24
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.27 1997/04/12 09:24:07 scrappy Exp $
24
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.28 1997/05/06 05:20:18 vadim Exp $
25
25
*
26
26
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
27
27
*
55
55
#include "postgres.h"
56
56
#include "access/htup.h"
57
57
#include "catalog/pg_type.h"
58
+ #include "catalog/pg_index.h"
58
59
#include "libpq-fe.h"
59
60
#ifndef HAVE_STRDUP
60
61
#include "strdup.h"
@@ -1142,16 +1143,16 @@ getIndices(int *numIndices)
1142
1143
int i_indamname ;
1143
1144
int i_indproc ;
1144
1145
int i_indkey ;
1145
- int i_indclassname ;
1146
+ int i_indclass ;
1146
1147
int i_indisunique ;
1147
1148
1148
- /* find all the user-defined indices.
1149
+ /*
1150
+ find all the user-defined indices.
1149
1151
We do not handle partial indices.
1150
- We also assume that only single key indices
1151
1152
1152
1153
skip 'Xinx*' - indices on inversion objects
1153
1154
1154
- this is a 5 -way join !!
1155
+ this is a 4 -way join !!
1155
1156
*/
1156
1157
1157
1158
res = PQexec (g_conn , "begin" );
@@ -1164,13 +1165,12 @@ getIndices(int *numIndices)
1164
1165
1165
1166
sprintf (query ,
1166
1167
"SELECT t1.relname as indexrelname, t2.relname as indrelname, "
1167
- "i.indproc, i.indkey[0], o.opcname as indclassname , "
1168
- "a.amname as indamname, i.indisunique from pg_index i, pg_class t1, "
1169
- "pg_class t2, pg_opclass o , pg_am a "
1168
+ "i.indproc, i.indkey, i.indclass , "
1169
+ "a.amname as indamname, i.indisunique "
1170
+ "from pg_index i, pg_class t1, pg_class t2 , pg_am a "
1170
1171
"where t1.oid = i.indexrelid and t2.oid = i.indrelid "
1171
- "and o.oid = i.indclass[0] and t1.relam = a.oid and "
1172
- "i.indexrelid > '%d'::oid and t2.relname !~ '^pg_' "
1173
- "and t1.relname !~ '^Xinx' ;" ,
1172
+ "and t1.relam = a.oid and i.indexrelid > '%d'::oid "
1173
+ "and t2.relname !~ '^pg_' and t1.relname !~ '^Xinx' ;" ,
1174
1174
g_last_builtin_oid );
1175
1175
1176
1176
res = PQexec (g_conn , query );
@@ -1191,16 +1191,18 @@ getIndices(int *numIndices)
1191
1191
i_indamname = PQfnumber (res ,"indamname" );
1192
1192
i_indproc = PQfnumber (res ,"indproc" );
1193
1193
i_indkey = PQfnumber (res ,"indkey" );
1194
- i_indclassname = PQfnumber (res ,"indclassname " );
1194
+ i_indclass = PQfnumber (res ,"indclass " );
1195
1195
i_indisunique = PQfnumber (res ,"indisunique" );
1196
1196
1197
1197
for (i = 0 ;i < ntups ;i ++ ) {
1198
1198
indinfo [i ].indexrelname = strdup (PQgetvalue (res ,i ,i_indexrelname ));
1199
1199
indinfo [i ].indrelname = strdup (PQgetvalue (res ,i ,i_indrelname ));
1200
1200
indinfo [i ].indamname = strdup (PQgetvalue (res ,i ,i_indamname ));
1201
1201
indinfo [i ].indproc = strdup (PQgetvalue (res ,i ,i_indproc ));
1202
- indinfo [i ].indkey = strdup (PQgetvalue (res ,i ,i_indkey ));
1203
- indinfo [i ].indclassname = strdup (PQgetvalue (res ,i ,i_indclassname ));
1202
+ parseArgTypes ((char * * )indinfo [i ].indkey ,
1203
+ (const char * )PQgetvalue (res ,i ,i_indkey ));
1204
+ parseArgTypes ((char * * )indinfo [i ].indclass ,
1205
+ (const char * )PQgetvalue (res ,i ,i_indclass ));
1204
1206
indinfo [i ].indisunique = strdup (PQgetvalue (res ,i ,i_indisunique ));
1205
1207
}
1206
1208
PQclear (res );
@@ -1634,23 +1636,21 @@ void
1634
1636
dumpIndices (FILE * fout , IndInfo * indinfo , int numIndices ,
1635
1637
TableInfo * tblinfo , int numTables , const char * tablename )
1636
1638
{
1637
- int i ;
1639
+ int i , k ;
1638
1640
int tableInd ;
1639
- const char * attname ; /* the name of the indexed attribute */
1641
+ char attlist [1000 ];
1642
+ char * classname [INDEX_MAX_KEYS ];
1640
1643
char * funcname ; /* the name of the function to comput the index key from*/
1641
- int indkey ;
1644
+ int indkey , indclass ;
1645
+ int nclass ;
1642
1646
1643
1647
char q [MAXQUERYLEN ];
1644
1648
PGresult * res ;
1645
1649
1646
1650
for (i = 0 ;i < numIndices ;i ++ ) {
1647
1651
tableInd = findTableByName (tblinfo , numTables ,
1648
1652
indinfo [i ].indrelname );
1649
- indkey = atoi (indinfo [i ].indkey ) - 1 ;
1650
- if (indkey == ObjectIdAttributeNumber - 1 )
1651
- attname = "oid" ;
1652
- else
1653
- attname = tblinfo [tableInd ].attnames [indkey ];
1653
+
1654
1654
if (strcmp (indinfo [i ].indproc ,"0" ) == 0 ) {
1655
1655
funcname = NULL ;
1656
1656
} else {
@@ -1664,10 +1664,74 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices,
1664
1664
"where pg_proc.oid = '%s'::oid" ,
1665
1665
indinfo [i ].indproc );
1666
1666
res = PQexec (g_conn , q );
1667
+ if ( !res || PQresultStatus (res ) != PGRES_TUPLES_OK )
1668
+ {
1669
+ fprintf (stderr ,"dumpIndices(): SELECT (funcname) failed\n" );
1670
+ exit_nicely (g_conn );
1671
+ }
1667
1672
funcname = strdup (PQgetvalue (res , 0 ,
1668
1673
PQfnumber (res ,"proname" )));
1669
1674
PQclear (res );
1670
1675
}
1676
+
1677
+ /* convert opclass oid(s) into names */
1678
+ for (nclass = 0 ; nclass < INDEX_MAX_KEYS ; nclass ++ )
1679
+ {
1680
+ indclass = atoi (indinfo [i ].indclass [nclass ]);
1681
+ if ( indclass == 0 )
1682
+ break ;
1683
+ sprintf (q ,
1684
+ "SELECT opcname from pg_opclass "
1685
+ "where pg_opclass.oid = '%u'::oid" ,
1686
+ indclass );
1687
+ res = PQexec (g_conn , q );
1688
+ if ( !res || PQresultStatus (res ) != PGRES_TUPLES_OK )
1689
+ {
1690
+ fprintf (stderr ,"dumpIndices(): SELECT (classname) failed\n" );
1691
+ exit_nicely (g_conn );
1692
+ }
1693
+ classname [nclass ] = strdup (PQgetvalue (res , 0 ,
1694
+ PQfnumber (res ,"opcname" )));
1695
+ PQclear (res );
1696
+ }
1697
+
1698
+ if ( funcname && nclass != 1 )
1699
+ {
1700
+ fprintf (stderr ,"dumpIndices(): Must be exactly one OpClass "
1701
+ "for functional index %s\n" , indinfo [i ].indexrelname );
1702
+ exit_nicely (g_conn );
1703
+ }
1704
+
1705
+ /* convert attribute numbers into attribute list */
1706
+ for (k = 0 , attlist [0 ] = 0 ; k < INDEX_MAX_KEYS ; k ++ )
1707
+ {
1708
+ char * attname ;
1709
+
1710
+ indkey = atoi (indinfo [i ].indkey [k ]);
1711
+ if ( indkey == 0 )
1712
+ break ;
1713
+ indkey -- ;
1714
+ if (indkey == ObjectIdAttributeNumber - 1 )
1715
+ attname = "oid" ;
1716
+ else
1717
+ attname = tblinfo [tableInd ].attnames [indkey ];
1718
+ if ( funcname )
1719
+ sprintf (attlist + strlen (attlist ), "%s%s" ,
1720
+ ( k == 0 ) ? "" : ", " , attname );
1721
+ else
1722
+ {
1723
+ if ( k >= nclass )
1724
+ {
1725
+ fprintf (stderr ,"dumpIndices(): OpClass not found for "
1726
+ "attribute %s of index %s\n" ,
1727
+ attname , indinfo [i ].indexrelname );
1728
+ exit_nicely (g_conn );
1729
+ }
1730
+ sprintf (attlist + strlen (attlist ), "%s%s %s" ,
1731
+ ( k == 0 ) ? "" : ", " , attname , classname [k ]);
1732
+ free (classname [k ]);
1733
+ }
1734
+ }
1671
1735
1672
1736
if (!tablename || (!strcmp (indinfo [i ].indrelname ,tablename ))) {
1673
1737
@@ -1677,12 +1741,13 @@ dumpIndices(FILE* fout, IndInfo* indinfo, int numIndices,
1677
1741
indinfo [i ].indrelname ,
1678
1742
indinfo [i ].indamname );
1679
1743
if (funcname ) {
1680
- sprintf (q , "%s %s(%s) %s);\n" ,
1681
- q ,funcname , attname , indinfo [ i ]. indclassname );
1744
+ sprintf (q , "%s %s (%s) %s );\n" ,
1745
+ q , funcname , attlist , classname [ 0 ] );
1682
1746
free (funcname );
1747
+ free (classname [0 ]);
1683
1748
} else
1684
- sprintf (q , "%s %s %s );\n" ,
1685
- q ,attname , indinfo [ i ]. indclassname );
1749
+ sprintf (q , "%s %s );\n" ,
1750
+ q , attlist );
1686
1751
1687
1752
fputs (q ,fout );
1688
1753
}
0 commit comments