PostgreSQL: ultimo tempo di accesso alla tabella


10

Sono responsabile di un grande database PostgreSQL, con poche decine di tabelle. Sospetto che a molte di queste tabelle non si acceda mai.

Qual è il modo migliore per verificare quando è stata acceduta l'ultima volta a un determinato tavolo? Ho pensato di aggiungere un grilletto DELETE, INSERTe UPDATE, ma spero che ci sia un modo più efficiente.



Grazie, corretto. La registrazione potrebbe essere la soluzione, ma il DB è molto utilizzato e probabilmente i registri occuperanno molto spazio su disco.
Adam Matan,

Risposte:


9

pg_catalog.pg_statio_all_tables è tuo amico. Tutto ciò che dovresti fare è eseguire periodicamente il polling di pg_statio_all_tables per le tabelle in questione. Modifica delle statistiche ~ tabella attiva, statistiche non modificabili ~ tabella potenzialmente inutilizzata. Fai solo attenzione a nessuno select pg_stat_reset () ;nel mezzo del monitoraggio.

Per esempio:

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)

inserti:

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)

Seleziona:

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)

Elimina:

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)

aggiornamento-- 2011-09-01

Ulteriori test indicano che vacuumsembra aumentare leggermente i valori in pg_statio_all_tables, il che è sfortunato per l'uso desiderato. Sebbene vacuumnon usi inutilmente pg_statio_all_tables, rende l'interpretazione dei risultati un po 'più sfacciata.

Forse un posto migliore da monitorare è pg_catalog.pg_stat_all_tables (almeno con le versioni più recenti di Pg). Sto guardando la versione 8.4 e che ha conteggi per le tuple inserite, lette, aggiornate ed eliminate-- ISTR 8.2 non ha tutto questo e non so di 8.3 quindi YMMV a seconda della versione di Pg che sei utilizzando.

Una terza opzione (per attività di inserimento, aggiornamento ed eliminazione) è quella di guardare i timestamp dei file nella directory $ PGDATA / base / $ datid. Il nome file deve essere mappato sull'oid della tabella, quindi è possibile utilizzarlo per identificare le tabelle che non ottengono inserimenti, aggiornamenti o eliminazioni. Sfortunatamente, questo non affronta le tabelle da cui si sta ancora selezionando, e l'uso di tablespace causerà ulteriori complicazioni (poiché quei file non saranno sotto $ PGDATA / base / $ datid). I timestamp non si aggiorneranno fino a quando non vengono cancellate eventuali modifiche in sospeso, ma se il file non è cambiato da mesi, le probabilità di una modifica attualmente in sospeso sono probabilmente piccole.


3

Puoi ottenere alcune informazioni sull'ultima modifica di una tabella con xmin, ad esempio:

select max(xmin::text::bigint) from t;

Ma devi essere consapevole del modulo e degli xids avvolgenti e congelati . Non c'è modo di convertirlo in un "tempo", ma se acquisisci il valore per le tue tabelle ora, quindi confronti in un secondo momento, puoi ottenere un elenco di tabelle che sono state modificate

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.