Skip to content

Fix CTE recognition after INSERT columns and preserve enclosing indentation - #414

Merged
darold merged 3 commits into
darold:masterfrom
arnodirlam:codex/fix-cte-indentation
Sep 19, 2026
Merged

darold merged 3 commits into
darold:masterfrom
arnodirlam:codex/fix-cte-indentation

Conversation

@arnodirlam

@arnodirlam arnodirlam commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

I ran into a couple of indentation problems with CTEs after an INSERT column list: sibling CTEs become progressively indented, and queries inside a PL/pgSQL block can jump back to column zero. This keeps the CTEs and their final query at the enclosing block’s indentation level.

Before (pg_format, default options):

DO $$
BEGIN
    INSERT INTO results (id)
    WITH first_cte AS (
        SELECT
            1 AS id),
        second_cte AS (
            SELECT
                id
            FROM
                first_cte
)
        SELECT
            id
        FROM
            second_cte;
END
$$;

After (pg_format, default options):

DO $$
BEGIN
    INSERT INTO results (id)
    WITH first_cte AS (
        SELECT
            1 AS id
    ),
    second_cte AS (
        SELECT
            id
        FROM
            first_cte
    )
    SELECT
        id
    FROM
        second_cte;
END
$$;

Two existing expected-output files also contained examples of queries losing their enclosing indentation. Their changes reflect the same fix.

I ran prove -l t/ and the larger SQL corpus with perl t/regress_test.pl; both pass. The focused cases also produce the same output when formatted twice.

Thanks for taking a look!

@darold
darold merged commit 50c8c44 into darold:master Sep 19, 2026
darold added a commit that referenced this pull request Sep 19, 2026
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