pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Schema in trigger in logical replication
4+ messages / 3 participants
[nested] [flat]

* Schema in trigger in logical replication
@ 2021-03-03 22:35 Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>
  2021-03-03 23:19 ` Re: Schema in trigger in logical replication Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Fontana Daniel C. \(Desartec S.R.L.\) @ 2021-03-03 22:35 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Using postgres 12.5 in DBA schema, this trigger is executed when the table
is updated through a logical replication. Why is it necessary to name the
schema for it to work?
When I update the table manually, if it Works.

Example.

This trigger function does not work

CREATE FUNCTION dba.ft_pos_sync_eco_tx()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
begin
	INSERT INTO pos_sync_eco_rx  
    		  (	id_terminales,   
           		fecha_y_hora_tx,   
           		fecha_y_hora_rx,   
			   	dato,
           		usuario,   
           		fecha_y_hora_proceso )  
	  VALUES (	new.id_terminales,   
    			localtimestamp,   
           		localtimestamp,   
			  	new.dato,
           		new.usuario ,   
           		localtimestamp )
	ON CONFLICT (id_terminales)
	DO UPDATE SET (fecha_y_hora_tx,
				   fecha_y_hora_rx,
				   dato,
				   usuario,
				   fecha_y_hora_proceso ) =
				  (new.fecha_y_hora_tx,
				   localtimestamp,
				   new.dato,
				   new.usuario,
				   new.fecha_y_hora_proceso );
				   
	return	new;
	
end
$BODY$;

This trigger function, if it works

CREATE FUNCTION dba.ft_pos_sync_eco_tx()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
begin
	INSERT INTO dba.pos_sync_eco_rx  
    		  (	id_terminales,   
           		fecha_y_hora_tx,   
           		fecha_y_hora_rx,   
			   	dato,
           		usuario,   
           		fecha_y_hora_proceso )  
	  VALUES (	new.id_terminales,   
    			localtimestamp,   
           		localtimestamp,   
			  	new.dato,
           		new.usuario ,   
           		localtimestamp )
	ON CONFLICT (id_terminales)
	DO UPDATE SET (fecha_y_hora_tx,
				   fecha_y_hora_rx,
				   dato,
				   usuario,
				   fecha_y_hora_proceso ) =
				  (new.fecha_y_hora_tx,
				   localtimestamp,
				   new.dato,
				   new.usuario,
				   new.fecha_y_hora_proceso );
				   
	return	new;
	
end
$BODY$;


-- 
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus






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

* Re: Schema in trigger in logical replication
  2021-03-03 22:35 Schema in trigger in logical replication Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>
@ 2021-03-03 23:19 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2021-03-04 16:58   ` Re: Schema in trigger in logical replication Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2021-03-03 23:19 UTC (permalink / raw)
  To: Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>; pgsql-general@lists.postgresql.org

On 3/3/21 2:35 PM, Fontana Daniel C. (Desartec S.R.L.) wrote:
> Using postgres 12.5 in DBA schema, this trigger is executed when the table
> is updated through a logical replication. Why is it necessary to name the
> schema for it to work?

Because the search_path does include the schema?

> When I update the table manually, if it Works.
> 
> Example.
> 
> This trigger function does not work
> 
> CREATE FUNCTION dba.ft_pos_sync_eco_tx()
>      RETURNS trigger
>      LANGUAGE 'plpgsql'
>      COST 100
>      VOLATILE NOT LEAKPROOF
> AS $BODY$
> begin
> 	INSERT INTO pos_sync_eco_rx
>      		  (	id_terminales,
>             		fecha_y_hora_tx,
>             		fecha_y_hora_rx,
> 			   	dato,
>             		usuario,
>             		fecha_y_hora_proceso )
> 	  VALUES (	new.id_terminales,
>      			localtimestamp,
>             		localtimestamp,
> 			  	new.dato,
>             		new.usuario ,
>             		localtimestamp )
> 	ON CONFLICT (id_terminales)
> 	DO UPDATE SET (fecha_y_hora_tx,
> 				   fecha_y_hora_rx,
> 				   dato,
> 				   usuario,
> 				   fecha_y_hora_proceso ) =
> 				  (new.fecha_y_hora_tx,
> 				   localtimestamp,
> 				   new.dato,
> 				   new.usuario,
> 				   new.fecha_y_hora_proceso );
> 				
> 	return	new;
> 	
> end
> $BODY$;
> 
> This trigger function, if it works
> 
> CREATE FUNCTION dba.ft_pos_sync_eco_tx()
>      RETURNS trigger
>      LANGUAGE 'plpgsql'
>      COST 100
>      VOLATILE NOT LEAKPROOF
> AS $BODY$
> begin
> 	INSERT INTO dba.pos_sync_eco_rx
>      		  (	id_terminales,
>             		fecha_y_hora_tx,
>             		fecha_y_hora_rx,
> 			   	dato,
>             		usuario,
>             		fecha_y_hora_proceso )
> 	  VALUES (	new.id_terminales,
>      			localtimestamp,
>             		localtimestamp,
> 			  	new.dato,
>             		new.usuario ,
>             		localtimestamp )
> 	ON CONFLICT (id_terminales)
> 	DO UPDATE SET (fecha_y_hora_tx,
> 				   fecha_y_hora_rx,
> 				   dato,
> 				   usuario,
> 				   fecha_y_hora_proceso ) =
> 				  (new.fecha_y_hora_tx,
> 				   localtimestamp,
> 				   new.dato,
> 				   new.usuario,
> 				   new.fecha_y_hora_proceso );
> 				
> 	return	new;
> 	
> end
> $BODY$;
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: Schema in trigger in logical replication
  2021-03-03 22:35 Schema in trigger in logical replication Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>
  2021-03-03 23:19 ` Re: Schema in trigger in logical replication Adrian Klaver <adrian.klaver@aklaver.com>
@ 2021-03-04 16:58   ` Adrian Klaver <adrian.klaver@aklaver.com>
  2021-03-04 18:07     ` Re: Schema in trigger in logical replication Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2021-03-04 16:58 UTC (permalink / raw)
  To: Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>; +Cc: pgsql-general

On 3/3/21 3:58 PM, Fontana Daniel C. (Desartec S.R.L.) wrote:
Please reply to list also.
Ccing list.
Also please do not top post, use inline and/or bottom posting.

> When the update is manual, it works.
> It does not work when the update is done using logical replication.
> It is as if the logical replication wizard did not use the search_path

Replication would imply at least two database instances in use. If they 
both don't have the same search_path set then there would be a problem. 
In psql does:

SHOW search_path;

return the same thing on both sides of the replication?

FYI, your life will be easier if you schema qualify objects whenever 
possible in any case.

> 
> -----Mensaje original-----
> De: Adrian Klaver [mailto:adrian.klaver@aklaver.com]
> Enviado el: miércoles, 3 de marzo de 2021 20:19
> Para: Fontana Daniel C. (Desartec S.R.L.); pgsql-general@lists.postgresql.org
> Asunto: Re: Schema in trigger in logical replication
> 
> On 3/3/21 2:35 PM, Fontana Daniel C. (Desartec S.R.L.) wrote:
>> Using postgres 12.5 in DBA schema, this trigger is executed when the
>> table is updated through a logical replication. Why is it necessary to
>> name the schema for it to work?
> 
> Because the search_path does include the schema?
> 
>> When I update the table manually, if it Works.
>>
>> Example.
>>
>> This trigger function does not work
>>
>> CREATE FUNCTION dba.ft_pos_sync_eco_tx()
>>       RETURNS trigger
>>       LANGUAGE 'plpgsql'
>>       COST 100
>>       VOLATILE NOT LEAKPROOF
>> AS $BODY$
>> begin
>> 	INSERT INTO pos_sync_eco_rx
>>       		  (	id_terminales,
>>              		fecha_y_hora_tx,
>>              		fecha_y_hora_rx,
>> 			   	dato,
>>              		usuario,
>>              		fecha_y_hora_proceso )
>> 	  VALUES (	new.id_terminales,
>>       			localtimestamp,
>>              		localtimestamp,
>> 			  	new.dato,
>>              		new.usuario ,
>>              		localtimestamp )
>> 	ON CONFLICT (id_terminales)
>> 	DO UPDATE SET (fecha_y_hora_tx,
>> 				   fecha_y_hora_rx,
>> 				   dato,
>> 				   usuario,
>> 				   fecha_y_hora_proceso ) =
>> 				  (new.fecha_y_hora_tx,
>> 				   localtimestamp,
>> 				   new.dato,
>> 				   new.usuario,
>> 				   new.fecha_y_hora_proceso );
>> 				
>> 	return	new;
>> 	
>> end
>> $BODY$;
>>
>> This trigger function, if it works
>>
>> CREATE FUNCTION dba.ft_pos_sync_eco_tx()
>>       RETURNS trigger
>>       LANGUAGE 'plpgsql'
>>       COST 100
>>       VOLATILE NOT LEAKPROOF
>> AS $BODY$
>> begin
>> 	INSERT INTO dba.pos_sync_eco_rx
>>       		  (	id_terminales,
>>              		fecha_y_hora_tx,
>>              		fecha_y_hora_rx,
>> 			   	dato,
>>              		usuario,
>>              		fecha_y_hora_proceso )
>> 	  VALUES (	new.id_terminales,
>>       			localtimestamp,
>>              		localtimestamp,
>> 			  	new.dato,
>>              		new.usuario ,
>>              		localtimestamp )
>> 	ON CONFLICT (id_terminales)
>> 	DO UPDATE SET (fecha_y_hora_tx,
>> 				   fecha_y_hora_rx,
>> 				   dato,
>> 				   usuario,
>> 				   fecha_y_hora_proceso ) =
>> 				  (new.fecha_y_hora_tx,
>> 				   localtimestamp,
>> 				   new.dato,
>> 				   new.usuario,
>> 				   new.fecha_y_hora_proceso );
>> 				
>> 	return	new;
>> 	
>> end
>> $BODY$;
>>
>>
> 
> 
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: Schema in trigger in logical replication
  2021-03-03 22:35 Schema in trigger in logical replication Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>
  2021-03-03 23:19 ` Re: Schema in trigger in logical replication Adrian Klaver <adrian.klaver@aklaver.com>
  2021-03-04 16:58   ` Re: Schema in trigger in logical replication Adrian Klaver <adrian.klaver@aklaver.com>
@ 2021-03-04 18:07     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2021-03-04 18:07 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>; pgsql-general

Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 3/3/21 3:58 PM, Fontana Daniel C. (Desartec S.R.L.) wrote:
>> When the update is manual, it works.
>> It does not work when the update is done using logical replication.
>> It is as if the logical replication wizard did not use the search_path

> Replication would imply at least two database instances in use. If they 
> both don't have the same search_path set then there would be a problem. 

I'm fairly sure that replication workers run with search_path set
to just "pg_catalog" for security reasons.  Any user-written code
that needs to execute in a replication worker *must* schema-qualify
all references to non-system objects.

As a general rule, code that runs in expression indexes, check
constraints, and the like needs to be written to not make assumptions
about what search path it's invoked with.  Just fix it; your life
will be less painful.

			regards, tom lane





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


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

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-03 22:35 Schema in trigger in logical replication Fontana Daniel C. (Desartec S.R.L.) <desartecsrl@gmail.com>
2021-03-03 23:19 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-03-04 16:58   ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-03-04 18:07     ` Tom Lane <tgl@sss.pgh.pa.us>

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