@@ -1380,8 +1380,6 @@ describeOneTableDetails(const char *schemaname,
1380
1380
int i ;
1381
1381
char * view_def = NULL ;
1382
1382
char * headers [11 ];
1383
- char * * seq_values = NULL ;
1384
- char * * ptr ;
1385
1383
PQExpBufferData title ;
1386
1384
PQExpBufferData tmpbuf ;
1387
1385
int cols ;
@@ -1563,27 +1561,125 @@ describeOneTableDetails(const char *schemaname,
1563
1561
res = NULL ;
1564
1562
1565
1563
/*
1566
- * If it's a sequence, fetch its values and store into an array that will
1567
- * be used later.
1564
+ * If it's a sequence, deal with it here separately.
1568
1565
*/
1569
1566
if (tableinfo .relkind == RELKIND_SEQUENCE )
1570
1567
{
1571
- printfPQExpBuffer (& buf , "SELECT * FROM %s" , fmtId (schemaname ));
1572
- /* must be separate because fmtId isn't reentrant */
1573
- appendPQExpBuffer (& buf , ".%s;" , fmtId (relationname ));
1568
+ PGresult * result = NULL ;
1569
+ printQueryOpt myopt = pset .popt ;
1570
+ char * footers [2 ] = {NULL , NULL };
1571
+
1572
+ if (pset .sversion >= 100000 )
1573
+ {
1574
+ printfPQExpBuffer (& buf ,
1575
+ "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n"
1576
+ " seqstart AS \"%s\",\n"
1577
+ " seqmin AS \"%s\",\n"
1578
+ " seqmax AS \"%s\",\n"
1579
+ " seqincrement AS \"%s\",\n"
1580
+ " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n"
1581
+ " seqcache AS \"%s\"\n" ,
1582
+ gettext_noop ("Type" ),
1583
+ gettext_noop ("Start" ),
1584
+ gettext_noop ("Minimum" ),
1585
+ gettext_noop ("Maximum" ),
1586
+ gettext_noop ("Increment" ),
1587
+ gettext_noop ("yes" ),
1588
+ gettext_noop ("no" ),
1589
+ gettext_noop ("Cycles?" ),
1590
+ gettext_noop ("Cache" ));
1591
+ appendPQExpBuffer (& buf ,
1592
+ "FROM pg_catalog.pg_sequence\n"
1593
+ "WHERE seqrelid = '%s';" ,
1594
+ oid );
1595
+ }
1596
+ else
1597
+ {
1598
+ printfPQExpBuffer (& buf ,
1599
+ "SELECT pg_catalog.format_type('bigint'::regtype, NULL) AS \"%s\",\n"
1600
+ " start_value AS \"%s\",\n"
1601
+ " min_value AS \"%s\",\n"
1602
+ " max_value AS \"%s\",\n"
1603
+ " increment_by AS \"%s\",\n"
1604
+ " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n"
1605
+ " cache_value AS \"%s\"\n" ,
1606
+ gettext_noop ("Type" ),
1607
+ gettext_noop ("Start" ),
1608
+ gettext_noop ("Minimum" ),
1609
+ gettext_noop ("Maximum" ),
1610
+ gettext_noop ("Increment" ),
1611
+ gettext_noop ("yes" ),
1612
+ gettext_noop ("no" ),
1613
+ gettext_noop ("Cycles?" ),
1614
+ gettext_noop ("Cache" ));
1615
+ appendPQExpBuffer (& buf , "FROM %s" , fmtId (schemaname ));
1616
+ /* must be separate because fmtId isn't reentrant */
1617
+ appendPQExpBuffer (& buf , ".%s;" , fmtId (relationname ));
1618
+ }
1574
1619
1575
1620
res = PSQLexec (buf .data );
1576
1621
if (!res )
1577
1622
goto error_return ;
1578
1623
1579
- seq_values = pg_malloc ((PQnfields (res ) + 1 ) * sizeof (* seq_values ));
1624
+ /* Footer information about a sequence */
1625
+
1626
+ /* Get the column that owns this sequence */
1627
+ printfPQExpBuffer (& buf , "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
1628
+ "\n pg_catalog.quote_ident(relname) || '.' ||"
1629
+ "\n pg_catalog.quote_ident(attname),"
1630
+ "\n d.deptype"
1631
+ "\nFROM pg_catalog.pg_class c"
1632
+ "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
1633
+ "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
1634
+ "\nINNER JOIN pg_catalog.pg_attribute a ON ("
1635
+ "\n a.attrelid=c.oid AND"
1636
+ "\n a.attnum=d.refobjsubid)"
1637
+ "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
1638
+ "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1639
+ "\n AND d.objid='%s'"
1640
+ "\n AND d.deptype IN ('a', 'i')" ,
1641
+ oid );
1642
+
1643
+ result = PSQLexec (buf .data );
1644
+
1645
+ /*
1646
+ * If we get no rows back, don't show anything (obviously). We should
1647
+ * never get more than one row back, but if we do, just ignore it and
1648
+ * don't print anything.
1649
+ */
1650
+ if (!result )
1651
+ goto error_return ;
1652
+ else if (PQntuples (result ) == 1 )
1653
+ {
1654
+ switch (PQgetvalue (result , 0 , 1 )[0 ])
1655
+ {
1656
+ case 'a' :
1657
+ footers [0 ] = psprintf (_ ("Owned by: %s" ),
1658
+ PQgetvalue (result , 0 , 0 ));
1659
+ break ;
1660
+ case 'i' :
1661
+ footers [0 ] = psprintf (_ ("Sequence for identity column: %s" ),
1662
+ PQgetvalue (result , 0 , 0 ));
1663
+ break ;
1664
+ }
1665
+ }
1666
+ PQclear (result );
1667
+
1668
+ printfPQExpBuffer (& title , _ ("Sequence \"%s.%s\"" ),
1669
+ schemaname , relationname );
1580
1670
1581
- for (i = 0 ; i < PQnfields (res ); i ++ )
1582
- seq_values [i ] = pg_strdup (PQgetvalue (res , 0 , i ));
1583
- seq_values [i ] = NULL ;
1671
+ myopt .footers = footers ;
1672
+ myopt .topt .default_footer = false;
1673
+ myopt .title = title .data ;
1674
+ myopt .translate_header = true;
1584
1675
1585
- PQclear (res );
1586
- res = NULL ;
1676
+ printQuery (res , & myopt , pset .queryFout , false, pset .logfile );
1677
+
1678
+ if (footers [0 ])
1679
+ free (footers [0 ]);
1680
+
1681
+ retval = true;
1682
+ goto error_return ; /* not an error, just return early */
1587
1683
}
1588
1684
1589
1685
/*
@@ -1667,10 +1763,6 @@ describeOneTableDetails(const char *schemaname,
1667
1763
printfPQExpBuffer (& title , _ ("Materialized view \"%s.%s\"" ),
1668
1764
schemaname , relationname );
1669
1765
break ;
1670
- case RELKIND_SEQUENCE :
1671
- printfPQExpBuffer (& title , _ ("Sequence \"%s.%s\"" ),
1672
- schemaname , relationname );
1673
- break ;
1674
1766
case RELKIND_INDEX :
1675
1767
if (tableinfo .relpersistence == 'u' )
1676
1768
printfPQExpBuffer (& title , _ ("Unlogged index \"%s.%s\"" ),
@@ -1729,9 +1821,6 @@ describeOneTableDetails(const char *schemaname,
1729
1821
show_column_details = true;
1730
1822
}
1731
1823
1732
- if (tableinfo .relkind == RELKIND_SEQUENCE )
1733
- headers [cols ++ ] = gettext_noop ("Value" );
1734
-
1735
1824
if (tableinfo .relkind == RELKIND_INDEX )
1736
1825
headers [cols ++ ] = gettext_noop ("Definition" );
1737
1826
@@ -1814,10 +1903,6 @@ describeOneTableDetails(const char *schemaname,
1814
1903
printTableAddCell (& cont , default_str , false, false);
1815
1904
}
1816
1905
1817
- /* Value: for sequences only */
1818
- if (tableinfo .relkind == RELKIND_SEQUENCE )
1819
- printTableAddCell (& cont , seq_values [i ], false, false);
1820
-
1821
1906
/* Expression for index column */
1822
1907
if (tableinfo .relkind == RELKIND_INDEX )
1823
1908
printTableAddCell (& cont , PQgetvalue (res , i , 7 ), false, false);
@@ -2030,55 +2115,6 @@ describeOneTableDetails(const char *schemaname,
2030
2115
2031
2116
PQclear (result );
2032
2117
}
2033
- else if (tableinfo .relkind == RELKIND_SEQUENCE )
2034
- {
2035
- /* Footer information about a sequence */
2036
- PGresult * result = NULL ;
2037
-
2038
- /* Get the column that owns this sequence */
2039
- printfPQExpBuffer (& buf , "SELECT pg_catalog.quote_ident(nspname) || '.' ||"
2040
- "\n pg_catalog.quote_ident(relname) || '.' ||"
2041
- "\n pg_catalog.quote_ident(attname),"
2042
- "\n d.deptype"
2043
- "\nFROM pg_catalog.pg_class c"
2044
- "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid"
2045
- "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace"
2046
- "\nINNER JOIN pg_catalog.pg_attribute a ON ("
2047
- "\n a.attrelid=c.oid AND"
2048
- "\n a.attnum=d.refobjsubid)"
2049
- "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
2050
- "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
2051
- "\n AND d.objid='%s'"
2052
- "\n AND d.deptype IN ('a', 'i')" ,
2053
- oid );
2054
-
2055
- result = PSQLexec (buf .data );
2056
- if (!result )
2057
- goto error_return ;
2058
- else if (PQntuples (result ) == 1 )
2059
- {
2060
- switch (PQgetvalue (result , 0 , 1 )[0 ])
2061
- {
2062
- case 'a' :
2063
- printfPQExpBuffer (& buf , _ ("Owned by: %s" ),
2064
- PQgetvalue (result , 0 , 0 ));
2065
- printTableAddFooter (& cont , buf .data );
2066
- break ;
2067
- case 'i' :
2068
- printfPQExpBuffer (& buf , _ ("Sequence for identity column: %s" ),
2069
- PQgetvalue (result , 0 , 0 ));
2070
- printTableAddFooter (& cont , buf .data );
2071
- break ;
2072
- }
2073
- }
2074
-
2075
- /*
2076
- * If we get no rows back, don't show anything (obviously). We should
2077
- * never get more than one row back, but if we do, just ignore it and
2078
- * don't print anything.
2079
- */
2080
- PQclear (result );
2081
- }
2082
2118
else if (tableinfo .relkind == RELKIND_RELATION ||
2083
2119
tableinfo .relkind == RELKIND_MATVIEW ||
2084
2120
tableinfo .relkind == RELKIND_FOREIGN_TABLE ||
@@ -2963,13 +2999,6 @@ describeOneTableDetails(const char *schemaname,
2963
2999
termPQExpBuffer (& title );
2964
3000
termPQExpBuffer (& tmpbuf );
2965
3001
2966
- if (seq_values )
2967
- {
2968
- for (ptr = seq_values ; * ptr ; ptr ++ )
2969
- free (* ptr );
2970
- free (seq_values );
2971
- }
2972
-
2973
3002
if (view_def )
2974
3003
free (view_def );
2975
3004
0 commit comments