Skip to content

Fix indentation of procedural IF EXISTS blocks - #413

Merged
darold merged 2 commits into
darold:masterfrom
arnodirlam:codex/fix-if-exists-indentation
Sep 19, 2026
Merged

darold merged 2 commits into
darold:masterfrom
arnodirlam:codex/fix-if-exists-indentation

Conversation

@arnodirlam

@arnodirlam arnodirlam commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

While formatting a PL/pgSQL block, I noticed that the body of IF EXISTS / IF NOT EXISTS was aligned with the IF, and END IF moved outside the enclosing block. This fixes the indentation so the branches are easier to follow.

Before (pg_format, default options):

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT
            1
        FROM
            widgets
        WHERE
            id = 1
            AND enabled) THEN
    EXECUTE $sql$select 1;$sql$;
END IF;
END
$$;

After (pg_format, default options):

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT
            1
        FROM
            widgets
        WHERE
            id = 1
            AND enabled) THEN
        EXECUTE $sql$select 1;$sql$;
    END IF;
END
$$;

The existing opr_sanity.sql expected output contained the same indentation issue, so I’ve corrected that too.

I ran prove -l t/ and the larger SQL corpus with perl t/regress_test.pl; both pass. I also checked that formatting the new example twice produces the same output.

Thanks for maintaining pgFormatter!

@darold
darold merged commit 1fec1ab into darold:master Sep 19, 2026
0 of 4 checks passed
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.

2 participants