pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Condor <condor@stz-bg.com>
To: Melvin Davidson <melvin6925@gmail.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Need help with trigger
Date: Sun, 24 Jan 2021 14:34:26 +0200
Message-ID: <e346b9bb5f73af879a0087f51a2477bc@stz-bg.com> (raw)
In-Reply-To: <CANu8FiyfSHd=9dewgCEDG0dUQUUnMQy2fuR6KFtFeOm8jVU8vA@mail.gmail.com>
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>
	<CANu8FiyfSHd=9dewgCEDG0dUQUUnMQy2fuR6KFtFeOm8jVU8vA@mail.gmail.com>

On 23-01-2021 23:29, Melvin Davidson wrote:
> Maybe this example will help.
> From https://www.postgresql.org/docs/current/sql-insert.html
> 
> INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil
> Distribution')
>     ON CONFLICT (did) DO UPDATE
>     SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'
>     WHERE d.zipcode <> '21201';
> 
> On Sat, Jan 23, 2021 at 3:47 PM Adrian Klaver
> <adrian.klaver@aklaver.com> wrote:
> 
>> 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:
>> 
>>> 
>>> Sorry,
>>> 
>>> I'm sorry, I don't understand something. You mean to do pure
>> INSERT ON
>>> CONFLICT DO or to modify the trigger ?
>> 
>> 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.
>> 
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com
> 
> --
> 
> Melvin Davidson
> Maj. Database & Exploration Specialist
> Universe Exploration Command – UXC
> Employment by invitation only!


Thanks for the ideas,

after sleeping with the thought, on the morning I decided to remove the 
trigger and do it with a simple function.

CREATE OR REPLACE FUNCTION public.log_last_chaged(contractid TEXT, 
service INTEGER, endd DATE)
  RETURNS INTEGER
  LANGUAGE plpgsql
AS $function$

DECLARE
enddate DATE;

BEGIN
   SELECT INTO enddate end_date FROM arhive_table WHERE contract = 
contractid AND servid = service AND command = 1;
   IF enddate IS NULL THEN
     INSERT INTO arhive_table (contract, serviceid, end_date) VALUES 
(contractid, service, endd);
   ELSIF enddate IS DISTINCT FROM endd THEN
     UPDATE arhive_table SET sendit = 0, end_date = endd, lastseen = 
CURRENT_TIMESTAMP WHERE contract = contractid AND serviceid = service 
AND command = 1;
   ELSE
     UPDATE arhive_table SET lastseen = CURRENT_TIMESTAMP WHERE contract 
= contractid AND serviceid = service AND command = 1;
   END IF;
   RETURN 1;
END;
$function$;

Thanks again for ideas.
HS





view thread (10+ messages)

Message-ID: <e346b9bb5f73af879a0087f51a2477bc@stz-bg.com>
Permalink:  ../e346b9bb5f73af879a0087f51a2477bc@stz-bg.com/
Also on:    postgresql.org/message-id/e346b9bb5f73af879a0087f51a2477bc@stz-bg.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: condor@stz-bg.com, melvin6925@gmail.com, adrian.klaver@aklaver.com
  Subject: Re: Need help with trigger
  In-Reply-To: <e346b9bb5f73af879a0087f51a2477bc@stz-bg.com>
  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> <CANu8FiyfSHd=9dewgCEDG0dUQUUnMQy2fuR6KFtFeOm8jVU8vA@mail.gmail.com> <e346b9bb5f73af879a0087f51a2477bc@stz-bg.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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