pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

plan on same db that was dumped isn't empty

Open bme-cgm opened this issue 8 months ago • 2 comments

I dump'ed my db (postgrest-17.6) and used the dump on my db to generate a plan.

I assumed the plan would be empty, but it isn't.

It contains a seeming random selection of my functions. I have 57 functions, but the plan lists 19 of those functions.

Is this to be expected? Can I avoid it?

Using pgschema 1.7.4 via podman/docker.

bme-cgm avatar Mar 17 '26 16:03 bme-cgm

Please provide your schema and the exact dump command you use.

tianzhou avatar Mar 18 '26 02:03 tianzhou

I have now analyzed the situation more.

I apply SQL change scripts using flyway and they succeed. But I haven't notice that the newlines in the files weren't the same.

The functions that pop up in the plan all come from SQL files that use 0x0a as newline, and those functions in SQL files that use 0x0d,0x0a do not show up in the plan. However, even after fixing all newlines to be 0d0a, it still pops up with the same 19 functions.

I run these commands:

podman run --rm --network databasenet -e PGPASSWORD=xxxx pgschema-custom:latest dump --host postgres --port 5432 --db accounting --schema public --user accounting_service_admin > schema.sql

podman run --rm --network databasenet -v "%cd%:/workspace" -e PGPASSWORD=xxxx pgschema-custom:latest plan --host postgres --port 5432 --db accounting --schema public --user accounting_service_admin  --file /workspace/schema.sql --output-json /workspace/plan.json

I have to use a variant of the pgschema container because I have to inject a certificate to reach the internet from inside the container. This is the dockerfile to create my custom pgschema. It shouldn't do anything negative to the original container:

FROM pgplex/pgschema:latest

USER root
RUN apk add --no-cache ca-certificates
COPY zscaler-root-ca.crt /usr/local/share/ca-certificates/zscaler.crt
RUN update-ca-certificates

USER pgschema

I don't think I am allowed to share the full schema, but I will share a bit of it:

Selection from the schema.sql file:

--
-- Name: get_account_group_by_id(uuid); Type: FUNCTION; Schema: -; Owner: -
--

CREATE OR REPLACE FUNCTION get_account_group_by_id(
    p_group_id uuid
)
RETURNS TABLE(id uuid, company_id uuid, name varchar, created_at datetimeoffset, updated_at datetimeoffset)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
AS $$
BEGIN
    RETURN QUERY
    SELECT
        ag.id,
        ag.company_id,
        ag.name,
        ag.created_at,
        ag.updated_at
    FROM account_groups ag
    WHERE ag.id = p_group_id;
END;
$$;

Newlines are LF (0a)

Selection from the plan.json file:

        {
          "sql": "DROP FUNCTION IF EXISTS get_account_group_by_id(uuid);",
          "type": "function",
          "operation": "alter",
          "path": "public.get_account_group_by_id"
        },
        {
          "sql": "CREATE OR REPLACE FUNCTION get_account_group_by_id(\n    p_group_id uuid\n)\nRETURNS TABLE(id uuid, company_id uuid, name varchar, created_at public.datetimeoffset, updated_at public.datetimeoffset)\nLANGUAGE plpgsql\nVOLATILE\nSECURITY DEFINER\nAS $$\nBEGIN\n    RETURN QUERY\n    SELECT\n        ag.id,\n        ag.company_id,\n        ag.name,\n        ag.created_at,\n        ag.updated_at\n    FROM account_groups ag\n    WHERE ag.id = p_group_id;\nEND;\n$$;",
          "type": "function",
          "operation": "alter",
          "path": "public.get_account_group_by_id"
        },

I notice the function parameters differ. The plan includes "public." on my custom type.

The type is defined like this:

--
-- Name: datetimeoffset; Type: TYPE; Schema: -; Owner: -
--

CREATE TYPE datetimeoffset AS (local_time timestamp without time zone, offset_minutes smallint);

The type is created in the public schema.

bme-cgm avatar Mar 18 '26 10:03 bme-cgm