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 1pcjWk-0005jx-DZ for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Mar 2023 09:04: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 1pcjWj-0003Ni-As for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Mar 2023 09:04:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcjWi-0003L8-Rd for pgsql-hackers@lists.postgresql.org; Thu, 16 Mar 2023 09:04:01 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pcjWg-0007iP-Bo for pgsql-hackers@lists.postgresql.org; Thu, 16 Mar 2023 09:03:59 +0000 Received: by mail-pl1-x62c.google.com with SMTP id c18so978066ple.11 for ; Thu, 16 Mar 2023 02:03:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678957437; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ihXv0zSab0Zdxu8TfmILVmgwu9mjzjmGcbT+7xz168Y=; b=bOY+l0jOFOoPfd2G3mYI1Y+6MGq2Nhh6w61LOLytNhFauIFV8oyRCxI5ZzQY+moMZf 2EDTbJT5VzDjhbA2j6RMbnlGfYutq+OlnUOZ78YbnKX795dLIJ67MP4h5Kd8NW7LbTLL lCT8L7nmHml/xVUREhQUZVgH33ko/BJzyVm1ISJFweDG3astDFLZ8VonHbtuaC/YvclO Pqn35n/oYFOT+HDFz1mLqqhMYB6vxarYARD34HDqDcr8M8qtbX7YFT3Pf2XO1VHo+f3j b/LfpxL7ME8Ek6qKj7x1wyLlBK0RfI7G4KoYwUcI0Mp8Twl3uWHM1SEkkvbTFg9hIpjW QJiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678957437; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ihXv0zSab0Zdxu8TfmILVmgwu9mjzjmGcbT+7xz168Y=; b=OPPKtIBLD+UqRn1IZgSeAzHcZf8oBhGqPljlNnUmnZ9RHZ98QKCmq8X7ry0k43ZrrA dEhgv5EvmwQ+ZHiK6kMUcxkQ0P3kqCNSUG1b+XWVGUWCCF9BcFa0cvQjE/LjwrtGvfeC zAxmw8q1Msuy+u7cw8HDVGFSs+WSt3ffBAPzUiPYHEDY99FDHqOkXkkev7LZv5zxbljl 2tPfA2Z6sCer5qaO3b+IMaGxBZ5CuH0QHSiUPQWMMUQXjBEFv9cRYh6LSclXiJXzoGeh cGfA7C7wlnnAcq/GZSDr22FsirVddYMLA3JmTjQyMG4qcxZAkRGhGog2XLCPXHqTH2yX Zb1w== X-Gm-Message-State: AO0yUKWg45PiM4WMUVdeE4IufBw6ZnqdHvjJHelIQaF2gmQdhKLZF9kq WK3wcP1aHtlrzq5NazJW/nM= X-Google-Smtp-Source: AK7set+rIt96C5NstT/HsGZ9WoK/LSndnp0J52HAWXdkdYwoPKPH7Hqrb2I1vpnZC7oaDHfGDDzPxA== X-Received: by 2002:a05:6a20:8b83:b0:d4:c1ba:9f4e with SMTP id m3-20020a056a208b8300b000d4c1ba9f4emr2833616pzh.35.1678957437091; Thu, 16 Mar 2023 02:03:57 -0700 (PDT) Received: from jrouhaud (2001-b011-1005-72e1-2c07-ac23-686e-aaea.dynamic-ip6.hinet.net. [2001:b011:1005:72e1:2c07:ac23:686e:aaea]) by smtp.gmail.com with ESMTPSA id a3-20020aa78643000000b005a8ba70315bsm4936078pfo.6.2023.03.16.02.03.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 02:03:56 -0700 (PDT) Date: Thu, 16 Mar 2023 17:03:52 +0800 From: Julien Rouhaud To: Tom Lane Cc: Andrew Dunstan , Alvaro Herrera , David Rowley , Peter Geoghegan , Robert Haas , pgsql-hackers@lists.postgresql.org, Andrew Subject: Re: pg_dump versus hash partitioning Message-ID: <20230316090352.v2m2d473fcv5tli6@jrouhaud> References: <1440373.1675299790@sss.pgh.pa.us> <1533086.1675349554@sss.pgh.pa.us> <1912821.1676402493@sss.pgh.pa.us> <20230311030154.pg63uspio35h4a6t@jrouhaud> <1078804.1678504214@sss.pgh.pa.us> <20230311033232.g6jgkuzxdawepsbe@jrouhaud> <1602203.1678650412@sss.pgh.pa.us> <20230313083314.pivmgfu36fi4eu2n@jrouhaud> <2441413.1678750752@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2441413.1678750752@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Mar 13, 2023 at 07:39:12PM -0400, Tom Lane wrote: > Julien Rouhaud writes: > > On Sun, Mar 12, 2023 at 03:46:52PM -0400, Tom Lane wrote: > >> The trick is to detect in pg_restore whether pg_dump chose to do > >> load-via-partition-root. > > > Given that this approach wouldn't help with existing dump files (at least if > > using COPY, in any case the one using INSERT are doomed), so I'm slightly in > > favor of the first approach, and later add an easy and non magic incantation > > way to produce dumps that don't depend on partitioning. > > Yeah, we need to do both. Attached find an updated patch series: I didn't find a CF entry, is it intended? > 0001: TAP test that exhibits both this deadlock problem and the > different-hash-codes problem. I'm not sure if we want to commit > this, or if it should be in exactly this form --- the second set > of tests with a manual --load-via-partition-root switch will be > pretty redundant after this patch series. I think there should be at least the first set of tests committed. I would also be happy to see some test with the --insert case, even if those are technically redundant too, as it's quite cheap to do once you setup the cluster. > 0002: Make pg_restore detect load-via-partition-root by examining the > COPY commands embedded in the dump, and skip the TRUNCATE if so, > thereby fixing the deadlock issue. This is the best we can do for > legacy dump files, I think, but it should be good enough. is_load_via_partition_root(): + * In newer archive files this can be detected by checking for a special + * comment placed in te->defn. In older files we have to fall back to seeing + * if the COPY statement targets the named table or some other one. This + * will not work for data dumped as INSERT commands, so we could give a false + * negative in that case; fortunately, that's a rarely-used option. I'm not sure if you intend to keep the current 0002 - 0003 separation, but if yes the part about te->defn and possible fallback should be moved to 0003. In 0002 we're only looking at the COPY statement. - * the run then we wrap the COPY in a transaction and - * precede it with a TRUNCATE. If archiving is not on - * this prevents WAL-logging the COPY. This obtains a - * speedup similar to that from using single_txn mode in - * non-parallel restores. + * the run and we are not restoring a + * load-via-partition-root data item then we wrap the COPY + * in a transaction and precede it with a TRUNCATE. If + * archiving is not on this prevents WAL-logging the COPY. + * This obtains a speedup similar to that from using + * single_txn mode in non-parallel restores. I know you're only inheriting this comment, but isn't it well outdated and not accurate anymore? I'm assuming that "archiving is not on" was an acceptable way to mean "wal_level < archive" at some point, but it's now completely misleading. Minor nitpicking: - should the function name prefixed with a "_" like almost all nearby code? - should there be an assert that the given toc entry is indeed a TABLE DATA? FWIW it unsurprisingly fixes the problem on my original use case. > 0003: Also detect load-via-partition-root by adding a label in the > dump. This is a more bulletproof solution going forward. > > 0004-0006: same as previous patches, but rebased over these. > This gets us to a place where the new TAP test passes. +getPartitioningInfo(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + int ntups; + + /* no partitions before v10 */ + if (fout->remoteVersion < 100000) + return; + [...] + /* + * Unsafe partitioning schemes are exactly those for which hash enum_ops + * appears among the partition opclasses. We needn't check partstrat. + * + * Note that this query may well retrieve info about tables we aren't + * going to dump and hence have no lock on. That's okay since we need not + * invoke any unsafe server-side functions. + */ + appendPQExpBufferStr(query, + "SELECT partrelid FROM pg_partitioned_table WHERE\n" + "(SELECT c.oid FROM pg_opclass c JOIN pg_am a " + "ON c.opcmethod = a.oid\n" + "WHERE opcname = 'enum_ops' " + "AND opcnamespace = 'pg_catalog'::regnamespace " + "AND amname = 'hash') = ANY(partclass)"); Hash partitioning was added with pg11, should we bypass pg10 too with a comment saying that we only care about hash, at least for the forseeable future? Other than that, the patchset looks quite good to me, modulo the upcoming doc changes. More generally, I also think that forcing --load-via-partition-root for known unsafe partitioning seems like the best compromise. I'm not sure if we should have an option to turn it off though.