Обсуждение: question about executor hooks

Поиск
Список
Период
Сортировка

question about executor hooks

От
Luca Ferrari
Дата:
In the file backend/executor/execMain.c there are the following hook types:

/* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
ExecutorStart_hook_type ExecutorStart_hook = NULL;
ExecutorRun_hook_type ExecutorRun_hook = NULL;
ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
ExecutorEnd_hook_type ExecutorEnd_hook = NULL;


It is not clear to me what every hook, or better, event is associated
to. I suspect they are:
- start => prepare resources (e.g., allocate memory and stuff)
- run
- finish => execution terminated
- end => free resources

Am I wrong?

Moreover, installing an hook is cluster-wide, right? Is there a way to
limit an hook only for connections related to a single database
(without taking into account FDW and friends)?

Thanks,
Luca



Re: question about executor hooks

От
jian he
Дата:
On Tue, Jan 14, 2025 at 5:08 PM Luca Ferrari <fluca1978@gmail.com> wrote:
>
> In the file backend/executor/execMain.c there are the following hook types:
>
> /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
> ExecutorStart_hook_type ExecutorStart_hook = NULL;
> ExecutorRun_hook_type ExecutorRun_hook = NULL;
> ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
> ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
>

> Moreover, installing an hook is cluster-wide, right? Is there a way to
> limit an hook only for connections related to a single database
> (without taking into account FDW and friends)?
>
> Thanks,
> Luca
>
module contrib/pg_stat_statements/pg_stat_statements.c
using these hooks.

in a Database cluster create extension pg_stat_statements in db A, not
create pg_stat_statements in db B.
then you can do an experiment by using gdb or
add elog(INFO,,,,) at the end of pgss_ExecutorStart.
then see the effect.



Re: question about executor hooks

От
Sami Imseih
Дата:
I recommend you to review the documentation in
backend/executor/README. It explains in good detail how
the executor works. Specifically the section "Query Processing Control Flow"
explains what each of the hooks you reference are responsible for.
Also, be aware that there are operations, called utility statements,
that may not go through the executor; i.e. CREATE TABLE t (id int),
will go through ProcessUtility ( also has a hook ) only as it's a utility
statement and not a plannable statement.

As mentioned above, pg_stat_statements is a good reference extension
that implements these hooks for query level executions statistics.

> Moreover, installing an hook is cluster-wide, right? Is there a way to
> limit an hook only for connections related to a single database
> (without taking into account FDW and friends)?

It depends on the hook. pg_stat_statements for example loads the
hooks at startup time, but the extension itself implements a GUC
that allows a user to skip doing any work inside these hooks.

Regards,

Sami