Skip to content
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

ADBDEV-6577: Use short-lived SPI contexts #40

Merged
merged 49 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1701482
Use short-lived SPI contexts
RekGRpth Oct 28, 2024
ce6fc81
warn
RekGRpth Oct 28, 2024
7fabd15
format
RekGRpth Oct 28, 2024
d628f7c
transaction
RekGRpth Oct 28, 2024
cded6ba
fix
RekGRpth Oct 28, 2024
f5fd4ce
optimize
RekGRpth Oct 28, 2024
865f9f8
context
RekGRpth Oct 28, 2024
ff7ab07
inc
RekGRpth Oct 28, 2024
983283c
snapshot
RekGRpth Oct 28, 2024
90a6577
rename
RekGRpth Oct 29, 2024
4e81a71
use enum and bit mask
RekGRpth Oct 30, 2024
25684b4
fix
RekGRpth Oct 30, 2024
0b49c70
formet
RekGRpth Oct 30, 2024
8652e1a
fix
RekGRpth Oct 30, 2024
e41ea95
optimize
RekGRpth Oct 30, 2024
f64447a
opmimize
RekGRpth Oct 30, 2024
3897da2
upper
RekGRpth Oct 30, 2024
ea50d56
optimize
RekGRpth Oct 30, 2024
a33012b
fix
RekGRpth Oct 30, 2024
8ae3517
fix
RekGRpth Oct 30, 2024
ad3a95f
fix
RekGRpth Oct 30, 2024
0069950
use context and error
RekGRpth Oct 30, 2024
9356184
move connect closer to execute
RekGRpth Oct 30, 2024
56f770e
additional move
RekGRpth Oct 30, 2024
8db6ada
rm
RekGRpth Oct 30, 2024
77212b6
mv
RekGRpth Oct 30, 2024
bb37067
mv
RekGRpth Oct 30, 2024
02020c1
partial revert
RekGRpth Oct 30, 2024
1aaf144
mv
RekGRpth Oct 30, 2024
cc262d3
mv
RekGRpth Oct 30, 2024
5cde9fa
partial revert
RekGRpth Oct 30, 2024
7d135ec
simplify
RekGRpth Oct 30, 2024
909f31c
fix
RekGRpth Oct 30, 2024
f05e5c6
simplify
RekGRpth Oct 30, 2024
5691fee
revert
RekGRpth Oct 30, 2024
40e2ff4
fix
RekGRpth Oct 30, 2024
b2a7df4
nl
RekGRpth Oct 30, 2024
f0bd18e
simplify
RekGRpth Oct 31, 2024
b73cb13
unuify
RekGRpth Oct 31, 2024
eb44b0f
optimize
RekGRpth Oct 31, 2024
5550449
fix
RekGRpth Oct 31, 2024
0bae1e1
simplify
RekGRpth Oct 31, 2024
dbfa9ab
rename
RekGRpth Oct 31, 2024
b3b8175
rename
RekGRpth Oct 31, 2024
9e126e2
rename
RekGRpth Oct 31, 2024
88224e0
mv
RekGRpth Oct 31, 2024
e818c97
mv
RekGRpth Oct 31, 2024
11602fd
mv
RekGRpth Oct 31, 2024
3777151
remove redundant code
RekGRpth Oct 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 23 additions & 49 deletions src/diskquota.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ static bool
is_altering_extension_to_default_version(char *version)
{
int spi_ret;
bool ret = false;
SPI_connect();
bool ret = false;
bool connected = SPI_connect_wrapper();
spi_ret = SPI_execute("select default_version from pg_available_extensions where name ='diskquota'", true, 0);
if (spi_ret != SPI_OK_SELECT)
elog(ERROR, "[diskquota] failed to select diskquota default version during diskquota update.");
Expand All @@ -194,7 +194,7 @@ is_altering_extension_to_default_version(char *version)
if (strcmp(version, default_version) == 0) ret = true;
}
}
SPI_finish();
SPI_finish_wrapper(connected);
return ret;
}

Expand Down Expand Up @@ -990,21 +990,14 @@ create_monitor_db_table(void)
*/
PG_TRY();
{
int ret_code = SPI_connect();
if (ret_code != SPI_OK_CONNECT)
{
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
errmsg("[diskquota launcher] unable to connect to execute internal query. return code: %d.",
ret_code)));
}
connected = true;
connected = SPI_connect_wrapper();
PushActiveSnapshot(GetTransactionSnapshot());
pushed_active_snap = true;

/* debug_query_string need to be set for SPI_execute utility functions. */
debug_query_string = sql;

ret_code = SPI_execute(sql, false, 0);
int ret_code = SPI_execute(sql, false, 0);
if (ret_code != SPI_OK_UTILITY)
{
int saved_errno = errno;
Expand All @@ -1024,7 +1017,7 @@ create_monitor_db_table(void)
RESUME_INTERRUPTS();
}
PG_END_TRY();
if (connected) SPI_finish();
SPI_finish_wrapper(connected);
if (pushed_active_snap) PopActiveSnapshot();
if (ret)
CommitTransactionCommand();
Expand Down Expand Up @@ -1054,14 +1047,8 @@ init_database_list(void)
StartTransactionCommand();
PushActiveSnapshot(GetTransactionSnapshot());

ret = SPI_connect();
if (ret != SPI_OK_CONNECT)
{
int saved_errno = errno;
ereport(ERROR, (errmsg("[diskquota launcher] SPI connect error, reason: %s, return code: %d.",
strerror(saved_errno), ret)));
}
ret = SPI_execute("select dbid from diskquota_namespace.database_list;", true, 0);
bool connected = SPI_connect_wrapper();
ret = SPI_execute("select dbid from diskquota_namespace.database_list;", true, 0);
if (ret != SPI_OK_SELECT)
{
int saved_errno = errno;
Expand Down Expand Up @@ -1120,6 +1107,7 @@ init_database_list(void)
}
}
num_db = num;
SPI_finish_wrapper(connected);
/* As update_monitor_db_mpp needs to execute sql, so can not put in the loop above */
for (int i = 0; i < diskquota_max_monitored_databases; i++)
{
Expand All @@ -1129,7 +1117,6 @@ init_database_list(void)
update_monitor_db_mpp(dbEntry->dbid, ADD_DB_TO_MONITOR, LAUNCHER_SCHEMA);
}
}
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
/* TODO: clean invalid database */
Expand Down Expand Up @@ -1181,7 +1168,6 @@ static void
do_process_extension_ddl_message(MessageResult *code, ExtensionDDLMessage local_extension_ddl_message)
{
int old_num_db = num_db;
bool connected = false;
bool pushed_active_snap = false;
bool ret = true;

Expand All @@ -1194,13 +1180,6 @@ do_process_extension_ddl_message(MessageResult *code, ExtensionDDLMessage local_
*/
PG_TRY();
{
int ret_code = SPI_connect();
if (ret_code != SPI_OK_CONNECT)
{
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
errmsg("unable to connect to execute internal query. return code: %d.", ret_code)));
}
connected = true;
PushActiveSnapshot(GetTransactionSnapshot());
pushed_active_snap = true;

Expand Down Expand Up @@ -1235,7 +1214,6 @@ do_process_extension_ddl_message(MessageResult *code, ExtensionDDLMessage local_
}
PG_END_TRY();

if (connected) SPI_finish();
if (pushed_active_snap) PopActiveSnapshot();
if (ret)
CommitTransactionCommand();
Expand All @@ -1251,12 +1229,6 @@ do_process_extension_ddl_message(MessageResult *code, ExtensionDDLMessage local_
PushActiveSnapshot(GetTransactionSnapshot());
pushed_active_snap = true;
Oid dbid = local_extension_ddl_message.dbid;
int ret_code = SPI_connect();
if (ret_code != SPI_OK_CONNECT)
{
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
errmsg("unable to connect to execute internal query. return code: %d.", ret_code)));
}
switch (local_extension_ddl_message.cmd)
{
case CMD_CREATE_EXTENSION:
Expand All @@ -1278,7 +1250,6 @@ do_process_extension_ddl_message(MessageResult *code, ExtensionDDLMessage local_
local_extension_ddl_message.cmd)));
break;
}
SPI_finish();
if (pushed_active_snap) PopActiveSnapshot();
CommitTransactionCommand();
}
Expand Down Expand Up @@ -1372,8 +1343,9 @@ add_dbid_to_database_list(Oid dbid)
{
int ret;

Oid argt[1] = {OIDOID};
Datum argv[1] = {ObjectIdGetDatum(dbid)};
Oid argt[1] = {OIDOID};
Datum argv[1] = {ObjectIdGetDatum(dbid)};
bool connected = SPI_connect_wrapper();

ret = SPI_execute_with_args("select * from diskquota_namespace.database_list where dbid = $1", 1, argt, argv, NULL,
true, 0);
Expand All @@ -1391,7 +1363,7 @@ add_dbid_to_database_list(Oid dbid)
ereport(WARNING, (errmsg("[diskquota launcher] database id %d is already actived, "
"skip database_list update",
dbid)));
return;
goto ret;
}

ret = SPI_execute_with_args("insert into diskquota_namespace.database_list values($1)", 1, argt, argv, NULL, false,
Expand All @@ -1405,7 +1377,8 @@ add_dbid_to_database_list(Oid dbid)
ret, strerror(saved_errno))));
}

return;
ret:
SPI_finish_wrapper(connected);
}

/*
Expand All @@ -1415,7 +1388,8 @@ add_dbid_to_database_list(Oid dbid)
static void
del_dbid_from_database_list(Oid dbid)
{
int ret;
int ret;
bool connected = SPI_connect_wrapper();

/* errors will be cached in outer function */
ret = SPI_execute_with_args("delete from diskquota_namespace.database_list where dbid = $1", 1,
Expand All @@ -1432,6 +1406,8 @@ del_dbid_from_database_list(Oid dbid)
ereport(ERROR, (errmsg("[diskquota launcher] del_dbid_from_database_list: reason: %s, ret_code: %d.",
strerror(saved_errno), ret)));
}

SPI_finish_wrapper(connected);
}

/*
Expand Down Expand Up @@ -1625,10 +1601,8 @@ static const char *
diskquota_status_schema_version()
{
static char ret_version[64];
int ret = SPI_connect();
Assert(ret = SPI_OK_CONNECT);

ret = SPI_execute("select extversion from pg_extension where extname = 'diskquota'", true, 0);
bool connected = SPI_connect_wrapper();
int ret = SPI_execute("select extversion from pg_extension where extname = 'diskquota'", true, 0);

if (ret != SPI_OK_SELECT || SPI_processed != 1)
{
Expand All @@ -1655,11 +1629,11 @@ diskquota_status_schema_version()

StrNCpy(ret_version, version, sizeof(ret_version) - 1);

SPI_finish();
SPI_finish_wrapper(connected);
return ret_version;

fail:
SPI_finish();
SPI_finish_wrapper(connected);
return "";
}

Expand Down
2 changes: 2 additions & 0 deletions src/diskquota.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,6 @@ extern HTAB *DiskquotaShmemInitHash(const char *name, long init_size, long max_s
extern void refresh_monitored_dbid_cache(void);
extern HASHACTION check_hash_fullness(HTAB *hashp, int max_size, const char *warning_message,
TimestampTz *last_overflow_report);
bool SPI_connect_wrapper(void);
void SPI_finish_wrapper(bool connected);
#endif
Loading
Loading