Sample Header Ad - 728x90

Rename table in Postgres and update its usages in all functions

2 votes
0 answers
1060 views
I need to safely rename a table and be sure, that all usages of this table in functions are also renamed. Example:
CREATE TABLE test (
    id serial,
    name varchar(255)
);

CREATE FUNCTION public.last_test() RETURNS SETOF test
    LANGUAGE sql STABLE
AS $$
    SELECT
      *
    FROM
      test
    LIMIT 10;
$$;

ALTER TABLE test RENAME TO tests;
Now, when I look into function last_test, I see, that it still references old test table name. Is it even possible to rename a table and all it's usages in functions automatically?
Asked by AntonAL (121 rep)
Feb 6, 2019, 09:32 AM