我需要编写一个删除PostgreSQL数据库的脚本.它可能有很多连接,但脚本应该忽略它.
DROP DATABASE db_name当存在打开的连接时,标准查询不起作用.
我该如何解决这个问题?
有些客户端连接到我们的postgresql数据库,但保持连接打开.是否有可能告诉Postgresql在一定量的不活动后关闭这些连接?
TL; DR
如果您使用的是Postgresql版本> =
9.2
那么请使用我提出的解决方案如果您不想编写任何代码,
那么使用arqnid的解决方案
根据PostgresSQL 文档,有参数keepalives和更多参数keepalives_idle,keepalives_interval并且keepalives_count仅在以下情况下才相关keepalives=1。
但我不清楚如果参数keepalives设置为零意味着什么?
keepalives如果参数设置为零 ( )到底会发生什么keepalives=0?
这是否意味着 PostgreSQL 会话永远不会终止?如果客户端应用程序关闭会发生什么?如果网络连接中断会发生什么?这是否意味着 PostgreSQL 服务器永远不会终止空闲会话?
我的堆栈包括 django (1.4.3)、psycopg2 (0.0.3) 和 postgres (9.1)。此外,我正在使用 psycogreen.gevent.patch_psycopg,因为我正在使用 gunicorn 和 gevent 提供我的 django。
一切似乎都很愉快,但我得到了很多(约 40 个)打开的数据库连接。直到我在 django 数据库设置中将 'autocommit' 设置为 True 之前,它们都处于“事务空闲”状态。现在他们都只是“空闲”。
这是我的 pg_top 输出示例。
last pid: 22043; load avg: 0.09, 0.05, 0.05; up 6+21:49:58 16:21:08
45 processes: 45 sleeping
CPU states: 3.0% user, 0.9% nice, 0.2% system, 96.0% idle, 0.0% iowait
Memory: 871M used, 130M free, 32M buffers, 530M cached
Swap: 10M used, 246M free, 2192K cached
PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND
10035 …Run Code Online (Sandbox Code Playgroud) 如果在当前打开与 RDS 的连接时轮换机密,该连接是否仍然能够查询数据库,还是会变为非活动状态?
我不时收到“致命:抱歉,已经有太多客户”,因为我在 Postgres 中有很多空闲连接,我无法理解它们来自哪里或如何阻止它们。
起初我CONN_MAX_AGE在 Django 中尝试了设置,但它似乎没有效果。
我也在idle_in_transaction_session_timeoutPostgres 中设置为 5 分钟,但我一直看到很多空闲事务:
postgres=# select client_addr, state, count(*) from pg_stat_activity group by client_addr, state;
client_addr | state | count
---------------+--------+-------
| | 5
| active | 1
| idle | 1
172.30.12.148 | idle | 2
172.30.12.74 | idle | 89
(5 rows)
Run Code Online (Sandbox Code Playgroud)
postgres=# select client_addr, state, backend_start, query_start from pg_stat_activity order by query_start ;
client_addr | state | backend_start | query_start
---------------+--------+-------------------------------+-------------------------------
| idle | 2020-03-24 20:03:16.060707+00 | …Run Code Online (Sandbox Code Playgroud) 春季启动 2.1.2。
2 个环境:local(postgres 11)和qa(docker swarm with postgres 10 和应用程序)
后maxLifetime传球,阿光重新创建连接(关闭旧的,创造新的),但对qa环境的连接没有得到关闭:与检查select * from pg_stat_activity wheredataname=...
,并在日志中,我们有:
2019-09-25 12:00:20.694 DEBUG 6 --- [nnection closer] c.z.h.p.PoolBase : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@54d41592: (connection has passed maxLifetime)
2019-09-25 12:00:20.699 DEBUG 6 --- [onnection adder] c.z.h.p.HikariPool : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@29e14ddb
2019-09-25 12:00:26.372 DEBUG 6 --- [nnection closer] c.z.h.p.PoolBase : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@11684c62: (connection has passed maxLifetime)
2019-09-25 12:00:26.376 DEBUG …Run Code Online (Sandbox Code Playgroud) 我正在像这样的代码(简化)中运行事务查询:
try {
runQuery("begin");
runQuery("some query ...");
runQuery("some other query ...");
runQuery("some more query ...");
runQuery("some extra query ...");
runQuery("commit");
} catch (e){
runQuery("rollback");
}
Run Code Online (Sandbox Code Playgroud)
这是来自postgres日志文件的语句列表。
请注意,第一次开始/提交工作正常,第二次提交给出了主题中提到的错误:
2015-02-18 20:19:17 UTC [6459-7] vdsr@sails LOG: statement: begin
2015-02-18 20:19:17 UTC [6459-8] vdsr@sails LOG: execute <unnamed>: INSERT INTO "groups" ("name", "parentGroupRef", "isCompany", "companyRef", "createdAt", "updatedAt") values ($1, $2, $3, $4, $5, $6) RETURNING *
2015-02-18 20:19:17 UTC [6459-9] vdsr@sails DETAIL: parameters: $1 = 'testclient', $2 = '5', $3 = 't', $4 = '1', …Run Code Online (Sandbox Code Playgroud) postgresql ×6
connection ×2
django ×2
amazon-rds ×1
commit ×1
docker-swarm ×1
gevent ×1
gunicorn ×1
hikaricp ×1
libpq ×1
logging ×1
psycopg2 ×1
python ×1
spring-boot ×1
timeout ×1
transactions ×1