pg_hint_plan이 있는 pg_stat_statements

PgDay Austria에서 pg_hint_plan을 발표할 때 이 질문을 받았습니다.

짧은 대답은 다음과 같습니다. 아니요, 변경되지 않습니다. queryid는 주석을 무시하고 구문 분석 후 트리에서 계산되며 여기에는 힌트가 포함됩니다.

그러나 이것은 또 다른 질문에 대한 답이기도 합니다. 쿼리 텍스트에 삽입된 힌트가 표시됩니까? 다음은 pg_stat_statements 테이블에서 쿼리가 수정된 경우에도 queryid 및 쿼리 텍스트가 동일하고 힌트가 없는 것과 동일함을 보여주는 작은 데모입니다.

내 데모에서 테이블을 만듭니다.

create table a (n int primary key, x int);
create table b (n int primary key, x int);
create table c (n int primary key, x int);
create index b_index on b(x) include (n);
select pg_stat_statements_reset();
select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
select queryid, calls, rows, query from pg_stat_statements;

hint_plan.hints에서 queryid를 확인합니다.

yugabyte=# select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
select queryid, calls, rows, query from pg_stat_statements;LOG:  statement: select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
 n | x
---+---
(0 rows)

yugabyte=# select queryid, calls, rows, query from pg_stat_statements;

       queryid       | calls | rows |                           query
---------------------+-------+------+-----------------------------------------------------------
 6990186059047281266 |     1 |    1 | select pg_stat_statements_reset()
 6386600050796028530 |     1 |    0 | select * from c "🍒" natural join b "🍌" natural join a "🍏"
(2 rows)


이제 queryid 테이블의 힌트가 있습니다.

grant yb_extension to yugabyte;
create extension pg_hint_plan;
set pg_hint_plan.enable_hint_table=on;
insert into hint_plan.hints(norm_query_string, application_name, hints) values (
$$select * from c "🍒" natural join b "🍌" natural join a "🍏" ;$$,
$$$$,
$hints$Leading( ( ("🍏" "🍌") "🍒" ) ) HashJoin( "🍏" "🍌" ) HashJoin("🍏" "🍌" "🍒") SeqScan("🍏") IndexOnlyScan("🍌") SeqScan("🍒")$hints$
);


동일한 진술 and and pg_stat_statements이 표시됩니다.

yugabyte=# select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
select queryid, calls, rows, query from pg_stat_statements; n | x
---+---
(0 rows)

yugabyte=# select queryid, calls, rows, query from pg_stat_statements;
       queryid        | calls | rows |                                                                              query
----------------------+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 -4857947532354760446 |     3 |    1 | SELECT hints   FROM hint_plan.hints  WHERE norm_query_string = $1    AND ( application_name = $2     OR application_name = '' )  ORDER BY application_name DESC
  6990186059047281266 |     1 |    1 | select pg_stat_statements_reset()
  6386600050796028530 |     1 |    0 | select * from c "🍒" natural join b "🍌" natural join a "🍏"
(3 rows)


그렇다면 힌트가 사용되었는지 어떻게 알 수 있을까요? 상세 로그를 활성화할 수 있습니다.

yugabyte=# set pg_hint_plan.debug_print=verbose;
SET
yugabyte=# set client_min_messages = log;
SET
yugabyte=# select pg_stat_statements_reset();
LOG:  statement: select pg_stat_statements_reset();
LOG:  pg_hint_plan[qno=0x1a]: no match found in table:  application name = "psql", normalized_query="select pg_stat_statements_reset();"
LOG:  hints in comment="(none)", query="select pg_stat_statements_reset();", debug_query_string="select pg_stat_statements_reset();"
LOG:  pg_hint_plan[qno=0x1c]: no match found in table:  application name = "psql", normalized_query="select pg_stat_statements_reset();"
LOG:  hints in comment="(none)", query="select pg_stat_statements_reset();", debug_query_string="select pg_stat_statements_reset();"
LOG:  pg_hint_plan[qno=0x1a]: planner: no valid hint
 pg_stat_statements_reset
--------------------------

(1 row)

yugabyte=# select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
select queryid, calls, rows, query from pg_stat_statements;LOG:  statement: select * from c "🍒" natural join b "🍌" natural join a "🍏" ;
LOG:  pg_hint_plan[qno=0x1e]: post_parse_analyze_hook: hints from table: "Leading( ( ("🍏" "🍌") "🍒" ) ) HashJoin( "🍏" "🍌" ) HashJoin("🍏" "🍌" "🍒") SeqScan("🍏") IndexOnlyScan("🍌") SeqScan("🍒")": normalized_query="select * from c "🍒" natural join b "🍌" natural join a "🍏" ;", application name ="psql"
LOG:  pg_hint_plan[qno=0x1c]: planner
LOG:  pg_hint_plan[qno=0x1c]: setup_hint_enforcement index deletion: relation=16659(c), inhparent=0, current_hint_state=0xe094648, hint_inhibit_level=0, scanmask=0x1
LOG:  pg_hint_plan[qno=0x1c]: setup_hint_enforcement index deletion: relation=16654(b), inhparent=0, current_hint_state=0xe094648, hint_inhibit_level=0, scanmask=0x12
LOG:  pg_hint_plan[qno=0x1c]: setup_hint_enforcement index deletion: relation=16649(a), inhparent=0, current_hint_state=0xe094648, hint_inhibit_level=0, scanmask=0x1
LOG:  pg_hint_plan[qno=0x1c]: HintStateDump: {used hints:IndexOnlyScan(🍌)SeqScan(🍏)SeqScan(🍒)HashJoin(🍌 🍏)HashJoin(🍌 🍏 🍒)Leading(((🍏 🍌) 🍒))}, {not used hints:(none)}, {duplicate hints:(none)}, {error hints:(none)}
 n | x
---+---
(0 rows)


hint_planqueryid에 의해 주입된 힌트가 pg_hint_plan.debug_print=verbose에 표시됩니다.

좋은 웹페이지 즐겨찾기