1
1
/* ------------------------------------------------------------------------
2
2
*
3
3
* init.sql
4
- * Creates config table and provides common utility functions
4
+ * Creates config table and provides common utility functions
5
5
*
6
6
* Copyright (c) 2015-2016, Postgres Professional
7
7
*
@@ -27,57 +27,9 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
27
27
CHECK (parttype>= 1 OR parttype<= 2 )/* check for allowed part types*/
28
28
);
29
29
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;
66
30
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' ,' ' );
73
32
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;
81
33
82
34
/*
83
35
* Copy rows to partitions
111
63
$$
112
64
LANGUAGE plpgsql;
113
65
114
-
115
66
/*
116
67
* Disable pathman partitioning for specified relation
117
68
*/
130
81
$$
131
82
LANGUAGE plpgsql;
132
83
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
-
170
84
/*
171
85
* Aggregates several common relation checks before partitioning. Suitable for every partitioning type.
172
86
*/
217
131
$$
218
132
LANGUAGE plpgsql;
219
133
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
-
231
134
/*
232
135
* Validates relation name. It must be schema qualified
233
136
*/
240
143
$$
241
144
LANGUAGE plpgsql;
242
145
243
-
244
146
/*
245
147
* Returns schema-qualified name for table
246
148
*/
288
190
$$
289
191
LANGUAGE plpgsql;
290
192
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
-
303
193
/*
304
194
* DDL trigger that deletes entry from pathman_config table
305
195
*/
322
212
$$
323
213
LANGUAGE plpgsql;
324
214
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
-
341
215
/*
342
216
* Drop trigger
343
217
*/
@@ -410,18 +284,122 @@ END
410
284
$$ LANGUAGE plpgsql
411
285
SET pg_pathman .enable_partitionfilter = off;
412
286
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
+
413
376
/*
414
377
* Returns hash function OID for specified type
415
378
*/
416
379
CREATEOR 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;
418
382
419
383
/*
420
384
* Calculates hash for integer value
421
385
*/
422
386
CREATEOR 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;
424
389
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;
425
403
426
404
/*
427
405
* Build check constraint name for a specified relation's column
@@ -433,3 +411,10 @@ LANGUAGE C STRICT;
433
411
CREATEOR REPLACE FUNCTION @extschema@.build_check_constraint_name(REGCLASS,TEXT )
434
412
RETURNSTEXT AS ' pg_pathman' ,' build_check_constraint_name_attname'
435
413
LANGUAGE 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;