psql: Display stats target of extended statistics
authorAlvaro Herrera <[email protected]>
Fri, 11 Sep 2020 19:15:47 +0000 (16:15 -0300)
committerAlvaro Herrera <[email protected]>
Fri, 11 Sep 2020 19:15:47 +0000 (16:15 -0300)
The stats target can be set since commit d06215d03, but wasn't shown by
psql.

Author: Justin Pryzby <[email protected]>
Discussion: https://postgr.es/m/20200831050047[email protected]
Reviewed-by: Georgios Kokolatos <[email protected]>
Reviewed-by: Tatsuro Yamada <[email protected]>
src/bin/psql/describe.c
src/test/regress/expected/stats_ext.out
src/test/regress/sql/stats_ext.sql

index 0861d74a6fe0ca95d36fbb3729871144caf52f09..f22d907b1f9f02f4eb059b2b6b173c70df9be3b2 100644 (file)
@@ -2683,8 +2683,13 @@ describeOneTableDetails(const char *schemaname,
                              "        a.attnum = s.attnum AND NOT attisdropped)) AS columns,\n"
                              "  'd' = any(stxkind) AS ndist_enabled,\n"
                              "  'f' = any(stxkind) AS deps_enabled,\n"
-                             "  'm' = any(stxkind) AS mcv_enabled\n"
-                             "FROM pg_catalog.pg_statistic_ext stat "
+                             "  'm' = any(stxkind) AS mcv_enabled,\n");
+
+           if (pset.sversion >= 130000)
+               appendPQExpBufferStr(&buf, "  stxstattarget\n");
+           else
+               appendPQExpBufferStr(&buf, "  -1 AS stxstattarget\n");
+           appendPQExpBuffer(&buf, "FROM pg_catalog.pg_statistic_ext stat\n"
                              "WHERE stxrelid = '%s'\n"
                              "ORDER BY 1;",
                              oid);
@@ -2732,6 +2737,11 @@ describeOneTableDetails(const char *schemaname,
                                      PQgetvalue(result, i, 4),
                                      PQgetvalue(result, i, 1));
 
+                   /* Show the stats target if it's not default */
+                   if (strcmp(PQgetvalue(result, i, 8), "-1") != 0)
+                       appendPQExpBuffer(&buf, "; STATISTICS %s",
+                                     PQgetvalue(result, i, 8));
+
                    printTableAddFooter(&cont, buf.data);
                }
            }
index 8c667d786a212d3513c36b041022068cc4fe8772..4c3edd213fb84307e6d6b0365850b0f974bdc8d8 100644 (file)
@@ -102,6 +102,15 @@ WARNING:  statistics object "public.ab1_a_b_stats" could not be computed for rel
 ALTER TABLE ab1 ALTER a SET STATISTICS -1;
 -- setting statistics target 0 skips the statistics, without printing any message, so check catalog
 ALTER STATISTICS ab1_a_b_stats SET STATISTICS 0;
+\d ab1
+                Table "public.ab1"
+ Column |  Type   | Collation | Nullable | Default 
+--------+---------+-----------+----------+---------
+ a      | integer |           |          | 
+ b      | integer |           |          | 
+Statistics objects:
+    "public"."ab1_a_b_stats" (ndistinct, dependencies, mcv) ON a, b FROM ab1; STATISTICS 0
+
 ANALYZE ab1;
 SELECT stxname, stxdndistinct, stxddependencies, stxdmcv
   FROM pg_statistic_ext s, pg_statistic_ext_data d
@@ -113,6 +122,15 @@ SELECT stxname, stxdndistinct, stxddependencies, stxdmcv
 (1 row)
 
 ALTER STATISTICS ab1_a_b_stats SET STATISTICS -1;
+\d+ ab1
+                                    Table "public.ab1"
+ Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
+--------+---------+-----------+----------+---------+---------+--------------+-------------
+ a      | integer |           |          |         | plain   |              | 
+ b      | integer |           |          |         | plain   |              | 
+Statistics objects:
+    "public"."ab1_a_b_stats" (ndistinct, dependencies, mcv) ON a, b FROM ab1
+
 -- partial analyze doesn't build stats either
 ANALYZE ab1 (a);
 WARNING:  statistics object "public.ab1_a_b_stats" could not be computed for relation "public.ab1"
index f8d947af9e80dd82d8d271dbf780e6f08246a15a..9781e590a30c68cc3a8d396505fbba1ce7b46e2c 100644 (file)
@@ -72,12 +72,14 @@ ANALYZE ab1;
 ALTER TABLE ab1 ALTER a SET STATISTICS -1;
 -- setting statistics target 0 skips the statistics, without printing any message, so check catalog
 ALTER STATISTICS ab1_a_b_stats SET STATISTICS 0;
+\d ab1
 ANALYZE ab1;
 SELECT stxname, stxdndistinct, stxddependencies, stxdmcv
   FROM pg_statistic_ext s, pg_statistic_ext_data d
  WHERE s.stxname = 'ab1_a_b_stats'
    AND d.stxoid = s.oid;
 ALTER STATISTICS ab1_a_b_stats SET STATISTICS -1;
+\d+ ab1
 -- partial analyze doesn't build stats either
 ANALYZE ab1 (a);
 ANALYZE ab1;