Skip to content

Commit

Permalink
keep catalog inconsistency of relhassubclass after analyze (main bran…
Browse files Browse the repository at this point in the history
…ch) (#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]>
  • Loading branch information
hyongtao-db authored and yjhjstz committed Jan 22, 2025
1 parent f0976ce commit a96a9df
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/backend/commands/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
{
Expand All @@ -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);
}

/*
Expand Down
34 changes: 34 additions & 0 deletions src/test/regress/expected/analyze.out
Original file line number Diff line number Diff line change
Expand Up @@ -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)

10 changes: 10 additions & 0 deletions src/test/regress/sql/analyze.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit a96a9df

Please sign in to comment.