]> granicus.if.org Git - postgresql/commitdiff
Fix a couple of problems in pg_get_statisticsextdef
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 27 Mar 2017 03:53:59 +0000 (00:53 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 27 Mar 2017 04:03:50 +0000 (01:03 -0300)
There was a thinko whereby we tested the wrong tuple after fetching it
from cache; avoid that by using generate_relation_name instead, which is
simpler.  Also, the statistics name was not qualified, so add that.  (It
could be argued that qualification should be conditional on the schema
not being on search path.  We can add that later, but at least this form
is correct.)

Author: David Rowley, Álvaro Herrera
Discussion: https://postgr.es/m/CAKJS1f8RjLeVZJ2+93pdQGuZJeBF-ifsHaFMR-q-6-Z0qxA8cA@mail.gmail.com

src/backend/utils/adt/ruleutils.c
src/test/regress/expected/rules.out
src/test/regress/expected/stats_ext.out
src/test/regress/sql/rules.sql
src/test/regress/sql/stats_ext.sql

index d57d5568b2800c698c7e5d68a03bef32de4bd367..c2681ced2afb01d98c3aaed4292a3a08df466fa2 100644 (file)
@@ -1448,11 +1448,10 @@ static char *
 pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
 {
        Form_pg_statistic_ext   statextrec;
-       Form_pg_class                   pgclassrec;
        HeapTuple       statexttup;
-       HeapTuple       pgclasstup;
        StringInfoData buf;
        int                     colno;
+       char       *nsp;
 
        statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
 
@@ -1465,20 +1464,12 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
 
        statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
 
-       pgclasstup = SearchSysCache1(RELOID, ObjectIdGetDatum(statextrec->starelid));
-
-       if (!HeapTupleIsValid(statexttup))
-       {
-               ReleaseSysCache(statexttup);
-               elog(ERROR, "cache lookup failed for relation %u", statextrec->starelid);
-       }
-
-       pgclassrec = (Form_pg_class) GETSTRUCT(pgclasstup);
-
        initStringInfo(&buf);
 
+       nsp = get_namespace_name(statextrec->stanamespace);
        appendStringInfo(&buf, "CREATE STATISTICS %s ON (",
-                                                       quote_identifier(NameStr(statextrec->staname)));
+                                        quote_qualified_identifier(nsp,
+                                                                                               NameStr(statextrec->staname)));
 
        for (colno = 0; colno < statextrec->stakeys.dim1; colno++)
        {
@@ -1494,10 +1485,9 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
        }
 
        appendStringInfo(&buf, ") FROM %s",
-                                                       quote_identifier(NameStr(pgclassrec->relname)));
+                                        generate_relation_name(statextrec->starelid, NIL));
 
        ReleaseSysCache(statexttup);
-       ReleaseSysCache(pgclasstup);
 
        return buf.data;
 }
index 71121c8663464ddef09100b884df5dbb6f889b1b..d706f42b2d77700b7ce482ad239640c815a6af61 100644 (file)
@@ -3167,6 +3167,12 @@ SELECT pg_get_ruledef(0);
  
 (1 row)
 
+SELECT pg_get_statisticsextdef(0);
+ pg_get_statisticsextdef 
+-------------------------
+(1 row)
+
 SELECT pg_get_triggerdef(0);
  pg_get_triggerdef 
 -------------------
index 83d70bf9b9a2934e4f34ce2c9fde5f32af1b7269..0e30861ab82e30ff5774fca57b128294ac452811 100644 (file)
@@ -5,6 +5,13 @@ CREATE STATISTICS ab1_a_b_stats ON (a, b) FROM ab1;
 DROP STATISTICS ab1_a_b_stats;
 CREATE SCHEMA regress_schema_2;
 CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON (a, b) FROM ab1;
+-- Let's also verify the pg_get_statisticsextdef output looks sane.
+SELECT pg_get_statisticsextdef(oid) FROM pg_statistic_ext WHERE staname = 'ab1_a_b_stats';
+                       pg_get_statisticsextdef                       
+---------------------------------------------------------------------
+ CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON (a, b) FROM ab1
+(1 row)
+
 DROP STATISTICS regress_schema_2.ab1_a_b_stats;
 -- Ensure statistics are dropped when columns are
 CREATE STATISTICS ab1_b_c_stats ON (b, c) FROM ab1;
index 90dc9ceaf46a93ed217dad2711aa6c3a014c6103..dcff0de2a516d233b3d7cba0269c35c9692b8ddb 100644 (file)
@@ -1150,6 +1150,7 @@ SELECT pg_get_constraintdef(0);
 SELECT pg_get_functiondef(0);
 SELECT pg_get_indexdef(0);
 SELECT pg_get_ruledef(0);
+SELECT pg_get_statisticsextdef(0);
 SELECT pg_get_triggerdef(0);
 SELECT pg_get_viewdef(0);
 SELECT pg_get_function_arguments(0);
index 946cb848535aa0fd0e7de1c0be61af5e53ecded2..8f23653a9b632cc1c9b9dd3e5951e58425f847e2 100644 (file)
@@ -7,6 +7,10 @@ DROP STATISTICS ab1_a_b_stats;
 
 CREATE SCHEMA regress_schema_2;
 CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON (a, b) FROM ab1;
+
+-- Let's also verify the pg_get_statisticsextdef output looks sane.
+SELECT pg_get_statisticsextdef(oid) FROM pg_statistic_ext WHERE staname = 'ab1_a_b_stats';
+
 DROP STATISTICS regress_schema_2.ab1_a_b_stats;
 
 -- Ensure statistics are dropped when columns are