Skip to content

The first future partition is created with wrong index name using partman.create_parent #821

@yulia-simkin-deel

Description

@yulia-simkin-deel

Hi,
We use pg_partman of version 5.1.0.
We try to convert the regular table to be partitioned using the attached scripts.
I noticed that the index name of first future partition is created with the wrong name and my script doesn't succeed to rename it. It happens only for this partition, other future partition indexes are created with correct name.

The wrong index name is screening_logs_partitioned_p2025110_screening_id_created_at_idx.
It should be screening_logs_partitioned_p20251101_screening_id_created_at (1 is missing in the partition name, regarding a new added suffix, it's ok - we will rename the parent table index name to have the correct suffix).
Can you explain the root case, seems like a bug. I tested it for several times and got the same result.
Thanks!

Original table:

CREATE TABLE public.screening_logs (
	screening_id int4 NULL,
	created_at timestamptz NOT NULL DEFAULT now(),
	status varchar(255) NULL
	CONSTRAINT screening_logs_screening_id_fkey FOREIGN KEY (screening_id) REFERENCES public."Screenings"(id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX screening_logs_screening_id_created_at ON public.screening_logs(screening_id, created_at DESC);
DO $$
DECLARE
	l_first_future_partition TEXT;
BEGIN

    SET time zone 'UTC';

    SELECT to_char((date_trunc('month', now()) + INTERVAL '1 month'),'YYYYMMDD') INTO l_first_future_partition;

    CREATE TABLE public.screening_logs_backup AS TABLE public.screening_logs;
    
    CREATE TABLE public.screening_logs_partitioned(
	screening_id int4 NULL,
	created_at timestamptz NOT NULL DEFAULT now(),
	status varchar(255) NULL
	) PARTITION BY RANGE(created_at);

    CREATE UNIQUE INDEX screening_logs_partitioned_screening_id_created_at ON public.screening_logs_partitioned USING btree (screening_id, created_at DESC);

    ALTER TABLE public.screening_logs_partitioned ADD CONSTRAINT screening_logs_partitioned_screening_id_fkey FOREIGN KEY (screening_id) REFERENCES public."Screenings"(id) ON DELETE CASCADE;

    CREATE TABLE public.screening_logs_template (LIKE public.screening_logs);

    PERFORM partman.create_parent(
    p_parent_table := 'public.screening_logs_partitioned', 
    p_control := 'created_at', 
    p_interval := '1 month', 
    p_template_table:= 'public.screening_logs_template', 
    p_premake := 1, 
    p_start_partition := l_first_future_partition,
    p_default_table := false);

    UPDATE partman.part_config 
    SET parent_table = 'public.screening_logs', 
        premake = 3, inherit_privileges = TRUE, 
        infinite_time_partitions = TRUE,
        retention = '6 months',
        retention_keep_table = FALSE,
        retention_keep_index = FALSE 
        WHERE parent_table = 'public.screening_logs_partitioned';

    LOCK TABLE public.screening_logs IN ACCESS EXCLUSIVE MODE;
    LOCK TABLE public.screening_logs_partitioned IN ACCESS EXCLUSIVE MODE;

    ALTER TABLE public.screening_logs RENAME TO screening_logs_default;

    ALTER TABLE public.screening_logs_partitioned RENAME TO screening_logs;
    
    EXECUTE FORMAT('ALTER TABLE public.screening_logs_partitioned_p%1$s RENAME TO screening_logs_p%1$s',l_first_future_partition);

    ALTER TABLE public.screening_logs ATTACH PARTITION public.screening_logs_default DEFAULT;
	
	ALTER TABLE public.screening_logs RENAME CONSTRAINT screening_logs_partitioned_screening_id_fkey TO screening_logs_screening_id_fkey;
	
	ALTER INDEX public.screening_logs_screening_id_created_at RENAME TO screening_logs_default_screening_id_created_at_idx;
	
	ALTER INDEX public.screening_logs_partitioned_screening_id_created_at RENAME TO screening_logs_screening_id_created_at;

    COMMIT;

END; $$

Second script:
DO $$

BEGIN

    SET time zone 'UTC';

    CALL partman.partition_data_proc('public.screening_logs', p_loop_count := 200);
    PERFORM partman.run_maintenance('public.screening_logs');

END; $$

Metadata

Metadata

Assignees

Labels

upstream issueIssue is with core PG or another upstream project

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions