Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example use of ASSERT with PostgreSQL

After reading the documentation for ASSERT, I am still confused how to use it, and can't find any examples online of how I would do something simple using ASSERT in a .sql script.

For example, say I want to ASSERT that the number of rows returned from SELECT * FROM my_table WHERE my_col = 3 is equal to 10.

Can someone provide a working example of that?

like image 396
atp Avatar asked May 08 '18 15:05

atp


People also ask

What is true for assertion in PostgreSQL?

The ASSERT statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions. ASSERT condition [ , message ]; The condition is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT statement does nothing further.

How do I get an error message in PostgreSQL?

Users can control where these error messages will be reported (i.e., on client screen, server logs or on both) by setting the postgresql. conf parameters “log_min_messages” and “client_min_messages.” Format specifies the error message that the user wants to display.

How do I write a raise notice in PostgreSQL?

The code that follows serves to illustrate this point. RAISE INFO 'Hello World !'; RAISE NOTICE '%', variable_name; RAISE NOTICE 'Current value of parameter (%)', my_var; RAISE EXCEPTION '% cannot have null salary', EmpName; Raise Notice In Postgresql Function.

How do you handle user defined exceptions in PostgreSQL?

if it is an user defined exception, delete the corresponding declaration and specify unique error code via ERRCODE in a USING clause. in catch-block replace SQLCODE by SQLSTATE.


1 Answers

I would assume you try todo smth similar?

so=# select count(*) from pg_database;
 count
-------
    21
(1 row)

so=# do $$ begin assert (select count(*) from pg_database) = 21, 'not 21!';end;$$;
DO
so=# do $$ begin assert (select count(*) from pg_database) = 22, 'not 22!';end;$$;
ERROR:  not 22!
CONTEXT:  PL/pgSQL function inline_code_block line 1 at ASSERT
like image 164
Vao Tsun Avatar answered Sep 23 '22 20:09

Vao Tsun