Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 243232
Collapse All | Expand All

(-)pgbouncer-1.2.3/etc/pgbouncer.ini (+12 lines)
Lines 15-18 Link Here
15
15
16
;;;
16
;;;
17
;;; Autodb settings
18
;;;
19
20
; If the client application is trying to connect to the database that is not
21
; listed in [databases] section, than this server connection settings are used.
22
; The format is the same as any connection string in the [databases] section
23
autodb_connstr = user=example port=6000
24
; if an auto-database has no connections more than this many seconds
25
; it is dropped with it's pool (releasing resources)
26
autodb_idle_timeout = 3600
27
28
;;;
17
;;; Administrative settings
29
;;; Administrative settings
18
;;;
30
;;;
(-)pgbouncer-1.2.3/include/bouncer.h (+6 lines)
Lines 221-224 Link Here
221
	bool db_paused;		/* PAUSE <db>; was issued */
221
	bool db_paused;		/* PAUSE <db>; was issued */
222
	bool db_dead;		/* used on RELOAD/SIGHUP to later detect removed dbs */
222
	bool db_dead;		/* used on RELOAD/SIGHUP to later detect removed dbs */
223
	bool db_auto;		/* is the database auto-created by autodb_connstr */
223
	bool admin;		/* internal console db */
224
	bool admin;		/* internal console db */
224
225
Lines 237-240 Link Here
237
	/* startup commands to send to server after connect. malloc-ed */
238
	/* startup commands to send to server after connect. malloc-ed */
238
	const char *connect_query;
239
	const char *connect_query;
240
241
	usec_t inactive_time; /* when auto-database became inactive (to kill it after timeout) */
239
};
242
};
240
243
Lines 308-311 Link Here
308
extern int cf_default_pool_size;
311
extern int cf_default_pool_size;
309
312
313
extern char * cf_autodb_connstr;
314
extern usec_t cf_autodb_idle_timeout;
315
310
extern usec_t cf_suspend_timeout;
316
extern usec_t cf_suspend_timeout;
311
extern usec_t cf_server_lifetime;
317
extern usec_t cf_server_lifetime;
(-)pgbouncer-1.2.3/include/objects.h (+2 lines)
Lines 21-24 Link Here
21
extern StatList pool_list;
21
extern StatList pool_list;
22
extern StatList database_list;
22
extern StatList database_list;
23
extern StatList autodatabase_idle_list;
23
extern StatList login_client_list;
24
extern StatList login_client_list;
24
extern ObjectCache *client_cache;
25
extern ObjectCache *client_cache;
Lines 41-44 Link Here
41
42
42
PgDatabase * add_database(const char *name) _MUSTCHECK;
43
PgDatabase * add_database(const char *name) _MUSTCHECK;
44
PgDatabase *register_auto_database(const char *name);
43
PgUser * add_user(const char *name, const char *passwd) _MUSTCHECK;
45
PgUser * add_user(const char *name, const char *passwd) _MUSTCHECK;
44
PgUser * force_user(PgDatabase *db, const char *username, const char *passwd) _MUSTCHECK;
46
PgUser * force_user(PgDatabase *db, const char *username, const char *passwd) _MUSTCHECK;
(-)pgbouncer-1.2.3/src/client.c (-2 / +8 lines)
Lines 58-63 Link Here
58
	db = find_database(dbname);
58
	db = find_database(dbname);
59
	if (!db) {
59
	if (!db) {
60
		disconnect_client(client, true, "No such database");
60
		db = register_auto_database(dbname);
61
		return false;
61
		if (!db) {
62
			disconnect_client(client, true, "No such database");
63
			return false;
64
		}
65
		else {
66
			slog_info(client, "registered new auto-database: db = %s", dbname );
67
		}
62
	}
68
	}
63
69
(-)pgbouncer-1.2.3/src/janitor.c (-4 / +36 lines)
Lines 477-487 Link Here
477
}
477
}
478
478
479
static void kill_database(PgDatabase *db);
480
static void cleanup_inactive_autodatabases(void)
481
{
482
	List *item, *tmp;
483
	PgDatabase *db;
484
	usec_t age;
485
	usec_t now = get_cached_time();
486
487
	if (cf_autodb_idle_timeout <= 0)
488
		return;
489
490
	statlist_for_each_safe(item, &autodatabase_idle_list, tmp) {
491
		db = container_of(item, PgDatabase, head);
492
		age = now - db->inactive_time;
493
		if (age > cf_autodb_idle_timeout) 
494
			kill_database(db);
495
		else
496
			break;
497
	}
498
}
499
479
/* full-scale maintenance, done only occasionally */
500
/* full-scale maintenance, done only occasionally */
480
static void do_full_maint(int sock, short flags, void *arg)
501
static void do_full_maint(int sock, short flags, void *arg)
481
{
502
{
482
	List *item;
503
	List *item, *tmp;
483
	PgPool *pool;
504
	PgPool *pool;
484
505
485
	statlist_for_each(item, &pool_list) {
506
	statlist_for_each_safe(item, &pool_list, tmp) {
486
		pool = container_of(item, PgPool, head);
507
		pool = container_of(item, PgPool, head);
487
		if (pool->db->admin)
508
		if (pool->db->admin)
Lines 489-494 Link Here
489
		pool_server_maint(pool);
510
		pool_server_maint(pool);
490
		pool_client_maint(pool);
511
		pool_client_maint(pool);
512
		if (pool->db->db_auto && pool->db->inactive_time == 0 &&
513
				pool_client_count(pool) == 0 && pool_server_count(pool) == 0 ) {
514
			pool->db->inactive_time = get_cached_time();
515
			statlist_remove(&pool->db->head, &database_list);
516
			statlist_append(&pool->db->head, &autodatabase_idle_list);
517
		}
491
	}
518
	}
492
519
520
	cleanup_inactive_autodatabases();
521
493
	cleanup_client_logins();
522
	cleanup_client_logins();
494
523
Lines 536-540 Link Here
536
	List *item, *tmp;
565
	List *item, *tmp;
537
566
538
	log_warning("dropping database '%s' as it does not exist anymore", db->name);
567
	log_warning("dropping database '%s' as it does not exist anymore or inactive auto-database", db->name);
539
568
540
	statlist_for_each_safe(item, &pool_list, tmp) {
569
	statlist_for_each_safe(item, &pool_list, tmp) {
Lines 547-551 Link Here
547
	if (db->connect_query)
576
	if (db->connect_query)
548
		free((void *)db->connect_query);
577
		free((void *)db->connect_query);
549
	statlist_remove(&db->head, &database_list);
578
	if (db->inactive_time)
579
		statlist_remove(&db->head, &autodatabase_idle_list);
580
	else
581
		statlist_remove(&db->head, &database_list);
550
	obj_free(db_cache, db);
582
	obj_free(db_cache, db);
551
}
583
}
(-)pgbouncer-1.2.3/src/loader.c (+3 lines)
Lines 265-268 Link Here
265
	/* tag the db as alive */
265
	/* tag the db as alive */
266
	db->db_dead = 0;
266
	db->db_dead = 0;
267
	/* assuming not an autodb */
268
	db->db_auto = 0;
269
	db->inactive_time = 0;
267
270
268
	/* if updating old db, check if anything changed */
271
	/* if updating old db, check if anything changed */
(-)pgbouncer-1.2.3/src/main.c (+6 lines)
Lines 97-100 Link Here
97
char *cf_ignore_startup_params = "";
97
char *cf_ignore_startup_params = "";
98
98
99
char *cf_autodb_connstr = "";
100
usec_t cf_autodb_idle_timeout = 3600*USEC;
101
99
usec_t cf_server_lifetime = 60*60*USEC;
102
usec_t cf_server_lifetime = 60*60*USEC;
100
usec_t cf_server_idle_timeout = 10*60*USEC;
103
usec_t cf_server_idle_timeout = 10*60*USEC;
Lines 140-143 Link Here
140
{"user",		false, CF_STR, &cf_username},
143
{"user",		false, CF_STR, &cf_username},
141
144
145
{"autodb_connstr",	true, CF_STR, &cf_autodb_connstr},
146
{"autodb_idle_timeout",	true, CF_TIME, &cf_autodb_idle_timeout},
147
142
{"server_reset_query",	true, CF_STR, &cf_server_reset_query},
148
{"server_reset_query",	true, CF_STR, &cf_server_reset_query},
143
{"server_check_query",	true, CF_STR, &cf_server_check_query},
149
{"server_check_query",	true, CF_STR, &cf_server_check_query},
(-)pgbouncer-1.2.3/src/objects.c (-1 / +42 lines)
Lines 52-55 Link Here
52
static STATLIST(justfree_server_list);
52
static STATLIST(justfree_server_list);
53
53
54
/* init autodb idle list */
55
STATLIST(autodatabase_idle_list);
56
54
/* fast way to get number of active clients */
57
/* fast way to get number of active clients */
55
int get_active_client_count(void)
58
int get_active_client_count(void)
Lines 305-308 Link Here
305
}
308
}
306
309
310
/* register new auto database */
311
PgDatabase *register_auto_database(const char *name)
312
{
313
	PgDatabase *db;
314
	char *cs = malloc(strlen(cf_autodb_connstr)+1);
315
316
	strcpy(cs, cf_autodb_connstr);
317
	parse_database((char*)name, cs);
318
	free(cs);
319
320
	db = find_database(name);
321
	if (db) {
322
		db->db_auto = 1;
323
		/* do not forget to check pool_size like in config_postprocess */
324
		if (db->pool_size < 0)
325
			db->pool_size = cf_default_pool_size;
326
	}
327
328
	return db;
329
}
330
307
/* add or update client users */
331
/* add or update client users */
308
PgUser *add_user(const char *name, const char *passwd)
332
PgUser *add_user(const char *name, const char *passwd)
Lines 346-350 Link Here
346
PgDatabase *find_database(const char *name)
370
PgDatabase *find_database(const char *name)
347
{
371
{
348
	List *item;
372
	List *item, *tmp;
349
	PgDatabase *db;
373
	PgDatabase *db;
350
	statlist_for_each(item, &database_list) {
374
	statlist_for_each(item, &database_list) {
Lines 353-356 Link Here
353
			return db;
377
			return db;
354
	}
378
	}
379
	/* also trying to find in idle autodatabases list */
380
	statlist_for_each_safe(item, &autodatabase_idle_list, tmp) {
381
		db = container_of(item, PgDatabase, head);
382
		if (strcmp(db->name, name) == 0) {
383
			db->inactive_time = 0;
384
			statlist_remove(&db->head, &autodatabase_idle_list);
385
			put_in_order(&db->head, &database_list, cmp_database);
386
			return db;
387
		}
388
	}
355
	return NULL;
389
	return NULL;
356
}
390
}
Lines 952-955 Link Here
952
	PktBuf tmp;
986
	PktBuf tmp;
953
	bool res;
987
	bool res;
988
	
989
	/* if the database not found, it's an auto database -> registering... */
990
	if (!db) {
991
		db = register_auto_database(dbname);
992
		if (!db)
993
			return true;
994
	}
954
995
955
	if (db->forced_user)
996
	if (db->forced_user)

Return to bug 243232