X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id 7140E11F6248 for ; Tue, 18 Apr 2006 11:31:09 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.25 X-Spam-Level: X-Spam-Status: No, score=-2.25 required=5 tests=[AWL=0.351, BAYES_00=-2.599, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id tHJWWbdjNmWd for ; Tue, 18 Apr 2006 11:31:04 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 6197811F60AB for ; Tue, 18 Apr 2006 11:31:01 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k3IEUw9m013038; Tue, 18 Apr 2006 10:30:58 -0400 (EDT) To: mario.splivalo@mobart.hr cc: Pgsql performance Subject: Re: SELECT FOR UPDATE performance is bad In-reply-to: <1145365720.18469.35.camel@localhost.localdomain> References: <1145365720.18469.35.camel@localhost.localdomain> Comments: In-reply-to Mario Splivalo message dated "Tue, 18 Apr 2006 15:08:39 +0200" Date: Tue, 18 Apr 2006 10:30:58 -0400 Message-ID: <13037.1145370658@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200604/370 X-Sequence-Number: 18454 Mario Splivalo writes: > For the purpose of the application I need to establish some form of > serialization, therefore I use FOR UPDATE. The query, inside the > function, is like this: > pulitzer2=# explain analyze select id FROM messages JOIN > ticketing_codes_played ON id = message_id WHERE service_id = 1102 AND > receiving_time BETWEEN '2006-03-01' AND '2006-06-30' FOR UPDATE; > Hash Join (cost=32131.04..34281.86 rows=627 width=16) (actual > time=742.806..1491.864 rows=58005 loops=1) ^^^^^ > Now, this query takes between 8 and 30 seconds, wich is a lot, since > during the day we have almost 20 requests per minute. Acquiring a row lock separately for each of 58000 rows is not going to be a cheap operation. Especially not if anyone else is locking any of the same rows and thereby blocking you. If there is concurrent locking, you're also running a big risk of deadlock because two processes might try to lock the same rows in different orders. Are you really intending to update all 58000 rows? If not, what is the serialization requirement exactly (ie, what are you trying to accomplish)? Seems like something about this app needs to be redesigned. regards, tom lane