Skip to content

Commit c5037c7

Browse files
committed
Add cfs_fragmentation function
1 parent 7a12174 commit c5037c7

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/backend/storage/file/cfs.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,11 @@ static bool cfs_gc_file(char* map_path)
702702
endTime = GetCurrentTimestamp();
703703
TimestampDifference(startTime, endTime, &secs, &usecs);
704704

705-
elog(LOG, "%d: defragment file %s: old size %d, new size %d, logical size %d, used %d, compression ratio %f, time %ld usec",
706-
MyProcPid, file_path, physSize, newSize, virtSize, usedSize, (double)virtSize/newSize,
707-
secs*USECS_PER_SEC + usecs);
708-
705+
if (succeed) {
706+
elog(LOG, "%d: defragment file %s: old size %d, new size %d, logical size %d, used %d, compression ratio %f, time %ld usec",
707+
MyProcPid, file_path, physSize, newSize, virtSize, usedSize, (double)virtSize/newSize,
708+
secs*USECS_PER_SEC + usecs);
709+
}
709710
pfree(file_path);
710711
pfree(file_bck_path);
711712
pfree(map_bck_path);
@@ -839,6 +840,7 @@ PG_FUNCTION_INFO_V1(cfs_enable_gc);
839840
PG_FUNCTION_INFO_V1(cfs_version);
840841
PG_FUNCTION_INFO_V1(cfs_estimate);
841842
PG_FUNCTION_INFO_V1(cfs_compression_ratio);
843+
PG_FUNCTION_INFO_V1(cfs_fragmentation);
842844
PG_FUNCTION_INFO_V1(cfs_gc_activity_processed_bytes);
843845
PG_FUNCTION_INFO_V1(cfs_gc_activity_processed_pages);
844846
PG_FUNCTION_INFO_V1(cfs_gc_activity_processed_files);
@@ -985,6 +987,55 @@ Datum cfs_compression_ratio(PG_FUNCTION_ARGS)
985987
PG_RETURN_FLOAT8((double)virtSize/physSize);
986988
}
987989

990+
Datum cfs_fragmentation(PG_FUNCTION_ARGS)
991+
{
992+
Oid oid = PG_GETARG_OID(0);
993+
Relation rel = try_relation_open(oid, AccessShareLock);
994+
uint64 usedSize = 0;
995+
uint64 physSize = 0;
996+
997+
if (rel != NULL) {
998+
char* path = relpathbackend(rel->rd_node, rel->rd_backend, MAIN_FORKNUM);
999+
char* map_path = (char*)palloc(strlen(path) + 16);
1000+
int i = 0;
1001+
1002+
while (true) {
1003+
int md;
1004+
FileMap* map;
1005+
1006+
if (i == 0) {
1007+
sprintf(map_path, "%s.cfm", path);
1008+
} else {
1009+
sprintf(map_path, "%s.%u.cfm", path, i);
1010+
}
1011+
md = open(map_path, O_RDWR|PG_BINARY, 0);
1012+
if (md < 0) {
1013+
break;
1014+
}
1015+
map = cfs_mmap(md);
1016+
if (map == MAP_FAILED) {
1017+
elog(LOG, "cfs_compression_ration failed to map file %s: %m", map_path);
1018+
close(md);
1019+
break;
1020+
}
1021+
usedSize += pg_atomic_read_u32(&map->usedSize);
1022+
physSize += pg_atomic_read_u32(&map->physSize);
1023+
1024+
if (cfs_munmap(map) < 0) {
1025+
elog(LOG, "Failed to unmap file %s: %m", map_path);
1026+
}
1027+
if (close(md) < 0) {
1028+
elog(LOG, "Failed to close file %s: %m", map_path);
1029+
}
1030+
i += 1;
1031+
}
1032+
pfree(path);
1033+
pfree(map_path);
1034+
relation_close(rel, AccessShareLock);
1035+
}
1036+
PG_RETURN_FLOAT8((double)physSize/usedSize);
1037+
}
1038+
9881039
Datum cfs_gc_activity_processed_bytes(PG_FUNCTION_ARGS)
9891040
{
9901041
PG_RETURN_INT64(cfs_state->gc_stat.processedBytes);

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5358,6 +5358,8 @@ DATA(insert OID = 6107 ( cfs_gc_activity_processed_pages PGNSP PGUID 12 1 0
53585358
DESCR("Number of transferred pages by CFS garbage collectors since system start");
53595359
DATA(insert OID = 6108 ( cfs_gc_activity_scanned_files PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 20 "" _null_ _null_ _null_ _null_ _null_ cfs_gc_activity_scanned_files _null_ _null_ _null_ ));
53605360
DESCR("Number of files scanned by CFS garbage collectors since system start");
5361+
DATA(insert OID = 6109 ( cfs_fragmentation 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_fragmentation _null_ _null_ _null_ ));
5362+
DESCR("Fragmentation level of relation");
53615363

53625364
/* distance functions */
53635365
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)