pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Planning on view with UNION subquery produces invalid SQL view definition

Open whoward opened this issue 1 year ago • 1 comments

Given the following database definition:

create table some_table(id integer primary key);

create table some_other_table(id integer primary key);

create view some_view as (
  select id from (
    select id from some_table
    union
    select id from some_other_table
  )
) ;

pgschema dump produces the following correct output:

--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 17.0
-- Dumped by pgschema version 1.4.0


--
-- Name: some_other_table; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS some_other_table (
    id integer,
    CONSTRAINT some_other_table_pkey PRIMARY KEY (id)
);

--
-- Name: some_table; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS some_table (
    id integer,
    CONSTRAINT some_table_pkey PRIMARY KEY (id)
);

--
-- Name: some_view; Type: VIEW; Schema: -; Owner: -
--

CREATE OR REPLACE VIEW some_view AS
 SELECT id
   FROM ( SELECT some_table.id
           FROM some_table
        UNION
         SELECT some_other_table.id
           FROM some_other_table) unnamed_subquery;

However, when we run pgschema plan --file dump.sql on the database we produce the following invalid plan:

Plan: 1 to modify.

Summary by type:
  views: 1 to modify

Views:
  ~ some_view

DDL to be executed:
--------------------------------------------------

CREATE OR REPLACE VIEW some_view AS
 SELECT
    id
   FROM (SELECT) unnamed_subquery;

whoward avatar Oct 20 '25 15:10 whoward

just tested, this works great! thanks for taking care of that

whoward avatar Oct 23 '25 14:10 whoward