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 1l3ITx-0001U6-7x for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 12:57:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3ITu-0001u8-Ry for pgsql-general@arkaria.postgresql.org; Sat, 23 Jan 2021 12:57:34 +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 1l3ITu-0001u1-HQ for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 12:57:34 +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 1l3ITn-0005Ew-Gy for pgsql-general@lists.postgresql.org; Sat, 23 Jan 2021 12:57:33 +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 35D502E0120 for ; Sat, 23 Jan 2021 14:57:25 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stz-bg.com; s=mail; t=1611406645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NbB7WFi7e643QHhYGWXjD/Hu9AQ+Ur5TunxO0Pcb9Rg=; b=r5XNPQYkCWmv06LP/dQLQeHJiqdOnMYzbhGAfr3hq7b3wPC8l/2LhuhOhvnGtXNT0VE9AG g4sG9psxfxKJ0th9TPoes9R0cyCcyNvYEDnwX71Ro7Y0SL49op3VCnw534zrAVpBPOAm+P 9SxjBXxghV4lzsM8mQCxTOTcxqdyQA4= MIME-Version: 1.0 Date: Sat, 23 Jan 2021 14:57:24 +0200 From: Condor To: pgsql-general@lists.postgresql.org Subject: Need help with trigger User-Agent: Roundcube Webmail/1.4.10 Message-ID: <6feca05ea1521b0a4da87bfcfe78c39f@stz-bg.com> X-Sender: condor@stz-bg.com Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 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. Here is my table DROP TABLE IF EXISTS arhive_table; CREATE TABLE arhive_table ( uts integer default date_part('epoch', CURRENT_TIMESTAMP)::integer, contract text, service integer, end_date date, lastseen timestamp WITHOUT time zone default CURRENT_TIMESTAMP, sendto integer default 0, error text ); CREATE UNIQUE INDEX arhive_table_uniq ON arhive_table(contract, service); Date is: INSERT INTO arhive_table (contract, service, end_date) VALUES ('CNT1', 1, '2021-01-31'), ('CNT1', 2, '2021-01-31'); after 30 min data can be: INSERT INTO arhive_table (contract, service, end_date) VALUES ('CNT1', 1, '2021-02-28'), ('CNT1', 2, '2021-01-31'); 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 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