Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 302879b

Browse files
committed
Add support for "dependencies" in pg_restore_extended_stats()
This commit adds support for the restore of extended statistics of the kind "dependencies", for the following input data: [{"attributes": [2], "dependency": 3, "degree": 1.000000}, {"attributes": [3], "dependency": 2, "degree": 1.000000}] This relies on the existing routines of "dependencies" to cross-check the input data with the definition of the extended statistics objects for the attribute numbers. An input argument of type "pg_dependencies" is required for this new option. Thanks to the work done in 0e80f3f for the restore function and e1405aa for the input handling of data type pg_dependencies, this addition is straight-forward. This will be used so as it is possible to transfer these statistics across dumps and upgrades, removing the need for a post-operation ANALYZE for these kinds of statistics. Author: Corey Huinker <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Discussion: https://postgr.es/m/CADkLM=dpz3KFnqP-dgJ-zvRvtjsa8UZv8wDAQdqho=qN3kX0Zg@mail.gmail.com
1 parent 19af794 commit 302879b

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

doc/src/sgml/func/func-admin.sgml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,9 +2194,8 @@ SELECT pg_restore_attribute_stats(
21942194
</programlisting>
21952195
</para>
21962196
<para>
2197-
For example, to set the <structfield>n_distinct</structfield>,
2198-
<structfield>dependencies</structfield>, and <structfield>exprs</structfield>
2199-
values for the statistics object <structname>myschema.mystatsobj</structname>:
2197+
For example, to set some values for the statistics object
2198+
<structname>myschema.mystatsobj</structname>:
22002199
<programlisting>
22012200
SELECT pg_restore_extended_stats(
22022201
'schemaname', 'tab_schema'::name,
@@ -2223,7 +2222,8 @@ SELECT pg_restore_attribute_stats(
22232222
Other arguments are the names and values of statistics corresponding
22242223
to columns in <link linkend="view-pg-stats-ext"><structname>pg_stats_ext</structname>
22252224
</link>.
2226-
This function currently supports <literal>n_distinct</literal>.
2225+
This function currently supports <literal>n_distinct</literal> and
2226+
<literal>dependencies</literal>.
22272227
</para>
22282228
<para>
22292229
Additionally, this function accepts argument name

src/backend/statistics/extended_stats_funcs.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum extended_stats_argnum
4545
STATNAME_ARG,
4646
INHERITED_ARG,
4747
NDISTINCT_ARG,
48+
DEPENDENCIES_ARG,
4849
NUM_EXTENDED_STATS_ARGS,
4950
};
5051

@@ -60,6 +61,7 @@ static struct StatsArgInfo extarginfo[] =
6061
[STATNAME_ARG] = {"statistics_name", TEXTOID},
6162
[INHERITED_ARG] = {"inherited", BOOLOID},
6263
[NDISTINCT_ARG] = {"n_distinct", PG_NDISTINCTOID},
64+
[DEPENDENCIES_ARG] = {"dependencies", PG_DEPENDENCIESOID},
6365
[NUM_EXTENDED_STATS_ARGS] = {0},
6466
};
6567

@@ -257,6 +259,7 @@ extended_statistics_update(FunctionCallInfo fcinfo)
257259
* were provided to the function.
258260
*/
259261
has.ndistinct = !PG_ARGISNULL(NDISTINCT_ARG);
262+
has.dependencies = !PG_ARGISNULL(DEPENDENCIES_ARG);
260263

261264
if (RecoveryInProgress())
262265
{
@@ -355,6 +358,23 @@ extended_statistics_update(FunctionCallInfo fcinfo)
355358
success = false;
356359
}
357360

361+
/*
362+
* If the object cannot support dependencies, we should not have data for
363+
* it.
364+
*/
365+
if (has.dependencies && !enabled.dependencies)
366+
{
367+
ereport(WARNING,
368+
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
369+
errmsg("cannot specify parameter \"%s\".",
370+
extarginfo[DEPENDENCIES_ARG].argname),
371+
errhint("Extended statistics object \"%s\".\"%s\" does not support statistics of this type.",
372+
quote_identifier(nspname),
373+
quote_identifier(stxname)));
374+
has.dependencies = false;
375+
success = false;
376+
}
377+
358378
/*
359379
* Populate the pg_statistic_ext_data result tuple.
360380
*/
@@ -396,6 +416,24 @@ extended_statistics_update(FunctionCallInfo fcinfo)
396416
statext_ndistinct_free(ndistinct);
397417
}
398418

419+
if (has.dependencies)
420+
{
421+
Datum dependencies_datum = PG_GETARG_DATUM(DEPENDENCIES_ARG);
422+
bytea *data = DatumGetByteaPP(dependencies_datum);
423+
MVDependencies *dependencies = statext_dependencies_deserialize(data);
424+
425+
if (statext_dependencies_validate(dependencies, &stxform->stxkeys, numexprs, WARNING))
426+
{
427+
values[Anum_pg_statistic_ext_data_stxddependencies - 1] = dependencies_datum;
428+
nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = false;
429+
replaces[Anum_pg_statistic_ext_data_stxddependencies - 1] = true;
430+
}
431+
else
432+
success = false;
433+
434+
statext_dependencies_free(dependencies);
435+
}
436+
399437
upsert_pg_statistic_ext_data(values, nulls, replaces);
400438

401439
cleanup:

src/test/regress/expected/stats_import.out

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,22 @@ HINT: Extended statistics object "stats_import"."test_stat_dependencies" does n
17561756
f
17571757
(1 row)
17581758

1759+
-- Incorrect extended stats kind, dependencies not supported
1760+
SELECT pg_catalog.pg_restore_extended_stats(
1761+
'schemaname', 'stats_import',
1762+
'relname', 'test',
1763+
'statistics_schemaname', 'stats_import',
1764+
'statistics_name', 'test_stat_ndistinct',
1765+
'inherited', false,
1766+
'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
1767+
{"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
1768+
WARNING: cannot specify parameter "dependencies".
1769+
HINT: Extended statistics object "stats_import"."test_stat_ndistinct" does not support statistics of this type.
1770+
pg_restore_extended_stats
1771+
---------------------------
1772+
f
1773+
(1 row)
1774+
17591775
-- ok: ndistinct
17601776
SELECT pg_catalog.pg_restore_extended_stats(
17611777
'schemaname', 'stats_import',
@@ -1769,6 +1785,35 @@ SELECT pg_catalog.pg_restore_extended_stats(
17691785
t
17701786
(1 row)
17711787

1788+
-- dependencies value doesn't match definition
1789+
SELECT pg_catalog.pg_restore_extended_stats(
1790+
'schemaname', 'stats_import',
1791+
'relname', 'test',
1792+
'statistics_schemaname', 'stats_import',
1793+
'statistics_name', 'test_stat_dependencies',
1794+
'inherited', false,
1795+
'dependencies', '[{"attributes": [1], "dependency": 3, "degree": 1.000000},
1796+
{"attributes": [3], "dependency": 1, "degree": 1.000000}]'::pg_dependencies);
1797+
WARNING: could not validate "pg_dependencies" object: invalid attribute number 1 found
1798+
pg_restore_extended_stats
1799+
---------------------------
1800+
f
1801+
(1 row)
1802+
1803+
-- ok: dependencies
1804+
SELECT pg_catalog.pg_restore_extended_stats(
1805+
'schemaname', 'stats_import',
1806+
'relname', 'test',
1807+
'statistics_schemaname', 'stats_import',
1808+
'statistics_name', 'test_stat_dependencies',
1809+
'inherited', false,
1810+
'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
1811+
{"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
1812+
pg_restore_extended_stats
1813+
---------------------------
1814+
t
1815+
(1 row)
1816+
17721817
SELECT replace(e.n_distinct, '}, ', E'},\n') AS n_distinct
17731818
FROM pg_stats_ext AS e
17741819
WHERE e.statistics_schemaname = 'stats_import' AND

src/test/regress/sql/stats_import.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,15 @@ SELECT pg_catalog.pg_restore_extended_stats(
12581258
'statistics_name', 'test_stat_dependencies',
12591259
'inherited', false,
12601260
'n_distinct', '[{"attributes" : [1,3], "ndistinct" : 4}]'::pg_ndistinct);
1261+
-- Incorrect extended stats kind, dependencies not supported
1262+
SELECT pg_catalog.pg_restore_extended_stats(
1263+
'schemaname', 'stats_import',
1264+
'relname', 'test',
1265+
'statistics_schemaname', 'stats_import',
1266+
'statistics_name', 'test_stat_ndistinct',
1267+
'inherited', false,
1268+
'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
1269+
{"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
12611270

12621271
-- ok: ndistinct
12631272
SELECT pg_catalog.pg_restore_extended_stats(
@@ -1268,6 +1277,26 @@ SELECT pg_catalog.pg_restore_extended_stats(
12681277
'inherited', false,
12691278
'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4}]'::pg_ndistinct);
12701279

1280+
-- dependencies value doesn't match definition
1281+
SELECT pg_catalog.pg_restore_extended_stats(
1282+
'schemaname', 'stats_import',
1283+
'relname', 'test',
1284+
'statistics_schemaname', 'stats_import',
1285+
'statistics_name', 'test_stat_dependencies',
1286+
'inherited', false,
1287+
'dependencies', '[{"attributes": [1], "dependency": 3, "degree": 1.000000},
1288+
{"attributes": [3], "dependency": 1, "degree": 1.000000}]'::pg_dependencies);
1289+
1290+
-- ok: dependencies
1291+
SELECT pg_catalog.pg_restore_extended_stats(
1292+
'schemaname', 'stats_import',
1293+
'relname', 'test',
1294+
'statistics_schemaname', 'stats_import',
1295+
'statistics_name', 'test_stat_dependencies',
1296+
'inherited', false,
1297+
'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
1298+
{"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
1299+
12711300
SELECT replace(e.n_distinct, '}, ', E'},\n') AS n_distinct
12721301
FROM pg_stats_ext AS e
12731302
WHERE e.statistics_schemaname = 'stats_import' AND

0 commit comments

Comments
 (0)