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 1l3ebG-0004bS-GL for pgsql-general@arkaria.postgresql.org; Sun, 24 Jan 2021 12:34:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3ebF-0001zV-Ey for pgsql-general@arkaria.postgresql.org; Sun, 24 Jan 2021 12:34:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3ebF-0001zO-3M for pgsql-general@lists.postgresql.org; Sun, 24 Jan 2021 12:34:37 +0000 Received: from stz-bg.com ([2a02:6800:0:99::2] helo=mail.stzbg.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3ebA-0005WF-SK for pgsql-general@lists.postgresql.org; Sun, 24 Jan 2021 12:34:36 +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 6B9CC2E1295; Sun, 24 Jan 2021 14:34:29 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stz-bg.com; s=mail; t=1611491669; 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=iaPWphbb7p2Okq6tYY1+/LsO9Ldvdoc6FiQtEucOzME=; b=XgRyas9FA961P/dYInVoNSGFBc+8g10uaCR69WQ0xDYuz+hPvzBrDGa4o1kKTa6+7sgJ5g AElb1MlTl7/5FqpqjPsjOlqXIuZNJR+6BbS9n+lJhFgKovn+pUv0KwSsIjk7OSRCYfp1yR 4I+PdoP4Zu9NyxmgWtJv2gBxUNHCLB4= MIME-Version: 1.0 Date: Sun, 24 Jan 2021 14:34:26 +0200 From: Condor To: Melvin Davidson Cc: Adrian Klaver , "pgsql-generallists.postgresql.org" Subject: Re: Need help with trigger In-Reply-To: References: <6feca05ea1521b0a4da87bfcfe78c39f@stz-bg.com> <62244d0f-03d7-6bed-cb7b-722ad32400ad@aklaver.com> <84864856b8ac6e5f35f68ace87c44556@stz-bg.com> <3294def5-09f6-c384-d1fe-f5faf4bcf797@aklaver.com> <533ed7d44063dca8b45626816aa51cad@stz-bg.com> <95e0e4d4-fb07-619f-fd51-036f8d9811ec@aklaver.com> User-Agent: Roundcube Webmail/1.4.10 Message-ID: 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 23:29, Melvin Davidson wrote: > Maybe this example will help. > From https://www.postgresql.org/docs/current/sql-insert.html >=20 > INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil > Distribution') > ON CONFLICT (did) DO UPDATE > SET dname =3D EXCLUDED.dname || ' (formerly ' || d.dname || ')' > WHERE d.zipcode <> '21201'; >=20 > On Sat, Jan 23, 2021 at 3:47 PM Adrian Klaver > wrote: >=20 >> On 1/23/21 12:14 PM, Condor wrote: >>> On 23-01-2021 20:49, Adrian Klaver wrote: >>>> On 1/23/21 10:20 AM, Condor wrote: >>=20 >>>=20 >>> Sorry, >>>=20 >>> I'm sorry, I don't understand something. You mean to do pure >> INSERT ON >>> CONFLICT DO or to modify the trigger ? >>=20 >> No I meant that in the external program you use to fetch the data >> from >> the other table and reorganize the fields. Do your test there and >> do >> either the INSERT or UPDATE. >>=20 >> -- >> Adrian Klaver >> adrian.klaver@aklaver.com >=20 > -- >=20 > Melvin Davidson > Maj. Database & Exploration Specialist > Universe Exploration Command =E2=80=93 UXC > Employment by invitation only! Thanks for the ideas, after sleeping with the thought, on the morning I decided to remove the=20 trigger and do it with a simple function. CREATE OR REPLACE FUNCTION public.log_last_chaged(contractid TEXT,=20 service INTEGER, endd DATE) RETURNS INTEGER LANGUAGE plpgsql AS $function$ DECLARE enddate DATE; BEGIN SELECT INTO enddate end_date FROM arhive_table WHERE contract =3D=20 contractid AND servid =3D service AND command =3D 1; IF enddate IS NULL THEN INSERT INTO arhive_table (contract, serviceid, end_date) VALUES=20 (contractid, service, endd); ELSIF enddate IS DISTINCT FROM endd THEN UPDATE arhive_table SET sendit =3D 0, end_date =3D endd, lastseen =3D= =20 CURRENT_TIMESTAMP WHERE contract =3D contractid AND serviceid =3D service= =20 AND command =3D 1; ELSE UPDATE arhive_table SET lastseen =3D CURRENT_TIMESTAMP WHERE contract= =20 =3D contractid AND serviceid =3D service AND command =3D 1; END IF; RETURN 1; END; $function$; Thanks again for ideas. HS