PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, particularly when working with procedural languages like PL/pgSQL, there's a concept of a block structure that's used to group and encapsulate related SQL statements and logic. This block structure is foundational to creating functions, procedures, and other modular code constructs.
Here's an overview of the block structure in PL/pgSQL:
[ <<label>> ] [ DECLARE -- variable declarations go here ] BEGIN -- SQL statements and PL/pgSQL commands go here EXCEPTION WHEN condition1 THEN -- handle exception WHEN condition2 THEN -- handle another exception -- ... more exception handling if needed END;
WHEN
defines a particular exception condition and how to handle it.Simple Block:
DO $$ BEGIN RAISE NOTICE 'This is a simple block'; END; $$ LANGUAGE plpgsql;
This PL/pgSQL block uses the DO
command to execute an anonymous code block that raises a notice.
Block with Variable Declaration and Exception Handling:
DO $$ DECLARE divisor INTEGER := 0; -- intentionally setting to 0 to cause an error result REAL; BEGIN result := 10 / divisor; -- This will raise an error EXCEPTION WHEN division_by_zero THEN RAISE NOTICE 'Tried to divide by zero!'; END; $$ LANGUAGE plpgsql;
In this block, we have declared a variable divisor
and initialized it to zero. Then we try to divide by it, which will raise a division by zero exception. This exception is caught and handled in the EXCEPTION
block.
You can nest blocks within other blocks to establish different scopes and levels of exception handling.
BEGIN -- outer block logic BEGIN -- inner block logic EXCEPTION -- inner block exception handling END; EXCEPTION -- outer block exception handling END;
Remember, when working with functions and procedures, you would also include other components like function parameters, return types, etc., alongside these block structures.
Declaring variables in PostgreSQL block structure:
DECLARE
keyword. Variables can store various data types.DO $$ DECLARE my_variable INT := 42; my_text_variable TEXT := 'Hello, World!'; BEGIN -- Your PL/pgSQL code here END $$;
Conditional statements in PostgreSQL block structure:
IF
, ELSIF
, and ELSE
to create conditional statements within a PL/pgSQL block.DO $$ DECLARE my_number INT := 10; BEGIN IF my_number > 0 THEN RAISE NOTICE 'Number is positive'; ELSE RAISE NOTICE 'Number is non-positive'; END IF; END $$;
Looping constructs in PL/pgSQL blocks in PostgreSQL:
FOR
, WHILE
, and LOOP
for repetitive tasks.DO $$ DECLARE counter INT := 1; BEGIN FOR counter IN 1..5 LOOP RAISE NOTICE 'Iteration: %', counter; END LOOP; END $$;
Exception handling in PostgreSQL block structure:
EXCEPTION
blocks to gracefully manage errors within a PL/pgSQL block.DO $$ BEGIN -- Your PL/pgSQL code here EXCEPTION WHEN others THEN RAISE NOTICE 'An error occurred: %', SQLERRM; END $$;
Nested blocks and scoping in PL/pgSQL:
DO $$ DECLARE outer_variable INT := 42; BEGIN DECLARE inner_variable INT := 10; BEGIN -- Access outer and inner variables here END; END $$;
Transactions within block structures in PostgreSQL:
BEGIN
and COMMIT
or ROLLBACK
.DO $$ BEGIN BEGIN -- Start of transaction UPDATE my_table SET column1 = value1 WHERE condition; EXCEPTION WHEN others THEN -- Handle error and possibly rollback changes ROLLBACK; END; -- Continue with more PL/pgSQL code COMMIT; END $$;
Returning values from PL/pgSQL blocks in PostgreSQL:
RETURN
statement to return values from PL/pgSQL blocks, especially when using the DO
anonymous block.DO $$ DECLARE result INT; BEGIN -- Your PL/pgSQL code here result := 42; RETURN result; END $$ LANGUAGE plpgsql;