Skip to content

Commit 57305e2

Browse files
committed
[WIP] huge refactoring, use local process cache instead of shared memory
1 parent 08b0125 commit 57305e2

29 files changed

+2093
-1530
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# contrib/pg_pathman/Makefile
22

33
MODULE_big = pg_pathman
4-
OBJS = src/init.o src/utils.o src/partition_filter.o src/runtimeappend.o src/runtime_merge_append.o src/pg_pathman.o src/dsm_array.o \
5-
src/rangeset.o src/pl_funcs.o src/worker.o src/hooks.o src/nodes_common.o $(WIN32RES)
4+
OBJS = src/init.o src/relation_info.o src/utils.o src/partition_filter.o src/runtimeappend.o \
5+
src/runtime_merge_append.o src/pg_pathman.o src/dsm_array.o src/rangeset.o src/pl_funcs.o \
6+
src/worker.o src/hooks.o src/nodes_common.o $(WIN32RES)
67

78
EXTENSION = pg_pathman
89
EXTVERSION = 0.1
@@ -35,6 +36,6 @@ submake-isolation:
3536
isolationcheck: | submake-isolation
3637
$(MKDIR_P) isolation_output
3738
$(pg_isolation_regress_check) \
38-
--temp-config=$(top_srcdir)/$(subdir)/conf.add \
39-
--outputdir=./isolation_output \
40-
$(ISOLATIONCHECKS)
39+
--temp-config=$(top_srcdir)/$(subdir)/conf.add \
40+
--outputdir=./isolation_output \
41+
$(ISOLATIONCHECKS)

hash.sql

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,29 @@ BEGIN
3636

3737
v_hashfunc := @extschema@.get_type_hash_func(v_type::regtype::oid)::regproc;
3838

39+
/* Insert new entry to pathman config */
40+
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
41+
VALUES (relation, attribute, 1);
42+
3943
/* Create partitions and update pg_pathman configuration */
4044
FOR partnum IN 0..partitions_count-1
4145
LOOP
4246
v_child_relname := format('%s.%s',
4347
v_plain_schema,
4448
quote_ident(v_plain_relname || '_' || partnum));
4549

46-
EXECUTE format('CREATE TABLE %s (LIKE %s INCLUDING ALL)'
47-
, v_child_relname
48-
, v_relname);
49-
50-
EXECUTE format('ALTER TABLE %s INHERIT %s'
50+
EXECUTE format('CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)'
5151
, v_child_relname
5252
, v_relname);
5353

54-
EXECUTE format('ALTER TABLE %s ADD CHECK (@extschema@.get_hash(%s(%s), %s) = %s)'
54+
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (@extschema@.get_hash(%s(%s), %s) = %s)'
5555
, v_child_relname
56+
, @extschema@.build_check_constraint_name(v_child_relname::regclass, attribute)
5657
, v_hashfunc
5758
, attribute
5859
, partitions_count
5960
, partnum);
6061
END LOOP;
61-
INSERT INTO @extschema@.pathman_config (relname, attname, parttype)
62-
VALUES (v_relname, attribute, 1);
63-
64-
/* Create triggers */
65-
/* Do not create update trigger by default */
66-
-- PERFORM @extschema@.create_hash_update_trigger(relation, attribute, partitions_count);
6762

6863
/* Notify backend about changes */
6964
PERFORM @extschema@.on_create_partitions(relation::oid);
@@ -83,25 +78,36 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_update_trigger(
8378
RETURNS VOID AS
8479
$$
8580
DECLARE
86-
func TEXT := '
87-
CREATE OR REPLACE FUNCTION %s()
88-
RETURNS TRIGGER AS
89-
$body$
90-
DECLARE old_hash INTEGER; new_hash INTEGER; q TEXT;
91-
BEGIN
92-
old_hash := @extschema@.get_hash(%9$s(OLD.%2$s), %3$s);
93-
new_hash := @extschema@.get_hash(%9$s(NEW.%2$s), %3$s);
94-
IF old_hash = new_hash THEN RETURN NEW; END IF;
95-
q := format(''DELETE FROM %8$s WHERE %4$s'', old_hash);
96-
EXECUTE q USING %5$s;
97-
q := format(''INSERT INTO %8$s VALUES (%6$s)'', new_hash);
98-
EXECUTE q USING %7$s;
99-
RETURN NULL;
100-
END $body$ LANGUAGE plpgsql';
101-
trigger TEXT := '
102-
CREATE TRIGGER %s
103-
BEFORE UPDATE ON %s
104-
FOR EACH ROW EXECUTE PROCEDURE %s()';
81+
func TEXT := 'CREATE OR REPLACE FUNCTION %s()
82+
RETURNS TRIGGER AS
83+
$body$
84+
DECLARE
85+
old_hash INTEGER;
86+
new_hash INTEGER;
87+
q TEXT;
88+
89+
BEGIN
90+
old_hash := @extschema@.get_hash(%9$s(OLD.%2$s), %3$s);
91+
new_hash := @extschema@.get_hash(%9$s(NEW.%2$s), %3$s);
92+
93+
IF old_hash = new_hash THEN
94+
RETURN NEW;
95+
END IF;
96+
97+
q := format(''DELETE FROM %8$s WHERE %4$s'', old_hash);
98+
EXECUTE q USING %5$s;
99+
100+
q := format(''INSERT INTO %8$s VALUES (%6$s)'', new_hash);
101+
EXECUTE q USING %7$s;
102+
103+
RETURN NULL;
104+
END $body$
105+
LANGUAGE plpgsql';
106+
107+
trigger TEXT := 'CREATE TRIGGER %s
108+
BEFORE UPDATE ON %s
109+
FOR EACH ROW EXECUTE PROCEDURE %s()';
110+
105111
att_names TEXT;
106112
old_fields TEXT;
107113
new_fields TEXT;
@@ -117,6 +123,7 @@ DECLARE
117123
child_relname_format TEXT;
118124
atttype TEXT;
119125
hashfunc TEXT;
126+
120127
BEGIN
121128
SELECT * INTO plain_schema, plain_relname
122129
FROM @extschema@.get_plain_schema_and_relname(relation);
@@ -136,7 +143,7 @@ BEGIN
136143
att_val_fmt,
137144
att_fmt;
138145

139-
attr := attname FROM @extschema@.pathman_config WHERE relname::regclass = relation;
146+
attr := attname FROM @extschema@.pathman_config WHERE partrel = relation;
140147

141148
IF attr IS NULL THEN
142149
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(relation::TEXT);

init.sql

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@
1010

1111
/*
1212
* Pathman config
13-
* relname - schema qualified relation name
14-
* attname - partitioning key
15-
* parttype - partitioning type:
16-
* 1 - HASH
17-
* 2 - RANGE
18-
* range_interval - base interval for RANGE partitioning in string representation
13+
* partrel - regclass (relation type, stored as Oid)
14+
* attname - partitioning key
15+
* parttype - partitioning type:
16+
* 1 - HASH
17+
* 2 - RANGE
18+
* range_interval - base interval for RANGE partitioning as string
1919
*/
2020
CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
2121
id SERIAL PRIMARY KEY,
22-
relname VARCHAR(127),
23-
attname VARCHAR(127),
24-
parttype INTEGER,
25-
range_interval TEXT
22+
partrel REGCLASS NOT NULL,
23+
attname TEXT NOT NULL,
24+
parttype INTEGER NOT NULL,
25+
range_interval TEXT,
26+
27+
CHECK (parttype >= 1 OR parttype <= 2) /* check for allowed part types */
2628
);
29+
2730
SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config', '');
2831

2932
CREATE OR REPLACE FUNCTION @extschema@.on_create_partitions(relid OID)
@@ -112,13 +115,13 @@ LANGUAGE plpgsql;
112115
/*
113116
* Disable pathman partitioning for specified relation
114117
*/
115-
CREATE OR REPLACE FUNCTION @extschema@.disable_partitioning(IN relation TEXT)
118+
CREATE OR REPLACE FUNCTION @extschema@.disable_partitioning(relation regclass)
116119
RETURNS VOID AS
117120
$$
118121
BEGIN
119122
relation := @extschema@.validate_relname(relation);
120123

121-
DELETE FROM @extschema@.pathman_config WHERE relname = relation;
124+
DELETE FROM @extschema@.pathman_config WHERE partrel = relation;
122125
PERFORM @extschema@.drop_triggers(relation);
123126

124127
/* Notify backend about changes */
@@ -176,7 +179,7 @@ DECLARE
176179
v_rec RECORD;
177180
is_referenced BOOLEAN;
178181
BEGIN
179-
IF EXISTS (SELECT * FROM @extschema@.pathman_config WHERE relname::regclass = p_relation) THEN
182+
IF EXISTS (SELECT * FROM @extschema@.pathman_config WHERE partrel = p_relation) THEN
180183
RAISE EXCEPTION 'Relation "%" has already been partitioned', p_relation;
181184
END IF;
182185

@@ -298,7 +301,7 @@ $$
298301
LANGUAGE plpgsql;
299302

300303
/*
301-
* DDL trigger that deletes entry from pathman_config
304+
* DDL trigger that deletes entry from pathman_config table
302305
*/
303306
CREATE OR REPLACE FUNCTION @extschema@.pathman_ddl_trigger_func()
304307
RETURNS event_trigger AS
@@ -307,11 +310,12 @@ DECLARE
307310
obj record;
308311
BEGIN
309312
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects() as events
310-
JOIN @extschema@.pathman_config as cfg ON cfg.relname = events.object_identity
313+
JOIN @extschema@.pathman_config as cfg
314+
ON partrel::oid = events.objid
311315
LOOP
312316
IF obj.object_type = 'table' THEN
313-
EXECUTE 'DELETE FROM @extschema@.pathman_config WHERE relname = $1'
314-
USING obj.object_identity;
317+
EXECUTE 'DELETE FROM @extschema@.pathman_config WHERE partrel = $1'
318+
USING obj.objid;
315319
END IF;
316320
END LOOP;
317321
END
@@ -375,7 +379,7 @@ BEGIN
375379
PERFORM @extschema@.drop_triggers(relation);
376380

377381
WITH config_num_deleted AS (DELETE FROM @extschema@.pathman_config
378-
WHERE relname::regclass = relation
382+
WHERE partrel = relation
379383
RETURNING *)
380384
SELECT count(*) from config_num_deleted INTO conf_num_del;
381385

@@ -417,3 +421,15 @@ RETURNS OID AS 'pg_pathman', 'get_type_hash_func' LANGUAGE C STRICT;
417421
*/
418422
CREATE OR REPLACE FUNCTION @extschema@.get_hash(INTEGER, INTEGER)
419423
RETURNS INTEGER AS 'pg_pathman', 'get_hash' LANGUAGE C STRICT;
424+
425+
426+
/*
427+
* Build check constraint name for a specified relation's column
428+
*/
429+
CREATE OR REPLACE FUNCTION @extschema@.build_check_constraint_name(REGCLASS, INT2)
430+
RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attnum'
431+
LANGUAGE C STRICT;
432+
433+
CREATE OR REPLACE FUNCTION @extschema@.build_check_constraint_name(REGCLASS, TEXT)
434+
RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attname'
435+
LANGUAGE C STRICT;

0 commit comments

Comments
 (0)