Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4ZR9-0003cb-QD for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 22:02:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f4ZR7-00033M-QV for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 22:02:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4ZR6-00033D-KK for pgsql-general@lists.postgresql.org; Fri, 06 Apr 2018 22:02:21 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4ZR0-0001Rk-Pw for pgsql-general@lists.postgresql.org; Fri, 06 Apr 2018 22:02:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 29D89211FC; Fri, 6 Apr 2018 18:02:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 06 Apr 2018 18:02:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=ymyVpL8UAxwD82yd/+ybkr4ve5+JB 61GTDGGYdKX0yM=; b=j4BGszud+3giciGgSL+n9SfhZqd/KuIz28QeFNKDJdYON x1jKXYFwyyQT0NCWxNdTbTGMa0Z0dhL3DieOdQCdDIXnrBOgGmu8dfQZnOW9Vg3O E1LAyjxlumBaGkgHTXOqlqqq1XTDJZFVPpoW9pLe/svJEdkJpiqqttNsZFRJHUwI jT0abMWd96Un4D9GqKE1ETXPYRRpTPmcCd679jgSjn9iIeZcLNJEpdi6XUT9shnL ilufOzjUcFNaIvH3Yn7mtlJF6QoddakAgfTVJGcRl27RsZ4bgNjTRmoILnkE4DPX Vgg0duy+dT2iieYa+VPgz9Vzt4Vrs+4AtnMsV0VqA== 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-sender:x-me-sender:x-sasl-enc; s=fm2; bh=ymyVpL 8UAxwD82yd/+ybkr4ve5+JB61GTDGGYdKX0yM=; b=btj1EEDGRuApz1mSiyUuA+ Vjp3hJm2/IxBNDz4sdySwkW7s8T/yj2bjg2LfR5j6X1Y8hg8fYVlWbgL0fNKoc9E 1q0nKfbXHM8E8HmFU1OQtAxLCGO6ZpEcpg1vAZOnG9p7KkCZeewk3TS18lGmrkvL 49Gxq2b3TK5ZTbVInNKOzeRBufFF1hz/7zJnJaH0n9madRdSC37XsZmRdlv/ZnPU +CoglIUKNfM2s1IoqHDhQIIqkilucfDsILLB/4CnWFVMxs076yoM54uR8fBL7Sld eyFGi2FQaIjjqd5NVGq+2q5WDhsjNyrSygHTWlj50WKF+pDTaB5vaQxYIjtXyQLQ == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id 8518CE43C8; Fri, 6 Apr 2018 18:02:12 -0400 (EDT) Subject: Re: dealing with lock To: Thomas Poty , Laurenz Albe Cc: pgsql-general@lists.postgresql.org References: <1523027471.3170.10.camel@cybertec.at> From: Adrian Klaver Message-ID: <53e9c170-f14a-e71e-b973-0325702cc6b5@aklaver.com> Date: Fri, 6 Apr 2018 15:02:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.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 04/06/2018 12:09 PM, Thomas Poty wrote: > Thank you Laurenz ! > > > We will certainly have to change our release management. > > Is there a way to identify the list of statements that have to rewrite > the table. https://www.postgresql.org/docs/10/static/sql-altertable.html Notes "Adding a column with a DEFAULT clause or changing the type of an existing column will require the entire table and its indexes to be rewritten. As an exception when changing the type of an existing column, if the USING clause does not change the column contents and the old type is either binary coercible to the new type or an unconstrained domain over the new type, a table rewrite is not needed; but any indexes on the affected columns must still be rebuilt. Adding or removing a system oid column also requires rewriting the entire table. Table and/or index rebuilds may take a significant amount of time for a large table; and will temporarily require as much as double the disk space." For the more general case of modifying a table and the locks it takes, search the above link for lock to see what locks are taken instead of the default of ACCESS EXCLUSIVE. For what the locks mean see: https://www.postgresql.org/docs/10/static/explicit-locking.html > > If I am right, at least these statements need to do this : > - create a unique index > - add a column with a default value > > Regards, > >  Thomas > > > 2018-04-06 17:11 GMT+02:00 Laurenz Albe >: > > On Fri, 2018-04-06 at 16:58 +0200, Thomas Poty wrote: > > Here is a bit of context : we are migrating from MySQL to PostgreSQL and we have about 1000 tables. > > Some tables are quite small but some others are very large. The service provided to our clients > > relies on a high avaiability with a minimum down time due to any legal deadlines. > > > > So, lets imagine : > > in Transaction 1 : I am querying Table A (select) > > in Transaction 2 : I am trying to alter Table A ( due to our product evolution) > > in Transaction 3 : I am want to query Table1 (select) > > > > in MySQL : Transaction 1 retrieve data in Table A. > > Transaction 2 : is trying to alter Table A but it is blocked by Transaction 1 > > Transaction 3 : Transaction 1 retrieves data in Table A ( Retreiving data is possible until Transaction 2 commit) > > > > In PostgreSQL, it is a bit different : Transaction 1 retrieve data in Table A. > > Transaction 2 : is trying to alter Table A but it is blocked by Transaction 1 > > Transaction 3 : Transaction 3 cannot retrieve data because  Transaction 2 did not terminate its transaction. > > > > So, with MySQL, the application is able to keep working with the table until the alter table completed. > > > > With PostgreSQL, the application will probably be blocked (until having the lock on this table). > > If I understand, if the alter table takes a long time (several hours) to execute, clients will be blocked during several hours. > > > > How do you deal with this problem? Maybe I missed something ? > > The solution is to avoid ALTER TABLE statements that have to rewrite > the table outside of maintenance windows. > > If your transactions are short, as they should be, it should not be > a big deal to add or drop a column, for example. > > Yours, > Laurenz Albe > -- > Cybertec | https://www.cybertec-postgresql.com > > > -- Adrian Klaver adrian.klaver@aklaver.com