Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w6uyF-004mSN-2d for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Mar 2026 18:34:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w6uyE-00H0fg-12 for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Mar 2026 18:34:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w6uyD-00H0fY-2X for pgsql-hackers@lists.postgresql.org; Sun, 29 Mar 2026 18:34:46 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w6uyB-00000001gMg-2T9W for pgsql-hackers@postgresql.org; Sun, 29 Mar 2026 18:34:45 +0000 Received: by mail-qk1-x741.google.com with SMTP id af79cd13be357-8cfc5941028so692122385a.1 for ; Sun, 29 Mar 2026 11:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1774809282; x=1775414082; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=O/xz2/EV7uihJ1uArgPI6vdMFuiLMYYre/Ziv74ZY8o=; b=MA/WhFVYprqalrvmthBatbRGpiWNnDY16Ht/iuIMv30qkGF4QiAOv959YqyGfltn7P FHvYtfamj+uiHPv7UBbSJc3wfcoWp2pwu/62VkIoeFouxE3RYO22MHl2OxEOsLnjRedt fnDG9KJ8JYhjGJU3Mg2eQ0KT/lCG8eCFJHHgBd9ObH0tgrX39CCD61JN3u0sQsxyWaHY YstYN4M3Vl62HzpD+5mLY4LOrXTRdLLcKBIR39JFAJGjncdPCJ2LJoZ38p6Pc4fQ3hK1 qf4Z6fAXd+NJatabv6Ik51K6/jX1ilZivLqunYGgu5PxDfKkX7vcZhOnoKt4cu8hJ0JL /Bpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774809282; x=1775414082; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=O/xz2/EV7uihJ1uArgPI6vdMFuiLMYYre/Ziv74ZY8o=; b=jd9teuzWeNBUk2/oCzHujS09RWXgBf+Wepusf05tvjaZHYnAXdtBcCKfRn7+bZwt35 dijJ79CdDjcul+Hh7BSrH42GEZkX1H1zROvFUzz34XbJi0/5OTDjRXkSbD6AFoDhyf4Z SIpypcSPcanQe3JffRg5IJm/ESmHh/2CozUAjEVLsjk7tkU6V0MCMDYsVYnYln5xIyyB tOG9iOsArcY3wXj5hxsFAP2Bx9yEEajmASMRLuRRRrCVbGvQHoKFncsIfy2SXhlO0osY KGm0dh496lStBQNY1yqnicAJw8x5+Tp2S8zIL0UhCO96CwEmAOMUVgnvEmY51ZmDwIJS kKWA== X-Forwarded-Encrypted: i=1; AJvYcCVg+E7cIzYqa7cvhgRzFiapclcYHeA2ZymGhxdHty9zsU+ZFWDx9iR+tXU1P0jR5FFK3fxBmySVfaPTquGt@postgresql.org X-Gm-Message-State: AOJu0YzKdLKXwnn7WG1ZiKZ4ViXcfuS5oMfm1x66wjnFvPvrTxCZc9Mm KGmF6sN8UhM9x0dZQ8563fuNGAfynyk/HjF+B3PZ4VZlERMaZoDDUKF4mgyePr8ce5M= X-Gm-Gg: ATEYQzz7hNoC3ENS2xVwcDIn0UVoeGfeqN5PlQEvhZiUXVtEGznHRleOOCJbtmD1Jxe xHwRHuN2Z2K9aD3O51696B3K1m6Gw17uPSICxsQv5xrczm5gS37/e+0MRt9Q1NWTNffrCRai47s V3pShUIVM2yhHkA4yaOGdGI3xukY53xLHEP2p+V6BXH03LuwUcK3ZK4p0LrlEUhDzZPww1iv4GV HTCX3NFd6WPDZeARmlk7fuYe8MBIAHkxE24QaRM+pqQd19p3QJlhhkp9wLcQlRdYQ0GAo0d8xXE 4Jp6zVm5wTkFTFNktF/CCekqBtqQQs8mXUI04Eq8c89fh3vJwTby2Jnz3WaoVoz/LuX3nX/Y6tg ITJ0LoAxAcJM8NPwp2NhIoHuDmAl1s7I5NyHOku9hXGOw8me5s2i0Lmw9T0h0iFNxTNrJRhJmXk gtw6lRInIirmPbLGW+P/LuQZpKelKAoA== X-Received: by 2002:a05:620a:1721:b0:8cf:d289:d0d6 with SMTP id af79cd13be357-8d01c596434mr1307466185a.14.1774809281765; Sun, 29 Mar 2026 11:34:41 -0700 (PDT) Received: from ?IPV6:2605:a601:a6b0:500::1cb? ([2605:a601:a6b0:500::1cb]) by smtp.googlemail.com with ESMTPSA id af79cd13be357-8d027edb7ddsm423058485a.9.2026.03.29.11.34.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Mar 2026 11:34:41 -0700 (PDT) Message-ID: Date: Sun, 29 Mar 2026 14:34:39 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] pg_dump: Restore extension config table data before user objects during pg_upgrade To: Jimmy Angelakos , pgsql-hackers@postgresql.org References: From: Andrew Dunstan Content-Language: en-US Autocrypt: addr=andrew@dunslane.net; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SG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QqZRtnEAEQEAAcLAfAQYAQgAJgIbDBYhBOQ+WEYd/Hy/RGkVpZn6f8tZ/DuBBQJoGNI2BQkd EODdAAoJEJn6f8tZ/DuBfw0IAKTsfD40teP/pp+bsLLMSxPXUYrrprTj7WFB5v61p6dkpSr/ qXmMlyahdxQFaPmfVgVirB1Vk/kHiWNnnGjfUV9nB2Zg9LI0Xb9/ts3LsUiRWXzG3tkMY6XL vsVOxW4XFRND9l2q+WW93aZ1DZl+fqWfYgMvsusFRhmGFOKTRfKPta2Pkv+AhA24N4+PrR5p bU4k2MO8PAGiK8eaYKGFG1bHKuAvoDoF7WXJ3FHxuWqLnKEt4dfOLm5pAe3zq1Lt6q8azT9i QWGpSAK5vQUWQHBHpiDjdPeqKZ6HiAXIIKfSmb+jrvXBqoP+D6/K7rUjG2aXiRtTIAXms9sm VRu7cmw= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2026-03-20 Fr 1:47 PM, Jimmy Angelakos wrote: > Hi All, > > I ran into this issue when pg_upgrade-ing a DB with PostGIS. This is > my first code patch, so any feedback on the approach will be appreciated! > > The problem: > ============ > pg_upgrade uses pg_dump --schema-only --binary-upgrade to copy the > schema from $oldcluster to $newcluster. Because this excludes all > table data, it leaves out data in extension config tables registered > with pg_extension_config_dump(). > > In $newcluster, binary_upgrade_create_empty_extension() creates the > extensions without populating any table data. The extensions' CREATE > EXTENSION scripts never get executed so any INSERTs are skipped. As a > consequence, if any CREATE TABLE statement in $newcluster requires > validation against these empty config tables, the upgrade fails. As an > example, > PostGIS registers config table spatial_ref_sys to hold ~8500 spatial > reference system definitions (SRIDs). When a table has, e.g. a > geometry column that specifies an SRID, this gets validated during the > CREATE TABLE: > > CREATE TABLE points (id int, location geometry(Point, 27700)); > ERROR:  Cannot find SRID (27700) in spatial_ref_sys > > This will happen for any SRID-constrained column, which will prevent > many real-world PostGIS deployments from being able to pg_upgrade. To > summarise the problem, our ordering is wrong here because extension > configuration data must be present before user tables that depend on > it get created, but --schema-only strips this data. > > The patch: > ========== > We are adding a new dump object type DO_EXTENSION_DATA that dumps > extension config table data in SECTION_PRE_DATA during > --binary-upgrade ONLY. This restores the needed data between extension > creation and user object creation, allowing the DDL to succeed. > > Four files are modified in bin/pg_dump: > > pg_dump.h: > Add DO_EXTENSION_DATA to the DumpableObjectType enum, between > DO_EXTENSION and DO_TYPE > > pg_dump_sort.c: > Add PRIO_EXTENSION_DATA between PRIO_EXTENSION and PRIO_TYPE > > pg_dump.c: > 1. Add makeExtensionDataInfo() to create a TableDataInfo with objType > = DO_EXTENSION_DATA. Called for plain tables (RELKIND_RELATION) during > --binary-upgrade ONLY. As it depends on the table def, the COPY will > be emitted after the CREATE TABLE. > 2. Add dumpExtensionData() to emit the entry in SECTION_PRE_DATA with > description "EXTENSION DATA" using dumpTableData_copy(). This allows > the config table data to go into the schema-only dump. > 3. In processExtensionTables(), when dopt->binary_upgrade is true, > call makeExtensionDataInfo() instead of makeTableDataInfo(). > Additionally, skip extcondition filter because we need to dump all > rows here. > 4. Include DO_EXTENSION_DATA in pre-data boundary in > addBoundaryDependencies() > > pg_backup_archiver.c: > Add "EXTENSION DATA" to the whitelist in _tocEntryRequired() similar > to BLOB, BLOB METADATA, etc. to include extension config table data in > --schema-only dumps during --binary-upgrade ONLY. > > What ends up happening: > ======================= > The inserted rows are basically scaffolding to allow the upgrade, and > do not persist. The pg_upgrade sequence goes like: > 1. pg_dump includes $oldcluster extension config data in schema-only dump > 2. pg_restore replays the dump into $newcluster and "EXTENSION DATA" > entries populate tables like spatial_ref_sys with COPY. Subsequent > CREATE TABLEs with e.g. SRID-constrained columns pass validation. > 3. pg_upgrade transfers all data files from $oldcluster to > $newcluster, making spatial_ref_sys byte-for-byte identical to its > previous state. > > This patch: > 1. Does NOT affect normal pg_dumps (without --binary-upgrade). > DO_EXTENSION_DATA objects are not created in this case. > 2. Leaves binary_upgrade_create_empty_extension() unchanged. > 3. Is not PostGIS-specific, and should solve this class of problem for > any extension that registers config tables that will be needed for DDL > validation. > 4. Has been tested against HEAD at 29bf4ee7496 with $oldcluster > PostGIS 3.3.9 on PG14 and $newcluster PostGIS 3.7.0dev/master on > PG19-devel. > > Thanks in advance for your review! Please find attached the patch for > HEAD. I believe this should be easily backpatchable to (at least) > PG15, and will be happy to work on backports. Hi, Jimmy. First, as you probably know, we don't backpatch features, and I think this comes into that category. Unfortunately, we're about to close release 19 for features, so this would need to wait till release 20. The patch didn't include any tests. It will need them (probably in src/test/modules/test_pg_dump) There appears to be a lot of code duplication between dumpExtensionData() and dumpTableData(). It might be better to refactor that, perhaps by supplying an extra flag to dumpTableData(). Do make sure to add a Commitfest entry for this is you haven't already done so. cheers andrew > > Best regards, > Jimmy -- Andrew Dunstan EDB: https://www.enterprisedb.com