Skip to content

[FORMATTING] Inconsistent formatting of PostgreSQL CREATE POLICY commands #928

Open
cnbei/sql-formatter
#2

Description

@peperoli

Input data

Which SQL and options did you provide as input?

CREATE POLICY "Allow users to select their own records" ON public.user_achievements
  FOR SELECT
  TO authenticated
  USING (user_id = auth.uid());

CREATE POLICY "Allow users to insert their own records" ON public.user_achievements
  FOR INSERT
  TO authenticated
  WITH CHECK (user_id = auth.uid());

Actual Output

CREATE POLICY "Allow users to select their own records" ON public.user_achievements FOR
SELECT
  TO authenticated USING (user_id = auth.uid ());

CREATE POLICY "Allow users to insert their own records" ON public.user_achievements FOR INSERT TO authenticated
WITH
  CHECK (user_id = auth.uid ());

Those two similar statements are formatted differently for no apparent reason.

Usage

  • How are you calling / using the library? tested with the demo and prettier-plugin-sql
  • What SQL language(s) does this apply to? PostgreSQL
  • Which SQL Formatter version are you using? (The exact version number.) 15.7.0

Activity

  1. nene commented on Jan 20, 2026

    @nene
    Collaborator

    Thanks for reporting.

    I guess this can be improved by explicitly detecting the FOR {SELECT | INSERT | ...} syntax. And then also the WITH CHECK syntax. But properly dealing with TO role syntax is pretty hard for the formatter. These are really just patches on top of the fundamental shortcomings of the architecture of the formatter.

    As you're already using Prettier, I would suggest you try out prettier-plugin-sql-cst which handles PostgreSQL quite a bit better. Though not all of PostgreSQL is fully supported yet.

  2. peperoli commented on Jan 20, 2026

    @peperoli
    Author

    Yeah, I think detecting FOR {SELECT | INSERT | ...}, USING and WITH CHECK would defenitely be an improvement.

    Thanks for the recommendation. I was a bit hesitant to use prettier-plugin-sql-cst as it says "postgresql: experimental! (expect crashes)" but I'll try it out.

  3. nene commented on Jan 20, 2026

    @nene
    Collaborator

    Yeah, it definitely can and will crash. The question is how often. For most of the usual PostgreSQL code it should work fine.

    The main difference is that when it encounters unknown syntax, it will crash, while SQL Formatter will just continue on.

    So you have a choice of: SQL Formatter with worse formatting, but fewer crashes, or the prettier plugin with better formatting, but more potential crashes. However, there's a workaround to use conditional comments to disable the parsing & formatting of problematic parts.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions