Skip to content

Refactor partition suffix logic to allow custom child prefixes - #830

Open
pavan-postgres wants to merge 2 commits into
pgpartman:developmentfrom
pavan-postgres:patch-1
Open

pavan-postgres wants to merge 2 commits into
pgpartman:developmentfrom
pavan-postgres:patch-1

Conversation

@pavan-postgres

Copy link
Copy Markdown

Updated the logic for determining suffixes of partitioned tables.

Previous implementation always appended '_p' for partitioned tables:

v_suffix := format('%s%s', CASE WHEN p_table_partition THEN '_p' END, p_suffix);

New logic allows using a simple naming convention for child tables without affecting existing behavior:

v_suffix := format('%s%s',
    CASE
        WHEN p_table_partition AND p_simple_naming THEN '_'
        WHEN p_table_partition THEN '_p'
    END,
    p_suffix
);

This change enables specifying a custom prefix/suffix for child table names (via p_suffix) while preserving the original '_p' behavior for partitions that do not use simple naming. It makes the code more flexible without breaking existing functionality.

@keithf4

keithf4 commented Dec 2, 2025

Copy link
Copy Markdown
Collaborator

Don't think I'm going to be able to get this into the next release, but I do like the idea. Will come back to review this after next release is out. Thank you!

@keithf4 keithf4 added this to the 5.5 milestone Dec 2, 2025
@keithf4 keithf4 self-assigned this Dec 2, 2025
@keithf4

keithf4 commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

Apologies for the delay in response. Reviewing this again, so this doesn't really allow custom prefixes to the child name suffix, it just allows the removal of the "p" in the suffix name. It also doesn't boil up to a point where it actually allows the user to put the setting into effect. So on second thought I'm not sure I'd like to implement this as is for now.

If maybe you wanted to redo this to actually allow full customization of the prefix to the childname suffix naming pattern, I may consider that. Although I am a little hesitant on adding more flags to the create_partition() function right now.

Can you perhaps more clearly state the intended usage of this?

pavan-postgres and others added 2 commits August 31, 2026 00:20
Updated the logic for determining suffixes of partitioned tables. 

Previous implementation always appended '_p' for partitioned tables:

    v_suffix := format('%s%s', CASE WHEN p_table_partition THEN '_p' END, p_suffix);

New logic allows using a simple naming convention for child tables without affecting existing behavior:

    v_suffix := format('%s%s',
        CASE
            WHEN p_table_partition AND p_simple_naming THEN '_'
            WHEN p_table_partition THEN '_p'
        END,
        p_suffix
    );

This change enables specifying a custom prefix/suffix for child table names (via `p_suffix`) while preserving the original '_p' behavior for partitions that do not use simple naming. It makes the code more flexible without breaking existing functionality.
Addresses review feedback on this PR: the previous p_simple_naming
flag only let you toggle between '_p' and '_', and never actually
took effect anywhere since nothing exposed it past
check_name_length(). This replaces it with a real, arbitrary
child_table_prefix column on part_config (default '_p', so existing
partition sets are unaffected) that is threaded through every place
child table names are generated: create_partition_time(),
create_partition_id(), show_partition_name(), partition_data_time(),
partition_data_id(), and dump_partitioned_table_definition().

It intentionally does not add a new parameter to create_partition()/
create_parent() - like other settings not exposed there (retention,
optimize_constraint, etc.), it's set afterwards with
UPDATE part_config SET child_table_prefix = '...' WHERE parent_table = ...

Also adds pgTAP coverage for both time- and id-based partitioning,
a migration script, and doc/changelog updates.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@pavan-postgres

pavan-postgres commented Aug 30, 2026 •

Copy link
Copy Markdown
Author

Sorry for the slow follow-up, and thanks for the detailed feedback — both points are fair, so I reworked this instead of just patching around them.

"Doesn't really allow custom prefixes" — agreed, p_simple_naming was just a boolean toggle between _p and _. I've replaced it with check_name_length(..., p_partition_prefix text DEFAULT '_p'), so the prefix is now an arbitrary string, not a fixed choice of two.

"Doesn't boil up to where the user can actually use it" — also agreed. It's now backed by a real part_config.child_table_prefix column (defaults to _p, so existing partition sets are untouched) and threaded through every place a child table name gets generated: create_partition_time(), create_partition_id(), show_partition_name(), partition_data_time(), partition_data_id(), and dump_partitioned_table_definition().

On the "hesitant to add more flags to create_partition()" point — I deliberately did not add a parameter there. Like retention, optimize_constraint, etc., it's set after the fact:

UPDATE part_config SET child_table_prefix = '_' WHERE parent_table = 'my_schema.my_table';

Intended usage: anyone who wants child tables named my_table_20260101 instead of my_table_p20260101, or a fully custom convention like my_table_batch_200, sets this once and it applies to every child table created after that (initial premake, run_maintenance(), manual create_partition_time()/create_partition_id() calls, and data-movement functions).

Scope-wise I kept this to top-level partition sets only (not part_config_sub/subpartitioning) to keep the diff small and reviewable, given subpartitioning is already fairly delicate code — happy to add sub-partition support as a follow-up if that's useful.

Added pgTAP coverage(took Claude's help) for both time- and id-based partitioning, a migration script, and doc/changelog updates. Ran the full test suite locally (PG17, pgxn/pgxn-tools, same as CI) — 4198/4198 passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants