

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# PG\$1LAST\$1COPY\$1ID
<a name="PG_LAST_COPY_ID"></a>

返回当前会话中最近完成的 COPY 命令的查询 ID。如果当前会话中尚未运行任何 COPY 命令，PG\$1LAST\$1COPY\$1ID 将返回 -1。

 当 COPY 命令开始加载过程时，将更新 PG\$1LAST\$1COPY\$1ID 的值。如果 COPY 因加载数据无效而失败，则会更新 COPY ID，因此您可在查询 STL\$1LOAD\$1ERRORS 表时使用 PG\$1LAST\$1COPY\$1ID。如果回滚了 COPY 事务，则不会更新 COPY ID。

如果 COPY 命令因加载过程开始前出现的错误（如语法错误、访问错误、凭证无效或权限不足）而失败，则不会更新 COPY ID。如果 COPY 在分析压缩步骤（在成功连接之后、数据加载之前开始）中失败，则将不会更新 COPY ID。

## 语法
<a name="PG_LAST_COPY_ID-synopsis"></a>

```
pg_last_copy_id()
```

## 返回类型
<a name="PG_LAST_COPY_ID-return-type"></a>

返回整数。

## 示例
<a name="PG_LAST_COPY_ID-example"></a>

以下查询返回当前会话中的最新 COPY 命令的查询 ID。

```
select pg_last_copy_id();

pg_last_copy_id
---------------
          5437
(1 row)
```

以下查询将 STL\$1LOAD\$1ERRORS 联接到 STL\$1LOADERROR\$1DETAIL 以查看当前会话中的最近一次加载过程中发生的错误的详细信息：

```
select d.query, substring(d.filename,14,20), 
d.line_number as line, 
substring(d.value,1,16) as value,
substring(le.err_reason,1,48) as err_reason
from stl_loaderror_detail d, stl_load_errors le
where d.query = le.query
and d.query = pg_last_copy_id(); 

 query |    substring      | line |  value   |                    err_reason
-------+-------------------+------+----------+-------------------------------------------------
    558| allusers_pipe.txt |  251 | 251      | String contains invalid or unsupported UTF8 code
    558| allusers_pipe.txt |  251 | ZRU29FGR | String contains invalid or unsupported UTF8 code
    558| allusers_pipe.txt |  251 | Kaitlin  | String contains invalid or unsupported UTF8 code
    558| allusers_pipe.txt |  251 | Walter   | String contains invalid or unsupported UTF8 code
```