Snowflake Postgres 日志记录¶
检索 Postgres 日志数据¶
All Postgres servers on Snowflake Postgres instances log locally to syslog. The log data is collected from there and sent to your account's active event table.
要查看某个实例的 Postgres 日志,请在事件表中查询满足 record_type = 'LOG' 条件的记录,并使用实例主机名的起始部分(即实例 ID,或称为 cluster_id)来筛选 TIMESTAMP 字段以及 VALUE 字段中的 MESSAGE 部分。例如,以下查询将提取实例 ID 为 oyrpb2cwtvbu5al5vtbyrsnkgy 的最近 10 分钟日志条目:
SELECT TIMESTAMP, VALUE:MESSAGE as log_line
FROM SNOWFLAKE.TELEMETRY.EVENTS
WHERE resource_attributes['snowflake.o11y.logtype'] = 'postgres-otelcol-vm-agent'
AND resource_attributes['instance.id'] = 'oyrpb2cwtvbu5al5vtbyrsnkgy'
AND record_type = 'LOG'
AND TIMESTAMP > CURRENT_TIMESTAMP() - INTERVAL '10 MINUTES'
LIMIT 100;
备注
上述查询使用的是账户的默认事件表 SNOWFLAKE.TELEMETRY.EVENTS。如果您配置了自定义事件表,则相应地调整查询语句。
Each row of the output will contain a single log-line entry that was logged by the Postgres server on the given Snowflake Postgres instance with the timestamp when it was originally logged. Note that it can take up to a few minutes between the time Postgres makes a log entry and it is available in the event table.
理解 Postgres 日志行交错现象¶
Note that Postgres uses multi-line logging and since multiple Postgres server processes will be making log entries concurrently, full log entries from different Postgres server processes will often be interleaved. For example, let's consider these log line entries:
timestamp |
log_line |
2025-12-09 23:16:38.760 |
"[14-1] [1592908][client backend][27/2][0] [user=snowflake_admin,db=postgres,app=psql] [34.214.158.144] ERROR: canceling statement due to user request" |
2025-12-09 23:16:38.760 |
"[10-1] [1593992][not initialized][][0] [user=[unknown],db=[unknown],app=[unknown]] [34.214.158.144] LOG: connection received: host=34.214.158.144 port=46114" |
2025-12-09 23:16:38.760 |
"[14-2] [1592908][client backend][27/2][0] [user=snowflake_admin,db=postgres,app=psql] [34.214.158.144] STATEMENT: select pg_sleep(10);" |
2025-12-09 23:16:43.007 |
"[15-1] [1592908][client backend][27/3][0] [user=snowflake_admin,db=postgres,app=psql] [34.214.158.144] LOG: AUDIT: SESSION,2,1,MISC,SHOW,,,show log_min_duration_statement,<not logged>" |
在每个日志行条目中:
第一个方括号内的值表示运行该命令的会话中的命令编号,以及该命令对应的日志行编号,两者之间以连字符分隔。例如,[1-1] 和 [1-2] 表示某个会话中第一个命令生成的两条日志行。
The second bracketed value is the process ID (pid) for the session that logged the line. Postgres uses a process-based (vs. thread-based) concurrency model so each session is run on its own server process.
在此示例中,您可以看到:
命令 14 是由 pid 为
1592908的会话执行的,其作用是取消一个select pg_sleep(10);查询。pid 为
1592908的会话在记录命令 14 时生成了两条日志行:[14-1] 和 [14-2]。pid 为
1593992的会话执行的第 10 个命令所产生的一条日志行,最终插入到了 pid 为1592908的命令 14 的两条日志行之间。pid 为
1592908的会话随后执行的下一个命令是一个show log_min_duration_statement查询,仅生成了一条日志行:[15-1]。
小技巧
Postgres 的日志行格式由其服务器设置 log_line_prefix (https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-LOG-LINE-PREFIX) 决定,在 Snowflake Postgres 实例中,其默认值为“[%p][%b][%v][%x] %q[user=%u,db=%d,app=%a] [%h]”。