How to return an array of table records in PostgreSQL - postgresql

I am getting a type mismatch error when trying to return an array of elements of type table1, the inherent type of the table1 I have declared.
Error occurred during SQL query execution
Razón:
SQL Error [42P13]: ERROR: return type mismatch in function declared to return table1[]
Detail: Actual return type is record[].
Where: SQL function "arrayof_records"
This is an oversimplified code that reproduces my problem.
drop table if exists table1 cascade;
create table table1 (
id serial primary key,
title text,
create_dt timestamp default now()
);
insert into table1 (title) values
('one'),
('two'),
('three');
create or replace function arrayof_records ()
returns table1[]
stable language sql as $$
select array_agg (t.*)
from (
select * from table1
order by create_dt desc
) as t;
$$;
It is clear that the parser is expecting some other expression in the array_agg function. I have tried t, t.* and *. All of them fail.
I expect there is a syntax, as PostgreSQL 12 documentations states "array_agg(expression)|any non-array type".
Any idea?

You can use a slightly different way of creating the array:
create or replace function arrayof_records ()
returns table1[]
stable language sql
as
$$
select array(
select table1
from table1
order by create_dt desc
);
$$;
That's typically faster than array_agg() as well.

Related

Using a column of a join for argument the a function table in postgresql

I am join a table with a postgresql function table.
SELECT * FROM tb_accounts a,
(SELECT column1, column2 FROM ft_extra_data(a.id) ) e
And it is throwing me the following error:
ERROR: invalid reference to FROM-clause entry for table "a"
LINE 4: ft_extra_data(a.id)
^
HINT: There is an entry for table "a", but it cannot be referenced from this part of the query.
SQL state: 42P01
Character: 153
This would be an example of the table function (this example is to view the definition only):
CREATE OR REPLACE FUNCTION ft_extra_data(IN p_account_id bigint)
RETURNS TABLE(
column1 character varying, column2 character varying) AS
$BODY$
DECLARE
BEGIN
RETURN QUERY
SELECT 'xxx' as column1, 'yyy' as column2;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
I've been doing some research and haven't found anything, is this impossible to do?
Code example:
dbfiddle
In order to access table a in the subquery (aka derived table) you must make the join lateral:
SELECT *
FROM tb_accounts a
CROSS JOIN LATERAL ( SELECT column1, column2 FROM ft_extra_data(a.id) ) e;
Alternatively you can call put the function directly into the FROM clause without a derived table:
SELECT a.*, ft.column1, ft.column2
FROM tb_accounts a
CROSS JOIN LATERAL ft_extra_data(a.id) as ft;
In that case the lateral is optional, as ft_extra_data() is defined to return a table.

ERROR: column "int4" specified more than once

Steps for Execution:
Table Creation
CREATE TABLE xyz.table_a(
id bigint NOT NULL,
scores jsonb,
CONSTRAINT table_a_pkey PRIMARY KEY (id)
);
Add some dummy data :
INSERT INTO xyz.table_a(
id, scores)
VALUES (1, '{"a":20,"b":20}');
Function Creation
CREATE OR REPLACE FUNCTION xyz.example(
table_name text,
regular_columns text,
json_column text,
view_name text
) RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
cols TEXT;
cols_sum TEXT;
BEGIN
EXECUTE
format(
$ex$SELECT string_agg(
format(
'CAST(%2$s->>%%1$L AS INTEGER)',
key),
', '
)
FROM (SELECT DISTINCT key
FROM %1$s, jsonb_each(%2$s)
ORDER BY 1
) s;$ex$,
table_name, json_column
)
INTO cols;
EXECUTE
format(
$ex$SELECT string_agg(
format(
'CAST(%2$s->>%%1$L AS INTEGER)',
key
),
'+'
)
FROM (SELECT DISTINCT key
FROM %1$s, jsonb_each(%2$s)
ORDER BY 1) s;$ex$,
table_name, json_column
)
INTO cols_sum;
EXECUTE
format(
$ex$DROP VIEW IF EXISTS %2$s;
CREATE VIEW %2$s AS
SELECT %3$s, %4$s, SUM(%5$s) AS total
FROM %1$s
GROUP BY %3$s$ex$,
table_name, view_name, regular_columns, cols, cols_sum
);
RETURN cols;
END
$BODY$:
Call Function
SELECT xyz.example(
'xyz.table_a',
' id',
'scores',
'xyz.view_table_a'
);
Once you run these steps, I am getting an error
ERROR: column "int4" specified more than once
CONTEXT: SQL statement "
DROP VIEW IF EXISTS xyz.view_table_a;
CREATE VIEW xyz.view_table_a AS
SELECT id, CAST(scores->>'a' AS INTEGER), CAST(scores->>'b' AS INTEGER), SUM(CAST(scores->>'a' AS INTEGER)+CAST(scores->>'b' AS INTEGER)) AS total FROM xyz.table_a GROUP BY id
Look at the error message closely:
...
SELECT id, CAST(scores->>'a' AS INTEGER), CAST(scores->>'b' AS INTEGER),
...
There are multiple expressions without column alias. A named column like "id" defaults to the given name. But other expressions default to the internal type name, which is "int4" for integer. One might assume that the JSON key name is used, but that's not so. CAST(scores->>'a' AS INTEGER) is just another expression returning an unnamed integer value.
This still works for a plain SELECT. Postgres tolerates duplicate column names in the (outer) SELECT list. But a VIEW cannot be created that way. Would result in ambiguities.
Either add column aliases to expressions in the SELECT list:
SELECT id, CAST(scores->>'a' AS INTEGER) AS a, CAST(scores->>'b' AS INTEGER) AS b, ...
Or add a list of column names to CREATE VIEW:
CREATE VIEW xyz.view_table_a(id, a, b, ...) AS ...
Something like this should fix your function (preserving literal spelling of JSON key names:
...
format(
'CAST(%2$s->>%%1$L AS INTEGER) AS %%1$I',
key),
...
See the working demo here:
db<>fiddle here
Aside, your nested format() calls make the code pretty hard to read and maintain.

How to stop the "insert or update on table ...violates foreign key constraint"?

How to construct an INSERT statement so that it would not generate the error "insert or update on table ... violates foreign key constraint" in case if the foreign key value does not exist in the reference table?
I just need no record created in this case and success response.
Thank you
Use a query as the source for the INSERT statement:
insert into the_table (id, some_data, some_fk_column
select *
from (
values (42, 'foobar', 100)
) as x(id, some_data, some_fk_column)
where exists (select *
from referenced_table rt
where rt.primary_key_column = x.some_fk_column);
This can also be extended to a multi-row insert:
insert into the_table (id, some_data, some_fk_column
select *
from (
values
(42, 'foobar', 100),
(24, 'barfoo', 101)
) as x(id, some_data, some_fk_column)
where exists (select *
from referenced_table rt
where rt.primary_key_column = x.some_fk_column);
You didn't show us your table definitions so I had to make up the table and column names. You will have to translate that to your names.
You could create a function with plpgsql, which inserts a row and catches the exception:
CREATE FUNCTION customInsert(int,varchar) RETURNS VOID
AS $$
BEGIN
INSERT INTO foo VALUES ($1,$2);
EXCEPTION
WHEN foreign_key_violation THEN --do nothing
END;
$$ LANGUAGE plpgsql
You can then call this function by this:
SELECT customInsert(1,'hello');
This function tries to insert the given parameters into the table foo and catches the foreign_key_violation error if occurs.
Of course you can generalise the function more, to be able to insert in more than one table, but your question sounded like this was only needed for one specific table.

DB2 Query issue

SELECT
Q."COLUMN1"
FROM
(SELECT
"COLUMN1",
CAST ((SELECT CAST (RTRIM (PARAM) AS VARCHAR(50)) FROM TABLE_VIEW WHERE PARAM_ID = :ID) AS VARCHAR(50)) AS "COLUMN2"
FROM ("TABLE1")Q
WHERE
RTRIM(CAST("COLUMN1" AS CHAR(10))) IN (SELECT VALUE_1 FROM TABLE (SPLIT_PARAMS(CAST(Q."COLUMN2" AS VARCHAR(50)),',',5)))
COLUMN2 gets its value from a separate table based on the input provided at run time.
The filter used in the query consists of a used defined table valued function that is used to split the comma separated valued to individual values.
The query throws the error message as:
"FUNCTION NOT SUPPORTED. SQLCODE=-270, SQLSTATE=42997"
Can anyone help me find the cause of the issue.

Avoid putting PostgreSQL function result into one field

The end result of what I am after is a query that calls a function and that function returns a set of records that are in their own separate fields. I can do this but the results of the function are all in one field.
ie: http://i.stack.imgur.com/ETLCL.png and the results I am after are: http://i.stack.imgur.com/wqRQ9.png
Here's the code to create the table
CREATE TABLE tbl_1_hm
(
tbl_1_hm_id bigserial NOT NULL,
tbl_1_hm_f1 VARCHAR (250),
tbl_1_hm_f2 INTEGER,
CONSTRAINT tbl_1_hm PRIMARY KEY (tbl_1_hm_id)
)
-- do that for a few times to get some data
INSERT INTO tbl_1_hm (tbl_1_hm_f1, tbl_1_hm_f2)
VALUES ('hello', 1);
CREATE OR REPLACE FUNCTION proc_1_hm(id BIGINT)
RETURNS TABLE(tbl_1_hm_f1 VARCHAR (250), tbl_1_hm_f2 int AS $$
SELECT tbl_1_hm_f1, tbl_1_hm_f2
FROM tbl_1_hm
WHERE tbl_1_hm_id = id
$$ LANGUAGE SQL;
--And here is the current query I am running for my results:
SELECT t1.tbl_1_hm_id, proc_1_hm(t1.tbl_1_hm_id) AS t3
FROM tbl_1_hm AS t1
Thanks for having a read. Please if you want to haggle about the semantics of what I am doing by hitting the same table twice or my naming convention --> this is a simplified test.
When a function returns a set of records, you should treat it as a table source:
SELECT t1.tbl_1_hm_id, t3.*
FROM tbl_1_hm AS t1, proc_1_hm(t1.tbl_1_hm_id) AS t3;
Note that functions are implicitly using a LATERAL join (scroll down to sub-sections 4 and 5) so you can use fields from tables listed previously without having to specify an explicit JOIN condition.