Unlocking PostgreSQL: Insights on Grammar & Operators

Unlocking PostgreSQL: Insights on Grammar & Operators

Category: Technology
Duration: 3 minutes
Added: June 23, 2025
Source: steve.dignam.xyz

Description

In this episode of Log Blog Kebab, we explore the fascinating intricacies of PostgreSQL grammar, focusing on the unique features that set it apart from other database systems. Our expert shares insights from building a custom linter named Squawk, highlighting the role of custom operators, complex SELECT statements, and the nuances of type specifications in functions. We discuss how string literals can be merged and the flexibility of quoted identifiers in SQL queries. Whether you're a seasoned developer or just curious about database management, this episode delves into the key aspects of PostgreSQL that can enhance your programming skills and understanding of SQL syntax. Tune in to discover how these features can empower your database interactions!

Show Notes

## Key Takeaways

1. Custom operators in PostgreSQL enhance flexibility and functionality.
2. Complex SELECT statements are parsed in a specific way that affects the order of operations.
3. Using percent types in functions helps maintain type consistency across database operations.
4. String literals can be merged, but comments disrupt this process, leading to syntax errors.
5. Quoted identifiers allow for flexibility in naming tables and columns, including special characters.

## Topics Discussed

- Custom Operators
- Complex SELECT Statements
- Percent Types in Functions
- Merging String Literals
- Quoted Identifiers

Topics

PostgreSQL database management custom operators SQL syntax Postgres grammar programming tips SQL functions linter for PostgreSQL complex SELECT statements string literals quoted identifiers lambda expressions in SQL operators in Postgres

Transcript

H

Host

Welcome back to Log Blog Kebab! Today, we’re diving into some fascinating aspects of Postgres grammar. If you’ve ever wondered what makes Postgres unique, you’re in the right place.

E

Expert

Thanks for having me! I’m excited to share what I've learned from building a linter for PostgreSQL called Squawk, especially focusing on its handmade parser.

H

Host

That sounds intriguing! So, let's start with custom operators. I hear they play a significant role in Postgres. Can you explain that?

E

Expert

Absolutely! In Postgres, very few operators are defined in the grammar itself. Instead, a lot of features rely on custom operators. For instance, operators like <-> are used for comparing geometric types. There are many others too—like ##, @>, and even <@>.

H

Host

Wow, that’s a lot of operators! Do they all behave the same way?

E

Expert

Not quite. They can be either prefix or infix, but not postfix. A neat feature of custom operators is that they make it possible to use lambda expressions from Trino directly in Postgres, though the precedence can be a little off.

H

Host

Interesting! Speaking of precedence, I heard there’s a specific way complex selects work in Postgres? Can you elaborate?

E

Expert

Sure! When you write something like 'select foo union select bar order by baz,' it actually parses as '(select foo union select bar) order by baz.' This means the order by applies to the whole compound select, not just one part.

H

Host

That’s a clever way to manage queries. Now, I’m curious about percent types. How do they work in practice?

E

Expert

With functions, you can specify a type based on a table’s column type. For example, if you have a column 'c' on a table 't', you could define a function like this: 'create function f(a t.c%type) as 'select 1' language plpgsql;'. It’s a convenient way to ensure type consistency.

H

Host

That’s very useful! Now, I’ve heard that string literals can be combined in a unique way. Can you explain that?

E

Expert

Yes! If you have two string literals separated by a new line, they get merged into one. So, 'select 'foo' 'bar';' would return 'foobar'. But if there's a comment in between, like 'select 'foo' /* hmm */ 'bar';', it triggers a syntax error. This is actually part of the SQL standard.

H

Host

That’s quite the quirk! What about quoted identifiers? How do they function in Postgres?

E

Expert

In Postgres, you can quote your identifiers. For instance, 'select * from t;' is the same as 'select * from "t";'. If you need to include a double quote in your identifier, you can escape it like this: 'select * from "foo "" bar";'. This will select from the foo " bar table.

H

Host

I love how flexible that is! Are there any other interesting features related to operators?

E

Expert

Definitely! Instead of using an operator directly, you can use the operator function like this: 'select 1 operator(+)' 2. This lets you specify the operator’s schema too, making it very versatile.

H

Host

This has been an enlightening discussion! Custom operators really set Postgres apart from other databases, don't they?

E

Expert

Absolutely! While they can be tricky to implement, they add a lot of power to the language.

H

Host

Thanks for sharing your insights today! That’s it for this episode of Log Blog Kebab. Join us next time for more tech adventures!

Create Your Own Podcast Library

Sign up to save articles and build your personalized podcast feed. Your first 3 episodes are free.