Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzdeF-0006Zl-VS for pgsql-docs@arkaria.postgresql.org; Thu, 18 Jul 2013 02:08:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UzdeF-0005AY-FJ for pgsql-docs@arkaria.postgresql.org; Thu, 18 Jul 2013 02:08:51 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzdeE-0005AS-ON for pgsql-docs@postgresql.org; Thu, 18 Jul 2013 02:08:50 +0000 Received: from moutng.kundenserver.de ([212.227.126.171]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzdeB-0007hX-NT for pgsql-docs@postgresql.org; Thu, 18 Jul 2013 02:08:50 +0000 Received: from [192.168.1.110] (mail.highperformancepostgresql.com [71.179.240.8]) by mrelayeu.kundenserver.de (node=mreu0) with ESMTP (Nemesis) id 0LuHL9-1TyDRx127r-011PBm; Thu, 18 Jul 2013 04:08:46 +0200 Message-ID: <51E74E2C.8020505@2ndQuadrant.com> Date: Wed, 17 Jul 2013 22:08:44 -0400 From: Greg Smith User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: pgsql-docs Subject: Improving index maintenance suggestions Content-Type: multipart/mixed; boundary="------------040702060708010208030002" X-Provags-ID: V02:K0:j0KcsK9rZrrBEEDyKl6xpypXD6IZQS0oMNRiQ/VaMYi Y5aWFffaWTAEpdDLTX/f8YBUE7DFT/uiGOWhXB+q49IAESzFab f1SpQQhqZhIlEvJxqNC4AqDHwXbswiPe9SvLAQustWvh+pQu/D QZU9Ful8ENKjpLGj1Mo76nv9Rq8GPQJ3qKRNAiFMByr3LJzRiq Au63GwhKW6YQJeGUkWn/MqysMyhwYyiepArrscO5RCGZd/oLCU za5+tuv8lWXd6snltTcabJHwcatMTNi3nCTRJ4qwv0MGWSxbOh 0JNv/kHe9I+8Tu9r9QnkKpxVmD7qtWsLcf/IDTYv3UjkQfi4jU dxNTTBG2gbn/0tawRiNc= X-Pg-Spam-Score: -0.0 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-docs Precedence: bulk Sender: pgsql-docs-owner@postgresql.org This is a multi-part message in MIME format. --------------040702060708010208030002 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit The attached documentation patch overhauls the "Routine Reindexing" documentation to introduce production index rebuilding techniques. REBUILD is useless for a lot of environments, replaced with CREATE INDEX CONCURRENTLY and various naming shuffles if you want the new index to have the same name. I tried to link to all of the commands needed to pull that off usefully. I also noticed that it's easy to get a wrong idea from the CREATE INDEX CONCURRENTLY documentation: that invalid indexes are only possible when building certain types of indexes, like unique ones. I mention deadlocks there to make it more obvious that any index built concurrently can fail. I have now seen several REBUILD simulation scripts written that assume simple concurrent index building will always work. Those can really crash and burn on a deadlock, leaving no index at all left behind in some bad sequences. (DROP the original, ALTER the new name to the old one, but it's invalid) I think all of this would make a good backport candidate going back to 9.1, when several of the ALTER TABLE methods were added for replacing indexes backing constraints. Before then this technique had so many limitations it was barely worth mentioning. I didn't want to push this approach so heavily in the manual at the time 9.1 was released, for fear there might have some unexpected real-world issues here. Indexes referred to by foreign keys have turned out to be the only thing I see regularly that are hard to rebuild in 9.1 and 9.2. The CREATE/ALTER shuffle works great for everything else. -- Greg Smith 2ndQuadrant US greg@2ndQuadrant.com Baltimore, MD PostgreSQL Training, Services, and 24x7 Support www.2ndQuadrant.com --------------040702060708010208030002 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="rebuild-steps-v1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rebuild-steps-v1.patch" diff --git a/doc/src/sgml/maintenance.sgml b/doc/src/sgml/maintenance.sgml new file mode 100644 index c34ca53..dc687f9 *** a/doc/src/sgml/maintenance.sgml --- b/doc/src/sgml/maintenance.sgml *************** analyze threshold = analyze base thresho *** 745,752 **** In some situations it is worthwhile to rebuild indexes periodically ! with the ! command. --- 745,753 ---- In some situations it is worthwhile to rebuild indexes periodically ! with the command or a series of individual ! rebuilding steps. ! *************** analyze threshold = analyze base thresho *** 772,777 **** --- 773,796 ---- (This consideration does not apply to non-B-tree indexes.) It might be worthwhile to reindex periodically just to improve access speed. + + + can be used safely and easily in all cases. + But since the command requires an exclusive table lock, it is + often preferable to execute an index rebuild with a sequence of + creation and replacement steps. Index types that support + with the CONCURRENTLY + option can instead be recreated that way. If that is successful and the + resulting index is valid, the original index can then be replaced by + the newly built one using combinations of + and . When an index is used to enforce + uniqueness or other constraints, may + also be necessary, to swap the existing constraint to one enforced by + the new index. Review this alternate, multiple step rebuild approach + very carefully before using it, as there are limitations on which + indexes can be reindexed this way, and errors you should detect and + handle correctly. + diff --git a/doc/src/sgml/ref/create_index.sgml b/doc/src/sgml/ref/create_index.sgml new file mode 100644 index 01faa3a..9a6a683 *** a/doc/src/sgml/ref/create_index.sgml --- b/doc/src/sgml/ref/create_index.sgml *************** CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] *** 405,411 **** ! If a problem arises while scanning the table, such as a uniqueness violation in a unique index, the CREATE INDEX command will fail but leave behind an invalid index. This index will be ignored for querying purposes because it might be incomplete; --- 405,411 ---- ! If a problem arises while scanning the table, such as a deadlock or a uniqueness violation in a unique index, the CREATE INDEX command will fail but leave behind an invalid index. This index will be ignored for querying purposes because it might be incomplete; --------------040702060708010208030002 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-docs --------------040702060708010208030002--