As promised some more background. It took longer to come back here due to capacity being needed to handle an out-of-control situation with one of the connected platforms.
The Invantive SQL-statements (including the PSQL-blocks started as a SQL-statement by surrounding them with begin
and end
) assume that all database objects exist format prior to execution and do not change definition during execution.
This is essentially the border between DML (Data Manipulation Language) and DDL (Data Definition Language).
However, during compilation of DML-statements (including PSQL), the Invantive SQL-engine does not always check that the database objects and code meet these requirements. Often code can create or replace database objects during compilation and execution without any impact given the nature of the compilation process of Invantive SQL.
Despite providing advantages, this current behavior is considered a bug and not a feature.
The next version of the SQL-engine will split the compilation phase into more sub-phases. Existing code dynamically creating database objects such as tables may fail or continue working.
We will try for the next version to keep the advantages, but removing the fuzzy nature of changing database objects during execution of DML.
To write maintainable code, it is recommended to create the database objects in a separate SQL statement-batch(es) using either PSQL, SQL DDL-statements or dynamic SQL. The original DML remains as the last SQL-batch executed.
A SQL-batch is a range of SQL-statements executed in one go (see Invantive SQL Grammar v22.1). In case there are questions, please address them in a separate topic and refer to this topic.