Skip to content

Commit

Permalink
plpgsql NULL and RETURN STATEMENTS (#18623)
Browse files Browse the repository at this point in the history
* plpgsql RETURN statements with no expression
* plpgsql NULL statements
  • Loading branch information
taroface authored Jun 5, 2024
1 parent 6a419be commit 095147c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/current/v24.1/create-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If you grant `EXECUTE` privilege as a default privilege at the database level, n
Parameter | Description
----------|------------
`routine_create_name` | The name of the function.
`routine_param` | A comma-separated list of function parameters.
`routine_param` | A comma-separated list of function parameters, specifying the mode, name, and type.
`routine_return_type` | The type returned by the function.
`routine_body_str` | The body of the function. For allowed contents, see [User-Defined Functions]({% link {{ page.version.version }}/user-defined-functions.md %}#overview).

Expand Down
4 changes: 2 additions & 2 deletions src/current/v24.1/create-procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ If you grant `EXECUTE` privilege as a default privilege at the database level, n
| Parameter | Description |
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| `routine_create_name` | The name of the procedure. |
| `routine_param` | A comma-separated list of procedure parameters. |
| `routine_body_str` | The body of the procedure. For allowed contents, see [Stored Procedures]({% link {{ page.version.version }}/stored-procedures.md %}#structure). |
| `routine_param` | A comma-separated list of procedure parameters, specifying the mode, name, and type. |
| `routine_body_str` | The body of the procedure. For allowed contents, see [Stored Procedures]({% link {{ page.version.version }}/stored-procedures.md %}#structure). |

## Examples

Expand Down
35 changes: 32 additions & 3 deletions src/current/v24.1/plpgsql.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ At the highest level, a PL/pgSQL block looks like the following:
END
~~~

PL/pgSQL blocks can be nested. An optional label can be placed above each block. Block labels can be targeted by [`EXIT` statements](#exit-and-continue-statements).
PL/pgSQL blocks can be nested. An optional label can be placed above each block. Block labels can be targeted by [`EXIT` statements](#exit).

~~~ sql
[ <<outer_block>> ]
Expand Down Expand Up @@ -217,7 +217,6 @@ IF condition THEN

`IF ... THEN ... ELSIF` executes statements if a boolean condition is true. If the condition is false, each `ELSIF` condition is evaluated until one is true. The corresponding `ELSIF` statements are executed. If no `ELSIF` conditions are true, no statements are executed unless an `ELSE` clause is included, in which case the `ELSE` statements are executed.

{% include_cached copy-clipboard.html %}
~~~ sql
IF condition THEN
statements;
Expand All @@ -230,6 +229,14 @@ IF condition THEN
END IF;
~~~

`IF`, `ELSE`, and `ELSIF` conditions are not required to execute statements. You can exclude any statements or add a placeholder `NULL` statement.

~~~ sql
IF condition THEN
NULL;
END IF;
~~~

For usage examples of conditional statements, see [Examples](#examples).

### Write loops
Expand All @@ -254,7 +261,9 @@ WHILE condition LOOP

For an example, see [Create a stored procedure that uses a `WHILE` loop]({% link {{ page.version.version }}/create-procedure.md %}#create-a-stored-procedure-that-uses-a-while-loop).

### `EXIT` and `CONTINUE` statements
### Control execution flow

#### `EXIT`

Add an `EXIT` statement to end a [loop](#write-loops). An `EXIT` statement can be combined with an optional `WHEN` boolean condition.

Expand Down Expand Up @@ -299,6 +308,18 @@ CREATE PROCEDURE p() AS $$
$$ LANGUAGE PLpgSQL;
~~~

#### `RETURN`

Add a `RETURN` statement to a routine with an `OUT` parameter or `VOID` return type to exit the routine immediately.

~~~ sql
BEGIN
...
RETURN;
~~~

#### `CONTINUE`

Add a `CONTINUE` statement to end the current iteration of a [loop](#write-loops), skipping any statements below `CONTINUE` and beginning the next iteration of the loop.

A `CONTINUE` statement can be combined with an optional `WHEN` boolean condition. In the following example, if a `WHEN` condition is defined and met, then `CONTINUE` causes the loop to skip the second group of statements and begin again.
Expand Down Expand Up @@ -459,6 +480,14 @@ BEGIN
END
~~~

`WHEN` conditions are not required to execute statements. You can exclude any statements or add a placeholder `NULL` statement.

~~~ sql
EXCEPTION
WHEN error THEN
NULL;
~~~

### Control transactions

Use a `COMMIT` or `ROLLBACK` statement within a PL/pgSQL [stored procedure]({% link {{ page.version.version }}/stored-procedures.md %}) to finish the current transaction and automatically start a new one.
Expand Down

0 comments on commit 095147c

Please sign in to comment.