Hi Corey,

Some more comments on v15.

+/*
+ * A more encapsulated version of can_modify_relation for when the the
+ * HeapTuple and Form_pg_class are not needed later.
+ */
+static void
+check_relation_permissions(Relation rel)

This function is used exactly at one place, so usually won't make much
sense to write a separate function. But given that the caller is so long,
this seems ok. If this function returns the cached tuple when permission
checks succeed, it can be used at the other place as well. The caller will
be responsible to release the tuple Or update it.

Attached patch contains a test to invoke this function on a view. ANALYZE
throws a WARNING when a view is passed to it. Similarly this function
should refuse to update the statistics on relations for which ANALYZE
throws a warning. A warning instead of an error seems fine.

+
+ const float4 min = 0.0;
+ const float4 max = 1.0;

When reading the validation condition, I have to look up variable values.
That can be avoided by directly using the values in the condition itself?
If there's some dependency elsewhere in the code, we can use macros. But I
have not seen using constant variables in such a way elsewhere in the code.

+ values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(relid);
+ values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(attnum);
+ values[Anum_pg_statistic_stainherit - 1] = PG_GETARG_DATUM(P_INHERITED);

For a partitioned table this value has to be true. For a normal table when
setting this value to true, it should at least make sure that the table has
at least one child. Otherwise it should throw an error. Blindly accepting
the given value may render the statistics unusable. Prologue of the
function needs to be updated accordingly.

I have fixed a documentation error in the patch as well. Please incorporate
it in your next patchset.
-- 
Best Wishes,
Ashutosh Bapat
commit 353a4077d07cf097c5cb90c732b7dde2f16f04a6
Author: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date:   Mon Apr 1 13:04:40 2024 +0530

    Review changes
    
    Ashutosh Bapat

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 153d0dc6ac..6018e81486 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -29178,6 +29178,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
         be less than 0.
        </para>
        </entry>
+       </row>
+       <row>
        <entry role="func_table_entry"><para role="func_signature">
         <indexterm>
          <primary>pg_set_attribute_stats</primary>
diff --git a/src/test/regress/sql/stats_export_import.sql b/src/test/regress/sql/stats_export_import.sql
index 1a7d02a2c7..e3f42f85f0 100644
--- a/src/test/regress/sql/stats_export_import.sql
+++ b/src/test/regress/sql/stats_export_import.sql
@@ -15,6 +15,8 @@ CREATE TABLE stats_export_import.test(
     tags text[]
 );
 
+CREATE VIEW stats_export_import.test_view AS SELECT id, length(name), (comp).e FROM stats_export_import.test;
+
 SELECT relpages, reltuples, relallvisible FROM pg_class WHERE oid = 'stats_export_import.test'::regclass;
 
 SELECT pg_set_relation_stats('stats_export_import.test'::regclass, 999, 3.6::real, 15000);
@@ -26,6 +28,16 @@ SELECT pg_set_relation_stats('stats_export_import.test'::regclass, NULL, 3.6::re
 
 SELECT relpages, reltuples, relallvisible FROM pg_class WHERE oid = 'stats_export_import.test'::regclass;
 
+SELECT relpages, reltuples, relallvisible FROM pg_class WHERE oid = 'stats_export_import.test_view'::regclass;
+
+ANALYZE stats_export_import.test_view;
+
+SELECT relpages, reltuples, relallvisible FROM pg_class WHERE oid = 'stats_export_import.test_view'::regclass;
+
+SELECT pg_set_relation_stats('stats_export_import.test_view'::regclass, 999, 3.6::real, 15000);
+
+SELECT relpages, reltuples, relallvisible FROM pg_class WHERE oid = 'stats_export_import.test_view'::regclass;
+
 -- error: object doesn't exist
 SELECT pg_catalog.pg_set_attribute_stats(
     relation => '0'::oid,
@@ -62,6 +74,25 @@ SELECT pg_catalog.pg_set_attribute_stats(
     avg_width => 2::integer,
     n_distinct => 0.3::real);
 
+-- error: inherited true for a table which does not have any child
+SELECT pg_catalog.pg_set_attribute_stats(
+    relation => 'stats_export_import.test'::regclass,
+    attname => 'id'::name,
+    inherited => true,
+    null_frac => 0.1::real,
+    avg_width => 2::integer,
+    n_distinct => 0.3::real);
+
+CREATE TABLE stats_export_import.child() INHERITS(stats_export_import.test);
+
+ANALYZE VERBOSE stats_export_import.test;
+
+SELECT *
+FROM pg_stats
+WHERE schemaname = 'stats_export_import'
+AND tablename = 'test'
+AND attname = 'id';
+
 -- error: null_frac null
 SELECT pg_catalog.pg_set_attribute_stats(
     relation => 'stats_export_import.test'::regclass,

Reply via email to