[PATCH] A hook for session start

Started by Yugo Nagataover 8 years ago65 messageshackers
Jump to latest
#1Yugo Nagata
nagata@sraoss.co.jp

Hi,

Currently, PostgreSQL doen't have a hook triggered at session
start. Although we already have ClientAuthentication_hook,
this is triggered during authentication, so we can not
access the database.

If we have a hook triggerd only once at session start, we may
do something useful on the session for certain database or user.

For example, one of our clients wanted such feature. He wanted
to handle encription for specific users, though I don't know
the detail.

The attached patch (session_start_hook.patch) implements such
hook.

Another patch, session_start_sample.patch, is a very simple
example of this hook that changes work_mem values for sessions
of a specific database.

I would appreciate hearing your opinion on this hook.

Regards,

--
Yugo Nagata <nagata@sraoss.co.jp>

Attachments:

session_start_hook.patchtext/x-diff; name=session_start_hook.patchDownload+11-0
session_start_sample.patchtext/x-diff; name=session_start_sample.patchDownload+68-0
#2Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Yugo Nagata (#1)
Re: [PATCH] A hook for session start

On Thu, Jul 20, 2017 at 8:47 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

Hi,

Currently, PostgreSQL doen't have a hook triggered at session
start. Although we already have ClientAuthentication_hook,
this is triggered during authentication, so we can not
access the database.

If we have a hook triggerd only once at session start, we may
do something useful on the session for certain database or user.

For example, one of our clients wanted such feature. He wanted
to handle encription for specific users, though I don't know
the detail.

The attached patch (session_start_hook.patch) implements such
hook.

Another patch, session_start_sample.patch, is a very simple
example of this hook that changes work_mem values for sessions
of a specific database.

I would appreciate hearing your opinion on this hook.

I'm not sure your real needs but doesn't it material for improve Event
Triggers???

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

#3Robert Haas
robertmhaas@gmail.com
In reply to: Fabrízio de Royes Mello (#2)
Re: [PATCH] A hook for session start

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve Event
Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you respond
to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single user
mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also less
accessible to the average user.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Craig Ringer
craig@2ndquadrant.com
In reply to: Robert Haas (#3)
Re: [PATCH] A hook for session start

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve Event
Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you respond
to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single user
mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also less
accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can be used
by an extension to implement trigger-like behaviour if anyone wants it,
including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice where we
don't have something that we don't have anything compelling destined for
core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like DDL
replication can be prototyped as extensions more practically).

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to serve the
same job as a session-start hook, albeit at slightly higher overhead? You
can just test to see if your initial tasks have run yet.

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

#5Yugo Nagata
nagata@sraoss.co.jp
In reply to: Craig Ringer (#4)
Re: [PATCH] A hook for session start

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve Event
Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you respond
to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single user
mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also less
accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can be used
by an extension to implement trigger-like behaviour if anyone wants it,
including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice where we
don't have something that we don't have anything compelling destined for
core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like DDL
replication can be prototyped as extensions more practically).

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to serve the
same job as a session-start hook, albeit at slightly higher overhead? You
can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job of a
session-start hook using these existing hooks, although these hooks are
triggered when the first query is executed not when the session is started.
Now I come to think that an additional hook is not need.

Thanks,

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

--
Yugo Nagata <nagata@sraoss.co.jp>

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#6Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Yugo Nagata (#5)
Re: [PATCH] A hook for session start

On Fri, Jul 21, 2017 at 9:35 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve

Event

Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you respond
to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single user
mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also less
accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can be

used

by an extension to implement trigger-like behaviour if anyone wants it,
including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice

where we

don't have something that we don't have anything compelling destined for
core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like DDL
replication can be prototyped as extensions more practically).

I agree with you both...

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to serve

the

same job as a session-start hook, albeit at slightly higher overhead?

You

can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job of a
session-start hook using these existing hooks, although these hooks are
triggered when the first query is executed not when the session is

started.

Now I come to think that an additional hook is not need.

As Nagata said hooks proposed by Craing will happens only when the first
query is called so I don't know how it works for session start... are we
missing something?

If we're going to add this hook what about add a session end hook also?

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

#7Yugo Nagata
nagata@sraoss.co.jp
In reply to: Fabrízio de Royes Mello (#6)
Re: [PATCH] A hook for session start

On Fri, 21 Jul 2017 10:31:57 -0300
Fabrízio de Royes Mello <fabriziomello@gmail.com> wrote:

On Fri, Jul 21, 2017 at 9:35 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve

Event

Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you respond
to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single user
mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also less
accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can be

used

by an extension to implement trigger-like behaviour if anyone wants it,
including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice

where we

don't have something that we don't have anything compelling destined for
core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like DDL
replication can be prototyped as extensions more practically).

I agree with you both...

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to serve

the

same job as a session-start hook, albeit at slightly higher overhead?

You

can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job of a
session-start hook using these existing hooks, although these hooks are
triggered when the first query is executed not when the session is

started.

Now I come to think that an additional hook is not need.

As Nagata said hooks proposed by Craing will happens only when the first
query is called so I don't know how it works for session start... are we
missing something?

Yes, ExecutorStart_hook + ProcessUtility_hook is not strictly same as
session_start hook. If a query is issued a long time since the session start,
the timing the hook happens is largely deviated. It is no problem if we only
want do something once at the session start, but it might be problem if
we want to record the timestamp of the session start, for example.

If we're going to add this hook what about add a session end hook also?

If someone want the session-start hook, he might want this too.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

--
Yugo Nagata <nagata@sraoss.co.jp>

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8Craig Ringer
craig@2ndquadrant.com
In reply to: Yugo Nagata (#7)
Re: [PATCH] A hook for session start

On 21 Jul. 2017 21:58, "Yugo Nagata" <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 10:31:57 -0300
Fabrízio de Royes Mello <fabriziomello@gmail.com> wrote:

On Fri, Jul 21, 2017 at 9:35 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve

Event

Triggers???

I've thought about that, too. One problem is what to do if the user
hits ^C while the event trigger procedure is running. If you

respond

to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted trigger
can lock users out of the database. Maybe that's OK. We could say
"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single

user

mode to recover".

A hook, as proposed here, is a lot simpler and lacks these concerns.
Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also

less

accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can be

used

by an extension to implement trigger-like behaviour if anyone wants

it,

including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice

where we

don't have something that we don't have anything compelling destined

for

core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like DDL
replication can be prototyped as extensions more practically).

I agree with you both...

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to

serve

the

same job as a session-start hook, albeit at slightly higher overhead?

You

can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job of a
session-start hook using these existing hooks, although these hooks are
triggered when the first query is executed not when the session is

started.

Now I come to think that an additional hook is not need.

As Nagata said hooks proposed by Craing will happens only when the first
query is called so I don't know how it works for session start... are we
missing something?

Yes, ExecutorStart_hook + ProcessUtility_hook is not strictly same as
session_start hook. If a query is issued a long time since the session
start,
the timing the hook happens is largely deviated. It is no problem if we only
want do something once at the session start, but it might be problem if
we want to record the timestamp of the session start, for example.

Don't we have that timestamp already?

What practical use cases are there for acting post-auth but that can't wait
until the user tries to do something?

Can a user do anything remotely interesting or useful without hitting
either ExecutorStart_hook or ProcessUtility_hook? They can parse queries I
guess but you could just set your hook up in the parser instead. If you
hook the parser all they can do is open an idle session and sit there...

So given that you can effectively do it already at the C hook level, if
you're going to do it at all I guess it it'd be more interesting to expose
a convenient event trigger for session start. As others suggested upthread.
So it's easy for DBAs and devs who won't have any idea where to start
writing extensions that register hooks.

But... I think you need a good use case. Such a trigger would have no way
to receive parameters from the user (except custom GUCs) or report any sort
of result other than an error/warning/notice. So what's it going to do that
can't already be decided by pg_hba.cond, pg_authid etc?

#9Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Yugo Nagata (#7)
Re: [PATCH] A hook for session start

On Fri, Jul 21, 2017 at 10:58 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 10:31:57 -0300
Fabrízio de Royes Mello <fabriziomello@gmail.com> wrote:

On Fri, Jul 21, 2017 at 9:35 AM, Yugo Nagata <nagata@sraoss.co.jp>

wrote:

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for improve

Event

Triggers???

I've thought about that, too. One problem is what to do if the

user

hits ^C while the event trigger procedure is running. If you

respond

to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If you
ignore ^C or make it turn into FATAL, then a poorly-crafted

trigger

can lock users out of the database. Maybe that's OK. We could

say

"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in single

user

mode to recover".

A hook, as proposed here, is a lot simpler and lacks these

concerns.

Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's also

less

accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and can

be

used

by an extension to implement trigger-like behaviour if anyone wants

it,

including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice

where we

don't have something that we don't have anything compelling

destined for

core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features like

DDL

replication can be prototyped as extensions more practically).

I agree with you both...

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to

serve

the

same job as a session-start hook, albeit at slightly higher

overhead?

You

can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job

of a

session-start hook using these existing hooks, although these hooks

are

triggered when the first query is executed not when the session is

started.

Now I come to think that an additional hook is not need.

As Nagata said hooks proposed by Craing will happens only when the first
query is called so I don't know how it works for session start... are we
missing something?

Yes, ExecutorStart_hook + ProcessUtility_hook is not strictly same as
session_start hook. If a query is issued a long time since the session

start,

the timing the hook happens is largely deviated. It is no problem if we

only

want do something once at the session start, but it might be problem if
we want to record the timestamp of the session start, for example.

If we're going to add this hook what about add a session end hook also?

If someone want the session-start hook, he might want this too.

Well if someone wants here are the patches... I just did a minor fix and
cleanup in your previous session_start sample and provide both samples into
the same patch.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

Attachments:

001_session_start_hook.patchtext/x-patch; charset=US-ASCII; name=001_session_start_hook.patchDownload+11-0
002_session_end_hook.patchtext/x-patch; charset=US-ASCII; name=002_session_end_hook.patchDownload+27-0
003_session_hooks_samples.patchtext/x-patch; charset=US-ASCII; name=003_session_hooks_samples.patchDownload+127-0
#10Robert Haas
robertmhaas@gmail.com
In reply to: Craig Ringer (#8)
Re: [PATCH] A hook for session start

On Fri, Jul 21, 2017 at 11:10 AM, Craig Ringer <craig@2ndquadrant.com> wrote:

Don't we have that timestamp already?

What practical use cases are there for acting post-auth but that can't wait
until the user tries to do something?

Have, yes; record, no.

Can a user do anything remotely interesting or useful without hitting either
ExecutorStart_hook or ProcessUtility_hook? They can parse queries I guess
but you could just set your hook up in the parser instead. If you hook the
parser all they can do is open an idle session and sit there...

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#11Jim Mlodgenski
jimmy76@gmail.com
In reply to: Robert Haas (#10)
Re: [PATCH] A hook for session start

Can a user do anything remotely interesting or useful without hitting

either

ExecutorStart_hook or ProcessUtility_hook? They can parse queries I guess
but you could just set your hook up in the parser instead. If you hook

the

parser all they can do is open an idle session and sit there...

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

When I first saw this thread, my initial thought of a use case is to
prepare some key application queries so they are there and ready to go.
That would need to be before the ExecutorStart_hook or ProcessUtility_hook
if an app would just want to execute the prepared statement.

#12Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Fabrízio de Royes Mello (#9)
Re: [PATCH] A hook for session start

On Fri, Jul 21, 2017 at 12:19 PM, Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:

On Fri, Jul 21, 2017 at 10:58 AM, Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Fri, 21 Jul 2017 10:31:57 -0300
Fabrízio de Royes Mello <fabriziomello@gmail.com> wrote:

On Fri, Jul 21, 2017 at 9:35 AM, Yugo Nagata <nagata@sraoss.co.jp>

wrote:

On Fri, 21 Jul 2017 09:53:19 +0800
Craig Ringer <craig@2ndquadrant.com> wrote:

On 21 July 2017 at 08:42, Robert Haas <robertmhaas@gmail.com>

wrote:

On Thu, Jul 20, 2017 at 8:27 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:

I'm not sure your real needs but doesn't it material for

improve

Event

Triggers???

I've thought about that, too. One problem is what to do if the

user

hits ^C while the event trigger procedure is running. If you

respond

to that by killing the event trigger and letting the user issue
commands, then the event trigger can't be used for security or
auditing purposes because the user might prevent it from doing
whatever it's intended to do with a well-timed interrupt. If

you

ignore ^C or make it turn into FATAL, then a poorly-crafted

trigger

can lock users out of the database. Maybe that's OK. We could

say

"well, if you lock yourself out of the database with your logon
trigger, you get to shut down the database and restart in

single user

mode to recover".

A hook, as proposed here, is a lot simpler and lacks these

concerns.

Installing code in C into the database is intrinsically risky
anywhere, and not any moreso here than elsewhere. But it's

also less

accessible to the average user.
<http://www.postgresql.org/mailpref/pgsql-hackers&gt;

I'd favour the c hook personally. It's a lot more flexible, and

can be

used

by an extension to implement trigger-like behaviour if anyone

wants it,

including the extension's choice of error handling decisions.

It's also a lot simpler and less intrusive for core. Which is nice

where we

don't have something that we don't have anything compelling

destined for

core that needs it. (I want to add a bunch of hooks in the logical
replication code in pg11 for similar reasons, and so features

like DDL

replication can be prototyped as extensions more practically).

I agree with you both...

That said, isn't ExecutorStart_hook + ProcessUtility_hook able to

serve

the

same job as a session-start hook, albeit at slightly higher

overhead?

You

can just test to see if your initial tasks have run yet.

Thank you for your suggestion. Certainly, we can do the similar job

of a

session-start hook using these existing hooks, although these hooks

are

triggered when the first query is executed not when the session is

started.

Now I come to think that an additional hook is not need.

As Nagata said hooks proposed by Craing will happens only when the

first

query is called so I don't know how it works for session start... are

we

missing something?

Yes, ExecutorStart_hook + ProcessUtility_hook is not strictly same as
session_start hook. If a query is issued a long time since the session

start,

the timing the hook happens is largely deviated. It is no problem if we

only

want do something once at the session start, but it might be problem if
we want to record the timestamp of the session start, for example.

If we're going to add this hook what about add a session end hook

also?

If someone want the session-start hook, he might want this too.

Well if someone wants here are the patches... I just did a minor fix and

cleanup in your previous session_start sample and provide both samples into
the same patch.

I made a mistake on previous patch... now the attached three patches in
their correct orders.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

Attachments:

001_session_start_hook_v1.patchtext/x-patch; charset=US-ASCII; name=001_session_start_hook_v1.patchDownload+11-0
002_session_end_hook_v1.patchtext/x-patch; charset=US-ASCII; name=002_session_end_hook_v1.patchDownload+26-3
003_session_hooks_samples_v1.patchtext/x-patch; charset=US-ASCII; name=003_session_hooks_samples_v1.patchDownload+127-0
#13Peter Eisentraut
peter_e@gmx.net
In reply to: Jim Mlodgenski (#11)
Re: [PATCH] A hook for session start

On 7/21/17 13:14, Jim Mlodgenski wrote:

When I first saw this thread, my initial thought of a use case is to
prepare some key application queries so they are there and ready to go.
That would need to be before the ExecutorStart_hook or
ProcessUtility_hook if an app would just want to execute the prepared
statement.

Isn't that what the preprepare extension does already?

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#14Peter Eisentraut
peter_e@gmx.net
In reply to: Yugo Nagata (#1)
Re: [PATCH] A hook for session start

On 7/20/17 07:47, Yugo Nagata wrote:

Another patch, session_start_sample.patch, is a very simple
example of this hook that changes work_mem values for sessions
of a specific database.

I think test modules should go into src/test/modules/ instead of contrib.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#15Peter Eisentraut
peter_e@gmx.net
In reply to: Robert Haas (#10)
Re: [PATCH] A hook for session start

On 7/21/17 12:59, Robert Haas wrote:

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

Still nobody has presented a concrete use case so far.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#16Andres Freund
andres@anarazel.de
In reply to: Peter Eisentraut (#15)
Re: [PATCH] A hook for session start

On 2017-08-01 15:37:40 -0400, Peter Eisentraut wrote:

On 7/21/17 12:59, Robert Haas wrote:

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

Still nobody has presented a concrete use case so far.

Citus for example starts a background worker (performing
e.g. distributed deadlock detection) if the citus extension exists. We
atm need annoying hacks to do so when the first query is executed.

- Andres

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#17Robert Haas
robertmhaas@gmail.com
In reply to: Peter Eisentraut (#15)
Re: [PATCH] A hook for session start

On Tue, Aug 1, 2017 at 3:37 PM, Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

On 7/21/17 12:59, Robert Haas wrote:

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

Still nobody has presented a concrete use case so far.

I've been asked for this at EDB, too. Inserting a row into some table
on each logon, for example.

A quick Google search found 6 previous requests for this feature, some
of which describe intended use cases:

/messages/by-id/4EBC6852.5030605@fuzzy.cz (2011)
/messages/by-id/CAHyXU0wrsYShxmwBxZSGYoiBJa=gzEJ17iAeRvaf_vA+coH_qA@mail.gmail.com
(2011)
/messages/by-id/BAY104-W513CF26C0046C9D28747B8D1DD0@phx.gbl
(2009, in Spanish)
/messages/by-id/758d5e7f0803130227m558d32cdl7159bed00d21f084@mail.gmail.com
(2008)
/messages/by-id/001a01c48077$0b118e60$0200030a@gendron.ca
(2004)
/messages/by-id/F96SgcOrBLSAQV6uPDV00000a2b@hotmail.com
(2000)

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#18Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Robert Haas (#17)
Re: [PATCH] A hook for session start

On Tue, Aug 1, 2017 at 4:55 PM, Robert Haas <robertmhaas@gmail.com> wrote:

On Tue, Aug 1, 2017 at 3:37 PM, Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

On 7/21/17 12:59, Robert Haas wrote:

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

Still nobody has presented a concrete use case so far.

I've been asked for this at EDB, too. Inserting a row into some table
on each logon, for example.

A quick Google search found 6 previous requests for this feature, some
of which describe intended use cases:

/messages/by-id/4EBC6852.5030605@fuzzy.cz (2011)

/messages/by-id/CAHyXU0wrsYShxmwBxZSGYoiBJa=gzEJ17iAeRvaf_vA+coH_qA@mail.gmail.com

(2011)

/messages/by-id/BAY104-W513CF26C0046C9D28747B8D1DD0@phx.gbl

(2009, in Spanish)

/messages/by-id/758d5e7f0803130227m558d32cdl7159bed00d21f084@mail.gmail.com

(2008)

/messages/by-id/001a01c48077$0b118e60$0200030a@gendron.ca

(2004)

/messages/by-id/F96SgcOrBLSAQV6uPDV00000a2b@hotmail.com

(2000)

Hi all,

I'm sending a new rebased patches and added tests to src/tests/modules as
suggested before.

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

Attachments:

0001-session_start_hook-v2.patchtext/x-patch; charset=US-ASCII; name=0001-session_start_hook-v2.patchDownload+11-0
0002-session_end_hook-v2.patchtext/x-patch; charset=US-ASCII; name=0002-session_end_hook-v2.patchDownload+26-3
0003-session_hooks_tests-v3.patchtext/x-patch; charset=US-ASCII; name=0003-session_hooks_tests-v3.patchDownload+159-0
#19Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Fabrízio de Royes Mello (#18)
Re: [PATCH] A hook for session start

On Thu, Oct 5, 2017 at 4:14 PM, Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:

On Tue, Aug 1, 2017 at 4:55 PM, Robert Haas <robertmhaas@gmail.com> wrote:

On Tue, Aug 1, 2017 at 3:37 PM, Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

On 7/21/17 12:59, Robert Haas wrote:

That's an exceedingly-weak argument for rejecting this patch. The
fact that you can probably hack around the lack of a hook for most
reasonable use cases is not an argument for having a hook that does
what people actually want to do.

Still nobody has presented a concrete use case so far.

I've been asked for this at EDB, too. Inserting a row into some table
on each logon, for example.

A quick Google search found 6 previous requests for this feature, some
of which describe intended use cases:

/messages/by-id/4EBC6852.5030605@fuzzy.cz (2011)

/messages/by-id/CAHyXU0wrsYShxmwBxZSGYoiBJa=gzEJ17iAeRvaf_vA+coH_qA@mail.gmail.com

(2011)

/messages/by-id/BAY104-W513CF26C0046C9D28747B8D1DD0@phx.gbl

(2009, in Spanish)

/messages/by-id/758d5e7f0803130227m558d32cdl7159bed00d21f084@mail.gmail.com

(2008)

/messages/by-id/001a01c48077$0b118e60$0200030a@gendron.ca

(2004)

/messages/by-id/F96SgcOrBLSAQV6uPDV00000a2b@hotmail.com

(2000)

Hi all,

I'm sending a new rebased patches and added tests to src/tests/modules as

suggested before.

Also added for the next commitfest:

https://commitfest.postgresql.org/15/1318/

Att,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

Show quoted text

Timbira: http://www.timbira.com.br
Blog: http://fabriziomello.github.io
Linkedin: http://br.linkedin.com/in/fabriziomello
Twitter: http://twitter.com/fabriziomello
Github: http://github.com/fabriziomello

#20Nico Williams
nico@cryptonector.com
In reply to: Craig Ringer (#8)
Re: [PATCH] A hook for session start

On Fri, Jul 21, 2017 at 11:10:52PM +0800, Craig Ringer wrote:

What practical use cases are there for acting post-auth but that can't wait
until the user tries to do something?

Creating TEMP schema that triggers and functions might need.

Doing CREATE TEMP TABLE IF NOT EXISTS in triggers slows things down.

It'd be super nice if PostgreSQL had some sort of persistent TEMP
schema option, where you can have schema elements that are persistent
in that they're always there, but where the data is all TEMP. Oracle
has this and they call it GLOBAL TEMP IIRC. There would be some
caveats, such as not being able to have FKs between these sorts of
persistent temp tables and persistent tables.

In the absence of such a feature, a session hook/trigger is a great
workaround.

Can a user do anything remotely interesting or useful without hitting
either ExecutorStart_hook or ProcessUtility_hook? They can parse queries I
guess but you could just set your hook up in the parser instead. If you
hook the parser all they can do is open an idle session and sit there...

In any other hook you'd have to check whether the session setup work you
wanted to do has been done. That could be potentially slow.

I actually have an all SQL implementation of session/begin/commit
triggers. The session triggers in that implementation only run on the
first DML statement, which could be too late for OP's purpose.

Nico
--

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#21Nico Williams
nico@cryptonector.com
In reply to: Peter Eisentraut (#13)
#22Pavel Stehule
pavel.stehule@gmail.com
In reply to: Nico Williams (#21)
#23Craig Ringer
craig@2ndquadrant.com
In reply to: Pavel Stehule (#22)
#24Nico Williams
nico@cryptonector.com
In reply to: Pavel Stehule (#22)
#25Nico Williams
nico@cryptonector.com
In reply to: Craig Ringer (#23)
#26Pavel Stehule
pavel.stehule@gmail.com
In reply to: Nico Williams (#24)
#27Nico Williams
nico@cryptonector.com
In reply to: Pavel Stehule (#26)
#28Pavel Stehule
pavel.stehule@gmail.com
In reply to: Nico Williams (#27)
#29Nico Williams
nico@cryptonector.com
In reply to: Pavel Stehule (#28)
#30Pavel Stehule
pavel.stehule@gmail.com
In reply to: Nico Williams (#29)
#31Nico Williams
nico@cryptonector.com
In reply to: Pavel Stehule (#30)
#32Pavel Stehule
pavel.stehule@gmail.com
In reply to: Nico Williams (#31)
#33Robert Haas
robertmhaas@gmail.com
In reply to: Nico Williams (#29)
#34Aleksandr Parfenov
a.parfenov@postgrespro.ru
In reply to: Fabrízio de Royes Mello (#19)
#35Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Aleksandr Parfenov (#34)
#36Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#35)
#37Aleksandr Parfenov
a.parfenov@postgrespro.ru
In reply to: Fabrízio de Royes Mello (#35)
#38Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Aleksandr Parfenov (#37)
#39Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#36)
#40Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#39)
#41Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#40)
#42Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#41)
#43Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#42)
#44Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#43)
#45Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#44)
#46Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#45)
#47Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#46)
#48Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#47)
#49Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#48)
#50Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#49)
#51Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Michael Paquier (#50)
#52Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Fabrízio de Royes Mello (#51)
#53Ashutosh Sharma
ashu.coek88@gmail.com
In reply to: Fabrízio de Royes Mello (#52)
#54Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Ashutosh Sharma (#53)
#55Andrew Dunstan
andrew@dunslane.net
In reply to: Fabrízio de Royes Mello (#52)
#56Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrew Dunstan (#55)
#57Andrew Dunstan
andrew@dunslane.net
In reply to: Tom Lane (#56)
#58Michael Paquier
michael@paquier.xyz
In reply to: Andrew Dunstan (#57)
#59Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#58)
#60Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#59)
#61Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#60)
#62Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Tom Lane (#61)
#63Tom Lane
tgl@sss.pgh.pa.us
In reply to: Fabrízio de Royes Mello (#62)
#64Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Tom Lane (#63)
#65Michael Paquier
michael@paquier.xyz
In reply to: Fabrízio de Royes Mello (#64)