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 1f4T1S-0001ZF-Dk for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 15:11:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f4T1Q-0002Nh-Sv for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 15:11:24 +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 1f4T1Q-0002NY-Kq for pgsql-general@lists.postgresql.org; Fri, 06 Apr 2018 15:11:24 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f4T1I-0008Ko-WB for pgsql-general@lists.postgresql.org; Fri, 06 Apr 2018 15:11:23 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id AE4AD42550; Fri, 6 Apr 2018 17:11:14 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id X4AprtDsfRG0; Fri, 6 Apr 2018 17:11:13 +0200 (CEST) Message-ID: <1523027471.3170.10.camel@cybertec.at> Subject: Re: dealing with lock From: Laurenz Albe To: Thomas Poty , pgsql-general@lists.postgresql.org Date: Fri, 06 Apr 2018 17:11:11 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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