Error: failed to parse desired state schema file: failed to split SQL statements: syntax error at or near "NOT"
I do dump && plan and see the error:
Error: failed to parse desired state schema file: failed to split SQL statements: syntax error at or near "NOT"
Running script:
pgschema dump \
--host xxx \
--port xxx \
--db xxx \
--user postgres \
--password xxx \
--schema public \
> schema.sql
pgschema plan \
--host xxx \
--port xxx \
--db xxx \
--user postgres \
--password xxx \
--schema public \
--file schema.sql \
--output-sql stdout
Host/user/db are the same for both dump and plan just for testing purposes, so the plan should've been empty, but there is an sql error.
So, in fact, it generates a wrong sql file, but the file is large (~1500 lines) and I can't just attach it here, cause it's my database in fact. I asked Claude to find NOT-related issues and that's the answer, I believe it will help:
Description
The schema.sql file contains syntax errors where the NOT VALID modifier is incorrectly placed inside the CHECK constraint parentheses instead of outside them. This causes a parsing error: "syntax error at or near 'NOT'".
Affected Lines
-
Line 409 (table
users):email text NOT NULL UNIQUE CHECK (((lower(email) = email)) NOT VALID), -
Lines 624-626 (table
modtemplate):filters jsonb NOT NULL CHECK (((jsonb_typeof(filters) = 'array'::text)) NOT VALID), sorters jsonb NOT NULL CHECK (((jsonb_typeof(sorters) = 'array'::text)) NOT VALID), modifiers jsonb NOT NULL CHECK (((jsonb_typeof(modifiers) = 'array'::text)) NOT VALID) -
Line 726 (table
search_log):found integer NOT NULL CHECK (((found >= 0)) NOT VALID) -
Line 919 (table
automata):website_categories jsonb NOT NULL CHECK (((jsonb_typeof(website_categories) = 'array'::text)) NOT VALID), -
Line 1032 (table
parser_queue):website_categories jsonb DEFAULT '[]' NOT NULL CHECK (((jsonb_typeof(website_categories) = 'array'::text)) NOT VALID),
Root Cause
The NOT VALID modifier is placed inside the CHECK constraint parentheses, which is invalid SQL syntax.
Correct syntax:
CHECK (condition) NOT VALID
Incorrect syntax (current):
CHECK ((condition) NOT VALID)
Solution
Move the NOT VALID modifier outside the CHECK constraint parentheses in all affected lines.
Example fix for line 409:
-- Before (incorrect):
email text NOT NULL UNIQUE CHECK (((lower(email) = email)) NOT VALID),
-- After (correct):
email text NOT NULL UNIQUE CHECK ((lower(email) = email)) NOT VALID,
I've checked "what if I apply these changes and run plan" - it doesn't not work, now the error is:
Error: failed to parse desired state schema file: failed to split SQL statements: syntax error at or near "VALID"
I think pgschema works wrongly with CONSTRAINT, because "original" create script (from pgAdmin) for this is:
CREATE TABLE IF NOT EXISTS public.users
(
...
email text COLLATE pg_catalog."default" NOT NULL,
...
CONSTRAINT users_email_is_lower CHECK (lower(email) = email) NOT VALID
)
Please provide a more clear case with before and after script. Otherwise, it's hard to narrow down.
Yeah, I'm trying to narrow it to exact lines
Found it.
# schema.sql generated by dump
# I removed everything else, but didn't touch the syntax or brackets
CREATE TABLE IF NOT EXISTS users (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
email text NOT NULL UNIQUE CHECK (((lower(email) = email)) NOT VALID)
);
Than run
pgschema plan \
--host xxx \
--port xxx \
--db xxx \
--user postgres \
--password xxx \
--schema public \
--file schema.sql \
--output-sql stdout
And you get
Error: failed to parse desired state schema file: failed to split SQL statements: syntax error at or near "NOT"
Real postgres says the syntax is invalid:
ERROR: syntax error at or near "NOT"
LINE 3: ...t NOT NULL UNIQUE CHECK (((lower(email) = email)) NOT VALID)
Version:
pgschema --help
Declarative schema migration for Postgres
Version: 1.2.1@a033416 darwin/arm64 2025-10-10 10:28:02
Valid syntax from create in pgAdmin:
CREATE TABLE IF NOT EXISTS public.users
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
email text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT users_email_is_lower CHECK (lower(email) = email) NOT VALID
)
TABLESPACE pg_default;
A minimal database example is this:
DROP TABLE IF EXISTS public.users;
CREATE TABLE IF NOT EXISTS public.users
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
email text COLLATE pg_catalog."default" NOT NULL
)
TABLESPACE pg_default;
INSERT INTO public.users (email) VALUES ('[email protected]');
ALTER TABLE public.users
ADD CONSTRAINT users_email_is_lower
CHECK (lower(email) = email) NOT VALID;
Than try to do dump && plan, it does not work.
NOT VALID is only useful in ALTER TABLE. This is not needed in CREATE TABLE.
pgschema dump doesn't create constraint with NOT VALID in the dump
tianzhou:~/Desktop/pgschema$ pgschema dump --host localhost --db testdb7 --user postgres --password testpwd1
--
-- pgschema database dump
--
-- Dumped from database version PostgreSQL 17.5
-- Dumped by pgschema version 1.3.0
--
-- Name: users; Type: TABLE; Schema: -; Owner: -
--
CREATE TABLE IF NOT EXISTS users (
id uuid DEFAULT gen_random_uuid() NOT NULL,
email text NOT NULL CHECK (lower(email) = email)
);
You can also check these 2 test cases as a reference:
- https://github.com/pgschema/pgschema/tree/main/testdata/diff/online/add_constraint
- https://github.com/pgschema/pgschema/tree/main/testdata/diff/online/alter_constraint
@tianzhou
pgschema dump doesn't create constraint with NOT VALID in the dump
It does. Pleas, use my sql database example. pgschema adds NOT VALID to the dump only when the table has some data, as I did in the example.
Just run on an example database:
CREATE TABLE IF NOT EXISTS public.users
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
email text COLLATE pg_catalog."default" NOT NULL
)
TABLESPACE pg_default;
INSERT INTO public.users (email) VALUES ('[email protected]');
ALTER TABLE public.users
ADD CONSTRAINT users_email_is_lower
CHECK (lower(email) = email) NOT VALID;
Than run dump and you will have schema.sql like this:
CREATE TABLE IF NOT EXISTS users (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
email text NOT NULL UNIQUE CHECK (((lower(email) = email)) NOT VALID)
);
Thanks for providing the extra info. It should be fixed in the head.
This error I'm facing in v1.4.1 is related:
CREATE TABLE IF NOT EXISTS saml_relay_states (
id uuid,
sso_provider_id uuid NOT NULL,
request_id text NOT NULL,
for_email text,
redirect_to text,
created_at timestamptz,
updated_at timestamptz,
flow_state_id uuid,
CONSTRAINT saml_relay_states_pkey PRIMARY KEY (id),
CONSTRAINT saml_relay_states_flow_state_id_fkey FOREIGN KEY (flow_state_id) REFERENCES flow_state (id) ON DELETE CASCADE,
CONSTRAINT saml_relay_states_sso_provider_id_fkey FOREIGN KEY (sso_provider_id) REFERENCES sso_providers (id) ON DELETE CASCADE,
CONSTRAINT request_id not empty CHECK (char_length(request_id) > 0)
);
That is dumped by pgschema. The problem lies with not empty in the CONSTRAINT request_id clause.
When I do a normal pg_dump, I get this:
CONSTRAINT "request_id not empty" CHECK ((char_length(request_id) > 0))
(P.S. I'm not in control of these constraint names)
@aleclarson thanks for reporting. Fixed in the head