Improve reporting of errors in extension script files.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 Oct 2024 15:31:45 +0000 (11:31 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 Oct 2024 15:31:45 +0000 (11:31 -0400)
commit774171c4f640853b1cf8747a4762631d2f5d25be
tree39c02f7bde287550d401e2e4fe698e1310053f7c
parent14e5680eee19df8b41ef77330d0b3857f498e4f7
Improve reporting of errors in extension script files.

Previously, CREATE/ALTER EXTENSION gave basically no useful
context about errors reported while executing script files.
I think the idea was that you could run the same commands
manually to see the error, but that's often quite inconvenient.
Let's improve that.

If we get an error during raw parsing, we won't have a current
statement identified by a RawStmt node, but we should always get
a syntax error position.  Show the portion of the script from
the last semicolon-newline before the error position to the first
one after it.  There are cases where this might show only a
fragment of a statement, but that should be uncommon, and it
seems better than showing the whole script file.

Without an error cursor, if we have gotten past raw parsing (which
we probably have), we can report just the current SQL statement as
an item of error context.

In any case also report the script file name as error context,
since it might not be entirely obvious which of a series of
update scripts failed.  We can also show an approximate script
line number in case whatever we printed of the query isn't
sufficiently identifiable.

The error-context code path is already exercised by some
test_extensions test cases, but add tests for the syntax-error
path.

Discussion: https://postgr.es/m/ZvV1ClhnbJLCz7Sm@msg.df7cb.de
src/backend/commands/extension.c
src/test/modules/test_extensions/Makefile
src/test/modules/test_extensions/expected/test_extensions.out
src/test/modules/test_extensions/meson.build
src/test/modules/test_extensions/sql/test_extensions.sql
src/test/modules/test_extensions/test_ext7--2.0--2.1bad.sql [new file with mode: 0644]
src/test/modules/test_extensions/test_ext7--2.0--2.2bad.sql [new file with mode: 0644]
src/tools/pgindent/typedefs.list