> 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/server-programming/plpgsql.md).

# Chapter 41. PL/pgSQL — SQL Procedural Language

**Table of Contents**

[41.1. Overview](/server-programming/plpgsql/plpgsql-overview.md) : [41.1.1. Advantages of Using PL/pgSQL](/server-programming/plpgsql/plpgsql-overview.md#PLPGSQL-ADVANTAGES)

```
[41.1.2. Supported Argument and Result Data Types](plpgsql-overview.md#PLPGSQL-ARGS-RESULTS)
```

[41.2. Structure of PL/pgSQL](/server-programming/plpgsql/plpgsql-structure.md)

[41.3. Declarations](/server-programming/plpgsql/plpgsql-declarations.md) : [41.3.1. Declaring Function Parameters](/server-programming/plpgsql/plpgsql-declarations.md#PLPGSQL-DECLARATION-PARAMETERS)

```
[41.3.2. `ALIAS`](plpgsql-declarations.md#PLPGSQL-DECLARATION-ALIAS)

[41.3.3. Copying Types](plpgsql-declarations.md#PLPGSQL-DECLARATION-TYPE)

[41.3.4. Row Types](plpgsql-declarations.md#PLPGSQL-DECLARATION-ROWTYPES)

[41.3.5. Record Types](plpgsql-declarations.md#PLPGSQL-DECLARATION-RECORDS)

[41.3.6. Collation of PL/pgSQL Variables](plpgsql-declarations.md#PLPGSQL-DECLARATION-COLLATION)
```

[41.4. Expressions](/server-programming/plpgsql/plpgsql-expressions.md)

[41.5. Basic Statements](/server-programming/plpgsql/plpgsql-statements.md) : [41.5.1. Assignment](/server-programming/plpgsql/plpgsql-statements.md#PLPGSQL-STATEMENTS-ASSIGNMENT)

```
[41.5.2. Executing SQL Commands](plpgsql-statements.md#PLPGSQL-STATEMENTS-GENERAL-SQL)

[41.5.3. Executing a Command with a Single-Row Result](plpgsql-statements.md#PLPGSQL-STATEMENTS-SQL-ONEROW)

[41.5.4. Executing Dynamic Commands](plpgsql-statements.md#PLPGSQL-STATEMENTS-EXECUTING-DYN)

[41.5.5. Obtaining the Result Status](plpgsql-statements.md#PLPGSQL-STATEMENTS-DIAGNOSTICS)

[41.5.6. Doing Nothing At All](plpgsql-statements.md#PLPGSQL-STATEMENTS-NULL)
```

[41.6. Control Structures](/server-programming/plpgsql/plpgsql-control-structures.md) : [41.6.1. Returning from a Function](/server-programming/plpgsql/plpgsql-control-structures.md#PLPGSQL-STATEMENTS-RETURNING)

```
[41.6.2. Returning from a Procedure](plpgsql-control-structures.md#PLPGSQL-STATEMENTS-RETURNING-PROCEDURE)

[41.6.3. Calling a Procedure](plpgsql-control-structures.md#PLPGSQL-STATEMENTS-CALLING-PROCEDURE)

[41.6.4. Conditionals](plpgsql-control-structures.md#PLPGSQL-CONDITIONALS)

[41.6.5. Simple Loops](plpgsql-control-structures.md#PLPGSQL-CONTROL-STRUCTURES-LOOPS)

[41.6.6. Looping through Query Results](plpgsql-control-structures.md#PLPGSQL-RECORDS-ITERATING)

[41.6.7. Looping through Arrays](plpgsql-control-structures.md#PLPGSQL-FOREACH-ARRAY)

[41.6.8. Trapping Errors](plpgsql-control-structures.md#PLPGSQL-ERROR-TRAPPING)

[41.6.9. Obtaining Execution Location Information](plpgsql-control-structures.md#PLPGSQL-CALL-STACK)
```

[41.7. Cursors](/server-programming/plpgsql/plpgsql-cursors.md) : [41.7.1. Declaring Cursor Variables](/server-programming/plpgsql/plpgsql-cursors.md#PLPGSQL-CURSOR-DECLARATIONS)

```
[41.7.2. Opening Cursors](plpgsql-cursors.md#PLPGSQL-CURSOR-OPENING)

[41.7.3. Using Cursors](plpgsql-cursors.md#PLPGSQL-CURSOR-USING)

[41.7.4. Looping through a Cursor's Result](plpgsql-cursors.md#PLPGSQL-CURSOR-FOR-LOOP)
```

[41.8. Transaction Management](/server-programming/plpgsql/plpgsql-transactions.md)

[41.9. Errors and Messages](/server-programming/plpgsql/plpgsql-errors-and-messages.md) : [41.9.1. Reporting Errors and Messages](/server-programming/plpgsql/plpgsql-errors-and-messages.md#PLPGSQL-STATEMENTS-RAISE)

```
[41.9.2. Checking Assertions](plpgsql-errors-and-messages.md#PLPGSQL-STATEMENTS-ASSERT)
```

[41.10. Trigger Functions](/server-programming/plpgsql/plpgsql-trigger.md) : [41.10.1. Triggers on Data Changes](/server-programming/plpgsql/plpgsql-trigger.md#PLPGSQL-DML-TRIGGER)

```
[41.10.2. Triggers on Events](plpgsql-trigger.md#PLPGSQL-EVENT-TRIGGER)
```

[41.11. PL/pgSQL under the Hood](/server-programming/plpgsql/plpgsql-implementation.md) : [41.11.1. Variable Substitution](/server-programming/plpgsql/plpgsql-implementation.md#PLPGSQL-VAR-SUBST)

```
[41.11.2. Plan Caching](plpgsql-implementation.md#PLPGSQL-PLAN-CACHING)
```

[41.12. Tips for Developing in PL/pgSQL](/server-programming/plpgsql/plpgsql-development-tips.md) : [41.12.1. Handling of Quotation Marks](/server-programming/plpgsql/plpgsql-development-tips.md#PLPGSQL-QUOTE-TIPS)

```
[41.12.2. Additional Compile-Time and Run-Time Checks](plpgsql-development-tips.md#PLPGSQL-EXTRA-CHECKS)
```

[41.13. Porting from Oracle PL/SQL](/server-programming/plpgsql/plpgsql-porting.md) : [41.13.1. Porting Examples](/server-programming/plpgsql/plpgsql-porting.md#PLPGSQL-PORTING-EXAMPLES)

```
[41.13.2. Other Things to Watch For](plpgsql-porting.md#PLPGSQL-PORTING-OTHER)

[41.13.3. Appendix](plpgsql-porting.md#PLPGSQL-PORTING-APPENDIX)
```

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/plpgsql.html)（英文原文，待翻譯）


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/server-programming/plpgsql.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
