BUG #17123: ALTER TABLE ... NO INHERIT leaves orphaned statistics behind

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: christoph(dot)berg(at)credativ(dot)de
Subject: BUG #17123: ALTER TABLE ... NO INHERIT leaves orphaned statistics behind
Date: 2021-07-26 12:54:22
Message-ID: 17123-734abc3934f500b6@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17123
Logged by: Christoph Berg
Email address: christoph(dot)berg(at)credativ(dot)de
PostgreSQL version: 10.16
Operating system: Any
Description:

Removing the last/only inheritance child from a table using ALTER TABLE NO
INHERIT leaves orphaned statistics behind that ANALYZE doesn't remove.
Original customer complaint on 10.16, but 15devel has the same issue.

psql -ef inherit.sql
create table log (ts timestamptz, data text);
CREATE TABLE
Zeit: 2,378 ms
insert into log values ('2021-07-01', '1');
INSERT 0 1
Zeit: 0,457 ms
insert into log values ('2021-08-01', '2');
INSERT 0 1
Zeit: 0,197 ms
create table log2 (like log) inherits (log);
psql:inherit.sql:4: HINWEIS: 00000: Spalte »ts« wird mit geerbter
Definition zusammengeführt
ORT: MergeAttributes, tablecmds.c:2837
psql:inherit.sql:4: HINWEIS: 00000: Spalte »data« wird mit geerbter
Definition zusammengeführt
ORT: MergeAttributes, tablecmds.c:2837
CREATE TABLE
Zeit: 1,396 ms
insert into log2 values ('2021-01-01', '3');
INSERT 0 1
Zeit: 0,351 ms
insert into log2 values ('2021-02-01', '4');
INSERT 0 1
Zeit: 0,174 ms
analyze log;
ANALYZE
Zeit: 1,093 ms
select * from pg_stats where tablename = 'log' and attname = 'ts'
─[ RECORD 1
]──────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
schemaname │ public
tablename │ log
attname │ ts
inherited │ f
null_frac │ 0
avg_width │ 8
n_distinct │ -1
most_common_vals │ ∅
most_common_freqs │ ∅
histogram_bounds │ {"2021-07-01 00:00:00+02","2021-08-01
00:00:00+02"}
correlation │ 1
most_common_elems │ ∅
most_common_elem_freqs │ ∅
elem_count_histogram │ ∅
─[ RECORD 2
]──────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
schemaname │ public
tablename │ log
attname │ ts
inherited │ t
null_frac │ 0
avg_width │ 8
n_distinct │ -1
most_common_vals │ ∅
most_common_freqs │ ∅
histogram_bounds │ {"2021-01-01 00:00:00+01","2021-02-01
00:00:00+01","2021-07-01 00:00:00+02","2021-08-01 00:00:00+02"}
correlation │ -0.6
most_common_elems │ ∅
most_common_elem_freqs │ ∅
elem_count_histogram │ ∅

Zeit: 4,616 ms
select ctid, stainherit from pg_statistic where starelid = 'log'::regclass
and staattnum = 1;
ctid │ stainherit
─────────┼────────────
(18,17) │ f
(18,19) │ t
(2 Zeilen)

Zeit: 0,856 ms
alter table log2 no inherit log;
ALTER TABLE
Zeit: 0,449 ms
analyze log;
ANALYZE
Zeit: 0,394 ms
select * from pg_stats where tablename = 'log' and attname = 'ts'
─[ RECORD 1
]──────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────
schemaname │ public
tablename │ log
attname │ ts
inherited │ f
null_frac │ 0
avg_width │ 8
n_distinct │ -1
most_common_vals │ ∅
most_common_freqs │ ∅
histogram_bounds │ {"2021-07-01 00:00:00+02","2021-08-01
00:00:00+02"}
correlation │ 1
most_common_elems │ ∅
most_common_elem_freqs │ ∅
elem_count_histogram │ ∅
─[ RECORD 2
]──────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────
schemaname │ public
tablename │ log
attname │ ts
inherited │ t <-- not removed
null_frac │ 0
avg_width │ 8
n_distinct │ -1
most_common_vals │ ∅
most_common_freqs │ ∅
histogram_bounds │ {"2021-01-01 00:00:00+01","2021-02-01
00:00:00+01","2021-07-01 00:00:00+02","2021-08-01 00:00:00+02"}
correlation │ -0.6
most_common_elems │ ∅
most_common_elem_freqs │ ∅
elem_count_histogram │ ∅

Zeit: 1,526 ms
select ctid, stainherit from pg_statistic where starelid = 'log'::regclass
and staattnum = 1;
ctid │ stainherit
─────────┼────────────
(18,21) │ f
(18,19) │ t <-- not touched
(2 Zeilen)

Zeit: 0,373 ms

cat inherit.sql
create table log (ts timestamptz, data text);
insert into log values ('2021-07-01', '1');
insert into log values ('2021-08-01', '2');
create table log2 (like log) inherits (log);
insert into log2 values ('2021-01-01', '3');
insert into log2 values ('2021-02-01', '4');

analyze log;
select * from pg_stats where tablename = 'log' and attname = 'ts' \gx
select ctid, stainherit from pg_statistic where starelid = 'log'::regclass
and staattnum = 1;

alter table log2 no inherit log;

analyze log;
select * from pg_stats where tablename = 'log' and attname = 'ts' \gx
select ctid, stainherit from pg_statistic where starelid = 'log'::regclass
and staattnum = 1;

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2021-07-26 14:29:24 Re: BUG #17123: ALTER TABLE ... NO INHERIT leaves orphaned statistics behind
Previous Message Thomas Munro 2021-07-26 07:36:01 Re: BUG #17116: Assert failed in SerialSetActiveSerXmin() on commit of parallelized serializable transaction