如何获取PostgreSQL存储过程的警告信息?

huangapple go评论82阅读模式
英文:

How to get Postgresql procedure warning messages?

问题

在运行存储过程时,该过程可以引发警告消息。

在Golang中,使用PostgreSQL驱动程序(https://github.com/lib/pq),是否有办法获取这些消息?

英文:

While running a stored procedure, the procedure can raise warning messages.

Is there any way to get these messages using Postgresql driver(https://github.com/lib/pq) in Golang ?

答案1

得分: 3

对于lib/pq驱动程序,答案是“仍然没有”。有关详细信息,请参见开放问题源代码

与此同时,另一个驱动程序https://github.com/jackc/pgx在当前(v4)版本中支持消息,详见https://godoc.org/github.com/jackc/pgconn#Notice和之前的版本(参见https://github.com/jackc/pgx/blob/v3/conn.go#L54)。

更新:根据已于2020年1月合并的PR#932,lib/pq现在支持NoticeHandler。请参见示例

英文:

For lib/pq driver, the answer is "still no".
See open issue and sources for details.

Meanwhile, another driver, https://github.com/jackc/pgx has support for messages in current (v4) version, see https://godoc.org/github.com/jackc/pgconn#Notice and earlier (see https://github.com/jackc/pgx/blob/v3/conn.go#L54)

Update: According to PR #932 which has been merged at Jan-2020, lib/pq supports NoticeHandler now. See example.

答案2

得分: 2

答案似乎是否定的。

在我的测试中,Postgres服务器似乎没有在结果中发送警告。即使发送了警告,将错误与sql.Result一起返回可能会令人困惑,而且可能需要对lib/pq进行修改。在函数中引发错误确实会返回错误,但(显然)没有结果。

如果这是一个关键要求(并且您的函数可以支持它),您可以考虑使用通知通道。请记住,这将使您的代码与Postgres绑定。

以下是我使用的函数:

CREATE OR REPLACE function fugo() RETURNS bool as $$ BEGIN RAISE WARNING 'My function notice.' USING errcode = '01000'; return TRUE; END;$$ language 'plpgsql';

英文:

The answer appears to be no.

In my tests the Postgres server did not appear to send the warning with the results. Even if it did, returning an error along with the sql.Result would be confusing at best and would require lib/pq modifications. Raising an error in the function did return an error, but (obviously) no result.

If this is a critical requirement (and your function can support it) you might consider using a notification channel. Bear in mind that this would tie your code to Postgres.

--

Here is the function I used:

CREATE OR REPLACE function fugo()
RETURNS bool as $$
BEGIN
RAISE WARNING 'My function notice.' USING errcode = '01000';
return TRUE;
END;$$
language 'plpgsql';

huangapple
  • 本文由 发表于 2017年2月6日 22:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/42070023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定