Skip to content

Commit a14707d

Browse files
committed
Show more-intuitive titles for psql commands \dt, \di, etc.
If exactly one relation type is requested in a command of the \dtisv family, say "tables", "indexes", etc instead of "relations". This should cover the majority of actual uses, without creating a huge number of new translatable strings. The error messages for no matching relations are adjusted as well. In passing, invent "pg_log_error_internal()" to be used for frontend error messages that don't seem to need translation, analogously to errmsg_internal() in the backend. The implementation is a bit cheesy, being just a macro to prevent xgettext from recognizing a trigger keyword. This won't avoid a useless gettext lookup cycle at runtime --- but surely we don't care about an extra microsecond or two in what's supposed to be a can't-happen case. I (tgl) also made "pg_fatal_internal()", though it's not used in this patch. Author: Greg Sabino Mullane <htamfids@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAKAnmm+7o93fQV-RFkGaN1QnP-0D4d3JTykD+cLueqjDMKdfag@mail.gmail.com
1 parent ee4667f commit a14707d

File tree

4 files changed

+88
-28
lines changed

4 files changed

+88
-28
lines changed

src/bin/psql/describe.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,14 +4011,18 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
40114011
bool showSeq = strchr(tabtypes, 's') != NULL;
40124012
bool showForeign = strchr(tabtypes, 'E') != NULL;
40134013

4014+
int ntypes;
40144015
PQExpBufferData buf;
40154016
PGresult *res;
40164017
printQueryOpt myopt = pset.popt;
40174018
int cols_so_far;
40184019
bool translate_columns[] = {false, false, true, false, false, false, false, false, false};
40194020

4020-
/* If tabtypes is empty, we default to \dtvmsE (but see also command.c) */
4021-
if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
4021+
/* Count the number of explicitly-requested relation types */
4022+
ntypes = showTables + showIndexes + showViews + showMatViews +
4023+
showSeq + showForeign;
4024+
/* If none, we default to \dtvmsE (but see also command.c) */
4025+
if (ntypes == 0)
40224026
showTables = showViews = showMatViews = showSeq = showForeign = true;
40234027

40244028
initPQExpBuffer(&buf);
@@ -4169,14 +4173,63 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
41694173
if (PQntuples(res) == 0 && !pset.quiet)
41704174
{
41714175
if (pattern)
4172-
pg_log_error("Did not find any relation named \"%s\".",
4173-
pattern);
4176+
{
4177+
if (ntypes != 1)
4178+
pg_log_error("Did not find any relations named \"%s\".",
4179+
pattern);
4180+
else if (showTables)
4181+
pg_log_error("Did not find any tables named \"%s\".",
4182+
pattern);
4183+
else if (showIndexes)
4184+
pg_log_error("Did not find any indexes named \"%s\".",
4185+
pattern);
4186+
else if (showViews)
4187+
pg_log_error("Did not find any views named \"%s\".",
4188+
pattern);
4189+
else if (showMatViews)
4190+
pg_log_error("Did not find any materialized views named \"%s\".",
4191+
pattern);
4192+
else if (showSeq)
4193+
pg_log_error("Did not find any sequences named \"%s\".",
4194+
pattern);
4195+
else if (showForeign)
4196+
pg_log_error("Did not find any foreign tables named \"%s\".",
4197+
pattern);
4198+
else /* should not get here */
4199+
pg_log_error_internal("Did not find any ??? named \"%s\".",
4200+
pattern);
4201+
}
41744202
else
4175-
pg_log_error("Did not find any relations.");
4203+
{
4204+
if (ntypes != 1)
4205+
pg_log_error("Did not find any relations.");
4206+
else if (showTables)
4207+
pg_log_error("Did not find any tables.");
4208+
else if (showIndexes)
4209+
pg_log_error("Did not find any indexes.");
4210+
else if (showViews)
4211+
pg_log_error("Did not find any views.");
4212+
else if (showMatViews)
4213+
pg_log_error("Did not find any materialized views.");
4214+
else if (showSeq)
4215+
pg_log_error("Did not find any sequences.");
4216+
else if (showForeign)
4217+
pg_log_error("Did not find any foreign tables.");
4218+
else /* should not get here */
4219+
pg_log_error_internal("Did not find any ??? relations.");
4220+
}
41764221
}
41774222
else
41784223
{
4179-
myopt.title = _("List of relations");
4224+
myopt.title =
4225+
(ntypes != 1) ? _("List of relations") :
4226+
(showTables) ? _("List of tables") :
4227+
(showIndexes) ? _("List of indexes") :
4228+
(showViews) ? _("List of views") :
4229+
(showMatViews) ? _("List of materialized views") :
4230+
(showSeq) ? _("List of sequences") :
4231+
(showForeign) ? _("List of foreign tables") :
4232+
"List of ???"; /* should not get here */
41804233
myopt.translate_header = true;
41814234
myopt.translate_columns = translate_columns;
41824235
myopt.n_translate_columns = lengthof(translate_columns);

src/include/common/logging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,11 @@ void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
153153
exit(1); \
154154
} while(0)
155155

156+
/*
157+
* Use these variants for "can't happen" cases, if it seems translating their
158+
* messages would be a waste of effort.
159+
*/
160+
#define pg_log_error_internal(...) pg_log_error(__VA_ARGS__)
161+
#define pg_fatal_internal(...) pg_fatal(__VA_ARGS__)
162+
156163
#endif /* COMMON_LOGGING_H */

src/test/regress/expected/dependency.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ FROM pg_type JOIN pg_class c ON typrelid = c.oid WHERE typname = 'deptest_t';
116116
RESET SESSION AUTHORIZATION;
117117
REASSIGN OWNED BY regress_dep_user1 TO regress_dep_user2;
118118
\dt deptest
119-
List of relations
119+
List of tables
120120
Schema | Name | Type | Owner
121121
--------+---------+-------+-------------------
122122
public | deptest | table | regress_dep_user2

src/test/regress/expected/psql.out

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,23 +3027,23 @@ Access method: heap
30273027
(4 rows)
30283028

30293029
\dt+
3030-
List of relations
3030+
List of tables
30313031
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
30323032
-----------------+---------------+-------+----------------------+-------------+---------------+---------+-------------
30333033
tableam_display | tbl_heap | table | regress_display_role | permanent | heap | 0 bytes |
30343034
tableam_display | tbl_heap_psql | table | regress_display_role | permanent | heap_psql | 0 bytes |
30353035
(2 rows)
30363036

30373037
\dm+
3038-
List of relations
3038+
List of materialized views
30393039
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
30403040
-----------------+--------------------+-------------------+----------------------+-------------+---------------+---------+-------------
30413041
tableam_display | mat_view_heap_psql | materialized view | regress_display_role | permanent | heap_psql | 0 bytes |
30423042
(1 row)
30433043

30443044
-- But not for views and sequences.
30453045
\dv+
3046-
List of relations
3046+
List of views
30473047
Schema | Name | Type | Owner | Persistence | Size | Description
30483048
-----------------+----------------+------+----------------------+-------------+---------+-------------
30493049
tableam_display | view_heap_psql | view | regress_display_role | permanent | 0 bytes |
@@ -6244,7 +6244,7 @@ List of access methods
62446244
(0 rows)
62456245

62466246
\dt "no.such.table.relation"
6247-
List of relations
6247+
List of tables
62486248
Schema | Name | Type | Owner
62496249
--------+------+------+-------
62506250
(0 rows)
@@ -6316,31 +6316,31 @@ List of access methods
63166316
(0 rows)
63176317

63186318
\di "no.such.index.relation"
6319-
List of relations
6319+
List of indexes
63206320
Schema | Name | Type | Owner | Table
63216321
--------+------+------+-------+-------
63226322
(0 rows)
63236323

63246324
\dm "no.such.materialized.view"
6325-
List of relations
6325+
List of materialized views
63266326
Schema | Name | Type | Owner
63276327
--------+------+------+-------
63286328
(0 rows)
63296329

63306330
\ds "no.such.relation"
6331-
List of relations
6331+
List of sequences
63326332
Schema | Name | Type | Owner
63336333
--------+------+------+-------
63346334
(0 rows)
63356335

63366336
\dt "no.such.relation"
6337-
List of relations
6337+
List of tables
63386338
Schema | Name | Type | Owner
63396339
--------+------+------+-------
63406340
(0 rows)
63416341

63426342
\dv "no.such.relation"
6343-
List of relations
6343+
List of views
63446344
Schema | Name | Type | Owner
63456345
--------+------+------+-------
63466346
(0 rows)
@@ -6474,7 +6474,7 @@ List of schemas
64746474
\dA "no.such.schema"."no.such.access.method"
64756475
improper qualified name (too many dotted names): "no.such.schema"."no.such.access.method"
64766476
\dt "no.such.schema"."no.such.table.relation"
6477-
List of relations
6477+
List of tables
64786478
Schema | Name | Type | Owner
64796479
--------+------+------+-------
64806480
(0 rows)
@@ -6526,31 +6526,31 @@ improper qualified name (too many dotted names): "no.such.schema"."no.such.table
65266526
(0 rows)
65276527

65286528
\di "no.such.schema"."no.such.index.relation"
6529-
List of relations
6529+
List of indexes
65306530
Schema | Name | Type | Owner | Table
65316531
--------+------+------+-------+-------
65326532
(0 rows)
65336533

65346534
\dm "no.such.schema"."no.such.materialized.view"
6535-
List of relations
6535+
List of materialized views
65366536
Schema | Name | Type | Owner
65376537
--------+------+------+-------
65386538
(0 rows)
65396539

65406540
\ds "no.such.schema"."no.such.relation"
6541-
List of relations
6541+
List of sequences
65426542
Schema | Name | Type | Owner
65436543
--------+------+------+-------
65446544
(0 rows)
65456545

65466546
\dt "no.such.schema"."no.such.relation"
6547-
List of relations
6547+
List of tables
65486548
Schema | Name | Type | Owner
65496549
--------+------+------+-------
65506550
(0 rows)
65516551

65526552
\dv "no.such.schema"."no.such.relation"
6553-
List of relations
6553+
List of views
65546554
Schema | Name | Type | Owner
65556555
--------+------+------+-------
65566556
(0 rows)
@@ -6641,7 +6641,7 @@ improper qualified name (too many dotted names): "no.such.schema"."no.such.insta
66416641
improper qualified name (too many dotted names): "no.such.schema"."no.such.event.trigger"
66426642
-- again, but with current database and dotted schema qualifications.
66436643
\dt regression."no.such.schema"."no.such.table.relation"
6644-
List of relations
6644+
List of tables
66456645
Schema | Name | Type | Owner
66466646
--------+------+------+-------
66476647
(0 rows)
@@ -6677,31 +6677,31 @@ improper qualified name (too many dotted names): "no.such.schema"."no.such.event
66776677
(0 rows)
66786678

66796679
\di regression."no.such.schema"."no.such.index.relation"
6680-
List of relations
6680+
List of indexes
66816681
Schema | Name | Type | Owner | Table
66826682
--------+------+------+-------+-------
66836683
(0 rows)
66846684

66856685
\dm regression."no.such.schema"."no.such.materialized.view"
6686-
List of relations
6686+
List of materialized views
66876687
Schema | Name | Type | Owner
66886688
--------+------+------+-------
66896689
(0 rows)
66906690

66916691
\ds regression."no.such.schema"."no.such.relation"
6692-
List of relations
6692+
List of sequences
66936693
Schema | Name | Type | Owner
66946694
--------+------+------+-------
66956695
(0 rows)
66966696

66976697
\dt regression."no.such.schema"."no.such.relation"
6698-
List of relations
6698+
List of tables
66996699
Schema | Name | Type | Owner
67006700
--------+------+------+-------
67016701
(0 rows)
67026702

67036703
\dv regression."no.such.schema"."no.such.relation"
6704-
List of relations
6704+
List of views
67056705
Schema | Name | Type | Owner
67066706
--------+------+------+-------
67076707
(0 rows)

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