-
Notifications
You must be signed in to change notification settings - Fork 284
/
create_partition_time.sql
520 lines (463 loc) · 22.9 KB
/
create_partition_time.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
CREATE FUNCTION @[email protected]_partition_time(p_parent_table text, p_partition_times timestamptz[], p_analyze boolean DEFAULT true, p_start_partition text DEFAULT NULL)
RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
ex_context text;
ex_detail text;
ex_hint text;
ex_message text;
v_all text[] := ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'];
v_analyze boolean := FALSE;
v_control text;
v_control_type text;
v_datetime_string text;
v_epoch text;
v_exists smallint;
v_grantees text[];
v_hasoids boolean;
v_inherit_privileges boolean;
v_inherit_fk boolean;
v_job_id bigint;
v_jobmon boolean;
v_jobmon_schema text;
v_new_search_path text;
v_old_search_path text;
v_parent_grant record;
v_parent_schema text;
v_parent_tablename text;
v_part_col text;
v_partition_created boolean := false;
v_partition_name text;
v_partition_suffix text;
v_parent_tablespace text;
v_partition_expression text;
v_partition_interval interval;
v_partition_timestamp_end timestamptz;
v_partition_timestamp_start timestamptz;
v_publications text[];
v_quarter text;
v_revoke text;
v_row record;
v_sql text;
v_step_id bigint;
v_step_overflow_id bigint;
v_sub_control text;
v_sub_parent text;
v_sub_partition_type text;
v_sub_timestamp_max timestamptz;
v_sub_timestamp_min timestamptz;
v_template_table text;
v_trunc_value text;
v_time timestamptz;
v_partition_type text;
v_unlogged char;
v_year text;
BEGIN
/*
* Function to create a child table in a time-based partition set
*/
SELECT partition_type
, control
, partition_interval
, epoch
, inherit_fk
, jobmon
, datetime_string
, template_table
, publications
, inherit_privileges
INTO v_partition_type
, v_control
, v_partition_interval
, v_epoch
, v_inherit_fk
, v_jobmon
, v_datetime_string
, v_template_table
, v_publications
, v_inherit_privileges
FROM @[email protected]_config
WHERE parent_table = p_parent_table;
IF NOT FOUND THEN
RAISE EXCEPTION 'ERROR: no config found for %', p_parent_table;
END IF;
SELECT n.nspname, c.relname, t.spcname
INTO v_parent_schema, v_parent_tablename, v_parent_tablespace
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
LEFT OUTER JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid
WHERE n.nspname = split_part(p_parent_table, '.', 1)::name
AND c.relname = split_part(p_parent_table, '.', 2)::name;
SELECT general_type INTO v_control_type FROM @[email protected]_control_type(v_parent_schema, v_parent_tablename, v_control);
IF v_control_type <> 'time' THEN
IF (v_control_type = 'id' AND v_epoch = 'none') OR v_control_type <> 'id' THEN
RAISE EXCEPTION 'Cannot run on partition set without time based control column or epoch flag set with an id column. Found control: %, epoch: %', v_control_type, v_epoch;
END IF;
END IF;
SELECT current_setting('search_path') INTO v_old_search_path;
IF length(v_old_search_path) > 0 THEN
v_new_search_path := '@extschema@,pg_temp,'||v_old_search_path;
ELSE
v_new_search_path := '@extschema@,pg_temp';
END IF;
IF v_jobmon THEN
SELECT nspname INTO v_jobmon_schema FROM pg_catalog.pg_namespace n, pg_catalog.pg_extension e WHERE e.extname = 'pg_jobmon'::name AND e.extnamespace = n.oid;
IF v_jobmon_schema IS NOT NULL THEN
v_new_search_path := format('%s,%s',v_jobmon_schema, v_new_search_path);
END IF;
END IF;
EXECUTE format('SELECT set_config(%L, %L, %L)', 'search_path', v_new_search_path, 'false');
-- Determine if this table is a child of a subpartition parent. If so, get limits of what child tables can be created based on parent suffix
SELECT sub_min::timestamptz, sub_max::timestamptz INTO v_sub_timestamp_min, v_sub_timestamp_max FROM @[email protected]_subpartition_limits(p_parent_table, 'time');
IF v_jobmon_schema IS NOT NULL THEN
v_job_id := add_job(format('PARTMAN CREATE TABLE: %s', p_parent_table));
END IF;
v_partition_expression := CASE
WHEN v_epoch = 'seconds' THEN format('to_timestamp(%I)', v_control)
WHEN v_epoch = 'milliseconds' THEN format('to_timestamp((%I/1000)::float)', v_control)
WHEN v_epoch = 'nanoseconds' THEN format('to_timestamp((%I/1000000000)::float)', v_control)
ELSE format('%I', v_control)
END;
RAISE DEBUG 'create_partition_time: v_partition_expression: %', v_partition_expression;
FOREACH v_time IN ARRAY p_partition_times LOOP
v_partition_timestamp_start := v_time;
BEGIN
v_partition_timestamp_end := v_time + v_partition_interval;
EXCEPTION WHEN datetime_field_overflow THEN
RAISE WARNING 'Attempted partition time interval is outside PostgreSQL''s supported time range.
Child partition creation after time % skipped', v_time;
v_step_overflow_id := add_step(v_job_id, 'Attempted partition time interval is outside PostgreSQL''s supported time range.');
PERFORM update_step(v_step_overflow_id, 'CRITICAL', 'Child partition creation after time '||v_time||' skipped');
CONTINUE;
END;
-- Do not create the child table if it's outside the bounds of the top parent.
IF v_sub_timestamp_min IS NOT NULL THEN
IF v_time < v_sub_timestamp_min OR v_time >= v_sub_timestamp_max THEN
RAISE DEBUG 'create_partition_time: p_parent_table: %, v_time: %, v_sub_timestamp_min: %, v_sub_timestamp_max: %'
, p_parent_table, v_time, v_sub_timestamp_min, v_sub_timestamp_max;
CONTINUE;
END IF;
END IF;
-- This suffix generation code is in partition_data_time() as well
v_partition_suffix := to_char(v_time, v_datetime_string);
v_partition_name := @[email protected]_name_length(v_parent_tablename, v_partition_suffix, TRUE);
-- Check if child exists.
SELECT count(*) INTO v_exists
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = v_parent_schema::name
AND c.relname = v_partition_name::name;
IF v_exists > 0 THEN
CONTINUE;
END IF;
-- Ensure analyze is run if a new partition is created. Otherwise if one isn't, will be false and analyze will be skipped
v_analyze := TRUE;
IF v_jobmon_schema IS NOT NULL THEN
v_step_id := add_step(v_job_id, format('Creating new partition %s.%s with interval from %s to %s'
, v_parent_schema
, v_partition_name
, v_partition_timestamp_start
, v_partition_timestamp_end-'1sec'::interval));
END IF;
v_sql := 'CREATE';
-- As of PG12, the unlogged/logged status of a native parent table cannot be changed via an ALTER TABLE in order to affect its children.
-- As of v4.2x, the unlogged state will be managed via the template table
SELECT relpersistence INTO v_unlogged
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE c.relname = v_parent_tablename::name
AND n.nspname = v_parent_schema::name;
IF v_unlogged = 'u' and v_partition_type != 'native' THEN
v_sql := v_sql || ' UNLOGGED';
END IF;
-- Close parentheses on LIKE are below due to differing requirements of native subpartitioning
-- Same INCLUDING list is used in create_parent()
v_sql := v_sql || format(' TABLE %I.%I (LIKE %I.%I INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING STORAGE INCLUDING COMMENTS '
, v_parent_schema
, v_partition_name
, v_parent_schema
, v_parent_tablename);
IF current_setting('server_version_num')::int >= 120000 THEN
v_sql := v_sql || ' INCLUDING GENERATED ';
END IF;
SELECT sub_partition_type, sub_control INTO v_sub_partition_type, v_sub_control
FROM @[email protected]_config_sub
WHERE sub_parent = p_parent_table;
IF v_sub_partition_type = 'native' THEN
-- INCLUDING INDEXES isn't necessary for native partitioning. It isn't supported in v10 and
-- for v11+ index inheritance is automatically handled when the partition is attached
v_sql := v_sql || format(') PARTITION BY RANGE (%I) ', v_sub_control);
ELSE
v_sql := v_sql || format(' INCLUDING INDEXES) ');
END IF;
IF current_setting('server_version_num')::int < 120000 THEN
-- column removed from pgclass in pg12
SELECT relhasoids INTO v_hasoids
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE c.relname = v_parent_tablename::name
AND n.nspname = v_parent_schema::name;
IF v_hasoids IS TRUE THEN
v_sql := v_sql || ' WITH (OIDS)';
END IF;
END IF;
RAISE DEBUG 'create_partition_time v_sql: %', v_sql;
EXECUTE v_sql;
IF v_partition_type = 'native' THEN
IF current_setting('server_version_num')::int >= 120000 THEN
-- PG12 fixed tablespace marking on the parent of a native partition set
-- Versions older than 12 handle tablespace setting via inherit_template_properties() call below
IF v_parent_tablespace IS NOT NULL THEN
EXECUTE format('ALTER TABLE %I.%I SET TABLESPACE %I', v_parent_schema, v_partition_name, v_parent_tablespace);
END IF;
END IF;
IF v_template_table IS NOT NULL THEN
PERFORM @[email protected]_template_properties(p_parent_table, v_parent_schema, v_partition_name);
END IF;
IF v_epoch = 'none' THEN
-- Attach with normal, time-based values for native constraint
EXECUTE format('ALTER TABLE %I.%I ATTACH PARTITION %I.%I FOR VALUES FROM (%L) TO (%L)'
, v_parent_schema
, v_parent_tablename
, v_parent_schema
, v_partition_name
, v_partition_timestamp_start
, v_partition_timestamp_end);
ELSE
-- Must attach with integer based values for native constraint and epoch
IF v_epoch = 'seconds' THEN
EXECUTE format('ALTER TABLE %I.%I ATTACH PARTITION %I.%I FOR VALUES FROM (%L) TO (%L)'
, v_parent_schema
, v_parent_tablename
, v_parent_schema
, v_partition_name
, EXTRACT('epoch' FROM v_partition_timestamp_start)::bigint
, EXTRACT('epoch' FROM v_partition_timestamp_end)::bigint);
ELSIF v_epoch = 'milliseconds' THEN
EXECUTE format('ALTER TABLE %I.%I ATTACH PARTITION %I.%I FOR VALUES FROM (%L) TO (%L)'
, v_parent_schema
, v_parent_tablename
, v_parent_schema
, v_partition_name
, EXTRACT('epoch' FROM v_partition_timestamp_start)::bigint * 1000
, EXTRACT('epoch' FROM v_partition_timestamp_end)::bigint * 1000);
ELSIF v_epoch = 'nanoseconds' THEN
EXECUTE format('ALTER TABLE %I.%I ATTACH PARTITION %I.%I FOR VALUES FROM (%L) TO (%L)'
, v_parent_schema
, v_parent_tablename
, v_parent_schema
, v_partition_name
, EXTRACT('epoch' FROM v_partition_timestamp_start)::bigint * 1000000000
, EXTRACT('epoch' FROM v_partition_timestamp_end)::bigint * 1000000000);
END IF;
-- Create secondary, time-based constraint since native's constraint is already integer based
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I CHECK (%s >= %L AND %4$s < %6$L)'
, v_parent_schema
, v_partition_name
, v_partition_name||'_partition_check'
, v_partition_expression
, v_partition_timestamp_start
, v_partition_timestamp_end);
END IF;
ELSE -- non-native
IF v_parent_tablespace IS NOT NULL THEN
EXECUTE format('ALTER TABLE %I.%I SET TABLESPACE %I', v_parent_schema, v_partition_name, v_parent_tablespace);
END IF;
-- Non-native always gets time-based constraint
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I CHECK (%s >= %L AND %4$s < %6$L)'
, v_parent_schema
, v_partition_name
, v_partition_name||'_partition_check'
, v_partition_expression
, v_partition_timestamp_start
, v_partition_timestamp_end);
IF v_epoch = 'seconds' THEN
-- Non-native needs secondary, integer based constraint for epoch
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I CHECK (%I >= %L AND %I < %L)'
, v_parent_schema
, v_partition_name
, v_partition_name||'_partition_int_check'
, v_control
, EXTRACT('epoch' from v_partition_timestamp_start)::bigint
, v_control
, EXTRACT('epoch' from v_partition_timestamp_end)::bigint );
ELSIF v_epoch = 'milliseconds' THEN
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I CHECK (%I >= %L AND %I < %L)'
, v_parent_schema
, v_partition_name
, v_partition_name||'_partition_int_check'
, v_control
, EXTRACT('epoch' from v_partition_timestamp_start)::bigint * 1000
, v_control
, EXTRACT('epoch' from v_partition_timestamp_end)::bigint * 1000);
ELSIF v_epoch = 'nanoseconds' THEN
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I CHECK (%I >= %L AND %I < %L)'
, v_parent_schema
, v_partition_name
, v_partition_name||'_partition_int_check'
, v_control
, EXTRACT('epoch' from v_partition_timestamp_start)::bigint * 1000000000
, v_control
, EXTRACT('epoch' from v_partition_timestamp_end)::bigint * 1000000000);
END IF;
EXECUTE format('ALTER TABLE %I.%I INHERIT %I.%I'
, v_parent_schema
, v_partition_name
, v_parent_schema
, v_parent_tablename);
-- If custom time, set extra config options.
IF v_partition_type = 'time-custom' THEN
INSERT INTO @[email protected]_time_partitions (parent_table, child_table, partition_range)
VALUES ( p_parent_table, v_parent_schema||'.'||v_partition_name, tstzrange(v_partition_timestamp_start, v_partition_timestamp_end, '[)') );
END IF;
-- Indexes cannot be created on the parent, so clustering cannot be used for native yet.
PERFORM @[email protected]_cluster(v_parent_schema, v_parent_tablename, v_parent_schema, v_partition_name);
-- Foreign keys to other tables not supported in native
IF v_inherit_fk THEN
PERFORM @[email protected]_foreign_keys(p_parent_table, v_parent_schema||'.'||v_partition_name, v_job_id);
END IF;
END IF; -- end native check
-- NOTE: Privileges not automatically inherited for native. Only do so if config flag is set
IF v_partition_type != 'native' OR (v_partition_type = 'native' AND v_inherit_privileges = TRUE) THEN
PERFORM @[email protected]_privileges(v_parent_schema, v_parent_tablename, v_parent_schema, v_partition_name, v_job_id);
END IF;
IF v_jobmon_schema IS NOT NULL THEN
PERFORM update_step(v_step_id, 'OK', 'Done');
END IF;
-- Will only loop once and only if sub_partitioning is actually configured
-- This seemed easier than assigning a bunch of variables then doing an IF condition
-- This column list must be kept consistent between:
-- create_parent, check_subpart_sameconfig, create_partition_id, create_partition_time, dump_partitioned_table_definition, and table definition
FOR v_row IN
SELECT sub_parent
, sub_partition_type
, sub_control
, sub_partition_interval
, sub_constraint_cols
, sub_premake
, sub_optimize_trigger
, sub_optimize_constraint
, sub_epoch
, sub_inherit_fk
, sub_retention
, sub_retention_schema
, sub_retention_keep_table
, sub_retention_keep_index
, sub_infinite_time_partitions
, sub_automatic_maintenance
, sub_jobmon
, sub_trigger_exception_handling
, sub_upsert
, sub_trigger_return_null
, sub_template_table
, sub_inherit_privileges
, sub_constraint_valid
, sub_subscription_refresh
, sub_date_trunc_interval
, sub_ignore_default_data
FROM @[email protected]_config_sub
WHERE sub_parent = p_parent_table
LOOP
IF v_jobmon_schema IS NOT NULL THEN
v_step_id := add_step(v_job_id, format('Subpartitioning %s.%s', v_parent_schema, v_partition_name));
END IF;
v_sql := format('SELECT @[email protected]_parent(
p_parent_table := %L
, p_control := %L
, p_type := %L
, p_interval := %L
, p_constraint_cols := %L
, p_premake := %L
, p_automatic_maintenance := %L
, p_inherit_fk := %L
, p_epoch := %L
, p_template_table := %L
, p_jobmon := %L
, p_start_partition := %L
, p_date_trunc_interval := %L )'
, v_parent_schema||'.'||v_partition_name
, v_row.sub_control
, v_row.sub_partition_type
, v_row.sub_partition_interval
, v_row.sub_constraint_cols
, v_row.sub_premake
, v_row.sub_automatic_maintenance
, v_row.sub_inherit_fk
, v_row.sub_epoch
, v_row.sub_template_table
, v_row.sub_jobmon
, p_start_partition
, v_row.sub_date_trunc_interval);
RAISE DEBUG 'create_partition_time (create_parent loop): %', v_sql;
EXECUTE v_sql;
UPDATE @[email protected]_config SET
retention_schema = v_row.sub_retention_schema
, retention_keep_table = v_row.sub_retention_keep_table
, retention_keep_index = v_row.sub_retention_keep_index
, optimize_trigger = v_row.sub_optimize_trigger
, optimize_constraint = v_row.sub_optimize_constraint
, infinite_time_partitions = v_row.sub_infinite_time_partitions
, trigger_exception_handling = v_row.sub_trigger_exception_handling
, upsert = v_row.sub_upsert
, inherit_privileges = v_row.sub_inherit_privileges
, trigger_return_null = v_row.sub_trigger_return_null
, constraint_valid = v_row.sub_constraint_valid
, subscription_refresh = v_row.sub_subscription_refresh
, ignore_default_data = v_row.sub_ignore_default_data
WHERE parent_table = v_parent_schema||'.'||v_partition_name;
END LOOP; -- end sub partitioning LOOP
-- Manage additonal constraints if set
PERFORM @[email protected]_constraints(p_parent_table, p_job_id := v_job_id);
IF v_publications IS NOT NULL THEN
-- NOTE: Native publication inheritance is only supported on PG14+
PERFORM @[email protected]_publications(p_parent_table, v_parent_schema, v_partition_name);
END IF;
v_partition_created := true;
END LOOP;
-- v_analyze is a local check if a new table is made.
-- p_analyze is a parameter to say whether to run the analyze at all. Used by create_parent() to avoid long exclusive lock or run_maintenence() to avoid long creation runs.
IF v_analyze AND p_analyze THEN
IF v_jobmon_schema IS NOT NULL THEN
v_step_id := add_step(v_job_id, format('Analyzing partition set: %s', p_parent_table));
END IF;
EXECUTE format('ANALYZE %I.%I', v_parent_schema, v_parent_tablename);
IF v_jobmon_schema IS NOT NULL THEN
PERFORM update_step(v_step_id, 'OK', 'Done');
END IF;
END IF;
IF v_jobmon_schema IS NOT NULL THEN
IF v_partition_created = false THEN
v_step_id := add_step(v_job_id, format('No partitions created for partition set: %s. Attempted intervals: %s', p_parent_table, p_partition_times));
PERFORM update_step(v_step_id, 'OK', 'Done');
END IF;
IF v_step_overflow_id IS NOT NULL THEN
PERFORM fail_job(v_job_id);
ELSE
PERFORM close_job(v_job_id);
END IF;
END IF;
EXECUTE format('SELECT set_config(%L, %L, %L)', 'search_path', v_old_search_path, 'false');
RETURN v_partition_created;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS ex_message = MESSAGE_TEXT,
ex_context = PG_EXCEPTION_CONTEXT,
ex_detail = PG_EXCEPTION_DETAIL,
ex_hint = PG_EXCEPTION_HINT;
IF v_jobmon_schema IS NOT NULL THEN
IF v_job_id IS NULL THEN
EXECUTE format('SELECT %I.add_job(''PARTMAN CREATE TABLE: %s'')', v_jobmon_schema, p_parent_table) INTO v_job_id;
EXECUTE format('SELECT %I.add_step(%s, ''EXCEPTION before job logging started'')', v_jobmon_schema, v_job_id, p_parent_table) INTO v_step_id;
ELSIF v_step_id IS NULL THEN
EXECUTE format('SELECT %I.add_step(%s, ''EXCEPTION before first step logged'')', v_jobmon_schema, v_job_id) INTO v_step_id;
END IF;
EXECUTE format('SELECT %I.update_step(%s, ''CRITICAL'', %L)', v_jobmon_schema, v_step_id, 'ERROR: '||coalesce(SQLERRM,'unknown'));
EXECUTE format('SELECT %I.fail_job(%s)', v_jobmon_schema, v_job_id);
END IF;
RAISE EXCEPTION '%
CONTEXT: %
DETAIL: %
HINT: %', ex_message, ex_context, ex_detail, ex_hint;
END
$$;