Statement logging in PostgreSQL 12.1. Has something changed from earlier versions?

丶灬走出姿态 提交于 2021-02-11 14:34:20

问题


I'm debugging some code that uses SQLAlchemy to interact with a PostgreSQL database. The errors I'm seeing suggest that PostgreSQL is receiving my SQL statements, but perhaps not in the order I intended. So, I thought I'd enable statement logging to see what PostgreSQL is actually getting.

I did that a few years ago, using the instructions in

How to log PostgreSQL queries?

and it worked just fine. (It must have, because I upvoted that question and the accepted answer.)

However, that was probably with PostgreSQL 9.x. This time I'm working with PostgreSQL 12.1. I added the following lines to "/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'

and restarted the PostgreSQL server. The log file gets created ...

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

... and it contains some entries ...

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

... but after running several queries like select 2+2; the above is all that ever appears in the log file.

I also ran the ALTER DATABASE command suggested by this answer, i.e.,

ALTER DATABASE mydb SET log_statement = 'all';

and restarted the server (again), but still I get nada.

Has something changed in the more recent versions of PostgreSQL that hasn't been included in the aforementioned answers?

Edit

As noted in the comments to the answer, this particular box had both PostgreSQL 12.1 server and PostgreSQL 10.11 server installed on it. I was editing the configuration file for 12.1, but my application was connecting to 10.11. The settings I was using were correct, they were just applied to the wrong copy of postgresql.conf. 🤦


回答1:


I don't think anything has changed. I get this in 12.1 with your settings:

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

What client are you using to submit your queries? Maybe your client inspects SQL and decided that select 2+2; was dumb and it wasn't going to submit it as written (but then decided to submit a blank query instead?)



来源:https://stackoverflow.com/questions/59399024/statement-logging-in-postgresql-12-1-has-something-changed-from-earlier-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!