Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YTcQm-0005Rk-0B for pgsql-sql@arkaria.postgresql.org; Thu, 05 Mar 2015 20:31:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YTcQl-0007Si-FO for pgsql-sql@arkaria.postgresql.org; Thu, 05 Mar 2015 20:31:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YTcQk-0007SH-DD for pgsql-sql@postgresql.org; Thu, 05 Mar 2015 20:31:38 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YTcQh-0005Qu-0o for pgsql-sql@postgresql.org; Thu, 05 Mar 2015 20:31:36 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 99CD4206DF for ; Thu, 5 Mar 2015 15:31:32 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 05 Mar 2015 15:31:33 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= x-sasl-enc:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=y9fUVnjDbgEMQtLXqxnpKRfBZN8=; b=Du8WUCVIMsw7XIDcs8 Cicty0OgCAs7vYdpdBvlIRlmssZR4oLKz/Rg1YHfTcBwSJ6kRxtVYPNoBpUWrrYA 6A5DbpFISb3x2PPJb0DGhhEtEtGNQt9moHZiBM6ZlxlcBB7s569DFoJd2dPYX1WL YB0hffWQtj4c2w6fIE/lxue3w= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=y9fUVnjDbgEMQtLXqxnpKR fBZN8=; b=GPFigFsyvfDfYg46OXQbhA4whbo2Hg4LY87BsNxYCcE40nHXgh//03 IpFltl4SpxOtwZ/Oq77nuC36Q1D2Cpg0Wn4IjwZzyJdhuaM5W4KnvvV3dhUDdDAr MHW18bJ3Y/3bRKGCjhl9tcC+k6S9cbsh1c1p9uvvZfP+4b/rJw5mU= X-Sasl-enc: V2EbSfrsNlBXcx4LGJuoXpiir6tHO0GaR67FGPVHhs+j 1425587493 Received: from [192.168.1.4] (unknown [174.21.191.40]) by mail.messagingengine.com (Postfix) with ESMTPA id 538756801DC; Thu, 5 Mar 2015 15:31:33 -0500 (EST) Message-ID: <54F8BD24.6070101@aklaver.com> Date: Thu, 05 Mar 2015 12:31:32 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Andreas Joseph Krogh , pgsql-sql@postgresql.org Subject: Re: Schema for caching message-count in folders using triggers References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 03/05/2015 12:04 PM, Andreas Joseph Krogh wrote: > På torsdag 05. mars 2015 kl. 20:59:28, skrev Adrian Klaver > >: > > > > The problem with this is locking (waiting for another TX to > commit when > > updating the same folder) and deadlock issues when trying to > > simultaneously insert/delete/update messages in a folder. > > Does anyone have any better ideas for safely caching the > message-count > > in each folder without locking and deadlock issues? > > How accurate does this have to be? > > Not exactly following what is folder? > Is it a table that contains the messages? > > A top of the head idea would be to use sequences. Create a sequence for > each folder starting at current count and then use nextval, setval to > change the value: > > http://www.postgresql.org/docs/9.4/interactive/functions-sequence.html > > It is not transactional, so it would probably not be spot on, which is > why I asked about accuracy earlier. > > Yes, 'folder' is a table which contains 'message': > > create tablefolder( > idserial PRIMARY KEY, > namevarchar not null unique, > message_countinteger not null default0 > ); > > create table message( > idserial PRIMARY KEY, > folder_idINTEGER NOT NULL REFERENCESfolder(id), > messagevarchar not null > ); > > The count has to be exact, no estimate from EXPLAIN or such... Well there goes my idea. Seems the way to go is partitioning: http://www.postgresql.org/docs/9.4/static/ddl-partitioning.html Break the data into smaller units > -- > *Andreas Joseph Krogh* > CTO / Partner - Visena AS > Mobile: +47 909 56 963 > andreas@visena.com > www.visena.com > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql