11

I am discovering TYPE in PostgreSQL. I have a TABLE TYPE that some table must respect (interface). For example:

CREATE TYPE dataset AS(
    ChannelId INTEGER
   ,GranulityIdIn INTEGER
   ,GranulityId INTEGER
   ,TimeValue TIMESTAMP
   ,FloatValue FLOAT
   ,Status BIGINT
   ,QualityCodeId INTEGER
   ,DataArray FLOAT[]
   ,DataCount BIGINT
   ,Performance FLOAT
   ,StepCount INTEGER
   ,TableRegClass regclass
   ,Tags TEXT[]
   ,WeightedMean FLOAT
   ,MeanData FLOAT
   ,StdData FLOAT
   ,MinData FLOAT
   ,MaxData FLOAT
   ,MedianData FLOAT
   ,Percentiles FLOAT[]
);

I can create table using this template with:

CREATE TABLE test OF dataset;

I have seen many options in the API, but I am a little lost. I would like to know if it is possible to assign this type to function INPUT/OUTPUT parameters.

Let say that I have a FUNCTION called process that receives a sample of records from a dataset TABLE source, processes them and then returns a TABLE sink with the same TYPE.

That is I would like to know if it is possible to create a TYPE that behaves like this:

CREATE FUNCTION process(
    input dataset
) RETURNS dataset
AS ...

And that can be called like this:

SELECT
    *
FROM
    source, process(input := source) AS sink;

I wonder that it is possible with PostgreSQL, and ask how to do so. Does anyone of you know?


Here is a MWE of what I am trying to do:

DROP TABLE IF EXISTS source;
DROP FUNCTION IF EXISTS process(dataset);
DROP TYPE dataset;

CREATE TYPE dataset AS (
    id INTEGER
   ,t  TIMESTAMP
   ,x  FLOAT
);


CREATE TABLE source OF dataset;
ALTER TABLE source ADD PRIMARY KEY(Id);
INSERT INTO source VALUES
    (1, '2016-01-01 00:00:00', 10.0)
   ,(2, '2016-01-01 00:30:00', 11.0)
   ,(3, '2016-01-01 01:00:00', 12.0)
   ,(4, '2016-01-01 01:30:00',  9.0)
   ;

CREATE OR REPLACE FUNCTION process(
    _source dataset
)
RETURNS SETOF dataset
AS
$BODY$
SELECT * FROM source;
$BODY$
LANGUAGE SQL;

SELECT * FROM process(source);

But it does not succeed, it is like source is perceived as a column instead of a SETOF RECORDS with the type of dataset.

12

Your parameter _source in the added MWE is not referenced anywhere. The identifier source in the function body has no leading underscore and is interpreted as constant table name independently.

More importantly, it would not work like this anyway. SQL only allows to parameterize values in DML statements. Details in this related answer:

Solution

You can still make it work using dynamic SQL with EXECUTE in a plpgsql function. Details:

Or try this search for related questions and answers

CREATE TYPE dataset AS (id integer, t timestamp, x float);
CREATE TABLE source OF dataset (PRIMARY KEY(Id));  -- add constraints in same command

INSERT INTO source VALUES
    (1, '2016-01-01 00:00:00', 10.0)
   ,(2, '2016-01-01 00:30:00', 11.0);

CREATE OR REPLACE FUNCTION process(_tbl regclass)
  RETURNS SETOF dataset AS
$func$
BEGIN
RETURN QUERY EXECUTE 'SELECT * FROM ' || _tbl;
END
$func$  LANGUAGE plpgsql;

SELECT * FROM process('source');  -- table name as string literal 

You can even make this work for any given table:

CREATE OR REPLACE FUNCTION process2(_tbl anyelement)
  RETURNS SETOF anyelement AS
$func$
BEGIN
RETURN QUERY EXECUTE 'SELECT * FROM ' || pg_typeof(_tbl);
END
$func$  LANGUAGE plpgsql;

SELECT * FROM process2(NULL::source);  -- note the call syntax!!

Detailed explanation:

  • Thanks for answering. Il will check it in few hours. Just to know before testing, is your solution accepting to receive rows from as SELECT. I mean SELECT * FROM process((SELECT * FROM source WHERE cond)). – jlandercy Jun 21 '16 at 9:35
  • @j: No, you pass a table name. There is no way to pass a table itself (no table variable). There are several ways around it. Related: stackoverflow.com/a/27853965/939860 or stackoverflow.com/a/31167928/939860. To work on the result of a query I would use a cursor or a temporary table ... – Erwin Brandstetter Jun 21 '16 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.