Skip to content

Commit

Permalink
rename "partition" arguments to "partition_relid"
Browse files Browse the repository at this point in the history
  • Loading branch information
zilder committed Feb 14, 2017
1 parent 411a111 commit b8659fa
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 78 deletions.
10 changes: 5 additions & 5 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -643,21 +643,21 @@ SET pg_pathman.enable_partitionfilter = off; /* ensures that PartitionFilter is
*/
CREATE OR REPLACE FUNCTION @extschema@.copy_foreign_keys(
parent_relid REGCLASS,
partition REGCLASS)
partition_relid REGCLASS)
RETURNS VOID AS
$$
DECLARE
rec RECORD;

BEGIN
PERFORM @extschema@.validate_relname(parent_relid);
PERFORM @extschema@.validate_relname(partition);
PERFORM @extschema@.validate_relname(partition_relid);

FOR rec IN (SELECT oid as conid FROM pg_catalog.pg_constraint
WHERE conrelid = parent_relid AND contype = 'f')
LOOP
EXECUTE format('ALTER TABLE %s ADD %s',
partition::TEXT,
partition_relid::TEXT,
pg_catalog.pg_get_constraintdef(rec.conid));
END LOOP;
END
Expand Down Expand Up @@ -880,7 +880,7 @@ LANGUAGE C STRICT;
*/
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
parent_relid REGCLASS,
partition REGCLASS,
partition_relid REGCLASS,
init_callback REGPROCEDURE,
start_value ANYELEMENT,
end_value ANYELEMENT)
Expand All @@ -892,7 +892,7 @@ LANGUAGE C;
*/
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
parent_relid REGCLASS,
partition REGCLASS,
partition_relid REGCLASS,
init_callback REGPROCEDURE)
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
LANGUAGE C;
133 changes: 98 additions & 35 deletions pg_pathman--1.2--1.3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ DROP FUNCTION @extschema@.get_attribute_type(REGCLASS, TEXT);
DROP FUNCTION @extschema@.create_hash_partitions(REGCLASS, TEXT, INTEGER, BOOLEAN);
DROP FUNCTION @extschema@.create_hash_partitions_internal(REGCLASS, TEXT, INTEGER);
DROP FUNCTION @extschema@.build_range_condition(TEXT, ANYELEMENT, ANYELEMENT);
DROP FUNCTION @extschema@.get_part_range(REGCLASS, ANYELEMENT);
DROP FUNCTION @extschema@.copy_foreign_keys(REGCLASS, REGCLASS);
DROP FUNCTION @extschema@.invoke_on_partition_created_callback(REGCLASS, REGCLASS, REGPROCEDURE, ANYELEMENT, ANYELEMENT);
DROP FUNCTION @extschema@.invoke_on_partition_created_callback(REGCLASS, REGCLASS, REGPROCEDURE);
DROP FUNCTION @extschema@.split_range_partition(REGCLASS, ANYELEMENT, TEXT, TEXT, OUT ANYARRAY);
DROP FUNCTION @extschema@.drop_range_partition(REGCLASS, BOOLEAN);
DROP FUNCTION @extschema@.attach_range_partition(REGCLASS, REGCLASS, ANYELEMENT, ANYELEMENT);
DROP FUNCTION @extschema@.detach_range_partition(REGCLASS);

/* ------------------------------------------------------------------------
* Alter functions' modifiers
Expand Down Expand Up @@ -441,7 +447,7 @@ LANGUAGE C;


CREATE OR REPLACE FUNCTION @extschema@.split_range_partition(
partition REGCLASS,
partition_relid REGCLASS,
split_value ANYELEMENT,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL,
Expand All @@ -458,13 +464,13 @@ DECLARE
v_check_name TEXT;

BEGIN
v_parent = @extschema@.get_parent_of_partition(partition);
v_parent = @extschema@.get_parent_of_partition(partition_relid);

/* Acquire lock on parent */
PERFORM @extschema@.lock_partitioned_relation(v_parent);

/* Acquire data modification lock (prevent further modifications) */
PERFORM @extschema@.prevent_relation_modification(partition);
PERFORM @extschema@.prevent_relation_modification(partition_relid);

v_atttype = @extschema@.get_partition_key_type(v_parent);

Expand All @@ -475,13 +481,13 @@ BEGIN

/* Check if this is a RANGE partition */
IF v_part_type != 2 THEN
RAISE EXCEPTION '"%" is not a RANGE partition', partition::TEXT;
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
END IF;

/* Get partition values range */
EXECUTE format('SELECT @extschema@.get_part_range($1, NULL::%s)',
@extschema@.get_base_type(v_atttype)::TEXT)
USING partition
USING partition_relid
INTO p_range;

IF p_range IS NULL THEN
Expand All @@ -507,21 +513,21 @@ BEGIN
v_attname, split_value, p_range[2]);
EXECUTE format('WITH part_data AS (DELETE FROM %s WHERE %s RETURNING *)
INSERT INTO %s SELECT * FROM part_data',
partition::TEXT,
partition_relid::TEXT,
v_cond,
v_new_partition);

/* Alter original partition */
v_cond := @extschema@.build_range_condition(partition::regclass,
v_cond := @extschema@.build_range_condition(partition_relid::regclass,
v_attname, p_range[1], split_value);
v_check_name := @extschema@.build_check_constraint_name(partition, v_attname);
v_check_name := @extschema@.build_check_constraint_name(partition_relid, v_attname);

EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
partition::TEXT,
partition_relid::TEXT,
v_check_name);

EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
partition::TEXT,
partition_relid::TEXT,
v_check_name,
v_cond);

Expand Down Expand Up @@ -747,7 +753,7 @@ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION @extschema@.attach_range_partition(
parent_relid REGCLASS,
partition REGCLASS,
partition_relid REGCLASS,
start_value ANYELEMENT,
end_value ANYELEMENT)
RETURNS TEXT AS
Expand All @@ -759,29 +765,29 @@ DECLARE

BEGIN
PERFORM @extschema@.validate_relname(parent_relid);
PERFORM @extschema@.validate_relname(partition);
PERFORM @extschema@.validate_relname(partition_relid);

/* Acquire lock on parent */
PERFORM @extschema@.lock_partitioned_relation(parent_relid);

/* Ignore temporary tables */
SELECT relpersistence FROM pg_catalog.pg_class
WHERE oid = partition INTO rel_persistence;
WHERE oid = partition_relid INTO rel_persistence;

IF rel_persistence = 't'::CHAR THEN
RAISE EXCEPTION 'temporary table "%" cannot be used as a partition',
partition::TEXT;
partition_relid::TEXT;
END IF;

/* check range overlap */
PERFORM @extschema@.check_range_available(parent_relid, start_value, end_value);

IF NOT @extschema@.validate_relations_equality(parent_relid, partition) THEN
IF NOT @extschema@.validate_relations_equality(parent_relid, partition_relid) THEN
RAISE EXCEPTION 'partition must have the exact same structure as parent';
END IF;

/* Set inheritance */
EXECUTE format('ALTER TABLE %s INHERIT %s', partition, parent_relid);
EXECUTE format('ALTER TABLE %s INHERIT %s', partition_relid, parent_relid);

v_attname := attname FROM @extschema@.pathman_config WHERE partrel = parent_relid;

Expand All @@ -791,9 +797,9 @@ BEGIN

/* Set check constraint */
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
partition::TEXT,
@extschema@.build_check_constraint_name(partition, v_attname),
@extschema@.build_range_condition(partition,
partition_relid::TEXT,
@extschema@.build_check_constraint_name(partition_relid, v_attname),
@extschema@.build_range_condition(partition_relid,
v_attname,
start_value,
end_value));
Expand All @@ -807,30 +813,30 @@ BEGIN
INTO v_init_callback;

PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
partition,
partition_relid,
v_init_callback,
start_value,
end_value);

/* Invalidate cache */
PERFORM @extschema@.on_update_partitions(parent_relid);

RETURN partition;
RETURN partition_relid;
END
$$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION @extschema@.detach_range_partition(
partition REGCLASS)
partition_relid REGCLASS)
RETURNS TEXT AS
$$
DECLARE
v_attname TEXT;
parent_relid REGCLASS;

BEGIN
parent_relid := @extschema@.get_parent_of_partition(partition);
parent_relid := @extschema@.get_parent_of_partition(partition_relid);

/* Acquire lock on parent */
PERFORM @extschema@.prevent_relation_modification(parent_relid);
Expand All @@ -845,23 +851,87 @@ BEGIN

/* Remove inheritance */
EXECUTE format('ALTER TABLE %s NO INHERIT %s',
partition::TEXT,
partition_relid::TEXT,
parent_relid::TEXT);

/* Remove check constraint */
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
partition::TEXT,
@extschema@.build_check_constraint_name(partition, v_attname));
partition_relid::TEXT,
@extschema@.build_check_constraint_name(partition_relid, v_attname));

/* Invalidate cache */
PERFORM @extschema@.on_update_partitions(parent_relid);

RETURN partition;
RETURN partition_relid;
END
$$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION @extschema@.drop_range_partition(
partition_relid REGCLASS,
delete_data BOOLEAN DEFAULT TRUE)
RETURNS TEXT AS
$$
DECLARE
parent_relid REGCLASS;
part_name TEXT;
v_relkind CHAR;
v_rows BIGINT;
v_part_type INTEGER;

BEGIN
parent_relid := @extschema@.get_parent_of_partition(partition_relid);
part_name := partition_relid::TEXT; /* save the name to be returned */

SELECT parttype
FROM @extschema@.pathman_config
WHERE partrel = parent_relid
INTO v_part_type;

/* Check if this is a RANGE partition */
IF v_part_type != 2 THEN
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
END IF;

/* Acquire lock on parent */
PERFORM @extschema@.lock_partitioned_relation(parent_relid);

IF NOT delete_data THEN
EXECUTE format('INSERT INTO %s SELECT * FROM %s',
parent_relid::TEXT,
partition_relid::TEXT);
GET DIAGNOSTICS v_rows = ROW_COUNT;

/* Show number of copied rows */
RAISE NOTICE '% rows copied from %', v_rows, partition_relid::TEXT;
END IF;

SELECT relkind FROM pg_catalog.pg_class
WHERE oid = partition_relid
INTO v_relkind;

/*
* Determine the kind of child relation. It can be either regular
* table (r) or foreign table (f). Depending on relkind we use
* DROP TABLE or DROP FOREIGN TABLE.
*/
IF v_relkind = 'f' THEN
EXECUTE format('DROP FOREIGN TABLE %s', partition_relid::TEXT);
ELSE
EXECUTE format('DROP TABLE %s', partition_relid::TEXT);
END IF;

/* Invalidate cache */
PERFORM @extschema@.on_update_partitions(parent_relid);

RETURN part_name;
END
$$
LANGUAGE plpgsql
SET pg_pathman.enable_partitionfilter = off;


CREATE OR REPLACE FUNCTION @extschema@.drop_range_partition_expand_next(
partition REGCLASS)
RETURNS VOID AS 'pg_pathman', 'drop_range_partition_expand_next'
Expand All @@ -875,10 +945,3 @@ CREATE OR REPLACE FUNCTION @extschema@.build_range_condition(
end_value ANYELEMENT)
RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
LANGUAGE C;


CREATE OR REPLACE FUNCTION @extschema@.get_part_range(
partition REGCLASS,
dummy ANYELEMENT)
RETURNS ANYARRAY AS 'pg_pathman', 'get_part_range_by_oid'
LANGUAGE C;
Loading

0 comments on commit b8659fa

Please sign in to comment.