Skip to content

Commit 9c132a3

Browse files
committed
Refactor stats API and remove ptrack_get_change_file_stat
1 parent ab17447 commit 9c132a3

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

Diff for: README.md

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ To disable `ptrack` and clean up all remaining service files set `ptrack.map_siz
6767
* ptrack_init_lsn() — returns LSN of the last ptrack map initialization.
6868
* ptrack_get_pagemapset(start_lsn pg_lsn) — returns a set of changed data files with bitmaps of changed blocks since specified `start_lsn`.
6969
* ptrack_get_change_stat(start_lsn pg_lsn) — returns statistic of changes (number of files, pages and size in MB) since specified `start_lsn`.
70-
* ptrack_get_change_file_stat(start_lsn pg_lsn) — returns per file statistic of changes (number of pages and size in MB) since specified `start_lsn`.
7170

7271
Usage example:
7372

Diff for: ptrack--2.1--2.2.sql

+11-30
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
-- Complain if script is sourced in psql, rather than via ALTER EXTENSION
44
\echo Use "ALTER EXTENSION ptrack UPDATE;" to load this file.\ quit
55

6+
DROP FUNCTION ptrack_get_pagemapset(start_lsn pg_lsn);
7+
CREATE FUNCTION ptrack_get_pagemapset(start_lsn pg_lsn)
8+
RETURNS TABLE (path text,
9+
pagecount bigint,
10+
pagemap bytea)
11+
AS 'MODULE_PATHNAME'
12+
LANGUAGE C STRICT VOLATILE;
13+
614
CREATE FUNCTION ptrack_get_change_stat(start_lsn pg_lsn)
715
RETURNS TABLE (
816
files bigint,
9-
pages bigint,
17+
pages numeric,
1018
"size, MB" numeric
1119
) AS
1220
$func$
@@ -18,37 +26,10 @@ BEGIN
1826
RETURN QUERY
1927
SELECT changed_files,
2028
changed_pages,
21-
block_size*changed_pages/(1024.0*1024)
29+
block_size * changed_pages / (1024.0 * 1024)
2230
FROM
2331
(SELECT count(path) AS changed_files,
24-
sum(
25-
length(replace(right((pagemap)::text, -1)::varbit::text, '0', ''))
26-
) AS changed_pages
32+
sum(pagecount) AS changed_pages
2733
FROM ptrack_get_pagemapset(start_lsn)) s;
2834
END
2935
$func$ LANGUAGE plpgsql;
30-
31-
CREATE FUNCTION ptrack_get_change_file_stat(start_lsn pg_lsn)
32-
RETURNS TABLE (
33-
file_path text,
34-
pages int,
35-
"size, MB" numeric
36-
) AS
37-
$func$
38-
DECLARE
39-
block_size bigint;
40-
BEGIN
41-
block_size := (SELECT setting FROM pg_settings WHERE name = 'block_size');
42-
43-
RETURN QUERY
44-
SELECT s.path,
45-
changed_pages,
46-
block_size*changed_pages/(1024.0*1024)
47-
FROM
48-
(SELECT path,
49-
length(replace(right((pagemap)::text, -1)::varbit::text, '0', ''))
50-
AS changed_pages
51-
FROM ptrack_get_pagemapset(start_lsn)) s
52-
ORDER BY (changed_pages, s.path) DESC;
53-
END
54-
$func$ LANGUAGE plpgsql;

Diff for: ptrack.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
424424
FuncCallContext *funcctx;
425425
MemoryContext oldcontext;
426426
datapagemap_t pagemap;
427+
int64 pagecount = 0;
427428
char gather_path[MAXPGPATH];
428429

429430
/* Exit immediately if there is no map */
@@ -444,12 +445,13 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
444445

445446
/* Make tuple descriptor */
446447
#if PG_VERSION_NUM >= 120000
447-
tupdesc = CreateTemplateTupleDesc(2);
448+
tupdesc = CreateTemplateTupleDesc(3);
448449
#else
449-
tupdesc = CreateTemplateTupleDesc(2, false);
450+
tupdesc = CreateTemplateTupleDesc(3, false);
450451
#endif
451452
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "path", TEXTOID, -1, 0);
452-
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "pagemap", BYTEAOID, -1, 0);
453+
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "pagecount", INT8OID, -1, 0);
454+
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "pagemap", BYTEAOID, -1, 0);
453455
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
454456

455457
funcctx->user_fctx = ctx;
@@ -497,8 +499,8 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
497499
/* We completed a segment and there is a bitmap to return */
498500
if (pagemap.bitmap != NULL)
499501
{
500-
Datum values[2];
501-
bool nulls[2] = {false};
502+
Datum values[3];
503+
bool nulls[3] = {false};
502504
char pathname[MAXPGPATH];
503505
bytea *result = NULL;
504506
Size result_sz = pagemap.bitmapsize + VARHDRSZ;
@@ -512,11 +514,13 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
512514
strcpy(pathname, ctx->relpath);
513515

514516
values[0] = CStringGetTextDatum(pathname);
515-
values[1] = PointerGetDatum(result);
517+
values[1] = Int64GetDatum(pagecount);
518+
values[2] = PointerGetDatum(result);
516519

517520
pfree(pagemap.bitmap);
518521
pagemap.bitmap = NULL;
519522
pagemap.bitmapsize = 0;
523+
pagecount = 0;
520524

521525
htup = heap_form_tuple(funcctx->tuple_desc, values, nulls);
522526
if (htup)
@@ -553,7 +557,10 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
553557

554558
/* Block has been changed since specified LSN. Mark it in the bitmap */
555559
if (update_lsn2 >= ctx->lsn)
560+
{
561+
pagecount += 1;
556562
datapagemap_add(&pagemap, ctx->bid.blocknum % ((BlockNumber) RELSEG_SIZE));
563+
}
557564
}
558565

559566
ctx->bid.blocknum += 1;

Diff for: t/001_basic.pl

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use TestLib;
1111
use Test::More;
1212

13-
plan tests => 25;
13+
plan tests => 24;
1414

1515
my $node;
1616
my $res;
@@ -119,9 +119,6 @@
119119
$res_stdout = $node->safe_psql("postgres", "SELECT pages FROM ptrack_get_change_stat('$flush_lsn')");
120120
is($res_stdout > 0, 1, 'should be able to get aggregated stats of changes');
121121

122-
$res_stdout = $node->safe_psql("postgres", "SELECT count(*) FROM ptrack_get_change_file_stat('$flush_lsn')");
123-
is($res_stdout > 0, 1, 'should be able to get per file stats of changes');
124-
125122
# We should be able to change ptrack map size (but loose all changes)
126123
$node->append_conf(
127124
'postgresql.conf', q{

0 commit comments

Comments
 (0)