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 1l3Lot-0003Jr-L0 for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 16:31:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3Lor-0003oj-Tq for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 16:31:25 +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 1l3Loq-0003oU-E3 for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 16:31:25 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3Lom-00043s-O7 for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 16:31:24 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 025F610BB; Sat, 23 Jan 2021 11:31:16 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 23 Jan 2021 11:31:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=x CN5+xXWLuybi7QXQDK70684J6d2AEk7xKCognBDYAQ=; b=nhoZB6l8uSF8Rr+is v2x53P693DdK61c73xY5VHBA9qPeFpl+GFyOkHKn8hYGYzT/69cVJXIV+SJDbBMK TRci+hn2qjzo3mndWh2Ys/sl8rmhovD5Rk6PlERree/4xAyIsJzrbLkmFze8cDGa py0HgsLVyKgs8CU38J8H1pZMGDODMxklup4vHYWQfILExCMDreB5aDP2zZTmzbAW Az2uUKunTCcZtUkQHuRksfQHCrJxiY5kDAcVzZRXw7vm8rWY2kJ3Fg7YG5gKkZxx TOo8mGGfgx1qX2vhx/dCAL7DIqx2mNhF7BxYI/5llDKwWW+LK6DTZAW8EbPM94Qw uIROA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=xCN5+xXWLuybi7QXQDK70684J6d2AEk7xKCognBDY AQ=; b=hYIKU9mbvQK3kyk0zDrPQglc8se4LxfgYj+Ad8E3k78Z8fzyN7ar5cxel XiJS3yV4DwUFYkCAbSiCa++hRdHzXia6iRAkzmmeOdZCX+DbpFIpWizn4pkcBelz vB/a3JKEfZTD3Uu/dOQm+McY7vp2jc51qt8Jj4R5CTD4dM8Iffynai7R/esx9GhP nGWOnSypf9n961zZHqS1w1NNj+7SO80OMUrIn5SLmeGV5+YimsRa3YiWk1sRvlBd lcR8a3f0iSZSYiIvT8vR0+FGmiy7XJNoO2Ln0HtsKzWil6b/cfy04f8jp2J48Kk4 THXMnr1tD/FdWQ6yRBLowF2/7dpAw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekgdelfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeevveefveefue egffekkefhgeeiueffgfetueejvdfghfeujeejhfekjeelgedtgeenucfkphepjeehrddu jedvrddugedrudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.4] (75-172-14-17.tukw.qwest.net [75.172.14.17]) by mail.messagingengine.com (Postfix) with ESMTPA id 0F88224005A; Sat, 23 Jan 2021 11:31:15 -0500 (EST) Subject: Re: Need help with trigger To: Condor , pgsql-general@lists.postgresql.org References: <6feca05ea1521b0a4da87bfcfe78c39f@stz-bg.com> From: Adrian Klaver Message-ID: <62244d0f-03d7-6bed-cb7b-722ad32400ad@aklaver.com> Date: Sat, 23 Jan 2021 08:31:15 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <6feca05ea1521b0a4da87bfcfe78c39f@stz-bg.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/23/21 4:57 AM, Condor wrote: > > > Hello ppl, > > I need help with trigger how to replace insert command with update. > External program read data from one table and in every 30 min I need to > refresh data in arhive table. What is the table being read from and is it in the same database? See more comments inline below. > What I want is: if data already inserted and end_date is not changed, > only to update lastseen column. If data not exists to insert data and if > data exists and end_date is changed > to update end_date, lastseen and sendto columns. Well, update probably > will never happened, this functionality is left for frontend but its can > be cut off. > > > and my trigger is: > > CREATE OR REPLACE FUNCTION public.log_last_chaged() >  RETURNS trigger >  LANGUAGE plpgsql > AS $function$ > > enddate DATE; > > BEGIN >   IF TG_OP = 'INSERT' THEN >     SELECT INTO enddate end_date FROM arhive_table WHERE contract = > NEW.contract AND service = NEW.service; >     IF enddate IS NULL THEN >       -- line below probably will do normal INSERT >       RETURN NEW; >     ELSIF enddate IS DISTINCT FROM NEW.end_date THEN >         NEW.sendto := 0; >         NEW.uts := date_part('epoch', CURRENT_TIMESTAMP)::integer; >         -- But here need to do UPDATE not INSERT So do an UPDATE and the RETURN NULL to cancel the INSERT. Untested example: UPDATE arhive_table SET sendto = 0, uts = date_part('epoch', CURRENT_TIMESTAMP)::integer WHERE contract = NEW.contract AND service = NEW.service; RETURN NULL; >     END IF; >   ELSIF TG_OP = 'UPDATE' THEN >     IF OLD.end_date IS DISTINCT FROM NEW.end_date THEN >         NEW.sendto := 0; >         NEW.uts := date_part('epoch', CURRENT_TIMESTAMP)::integer; >     END IF; >   END IF; >   -- lastseen must always be updated with CURRENT_TIMESTAMP if contract > is seen >   NEW.lastseen := CURRENT_TIMESTAMP; >   RETURN NEW; > END > $function$; > > > DROP TRIGGER IF EXISTS last_changes ON arhive_table; > CREATE TRIGGER last_changes >   BEFORE INSERT OR UPDATE OF end_date ON arhive_table >   FOR EACH ROW >   WHEN (pg_trigger_depth() < 1) >   EXECUTE FUNCTION log_last_chaged(); > > > Regards, > HS > > -- Adrian Klaver adrian.klaver@aklaver.com