- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2017-08-16T21:52:49+09:00","default:haikikyou","haikikyou")
#author("2017-08-17T20:22:30+09:00","default:haikikyou","haikikyou")
[[PostgreSQL/開発/フック]]
#contents
* フック [#q2d47c89]
|~Hookインターフェース|~説明|h
|post_parse_analyze_hook(ParseState *pstate, Query *query)|アナライズでパースツリーがクエリーツリーに変換された後に呼ばれる。|
|void post_parse_analyze_hook(ParseState *pstate, Query *query)|アナライズでパースツリーがクエリーツリーに変換された後に呼ばれる。|
&size(12){&color(white,orange){ 参考 };}; [[pg_analyze()>https://doxygen.postgresql.org/parser_2analyze_8c.html#a3dff9e1f60fde61c4ee50d4d9f29f4ca]] - &size(11){&color(gray){on [[doxygen.postgresql.org>https://doxygen.postgresql.org]]};};
* サンプル [#w9cc056a]
&size(12){&color(white,#00afcc){ サンプル };};
&size(12){&color(white,#800080){ 実験 };};
全く役に立たないサンプルです。
''myext.c''
#geshi(c){{{
#include "postgres.h"
#include "fmgr.h"
#include "parser/analyze.h"
#include "lib/stringinfo.h"
PG_MODULE_MAGIC;
extern void _PG_init(void);
extern post_parse_analyze_hook_type post_parse_analyze_hook;
static void my_post_parse_analyze_hook(ParseState *pstate, Query *query);
void _PG_init(void)
{
post_parse_analyze_hook = my_post_parse_analyze_hook;
}
static void my_post_parse_analyze_hook(ParseState *pstate, Query *query)
{
Node *node = query->limitCount;
if (!node) {
return;
}
elog(INFO, "my_post_parse_analyze_hook always changes limit count to 1");
// 決め打ちでlimitに定数指定されているとして値を上書きする
// SELECT * FROM hoge LIMIT 10;
// => SELECT * FROM hoge LIMIT 1;
if (nodeTag(node) == T_FuncExpr) {
FuncExpr *expr = (FuncExpr *)node;
Const *con = (Const *)linitial(expr->args);
con->constvalue = Int32GetDatum(1);
}
}
}}}
''postgresql.conf''
#geshi{{{
shared_preload_libraries = 'myext'
}}}
''実行例''
#geshi(sql){{{
postgres=# select count(*) FROM hoge LIMIT 100;
INFO: my_post_parse_analyze_hook always changes limit count to 1
count
-------
101
(1 row)
postgres=# select * FROM hoge LIMIT 100;
INFO: my_post_parse_analyze_hook always changes limit count to 1
id | name
----+------
0 |
(1 row)
}}}
* 参考リンク [#p5771882]
- [[parser/analyze.c>https://doxygen.postgresql.org/parser_2analyze_8c.html]] - &size(11){&color(gray){on [[doxygen.postgresql.org>https://doxygen.postgresql.org]]};};
- [[pg_analyze()>https://doxygen.postgresql.org/parser_2analyze_8c.html#a3dff9e1f60fde61c4ee50d4d9f29f4ca]] - &size(11){&color(gray){on [[doxygen.postgresql.org>https://doxygen.postgresql.org]]};};
* コメント [#a2a74e36]
#comment