2
* psql - the PostgreSQL interactive terminal
4
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
6
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.111 2005-01-01 05:43:08 momjian Exp $
8
#include "postgres_fe.h"
12
#include "pqexpbuffer.h"
17
#include "variables.h"
23
* mbvalidate() is used in function describeOneTableDetails() to make sure
24
* all characters of the cells will be printed to the DOS console in a
31
#define _(x) gettext((x))
33
static bool describeOneTableDetails(const char *schemaname,
34
const char *relationname,
37
static void processNamePattern(PQExpBuffer buf, const char *pattern,
38
bool have_where, bool force_escape,
39
const char *schemavar, const char *namevar,
40
const char *altnamevar, const char *visibilityrule);
42
static void add_tablespace_footer(char relkind, Oid tablespace,
43
char **footers, int *count, PQExpBufferData buf);
46
* Handlers for various slash commands displaying some sort of list
47
* of things in the database.
49
* If you add something here, try to format the query to look nice in -E output.
55
* Takes an optional regexp to select particular aggregates
58
describeAggregates(const char *pattern, bool verbose)
62
printQueryOpt myopt = pset.popt;
64
initPQExpBuffer(&buf);
67
* There are two kinds of aggregates: ones that work on particular
68
* types and ones that work on all (denoted by input type = "any")
70
printfPQExpBuffer(&buf,
71
"SELECT n.nspname as \"%s\",\n"
72
" p.proname AS \"%s\",\n"
73
" CASE p.proargtypes[0]\n"
74
" WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype\n"
75
" THEN CAST('%s' AS pg_catalog.text)\n"
76
" ELSE pg_catalog.format_type(p.proargtypes[0], NULL)\n"
78
" pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"
79
"FROM pg_catalog.pg_proc p\n"
80
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
82
_("Schema"), _("Name"), _("(all types)"),
83
_("Data type"), _("Description"));
85
processNamePattern(&buf, pattern, true, false,
86
"n.nspname", "p.proname", NULL,
87
"pg_catalog.pg_function_is_visible(p.oid)");
89
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
91
res = PSQLexec(buf.data, false);
92
termPQExpBuffer(&buf);
96
myopt.nullPrint = NULL;
97
myopt.title = _("List of aggregate functions");
99
printQuery(res, &myopt, pset.queryFout);
106
* Takes an optional regexp to select particular tablespaces
109
describeTablespaces(const char *pattern, bool verbose)
113
printQueryOpt myopt = pset.popt;
115
if (pset.sversion < 80000)
117
fprintf(stderr, _("The server version (%d) does not support tablespaces.\n"),
122
initPQExpBuffer(&buf);
124
printfPQExpBuffer(&buf,
125
"SELECT spcname AS \"%s\",\n"
126
" pg_catalog.pg_get_userbyid(spcowner) AS \"%s\",\n"
127
" spclocation AS \"%s\"",
128
_("Name"), _("Owner"), _("Location"));
131
appendPQExpBuffer(&buf,
132
",\n spcacl as \"%s\"",
133
_("Access privileges"));
135
appendPQExpBuffer(&buf,
136
"\nFROM pg_catalog.pg_tablespace\n");
138
processNamePattern(&buf, pattern, false, false,
139
NULL, "spcname", NULL,
142
appendPQExpBuffer(&buf, "ORDER BY 1;");
144
res = PSQLexec(buf.data, false);
145
termPQExpBuffer(&buf);
149
myopt.nullPrint = NULL;
150
myopt.title = _("List of tablespaces");
152
printQuery(res, &myopt, pset.queryFout);
160
* Takes an optional regexp to select particular functions
163
describeFunctions(const char *pattern, bool verbose)
167
printQueryOpt myopt = pset.popt;
169
initPQExpBuffer(&buf);
171
printfPQExpBuffer(&buf,
172
"SELECT n.nspname as \"%s\",\n"
173
" p.proname as \"%s\",\n"
174
" CASE WHEN p.proretset THEN 'setof ' ELSE '' END ||\n"
175
" pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
176
" pg_catalog.oidvectortypes(p.proargtypes) as \"%s\"",
177
_("Schema"), _("Name"), _("Result data type"),
178
_("Argument data types"));
181
appendPQExpBuffer(&buf,
182
",\n u.usename as \"%s\",\n"
183
" l.lanname as \"%s\",\n"
184
" p.prosrc as \"%s\",\n"
185
" pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"",
186
_("Owner"), _("Language"),
187
_("Source code"), _("Description"));
190
appendPQExpBuffer(&buf,
191
"\nFROM pg_catalog.pg_proc p"
192
"\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n");
194
appendPQExpBuffer(&buf,
195
"\nFROM pg_catalog.pg_proc p"
196
"\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace"
197
"\n LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang"
198
"\n LEFT JOIN pg_catalog.pg_user u ON u.usesysid = p.proowner\n");
201
* we skip in/out funcs by excluding functions that take or return
204
appendPQExpBuffer(&buf,
205
"WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
206
" AND p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
207
" AND NOT p.proisagg\n");
209
processNamePattern(&buf, pattern, true, false,
210
"n.nspname", "p.proname", NULL,
211
"pg_catalog.pg_function_is_visible(p.oid)");
213
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
215
res = PSQLexec(buf.data, false);
216
termPQExpBuffer(&buf);
220
myopt.nullPrint = NULL;
221
myopt.title = _("List of functions");
223
printQuery(res, &myopt, pset.queryFout);
236
describeTypes(const char *pattern, bool verbose)
240
printQueryOpt myopt = pset.popt;
242
initPQExpBuffer(&buf);
244
printfPQExpBuffer(&buf,
245
"SELECT n.nspname as \"%s\",\n"
246
" pg_catalog.format_type(t.oid, NULL) AS \"%s\",\n",
247
_("Schema"), _("Name"));
249
appendPQExpBuffer(&buf,
250
" t.typname AS \"%s\",\n"
251
" CASE WHEN t.typrelid != 0\n"
252
" THEN CAST('tuple' AS pg_catalog.text)\n"
253
" WHEN t.typlen < 0\n"
254
" THEN CAST('var' AS pg_catalog.text)\n"
255
" ELSE CAST(t.typlen AS pg_catalog.text)\n"
257
_("Internal name"), _("Size"));
258
appendPQExpBuffer(&buf,
259
" pg_catalog.obj_description(t.oid, 'pg_type') as \"%s\"\n",
262
appendPQExpBuffer(&buf, "FROM pg_catalog.pg_type t\n"
263
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n");
266
* do not include array types (start with underscore); do not include
267
* complex types (typrelid!=0) unless they are standalone composite
270
appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
271
appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c "
272
"WHERE c.oid = t.typrelid)) ");
273
appendPQExpBuffer(&buf, "AND t.typname !~ '^_'\n");
275
/* Match name pattern against either internal or external name */
276
processNamePattern(&buf, pattern, true, false,
277
"n.nspname", "t.typname",
278
"pg_catalog.format_type(t.oid, NULL)",
279
"pg_catalog.pg_type_is_visible(t.oid)");
281
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
283
res = PSQLexec(buf.data, false);
284
termPQExpBuffer(&buf);
288
myopt.nullPrint = NULL;
289
myopt.title = _("List of data types");
291
printQuery(res, &myopt, pset.queryFout);
302
describeOperators(const char *pattern)
306
printQueryOpt myopt = pset.popt;
308
initPQExpBuffer(&buf);
310
printfPQExpBuffer(&buf,
311
"SELECT n.nspname as \"%s\",\n"
312
" o.oprname AS \"%s\",\n"
313
" CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS \"%s\",\n"
314
" CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS \"%s\",\n"
315
" pg_catalog.format_type(o.oprresult, NULL) AS \"%s\",\n"
316
" coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),\n"
317
" pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS \"%s\"\n"
318
"FROM pg_catalog.pg_operator o\n"
319
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
320
_("Schema"), _("Name"),
321
_("Left arg type"), _("Right arg type"),
322
_("Result type"), _("Description"));
324
processNamePattern(&buf, pattern, false, true,
325
"n.nspname", "o.oprname", NULL,
326
"pg_catalog.pg_operator_is_visible(o.oid)");
328
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
330
res = PSQLexec(buf.data, false);
331
termPQExpBuffer(&buf);
335
myopt.nullPrint = NULL;
336
myopt.title = _("List of operators");
338
printQuery(res, &myopt, pset.queryFout);
348
* for \l, \list, and -l switch
351
listAllDbs(bool verbose)
355
printQueryOpt myopt = pset.popt;
357
initPQExpBuffer(&buf);
359
printfPQExpBuffer(&buf,
360
"SELECT d.datname as \"%s\",\n"
361
" u.usename as \"%s\"",
362
_("Name"), _("Owner"));
363
appendPQExpBuffer(&buf,
364
",\n pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\"",
367
appendPQExpBuffer(&buf,
368
",\n pg_catalog.obj_description(d.oid, 'pg_database') as \"%s\"",
370
appendPQExpBuffer(&buf,
371
"\nFROM pg_catalog.pg_database d"
372
"\n LEFT JOIN pg_catalog.pg_user u ON d.datdba = u.usesysid\n"
375
res = PSQLexec(buf.data, false);
376
termPQExpBuffer(&buf);
380
myopt.nullPrint = NULL;
381
myopt.title = _("List of databases");
383
printQuery(res, &myopt, pset.queryFout);
391
* List Tables Grant/Revoke Permissions
392
* \z (now also \dp -- perhaps more mnemonic)
395
permissionsList(const char *pattern)
399
printQueryOpt myopt = pset.popt;
401
initPQExpBuffer(&buf);
404
* we ignore indexes and toast tables since they have no meaningful
407
printfPQExpBuffer(&buf,
408
"SELECT n.nspname as \"%s\",\n"
409
" c.relname as \"%s\",\n"
410
" CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'S' THEN '%s' END as \"%s\",\n"
411
" c.relacl as \"%s\"\n"
412
"FROM pg_catalog.pg_class c\n"
413
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
414
"WHERE c.relkind IN ('r', 'v', 'S')\n",
415
_("Schema"), _("Name"), _("table"), _("view"), _("sequence"), _("Type"), _("Access privileges"));
418
* Unless a schema pattern is specified, we suppress system and temp
419
* tables, since they normally aren't very interesting from a
420
* permissions point of view. You can see 'em by explicit request
421
* though, eg with \z pg_catalog.*
423
processNamePattern(&buf, pattern, true, false,
424
"n.nspname", "c.relname", NULL,
425
"pg_catalog.pg_table_is_visible(c.oid) AND n.nspname !~ '^pg_'");
427
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
429
res = PSQLexec(buf.data, false);
432
termPQExpBuffer(&buf);
436
myopt.nullPrint = NULL;
437
printfPQExpBuffer(&buf, _("Access privileges for database \"%s\""), PQdb(pset.db));
438
myopt.title = buf.data;
440
printQuery(res, &myopt, pset.queryFout);
442
termPQExpBuffer(&buf);
450
* Get object comments
454
* Note: This only lists things that actually have a description. For complete
455
* lists of things, there are other \d? commands.
458
objectDescription(const char *pattern)
462
printQueryOpt myopt = pset.popt;
464
initPQExpBuffer(&buf);
466
appendPQExpBuffer(&buf,
467
"SELECT DISTINCT tt.nspname AS \"%s\", tt.name AS \"%s\", tt.object AS \"%s\", d.description AS \"%s\"\n"
469
_("Schema"), _("Name"), _("Object"), _("Description"));
471
/* Aggregate descriptions */
472
appendPQExpBuffer(&buf,
473
" SELECT p.oid as oid, p.tableoid as tableoid,\n"
474
" n.nspname as nspname,\n"
475
" CAST(p.proname AS pg_catalog.text) as name,"
476
" CAST('%s' AS pg_catalog.text) as object\n"
477
" FROM pg_catalog.pg_proc p\n"
478
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
479
" WHERE p.proisagg\n",
481
processNamePattern(&buf, pattern, true, false,
482
"n.nspname", "p.proname", NULL,
483
"pg_catalog.pg_function_is_visible(p.oid)");
485
/* Function descriptions (except in/outs for datatypes) */
486
appendPQExpBuffer(&buf,
488
" SELECT p.oid as oid, p.tableoid as tableoid,\n"
489
" n.nspname as nspname,\n"
490
" CAST(p.proname AS pg_catalog.text) as name,"
491
" CAST('%s' AS pg_catalog.text) as object\n"
492
" FROM pg_catalog.pg_proc p\n"
493
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"
495
" WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
496
" AND p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype\n"
497
" AND NOT p.proisagg\n",
499
processNamePattern(&buf, pattern, true, false,
500
"n.nspname", "p.proname", NULL,
501
"pg_catalog.pg_function_is_visible(p.oid)");
503
/* Operator descriptions (only if operator has its own comment) */
504
appendPQExpBuffer(&buf,
506
" SELECT o.oid as oid, o.tableoid as tableoid,\n"
507
" n.nspname as nspname,\n"
508
" CAST(o.oprname AS pg_catalog.text) as name,"
509
" CAST('%s' AS pg_catalog.text) as object\n"
510
" FROM pg_catalog.pg_operator o\n"
511
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace\n",
513
processNamePattern(&buf, pattern, false, false,
514
"n.nspname", "o.oprname", NULL,
515
"pg_catalog.pg_operator_is_visible(o.oid)");
517
/* Type description */
518
appendPQExpBuffer(&buf,
520
" SELECT t.oid as oid, t.tableoid as tableoid,\n"
521
" n.nspname as nspname,\n"
522
" pg_catalog.format_type(t.oid, NULL) as name,"
523
" CAST('%s' AS pg_catalog.text) as object\n"
524
" FROM pg_catalog.pg_type t\n"
525
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n",
527
processNamePattern(&buf, pattern, false, false,
528
"n.nspname", "pg_catalog.format_type(t.oid, NULL)", NULL,
529
"pg_catalog.pg_type_is_visible(t.oid)");
531
/* Relation (tables, views, indexes, sequences) descriptions */
532
appendPQExpBuffer(&buf,
534
" SELECT c.oid as oid, c.tableoid as tableoid,\n"
535
" n.nspname as nspname,\n"
536
" CAST(c.relname AS pg_catalog.text) as name,\n"
538
" CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' END"
539
" AS pg_catalog.text) as object\n"
540
" FROM pg_catalog.pg_class c\n"
541
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
542
" WHERE c.relkind IN ('r', 'v', 'i', 'S')\n",
543
_("table"), _("view"), _("index"), _("sequence"));
544
processNamePattern(&buf, pattern, true, false,
545
"n.nspname", "c.relname", NULL,
546
"pg_catalog.pg_table_is_visible(c.oid)");
548
/* Rule description (ignore rules for views) */
549
appendPQExpBuffer(&buf,
551
" SELECT r.oid as oid, r.tableoid as tableoid,\n"
552
" n.nspname as nspname,\n"
553
" CAST(r.rulename AS pg_catalog.text) as name,"
554
" CAST('%s' AS pg_catalog.text) as object\n"
555
" FROM pg_catalog.pg_rewrite r\n"
556
" JOIN pg_catalog.pg_class c ON c.oid = r.ev_class\n"
557
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
558
" WHERE r.rulename != '_RETURN'\n",
560
/* XXX not sure what to do about visibility rule here? */
561
processNamePattern(&buf, pattern, true, false,
562
"n.nspname", "r.rulename", NULL,
563
"pg_catalog.pg_table_is_visible(c.oid)");
565
/* Trigger description */
566
appendPQExpBuffer(&buf,
568
" SELECT t.oid as oid, t.tableoid as tableoid,\n"
569
" n.nspname as nspname,\n"
570
" CAST(t.tgname AS pg_catalog.text) as name,"
571
" CAST('%s' AS pg_catalog.text) as object\n"
572
" FROM pg_catalog.pg_trigger t\n"
573
" JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid\n"
574
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n",
576
/* XXX not sure what to do about visibility rule here? */
577
processNamePattern(&buf, pattern, false, false,
578
"n.nspname", "t.tgname", NULL,
579
"pg_catalog.pg_table_is_visible(c.oid)");
581
appendPQExpBuffer(&buf,
583
" JOIN pg_catalog.pg_description d ON (tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0)\n");
585
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
587
res = PSQLexec(buf.data, false);
588
termPQExpBuffer(&buf);
592
myopt.nullPrint = NULL;
593
myopt.title = _("Object descriptions");
595
printQuery(res, &myopt, pset.queryFout);
604
* describeTableDetails (for \d)
606
* This routine finds the tables to be displayed, and calls
607
* describeOneTableDetails for each one.
609
* verbose: if true, this is \d+
612
describeTableDetails(const char *pattern, bool verbose)
618
initPQExpBuffer(&buf);
620
printfPQExpBuffer(&buf,
624
"FROM pg_catalog.pg_class c\n"
625
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
627
processNamePattern(&buf, pattern, false, false,
628
"n.nspname", "c.relname", NULL,
629
"pg_catalog.pg_table_is_visible(c.oid)");
631
appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
633
res = PSQLexec(buf.data, false);
634
termPQExpBuffer(&buf);
638
if (PQntuples(res) == 0)
641
fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
647
for (i = 0; i < PQntuples(res); i++)
653
oid = PQgetvalue(res, i, 0);
654
nspname = PQgetvalue(res, i, 1);
655
relname = PQgetvalue(res, i, 2);
657
if (!describeOneTableDetails(nspname, relname, oid, verbose))
669
* describeOneTableDetails (for \d)
671
* Unfortunately, the information presented here is so complicated that it
672
* cannot be done in a single query. So we have to assemble the printed table
673
* by hand and pass it to the underlying printTable() function.
676
describeOneTableDetails(const char *schemaname,
677
const char *relationname,
682
PGresult *res = NULL;
683
printTableOpt myopt = pset.popt.topt;
685
char *view_def = NULL;
686
const char *headers[5];
688
char **footers = NULL;
690
PQExpBufferData title;
691
PQExpBufferData tmpbuf;
704
bool show_modifiers = false;
709
initPQExpBuffer(&buf);
710
initPQExpBuffer(&title);
711
initPQExpBuffer(&tmpbuf);
713
/* Get general table info */
714
printfPQExpBuffer(&buf,
715
"SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, \n"
717
"FROM pg_catalog.pg_class WHERE oid = '%s'",
718
pset.sversion >= 80000 ? ", reltablespace" : "",
720
res = PSQLexec(buf.data, false);
724
/* Did we get anything? */
725
if (PQntuples(res) == 0)
728
fprintf(stderr, _("Did not find any relation with OID %s.\n"),
733
/* FIXME: check for null pointers here? */
734
tableinfo.checks = atoi(PQgetvalue(res, 0, 2));
735
tableinfo.triggers = atoi(PQgetvalue(res, 0, 3));
736
tableinfo.relkind = *(PQgetvalue(res, 0, 1));
737
tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 0), "t") == 0;
738
tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
739
tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
740
tableinfo.tablespace = (pset.sversion >= 80000) ?
741
atooid(PQgetvalue(res, 0, 6)) : 0;
744
headers[0] = _("Column");
745
headers[1] = _("Type");
748
if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
750
show_modifiers = true;
752
headers[cols - 1] = _("Modifiers");
758
headers[cols - 1] = _("Description");
761
headers[cols] = NULL;
763
/* Get column info (index requires additional checks) */
764
printfPQExpBuffer(&buf, "SELECT a.attname,");
765
appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
766
"\n (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d"
767
"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
768
"\n a.attnotnull, a.attnum");
770
appendPQExpBuffer(&buf, ", pg_catalog.col_description(a.attrelid, a.attnum)");
771
appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
772
if (tableinfo.relkind == 'i')
773
appendPQExpBuffer(&buf, ", pg_catalog.pg_index i");
774
appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
775
if (tableinfo.relkind == 'i')
776
appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
777
appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
779
res = PSQLexec(buf.data, false);
782
numrows = PQntuples(res);
784
/* Check if table is a view */
785
if (tableinfo.relkind == 'v')
789
printfPQExpBuffer(&buf, "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid, true)", oid);
790
result = PSQLexec(buf.data, false);
794
if (PQntuples(result) > 0)
795
view_def = pg_strdup(PQgetvalue(result, 0, 0));
800
/* Generate table cells to be printed */
801
/* note: initialize all cells[] to NULL in case of error exit */
802
cells = pg_malloc_zero((numrows * cols + 1) * sizeof(*cells));
804
for (i = 0; i < numrows; i++)
808
cells[i * cols + 0] = mbvalidate(PQgetvalue(res, i, 0), myopt.encoding);
810
cells[i * cols + 0] = PQgetvalue(res, i, 0); /* don't free this
816
cells[i * cols + 1] = mbvalidate(PQgetvalue(res, i, 1), myopt.encoding);
818
cells[i * cols + 1] = PQgetvalue(res, i, 1); /* don't free this
822
/* Extra: not null and default */
825
resetPQExpBuffer(&tmpbuf);
826
if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
827
appendPQExpBufferStr(&tmpbuf, "not null");
829
/* handle "default" here */
830
/* (note: above we cut off the 'default' string at 128) */
831
if (strlen(PQgetvalue(res, i, 2)) != 0)
834
appendPQExpBufferStr(&tmpbuf, " ");
835
appendPQExpBuffer(&tmpbuf, "default %s",
836
PQgetvalue(res, i, 2));
840
cells[i * cols + 2] = pg_strdup(mbvalidate(tmpbuf.data, myopt.encoding));
842
cells[i * cols + 2] = pg_strdup(tmpbuf.data);
849
cells[i * cols + cols - 1] = mbvalidate(PQgetvalue(res, i, 5), myopt.encoding);
851
cells[i * cols + cols - 1] = PQgetvalue(res, i, 5);
856
switch (tableinfo.relkind)
859
printfPQExpBuffer(&title, _("Table \"%s.%s\""),
860
schemaname, relationname);
863
printfPQExpBuffer(&title, _("View \"%s.%s\""),
864
schemaname, relationname);
867
printfPQExpBuffer(&title, _("Sequence \"%s.%s\""),
868
schemaname, relationname);
871
printfPQExpBuffer(&title, _("Index \"%s.%s\""),
872
schemaname, relationname);
875
printfPQExpBuffer(&title, _("Special relation \"%s.%s\""),
876
schemaname, relationname);
879
printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""),
880
schemaname, relationname);
883
printfPQExpBuffer(&title, _("Composite type \"%s.%s\""),
884
schemaname, relationname);
887
printfPQExpBuffer(&title, _("?%c? \"%s.%s\""),
888
tableinfo.relkind, schemaname, relationname);
893
if (tableinfo.relkind == 'i')
895
/* Footer information about an index */
898
printfPQExpBuffer(&buf,
899
"SELECT i.indisunique, i.indisprimary, i.indisclustered, a.amname, c2.relname,\n"
900
" pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
901
"FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
902
"WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
903
"AND i.indrelid = c2.oid",
906
result = PSQLexec(buf.data, false);
909
else if (PQntuples(result) != 1)
916
char *indisunique = PQgetvalue(result, 0, 0);
917
char *indisprimary = PQgetvalue(result, 0, 1);
918
char *indisclustered = PQgetvalue(result, 0, 2);
919
char *indamname = PQgetvalue(result, 0, 3);
920
char *indtable = PQgetvalue(result, 0, 4);
921
char *indpred = PQgetvalue(result, 0, 5);
922
int count_footers = 0;
924
if (strcmp(indisprimary, "t") == 0)
925
printfPQExpBuffer(&tmpbuf, _("primary key, "));
926
else if (strcmp(indisunique, "t") == 0)
927
printfPQExpBuffer(&tmpbuf, _("unique, "));
929
resetPQExpBuffer(&tmpbuf);
930
appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
932
/* we assume here that index and table are in same schema */
933
appendPQExpBuffer(&tmpbuf, _("for table \"%s.%s\""),
934
schemaname, indtable);
937
appendPQExpBuffer(&tmpbuf, _(", predicate (%s)"), indpred);
939
if (strcmp(indisclustered, "t") == 0)
940
appendPQExpBuffer(&tmpbuf, _(", clustered"));
942
footers = pg_malloc_zero(4 * sizeof(*footers));
943
footers[count_footers++] = pg_strdup(tmpbuf.data);
944
add_tablespace_footer(tableinfo.relkind, tableinfo.tablespace,
945
footers, &count_footers, tmpbuf);
946
footers[count_footers] = NULL;
954
PGresult *result = NULL;
956
int count_footers = 0;
958
/* count rules other than the view rule */
959
if (tableinfo.hasrules)
961
printfPQExpBuffer(&buf,
962
"SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
963
"FROM pg_catalog.pg_rewrite r\n"
964
"WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'",
966
result = PSQLexec(buf.data, false);
970
rule_count = PQntuples(result);
973
/* Footer information about a view */
974
footers = pg_malloc_zero((rule_count + 3) * sizeof(*footers));
975
footers[count_footers] = pg_malloc(64 + strlen(view_def));
976
snprintf(footers[count_footers], 64 + strlen(view_def),
977
_("View definition:\n%s"), view_def);
983
printfPQExpBuffer(&buf, _("Rules:"));
984
footers[count_footers++] = pg_strdup(buf.data);
985
for (i = 0; i < rule_count; i++)
989
/* Everything after "CREATE RULE" is echoed verbatim */
990
ruledef = PQgetvalue(result, i, 1);
993
printfPQExpBuffer(&buf, " %s", ruledef);
995
footers[count_footers++] = pg_strdup(buf.data);
1000
footers[count_footers] = NULL;
1003
else if (tableinfo.relkind == 'r')
1005
/* Footer information about a table */
1006
PGresult *result1 = NULL,
1012
int check_count = 0,
1014
foreignkey_count = 0,
1018
int count_footers = 0;
1021
if (tableinfo.hasindex)
1023
printfPQExpBuffer(&buf,
1024
"SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, "
1025
"pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n"
1026
"FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
1027
"WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
1028
"ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
1030
result1 = PSQLexec(buf.data, false);
1034
index_count = PQntuples(result1);
1037
/* count table (and column) check constraints */
1038
if (tableinfo.checks)
1040
printfPQExpBuffer(&buf,
1042
"pg_catalog.pg_get_constraintdef(r.oid, true), "
1044
"FROM pg_catalog.pg_constraint r\n"
1045
"WHERE r.conrelid = '%s' AND r.contype = 'c'",
1047
result2 = PSQLexec(buf.data, false);
1054
check_count = PQntuples(result2);
1058
if (tableinfo.hasrules)
1060
printfPQExpBuffer(&buf,
1061
"SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
1062
"FROM pg_catalog.pg_rewrite r\n"
1063
"WHERE r.ev_class = '%s'",
1065
result3 = PSQLexec(buf.data, false);
1073
rule_count = PQntuples(result3);
1076
/* count triggers (but ignore foreign-key triggers) */
1077
if (tableinfo.triggers)
1079
printfPQExpBuffer(&buf,
1080
"SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid)\n"
1081
"FROM pg_catalog.pg_trigger t\n"
1082
"WHERE t.tgrelid = '%s' "
1083
"AND (not tgisconstraint "
1085
" (SELECT 1 FROM pg_catalog.pg_depend d "
1086
" JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
1087
" WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
1089
result4 = PSQLexec(buf.data, false);
1098
trigger_count = PQntuples(result4);
1101
/* count foreign-key constraints (there are none if no triggers) */
1102
if (tableinfo.triggers)
1104
printfPQExpBuffer(&buf,
1106
" pg_catalog.pg_get_constraintdef(oid, true) as condef\n"
1107
"FROM pg_catalog.pg_constraint r\n"
1108
"WHERE r.conrelid = '%s' AND r.contype = 'f'",
1110
result5 = PSQLexec(buf.data, false);
1120
foreignkey_count = PQntuples(result5);
1123
/* count inherited tables */
1124
printfPQExpBuffer(&buf, "SELECT c.relname FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno ASC", oid);
1126
result6 = PSQLexec(buf.data, false);
1130
inherits_count = PQntuples(result6);
1132
footers = pg_malloc_zero((index_count + check_count + rule_count + trigger_count + foreignkey_count + inherits_count + 7 + 1)
1133
* sizeof(*footers));
1136
if (index_count > 0)
1138
printfPQExpBuffer(&buf, _("Indexes:"));
1139
footers[count_footers++] = pg_strdup(buf.data);
1140
for (i = 0; i < index_count; i++)
1142
const char *indexdef;
1143
const char *usingpos;
1145
/* Output index name */
1146
printfPQExpBuffer(&buf, _(" \"%s\""),
1147
PQgetvalue(result1, i, 0));
1149
/* Label as primary key or unique (but not both) */
1150
appendPQExpBuffer(&buf,
1151
strcmp(PQgetvalue(result1, i, 1), "t") == 0
1153
(strcmp(PQgetvalue(result1, i, 2), "t") == 0
1156
/* Everything after "USING" is echoed verbatim */
1157
indexdef = PQgetvalue(result1, i, 4);
1158
usingpos = strstr(indexdef, " USING ");
1160
indexdef = usingpos + 7;
1162
appendPQExpBuffer(&buf, " %s", indexdef);
1164
if (strcmp(PQgetvalue(result1, i, 3), "t") == 0)
1165
appendPQExpBuffer(&buf, " CLUSTER");
1167
footers[count_footers++] = pg_strdup(buf.data);
1171
/* print check constraints */
1172
if (check_count > 0)
1174
printfPQExpBuffer(&buf, _("Check constraints:"));
1175
footers[count_footers++] = pg_strdup(buf.data);
1176
for (i = 0; i < check_count; i++)
1178
printfPQExpBuffer(&buf, _(" \"%s\" %s"),
1179
PQgetvalue(result2, i, 1),
1180
PQgetvalue(result2, i, 0));
1182
footers[count_footers++] = pg_strdup(buf.data);
1186
/* print foreign key constraints */
1187
if (foreignkey_count > 0)
1189
printfPQExpBuffer(&buf, _("Foreign-key constraints:"));
1190
footers[count_footers++] = pg_strdup(buf.data);
1191
for (i = 0; i < foreignkey_count; i++)
1193
printfPQExpBuffer(&buf, _(" \"%s\" %s"),
1194
PQgetvalue(result5, i, 0),
1195
PQgetvalue(result5, i, 1));
1197
footers[count_footers++] = pg_strdup(buf.data);
1204
printfPQExpBuffer(&buf, _("Rules:"));
1205
footers[count_footers++] = pg_strdup(buf.data);
1206
for (i = 0; i < rule_count; i++)
1208
const char *ruledef;
1210
/* Everything after "CREATE RULE" is echoed verbatim */
1211
ruledef = PQgetvalue(result3, i, 1);
1214
printfPQExpBuffer(&buf, " %s", ruledef);
1216
footers[count_footers++] = pg_strdup(buf.data);
1220
/* print triggers */
1221
if (trigger_count > 0)
1223
printfPQExpBuffer(&buf, _("Triggers:"));
1224
footers[count_footers++] = pg_strdup(buf.data);
1225
for (i = 0; i < trigger_count; i++)
1228
const char *usingpos;
1230
/* Everything after "TRIGGER" is echoed verbatim */
1231
tgdef = PQgetvalue(result4, i, 1);
1232
usingpos = strstr(tgdef, " TRIGGER ");
1234
tgdef = usingpos + 9;
1236
printfPQExpBuffer(&buf, " %s", tgdef);
1238
footers[count_footers++] = pg_strdup(buf.data);
1242
/* print inherits */
1243
for (i = 0; i < inherits_count; i++)
1245
char *s = _("Inherits");
1248
printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result6, i, 0));
1250
printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result6, i, 0));
1251
if (i < inherits_count - 1)
1252
appendPQExpBuffer(&buf, ",");
1254
footers[count_footers++] = pg_strdup(buf.data);
1259
char *s = _("Has OIDs");
1261
printfPQExpBuffer(&buf, "%s: %s", s,
1262
(tableinfo.hasoids ? _("yes") : _("no")));
1263
footers[count_footers++] = pg_strdup(buf.data);
1266
add_tablespace_footer(tableinfo.relkind, tableinfo.tablespace,
1267
footers, &count_footers, buf);
1268
/* end of list marker */
1269
footers[count_footers] = NULL;
1279
printTable(title.data, headers,
1280
(const char **) cells, (const char **) footers,
1281
"llll", &myopt, pset.queryFout);
1288
termPQExpBuffer(&buf);
1289
termPQExpBuffer(&title);
1290
termPQExpBuffer(&tmpbuf);
1294
for (i = 0; i < numrows; i++)
1297
free(cells[i * cols + 2]);
1304
for (ptr = footers; *ptr; ptr++)
1320
add_tablespace_footer(char relkind, Oid tablespace, char **footers,
1321
int *count, PQExpBufferData buf)
1323
/* relkinds for which we support tablespaces */
1324
if (relkind == 'r' || relkind == 'i')
1327
* We ignore the database default tablespace so that users not
1328
* using tablespaces don't need to know about them.
1330
if (tablespace != 0)
1332
PGresult *result1 = NULL;
1334
printfPQExpBuffer(&buf, "SELECT spcname FROM pg_tablespace \n"
1335
"WHERE oid = '%u';", tablespace);
1336
result1 = PSQLexec(buf.data, false);
1339
/* Should always be the case, but.... */
1340
if (PQntuples(result1) > 0)
1342
printfPQExpBuffer(&buf, _("Tablespace: \"%s\""),
1343
PQgetvalue(result1, 0, 0));
1344
footers[(*count)++] = pg_strdup(buf.data);
1354
* Describes users. Any schema portion of the pattern is ignored.
1357
describeUsers(const char *pattern)
1359
PQExpBufferData buf;
1361
printQueryOpt myopt = pset.popt;
1363
initPQExpBuffer(&buf);
1365
printfPQExpBuffer(&buf,
1366
"SELECT u.usename AS \"%s\",\n"
1367
" u.usesysid AS \"%s\",\n"
1368
" CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('%s' AS pg_catalog.text)\n"
1369
" WHEN u.usesuper THEN CAST('%s' AS pg_catalog.text)\n"
1370
" WHEN u.usecreatedb THEN CAST('%s' AS pg_catalog.text)\n"
1371
" ELSE CAST('' AS pg_catalog.text)\n"
1373
" ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as \"%s\"\n"
1374
"FROM pg_catalog.pg_user u\n",
1375
_("User name"), _("User ID"),
1376
_("superuser, create database"),
1377
_("superuser"), _("create database"),
1378
_("Attributes"), _("Groups"));
1380
processNamePattern(&buf, pattern, false, false,
1381
NULL, "u.usename", NULL, NULL);
1383
appendPQExpBuffer(&buf, "ORDER BY 1;");
1385
res = PSQLexec(buf.data, false);
1386
termPQExpBuffer(&buf);
1390
myopt.nullPrint = NULL;
1391
myopt.title = _("List of users");
1393
printQuery(res, &myopt, pset.queryFout);
1406
describeGroups(const char *pattern)
1408
PQExpBufferData buf;
1410
printQueryOpt myopt = pset.popt;
1412
initPQExpBuffer(&buf);
1414
printfPQExpBuffer(&buf,
1415
"SELECT g.groname AS \"%s\",\n"
1416
" g.grosysid AS \"%s\"\n"
1417
"FROM pg_catalog.pg_group g\n",
1418
_("Group name"), _("Group ID"));
1420
processNamePattern(&buf, pattern, false, false,
1421
NULL, "g.groname", NULL, NULL);
1423
appendPQExpBuffer(&buf, "ORDER BY 1;");
1425
res = PSQLexec(buf.data, false);
1426
termPQExpBuffer(&buf);
1430
myopt.nullPrint = NULL;
1431
myopt.title = _("List of groups");
1433
printQuery(res, &myopt, pset.queryFout);
1443
* handler for \d, \dt, etc.
1445
* tabtypes is an array of characters, specifying what info is desired:
1450
* S - system tables (pg_catalog)
1451
* (any order of the above is fine)
1454
listTables(const char *tabtypes, const char *pattern, bool verbose)
1456
bool showTables = strchr(tabtypes, 't') != NULL;
1457
bool showIndexes = strchr(tabtypes, 'i') != NULL;
1458
bool showViews = strchr(tabtypes, 'v') != NULL;
1459
bool showSeq = strchr(tabtypes, 's') != NULL;
1460
bool showSystem = strchr(tabtypes, 'S') != NULL;
1462
PQExpBufferData buf;
1464
printQueryOpt myopt = pset.popt;
1466
if (!(showTables || showIndexes || showViews || showSeq))
1467
showTables = showViews = showSeq = true;
1469
initPQExpBuffer(&buf);
1471
printfPQExpBuffer(&buf,
1472
"SELECT n.nspname as \"%s\",\n"
1473
" c.relname as \"%s\",\n"
1474
" CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n"
1475
" u.usename as \"%s\"",
1476
_("Schema"), _("Name"),
1477
_("table"), _("view"), _("index"), _("sequence"),
1478
_("special"), _("Type"), _("Owner"));
1481
appendPQExpBuffer(&buf,
1482
",\n c2.relname as \"%s\"",
1486
appendPQExpBuffer(&buf,
1487
",\n pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
1491
appendPQExpBuffer(&buf,
1492
"\nFROM pg_catalog.pg_class c"
1493
"\n JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
1494
"\n JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid"
1495
"\n LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner"
1496
"\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1498
appendPQExpBuffer(&buf,
1499
"\nFROM pg_catalog.pg_class c"
1500
"\n LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner"
1501
"\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n");
1503
appendPQExpBuffer(&buf, "WHERE c.relkind IN (");
1505
appendPQExpBuffer(&buf, "'r',");
1507
appendPQExpBuffer(&buf, "'v',");
1509
appendPQExpBuffer(&buf, "'i',");
1511
appendPQExpBuffer(&buf, "'S',");
1512
if (showSystem && showTables)
1513
appendPQExpBuffer(&buf, "'s',");
1514
appendPQExpBuffer(&buf, "''"); /* dummy */
1515
appendPQExpBuffer(&buf, ")\n");
1518
* If showSystem is specified, show only system objects (those in
1519
* pg_catalog). Otherwise, suppress system objects, including those
1520
* in pg_catalog and pg_toast. (We don't want to hide temp tables
1524
appendPQExpBuffer(&buf, " AND n.nspname = 'pg_catalog'\n");
1526
appendPQExpBuffer(&buf, " AND n.nspname NOT IN ('pg_catalog', 'pg_toast')\n");
1528
processNamePattern(&buf, pattern, true, false,
1529
"n.nspname", "c.relname", NULL,
1530
"pg_catalog.pg_table_is_visible(c.oid)");
1532
appendPQExpBuffer(&buf, "ORDER BY 1,2;");
1534
res = PSQLexec(buf.data, false);
1535
termPQExpBuffer(&buf);
1539
if (PQntuples(res) == 0 && !QUIET())
1542
fprintf(pset.queryFout, _("No matching relations found.\n"));
1544
fprintf(pset.queryFout, _("No relations found.\n"));
1548
myopt.nullPrint = NULL;
1549
myopt.title = _("List of relations");
1551
printQuery(res, &myopt, pset.queryFout);
1562
* Describes domains.
1565
listDomains(const char *pattern)
1567
PQExpBufferData buf;
1569
printQueryOpt myopt = pset.popt;
1571
initPQExpBuffer(&buf);
1573
printfPQExpBuffer(&buf,
1574
"SELECT n.nspname as \"%s\",\n"
1575
" t.typname as \"%s\",\n"
1576
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
1577
" CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
1578
" WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
1579
" WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
1582
"FROM pg_catalog.pg_type t\n"
1583
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
1584
"WHERE t.typtype = 'd'\n",
1590
processNamePattern(&buf, pattern, true, false,
1591
"n.nspname", "t.typname", NULL,
1592
"pg_catalog.pg_type_is_visible(t.oid)");
1594
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1596
res = PSQLexec(buf.data, false);
1597
termPQExpBuffer(&buf);
1601
myopt.nullPrint = NULL;
1602
myopt.title = _("List of domains");
1604
printQuery(res, &myopt, pset.queryFout);
1613
* Describes conversions.
1616
listConversions(const char *pattern)
1618
PQExpBufferData buf;
1620
printQueryOpt myopt = pset.popt;
1622
initPQExpBuffer(&buf);
1624
printfPQExpBuffer(&buf,
1625
"SELECT n.nspname AS \"%s\",\n"
1626
" c.conname AS \"%s\",\n"
1627
" pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
1628
" pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
1629
" CASE WHEN c.condefault THEN '%s'\n"
1630
" ELSE '%s' END AS \"%s\"\n"
1631
"FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
1632
"WHERE n.oid = c.connamespace\n",
1641
processNamePattern(&buf, pattern, true, false,
1642
"n.nspname", "c.conname", NULL,
1643
"pg_catalog.pg_conversion_is_visible(c.oid)");
1645
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1647
res = PSQLexec(buf.data, false);
1648
termPQExpBuffer(&buf);
1652
myopt.nullPrint = NULL;
1653
myopt.title = _("List of conversions");
1655
printQuery(res, &myopt, pset.queryFout);
1667
listCasts(const char *pattern)
1669
PQExpBufferData buf;
1671
printQueryOpt myopt = pset.popt;
1673
initPQExpBuffer(&buf);
1674
/* NEED LEFT JOIN FOR BINARY CASTS */
1675
printfPQExpBuffer(&buf,
1676
"SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
1677
" pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n"
1678
" CASE WHEN castfunc = 0 THEN '%s'\n"
1681
" CASE WHEN c.castcontext = 'e' THEN '%s'\n"
1682
" WHEN c.castcontext = 'a' THEN '%s'\n"
1685
"FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
1686
" ON c.castfunc = p.oid\n"
1690
_("(binary compatible)"),
1697
res = PSQLexec(buf.data, false);
1698
termPQExpBuffer(&buf);
1702
myopt.nullPrint = NULL;
1703
myopt.title = _("List of casts");
1705
printQuery(res, &myopt, pset.queryFout);
1714
* Describes schemas (namespaces)
1717
listSchemas(const char *pattern, bool verbose)
1719
PQExpBufferData buf;
1721
printQueryOpt myopt = pset.popt;
1723
initPQExpBuffer(&buf);
1724
printfPQExpBuffer(&buf,
1725
"SELECT n.nspname AS \"%s\",\n"
1726
" u.usename AS \"%s\"",
1727
_("Name"), _("Owner"));
1730
appendPQExpBuffer(&buf,
1731
",\n n.nspacl as \"%s\","
1732
" pg_catalog.obj_description(n.oid, 'pg_namespace') as \"%s\"",
1733
_("Access privileges"), _("Description"));
1735
appendPQExpBuffer(&buf,
1736
"\nFROM pg_catalog.pg_namespace n LEFT JOIN pg_catalog.pg_user u\n"
1737
" ON n.nspowner=u.usesysid\n"
1738
"WHERE (n.nspname NOT LIKE 'pg\\\\_temp\\\\_%%' OR\n"
1739
" n.nspname = (pg_catalog.current_schemas(true))[1])\n"); /* temp schema is first */
1741
processNamePattern(&buf, pattern, true, false,
1742
NULL, "n.nspname", NULL,
1745
appendPQExpBuffer(&buf, "ORDER BY 1;");
1747
res = PSQLexec(buf.data, false);
1748
termPQExpBuffer(&buf);
1752
myopt.nullPrint = NULL;
1753
myopt.title = _("List of schemas");
1755
printQuery(res, &myopt, pset.queryFout);
1763
* processNamePattern
1765
* Scan a wildcard-pattern option and generate appropriate WHERE clauses
1766
* to limit the set of objects returned. The WHERE clauses are appended
1769
* pattern: user-specified pattern option to a \d command, or NULL if none.
1770
* have_where: true if caller already emitted WHERE.
1771
* force_escape: always quote regexp special characters, even outside quotes.
1772
* schemavar: name of WHERE variable to match against a schema-name pattern.
1773
* Can be NULL if no schema.
1774
* namevar: name of WHERE variable to match against an object-name pattern.
1775
* altnamevar: NULL, or name of an alternate variable to match against name.
1776
* visibilityrule: clause to use if we want to restrict to visible objects
1777
* (for example, "pg_catalog.pg_table_is_visible(p.oid)"). Can be NULL.
1780
processNamePattern(PQExpBuffer buf, const char *pattern,
1781
bool have_where, bool force_escape,
1782
const char *schemavar, const char *namevar,
1783
const char *altnamevar, const char *visibilityrule)
1785
PQExpBufferData schemabuf;
1786
PQExpBufferData namebuf;
1791
#define WHEREAND() \
1792
(appendPQExpBuffer(buf, have_where ? " AND " : "WHERE "), have_where = true)
1794
if (pattern == NULL)
1796
/* Default: select all visible objects */
1800
appendPQExpBuffer(buf, "%s\n", visibilityrule);
1805
initPQExpBuffer(&schemabuf);
1806
initPQExpBuffer(&namebuf);
1809
* Parse the pattern, converting quotes and lower-casing unquoted
1810
* letters; we assume this was NOT done by scan_option. Also, adjust
1811
* shell-style wildcard characters into regexp notation.
1820
if (inquotes && cp[1] == '"')
1822
/* emit one quote */
1823
appendPQExpBufferChar(&namebuf, '"');
1826
inquotes = !inquotes;
1829
else if (!inquotes && isupper((unsigned char) *cp))
1831
appendPQExpBufferChar(&namebuf,
1832
pg_tolower((unsigned char) *cp));
1835
else if (!inquotes && *cp == '*')
1837
appendPQExpBuffer(&namebuf, ".*");
1840
else if (!inquotes && *cp == '?')
1842
appendPQExpBufferChar(&namebuf, '.');
1845
else if (!inquotes && *cp == '.')
1847
/* Found schema/name separator, move current pattern to schema */
1848
resetPQExpBuffer(&schemabuf);
1849
appendPQExpBufferStr(&schemabuf, namebuf.data);
1850
resetPQExpBuffer(&namebuf);
1856
* Ordinary data character, transfer to pattern
1858
* Inside double quotes, or at all times if parsing an operator
1859
* name, quote regexp special characters with a backslash to
1860
* avoid regexp errors. Outside quotes, however, let them
1861
* pass through as-is; this lets knowledgeable users build
1862
* regexp expressions that are more powerful than shell-style
1865
if ((inquotes || force_escape) &&
1866
strchr("|*+?()[]{}.^$\\", *cp))
1867
appendPQExpBuffer(&namebuf, "\\\\");
1869
/* Ensure chars special to string literals are passed properly */
1870
if (*cp == '\'' || *cp == '\\')
1871
appendPQExpBufferChar(&namebuf, *cp);
1873
i = PQmblen(cp, pset.encoding);
1876
appendPQExpBufferChar(&namebuf, *cp);
1883
* Now decide what we need to emit.
1885
if (schemabuf.len > 0)
1887
/* We have a schema pattern, so constrain the schemavar */
1889
appendPQExpBufferChar(&schemabuf, '$');
1890
/* Optimize away ".*$", and possibly the whole pattern */
1891
if (schemabuf.len >= 3 &&
1892
strcmp(schemabuf.data + (schemabuf.len - 3), ".*$") == 0)
1893
schemabuf.data[schemabuf.len - 3] = '\0';
1895
if (schemabuf.data[0] && schemavar)
1898
appendPQExpBuffer(buf, "%s ~ '^%s'\n",
1899
schemavar, schemabuf.data);
1904
/* No schema pattern given, so select only visible objects */
1908
appendPQExpBuffer(buf, "%s\n", visibilityrule);
1912
if (namebuf.len > 0)
1914
/* We have a name pattern, so constrain the namevar(s) */
1916
appendPQExpBufferChar(&namebuf, '$');
1917
/* Optimize away ".*$", and possibly the whole pattern */
1918
if (namebuf.len >= 3 &&
1919
strcmp(namebuf.data + (namebuf.len - 3), ".*$") == 0)
1920
namebuf.data[namebuf.len - 3] = '\0';
1922
if (namebuf.data[0])
1926
appendPQExpBuffer(buf,
1928
" OR %s ~ '^%s')\n",
1929
namevar, namebuf.data,
1930
altnamevar, namebuf.data);
1932
appendPQExpBuffer(buf,
1934
namevar, namebuf.data);
1938
termPQExpBuffer(&schemabuf);
1939
termPQExpBuffer(&namebuf);