1

我正在调试一些使用 SQLAlchemy 与 PostgreSQL 数据库交互的代码。我看到的错误表明 PostgreSQL 正在接收我的 SQL 语句,但可能不是我想要的顺序。所以,我想我应该启用语句日志来查看 PostgreSQL 实际得到了什么。

几年前我这样做了,使用中的说明

如何记录 PostgreSQL 查询?

它工作得很好。(它必须有,因为我赞成这个问题和接受的答案。)

但是,这可能是 PostgreSQL 9.x 的情况。这次我使用的是 PostgreSQL 12.1。我在“/etc/postgresql/12/main/postgresql.conf”中添加了以下几行:

log_destination = 'stderr'
logging_collector = on
log_directory = '/tmp'
log_file_mode = 0777
log_min_duration_statement = 0
log_statement = 'all'

并重新启动 PostgreSQL 服务器。日志文件被创建...

gord@gord-dv7-xubuntu0:~$ ll /tmp/postgresql-2019-12-18_115629.log 
-rw-rw-rw- 1 postgres postgres 632 Dec 18 11:56 /tmp/postgresql-2019-12-18_115629.log

...它包含一些条目...

gord@gord-dv7-xubuntu0:~$ cat /tmp/postgresql-2019-12-18_115629.log 
2019-12-18 11:56:30.478 MST [697] LOG:  database system was shut down at 2019-12-18 11:55:32 MST
2019-12-18 11:56:31.170 MST [689] LOG:  database system is ready to accept connections
2019-12-18 11:56:32.832 MST [743] postgres@template1 LOG:  statement: 
2019-12-18 11:56:32.832 MST [743] postgres@template1 LOG:  duration: 43.323 ms
2019-12-18 11:56:33.459 MST [749] postgres@template1 LOG:  statement: 
2019-12-18 11:56:33.459 MST [749] postgres@template1 LOG:  duration: 0.257 ms
2019-12-18 11:56:34.021 MST [755] postgres@template1 LOG:  statement: 
2019-12-18 11:56:34.021 MST [755] postgres@template1 LOG:  duration: 0.460 ms

...但是在运行像select 2+2;上面这样的几个查询之后,日志文件中只出现了所有内容。

我还运行了这个答案ALTER DATABASE建议的命令,即

ALTER DATABASE mydb SET log_statement = 'all';

并重新启动服务器(再次),但我仍然得到 nada。

在上述答案中未包含的最新版本的 PostgreSQL 中是否发生了一些变化?

编辑

正如对答案的评论中所指出的,这个特定的盒子上安装了 PostgreSQL 12.1 服务器PostgreSQL 10.11 服务器。我正在编辑 12.1 的配置文件,但我的应用程序正在连接到 10.11。我使用的设置是正确的,它们只是应用于错误的 postgresql.conf 副本。

4

1 回答 1

1

我不认为有什么改变。我用你的设置在 12.1 中得到了这个:

2019-12-18 15:28:56.365 EST [20298] LOG:  statement: select 2+2;
2019-12-18 15:28:56.365 EST [20298] LOG:  duration: 0.373 ms
2019-12-18 15:28:57.409 EST [20298] LOG:  statement: select 2+2;
2019-12-18 15:28:57.409 EST [20298] LOG:  duration: 0.111 ms
2019-12-18 15:28:58.370 EST [20298] LOG:  statement: select 2+2;
2019-12-18 15:28:58.370 EST [20298] LOG:  duration: 0.163 ms

您使用什么客户端提交查询?也许您的客户检查 SQL 并认为这select 2+2;是愚蠢的,它不会按书面形式提交(但后来决定提交一个空白查询?)

于 2019-12-18T20:33:22.987 回答