This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit a96a9df03699a283f0285496c28f4545d5316491
Author: hyongtao-db <[email protected]>
AuthorDate: Wed Feb 22 03:42:19 2023 +0800

    keep catalog inconsistency of relhassubclass after analyze (main branch) 
(#14978)
    
    This pr can fix the bug reported in [issue 
14644](https://github.com/greenplum-db/gpdb/issues/14644)
    
    **Bug Detail:**
    After analyzing a table with a dropped subclass, the value on the 
coordinator may differ from the segments. See the SQL result with bug below:
    ```
    gpadmin=# CREATE TYPE test_type2 AS (a int, b text);
    CREATE TYPE
    gpadmin=# CREATE TABLE test_tbl2 OF test_type2;
    CREATE TABLE
    gpadmin=# CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
    CREATE TABLE
    gpadmin=# DROP TABLE test_tbl2_subclass;
    DROP TABLE
    gpadmin=# analyze;
    ANALYZE
    gpadmin=# select gp_segment_id, relhassubclass from pg_class where relname 
= 'test_tbl2';
    gp_segment_id | relhassubclass
    ---------------+----------------
                -1 | t
    (1 row)
    
    gpadmin=# select gp_segment_id, relhassubclass from 
gp_dist_random('pg_class') where relname = 'test_tbl2';
     gp_segment_id | relhassubclass
    ---------------+----------------
                 2 | f
                 1 | f
                 0 | f
    (3 rows)
    ```
    
    **Correct Behavior:**
    the value of `relhassubclass `on the coordinator should be the same as 
segments.
    ```
    gpadmin=# DROP TABLE test_tbl2_subclass;
    DROP TABLE
    gpadmin=# analyze;
    ANALYZE
    gpadmin=# select gp_segment_id, relhassubclass from pg_class where relname 
= 'test_tbl2';
    gp_segment_id | relhassubclass
    ---------------+----------------
                -1 | f
    (1 row)
    
    gpadmin=# select gp_segment_id, relhassubclass from 
gp_dist_random('pg_class') where relname = 'test_tbl2';
     gp_segment_id | relhassubclass
    ---------------+----------------
                 2 | f
                 1 | f
                 0 | f
    (3 rows)
    ```
    Signed-off-by: Yongtao Huang <[email protected]>
---
 src/backend/commands/analyze.c        | 28 +++++++++++++++-------------
 src/test/regress/expected/analyze.out | 34 ++++++++++++++++++++++++++++++++++
 src/test/regress/sql/analyze.sql      | 10 ++++++++++
 3 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 1216e90186..a67f822b59 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -1975,18 +1975,6 @@ acquire_inherited_sample_rows(Relation onerel, int 
elevel,
        ListCell   *lc;
        bool            has_child;
 
-       /*
-        * Like in acquire_sample_rows(), if we're in the QD, fetch the sample
-        * from segments.
-        */
-       if (Gp_role == GP_ROLE_DISPATCH && ENABLE_DISPATCH())
-       {
-               return acquire_sample_rows_dispatcher(onerel,
-                                                                               
          true, /* inherited stats */
-                                                                               
          elevel, rows, targrows,
-                                                                               
          totalrows, totaldeadrows);
-       }
-
        /*
         * Find all members of inheritance set.  We only need AccessShareLock on
         * the children.
@@ -2000,6 +1988,7 @@ acquire_inherited_sample_rows(Relation onerel, int elevel,
         * child but no longer does.  In that case, we can clear the
         * relhassubclass field so as not to make the same mistake again later.
         * (This is safe because we hold ShareUpdateExclusiveLock.)
+        * Please refer to https://github.com/greenplum-db/gpdb/issues/14644
         */
        if (list_length(tableOIDs) < 2)
        {
@@ -2012,7 +2001,20 @@ acquire_inherited_sample_rows(Relation onerel, int 
elevel,
                                (errmsg("skipping analyze of \"%s.%s\" 
inheritance tree --- this inheritance tree contains no child tables",
                                                
get_namespace_name(RelationGetNamespace(onerel)),
                                                
RelationGetRelationName(onerel))));
-               return 0;
+               if (Gp_role == GP_ROLE_EXECUTE)
+                       return 0;
+       }
+
+       /*
+        * Like in acquire_sample_rows(), if we're in the QD, fetch the sample
+        * from segments.
+        */
+       if (Gp_role == GP_ROLE_DISPATCH)
+       {
+               return acquire_sample_rows_dispatcher(onerel,
+                                                                               
          true, /* inherited stats */
+                                                                               
          elevel, rows, targrows,
+                                                                               
          totalrows, totaldeadrows);
        }
 
        /*
diff --git a/src/test/regress/expected/analyze.out 
b/src/test/regress/expected/analyze.out
index 37f490b75a..1f5fa43bb7 100644
--- a/src/test/regress/expected/analyze.out
+++ b/src/test/regress/expected/analyze.out
@@ -1230,3 +1230,37 @@ create table analyze_replicated(tc1 int,tc2 int) 
distributed replicated;
 insert into analyze_replicated select i, i from generate_series(1,1000) i;
 analyze analyze_replicated;
 drop table analyze_replicated;
+-- Issue 14644 keep catalog inconsistency of relhassubclass after analyze
+CREATE TYPE test_type_14644 AS (a int, b text);
+CREATE TABLE test_tb_14644 OF test_type_14644;
+CREATE TABLE test_tb_14644_subclass () INHERITS (test_tb_14644);
+DROP TABLE test_tb_14644_subclass;
+select relhassubclass from pg_class where relname = 'test_tb_14644';
+ relhassubclass 
+----------------
+ t
+(1 row)
+
+select relhassubclass from gp_dist_random('pg_class') where relname = 
'test_tb_14644';
+ relhassubclass 
+----------------
+ t
+ t
+ t
+(3 rows)
+
+ANALYZE;
+select relhassubclass from pg_class where relname = 'test_tb_14644';
+ relhassubclass 
+----------------
+ f
+(1 row)
+
+select relhassubclass from gp_dist_random('pg_class') where relname = 
'test_tb_14644';
+ relhassubclass 
+----------------
+ f
+ f
+ f
+(3 rows)
+
diff --git a/src/test/regress/sql/analyze.sql b/src/test/regress/sql/analyze.sql
index 3210c059f5..f8705d8aa2 100644
--- a/src/test/regress/sql/analyze.sql
+++ b/src/test/regress/sql/analyze.sql
@@ -631,3 +631,13 @@ create table analyze_replicated(tc1 int,tc2 int) 
distributed replicated;
 insert into analyze_replicated select i, i from generate_series(1,1000) i;
 analyze analyze_replicated;
 drop table analyze_replicated;
+-- Issue 14644 keep catalog inconsistency of relhassubclass after analyze
+CREATE TYPE test_type_14644 AS (a int, b text);
+CREATE TABLE test_tb_14644 OF test_type_14644;
+CREATE TABLE test_tb_14644_subclass () INHERITS (test_tb_14644);
+DROP TABLE test_tb_14644_subclass;
+select relhassubclass from pg_class where relname = 'test_tb_14644';
+select relhassubclass from gp_dist_random('pg_class') where relname = 
'test_tb_14644';
+ANALYZE;
+select relhassubclass from pg_class where relname = 'test_tb_14644';
+select relhassubclass from gp_dist_random('pg_class') where relname = 
'test_tb_14644';


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to