> 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/pltcl/pltcl-dbaccess.md).

# 42.5. Database Access from PL/Tcl \#

In this section, we follow the usual Tcl convention of using question marks, rather than brackets, to indicate an optional element in a syntax synopsis. The following commands are available to access the database from the body of a PL/Tcl function:

`spi_exec ?-count n? ?-array name? command ?loop-body?` : Executes an SQL command given as a string. An error in the command causes an error to be raised. Otherwise, the return value of `spi_exec` is the number of rows processed (selected, inserted, updated, or deleted) by the command, or zero if the command is a utility statement. In addition, if the command is a `SELECT` statement, the values of the selected columns are placed in Tcl variables as described below.

````
The optional `-count` value tells
`spi_exec` to stop
once *`n`* rows have been retrieved,
much as if the query included a `LIMIT` clause.
If *`n`* is zero, the query is run to
completion, the same as when `-count` is omitted.

If the command is a `SELECT` statement, the values of the
result columns are placed into Tcl variables named after the columns.
If the `-array` option is given, the column values are
instead stored into elements of the named associative array, with the
column names used as array indexes. In addition, the current row
number within the result (counting from zero) is stored into the array
element named “`.tupno`”, unless that name is
in use as a column name in the result.

If the command is a `SELECT` statement and no *`loop-body`*
script is given, then only the first row of results are stored into
Tcl variables or array elements; remaining rows, if any, are ignored.
No storing occurs if the query returns no rows. (This case can be
detected by checking the result of `spi_exec`.)
For example:

```

spi_exec "SELECT count(*) AS cnt FROM pg_proc"
```

will set the Tcl variable `$cnt` to the number of rows in
the `pg_proc` system catalog.

If the optional *`loop-body`* argument is given, it is
a piece of Tcl script that is executed once for each row in the
query result. (*`loop-body`* is ignored if the given
command is not a `SELECT`.)
The values of the current row's columns
are stored into Tcl variables or array elements before each iteration.
For example:

```

spi_exec -array C "SELECT * FROM pg_class" {
    elog DEBUG "have table $C(relname)"
}
```

will print a log message for every row of `pg_class`. This
feature works similarly to other Tcl looping constructs; in
particular `continue` and `break` work in the
usual way inside the loop body.

If a column of a query result is null, the target
variable for it is “unset” rather than being set.
````

`spi_prepare` *`query`* *`typelist`* : Prepares and saves a query plan for later execution. The saved plan will be retained for the life of the current session.

```
The query can use parameters, that is, placeholders for
values to be supplied whenever the plan is actually executed.
In the query string, refer to parameters
by the symbols `$1` ... `$n`.
If the query uses parameters, the names of the parameter types
must be given as a Tcl list. (Write an empty list for
*`typelist`* if no parameters are used.)

The return value from `spi_prepare` is a query ID
to be used in subsequent calls to `spi_execp`. See
`spi_execp` for an example.
```

`spi_execp ?-count n? ?-array name? ?-nulls string? queryid ?value-list? ?loop-body?` : Executes a query previously prepared with `spi_prepare`. *`queryid`* is the ID returned by `spi_prepare`. If the query references parameters, a *`value-list`* must be supplied. This is a Tcl list of actual values for the parameters. The list must be the same length as the parameter type list previously given to `spi_prepare`. Omit *`value-list`* if the query has no parameters.

````
The optional value for `-nulls` is a string of spaces and
`'n'` characters telling `spi_execp`
which of the parameters are null values. If given, it must have exactly the
same length as the *`value-list`*. If it
is not given, all the parameter values are nonnull.

Except for the way in which the query and its parameters are specified,
`spi_execp` works just like `spi_exec`.
The `-count`, `-array`, and
*`loop-body`* options are the same,
and so is the result value.

Here's an example of a PL/Tcl function using a prepared plan:

```

CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS $$
    if {![ info exists GD(plan) ]} {
        # prepare the saved plan on the first call
        set GD(plan) [ spi_prepare \
                "SELECT count(*) AS cnt FROM t1 WHERE num >= \$1 AND num <= \$2" \
                [ list int4 int4 ] ]
    }
    spi_execp -count 1 $GD(plan) [ list $1 $2 ]
    return $cnt
$$ LANGUAGE pltcl;
```

We need backslashes inside the query string given to
`spi_prepare` to ensure that the
`$n` markers will be passed
through to `spi_prepare` as-is, and not replaced by Tcl
variable substitution.
````

`subtransaction` *`command`* : The Tcl script contained in *`command`* is executed within an SQL subtransaction. If the script returns an error, that entire subtransaction is rolled back before returning the error out to the surrounding Tcl code. See [Section 42.9](/server-programming/pltcl/pltcl-subtransactions.md) for more details and an example.

`quote` *`string`* : Doubles all occurrences of single quote and backslash characters in the given string. This can be used to safely quote strings that are to be inserted into SQL commands given to `spi_exec` or `spi_prepare`. For example, think about an SQL command string like:

````
```

"SELECT '$val' AS ret"
```

where the Tcl variable `val` actually contains
`doesn't`. This would result
in the final command string:

```

SELECT 'doesn't' AS ret
```

which would cause a parse error during
`spi_exec` or
`spi_prepare`.
To work properly, the submitted command should contain:

```

SELECT 'doesn''t' AS ret
```

which can be formed in PL/Tcl using:

```

"SELECT '[ quote $val ]' AS ret"
```

One advantage of `spi_execp` is that you don't
have to quote parameter values like this, since the parameters are never
parsed as part of an SQL command string.
````

`elog` *`level`* *`msg`*: Emits a log or error message. Possible levels are `DEBUG`, `LOG`, `INFO`, `NOTICE`, `WARNING`, `ERROR`, and `FATAL`. `ERROR` raises an error condition; if this is not trapped by the surrounding Tcl code, the error propagates out to the calling query, causing the current transaction or subtransaction to be aborted. This is effectively the same as the Tcl `error` command. `FATAL` aborts the transaction and causes the current session to shut down. (There is probably no good reason to use this error level in PL/Tcl functions, but it's provided for completeness.) The other levels only generate messages of different priority levels. Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the [log\_min\_messages](/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-MIN-MESSAGES) and [client\_min\_messages](/server-administration/runtime-config/runtime-config-client.md#GUC-CLIENT-MIN-MESSAGES) configuration variables. See [Chapter 19](/server-administration/runtime-config.md) and [Section 42.8](/server-programming/pltcl/pltcl-error-handling.md) for more information.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/pltcl-dbaccess.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/pltcl/pltcl-dbaccess.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.
