Skip to content

Commit 54de647

Browse files
committed
chore: add/fix remaining unit tests for this release
1 parent 9cd69e0 commit 54de647

File tree

4 files changed

+72
-36
lines changed

4 files changed

+72
-36
lines changed

sql/functions/apply_constraints.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ IF p_child_table IS NULL THEN
131131
ELSE
132132
v_optimize_counter := v_optimize_counter + 1;
133133
IF v_optimize_counter > v_optimize_constraint THEN
134-
v_child_tablename = v_row_max_value.partition_tablename;
134+
v_child_tablename := v_row_max_value.partition_tablename;
135135
EXIT;
136136
END IF;
137137
END IF;

test/test-id-time-subpart-custom-start.sql

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
-- ########## NATIVE ID PARENT / TIME SUBPARENT TESTS ##########
1+
-- ########## ID PARENT / TIME SUBPARENT TESTS ##########
22
-- Additional tests: no pg_jobmon
33
-- Test using a pre-created template table and passing to create_parent. Should allow indexes to be made for initial children.
4-
-- additional constraint column
4+
-- additional constraint column (top lvl int)
55

66
\set ON_ERROR_ROLLBACK 1
77
\set ON_ERROR_STOP true
88

99
BEGIN;
1010
SELECT set_config('search_path','partman, public',false);
1111

12-
SELECT plan(323);
12+
SELECT plan(321);
1313
CREATE SCHEMA partman_test;
1414

1515
CREATE TABLE partman_test.fk_test_reference (col2 text unique not null);
@@ -31,7 +31,7 @@ ALTER TABLE partman_test.template_id_taptest_table ADD PRIMARY KEY (col1);
3131
CREATE INDEX ON partman_test.id_taptest_table (col3);
3232
ALTER TABLE partman_test.id_taptest_table ADD FOREIGN KEY (col2) REFERENCES partman_test.fk_test_reference(col2);
3333

34-
SELECT create_parent('partman_test.id_taptest_table', 'col1', '10', p_constraint_cols =>'{"col3"}', p_jobmon => false, p_template_table => 'partman_test.template_id_taptest_table');
34+
SELECT create_parent('partman_test.id_taptest_table', 'col1', '10', p_constraint_cols =>'{"col2"}', p_jobmon => false, p_template_table => 'partman_test.template_id_taptest_table');
3535
INSERT INTO partman_test.id_taptest_table (col1) VALUES (generate_series(1,9));
3636

3737
SELECT is_partitioned('partman_test', 'id_taptest_table', 'Check that id_taptest_table is natively partitioned');
@@ -295,6 +295,7 @@ SELECT is_empty('SELECT * FROM partman_test.id_taptest_table_p40', 'Check count
295295
INSERT INTO partman_test.id_taptest_table (col1, col3) VALUES (generate_series(10,20), CURRENT_TIMESTAMP+'1 day'::interval);
296296

297297
SELECT run_maintenance();
298+
298299
SELECT is_empty('SELECT * FROM ONLY partman_test.id_taptest_table_default', 'Check that top parent default is empty');
299300
SELECT is_empty('SELECT * FROM ONLY partman_test.id_taptest_table_p0_default', 'Check that subparent default p0 is empty');
300301
SELECT results_eq('SELECT count(*)::int FROM partman_test.id_taptest_table_p0', ARRAY[9], 'Check count from parent table partman_test.id_taptest_table_p0');
@@ -387,7 +388,6 @@ SELECT is_empty('SELECT * FROM partman_test.id_taptest_table_p60', 'Check count
387388

388389
SELECT hasnt_table('partman_test', 'id_taptest_table_p70', 'Check id_taptest_table_p70 doesn''t exists yet');
389390

390-
-- Ensure time partitioning works for all sub partitions
391391
UPDATE part_config SET premake = 5 WHERE parent_table ~ 'partman_test.id_taptest_table_p';
392392
SELECT run_maintenance();
393393

@@ -448,6 +448,14 @@ SELECT hasnt_table('partman_test', 'id_taptest_table_p50_p'||to_char(CURRENT_TIM
448448
SELECT hasnt_table('partman_test', 'id_taptest_table_p60_p'||to_char(CURRENT_TIMESTAMP+'5 days'::interval, 'YYYYMMDD'),
449449
'Check id_taptest_table_p60_p'||to_char(CURRENT_TIMESTAMP+'5 days'::interval, 'YYYYMMDD')||' does not exist');
450450

451+
-- Ensure constraint exclusion for top level. Latest data is in _p5, so optimize_constraint of 4 would be before _p1
452+
INSERT INTO partman_test.id_taptest_table (col1, col3) VALUES (generate_series(30,50), CURRENT_TIMESTAMP+'1 day'::interval);
453+
UPDATE partman.part_config SET optimize_constraint = 4 WHERE parent_table = 'partman_test.id_taptest_table';
454+
455+
SELECT run_maintenance();
456+
SELECT col_has_check('partman_test', 'id_taptest_table_p0', 'col2'
457+
, 'Check for additional constraint on col2 on id_taptest_table_p0');
458+
451459
-- Test dropping without retention set
452460
SELECT drop_partition_time ('partman_test.id_taptest_table_p0', '2 days', p_keep_table := false);
453461
SELECT has_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'2 days'::interval, 'YYYYMMDD'),
@@ -462,7 +470,7 @@ SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIME
462470
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'6 days'::interval, 'YYYYMMDD')||' does not exist');
463471

464472

465-
UPDATE part_config SET retention = '10', retention_keep_table = false WHERE parent_table = 'partman_test.id_taptest_table';
473+
UPDATE part_config SET retention = '30', retention_keep_table = false WHERE parent_table = 'partman_test.id_taptest_table';
466474
UPDATE part_config SET retention = '2 days', retention_keep_table = false WHERE parent_table = 'partman_test.id_taptest_table_p0';
467475
UPDATE part_config SET retention = '2 days', retention_keep_table = false WHERE parent_table = 'partman_test.id_taptest_table_p10';
468476
UPDATE part_config SET retention = '2 days', retention_keep_table = false WHERE parent_table = 'partman_test.id_taptest_table_p20';
@@ -475,31 +483,19 @@ UPDATE part_config SET retention = '2 days', retention_keep_table = false WHERE
475483

476484
SELECT run_maintenance();
477485

486+
SELECT hasnt_table('partman_test', 'id_taptest_table_p0',
487+
'Check id_taptest_table_p0 does not exist');
478488
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD'),
479489
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD')||' does not exist');
480490
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'1 day'::interval, 'YYYYMMDD'),
481491
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'1 day'::interval, 'YYYYMMDD')||' does not exist');
482-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'2 days'::interval, 'YYYYMMDD'),
483-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'2 days'::interval, 'YYYYMMDD')||' does not exist');
484-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'3 days'::interval, 'YYYYMMDD'),
485-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'3 days'::interval, 'YYYYMMDD')||' does not exist');
486-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'4 days'::interval, 'YYYYMMDD'),
487-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'4 days'::interval, 'YYYYMMDD')||' does not exist');
488-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'5 days'::interval, 'YYYYMMDD'),
489-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP+'5 days'::interval, 'YYYYMMDD')||' does not exist');
490-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'1 days'::interval, 'YYYYMMDD'),
491-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'1 days'::interval, 'YYYYMMDD')||' does not exist');
492-
SELECT hasnt_table('partman_test', 'id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'2 days'::interval, 'YYYYMMDD'),
493-
'Check id_taptest_table_p0_p'||to_char(CURRENT_TIMESTAMP-'2 days'::interval, 'YYYYMMDD')||' does not exist');
494492

493+
SELECT hasnt_table('partman_test', 'id_taptest_table_p10',
494+
'Check id_taptest_table_p10 does not exist');
495+
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD'),
496+
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD')||' does not exist');
495497
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'3 days'::interval, 'YYYYMMDD'),
496498
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'3 days'::interval, 'YYYYMMDD')||' does not exist');
497-
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'4 days'::interval, 'YYYYMMDD'),
498-
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'4 days'::interval, 'YYYYMMDD')||' does not exist');
499-
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'5 days'::interval, 'YYYYMMDD'),
500-
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'5 days'::interval, 'YYYYMMDD')||' does not exist');
501-
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'6 days'::interval, 'YYYYMMDD'),
502-
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP-'6 days'::interval, 'YYYYMMDD')||' does not exist');
503499

504500
SELECT hasnt_table('partman_test', 'id_taptest_table_p20_p'||to_char(CURRENT_TIMESTAMP-'3 days'::interval, 'YYYYMMDD'),
505501
'Check id_taptest_table_p20_p'||to_char(CURRENT_TIMESTAMP-'3 days'::interval, 'YYYYMMDD')||' does not exist');
@@ -546,7 +542,6 @@ SELECT hasnt_table('partman_test', 'id_taptest_table_p60_p'||to_char(CURRENT_TIM
546542
SELECT hasnt_table('partman_test', 'id_taptest_table_p60_p'||to_char(CURRENT_TIMESTAMP-'6 days'::interval, 'YYYYMMDD'),
547543
'Check id_taptest_table_p60_p'||to_char(CURRENT_TIMESTAMP-'6 days'::interval, 'YYYYMMDD')||' does not exist');
548544

549-
SELECT undo_partition('partman_test.id_taptest_table_p10', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
550545
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
551546
SELECT undo_partition('partman_test.id_taptest_table_p20', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
552547
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
@@ -558,9 +553,15 @@ SELECT undo_partition('partman_test.id_taptest_table_p50', p_target_table := 'pa
558553
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
559554
SELECT undo_partition('partman_test.id_taptest_table_p60', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
560555
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
556+
SELECT undo_partition('partman_test.id_taptest_table_p70', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
557+
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
558+
SELECT undo_partition('partman_test.id_taptest_table_p80', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
559+
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
560+
SELECT undo_partition('partman_test.id_taptest_table_p90', p_target_table := 'partman_test.undo_taptest', p_loop_count => 20, p_keep_table := false);
561+
SELECT has_table('partman_test', 'template_id_taptest_table', 'Check that template table was not removed yet');
561562

562563

563-
SELECT results_eq('SELECT count(*)::int FROM partman_test.undo_taptest', ARRAY[11], 'Check count from target of undo_partition');
564+
SELECT results_eq('SELECT count(*)::int FROM partman_test.undo_taptest', ARRAY[22], 'Check count from target of undo_partition');
564565

565566
SELECT hasnt_table('partman_test', 'id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD'),
566567
'Check id_taptest_table_p10_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD')||' does not exist');
@@ -698,7 +699,7 @@ SELECT is_empty('SELECT parent_table from part_config where parent_table = ''par
698699

699700
SELECT hasnt_table('partman_test', 'template_id_taptest_table', 'Check that template table was removed');
700701

701-
SELECT results_eq('SELECT count(*)::int FROM ONLY partman_test.undo_taptest', ARRAY[11], 'Check count from final unpartitioned target table');
702+
SELECT results_eq('SELECT count(*)::int FROM ONLY partman_test.undo_taptest', ARRAY[22], 'Check count from final unpartitioned target table');
702703

703704
SELECT * FROM finish();
704705
ROLLBACK;

test/test-time-epoch-id-subpart-native.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- Set analyze to true for maintenance
44
-- Premake template table and ensure pk is made on initial child tables
55
-- TODO Add additional pk checks to all children
6+
-- Allow NOT NULL for control (top level)
67

78
\set ON_ERROR_ROLLBACK 1
89
\set ON_ERROR_STOP true
@@ -16,14 +17,14 @@ CREATE SCHEMA partman_test;
1617
CREATE TABLE partman_test.time_taptest_table (
1718
col1 int NOT NULL
1819
, col2 text
19-
, col3 int NOT NULL DEFAULT extract('epoch' from CURRENT_TIMESTAMP)::int)
20+
, col3 int DEFAULT extract('epoch' from CURRENT_TIMESTAMP)::int)
2021
PARTITION BY RANGE (col3);
2122
CREATE TABLE partman_test.undo_taptest (LIKE partman_test.time_taptest_table INCLUDING ALL);
2223

2324
CREATE TABLE partman_test.template_time_taptest_table (LIKE partman_test.time_taptest_table);
2425
ALTER TABLE partman_test.template_time_taptest_table ADD PRIMARY KEY (col1);
2526

26-
SELECT create_parent('partman_test.time_taptest_table', 'col3', '1 day', p_epoch := 'seconds', p_template_table := 'partman_test.template_time_taptest_table' );
27+
SELECT create_parent('partman_test.time_taptest_table', 'col3', '1 day', p_epoch := 'seconds', p_template_table := 'partman_test.template_time_taptest_table', p_control_not_null := false );
2728
INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), extract('epoch' from CURRENT_TIMESTAMP)::int);
2829

2930
SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD'), 'Check time_taptest_table_p'||to_char(CURRENT_TIMESTAMP, 'YYYYMMDD')||' exists');

updates/pg_partman--5.1.0--5.2.0.sql

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
-- TODO - add test for PR#650 - not creating partitions that aren't being retained anyway
2-
-- - add test to ensure sub partitioning preserves default table settings and not null enforcement
3-
-- - add test for constraint exclusion with integer-based partitioning
1+
-- SEE CHANGELOG.md for all notes and details on this update
2+
3+
CREATE TEMP TABLE partman_preserve_privs_temp (statement text);
4+
5+
INSERT INTO partman_preserve_privs_temp
6+
SELECT 'GRANT EXECUTE ON FUNCTION @extschema@.create_parent(text, text, text, text, text, int, text, boolean, text, text[], text, boolean, text, boolean) TO '||array_to_string(array_agg('"'||grantee::text||'"'), ',')||';'
7+
FROM information_schema.routine_privileges
8+
WHERE routine_schema = '@extschema@'
9+
AND routine_name = 'create_parent'
10+
AND grantee != 'PUBLIC';
11+
12+
INSERT INTO partman_preserve_privs_temp
13+
SELECT 'GRANT EXECUTE ON FUNCTION @extschema@.create_sub_parent(text, text, text, text, boolean, text, text[], int, text, text, boolean, text, boolean) TO '||array_to_string(array_agg('"'||grantee::text||'"'), ',')||';'
14+
FROM information_schema.routine_privileges
15+
WHERE routine_schema = '@extschema@'
16+
AND routine_name = 'create_sub_parent'
17+
AND grantee != 'PUBLIC';
18+
19+
INSERT INTO partman_preserve_privs_temp
20+
SELECT 'GRANT EXECUTE ON FUNCTION @extschema@.check_subpart_sameconfig(text) TO '||array_to_string(array_agg('"'||grantee::text||'"'), ',')||';'
21+
FROM information_schema.routine_privileges
22+
WHERE routine_schema = '@extschema@'
23+
AND routine_name = 'check_subpart_sameconfig'
24+
AND grantee != 'PUBLIC';
425

5-
-- TODO PRESERVE PRIVILEGES
626
DROP FUNCTION @extschema@.create_parent(text, text, text, text, text, int, text, boolean, text, text[], text, boolean, text);
727
DROP FUNCTION @extschema@.create_sub_parent(text, text, text, text, boolean, text, text[], int, text, text, boolean, text);
828
DROP FUNCTION @extschema@.check_subpart_sameconfig(text);
929

10-
1130
ALTER TABLE @extschema@.part_config DROP COLUMN default_table;
1231
ALTER TABLE @extschema@.part_config_sub ADD COLUMN sub_control_not_null boolean DEFAULT true;
1332

@@ -3842,7 +3861,7 @@ IF p_child_table IS NULL THEN
38423861
ELSE
38433862
v_optimize_counter := v_optimize_counter + 1;
38443863
IF v_optimize_counter > v_optimize_constraint THEN
3845-
v_child_tablename = v_row_max_value.partition_tablename;
3864+
v_child_tablename := v_row_max_value.partition_tablename;
38463865
EXIT;
38473866
END IF;
38483867
END IF;
@@ -4110,3 +4129,18 @@ EXECUTE format('ANALYZE %I.%I', v_parent_schema, v_parent_tablename);
41104129
PERFORM pg_advisory_unlock(hashtext('pg_partman reapply_constraints'));
41114130
END
41124131
$$;
4132+
4133+
-- Restore dropped object privileges
4134+
DO $$
4135+
DECLARE
4136+
v_row record;
4137+
BEGIN
4138+
FOR v_row IN SELECT statement FROM partman_preserve_privs_temp LOOP
4139+
IF v_row.statement IS NOT NULL THEN
4140+
EXECUTE v_row.statement;
4141+
END IF;
4142+
END LOOP;
4143+
END
4144+
$$;
4145+
4146+
DROP TABLE IF EXISTS partman_preserve_privs_temp;

0 commit comments

Comments
 (0)