Continuous Integration (CI) using automated open source tools such as Jenkins and Hudson is getting adoption rapidly. These tools help developers to gain confidence for creating more robust code rapidly by improving testing and QA process. The flexibility of these softwares add other challenges for the DBAs!
One of our client came across challenge to cleanup databases after X number of days from the Jenkins CI database because each run create seperate database and database names are not standard because they are provided by users. If they don’t cleanup old database, the cluster will have hundreds of databases at the end of the week. We tried to standardize database names but you can’t control users to make mistakes or input db names 🙂 On the other hand, Postgres’s system catalog view doesn’t provide database creation date. How can I find out databases older than X days and drop them?
I came across this blog entry that answers my question but I was looking for easier way! I found easier way to get the database creation time with single query! Yay 🙂 Following is the query that can be used to find the database creation time. The query should return correct created_date as long as you haven’t run pg_upgrade on the data directory. I thought to share here so it will be useful for others!
SELECT datname, (pg_stat_file(‘base/’||oid||’/PG_VERSION’)).modification AS datcreated
postgres=# SELECT datname, (pg_stat_file(‘base/’||oid||’/PG_VERSION’)).modification AS datcreated
postgres-# FROM pg_database;
datname | datcreated
template1 | 2013-03-28 16:04:13-04
template0 | 2013-03-28 16:04:14-04
postgres | 2013-03-28 16:04:14-04
rangetypes | 2013-03-28 16:14:42-04
puppet | 2013-03-28 16:23:13-04
omniti | 2013-04-20 10:02:22-04
Ideally, pg_database system catalog view should include database_created timestamp ! Hopefully, that day will come sooner than later 🙂
Feel free to comment, if you have any other ideas for getting this details or you see any corner cases with above query that I haven’t mentioned here 🙂