CREATE FUNCTION pg_lock_hash_info()
RETURNS TABLE (
    dsize INT,
    nsegs INT,
    max_bucket INT,
    keysize INT,
    entrysize INT,
    num_partitions INT,
    max_dsize INT,
    ssize INT,
    sshift INT,
    nelem_alloc INT,
    nfree INT,
    nlockents INT,
    low_mask INT,
    high_mask INT
) AS 'pg_lock_hash_info', 'pg_lock_hash_info'
LANGUAGE C STRICT;

CREATE FUNCTION pg_lock_hash_freelist_info()
RETURNS TABLE (
    freelist INT,
    nentries INT,
    count INT
) AS 'pg_lock_hash_info', 'pg_lock_hash_freelist_info'
LANGUAGE C STRICT;

CREATE FUNCTION pg_lock_hash_bucket_info()
RETURNS TABLE (
    segment INT,
    bucket INT,
    bucket_hash INT,
    bucket_hash_low INT,
    bucket_hash_high INT,
    entries INT
) AS 'pg_lock_hash_info', 'pg_lock_hash_bucket_info'
LANGUAGE C STRICT;


CREATE VIEW pg_lock_hash_info AS
SELECT * from pg_lock_hash_info();

CREATE VIEW pg_lock_hash_freelist_info AS
SELECT * FROM pg_lock_hash_freelist_info();

CREATE VIEW pg_lock_hash_bucket_info AS
SELECT * from pg_lock_hash_bucket_info();

CREATE VIEW pg_lock_hash_freelist_summary AS
SELECT count(*) AS freelist_total,
       sum(nentries) AS nentries_total,
       avg(nentries) AS nentries_avg,
       stddev_pop(nentries) nentries_stddev,
       sum(count) AS count_total,
       avg(count) AS count_avg,
       stddev_pop(count) AS count_stddev,
       sum(nentries+count) AS total
FROM pg_lock_hash_freelist_info();

CREATE VIEW pg_lock_hash_bucket_summary AS
SELECT count(*) AS buckets_total,
       sum(entries) AS total_active_locks,
       avg(entries) AS avg_bucket_size,
       stddev_pop(entries) AS stddev_bucket_size
FROM pg_lock_hash_bucket_info();
