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 1glzDH-0003Ro-Vm for pgsql-general@arkaria.postgresql.org; Tue, 22 Jan 2019 16:47:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glzDG-0002Rc-0q for pgsql-general@arkaria.postgresql.org; Tue, 22 Jan 2019 16:47:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glzDF-0002RV-I4 for pgsql-general@lists.postgresql.org; Tue, 22 Jan 2019 16:47:45 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glzDB-0002FY-Ud for pgsql-general@lists.postgresql.org; Tue, 22 Jan 2019 16:47:44 +0000 Received: by mail-qt1-x844.google.com with SMTP id k12so28294769qtf.7 for ; Tue, 22 Jan 2019 08:47:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=fmJ/tF6FTkjAlEg524enP2MKt++EiJo0Svz5JrcFOtw=; b=Ik0rypyN3Q4tNKSbn49orszJAUU5yXrfOXl/T9oLX/ieONpwR4D0xXzyCEDeu2N/Ek ggxV51l0+LIBRdqmQx5VMpVKabgXZz56OLYYBSQv6+c81gPYq/2Al1rjn4LFRBHYZLHL VKPNoIZzGTb5bQD84+HDYPa49m9tRkNGs8Hebspe8U1YppYc9Lhj63lbUaVLGgbmU2mW jvSV3b5fnGGHVo7Hq41P55mCUeCYFkUBd/QVNvqI5FFiOvACMqoiN9hYafR2PViDhx9l r5Qa5cyc+HIDS6UKkePlbyayuUvxUhSVvLMJ6W7Ae0qEIcVSphdVYchMwy5v+LjXpkuD hb+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=fmJ/tF6FTkjAlEg524enP2MKt++EiJo0Svz5JrcFOtw=; b=CgBWptazbdBilgSFoECnSwRgVEUonIV5yZBGbNwQP5F7RMIgRnE4dl27o4ysRV/rOM pmN7Pkczp+LNnJeHqKIuHztn4sFHAr67atPTHkm3N2ax8DAMI3O+k5t+FYMGv1S0evRN fG7qNMqj0O0g8UnA+L5fHSK3LE6YACM4iWJPgrWkMJWb7LBC/oqUuDxJjCvh0P9WKMlh YwFAB1eML+HWeWDqNAZ1+hyePFMOmAMxXh/pLkVbH5tk7XkEsB3Ci+BvxGViPcsbgd9Q NGdbSZGBJz45smeZh48WjEcL9eQqX9bb4s/KcOxljUzhg7mVU9IS55GsQaIwwaD7NlaE 3CEw== X-Gm-Message-State: AJcUukdLcRF26uaWmgyQ/N+KXqcjuWxjRrrFoXwlnI5APr1gVHN9dv6q bHdImyONSwK0q+4X1CaA8HnpgDvS1xpAbyPih9Cqy6B96Rw1Vl2lIAkRYd9vPCMQrpu1sJpkMF8 HggnTy63kepnZ+iExpNyF/k7RXXO1kI6k2zvknvzq9ocj0C1djGgSKqlhdRq3DqgFVcXOxdGU6E YsE0aatYP0wSDwuw== X-Google-Smtp-Source: ALg8bN5NZbLBochbvOCVewNJleEqoqlQUlBdWjap17lCoAHb5NssuLd4T9VUNwAMjRfk1cL2pTJdDg== X-Received: by 2002:ac8:2a81:: with SMTP id b1mr32306138qta.110.1548175660489; Tue, 22 Jan 2019 08:47:40 -0800 (PST) Received: from alvin.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id a2sm32339570qkk.4.2019.01.22.08.47.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 22 Jan 2019 08:47:39 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id EBF5EA74; Tue, 22 Jan 2019 13:47:37 -0300 (-03) Date: Tue, 22 Jan 2019 13:47:37 -0300 From: Alvaro Herrera To: Denisa Cirstescu Cc: "pgsql-general@lists.postgresql.org" Subject: Re: Decrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL Message-ID: <201901221647.kce4ahwfygur@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20180716-1013-2c1e88 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Jan-22, Denisa Cirstescu wrote: > I am trying to add a new column to a really big table and to define an INDEX and a FOREIGN KEY on that new column using the following instructions: > > ALTER TABLE Employee ADD COLUMN DepartmentId INTEGER; > CREATE INDEX IDX_Employee_DepartmentId ON Employee(DepartmentId); > ALTER TABLE Employee ADD CONSTRAINT FK_Employee_Department FOREIGN KEY(DepartmentId) REFERENCES Department(DepartmentId); > > The table is huge and it takes a lot of time to add the INDEX and the FOREIGN KEY although all values are NULL. > Considering that the new DepartmentId column is NULL for all rows at this point, is there a way to make the INDEX and FOREIGN KEY creation run faster? The typical advice is to run CREATE INDEX CONCURRENTLY instead of plain CREATE INDEX. Also, use "ADD CONSTRAINT ... NOT VALID;" instead, and later do another ALTER TABLE ... VALIDATE CONSTRAINT. That decreases the impact considerably in production scenarios. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services