polar_ddl_manager

Updated at:

This topic describes how to use the polar_ddl_manager extension and provides usage examples.

Background information

In PostgreSQL, when you increase the length of a varchar column in a partitioned table, PostgreSQL automatically rebuilds the indexes related to the modified column. This makes the ALTER TABLE statement inefficient. The ALTER TABLE statement also requires an ACCESS EXCLUSIVE lock, which blocks other connections from accessing the table for a long time.

and PolarDB for PostgreSQL (Compatible with Oracle) support the polar_ddl_manager extension. This extension provides functions to optimize the performance of increasing the length of a varchar column in a partitioned table.

Usage

Install the extension

CREATE EXTENSION polar_ddl_manager;

Functions

  • polar_ddl_manager.alter_varchar_column_length_cmd

    This function retrieves a list of ALTER TABLE commands to increase the length of a varchar column. You can use it to check the commands before you run the Data Definition Language (DDL) statements. The following table describes the parameters.

    Parameter

    Description

    parent_table_name

    The name of the partitioned table.

    parent_table_schema

    The schema where the partitioned table resides.

    column_name

    The name of the column to increment.

    target_len

    The target length after the increase.

    novalidate

    Specifies whether to skip the partition constraint check. Valid values:

    • true (default): Skips the partition constraint check.

    • false: Does not skip the partition constraint check.

  • polar_ddl_manager.alter_varchar_column_length

    This function retrieves DDL commands using the polar_ddl_manager.alter_varchar_column_length_cmd function and automatically runs them to increase the length of a varchar column in a partitioned table. The function works as follows:

    1. Locks the partitioned table and its child tables, and then performs a validity check.

    2. Detaches all subpartitions.

    3. Modifies the length of the varchar column in the partitioned table and its child tables.

    4. Attaches all subpartitions.

Usage example

CREATE TABLE t(a int, b varchar(10), c char(128)) PARTITION BY HASH (a);
CREATE TABLE t_p1 PARTITION OF t FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE t_p2 PARTITION OF t FOR VALUES WITH (MODULUS 2, REMAINDER 1);
CREATE index ON t(b);
INSERT INTO t SELECT i, i FROM generate_series(1, 1000000) i;
-- Increase the length of column b without skipping the partition constraint check.
SELECT polar_ddl_manager.alter_varchar_column_length('t', 'public', 'b', 15, false);
-- Increase the length of column b and skip the partition constraint check.
SELECT polar_ddl_manager.alter_varchar_column_length('t', 'public', 'b', 20);

Uninstall the extension

DROP EXTENSION polar_ddl_manager;
Note

To speed up the ATTACH PARTITION process, the NOCHECK_CONSTRAINT syntax is used to skip the partition constraint check. The complete syntax is as follows:

ALTER TABLE [ IF EXISTS ] name
    ATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT } [NOCHECK_CONSTRAINT]

You must ensure that the data in the partition to be attached and the default partition already meets the partition constraint. Otherwise, data integrity issues may occur.

Performance comparison

The following table schema was used for testing:

CREATE TABLE t(a int, b varchar(10), c char(128)) PARTITION BY HASH (a);
CREATE TABLE t_p1 PARTITION OF t FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE t_p2 PARTITION OF t FOR VALUES WITH (MODULUS 2, REMAINDER 1);

The following table shows the running time in milliseconds (ms) for different data volumes and scenarios with and without an index.

Data volume

Number of indexes

Native ALTER

Without skipping constraint check

Skipping constraint check

100 MB

0

0.562

222.37

10.47

1

5592.84

204.43

10.96

1 GB

0

0.56

2122.05

10.64

1

28588.58

1943.39

11.65

10 GB

0

0.53

39535.99

10.58

1

271934.90

35554.38

11.64