diff options
author | Noah Misch <noah@leadboat.com> | 2022-07-02 13:00:30 -0700 |
---|---|---|
committer | Noah Misch <noah@leadboat.com> | 2022-07-02 13:00:34 -0700 |
commit | 5b94e2bd4d5430f5ea4e965a32727a6006972a55 (patch) | |
tree | 0b88867b8b8dd4ced53b466c489020377de24a5a /src/interfaces/ecpg/ecpglib/execute.c | |
parent | fb81a93a6442e55d8c7376a01c27cb5d6c062c80 (diff) | |
download | postgresql-5b94e2bd4d5430f5ea4e965a32727a6006972a55.tar.gz postgresql-5b94e2bd4d5430f5ea4e965a32727a6006972a55.zip |
ecpglib: call newlocale() once per process.
ecpglib has been calling it once per SQL query and once per EXEC SQL GET
DESCRIPTOR. Instead, if newlocale() has not succeeded before, call it
while establishing a connection. This mitigates three problems:
- If newlocale() failed in EXEC SQL GET DESCRIPTOR, the command silently
proceeded without the intended locale change.
- On AIX, each newlocale()+freelocale() cycle leaked memory.
- newlocale() CPU usage may have been nontrivial.
Fail the connection attempt if newlocale() fails. Rearrange
ecpg_do_prologue() to validate the connection before its uselocale().
The sort of program that may regress is one running in an environment
where newlocale() fails. If that program establishes connections
without running SQL statements, it will stop working in response to this
change. I'm betting against the importance of such an ECPG use case.
Most SQL execution (any using ECPGdo()) has long required newlocale()
success, so there's little a connection could do without newlocale().
Back-patch to v10 (all supported versions).
Reviewed by Tom Lane. Reported by Guillaume Lelarge.
Discussion: https://postgr.es/m/20220101074055.GA54621@rfd.leadboat.com
Diffstat (limited to 'src/interfaces/ecpg/ecpglib/execute.c')
-rw-r--r-- | src/interfaces/ecpg/ecpglib/execute.c | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c index 930b6adbe4f..e8e8fb2b2c3 100644 --- a/src/interfaces/ecpg/ecpglib/execute.c +++ b/src/interfaces/ecpg/ecpglib/execute.c @@ -101,10 +101,7 @@ free_statement(struct statement *stmt) free_variable(stmt->outlist); ecpg_free(stmt->command); ecpg_free(stmt->name); -#ifdef HAVE_USELOCALE - if (stmt->clocale) - freelocale(stmt->clocale); -#else +#ifndef HAVE_USELOCALE ecpg_free(stmt->oldlocale); #endif ecpg_free(stmt); @@ -1966,6 +1963,15 @@ ecpg_do_prologue(int lineno, const int compat, const int force_indicator, return false; } +#ifdef ENABLE_THREAD_SAFETY + ecpg_pthreads_init(); +#endif + + con = ecpg_get_connection(connection_name); + + if (!ecpg_init(con, connection_name, lineno)) + return false; + stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno); if (stmt == NULL) @@ -1980,13 +1986,13 @@ ecpg_do_prologue(int lineno, const int compat, const int force_indicator, * treat that situation as if the function doesn't exist. */ #ifdef HAVE_USELOCALE - stmt->clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); - if (stmt->clocale == (locale_t) 0) - { - ecpg_do_epilogue(stmt); - return false; - } - stmt->oldlocale = uselocale(stmt->clocale); + + /* + * Since ecpg_init() succeeded, we have a connection. Any successful + * connection initializes ecpg_clocale. + */ + Assert(ecpg_clocale); + stmt->oldlocale = uselocale(ecpg_clocale); if (stmt->oldlocale == (locale_t) 0) { ecpg_do_epilogue(stmt); @@ -2005,18 +2011,6 @@ ecpg_do_prologue(int lineno, const int compat, const int force_indicator, setlocale(LC_NUMERIC, "C"); #endif -#ifdef ENABLE_THREAD_SAFETY - ecpg_pthreads_init(); -#endif - - con = ecpg_get_connection(connection_name); - - if (!ecpg_init(con, connection_name, lineno)) - { - ecpg_do_epilogue(stmt); - return false; - } - /* * If statement type is ECPGst_prepnormal we are supposed to prepare the * statement before executing them |