The following bug has been logged on the website:
Bug reference: 19427
Logged by: chunling qin
Email address: 303677365@qq.com
PostgreSQL version: 15.0
Operating system: centos
Description:
## Description
PostgreSQL crashes with an assertion failure when executing UPDATE
statements with RETURNING clause that returns TOAST-compressed data
(specifically `pg_node_tree` type from system catalogs). The assertion
`HaveRegisteredOrActiveSnapshot()` fails in `init_toast_snapshot()`
function.
## Crash Information
### Signal
```
SIGABRT (Assertion failure)
```
### Stack Trace
```
#0 __pthread_kill_implementation () from /lib64/libc.so.6
#1 raise () from /lib64/libc.so.6
#2 abort () from /lib64/libc.so.6
#3 ExceptionalCondition (conditionName="HaveRegisteredOrActiveSnapshot()",
errorType="FailedAssertion",
fileName="toast_internals.c", lineNumber=670)
#4 init_toast_snapshot (toast_snapshot=0x7ffd01b64e50) at
toast_internals.c:670
#5 heap_fetch_toast_slice (toastrel=..., valueid=12032, attrsize=2448,
sliceoffset=0, slicelength=2448, result=...)
at heaptoast.c:688
#6 table_relation_fetch_toast_slice (...) at tableam.h:1892
#7 toast_fetch_datum (attr=...) at detoast.c:375
#8 detoast_attr (attr=...) at detoast.c:123
#9 pg_detoast_datum_packed (datum=...) at fmgr.c:1757
#10 text_to_cstring (t=...) at varlena.c:225
#11 textout (fcinfo=...) at varlena.c:574
#12 pg_node_tree_out (fcinfo=...) at pseudotypes.c:354
#13 FunctionCall1Coll (...) at fmgr.c:1138
#14 OutputFunctionCall (...) at fmgr.c:1575
#15 printtup (...) at printtup.c:357
```
## Reproduction
### Minimal Test Case
```sql
-- Case 1: UPDATE system catalog with RETURNING TOAST column
UPDATE pg_catalog.pg_rewrite SET rulename = rulename
WHERE oid = (SELECT oid FROM pg_catalog.pg_rewrite LIMIT 1)
RETURNING ev_action;
-- Case 2: UPDATE with subquery returning TOAST data
CREATE TABLE test_table (id int, col_varchar varchar);
INSERT INTO test_table VALUES (1, 'test');
UPDATE test_table SET id = id
RETURN
id,
(SELECT ev_action FROM pg_catalog.pg_rewrite LIMIT 1) AS ev_action;
```