Skip to content

Commit 13fc2e4

Browse files
committed
Fix two error-recovery bugs in describeOneTableDetails(), and make the code
to dump sequence values cope with sequences outside the search path and/or having names that need quoting. No back-patch needed because these are new problems in 8.4. Kris Jurka (also a little bit of code beautification by tgl)
1 parent 88dd4b0 commit 13fc2e4

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/bin/psql/describe.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
1010
*
11-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.185 2008/09/23 09:20:38 heikki Exp $
11+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.186 2008/11/03 19:08:56 tgl Exp $
1212
*/
1313
#include "postgres_fe.h"
1414

@@ -826,6 +826,7 @@ describeOneTableDetails(const char *schemaname,
826826
PGresult *res = NULL;
827827
printTableOpt myopt = pset.popt.topt;
828828
printTableContent cont;
829+
bool printTableInitialized = false;
829830
int i;
830831
char *view_def = NULL;
831832
char *headers[6];
@@ -887,9 +888,10 @@ describeOneTableDetails(const char *schemaname,
887888
tableinfo.tablespace = (pset.sversion >= 80000) ?
888889
atooid(PQgetvalue(res, 0, 6)) : 0;
889890
PQclear(res);
891+
res = NULL;
890892

891893
/*
892-
* This is used to get the values of a sequence and store it in an
894+
* If it's a sequence, fetch its values and store into an
893895
* array that will be used later.
894896
*/
895897
if (tableinfo.relkind == 'S')
@@ -898,12 +900,14 @@ describeOneTableDetails(const char *schemaname,
898900

899901
#define SEQ_NUM_COLS 10
900902
printfPQExpBuffer(&buf,
901-
"SELECT sequence_name, last_value, \n"
902-
" start_value, increment_by, \n"
903-
" max_value, min_value, cache_value, \n"
904-
" log_cnt, is_cycled, is_called \n"
905-
"FROM \"%s\"",
906-
relationname);
903+
"SELECT sequence_name, last_value,\n"
904+
" start_value, increment_by,\n"
905+
" max_value, min_value, cache_value,\n"
906+
" log_cnt, is_cycled, is_called\n"
907+
"FROM %s",
908+
fmtId(schemaname));
909+
/* must be separate because fmtId isn't reentrant */
910+
appendPQExpBuffer(&buf, ".%s", fmtId(relationname));
907911

908912
result = PSQLexec(buf.data, false);
909913
if (!result)
@@ -1000,6 +1004,7 @@ describeOneTableDetails(const char *schemaname,
10001004
}
10011005

10021006
printTableInit(&cont, &myopt, title.data, cols, numrows);
1007+
printTableInitialized = true;
10031008

10041009
for (i = 0; i < cols; i++)
10051010
printTableAddHeader(&cont, headers[i], true, 'l');
@@ -1030,12 +1035,8 @@ describeOneTableDetails(const char *schemaname,
10301035

10311036
/* Type */
10321037
printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
1033-
1034-
/* A special 'Value' column for sequences */
1035-
if (tableinfo.relkind == 'S')
1036-
printTableAddCell(&cont, seq_values[i], false);
1037-
1038-
/* Extra: not null and default */
1038+
1039+
/* Modifiers: not null and default */
10391040
if (show_modifiers)
10401041
{
10411042
resetPQExpBuffer(&tmpbuf);
@@ -1057,10 +1058,15 @@ describeOneTableDetails(const char *schemaname,
10571058
printTableAddCell(&cont, modifiers[i], false);
10581059
}
10591060

1061+
/* Value: for sequences only */
1062+
if (tableinfo.relkind == 'S')
1063+
printTableAddCell(&cont, seq_values[i], false);
1064+
10601065
/* Storage and Description */
10611066
if (verbose)
10621067
{
10631068
char *storage = PQgetvalue(res, i, 5);
1069+
10641070
/* these strings are literal in our syntax, so not translated. */
10651071
printTableAddCell(&cont, (storage[0]=='p' ? "plain" :
10661072
(storage[0]=='m' ? "main" :
@@ -1593,7 +1599,8 @@ describeOneTableDetails(const char *schemaname,
15931599
error_return:
15941600

15951601
/* clean up */
1596-
printTableCleanup(&cont);
1602+
if (printTableInitialized)
1603+
printTableCleanup(&cont);
15971604
termPQExpBuffer(&buf);
15981605
termPQExpBuffer(&title);
15991606
termPQExpBuffer(&tmpbuf);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy