如何从Go应用程序中查看PostgreSQL的”raise notice”消息?

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

how to view postgresql "raise notice" from go app

问题

我正在使用Go编写一个小应用程序。现在我需要在应用程序控制台中记录PostgreSQL的"raise notice"消息。

我没有找到在*sql.Conn中存储PostgreSQL引发的消息的位置(我使用的是带有lib/pq驱动程序的go/sql)。

例如,

create function show_mes() returns int as $$
begin
     raise notice 'test message from pg';
     return 1;
end
$$ language plpgsql;

从Go应用程序中,我调用这个函数并可以得到结果。

但是我如何从Go应用程序中访问那些消息"test message from pg"呢?

在当前版本中,我们在控制台中记录消息以进行调试:

如何从Go应用程序中查看PostgreSQL的”raise notice”消息?

谢谢!

英文:

I'm writting small app in go. And now I need to log postgres "raise notice" message in app console.

I was not found where postgresql raised messages stored in *sql.Conn (I'm use go/sql with lib/pq driver)

example,

create function show_mes() returns int as $$
begin
     raise notice 'test message from pg';
     return 1;
end
$$ language plpgsql;

from go app I call this function and can get the result.

But how can i access to that messages "test message from pg" from go app?

In current version written in Node we log messages in console for debbugging:

如何从Go应用程序中查看PostgreSQL的”raise notice”消息?

thank you!

答案1

得分: 1

如果你正在使用lib/pq,你可以使用它的LISTEN/NOTIFY功能来实现你想要的效果。

在Go端,你可以使用类似以下的代码:

package main

import (
    "log"
    "time"

    "github.com/lib/pq"
)

func main() {
    dburl := "postgres:///?sslmode=disable"
    li := pq.NewListener(dburl, 10*time.Second, time.Minute, func(event pq.ListenerEventType, err error) {
        if err != nil {
            log.Fatal(err)
        }
    })

    if err := li.Listen("raise_notice"); err != nil {
        panic(err)
    }

    for {
        select {
        case n := <-li.Notify:
            // n.Extra contains the payload from the notification
            log.Println("notification:", n.Extra)
        case <-time.After(5 * time.Minute):
            if err := li.Ping(); err != nil {
                panic(err)
            }
        }
    }
}

然后你的PostgreSQL函数应该是这样的:

CREATE FUNCTION show_mes() RETURNS int AS $$
BEGIN
     PERFORM pg_notify('raise_notice', 'test message from pg');
     RETURN 1;
END
$$ LANGUAGE plpgsql;

以上是你要翻译的内容。

英文:

If you're using lib/pq you can use its LISTEN/NOTIFY support to achieve what you want.

On the Go side you can use something like this:

package main

import (
    &quot;log&quot;
    &quot;time&quot;

    &quot;github.com/lib/pq&quot;
)

func main() {
    dburl := &quot;postgres:///?sslmode=disable&quot;
    li := pq.NewListener(dburl, 10*time.Second, time.Minute, func(event pq.ListenerEventType, err error) {
	    if err != nil {
		    log.Fatal(err)
	    }
    })

    if err := li.Listen(&quot;raise_notice&quot;); err != nil {
	    panic(err)
    }

    for {
	    select {
	    case n := &lt;-li.Notify:
            // n.Extra contains the payload from the notification
		    log.Println(&quot;notification:&quot;, n.Extra)
	    case &lt;-time.After(5 * time.Minute):
		    if err := li.Ping(); err != nil {
			    panic(err)
		    }
	    }
    }
}

Then your postgres function would look like this:

CREATE FUNCTION show_mes() RETURNS int AS $$
BEGIN
     PERFORM pg_notify(&#39;raise_notice&#39;, &#39;test message from pg&#39;);
     RETURN 1;
END
$$ LANGUAGE plpgsql;

答案2

得分: 0

我已经创建了一个示例:

https://github.com/jackc/pgx/issues/838

使用pgx驱动程序

在连接中使用回调函数处理PostgreSQL消息

英文:

I have created example:

https://github.com/jackc/pgx/issues/838

with pgx driver

Call back function in connection for postgres messages

huangapple
  • 本文由 发表于 2017年3月14日 22:55:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/42789361.html
匿名

发表评论

匿名网友

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

确定