Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 93bbeec

Browse files
committed
extstats: change output functions to emit valid JSON
Manipulating extended statistics is more convenient as JSON than the current ad-hoc format, so let's change before it's too late. Discussion: https://postgr.es/m/20170420193828.k3fliiock5hdnehn@alvherre.pgsql
1 parent e950024 commit 93bbeec

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

doc/src/sgml/perform.sgml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,9 @@ ANALYZE zipcodes;
11381138
SELECT stxname, stxkeys, stxdependencies
11391139
FROM pg_statistic_ext
11401140
WHERE stxname = 'stts';
1141-
stxname | stxkeys | stxdependencies
1142-
---------+---------+--------------------------------------------
1143-
stts | 1 5 | [{1 => 5 : 1.000000}, {5 => 1 : 0.423130}]
1141+
stxname | stxkeys | stxdependencies
1142+
---------+---------+------------------------------------------
1143+
stts | 1 5 | {"1 => 5": 1.000000, "5 => 1": 0.423130}
11441144
(1 row)
11451145
</programlisting>
11461146
where it can be seen that column 1 (a zip code) fully determines column
@@ -1225,10 +1225,9 @@ ANALYZE zipcodes;
12251225
SELECT stxkeys AS k, stxndistinct AS nd
12261226
FROM pg_statistic_ext
12271227
WHERE stxname = 'stts2';
1228-
-[ RECORD 1 ]---------------------------------------------
1228+
-[ RECORD 1 ]--------------------------------------------------------
12291229
k | 1 2 5
1230-
nd | [{(b 1 2), 33178.000000}, {(b 1 5), 33178.000000},
1231-
{(b 2 5), 27435.000000}, {(b 1 2 5), 33178.000000}]
1230+
nd | {"1, 2": 33178, "1, 5": 33178, "2, 5": 27435, "1, 2, 5": 33178}
12321231
(1 row)
12331232
</programlisting>
12341233
which indicates that there are three combinations of columns that

src/backend/statistics/dependencies.c

+7-16
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ dependency_is_fully_matched(MVDependency * dependency, Bitmapset *attnums)
624624
* check that the attnum matches is implied by the functional dependency
625625
*/
626626
static bool
627-
dependency_implies_attribute(MVDependency * dependency, AttrNumber attnum)
627+
dependency_implies_attribute(MVDependency *dependency, AttrNumber attnum)
628628
{
629629
if (attnum == dependency->attributes[dependency->nattributes - 1])
630630
return true;
@@ -641,19 +641,13 @@ staext_dependencies_load(Oid mvoid)
641641
{
642642
bool isnull;
643643
Datum deps;
644-
645-
/*
646-
* Prepare to scan pg_statistic_ext for entries having indrelid = this
647-
* rel.
648-
*/
649644
HeapTuple htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(mvoid));
650645

651646
if (!HeapTupleIsValid(htup))
652647
elog(ERROR, "cache lookup failed for extended statistics %u", mvoid);
653648

654649
deps = SysCacheGetAttr(STATEXTOID, htup,
655650
Anum_pg_statistic_ext_stxdependencies, &isnull);
656-
657651
Assert(!isnull);
658652

659653
ReleaseSysCache(htup);
@@ -687,16 +681,14 @@ pg_dependencies_in(PG_FUNCTION_ARGS)
687681
Datum
688682
pg_dependencies_out(PG_FUNCTION_ARGS)
689683
{
684+
bytea *data = PG_GETARG_BYTEA_PP(0);
685+
MVDependencies *dependencies = statext_dependencies_deserialize(data);
690686
int i,
691687
j;
692688
StringInfoData str;
693689

694-
bytea *data = PG_GETARG_BYTEA_PP(0);
695-
696-
MVDependencies *dependencies = statext_dependencies_deserialize(data);
697-
698690
initStringInfo(&str);
699-
appendStringInfoChar(&str, '[');
691+
appendStringInfoChar(&str, '{');
700692

701693
for (i = 0; i < dependencies->ndeps; i++)
702694
{
@@ -705,7 +697,7 @@ pg_dependencies_out(PG_FUNCTION_ARGS)
705697
if (i > 0)
706698
appendStringInfoString(&str, ", ");
707699

708-
appendStringInfoChar(&str, '{');
700+
appendStringInfoChar(&str, '"');
709701
for (j = 0; j < dependency->nattributes; j++)
710702
{
711703
if (j == dependency->nattributes - 1)
@@ -715,11 +707,10 @@ pg_dependencies_out(PG_FUNCTION_ARGS)
715707

716708
appendStringInfo(&str, "%d", dependency->attributes[j]);
717709
}
718-
appendStringInfo(&str, " : %f", dependency->degree);
719-
appendStringInfoChar(&str, '}');
710+
appendStringInfo(&str, "\": %f", dependency->degree);
720711
}
721712

722-
appendStringInfoChar(&str, ']');
713+
appendStringInfoChar(&str, '}');
723714

724715
PG_RETURN_CSTRING(str.data);
725716
}

src/backend/statistics/mvdistinct.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,26 @@ pg_ndistinct_out(PG_FUNCTION_ARGS)
354354
StringInfoData str;
355355

356356
initStringInfo(&str);
357-
appendStringInfoChar(&str, '[');
357+
appendStringInfoChar(&str, '{');
358358

359359
for (i = 0; i < ndist->nitems; i++)
360360
{
361361
MVNDistinctItem item = ndist->items[i];
362+
int x = -1;
363+
bool first = true;
362364

363365
if (i > 0)
364366
appendStringInfoString(&str, ", ");
365367

366-
appendStringInfoChar(&str, '{');
367-
outBitmapset(&str, item.attrs);
368-
appendStringInfo(&str, ", %f}", item.ndistinct);
368+
while ((x = bms_next_member(item.attrs, x)) >= 0)
369+
{
370+
appendStringInfo(&str, "%s%d", first ? "\"" : ", ", x);
371+
first = false;
372+
}
373+
appendStringInfo(&str, "\": %d", (int) item.ndistinct);
369374
}
370375

371-
appendStringInfoChar(&str, ']');
376+
appendStringInfoChar(&str, '}');
372377

373378
PG_RETURN_CSTRING(str.data);
374379
}

src/test/regress/expected/stats_ext.out

+6-6
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ CREATE STATISTICS s10 ON (a, b, c) FROM ndistinct;
175175
ANALYZE ndistinct;
176176
SELECT stxkind, stxndistinct
177177
FROM pg_statistic_ext WHERE stxrelid = 'ndistinct'::regclass;
178-
stxkind | stxndistinct
179-
---------+------------------------------------------------------------------------------------------------
180-
{d,f} | [{(b 3 4), 301.000000}, {(b 3 6), 301.000000}, {(b 4 6), 301.000000}, {(b 3 4 6), 301.000000}]
178+
stxkind | stxndistinct
179+
---------+---------------------------------------------------------
180+
{d,f} | {"3, 4": 301, "3, 6": 301, "4, 6": 301, "3, 4, 6": 301}
181181
(1 row)
182182

183183
-- Hash Aggregate, thanks to estimates improved by the statistic
@@ -241,9 +241,9 @@ INSERT INTO ndistinct (a, b, c, filler1)
241241
ANALYZE ndistinct;
242242
SELECT stxkind, stxndistinct
243243
FROM pg_statistic_ext WHERE stxrelid = 'ndistinct'::regclass;
244-
stxkind | stxndistinct
245-
---------+----------------------------------------------------------------------------------------------------
246-
{d,f} | [{(b 3 4), 2550.000000}, {(b 3 6), 800.000000}, {(b 4 6), 1632.000000}, {(b 3 4 6), 10000.000000}]
244+
stxkind | stxndistinct
245+
---------+-------------------------------------------------------------
246+
{d,f} | {"3, 4": 2550, "3, 6": 800, "4, 6": 1632, "3, 4, 6": 10000}
247247
(1 row)
248248

249249
-- plans using Group Aggregate, thanks to using correct esimates

0 commit comments

Comments
 (0)