Tech News
← Back to articles

Interesting Bits of Postgres Grammar

read original related products more articles

I’ve been working on Squawk for a while, it’s a linter for PostgreSQL, and it now uses a handmade parser.

So let’s explore some interesting bits from the Postgres grammar.

Custom Operators

Very few operators are defined in the grammar itself and lots of Postgres features rely on custom operators.

For example, Postgres uses <-> for comparing geometric types, along with a whole host of others: ## , @-@ , # , @> , <@> , &< , &> , |>> , |<< , <^ , >^ , ?- , ?| , ?|| , ~= .

Note: custom operators can be prefix or infix, but not postfix.

A neat consequence of custom operators is that the following lambda expression syntax from Trino parses natively in Postgres:

select array_filter ( array [ 1 , 2 , 2 , 3 ], e -> ( e % 2 ) = 0 );

Albiet with the wrong precedence.

Precedence with Compound select s

... continue reading