记录 SQL 脚本错误

发布于 2024-12-15 13:40:31 字数 295 浏览 5 评论 0原文

我有一个带有以下命令的 postgres 文件的 SQL 脚本。

COPY product_master(productId, productName) FROM 'product.txt' DELIMITERS ',' CSV; 

我想处理此命令的错误(记录错误)

示例

错误:违反重复键值。

COPY 命令是否返回任何值?如果不是,那么如何记录 shell 脚本的输出?

I have a SQL script for postgres file with following command.

COPY product_master(productId, productName) FROM 'product.txt' DELIMITERS ',' CSV; 

I want to handle errors of this command (log the error)

example

ERROR: duplicate key value violates.

Does the COPY command return any value ? If no, then how to log the output of the shell script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-12-22 13:40:31

您可以在数据库日志文件中记录任何和所有消息(错误、警告等)以及大量附加信息。这是标准行为。当然,您的数据库集群必须进行配置才能执行此操作。 在此处阅读详细手册

根据您的客户端,您还应该能够从数据库服务器获取错误消息作为直接答案。请注意,错误是在与数据输出不同的流上报告的。就像 shell 中的 stoutstderr 一样。

shell 中,您可能会调用 psql -f 执行脚本。看看这个演示中发生了什么:

在 shell 中创建一个虚拟 SQL 脚本:

vim test.sql

将类似的内容放入其中:

CREATE temp table x (a int primary key, b int);
insert into x values (1,2),(3,4);
COPY x TO '/var/lib/postgres/dev/test.out';
COPY x FROM '/var/lib/postgres/dev/test.out';

执行它:

psql mydb -f test.sql

输出取决于各种 client_min_messages 这样的设置:

psql:test.sql:2: NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "x_pkey" for table "x"
CREATE TABLE
INSERT 0 2
psql:test.sql:4: ERROR:  duplicate key value violates unique constraint "x_pkey"
KONTEXT:  COPY x, line 1: "1    2"

因为我已经配置了 log_statement = all (除其他外)我的服务器日志内容如下:

2011-11-15 22:36:23 CET postgres LOG:  statement: CREATE temp table x (a int primary key, b int);
2011-11-15 22:36:23 CET postgres LOG:  statement: insert into x values (1,2),(3,4);
2011-11-15 22:36:23 CET postgres LOG:  statement: COPY x FROM '/var/lib/postgres/dev/test.out';
2011-11-15 22:36:23 CET postgres ERROR:  duplicate key value violates unique constraint "x_pkey"
2011-11-15 22:36:23 CET postgres CONTEXT:  COPY x, line 1: "1  2"
2011-11-15 22:36:23 CET postgres STATEMENT:  COPY x FROM '/var/lib/postgres/dev/test.out';

我不会在高效服务器上使用 log_statement = all。这会产生巨大日志文件。

You can log any and all messages (error, warning, ..) with a plethora of additional information in the database log file. This is standard behavior. Of course your database cluster has to be configured to do so. Read the fine manual here.

Dpending on your client you should also be able to get error messages as direct answer from the database server. Note that errors are reported on a different stream than data output. Like stout and stderr in the shell.

From the shell you would probably call psql -f to execute a script. See what happens in this demo:

Create a dummy SQL script in the shell:

vim test.sql

Put something like this into it:

CREATE temp table x (a int primary key, b int);
insert into x values (1,2),(3,4);
COPY x TO '/var/lib/postgres/dev/test.out';
COPY x FROM '/var/lib/postgres/dev/test.out';

Execute it:

psql mydb -f test.sql

Output depends on various settings like client_min_messages:

psql:test.sql:2: NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "x_pkey" for table "x"
CREATE TABLE
INSERT 0 2
psql:test.sql:4: ERROR:  duplicate key value violates unique constraint "x_pkey"
KONTEXT:  COPY x, line 1: "1    2"

Since I have configured log_statement = all (among others) my server log reads:

2011-11-15 22:36:23 CET postgres LOG:  statement: CREATE temp table x (a int primary key, b int);
2011-11-15 22:36:23 CET postgres LOG:  statement: insert into x values (1,2),(3,4);
2011-11-15 22:36:23 CET postgres LOG:  statement: COPY x FROM '/var/lib/postgres/dev/test.out';
2011-11-15 22:36:23 CET postgres ERROR:  duplicate key value violates unique constraint "x_pkey"
2011-11-15 22:36:23 CET postgres CONTEXT:  COPY x, line 1: "1  2"
2011-11-15 22:36:23 CET postgres STATEMENT:  COPY x FROM '/var/lib/postgres/dev/test.out';

I would not use log_statement = all on a productive server. That produces huge log files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文