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 1iKAXq-00029W-1x for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Oct 2019 00:18:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iKAXn-0007UQ-UE for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Oct 2019 00:18:31 +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 1iKAXn-0007Tx-EY for pgsql-bugs@lists.postgresql.org; Tue, 15 Oct 2019 00:18:31 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iKAXk-0001zq-9k for pgsql-bugs@lists.postgresql.org; Tue, 15 Oct 2019 00:18:30 +0000 Received: by mail-wm1-x342.google.com with SMTP id a6so18845466wma.5 for ; Mon, 14 Oct 2019 17:18:27 -0700 (PDT) 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:references:mime-version :content-disposition:in-reply-to:user-agent; bh=sZ1aEdsJbJOoqFtCl4XP0l3z9Ht9E7Ql+oIg9PoLHPo=; b=EDih0k/bzhfxM3Rp5wnKsQp3ZGy+EvGQINuMaqYgeCyDrqDSRM/pavtCqYudteLpLH TU9ek5qBzae2Px4HFMkGFVmARm+K0YqT2+P99S5gtSZTUu+1zc3VV1AiJItC4P3ggzAI +C+hc31ssMU5011M8Cw9jvJ2TH3B0zNqcyDq5xwLNoDGbTfrIIglPmrlR84FdIJX4eIw RGeAD43d4iyVre5PZod8t+Hs6bTdIYsbL/EPToCGbNVDZ88zMmyON3f+sz/RCO/5WxnQ AHwqjIJYRwM9RTy84B89h1tQ1OMVgbIV2WPqCW51HuDndOTuACK6fzs2e46Xoy1+Y94O KsNA== 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=sZ1aEdsJbJOoqFtCl4XP0l3z9Ht9E7Ql+oIg9PoLHPo=; b=OxDnFOPcUqoF5niJUU7VchDraqn9pU+pHGHtX0oB4TyF9BPySZAMpwH13RqlZfHpPY LAsbH9mC4IQU4L0T6r1zp60sdxmwVXmkHYg41Q600GF8xLSEbouOuQDeWdC/CRBXBmjo hWhE+yZxT+2ylN0XWwDxi0282RdFJLYzepP/oepS+bSUCq1UVAXNcy/My/GhG7zG8NqL AbHonhg3ZHK2nNqAdvewE3CSd6vLtZQhWIRargAypAn8ML6TyYdgyE7q7zDmlE9aElIX Ygz7m1thwegB1oFRpGr8vdgydDBxwVnU7nZZMCkB4MnmojtZJYcEoEACO+HLMYP8uh23 TB/Q== X-Gm-Message-State: APjAAAVOiuyIIQF4oZLPzb9ibd2s2v8clkqW8X3PiKtijAljizHL3vc6 h5TiUAzThLls54jaSxwxNLH0eUkfwwJN6Q== X-Google-Smtp-Source: APXvYqyptgcKFVqFQs7LPnu5RcIWhfrOwOfwpbd0SP9EhmqxxqlVUQPVNXp9/HlE3Ie8AF1o09EG1Q== X-Received: by 2002:a7b:c40a:: with SMTP id k10mr18433247wmi.115.1571098705594; Mon, 14 Oct 2019 17:18:25 -0700 (PDT) Received: from localhost (net-188-219-235-30.cust.vodafonedsl.it. [188.219.235.30]) by smtp.gmail.com with ESMTPSA id r13sm28600457wrn.0.2019.10.14.17.18.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2019 17:18:19 -0700 (PDT) Date: Tue, 15 Oct 2019 02:18:17 +0200 From: Tomas Vondra To: Tom Lane Cc: Andres Freund , buschmann@nidsa.net, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16045: vacuum_db crash and illegal memory alloc after pg_upgrade from PG11 to PG12 Message-ID: <20191015001817.zei45apgpu5dzngr@development> References: <20191010134303.bkwjzchjb2w66iro@development> <30706.1570717152@sss.pgh.pa.us> <20191010163350.2rtcjtje6tkgilqi@development> <1542.1570738460@sss.pgh.pa.us> <20191010204022.6hiiubm2cueu3ghk@development> <20191013001032.lqrn4kqaovhrf77l@development> <17717.1570991208@sss.pgh.pa.us> <20191013203848.2eksimmpux3546wr@development> <27596.1571062600@sss.pgh.pa.us> <20191014163538.cqn6j4cl7s2mk2yz@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="sspr6rxkpcihpkly" Content-Disposition: inline In-Reply-To: <20191014163538.cqn6j4cl7s2mk2yz@development> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --sspr6rxkpcihpkly Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Mon, Oct 14, 2019 at 06:35:38PM +0200, Tomas Vondra wrote: > ... > >Aha! I forgot we copy the necessary stuff into pg_attribute. Thanks for >clarifying, I'll polish and push the fix shortly. > I've pushed and backpatched the fix. Attached are similar fixes for the existing pg_upgrade checks for pg_catalog.line and pg_catalog.unknown types, which have the same issues with composite types and domains. There are some additional details & examples in the commit messages. I've kept this in two patches primarily because of backpatching - the line fix should go back up to 9.4, the unknown is for 10. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --sspr6rxkpcihpkly Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-Correct-the-check-for-pg_catalog.line-in-pg_upgrade.patch" From 52821ea02fca502ff070508ee7be278563117509 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Tue, 15 Oct 2019 01:16:21 +0200 Subject: [PATCH 1/2] Correct the check for pg_catalog.line in pg_upgrade The pg_upgrade check for pg_catalog.line data type when upgrading from 9.3 had a number of issues with domains and composite types. Firstly, it detected even composite types unused in objects with storage. So for example this was enough to trigger pg_upgrade failure: CREATE TYPE line_composite AS (l pg_catalog.line) On the other hand, this only happened with composite types directly on the pg_catalog.line data type, but not with a domain. So this was not detected CREATE DOMAIN line_domain AS pg_catalog.line; CREATE TYPE line_composite_2 AS (l line_domain); unlike the first example. What's worse, we have not detected this even when used in a table. So we missed cases like this: CREATE TABLE t (l line_composite_2); This fixes these false positives and false negatives by adopting the same recursive CTE introduced by eaf900e842 for sql_identifier. Backpatch all the way to 9.4, where the storage for pg_catalog.line data type changed. Author: Tomas Vondra Backpatch-to: 9.4- Discussion: https://postgr.es/m/16045-673e8fa6b5ace196%40postgresql.org --- src/bin/pg_upgrade/version.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/bin/pg_upgrade/version.c b/src/bin/pg_upgrade/version.c index 8375a46454..cfe69ea554 100644 --- a/src/bin/pg_upgrade/version.c +++ b/src/bin/pg_upgrade/version.c @@ -131,14 +131,41 @@ old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster) DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; PGconn *conn = connectToServer(cluster, active_db->db_name); + /* + * We need the recursive CTE because the pg_catalog.line may be wrapped + * either in a domain or composite type, or both (9.3 did not allow domains + * on composite types, but there may be multi-level composite type). + */ res = executeQueryOrDie(conn, + "WITH RECURSIVE oids AS ( " + /* the pg_catalog.line type itself */ + " SELECT 'pg_catalog.line'::pg_catalog.regtype AS oid " + " UNION ALL " + " SELECT * FROM ( " + /* domains on the type */ + " WITH x AS (SELECT oid FROM oids) " + " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' " + " UNION " + /* composite types containing the type */ + " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x " + " WHERE t.typtype = 'c' AND " + " t.oid = c.reltype AND " + " c.oid = a.attrelid AND " + " NOT a.attisdropped AND " + " a.atttypid = x.oid " + " ) foo " + ") " "SELECT n.nspname, c.relname, a.attname " "FROM pg_catalog.pg_class c, " " pg_catalog.pg_namespace n, " " pg_catalog.pg_attribute a " "WHERE c.oid = a.attrelid AND " " NOT a.attisdropped AND " - " a.atttypid = 'pg_catalog.line'::pg_catalog.regtype AND " + " a.atttypid IN (SELECT oid FROM oids) AND " + " c.relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_INDEX) ") AND " " c.relnamespace = n.oid AND " /* exclude possible orphaned temp tables */ " n.nspname !~ '^pg_temp_' AND " -- 2.21.0 --sspr6rxkpcihpkly Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0002-Correct-the-check-for-pg_catalog.unknown-in-pg_upgra.patch" From de98ca82812798e6b7392196fdcf34dff8a78133 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Tue, 15 Oct 2019 01:16:47 +0200 Subject: [PATCH 2/2] Correct the check for pg_catalog.unknown in pg_upgrade The pg_upgrade check for pg_catalog.unknown type when upgrading from 9.6 had a couple of issues with domains and composite types. Firstly, it detected even composite types unused in objects with storage. So for example this was enough to trigger pg_upgrade failure: CREATE TYPE unknown_composite AS (u pg_catalog.unknown) On the other hand, this only happened with composite types directly on the pg_catalog.unknown data type, but not with a domain. So this was not detected CREATE DOMAIN unknown_domain AS pg_catalog.unknown; CREATE TYPE unknown_composite_2 AS (u unknown_domain); unlike the first example. What's worse, we have not detected this even when used in a table. So we missed cases like this: CREATE TABLE t (u unknown_composite_2); This fixes these false positives and false negatives by using the same recursive CTE introduced by eaf900e842 for sql_identifier. Backpatch all the way to 10, where the of pg_catalog.unknown data type was restricted. Author: Tomas Vondra Backpatch-to: 10- Discussion: https://postgr.es/m/16045-673e8fa6b5ace196%40postgresql.org --- src/bin/pg_upgrade/version.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/bin/pg_upgrade/version.c b/src/bin/pg_upgrade/version.c index cfe69ea554..ccfb83c7eb 100644 --- a/src/bin/pg_upgrade/version.c +++ b/src/bin/pg_upgrade/version.c @@ -256,16 +256,33 @@ old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster) PGconn *conn = connectToServer(cluster, active_db->db_name); res = executeQueryOrDie(conn, + "WITH RECURSIVE oids AS ( " + /* the sql_identifier type itself */ + " SELECT 'pg_catalog.unknown'::pg_catalog.regtype AS oid " + " UNION ALL " + " SELECT * FROM ( " + /* domains on the type */ + " WITH x AS (SELECT oid FROM oids) " + " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' " + " UNION " + /* composite types containing the type */ + " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x " + " WHERE t.typtype = 'c' AND " + " t.oid = c.reltype AND " + " c.oid = a.attrelid AND " + " NOT a.attisdropped AND " + " a.atttypid = x.oid " + " ) foo " + ") " "SELECT n.nspname, c.relname, a.attname " "FROM pg_catalog.pg_class c, " " pg_catalog.pg_namespace n, " " pg_catalog.pg_attribute a " "WHERE c.oid = a.attrelid AND " " NOT a.attisdropped AND " - " a.atttypid = 'pg_catalog.unknown'::pg_catalog.regtype AND " + " a.atttypid IN (SELECT oid FROM oids) AND " " c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " - CppAsString2(RELKIND_COMPOSITE_TYPE) ", " CppAsString2(RELKIND_MATVIEW) ") AND " " c.relnamespace = n.oid AND " /* exclude possible orphaned temp tables */ -- 2.21.0 --sspr6rxkpcihpkly--