Thread: Using fmgr_hook

Using fmgr_hook

From
Sameer Thakur
Date:
Hello,
In the process of implementing my own version of sysdate, i was trying
to use the fmgr_hook.
I had a look at the sepgsql contrib module and tried to do the same by
modifying auto_explain just to test using fmgr_hook.

My code changes are:

static needs_fmgr_hook_type prev_needs_fmgr_hook = NULL;
static fmgr_hook_type prev_fmgr_hook = NULL;

static bool custom_needs_fmgr_hook(Oid functionId);
static void custom_fmgr_hook(FmgrHookEventType event,FmgrInfo *flinfo,
Datum *private);

in PG_init(void)
prev_needs_fmgr_hook = needs_fmgr_hook;
needs_fmgr_hook = custom_needs_fmgr_hook;
prev_fmgr_hook = fmgr_hook;
fmgr_hook = custom_fmgr_hook;

in _PG_fini(void)
needs_fmgr_hook=prev_needs_fmgr_hook;
fmgr_hook=prev_fmgr_hook;


static bool custom_needs_fmgr_hook(Oid functionId)
{
return true;
}
void custom_fmgr_hook(FmgrHookEventType event,FmgrInfo *flinfo, Datum *private)
{
if(flinfo->fn_extra == NULL)
{
TimestampTz current_timestamp = GetCurrentTimestamp();
flinfo->fn_extra = palloc(sizeof(TimestampTz));
flinfo->fn_extra = (void*) current_timestamp;
}
}

To debug i have a breakpoint inside custom_fmgr_hook.

Debugging:
1. Start postgres
2. Start psql connecting to postgres
3. Attach gdb to process spawned off by postmaster  representing psql session.
4. execute select * from now();

Problem:
 The breakpoint seems to get skipped. Just to be sure i put a
breakpoint in explain_ExecutorStart and i could debug that function.
So i am attaching gdb to correct process.
What am i doing wrong?

Thank you,
Sameer


Re: Using fmgr_hook

From
Albe Laurenz
Date:
Sameer Thakur wrote:
> In the process of implementing my own version of sysdate, i was trying
> to use the fmgr_hook.

[...]

> To debug i have a breakpoint inside custom_fmgr_hook.
> 
> Debugging:
> 1. Start postgres
> 2. Start psql connecting to postgres
> 3. Attach gdb to process spawned off by postmaster  representing psql session.
> 4. execute select * from now();
> 
> Problem:
>  The breakpoint seems to get skipped. Just to be sure i put a
> breakpoint in explain_ExecutorStart and i could debug that function.
> So i am attaching gdb to correct process.
> What am i doing wrong?

My experience is that you cannot set breakpoints before the library
is loaded, so you first have to call a function in the library, then
you interrupt and set the breakpoint.

I don't know if there is a way to get around that with gdb on Linux.

Yours,
Laurenz Albe

Re: Using fmgr_hook

From
Sameer Thakur
Date:
Hello,
Thank you for responding
>My experience is that you cannot set breakpoints before the library
>is loaded, so you first have to call a function in the library, then
>you interrupt and set the breakpoint.
I tried to do the following
1. Execute Postgres (now auto_explain is loaded)
2. Start a psql session and attach gdb to forked Postmaster process
3. Now set break point in custom_fmgr_hook
4. Execute select * from now();

Still the breakpoint gets skipped.

Also i checked by putting a breakpoint in explain_ExecutorStart before
starting Postgres (before auto_explain is loaded), and then started
psql session, attached gdb and executed select* from now(), in this
case the debugger does stop at the breakpoint.

The strange thing is i could swear that i had got the debugger to stop
in custom_fmgr_hook , just after i added custom_needs_fmgr_hook and
had figured out that we need to use custom_needs_fmgr_hook and
custom_fmgr_hook together. But i have not been able to reproduce that.
So maybe there is something in what you say. I just cannot nail the
sequence correctly

regards
Sameer


Re: Using fmgr_hook

From
Albe Laurenz
Date:
Sameer Thakur wrote:
>> My experience is that you cannot set breakpoints before the library
>> is loaded, so you first have to call a function in the library, then
>> you interrupt and set the breakpoint.

> I tried to do the following
> 1. Execute Postgres (now auto_explain is loaded)
> 2. Start a psql session and attach gdb to forked Postmaster process
> 3. Now set break point in custom_fmgr_hook
> 4. Execute select * from now();
> 
> Still the breakpoint gets skipped.

But gdb tells you that it cannot set the breakpoint correctly
when you try to, right?

Try like this:
- connect with psql
- call your custom_fmgr_hook
- attach to the backend with gdb
- set the breakpoint
- call custom_fmgr_hook again

Yours,
Laurenz Albe

Re: Using fmgr_hook

From
Sameer Thakur
Date:
Hello,
> Try like this:
> - connect with psql
> - call your custom_fmgr_hook
> - attach to the backend with gdb
> - set the breakpoint
> - call custom_fmgr_hook again

I tried to check in a different way, by just using log statements to
see if customs* hook functions ever get executed. They were not. So it
was not a breakpoint or debugger issue.
Then instead of using auto_explain, we created a user defined sysdate
function, implemented in C and added the fmgr hook code in there.
Now we see custom hook functions being called. So looks like it was
not a debugger issue, but fmgr hook work within user defined functions

Thank you,
Sameer