Skip to content

Commit 8f87c53

Browse files
committed
Rewrite all mixed-sign comparisons, rewrite some plpgsql functions to C (append_partitions_on_demand, get_range_condition, is_date_type, is_attribute_nullable, get_attribute_type_name), introduce 'delayed invalidation' list & finish_delayed_invalidation() function, group functions in utils.h
1 parent 4d8e430 commit 8f87c53

20 files changed

+939
-666
lines changed

hash.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ BEGIN
6868

6969
RETURN partitions_count;
7070
END
71-
$$ LANGUAGE plpgsql;
71+
$$ LANGUAGE plpgsql
72+
SET client_min_messages = WARNING;
7273

7374
/*
7475
* Creates an update trigger

init.sql

Lines changed: 115 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ------------------------------------------------------------------------
22
*
33
* init.sql
4-
* Creates config table and provides common utility functions
4+
* Creates config table and provides common utility functions
55
*
66
* Copyright (c) 2015-2016, Postgres Professional
77
*
@@ -27,57 +27,9 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
2727
CHECK (parttype >= 1 OR parttype <= 2) /* check for allowed part types */
2828
);
2929

30-
SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config', '');
31-
32-
CREATE OR REPLACE FUNCTION @extschema@.on_create_partitions(relid OID)
33-
RETURNS VOID AS 'pg_pathman', 'on_partitions_created' LANGUAGE C STRICT;
34-
35-
CREATE OR REPLACE FUNCTION @extschema@.on_update_partitions(relid OID)
36-
RETURNS VOID AS 'pg_pathman', 'on_partitions_updated' LANGUAGE C STRICT;
37-
38-
CREATE OR REPLACE FUNCTION @extschema@.on_remove_partitions(relid OID)
39-
RETURNS VOID AS 'pg_pathman', 'on_partitions_removed' LANGUAGE C STRICT;
40-
41-
CREATE OR REPLACE FUNCTION @extschema@.find_or_create_range_partition(relid OID, value ANYELEMENT)
42-
RETURNS OID AS 'pg_pathman', 'find_or_create_range_partition' LANGUAGE C STRICT;
43-
44-
45-
/*
46-
* Returns min and max values for specified RANGE partition.
47-
*/
48-
CREATE OR REPLACE FUNCTION @extschema@.get_partition_range(
49-
parent_relid OID, partition_relid OID, dummy ANYELEMENT)
50-
RETURNS ANYARRAY AS 'pg_pathman', 'get_partition_range' LANGUAGE C STRICT;
51-
52-
53-
/*
54-
* Returns N-th range (in form of array)
55-
*/
56-
CREATE OR REPLACE FUNCTION @extschema@.get_range_by_idx(
57-
parent_relid OID, idx INTEGER, dummy ANYELEMENT)
58-
RETURNS ANYARRAY AS 'pg_pathman', 'get_range_by_idx' LANGUAGE C STRICT;
59-
60-
/*
61-
* Returns min value of the first range for relation
62-
*/
63-
CREATE OR REPLACE FUNCTION @extschema@.get_min_range_value(
64-
parent_relid OID, dummy ANYELEMENT)
65-
RETURNS ANYELEMENT AS 'pg_pathman', 'get_min_range_value' LANGUAGE C STRICT;
6630

67-
/*
68-
* Returns max value of the last range for relation
69-
*/
70-
CREATE OR REPLACE FUNCTION @extschema@.get_max_range_value(
71-
parent_relid OID, dummy ANYELEMENT)
72-
RETURNS ANYELEMENT AS 'pg_pathman', 'get_max_range_value' LANGUAGE C STRICT;
31+
SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config', '');
7332

74-
/*
75-
* Checks if range overlaps with existing partitions.
76-
* Returns TRUE if overlaps and FALSE otherwise.
77-
*/
78-
CREATE OR REPLACE FUNCTION @extschema@.check_overlap(
79-
parent_relid OID, range_min ANYELEMENT, range_max ANYELEMENT)
80-
RETURNS BOOLEAN AS 'pg_pathman', 'check_overlap' LANGUAGE C STRICT;
8133

8234
/*
8335
* Copy rows to partitions
@@ -111,7 +63,6 @@ END
11163
$$
11264
LANGUAGE plpgsql;
11365

114-
11566
/*
11667
* Disable pathman partitioning for specified relation
11768
*/
@@ -130,43 +81,6 @@ END
13081
$$
13182
LANGUAGE plpgsql;
13283

133-
134-
/*
135-
* Returns attribute type name for relation
136-
*/
137-
CREATE OR REPLACE FUNCTION @extschema@.get_attribute_type_name(
138-
p_relation REGCLASS
139-
, p_attname TEXT
140-
, OUT p_atttype TEXT)
141-
RETURNS TEXT AS
142-
$$
143-
BEGIN
144-
SELECT typname::TEXT INTO p_atttype
145-
FROM pg_type JOIN pg_attribute on atttypid = "oid"
146-
WHERE attrelid = p_relation::oid and attname = lower(p_attname);
147-
END
148-
$$
149-
LANGUAGE plpgsql;
150-
151-
152-
/*
153-
* Checks if attribute is nullable
154-
*/
155-
CREATE OR REPLACE FUNCTION @extschema@.is_attribute_nullable(
156-
p_relation REGCLASS
157-
, p_attname TEXT
158-
, OUT p_nullable BOOLEAN)
159-
RETURNS BOOLEAN AS
160-
$$
161-
BEGIN
162-
SELECT NOT attnotnull INTO p_nullable
163-
FROM pg_type JOIN pg_attribute on atttypid = "oid"
164-
WHERE attrelid = p_relation::oid and attname = lower(p_attname);
165-
END
166-
$$
167-
LANGUAGE plpgsql;
168-
169-
17084
/*
17185
* Aggregates several common relation checks before partitioning. Suitable for every partitioning type.
17286
*/
@@ -217,17 +131,6 @@ END
217131
$$
218132
LANGUAGE plpgsql;
219133

220-
221-
CREATE OR REPLACE FUNCTION @extschema@.get_plain_relname(cls regclass)
222-
RETURNS TEXT AS
223-
$$
224-
BEGIN
225-
RETURN relname FROM pg_class WHERE oid = cls::oid;
226-
END
227-
$$
228-
LANGUAGE plpgsql;
229-
230-
231134
/*
232135
* Validates relation name. It must be schema qualified
233136
*/
@@ -240,7 +143,6 @@ END
240143
$$
241144
LANGUAGE plpgsql;
242145

243-
244146
/*
245147
* Returns schema-qualified name for table
246148
*/
@@ -288,18 +190,6 @@ END
288190
$$
289191
LANGUAGE plpgsql;
290192

291-
/*
292-
* Check if regclass if date or timestamp
293-
*/
294-
CREATE OR REPLACE FUNCTION @extschema@.is_date(cls REGTYPE)
295-
RETURNS BOOLEAN AS
296-
$$
297-
BEGIN
298-
RETURN cls IN ('timestamp'::regtype, 'timestamptz'::regtype, 'date'::regtype);
299-
END
300-
$$
301-
LANGUAGE plpgsql;
302-
303193
/*
304194
* DDL trigger that deletes entry from pathman_config table
305195
*/
@@ -322,22 +212,6 @@ END
322212
$$
323213
LANGUAGE plpgsql;
324214

325-
CREATE EVENT TRIGGER pathman_ddl_trigger
326-
ON sql_drop
327-
EXECUTE PROCEDURE @extschema@.pathman_ddl_trigger_func();
328-
329-
/*
330-
* Acquire partitions lock to prevent concurrent partitions creation
331-
*/
332-
CREATE OR REPLACE FUNCTION @extschema@.acquire_partitions_lock()
333-
RETURNS VOID AS 'pg_pathman', 'acquire_partitions_lock' LANGUAGE C STRICT;
334-
335-
/*
336-
* Release partitions lock
337-
*/
338-
CREATE OR REPLACE FUNCTION @extschema@.release_partitions_lock()
339-
RETURNS VOID AS 'pg_pathman', 'release_partitions_lock' LANGUAGE C STRICT;
340-
341215
/*
342216
* Drop trigger
343217
*/
@@ -410,18 +284,122 @@ END
410284
$$ LANGUAGE plpgsql
411285
SET pg_pathman.enable_partitionfilter = off;
412286

287+
288+
289+
CREATE EVENT TRIGGER pathman_ddl_trigger
290+
ON sql_drop
291+
EXECUTE PROCEDURE @extschema@.pathman_ddl_trigger_func();
292+
293+
294+
/*
295+
* Acquire partitions lock to prevent concurrent partitions creation
296+
*/
297+
CREATE OR REPLACE FUNCTION @extschema@.acquire_partitions_lock()
298+
RETURNS VOID AS 'pg_pathman', 'acquire_partitions_lock'
299+
LANGUAGE C STRICT;
300+
301+
/*
302+
* Release partitions lock
303+
*/
304+
CREATE OR REPLACE FUNCTION @extschema@.release_partitions_lock()
305+
RETURNS VOID AS 'pg_pathman', 'release_partitions_lock'
306+
LANGUAGE C STRICT;
307+
308+
/*
309+
* Check if regclass is date or timestamp
310+
*/
311+
CREATE OR REPLACE FUNCTION @extschema@.is_date_type(cls REGTYPE)
312+
RETURNS BOOLEAN AS 'pg_pathman', 'is_date_type'
313+
LANGUAGE C STRICT;
314+
315+
/*
316+
* Checks if range overlaps with existing partitions.
317+
* Returns TRUE if overlaps and FALSE otherwise.
318+
*/
319+
CREATE OR REPLACE FUNCTION @extschema@.check_overlap(
320+
parent_relid OID, range_min ANYELEMENT, range_max ANYELEMENT)
321+
RETURNS BOOLEAN AS 'pg_pathman', 'check_overlap'
322+
LANGUAGE C STRICT;
323+
324+
325+
CREATE OR REPLACE FUNCTION @extschema@.on_create_partitions(relid OID)
326+
RETURNS VOID AS 'pg_pathman', 'on_partitions_created'
327+
LANGUAGE C STRICT;
328+
329+
CREATE OR REPLACE FUNCTION @extschema@.on_update_partitions(relid OID)
330+
RETURNS VOID AS 'pg_pathman', 'on_partitions_updated'
331+
LANGUAGE C STRICT;
332+
333+
CREATE OR REPLACE FUNCTION @extschema@.on_remove_partitions(relid OID)
334+
RETURNS VOID AS 'pg_pathman', 'on_partitions_removed'
335+
LANGUAGE C STRICT;
336+
337+
338+
CREATE OR REPLACE FUNCTION @extschema@.find_or_create_range_partition(relid OID, value ANYELEMENT)
339+
RETURNS OID AS 'pg_pathman', 'find_or_create_range_partition'
340+
LANGUAGE C STRICT;
341+
342+
343+
/*
344+
* Returns min and max values for specified RANGE partition.
345+
*/
346+
CREATE OR REPLACE FUNCTION @extschema@.get_partition_range(
347+
parent_relid OID, partition_relid OID, dummy ANYELEMENT)
348+
RETURNS ANYARRAY AS 'pg_pathman', 'get_partition_range'
349+
LANGUAGE C STRICT;
350+
351+
352+
/*
353+
* Returns N-th range (in form of array)
354+
*/
355+
CREATE OR REPLACE FUNCTION @extschema@.get_range_by_idx(
356+
parent_relid OID, idx INTEGER, dummy ANYELEMENT)
357+
RETURNS ANYARRAY AS 'pg_pathman', 'get_range_by_idx'
358+
LANGUAGE C STRICT;
359+
360+
/*
361+
* Returns min value of the first range for relation
362+
*/
363+
CREATE OR REPLACE FUNCTION @extschema@.get_min_range_value(
364+
parent_relid OID, dummy ANYELEMENT)
365+
RETURNS ANYELEMENT AS 'pg_pathman', 'get_min_range_value'
366+
LANGUAGE C STRICT;
367+
368+
/*
369+
* Returns max value of the last range for relation
370+
*/
371+
CREATE OR REPLACE FUNCTION @extschema@.get_max_range_value(
372+
parent_relid OID, dummy ANYELEMENT)
373+
RETURNS ANYELEMENT AS 'pg_pathman', 'get_max_range_value'
374+
LANGUAGE C STRICT;
375+
413376
/*
414377
* Returns hash function OID for specified type
415378
*/
416379
CREATE OR REPLACE FUNCTION @extschema@.get_type_hash_func(OID)
417-
RETURNS OID AS 'pg_pathman', 'get_type_hash_func' LANGUAGE C STRICT;
380+
RETURNS OID AS 'pg_pathman', 'get_type_hash_func'
381+
LANGUAGE C STRICT;
418382

419383
/*
420384
* Calculates hash for integer value
421385
*/
422386
CREATE OR REPLACE FUNCTION @extschema@.get_hash(INTEGER, INTEGER)
423-
RETURNS INTEGER AS 'pg_pathman', 'get_hash' LANGUAGE C STRICT;
387+
RETURNS INTEGER AS 'pg_pathman', 'get_hash'
388+
LANGUAGE C STRICT;
424389

390+
/*
391+
* Checks if attribute is nullable
392+
*/
393+
CREATE OR REPLACE FUNCTION @extschema@.is_attribute_nullable(REGCLASS, TEXT)
394+
RETURNS BOOLEAN AS 'pg_pathman', 'is_attribute_nullable'
395+
LANGUAGE C STRICT;
396+
397+
/*
398+
* Returns attribute type name for relation
399+
*/
400+
CREATE OR REPLACE FUNCTION @extschema@.get_attribute_type_name(REGCLASS, TEXT)
401+
RETURNS TEXT AS 'pg_pathman', 'get_attribute_type_name'
402+
LANGUAGE C STRICT;
425403

426404
/*
427405
* Build check constraint name for a specified relation's column
@@ -433,3 +411,10 @@ LANGUAGE C STRICT;
433411
CREATE OR REPLACE FUNCTION @extschema@.build_check_constraint_name(REGCLASS, TEXT)
434412
RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attname'
435413
LANGUAGE C STRICT;
414+
415+
/*
416+
* DEBUG: Place this inside some plpgsql fuction and set breakpoint.
417+
*/
418+
CREATE OR REPLACE FUNCTION @extschema@.debug_capture()
419+
RETURNS VOID AS 'pg_pathman', 'debug_capture'
420+
LANGUAGE C STRICT;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy