From: Drouvot, Bertrand <bdrouvot@amazon.com>
To: Andres Freund <andres@anarazel.de>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: pg_stat_have_stats() returns true for dropped indexes (or for index creation transaction rolled back)
Date: Tue, 23 Aug 2022 09:58:03 +0200
Message-ID: <61a95a06-cf88-cd15-0412-172717943e18@amazon.com> (raw)
In-Reply-To: <20220822173654.y4h2smhc4kpkvoeg@awork3.anarazel.de>
References: <51bbf286-2b4a-8998-bd12-eaae4b765d99@amazon.com>
<20220822173654.y4h2smhc4kpkvoeg@awork3.anarazel.de>
Hi,
On 8/22/22 7:36 PM, Andres Freund wrote:
> On 2022-08-22 18:39:07 +0200, Drouvot, Bertrand wrote:
>> Please find attached a patch proposal to fix it.
> Perhaps a better fix would be to move the pgstat_create_relation() from
> heap_create_with_catalog() into heap_create()? Although I guess it's a bit
> pointless to deduplicate given that you're going to split it up again...
Thanks for looking at it!
Agree it's better to move it to heap_create(): it's done in the new
version attached.
We'll see later on if it needs to be duplicated for the table/index
split work.
>> It does contain additional calls to pgstat_create_relation() and
>> pgstat_drop_relation() as well as additional TAP tests.
> Would be good to add a test for CREATE INDEX / DROP INDEX / REINDEX
> CONCURRENTLY as well.
>
> Might be worth adding a test to stats.sql or stats.spec in the main regression
> tests. Perhaps that's best where the aforementioned things should be tested?
Yeah that sounds better, I'm also adding more tests around table
creation while at it.
I ended up adding the new tests in stats.sql.
>
>> @@ -2349,6 +2354,7 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
>> CatalogTupleDelete(indexRelation, &tuple->t_self);
>>
>> ReleaseSysCache(tuple);
>> +
>> table_close(indexRelation, RowExclusiveLock);
>>
>> /*
> Assume this was just an accident?
Oops, thanks!
New version attached.
Regards,
--
Bertrand Drouvot
Amazon Web Services: https://aws.amazon.comAttachments:v1-0002-pgstat_drop_relation-for-indexes.patch (text/plain, 9.5K) downloaddiff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.cindex 9b03579e6e..9a80ccdccd 100644--- a/src/backend/catalog/heap.c+++ b/src/backend/catalog/heap.c@@ -403,6 +403,9 @@ heap_create(const char *relname,
recordDependencyOnTablespace(RelationRelationId, relid,
reltablespace);
+ /* ensure that stats are dropped if transaction aborts */+ pgstat_create_relation(rel);+
return rel;
}
@@ -1477,9 +1480,6 @@ heap_create_with_catalog(const char *relname,
if (oncommit != ONCOMMIT_NOOP)
register_on_commit_action(relid, oncommit);
- /* ensure that stats are dropped if transaction aborts */- pgstat_create_relation(new_rel_desc);-
/*
* ok, the relation has been cataloged, so close our relations and return
* the OID of the newly created relation.
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.cindex d7192f35e3..61f1d3926a 100644--- a/src/backend/catalog/index.c+++ b/src/backend/catalog/index.c@@ -2325,6 +2325,9 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
if (RELKIND_HAS_STORAGE(userIndexRelation->rd_rel->relkind))
RelationDropStorage(userIndexRelation);
+ /* ensure that stats are dropped if transaction commits */+ pgstat_drop_relation(userIndexRelation);+
/*
* Close and flush the index's relcache entry, to ensure relcache doesn't
* try to rebuild it while we're deleting catalog entries. We keep the
diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.outindex 6b233ff4c0..5b01ef68fe 100644--- a/src/test/regress/expected/stats.out+++ b/src/test/regress/expected/stats.out@@ -783,6 +783,160 @@ SELECT pg_stat_have_stats('database', (SELECT oid FROM pg_database WHERE datname
t
(1 row)
+-- pg_stat_have_stats returns true for table creation inserting data+CREATE table stats_test_tab1 as select generate_series(1,10) a;+SELECT 'stats_test_tab1'::regclass::oid AS stats_test_tab1_oid \gset+SELECT oid AS dboid from pg_database where datname = current_database() \gset+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++-- pg_stat_have_stats returns true for a rolled back drop table with stats+BEGIN;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++DROP table stats_test_tab1;+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++-- pg_stat_have_stats returns false for a dropped table with stats+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++DROP table stats_test_tab1;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ f+(1 row)++-- pg_stat_have_stats returns false for rolled back table creation inserting data+BEGIN;+CREATE table stats_test_tab1 as select generate_series(1,10) a;+SELECT 'stats_test_tab1'::regclass::oid AS stats_test_tab1_oid \gset+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+ pg_stat_have_stats +--------------------+ f+(1 row)++-- pg_stat_have_stats returns true for committed index creation+CREATE table stats_test_tab1 as select generate_series(1,10) a;+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+SET enable_seqscan TO off;+select a from stats_test_tab1 where a = 3;+ a +---+ 3+(1 row)++SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++-- pg_stat_have_stats returns false for dropped index with stats+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++DROP index stats_test_idx1;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ f+(1 row)++-- pg_stat_have_stats returns false for rolled back index creation+BEGIN;+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+select a from stats_test_tab1 where a = 3;+ a +---+ 3+(1 row)++SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ f+(1 row)++-- pg_stat_have_stats returns true for reindex CONCURRENTLY+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+select a from stats_test_tab1 where a = 3;+ a +---+ 3+(1 row)++SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++REINDEX index CONCURRENTLY stats_test_idx1;+-- false for previous oid+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ f+(1 row)++-- true for new oid+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++-- pg_stat_have_stats returns true for a rolled back drop index with stats+BEGIN;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++DROP index stats_test_idx1;+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ pg_stat_have_stats +--------------------+ t+(1 row)++-- put enable_seqscan back to on+SET enable_seqscan TO on;
-- ensure that stats accessors handle NULL input correctly
SELECT pg_stat_get_replication_slot(NULL);
pg_stat_get_replication_slot
diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sqlindex 096f00ce8b..7cfefb2abc 100644--- a/src/test/regress/sql/stats.sql+++ b/src/test/regress/sql/stats.sql@@ -390,6 +390,74 @@ SELECT pg_stat_have_stats('zaphod', 0, 0);
SELECT pg_stat_have_stats('database', (SELECT oid FROM pg_database WHERE datname = current_database()), 1);
SELECT pg_stat_have_stats('database', (SELECT oid FROM pg_database WHERE datname = current_database()), 0);
+-- pg_stat_have_stats returns true for table creation inserting data+CREATE table stats_test_tab1 as select generate_series(1,10) a;+SELECT 'stats_test_tab1'::regclass::oid AS stats_test_tab1_oid \gset+SELECT oid AS dboid from pg_database where datname = current_database() \gset+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);++-- pg_stat_have_stats returns true for a rolled back drop table with stats+BEGIN;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+DROP table stats_test_tab1;+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);++-- pg_stat_have_stats returns false for a dropped table with stats+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);+DROP table stats_test_tab1;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);++-- pg_stat_have_stats returns false for rolled back table creation inserting data+BEGIN;+CREATE table stats_test_tab1 as select generate_series(1,10) a;+SELECT 'stats_test_tab1'::regclass::oid AS stats_test_tab1_oid \gset+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_tab1_oid);++-- pg_stat_have_stats returns true for committed index creation+CREATE table stats_test_tab1 as select generate_series(1,10) a;+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+SET enable_seqscan TO off;+select a from stats_test_tab1 where a = 3;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);++-- pg_stat_have_stats returns false for dropped index with stats+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+DROP index stats_test_idx1;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);++-- pg_stat_have_stats returns false for rolled back index creation+BEGIN;+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+select a from stats_test_tab1 where a = 3;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);++-- pg_stat_have_stats returns true for reindex CONCURRENTLY+CREATE index stats_test_idx1 on stats_test_tab1(a);+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+select a from stats_test_tab1 where a = 3;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+REINDEX index CONCURRENTLY stats_test_idx1;+-- false for previous oid+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+-- true for new oid+SELECT 'stats_test_idx1'::regclass::oid AS stats_test_idx1_oid \gset+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);++-- pg_stat_have_stats returns true for a rolled back drop index with stats+BEGIN;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);+DROP index stats_test_idx1;+ROLLBACK;+SELECT pg_stat_have_stats('relation', :dboid, :stats_test_idx1_oid);++-- put enable_seqscan back to on+SET enable_seqscan TO on;
-- ensure that stats accessors handle NULL input correctly
SELECT pg_stat_get_replication_slot(NULL);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: bdrouvot@amazon.com, andres@anarazel.de
Subject: Re: pg_stat_have_stats() returns true for dropped indexes (or for index creation transaction rolled back)
In-Reply-To: <61a95a06-cf88-cd15-0412-172717943e18@amazon.com>
References: <51bbf286-2b4a-8998-bd12-eaae4b765d99@amazon.com> <20220822173654.y4h2smhc4kpkvoeg@awork3.anarazel.de> <61a95a06-cf88-cd15-0412-172717943e18@amazon.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox