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

Commit 00d4099

Browse files
author
Nikita Glukhov
committed
Add jsonb statistics
1 parent dcd467a commit 00d4099

File tree

15 files changed

+3944
-10
lines changed

15 files changed

+3944
-10
lines changed

src/backend/catalog/system_functions.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,42 @@ LANGUAGE internal
594594
STRICT IMMUTABLE PARALLEL SAFE
595595
AS 'unicode_is_normalized';
596596

597+
-- XXX is this function immutable / parallel safe?
598+
-- XXX do we actually need to cast to text and then to jsonb?
599+
CREATE FUNCTION pg_json_path_stats(tab regclass, path_index integer) RETURNS text
600+
AS $$
601+
SELECT jsonb_pretty((
602+
CASE
603+
WHEN stakind1 = 8 THEN stavalues1
604+
WHEN stakind2 = 8 THEN stavalues2
605+
WHEN stakind3 = 8 THEN stavalues3
606+
WHEN stakind4 = 8 THEN stavalues4
607+
WHEN stakind5 = 8 THEN stavalues5
608+
END::text::jsonb[])[$2])
609+
FROM pg_statistic
610+
WHERE starelid = $1
611+
$$ LANGUAGE 'sql';
612+
613+
-- XXX is this function immutable / parallel safe?
614+
-- XXX do we actually need to cast to text and then to jsonb?
615+
CREATE FUNCTION pg_json_path_stats(tab regclass, path text) RETURNS text
616+
AS $$
617+
SELECT jsonb_pretty(pathstats)
618+
FROM (
619+
SELECT unnest(
620+
CASE
621+
WHEN stakind1 = 8 THEN stavalues1
622+
WHEN stakind2 = 8 THEN stavalues2
623+
WHEN stakind3 = 8 THEN stavalues3
624+
WHEN stakind4 = 8 THEN stavalues4
625+
WHEN stakind5 = 8 THEN stavalues5
626+
END::text::jsonb[]) pathstats
627+
FROM pg_statistic
628+
WHERE starelid = $1
629+
) paths
630+
WHERE pathstats->>'path' = $2
631+
$$ LANGUAGE 'sql';
632+
597633
--
598634
-- The default permissions for functions mean that anyone can execute them.
599635
-- A number of functions shouldn't be executable by just anyone, but rather

src/backend/catalog/system_views.sql

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,62 @@ CREATE VIEW pg_stats_ext_exprs WITH (security_barrier) AS
364364
-- unprivileged users may read pg_statistic_ext but not pg_statistic_ext_data
365365
REVOKE ALL ON pg_statistic_ext_data FROM public;
366366

367+
-- XXX This probably needs to do the same checks as pg_stats, i.e.
368+
-- WHERE NOT attisdropped
369+
-- AND has_column_privilege(c.oid, a.attnum, 'select')
370+
-- AND (c.relrowsecurity = false OR NOT row_security_active(c.oid));
371+
CREATE VIEW pg_stats_json AS
372+
SELECT
373+
nspname AS schemaname,
374+
relname AS tablename,
375+
attname AS attname,
376+
377+
path->>'path' AS json_path,
378+
379+
stainherit AS inherited,
380+
381+
(path->'json'->>'nullfrac')::float4 AS null_frac,
382+
(path->'json'->>'width')::float4 AS avg_width,
383+
(path->'json'->>'distinct')::float4 AS n_distinct,
384+
385+
ARRAY(SELECT val FROM jsonb_array_elements(
386+
path->'json'->'mcv'->'values') val)::anyarray
387+
AS most_common_vals,
388+
389+
ARRAY(SELECT num::text::float4 FROM jsonb_array_elements(
390+
path->'json'->'mcv'->'numbers') num)
391+
AS most_common_freqs,
392+
393+
ARRAY(SELECT val FROM jsonb_array_elements(
394+
path->'json'->'histogram'->'values') val)
395+
AS histogram_bounds,
396+
397+
ARRAY(SELECT val::text::int FROM jsonb_array_elements(
398+
path->'array_length'->'mcv'->'values') val)
399+
AS most_common_array_lengths,
400+
401+
ARRAY(SELECT num::text::float4 FROM jsonb_array_elements(
402+
path->'array_length'->'mcv'->'numbers') num)
403+
AS most_common_array_length_freqs,
404+
405+
(path->'json'->>'correlation')::float4 AS correlation
406+
407+
FROM
408+
pg_statistic s JOIN pg_class c ON (c.oid = s.starelid)
409+
JOIN pg_attribute a ON (c.oid = attrelid AND attnum = s.staattnum)
410+
LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace),
411+
LATERAL (
412+
SELECT unnest((CASE
413+
WHEN stakind1 = 8 THEN stavalues1
414+
WHEN stakind2 = 8 THEN stavalues2
415+
WHEN stakind3 = 8 THEN stavalues3
416+
WHEN stakind4 = 8 THEN stavalues4
417+
WHEN stakind5 = 8 THEN stavalues5
418+
END ::text::jsonb[])[2:]) AS path
419+
) paths;
420+
421+
-- no need to revoke any privileges, we've already revoked accss to pg_statistic
422+
367423
CREATE VIEW pg_publication_tables AS
368424
SELECT
369425
P.pubname AS pubname,

src/backend/utils/adt/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ OBJS = \
5050
jsonb.o \
5151
jsonb_gin.o \
5252
jsonb_op.o \
53+
jsonb_selfuncs.o \
54+
jsonb_typanalyze.o \
5355
jsonb_util.o \
5456
jsonfuncs.o \
5557
jsonbsubs.o \

0 commit comments

Comments
 (0)