mirror of
https://github.com/postgres/postgres.git
synced 2025-06-17 17:02:08 +03:00
Change relpath() et al to return path by value
For AIO, and also some other recent patches, we need the ability to call relpath() in a critical section. Until now that was not feasible, as it allocated memory. The fact that relpath() allocated memory also made it awkward to use in log messages because we had to take care to free the memory afterwards. Which we e.g. didn't do for when zeroing out an invalid buffer. We discussed other solutions, e.g. filling a pre-allocated buffer that's passed to relpath(), but they all came with plenty downsides or were larger projects. The easiest fix seems to be to make relpath() return the path by value. To be able to return the path by value we need to determine the maximum length of a relation path. This patch adds a long #define that computes the exact maximum, which is verified to be correct in a regression test. As this change the signature of relpath(), extensions using it will need to adapt their code. We discussed leaving a backward-compat shim in place, but decided it's not worth it given the use of relpath() doesn't seem widespread. Discussion: https://postgr.es/m/xeri5mla4b5syjd5a25nok5iez2kr3bm26j2qn4u7okzof2bmf@kwdh2vf7npra
This commit is contained in:
@ -326,7 +326,7 @@ static int64
|
||||
calculate_relation_size(RelFileLocator *rfn, ProcNumber backend, ForkNumber forknum)
|
||||
{
|
||||
int64 totalsize = 0;
|
||||
char *relationpath;
|
||||
RelPathStr relationpath;
|
||||
char pathname[MAXPGPATH];
|
||||
unsigned int segcount = 0;
|
||||
|
||||
@ -340,10 +340,10 @@ calculate_relation_size(RelFileLocator *rfn, ProcNumber backend, ForkNumber fork
|
||||
|
||||
if (segcount == 0)
|
||||
snprintf(pathname, MAXPGPATH, "%s",
|
||||
relationpath);
|
||||
relationpath.str);
|
||||
else
|
||||
snprintf(pathname, MAXPGPATH, "%s.%u",
|
||||
relationpath, segcount);
|
||||
relationpath.str, segcount);
|
||||
|
||||
if (stat(pathname, &fst) < 0)
|
||||
{
|
||||
@ -973,7 +973,7 @@ pg_relation_filepath(PG_FUNCTION_ARGS)
|
||||
Form_pg_class relform;
|
||||
RelFileLocator rlocator;
|
||||
ProcNumber backend;
|
||||
char *path;
|
||||
RelPathStr path;
|
||||
|
||||
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
@ -1039,5 +1039,5 @@ pg_relation_filepath(PG_FUNCTION_ARGS)
|
||||
|
||||
path = relpathbackend(rlocator, backend, MAIN_FORKNUM);
|
||||
|
||||
PG_RETURN_TEXT_P(cstring_to_text(path));
|
||||
PG_RETURN_TEXT_P(cstring_to_text(path.str));
|
||||
}
|
||||
|
Reference in New Issue
Block a user