* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
- * Copyright (c) 2003-2023 PgPool Global Development Group
+ * Copyright (c) 2003-2024 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
return oidbufp;
}
+/*
+ * Extract table oids contained in memqcache_oiddir by database oid.
+ */
static int
pool_get_dropdb_table_oids(int **oids, int dboid)
{
int num_oids = 0;
DIR *dir;
struct dirent *dp;
- char path[1024];
+ char *path;
- snprintf(path, sizeof(path), "%s/%d", pool_config->memqcache_oiddir, dboid);
+ path = psprintf("%s/%d", pool_config->memqcache_oiddir, dboid);
if ((dir = opendir(path)) == NULL)
{
ereport(DEBUG1,
(errmsg("memcache: getting drop table oids"),
errdetail("Failed to open dir: %s", path)));
+ pfree(path);
return 0;
}
if (tmp == NULL)
{
closedir(dir);
+ pfree(path);
return 0;
}
rtn = tmp;
num_oids++;
}
+ pfree(path);
closedir(dir);
*oids = rtn;
{
int dboid = 0;
POOL_SELECT_RESULT *res;
- char query[1024];
+ char *query;
POOL_CONNECTION_POOL *backend;
backend = pool_get_session_context(false)->backend;
- snprintf(query, sizeof(query), DATABASE_TO_OID_QUERY, dbname);
+ query = psprintf(DATABASE_TO_OID_QUERY, dbname);
do_query(MAIN(backend), query, &res, MAJOR(backend));
+ pfree(query);
if (res->numrows != 1)
{
{
char *dir;
int dboid;
- char path[1024];
+ char *path;
int i;
int len;
return;
}
- snprintf(path, sizeof(path), "%s/%d", dir, dboid);
+ path = psprintf("%s/%d", dir, dboid);
if (mkdir(path, S_IREAD | S_IWRITE | S_IEXEC) == -1)
{
if (errno != EEXIST)
ereport(WARNING,
(errmsg("memcache: adding table oid maps, failed to create directory:\"%s\"", path),
errdetail("%m")));
+ pfree(path);
return;
}
}
+ pfree(path);
if (pool_is_shmem_cache())
{
/*
* Create or open each memqcache_oiddir/database_oid/table_oid
*/
- snprintf(path, sizeof(path), "%s/%d/%d", dir, dboid, oid);
+ path = psprintf("%s/%d/%d", dir, dboid, oid);
if ((fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
{
ereport(WARNING,
(errmsg("memcache: adding table oid maps, failed to open file:\"%s\"", path),
errdetail("%m")));
+ pfree(path);
return;
}
errdetail("%m")));
close(fd);
+ pfree(path);
return;
}
(errmsg("memcache: adding table oid maps, failed seek on file:\"%s\"", path),
errdetail("%m")));
close(fd);
+ pfree(path);
return;
}
return;
}
close(fd);
+ pfree(path);
}
}
/*
- * Discard all oid maps at pgpool-II startup.
+ * Discard all oid maps at Pgpool-II startup.
* This is necessary for shmem case.
*/
void
pool_discard_oid_maps(void)
{
- char command[1024];
+ char *command;
- snprintf(command, sizeof(command), "/bin/rm -fr %s/[0-9]*",
- pool_config->memqcache_oiddir);
+ command = psprintf("/bin/rm -fr %s/[0-9]*",
+ pool_config->memqcache_oiddir);
if (system(command) == -1)
ereport(WARNING,
(errmsg("unable to execute command \"%s\"", command),
errdetail("system() command failed with error \"%m\"")));
-
+ pfree(command);
}
+/*
+ * Discard all oid maps contained in database specified by dboid.
+ */
void
pool_discard_oid_maps_by_db(int dboid)
{
- char command[1024];
+ char *command;
if (pool_is_shmem_cache())
{
- snprintf(command, sizeof(command), "/bin/rm -fr %s/%d/",
- pool_config->memqcache_oiddir, dboid);
+ command = psprintf("/bin/rm -fr %s/%d/",
+ pool_config->memqcache_oiddir, dboid);
ereport(DEBUG1,
(errmsg("memcache: discarding oid maps by db"),
ereport(WARNING,
(errmsg("unable to execute command \"%s\"", command),
errdetail("system() command failed with error \"%m\"")));
+
+ pfree(command);
}
}
pool_invalidate_query_cache(int num_table_oids, int *table_oid, bool unlinkp, int dboid)
{
char *dir;
- char path[1024];
+ char *path;
int i;
int len;
POOL_CACHEKEY buf;
}
}
- snprintf(path, sizeof(path), "%s/%d", dir, dboid);
+ path = psprintf("%s/%d", dir, dboid);
if (mkdir(path, S_IREAD | S_IWRITE | S_IEXEC) == -1)
{
if (errno != EEXIST)
ereport(WARNING,
(errmsg("memcache: invalidating query cache, failed to create directory:\"%s\"", path),
errdetail("%m")));
+ pfree(path);
return;
}
}
/*
* Open each memqcache_oiddir/database_oid/table_oid
*/
- snprintf(path, sizeof(path), "%s/%d/%d", dir, dboid, oid);
+ path = psprintf("%s/%d/%d", dir, dboid, oid);
if ((fd = open(path, O_RDONLY)) == -1)
{
/*
(errmsg("memcache: invalidating query cache, failed to lock file:\"%s\"", path),
errdetail("%m")));
close(fd);
+ pfree(path);
return;
}
for (;;)
errdetail("%m")));
close(fd);
+ pfree(path);
return;
}
else if (sts == len)
ereport(WARNING,
(errmsg("memcache: invalidating query cache, invalid data length:%d in file:\"%s\"", sts, path)));
close(fd);
+ pfree(path);
return;
}
break;
{
unlink(path);
}
+ pfree(path);
close(fd);
}
#ifdef SHMEMCACHE_DEBUG
{
if (memq_lock_fd == 0)
{
- char path[1024];
+ char *path;
- snprintf(path, sizeof(path), "%s/%s", pool_config->logdir, QUERY_CACHE_LOCK_FILE);
+ path = psprintf("%s/%s", pool_config->logdir, QUERY_CACHE_LOCK_FILE);
memq_lock_fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
if (memq_lock_fd == -1)
{
(errmsg("Failed to open lock file for query cache \"%s\"", path),
errdetail("%m")));
}
+ pfree(path);
}
#ifdef LOCK_TRACE