Thread: [HACKERS] [PATCH] pg_reload_backend to signal SIGHUP to a specific backend

Hi,

Attached is a patch of pg_reload_backend that is a function signaling
SIGHUP to a specific backend. The original idea is from Michael Paquier[1].
The documatation isn't included in this patch yet.

We can change some parameters of other backend using the function
as bellow.

postgres=# alter system set log_min_messages to debug2;
ALTER SYSTEM
postgres=# select pg_reload_backend(18375);
 pg_reload_backend 
-------------------
 t
(1 row)

There are some usecases. For example:

- changing log configuration in other backends temporally.
- changing cost parameters for planner in other backends.
- changing autovacuum parameters of an already running autovacuum worker.


Hoever, this function need the superuser previlige to execute as well as
pg_reload_conf(). Although we can grant the execution to public, it is
useless for normal users bacause only superuser can change postgresql.conf.

To allow normal users to change parameters in ohter backends, instead
we might want another feature, for example, a function like set_config()
than can change other backend's parameters using shared memory and SIGUSR1.

Any comments would be appreciated.

Regards,

[1]
https://www.postgresql.org/message-id/CAB7nPqT4y8-QoGKEugk99_tFuEOtAshcs5kxOeZ_0w27UtdGyA%40mail.gmail.com

-- 
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

Attachment
> On 28 June 2017 at 12:17, Yugo Nagata <nagata@sraoss.co.jp> wrote:
>
> Hi,
>
> Attached is a patch of pg_reload_backend that is a function signaling
> SIGHUP to a specific backend. The original idea is from Michael Paquier[1].
> The documatation isn't included in this patch yet.

I have few questions. I'm curious, why this function returns something
different from bool when I'm passing null as an argument?

=# select pg_reload_backend(27961);
WARNING:  PID 27961 is not a PostgreSQL server process
WARNING:  failed to send signal to backend: 27961
 pg_reload_backend
-------------------
 f
(1 row)

=# select pg_reload_backend(27962);
 pg_reload_backend
-------------------
 t
(1 row)

=# select pg_reload_backend(null);
 pg_reload_backend
-------------------

(1 row)

Also for some reason I can't grant an execute permission on this function,
am I doing something wrong?

=# grant execute on function pg_reload_backend() to test_user;
ERROR:  function pg_reload_backend() does not exist
=# grant execute on function pg_reload_conf() to test_user;
GRANT

Hi Yugo,

The patch looks fine. Installed and tested.

But a similar function already exists for the Postmaster process.

Datum
pg_reload_conf(PG_FUNCTION_ARGS)
{
        if (kill(PostmasterPid, SIGHUP))
        {
                ereport(WARNING,
                                (errmsg("failed to send signal to postmaster: %m")));
                PG_RETURN_BOOL(false);
        }

        PG_RETURN_BOOL(true);
}

I would suggest to merge your patch with above function which is close to yours.
Btw, your patch looks better that above function code.

You may for instance retain pg_reload_conf() without argument for the same purpose as currently (Postmaster signaling).
And provide a pid argument to signal a given backend.

Regards
Remi
 

2017-06-28 12:17 GMT+02:00 Yugo Nagata <nagata@sraoss.co.jp>:
Hi,

Attached is a patch of pg_reload_backend that is a function signaling
SIGHUP to a specific backend. The original idea is from Michael Paquier[1].
The documatation isn't included in this patch yet.

We can change some parameters of other backend using the function
as bellow.

postgres=# alter system set log_min_messages to debug2;
ALTER SYSTEM
postgres=# select pg_reload_backend(18375);
 pg_reload_backend
-------------------
 t
(1 row)

There are some usecases. For example:

- changing log configuration in other backends temporally.
- changing cost parameters for planner in other backends.
- changing autovacuum parameters of an already running autovacuum worker.


Hoever, this function need the superuser previlige to execute as well as
pg_reload_conf(). Although we can grant the execution to public, it is
useless for normal users bacause only superuser can change postgresql.conf.

To allow normal users to change parameters in ohter backends, instead
we might want another feature, for example, a function like set_config()
than can change other backend's parameters using shared memory and SIGUSR1.

Any comments would be appreciated.

Regards,

[1]
https://www.postgresql.org/message-id/CAB7nPqT4y8-QoGKEugk99_tFuEOtAshcs5kxOeZ_0w27UtdGyA%40mail.gmail.com

--
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


On Wed, 28 Jun 2017 13:35:12 +0200
Dmitry Dolgov <9erthalion6@gmail.com> wrote:

> > On 28 June 2017 at 12:17, Yugo Nagata <nagata@sraoss.co.jp> wrote:
> >
> > Hi,
> >
> > Attached is a patch of pg_reload_backend that is a function signaling
> > SIGHUP to a specific backend. The original idea is from Michael
> Paquier[1].
> > The documatation isn't included in this patch yet.
> 
> I have few questions. I'm curious, why this function returns something
> different from bool when I'm passing null as an argument?

This is because this function is defined as strict, that is pg_proc.proisstrict
is true, as well as pg_reload_conf, pg_terminate_backend, pg_cancel_bacnend, and so on.

> 
> =# select pg_reload_backend(27961);
> WARNING:  PID 27961 is not a PostgreSQL server process
> WARNING:  failed to send signal to backend: 27961
>  pg_reload_backend
> -------------------
>  f
> (1 row)
> 
> =# select pg_reload_backend(27962);
>  pg_reload_backend
> -------------------
>  t
> (1 row)
> 
> =# select pg_reload_backend(null);
>  pg_reload_backend
> -------------------
> 
> (1 row)
> 
> Also for some reason I can't grant an execute permission on this function,
> am I doing something wrong?
> 
> =# grant execute on function pg_reload_backend() to test_user;
> ERROR:  function pg_reload_backend() does not exist
> =# grant execute on function pg_reload_conf() to test_user;
> GRANT

Arg type is needed.

=# grant execute on function pg_reload_backend(int) to test_user;

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



On Wed, 28 Jun 2017 13:59:51 +0200
Remi Colinet <remi.colinet@gmail.com> wrote:

> Hi Yugo,
> 
> The patch looks fine. Installed and tested.

Thank you for your interest.

> 
> But a similar function already exists for the Postmaster process.
> 
> Datum
> pg_reload_conf(PG_FUNCTION_ARGS)
> {
>         if (kill(PostmasterPid, SIGHUP))
>         {
>                 ereport(WARNING,
>                                 (errmsg("failed to send signal to
> postmaster: %m")));
>                 PG_RETURN_BOOL(false);
>         }
> 
>         PG_RETURN_BOOL(true);
> }
> 
> I would suggest to merge your patch with above function which is close to
> yours.
> Btw, your patch looks better that above function code.
> 
> You may for instance retain pg_reload_conf() without argument for the same
> purpose as currently (Postmaster signaling).
> And provide a pid argument to signal a given backend.

Actually, I proposed that version patch in the previous thread[1], but there
as a suggestion to use other function name, so I attached fixed version
in this thread. 

[1] https://www.postgresql.org/message-id/20170624000156.74ca9485.nagata%40sraoss.co.jp

> 
> Regards
> Remi
> 
> 
> 2017-06-28 12:17 GMT+02:00 Yugo Nagata <nagata@sraoss.co.jp>:
> 
> > Hi,
> >
> > Attached is a patch of pg_reload_backend that is a function signaling
> > SIGHUP to a specific backend. The original idea is from Michael Paquier[1].
> > The documatation isn't included in this patch yet.
> >
> > We can change some parameters of other backend using the function
> > as bellow.
> >
> > postgres=# alter system set log_min_messages to debug2;
> > ALTER SYSTEM
> > postgres=# select pg_reload_backend(18375);
> >  pg_reload_backend
> > -------------------
> >  t
> > (1 row)
> >
> > There are some usecases. For example:
> >
> > - changing log configuration in other backends temporally.
> > - changing cost parameters for planner in other backends.
> > - changing autovacuum parameters of an already running autovacuum worker.
> >
> >
> > Hoever, this function need the superuser previlige to execute as well as
> > pg_reload_conf(). Although we can grant the execution to public, it is
> > useless for normal users bacause only superuser can change postgresql.conf.
> >
> > To allow normal users to change parameters in ohter backends, instead
> > we might want another feature, for example, a function like set_config()
> > than can change other backend's parameters using shared memory and SIGUSR1.
> >
> > Any comments would be appreciated.
> >
> > Regards,
> >
> > [1]
> > https://www.postgresql.org/message-id/CAB7nPqT4y8-
> > QoGKEugk99_tFuEOtAshcs5kxOeZ_0w27UtdGyA%40mail.gmail.com
> >
> > --
> > 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
> >
> >


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



> On 29 Jun 2017 08:18, "Yugo Nagata" <nagata@sraoss.co.jp> wrote:
>
> This is because this function is defined as strict

Yes, I realized that few moments after I sent my message.

> Arg type is needed.
>
> =# grant execute on function pg_reload_backend(int) to test_user;

Oh, I see, thank you.