Add getQueryHintString() for PostgreSQLDialect

Description

PostgreSQL supports hints with the pg_hint_plan extension. This extension is installed in many managed services like Amazon Aurora, or YugabyteDB. Hints are prepended to the statement in a /*+ */ comment. We cannot use setComment() because it adds a space after /* so there are no workarounds except Native SQL queries.


Hints in pg_hint_plan must be in front of the statement (only characters in [0-9 \t\n,_()A-Za-z] can be in front or the hint is not picked up) so the following should work:

@Override public String getQueryHintString(String sql, String hints) { return "/*+ " + hints + " */ " + sql; }

Note that if the user adds a hint and a comment, the hint must be in front, which means that the prependComment() in addSqlHintOrComment() should do it in the inverse order. For example, this:

Query query = s.createQuery( "FROM Employee e WHERE e.department.name = :departmentName" ) .addQueryHint( "IndexScan(e)" ) .setComment( "My_Query" ) .setParameter( "departmentName", "Sales" );

should return: /*+ IndexScan(e) */ /* My_Query */ select

Activity

Show:
Fixed

Details

Assignee

Reporter

Labels

Components

Sprint

Fix versions

Priority

Created November 5, 2023 at 9:20 PM
Updated November 23, 2023 at 1:53 PM
Resolved November 9, 2023 at 3:09 PM

Flag notifications