> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/15/server-programming/pl-pgsql-sql-procedural-language.md).

# 43. PL/pgSQL - SQL Procedural Language

## 43. PL/pgSQL - SQL Procedural Language

**Table of Contents**

[43.1. Overview](/15/server-programming/pl-pgsql-sql-procedural-language/overview.md)

[43.1.1. Advantages of Using PL/pgSQL](/15/server-programming/pl-pgsql-sql-procedural-language/overview.md#PLPGSQL-ADVANTAGES)

[43.1.2. Supported Argument and Result Data Types](/15/server-programming/pl-pgsql-sql-procedural-language/overview.md#PLPGSQL-ARGS-RESULTS)

[43.2. Structure of PL/pgSQL](/15/server-programming/pl-pgsql-sql-procedural-language/structure-of-pl-pgsql.md)

[43.3. Declarations](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md)

[43.3.1. Declaring Function Parameters](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-PARAMETERS)

[43.3.2. `ALIAS`](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-ALIAS)

[43.3.3. Copying Types](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-TYPE)

[43.3.4. Row Types](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-ROWTYPES)

[43.3.5. Record Types](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-RECORDS)

[43.3.6. Collation of PL/pgSQL Variables](/15/server-programming/pl-pgsql-sql-procedural-language/declarations.md#PLPGSQL-DECLARATION-COLLATION)

[43.4. Expressions](/15/server-programming/pl-pgsql-sql-procedural-language/expressions.md)

[43.5. Basic Statements](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md)

[43.5.1. Assignment](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-ASSIGNMENT)

[43.5.2. Executing SQL Commands](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-GENERAL-SQL)

[43.5.3. Executing a Command with a Single-Row Result](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-SQL-ONEROW)

[43.5.4. Executing Dynamic Commands](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-EXECUTING-DYN)

[43.5.5. Obtaining the Result Status](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-DIAGNOSTICS)

[43.5.6. Doing Nothing At All](/15/server-programming/pl-pgsql-sql-procedural-language/basic-statements.md#PLPGSQL-STATEMENTS-NULL)

[43.6. Control Structures](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md)

[43.6.1. Returning from a Function](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-STATEMENTS-RETURNING)

[43.6.2. Returning from a Procedure](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-STATEMENTS-RETURNING-PROCEDURE)

[43.6.3. Calling a Procedure](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-STATEMENTS-CALLING-PROCEDURE)

[43.6.4. Conditionals](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-CONDITIONALS)

[43.6.5. Simple Loops](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-CONTROL-STRUCTURES-LOOPS)

[43.6.6. Looping through Query Results](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-RECORDS-ITERATING)

[43.6.7. Looping through Arrays](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-FOREACH-ARRAY)

[43.6.8. Trapping Errors](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-ERROR-TRAPPING)

[43.6.9. Obtaining Execution Location Information](/15/server-programming/pl-pgsql-sql-procedural-language/control-structures.md#PLPGSQL-CALL-STACK)

[43.7. Cursors](/15/server-programming/pl-pgsql-sql-procedural-language/43.7.-cursors.md)

[43.7.1. Declaring Cursor Variables](/15/server-programming/pl-pgsql-sql-procedural-language/43.7.-cursors.md#PLPGSQL-CURSOR-DECLARATIONS)

[43.7.2. Opening Cursors](/15/server-programming/pl-pgsql-sql-procedural-language/43.7.-cursors.md#PLPGSQL-CURSOR-OPENING)

[43.7.3. Using Cursors](/15/server-programming/pl-pgsql-sql-procedural-language/43.7.-cursors.md#PLPGSQL-CURSOR-USING)

[43.7.4. Looping through a Cursor's Result](/15/server-programming/pl-pgsql-sql-procedural-language/43.7.-cursors.md#PLPGSQL-CURSOR-FOR-LOOP)

[43.8. Transaction Management](/15/server-programming/pl-pgsql-sql-procedural-language/43.8.-transaction-management.md)

[43.9. Errors and Messages](/15/server-programming/pl-pgsql-sql-procedural-language/43.9.-errors-and-messages.md)

[43.9.1. Reporting Errors and Messages](/15/server-programming/pl-pgsql-sql-procedural-language/43.9.-errors-and-messages.md#PLPGSQL-STATEMENTS-RAISE)

[43.9.2. Checking Assertions](/15/server-programming/pl-pgsql-sql-procedural-language/43.9.-errors-and-messages.md#PLPGSQL-STATEMENTS-ASSERT)

[43.10. Trigger Functions](/15/server-programming/pl-pgsql-sql-procedural-language/43.10.-trigger-functions.md)

[43.10.1. Triggers on Data Changes](/15/server-programming/pl-pgsql-sql-procedural-language/43.10.-trigger-functions.md#PLPGSQL-DML-TRIGGER)

[43.10.2. Triggers on Events](/15/server-programming/pl-pgsql-sql-procedural-language/43.10.-trigger-functions.md#PLPGSQL-EVENT-TRIGGER)

[43.11. PL/pgSQL under the Hood](/15/server-programming/pl-pgsql-sql-procedural-language/43.11.-pl-pgsql-under-the-hood.md)

[43.11.1. Variable Substitution](/15/server-programming/pl-pgsql-sql-procedural-language/43.11.-pl-pgsql-under-the-hood.md#PLPGSQL-VAR-SUBST)

[43.11.2. Plan Caching](/15/server-programming/pl-pgsql-sql-procedural-language/43.11.-pl-pgsql-under-the-hood.md#PLPGSQL-PLAN-CACHING)

[43.12. Tips for Developing in PL/pgSQL](/15/server-programming/pl-pgsql-sql-procedural-language/43.12.-tips-for-developing-in-pl-pgsql.md)

[43.12.1. Handling of Quotation Marks](/15/server-programming/pl-pgsql-sql-procedural-language/43.12.-tips-for-developing-in-pl-pgsql.md#PLPGSQL-QUOTE-TIPS)

[43.12.2. Additional Compile-Time and Run-Time Checks](/15/server-programming/pl-pgsql-sql-procedural-language/43.12.-tips-for-developing-in-pl-pgsql.md#PLPGSQL-EXTRA-CHECKS)

[43.13. Porting from Oracle PL/SQL](/15/server-programming/pl-pgsql-sql-procedural-language/43.13.-porting-from-oracle-pl-sql.md)

[43.13.1. Porting Examples](https://docs.postgresql.tw/15/server-programming/pages/EAMrRsN5L1p0w5GNrflg#id-1.8.8.15.6)

[43.13.2. Other Things to Watch For](/15/server-programming/pl-pgsql-sql-procedural-language/43.13.-porting-from-oracle-pl-sql.md#PLPGSQL-PORTING-OTHER)

[43.13.3. Appendix](/15/server-programming/pl-pgsql-sql-procedural-language/43.13.-porting-from-oracle-pl-sql.md#PLPGSQL-PORTING-APPENDIX)

***

原文：[PostgreSQL 15.19 Documentation](/15/server-programming/pl-pgsql-sql-procedural-language.md)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/15/server-programming/pl-pgsql-sql-procedural-language.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
