PL/pgSQL anonymous code block

有些话、适合烂在心里 提交于 2019-12-29 01:37:15

问题


In PostgreSQL 9.0 I have this PLPGSQL anonymous code block:

DO $$
    DECLARE
        bigobject integer;
    BEGIN
        SELECT lo_creat(-1) INTO bigobject;
        ALTER LARGE OBJECT bigobject OWNER TO postgres;
        INSERT INTO files (id, "mountPoint", data, comment) VALUES (15, '/images/image.png', bigobject, 'image data');
        SET search_path = pg_catalog;
        SELECT pg_catalog.lo_open(bigobject, 131072);
        SELECT pg_catalog.lowrite(0, '\\x000001000100101010000000000028010000160000002800000010000000200000000100040');
        SELECT pg_catalog.lo_close(0);
        REVOKE ALL ON LARGE OBJECT bigobject FROM PUBLIC;
        REVOKE ALL ON LARGE OBJECT bigobject FROM postgres;
        GRANT ALL ON LARGE OBJECT bigobject TO postgres;
        GRANT ALL ON LARGE OBJECT bigobject TO "com.ektyn.eshops.myuser";
    END
$$;

but it fails:

ERROR:  syntax error at or near "bigobject"
LINE 6:   ALTER LARGE OBJECT bigobject OWNER TO postgres;
                             ^

********** Error **********

ERROR: syntax error at or near "bigobject"
SQL state: 42601
Character: 103

and I can't find mistake in code.


回答1:


There must be an oid constant in ALTER LARGE OBJECT oid .... Try this workaround:

DO $$
    DECLARE
        bigobject integer;
    BEGIN
        SELECT lo_creat(-1) INTO bigobject;
        EXECUTE 'ALTER LARGE OBJECT ' || bigobject::text || ' OWNER TO postgres';
        ...

The same also applies to GRANT and REVOKE, of course.




回答2:


In addition to what @klin already cleared up, you cannot use SELECT without a target in plpgsql code. Replace it with PERFORM in those calls.

Aside: Using "com.ektyn.eshops.myuser" as name for a role is a terrible idea. Use legal, lower case identifiers that don't have to be double-quoted.




回答3:


This is an artifact of the fact that PostgreSQL has two completely different kinds of SQL statements internally - plannable (SELECT, INSERT, UPDATE, and DELETE) and unplannable (everything else) statements.

Only plannable statements support query parameters.

PL/pgSQL implements variable substitutions into statements, like your bigobject, using query parameters.

Because they aren't supported for non-plannable statements, no substitution is performed. So PL/pgSQL tries to execute the statement literally, as if you'd typed:

ALTER LARGE OBJECT bigobject OWNER TO postgres;

directly at the psql prompt. It does not detect this as an error.

To work around this, use EXECUTE ... FORMAT, e.g.

EXECUTE format('ALTER LARGE OBJECT %s OWNER TO postgres', bigobject);

See this related answer about COPY.



来源:https://stackoverflow.com/questions/23586848/pl-pgsql-anonymous-code-block

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