Postgresql 如何侦听来自Go的Postgres存储过程触发器?

Postgresql 如何侦听来自Go的Postgres存储过程触发器?,postgresql,go,Postgresql,Go,在Postgres数据库中,我有一个可以触发的存储过程: 选择“添加客户名称”、“姓氏” 我希望从我的Go代码中听到这个触发器 我所尝试的: import ( "github.com/jmoiron/sqlx" "github.com/lib/pq" ) // Code beforehand.. db, _ := sqlx.Connect("postgres", co

在Postgres数据库中,我有一个可以触发的存储过程:

选择“添加客户名称”、“姓氏”

我希望从我的Go代码中听到这个触发器

我所尝试的:

    import (
        "github.com/jmoiron/sqlx"
        "github.com/lib/pq" 
    )

    // Code beforehand..

    db, _ := sqlx.Connect("postgres", connStr)
    // Assume db is connected and works

    minReconn := 10 * time.Second
    maxReconn := time.Minute
    listener := pq.NewListener(psqlInfo, minReconn, maxReconn, reportProblem)
    err = listener.Listen("add_customer")
    if err != nil {
        return nil, err
    }

在我的主要代码中,我调用了一个等待通知的函数:

    go func() {
        somepackage.WaitForNotification()
    }()
这看起来像:


func (k *someStruct) WaitForNotification() {
    select {
    // This case is never satisfied
    case notification := <-k.db.Listener.Notify:
        k.logger.Info(fmt.Sprintf("%v", notification))
        err := doWork()
        // ...
    }
}
如注释所述,上述代码路径从未到达

搜索文档时,很明显存储的过程与Postgres中的侦听/通知事件不同

如何侦听从Go触发的存储过程?

更新您的add\u customer函数以向频道发送通知。要发送通知,请使用命令或函数

创建或替换函数add_customerfirst_name text,last_name text返回void 语言plpgsql-STRICT 作为$_$ 开始 通知add_客户; -或 选择pg_notify'add_customer'; -代码的其余部分 终止 $_$;
在Add_customer过程中添加一个NOTIFY语句或pg_NOTIFY调用,以便您可以在Golang代码中收听。我们希望避免这种情况,但是,是的,这是一种非常可能的方式。非常感谢。