Re: Cleaning up PREPARE query strings?

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: Julien Rouhaud <rjuju123(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Cleaning up PREPARE query strings?
Date: 2026-01-19 06:43:53
Message-ID: CAA5RZ0tO7KXtOSGLGzhGijneTs5+v32PqOxhQ0bSj9Q8stLPyw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> I think that you have a misunderstanding of how memory context works in
> postgres.

No, I understand how memory contexts work, but I skipped over the fact that the
cleaned up query text is copied into CachedPlanSource memory context during

CreateCachedPlan:
```
plansource->query_string = pstrdup(query_string);

```
Which is long-lived and will remain until DEALLOCATE.

while pstate->p_sourcetext and new_query are in the current execution's
memory context.

The patch is indeed better from a memory perspective. My apologies.

However, the error reporting does break with the patch. Notice with the patch
the cursor for the error reporting shifts incorrectly. This is due to the fact
rawstmt->stmt_location/length are no longer representative of the original
qurey text.

## unpatched
```
postgres=# SELECT 1 \; PREPARE stmt AS SELECT nonexistent_column
FROM users\; SELECT 2;
?column?
----------
1
(1 row)

ERROR: relation "users" does not exist
LINE 1: ... ; PREPARE stmt AS SELECT nonexistent_column FROM users; SEL...

^
```

## patched
```
postgres=# SELECT 1 \; PREPARE stmt AS SELECT nonexistent_column
FROM users\; SELECT 2;
?column?
----------
1
(1 row)

ERROR: relation "users" does not exist
LINE 1: ...LECT 1 ; PREPARE stmt AS SELECT nonexistent_column FROM u...

^
```

--
Sami Imseih
Amazon Web Services (AWS)

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tatsuo Ishii 2026-01-19 06:45:10 Re: Row pattern recognition
Previous Message wenhui qiu 2026-01-19 06:35:00 Re: Can we change pg_rewind used without wal_log_hints and data_checksums