PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Stored Procedures / PL/pgSQL Block Structure

PL/pgSQL Block Structure

Summary: in this tutorial, you will learn about the block structure of PL/pgSQL. You will write and execute the first PL/pgSQL block.

A PL/pgSQL function is organized into blocks.  The following illustrates the syntax of a complete block in PL/pgSQL:

1
2
3
4
5
6
7
[ <<label>> ]
[ DECLARE
    declarations ]
BEGIN
    statements;
...
END [ label ];

Let’s examine the block structure in more detail:

  • Each block has two sections called declaration and body. The declaration section is optional while the body section is required. The block is ended with a semicolon (;) after the END keyword.
  • A block may have optional labels at the beginning and at the end. The label at the beginning and at the end must be the same. The block label is used in case you want to use the block in EXIT statement or you want to qualify the names of variables declared in the block.
  • The declaration section is where you declare all variables used within the body section. Each statement in the declaration section is terminated with a semicolon (;).
  • The body section is where you put the logic of the block. It contains any valid statements. Each statement in the body section is also terminated with a semicolon (;).

PL/pgSQL block structure example

The following example illustrates a very simple block.

1
2
3
4
5
6
7
8
DO $$
<<first_block>>
DECLARE
  counter integer := 0;
BEGIN
   counter := counter + 1;
   RAISE NOTICE 'The current value of counter is %', counter;
END first_block $$;

1
NOTICE:  The current value of counter is 1

Notice that the DO statement does not belong to the block. It is used to execute an anonymous block. PostgreSQL introduced the DO statement since version 9.0.

In the declaration section, we declared a variable named counter and set its value to 0. Inside the body section, we increased the counter to 1 and output its value using RAISE NOTICE statement. The first_block label is just for demonstration purpose. It does nothing in this example.

PL/pgSQL Subblock

You can put a block inside the body of another block. This block nested inside another is called subblock. The block that contains the subblock is referred to as an outer block.

PL/pgSQL Block Structure

You often use subblocks for grouping statements so that a large block can be divided into smaller and more logical subblocks. The variables in the subblock can have the names as the ones in the outer block, even though it is not a good practice.

When you define a variable within subblock with the same name as the one in the outer block, the variable in the outer block is hidden in the subblock. In case you want to access a variable in the outer block, you use block label to qualify its name; see the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DO $$
<<outer_block>>
DECLARE
  counter integer := 0;
BEGIN
   counter := counter + 1;
   RAISE NOTICE 'The current value of counter is %', counter;
 
   DECLARE
       counter integer := 0;
   BEGIN
       counter := counter + 10;
       RAISE NOTICE 'The current value of counter in the subblock is %', counter;
       RAISE NOTICE 'The current value of counter in the outer block is %', outer_block.counter;
   END;
 
   RAISE NOTICE 'The current value of counter in the outer block is %', counter;
  
END outer_block $$;

1
2
3
4
NOTICE:  The current value of counter is 1
NOTICE:  The current value of counter in the subblock is 10
NOTICE:  The current value of counter in the outer block is 1
NOTICE:  The current value of counter in the outer block is 1

In this example, we first declared a variable named counter in the outer_block.

Next, in the subblock, we also declared a variable with the same name.

Then, before entering into the subblock, the value of counter is 1. In the subblock, we increased the value of the counter to 10 and print it out. Notice that the change only affects the counter variable in the subblock.

After that, we referred to the counter variable in the outer block using block label to qualify its name outer_block.counter.

Finally, we print out the value counter variable in the outer block, its value remains intact.

In this tutorial, you have learned about the block structure of PL/pgSQL and how to execute a block using DO statement.

Previous Tutorial: Introduction to PostgreSQL Stored Procedures
Next Tutorial: PL/pgSQL Variables

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PL/pgSQL Getting Started

  • Introduction to Stored Procedures
  • PL/pgSQL Block Structure
  • PL/pgSQL Errors and Messages
  • PL/pgSQL Create Function
  • PL/pgSQL Function Parameters
  • PL/pgSQL Function Overloading
  • PL/pgSQL Function That Returns A Table
  • PL/pgSQL Variables
  • PL/pgSQL Constants
  • PL/pgSQL IF Statement
  • PL/pgSQL CASE Statement
  • PL/pgSQL Loop Statements
  • PL/pgSQL Cursor

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.