수십 개의 테이블이있는 대규모 PostgreSQL 데이터베이스를 담당하고 있습니다. 이러한 테이블 중 많은 부분에 액세스하지 못할 것으로 생각됩니다.
특정 테이블에 마지막으로 액세스 한 시간을 확인하는 가장 좋은 방법은 무엇입니까? 나는에 트리거를 추가하는 생각 DELETE, INSERT그리고 UPDATE,하지만 난 더 효율적인 방법이 있기를 바랍니다.
수십 개의 테이블이있는 대규모 PostgreSQL 데이터베이스를 담당하고 있습니다. 이러한 테이블 중 많은 부분에 액세스하지 못할 것으로 생각됩니다.
특정 테이블에 마지막으로 액세스 한 시간을 확인하는 가장 좋은 방법은 무엇입니까? 나는에 트리거를 추가하는 생각 DELETE, INSERT그리고 UPDATE,하지만 난 더 효율적인 방법이 있기를 바랍니다.
답변:
pg_catalog.pg_statio_all_tables 는 당신의 친구입니다. 해당 테이블에 대해 정기적으로 pg_statio_all_tables를 폴링하기 만하면됩니다. 통계 변경 ~ 활성 테이블, 변경되지 않는 통계 ~ 잠재적으로 사용되지 않는 테이블. select pg_stat_reset () ;모니터링 도중에 아무도 수행하지 않도록주의 하십시오.
예를 들면 다음과 같습니다.
test_1=# create table test_stats (col1 integer);
CREATE TABLE
test_1=# select * from pg_catalog.pg_statio_all_tables
where schemaname not in ('pg_catalog', 'information_schema')
and relname = 'test_stats';
relid | schemaname | relname | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
22957 | public | test_stats | 0 | 0 | [null] | [null] | [null] | [null] | [null] | [null]
(1 row)
삽입물 :
test_1=# insert into test_stats (col1) select generate_series( 1, 10000000);
INSERT 0 10000000
test_1=# select * from pg_catalog.pg_statio_all_tables
where schemaname not in ('pg_catalog', 'information_schema')
and relname = 'test_stats';
relid | schemaname | relname | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
22957 | public | test_stats | 44260 | 10088481 | [null] | [null] | [null] | [null] | [null] | [null]
(1 row)
선택합니다 :
test_1=# select count (*) from test_stats where col1 between 10000 and 50000;
count
-------
40001
(1 row)
test_1=# select * from pg_catalog.pg_statio_all_tables
where schemaname not in ('pg_catalog', 'information_schema')
and relname = 'test_stats';
relid | schemaname | relname | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
22957 | public | test_stats | 85560 | 10091429 | [null] | [null] | [null] | [null] | [null] | [null]
(1 row)
삭제 :
test_1=# delete from test_stats where col1 between 10000 and 50000;
DELETE 40001
test_1=# select * from pg_catalog.pg_statio_all_tables
where schemaname not in ('pg_catalog', 'information_schema')
and relname = 'test_stats';
relid | schemaname | relname | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
22957 | public | test_stats | 155075 | 10136163 | [null] | [null] | [null] | [null] | [null] | [null]
(1 row)
update-- 2011-09-01
추가 테스트 결과 vacuumpg_statio_all_tables의 값이 다소 증가한 것으로 나타 났으며, 이는 원하는 용도로는 불행한 일입니다. 하지만 vacuumpg_statio_all_tables의 사용 쓸모가되지 않습니다, 그것은 비트 불분명 결과를 해석 할 않습니다.
아마도 모니터링하기에 더 좋은 곳은 pg_catalog.pg_stat_all_tables (적어도 최신 버전의 Pg)입니다. 버전 8.4를보고 있는데 삽입, 읽기, 업데이트 및 삭제 된 튜플의 개수가 있습니다. ISTR 8.2에는 모든 것이 없으며 8.3에 대해 잘 모르기 때문에 PMM 버전에 따라 YMMV 사용.
삽입, 업데이트 및 삭제 활동에 대한 세 번째 옵션은 $ PGDATA / base / $ datid 디렉토리에서 파일 타임 스탬프를 보는 것입니다. 파일 이름은 테이블의 공백에 매핑되어야하므로 삽입, 업데이트 또는 삭제되지 않는 테이블을 식별하는 데 사용할 수 있습니다. 불행히도, 이것은 여전히 선택된 테이블을 해결하지 못하며 테이블 스페이스를 사용하면 추가적인 합병증이 발생합니다 (파일이 $ PGDATA / base / $ datid에 있지 않기 때문에). 타임 스탬프는 보류중인 변경 내용이 플러시 될 때까지 업데이트되지 않지만 파일이 몇 달 내에 변경되지 않은 경우 현재 보류중인 변경 가능성은 적습니다.
다음을 사용하여 테이블의 마지막 변경 사항에 대한 정보를 얻을 수 있습니다 xmin.
select max(xmin::text::bigint) from t;
그러나 모듈로와 랩 어라운드 및 고정 된 xid를 알고 있어야합니다 . 이것을 "시간"으로 변환하는 방법은 없지만 지금 테이블의 값을 캡처 한 다음 나중에 비교하면 변경된 테이블 목록을 얻을 수 있습니다.
select. 로깅 을 고려 했습니까 ?