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 1iyHwF-0002No-ML for pgsql-hackers@arkaria.postgresql.org; Sun, 02 Feb 2020 16:17:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iyHwC-0004JC-7Q for pgsql-hackers@arkaria.postgresql.org; Sun, 02 Feb 2020 16:17:32 +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 1iyHwB-0004J5-Ie for pgsql-hackers@lists.postgresql.org; Sun, 02 Feb 2020 16:17:31 +0000 Received: from mail-yw1-xc2e.google.com ([2607:f8b0:4864:20::c2e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iyHw3-0002Xx-Rk for pgsql-hackers@postgresql.org; Sun, 02 Feb 2020 16:17:29 +0000 Received: by mail-yw1-xc2e.google.com with SMTP id f204so9862144ywc.10 for ; Sun, 02 Feb 2020 08:17:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=GgnTU9AaVwtLp1zLfU1cq6cn8OTWgBlGsxjoTHZBG7Q=; b=V4gRVcd5cn5sFviX+AMpSXMrj8j6SKWddn1EamrNLKMPJbzYT9d5uqx1xAdku+Razm MhhRnhPRu1I0OYC8Dd5k15O2l9QoJk6+LtpKgQFwsJZoEmZEgMaBIwvaLrTGgZl5y6eZ dWaZXuovDvdE2zAadOha3bh+hXhvH329FKoLkPwYN2TQwDKuAeN7QkmIHHqcALWXSra4 kIjVhPH+Ti6Nt8ohdL4uyYMeuAZQWhmAOa7xDE6Lfpb2tkRrvKAcsiRWcbYc0uamJpFR SuPrS4CwTv6KrVcb65jBYOcq858QYzW5Mn60yBkVcMWWHulYFXHau7pzd3wWqxU1cDgy 72hw== 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:subject:message-id:mime-version :content-disposition:user-agent; bh=GgnTU9AaVwtLp1zLfU1cq6cn8OTWgBlGsxjoTHZBG7Q=; b=ql2Pcli+4EWhlVDTab4usHjqi3J6vr9CAryfcvo2/hAHb1LAAJhYt0hhyp8tBVYJ31 LtCyHH8h98zTa3pFA/Ac7a7YKAziOG1fjZJBsNA9J3QoK59tsCGlJceuFurj6it0wSDE jBCu6S2tSC9eX3Wr3ad9MglciSXNDTxgub/3xN5s4Mom/nBI240fz6EaZgGC6UPp2biT xeF0/JxeK6hh2tEX98YpqFew7hYhIrl0CQtJght45wfze4mc4K2rrHEGGmlNUf3ip1tl wyr59IcJQRC+IKDIVt0XdB5U+GYEGEkh2Ha44n5JWPsno2VyDWTiGzfetNpUZqWWD+TL 8jLg== X-Gm-Message-State: APjAAAVddELT6bQbDwtRsVrS6Ny6uOfVPr3fDTrMP3x1Wxs/KdOpwMDb XAtNgwNhuYZMduZmBVwtQfBGHp0cfQA= X-Google-Smtp-Source: APXvYqwgM1Vg/290p5XVvS4FKeO5mkP9Quk2ECnYfrxdS4F1jifMGsmONI2Ct5uS14oDBUysQFkaSQ== X-Received: by 2002:a25:ab07:: with SMTP id u7mr16396560ybi.308.1580660242381; Sun, 02 Feb 2020 08:17:22 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id d66sm7069133ywc.16.2020.02.02.08.17.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 Feb 2020 08:17:21 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id E86F1800925; Sun, 2 Feb 2020 10:17:18 -0600 (CST) Date: Sun, 2 Feb 2020 10:17:18 -0600 From: Justin Pryzby To: pgsql-hackers@postgresql.org Subject: ALTER tbl rewrite loses CLUSTER ON index Message-ID: <20200202161718.GI13621@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2+IDmuKgu0wSQZlt" Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --2+IDmuKgu0wSQZlt Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Other options are preserved by ALTER (and CLUSTER ON is and most obviously should be preserved by CLUSTER's rewrite), so I think (SET) CLUSTER should be preserved by ALTER, too. As far as I can see, this should be the responsibility of something in the vicinity of ATPostAlterTypeParse/RememberIndexForRebuilding. Attach patch sketches a fix. ts=# SET client_min_messages=debug; DROP TABLE t; CREATE TABLE t(i int); CREATE INDEX ON t(i)WITH(fillfactor=11, vacuum_cleanup_index_scale_factor=12); CLUSTER t USING t_i_key; ALTER TABLE t ALTER i TYPE bigint; \d t SET DEBUG: drop auto-cascades to type t DEBUG: drop auto-cascades to type t[] DEBUG: drop auto-cascades to index t_i_idx DROP TABLE CREATE TABLE DEBUG: building index "t_i_idx" on table "t" serially CREATE INDEX ERROR: index "t_i_key" for table "t" does not exist DEBUG: rewriting table "t" DEBUG: building index "t_i_idx" on table "t" serially DEBUG: drop auto-cascades to type pg_temp_3091172777 DEBUG: drop auto-cascades to type pg_temp_3091172777[] ALTER TABLE Table "public.t" Column | Type | Collation | Nullable | Default --------+--------+-----------+----------+--------- i | bigint | | | Indexes: "t_i_idx" btree (i) WITH (fillfactor='11', vacuum_cleanup_index_scale_factor='12') --2+IDmuKgu0wSQZlt Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-preserve-CLUSTER-ON-during-ALTER-TABLE.patch" From f235a691722a464059358cd6b1d744f75d7bf92f Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Sun, 2 Feb 2020 09:49:57 -0600 Subject: [PATCH v1] preserve CLUSTER ON during ALTER TABLE --- src/backend/commands/tablecmds.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index f599393..c4e6cbd 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -11616,6 +11616,7 @@ RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab) } else { + Relation indrel; /* OK, capture the index's existing definition string */ char *defstring = pg_get_indexdef_string(indoid); @@ -11623,6 +11624,18 @@ RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab) indoid); tab->changedIndexDefs = lappend(tab->changedIndexDefs, defstring); + /* Preserve CLUSTER ON if set */ + indrel = index_open(indoid, AccessShareLock); + if (indrel->rd_index->indisclustered) { + char buf[3*NAMEDATALEN + 24]; + sprintf(buf, "ALTER TABLE %s CLUSTER ON %s", + get_rel_name(tab->relid), get_rel_name(indoid)); // XXX: schema + tab->changedIndexOids = lappend_oid(tab->changedIndexOids, + indoid); + tab->changedIndexDefs = lappend(tab->changedIndexDefs, + pstrdup(buf)); + } + index_close(indrel, NoLock); } } } @@ -11901,6 +11914,11 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd, * the new table definition. */ } + else if (cmd->subtype == AT_ClusterOn) + { + tab->subcmds[AT_PASS_OLD_INDEX] = + lappend(tab->subcmds[AT_PASS_OLD_INDEX], cmd); + } else elog(ERROR, "unexpected statement subtype: %d", (int) cmd->subtype); -- 2.7.4 --2+IDmuKgu0wSQZlt--