agora inbox for pgsql-hackers@postgresql.orghelp / color / mirror / Atom feed
[PATCH] Allow wildcard (%) in extension upgrade paths 14+ messages / 2 participants [nested] [flat]
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 14 +++++ src/backend/commands/extension.c | 58 +++++++++++++++++-- src/test/modules/test_extensions/Makefile | 6 +- .../expected/test_extensions.out | 15 +++++ .../test_extensions/sql/test_extensions.sql | 7 +++ .../test_ext_wildcard1--%--2.0.sql | 6 ++ .../test_ext_wildcard1--1.0.sql | 6 ++ .../test_ext_wildcard1.control | 4 ++ 8 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..4012652574 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -807,6 +807,20 @@ RETURNS anycompatible AS ... </para> </listitem> </varlistentry> + + <varlistentry> + <term><varname>wildcard_upgrades</varname> (<type>boolean</type>)</term> + <listitem> + <para> + This parameter, if set to <literal>true</literal> (which is not the + default), allows <command>ALTER EXTENSION</command> to consider + a wildcard character <literal>%</literal> as matching any version of + the extension. Such wildcard match will only be used when no + perfect match is found for a version. + </para> + </listitem> + </varlistentry> + </variablelist> <para> diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..ea8825fcff 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -86,6 +86,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ } ExtensionControlFile; @@ -128,6 +129,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -579,6 +581,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -636,6 +646,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -890,7 +901,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1215,13 +1234,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3421,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..865e37fa88 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,4 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true +wildcard_upgrades = true -- 2.34.1 --sbf3wqvs4ajivmh2-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v3] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 14 +++++ src/backend/commands/extension.c | 58 +++++++++++++++++-- src/test/modules/test_extensions/Makefile | 7 ++- .../expected/test_extensions.out | 15 +++++ .../test_extensions/sql/test_extensions.sql | 9 +++ .../test_ext_wildcard1--%--2.0.sql | 6 ++ .../test_ext_wildcard1--1.0.sql | 6 ++ .../test_ext_wildcard1.control | 4 ++ 8 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 218940ee5c..3d4003eaef 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -822,6 +822,20 @@ RETURNS anycompatible AS ... </para> </listitem> </varlistentry> + + <varlistentry id="extend-extensions-wildcard-upgrade"> + <term><varname>wildcard_upgrades</varname> (<type>boolean</type>)</term> + <listitem> + <para> + This parameter, if set to <literal>true</literal> (which is not the + default), allows <command>ALTER EXTENSION</command> to consider + a wildcard character <literal>%</literal> as matching any version of + the extension. Such wildcard match will only be used when no + perfect match is found for a version. + </para> + </listitem> + </varlistentry> + </variablelist> <para> diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 0eabe18335..207b4649f2 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -88,6 +88,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ List *no_relocate; /* names of prerequisite extensions that @@ -132,6 +133,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -584,6 +586,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -656,6 +666,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -913,7 +924,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1259,13 +1278,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3470,3 +3499,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index 70fc0c8e66..5a8205fd5d 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -7,8 +7,8 @@ EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ test_ext_evttrig \ - test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 - + test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 \ + test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ @@ -18,7 +18,8 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ test_ext_req_schema1--1.0.sql \ test_ext_req_schema2--1.0.sql \ - test_ext_req_schema3--1.0.sql + test_ext_req_schema3--1.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index a31775a260..0d4d8b4b70 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -389,3 +389,18 @@ SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; NOTICE: drop cascades to extension test_ext_req_schema2 +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index f4947e7da6..3c40710fc1 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -232,3 +232,12 @@ ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_s_dep2; -- now ok SELECT test_s_dep2.dep_req1(); SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; + +-- +-- Test wildcard based upgrade paths +-- +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..865e37fa88 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,4 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true +wildcard_upgrades = true -- 2.34.1 --twb4kgisshvw5z4p-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 6 ++- .../expected/test_extensions.out | 15 +++++++ .../test_extensions/sql/test_extensions.sql | 7 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 8 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..c79140f669 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1081,6 +1081,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..e3ea9dba30 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -128,6 +128,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -890,7 +891,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1214,14 +1222,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3405,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --pikujl27r76rlaub-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v2] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 7 ++-- .../expected/test_extensions.out | 16 +++++++ src/test/modules/test_extensions/meson.build | 3 ++ .../test_extensions/sql/test_extensions.sql | 9 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 9 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 218940ee5c..bdd463b81f 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1120,6 +1120,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 0eabe18335..36b6d7e01a 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -132,6 +132,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -913,7 +914,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1258,14 +1266,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3470,3 +3483,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index 70fc0c8e66..beec04eea3 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -7,8 +7,8 @@ EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ test_ext_evttrig \ - test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 - + test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 \ + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ @@ -18,7 +18,8 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ test_ext_req_schema1--1.0.sql \ test_ext_req_schema2--1.0.sql \ - test_ext_req_schema3--1.0.sql + test_ext_req_schema3--1.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index a31775a260..790b9b9368 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -389,3 +389,19 @@ SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; NOTICE: drop cascades to extension test_ext_req_schema2 + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/meson.build b/src/test/modules/test_extensions/meson.build index 29e5bb2fb5..7b14d65545 100644 --- a/src/test/modules/test_extensions/meson.build +++ b/src/test/modules/test_extensions/meson.build @@ -36,6 +36,9 @@ test_install_data += files( 'test_ext_req_schema2.control', 'test_ext_req_schema3--1.0.sql', 'test_ext_req_schema3.control', + 'test_ext_wildcard1--1.0.sql', + 'test_ext_wildcard1--%--2.0.sql', + 'test_ext_wildcard1.control', ) tests += { diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index f4947e7da6..676face363 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -232,3 +232,12 @@ ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_s_dep2; -- now ok SELECT test_s_dep2.dep_req1(); SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; + +-- +-- Test wildcard upgrade +-- +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --nzgxg63vlhpg7nhu-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 6 ++- .../expected/test_extensions.out | 15 +++++++ src/test/modules/test_extensions/meson.build | 3 ++ .../test_extensions/sql/test_extensions.sql | 7 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 9 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..c79140f669 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1081,6 +1081,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..e3ea9dba30 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -128,6 +128,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -890,7 +891,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1214,14 +1222,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3405,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/meson.build b/src/test/modules/test_extensions/meson.build index e95a9f2e7e..79d90b34c1 100644 --- a/src/test/modules/test_extensions/meson.build +++ b/src/test/modules/test_extensions/meson.build @@ -29,6 +29,9 @@ install_data( 'test_ext_evttrig--1.0--2.0.sql', 'test_ext_evttrig--1.0.sql', 'test_ext_evttrig.control', + 'test_ext_wildcard1--1.0.sql', + 'test_ext_wildcard1--%--2.0.sql', + 'test_ext_wildcard1.control', kwargs: contrib_data_args, ) diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --flyqbkgig2a5xh3s-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v2] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. --- src/backend/commands/extension.c | 58 ++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 6b6720c690..e36a79ae75 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -86,6 +86,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ } ExtensionControlFile; @@ -128,6 +129,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -579,6 +581,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -636,6 +646,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -890,7 +901,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1215,13 +1234,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3421,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + AssertArg(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} -- 2.34.1 --339zka8KxudZ+6RE-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. --- src/backend/commands/extension.c | 58 ++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..ea8825fcff 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -86,6 +86,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ } ExtensionControlFile; @@ -128,6 +129,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -579,6 +581,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -636,6 +646,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -890,7 +901,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1215,13 +1234,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3421,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} -- 2.34.1 --jbsevty42g3gdjf3-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v1] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 6 ++- .../expected/test_extensions.out | 15 +++++++ src/test/modules/test_extensions/meson.build | 3 ++ .../test_extensions/sql/test_extensions.sql | 7 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 9 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index b70cbe83ae..f1f0ae1244 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1081,6 +1081,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 02ff4a9a7f..6df0fd403a 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -130,6 +130,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -893,7 +894,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1217,14 +1225,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3395,3 +3408,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/meson.build b/src/test/modules/test_extensions/meson.build index c3af3e1721..026be6a879 100644 --- a/src/test/modules/test_extensions/meson.build +++ b/src/test/modules/test_extensions/meson.build @@ -30,6 +30,9 @@ test_install_data += files( 'test_ext_evttrig--1.0--2.0.sql', 'test_ext_evttrig--1.0.sql', 'test_ext_evttrig.control', + 'test_ext_wildcard1--1.0.sql', + 'test_ext_wildcard1--%--2.0.sql', + 'test_ext_wildcard1.control', ) tests += { diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --bhtumk242qxsvzi5-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 14 +++++ src/backend/commands/extension.c | 58 +++++++++++++++++-- src/test/modules/test_extensions/Makefile | 6 +- .../expected/test_extensions.out | 15 +++++ .../test_extensions/sql/test_extensions.sql | 7 +++ .../test_ext_wildcard1--%--2.0.sql | 6 ++ .../test_ext_wildcard1--1.0.sql | 6 ++ .../test_ext_wildcard1.control | 4 ++ 8 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..4012652574 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -807,6 +807,20 @@ RETURNS anycompatible AS ... </para> </listitem> </varlistentry> + + <varlistentry> + <term><varname>wildcard_upgrades</varname> (<type>boolean</type>)</term> + <listitem> + <para> + This parameter, if set to <literal>true</literal> (which is not the + default), allows <command>ALTER EXTENSION</command> to consider + a wildcard character <literal>%</literal> as matching any version of + the extension. Such wildcard match will only be used when no + perfect match is found for a version. + </para> + </listitem> + </varlistentry> + </variablelist> <para> diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..ea8825fcff 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -86,6 +86,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ } ExtensionControlFile; @@ -128,6 +129,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -579,6 +581,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -636,6 +646,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -890,7 +901,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1215,13 +1234,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3421,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..865e37fa88 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,4 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true +wildcard_upgrades = true -- 2.34.1 --sbf3wqvs4ajivmh2-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 6 ++- .../expected/test_extensions.out | 15 +++++++ .../test_extensions/sql/test_extensions.sql | 7 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 8 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..c79140f669 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1081,6 +1081,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..e3ea9dba30 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -128,6 +128,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -890,7 +891,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1214,14 +1222,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3405,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --pikujl27r76rlaub-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 8 ++++ src/backend/commands/extension.c | 42 ++++++++++++++++--- src/test/modules/test_extensions/Makefile | 6 ++- .../expected/test_extensions.out | 15 +++++++ src/test/modules/test_extensions/meson.build | 3 ++ .../test_extensions/sql/test_extensions.sql | 7 ++++ .../test_ext_wildcard1--%--2.0.sql | 6 +++ .../test_ext_wildcard1--1.0.sql | 6 +++ .../test_ext_wildcard1.control | 3 ++ 9 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 46e873a166..c79140f669 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1081,6 +1081,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr <literal>1.1</literal>). </para> + <para> + The literal value <literal>%</literal> can be used as the + <replaceable>old_version</replaceable> component in an extension + update script for it to match any version. Such wildcard update + scripts will only be used when no explicit path is found from + old to target version. + </para> + <para> Given that a suitable update script is available, the command <command>ALTER EXTENSION UPDATE</command> will update an installed extension diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..e3ea9dba30 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -128,6 +128,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -890,7 +891,14 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( ! file_exists(filename) ) + { + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1214,14 +1222,19 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + /* Find wildcard path, if no explicit path was found */ + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + if (result != NIL) + return result; - return result; + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3405,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index c3139ab0fc..4fe2d82b6e 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -6,14 +6,16 @@ PGFILEDESC = "test_extensions - regression testing for EXTENSION support" EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext7 test_ext8 test_ext_cine test_ext_cor \ test_ext_cyclic1 test_ext_cyclic2 \ - test_ext_evttrig + test_ext_evttrig test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \ test_ext_cor--1.0.sql \ test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \ - test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql + test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql \ + REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 821fed38d1..1c4dc5be42 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -312,3 +312,18 @@ Objects in extension "test_ext_cine" table ext_cine_tab3 (9 rows) +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/meson.build b/src/test/modules/test_extensions/meson.build index e95a9f2e7e..79d90b34c1 100644 --- a/src/test/modules/test_extensions/meson.build +++ b/src/test/modules/test_extensions/meson.build @@ -29,6 +29,9 @@ install_data( 'test_ext_evttrig--1.0--2.0.sql', 'test_ext_evttrig--1.0.sql', 'test_ext_evttrig.control', + 'test_ext_wildcard1--1.0.sql', + 'test_ext_wildcard1--%--2.0.sql', + 'test_ext_wildcard1.control', kwargs: contrib_data_args, ) diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 41b6cddf0b..071845e8df 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -209,3 +209,10 @@ CREATE EXTENSION test_ext_cine; ALTER EXTENSION test_ext_cine UPDATE TO '1.1'; \dx+ test_ext_cine + + +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..0c2fc6fca6 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,3 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true -- 2.34.1 --flyqbkgig2a5xh3s-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v4] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. Includes regression test and documentation. --- doc/src/sgml/extend.sgml | 14 +++++ src/backend/commands/extension.c | 58 +++++++++++++++++-- src/test/modules/test_extensions/Makefile | 7 ++- .../expected/test_extensions.out | 18 ++++++ .../test_extensions/sql/test_extensions.sql | 9 +++ .../test_ext_wildcard1--%--2.0.sql | 6 ++ .../test_ext_wildcard1--1.0.sql | 6 ++ .../test_ext_wildcard1.control | 4 ++ 8 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql create mode 100644 src/test/modules/test_extensions/test_ext_wildcard1.control diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 218940ee5c..3d4003eaef 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -822,6 +822,20 @@ RETURNS anycompatible AS ... </para> </listitem> </varlistentry> + + <varlistentry id="extend-extensions-wildcard-upgrade"> + <term><varname>wildcard_upgrades</varname> (<type>boolean</type>)</term> + <listitem> + <para> + This parameter, if set to <literal>true</literal> (which is not the + default), allows <command>ALTER EXTENSION</command> to consider + a wildcard character <literal>%</literal> as matching any version of + the extension. Such wildcard match will only be used when no + perfect match is found for a version. + </para> + </listitem> + </varlistentry> + </variablelist> <para> diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 535072d181..c05055f5a0 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -88,6 +88,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ List *no_relocate; /* names of prerequisite extensions that @@ -132,6 +133,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -584,6 +586,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -656,6 +666,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -913,7 +924,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1281,13 +1300,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3491,3 +3520,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile index 1388c0fb0b..105138d08a 100644 --- a/src/test/modules/test_extensions/Makefile +++ b/src/test/modules/test_extensions/Makefile @@ -8,8 +8,8 @@ EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \ test_ext_cyclic1 test_ext_cyclic2 \ test_ext_extschema \ test_ext_evttrig \ - test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 - + test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3 \ + test_ext_wildcard1 DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \ test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \ @@ -20,7 +20,8 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \ test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \ test_ext_req_schema1--1.0.sql \ test_ext_req_schema2--1.0.sql \ - test_ext_req_schema3--1.0.sql + test_ext_req_schema3--1.0.sql \ + test_ext_wildcard1--1.0.sql test_ext_wildcard1--%--2.0.sql REGRESS = test_extensions test_extdepend diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out index 472627a232..270840183d 100644 --- a/src/test/modules/test_extensions/expected/test_extensions.out +++ b/src/test/modules/test_extensions/expected/test_extensions.out @@ -445,3 +445,21 @@ SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; NOTICE: drop cascades to extension test_ext_req_schema2 +-- +-- Test wildcard based upgrade paths +-- +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 1.0 +(1 row) + +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); + ext_wildcard1_version +----------------------- + 2.0 +(1 row) + +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql index 51327cc321..bb567e0f19 100644 --- a/src/test/modules/test_extensions/sql/test_extensions.sql +++ b/src/test/modules/test_extensions/sql/test_extensions.sql @@ -276,3 +276,12 @@ ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_s_dep2; -- now ok SELECT test_s_dep2.dep_req1(); SELECT test_s_dep.dep_req2(); DROP EXTENSION test_ext_req_schema1 CASCADE; + +-- +-- Test wildcard based upgrade paths +-- +CREATE EXTENSION test_ext_wildcard1; +SELECT ext_wildcard1_version(); +ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'; +SELECT ext_wildcard1_version(); +DROP EXTENSION test_ext_wildcard1; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql new file mode 100644 index 0000000000..75154e5c55 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--%--2.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION test_ext_wildcard1 UPDATE TO '2.0'" to load this file. \quit + +CREATE OR REPLACE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 2.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql new file mode 100644 index 0000000000..a69e791fda --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql @@ -0,0 +1,6 @@ +/* src/test/modules/test_extensions/test_ext_wildcard1--1.0.sql */ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "CREATE EXTENSION test_ext_wildcard1" to load this file. \quit + +CREATE FUNCTION ext_wildcard1_version() returns TEXT +AS 'SELECT 1.0' LANGUAGE 'sql'; diff --git a/src/test/modules/test_extensions/test_ext_wildcard1.control b/src/test/modules/test_extensions/test_ext_wildcard1.control new file mode 100644 index 0000000000..865e37fa88 --- /dev/null +++ b/src/test/modules/test_extensions/test_ext_wildcard1.control @@ -0,0 +1,4 @@ +comment = 'Test extension wildcard 1' +default_version = '1.0' +relocatable = true +wildcard_upgrades = true -- 2.34.1 --6ay2r5v7k3ozwyik-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH] Allow wildcard (%) in extension upgrade paths @ 2022-09-14 09:10 Sandro Santilli <strk@kbt.io> 0 siblings, 0 replies; 14+ messages in thread From: Sandro Santilli @ 2022-09-14 09:10 UTC (permalink / raw) A wildcard character "%" will be accepted in the "source" side of the upgrade script and be considered usable to upgrade any version to the "target" side. Using wildcards needs to be explicitly requested by extensions via a "wildcard_upgrades" setting in their control file. --- src/backend/commands/extension.c | 58 ++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 1a62e5dac5..ea8825fcff 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -86,6 +86,7 @@ typedef struct ExtensionControlFile bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */ bool superuser; /* must be superuser to install? */ bool trusted; /* allow becoming superuser on the fly? */ + bool wildcard_upgrades; /* allow using wildcards in upgrade scripts */ int encoding; /* encoding of the script file, or -1 */ List *requires; /* names of prerequisite extensions */ } ExtensionControlFile; @@ -128,6 +129,7 @@ static void ApplyExtensionUpdates(Oid extensionOid, bool cascade, bool is_create); static char *read_whole_file(const char *filename, int *length); +static bool file_exists(const char *name); /* @@ -579,6 +581,14 @@ parse_extension_control_file(ExtensionControlFile *control, errmsg("parameter \"%s\" requires a Boolean value", item->name))); } + else if (strcmp(item->name, "wildcard_upgrades") == 0) + { + if (!parse_bool(item->value, &control->wildcard_upgrades)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a Boolean value", + item->name))); + } else if (strcmp(item->name, "encoding") == 0) { control->encoding = pg_valid_server_encoding(item->value); @@ -636,6 +646,7 @@ read_extension_control_file(const char *extname) control->relocatable = false; control->superuser = true; control->trusted = false; + control->wildcard_upgrades = false; control->encoding = -1; /* @@ -890,7 +901,15 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control, if (from_version == NULL) elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version); else + { + if ( control->wildcard_upgrades && ! file_exists(filename) ) + { + elog(DEBUG1, "extension upgrade script \"%s\" does not exist, will try wildcard", filename); + /* if filename does not exist, try wildcard */ + filename = get_extension_script_filename(control, "%", version); + } elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version); + } /* * If installing a trusted extension on behalf of a non-superuser, become @@ -1215,13 +1234,23 @@ identify_update_path(ExtensionControlFile *control, /* Find shortest path */ result = find_update_path(evi_list, evi_start, evi_target, false, false); - if (result == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", - control->name, oldVersion, newVersion))); + if (result != NIL) + return result; - return result; + /* Find wildcard path, if allowed by control file */ + if ( control->wildcard_upgrades ) + { + evi_start = get_ext_ver_info("%", &evi_list); + result = find_update_path(evi_list, evi_start, evi_target, false, false); + + if (result != NIL) + return result; + } + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"", + control->name, oldVersion, newVersion))); } /* @@ -3392,3 +3421,20 @@ read_whole_file(const char *filename, int *length) buf[*length] = '\0'; return buf; } + +static bool +file_exists(const char *name) +{ + struct stat st; + + Assert(name != NULL); + + if (stat(name, &st) == 0) + return !S_ISDIR(st.st_mode); + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES)) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not access file \"%s\": %m", name))); + + return false; +} -- 2.34.1 --jbsevty42g3gdjf3-- ^ permalink raw reply [nested|flat] 14+ messages in thread
* [PATCH v18 1/8] Row pattern recognition patch for raw parser. @ 2024-05-11 07:11 Tatsuo Ishii <ishii@postgresql.org> 0 siblings, 0 replies; 14+ messages in thread From: Tatsuo Ishii @ 2024-05-11 07:11 UTC (permalink / raw) --- src/backend/parser/gram.y | 221 ++++++++++++++++++++++++++++++-- src/include/nodes/parsenodes.h | 67 ++++++++++ src/include/parser/kwlist.h | 8 ++ src/include/parser/parse_node.h | 1 + 4 files changed, 285 insertions(+), 12 deletions(-) diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index e8b619926e..c09a0f7e4f 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -680,6 +680,21 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); json_object_constructor_null_clause_opt json_array_constructor_null_clause_opt +%type <target> row_pattern_measure_item + row_pattern_definition +%type <node> opt_row_pattern_common_syntax + opt_row_pattern_skip_to + row_pattern_subset_item + row_pattern_term +%type <list> opt_row_pattern_measures + row_pattern_measure_list + row_pattern_definition_list + opt_row_pattern_subset_clause + row_pattern_subset_list + row_pattern_subset_rhs + row_pattern +%type <boolean> opt_row_pattern_initial_or_seek + first_or_last /* * Non-keyword token types. These are hard-wired into the "flex" lexer. @@ -723,7 +738,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS - DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DEPTH DESC + DEFERRABLE DEFERRED DEFINE DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DEPTH DESC DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP @@ -739,7 +754,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); HANDLER HAVING HEADER_P HOLD HOUR_P IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE - INCLUDING INCREMENT INDENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P + INCLUDING INCREMENT INDENT INDEX INDEXES INHERIT INHERITS INITIAL INITIALLY INLINE_P INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION @@ -752,7 +767,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED - MAPPING MATCH MATCHED MATERIALIZED MAXVALUE MERGE MERGE_ACTION METHOD + MAPPING MATCH MATCHED MATERIALIZED MAXVALUE MEASURES MERGE MERGE_ACTION METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE NAME_P NAMES NATIONAL NATURAL NCHAR NESTED NEW NEXT NFC NFD NFKC NFKD NO @@ -764,9 +779,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); ORDER ORDINALITY OTHERS OUT_P OUTER_P OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER - PARALLEL PARAMETER PARSER PARTIAL PARTITION PARTITIONS PASSING PASSWORD PATH - PERIOD PLACING PLAN PLANS POLICY - + PARALLEL PARAMETER PARSER PARTIAL PARTITION PARTITIONS PASSING PASSWORD PAST + PATH PATTERN_P PERIOD PERMUTE PLACING PLAN PLANS POLICY POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION @@ -777,12 +791,13 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); RESET RESTART RESTRICT RETURN RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP ROUTINE ROUTINES ROW ROWS RULE - SAVEPOINT SCALAR SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT + SAVEPOINT SCALAR SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SEEK SELECT SEQUENCE SEQUENCES + SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SPLIT SOURCE SQL_P STABLE STANDALONE_P START STATEMENT STATISTICS STDIN STDOUT STORAGE STORED STRICT_P STRING_P STRIP_P - SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P SYSTEM_USER + SUBSCRIPTION SUBSET SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P SYSTEM_USER TABLE TABLES TABLESAMPLE TABLESPACE TARGET TEMP TEMPLATE TEMPORARY TEXT_P THEN TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM @@ -891,6 +906,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %nonassoc UNBOUNDED NESTED /* ideally would have same precedence as IDENT */ %nonassoc IDENT PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP SET KEYS OBJECT_P SCALAR VALUE_P WITH WITHOUT PATH + MEASURES AFTER INITIAL SEEK PATTERN_P %left Op OPERATOR /* multi-character ops and user-defined operators */ %left '+' '-' %left '*' '/' '%' @@ -16333,7 +16349,8 @@ over_clause: OVER window_specification ; window_specification: '(' opt_existing_window_name opt_partition_clause - opt_sort_clause opt_frame_clause ')' + opt_sort_clause opt_row_pattern_measures opt_frame_clause + opt_row_pattern_common_syntax ')' { WindowDef *n = makeNode(WindowDef); @@ -16341,10 +16358,12 @@ window_specification: '(' opt_existing_window_name opt_partition_clause n->refname = $2; n->partitionClause = $3; n->orderClause = $4; + n->rowPatternMeasures = $5; /* copy relevant fields of opt_frame_clause */ - n->frameOptions = $5->frameOptions; - n->startOffset = $5->startOffset; - n->endOffset = $5->endOffset; + n->frameOptions = $6->frameOptions; + n->startOffset = $6->startOffset; + n->endOffset = $6->endOffset; + n->rpCommonSyntax = (RPCommonSyntax *)$7; n->location = @1; $$ = n; } @@ -16368,6 +16387,31 @@ opt_partition_clause: PARTITION BY expr_list { $$ = $3; } | /*EMPTY*/ { $$ = NIL; } ; +/* + * ROW PATTERN_P MEASURES + */ +opt_row_pattern_measures: MEASURES row_pattern_measure_list { $$ = $2; } + | /*EMPTY*/ { $$ = NIL; } + ; + +row_pattern_measure_list: + row_pattern_measure_item + { $$ = list_make1($1); } + | row_pattern_measure_list ',' row_pattern_measure_item + { $$ = lappend($1, $3); } + ; + +row_pattern_measure_item: + a_expr AS ColLabel + { + $$ = makeNode(ResTarget); + $$->name = $3; + $$->indirection = NIL; + $$->val = (Node *) $1; + $$->location = @1; + } + ; + /* * For frame clauses, we return a WindowDef, but only some fields are used: * frameOptions, startOffset, and endOffset. @@ -16527,6 +16571,143 @@ opt_window_exclusion_clause: | /*EMPTY*/ { $$ = 0; } ; +opt_row_pattern_common_syntax: +opt_row_pattern_skip_to opt_row_pattern_initial_or_seek + PATTERN_P '(' row_pattern ')' + opt_row_pattern_subset_clause + DEFINE row_pattern_definition_list + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = ((RPCommonSyntax *)$1)->rpSkipTo; + n->rpSkipVariable = ((RPCommonSyntax *)$1)->rpSkipVariable; + n->initial = $2; + n->rpPatterns = $5; + n->rpSubsetClause = $7; + n->rpDefs = $9; + $$ = (Node *) n; + } + | /*EMPTY*/ { $$ = NULL; } + ; + +opt_row_pattern_skip_to: + AFTER MATCH SKIP TO NEXT ROW + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = ST_NEXT_ROW; + n->rpSkipVariable = NULL; + $$ = (Node *) n; + } + | AFTER MATCH SKIP PAST LAST_P ROW + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = ST_PAST_LAST_ROW; + n->rpSkipVariable = NULL; + $$ = (Node *) n; + } + | AFTER MATCH SKIP TO first_or_last ColId + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = $5? ST_FIRST_VARIABLE : ST_LAST_VARIABLE; + n->rpSkipVariable = $6; + $$ = (Node *) n; + } +/* + | AFTER MATCH SKIP TO LAST_P ColId %prec LAST_P + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = ST_LAST_VARIABLE; + n->rpSkipVariable = $6; + $$ = n; + } + | AFTER MATCH SKIP TO ColId + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + n->rpSkipTo = ST_VARIABLE; + n->rpSkipVariable = $5; + $$ = n; + } +*/ + | /*EMPTY*/ + { + RPCommonSyntax *n = makeNode(RPCommonSyntax); + /* temporary set default to ST_NEXT_ROW */ + n->rpSkipTo = ST_PAST_LAST_ROW; + n->rpSkipVariable = NULL; + $$ = (Node *) n; + } + ; + +first_or_last: + FIRST_P { $$ = true; } + | LAST_P { $$ = false; } + ; + +opt_row_pattern_initial_or_seek: + INITIAL { $$ = true; } + | SEEK + { + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("SEEK is not supported"), + errhint("Use INITIAL."), + parser_errposition(@1))); + } + | /*EMPTY*/ { $$ = true; } + ; + +row_pattern: + row_pattern_term { $$ = list_make1($1); } + | row_pattern row_pattern_term { $$ = lappend($1, $2); } + ; + +row_pattern_term: + ColId { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "", (Node *)makeString($1), NULL, @1); } + | ColId '*' { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", (Node *)makeString($1), NULL, @1); } + | ColId '+' { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", (Node *)makeString($1), NULL, @1); } + | ColId '?' { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "?", (Node *)makeString($1), NULL, @1); } + ; + +opt_row_pattern_subset_clause: + SUBSET row_pattern_subset_list { $$ = $2; } + | /*EMPTY*/ { $$ = NIL; } + ; + +row_pattern_subset_list: + row_pattern_subset_item { $$ = list_make1($1); } + | row_pattern_subset_list ',' row_pattern_subset_item { $$ = lappend($1, $3); } + | /*EMPTY*/ { $$ = NIL; } + ; + +row_pattern_subset_item: ColId '=' '(' row_pattern_subset_rhs ')' + { + RPSubsetItem *n = makeNode(RPSubsetItem); + n->name = $1; + n->rhsVariable = $4; + $$ = (Node *) n; + } + ; + +row_pattern_subset_rhs: + ColId { $$ = list_make1(makeStringConst($1, @1)); } + | row_pattern_subset_rhs ',' ColId { $$ = lappend($1, makeStringConst($3, @1)); } + | /*EMPTY*/ { $$ = NIL; } + ; + +row_pattern_definition_list: + row_pattern_definition { $$ = list_make1($1); } + | row_pattern_definition_list ',' row_pattern_definition { $$ = lappend($1, $3); } + ; + +row_pattern_definition: + ColId AS a_expr + { + $$ = makeNode(ResTarget); + $$->name = $1; + $$->indirection = NIL; + $$->val = (Node *) $3; + $$->location = @1; + } + ; /* * Supporting nonterminals for expressions. @@ -17737,6 +17918,7 @@ unreserved_keyword: | INDEXES | INHERIT | INHERITS + | INITIAL | INLINE_P | INPUT_P | INSENSITIVE @@ -17765,6 +17947,7 @@ unreserved_keyword: | MATCHED | MATERIALIZED | MAXVALUE + | MEASURES | MERGE | METHOD | MINUTE_P @@ -17810,8 +17993,11 @@ unreserved_keyword: | PARTITIONS | PASSING | PASSWORD + | PAST | PATH + | PATTERN_P | PERIOD + | PERMUTE | PLAN | PLANS | POLICY @@ -17864,6 +18050,7 @@ unreserved_keyword: | SEARCH | SECOND_P | SECURITY + | SEEK | SEQUENCE | SEQUENCES | SERIALIZABLE @@ -17892,6 +18079,7 @@ unreserved_keyword: | STRING_P | STRIP_P | SUBSCRIPTION + | SUBSET | SUPPORT | SYSID | SYSTEM_P @@ -18086,6 +18274,7 @@ reserved_keyword: | CURRENT_USER | DEFAULT | DEFERRABLE + | DEFINE | DESC | DISTINCT | DO @@ -18249,6 +18438,7 @@ bare_label_keyword: | DEFAULTS | DEFERRABLE | DEFERRED + | DEFINE | DEFINER | DELETE_P | DELIMITER @@ -18326,6 +18516,7 @@ bare_label_keyword: | INDEXES | INHERIT | INHERITS + | INITIAL | INITIALLY | INLINE_P | INNER_P @@ -18380,6 +18571,7 @@ bare_label_keyword: | MATCHED | MATERIALIZED | MAXVALUE + | MEASURES | MERGE | MERGE_ACTION | METHOD @@ -18437,8 +18629,11 @@ bare_label_keyword: | PARTITIONS | PASSING | PASSWORD + | PAST | PATH + | PATTERN_P | PERIOD + | PERMUTE | PLACING | PLAN | PLANS @@ -18497,6 +18692,7 @@ bare_label_keyword: | SCROLL | SEARCH | SECURITY + | SEEK | SELECT | SEQUENCE | SEQUENCES @@ -18531,6 +18727,7 @@ bare_label_keyword: | STRING_P | STRIP_P | SUBSCRIPTION + | SUBSET | SUBSTRING | SUPPORT | SYMMETRIC diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 3ca06fc3af..9e65e9132c 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -550,6 +550,48 @@ typedef struct SortBy ParseLoc location; /* operator location, or -1 if none/unknown */ } SortBy; +/* + * AFTER MATCH row pattern skip to types in row pattern common syntax + */ +typedef enum RPSkipTo +{ + ST_NONE, /* AFTER MATCH omitted */ + ST_NEXT_ROW, /* SKIP TO NEXT ROW */ + ST_PAST_LAST_ROW, /* SKIP TO PAST LAST ROW */ + ST_FIRST_VARIABLE, /* SKIP TO FIRST variable name */ + ST_LAST_VARIABLE, /* SKIP TO LAST variable name */ + ST_VARIABLE /* SKIP TO variable name */ +} RPSkipTo; + +/* + * Row Pattern SUBSET clause item + */ +typedef struct RPSubsetItem +{ + NodeTag type; + char *name; /* Row Pattern SUBSET clause variable name */ + List *rhsVariable; /* Row Pattern SUBSET rhs variables (list of + * char *string) */ +} RPSubsetItem; + +/* + * RowPatternCommonSyntax - raw representation of row pattern common syntax + * + */ +typedef struct RPCommonSyntax +{ + NodeTag type; + RPSkipTo rpSkipTo; /* Row Pattern AFTER MATCH SKIP type */ + char *rpSkipVariable; /* Row Pattern Skip To variable name, if any */ + bool initial; /* true if <row pattern initial or seek> is + * initial */ + List *rpPatterns; /* PATTERN variables (list of A_Expr) */ + List *rpSubsetClause; /* row pattern subset clause (list of + * RPSubsetItem), if any */ + List *rpDefs; /* row pattern definitions clause (list of + * ResTarget) */ +} RPCommonSyntax; + /* * WindowDef - raw representation of WINDOW and OVER clauses * @@ -565,6 +607,9 @@ typedef struct WindowDef char *refname; /* referenced window name, if any */ List *partitionClause; /* PARTITION BY expression list */ List *orderClause; /* ORDER BY (list of SortBy) */ + List *rowPatternMeasures; /* row pattern measures (list of + * ResTarget) */ + RPCommonSyntax *rpCommonSyntax; /* row pattern common syntax */ int frameOptions; /* frame_clause options, see below */ Node *startOffset; /* expression for starting bound, if any */ Node *endOffset; /* expression for ending bound, if any */ @@ -1533,6 +1578,11 @@ typedef struct GroupingSet * the orderClause might or might not be copied (see copiedOrder); the framing * options are never copied, per spec. * + * "defineClause" is Row Pattern Recognition DEFINE clause (list of + * TargetEntry). TargetEntry.resname represents row pattern definition + * variable name. "patternVariable" and "patternRegexp" represents PATTERN + * clause. + * * The information relevant for the query jumbling is the partition clause * type and its bounds. */ @@ -1562,6 +1612,23 @@ typedef struct WindowClause Index winref; /* ID referenced by window functions */ /* did we copy orderClause from refname? */ bool copiedOrder pg_node_attr(query_jumble_ignore); + /* Row Pattern AFTER MACH SKIP clause */ + RPSkipTo rpSkipTo; /* Row Pattern Skip To type */ + char *rpSkipVariable; /* Row Pattern Skip To variable */ + bool initial; /* true if <row pattern initial or seek> is + * initial */ + /* Row Pattern DEFINE clause (list of TargetEntry) */ + List *defineClause; + /* Row Pattern DEFINE variable initial names (list of String) */ + List *defineInitial; + /* Row Pattern PATTERN variable name (list of String) */ + List *patternVariable; + + /* + * Row Pattern PATTERN regular expression quantifier ('+' or ''. list of + * String) + */ + List *patternRegexp; } WindowClause; /* diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index f9a4afd472..df49492629 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -129,6 +129,7 @@ PG_KEYWORD("default", DEFAULT, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("defaults", DEFAULTS, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("deferrable", DEFERRABLE, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("deferred", DEFERRED, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("define", DEFINE, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("definer", DEFINER, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("delete", DELETE_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("delimiter", DELIMITER, UNRESERVED_KEYWORD, BARE_LABEL) @@ -215,6 +216,7 @@ PG_KEYWORD("index", INDEX, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("indexes", INDEXES, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("inherit", INHERIT, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("inherits", INHERITS, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("initial", INITIAL, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("initially", INITIALLY, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("inline", INLINE_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("inner", INNER_P, TYPE_FUNC_NAME_KEYWORD, BARE_LABEL) @@ -273,6 +275,7 @@ PG_KEYWORD("match", MATCH, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("matched", MATCHED, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("materialized", MATERIALIZED, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("maxvalue", MAXVALUE, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("measures", MEASURES, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("merge", MERGE, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("merge_action", MERGE_ACTION, COL_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("method", METHOD, UNRESERVED_KEYWORD, BARE_LABEL) @@ -338,8 +341,11 @@ PG_KEYWORD("partition", PARTITION, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("partitions", PARTITIONS, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("passing", PASSING, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("password", PASSWORD, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("past", PAST, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("path", PATH, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("pattern", PATTERN_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("period", PERIOD, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("permute", PERMUTE, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plan", PLAN, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD, BARE_LABEL) @@ -401,6 +407,7 @@ PG_KEYWORD("scroll", SCROLL, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("search", SEARCH, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("second", SECOND_P, UNRESERVED_KEYWORD, AS_LABEL) PG_KEYWORD("security", SECURITY, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("seek", SEEK, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("select", SELECT, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("sequence", SEQUENCE, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("sequences", SEQUENCES, UNRESERVED_KEYWORD, BARE_LABEL) @@ -435,6 +442,7 @@ PG_KEYWORD("strict", STRICT_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("string", STRING_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("strip", STRIP_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("subscription", SUBSCRIPTION, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("subset", SUBSET, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("substring", SUBSTRING, COL_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("support", SUPPORT, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("symmetric", SYMMETRIC, RESERVED_KEYWORD, BARE_LABEL) diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index 5b781d87a9..66935afff8 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -51,6 +51,7 @@ typedef enum ParseExprKind EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */ EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */ EXPR_KIND_WINDOW_FRAME_GROUPS, /* window frame clause with GROUPS */ + EXPR_KIND_RPR_DEFINE, /* DEFINE */ EXPR_KIND_SELECT_TARGET, /* SELECT target list item */ EXPR_KIND_INSERT_TARGET, /* INSERT target list item */ EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */ -- 2.25.1 ----Next_Part(Sat_May_11_16_23_07_2024_789)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v18-0002-Row-pattern-recognition-patch-parse-analysis.patch" ^ permalink raw reply [nested|flat] 14+ messages in thread
end of thread, other threads:[~2024-05-11 07:11 UTC | newest] Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH v3] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH v2] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH v2] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH v1] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH v4] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2022-09-14 09:10 [PATCH] Allow wildcard (%) in extension upgrade paths Sandro Santilli <strk@kbt.io> 2024-05-11 07:11 [PATCH v18 1/8] Row pattern recognition patch for raw parser. Tatsuo Ishii <ishii@postgresql.org>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox