Turning a pl/pgSQL select statement into an insert - postgresql

I'm trying to turn this select query into an insert query to insert the results into another table, but PostgreSQL is telling me no. This query works and returns a 'reports' data type:
drop function counties();
create or replace function counties()
returns table(code varchar(16), county varchar(50), count bigint) as $$
DECLARE
new_version_number varchar(50) := concat('gcversa00', MAX("versionnumber")) from "gcdefault"."versionhistory";
old_version_number varchar(50) := concat('gcversa00', MAX("versionnumber"-2)) from "gcdefault"."versionhistory";
BEGIN RETURN QUERY EXECUTE
format(
'with cte_current as (select distinct a.code as code, b.countyname as county from %I.servicearea a, public.counties b
where st_intersects(a.geom,b.geom) = True group by a.code, b.countyname),
cte_new as (select distinct a.code as code, b.countyname as county from %I.servicearea a, public.counties b
where st_intersects(a.geom,b.geom) = True group by a.code, b.countyname),
cte_union as (select code, county from cte_current
union all
select code, county from cte_new)
select code,county, count(*) as count
from cte_union
group by code, county
Having count (*) <> 2', new_version_number, old_version_number
);
END;
$$
LANGUAGE plpgsql;
When I turn this into an insert query and call select counties():
drop function counties();
create or replace function counties()
returns table(code varchar(16), county varchar(50), count bigint) as $$
DECLARE
new_version_number varchar(50) := concat('gcversa00', MAX("versionnumber")) from "gcdefault"."versionhistory";
old_version_number varchar(50) := concat('gcversa00', MAX("versionnumber"-2)) from "gcdefault"."versionhistory";
BEGIN RETURN QUERY EXECUTE
format(
'
with cte_current as (select distinct a.code as code, b.countyname as county from %I.servicearea a, public.counties b
where st_intersects(a.geom,b.geom) = True group by a.code, b.countyname),
cte_new as (select distinct a.code as code, b.countyname as county from %I.servicearea a, public.counties b
where st_intersects(a.geom,b.geom) = True group by a.code, b.countyname),
cte_union as (select code, county from cte_current
union all
select code, county from cte_new)
insert into county_check (code, county, count)
select code, county, count(*) as count from cte_union
group by code, county
Having count (*) <> 2', new_version_number, old_version_number
);
END;
$$
LANGUAGE plpgsql;
This is the error I get:
ERROR: cannot open INSERT query as cursor
CONTEXT: PL/pgSQL function counties() line 5 at RETURN QUERY
How can I make this work as an insert statement similar to the way I have it laid out now (if possible)? I looked at creating some temp tables instead of using the cte's in the query, and then using a select into loop, but I can't find any examples this complex that I can use.

you can add RETURNING to fix it (to actually return declared return type), eg:
t=# create table so(i int);
CREATE TABLE
t=# create or replace function f() returns table (i int) as
$$
begin
return query execute format ('
with c(c) as (select 2)
, i as (insert into so select c from c returning *)
select * from i');
end;
$$
language plpgsql;
CREATE FUNCTION
t=# select f();
f
---
2
t=# select * from so;
i
---
2
(1 row)
but in your case why not just:
insert into county_check (code, county, count) select * from counties()

Related

PgSQL function returning table and extra data computed in process

In PgSQL I make huge select, and then I want count it's size and apply some extra filters.
execute it twice sound dumm,
so I wrapped it in function
and then "cache" it and return union of filtered table and extra row at the end where in "id" column store size
with q as (select * from myFunc())
select * from q
where q.distance < 400
union all
select count(*) as id, null,null,null
from q
but it also doesn't look like proper solution...
and so the question: is in pg something like "generator function" or any other stuff that can properly solve this ?
postgreSQL 13
myFunc aka "selectItemsByRootTag"
CREATE OR REPLACE FUNCTION selectItemsByRootTag(
in tag_name VARCHAR(50)
)
RETURNS table(
id BIGINT,
name VARCHAR(50),
description TEXT,
/*info JSON,*/
distance INTEGER
)
AS $$
BEGIN
RETURN QUERY(
WITH RECURSIVE prod AS (
SELECT
tags.name, tags.id, tags.parent_tags
FROM
tags
WHERE tags.name = (tags_name)
UNION
SELECT c.name, c.id , c.parent_tags
FROM
tags as c
INNER JOIN prod as p
ON c.parent_tags = p.id
)
SELECT
points.id,
points.name,
points.description,
/*points.info,*/
points.distance
from points
left join tags on points.tag_id = tags.id
where tags.name in (select prod.name from prod)
);
END;
$$ LANGUAGE plpgsql;
as a result i want see maybe set of 2 table or generator function that yield some intermediate result not shure how exacltly it should look
demo
CREATE OR REPLACE FUNCTION pg_temp.selectitemsbyroottag(tag_name text, _distance numeric)
RETURNS TABLE(id bigint, name text, description text, distance numeric, count bigint)
LANGUAGE plpgsql
AS $function$
DECLARE _sql text;
BEGIN
_sql := $p1$WITH RECURSIVE prod AS (
SELECT
tags.name, tags.id, tags.parent_tags
FROM
tags
WHERE tags.name ilike '%$p1$ || tag_name || $p2$%'
UNION
SELECT c.name, c.id , c.parent_tags
FROM
tags as c
INNER JOIN prod as p
ON c.parent_tags = p.id
)
SELECT
points.id,
points.name,
points.description,
points.distance,
count(*) over ()
from points
left join tags on points.tag_id = tags.id
where tags.name in (select prod.name from prod)
and points.distance > $p2$ || _distance
;
raise notice '_sql: %', _sql;
return query execute _sql;
END;
$function$
You can call it throug following way
select * from pg_temp.selectItemsByRootTag('test',20);
select * from pg_temp.selectItemsByRootTag('test_8',20) with ORDINALITY;
The 1 way to call the function, will have a row of total count total number of rows. Second way call have number of rows plus a serial incremental number.
I also make where q.distance < 400 into function input argument.
selectItemsByRootTag('test',20); means that q.distance > 20 and tags.name ilike '%test%'.

How to implement stored procedure/function in postgresql version 12?

How I can implement it? I am confused in syntax also?
CREATE OR REPLACE Function dailyyyy_volation_routine(_ispeed_count integer,_rlvd_count integer,_stopline_count integer,_wrongdir_count integer
,_wronglane_count integer,_zebracross_count integer,_total_count integer)
RETURNS void AS $$
BEGIN
INSERT INTO temp_daily_stats(ispeed_count,rlvd_count,stopline_count,wrongdir_count,wronglane_count,zebracross_count,total_count)
with t1 as (SELECT SUM(ispeed_count) as ispeed_count from temp_hourly_stats),
t3 as (SELECT SUM(rlvd_count) as rlvd_count from temp_hourly_stats),
t4 as (SELECT SUM(stop_line_count) as stopline_count from temp_hourly_stats),
t5 as (SELECT SUM(wrong_dir_count) as wrong_dir_count from temp_hourly_stats),
t6 as (SELECT SUM(wrong_lane_count) as wrong_lane_count from temp_hourly_stats),
t7 as (SELECT SUM(zebra_cross_count) as zebra_cross_count from temp_hourly_stats),
t8 as (SELECT #tc := SUM(total_count) as total_day_count from temp_hourly_stats),
SELECT DISTINCT t1.ispeed_count,t3.rlvd_count,t4.stopline_count,t5.wrong_dir_count,t6.wrong_lane_count,t7.zebra_cross_count,t8.total_day_count
FROM t1,t3,t4,t5,t6,t7,t8 limit 1;
END
$$
LANGUAGE SQL;
I want to put data from one table to another using postgresql function?*
Here is an attempt to translate your function to correct code:
CREATE OR REPLACE Function dailyyyy_volation_routine(
_ispeed_count integer,
_rlvd_count integer,
_stopline_count integer,
_wrongdir_count integer,
_wronglane_count integer,
_zebracross_count integer,
_total_count integer) RETURNS void AS
$$INSERT INTO temp_daily_stats(
ispeed_count,
rlvd_count,
stopline_count,
wrongdir_count,
wronglane_count,
zebracross_count,
total_count
)
SELECT SUM(ispeed_count),
SUM(rlvd_count),
SUM(stop_line_count),
SUM(wrong_dir_count),
SUM(wrong_lane_count),
SUM(zebra_cross_count),
SUM(total_count)
FROM temp_hourly_stats$$
LANGUAGE SQL;

Accessing columns when looping over record composed of result of 2 subqueries

I have a code block in which I am looping over a record that contains two joined subqueries that contain equally named columns in different tables.
Now I seem to be able to access sq1 and sq2 in the record, but not the contents and I always get "could not identify column 'c1' in record data type", even if I add explicit aliases to the columns:
DO $$
DECLARE
r record;
BEGIN
FOR r IN SELECT sq1, sq2
FROM (SELECT t1.someColumn as c1, t2.someColumn as c2, ... FROM Table1 t1 JOIN Table2 t2 ...) sq1
JOIN (SELECT t1.someColumn as c1, t2.someColumn as c2, ... FROM Table1 t1 JOIN Table2 t2 ...) sq2
ON (sq1.joinColumn1 = sq2.joinColumn2 AND sq1.joinColumn2 = sq2.joinColumn1)
LOOP
INSERT INTO Table3 (column1, column2)
VALUES ((r.sq1).c1, (r.sq2).c1);
--^ error occurs here
END LOOP;
END$$;
I am looking for a way to access the records similar to the following way:
r.sq1.t1.someColumn
For access to the 3 level in your variable you must cast it as named record type. It might be table or type:
For type:
CREATE TYPE my_type AS (c1 int4, c2 int4, joinColumn1 int4);
For table:
CREATE TABLE my_type (c1 int4, c2 int4, joinColumn1 int4);
And after that you can do something like this:
DO $$
DECLARE
r record;
BEGIN
FOR r IN SELECT (sq1.*)::my_type AS sq1, (sq2.*)::my_type AS sq2
FROM (SELECT 10 as c1, 11 as c2, 1 as joinColumn1) sq1
JOIN (SELECT 20 as c1, 21 as c2, 1 as joinColumn2) sq2
ON (sq1.joinColumn1 = sq2.joinColumn2)
LOOP
RAISE NOTICE '%', r;
RAISE NOTICE '%', r.sq1;
RAISE NOTICE '% %', (r.sq1).c1, (r.sq2).c1;
INSERT INTO Table3 (column1, column2)
VALUES ((r.sq1).c1, (r.sq2).c1);
END LOOP;
END$$;

Postgres find all rows in database tables matching criteria on a given column

I am trying to write sub-queries so that I search all tables for a column named id and since there are multiple tables with id column, I want to add the condition, so that id = 3119093.
My attempt was:
Select *
from information_schema.tables
where id = '3119093' and id IN (
Select table_name
from information_schema.columns
where column_name = 'id' );
This didn't work so I tried:
Select *
from information_schema.tables
where table_name IN (
Select table_name
from information_schema.columns
where column_name = 'id' and 'id' IN (
Select * from table_name where 'id' = 3119093));
This isn't the right way either. Any help would be appreciated. Thanks!
A harder attempt is:
CREATE OR REPLACE FUNCTION search_columns(
needle text,
haystack_tables name[] default '{}',
haystack_schema name[] default '{public}'
)
RETURNS table(schemaname text, tablename text, columnname text, rowctid text)
AS $$
begin
FOR schemaname,tablename,columnname IN
SELECT c.table_schema,c.table_name,c.column_name
FROM information_schema.columns c
JOIN information_schema.tables t ON
(t.table_name=c.table_name AND t.table_schema=c.table_schema)
WHERE (c.table_name=ANY(haystack_tables) OR haystack_tables='{}')
AND c.table_schema=ANY(haystack_schema)
AND t.table_type='BASE TABLE'
--AND c.column_name = "id"
LOOP
EXECUTE format('SELECT ctid FROM %I.%I WHERE cast(%I as text) like %L',
schemaname,
tablename,
columnname,
needle
) INTO rowctid;
IF rowctid is not null THEN
RETURN NEXT;
END IF;
END LOOP;
END;
$$ language plpgsql;
select * from search_columns('%3119093%'::varchar,'{}'::name[]) ;
The only problem is this code displays the table name and column name. I have to then manually enter
Select * from table_name where id = 3119093
where I got the table name from the code above.
I want to automatically implement returning rows from a table but I don't know how to get the table name automatically.
I took the time to make it work for you.
For starters, some information on what is going on inside the code.
Explanation
function takes two input arguments: column name and column value
it requires a created type that it will be returning a set of
first loop identifies tables that have a column name specified as the input argument
then it forms a query which aggregates all rows that match the input condition inside every table taken from step 3 with comparison based on ILIKE - as per your example
function goes into the second loop only if there is at least one row in currently visited table that matches specified condition (then the array is not null)
second loop unnests the array of rows that match the condition and for every element it puts it in the function output with RETURN NEXT rec clause
Notes
Searching with LIKE is inefficient - I suggest adding another input argument "column type" and restrict it in the lookup by adding a join to pg_catalog.pg_type table.
The second loop is there so that if more than 1 row is found for a particular table, then every row gets returned.
If you are looking for something else, like you need key-value pairs, not just the values, then you need to extend the function. You could for example build json format from rows.
Now, to the code.
Test case
CREATE TABLE tbl1 (col1 int, id int); -- does contain values
CREATE TABLE tbl2 (col1 int, col2 int); -- doesn't contain column "id"
CREATE TABLE tbl3 (id int, col5 int); -- doesn't contain values
INSERT INTO tbl1 (col1, id)
VALUES (1, 5), (1, 33), (1, 25);
Table stores data:
postgres=# select * From tbl1;
col1 | id
------+----
1 | 5
1 | 33
1 | 25
(3 rows)
Creating type
CREATE TYPE sometype AS ( schemaname text, tablename text, colname text, entirerow text );
Function code
CREATE OR REPLACE FUNCTION search_tables_for_column (
v_column_name text
, v_column_value text
)
RETURNS SETOF sometype
LANGUAGE plpgsql
STABLE
AS
$$
DECLARE
rec sometype%rowtype;
v_row_array text[];
rec2 record;
arr_el text;
BEGIN
FOR rec IN
SELECT
nam.nspname AS schemaname
, cls.relname AS tablename
, att.attname AS colname
, null::text AS entirerow
FROM
pg_attribute att
JOIN pg_class cls ON att.attrelid = cls.oid
JOIN pg_namespace nam ON cls.relnamespace = nam.oid
WHERE
cls.relkind = 'r'
AND att.attname = v_column_name
LOOP
EXECUTE format('SELECT ARRAY_AGG(row(tablename.*)::text) FROM %I.%I AS tablename WHERE %I::text ILIKE %s',
rec.schemaname, rec.tablename, rec.colname, quote_literal(concat('%',v_column_value,'%'))) INTO v_row_array;
IF v_row_array is not null THEN
FOR rec2 IN
SELECT unnest(v_row_array) AS one_row
LOOP
rec.entirerow := rec2.one_row;
RETURN NEXT rec;
END LOOP;
END IF;
END LOOP;
END
$$;
Exemplary call & output
postgres=# select * from search_tables_for_column('id','5');
schemaname | tablename | colname | entirerow
------------+-----------+---------+-----------
public | tbl1 | id | (1,5)
public | tbl1 | id | (1,25)
(2 rows)

postgresql column names as variable in a subquery

table1
id col1 col2 col3...
table2
col_id col_name
3432 col1
5342 col2
6756 col3
Now I want to generate table 3 like this:
id col_name col_value col_id
Please note that col1, col2,col3... are not in order. Therefore I have to query table2 to obtain col_id ( I think pivot does not work here)
How can I do it in SQL?
It appears that you want a select like this:
SELECT t2.id,
CASE
WHEN t2.col_name='col1' THEN t1.col1
WHEN t2.col_name='col2' THEN t1.col2
WHEN t2.col_name='col2' THEN t1.col2
-- ... more columns
ELSE NULL
END
FROM table2 t2 LEFT JOIN table2 t1 ON t2.col_id = t1.id
You could also create a function, though this will be slower in practice:
CREATE OR REPLACE FUNCTION table1_col(id integer, name text) RETURNS text as $$
DECLARE
col_val text;
BEGIN
EXECUTE format('SELECT %s FROM table1 WHERE id=$1', name)
INTO col_val
USING id;
RETURN col_val;
END;
$$ LANGUAGE plpgsql;
SELECT table1_col(col_id,col_name) FROM table2;