-
-
Notifications
You must be signed in to change notification settings - Fork 15
fix: run custom scripts on non privileged extensions #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
soedirgo
merged 5 commits into
master
from
fix/run-custom-scripts-on-non-privileged-extensions
May 24, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb9e05f
fix: run custom scripts on non-privileged extensions
soedirgo 7b069c7
feat: `supautils.extension_custom_scripts_path`
soedirgo 8793cc2
refactor: move `override_create_ext_statement`
soedirgo ee5f371
refactor: move custom scripts logic to its own file
soedirgo d14645a
test: split extension custom scripts tests
soedirgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#include "extension_custom_scripts.h" | ||
|
||
// Prevent recursively running custom scripts | ||
static bool running_custom_script = false; | ||
|
||
// This produces a char surrounded by a triple single quote like '''x''' | ||
// This is so when it gets interpreted by SQL it converts to a single quote surround: 'x' | ||
// To see an example, do `select 'x';` vs `select '''x''';` on psql. | ||
static char *sql_literal(const char *str){ | ||
return str == NULL? | ||
"'null'": // also handle the NULL cstr case | ||
quote_literal_cstr(quote_literal_cstr(str)); | ||
} | ||
|
||
static void run_custom_script(const char *filename, const char *extname, | ||
const char *extschema, const char *extversion, | ||
bool extcascade) { | ||
if (running_custom_script) { | ||
return; | ||
} | ||
running_custom_script = true; | ||
|
||
static const char sql_replace_template[] = "\ | ||
do $_$\ | ||
begin\ | ||
execute replace(replace(replace(replace(\ | ||
pg_read_file(%s)\ | ||
, '@extname@', %s)\ | ||
, '@extschema@', %s)\ | ||
, '@extversion@', %s)\ | ||
, '@extcascade@', %s);\ | ||
exception\ | ||
when undefined_file then\ | ||
null;\ | ||
end; $_$"; | ||
|
||
static const size_t max_sql_len | ||
= sizeof (sql_replace_template) | ||
+ MAXPGPATH // max size of a file path | ||
+ 3 * (NAMEDATALEN + 6) // 3 *(identifier + 6 single quotes of the SQL literal, see sql_literal) | ||
+ sizeof ("false") // max size of a bool string value | ||
; | ||
|
||
char sql[max_sql_len]; | ||
|
||
snprintf(sql, | ||
max_sql_len, | ||
sql_replace_template, | ||
quote_literal_cstr(filename), | ||
sql_literal(extname), | ||
sql_literal(extschema), | ||
sql_literal(extversion), | ||
extcascade?"'true'":"'false'"); | ||
|
||
PushActiveSnapshot(GetTransactionSnapshot()); | ||
SPI_connect(); | ||
|
||
int rc = SPI_execute(sql, false, 0); | ||
if (rc != SPI_OK_UTILITY) { | ||
elog(ERROR, "SPI_execute failed with error code %d", rc); | ||
} | ||
SPI_finish(); | ||
PopActiveSnapshot(); | ||
running_custom_script = false; | ||
} | ||
|
||
void run_global_before_create_script(char *extname, List *options, const char *privileged_extensions_custom_scripts_path){ | ||
DefElem *d_schema = NULL, *d_new_version = NULL, *d_cascade = NULL; | ||
char *extschema = NULL, *extversion = NULL; | ||
bool extcascade = false; | ||
char filename[MAXPGPATH]; | ||
|
||
ListCell *option_cell = NULL; | ||
|
||
foreach (option_cell, options) { | ||
DefElem *defel = (DefElem *)lfirst(option_cell); | ||
|
||
if (strcmp(defel->defname, "schema") == 0) { | ||
d_schema = defel; | ||
extschema = defGetString(d_schema); | ||
} else if (strcmp(defel->defname, "new_version") == 0) { | ||
d_new_version = defel; | ||
extversion = defGetString(d_new_version); | ||
} else if (strcmp(defel->defname, "cascade") == 0) { | ||
d_cascade = defel; | ||
extcascade = defGetBoolean(d_cascade); | ||
} | ||
} | ||
|
||
snprintf(filename, MAXPGPATH, "%s/before-create.sql", | ||
privileged_extensions_custom_scripts_path); | ||
run_custom_script(filename, extname, extschema, extversion, | ||
extcascade); | ||
} | ||
|
||
void run_ext_before_create_script(char *extname, List *options, const char *privileged_extensions_custom_scripts_path){ | ||
DefElem *d_schema = NULL; | ||
DefElem *d_new_version = NULL; | ||
DefElem *d_cascade = NULL; | ||
char *extschema = NULL; | ||
char *extversion = NULL; | ||
bool extcascade = false; | ||
ListCell *option_cell = NULL; | ||
char filename[MAXPGPATH]; | ||
|
||
foreach (option_cell, options) { | ||
DefElem *defel = (DefElem *)lfirst(option_cell); | ||
|
||
if (strcmp(defel->defname, "schema") == 0) { | ||
d_schema = defel; | ||
extschema = defGetString(d_schema); | ||
} else if (strcmp(defel->defname, "new_version") == 0) { | ||
d_new_version = defel; | ||
extversion = defGetString(d_new_version); | ||
} else if (strcmp(defel->defname, "cascade") == 0) { | ||
d_cascade = defel; | ||
extcascade = defGetBoolean(d_cascade); | ||
} | ||
} | ||
|
||
|
||
snprintf(filename, MAXPGPATH, "%s/%s/before-create.sql", | ||
privileged_extensions_custom_scripts_path, extname); | ||
run_custom_script(filename, extname, extschema, extversion, | ||
extcascade); | ||
} | ||
|
||
void run_ext_after_create_script(char *extname, List *options, const char *privileged_extensions_custom_scripts_path){ | ||
DefElem *d_schema = NULL; | ||
DefElem *d_new_version = NULL; | ||
DefElem *d_cascade = NULL; | ||
char *extschema = NULL; | ||
char *extversion = NULL; | ||
bool extcascade = false; | ||
ListCell *option_cell = NULL; | ||
char filename[MAXPGPATH]; | ||
|
||
foreach (option_cell, options) { | ||
DefElem *defel = (DefElem *)lfirst(option_cell); | ||
|
||
if (strcmp(defel->defname, "schema") == 0) { | ||
d_schema = defel; | ||
extschema = defGetString(d_schema); | ||
} else if (strcmp(defel->defname, "new_version") == 0) { | ||
d_new_version = defel; | ||
extversion = defGetString(d_new_version); | ||
} else if (strcmp(defel->defname, "cascade") == 0) { | ||
d_cascade = defel; | ||
extcascade = defGetBoolean(d_cascade); | ||
} | ||
} | ||
|
||
snprintf(filename, MAXPGPATH, "%s/%s/after-create.sql", | ||
privileged_extensions_custom_scripts_path, extname); | ||
run_custom_script(filename, extname, extschema, extversion, | ||
extcascade); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef EXTENSION_CUSTOM_SCRIPTS_H | ||
#define EXTENSION_CUSTOM_SCRIPTS_H | ||
|
||
#include "pg_prelude.h" | ||
|
||
extern void run_global_before_create_script( | ||
char *extname, List *options, | ||
const char *privileged_extensions_custom_scripts_path); | ||
|
||
extern void run_ext_before_create_script( | ||
char *extname, List *options, | ||
const char *privileged_extensions_custom_scripts_path); | ||
|
||
extern void run_ext_after_create_script( | ||
char *extname, List *options, | ||
const char *privileged_extensions_custom_scripts_path); | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this recursive custom scripts behavior mysterious. When can this happen? Can we cover it by tests so it can be fully understood?
I can help by adding the tests on another PR too, if it requires some other extension it's fine (I've added tests requiring plpgsql_check and pgmq before). Just let me know how can I reproduce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pathological behavior we're trying to avoid is an infinite loop caused by a
create extension
triggering a custom script that then runs acreate extension
, which in turn triggers a custom script, etc.Can't really put the infinite loop as a test case, but preventing nested custom scripts can be done with 2 extensions: have custom scripts for both where one does a
create extension
for the other, and have the other one do a side effect (e.g. create a table with some sentinel name) and we assert not to happen.