dump and plan uses wrong order
I'm getting this error:
[chris@P14s-Arch dbworking]$ pgschema plan --host localhost --db apni --user nsl --schema public --file schema-apni.sql --output-json plan1.json --output-human plan1.sql
hema pgschema_tmp_20260831_113936_a08e9368: ERROR: function f_unaccent(text) does not exist (SQLSTATE 42883)
Error location (line 1607, column 80):
1604 | -- Name: name_lower_f_unaccent_full_name_like; Type: INDEX; Schema: -; 1605 | --
1606 |
> 1607 | CREATE INDEX IF NOT EXISTS name_lower_f_unaccent_full_name_like ON name (lower(f_unaccent(full_name::text)) varchar_pattern_ops);
1608 |
1609 | --
1610 | -- Name: name_lower_full_name_gin_trgm; Type: INDEX; Schema: -; Owner: -
Hint: this schema may depend on a PostgreSQL extension, which the embedded plan database cannot provide. Use an external plan database with the extension installed (--plan-host), see https://www.pgschema.com/cli/plan-db
If I look at the pgschema dump, the function f_unaccent is declared, however... I don't know if this is important, but it's declared after the declaration of name_lower_f_unaccent_full_name_gin_trgm index. The pgschema dump documentation says that dump is "order aware" so this seems like the bug.
CREATE INDEX IF NOT EXISTS name_lower_f_unaccent_full_name_like ON name (lower(public.f_unaccent(full_name::text)) varchar_pattern_ops);
--- some time later...
CREATE OR REPLACE FUNCTION f_unaccent(
text
)
RETURNS text
LANGUAGE sql
IMMUTABLE
SET search_path = public, pg_temp
AS $_$
SELECT unaccent('unaccent', $1)
$_$;
if I do a pg_dump --schema-only of the database, f_unaccent is exported before the index that uses it.
I'm not sure what to make of the mention of extensions. I manually put this at the beginning of the dump:
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS unaccent;
I'm not sure why I needed to, surely pgschema should if it needs it figure out the extensions and dump that too? In any case the error I'm getting, " function f_unaccent(text) does not exist" sounds like maybe pgschema is not creating things in the right order, that's my interpretation.
I have another error similar:
[chris@P14s-Arch dbworking]$ pgschema plan --host localhost --db apni --user nsl --schema public --file schema-apni.sql --output-json plan1.json --output-human plan1.sql
Error: failed to apply desired state: failed to apply schema SQL to temporary schema pgschema_tmp_20260831_133653_e801d759: ERROR: relation "public.name" does not exist (SQLSTATE 42P01)
The weird thing here is though that name is indeed declared before find_tree_rank, yet it can't find it. I'm guessing that it doesn't strictly apply them in order (although that's also strange because I overcame the previous error above by reordering).
CREATE TABLE IF NOT EXISTS name (
id BIGSERIAL,
lock_version bigint DEFAULT 0 NOT NULL,
author_id bigint,
base_author_id bigint,
created_at timestamptz NOT NULL,
created_by varchar(50) NOT NULL,
duplicate_of_id bigint,
ex_author_id bigint,
ex_base_author_id bigint,
full_name varchar(512),
full_name_html varchar(2048),
name_element varchar(255),
name_rank_id bigint NOT NULL,
name_status_id bigint NOT NULL,
name_type_id bigint NOT NULL,
namespace_id bigint NOT NULL,
orth_var boolean DEFAULT false NOT NULL,
parent_id bigint,
sanctioning_author_id bigint,
second_parent_id bigint,
simple_name varchar(250),
simple_name_html varchar(2048),
source_dup_of_id bigint,
source_id bigint,
source_id_string varchar(100),
source_system varchar(50),
status_summary varchar(50),
updated_at timestamptz NOT NULL,
updated_by varchar(50) NOT NULL,
valid_record boolean DEFAULT false NOT NULL,
verbatim_rank varchar(50),
sort_name varchar(250),
family_id bigint,
name_path text DEFAULT '' NOT NULL,
uri text,
changed_combination boolean DEFAULT false NOT NULL,
published_year integer,
apni_json jsonb,
basionym_id bigint,
primary_instance_id bigint,
api_name text,
api_at timestamptz,
deleted_at timestamptz,
CONSTRAINT name_pkey PRIMARY KEY (id),
CONSTRAINT uk_66rbixlxv32riosi9ob62m8h5 UNIQUE (uri),
CONSTRAINT fk_156ncmx4599jcsmhh5k267cjv FOREIGN KEY (namespace_id) REFERENCES namespace (id),
CONSTRAINT fk_3pqdqa03w5c6h4yyrrvfuagos FOREIGN KEY (duplicate_of_id) REFERENCES name (id),
CONSTRAINT fk_5fpm5u0ukiml9nvmq14bd7u51 FOREIGN KEY (name_status_id) REFERENCES name_status (id),
CONSTRAINT fk_5gp2lfblqq94c4ud3340iml0l FOREIGN KEY (second_parent_id) REFERENCES name (id),
CONSTRAINT fk_ai81l07vh2yhmthr3582igo47 FOREIGN KEY (sanctioning_author_id) REFERENCES author (id),
CONSTRAINT fk_airfjupm6ohehj1lj82yqkwdx FOREIGN KEY (author_id) REFERENCES author (id),
CONSTRAINT fk_bcef76k0ijrcquyoc0yxehxfp FOREIGN KEY (name_type_id) REFERENCES name_type (id),
CONSTRAINT fk_coqxx3ewgiecsh3t78yc70b35 FOREIGN KEY (base_author_id) REFERENCES author (id),
CONSTRAINT fk_dd33etb69v5w5iah1eeisy7yt FOREIGN KEY (parent_id) REFERENCES name (id),
CONSTRAINT fk_rp659tjcxokf26j8551k6an2y FOREIGN KEY (ex_base_author_id) REFERENCES author (id),
CONSTRAINT fk_sgvxmyj7r9g4wy9c4hd1yn4nu FOREIGN KEY (ex_author_id) REFERENCES author (id),
CONSTRAINT fk_sk2iikq8wla58jeypkw6h74hc FOREIGN KEY (name_rank_id) REFERENCES name_rank (id),
CONSTRAINT fk_whce6pgnqjtxgt67xy2lfo34 FOREIGN KEY (family_id) REFERENCES name (id),
CONSTRAINT name_basionym_id_fkey FOREIGN KEY (basionym_id) REFERENCES name (id),
CONSTRAINT name_primary_instance_id_fkey FOREIGN KEY (primary_instance_id) REFERENCES instance (id),
CONSTRAINT published_year_limits CHECK (published_year > 0 AND published_year < 2500)
);
CREATE OR REPLACE FUNCTION find_tree_rank(
tve_id text,
rank_sort_order integer
)
RETURNS TABLE(name_element text, rank text, sort_order integer)
LANGUAGE sql
VOLATILE
AS $$
WITH RECURSIVE walk (parent_id, name_element, rank, sort_order) AS (
SELECT tve.parent_id,
n.name_element,
r.name,
r.sort_order
FROM public.tree_version_element tve
JOIN public.tree_element te ON tve.tree_element_id = te.id
JOIN public.name n ON te.name_id = n.id
JOIN public.name_rank r ON n.name_rank_id = r.id
WHERE tve.element_link = tve_id
AND r.sort_order >= rank_sort_order
UNION ALL
SELECT tve.parent_id,
n.name_element,
r.name,
r.sort_order
FROM walk w,
public.tree_version_element tve
JOIN public.tree_element te ON tve.tree_element_id = te.id
JOIN public.name n ON te.name_id = n.id
JOIN public.name_rank r ON n.name_rank_id = r.id
WHERE tve.element_link = w.parent_id
AND r.sort_order >= rank_sort_order
)
SELECT w.name_element,
w.rank,
w.sort_order
FROM walk w
WHERE w.sort_order = rank_sort_order
limit 1
$$;
I assume find_tree_rank is the problem because if I remove it, it stops happening, albeit its non existence cascades into more errors.