/*------------------------------------------------------------------------- * cfs_path.c * Compressed File Store (CFS) — sidecar filename helpers. * * Implements the helpers declared in cfs_path.h. Path construction is based * on relpathperm(rlocator, MAIN_FORKNUM) so that sidecars live alongside the * relation's main fork in the correct tablespace / database directory. * * Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group * * IDENTIFICATION * src/backend/storage/cfs/cfs_path.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include #include #include #include "common/relpath.h" #include "port.h" #include "storage/cfs/cfs.h" #include "storage/cfs/cfs_path.h" #include "storage/relfilelocator.h" #include "utils/palloc.h" const char *const CfsFileKindSuffix[CFS_FILE_KIND_COUNT] = { CFS_DATA_SUFFIX, /* CFS_FILE_DATA */ CFS_MAP_SUFFIX, /* CFS_FILE_MAP */ CFS_JOURNAL_SUFFIX, /* CFS_FILE_JOURNAL */ }; const char * cfs_file_suffix(CfsFileKind kind) { if ((unsigned int) kind >= (unsigned int) CFS_FILE_KIND_COUNT) return NULL; return CfsFileKindSuffix[kind]; } bool cfs_locator_supported(RelFileLocatorBackend rlocator) { if (RelFileLocatorBackendIsTemp(rlocator)) { ereport(DEBUG1, (errmsg("CFS: temporary relation locators are not supported"))); return false; } return true; } /* * Core path builder shared by cfs_make_filename() and * cfs_make_segment_filename(). Returns a palloc'd string or NULL when the * locator is unsupported. */ static char * cfs_build_sidecar_path(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind, uint32 segno) { RelPathStr base; const char *suffix; /* * Sidecar files belong to the MAIN fork; 'forknum' is accepted for API * symmetry but does not change the base path. */ (void) forknum; if (!cfs_locator_supported(rlocator)) return NULL; base = relpathperm(rlocator.locator, MAIN_FORKNUM); suffix = cfs_file_suffix(kind); if (suffix == NULL) { ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("CFS: unknown sidecar file kind %d", (int) kind))); } if (segno == 0) { char *result = psprintf("%s%s", base.str, suffix); ereport(DEBUG1, (errmsg("CFS: built sidecar path \"%s\" (kind=%d)", result, (int) kind))); return result; } else { char *result = psprintf("%s%s.%u", base.str, suffix, segno); ereport(DEBUG1, (errmsg("CFS: built sidecar path \"%s\" (kind=%d segno=%u)", result, (int) kind, segno))); return result; } } char * cfs_make_filename(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind) { return cfs_build_sidecar_path(rlocator, forknum, kind, 0); } char * cfs_make_segment_filename(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind, uint32 segno) { return cfs_build_sidecar_path(rlocator, forknum, kind, segno); } /* ---------------------------------------------------------------- * Suffix parsing helpers * ---------------------------------------------------------------- */ static bool cfs_ends_with(const char *s, const char *suffix) { size_t ls = strlen(s); size_t lf = strlen(suffix); return ls >= lf && strcmp(s + ls - lf, suffix) == 0; } /* * Truncate a single trailing "." segment component in place (e.g. * "12345.cfp.1" -> "12345.cfp"). Leaves anything that is not a pure numeric * suffix untouched. Exported for the startup directory scan * (cfs_recovery.c), which derives the swap-marker name from a numbered * candidate file name. */ void cfs_strip_trailing_segment(char *s) { char *dot = strrchr(s, '.'); size_t i; if (dot == NULL || dot == s) return; for (i = 1; dot[i] != '\0'; i++) { if (!isdigit((unsigned char) dot[i])) return; } *dot = '\0'; } bool cfs_sidecar_kind_from_suffix(const char *name, CfsFileKind *kind) { char base[MAXPGPATH]; const char *bn; CfsFileKind k; if (name == NULL) return false; /* Work on the basename. */ bn = last_dir_separator(name); bn = (bn == NULL) ? name : bn + 1; strlcpy(base, bn, sizeof(base)); cfs_strip_trailing_segment(base); for (k = 0; k < CFS_FILE_KIND_COUNT; k++) { if (cfs_ends_with(base, CfsFileKindSuffix[k])) { if (kind != NULL) *kind = k; return true; } } return false; } bool cfs_is_sidecar_filename(const char *name) { return cfs_sidecar_kind_from_suffix(name, NULL); } /* ---------------------------------------------------------------- * Sidecar size helpers * * Used by the SQL size functions (pg_relation_size, pg_total_relation_size) * so that the figures reported to users include the bytes physically used * by CFS sidecar files. Non-CFS relations report 0 because no sidecars * exist. * ---------------------------------------------------------------- */ /* * Add the size of every numbered segment of a sidecar kind (e.g. * ".cfp", ".cfp.1", ".cfp.2", ...). Stops at * the first missing segment, matching the relation segment convention. */ static void cfs_add_segmented_sidecar_size(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind, int64 *total) { uint32 segno; char *path; for (segno = 0;; segno++) { path = cfs_make_segment_filename(rlocator, forknum, kind, segno); if (path == NULL) return; /* * For segment 0 a missing file aborts the scan; for segment > 0 a * missing file is the natural end of the segment chain and is * silently treated as "no more segments". */ { struct stat st; if (stat(path, &st) < 0) { if (errno == ENOENT) { pfree(path); return; } ereport(WARNING, (errcode_for_file_access(), errmsg("could not stat CFS sidecar file \"%s\": %m", path))); pfree(path); return; } *total += st.st_size; } pfree(path); } } int64 cfs_segmented_sidecar_size(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind) { int64 total = 0; if (!cfs_locator_supported(rlocator)) return 0; cfs_add_segmented_sidecar_size(rlocator, forknum, kind, &total); return total; } int64 cfs_relation_sidecar_size(RelFileLocatorBackend rlocator) { int64 total = 0; if (!cfs_locator_supported(rlocator)) return 0; /* .cfp (with numbered segments) + .cfsmap + .cfsjrn */ cfs_add_segmented_sidecar_size(rlocator, MAIN_FORKNUM, CFS_FILE_DATA, &total); cfs_add_segmented_sidecar_size(rlocator, MAIN_FORKNUM, CFS_FILE_MAP, &total); cfs_add_segmented_sidecar_size(rlocator, MAIN_FORKNUM, CFS_FILE_JOURNAL, &total); if (total > 0) ereport(DEBUG1, (errmsg_internal("CFS: sidecar size for relation %u/%u/%u is " INT64_FORMAT, rlocator.locator.spcOid, rlocator.locator.dbOid, rlocator.locator.relNumber, total))); return total; }