All articles
Article 3 min read

PostgreSQL Said No to Query Hints for 20 Years. Postgres 19 Ships Them

PostgreSQL has finally embraced query hints, a feature long resisted by developers.

Introduction

PostgreSQL, known for its steadfast adherence to best practices and rigorous standards, has been a notable holdout on the concept of query hints, a powerful but often controversial aspect of database querying. Query hints are directives inserted into SQL queries to guide the database engine in how it should process that particular piece of code; developers might use them when they know their queries work better with certain parameters than the default settings would allow.

However, PostgreSQL has long resisted this practice, believing it can lead to overly complex and brittle queries. Now, after years of development and consideration, Postgres 19 is set to introduce a new contrib module that will embrace query hints in a more controlled manner. This article explores what "pg_plan_advice," the feature being introduced by PostgreSQL's community-driven project, DevOps, can offer.

The Rise of Query Hints

For years, developers have found themselves needing to tweak how databases handle queries through SQL hint syntax, which provides explicit instructions about how a query should be processed. For instance, the "FORCE INDEX" or "SKIP LOCKED" hints force PostgreSQL to use specific indexes in non-standard ways or skip certain locked rows, respectively.

These tools can sometimes seem like a necessary evil when working with complex databases and schemas, especially in multi-user environments where performance optimization often depends on developers’ input. However, they also introduce significant complexity into queries that could otherwise be more straightforward without hints. They require maintaining up-to-date knowledge of the database internals, which can lead to errors or inconsistencies if not handled carefully.

The Postgres 19 Release

PostgreSQL's long-standing stance against query hints is understandable given their potential drawbacks. However, with the release of PostgreSQL 19, the community has now embraced this concept in a more controlled fashion through "pg_plan_advice," an enhancement to the contrib module. This feature introduces explicit directives that allow developers to influence how the planner generates execution plans for queries.

How pg_plan_advice Works

"pg_plan_advice" functions by allowing users to provide hints directly within their SQL statements, similar to traditional query hints but with a more refined and managed approach. Here’s an example of how you might use this feature:

sql
-- Example of using 'pg_plan_advice' in Postgres 19
SELECT * 
FROM pg_plan_advice 
EXPLAIN (FORMAT JSON) 
(SELECT * FROM big_table WHERE some_column > 50);

This snippet illustrates calling the "pg_plan_advice" module to modify how PostgreSQL handles the EXPLAIN process for a specific query. By doing so, developers can guide the planner towards creating more efficient or tailored execution plans, depending on their needs.

Benefits and Best Practices

While this feature does introduce some complexity around writing queries with hints, it also offers several benefits:

Flexibility: Developers gain control over how certain parts of complex queries are processed.

Performance Tweaks: Explicit hints can be crucial for fine-tuning performance-critical sections of the database.

Maintainability: By explicitly stating what you want from the planner rather than relying on implicit defaults, queries become clearer and easier to maintain.

However, it's important that developers use this feature judiciously. "pg_plan_advice" should be employed sparingly—where traditional query optimization methods fall short or where performance improvements are critical for specific parts of a query. The key is finding the right balance between flexibility and maintaining strong database governance practices.

Conclusion

Postgres 19's introduction of "pg_plan_advice," a contrib module that manages planner hints, represents an important step in PostgreSQL’s evolution as a robust, standards-driven database system. By embracing controlled query hints rather than outright rejection, developers have the ability to fine-tune their queries effectively while still upholding best practices and maintaining clear separation of concerns between application logic and database management.

As with any powerful tool, "pg_plan_advice" must be used thoughtfully and responsibly to ensure it does not compromise the reliability or security of a database system. Nonetheless, its introduction underscores PostgreSQL’s commitment to staying modern and adapting to new needs while preserving core strengths.