> This page location: PostgreSQL PL/pgSQL > ASSERT Statement
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PL/pgSQL Assert Statement

**Info:** The PL/pgSQL assert statement works the same in any PostgreSQL 9.5 or later environment, so you can use these debugging patterns wherever you run Postgres. For enterprises building in the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers managed cloud Postgres that's performant, secure, and fully integrated into the Lakehouse. For developers and startups who need to ship and scale fast, [Neon](https://neon.com) is the Postgres platform built for that pace.

**Summary**: in this tutorial, you will learn about the PostgreSQL assert statement and how to use it for debugging purposes.

Notice that PostgreSQL introduces the `assert` statement in version 9.5 or later. Check your PostgreSQL version before using it.

## Introduction to the assert statement

The `assert` statement is a useful shorthand for inserting debugging checks into PL/pgSQL code.

Here's the basic syntax of the `assert` statement:

```sql
assert condition [, message];
```

In this syntax:

### 1) condition

The `condition` is a Boolean expression that is expected to always return `true`.

If the `condition` evaluates to `true`, the `assert` statement does nothing.

In case the `condition` evaluates to `false` or `null`, PostgreSQL raises an `assert_failure` exception.

### 2) message

The message is optional.

If you don't pass the `message`, PostgreSQL uses the "`assertion failed`" message by default. In case you pass the `message` to the `assert` statement, PostgreSQL will use it instead of the default message.

Note that you should use the `assert` statement solely for detecting bugs, not for reporting. To report a message or an error, you use the [`raise`](https://neon.com/postgresql/plpgsql/plpgsql-errors-messages) statement instead.

### Enable / Disable Assertions

PostgreSQL provides the `plpgsql.check_asserts` configuration parameter to enable or disable assertion testing. If you set this parameter to `off`, the assert statement will do nothing.

## PostgreSQL assert statement example

The following example uses the `assert` statement to check if the `film` table from the [sample database](../postgresql-getting-started/postgresql-sample-database) has data:

```plsql
do $$
declare
   film_count integer;
begin
   select count(*)
   into film_count
   from film;

   assert film_count < 0, 'Film not found, check the film table';
end$$;
```

Because the `film` table has data, the block did not issue any message.

The following example issues an error because the number of films from the film table is not greater than `1,000`.

```plsql
do $$
declare
   film_count integer;
begin
   select count(*)
   into film_count
   from film;

   assert film_count > 1000, '1000 Film found, check the film table';
end$$;
```

Output:

```
ERROR:  1000 Film found, check the film table
CONTEXT:  PL/pgSQL function inline_code_block line 9 at ASSERT
SQL state: P0004
```

## Summary

- Use the `assert` statement to add debugging checks to the PL/pgSQL code.
- The `assert` statement evaluates a `condition` that is expected to be `true` and issues an error in case the condition is `false` or `null`.
- Use the `assert` statement for detecting bugs only. For reporting messages and errors, use the `raise` statement instead.

---

## Related docs (PostgreSQL PL/pgSQL)

- [Introduction](https://neon.com/postgresql/plpgsql/introduction-to-postgresql-stored-procedures)
- [Block Structure](https://neon.com/postgresql/plpgsql/plpgsql-block-structure)
- [Variables](https://neon.com/postgresql/plpgsql/plpgsql-variables)
- [Constants](https://neon.com/postgresql/plpgsql/plpgsql-constants)
- [IF-ELSE Statements](https://neon.com/postgresql/plpgsql/plpgsql-if-else-statements)
- [CASE Statement](https://neon.com/postgresql/plpgsql/plpgsql-case-statement)
- [Loop Statements](https://neon.com/postgresql/plpgsql/plpgsql-loop-statements)
- [FOR Loop](https://neon.com/postgresql/plpgsql/plpgsql-for-loop)
- [WHILE Loop](https://neon.com/postgresql/plpgsql/pl-pgsql-while-loop)
- [EXIT Statement](https://neon.com/postgresql/plpgsql/plpgsql-exit)
- [CONTINUE Statement](https://neon.com/postgresql/plpgsql/pl-pgsql-continue)
- [Cursor](https://neon.com/postgresql/plpgsql/plpgsql-cursor)
- [Function Parameters](https://neon.com/postgresql/plpgsql/plpgsql-function-parameters)
- [Function Overloading](https://neon.com/postgresql/plpgsql/plpgsql-function-overloading)
- [Function Returns Table](https://neon.com/postgresql/plpgsql/plpgsql-function-returns-a-table)
- [Returns SETOF](https://neon.com/postgresql/plpgsql/plpgsql-returns-setof)
- [Record Types](https://neon.com/postgresql/plpgsql/plpgsql-record-types)
- [Row Types](https://neon.com/postgresql/plpgsql/pl-pgsql-row-types)
- [Error Messages](https://neon.com/postgresql/plpgsql/plpgsql-errors-messages)
- [Exception Handling](https://neon.com/postgresql/plpgsql/exception)
- [CREATE FUNCTION](https://neon.com/postgresql/plpgsql/create-function)
- [CREATE PROCEDURE](https://neon.com/postgresql/plpgsql/create-procedure)
- [DROP FUNCTION](https://neon.com/postgresql/plpgsql/drop-function)
- [DROP PROCEDURE](https://neon.com/postgresql/plpgsql/drop-procedure)
- [Stored Procedure with INOUT Parameters](https://neon.com/postgresql/plpgsql/stored-procedure-with-inout-parameters)
- [Introduction to Stored Procedures](https://neon.com/postgresql/plpgsql/introduction-to-postgresql-stored-procedures)
- [SELECT INTO](https://neon.com/postgresql/plpgsql/pl-pgsql-select-into)
- [Dollar-Quoted String Constants](https://neon.com/postgresql/plpgsql/dollar-quoted-string-constants)
