Skip to content

Support MERGE statement in PostgreSQL - #993

Open
emirmrkaljevic wants to merge 1 commit into
sql-formatter-org:masterfrom
emirmrkaljevic:postgresql-merge
Open

emirmrkaljevic wants to merge 1 commit into
sql-formatter-org:masterfrom
emirmrkaljevic:postgresql-merge

Conversation

@emirmrkaljevic

Copy link
Copy Markdown

Closes #986

PostgreSQL has had MERGE since version 15, but the postgresql dialect didn't know about it, so MERGE INTO, WHEN [NOT] MATCHED and the bare INSERT inside a merge action weren't treated as clauses and kept their original case.

Changes:

  • Added MERGE INTO, WHEN [NOT] MATCHED [BY SOURCE | BY TARGET] [THEN] and UPDATE SET to the PostgreSQL reserved clauses (BY SOURCE / BY TARGET are PostgreSQL 17 syntax).
  • Changed INSERT INTO to INSERT [INTO], since a merge action is just INSERT (cols) VALUES (...) without INTO. This is the same as what transactsql and plsql already do.
  • Added MERGE_ACTION (PostgreSQL 17) to the function list, so merge_action() in RETURNING isn't formatted as merge_action ().

Tests: the PostgreSQL suite now runs the shared supportsMergeInto test, plus a PostgreSQL-specific test covering WHEN MATCHED AND ..., DELETE, DO NOTHING, BY SOURCE / BY TARGET and RETURNING merge_action().

One side effect to note: because bare INSERT is now a clause in PostgreSQL, something like GRANT INSERT, UPDATE ON t TO bob breaks before INSERT the same way it already broke before UPDATE. Happy to narrow it down if you'd rather avoid that.

pnpm test, pnpm ts:check and prettier all pass locally.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

[FORMATTING] PostgreSQL: MERGE (PostgreSQL 15+) is not recognized

1 participant