Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set concatenated string as a application_name

I’m trying to set application_name with some concatenated string as shown below in the example:

Example:

do
$$
declare var1 text := 'Text1';
        var2 text := 'Text2';
        result text;
begin
    set application_name = var1||'-'||var2;
    
    select application_name into result 
    from pg_stat_activity where pid = pg_backend_pid();

    raise info '%',result;
end;
$$;

But getting an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SQL Error [42601]: ERROR: syntax error at or near "||" Position: 116

>Solution :

Use format to concatenate the string and then execute it, e.g.

do
$$
declare var1 text := 'Text1';
        var2 text := 'Text2';
        result text;
begin    
    execute format('set application_name=%s',quote_ident(var1||'-'||var2));
    select application_name into result 
    from pg_stat_activity where pid = pg_backend_pid();    
    raise info '%',result;
end;
$$;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading