0001-Make-pgstattuple-use-heap_form_tuple-instead-of-Buil.patch
text/x-diff
Filename: 0001-Make-pgstattuple-use-heap_form_tuple-instead-of-Buil.patch
Type: text/x-diff
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch 0001
| File | + | − |
|---|---|---|
| contrib/pgstattuple/pgstatindex.c | 21 | 22 |
| contrib/pgstattuple/pgstattuple.c | 16 | 29 |
>From 421267bba8394255feed8f9b9424d25d0be9f979 Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon, 11 May 2015 15:54:48 +0530
Subject: Make pgstattuple use heap_form_tuple instead of
BuildTupleFromCStrings
---
contrib/pgstattuple/pgstatindex.c | 43 ++++++++++++++++++-------------------
contrib/pgstattuple/pgstattuple.c | 45 ++++++++++++++-------------------------
2 files changed, 37 insertions(+), 51 deletions(-)
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index a2ea5d7..608f729 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -257,7 +257,8 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
{
TupleDesc tupleDesc;
int j;
- char *values[10];
+ Datum values[10];
+ bool nulls[10];
HeapTuple tuple;
/* Build a tuple descriptor for our result type */
@@ -265,33 +266,31 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
elog(ERROR, "return type must be a row type");
j = 0;
- values[j++] = psprintf("%d", indexStat.version);
- values[j++] = psprintf("%d", indexStat.level);
- values[j++] = psprintf(INT64_FORMAT,
- (indexStat.root_pages +
- indexStat.leaf_pages +
- indexStat.internal_pages +
- indexStat.deleted_pages +
- indexStat.empty_pages) * BLCKSZ);
- values[j++] = psprintf("%u", indexStat.root_blkno);
- values[j++] = psprintf(INT64_FORMAT, indexStat.internal_pages);
- values[j++] = psprintf(INT64_FORMAT, indexStat.leaf_pages);
- values[j++] = psprintf(INT64_FORMAT, indexStat.empty_pages);
- values[j++] = psprintf(INT64_FORMAT, indexStat.deleted_pages);
+ values[j++] = Int32GetDatum(indexStat.version);
+ values[j++] = Int32GetDatum(indexStat.level);
+ values[j++] = Int64GetDatum((indexStat.root_pages +
+ indexStat.leaf_pages +
+ indexStat.internal_pages +
+ indexStat.deleted_pages +
+ indexStat.empty_pages) * BLCKSZ);
+ values[j++] = Int32GetDatum(indexStat.root_blkno);
+ values[j++] = Int32GetDatum(indexStat.internal_pages);
+ values[j++] = Int32GetDatum(indexStat.leaf_pages);
+ values[j++] = Int32GetDatum(indexStat.empty_pages);
+ values[j++] = Int32GetDatum(indexStat.deleted_pages);
if (indexStat.max_avail > 0)
- values[j++] = psprintf("%.2f",
- 100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
+ values[j++] = Float8GetDatum(100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
else
- values[j++] = pstrdup("NaN");
+ values[j++] = CStringGetDatum("NaN");
if (indexStat.leaf_pages > 0)
- values[j++] = psprintf("%.2f",
- (double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
+ values[j++] = Float8GetDatum((double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
else
- values[j++] = pstrdup("NaN");
+ values[j++] = CStringGetDatum("NaN");
- tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
- values);
+ for (j = 0; j < 10; j++)
+ nulls[j] = false;
+ tuple = heap_form_tuple(tupleDesc, values, nulls);
result = HeapTupleGetDatum(tuple);
}
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index c3a8b1d..552f188 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -85,28 +85,20 @@ static Datum
build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
{
#define NCOLUMNS 9
-#define NCHARS 32
HeapTuple tuple;
- char *values[NCOLUMNS];
- char values_buf[NCOLUMNS][NCHARS];
+ Datum values[NCOLUMNS];
+ bool nulls[NCOLUMNS];
int i;
double tuple_percent;
double dead_tuple_percent;
double free_percent; /* free/reusable space in % */
TupleDesc tupdesc;
- AttInMetadata *attinmeta;
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
- /*
- * Generate attribute metadata needed later to produce tuples from raw C
- * strings
- */
- attinmeta = TupleDescGetAttInMetadata(tupdesc);
-
if (stat->table_len == 0)
{
tuple_percent = 0.0;
@@ -120,26 +112,21 @@ build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
free_percent = 100.0 * stat->free_space / stat->table_len;
}
- /*
- * Prepare a values array for constructing the tuple. This should be an
- * array of C strings which will be processed later by the appropriate
- * "in" functions.
- */
- for (i = 0; i < NCOLUMNS; i++)
- values[i] = values_buf[i];
i = 0;
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->table_len);
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_count);
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_len);
- snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_count);
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_len);
- snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
- snprintf(values[i++], NCHARS, INT64_FORMAT, stat->free_space);
- snprintf(values[i++], NCHARS, "%.2f", free_percent);
-
- /* build a tuple */
- tuple = BuildTupleFromCStrings(attinmeta, values);
+ values[i++] = Int64GetDatum(stat->table_len);
+ values[i++] = Int64GetDatum(stat->tuple_count);
+ values[i++] = Int64GetDatum(stat->tuple_len);
+ values[i++] = Float8GetDatum(tuple_percent);
+ values[i++] = Int64GetDatum(stat->dead_tuple_count);
+ values[i++] = Int64GetDatum(stat->dead_tuple_len);
+ values[i++] = Float8GetDatum(dead_tuple_percent);
+ values[i++] = Int64GetDatum(stat->free_space);
+ values[i++] = Float8GetDatum(free_percent);
+
+ for (i = 0; i < NCOLUMNS; i++)
+ nulls[i] = false;
+
+ tuple = heap_form_tuple(tupdesc, values, nulls);
/* make the tuple into a datum */
return HeapTupleGetDatum(tuple);
--
1.9.1