Skip to content

Commit c1a6f4f

Browse files
committed
Add cfs_compression_ratio function
1 parent 0e86701 commit c1a6f4f

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/backend/storage/file/cfs.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ PG_FUNCTION_INFO_V1(cfs_start_gc);
831831
PG_FUNCTION_INFO_V1(cfs_enable_gc);
832832
PG_FUNCTION_INFO_V1(cfs_version);
833833
PG_FUNCTION_INFO_V1(cfs_estimate);
834+
PG_FUNCTION_INFO_V1(cfs_compression_ratio);
834835

835836
Datum cfs_start_gc(PG_FUNCTION_ARGS)
836837
{
@@ -923,3 +924,53 @@ Datum cfs_estimate(PG_FUNCTION_ARGS)
923924
}
924925
PG_RETURN_FLOAT8(avgRatio);
925926
}
927+
928+
Datum cfs_compression_ratio(PG_FUNCTION_ARGS)
929+
{
930+
Oid oid = PG_GETARG_OID(0);
931+
Relation rel = try_relation_open(oid, AccessShareLock);
932+
uint64 virtSize = 0;
933+
uint64 physSize = 0;
934+
935+
if (rel != NULL) {
936+
char* path = relpathbackend(rel->rd_node, rel->rd_backend, MAIN_FORKNUM);
937+
char* map_path = (char*)palloc(strlen(path) + 16);
938+
int i = 0;
939+
940+
while (true) {
941+
int md;
942+
FileMap* map;
943+
944+
if (i == 0) {
945+
sprintf(map_path, "%s.cfm", path);
946+
} else {
947+
sprintf(map_path, "%s.%u.cfm", path, i);
948+
}
949+
md = open(map_path, O_RDONLY|PG_BINARY, 0);
950+
if (md < 0) {
951+
break;
952+
}
953+
map = cfs_mmap(md);
954+
if (map == MAP_FAILED) {
955+
elog(LOG, "cfs_compression_ration failed to map file %s: %m", map_path);
956+
close(md);
957+
break;
958+
}
959+
virtSize += pg_atomic_read_u32(&map->virtSize);
960+
physSize += pg_atomic_read_u32(&map->physSize);
961+
962+
if (cfs_munmap(map) < 0) {
963+
elog(LOG, "Failed to unmap file %s: %m", map_path);
964+
}
965+
if (close(md) < 0) {
966+
elog(LOG, "Failed to close file %s: %m", map_path);
967+
}
968+
i += 1;
969+
}
970+
pfree(path);
971+
pfree(map_path);
972+
relation_close(rel, AccessShareLock);
973+
}
974+
PG_RETURN_FLOAT8((double)virtSize/physSize);
975+
}
976+

src/backend/storage/file/reinit.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ parse_filename_for_nontemp_relation(const char *name, int *oidchars,
431431
int segchar;
432432

433433
/* .cfm is used by compression */
434-
if (strcmp(name+pos+1, "cfm") == 0) {
434+
if (strcmp(name+pos, ".cfm") == 0) {
435435
return true;
436436
}
437437
for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
@@ -441,6 +441,11 @@ parse_filename_for_nontemp_relation(const char *name, int *oidchars,
441441
pos += segchar;
442442
}
443443

444+
/* .cfm is used by compression */
445+
if (strcmp(name+pos, ".cfm") == 0) {
446+
return true;
447+
}
448+
444449
/* Now we should be at the end. */
445450
if (name[pos] != '\0')
446451
return false;

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5348,6 +5348,8 @@ DATA(insert OID = 6102 ( cfs_enable_gc PGNSP PGUID 12 1 0 0 0 f f f f t f v
53485348
DESCR("Enable or disable CFS garbage collection");
53495349
DATA(insert OID = 6103 ( cfs_estimate PGNSP PGUID 12 1 0 0 0 f f f f t f v s 1 0 701 "2205" _null_ _null_ _null_ _null_ _null_ cfs_estimate _null_ _null_ _null_ ));
53505350
DESCR("Estimate relation compression ratio");
5351+
DATA(insert OID = 6019 ( cfs_compression_ratio PGNSP PGUID 12 1 0 0 0 f f f f t f v s 1 0 701 "2205" _null_ _null_ _null_ _null_ _null_ cfs_compression_ratio _null_ _null_ _null_ ));
5352+
DESCR("Compression ration of relation");
53515353

53525354
/* distance functions */
53535355
DATA(insert OID = 3343 ( int2_dist PGNSP PGUID 12 1 0 0 0 f f f f t f i s 2 0 21 "21 21" _null_ _null_ _null_ _null_ _null_ int2_dist _null_ _null_ _null_ ));

0 commit comments

Comments
 (0)