pg.ddx.io pgsql-general@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Why adding BEFORE TRIGGER affect client CPU? 5+ messages / 3 participants [nested] [flat]
* Why adding BEFORE TRIGGER affect client CPU? @ 2019-01-18 18:23 George Woodring <george.woodring@iglass.net> 0 siblings, 1 reply; 5+ messages in thread From: George Woodring @ 2019-01-18 18:23 UTC (permalink / raw) To: pgsql-general We are running 9.6.8-10 on our five postgres clusters. We have ~25 database per cluster. We have two servers that update a table in each database. During the update process we were running a pgpsql function that calculated the difference in the new value with the previous. This function would do a SELECT to get the previous value from the table and do the calculation before the UPDATE. We also have an AFTER TRIGGER to save the row into a logging table. We recently changed the process to do a BEFORE TRIGGER to do the calculation between OLD and NEW instead of the separate function with the SELECT. After doing this, CPU on our two client servers went crazy. CPU on the database servers look unchanged. Rolling back this change fixed our client CPU issue. Anyone know of a reason why this change would affect the client? I would think that this would only affect the server side. I could not see any messages printed out anywhere in logs showing any type of error. Thanks, George Woodring iGLASS Networks www.iglass.net ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Why adding BEFORE TRIGGER affect client CPU? @ 2019-01-18 18:28 Adrian Klaver <adrian.klaver@aklaver.com> parent: George Woodring <george.woodring@iglass.net> 0 siblings, 1 reply; 5+ messages in thread From: Adrian Klaver @ 2019-01-18 18:28 UTC (permalink / raw) To: George Woodring <george.woodring@iglass.net>; pgsql-general On 1/18/19 10:23 AM, George Woodring wrote: > We are running 9.6.8-10 on our five postgres clusters. > We have ~25 database per cluster. > We have two servers that update a table in each database. > > During the update process we were running a pgpsql function that > calculated the difference in the new value with the previous. This > function would do a SELECT to get the previous value from the table and > do the calculation before the UPDATE. We also have an AFTER TRIGGER to > save the row into a logging table. > > We recently changed the process to do a BEFORE TRIGGER to do the > calculation between OLD and NEW instead of the separate function with > the SELECT. So what was the exact change? > > After doing this, CPU on our two client servers went crazy. CPU on the > database servers look unchanged. Rolling back this change fixed our > client CPU issue. What is the client server and what is it doing? > > Anyone know of a reason why this change would affect the client? I > would think that this would only affect the server side. I could not > see any messages printed out anywhere in logs showing any type of error. > > Thanks, > George Woodring > iGLASS Networks > www.iglass.net <http://www.iglass.net; -- Adrian Klaver adrian.klaver@aklaver.com ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Why adding BEFORE TRIGGER affect client CPU? @ 2019-01-18 19:01 Tom Lane <tgl@sss.pgh.pa.us> parent: Adrian Klaver <adrian.klaver@aklaver.com> 0 siblings, 1 reply; 5+ messages in thread From: Tom Lane @ 2019-01-18 19:01 UTC (permalink / raw) To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: George Woodring <george.woodring@iglass.net>; pgsql-general Adrian Klaver <adrian.klaver@aklaver.com> writes: > On 1/18/19 10:23 AM, George Woodring wrote: >> We recently changed the process to do a BEFORE TRIGGER to do the >> calculation between OLD and NEW instead of the separate function with >> the SELECT. > So what was the exact change? >> After doing this, CPU on our two client servers went crazy. CPU on the >> database servers look unchanged. Rolling back this change fixed our >> client CPU issue. > What is the client server and what is it doing? Indeed. There's no direct way that messing with a trigger would have caused extra CPU on the client side. I speculate that the trigger caused the data to look different in a way that your client app wasn't expecting, causing it to do something funny --- maybe loop trying to find a matching record, or something like that. Or maybe removing the step that did the calculation client-side had side effects you weren't expecting --- what uses that value client-side, exactly? regards, tom lane ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Why adding BEFORE TRIGGER affect client CPU? @ 2019-01-18 20:21 George Woodring <george.woodring@iglass.net> parent: Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 1 reply; 5+ messages in thread From: George Woodring @ 2019-01-18 20:21 UTC (permalink / raw) To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general Adrian Klaver <adrian.klaver@aklaver.com> 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 <tgl@sss.pgh.pa.us> wrote: > what uses that value client-side, exactly? > 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: image.png] iGLASS Networks www.iglass.net > > Attachments:^ permalink raw reply [nested|flat] 5+ messages in threadimage.png (image/png, 7.2K) download
view image
* Re: Why adding BEFORE TRIGGER affect client CPU? @ 2019-01-18 22:18 Adrian Klaver <adrian.klaver@aklaver.com> parent: George Woodring <george.woodring@iglass.net> 0 siblings, 0 replies; 5+ messages in thread From: Adrian Klaver @ 2019-01-18 22:18 UTC (permalink / raw) To: George Woodring <george.woodring@iglass.net>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general On 1/18/19 12:21 PM, George Woodring wrote: > Adrian Klaver <adrian.klaver@aklaver.com > <mailto:adrian.klaver@aklaver.com>> 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 <tgl@sss.pgh.pa.us > <mailto:tgl@sss.pgh.pa.us>> 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 <http://www.iglass.net; > > > > -- Adrian Klaver adrian.klaver@aklaver.com ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-01-18 22:18 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2019-01-18 18:23 Why adding BEFORE TRIGGER affect client CPU? George Woodring <george.woodring@iglass.net> 2019-01-18 18:28 ` Adrian Klaver <adrian.klaver@aklaver.com> 2019-01-18 19:01 ` Tom Lane <tgl@sss.pgh.pa.us> 2019-01-18 20:21 ` George Woodring <george.woodring@iglass.net> 2019-01-18 22:18 ` Adrian Klaver <adrian.klaver@aklaver.com>
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