Search code examples
sqlpostgresqlassert

Include parameters in posgresql assert message


Is there a way to include parameters in PostgreSQL ASSERT messages.

Example:

do $$ 
declare
    c integer;
begin
    c := (select count(*) from pg_database);
    assert c = 7, 'not 7!';
    assert c = 8, 'not 8!';
    assert c = 5, ('% not 5!' , c);
end;$$;

This works:

assert c = 8, 'not 8!';

This assert:

assert c = 5, ('% not 5!' , c);

show an error message as it should:

SQL Error [P0004]: ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT
ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT
ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT

but the variable do not replace the % in the text.


Solution

  • Use FORMAT:

    ASSERT c = 5, FORMAT('%s not 5!', c);