IF EXISTS and IF NOT EXISTS are clauses allowing to return a notice message instead of an error if a DDL query running on a given object already exists or not depending on the DDL action done. If a given query tries to create an object when IF NOT EXISTS is specified, a notice message is returned to client if the object has already been created and nothing is done on server side. If the object is altered or dropped when IF EXISTS is used, a notice message is returned back to client if the object does not exist and nothing is done.
Prior to 9.3, PostgreSQL already proposed this feature with many objects: tables, index, functions, triggers, language, etc. Such SQL extensions are useful when running several times the same script several times and avoiding errors on environments already installed.
9.3 introduces some new flavors of IF [NOT] EXISTS completing a bit more the set of objects already supported.
- CREATE SCHEMA [IF NOT EXISTS]
- ALTER TYPE ADD VALUE [IF NOT EXISTS]
- Extension of DROP TABLE IF EXISTS such as it succeeds if the specified schema does not exists
- Note also that the new materialized views are also supported with IF [NOT] EXISTS for CREATE, ALTER and DROP.