This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing an important tip for PostgreSQL DBAs.
I recently started work with a new PostgreSQL Client. They have total 326 databases in their PostgreSQL Server. They are taking full backup for all the database and this process is taking around total 7 hours.
When I looked this, my first step was to find database statistics. I have found 25% of databases are not being updated frequently.
I added one additional check in a backup process like “skip the backup for those databases which have not been changed for any operations (DDL or DML operation)”.
After this check, the backup process is taking around total 5 hours to complete the job.
Using pg_stat_database, you can check the stats of database and can compare the value for tuple columns with the old value. If you found any changes, your database has been changed.
If any of your DBA reset the PostgreSQL database statistics using something like pg_stat_reset, you have to add some more extra logic to understand the difference between statistics.
Below is a sample demonstration:
Create a backup table to store DatabaseStats:
1 2 3 4 5 6 7 |
CREATE TABLE public.tbl_DatabaseStats AS SELECT datname ,tup_inserted ,tup_updated ,tup_deleted FROM pg_stat_database; |
Perform few DDLs and DMLs actions:
1 2 3 |
CREATE TABLE public.tbl_Students(rno int, name character varying); INSERT INTO public.tbl_Students VALUES(1,'Anvesh'); CREATE TABLE public.tbl_Test88(id int); |
Compare the result of below two queries:
If you find differences on any of the columns of your Database, your database stats has been changed and you should take the backup of your database.
1 2 3 4 5 6 7 8 |
SELECT datname ,tup_inserted ,tup_updated ,tup_deleted FROM pg_stat_database; SELECT *FROM public.tbl_DatabaseStats; |
Leave a Reply