0

I created a script in pgadmin4. The script consists of querying a table.

In this table I have an element which contains value1 | value2 | valueX (the number of elements may vary from row to row).

In pgadmin4 I used this script:

#!/bin/bash

psql "postgresql://id:[email protected]/table" << EOF
do $$
DECLARE
        _id int;
        _name text;
        _value text;
begin
FOR _id, _name IN select id, unnest(string_to_array(themes, '|')) from data LOOP
if EXISTS (select id_theme from theme where uri_theme = concat('<',_name,'>')) then
 insert into data_themes(data_id, theme_id) values (_id, (select id_theme from theme where uri_theme = concat('<',_name,'>')) );
RAISE NOTICE 'test % / %', _id, _name;
end if;
end loop;
end;
$$;
EOF

the script works as I want it to, in pgadmin4.

However, when I want to run this script in a bash script it gives me an error

WARNING: there is no transaction in progress COMMIT

It stops in the middle of the loop (around 25,000 lines) and shows me the error.

I put this:

\echo :AUTOCOMMIT
\set AUTOCOMMIT off
\echo :AUTOCOMMIT

I don't understand why the script works on pgadmin and doesn't work in a bash script. Thanks for your help

New contributor
Camel4488 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

I don't understand why the script works on pgadmin and doesn't work in a bash script.

Because you effectively run a different script. $$ is a variable in bash. Inside a here-document (<< EOF ... EOF) variables are expanded. Therefore, you run something like

do 1234
....
1234;

To fix this, quote the here document:

psql "postgresql://id:[email protected]/table" << 'EOF'
do $$
...
$$;
EOF
3
  • How to thank you? I went to look in all directions except that of the 'EOF'. Indeed, everything works, a big thank you !!In terms of performance, (because I have around 3,000,000 lines to record), is it good to leave the loop in the bash script or is it more efficient to put it in a function? – Camel4488 Jul 12 at 10:18
  • @Camel4488 Glad I could help. About the loop: I don't know much about postresql, so I cannot help you with that. Best to just try both versions I guess. Even though I don't think packing part of a program inside a function will improve performance in any language. After all, you still execute the same instructions. – Socowi Jul 12 at 10:21
  • On pgadmin the script is executed in 10 minutes, the with psql and the bash script already 20 minutes and it has not yet finished. – Camel4488 Jul 12 at 10:33

Your Answer

Camel4488 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.