プランナ統計情報リストア

 

 

(8.0.34)

調べた限りなし

 

(23c)
https://docs.oracle.com/cd/F19136_01/tgsql/managing-historical-optimizer-statistics.html#GUID-F1399D77-7E2E-434F-A67C-6ADB4C648D95

https://blog.usize-tech.com/oracledb_stats_restore/

RESTORE_TABLE_STATS Procedure
This procedure restores statistics of a table as of a specified timestamp (as_of_timestamp).
It also restores statistics of associated indexes and columns.

 

drop table tab1 purge;
create table tab1(col1 int);
create index ind11 on tab1(col1);
insert into tab1 values(1);
insert into tab1 values(2);
insert into tab1 values(3);
commit;

alter session set nls_timestamp_tz_format='YYYY-MM-DD HH24:MI:SS';
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';

select * from all_tab_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_ind_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_tab_stats_history where OWNER = 'TEST' and TABLE_NAME = 'TAB1';


exec dbms_stats.gather_table_stats('TEST','TAB1');

select * from all_tab_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_ind_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_tab_stats_history where OWNER = 'TEST' and TABLE_NAME = 'TAB1';


-- リストアしたい時刻を指定する(例: 統計履歴の1秒前)
-- 統計情報未取得の時刻の場合、統計情報がない状態に戻る


exec dbms_stats.restore_table_stats(ownname => 'TEST', -
                                    tabname => 'TAB1', -
                                    as_of_timestamp => '2024-03-23 19:45:59', -
                                    no_invalidate => false -
                                    );

select * from all_tab_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_ind_statistics where OWNER = 'TEST' and TABLE_NAME = 'TAB1';
select * from all_tab_stats_history where OWNER = 'TEST' and TABLE_NAME = 'TAB1';


-- オプティマイザ統計保存日数の確認と変更。デフォルトは31日間

select dbms_stats.get_stats_history_retention from dual;

exec dbms_stats.alter_stats_history_retention(366);

select dbms_stats.get_stats_history_retention from dual;

 

 

(14)
https://github.com/ossc-db/pg_dbms_stats
https://www.sraoss.co.jp/tech-blog/pgsql/pg_dbms_stats/
https://www.fujitsu.com/jp/products/software/resources/feature-stories/postgres/article-index/pg-dbms-stats/

 


-- 1. インストール

su - postgres
git clone https://github.com/ossc-db/pg_dbms_stats.git


cd pg_dbms_stats

export PATH=$PATH:/usr/pgsql-14/bin

make
su
make install


\dx
CREATE EXTENSION pg_dbms_stats;
\dx

vi postgresql.conf

shared_preload_libraries = 'pg_dbms_stats'

sudo systemctl restart postgresql-14

 


-- 2. 動作確認


drop table tab1;
create table tab1(col1 int);
create index ind11 on tab1(col1);
insert into tab1 values(1);
insert into tab1 values(2);
insert into tab1 values(3);
\d tab1


select dbms_stats.clean_up_stats();

analyze verbose tab1;

select dbms_stats.backup_table_stats('public','tab1','comment31');
select * from dbms_stats.backup_history;
select * from dbms_stats.stats where tablename = 'tab1';
select * from pg_stats where tablename='tab1';

insert into tab1 values(11);
insert into tab1 values(12);
insert into tab1 values(13);

analyze verbose tab1;

select dbms_stats.backup_table_stats('public','tab1','comment32');
select * from dbms_stats.backup_history;
select * from dbms_stats.stats where tablename = 'tab1';
select * from pg_stats where tablename='tab1';

 


as_of_timestamp -> リストアしたいタイミングの基準になるタイムスタンプです。
このタイムスタンプ以前で最新のバックアップデータをリストアします。
該当するバックアップが存在しない場合、統計情報は現在プランナが見ている値から変更されません。★
リストアした時点で統計情報はバックアップした時点のもので固定されていますので、リストア後に明示的にロックする必要はありません。

Oracleと異なり、統計情報未取得の時刻の場合、統計情報がない状態に戻ることはできない

select * from dbms_stats.stats where tablename = 'tab1';
select * from pg_stats where tablename='tab1';
select * from dbms_stats.relation_stats_locked;

select dbms_stats.restore_table_stats('public','tab1','2024-03-23 20:42:16'::timestamp);

select * from dbms_stats.stats where tablename = 'tab1';
select * from pg_stats where tablename='tab1';
select * from dbms_stats.relation_stats_locked;


\dx+ pg_dbms_stats

 

(2022)


調べた限りなし