2

我有一个返回记录类型的存储过程。如果我 ALTER 这个存储过程返回的表(假设我添加了一个列),我必须断开我的数据库会话或重新运行CREATE OR REPLACE. 当我不这样做时,我会得到错误wrong record type supplied in RETURN NEXT

这是我在单个psql会话中运行时失败的脚本:

CREATE TABLE p1(a INT, b TEXT);

CREATE OR REPLACE FUNCTION authenticate() RETURNS SETOF p1 as '
DECLARE
    player_row p1;
BEGIN

    -- query is more complicated but always returns zero or one rows
    SELECT p.* INTO player_row
    FROM p1 p;

    IF FOUND THEN
        RETURN NEXT player_row;

        -- more code in here..
    END IF;

    RETURN;
END;' LANGUAGE plpgsql ROWS 1;


ALTER TABLE p1 ADD COLUMN c VARCHAR(2);
INSERT INTO p1 VALUES(1,'a', 'c');
SELECT * FROM AUTHENTICATE();

是否有可以自动重新编译存储过程的 Postgres 命令或设置?我尝试了@vao-tsunDISCARD PLANS在迁移后运行的建议,但不幸的是它没有帮助。

4

1 回答 1

2

您正在寻找此DO声明:

CREATE TABLE p1(a INT, b TEXT);

CREATE OR REPLACE FUNCTION authenticate() RETURNS SETOF p1 as '
DECLARE
    player_row p1;
BEGIN

    -- query is more complicated but always returns zero or one rows
    SELECT p.* INTO player_row
    FROM p1 p;

    IF FOUND THEN
        RETURN NEXT player_row;

        -- more code in here..
    END IF;

    RETURN;
END;' LANGUAGE plpgsql ROWS 1;

ALTER TABLE p1 ADD COLUMN c VARCHAR(2);
INSERT INTO p1 VALUES(1,'a', 'c');

do $$ begin execute pg_get_functiondef('authenticate'::regproc); end; $$;

SELECT * FROM AUTHENTICATE();

但正如 a_horse_with_no_name 所暗示的那样,您可以\c重新连接psql

于 2018-04-06T15:59:05.087 回答