why does this DO block have "syntax error near declare" in pgagent?

403 views Asked by At

I have the following code:

DROP TABLE IF EXISTS pltest;
CREATE TABLE pltest (x jsonb);

DO $$
DECLARE startdate text := to_char(current_date - 1, 'YYYYMMDD');
BEGIN 

EXECUTE format(
           'COPY pltest FROM PROGRAM ''curl "https://example.com/events/start_date=%sT000000Z"''',
           startdate
        );
        
END
$$ ;

It basically imports the URL with a parameterized COPY statement so it always imports the data of the last 24 hours, it runs perfectly in SQL shell, but when i tried adding a pgagent job with that code, pgagent returns "syntax error near DECLARE".

PostgreSQL version: 13.3

PgAgent version: 13

2

There are 2 answers

2
Pavel Stehule On

This code looks correct. It fails with expected error.

ERROR:  invalid input syntax for type json
DETAIL:  Token "<" is invalid.
CONTEXT:  JSON data, line 1: <...
COPY pltest, line 1, column x: "<!doctype html>"
SQL statement "COPY pltest FROM PROGRAM 'curl "https://example.com/events /start_date=20210526T000000Z"'"
PL/pgSQL function inline_code_block line 5 at EXECUTE

Maybe your client breaks source code, or maybe your Postgres is too old.

0
AlexxxeyS On

Try to remove double dollar symbol before you push a save button. Then, after step was saved successfully, edit it and bring double dollars back and save changes. Good luck!