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 1lR4et-00028N-Cq for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Mar 2021 03:03:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lR4er-0003DC-Tl for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Mar 2021 03:03:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lR4eq-0003Ak-JX for pgsql-hackers@lists.postgresql.org; Tue, 30 Mar 2021 03:03:09 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lR4em-0001mW-WA for pgsql-hackers@lists.postgresql.org; Tue, 30 Mar 2021 03:03:08 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 22622128D; Mon, 29 Mar 2021 23:02:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Mon, 29 Mar 2021 23:02:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=sbruwCaCSjBH/AV1pxunNCmAz6C 9VxbZRWL/EaM+ilU=; b=osilEUsOaq+6DGGfMxs5qKdw5CTsl4za+isa5Oim+sU jPzZcoWkTZJ2bfo8KwQxR8In9HqtcvctEmKo0+luHQOAUsEyeQE1PDE1jev3QuwU KssZDytFjDT/C1SETVEKHAAhITIArbW5CpCjer5/p9vaigK5GqhTXxrxX0n4NX9r QGePX1o0Z31v5roAVuN0ad/AHQfSVXwxsxwOmt8nRHpSk5IFnS3sZDoYQPopi/60 T8a5LSNWbpk//4Rdn15xV2UebmFaDrzgAjK4wFs0enGk/rdembj5Ukf9/PKJs3t2 QPAJcrg0ETOdC0OPuOjtmnacvrWi0RjGK1kDB1UV7kA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=sbruwC aCSjBH/AV1pxunNCmAz6C9VxbZRWL/EaM+ilU=; b=XJfmoJEdGxR7787TgxrzgP YQaUPDWcOip3XTigAzbKCZW4Rb5MjZQInLqtNWkeyT1uj1pk/j1WMAImoTRgJce5 ZY2uumXC8+17vR2hsMQVaZXgZFgduP4bb0Qa4+pr7lzRj+dHDv25FhrzuE2+nTYL Orz3oY2CA03jVDUoTXnk2r/7X9NpkPJ96PtN+7M+Nh+0K/YcZEM249KuHAmDuV/N 57OokVo6ZZDEn4vKls9arooZ/PCchUWn2VhtAz3ZNKXsmeCgs5I6gg5G1sXsTQOQ N2bS3Q0q04j5KAcw5qJGU1FbnELvIFoGyFws87qSd8s5yVPXdzHBhH3OES8NTczQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudehledgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpedvgeduuefhtdeuleettdevjeeh heeiveeuieegleetgeeljeelieeuieehgeevhfenucfkphepudduuddruddtvddrudektd drudekheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhm pehmihgthhgrvghlsehprghquhhivghrrdighiii X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id D65E824005A; Mon, 29 Mar 2021 23:02:48 -0400 (EDT) Date: Tue, 30 Mar 2021 12:02:45 +0900 From: Michael Paquier To: Guillaume Lelarge Cc: Julien Rouhaud , Tom Lane , Daniel Gustafsson , PostgreSQL Hackers Subject: Re: Extensions not dumped when --schema is used Message-ID: References: <3197673.1613838700@sss.pgh.pa.us> <20210315102155.qgp32wo6tcuu6w4l@nol> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="JfiLQFQiutbvVMJg" Content-Disposition: inline In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --JfiLQFQiutbvVMJg Content-Type: multipart/mixed; boundary="CGRpL4+syC4YUDF4" Content-Disposition: inline --CGRpL4+syC4YUDF4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Mar 15, 2021 at 11:37:02AM +0100, Guillaume Lelarge wrote: > Anyways. Yeah, I know we're near feature freeze. This feature would be nice > to have, but I don't feel strongly about it. I think this feature is > currently lacking in PostgreSQL but I don't much care if it makes it to 14 > or any future release. If you have time to work on the pg_dump test suite > and are interested, then sure, go ahead, I'm fine with this. Otherwise I'll > do it in a few weeks and if it means it'll land in v15, then be it. That's > not an issue in itself. Okay. So I have looked at that stuff in details, and after fixing all the issues reported upthread in the code, docs and tests I am finishing with the attached. The tests have been moved out of src/bin/pg_dump/ to src/test/modules/test_pg_dump/, and include both positive and negative tests (used the trick with plpgsql for the latter to avoid the dump of the extension test_pg_dump or any data related to it). -- Michael --CGRpL4+syC4YUDF4 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Add-support-for-extension-in-pg_dump.patch" Content-Transfer-Encoding: quoted-printable =46rom ca1388a4c4441f15c02d332b8a9996408a90662c Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Tue, 30 Mar 2021 12:01:13 +0900 Subject: [PATCH] Add support for --extension in pg_dump --- src/bin/pg_dump/pg_dump.c | 93 +++++++++++++++++++-- src/test/modules/test_pg_dump/t/001_base.pl | 53 ++++++++---- doc/src/sgml/ref/pg_dump.sgml | 43 ++++++++-- 3 files changed, 161 insertions(+), 28 deletions(-) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index da6cc054b0..382d33f2c6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -123,6 +123,9 @@ static SimpleOidList tabledata_exclude_oids =3D {NULL, = NULL}; static SimpleStringList foreign_servers_include_patterns =3D {NULL, NULL}; static SimpleOidList foreign_servers_include_oids =3D {NULL, NULL}; =20 +static SimpleStringList extension_include_patterns =3D {NULL, NULL}; +static SimpleOidList extension_include_oids =3D {NULL, NULL}; + static const CatalogId nilCatalogId =3D {0, 0}; =20 /* override for standard extra_float_digits setting */ @@ -151,6 +154,10 @@ static void expand_schema_name_patterns(Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names); +static void expand_extension_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names); static void expand_foreign_server_name_patterns(Archive *fout, SimpleStringList *patterns, SimpleOidList *oids); @@ -335,6 +342,7 @@ main(int argc, char **argv) {"clean", no_argument, NULL, 'c'}, {"create", no_argument, NULL, 'C'}, {"dbname", required_argument, NULL, 'd'}, + {"extension", required_argument, NULL, 'e'}, {"file", required_argument, NULL, 'f'}, {"format", required_argument, NULL, 'F'}, {"host", required_argument, NULL, 'h'}, @@ -426,7 +434,7 @@ main(int argc, char **argv) =20 InitDumpOptions(&dopt); =20 - while ((c =3D getopt_long(argc, argv, "abBcCd:E:f:F:h:j:n:N:Op:RsS:t:T:U:= vwWxZ:", + while ((c =3D getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:= U:vwWxZ:", long_options, &optindex)) !=3D -1) { switch (c) @@ -455,6 +463,11 @@ main(int argc, char **argv) dopt.cparams.dbname =3D pg_strdup(optarg); break; =20 + case 'e': /* include extension(s) */ + simple_string_list_append(&extension_include_patterns, optarg); + dopt.include_everything =3D false; + break; + case 'E': /* Dump encoding */ dumpencoding =3D pg_strdup(optarg); break; @@ -834,6 +847,16 @@ main(int argc, char **argv) =20 /* non-matching exclusion patterns aren't an error */ =20 + /* Expand extension selection patterns into OID lists */ + if (extension_include_patterns.head !=3D NULL) + { + expand_extension_name_patterns(fout, &extension_include_patterns, + &extension_include_oids, + strict_names); + if (extension_include_oids.head =3D=3D NULL) + fatal("no matching extensions were found"); + } + /* * Dumping blobs is the default for dumps where an inclusion switch is not * used (an "include everything" dump). -B can be used to exclude blobs @@ -1025,6 +1048,7 @@ help(const char *progname) printf(_(" -B, --no-blobs exclude large objects in dump\n"= )); printf(_(" -c, --clean clean (drop) database objects be= fore recreating\n")); printf(_(" -C, --create include commands to create datab= ase in dump\n")); + printf(_(" -e, --extension=3DPATTERN dump the specified extension(s= ) only\n")); printf(_(" -E, --encoding=3DENCODING dump the data in encoding ENCO= DING\n")); printf(_(" -n, --schema=3DPATTERN dump the specified schema(s) o= nly\n")); printf(_(" -N, --exclude-schema=3DPATTERN do NOT dump the specified sche= ma(s)\n")); @@ -1367,6 +1391,53 @@ expand_schema_name_patterns(Archive *fout, destroyPQExpBuffer(query); } =20 +/* + * Find the OIDs of all extensions matching the given list of patterns, + * and append them to the given OID list. + */ +static void +expand_extension_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names) +{ + PQExpBuffer query; + PGresult *res; + SimpleStringListCell *cell; + int i; + + if (patterns->head =3D=3D NULL) + return; /* nothing to do */ + + query =3D createPQExpBuffer(); + + /* + * The loop below runs multiple SELECTs might sometimes result in + * duplicate entries in the OID list, but we don't care. + */ + for (cell =3D patterns->head; cell; cell =3D cell->next) + { + appendPQExpBufferStr(query, + "SELECT oid FROM pg_catalog.pg_extension e\n"); + processSQLNamePattern(GetConnection(fout), query, cell->val, false, + false, NULL, "e.extname", NULL, NULL); + + res =3D ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + if (strict_names && PQntuples(res) =3D=3D 0) + fatal("no matching extensions were found for pattern \"%s\"", cell->val= ); + + for (i =3D 0; i < PQntuples(res); i++) + { + simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + } + + PQclear(res); + resetPQExpBuffer(query); + } + + destroyPQExpBuffer(query); +} + /* * Find the OIDs of all foreign servers matching the given list of pattern= s, * and append them to the given OID list. @@ -1793,8 +1864,9 @@ selectDumpableAccessMethod(AccessMethodInfo *method, = Archive *fout) * Built-in extensions should be skipped except for checking ACLs, since we * assume those will already be installed in the target database. We iden= tify * such extensions by their having OIDs in the range reserved for initdb. - * We dump all user-added extensions by default, or none of them if - * include_everything is false (i.e., a --schema or --table switch was giv= en). + * We dump all user-added extensions by default. No extensions are dumped + * if include_everything is false (i.e., a --schema or --table switch was + * given), except if --extension specifies a list of extensions to dump. */ static void selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt) @@ -1807,9 +1879,18 @@ selectDumpableExtension(ExtensionInfo *extinfo, Dump= Options *dopt) if (extinfo->dobj.catId.oid <=3D (Oid) g_last_builtin_oid) extinfo->dobj.dump =3D extinfo->dobj.dump_contains =3D DUMP_COMPONENT_AC= L; else - extinfo->dobj.dump =3D extinfo->dobj.dump_contains =3D - dopt->include_everything ? DUMP_COMPONENT_ALL : - DUMP_COMPONENT_NONE; + { + /* check if there is a list of extensions to dump */ + if (extension_include_oids.head !=3D NULL) + extinfo->dobj.dump =3D extinfo->dobj.dump_contains =3D + simple_oid_list_member(&extension_include_oids, + extinfo->dobj.catId.oid) ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + else + extinfo->dobj.dump =3D extinfo->dobj.dump_contains =3D + dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + } } =20 /* diff --git a/src/test/modules/test_pg_dump/t/001_base.pl b/src/test/modules= /test_pg_dump/t/001_base.pl index 501aff0920..7c053c4e49 100644 --- a/src/test/modules/test_pg_dump/t/001_base.pl +++ b/src/test/modules/test_pg_dump/t/001_base.pl @@ -194,6 +194,20 @@ my %pgdump_runs =3D ( 'pg_dump', '--no-sync', "--file=3D$tempdir/section_post_data.sql", '--section=3Dpost-data', 'postgres', ], + }, + with_extension =3D> { + dump_cmd =3D> [ + 'pg_dump', '--no-sync', "--file=3D$tempdir/with_extension.sql", + '--extension=3Dtest_pg_dump', '--no-sync', 'postgres', + ], + }, + + # plgsql in the list blocks the dump of extension test_pg_dump + without_extension =3D> { + dump_cmd =3D> [ + 'pg_dump', '--no-sync', "--file=3D$tempdir/without_extension.sql", + '--extension=3Dplpgsql', '--no-sync', 'postgres', + ], },); =20 ############################################################### @@ -228,14 +242,16 @@ my %pgdump_runs =3D ( # Tests which are considered 'full' dumps by pg_dump, but there # are flags used to exclude specific items (ACLs, blobs, etc). my %full_runs =3D ( - binary_upgrade =3D> 1, - clean =3D> 1, - clean_if_exists =3D> 1, - createdb =3D> 1, - defaults =3D> 1, - exclude_table =3D> 1, - no_privs =3D> 1, - no_owner =3D> 1,); + binary_upgrade =3D> 1, + clean =3D> 1, + clean_if_exists =3D> 1, + createdb =3D> 1, + defaults =3D> 1, + exclude_table =3D> 1, + no_privs =3D> 1, + no_owner =3D> 1, + with_extension =3D> 1, + without_extension =3D> 1); =20 my %tests =3D ( 'ALTER EXTENSION test_pg_dump' =3D> { @@ -261,7 +277,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { binary_upgrade =3D> 1, }, + unlike =3D> { binary_upgrade =3D> 1, without_extension =3D> 1 }, }, =20 'CREATE ROLE regress_dump_test_role' =3D> { @@ -320,6 +336,7 @@ my %tests =3D ( section_data =3D> 1, extension_schema =3D> 1, }, + unlike =3D> { without_extension =3D> 1, }, }, =20 'CREATE TABLE regress_pg_dump_table' =3D> { @@ -343,8 +360,9 @@ my %tests =3D ( extension_schema =3D> 1, }, unlike =3D> { - binary_upgrade =3D> 1, - exclude_table =3D> 1, + binary_upgrade =3D> 1, + exclude_table =3D> 1, + without_extension =3D> 1, }, }, =20 @@ -367,7 +385,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1, }, }, =20 'REVOKE GRANT OPTION FOR UPDATE ON SEQUENCE wgo_then_regular' =3D> { @@ -384,7 +402,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1, }, }, =20 'CREATE ACCESS METHOD regress_test_am' =3D> { @@ -404,6 +422,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, + unlike =3D> { without_extension =3D> 1, }, }, =20 'GRANT SELECT regress_pg_dump_table_added pre-ALTER EXTENSION' =3D> { @@ -428,7 +447,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1, }, }, =20 'GRANT SELECT ON TABLE regress_pg_dump_table' =3D> { @@ -462,7 +481,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1 }, }, =20 'GRANT USAGE ON regress_pg_dump_table_col1_seq TO regress_dump_test_role' @@ -478,7 +497,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1, }, }, =20 'GRANT USAGE ON regress_pg_dump_seq TO regress_dump_test_role' =3D> { @@ -500,7 +519,7 @@ my %tests =3D ( schema_only =3D> 1, section_pre_data =3D> 1, }, - unlike =3D> { no_privs =3D> 1, }, + unlike =3D> { no_privs =3D> 1, without_extension =3D> 1, }, }, =20 # Objects included in extension part of a schema created by this extensio= n */ diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 989b8e2381..529b167c96 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -215,6 +215,38 @@ PostgreSQL documentation =20 + + + + + + Dump only extensions matching pattern. When this option is not + specified, all non-system extensions in the target database will be + dumped. Multiple extensions can be selected by writing multiple + switches. The pattern parameter is interpreted= as a + pattern according to the same rules used by + psql's \d commands (= see + ), so multiple extensions can= also + be selected by writing wildcard characters in the pattern. When u= sing + wildcards, be careful to quote the pattern if needed to prevent the + shell from expanding the wildcards. + + + + + When is specified, + pg_dump makes no attempt to dump any o= ther + database objects that the selected extension(s) might depend upon. + Therefore, there is no guarantee that the results of a + specific-extension dump can be successfully restored by themselves + into a clean database. + + + + + @@ -1079,11 +1111,12 @@ PostgreSQL documentation - Require that each schema - (/) and table - (/) qualifier match at - least one schema/table in the database to be dumped. Note that if - none of the schema/table qualifiers find + Require that each + extension (/), + schema (/) and + table (/) qualifier + match at least one extension/schema/table in the database to be du= mped. + Note that if none of the extension/schema/table qualifiers find matches, pg_dump will generate an error even without . --=20 2.31.0 --CGRpL4+syC4YUDF4-- --JfiLQFQiutbvVMJg Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmBilNUACgkQnvQgOdby QH27xw//TGcL8SuouNzhOpqyEX4el5fjMb7g/LsOZ8Iv9rSV14doJCIo6z3yd6Z7 R+eR1CYxCaFJn8EUs23sxh4OUc/lr/m0aUeNCKCmXUh7LiPNhvjv4DUjNYL10lEX 6365fPKhYFiv9McXQM6UPwSgHyAo8hwb/bTkNf+eK+7axvGsKOuQYBLTE+hTbBlb oI3W92HWUOjBAhmJWmOnEGz1nt+0tZOSHvTREm8jOokFCZpI+Zq6dq5w+nNLUMBD amh5ufb4zYqIBd8+GunbYXMWda8FQPs/8kRMX7/geZoP1l8F80SOrF9YvM8pZs3x BgCBsdXZFfG64wu5qCBm2H4Eia0C19+q7pcpAXAUf9eyjMY7fq9+2snZvaSjV8G2 59W1XzDw7X9NHJYQBXsKMhAgP0Nl4mIje++bsZ2xIcWWUokF8HBQq8QcHe0Xm/GI rlqu6BmwNvEtybrMuB6m7XZwyFwfhG6RT/Qyrk16XP5aMddgyiEHmiKtwN8LbKdC LSK+7GcuwwIlISrPQe11bqMBez/hP/tJhMc4tTqwQxRHgTk/rMulgMKMpXFbOY7G v4X1GKlmPkg+pXma4cqZzsgzCvDR6EahT0r2BJzeLHodBSCjA/00MKw3CSvWVJ+I EI0Z5I9Pji9drW1H+FwfTlp5h63LcYek02woaLBeraSA5riQHXI= =P8EF -----END PGP SIGNATURE----- --JfiLQFQiutbvVMJg--