我可以通过 $1 将变量传递给 PostgreSQL 中的 "NOTIFY" 命令吗?
Can I pass a variable through $1 to a "NOTIFY" command in PostgreSQL?
我有以下工作代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject;
"""
plan = plpy.prepare(updateQuery)
plpy.execute(plan)
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
我现在想将一个字符串传递给我的 sql 代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject, ;
"""
plan = plpy.prepare(updateQuery, ["text"])
plpy.execute(plan, ["test"])
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
但这给了我这个错误:
ERROR: spiexceptions.SyntaxError: syntax error at or near ""
LINE 3: NOTIFY updateObject, ;
我不明白我的错误:我尝试了一个简单的 SELECT 并且成功了。但是我不能在 NOTIFY 上使用 $1。您有什么想法或方法可以通过 plpython 通知自定义字符串吗? (我的代码应该传递一个带有更新值的转换字典,所以我真的需要首先使用 plpython 然后使用 NOTIFY)
我终于找到了使用字符串的 format
方法的解决方法。
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQueryTemplate = """
NOTIFY updateObject, {value};
"""
updateQuery = updateQueryTemplate.format(value="test")
plan = plpy.prepare(updateQuery)
plpy.execute(plan)
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
我有以下工作代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject;
"""
plan = plpy.prepare(updateQuery)
plpy.execute(plan)
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
我现在想将一个字符串传递给我的 sql 代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject, ;
"""
plan = plpy.prepare(updateQuery, ["text"])
plpy.execute(plan, ["test"])
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
但这给了我这个错误:
ERROR: spiexceptions.SyntaxError: syntax error at or near ""
LINE 3: NOTIFY updateObject, ;
我不明白我的错误:我尝试了一个简单的 SELECT 并且成功了。但是我不能在 NOTIFY 上使用 $1。您有什么想法或方法可以通过 plpython 通知自定义字符串吗? (我的代码应该传递一个带有更新值的转换字典,所以我真的需要首先使用 plpython 然后使用 NOTIFY)
我终于找到了使用字符串的 format
方法的解决方法。
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQueryTemplate = """
NOTIFY updateObject, {value};
"""
updateQuery = updateQueryTemplate.format(value="test")
plan = plpy.prepare(updateQuery)
plpy.execute(plan)
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;