Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gkcSs-0000pR-5C for pgsql-general@arkaria.postgresql.org; Fri, 18 Jan 2019 22:18:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gkcSq-0004t3-Rb for pgsql-general@arkaria.postgresql.org; Fri, 18 Jan 2019 22:18:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gkcSp-0004sq-4r for pgsql-general@lists.postgresql.org; Fri, 18 Jan 2019 22:18:12 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gkcSl-0000rw-B0 for pgsql-general@postgresql.org; Fri, 18 Jan 2019 22:18:09 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 700E71DDE; Fri, 18 Jan 2019 17:18:05 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 18 Jan 2019 17:18:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=F 6MYKb0loYd4udBu9PhCgpoEsXf+ynBPJaO0KrjV9og=; b=UEL/PzjLIbciO4ovm 00SU9sDQ+W1/G+2tVHCm8tJjglHVqZfoqjhfR41NdUSr8djCubEFqq/lkJ2xdLli Ku1SDxWZ+ZxFKKd0V5QjwXa8zb19bSsNlcACk5dmc7MFB7vFENjKk3deGbPlxMmR oqhITakRmNZNs5VAe47kU9euTaNh4xrOvwtwXr0aiLJIXxIHf48dV4+hN7p3oxWE 2EA4fK1WPtDqXOl4Bz46rzBfKxXYrpwFPdKEpBw3cWpQKeoATWMi29nWOAJNqt/6 dWDq0zi4THacFZ7ZM2uP0LjJxcsa/6CCtqs0v/ZZxiTOGG/mwRWoJdA1OeAMZMJQ gZHcQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=F6MYKb0loYd4udBu9PhCgpoEsXf+ynBPJaO0KrjV9 og=; b=X5QnJX5Y0QhvjYnC3kOf2e3+YfaHthonLX/LDQgDYaO6SJtgXI+2Nly4W eFO6zXmHwfXuJtvGxXvo87dY1K9WYPI74P8ELzWFlwyyn/soyOn90K9tKDV4f6vr Gl0oX2av9y5KahXTGxQ/rUYRZshzcdyWEq/2MLQa1+uXIMAy9pTzDfesPpshtS1L GqTNrmGaJLnp3yznuwtVqASWidyEw/t15CIzGAXqoMSlxySAiSJB4a7x3ZzIFdZt 8MzPo+s85V5F9Smo1ZZWL3DT7LxedxXD5INpeNxKyHUSysF5EyXXceqgmI7MXPXb BXecaHzh1q9FAMscKXzr8OfNMfPhg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrhedtgdduiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpe highhlrghsshdrnhgvthenucfkphepjedurddvuddvrddufeegrdefnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmne cuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id D9E43E4662; Fri, 18 Jan 2019 17:18:03 -0500 (EST) Subject: Re: Why adding BEFORE TRIGGER affect client CPU? To: George Woodring , Tom Lane Cc: pgsql-general References: <6e15d38d-e6f9-1836-15db-5b4e5e9f3a94@aklaver.com> <1130.1547838060@sss.pgh.pa.us> From: Adrian Klaver Message-ID: Date: Fri, 18 Jan 2019 14:18:02 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: 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/18/19 12:21 PM, George Woodring wrote: > Adrian Klaver > writes: > > What is the client server and what is it doing? > > Our client creates a temp table > CREATE TEMP TABLE myraw(LIKE raw INCLUDING DEFAULTS) > We load data with a copy > COPY myraw (transferid, pollgrpid, date, data, rrdtypeid, error ) FROM > STDIN WITH DELIMITER '|' > We do some checking on the data in the temp table > SELECT rawdate, raw_maint(rawdate) AS checked FROM (SELECT > date_trunc('minute', date) AS rawdate FROM myraw GROUP BY rawdate) AS foo; > Then we save the data into the permanent location > SELECT transferid, pollgrpid, process_lastpoll(ROW(myraw.*)::raw) AS > processed FROM myraw ORDER BY transferid > > Our change was in the process_lastpoll() function.  We went from >       IF cntr THEN >          oval := calc_last_cntr(ptype, pgid, ds, pdate, data); >       ELSE >          oval := data; >       END IF; > >       -- Insert data into table >       -- RAISE DEBUG 'Process: dsnum=%, polltype=%, cntr?=%, pdate=%, > data=%, oval=%', dsnum, ptype, cntr, pdate, data, oval; >       UPDATE lastpoll SET polltype=ptype, timeslot=ts, lasttime=pdate, > lastval=data, outval=oval, error=err WHERE pollgrpid=pgid AND dsnum=ds; >       IF found THEN >          RETURN true; >       END IF; >       BEGIN >          INSERT INTO lastpoll (timeslot, pollgrpid, dsnum, polltype, > lasttime, lastval, outval, error) VALUES (ts, pgid, ds, ptype, pdate, > data, oval, err); >          RETURN true; >       EXCEPTION WHEN unique_violation THEN >          -- Don't do anything >       END; > to this >       IF NOT cntr THEN >          oval := data; >       END IF; > >       -- Insert data into table >       -- RAISE DEBUG 'Process: dsnum=%, polltype=%, cntr?=%, pdate=%, > data=%, oval=%', dsnum, ptype, cntr, pdate, data, oval; >       UPDATE lastpoll SET polltype=ptype, timeslot=ts, lasttime=pdate, > lastval=data, outval=oval, error=err WHERE pollgrpid=pgid AND dsnum=ds; >       IF found THEN >          RETURN true; >       END IF; >       BEGIN >          INSERT INTO lastpoll (timeslot, pollgrpid, dsnum, polltype, > lasttime, lastval, outval, error) VALUES (ts, pgid, ds, ptype, pdate, > data, oval, err); >          RETURN true; >       EXCEPTION WHEN unique_violation THEN >          -- Don't do anything >       END; > > The calc_last_cntr() was replaced with the trigger. > from: > CREATE OR REPLACE FUNCTION calc_last_cntr(ptype text, pgid int4, ds > int4, pdate timestamp with time zone, data double precision) RETURNS > double precision AS $$ >    DECLARE >       mcurr RECORD; >       res double precision; >       dwindow int4; >    BEGIN >       SELECT lasttime, lastval INTO mcurr FROM lastpoll WHERE > pollgrpid=pgid AND dsnum=ds; >       IF mcurr IS NULL THEN >          -- RAISE DEBUG 'No previous data found for pgid=%', pgid; >          return NULL; >       END IF; > >       -- Calculate the counter rate >       -- Check for div by 0 >       dwindow := EXTRACT(EPOCH FROM (pdate - mcurr.lasttime)); >       IF dwindow = 0 THEN >          RAISE WARNING '(%) Time difference is zero (% - %)', > current_database(), pdate, mcurr.lasttime; >          return NULL; >       END IF; > >       res := (data - mcurr.lastval)/dwindow; >       IF res < 0 THEN >          -- RAISE DEBUG 'Counter loop for pgid=%', pgid; >          return data/dwindow; >       END IF; > >       return res; >    END; > $$ LANGUAGE plpgsql; > > to: > CREATE OR REPLACE FUNCTION cntr_lastpoll() RETURNS trigger AS $$ >    DECLARE >       dwindow int4; >    BEGIN >       -- Log new value into history if has a date >       IF NEW.outval IS NULL THEN >          -- Calculate counter value for this DS. >          dwindow := EXTRACT(EPOCH FROM (NEW.lasttime - OLD.lasttime)); >          IF dwindow = 0 THEN >             RAISE WARNING '(%) Time difference is zero (% - %)', > current_database(), NEW.lasttime, OLD.lasttime; >             return NEW; >          END IF; > >          NEW.outval := (NEW.lastval - OLD.lastval)/dwindow; >          IF NEW.outval < 0 THEN >             NEW.outval := NEW.lastval/dwindow; >          END IF; > >       END IF; >       RETURN NEW; >    END; > $$ LANGUAGE plpgsql; > > -- Create Trigger to calculate counter values if needed. > CREATE TRIGGER lastpoll_counter BEFORE UPDATE ON lastpoll >    FOR EACH ROW EXECUTE PROCEDURE cntr_lastpoll(); > > On Fri, Jan 18, 2019 at 2:01 PM Tom Lane > wrote: > >  what uses that value client-side, exactly? > Not claiming to understand all that is going on above. What I think I see: 1) New process_lastpoll() has: UPDATE lastpoll SET polltype=ptype, timeslot=ts, lasttime=pdate, lastval=data, outval=oval, error=err WHERE pollgrpid=pgid AND dsnum=ds; 2) calc_last_cntr() is now function for trigger: CREATE TRIGGER lastpoll_counter BEFORE UPDATE ON lastpoll FOR EACH ROW EXECUTE PROCEDURE cntr_lastpoll(); Seems to me you are double clutching on the outval value. You UPDATE it in 1) then recalculate it again in 2). As said earlier I do not fully understand what is going on, still seems to me process_lastpoll() could go away and its code moved into calc_last_cntr(). > > The outval number is later read looking for values outside of a > thresholds.  I did not think about it affecting selects later in the > code.  All in all everything worked fine, but since the client box is an > AWS EC2 instance, it started eating all of our CPU credits. > > image.png > > > iGLASS Networks > www.iglass.net > > > > -- Adrian Klaver adrian.klaver@aklaver.com