How do I get Postgresql procedure warning messages in Golang

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 17:31:23

问题


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:


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';




回答2:


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)



来源:https://stackoverflow.com/questions/42070023/how-do-i-get-postgresql-procedure-warning-messages-in-golang

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