Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 323D46325E5 for ; Thu, 5 Nov 2009 00:27:43 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 69316-02 for ; Thu, 5 Nov 2009 04:27:34 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f213.google.com (mail-bw0-f213.google.com [209.85.218.213]) by mail.postgresql.org (Postfix) with ESMTP id D99166331C2 for ; Thu, 5 Nov 2009 00:27:29 -0400 (AST) Received: by bwz5 with SMTP id 5so403596bwz.3 for ; Wed, 04 Nov 2009 20:27:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=77KQjf2+DmcjVJS3Wqd0N0Vn773HUBfkDJ0iJwMaS3c=; b=duEJqEngzmnVJAsxNkmqbtw3LOQ8MxWk4G5Masktnc36c8xkbqZOoh/aE5rP9QWi2D 05r8jMLsUXys25tY2I2vc5IkAK0W6F7/IwWUTUPziyXo/NH8wUg6hObjlNpFPMuOv2bA JsFqi1wNgev4M+gYTTBdZfxSLjHB5jjw3Ppcw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=p/MxDtGfQFFWtadywztqVy/EHPoKwv/gqmgvX+Ke3fttIja+8yTYbwDTxbFvp/N4Bu pC95X8jhlpoHdkoRaBdVeOmBVxHmQbymQogdXldiYr+H9ajBVQ+XNZ5BJd5oH5PbdZyf DQVyUu/MXAQnqWms3IF08aofTr2tr+IZ91P0s= MIME-Version: 1.0 Received: by 10.239.139.91 with SMTP id s27mr271489hbs.84.1257395248026; Wed, 04 Nov 2009 20:27:28 -0800 (PST) In-Reply-To: <82545852-CE9E-4B0D-ABAB-B628C888D086@metaweb.com> References: <22AC581D-50D1-4863-9FDE-35EDDB171269@metaweb.com> <82545852-CE9E-4B0D-ABAB-B628C888D086@metaweb.com> Date: Wed, 4 Nov 2009 20:27:27 -0800 Message-ID: Subject: Re: maintaining a reference to a fetched row From: Jeff Janes To: Brian Karlak Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/57 X-Sequence-Number: 36225 On Wed, Nov 4, 2009 at 9:41 AM, Brian Karlak wrote: > > I do a select for update in a stored proc: > > FOR queue_item IN > > =A0=A0SELECT * =A0FROM queue > =A0=A0 WHERE status IS NULL=A0AND id >=3D low_bound_id > =A0=A0 ORDER BY id LIMIT batch_size > =A0=A0 =A0 FOR UPDATE > > LOOP > > =A0=A0UPDATE queue_proc set status =3D 'proc' where id =3D queue_item.id = ; > > The daemons keep track of their last position in the queue with > low_bound_id. =A0Also, as you probably notice, I also fetch a batch of (1= 00) > items at a time. =A0In practice, it's pretty fast. =A0The job I'm running= now is > showing an average fetch time of 30ms per 100 actions, which ain't bad. > > However, the writing of results back to the row takes ~5ms, which is slow= er > than I'd like. 5 ms per each of the 100 actions? With one commit per action? > > It seems you have an select, and update, and another update. =A0Where i= n > > this process do you commit? =A0Are you using fsync=3Doff or > > synchronous_commit=3Doff? > > First commit occurs after the stored proc to select/update a batch of ite= ms > is complete. So one commit per 100 items? >=A0Second commit occurs on the writing of results back for each > particular action. So one commit per 1 item? If so, this completely explains the difference in speed, I think. > Two commits are required because the time it takes to > complete the intervening action can vary wildly: anywhere between 20ms an= d > 45min. Is there any way of knowing/approximating ahead of time how long it will ta= ke? The 45 min monsters must be exceedingly rare, or else the average could not be ~50ms. >> Why would the index scan take 1 ms two of the times it is done but 5ms >> the third time? =A0Isn't it the same index scan each time? =A0Or does th= e >> change in queue.status change the plan? > > The final update is a different query -- just a plain old update by ID: > > UPDATE queue_proc set status =3D 'proc' where id =3D %s ; That looks very much like the other UPDATE you showed. The difference it seems is that you commit after every one, rather than after every 100. Right? > This update by ID takes ~2.5ms, which means it's where the framework is > spending most of its overhead. You said the computation task can take anywhere from 20ms to 45min, so it seems that this update overhead is at most 1/8 of the irreducible time. That doesn't seem like it is enough to worry about, to me. Jeff