Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/pgSQL anonymous code block

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.

like image 594
1ac0 Avatar asked May 10 '14 22:05

1ac0


People also ask

How do I create an anonymous block in PostgreSQL?

It is used to execute an anonymous block. PostgreSQL introduced the DO statement since version 9.0. In the declaration section, we declared a variable counter and set its value to zero. Inside the body section, we increased the value of the counter to one and output its value using the RAISE NOTICE statement.

Does anonymous code block execute?

Description. DO executes an anonymous code block, or in other words a transient anonymous function in a procedural language. The code block is treated as though it were the body of a function with no parameters, returning void . It is parsed and executed a single time.

What is an anonymous code block?

An anonymous block is Apex code that doesn't get stored in the metadata, but that can be compiled and executed. User Permissions Needed. To execute anonymous Apex: (Anonymous Apex execution through the API allows restricted access without the “Author Apex” permission.)

What is do $$ in PostgreSQL?

In PostgreSQL, the dollar-quoted string constants ($$) is used in user-defined functions and stored procedures. In PostgreSQL, you use single quotes for a string constant like this: select 'String constant';


1 Answers

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.

like image 85
klin Avatar answered Oct 03 '22 10:10

klin