pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14699: Statement trigger and logical replication
8+ messages / 6 participants
[nested] [flat]

* BUG #14699: Statement trigger and logical replication
@ 2017-06-09 16:01  konst583@gmail.com
  0 siblings, 1 reply; 8+ messages in thread

From: konst583@gmail.com @ 2017-06-09 16:01 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14699
Logged by:          Konstantin Evteev
Email address:      konst583@gmail.com
PostgreSQL version: 10beta1
Operating system:   Debian GNU/Linux 8 (jessie)
Description:        

Hello!
I have found a bug in logical replication and statement trigger on
subscriber.
Statement trigger works on initialisation table statement.
But then it doesn't work.
I'm using postgres 10 compiled from master branch
commit af51fea039bb8e00066d68d919312df1701dc03e



-- create database test_src;
-- create database test_dst;

\c test_src

create table tbl(id int primary key, v text);

create publication pub
for table tbl;





\c test_dst

create table tbl(id int primary key, v text);

create function show_stmt() returns trigger language plpgsql as
$$
declare
  r record;
begin
  raise notice 'role: %, level: %, name: %, newtbl:',
current_setting('session_replication_role'), tg_level, tg_name;
  for r in select * from newtbl
  loop
    raise notice '    %', r;
  end loop;
  return null;
end
$$;


create trigger show_stmt
 after insert on tbl
 referencing new table as newtbl
 for each statement
 execute procedure show_stmt();

alter table tbl enable always trigger show_stmt;

----

create subscription sub
connection 'dbname=test_src host=postgres-test01 port=5420'
publication pub
with (create_slot = false);

--------------
in log - we can see that trigger works:
--------------
2017-06-09 18:29:31.377 MSK [27517] STATEMENT:  create subscription sub
        connection 'dbname=test_src'
        publication pub
        with (create_slot = false);
2017-06-09 18:30:04.019 MSK [27517] ERROR:  invalid connection string
syntax: missing "=" after "-h" in connection info string
        
2017-06-09 18:30:04.019 MSK [27517] STATEMENT:  create subscription sub
        connection 'dbname=test_src -h postgres-test01 -p 5420'
        publication pub
        with (create_slot = false);
2017-06-09 18:30:31.616 MSK [27517] NOTICE:  synchronized table states
2017-06-09 18:30:31.619 MSK [28822] LOG:  logical replication apply worker
for subscription "sub" has started
2017-06-09 18:30:31.626 MSK [28823] LOG:  logical replication table
synchronization worker for subscription "sub", table "tbl" has started
2017-06-09 18:30:31.638 MSK [28823] NOTICE:  role: replica, level:
STATEMENT, name: show_stmt, newtbl:
2017-06-09 18:30:31.638 MSK [28823] CONTEXT:  PL/pgSQL function show_stmt()
line 5 at RAISE
2017-06-09 18:30:32.625 MSK [28823] LOG:  logical replication table
synchronization worker for subscription "sub", table "tbl" has finished
--------------

\c test_src

insert into tbl values (1, 'one'), (2, 'two');
insert into tbl values (3, 'three');

--------------
log on dst db did not changed
--------------

but all rows were successfully replicated:
--------------
\c test_dst
test_dst=# select * From tbl;
 id |   v   
----+-------
  1 | one
  2 | two
  3 | three
(3 rows)



--
Konstantin Evteev.


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


^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #14699: Statement trigger and logical replication
@ 2017-06-13 02:57  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: konst583@gmail.com
  0 siblings, 1 reply; 8+ messages in thread

From: Peter Eisentraut @ 2017-06-13 02:57 UTC (permalink / raw)
  To: konst583@gmail.com; pgsql-bugs

On 6/9/17 12:01, konst583@gmail.com wrote:
> I have found a bug in logical replication and statement trigger on
> subscriber.
> Statement trigger works on initialisation table statement.
> But then it doesn't work.

The inserting of rows from the stream is not a "statement".  Therefore,
statement triggers are not fired for that.

The initial data copy is implemented as a COPY command, which is why
statement triggers fire for that.

I think this is all working correctly and as intended.

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


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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #14699: Statement trigger and logical replication
@ 2017-06-16 13:13  Константин Евтеев <konst583@gmail.com>
  parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Константин Евтеев @ 2017-06-16 13:13 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-bugs

2017-06-13 5:57 GMT+03:00 Peter Eisentraut <peter.eisentraut@2ndquadrant.com
>:

>
> I think this is all working correctly and as intended.
>
>
But then, why data copy for init logical replication fires statement
trigger. May be it is also not nedeed?
Or this feature needs to be mentioned in documentation?

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [BUGS] BUG #14699: Statement trigger and logical replication
@ 2017-06-17 01:08  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Константин Евтеев <konst583@gmail.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Peter Eisentraut @ 2017-06-17 01:08 UTC (permalink / raw)
  To: pgsql-hackers <pgsql-hackers@postgresql.org>; +Cc: Константин Евтеев <konst583@gmail.com>

On 6/16/17 09:13, Константин Евтеев wrote:
> 2017-06-13 5:57 GMT+03:00 Peter Eisentraut
> <peter.eisentraut@2ndquadrant.com
> <mailto:peter.eisentraut@2ndquadrant.com>>:
> 
>     I think this is all working correctly and as intended.
> 
> But then, why data copy for init logical replication fires statement
> trigger. May be it is also not nedeed?
> Or this feature needs to be mentioned in documentation?

I don't know.  Hackers?

The issue is that the logical replication initial data copy fires a
statement trigger for INSERT, because it's implemented as a COPY internally.

By contrast, the normal apply worker does not fire any statement
triggers (because they are not "statements").

We could adjust one or the other or leave it as is.

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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [BUGS] BUG #14699: Statement trigger and logical replication
@ 2017-06-17 01:22  Andres Freund <andres@anarazel.de>
  parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Andres Freund @ 2017-06-17 01:22 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-hackers <pgsql-hackers@postgresql.org>; Константин Евтеев <konst583@gmail.com>

On 2017-06-16 21:08:44 -0400, Peter Eisentraut wrote:
> On 6/16/17 09:13, Константин Евтеев wrote:
> > 2017-06-13 5:57 GMT+03:00 Peter Eisentraut
> > <peter.eisentraut@2ndquadrant.com
> > <mailto:peter.eisentraut@2ndquadrant.com>>:
> > 
> >     I think this is all working correctly and as intended.
> > 
> > But then, why data copy for init logical replication fires statement
> > trigger. May be it is also not nedeed?
> > Or this feature needs to be mentioned in documentation?
> 
> I don't know.  Hackers?
> 
> The issue is that the logical replication initial data copy fires a
> statement trigger for INSERT, because it's implemented as a COPY internally.
> 
> By contrast, the normal apply worker does not fire any statement
> triggers (because they are not "statements").
> 
> We could adjust one or the other or leave it as is.

Leave it as is.

- Andres


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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [BUGS] BUG #14699: Statement trigger and logical replication
@ 2017-06-17 01:51  Thomas Munro <thomas.munro@enterprisedb.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 8+ messages in thread

From: Thomas Munro @ 2017-06-17 01:51 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-hackers <pgsql-hackers@postgresql.org>; Константин Евтеев <konst583@gmail.com>

On Sat, Jun 17, 2017 at 1:22 PM, Andres Freund <andres@anarazel.de> wrote:
> On 2017-06-16 21:08:44 -0400, Peter Eisentraut wrote:
>> On 6/16/17 09:13, Константин Евтеев wrote:
>> > 2017-06-13 5:57 GMT+03:00 Peter Eisentraut
>> > <peter.eisentraut@2ndquadrant.com
>> > <mailto:peter.eisentraut@2ndquadrant.com>>:
>> >
>> >     I think this is all working correctly and as intended.
>> >
>> > But then, why data copy for init logical replication fires statement
>> > trigger. May be it is also not nedeed?
>> > Or this feature needs to be mentioned in documentation?
>>
>> I don't know.  Hackers?
>>
>> The issue is that the logical replication initial data copy fires a
>> statement trigger for INSERT, because it's implemented as a COPY internally.
>>
>> By contrast, the normal apply worker does not fire any statement
>> triggers (because they are not "statements").
>>
>> We could adjust one or the other or leave it as is.
>
> Leave it as is.

I also noticed this while working on the open items for transition
tables.  One of my patches adds a comment to execReplication.c about
the need to do a bit more work if/when statement triggers are fired
here in future.  I assume that we'll eventually want statement
triggers to fire if eager incremental matviews depend on that (as is
proposed), or if that set-oriented FK check idea goes somewhere.
Transition tables make statement triggers a lot more powerful
(something like batch-mode after row triggers).

-- 
Thomas Munro
http://www.enterprisedb.com


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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [BUGS] BUG #14699: Statement trigger and logical replication
@ 2017-06-17 12:29  Euler Taveira <euler@timbira.com.br>
  parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Euler Taveira @ 2017-06-17 12:29 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-hackers <pgsql-hackers@postgresql.org>; Константин Евтеев <konst583@gmail.com>

2017-06-16 22:08 GMT-03:00 Peter Eisentraut <peter.eisentraut@2ndquadrant.
com>:

>
> The issue is that the logical replication initial data copy fires a
> statement trigger for INSERT, because it's implemented as a COPY
> internally.
>
> We should document such behavior. AFAICS we discuss later if we should
provide an option to fire statement triggers during initial copy.


> By contrast, the normal apply worker does not fire any statement
> triggers (because they are not "statements").
>
> +1.


> We could adjust one or the other or leave it as is.


Let's leave it as is. At least until 11.


-- 
   Euler Taveira                                   Timbira -
http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento
<http://www.timbira.com.br;

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [BUGS] BUG #14699: Statement trigger and logical replication
@ 2017-07-04 03:39  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Euler Taveira <euler@timbira.com.br>
  0 siblings, 0 replies; 8+ messages in thread

From: Peter Eisentraut @ 2017-07-04 03:39 UTC (permalink / raw)
  To: Euler Taveira <euler@timbira.com.br>; +Cc: pgsql-hackers <pgsql-hackers@postgresql.org>; Константин Евтеев <konst583@gmail.com>

On 6/17/17 08:29, Euler Taveira wrote:
> 2017-06-16 22:08 GMT-03:00 Peter Eisentraut
> <peter.eisentraut@2ndquadrant.com
> <mailto:peter.eisentraut@2ndquadrant.com>>:
> 
> 
>     The issue is that the logical replication initial data copy fires a
>     statement trigger for INSERT, because it's implemented as a COPY
>     internally.
> 
> We should document such behavior. AFAICS we discuss later if we should
> provide an option to fire statement triggers during initial copy.

I have added the documentation.

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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2017-07-04 03:39 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-06-09 16:01 BUG #14699: Statement trigger and logical replication konst583@gmail.com
2017-06-13 02:57 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-06-16 13:13   ` Константин Евтеев <konst583@gmail.com>
2017-06-17 01:08     ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-06-17 01:22       ` Andres Freund <andres@anarazel.de>
2017-06-17 01:51         ` Thomas Munro <thomas.munro@enterprisedb.com>
2017-06-17 12:29       ` Euler Taveira <euler@timbira.com.br>
2017-07-04 03:39         ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

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