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 1iIfEd-0003aG-Qt for pgsql-bugs@arkaria.postgresql.org; Thu, 10 Oct 2019 20:40:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIfEc-00080B-EY for pgsql-bugs@arkaria.postgresql.org; Thu, 10 Oct 2019 20:40:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIfEc-0007zu-2Z for pgsql-bugs@lists.postgresql.org; Thu, 10 Oct 2019 20:40:30 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIfEY-0004I3-AO for pgsql-bugs@lists.postgresql.org; Thu, 10 Oct 2019 20:40:29 +0000 Received: by mail-wr1-x441.google.com with SMTP id v8so9455770wrt.2 for ; Thu, 10 Oct 2019 13:40:25 -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=0XcUY5IEa4JhQGOeQh2dEGzon+XdrzXhPqZRqVPI/Ew=; b=kFT5aKlAzwP4K59hDNZBo8HVJqjuYf1RDb4hKX2iHVonV1mMe4xtKoPZHeapgQTkTb d7c4J0dLtNxD38fXS9dF8Rytrmncz7pPhNCvZuk3h+jr0fBBnfdzNTLAiYy5PEqpjdaL cenqO4Q879LH9XEWMsfBORXveNsbDhQOzZbVl++zBk3Jzike2QHAdM3KCwbvFpOQDraS +bNj/XuNFABPbWrbBpNDo8/f2mEUUZiFlAE3BXobmqwCzQ0vZQrFSUOCdyaY1dvIYVg7 xiipGDgXddYZXHNu7xeL43/2tI3NAeCkP32EASz4oNU1Fd6l0Fze0VYwj3NFuZRoCGks wqjg== 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=0XcUY5IEa4JhQGOeQh2dEGzon+XdrzXhPqZRqVPI/Ew=; b=WuydOj/kpeJrvgJuxWeyvgVdu9wZZZ6wX45Af72LV8rdYKTmU6TTlDidUhb2UhNODh BJ6UIVMN7pdEWVIMuWjCGudJbj2clwIe8oLpiCqFR0nqN5tga+OdwQOSRZjX02M68KwO zh9Ud+5+cbSqsRG8IbwGv/B5bY34To7S8WSyBqWguKrwf/FfWDuSbBe4ZVSRUSMvDp0w YNtrW3+utFBSQw1qEd+bdjNO5jTPTOXARiUpFyknWOXwv/C64/u0YKm6LJx8HrLTlWDe NL5aWmT0HnIeWrsN2RntvAaO5nApWnusXJsSN+SaWUsquJ4KDRtYlWkCAugOnQ/BuRB8 mg+Q== X-Gm-Message-State: APjAAAU5TAVzkxFkVb1zeeOPXQEq+i7DA9L4B/YBRfVg7Bckk6knTRov Ntlcba+tSuTZEyWv/rQ+2xt4Cw== X-Google-Smtp-Source: APXvYqyZmNHB1o3duxfxBoy9AcPlKDNyMajxQTFtediNM99nPuvPmgIaGH1hDF6CJpgS/xvRo+tOgA== X-Received: by 2002:adf:e48f:: with SMTP id i15mr10610369wrm.288.1570740025031; Thu, 10 Oct 2019 13:40:25 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id r13sm10679270wrn.0.2019.10.10.13.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 13:40:23 -0700 (PDT) Date: Thu, 10 Oct 2019 22:40:22 +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: <20191010204022.6hiiubm2cueu3ghk@development> References: <20191009141841.my6k44wfiaquj3ks@development> <20191009230759.ge66neof5qj627a2@development> <19314.1570663125@sss.pgh.pa.us> <20191009232836.jg77wccqwy5fgs6s@development> <20265.1570664514@sss.pgh.pa.us> <20191010014813.3b2btmkakhrskwgd@alap3.anarazel.de> <20191010134303.bkwjzchjb2w66iro@development> <30706.1570717152@sss.pgh.pa.us> <20191010163350.2rtcjtje6tkgilqi@development> <1542.1570738460@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <1542.1570738460@sss.pgh.pa.us> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Oct 10, 2019 at 04:14:20PM -0400, Tom Lane wrote: >Tomas Vondra writes: >> On Thu, Oct 10, 2019 at 10:19:12AM -0400, Tom Lane wrote: >>> To identify such domains, I think we'd need something like >>> WHERE attypid IN (recursive-WITH-query), which makes me nervous. >>> We did support those starting with 8.4, which is as far back as >>> pg_upgrade will go, so in theory it should work. But I think we >>> had bugs with such cases in old releases. Do we want to assume >>> that the source server has been updated enough to avoid any such >>> bugs? The expense of such a query might be daunting, too. > >> For the query cost, I think we can assume the domain hierarchies are not >> particularly deep (in practice I'd expect just domains directly on the >> sql_identifier type). And I doubt people are using that very widely, >> it's probably more like this report - ad-hoc CTAS, so just a couple of >> items. So I wouldn't expect it to be a huge deal in most cases. But even >> if it takes a second or two, it's a one-time cost. > >What I was worried about was the planner possibly trying to apply the >atttypid restriction as a scan qual using a subplan, which might be rather >awful. But it doesn't look like that happens. OK. > I get a hash semijoin to >the CTE output, in all branches back to 8.4, on this trial query: > >explain >with recursive sqlidoids(toid) as ( >select 'information_schema.sql_identifier'::pg_catalog.regtype as toid >union >select oid from pg_catalog.pg_type, sqlidoids > where typtype = 'd' and typbasetype = sqlidoids.toid >) >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 in (select toid from sqlidoids) AND > c.relkind IN ('r','v','i') and > c.relnamespace = n.oid AND > n.nspname !~ '^pg_temp_' AND > n.nspname !~ '^pg_toast_temp_' AND > n.nspname NOT IN ('pg_catalog', 'information_schema'); > I think that's not quite sufficient - the problem is that we can have domains and composite types on sql_identifier, in some arbitrary order. And the recursive CTE won't handle that the way it's written - it will miss domains on composite types containing sql_identifier. And we have quite a few of them in the information schema, so maybe someone created a domain on one of those (however unlikely it may seem). I think this recursive CTE does it correctly: WITH RECURSIVE oids AS ( -- type itself SELECT 'information_schema.sql_identifier'::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 a.atttypid = x.oid ) foo ) I had to use CTE within CTE, because the 'oids' can be referenced only once, but we have two subqueries there. Maybe there's a better solution. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services