E
E
eliasum2022-01-18 14:41:11
PostgreSQL
eliasum, 2022-01-18 14:41:11

How to return a temporary table from a hypertable?

There is a hypertable:

CREATE TABLE IF NOT EXISTS hyptab(
key integer NOT NULL,
tstamp timestamptz NOT NULL,
val FLOAT NOT NULL);

SELECT create_hypertable(
'hyptab', 'tstamp',
chunk_time_interval => INTERVAL '1 day',
if_not_exists => TRUE
);


Filled with data for example:
INSERT INTO hyptab (key, tstamp, val) 
SELECT n, NOW(), n FROM (
SELECT generate_series(1, 100500) AS n
) AS newvalues


The initial and final values ​​of the selection from the hypertable come to the input of the stored procedure:
CREATE OR REPLACE FUNCTION Foo(st TIMESTAMP, fin TIMESTAMP)
RETURNS ... AS $$
DECLARE 

  --
  
BEGIN

  --
        
END;

$$ LANGUAGE plpgsql;


How can you return from a stored procedure a selection of values ​​tstamp, val in time from st to fin?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-01-19
@eliasum

Yes, as usual, I don’t think that something is different in TimescaleDB:

CREATE OR REPLACE FUNCTION Foo(st TIMESTAMP, fin TIMESTAMP)
RETURNS TABLE (tstamp timestamptz, val float) AS $$
...
RETURN QUERY SELECT tstamp, val FROM hyptab WHERE tstamp >= st AND tstamp < fin;
END;
$$ LANGUAGE plpgsql;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question