Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3NWK-0001DF-Nx for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 18:20:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3NWI-0005wG-Sc for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 18:20:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3NWI-0005w9-Jy for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 18:20:22 +0000 Received: from stz-bg.com ([46.40.123.212] helo=mail.stzbg.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3NWG-0007fj-Gy for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 18:20:21 +0000 Received: from www.stz-bg.com (stz-bg.com [IPv6:2a02:6800:0:99::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: condor@stz-bg.com) by mail.stzbg.com (Postfix) with ESMTPSA id D63BD2E012D; Sat, 23 Jan 2021 20:20:18 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stz-bg.com; s=mail; t=1611426018; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cx33mneKJwDgGLki4fibjXPuiOkalMYgJeZZrNjzcm4=; b=ULFsN2ea83sk61Y0++Jgbf7iMjMlwcO0RLDW0oD4O6cPaA4IQadg/MuHDrfS2BgGBBU1O/ wYH8EBNSO8xpQrzx6h9/IoDm3pgqYZ05AkeylaH2LVbxKDB8lpYnlbP9XdM5cnz0fm51fk e3xDnQ6XeZbF8LURQq9oXOTVpB0kf9o= MIME-Version: 1.0 Date: Sat, 23 Jan 2021 20:20:18 +0200 From: Condor To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Need help with trigger In-Reply-To: <62244d0f-03d7-6bed-cb7b-722ad32400ad@aklaver.com> References: <6feca05ea1521b0a4da87bfcfe78c39f@stz-bg.com> <62244d0f-03d7-6bed-cb7b-722ad32400ad@aklaver.com> User-Agent: Roundcube Webmail/1.4.10 Message-ID: <84864856b8ac6e5f35f68ace87c44556@stz-bg.com> X-Sender: condor@stz-bg.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 23-01-2021 18:31, Adrian Klaver wrote: > On 1/23/21 4:57 AM, Condor wrote: >>=20 >>=20 >> Hello ppl, >>=20 >> I need help with trigger how to replace insert command with update.=20 >> External program read data from one table and in every 30 min I need=20 >> to refresh data in arhive table. >=20 > What is the table being read from and is it in the same database? Data is read from another table in the same database but is not in the=20 same format. >=20 > See more comments inline below. >=20 >> What I want is: if data already inserted and end_date is not changed,=20 >> only to update lastseen column. If data not exists to insert data and=20 >> if data exists and end_date is changed >> to update end_date, lastseen and sendto columns. Well, update probably= =20 >> will never happened, this functionality is left for frontend but its=20 >> can be cut off. >>=20 >=20 >>=20 >> and my trigger is: >>=20 >> CREATE OR REPLACE FUNCTION public.log_last_chaged() >> =C2=A0RETURNS trigger >> =C2=A0LANGUAGE plpgsql >> AS $function$ >>=20 >> enddate DATE; >>=20 >> BEGIN >> =C2=A0 IF TG_OP =3D 'INSERT' THEN >> =C2=A0=C2=A0=C2=A0 SELECT INTO enddate end_date FROM arhive_table WHERE= contract =3D=20 >> NEW.contract AND service =3D NEW.service; >> =C2=A0=C2=A0=C2=A0 IF enddate IS NULL THEN >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- line below probably will do normal IN= SERT >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 RETURN NEW; >> =C2=A0=C2=A0=C2=A0 ELSIF enddate IS DISTINCT FROM NEW.end_date THEN >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NEW.sendto :=3D 0; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NEW.uts :=3D date_part('epoc= h', CURRENT_TIMESTAMP)::integer; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- But here need to do UPDAT= E not INSERT >=20 > So do an UPDATE and the RETURN NULL to cancel the INSERT. Untested=20 > example: >=20 > UPDATE arhive_table SET sendto =3D 0, uts =3D date_part('epoch', > CURRENT_TIMESTAMP)::integer WHERE contract =3D NEW.contract AND service > =3D NEW.service; >=20 > RETURN NULL; >=20 Yep, I think about this update and was my first approach but need to be=20 done only if end_date is different. If end_date is the same this mean contract still exists so only update=20 lastseen column, if the lastseen is not updated other process will read=20 not seen contracts and will generate report and send them to delete=20 services. If lastseen is changed and sendto is zero again, mean end_date=20 is changed and need to be send new end_date again to other systems. >=20 >> =C2=A0=C2=A0=C2=A0 END IF; >> =C2=A0 ELSIF TG_OP =3D 'UPDATE' THEN >> =C2=A0=C2=A0=C2=A0 IF OLD.end_date IS DISTINCT FROM NEW.end_date THEN >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NEW.sendto :=3D 0; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NEW.uts :=3D date_part('epoc= h', CURRENT_TIMESTAMP)::integer; >> =C2=A0=C2=A0=C2=A0 END IF; >> =C2=A0 END IF; >> =C2=A0 -- lastseen must always be updated with CURRENT_TIMESTAMP if=20 >> contract is seen >> =C2=A0 NEW.lastseen :=3D CURRENT_TIMESTAMP; >> =C2=A0 RETURN NEW; >> END >> $function$; >>=20 >>=20 >> DROP TRIGGER IF EXISTS last_changes ON arhive_table; >> CREATE TRIGGER last_changes >> =C2=A0 BEFORE INSERT OR UPDATE OF end_date ON arhive_table >> =C2=A0 FOR EACH ROW >> =C2=A0 WHEN (pg_trigger_depth() < 1) >> =C2=A0 EXECUTE FUNCTION log_last_chaged(); >>=20 >>=20 >> Regards, >> HS >>=20 >>=20