Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H2 crashes if view with subquery exists #3301

Open
t-pa opened this issue Dec 23, 2021 · 1 comment · May be fixed by #3403
Open

H2 crashes if view with subquery exists #3301

t-pa opened this issue Dec 23, 2021 · 1 comment · May be fixed by #3403

Comments

@t-pa
Copy link

t-pa commented Dec 23, 2021

H2 version 2.0.204 crashes when a database containing a view with a CTE is reopened for the second time. Reproducer:

user@test:~$ java -cp h2-2.0.204.jar org.h2.tools.Shell -url "jdbc:h2:~/tmp/h2test" -sql "CREATE OR REPLACE VIEW testview AS WITH subquery AS (SELECT 1 AS testcolumn) SELECT testcolumn FROM subquery;"
(Update count: 0, 129 ms)
user@test:~$ java -cp h2-2.0.204.jar org.h2.tools.Shell -url "jdbc:h2:~/tmp/h2test" -sql "SELECT * FROM testview"
TESTCOLUMN
1
(1 row, 16 ms)
user@test:~$ java -cp h2-2.0.204.jar org.h2.tools.Shell -url "jdbc:h2:~/tmp/h2test" -sql "SELECT * FROM testview"
Exception in thread "main" org.h2.jdbc.JdbcSQLSyntaxErrorException: View "SUBQUERY" already exists; SQL statement:
CREATE FORCE VIEW TABLE_EXPRESSION "PUBLIC"."SUBQUERY"("TESTCOLUMN") AS
SELECT
    1 AS "TESTCOLUMN" [90038-204]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:651)
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
        at org.h2.message.DbException.get(DbException.java:227)
        at org.h2.message.DbException.get(DbException.java:203)
        at org.h2.command.ddl.CreateView.update(CreateView.java:90)
        at org.h2.command.ddl.SchemaOwnerCommand.update(SchemaOwnerCommand.java:33)
        at org.h2.engine.MetaRecord.prepareAndExecute(MetaRecord.java:77)
        at org.h2.engine.Database.executeMeta(Database.java:660)
        at org.h2.engine.Database.executeMeta(Database.java:634)
        at org.h2.engine.Database.<init>(Database.java:357)
        at org.h2.engine.Engine.openSession(Engine.java:92)
        at org.h2.engine.Engine.openSession(Engine.java:222)
        at org.h2.engine.Engine.createSession(Engine.java:201)
        at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:117)
        at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:287)
        at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:270)
        at org.h2.tools.Shell.runTool(Shell.java:148)
        at org.h2.tools.Shell.main(Shell.java:82)
@t-pa
Copy link
Author

t-pa commented Jan 25, 2022

Bisecting this issue leads to commit 74b1b25 ("populate meta with NEW objects even during startup"). After this commit, Database.addMeta() adds objects to the meta table also during the startup phase. However, Database.removeMeta()still does nothing during startup.

This means that the temporary cached table created by TableView.createShadowTableForRecursiveTableExpression() is never destroyed and clashes with the creation of the view when the database is reopened.

t-pa added a commit to t-pa/h2database that referenced this issue Jan 25, 2022
…e commit h2database@74b1b25 changes the behavior of `addMeta()` in the same way.

Fixes h2database#3301.
@t-pa t-pa linked a pull request Jan 25, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant