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.98.2) (envelope-from ) id 1x97a0-00000001knU-21Ul for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Sep 2026 20:59:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x97Zz-00000001WQ9-2odm for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Sep 2026 20:59:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x97Zz-00000001WQ1-1TNp for pgsql-hackers@lists.postgresql.org; Tue, 22 Sep 2026 20:59:07 +0000 Received: from mail-vs2-x10.google.com ([2a00:1450:4864:3a::10]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x97Zx-00000000lTc-0HHs for pgsql-hackers@lists.postgresql.org; Tue, 22 Sep 2026 20:59:07 +0000 Received: by mail-vs2-x10.google.com with SMTP id 71dfb90a1353d-5c6560f7a78so27120e0c.3 for ; Tue, 22 Sep 2026 13:59:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790110743; x=1790715543; darn=lists.postgresql.org; h=content-type:mime-version:references:in-reply-to:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6iLlBrHNTJLJgxUPe9Gk2HN/EJRl7EfyDsKtOJ1tBcU=; b=DOk/jt9e9N/EC3o6wl0tz8bLrGX+MvSvGjMQAiBmyKM3KMWqMBldEKA3XMQHW7kfZr ynk7WmeKZRyTnzs2GTIO506Di7O333P1vGxxTsO9vwFJSOnSN+ysnjBdu0G24WalwCpW KY7JfWHc18MFDH2YDxm5YC6+4keHG0As6C5LF4+FK0M8TFzISXehs2v7R+PkA+3xjCQY 3LWgvlbXrvd69svm4awyQTm0g7na0XNzFkGX5sO66oc6i3fKkH+oAeBJdi9fo4hbbWEL UCk9r+UPp/8iGkvmiOg3agG3hA67YK5fRaKIBXOppgzgwS1XoE1KqOjMzMpFvXO9AiLJ BjgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790110743; x=1790715543; h=content-type:mime-version:references:in-reply-to:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=6iLlBrHNTJLJgxUPe9Gk2HN/EJRl7EfyDsKtOJ1tBcU=; b=JMAVv5VreXoc07R9eLgXEVYcIw4pNonioJV3esZjpDCMCvPkTFMTfzBg+FGkDyg1Gk I14w4IWH1E7zFgZKq7NtXCN7U+E3/jsSLcqvkfI6eYdtjFEH8JzfXEgAqrMhOWgcYhpP nLPpOVwVXVpOxUwBkDYlb1iVrOguECQ56G8DXFzqqeIJ2oSe+1btsqbL6L9WpTV7ZItG mPYxrHAKDNBg5qnpf/FrtYLR93/NbSjDjuqTrhDQ/q+cxrv1BNLUiKLUNwsq9uAcZsBp Kt7yv/0Rek7pMP7bMeTRNiCnS64pHH2+S/sMWbCUV2A/ZPgwt3kkwx3rK0D6FGEZRzoU RuEA== X-Forwarded-Encrypted: i=1; AKwUvByd0mEPCJSG5f96bIH6mHoXCl/6+l9VhVnUIKJ/FrYmHDaz30IhIx62M4PuXeT10/DHTJ1CVb+1Kcy9fKfH@lists.postgresql.org X-Gm-Message-State: AFuF++nBEwfyzjXW6R96URr5ZPUeVGD52z4qz5slRScpPjaCWCezKJrq De1dfHOCDAF7aKokbHp1sGi5TtX4eq7oc0dU8CXniW+pgxkI9K1CPuTyB6BuuM2nNNc= X-Gm-Gg: AYBFou3IPeOd8GpLBz1czJ+ryBJWKGRfwkVXIiYeCxF2IIFI9UzULPrpDS1RwdAeKAE o33U5oVTkD0EMYJAaExtHMRmIS0CTCbhBj+VnrBdW0dZQBrW8efgBBl3Ra7vidjgEODcUMUPO2i BB/HhOVgh5M2gBoYlChUpu5qNNutM5XDWLyCHBIpJDkRnze0NWQYm3E5wvtZFWOywgP3r2Wsg+H tgulJ2cGdZsuxlQTtvwTkLzaWQ5B8XXaYqAHRf0Stl8GvEKer3s7Ca27jP1VDBN9whPXCAGyxRv zO+RpBdXUIyH+V56nzIv9E81xx7SuH6szb+7dvd1N/eZ85lcVyqDppEjnkrlBCFR0lNvH1pFm93 DA+xNpnLGZLwYFqXAAs1yFNDnvqT690Kh14eutYiVFEpr4Sz1ZzgY2o30n+Lhoto5HwCN25G04V +2eowqeWdc9BPZHg9c+tQqJc9TgX7LzxWZKqUr5zD+UGu+CM0285k5j7X9gYtKTC3QhdVFSXoIT PbyDuMz/zLl2Bx19KnV3U30Nb/txWtNCxhdc+fz/YHX+3JkNDhGevAW9/hIYg487r3wCwfk/9CQ nBraBNkMzyIAtiLudlrbKUvC4m5o2lUtTzJg7A== X-Received: by 2002:a05:6122:6b1c:b0:5c9:e558:53da with SMTP id 71dfb90a1353d-5c9f1656ad9mr1517168e0c.3.1790110742754; Tue, 22 Sep 2026 13:59:02 -0700 (PDT) Received: from 192.168.1.109 ([2803:3c90:cee:ea00:5c3c:7e70:29e3:7a55]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5c9f04c161fsm982420e0c.4.2026.09.22.13.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2026 13:59:02 -0700 (PDT) From: Manu To: Jelte Fennema-Nio Cc: Robert Haas , Tomas Vondra , Julien Rouhaud , pgsql-hackers@lists.postgresql.org Subject: Re: Extension security improvement: Add support for extensions with an owned schema Date: Tue, 22 Sep 2026 17:58:59 -0300 Message-ID: <179011073934.1743297.13790335223697741830@gmail.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4463216974647021584==" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --===============4463216974647021584== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Hello Jelte, everyone, v11 applies cleanly to master (09a579abaca) and CI is green, so I went looking at the one part that CI cannot reach. In June 2024 you wrote: > I also think I changed the pg_upgrade to do the correct thing, but I'm > not sure how to test this (even manually). Because part of it would > only be relevant once we support upgrading from PG18. So for now the > upgrade_code I haven't actually run. That is still true, and unfortunately that code does not work. It is easy to run now: pg_upgrade invokes the NEW pg_dump against the OLD server, so all it takes is a real PG18 cluster and the patched binary. Everything below was measured on master 09a579abaca with clean builds (--enable-cassert), against a PG18.6 cluster built from REL_18_STABLE, with unpatched master as the control. Scripts attached. 1. pg_dump against any older server fails ------------------------------------------ getExtensions() asks: if (fout->remoteVersion >= 180000) appendPQExpBufferStr(query, ", x.extownedschema "); The patch was written when 18 was the development target. master is 20devel now, so the column would appear in 20, not in 18. Against an 18 or 19 server the query asks for a column that does not exist: pg_dump (master), PG18 server: ok pg_dump (v11), PG18 server: ERROR: column x.extownedschema does not exist pg_dumpall (v11), PG18 server: same error pg_upgrade PG18 -> v11: Failure, exiting This is not only pg_upgrade. getExtensions() runs for every pg_dump, so dumping an older server with new binaries - the normal way to move between major versions - fails too. The one-line change is attached as nocfbot-fix-remoteVersion.diff.txt (180000 -> 200000). With it, on the same clusters: pg_dump (fixed), PG18 server: ok, same output as master except for the random \restrict token pg_upgrade PG18 -> fixed: rc=0 It might be worth a comment there, since this number has to be bumped again every time the patch misses a release. 2. Every pg_dump of a database with an owned-schema extension warns -------------------------------------------------------------------- pg_dump: warning: could not resolve dependency loop among these items: pg_dump: detail: EXTENSION loopdemo_owned (ID 2 OID 16386) pg_dump: detail: SCHEMA loopdemo_owned (ID 8 OID 16385) Control: the same trivial extension installed twice, where the only difference is owned_schema in the control file. owned_schema = true -> warning on every dump (plain and --binary-upgrade) owned_schema = false -> no warning The dump still restores correctly, because pg_dump breaks the loop on its own, but it is a loop it knows nothing about, and users of the feature would see this on every single dump. The loop is real: the extension is in the schema, and the schema is a member of the extension. pg_dump_sort.c already repairs the loops it knows (type/function, view/rule), so the second attached diff adds this case: drop the extension's dependency on the schema, since CREATE EXTENSION is what creates the schema. That is the direction that can go, and it leaves the schema-is-a-member dependency intact. With that diff the warning is gone, and make check, src/test/modules, src/bin/pg_dump and src/bin/pg_upgrade all still pass. 3. test_pg_dump cannot be run on its own any more -------------------------------------------------- make -C src/test/modules/test_pg_dump check master: PASS (1044 tests) v11: FAIL - extension "test_ext_owned_schema" is not available The new case in test_pg_dump/t/001_base.pl installs an extension that lives in src/test/modules/test_extensions, and nothing makes that module available to this one. Running the whole src/test/modules directory passes, which is presumably why CI never noticed. 4. What I checked that does work --------------------------------- Same-version pg_upgrade of a cluster with an owned-schema extension installed keeps the catalog right (extownedschema still true, schema still the extension's own), the --binary-upgrade dump emits the expected binary_upgrade_create_empty_extension(..., true, ...) and no separate CREATE SCHEMA, and a plain dump/restore round trip works. make check, src/test/modules and src/bin/pg_dump pass with v11 as is. I have not reviewed the security model itself; the thread has already covered that ground at length and I did not want to reopen it. These are only the things I could measure. Regards, Manu --===============4463216974647021584== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nocfbot-fix-remoteVersion.diff.txt" MIME-Version: 1.0 diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 72ed8fc..4492479 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -6222,7 +6222,7 @@ getExtensions(Archive *fout, int *numExtensions) "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x= .extcondition " ); =20 - if (fout->remoteVersion >=3D 180000) + if (fout->remoteVersion >=3D 200000) appendPQExpBufferStr(query, ", x.extownedschema "); else appendPQExpBufferStr(query, ", false AS extownedschema "); --===============4463216974647021584== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nocfbot-fix-dependency-loop.diff.txt" MIME-Version: 1.0 diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 4f3469d..b10ec6b 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -174,6 +174,8 @@ static int findLoop(DumpableObject *obj, DumpId *searchFailed, DumpableObject **workspace, int depth); +static void repairExtensionOwnedSchemaLoop(DumpableObject *extobj, + DumpableObject *nspobj); static void repairDependencyLoop(DumpableObject **loop, int nLoop); static void describeDumpableObject(DumpableObject *obj, @@ -928,6 +930,24 @@ findLoop(DumpableObject *obj, return 0; } =20 +/* + * An extension whose control file sets owned_schema=3Dtrue has a dependency + * loop with its schema: the extension is in the schema, and the schema is a + * member of the extension. Break the loop by dropping the extension's + * dependency on the schema, since CREATE EXTENSION creates the schema. + */ +static void +repairExtensionOwnedSchemaLoop(DumpableObject *extobj, DumpableObject *nspob= j) +{ + /* + * An extension with owned_schema=3Dtrue creates its schema itself, as part + * of CREATE EXTENSION, so the extension need not wait for the schema to + * be created first. (The schema is a member of the extension, hence the + * dependency in the other direction, which is the one we keep.) + */ + removeObjectDependency(extobj, nspobj->dumpId); +} + /* * A user-defined datatype will have a dependency loop with each of its * I/O functions (since those have the datatype as input or output). @@ -1191,6 +1211,22 @@ repairDependencyLoop(DumpableObject **loop, return; } =20 + /* Extension and the schema that the extension itself creates */ + if (nLoop =3D=3D 2 && + loop[0]->objType =3D=3D DO_EXTENSION && + loop[1]->objType =3D=3D DO_NAMESPACE) + { + repairExtensionOwnedSchemaLoop(loop[0], loop[1]); + return; + } + if (nLoop =3D=3D 2 && + loop[1]->objType =3D=3D DO_EXTENSION && + loop[0]->objType =3D=3D DO_NAMESPACE) + { + repairExtensionOwnedSchemaLoop(loop[1], loop[0]); + return; + } + /* View (including matview) and its ON SELECT rule */ if (nLoop =3D=3D 2 && loop[0]->objType =3D=3D DO_TABLE && --===============4463216974647021584== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="upgrade_18.sh.txt" MIME-Version: 1.0 #!/usr/bin/env bash # #5018: el camino de pg_upgrade que el autor dijo (2024-06-19) no haber # ejecutado nunca: "I'm not sure how to test this (even manually) ... the # upgrade code I haven't actually run." # # pg_upgrade corre el pg_dump NUEVO contra el servidor VIEJO. Asi que basta # un cluster PG18 real y el pg_dump del build parchado. # # Control: el mismo pg_dump de master (sin el patch) contra el mismo cluster. set -u BASE=3D$HOME/pg5018 D18=3D$BASE/data18 P18=3D55418 rm -rf "$D18" "$BASE/i-rel18/bin/initdb" -D "$D18" -U postgres --no-sync -A trust > "$BASE/= initdb18.log" 2>&1 \ || { echo "initdb 18 FALLO"; tail -5 "$BASE/initdb18.log"; exit 1; } "$BASE/i-rel18/bin/pg_ctl" -D "$D18" -o "-p $P18" -l "$BASE/pg18.log" -w star= t > /dev/null 2>&1 \ || { echo "start 18 FALLO"; tail -5 "$BASE/pg18.log"; exit 1; } echo "=3D=3D servidor viejo: $("$BASE/i-rel18/bin/psql" -p $P18 -U postgres -= tAc 'select version()' | cut -c1-40)" # Una extension cualquiera, de las que trae el propio arbol. "$BASE/i-rel18/bin/psql" -p $P18 -U postgres -qc 'CREATE EXTENSION IF NOT EXI= STS plpgsql' 2>/dev/null "$BASE/i-rel18/bin/psql" -p $P18 -U postgres -tAc \ "select extname from pg_extension order by 1" | sed 's/^/ extension: /' for w in master v11; do echo "=3D=3D pg_dump --binary-upgrade del build '$w' contra el servidor PG1= 8" if "$BASE/i-$w/bin/pg_dump" -p $P18 -U postgres --binary-upgrade -s -d post= gres \ > "$BASE/dump_${w}_desde18.sql" 2> "$BASE/dump_${w}_desde18.err"; then echo " OK ($(wc -l < "$BASE/dump_${w}_desde18.sql") lineas)" else echo " FALLA:" sed 's/^/ /' "$BASE/dump_${w}_desde18.err" | head -5 fi done "$BASE/i-rel18/bin/pg_ctl" -D "$D18" -w stop > /dev/null 2>&1 echo "=3D=3D FIN" --===============4463216974647021584== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="control_loop.sh.txt" MIME-Version: 1.0 #!/usr/bin/env bash # Control limpio del warning "could not resolve dependency loop": # la MISMA extension trivial, dos veces, y lo unico que cambia entre las dos # es owned_schema =3D true|false en el control file. set -u B=3D$HOME/pg5018/i-fix EXT=3D$B/share/postgresql/extension D=3D$HOME/pg5018/data_ctl P=3D55422 for n in loopdemo_owned loopdemo_plain; do cat > "$EXT/$n--1.0.sql" < "$EXT/loopdemo_owned.control" <<'EOF' comment =3D 'demo con schema propio' default_version =3D '1.0' relocatable =3D false owned_schema =3D true schema =3D 'loopdemo_owned' EOF cat > "$EXT/loopdemo_plain.control" <<'EOF' comment =3D 'demo sin schema propio' default_version =3D '1.0' relocatable =3D false schema =3D 'loopdemo_plain' EOF rm -rf "$D"; "$B/bin/initdb" -D "$D" -U postgres --no-sync -A trust > /dev/nu= ll 2>&1 "$B/bin/pg_ctl" -D "$D" -o "-p $P" -l "$HOME/pg5018/ctl.log" -w start > /dev/= null 2>&1 for n in loopdemo_owned loopdemo_plain; do "$B/bin/psql" -p $P -U postgres -qc "CREATE DATABASE d_$n" 2>/dev/null # la extension "plain" necesita su schema creado a mano [ "$n" =3D loopdemo_plain ] && "$B/bin/psql" -p $P -U postgres -d "d_$n" -q= c "CREATE SCHEMA $n" 2>/dev/null if ! "$B/bin/psql" -p $P -U postgres -d "d_$n" -qc "CREATE EXTENSION $n" 2>= "/tmp/ce_$n.err"; then echo " $n: no se pudo crear -> $(head -1 "/tmp/ce_$n.err")"; continue fi "$B/bin/pg_dump" -p $P -U postgres -d "d_$n" > "/tmp/dump_$n.sql" 2> "/tmp/= dump_$n.err" printf " %-16s owned_schema=3D%-5s -> " "$n" "$([ "$n" =3D loopdemo_owned= ] && echo true || echo false)" if [ -s "/tmp/dump_$n.err" ]; then echo "WARNING"; sed 's/^/ /' "/tmp/dump_$n.err" | head -4 else echo "sin warnings" fi done "$B/bin/pg_ctl" -D "$D" -w stop > /dev/null 2>&1 rm -f "$EXT"/loopdemo_* echo "=3D=3D FIN" --===============4463216974647021584==--