Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixCBF-0001lD-8c for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 15:56:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ixCBE-0002BG-1M for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 15:56:32 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixCBB-0002B7-UW for pgsql-general@lists.postgresql.org; Thu, 30 Jan 2020 15:56:31 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ixCB7-0005Ld-0w for pgsql-general@postgresql.org; Thu, 30 Jan 2020 15:56:29 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id EBACB313; Thu, 30 Jan 2020 10:56:21 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Thu, 30 Jan 2020 10:56:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=T A8oSYUbxeBLkk776898KcJJgE9sSLD+kj2SqBSELt8=; b=PtyxI9riN7o6PWX16 QSxHlEO1jW5NvRTwp4dGI+FcBwdOeUSl/Zv1hoJYPJIoERbcwwV9yH2hahbt+T8c l4uQPxJ+H1iq/sY+i7c40I5ay3P+bKIU0IBO0JMtIOGx33SPZiTdLuEw1mbswT5R YzEGN0rmfWAFRVvKbHR+x0D5X/1OVj+YDffTo4ccSWO9cuDXEdVSrBd0wwAc5HDF lfbx3aM6RhMxpCyrQffihqiMm0AlA/8PW1KPSAJb8dp1aFSVm+CFPHDpVgEKmLpR JszQ8AcNauLRbeaYjmurtfmUgqUmkjHzyOivdxlIsJgHDIOFCOjLFv84hp3Zl+VV k/M3g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=TA8oSYUbxeBLkk776898KcJJgE9sSLD+kj2SqBSEL t8=; b=ewdSEPUIveXSC/xxii9jEZY35vXyCLCjwRWuZuWLArb4CZiBGBTjJi+j2 +jInjqlELbqdhlAKxf4TRiWmAgpnbxf6Sa7QXU8AKg+Zqkda1kqo7gML8CXK2QDf 7rVsMP6PYt5C/6Q3KEwGbJRqnGdeVU5hcTgFiFmXshaiDs9p0h2qSD+JB/f8IV+3 lzU+ctwSIWhd8eICnJxDEryBIBg814lWA4E0gvinILjl1NisNBEOSoaVs3B1kSl2 H4DEPtaFKDq1cogJv8GG8UzSUk5MM9Cx5Y8xwrtidRNMjMC9ERL8TF6NCUCaGLFv SL8LvQ8lkxHMHqwf6OktnkTXpCqTg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrfeekgdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehlvghophgrrhgurdhinhdruhgrpdhpohhsthhgrhgvshhqlhdr ohhrghenucfkphepjedurddvuddvrdduvdelrdduleefnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id AB9AD3280060; Thu, 30 Jan 2020 10:56:20 -0500 (EST) Subject: Re: Add column with default value in big table - splitting of updates can help? To: Durumdara , Postgres General References: From: Adrian Klaver Message-ID: Date: Thu, 30 Jan 2020 07:56:19 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 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 1/30/20 7:51 AM, Durumdara wrote: > Dear Members! > > I've read this article, but I need your experience in this theme. > > https://leopard.in.ua/2016/09/20/safe-and-unsafe-operations-postgresql#.XjL3fcqYXDc > > alter table tk >   add colum field1 default 'MUCH'; > > The table tk have 200 million rows. The autovacuum is no problem, only > the long update. > > But as I read the alter makes table lock, so this update locks the table > for long time. What version of Postgres are you using? I ask because: https://www.postgresql.org/docs/11/release-11.html "Many other useful performance improvements, including the ability to avoid a table rewrite for ALTER TABLE ... ADD COLUMN with a non-null column default" > > The article said we need to do this: > > 1. add column without default - fast. > 2. set default on column. > 3. update it where is null. > > What we can save with this method? > > As I suppose the table lock substituted with long update (row locks on > whole table). > > The article suggested to replace long update to shorter sequences > (10000-100000 records by cycle). > > We used to execute these SQL-s (alter, default, update) in one transaction. > So I can't make commit there. > > What is the difference between "full update" and "updates by 10000 > records" when I'm in a single transaction? > > Is it possible that this pseudo code makes less locks WITHOUT INNER COMMIT? > > Pseudo: > ---------------------- > begin >   while True loop >      update tk set field1 = ' MUCH' when field1 is NULL and id in >         (select id from tk where field1 is NULL limit 100000); >      if not FOUND then >         break; >   end while; > end >   ---------------------- > > Is there any advance when I split updates? I'm in one transaction. > > TR. START > ---- > 1. alter add col > 2. set default > 3. updates > --- > TR. COMMIT > > Or it isn't help me? > > Because the whole transaction locks the other users also, just like > "alter add colum wit hdefault statement"? > > Thank you for your and help! > > Best regards >    dd -- Adrian Klaver adrian.klaver@aklaver.com