Skip to content

Incorrect query_id persistence when pg_hint_plan.enable_hint_table is enabled #236

@jjja5555

Description

@jjja5555

This can be reproduce in psql

  • create extension pg_hint_plan;
  • set pg_hint_plan.enable_hint_table = on;
postgres=# select version();
                                                           version                                                           
-----------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.1 (Homebrew) on aarch64-apple-darwin24.6.0, compiled by Apple clang version 17.0.0 (clang-1700.4.4.1), 64-bit
(1 row)

Session 1

load 'pg_hint_plan';
create extension pg_hint_plan;
set pg_hint_plan.enable_hint_table = on;
postgres=# set pg_hint_plan.enable_hint_table = on;
SET
postgres=# select pg_sleep(10) from t2;
 pg_sleep 
----------
 
(1 row)

postgres=# select pg_sleep(10) from t3;
 pg_sleep 
----------
 
(1 row)

Session 2 monitoring process

postgres=# select query_id, query from pg_stat_activity where pid = 15282;
      query_id       |            query             
---------------------+------------------------------
 9119088274641895962 | select pg_sleep(10) from t2;
(1 row)

postgres=# select query_id, query from pg_stat_activity where pid = 15282;
      query_id       |            query             
---------------------+------------------------------
 9119088274641895962 | select pg_sleep(10) from t3;
(1 row)

The hint_plan.hints table lookup query (shown below) sets backendStatus first, preventing the actual user query from setting its own query_id since backendStatus is already non-zero:

const char *search_query =
		"SELECT hints "
		"  FROM hint_plan.hints "
		" WHERE query_id = $1 "
		"   AND ( application_name = $2 "
		"    OR application_name = '' ) "
		" ORDER BY application_name DESC";

p = SPI_prepare(search_query, 2, argtypes);
plan = SPI_saveplan(p);
SPI_execute_plan(plan, values, nulls, true, 1);

This explains why query_id 9119088274641895962 persists across different queries (pg_sleep(10) from t2 and pg_sleep(10) from t3) when pg_hint_plan.enable_hint_table is enabled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions