简体   繁体   English

在Postgres日志记录机制中更改定界符和换行符

[英]Change delimiter and newline character in Postgres logging mechanism

I have enabled logging in postgres. 我已启用登录postgres。 It logs in CSV. 它登录CSV。 The ninth column is the query that's actually executed. 第九列是实际执行的查询。 I want to run a cut command to pull that 9th column, but the issue is that the commas and newlines in the query would break the parser. 我想运行一个cut命令来拉第9列,但是问题是查询中的逗号和换行符会破坏解析器。 Is there any way to change the delimiter and the newline character? 有什么方法可以更改定界符和换行符吗?

As suggested in PostgreSQL Manual you can use postgres to parse its own logs: PostgreSQL手册中所建议,您可以使用postgres解析其自己的日志:

  • create temporary table; 创建临时表;
  • import log file into it; 将日志文件导入其中;
  • select required data. 选择所需的数据。

Here is simple bash script to automate the process. 这是使过程自动化的简单bash脚本。

#!/usr/bin/env bash

psql -t << EOF
CREATE TEMPORARY TABLE postgres_log
(
  log_time timestamp(3) with time zone,
  user_name text,
  database_name text,
  process_id integer,
  connection_from text,
  session_id text,
  session_line_num bigint,
  command_tag text,
  session_start_time timestamp with time zone,
  virtual_transaction_id text,
  transaction_id bigint,
  error_severity text,
  sql_state_code text,
  message text,
  detail text,
  hint text,
  internal_query text,
  internal_query_pos integer,
  context text,
  query text,
  query_pos integer,
  location text,
  application_name text,
  PRIMARY KEY (session_id, session_line_num)
);

COPY postgres_log FROM '$1' WITH csv;

SELECT query from postgres_log;
EOF

You can run it as: ./fetch-queries.sh /full/path/to/logfile.csv 您可以将其运行为: ./fetch-queries.sh /full/path/to/logfile.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM