pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ivan Panchenko <wao@mail.ru>
To: Andres Freund <andres@anarazel.de>
Cc: a.sokolov@postgrespro.ru, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Greg Nancarrow <gregn4422@gmail.com>
Cc: Teodor Sigaev <teodor@sigaev.ru>
Cc: Ibrar Ahmed <ibrar.ahmad@gmail.com>
Cc: vignesh C <vignesh21@gmail.com>
Cc: Pavel Stehule <pavel.stehule@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Amit Kapila <amit.kapila16@gmail.com>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Daniel Gustafsson <daniel@yesql.se>
Subject: Re[2]: On login trigger: take three
Date: Wed, 30 Mar 2022 14:21:28 +0300
Message-ID: <1648639288.568040808@f194.i.mail.ru> (raw)
In-Reply-To: <20220328213125.omlbcyzngqztxnyi@alap3.anarazel.de>
References: <2E4E472A-69F8-4EAC-B337-3341AC2F0188@yesql.se>
	<3378A73C-4441-43C7-B8F8-1B3611505524@yesql.se>
	<20220328213125.omlbcyzngqztxnyi@alap3.anarazel.de>


 
Hi, 
>Tue, March 29, 2022, 0:31 +03:00 from Andres Freund <andres@anarazel.de>:
> 
>Hi,
>
>On 2022-03-28 23:27:56 +0200, Daniel Gustafsson wrote:
>> > On 28 Mar 2022, at 19:10, Andres Freund < andres@anarazel.de > wrote:
>> > On 2022-03-28 15:57:37 +0300,  a.sokolov@postgrespro.ru wrote:
>>
>> >> + data initialization. It is vital that any event trigger using the
>> >> + <literal>login</literal> event checks whether or not the database is in
>> >> + recovery.
>> »
 
>> >> Does any trigger really have to contain a pg_is_in_recovery() call?
>> >
>> > Not *any* trigger, just any trigger that writes.
>>
>> Thats correct, the docs should be updated with something like the below I
>> reckon.
>>
>> It is vital that event trigger using the <literal>login</literal> event
>> which has side-effects checks whether or not the database is in recovery to
>> ensure they are not performing modifications to hot standby nodes.
>
>Maybe side-effects is a bit too general? Emitting a log message, rejecting a
>login, setting some GUCs, etc are all side-effects too.
Something like this:
 
<important>
    <para>
      The <literal>login</literal> triggers fire also on standby servers.
      To keep them from becoming inaccessible, such triggers should
      avoid writing anything to the database when running on a standby.
      This can be achieved by checking <function>pg_is_in_recovery</function>(), see an example below.
    </para>
</important>
> 
Also, please fix a typo in doc/src/sgml/ref/create_event_trigger.sgml :
 
- single-user mode and you'll be able to do that. Even triggers can also be
+ single-user mode and you'll be able to do that. Event triggers can also be
 
Regarding the trigger function example:
It does not do anything if run on a standby. To show that it can do something on a standby to, I propose to move throwing the night exception to the beginning.
So it will be:
 
CREATE OR REPLACE FUNCTION init_session() 
RETURNS event_trigger SECURITY DEFINER LANGUAGE plpgsql AS 
$$ 
DECLARE 
  hour integer = EXTRACT('hour' FROM current_time); 
  rec boolean;
BEGIN

-- 1) Forbid logging in late:
IF hour BETWEEN 2 AND 4 THEN
  RAISE EXCEPTION 'Login forbidden';  -- do not allow to login these hours
END IF;

-- The remaining stuff cannot be done on standbys,
-- so ensure the database is not in recovery
SELECT pg_is_in_recovery() INTO rec;
IF rec THEN
  RETURN;
END IF

-- 2) Assign some roles

IF hour BETWEEN 8 AND 20 THEN         -- at daytime grant the day_worker role
  EXECUTE 'REVOKE night_worker FROM ' || quote_ident(session_user);
  EXECUTE 'GRANT    day_worker TO '   || quote_ident(session_user);
ELSE                                  -- at other time grant the night_worker role
  EXECUTE 'REVOKE day_worker FROM ' || quote_ident(session_user);
  EXECUTE 'GRANT  night_worker TO ' || quote_ident(session_user);
END IF;

-- 3) Initialize some user session data

CREATE TEMP TABLE session_storage (x float, y integer);

-- 4) Log the connection time

INSERT INTO user_login_log VALUES (session_user, current_timestamp);

END;
$$;
Finally, let me propose to append to the regression test the following:
 
 
\c 
SELECT dathasloginevt FROM pg_database WHERE datname= :'DBNAME';
 
which should output:
 dathasloginevt 
----------------
 f
(1 row)
 
So we can check that removal of the event trigger resets this flag in pg_database. Note that reconnect (\c) is necessary here.
 
Regards,
Ivan
 
>
>> >> In this message
>> >> ( https://www.postgresql.org/message-id/20220312024652.lvgehszwke4hhove%40alap3.anarazel.de )
>> >> it was only about triggers on hot standby, which run not read-only queries
>> >
>> > The problem precisely is that the login triggers run on hot standby nodes, and
>> > that if they do writes, you can't login anymore.
>>
>> Do you think this potential foot-gun is scary enough to reject this patch?
>> There are lots of creative ways to cause Nagios alerts from ones database, but
>> this has the potential to do so with a small bug in userland code. Still, I
>> kind of like the feature so I'm indecisive.
>
>It does seem like a huge footgun. But also kinda useful. So I'm really +-0.
>
>Greetings,
>
>Andres Freund
 

view thread (3+ messages)

Message-ID: <1648639288.568040808@f194.i.mail.ru>
Permalink:  ../1648639288.568040808@f194.i.mail.ru/
Also on:    postgresql.org/message-id/1648639288.568040808@f194.i.mail.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: wao@mail.ru, andres@anarazel.de, tgl@sss.pgh.pa.us, gregn4422@gmail.com, teodor@sigaev.ru, ibrar.ahmad@gmail.com, vignesh21@gmail.com, pavel.stehule@gmail.com, amit.kapila16@gmail.com, sawada.mshk@gmail.com, daniel@yesql.se
  Subject: Re: On login trigger: take three
  In-Reply-To: <1648639288.568040808@f194.i.mail.ru>
  References: <2E4E472A-69F8-4EAC-B337-3341AC2F0188@yesql.se> <3378A73C-4441-43C7-B8F8-1B3611505524@yesql.se> <20220328213125.omlbcyzngqztxnyi@alap3.anarazel.de> <1648639288.568040808@f194.i.mail.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox