2018-06-21 00:30:03 +08:00
|
|
|
-- Taken from https://wiki.postgresql.org/wiki/Count_estimate
|
2018-07-18 20:29:50 +08:00
|
|
|
CREATE FUNCTION cdb_dataservices_client.cdb_count_estimate(query text) RETURNS INTEGER AS
|
2018-06-21 00:30:03 +08:00
|
|
|
$func$
|
|
|
|
DECLARE
|
|
|
|
rec record;
|
|
|
|
ROWS INTEGER;
|
|
|
|
BEGIN
|
|
|
|
FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
|
|
|
|
ROWS := SUBSTRING(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
|
|
|
|
EXIT WHEN ROWS IS NOT NULL;
|
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
RETURN ROWS;
|
|
|
|
END
|
2018-07-17 01:55:04 +08:00
|
|
|
$func$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
-- Taken from https://stackoverflow.com/a/48013356/351721
|
2018-07-18 20:29:50 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_client.cdb_jsonb_array_casttext(jsonb) RETURNS text[] AS $f$
|
2018-07-17 01:55:04 +08:00
|
|
|
SELECT array_agg(x) || ARRAY[]::text[] FROM jsonb_array_elements_text($1) t(x);
|
|
|
|
$f$ LANGUAGE sql IMMUTABLE;
|