Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 05fabca

Browse files
tglsfdcCommitfest Bot
authored and
Commitfest Bot
committed
Delay parse analysis and rewrite until we're ready to execute the query.
This change fixes a longstanding bugaboo with SQL functions: you could not write DDL that would affect later statements in the same function. That's mostly still true with new-style SQL functions, since the results of parse analysis are baked into the stored query trees (and protected by dependency records). But for old-style SQL functions, it will now work much as it does with plpgsql functions. The key changes required are to (1) stash the parsetrees read from pg_proc somewhere safe until we're ready to process them, and (2) adjust the error context reporting. sql_compile_error_callback is now only useful for giving context for errors detected by raw parsing. Errors detected in either parse analysis or planning are handled by sql_exec_error_callback, as they were before this patch series. Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/8216639.NyiUUSuA9g@aivenlaptop
1 parent 019a533 commit 05fabca

File tree

5 files changed

+241
-158
lines changed

5 files changed

+241
-158
lines changed

doc/src/sgml/xfunc.sgml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,6 @@ CALL clean_emp();
234234
whereas returning <type>void</type> is a PostgreSQL extension.
235235
</para>
236236

237-
<note>
238-
<para>
239-
The entire body of an SQL function is parsed before any of it is
240-
executed. While an SQL function can contain commands that alter
241-
the system catalogs (e.g., <command>CREATE TABLE</command>), the effects
242-
of such commands will not be visible during parse analysis of
243-
later commands in the function. Thus, for example,
244-
<literal>CREATE TABLE foo (...); INSERT INTO foo VALUES(...);</literal>
245-
will not work as desired if packaged up into a single SQL function,
246-
since <structname>foo</structname> won't exist yet when the <command>INSERT</command>
247-
command is parsed. It's recommended to use <application>PL/pgSQL</application>
248-
instead of an SQL function in this type of situation.
249-
</para>
250-
</note>
251-
252237
<para>
253238
The syntax of the <command>CREATE FUNCTION</command> command requires
254239
the function body to be written as a string constant. It is usually

0 commit comments

Comments
 (0)