Home > OS >  Set concatenated string as a application_name
Set concatenated string as a application_name

Time:09-15

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:

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

CodePudding user response:

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;
$$;
  • Related