pgschema doesn't quote some keywords (e.g `desc`)
pgschema dump does not quote 13 PostgreSQL reserved keywords used as identifiers
Summary
pgschema dump (v1.7.2) emits unquoted column names for several PostgreSQL
reserved keywords. The dumped SQL fails with a syntax error when re-applied.
The reservedWords map in ir/quote.go is missing 13 keywords that are
classified as RESERVED_KEYWORD or TYPE_FUNC_NAME_KEYWORD in PostgreSQL's
parser.
Minimal reproduction
schema.sql -- valid SQL with quoted reserved-word columns:
CREATE TABLE test_reserved_words (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
"desc" text NOT NULL,
"asc" text,
title text NOT NULL
);
Steps:
# 1. Start ephemeral Postgres
docker run -d --name pgschema-bug \
-e POSTGRES_PASSWORD=test -e POSTGRES_DB=testdb \
-p 54397:5432 postgres:16-alpine
# (wait for ready)
# 2. Load schema
PGPASSWORD=test psql -h 127.0.0.1 -p 54397 -U postgres -d testdb \
-f schema.sql
# 3. Dump with pgschema
PGPASSWORD=test pgschema dump \
--host 127.0.0.1 --port 54397 --user postgres --db testdb \
> dump.sql
# 4. Re-apply to fresh database
PGPASSWORD=test psql -h 127.0.0.1 -p 54397 -U postgres \
-c "CREATE DATABASE testdb2;"
PGPASSWORD=test psql -h 127.0.0.1 -p 54397 -U postgres -d testdb2 \
-v ON_ERROR_STOP=1 -f dump.sql
# Cleanup
docker rm -f pgschema-bug
A self-contained run.sh script is included in this directory. Running it
generates dump.sql (pgschema output) and stderr/error logs under /tmp/.
These are ephemeral artifacts and are not checked in.
Actual output
Captured from pgschema v1.7.2 against PostgreSQL 16.13 (alpine).
pgschema dump output (v1.7.2):
CREATE TABLE IF NOT EXISTS test_reserved_words (
id uuid DEFAULT gen_random_uuid(),
desc text NOT NULL,
asc text,
title text NOT NULL,
CONSTRAINT test_reserved_words_pkey PRIMARY KEY (id)
);
Re-apply fails:
psql:dump.sql:19: ERROR: syntax error at or near "desc"
LINE 3: desc text NOT NULL,
^
Expected output
CREATE TABLE IF NOT EXISTS test_reserved_words (
id uuid DEFAULT gen_random_uuid(),
"desc" text NOT NULL,
"asc" text,
title text NOT NULL,
CONSTRAINT test_reserved_words_pkey PRIMARY KEY (id)
);
Root cause
The reservedWords map in
ir/quote.go is
a hand-maintained map[string]bool of identifiers that pgschema considers
"needs quoting." It is a broader superset of PostgreSQL's keyword categories --
it includes entries from RESERVED_KEYWORD, TYPE_FUNC_NAME_KEYWORD,
COL_NAME_KEYWORD, and even some UNRESERVED_KEYWORD entries. Despite this
breadth, it is missing 13 keywords that PostgreSQL classifies as
RESERVED_KEYWORD or TYPE_FUNC_NAME_KEYWORD (both of which require quoting
when used as column identifiers).
Missing keywords
| # | Keyword | PG Category | PG Docs Label |
|---|---|---|---|
| 1 | analyse |
RESERVED_KEYWORD | reserved |
| 2 | analyze |
RESERVED_KEYWORD | reserved |
| 3 | asc |
RESERVED_KEYWORD | reserved |
| 4 | concurrently |
TYPE_FUNC_NAME_KEYWORD | reserved (can be function or type) |
| 5 | desc |
RESERVED_KEYWORD | reserved |
| 6 | full |
TYPE_FUNC_NAME_KEYWORD | reserved (can be function or type) |
| 7 | leading |
RESERVED_KEYWORD | reserved |
| 8 | localtime |
RESERVED_KEYWORD | reserved |
| 9 | localtimestamp |
RESERVED_KEYWORD | reserved |
| 10 | notnull |
TYPE_FUNC_NAME_KEYWORD | reserved (can be function or type), requires AS |
| 11 | overlaps |
TYPE_FUNC_NAME_KEYWORD | reserved (can be function or type), requires AS |
| 12 | placing |
RESERVED_KEYWORD | reserved |
| 13 | session_user |
RESERVED_KEYWORD | reserved |
References
- PostgreSQL keyword categories (docs): https://www.postgresql.org/docs/17/sql-keywords-appendix.html
- PostgreSQL keyword source (
kwlist.h): https://github.com/postgres/postgres/blob/REL_17_STABLE/src/include/parser/kwlist.h
Suggested fix
- Add the 13 missing keywords to the
reservedWordsmap inir/quote.go:"analyse": true, "analyze": true, "asc": true, "concurrently": true, "desc": true, "full": true, "leading": true, "localtime": true, "localtimestamp": true, "notnull": true, "overlaps": true, "placing": true, "session_user": true, - Alternatively, consider sourcing the keyword list directly from PostgreSQL's
kwlist.hat build time to stay in sync across PG versions. - Ideally, also allow users to specify additional keywords via a config file or CLI flag, to handle edge cases and future-proof against new reserved words.
Environment
- pgschema version: 1.7.2
- PostgreSQL: 16.13 (tested)
- OS: Linux amd64