Skip to content

Commit 0ae222a

Browse files
committed
refactoring (Pl/PgSQL), remove function common_relation_checks()
1 parent e58905b commit 0ae222a

File tree

4 files changed

+62
-67
lines changed

4 files changed

+62
-67
lines changed

expected/pathman_foreign_keys.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ CREATE TABLE fkeys.replies(
7171
INSERT INTO fkeys.messages SELECT g, md5(g::text) FROM generate_series(1, 10) as g;
7272
INSERT INTO fkeys.replies SELECT g, g, md5(g::text) FROM generate_series(1, 10) as g;
7373
SELECT create_range_partitions('fkeys.messages', 'id', 1, 100, 2); /* not ok */
74-
WARNING: foreign key "replies_message_id_fkey" references relation "fkeys.messages"
75-
ERROR: relation "fkeys.messages" is referenced from other relations
74+
WARNING: foreign key "replies_message_id_fkey" references table "fkeys.messages"
75+
ERROR: table "fkeys.messages" is referenced from other tables
7676
ALTER TABLE fkeys.replies DROP CONSTRAINT replies_message_id_fkey;
7777
SELECT create_range_partitions('fkeys.messages', 'id', 1, 100, 2); /* ok */
7878
NOTICE: sequence "messages_seq" does not exist, skipping

hash.sql

+3-11
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
2121
RETURNS INTEGER AS
2222
$$
2323
BEGIN
24-
PERFORM @extschema@.validate_relname(parent_relid);
25-
26-
IF partition_data = true THEN
27-
/* Acquire data modification lock */
28-
PERFORM @extschema@.prevent_relation_modification(parent_relid);
29-
ELSE
30-
/* Acquire lock on parent */
31-
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
32-
END IF;
33-
3424
expression := lower(expression);
35-
PERFORM @extschema@.common_relation_checks(parent_relid, expression);
25+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
26+
expression,
27+
partition_data);
3628

3729
/* Insert new entry to pathman config */
3830
EXECUTE format('ANALYZE %s', parent_relid);

init.sql

+27-20
Original file line numberDiff line numberDiff line change
@@ -438,51 +438,58 @@ $$
438438
LANGUAGE plpgsql STRICT;
439439

440440
/*
441-
* Aggregates several common relation checks before partitioning.
442-
* Suitable for every partitioning type.
441+
* Check a few things and take locks before partitioning.
443442
*/
444-
CREATE OR REPLACE FUNCTION @extschema@.common_relation_checks(
445-
relation REGCLASS,
446-
expression TEXT)
447-
RETURNS BOOLEAN AS
443+
CREATE OR REPLACE FUNCTION @extschema@.prepare_for_partitioning(
444+
parent_relid REGCLASS,
445+
expression TEXT,
446+
partition_data BOOLEAN)
447+
RETURNS VOID AS
448448
$$
449449
DECLARE
450-
v_rec RECORD;
450+
constr_name TEXT;
451451
is_referenced BOOLEAN;
452452
rel_persistence CHAR;
453453

454454
BEGIN
455+
PERFORM @extschema@.validate_relname(parent_relid);
456+
457+
IF partition_data = true THEN
458+
/* Acquire data modification lock */
459+
PERFORM @extschema@.prevent_relation_modification(parent_relid);
460+
ELSE
461+
/* Acquire lock on parent */
462+
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
463+
END IF;
464+
455465
/* Ignore temporary tables */
456466
SELECT relpersistence FROM pg_catalog.pg_class
457-
WHERE oid = relation INTO rel_persistence;
467+
WHERE oid = parent_relid INTO rel_persistence;
458468

459469
IF rel_persistence = 't'::CHAR THEN
460-
RAISE EXCEPTION 'temporary table "%" cannot be partitioned',
461-
relation::TEXT;
470+
RAISE EXCEPTION 'temporary table "%" cannot be partitioned', parent_relid;
462471
END IF;
463472

464473
IF EXISTS (SELECT * FROM @extschema@.pathman_config
465-
WHERE partrel = relation) THEN
466-
RAISE EXCEPTION 'relation "%" has already been partitioned', relation;
474+
WHERE partrel = parent_relid) THEN
475+
RAISE EXCEPTION 'table "%" has already been partitioned', parent_relid;
467476
END IF;
468477

469478
/* Check if there are foreign keys that reference the relation */
470-
FOR v_rec IN (SELECT * FROM pg_catalog.pg_constraint
471-
WHERE confrelid = relation::REGCLASS::OID)
479+
FOR constr_name IN (SELECT conname FROM pg_catalog.pg_constraint
480+
WHERE confrelid = parent_relid::REGCLASS::OID)
472481
LOOP
473482
is_referenced := TRUE;
474-
RAISE WARNING 'foreign key "%" references relation "%"',
475-
v_rec.conname, relation;
483+
RAISE WARNING 'foreign key "%" references table "%"', constr_name, parent_relid;
476484
END LOOP;
477485

478486
IF is_referenced THEN
479-
RAISE EXCEPTION 'relation "%" is referenced from other relations', relation;
487+
RAISE EXCEPTION 'table "%" is referenced from other tables', parent_relid;
480488
END IF;
481489

482-
RETURN FALSE;
483490
END
484-
$$
485-
LANGUAGE plpgsql;
491+
$$ LANGUAGE plpgsql;
492+
486493

487494
/*
488495
* Returns relname without quotes or something.

range.sql

+30-34
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,6 @@ BEGIN
6060
END
6161
$$ LANGUAGE plpgsql;
6262

63-
64-
CREATE OR REPLACE FUNCTION @extschema@.prepare_for_partitioning(
65-
parent_relid REGCLASS,
66-
expression TEXT,
67-
partition_data BOOLEAN)
68-
RETURNS VOID AS
69-
$$
70-
BEGIN
71-
PERFORM @extschema@.validate_relname(parent_relid);
72-
73-
IF partition_data = true THEN
74-
/* Acquire data modification lock */
75-
PERFORM @extschema@.prevent_relation_modification(parent_relid);
76-
ELSE
77-
/* Acquire lock on parent */
78-
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
79-
END IF;
80-
81-
expression := lower(expression);
82-
PERFORM @extschema@.common_relation_checks(parent_relid, expression);
83-
END
84-
$$ LANGUAGE plpgsql;
85-
8663
/*
8764
* Creates RANGE partitions for specified relation based on datetime attribute
8865
*/
@@ -106,7 +83,9 @@ DECLARE
10683

10784
BEGIN
10885
expression := lower(expression);
109-
PERFORM @extschema@.prepare_for_partitioning(parent_relid, expression, partition_data);
86+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
87+
expression,
88+
partition_data);
11089

11190
IF p_count < 0 THEN
11291
RAISE EXCEPTION '"p_count" must not be less than 0';
@@ -206,7 +185,9 @@ DECLARE
206185

207186
BEGIN
208187
expression := lower(expression);
209-
PERFORM @extschema@.prepare_for_partitioning(parent_relid, expression, partition_data);
188+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
189+
expression,
190+
partition_data);
210191

211192
IF p_count < 0 THEN
212193
RAISE EXCEPTION 'partitions count must not be less than zero';
@@ -307,7 +288,9 @@ BEGIN
307288
END IF;
308289

309290
expression := lower(expression);
310-
PERFORM @extschema@.prepare_for_partitioning(parent_relid, expression, partition_data);
291+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
292+
expression,
293+
partition_data);
311294

312295
/* Check boundaries */
313296
PERFORM @extschema@.check_boundaries(parent_relid,
@@ -358,7 +341,9 @@ DECLARE
358341

359342
BEGIN
360343
expression := lower(expression);
361-
PERFORM @extschema@.prepare_for_partitioning(parent_relid, expression, partition_data);
344+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
345+
expression,
346+
partition_data);
362347

363348
/* Check boundaries */
364349
PERFORM @extschema@.check_boundaries(parent_relid,
@@ -415,8 +400,9 @@ DECLARE
415400

416401
BEGIN
417402
expression := lower(expression);
418-
PERFORM @extschema@.prepare_for_partitioning(parent_relid, expression,
419-
partition_data);
403+
PERFORM @extschema@.prepare_for_partitioning(parent_relid,
404+
expression,
405+
partition_data);
420406

421407
/* Check boundaries */
422408
PERFORM @extschema@.check_boundaries(parent_relid,
@@ -483,6 +469,9 @@ DECLARE
483469
BEGIN
484470
parent_relid = @extschema@.get_parent_of_partition(partition_relid);
485471

472+
PERFORM @extschema@.validate_relname(parent_relid);
473+
PERFORM @extschema@.validate_relname(partition_relid);
474+
486475
/* Acquire lock on parent */
487476
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
488477

@@ -839,8 +828,11 @@ DECLARE
839828

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

832+
PERFORM @extschema@.validate_relname(parent_relid);
833+
PERFORM @extschema@.validate_relname(partition_relid);
834+
835+
part_name := partition_relid::TEXT; /* save the name to be returned */
844836
part_type := @extschema@.get_partition_type(parent_relid);
845837

846838
/* Check if this is a RANGE partition */
@@ -972,18 +964,22 @@ RETURNS TEXT AS
972964
$$
973965
DECLARE
974966
parent_relid REGCLASS;
975-
part_expr TEXT;
967+
part_type INTEGER;
976968

977969
BEGIN
978970
parent_relid := @extschema@.get_parent_of_partition(partition_relid);
979971

972+
PERFORM @extschema@.validate_relname(parent_relid);
973+
PERFORM @extschema@.validate_relname(partition_relid);
974+
980975
/* Acquire lock on parent */
981976
PERFORM @extschema@.prevent_relation_modification(parent_relid);
982977

983-
part_expr := @extschema@.get_partition_key(parent_relid);
978+
part_type := @extschema@.get_partition_type(parent_relid);
984979

985-
IF part_expr IS NULL THEN
986-
RAISE EXCEPTION 'table "%" is not partitioned', parent_relid::TEXT;
980+
/* Check if this is a RANGE partition */
981+
IF part_type != 2 THEN
982+
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
987983
END IF;
988984

989985
/* Remove inheritance */

0 commit comments

Comments
 (0)