Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDcww-0002SP-Fb for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Jul 2022 02:27:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oDcwv-0000Ti-E2 for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Jul 2022 02:27:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDcwu-0000T7-Vz for pgsql-hackers@lists.postgresql.org; Tue, 19 Jul 2022 02:27:01 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oDcwr-0003cG-N4 for pgsql-hackers@postgresql.org; Tue, 19 Jul 2022 02:27:00 +0000 Received: by mail-qk1-x729.google.com with SMTP id c24so4727800qkm.4 for ; Mon, 18 Jul 2022 19:26:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=mUdSooOxOmjSnTOD7OuAKXenSJrPj/TKYIcf69DmxMo=; b=c+p59/Uaj8AhrAPlYUyjDt90+7cuT1JVuYL35ctS/pSSO1+IDwoeV/b9NnmdK6MN7S 0x/xxZDzJ7l9G/Lfo4WHzUKTo/sBoEhcP5QQVuo2bilqc4raXyCu5lGh5wAB1wngmVMm nxQlUyBnhFB2gDfc3b+BEezIjphe9iF/FndWTLwWcQdXhoUTgDrTEojuyxl5YO/5rjlH YG/zMlJly/g0iJBbg4bh+wE5L1WFovtGlp0JLadqbDsb0DQJynEZls5M6SjcuUWHfP0h pViyh5VMlcLSCNs4WqvWaMM4dmqjsvrMkE+DnBRKCWnk+qd7C7BnLoszGRe/+zUXZPo5 0dNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=mUdSooOxOmjSnTOD7OuAKXenSJrPj/TKYIcf69DmxMo=; b=4h0zkUZ3EkgxOpyBcnWEkQWATuWm9nfKm7qS42En8XaqVWQb9ZbBUCAoaLFrqJngjR Apfin5WHJBPAgLDTFDxe9LTSsYzJ1WEu0LUBSD9o1HDUAmlT8Pw63It7aazjyG8eV91p 8mOsfi5vXrmY2rE5iF/LhpcfqyQ8yXsf5gg0lzrsjOo7vXi099EY6CKIkARG9a77JUJs 1ah8kmHVD08/gFM6/xgKHa4L61sDwGZ3xdhEbpijd/lnML7WNvR31jB9xwprEXnJdtzz Gv/w7GeelSo1dfkoQSeACRGWBzqgflLYUyoJRG3/CEZ79vVYDLkmLY7MuFElmVHUENmE 8C0w== X-Gm-Message-State: AJIora+q3ML0+2oINTvh1NWG/c/zY4LIxN3WpCw7ge5EYKFIL1f7x61T k5ROgA7YkuIhFjrYp46g3VpY1w== X-Google-Smtp-Source: AGRyM1uYXsBlnof0TigadTEPiFWlBNO1eRnE5HOb5mnO2yXjhKLiboPSRt2TviaxQnvfPJxOV/SVkA== X-Received: by 2002:a05:620a:2f8:b0:6b5:e8a2:cf3d with SMTP id a24-20020a05620a02f800b006b5e8a2cf3dmr4878615qko.447.1658197615408; Mon, 18 Jul 2022 19:26:55 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id g19-20020ac870d3000000b0031edab8ae91sm7527043qtp.49.2022.07.18.19.26.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Jul 2022 19:26:54 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 33A41800DDD; Mon, 18 Jul 2022 21:26:53 -0500 (CDT) Date: Mon, 18 Jul 2022 21:26:53 -0500 From: Justin Pryzby To: Michael Paquier Cc: Simon Riggs , Tom Lane , Alvaro Herrera , Bernd Helmle , Ashutosh Bapat , pgsql-hackers@postgresql.org, Ashutosh Bapat Subject: Re: Allowing REINDEX to have an optional name Message-ID: <20220719022652.GE12702@telsasoft.com> References: <1386216.1656884491@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sorry, I meant to send this earlier.. On Sun, Jul 17, 2022 at 03:19:47PM +0900, Michael Paquier wrote: > The second thing is test coverage. Using a REINDEX DATABASE/SYSTEM > +my $catalog_toast_index = $node->safe_psql('postgres', > + "SELECT indexrelid::regclass FROM pg_index WHERE indrelid = '$toast_table'::regclass;" > +); > + > +# Set of SQL queries to cross-check the state of relfilenodes across > +# REINDEX operations. A set of relfilenodes is saved from the catalogs > +# and then compared with pg_class. > +$node->safe_psql('postgres', > + 'CREATE TABLE toast_relfilenodes (relname regclass, relfilenode oid);'); It looks like you named the table "toast_relfilenodes", but then also store to it data for non-toast tables. It's also a bit weird to call the column "relname" but use it to store the ::regclass. You later need to cast the column to text, so you may as well store it as text, either relname or oid::regclass. It seems like cluster.sql does this more succinctly. -- Check that clustering sets new relfilenodes: CREATE TEMP TABLE old_cluster_info AS SELECT relname, level, relfilenode, relkind FROM pg_partition_tree('clstrpart'::regclass) AS tree JOIN pg_class c ON c.oid=tree.relid ; CLUSTER clstrpart USING clstrpart_idx; CREATE TEMP TABLE new_cluster_info AS SELECT relname, level, relfilenode, relkind FROM pg_partition_tree('clstrpart'::regclass) AS tree JOIN pg_class c ON c.oid=tree.relid ; SELECT relname, old.level, old.relkind, old.relfilenode = new.relfilenode FROM old_cluster_info AS old JOIN new_cluster_info AS new USING (relname) ORDER BY relname COLLATE "C"; > +# Save the relfilenode of a set of toast indexes, one from the catalog > +# pg_constraint and one from the test table. This data is used for checks > +# after some of the REINDEX operations done below, checking if they are > +# changed. > +my $fetch_toast_relfilenodes = qq{SELECT c.oid::regclass, c.relfilenode > + FROM pg_class a > + JOIN pg_class b ON (a.oid = b.reltoastrelid) > + JOIN pg_index i on (a.oid = i.indrelid) > + JOIN pg_class c on (i.indexrelid = c.oid) > + WHERE b.oid IN ('pg_constraint'::regclass, 'test1'::regclass)}; > +# Same for relfilenodes of normal indexes. This saves the relfilenode > +# from a catalog of pg_constraint, and the one from the test table. > +my $fetch_index_relfilenodes = qq{SELECT oid, relfilenode > + FROM pg_class > + WHERE relname IN ('pg_constraint_oid_index', 'test1x')}; > +my $save_relfilenodes = > + "INSERT INTO toast_relfilenodes $fetch_toast_relfilenodes;" > + . "INSERT INTO toast_relfilenodes $fetch_index_relfilenodes;"; > + > +# Query to compare a set of relfilenodes saved with the contents of pg_class. > +# Note that this does not join using OIDs, as CONCURRENTLY would change them > +# when reindexing. A filter is applied on the toast index names, even if this > +# does not make a difference between the catalog and normal ones, the ordering > +# based on the name is enough to ensure a fixed output. > +my $compare_relfilenodes = > + qq(SELECT regexp_replace(b.relname::text, '(pg_toast.pg_toast_)\\d{4,5}(_index)', '\\1\\2'), Why {4,5} ? > + CASE WHEN a.relfilenode = b.relfilenode THEN 'relfilenode is unchanged' > + ELSE 'relfilenode has changed' END > + FROM toast_relfilenodes b > + JOIN pg_class a ON b.relname::text = a.oid::regclass::text > + ORDER BY b.relname::text); > + > +# Save the set of relfilenodes and compare them. > +$node->safe_psql('postgres', $save_relfilenodes); > +$node->issues_sql_like( > + [ 'reindexdb', 'postgres' ], > + qr/statement: REINDEX DATABASE postgres;/, > + 'SQL REINDEX run'); > +my $relnode_info = $node->safe_psql('postgres', $compare_relfilenodes); > +is( $relnode_info, qq(pg_constraint_oid_index|relfilenode is unchanged > +pg_toast.pg_toast__index|relfilenode has changed > +pg_toast.pg_toast__index|relfilenode is unchanged > +test1x|relfilenode has changed), 'relfilenode change after REINDEX DATABASE'); > + > +# Re-save and run the second one. > +$node->safe_psql('postgres', > + "TRUNCATE toast_relfilenodes; $save_relfilenodes"); > +$node->issues_sql_like( > + [ 'reindexdb', '-s', 'postgres' ], > + qr/statement: REINDEX SYSTEM postgres;/, > + 'reindex system tables'); > +$relnode_info = $node->safe_psql('postgres', $compare_relfilenodes); > +is( $relnode_info, qq(pg_constraint_oid_index|relfilenode has changed > +pg_toast.pg_toast__index|relfilenode is unchanged > +pg_toast.pg_toast__index|relfilenode has changed > +test1x|relfilenode is unchanged), 'relfilenode change after REINDEX SYSTEM');