add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
CREATE TABLE country(entity_id BIGINT, valid tsrange);


CREATE TABLE
INSERT INTO country
VALUES(1, '[2016-06-22 19:10:25-07, 2019-01-01 00:00:00)')
,(1,'[2018-06-22 19:10:25-07, 2020-01-01 00:00:00)')
INSERT 0 2
CREATE OR REPLACE AGGREGATE range_merge(anyrange)
(
sfunc = range_merge,
stype = anyrange
);
CREATE AGGREGATE
CREATE OR REPLACE FUNCTION aggregate_validity(entity_name regclass, entry bigint) returns tsrange AS
$$
DECLARE
result tsrange;
BEGIN
EXECUTE format('select range_merge(valid) from %I where entity_id = %s', entity_name, entry) into result;
return result;
END
$$ LANGUAGE plpgsql;
CREATE FUNCTION
select * from aggregate_validity('country', 1)
aggregate_validity
["2016-06-22 19:10:25","2020-01-01 00:00:00")
SELECT 1