/* startup commands to send to server after connect. malloc-ed */
const char *connect_query;
- usec_t inactive_time; /* when auto-database became inactive (to kill it after timeout) */
+ usec_t inactive_time; /* when auto-database became inactive (to kill it after timeout) */
+ unsigned active_stamp; /* set if autodb has connections */
};
if (cf_autodb_idle_timeout <= 0)
return;
+ /* now kill the old ones */
statlist_for_each_safe(item, &autodatabase_idle_list, tmp) {
db = container_of(item, PgDatabase, head);
age = now - db->inactive_time;
{
struct List *item, *tmp;
PgPool *pool;
+ PgDatabase *db;
+
+ static unsigned int seq;
+ seq++;
/*
* Avoid doing anything that may surprise other pgbouncer.
continue;
pool_server_maint(pool);
pool_client_maint(pool);
- if (pool->db->db_auto && pool->db->inactive_time == 0 &&
- pool_client_count(pool) == 0 && pool_server_count(pool) == 0 ) {
- pool->db->inactive_time = get_cached_time();
+
+ /* is autodb active? */
+ if (pool->db->db_auto && pool->db->inactive_time == 0) {
+ if (pool_client_count(pool) > 0 || pool_server_count(pool) > 0)
+ pool->db->active_stamp = seq;
+ }
+ }
+
+ /* find inactive autodbs */
+ statlist_for_each_safe(item, &database_list, tmp) {
+ db = container_of(item, PgDatabase, head);
+ if (db->db_auto && db->inactive_time == 0) {
+ if (db->active_stamp == seq)
+ continue;
+ db->inactive_time = get_cached_time();
statlist_remove(&database_list, &pool->db->head);
statlist_append(&autodatabase_idle_list, &pool->db->head);
}