[PATCH] Add native PIVOT syntax for SQL Server/Oracle compatibility

From: Myles Lewis <myles93(at)sbcglobal(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] Add native PIVOT syntax for SQL Server/Oracle compatibility
Date: 2025-11-25 05:01:56
Message-ID: 5D71B366-0C2D-4BD9-B197-93851A86B1C1@sbcglobal.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I've developed a patch that adds native PIVOT syntax to PostgreSQL,
enabling SQL Server and Oracle-style pivot queries.

Example:
SELECT region
FROM sales
PIVOT (SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4'));

Key features:
- Parser-level transformation to FILTER aggregates
- No executor changes required
- Supports SUM, COUNT, AVG, MIN, MAX
- View creation with pg_get_viewdef() roundtrip
- Comprehensive regression tests (788 lines)

Patch attached.

Myles

Attachment Content-Type Size
0001-Add-native-PIVOT-syntax-support-for-SQL-Server-Oracl.patch application/octet-stream 106.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-11-25 05:16:55 Re: [Proposal] Adding TRIM_SPACE option to COPY
Previous Message Fujii Masao 2025-11-25 04:54:45 Re: [Proposal] Adding TRIM_SPACE option to COPY