I thought that would be desirable, although I don't see any better way of
getting there than this.

I don't see other commands for which which watch is wanted...but who am I to
say that watching creation extention isn't useful?  So I imagine this should be
generalized to save query buffer for all \d commands.

BTW, for amusement value, I briefly looked at implementing it for \!

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 4c85f43..580d708 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -68,7 +68,7 @@ static backslashResult exec_command_copy(PsqlScanState 
scan_state, bool active_b
 static backslashResult exec_command_copyright(PsqlScanState scan_state, bool 
active_branch);
 static backslashResult exec_command_crosstabview(PsqlScanState scan_state, 
bool active_branch);
 static backslashResult exec_command_d(PsqlScanState scan_state, bool 
active_branch,
-                          const char *cmd);
+                          const char *cmd, PQExpBuffer query_buf);
 static backslashResult exec_command_edit(PsqlScanState scan_state, bool 
active_branch,
                                  PQExpBuffer query_buf, PQExpBuffer 
previous_buf);
 static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool 
active_branch,
@@ -310,7 +310,7 @@ exec_command(const char *cmd,
        else if (strcmp(cmd, "crosstabview") == 0)
                status = exec_command_crosstabview(scan_state, active_branch);
        else if (cmd[0] == 'd')
-               status = exec_command_d(scan_state, active_branch, cmd);
+               status = exec_command_d(scan_state, active_branch, cmd, 
query_buf);
        else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
                status = exec_command_edit(scan_state, active_branch,
                                                                   query_buf, 
previous_buf);
@@ -693,7 +693,7 @@ exec_command_crosstabview(PsqlScanState scan_state, bool 
active_branch)
  * \d* commands
  */
 static backslashResult
-exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
+exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd, 
PQExpBuffer query_buf)
 {
        backslashResult status = PSQL_CMD_SKIP_LINE;
        bool            success = true;
@@ -720,7 +720,7 @@ exec_command_d(PsqlScanState scan_state, bool 
active_branch, const char *cmd)
                                        success = describeTableDetails(pattern, 
show_verbose, show_system);
                                else
                                        /* standard listing of interesting 
things */
-                                       success = listTables("tvmsE", NULL, 
show_verbose, show_system);
+                                       success = listTables("tvmsE", NULL, 
show_verbose, show_system, query_buf);
                                break;
                        case 'A':
                                success = describeAccessMethods(pattern, 
show_verbose);
@@ -794,7 +794,7 @@ exec_command_d(PsqlScanState scan_state, bool 
active_branch, const char *cmd)
                        case 'i':
                        case 's':
                        case 'E':
-                               success = listTables(&cmd[1], pattern, 
show_verbose, show_system);
+                               success = listTables(&cmd[1], pattern, 
show_verbose, show_system, query_buf);
                                break;
                        case 'r':
                                if (cmd[2] == 'd' && cmd[3] == 's')
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 410131e..b637e1b 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -3404,7 +3404,7 @@ listDbRoleSettings(const char *pattern, const char 
*pattern2)
  * (any order of the above is fine)
  */
 bool
-listTables(const char *tabtypes, const char *pattern, bool verbose, bool 
showSystem)
+listTables(const char *tabtypes, const char *pattern, bool verbose, bool 
showSystem, PQExpBuffer buf)
 {
        bool            showTables = strchr(tabtypes, 't') != NULL;
        bool            showIndexes = strchr(tabtypes, 'i') != NULL;
@@ -3413,7 +3413,6 @@ listTables(const char *tabtypes, const char *pattern, 
bool verbose, bool showSys
        bool            showSeq = strchr(tabtypes, 's') != NULL;
        bool            showForeign = strchr(tabtypes, 'E') != NULL;
 
-       PQExpBufferData buf;
        PGresult   *res;
        printQueryOpt myopt = pset.popt;
        static const bool translate_columns[] = {false, false, true, false, 
false, false, false};
@@ -3422,13 +3421,13 @@ listTables(const char *tabtypes, const char *pattern, 
bool verbose, bool showSys
        if (!(showTables || showIndexes || showViews || showMatViews || showSeq 
|| showForeign))
                showTables = showViews = showMatViews = showSeq = showForeign = 
true;
 
-       initPQExpBuffer(&buf);
+       initPQExpBuffer(buf);
 
        /*
         * Note: as of Pg 8.2, we no longer use relkind 's' (special), but we 
keep
         * it here for backwards compatibility.
         */
-       printfPQExpBuffer(&buf,
+       printfPQExpBuffer(buf,
                                          "SELECT n.nspname as \"%s\",\n"
                                          "  c.relname as \"%s\",\n"
                                          "  CASE c.relkind"
@@ -3458,7 +3457,7 @@ listTables(const char *tabtypes, const char *pattern, 
bool verbose, bool showSys
                                          gettext_noop("Owner"));
 
        if (showIndexes)
-               appendPQExpBuffer(&buf,
+               appendPQExpBuffer(buf,
                                                  ",\n c2.relname as \"%s\"",
                                                  gettext_noop("Table"));
 
@@ -3469,50 +3468,50 @@ listTables(const char *tabtypes, const char *pattern, 
bool verbose, bool showSys
                 * size of a table, including FSM, VM and TOAST tables.
                 */
                if (pset.sversion >= 90000)
-                       appendPQExpBuffer(&buf,
+                       appendPQExpBuffer(buf,
                                                          ",\n  
pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"",
                                                          gettext_noop("Size"));
                else if (pset.sversion >= 80100)
-                       appendPQExpBuffer(&buf,
+                       appendPQExpBuffer(buf,
                                                          ",\n  
pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(c.oid)) as \"%s\"",
                                                          gettext_noop("Size"));
 
-               appendPQExpBuffer(&buf,
+               appendPQExpBuffer(buf,
                                                  ",\n  
pg_catalog.obj_description(c.oid, 'pg_class') as \"%s\"",
                                                  gettext_noop("Description"));
        }
 
-       appendPQExpBufferStr(&buf,
+       appendPQExpBufferStr(buf,
                                                 "\nFROM pg_catalog.pg_class c"
                                                 "\n     LEFT JOIN 
pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
        if (showIndexes)
-               appendPQExpBufferStr(&buf,
+               appendPQExpBufferStr(buf,
                                                         "\n     LEFT JOIN 
pg_catalog.pg_index i ON i.indexrelid = c.oid"
                                                         "\n     LEFT JOIN 
pg_catalog.pg_class c2 ON i.indrelid = c2.oid");
 
-       appendPQExpBufferStr(&buf, "\nWHERE c.relkind IN (");
+       appendPQExpBufferStr(buf, "\nWHERE c.relkind IN (");
        if (showTables)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_RELATION) ","
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_RELATION) ","
                                                         
CppAsString2(RELKIND_PARTITIONED_TABLE) ",");
        if (showViews)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_VIEW) ",");
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_VIEW) ",");
        if (showMatViews)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_MATVIEW) ",");
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_MATVIEW) ",");
        if (showIndexes)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_INDEX) ","
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_INDEX) ","
                                                         
CppAsString2(RELKIND_PARTITIONED_INDEX) ",");
        if (showSeq)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_SEQUENCE) ",");
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_SEQUENCE) ",");
        if (showSystem || pattern)
-               appendPQExpBufferStr(&buf, "'s',"); /* was RELKIND_SPECIAL */
+               appendPQExpBufferStr(buf, "'s',"); /* was RELKIND_SPECIAL */
        if (showForeign)
-               appendPQExpBufferStr(&buf, CppAsString2(RELKIND_FOREIGN_TABLE) 
",");
+               appendPQExpBufferStr(buf, CppAsString2(RELKIND_FOREIGN_TABLE) 
",");
 
-       appendPQExpBufferStr(&buf, "''");       /* dummy */
-       appendPQExpBufferStr(&buf, ")\n");
+       appendPQExpBufferStr(buf, "''");        /* dummy */
+       appendPQExpBufferStr(buf, ")\n");
 
        if (!showSystem && !pattern)
-               appendPQExpBufferStr(&buf, "      AND n.nspname <> 
'pg_catalog'\n"
+               appendPQExpBufferStr(buf, "      AND n.nspname <> 
'pg_catalog'\n"
                                                         "      AND n.nspname 
<> 'information_schema'\n");
 
        /*
@@ -3522,16 +3521,15 @@ listTables(const char *tabtypes, const char *pattern, 
bool verbose, bool showSys
         * be shown.  Use plain \d if you really need to look at a TOAST
         * table/index.
         */
-       appendPQExpBufferStr(&buf, "      AND n.nspname !~ '^pg_toast'\n");
+       appendPQExpBufferStr(buf, "      AND n.nspname !~ '^pg_toast'\n");
 
-       processSQLNamePattern(pset.db, &buf, pattern, true, false,
+       processSQLNamePattern(pset.db, buf, pattern, true, false,
                                                  "n.nspname", "c.relname", 
NULL,
                                                  
"pg_catalog.pg_table_is_visible(c.oid)");
 
-       appendPQExpBufferStr(&buf, "ORDER BY 1,2;");
+       appendPQExpBufferStr(buf, "ORDER BY 1,2;");
 
-       res = PSQLexec(buf.data);
-       termPQExpBuffer(&buf);
+       res = PSQLexec(buf->data);
        if (!res)
                return false;
 
diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h
index a4cc5ef..57f2964 100644
--- a/src/bin/psql/describe.h
+++ b/src/bin/psql/describe.h
@@ -61,7 +61,7 @@ extern bool listTSTemplates(const char *pattern, bool 
verbose);
 extern bool listAllDbs(const char *pattern, bool verbose);
 
 /* \dt, \di, \ds, \dS, etc. */
-extern bool listTables(const char *tabtypes, const char *pattern, bool 
verbose, bool showSystem);
+extern bool listTables(const char *tabtypes, const char *pattern, bool 
verbose, bool showSystem, PQExpBuffer buf);
 
 /* \dD */
 extern bool listDomains(const char *pattern, bool verbose, bool showSystem);

Reply via email to