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 (
2727CHECK (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- CREATEOR REPLACE FUNCTION @extschema@.on_create_partitions(relidOID )
33- RETURNS VOIDAS ' pg_pathman' ,' on_partitions_created' LANGUAGE C STRICT;
34-
35- CREATEOR REPLACE FUNCTION @extschema@.on_update_partitions(relidOID )
36- RETURNS VOIDAS ' pg_pathman' ,' on_partitions_updated' LANGUAGE C STRICT;
37-
38- CREATEOR REPLACE FUNCTION @extschema@.on_remove_partitions(relidOID )
39- RETURNS VOIDAS ' pg_pathman' ,' on_partitions_removed' LANGUAGE C STRICT;
40-
41- CREATEOR REPLACE FUNCTION @extschema@.find_or_create_range_partition(relidOID , value ANYELEMENT)
42- RETURNSOID 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- CREATEOR REPLACE FUNCTION @extschema@.get_partition_range(
49- parent_relidOID , partition_relidOID , dummy ANYELEMENT)
50- RETURNS ANYARRAYAS ' pg_pathman' ,' get_partition_range' LANGUAGE C STRICT;
51-
52-
53- /*
54- * Returns N-th range (in form of array)
55- */
56- CREATEOR REPLACE FUNCTION @extschema@.get_range_by_idx(
57- parent_relidOID , idxINTEGER , dummy ANYELEMENT)
58- RETURNS ANYARRAYAS ' pg_pathman' ,' get_range_by_idx' LANGUAGE C STRICT;
59-
60- /*
61- * Returns min value of the first range for relation
62- */
63- CREATEOR REPLACE FUNCTION @extschema@.get_min_range_value(
64- parent_relidOID , dummy ANYELEMENT)
65- RETURNS ANYELEMENTAS ' pg_pathman' ,' get_min_range_value' LANGUAGE C STRICT;
6630
67- /*
68- * Returns max value of the last range for relation
69- */
70- CREATEOR REPLACE FUNCTION @extschema@.get_max_range_value(
71- parent_relidOID , dummy ANYELEMENT)
72- RETURNS ANYELEMENTAS ' 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- CREATEOR REPLACE FUNCTION @extschema@.check_overlap(
79- parent_relidOID , range_min ANYELEMENT, range_max ANYELEMENT)
80- RETURNSBOOLEAN AS ' pg_pathman' ,' check_overlap' LANGUAGE C STRICT;
8133
8234/*
8335 * Copy rows to partitions
11163$$
11264LANGUAGE plpgsql;
11365
114-
11566/*
11667 * Disable pathman partitioning for specified relation
11768*/
13081$$
13182LANGUAGE plpgsql;
13283
133-
134- /*
135- * Returns attribute type name for relation
136- */
137- CREATEOR REPLACE FUNCTION @extschema@.get_attribute_type_name(
138- p_relation REGCLASS
139- , p_attnameTEXT
140- , OUT p_atttypeTEXT )
141- RETURNSTEXT AS
142- $$
143- BEGIN
144- SELECT typname::TEXT INTO p_atttype
145- FROM pg_typeJOIN pg_attributeon 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- CREATEOR REPLACE FUNCTION @extschema@.is_attribute_nullable(
156- p_relation REGCLASS
157- , p_attnameTEXT
158- , OUT p_nullableBOOLEAN )
159- RETURNSBOOLEAN AS
160- $$
161- BEGIN
162- SELECT NOT attnotnull INTO p_nullable
163- FROM pg_typeJOIN pg_attributeon 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*/
217131$$
218132LANGUAGE plpgsql;
219133
220-
221- CREATEOR REPLACE FUNCTION @extschema@.get_plain_relname(cls regclass)
222- RETURNSTEXT AS
223- $$
224- BEGIN
225- RETURN relnameFROM pg_classWHERE oid = cls::oid ;
226- END
227- $$
228- LANGUAGE plpgsql;
229-
230-
231134/*
232135 * Validates relation name. It must be schema qualified
233136*/
240143$$
241144LANGUAGE plpgsql;
242145
243-
244146/*
245147 * Returns schema-qualified name for table
246148*/
288190$$
289191LANGUAGE plpgsql;
290192
291- /*
292- * Check if regclass if date or timestamp
293- */
294- CREATEOR REPLACE FUNCTION @extschema@.is_date(cls REGTYPE)
295- RETURNSBOOLEAN AS
296- $$
297- BEGIN
298- RETURN clsIN (' 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*/
322212$$
323213LANGUAGE 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- CREATEOR REPLACE FUNCTION @extschema@.acquire_partitions_lock()
333- RETURNS VOIDAS ' pg_pathman' ,' acquire_partitions_lock' LANGUAGE C STRICT;
334-
335- /*
336- * Release partitions lock
337- */
338- CREATEOR REPLACE FUNCTION @extschema@.release_partitions_lock()
339- RETURNS VOIDAS ' pg_pathman' ,' release_partitions_lock' LANGUAGE C STRICT;
340-
341215/*
342216 * Drop trigger
343217*/
@@ -410,18 +284,122 @@ END
410284$$ LANGUAGE plpgsql
411285SET 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+ CREATEOR REPLACE FUNCTION @extschema@.acquire_partitions_lock()
298+ RETURNS VOIDAS ' pg_pathman' ,' acquire_partitions_lock'
299+ LANGUAGE C STRICT;
300+
301+ /*
302+ * Release partitions lock
303+ */
304+ CREATEOR REPLACE FUNCTION @extschema@.release_partitions_lock()
305+ RETURNS VOIDAS ' pg_pathman' ,' release_partitions_lock'
306+ LANGUAGE C STRICT;
307+
308+ /*
309+ * Check if regclass is date or timestamp
310+ */
311+ CREATEOR REPLACE FUNCTION @extschema@.is_date_type(cls REGTYPE)
312+ RETURNSBOOLEAN 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+ CREATEOR REPLACE FUNCTION @extschema@.check_overlap(
320+ parent_relidOID , range_min ANYELEMENT, range_max ANYELEMENT)
321+ RETURNSBOOLEAN AS ' pg_pathman' ,' check_overlap'
322+ LANGUAGE C STRICT;
323+
324+
325+ CREATEOR REPLACE FUNCTION @extschema@.on_create_partitions(relidOID )
326+ RETURNS VOIDAS ' pg_pathman' ,' on_partitions_created'
327+ LANGUAGE C STRICT;
328+
329+ CREATEOR REPLACE FUNCTION @extschema@.on_update_partitions(relidOID )
330+ RETURNS VOIDAS ' pg_pathman' ,' on_partitions_updated'
331+ LANGUAGE C STRICT;
332+
333+ CREATEOR REPLACE FUNCTION @extschema@.on_remove_partitions(relidOID )
334+ RETURNS VOIDAS ' pg_pathman' ,' on_partitions_removed'
335+ LANGUAGE C STRICT;
336+
337+
338+ CREATEOR REPLACE FUNCTION @extschema@.find_or_create_range_partition(relidOID , value ANYELEMENT)
339+ RETURNSOID 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+ CREATEOR REPLACE FUNCTION @extschema@.get_partition_range(
347+ parent_relidOID , partition_relidOID , dummy ANYELEMENT)
348+ RETURNS ANYARRAYAS ' pg_pathman' ,' get_partition_range'
349+ LANGUAGE C STRICT;
350+
351+
352+ /*
353+ * Returns N-th range (in form of array)
354+ */
355+ CREATEOR REPLACE FUNCTION @extschema@.get_range_by_idx(
356+ parent_relidOID , idxINTEGER , dummy ANYELEMENT)
357+ RETURNS ANYARRAYAS ' pg_pathman' ,' get_range_by_idx'
358+ LANGUAGE C STRICT;
359+
360+ /*
361+ * Returns min value of the first range for relation
362+ */
363+ CREATEOR REPLACE FUNCTION @extschema@.get_min_range_value(
364+ parent_relidOID , dummy ANYELEMENT)
365+ RETURNS ANYELEMENTAS ' pg_pathman' ,' get_min_range_value'
366+ LANGUAGE C STRICT;
367+
368+ /*
369+ * Returns max value of the last range for relation
370+ */
371+ CREATEOR REPLACE FUNCTION @extschema@.get_max_range_value(
372+ parent_relidOID , dummy ANYELEMENT)
373+ RETURNS ANYELEMENTAS ' pg_pathman' ,' get_max_range_value'
374+ LANGUAGE C STRICT;
375+
413376/*
414377 * Returns hash function OID for specified type
415378*/
416379CREATEOR REPLACE FUNCTION @extschema@.get_type_hash_func(OID )
417- RETURNSOID AS ' pg_pathman' ,' get_type_hash_func' LANGUAGE C STRICT;
380+ RETURNSOID AS ' pg_pathman' ,' get_type_hash_func'
381+ LANGUAGE C STRICT;
418382
419383/*
420384 * Calculates hash for integer value
421385*/
422386CREATEOR REPLACE FUNCTION @extschema@.get_hash(INTEGER ,INTEGER )
423- RETURNSINTEGER AS ' pg_pathman' ,' get_hash' LANGUAGE C STRICT;
387+ RETURNSINTEGER AS ' pg_pathman' ,' get_hash'
388+ LANGUAGE C STRICT;
424389
390+ /*
391+ * Checks if attribute is nullable
392+ */
393+ CREATEOR REPLACE FUNCTION @extschema@.is_attribute_nullable(REGCLASS,TEXT )
394+ RETURNSBOOLEAN AS ' pg_pathman' ,' is_attribute_nullable'
395+ LANGUAGE C STRICT;
396+
397+ /*
398+ * Returns attribute type name for relation
399+ */
400+ CREATEOR REPLACE FUNCTION @extschema@.get_attribute_type_name(REGCLASS,TEXT )
401+ RETURNSTEXT 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;
433411CREATEOR REPLACE FUNCTION @extschema@.build_check_constraint_name(REGCLASS,TEXT )
434412RETURNSTEXT AS ' pg_pathman' ,' build_check_constraint_name_attname'
435413LANGUAGE C STRICT;
414+
415+ /*
416+ * DEBUG: Place this inside some plpgsql fuction and set breakpoint.
417+ */
418+ CREATEOR REPLACE FUNCTION @extschema@.debug_capture()
419+ RETURNS VOIDAS ' pg_pathman' ,' debug_capture'
420+ LANGUAGE C STRICT;