postgresql - Syntax error in function using dblink to replicate new data -


i never created function in postgres, followed tutorials , made code, don't know why wrong, error in console is:

"syntax error @ or near "select" line 5:     select public.dblink_connect('hostaddr=127.0.0.1 port=54... 

i'm using version 9.3.6 on ubuntu.

create or replace function fn_replicate_insertof_students()       returns text     $body$     begin     select public.dblink_connect('hostaddr=127.0.0.1 port=5433 dbname=utiles user=postgres password=mypass');  insert res_partner (company_id,name,lang,comment,street,supplier,city,zip,country_id,email,phone,date,customer,mobile,ref,state_id,opt_out,city_id,l10n_mx_city2,l10n_mx_street3,l10n_mx_street4,notification_email_send,type,street2,active)                         values  (1,new.name,'es_mx',new.comment,new.street,false,new.city,new.zip,new.country_id,new.email,new.phone,new.date,true,new.mobile,new.ref,new.state_id,false,new.city_id,new.l10n_mx_city2,new.l10n_mx_street3,new.l10n_mx_street4,new.notification_email_send,new.type,new.street2,new.active));  select public.dblink_disconnect();  end; $body$ language sql volatile security definer set search_path=myschema, pg_temp;   create trigger tr_replicate_insertof_students   after insert    on res_partner   each row   execute procedure fn_replicate_insertof_students(); 

@nick raised points, there's more:

your search_path done right. pg_catalog automatically included first unless explicitly put in there on different position.

more importantly, whole function nonsense in current form. open dblink connection, don't use it. looks want throw in dblink_exec(). need concatenate query string values form new first, since new not visible on other side of wormhole. have nice example of dynamic sql. quite steep start beginner!

detailed code example , explanation function dblink in recent related answer on dba.se:

also, must trigger function used in trigger.
function work this:

create or replace function fn_replicate_insertof_students()   returns trigger $func$ begin perform public.dblink_connect('hostaddr=127.0.0.1 port=5433                  dbname=utiles user=postgres password=mypass');  perform public.dblink_exec(format(    $f$insert res_partner (company_id, name, lang, comment, ... )       values  (1, %l, 'es_mx', %l, ... )$f$     , new.name, new.comment, ... ));  perform public.dblink_disconnect();  return null;  -- ok after trigger  end $func$ language plpgsql volatile security definer                         set search_path=myschema, pg_temp; alter function fn_replicate_insertof_students() owner postgres;  -- guessing 

also make sure set owner right security definer function.

consider using foreign server, user mapping , password file on target server. details in above link.

the whole idea rather expensive special case of replication. inserts table, ok, there better solutions massive load.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -