Skip to content

Typecheck skips the SELECT inside CREATE TABLE AS / CREATE MATERIALIZED VIEW / CREATE VIEW #786

Description

@edjubert

Is your feature request related to a problem?

The typechecker silently skips the query inside CREATE TABLE ... AS SELECT,
CREATE MATERIALIZED VIEW ... AS SELECT and CREATE VIEW ... AS SELECT. Neither an
unknown column nor an unknown table is reported.

This matters for ETL and analytics codebases, where CREATE TABLE AS is the dominant
shape. In ours it is roughly 640 files out of 1880: the tool reports nothing on a third
of the codebase, and the absence of a diagnostic reads as a pass.

Describe the solution you'd like

Typecheck the inner query for these forms.

For CREATE TABLE AS and CREATE MATERIALIZED VIEW AS this looks cheap, because
Postgres accepts EXPLAIN on both and surfaces the error itself:

postgres=# EXPLAIN CREATE TABLE t2 AS SELECT t.nope FROM t;
ERROR:  column t.nope does not exist

postgres=# EXPLAIN CREATE MATERIALIZED VIEW mv1 AS SELECT t.nope FROM t;
ERROR:  column t.nope does not exist

CREATE VIEW is the odd one out, since EXPLAIN CREATE VIEW is a syntax error in
Postgres. That one would need the inner SELECT to be extracted and checked on its own.

Current behavior

Against a database with CREATE TABLE t (a INT);:

Statement Diagnostic
SELECT t.nope FROM t; 42703 reported
INSERT INTO t (a) SELECT t.nope FROM t; 42703 reported
CREATE TABLE t2 AS SELECT t.nope FROM t; none
CREATE TABLE t3 AS SELECT t.a FROM nonexistent_table AS t; none
CREATE VIEW v1 AS SELECT t.nope FROM t; none
CREATE MATERIALIZED VIEW mv1 AS SELECT t.nope FROM t; none

System information

  • postgres-language-server 0.25.7, Postgres 15
  • macOS (aarch64), release binary postgres-language-server_aarch64-apple-darwin

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions