Postgres Recursive Query, CTE goes in infinite loop inside postgres function using sql?

你离开我真会死。 提交于 2020-04-18 05:46:27

问题


Trying to make a postgres function, inside of which I made a CTE, to recursively iterate through hierarchical data (Parent child relationship) MY Table structure

In this I need to recursively iterate if text_id != new_text_id and stop (terminating condition) when text_id == new_text_id

Table Schema for reference

create table demoTextTable
(
    text_id serial primary key,
    text_details character varying,
    new_text_id integer
)

insert into demoTextTable(text_details, new_text_id)
values ('Comment 1', 2),
       ('Comment 1 updated 1st Time',3),
       ('Comment 1 updated 2nd Time',4),
       ('Comment 1 updated 3rd Time',5),
       ('Comment 1 updated 4th Time',5);

My Postgres Function

create or replace function get_text_history(textId integer)
RETURNS Table (
    text_id integer,
    text_details varchar,
    new_text_id integer)
AS $$
BEGIN
WITH RECURSIVE textHierarchy AS (

    select tm.text_id, tm.text_details, tm.new_text_id
    from text_master tm where tm.text_id = textId

    UNION ALL

    select tm.text_id, tm.text_details, tm.new_text_id
    FROM textHierarchy AS txtHr, text_master AS tm where tm.text_id != txtHr.new_text_id
    and txtHr.new_text_id is not null
)
select * from textHierarchy;
END;
$$ Language plpgsql;

OK So tried more and got the inside CTE working but if I execute it inside function then it gives error on executing function as

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function get_text_history(integer) line 3 at SQL statement
SQL state: 42601

My Updated Function:-

create or replace function get_text_history(textId integer)
RETURNS Table (
    text_id integer,
    text_details varchar,
    new_text_id integer)
AS $$
BEGIN
WITH RECURSIVE textHierarchy AS (

    select dtbl.text_id, dtbl.text_details, dtbl.new_text_id
    from demoTextTable dtbl where dtbl.text_id = textId

    UNION

    select dtbl.text_id, dtbl.text_details, dtbl.new_text_id
    FROM demoTextTable dtbl where dtbl.text_id != dtbl.new_text_id or 
    dtbl.text_id = dtbl.new_text_id order by text_id asc

)
select * from textHierarchy;
END;
$$ Language plpgsql;

回答1:


You want to use SQL, not PL/pgSQL, you need UNION rather than UNION ALL, and you need to join with = rather than <>:

CREATE OR REPLACE FUNCTION get_text_history(textId integer)
RETURNS TABLE (
    text_id integer,
    text_details varchar,
    new_text_id integer
) LANGUAGE sql AS
$$WITH RECURSIVE textHierarchy AS (
    SELECT tm.text_id, tm.text_details, tm.new_text_id
    FROM demotexttable tm 
    WHERE tm.text_id = textId
  UNION
    SELECT tm.text_id, tm.text_details, tm.new_text_id
    FROM textHierarchy AS txtHr 
        JOIN demotexttable AS tm ON tm.text_id = txtHr.new_text_id
    WHERE txtHr.new_text_id IS NOT NULL
)
SELECT * FROM textHierarchy$$;


来源:https://stackoverflow.com/questions/61096799/postgres-recursive-query-cte-goes-in-infinite-loop-inside-postgres-function-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!