pg.ddx.io  pgpartman/pg_partman GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgpartman/pg_partman] PR #883: Version 5.5.1
13+ messages / 2 participants
[nested] [flat]

* [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-03 17:04  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  0 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-03 17:04 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>



inline patch 1: .github/workflows/ci.yml, CHANGELOG.md, CLAUDE.md and 38 more (+3143/-71 lines) download inline-1.patchdiff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c152f9..ec37b09 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: container: pgxn/pgxn-tools steps: - run: CREATE_OPTIONS="--pgoption max_locks_per_transaction=128" pg-start ${{ matrix.pg }} postgresql-${{ matrix.pg }}-pgtap - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - run: make install - run: psql -U postgres -c 'CREATE SCHEMA partman; CREATE EXTENSION pg_partman SCHEMA partman; CREATE EXTENSION pgtap' - run: pg_prove --username postgres --ext .sql --comments --verbose --failures test diff --git a/CHANGELOG.md b/CHANGELOG.md index c5f3e73..a8ea51e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ +5.5.1 +===== +BUG FIXES +--------- + - Fix variable assignment misalignment in partition_data_time() when using the p_lock_wait parameter. (Github PR #871 & #878) + - Fix inherit_template_properties() adding a template table's INCLUDE index columns as real key columns on child tables instead of ignoring them. (Github Issue #674) + - Mark uuid7_time_encoder() and uuid7_time_decoder() IMMUTABLE and PARALLEL SAFE. As VOLATILE functions they blocked partition pruning, so queries on a UUIDv7 partition set filtered with uuid7_time_encoder() scanned every child table. (Github Issue #873 / PR #880) + - Fix partition_data_time() and partition_data_proc() leaving rows on the first day of a child table's range in the default, and taking rows on the first day of the next range, when the control column is a `date` data type. The rows are compared in the control column's own type again instead of as text. (Github Issue #856) + - Fix portal lifecycle management in background worker. Thank you to Michael Paquier and the PG Team for helping to keep this extension up to date with upstream changes! + +DOCUMENTATION +------------- + - Clarify that the role running `create_partition()` must have ownership level permissions on the target table (Github Issue #858). + + 5.5.0 ===== BREAKING CHANGES diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..818c621 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,129 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Git + +Never commit or push automatically in this repo, even if global instructions say to. Only commit or push when explicitly asked. This overrides the global "push immediately after every commit" rule. + +## What this is + +pg_partman is a PostgreSQL extension (PL/pgSQL functions/procedures + a C background worker) that manages +time-based and ID-based declarative partition sets: creating child partitions ahead of time, applying +retention policies, migrating existing tables into partitioning, and running all of this automatically via +a background worker (BGW) or an external scheduler. Minimum supported PostgreSQL version is 14 (see +`Makefile`'s `PG_VER_min` and `META.json`). + +## Build + +```sh +make install # builds sql/pg_partman--<version>.sql and installs, including the BGW +make NO_BGW=1 install # skip compiling/installing the background worker, PL/pgSQL only +``` + +`PG_CONFIG` must point at the target `pg_config` (defaults to whatever is on `PATH`). The extension version +comes from `pg_partman.control`'s `default_version`. + +Do not hand-edit `sql/pg_partman--<version>.sql` — it's a generated concatenation (see Architecture below) +and is also in `EXTRA_CLEAN`, so `make clean` removes it. + +## Lint + +```sh +make lint # runs pre-commit (see .pre-commit-config.yaml): merge-conflict/yaml checks, EOF fixer, + # line-ending fixer, trailing-whitespace, codespell +``` + +## Tests + +Tests are pgTAP (`.sql` files), not a PL/pgSQL-only harness — pgTAP >= 1.2.0 must be installed. Full details +in `test/README_test.md`; the essentials: + +- **`max_locks_per_transaction` must be raised above the default 64** (128 has worked) before running the + suite — subpartitioning tests create/drop hundreds of tables in one transaction and can crash the cluster + otherwise. +- Tests assume `pg_partman` is installed to schema `partman` and pgTAP to `public`; each test file has a + config block at the top if a different setup is needed. +- Run the whole top-level suite with `pg_prove` (rolled back in a transaction, so side-effect free except + jobmon logging if pg_jobmon is installed): + ```sh + pg_prove -ovf /path/to/pg_partman/test/*.sql + ``` +- Run a single test the same way, just pass one file: `pg_prove -ovf test/test-time-daily.sql`. +- Subfolders are **not** part of the "run everything" wildcard and need manual handling: + - `test/test_bgw/` — background worker tests; read the header comment in each file for the + `postgresql.conf` settings it requires first. + - `test/test_procedure/` — tests for the `*_proc` procedures, split into numbered `-partN` files that must + be run in order with manual commits between them (pgTAP can't span distinct transactions in one run). + - `test/test_manual/`, `test/test_nonsuperuser/`, `test/test_no_search_path/`, `test/test_tablespace/` — + scenario-specific setups, read each file before running. + - `test/not_working_yet/` — known-broken/in-progress tests, not expected to pass. +- CI (`.github/workflows/ci.yml`) runs this matrix against PostgreSQL 14–18 using `pgxn/pgxn-tools`, + installing into the `partman` schema and running `pg_prove` over the top-level `test` folder only. + +## Architecture + +**SQL build pipeline.** The installable extension script is assembled, not hand-written: +`sql/pg_partman--<version>.sql` = concatenation of `sql/types/*.sql`, `sql/tables/*.sql`, +`sql/functions/*.sql`, `sql/procedures/*.sql`, in that order (see the `Makefile` rule). Table/type +definitions and the constraint-check functions that reference them live in `sql/tables/tables.sql` together +(functions have to be created before the `ALTER TABLE ... ADD CONSTRAINT` that uses them, but after the +table itself — ordering across separate files would be harder to control, hence they're combined there +instead of under `sql/functions/`). Version upgrades between releases are hand-written diff scripts in +`updates/pg_partman--X.Y.Z--A.B.C.sql`, applied via `ALTER EXTENSION pg_partman UPDATE TO`. + +**Configuration tables.** `part_config` (one row per managed parent/partitioned table) and `part_config_sub` +(one row per sub-partition template, FK'd to `part_config.parent_table`) hold all per-partition-set settings: +control column, partition interval/type, retention policy, template table, privileges-inheritance flags, +and `maintenance_role` (used for Row Level Security multi-tenancy, see README). Almost every function takes +`p_parent_table` and looks up its config row as the first step. + +**Core operations**, each with a `_time` and `_id` variant (time-based vs. serial/ID-based partitioning), +plus shared helpers: +- `create_parent` / `create_sub_parent` — set up a new partition set (or sub-partitioned set) from an + existing/new table, insert its `part_config` row. +- `create_partition_time` / `create_partition_id` (called by `create_partition`) — create individual child + tables. +- `run_maintenance` (and `run_maintenance_proc`) — the periodic driver: premakes upcoming children, applies + retention. This is what the BGW calls on its interval, and what an external scheduler would call instead + of running the BGW. As of 5.5.0, a failure in one partition set's maintenance no longer aborts maintenance + for other partition sets — it's logged as a warning and that set's `part_config.last_run` is cleared. +- `partition_data_time` / `partition_data_id` (and their `_proc`/`partition_data_async` procedure forms) — + move existing rows from a non-partitioned (or default) table into the correct child partitions in + batches. +- `drop_partition_time` / `drop_partition_id` — retention enforcement; can detach-then-drop + (`detach_before_drop` in `part_config`) when a child is still referenced by a foreign key elsewhere. +- `undo_partition` / `undo_partition_proc` — reverse of the above: migrate a partition set back to a single + plain table. +- `apply_privileges` / `reapply_privileges`, `apply_constraints` / `drop_constraints`, + `inherit_parent_properties` / `inherit_template_properties` / `inherit_replica_identity` — keep child + tables in sync with the parent/template table's grants, constraints, and other properties as new children + are created. +- `show_partitions` / `show_partition_info` / `show_partition_name` — introspection helpers. + +**Background worker (`src/pg_partman_bgw.c`).** A C bgworker loaded via `shared_preload_libraries`. Reads +GUCs (`pg_partman_bgw.interval`, `.role`, `.dbname`, `.analyze`, `.jobmon`, `.maintenance_wait`) and, per +configured database, connects as `pg_partman_bgw.role` and calls `run_maintenance()` on its interval. +`pg_partman_bgw.role` intentionally has no safe default that resolves to a superuser — since 5.5.0 it +defaults to a nonexistent placeholder (`partman_maintainer`) rather than silently running as whatever role +started the cluster, so operators are forced to configure a properly-scoped, non-superuser role (see +README's SETUP/privileges sections and the CVEs noted in `CHANGELOG.md` around running as superuser). + +**Security model.** pg_partman does not require a superuser to install or run. Multiple CVEs fixed in 5.5.0 +(see top of `CHANGELOG.md`) were about SQL injection / privilege escalation through user-controlled inputs +(time encoder/decoder functions, template table inheritance, pg_jobmon integration) and about +retention-schema table-ownership takeover in multi-tenant setups — when touching any code path that builds +dynamic SQL from `part_config`/`part_config_sub` values or from user-supplied identifiers, treat those +inputs as untrusted and check `partman_safe_obj_name.sql` / existing quoting patterns in similar functions +rather than introducing new string concatenation. Row Level Security policies (via `maintenance_role`) are +an intentional mitigation for multi-tenant use, documented in `README.md`'s "Row Level Security" section. + +## Repo layout notes + +- `doc/pg_partman.md` — the full reference manual; `doc/pg_partman_howto.md` — usage walkthrough; + `doc/migrate_to_partman.md` / `doc/migrate_to_declarative.md` — migration guides. Update these alongside + behavior changes to functions/procedures they document. +- `bin/common/*.py` — standalone maintenance scripts (dump/restore partition definitions, vacuum + maintenance, unique-constraint checks), not part of the extension proper. +- `CHANGELOG.md` (current) and `CHANGELOG-pre-5.0.0.txt` (archived) — check the current file for the + in-progress/unreleased entry style before adding a new one. diff --git a/META.json b/META.json index 3553c25..b92d657 100644 --- a/META.json +++ b/META.json @@ -1,7 +1,7 @@ { "name": "pg_partman", "abstract": "Extension to manage partitioned tables by time or ID", - "version": "5.5.0", + "version": "5.5.1", "maintainer": [ "Keith Fiske <keith@keithf4.com>" ], @@ -20,9 +20,9 @@ }, "provides": { "pg_partman": { - "file": "sql/pg_partman--5.5.0.sql", + "file": "sql/pg_partman--5.5.1.sql", "docfile": "doc/pg_partman.md", - "version": "5.5.0", + "version": "5.5.1", "abstract": "Extension to manage partitioned tables by time or ID" } }, diff --git a/README.md b/README.md index a511638..00003ea 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA partman TO partman_maintainer; GRANT ALL ON SCHEMA my_partition_schema TO partman_maintainer; GRANT TEMPORARY ON DATABASE mydb to partman_maintainer; -- allow creation of temp tables to move data out of default ``` +Note that in order to create a partition set, the role running `create_partition()` will need ownership level permissions on that table, not just GRANTs. If you need the role to also be able to create schemas, you will need to grant create on the database as well. In general this shouldn't be required as long as you give the above role CREATE privileges on any pre-existing schemas that will contain partition sets. @@ -124,28 +125,28 @@ ALTER TABLE part_config_sub ENABLE ROW LEVEL SECURITY; Now create the policies that will allow the roles set in the `maintenance_role` column to be able to fully access their own rows for reads and writes. Once this is in place, those roles should no longer be able to even see any other rows in the table. ```sql -CREATE POLICY part_config_user_access_policy ON part_config - FOR ALL - TO PUBLIC +CREATE POLICY part_config_user_access_policy ON part_config + FOR ALL + TO PUBLIC USING (maintenance_role = current_user) - WITH CHECK (maintenance_role = current_user); + WITH CHECK (maintenance_role = current_user); CREATE POLICY part_config_sub_user_access_policy ON part_config_sub - FOR ALL - TO PUBLIC + FOR ALL + TO PUBLIC USING (sub_maintenance_role = current_user) - WITH CHECK (sub_maintenance_role = current_user); + WITH CHECK (sub_maintenance_role = current_user); ``` -If you are using the background worker and/or have a dedicated maintence role for pg_partman, you will have to create another policy to allow them to see all rows in the partman configuration tables. +If you are using the background worker and/or have a dedicated maintenance role for pg_partman, you will have to create another policy to allow them to see all rows in the partman configuration tables. ```sql -CREATE POLICY part_config_maintenance_access_policy ON part_config - FOR ALL +CREATE POLICY part_config_maintenance_access_policy ON part_config + FOR ALL TO partman_maintainer USING (true); CREATE POLICY part_config_sub_maintenance_access_policy ON part_config_sub - FOR ALL + FOR ALL TO partman_maintainer USING (true); ``` diff --git a/SECURITY.md b/SECURITY.md index d1b7905..5c8305b 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -10,5 +10,5 @@ ## Reporting a Vulnerability -Please use the private vulnerablity reporting system here on Github to report any security issues you may find with the pg_partman project. I will try to respond within 48 hrs to +Please use the private vulnerability reporting system here on Github to report any security issues you may find with the pg_partman project. I will try to respond within 48 hrs to discuss how to move forward. Thank you for contributing to the project! diff --git a/doc/pg_partman.md b/doc/pg_partman.md index 03405e3..216f4d6 100644 --- a/doc/pg_partman.md +++ b/doc/pg_partman.md @@ -864,12 +864,12 @@ Stores all configuration data for partition sets managed by the extension. - Must be a value that can either be cast to the interval (for time-based partitioning) or bigint (for number partitioning) data types. - Leave this column NULL (the default) to always keep all child partitions. See **About** section for more info. - `retention_schema` - - Schema to move tables to as part of the retentions system instead of dropping them. Overrides retention_keep_table option. + - Schema to move tables to as part of the retention system instead of dropping them. Overrides `retention_keep_table` option. - `retention_keep_index` - Boolean value to determine whether indexes are dropped for child tables that are detached. - Default is TRUE. Set to FALSE to have the child table's indexes dropped when it is detached. - `retention_keep_table` - - Boolean value to determine whether dropped child tables only detached or actually dropped. + - Boolean value to determine whether dropped child tables only detached or actually dropped. Ignored if `retention_schema` is not NULL. - Default is TRUE to keep the table and only uninherit it. Set to FALSE to have the child tables removed from the database completely. - `retention_keep_publication` - If `retention_keep_table` is set to true, determines whether to drop the table from any publications that it may be a member of. diff --git a/pg_partman.control b/pg_partman.control index d29871c..b71989c 100644 --- a/pg_partman.control +++ b/pg_partman.control @@ -1,4 +1,4 @@ -default_version = '5.5.0' +default_version = '5.5.1' comment = 'Extension to manage partitioned tables by time or ID' relocatable = false superuser = false diff --git a/sql/functions/apply_constraints.sql b/sql/functions/apply_constraints.sql index 52f01ed..31a0fd5 100644 --- a/sql/functions/apply_constraints.sql +++ b/sql/functions/apply_constraints.sql @@ -264,4 +264,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/apply_privileges.sql b/sql/functions/apply_privileges.sql index 0097750..315507f 100644 --- a/sql/functions/apply_privileges.sql +++ b/sql/functions/apply_privileges.sql @@ -193,4 +193,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/check_subpart_sameconfig.sql b/sql/functions/check_subpart_sameconfig.sql index 98491b0..00d2c90 100644 --- a/sql/functions/check_subpart_sameconfig.sql +++ b/sql/functions/check_subpart_sameconfig.sql @@ -83,4 +83,3 @@ AS $$ FROM @extschema@.part_config_sub a JOIN child_tables b on a.sub_parent = b.tablename; $$; - diff --git a/sql/functions/create_partition.sql b/sql/functions/create_partition.sql index 4b1117d..dff97a9 100644 --- a/sql/functions/create_partition.sql +++ b/sql/functions/create_partition.sql @@ -262,8 +262,8 @@ FOR v_row IN , a.sub_template_table , a.sub_retention , a.sub_retention_schema - , a.sub_retention_keep_index , a.sub_retention_keep_table + , a.sub_retention_keep_index , a.sub_epoch , a.sub_constraint_cols , a.sub_optimize_constraint @@ -313,8 +313,8 @@ LOOP , v_row.sub_premake , v_row.sub_retention , v_row.sub_retention_schema - , v_row.sub_retention_keep_index , v_row.sub_retention_keep_table + , v_row.sub_retention_keep_index , v_row.sub_automatic_maintenance , v_row.sub_epoch , v_row.sub_optimize_constraint @@ -671,4 +671,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/create_partition_id.sql b/sql/functions/create_partition_id.sql index 2b51e0a..6c759bc 100644 --- a/sql/functions/create_partition_id.sql +++ b/sql/functions/create_partition_id.sql @@ -319,4 +319,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/create_partition_time.sql b/sql/functions/create_partition_time.sql index 788dae6..4ac2239 100644 --- a/sql/functions/create_partition_time.sql +++ b/sql/functions/create_partition_time.sql @@ -423,4 +423,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/drop_constraints.sql b/sql/functions/drop_constraints.sql index 8fd7bfe..a7ba475 100644 --- a/sql/functions/drop_constraints.sql +++ b/sql/functions/drop_constraints.sql @@ -128,4 +128,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/drop_partition_id.sql b/sql/functions/drop_partition_id.sql index cf3899d..244fb18 100644 --- a/sql/functions/drop_partition_id.sql +++ b/sql/functions/drop_partition_id.sql @@ -341,4 +341,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/drop_partition_time.sql b/sql/functions/drop_partition_time.sql index f9ee2d8..2de9c91 100644 --- a/sql/functions/drop_partition_time.sql +++ b/sql/functions/drop_partition_time.sql @@ -345,4 +345,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/dump_partitioned_table_definition.sql b/sql/functions/dump_partitioned_table_definition.sql index 306be49..3b29c68 100644 --- a/sql/functions/dump_partitioned_table_definition.sql +++ b/sql/functions/dump_partitioned_table_definition.sql @@ -211,4 +211,3 @@ WHERE parent_table = %L;', ); END $$; - diff --git a/sql/functions/inherit_parent_properties.sql b/sql/functions/inherit_parent_properties.sql index fe2c882..d6573e4 100644 --- a/sql/functions/inherit_parent_properties.sql +++ b/sql/functions/inherit_parent_properties.sql @@ -37,5 +37,3 @@ END IF; END $$; - - diff --git a/sql/functions/inherit_template_properties.sql b/sql/functions/inherit_template_properties.sql index 65bdf79..b4ef8cc 100644 --- a/sql/functions/inherit_template_properties.sql +++ b/sql/functions/inherit_template_properties.sql @@ -96,6 +96,7 @@ FOR v_index_list IN JOIN ( SELECT k, row_number() over () as r FROM unnest(i.indkey) k ) as x ON a.attnum = x.k AND a.attrelid = i.indrelid + WHERE x.r <= i.indnkeyatts ) AS indkey_names , c.relname AS index_name , ts.spcname AS tablespace_name @@ -118,6 +119,7 @@ LOOP JOIN ( SELECT k, row_number() over () as r FROM unnest(i.indkey) k ) as x ON a.attnum = x.k AND a.attrelid = i.indrelid + WHERE x.r <= i.indnkeyatts ) AS indkey_names FROM pg_catalog.pg_index i WHERE i.indrelid = v_parent_oid @@ -243,4 +245,3 @@ RETURN true; END $$; - diff --git a/sql/functions/partition_data_time.sql b/sql/functions/partition_data_time.sql index 0f72f5c..4bbbdb4 100644 --- a/sql/functions/partition_data_time.sql +++ b/sql/functions/partition_data_time.sql @@ -21,7 +21,6 @@ v_async_rowcount int; v_column_list_filtered text; v_column_list_full text; v_control text; -v_control_exact_type text; v_control_type text; v_datetime_string text; v_current_partition_name text; @@ -91,7 +90,7 @@ FROM pg_catalog.pg_tables WHERE schemaname = split_part(p_parent_table, '.', 1)::name AND tablename = split_part(p_parent_table, '.', 2)::name; -SELECT general_type, exact_type INTO v_control_type, v_control_exact_type FROM @extschema@.check_control_type(v_parent_schemaname, v_parent_tablename, v_control); +SELECT general_type INTO v_control_type FROM @extschema@.check_control_type(v_parent_schemaname, v_parent_tablename, v_control); IF v_control_type <> 'time' THEN IF (v_control_type = 'id' AND v_epoch = 'none') OR v_control_type NOT IN ('text', 'id', 'uuid') OR (v_control_type IN ('text', 'uuid') AND v_time_encoder IS NULL) THEN RAISE EXCEPTION 'Cannot run on partition set without time based control column, an epoch flag set with an id column or time_encoder set with text column. Found control: %, epoch: %, time_encoder: %s', v_control_type, v_epoch, v_time_encoder; @@ -283,7 +282,7 @@ FOR i IN 1..p_batch_count LOOP WHILE v_lock_iter <= 5 LOOP v_lock_iter := v_lock_iter + 1; BEGIN - v_sql := format('SELECT * FROM ONLY %I.%I WHERE %s >= %L AND %4$s < %5$L FOR UPDATE NOWAIT' + v_sql := format('SELECT * FROM ONLY %I.%I WHERE %s >= %L AND %3$s < %5$L FOR UPDATE NOWAIT' , v_source_schemaname , v_source_tablename , v_partition_expression @@ -316,13 +315,10 @@ FOR i IN 1..p_batch_count LOOP -- Create a variable to use in all scenarios below for handling encoded columns IF v_time_decoder IS NULL THEN - IF v_control_exact_type = 'timestamp' THEN - -- Conversion to text of timestamp without timezone cannot be directly compared to timestamptz - -- that is returned by other functions and used below (See Github Issue #838) - v_decoded_col := format('%s::timestamptz::text', v_partition_expression); - ELSE - v_decoded_col := format('%s::text', v_partition_expression); - END IF; + -- Compare in the control column's own type, which is how the child table's bounds are read. + -- Compared as text, a date sorts before the timestamptz bound it is equal to ('2026-10-01' < '2026-10-01 00:00:00+00'), + -- so the first day of a child table's range was left behind and the first day of the next one taken (Github Issues #838, #856) + v_decoded_col := v_partition_expression; ELSE v_decoded_col := format('%s(%s::text)', v_time_decoder_safe, v_partition_expression); END IF; @@ -404,4 +400,3 @@ RETURN v_total_rows; END $$; - diff --git a/sql/functions/partman_safe_obj_name.sql b/sql/functions/partman_safe_obj_name.sql index 02e4f36..ca7022e 100644 --- a/sql/functions/partman_safe_obj_name.sql +++ b/sql/functions/partman_safe_obj_name.sql @@ -24,4 +24,3 @@ BEGIN END $$; - diff --git a/sql/functions/reapply_privileges.sql b/sql/functions/reapply_privileges.sql index 5e3f2a3..4289be5 100644 --- a/sql/functions/reapply_privileges.sql +++ b/sql/functions/reapply_privileges.sql @@ -84,4 +84,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/run_maintenance.sql b/sql/functions/run_maintenance.sql index 3268f34..4cb37ac 100644 --- a/sql/functions/run_maintenance.sql +++ b/sql/functions/run_maintenance.sql @@ -527,4 +527,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/show_partition_info.sql b/sql/functions/show_partition_info.sql index a6d68b1..369e697 100644 --- a/sql/functions/show_partition_info.sql +++ b/sql/functions/show_partition_info.sql @@ -199,4 +199,3 @@ RETURN; END $$; - diff --git a/sql/functions/show_partitions.sql b/sql/functions/show_partitions.sql index d767460..2362766 100644 --- a/sql/functions/show_partitions.sql +++ b/sql/functions/show_partitions.sql @@ -151,4 +151,3 @@ RETURN QUERY EXECUTE v_sql; END $$; - diff --git a/sql/functions/stop_sub_partition.sql b/sql/functions/stop_sub_partition.sql index 94b2368..54ef678 100644 --- a/sql/functions/stop_sub_partition.sql +++ b/sql/functions/stop_sub_partition.sql @@ -33,4 +33,3 @@ RETURN true; END $$; - diff --git a/sql/functions/undo_partition.sql b/sql/functions/undo_partition.sql index 61af27c..1dcc205 100644 --- a/sql/functions/undo_partition.sql +++ b/sql/functions/undo_partition.sql @@ -497,4 +497,3 @@ DETAIL: % HINT: %', ex_message, ex_context, ex_detail, ex_hint; END $$; - diff --git a/sql/functions/util_uuid7_time.sql b/sql/functions/util_uuid7_time.sql index 68e5c6a..9c34909 100644 --- a/sql/functions/util_uuid7_time.sql +++ b/sql/functions/util_uuid7_time.sql @@ -2,10 +2,14 @@ * Convenience helper functions to support time based partitioning for UUIDv7 columns. * Set the following functions as p_time_encoder, p_time_decoder params for time based * partitioning of UUIDv7 columns + * + * Both are IMMUTABLE so calls with constant arguments get folded at plan time, which + * partition pruning needs. Neither depends on session settings (timestamptz is an + * absolute instant). */ CREATE FUNCTION @extschema@.uuid7_time_encoder(ts TIMESTAMPTZ) RETURNS UUID - LANGUAGE plpgsql + LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE SET search_path = @extschema@, pg_catalog, pg_temp AS $$ DECLARE @@ -24,7 +28,7 @@ $$; -- Currently time decoder function must take a text parameter. See if this can be more flexible in the future CREATE FUNCTION @extschema@.uuid7_time_decoder(uuidv7 TEXT) RETURNS TIMESTAMPTZ - LANGUAGE plpgsql + LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE SET search_path = @extschema@, pg_catalog, pg_temp AS $$ DECLARE diff --git a/sql/procedures/partition_data_proc.sql b/sql/procedures/partition_data_proc.sql index b7589b9..c681dfe 100644 --- a/sql/procedures/partition_data_proc.sql +++ b/sql/procedures/partition_data_proc.sql @@ -172,4 +172,3 @@ EXCEPTION END; $$; - diff --git a/src/pg_partman_bgw.c b/src/pg_partman_bgw.c index a7ccd10..88961af 100644 --- a/src/pg_partman_bgw.c +++ b/src/pg_partman_bgw.c @@ -358,6 +358,17 @@ void pg_partman_bgw_main(Datum main_arg) { } // end main +/* + * Clean up a portal spawned by a maintenance worker in + * pg_partman_bgw_run_maint(). + */ +static void pg_partman_bgw_release_portal(Portal portal) { + MarkPortalDone(portal); + PortalDrop(portal, false); + ActivePortal = NULL; + PortalContext = NULL; +} + /* * Unable to pass the database name as a string argument (not sure why yet) * Instead, the GUC is parsed both in the main function and below and a counter integer @@ -422,9 +433,15 @@ void pg_partman_bgw_run_maint(Datum arg) { portal = CreateNewPortal(); portal->visible = false; portal->resowner = CurrentResourceOwner; + StartTransactionCommand(); + + // This portal does not run any query. Start it and mark it as + // active + PortalDefineQuery(portal, NULL, "", CMDTAG_UNKNOWN, NIL, NULL); + PortalStart(portal, NULL, 0, InvalidSnapshot); + MarkPortalActive(portal); ActivePortal = portal; PortalContext = portal->portalContext; - StartTransactionCommand(); EnsurePortalSnapshotExists(); } @@ -444,8 +461,9 @@ void pg_partman_bgw_run_maint(Datum arg) { SPI_finish(); PopActiveSnapshot(); CommitTransactionCommand(); + if (portal_created) + pg_partman_bgw_release_portal(portal); pgstat_report_activity(STATE_IDLE, NULL); - pfree(rawstring); list_free(elemlist); @@ -521,9 +539,8 @@ void pg_partman_bgw_run_maint(Datum arg) { if (ActiveSnapshotSet()) PopActiveSnapshot(); CommitTransactionCommand(); - PortalDrop(portal, false); - ActivePortal = NULL; - PortalContext = NULL; + + pg_partman_bgw_release_portal(portal); } #if (PG_VERSION_NUM < 150000) diff --git a/test/test-time-daily-date.sql b/test/test-time-daily-date.sql new file mode 100644 index 0000000..47e0196 --- /dev/null +++ b/test/test-time-daily-date.sql @@ -0,0 +1,660 @@ +-- ########## TIME DAILY (DATE CONTROL COLUMN) TESTS ########## +-- Other tests: + -- Privilege inheritance + -- check that maintenance catches up if tables are missing + -- Test using default template table. Initial child tables will have no indexes or primary keys. New tables after template has indexes added should. + -- Test for native FK inheritance + -- Test that column statistics target is inherited from parent tables (this is only testing that the commands work. Actually testing that they're inherited is a different pattern match between 16 and 17. There is an actual test in the pg17plus folder) + +\set ON_ERROR_ROLLBACK 1 +\set ON_ERROR_STOP true + +BEGIN; +SELECT set_config('search_path','partman, public',false); + +SELECT plan(223); + +CREATE ROLE partman_basic; +CREATE ROLE partman_revoke; +CREATE ROLE partman_owner; +CREATE SCHEMA partman_test; +CREATE SCHEMA partman_retention_test AUTHORIZATION partman_owner; + +CREATE TABLE partman_test.fk_test_reference (col2 text unique not null); +INSERT INTO partman_test.fk_test_reference VALUES ('stuff'); + +CREATE TABLE partman_test.time_taptest_table + (col1 int + , col2 text default 'stuff' + , col3 date NOT NULL DEFAULT CURRENT_DATE + , CONSTRAINT fk_test FOREIGN KEY (col2) REFERENCES partman_test.fk_test_reference(col2) ) + PARTITION BY RANGE (col3); +ALTER TABLE partman_test.time_taptest_table + ALTER COLUMN col1 SET STATISTICS 1000, + ALTER COLUMN col2 SET STATISTICS 0; +CREATE TABLE partman_test.undo_taptest (LIKE partman_test.time_taptest_table INCLUDING ALL); +GRANT SELECT,INSERT,UPDATE ON partman_test.time_taptest_table TO partman_basic; +GRANT ALL ON partman_test.time_taptest_table TO partman_revoke; +ALTER TABLE partman_test.time_taptest_table OWNER TO partman_owner; + +SELECT create_partition('partman_test.time_taptest_table', 'col3', '1 day'); +UPDATE part_config SET inherit_privileges = TRUE; +SELECT reapply_privileges('partman_test.time_taptest_table'); + +SELECT is_partitioned('partman_test', 'time_taptest_table', 'Check that time_taptest_table is natively partitioned'); +SELECT has_table('partman', 'template_partman_test_time_taptest_table', 'Check that default template table was created'); +SELECT table_owner_is ('partman', 'template_partman_test_time_taptest_table', 'partman_owner', + 'Check that template table ownership is set properly'); + +-- Add inheritable stuff to template table +ALTER TABLE template_partman_test_time_taptest_table ADD PRIMARY KEY (col1); + +-- Regression test: a template table's primary key built from a unique index with an INCLUDE clause should only inherit its real key column(s) onto child tables, not the INCLUDE column(s). +CREATE TABLE partman_test.time_taptest_include_table (col1 int, col2 text, col3 date NOT NULL DEFAULT CURRENT_DATE) + PARTITION BY RANGE (col3); +SELECT create_partition('partman_test.time_taptest_include_table', 'col3', '1 day'); +CREATE UNIQUE INDEX time_taptest_include_template_idx ON template_partman_test_time_taptest_include_table (col1) INCLUDE (col2); +ALTER TABLE template_partman_test_time_taptest_include_table + ADD CONSTRAINT time_taptest_include_template_pkey PRIMARY KEY USING INDEX time_taptest_include_template_idx; +SELECT create_partition_time('partman_test.time_taptest_include_table', ARRAY[(CURRENT_TIMESTAMP+'100 days'::interval)]); +SELECT col_is_pk('partman_test', 'time_taptest_include_table_p'||to_char(CURRENT_DATE + 100, 'YYYYMMDD'), ARRAY['col1'], + 'Check primary key inherited from template with INCLUDE index contains only the real key column col1'); +SELECT col_isnt_pk('partman_test', 'time_taptest_include_table_p'||to_char(CURRENT_DATE + 100, 'YYYYMMDD'), ARRAY['col2'], + 'Check primary key inherited from template with INCLUDE index does not contain the INCLUDE column col2'); +DROP TABLE partman_test.time_taptest_include_table CASCADE; +DELETE FROM part_config WHERE parent_table = 'partman_test.time_taptest_include_table'; +-- End Regresstion test + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_DATE); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'Check time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||' does not exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 5, 'YYYYMMDD')||' does not exist'); + +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT col_isnt_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), ARRAY['col1'], + 'Check for NO primary key in time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER'], + 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + + +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_table', 'Check that parent table is empty. Should be impossible for native, but leaving test here just cause.'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table', ARRAY[10], 'Check count from parent table'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); + +REVOKE INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON partman_test.time_taptest_table FROM partman_revoke; +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(11,20), CURRENT_DATE + 1); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(21,25), CURRENT_DATE + 2); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(26,30), CURRENT_DATE + 3); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(31,37), CURRENT_DATE + 4); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(40,49), CURRENT_DATE - 1); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(50,70), CURRENT_DATE - 2); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(71,85), CURRENT_DATE - 3); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(86,100), CURRENT_DATE - 4); + +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_table', 'Check that parent table has had no data inserted to it'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), + ARRAY[5], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), + ARRAY[5], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + ARRAY[7], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), + ARRAY[21], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), + ARRAY[15], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), + ARRAY[15], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +UPDATE part_config SET premake = 5 WHERE parent_table = 'partman_test.time_taptest_table'; + +-- Run to create proper future partitions +SELECT run_maintenance(); +-- Insert after maintenance since native fails with no child +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(101,122), CURRENT_DATE + 5); +-- Run again to create +5 partition now that data exists +SELECT run_maintenance(); + +-- Data exists for +5 days, with 5 premake so +10 day table should exist +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||' does not exist'); + +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')); + +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||'.'); + + +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_table', 'Check that parent table has had no data inserted to it'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + ARRAY[22], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_basic', ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_basic', +ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_basic', +ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); + +GRANT DELETE ON partman_test.time_taptest_table TO partman_basic; +REVOKE ALL ON partman_test.time_taptest_table FROM partman_revoke; + +ALTER TABLE partman_test.time_taptest_table OWNER TO partman_owner; + +UPDATE part_config SET premake = 6 WHERE parent_table = 'partman_test.time_taptest_table'; +SELECT run_maintenance(); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(123,150), CURRENT_DATE + 6); +-- Run again now that +6 data exists +SELECT run_maintenance(); + +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_table', 'Check that parent table has had no data inserted to it'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table', ARRAY[148], 'Check count from parent table'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), + ARRAY[28], 'Check count from time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 13, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 13, 'YYYYMMDD')||' does not exist'); + +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); + +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||'.'); +SELECT col_is_fk('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), ARRAY['col2'], + 'Check for foreign key in time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')||'.'); + + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_basic', ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), 'partman_revoke', + ARRAY['SELECT'], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); + +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); + +SELECT reapply_privileges('partman_test.time_taptest_table'); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_basic', + ARRAY['SELECT','INSERT','UPDATE', 'DELETE'], + 'Check partman_basic privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_privs_are('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_revoke', + '{}'::text[], 'Check partman_revoke privileges of time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')); +SELECT table_owner_is ('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), 'partman_owner', + 'Check that ownership change worked for time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')); + +-- Test that maintenance will catch up +DO $$ +BEGIN + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'); + EXECUTE 'DROP TABLE partman_test.time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'); +END +$$; + +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')||' does not exist'); + +SELECT run_maintenance(); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')||' does exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')||' does not exist'); + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(11,20), CURRENT_DATE + 1); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(21,25), CURRENT_DATE + 2); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(26,30), CURRENT_DATE + 3); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_DATE); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(40,49), CURRENT_DATE - 1); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(50,70), CURRENT_DATE - 2); + +SELECT run_maintenance(); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')||' does exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||' does not exist'); + + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(31,37), CURRENT_DATE + 4); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(101,122), CURRENT_DATE + 5); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(123,150), CURRENT_DATE + 6); + +SELECT run_maintenance(); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||' does exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')||' does exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 13, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 13, 'YYYYMMDD')||' does not exist'); + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(200,210), CURRENT_DATE + 20); +SELECT results_eq('SELECT count(*)::int FROM ONLY partman_test.time_taptest_table_default', ARRAY[11], 'Check that data child scope goes to default'); + +SELECT drop_partition_time('partman_test.time_taptest_table', '3 days'::interval, p_keep_table := false); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 4, 'YYYYMMDD')||' does not exist'); + +UPDATE part_config SET retention = '2 days'::interval WHERE parent_table = 'partman_test.time_taptest_table'; +SELECT drop_partition_time('partman_test.time_taptest_table', p_retention_schema := 'partman_retention_test'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')||' does not exist'); +SELECT has_table('partman_retention_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 3, 'YYYYMMDD')||' got moved to new schema'); + +SELECT undo_partition('partman_test.time_taptest_table', 'partman_test.undo_taptest', 20, p_keep_table := false); +SELECT results_eq('SELECT count(*)::int FROM partman_test.undo_taptest', ARRAY[129], 'Check count from target table after undo'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 1, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 2, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 3, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 4, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 5, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 6, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 7, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 8, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 9, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 10, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 11, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE + 12, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 1, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(CURRENT_DATE - 2, 'YYYYMMDD')||' does not exist'); + +SELECT hasnt_table('partman', 'template_partman_test_time_taptest_table', 'Check that template table was dropped'); + +SELECT * FROM finish(); +ROLLBACK; diff --git a/test/test-time-daily.sql b/test/test-time-daily.sql index df832e8..b86d270 100644 --- a/test/test-time-daily.sql +++ b/test/test-time-daily.sql @@ -12,7 +12,7 @@ BEGIN; SELECT set_config('search_path','partman, public',false); -SELECT plan(221); +SELECT plan(223); CREATE ROLE partman_basic; CREATE ROLE partman_revoke; @@ -49,6 +49,22 @@ SELECT table_owner_is ('partman', 'template_partman_test_time_taptest_table', 'p -- Add inheritable stuff to template table ALTER TABLE template_partman_test_time_taptest_table ADD PRIMARY KEY (col1); +-- Regression test: a template table's primary key built from a unique index with an INCLUDE clause should only inherit its real key column(s) onto child tables, not the INCLUDE column(s). +CREATE TABLE partman_test.time_taptest_include_table (col1 int, col2 text, col3 timestamptz NOT NULL DEFAULT now()) + PARTITION BY RANGE (col3); +SELECT create_partition('partman_test.time_taptest_include_table', 'col3', '1 day'); +CREATE UNIQUE INDEX time_taptest_include_template_idx ON template_partman_test_time_taptest_include_table (col1) INCLUDE (col2); +ALTER TABLE template_partman_test_time_taptest_include_table + ADD CONSTRAINT time_taptest_include_template_pkey PRIMARY KEY USING INDEX time_taptest_include_template_idx; +SELECT create_partition_time('partman_test.time_taptest_include_table', ARRAY[(CURRENT_TIMESTAMP+'100 days'::interval)]); +SELECT col_is_pk('partman_test', 'time_taptest_include_table_p'||to_char(CURRENT_TIMESTAMP+'100 days'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check primary key inherited from template with INCLUDE index contains only the real key column col1'); +SELECT col_isnt_pk('partman_test', 'time_taptest_include_table_p'||to_char(CURRENT_TIMESTAMP+'100 days'::interval, 'YYYYMMDD'), ARRAY['col2'], + 'Check primary key inherited from template with INCLUDE index does not contain the INCLUDE column col2'); +DROP TABLE partman_test.time_taptest_include_table CASCADE; +DELETE FROM part_config WHERE parent_table = 'partman_test.time_taptest_include_table'; +-- End Regresstion test + INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_TIMESTAMP); 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'); SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(CURRENT_TIMESTAMP+'1 day'::interval, 'YYYYMMDD'), diff --git a/test/test-time-monthly-boundary.sql b/test/test-time-monthly-boundary.sql new file mode 100644 index 0000000..548c559 --- /dev/null +++ b/test/test-time-monthly-boundary.sql @@ -0,0 +1,79 @@ +-- ########## TIME MONTHLY, ROWS ON A CHILD TABLE'S BOUNDARIES ########## +-- Other tests: + -- partition_data_time() moves a row on the first instant of a month into that month, and leaves + -- a row on the first instant of the next month out of it + -- date control column, out of the default and out of a source table (Github Issue #856) + -- timestamp without time zone control column (Github Issue #838) + +\set ON_ERROR_ROLLBACK 1 +\set ON_ERROR_STOP true + +BEGIN; +SELECT set_config('search_path','partman, public',false); + +SELECT plan(12); +CREATE SCHEMA partman_test; + +CREATE TABLE partman_test.time_taptest_table + (col1 int + , col2 text + , col3 date NOT NULL) + PARTITION BY RANGE (col3); + +SELECT create_partition('partman_test.time_taptest_table', 'col3', '1 month', p_start_partition := to_char(date_trunc('month', CURRENT_DATE), 'YYYY-MM-DD')); + +-- First, middle and last day of the month three months back, and the first day of the month after it. +-- No child table covers either month yet, so all four rows go to the default. +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES + (1, date_trunc('month', CURRENT_DATE - '3 months'::interval)::date) + , (2, date_trunc('month', CURRENT_DATE - '3 months'::interval)::date + 14) + , (3, date_trunc('month', CURRENT_DATE - '2 months'::interval)::date - 1) + , (4, date_trunc('month', CURRENT_DATE - '2 months'::interval)::date); + +SELECT results_eq('SELECT partition_data_time(''partman_test.time_taptest_table''::text, p_batch_count := 2)::int', ARRAY[4], 'Check that partition_data_time moves all four rows out of the default'); +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_table_default', 'Check that the default is empty'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '3 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[1, 2, 3], 'Check that time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '3 months'::interval), 'YYYYMMDD')||' holds its first, middle and last day'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '2 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[4], 'Check that time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '2 months'::interval), 'YYYYMMDD')||' holds its first day'); + +-- The same four days, six and five months back, out of a source table +CREATE TABLE partman_test.time_taptest_source (col1 int, col2 text, col3 date NOT NULL); +INSERT INTO partman_test.time_taptest_source (col1, col3) VALUES + (5, date_trunc('month', CURRENT_DATE - '6 months'::interval)::date) + , (6, date_trunc('month', CURRENT_DATE - '6 months'::interval)::date + 14) + , (7, date_trunc('month', CURRENT_DATE - '5 months'::interval)::date - 1) + , (8, date_trunc('month', CURRENT_DATE - '5 months'::interval)::date); + +SELECT results_eq('SELECT partition_data_time(''partman_test.time_taptest_table''::text, p_batch_count := 2, p_source_table := ''partman_test.time_taptest_source'')::int', + ARRAY[4], 'Check that partition_data_time moves all four rows out of the source table'); +SELECT is_empty('SELECT * FROM partman_test.time_taptest_source', 'Check that the source table is empty'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '6 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[5, 6, 7], 'Check that time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '6 months'::interval), 'YYYYMMDD')||' holds its first, middle and last day'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '5 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[8], 'Check that time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE - '5 months'::interval), 'YYYYMMDD')||' holds its first day'); + +-- A timestamp without time zone control column, with rows on the first and last second of a month +CREATE TABLE partman_test.time_taptest_timestamp + (col1 int + , col2 text + , col3 timestamp NOT NULL) + PARTITION BY RANGE (col3); + +SELECT create_partition('partman_test.time_taptest_timestamp', 'col3', '1 month', p_start_partition := to_char(date_trunc('month', CURRENT_DATE), 'YYYY-MM-DD')); + +INSERT INTO partman_test.time_taptest_timestamp (col1, col3) VALUES + (9, date_trunc('month', LOCALTIMESTAMP - '3 months'::interval)) + , (10, date_trunc('month', LOCALTIMESTAMP - '3 months'::interval) + '14 days'::interval) + , (11, date_trunc('month', LOCALTIMESTAMP - '2 months'::interval) - '1 second'::interval) + , (12, date_trunc('month', LOCALTIMESTAMP - '2 months'::interval)); + +SELECT results_eq('SELECT partition_data_time(''partman_test.time_taptest_timestamp''::text, p_batch_count := 2)::int', ARRAY[4], 'Check that partition_data_time moves all four timestamp rows out of the default'); +SELECT is_empty('SELECT * FROM ONLY partman_test.time_taptest_timestamp_default', 'Check that the timestamp default is empty'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_timestamp_p'||to_char(date_trunc('month', CURRENT_DATE - '3 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[9, 10, 11], 'Check that time_taptest_timestamp_p'||to_char(date_trunc('month', CURRENT_DATE - '3 months'::interval), 'YYYYMMDD')||' holds its first and last second'); +SELECT results_eq('SELECT col1 FROM partman_test.time_taptest_timestamp_p'||to_char(date_trunc('month', CURRENT_DATE - '2 months'::interval), 'YYYYMMDD')||' ORDER BY col1', + ARRAY[12], 'Check that time_taptest_timestamp_p'||to_char(date_trunc('month', CURRENT_DATE - '2 months'::interval), 'YYYYMMDD')||' holds its first second'); + +SELECT * FROM finish(); +ROLLBACK; diff --git a/test/test-time-monthly-date.sql b/test/test-time-monthly-date.sql new file mode 100644 index 0000000..c40dfc2 --- /dev/null +++ b/test/test-time-monthly-date.sql @@ -0,0 +1,307 @@ +-- ########## TIME MONTHLY (DATE CONTROL COLUMN) ########## +-- Other tests: + -- Ensure partitions that would be dropped by retention are not created during maintenance + +\set ON_ERROR_ROLLBACK 1 +\set ON_ERROR_STOP true + +BEGIN; +SELECT set_config('search_path','partman, public',false); + +SELECT plan(121); +CREATE SCHEMA partman_test; +CREATE SCHEMA partman_retention_test; + +CREATE TABLE partman_test.time_taptest_table + (col1 int + , col2 text + , col3 date DEFAULT CURRENT_DATE NOT NULL) + PARTITION BY RANGE (col3); +CREATE TABLE partman_test.time_taptest_table_template (LIKE partman_test.time_taptest_table INCLUDING ALL); +ALTER TABLE partman_test.time_taptest_table_template ADD PRIMARY KEY (col1); +CREATE TABLE partman_test.undo_taptest (LIKE partman_test.time_taptest_table INCLUDING ALL); + +CREATE INDEX ON partman_test.time_taptest_table (col3); + +SELECT create_partition('partman_test.time_taptest_table', 'col3', '1 month', p_template_table => 'partman_test.time_taptest_table_template', p_start_partition := date_trunc('month', (CURRENT_DATE-'12 months'::interval))::text ); + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_DATE); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_DATE-'12 month'::interval); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'13 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'13 month'::interval, 'YYYYMMDD')||' does not exist'); + +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')); + +-- Tests to ensure that old tables are not recreated if they would be included in the retention period + +UPDATE part_config SET retention = '5 months', infinite_time_partitions = true, retention_keep_table = false WHERE parent_table = 'partman_test.time_taptest_table'; + +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD') ); +SELECT lives_ok('DROP TABLE partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), 'Drop table time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD') ); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD')||' still exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')||' does not exist'); + +SELECT run_maintenance(); + +-- Tables older than months should not have been created, 12 month old table should have been dropped and default premade tables should have been remade +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'12 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'11 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'10 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'9 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'8 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'7 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'6 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'5 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')||' exists'); +-- For some reason, edge case of re-creating child tables sometimes goes one beyond premake. Investigate when time. Should not create one 2 beyond premake +--SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), +-- 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD')||' does not exist'); + +-- Reinsert original data +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(1,10), CURRENT_DATE); + +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')); + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(11,20), CURRENT_DATE + '1 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(21,25), CURRENT_DATE + '2 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(26,30), CURRENT_DATE + '3 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(31,37), CURRENT_DATE + '4 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(40,49), CURRENT_DATE - '1 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(50,70), CURRENT_DATE - '2 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(71,85), CURRENT_DATE - '3 month'::interval); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(86,100), CURRENT_DATE - '4 month'::interval); + +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), + ARRAY[5], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), + ARRAY[5], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), + ARRAY[7], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), + ARRAY[10], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), + ARRAY[21], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + ARRAY[15], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), + ARRAY[15], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')); + +UPDATE part_config SET premake = 5 WHERE parent_table = 'partman_test.time_taptest_table'; +SELECT run_maintenance(); + +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(101,122), CURRENT_DATE + '5 month'::interval); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD')); + +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), + ARRAY[22], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')); + +UPDATE part_config SET premake = 6 WHERE parent_table = 'partman_test.time_taptest_table'; +SELECT run_maintenance(); +INSERT INTO partman_test.time_taptest_table (col1, col3) VALUES (generate_series(123,150), CURRENT_DATE + '6 month'::interval); + +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table', ARRAY[148], 'Check count from parent table'); +SELECT results_eq('SELECT count(*)::int FROM partman_test.time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD'), + ARRAY[28], 'Check count from time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD')); + +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD')||' exists'); +SELECT has_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD')||' exists'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'12 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'12 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD')); +SELECT col_is_pk('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD'), ARRAY['col1'], + 'Check for primary key in time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD')); + + + + +SELECT drop_partition_time('partman_test.time_taptest_table', '3 month', p_keep_table := false); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)-'4 month'::interval, 'YYYYMMDD')||' does not exist'); + +UPDATE part_config SET retention = '2 month'::interval WHERE parent_table = 'partman_test.time_taptest_table'; +SELECT drop_partition_time('partman_test.time_taptest_table', p_retention_schema := 'partman_retention_test'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT has_table('partman_retention_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)-'3 month'::interval, 'YYYYMMDD')||' got moved to new schema'); + +SELECT undo_partition('partman_test.time_taptest_table', p_loop_count => 20, p_target_table := 'partman_test.undo_taptest', p_keep_table := false); +SELECT results_eq('SELECT count(*)::int FROM partman_test.undo_taptest', ARRAY[118], 'Check count from target table after undo'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE), 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)-'1 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)-'2 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'1 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'2 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'3 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'4 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'5 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'6 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'7 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'8 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'9 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'10 month'::interval, 'YYYYMMDD')||' does not exist'); +SELECT hasnt_table('partman_test', 'time_taptest_table_p'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD'), + 'Check time_taptest_table_'||to_char(date_trunc('month', CURRENT_DATE)+'11 month'::interval, 'YYYYMMDD')||' does not exist'); + + +SELECT hasnt_table('partman_test', 'time_taptest_table_template', 'Check that template table was dropped'); + +SELECT * FROM finish(); +ROLLBACK; diff --git a/test/test-uuid-pruning.sql b/test/test-uuid-pruning.sql new file mode 100644 index 0000000..8fcf175 --- /dev/null +++ b/test/test-uuid-pruning.sql @@ -0,0 +1,99 @@ +-- ########## UUID7 ENCODER/DECODER PRUNING TESTS ########## +-- Github Issue #873 +-- A query filtered with uuid7_time_encoder() should only scan the matching child table. + +\set ON_ERROR_ROLLBACK 1 +\set ON_ERROR_STOP true + +BEGIN; +SELECT set_config('search_path','partman, public',false); +-- Child boundaries are built in the session time zone and the queries below use UTC bounds +SET TimeZone = 'UTC'; + +SELECT plan(10); + +CREATE SCHEMA partman_test; + +SELECT volatility_is('partman', 'uuid7_time_encoder', ARRAY['timestamp with time zone'], 'immutable', 'Check uuid7_time_encoder() is immutable'); +SELECT volatility_is('partman', 'uuid7_time_decoder', ARRAY['text'], 'immutable', 'Check uuid7_time_decoder() is immutable'); +SELECT is( + (SELECT array_agg(proparallel ORDER BY proname) FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname = 'partman' AND proname IN ('uuid7_time_encoder', 'uuid7_time_decoder')) + , ARRAY['s','s']::"char"[] + , 'Check uuid7 helper functions are parallel safe'); + +CREATE TABLE partman_test.uuid_pruning_table + (col1 uuid NOT NULL + , col2 text + , CONSTRAINT uuid_pruning_table_pkey PRIMARY KEY (col1)) + PARTITION BY RANGE (col1); + +SELECT create_partition( + p_parent_table := 'partman_test.uuid_pruning_table' + , p_control := 'col1' + , p_interval := '1 month' + , p_time_encoder := 'partman.uuid7_time_encoder' + , p_time_decoder := 'partman.uuid7_time_decoder' + , p_start_partition := '2026-01-01' + , p_premake := 6 +); + +INSERT INTO partman_test.uuid_pruning_table (col1, col2) +SELECT uuid7_time_encoder(ts), ts::text +FROM generate_series('2026-01-15 00:00:00+00'::timestamptz, '2026-06-15 00:00:00+00'::timestamptz, '1 month') AS ts; + +SELECT cmp_ok( + (SELECT count(*)::int FROM pg_catalog.pg_inherits WHERE inhparent = 'partman_test.uuid_pruning_table'::regclass) + , '>', 6, 'Check partition set has more than 6 child tables'); + +-- Capture the plan. Index and bitmap scans are off so each child shows up as one Seq Scan line. +SET enable_indexscan = off; +SET enable_indexonlyscan = off; +SET enable_bitmapscan = off; +CREATE TEMP TABLE uuid_pruning_plan (line text); +CREATE FUNCTION partman_test.capture_plan(p_query text) RETURNS void LANGUAGE plpgsql AS $$ +DECLARE v_line text; +BEGIN + DELETE FROM uuid_pruning_plan; + FOR v_line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || p_query LOOP + INSERT INTO uuid_pruning_plan VALUES (v_line); + END LOOP; +END +$$; + +SELECT partman_test.capture_plan($q$SELECT count(*) FROM partman_test.uuid_pruning_table + WHERE col1 >= partman.uuid7_time_encoder('2026-03-01 00:00:00+00') + AND col1 < partman.uuid7_time_encoder('2026-04-01 00:00:00+00')$q$); +SELECT is( + (SELECT count(*)::int FROM uuid_pruning_plan WHERE line ~ 'Seq Scan on uuid_pruning_table_') + , 1, 'Check query filtered with uuid7_time_encoder() is pruned to one child table'); +SELECT is( + (SELECT count(*)::int FROM uuid_pruning_plan WHERE line ~ 'Seq Scan on uuid_pruning_table_p20260301 ') + , 1, 'Check the remaining child table is uuid_pruning_table_p20260301'); +SELECT is( + (SELECT count(*)::int FROM uuid_pruning_plan WHERE line ~ 'uuid7_time_encoder') + , 0, 'Check uuid7_time_encoder() calls were folded to constants at plan time'); + +RESET enable_indexscan; +RESET enable_indexonlyscan; +RESET enable_bitmapscan; + +SELECT results_eq( + $q$SELECT col2 FROM partman_test.uuid_pruning_table + WHERE col1 >= partman.uuid7_time_encoder('2026-03-01 00:00:00+00') + AND col1 < partman.uuid7_time_encoder('2026-04-01 00:00:00+00')$q$ + , ARRAY[('2026-03-15 00:00:00+00'::timestamptz)::text] + , 'Check pruned query still returns the matching row'); + +SELECT is( + (SELECT count(*)::int FROM partman_test.uuid_pruning_table + WHERE uuid7_time_decoder(col1::text) >= '2026-03-01 00:00:00+00' + AND uuid7_time_decoder(col1::text) < '2026-05-01 00:00:00+00') + , 2, 'Check uuid7_time_decoder() returns the encoded timestamps'); + +SELECT lives_ok( + $q$CREATE INDEX uuid_pruning_table_decoded_idx ON partman_test.uuid_pruning_table (partman.uuid7_time_decoder(col1::text))$q$ + , 'Check uuid7_time_decoder() can be used in an index expression'); + +SELECT * FROM finish(); +ROLLBACK; diff --git a/test/test_bgw/test-id-nonsuperuser-bgw-part1.sql b/test/test_bgw/test-id-nonsuperuser-bgw-part1.sql index 10cb384..9a73bc6 100644 --- a/test/test_bgw/test-id-nonsuperuser-bgw-part1.sql +++ b/test/test_bgw/test-id-nonsuperuser-bgw-part1.sql @@ -8,7 +8,7 @@ -- Test create_parent() alias -- Test that BGW can run as a non-superuser. Requires creation of partman_maintainer role and that the BGW be running as that role -- Run in psql: CREATE ROLE partman_maintainer WITH LOGIN; - -- Set in postgresql.conf: pg_partman_bgw.role = 'partman_maintainer' + -- Set in postgresql.conf: pg_partman_bgw.role = 'partman_maintainer' -- Part1 must be run as a superuser -- Part2 can be run as partman_maintainer or partman_owner, but ideal as partman_owner for true testing that maintainer role can affect other role's partition sets -- Example call: pg_prove -U partman_maintainer -d keith -ovf test/test_bgw/test-id-nonsuperuser-bgw-part2.sql @@ -43,7 +43,7 @@ GRANT ALL ON ALL TABLES IN SCHEMA partman TO partman_maintainer; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA partman TO partman_maintainer; GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA partman TO partman_maintainer; -GRANT USAGE ON SCHEMA partman TO partman_owner; +GRANT USAGE,CREATE ON SCHEMA partman TO partman_owner; GRANT ALL ON ALL TABLES IN SCHEMA partman TO partman_owner; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA partman TO partman_owner; GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA partman TO partman_owner; diff --git a/test/test_bgw/test-id-nonsuperuser-bgw-part3.sql b/test/test_bgw/test-id-nonsuperuser-bgw-part3.sql index 2893f32..a34cdef 100644 --- a/test/test_bgw/test-id-nonsuperuser-bgw-part3.sql +++ b/test/test_bgw/test-id-nonsuperuser-bgw-part3.sql @@ -11,7 +11,7 @@ SELECT set_config('search_path','partman, public',false); SELECT plan(5); -REVOKE USAGE ON SCHEMA partman FROM partman_owner; +REVOKE ALL ON SCHEMA partman FROM partman_owner; REVOKE ALL ON ALL TABLES IN SCHEMA partman FROM partman_owner; REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA partman FROM partman_owner; REVOKE EXECUTE ON ALL PROCEDURES IN SCHEMA partman FROM partman_owner; diff --git a/test/test_manual/rls/test-part1-rls.sql b/test/test_manual/rls/test-part1-rls.sql index 531a742..ae9cd68 100644 --- a/test/test_manual/rls/test-part1-rls.sql +++ b/test/test_manual/rls/test-part1-rls.sql @@ -18,7 +18,13 @@ SELECT plan(6); CREATE SCHEMA partman_test; CREATE ROLE partman_alpha WITH LOGIN; CREATE ROLE partman_beta WITH LOGIN; -CREATE ROLE partman_maintenance WITH LOGIN; +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'partman_maintenance') THEN + CREATE ROLE partman_maintenance WITH LOGIN; + END IF; +END +$$; GRANT partman_alpha, partman_beta TO partman_maintenance; DO $$ BEGIN @@ -35,14 +41,14 @@ GRANT ALL ON SCHEMA partman_test TO partman_alpha, partman_beta, partman_mainten ALTER TABLE part_config ENABLE ROW LEVEL SECURITY; -CREATE POLICY part_config_test_user_access_policy ON part_config - FOR ALL - TO PUBLIC +CREATE POLICY part_config_test_user_access_policy ON part_config + FOR ALL + TO PUBLIC USING (maintenance_role = current_user) - WITH CHECK (maintenance_role = current_user); + WITH CHECK (maintenance_role = current_user); -CREATE POLICY part_config_test_maintenance_access_policy ON part_config - FOR ALL +CREATE POLICY part_config_test_maintenance_access_policy ON part_config + FOR ALL TO partman_maintenance USING (true); diff --git a/updates/pg_partman--5.4.3--5.5.0.sql b/updates/pg_partman--5.4.3--5.5.0.sql index 5af00b1..16b6e49 100644 --- a/updates/pg_partman--5.4.3--5.5.0.sql +++ b/updates/pg_partman--5.4.3--5.5.0.sql @@ -4,6 +4,9 @@ ALTER TABLE @extschema@.part_config_sub ADD COLUMN sub_detach_before_drop BOOLEA ALTER TABLE @extschema@.part_config ADD COLUMN maintenance_role TEXT DEFAULT current_user; ALTER TABLE @extschema@.part_config_sub ADD COLUMN sub_maintenance_role TEXT DEFAULT current_user; +UPDATE @extschema@.part_config SET maintenance_role = COALESCE(current_setting('pg_partman_bgw.role', 'partman_maintainer'); +UPDATE @extschema@.part_config_sub SET sub_maintenance_role = COALESCE(current_setting('pg_partman_bgw.role', 'partman_maintainer'); + CREATE TEMP TABLE partman_preserve_privs_temp (statement text); INSERT INTO partman_preserve_privs_temp diff --git a/updates/pg_partman--5.5.0--5.5.1.sql b/updates/pg_partman--5.5.0--5.5.1.sql new file mode 100644 index 0000000..0ef2300 --- /dev/null +++ b/updates/pg_partman--5.5.0--5.5.1.sql @@ -0,0 +1,1760 @@ +-- TODO Go back and test the 5.4.3 to 5.5.0 file again to ensure setting the new maintenance role column works properly + +CREATE OR REPLACE FUNCTION @extschema@.partition_data_time( + p_parent_table text + , p_batch_count int DEFAULT 1 + , p_batch_interval interval DEFAULT NULL + , p_lock_wait numeric DEFAULT 0 + , p_order text DEFAULT 'ASC' + , p_analyze boolean DEFAULT true + , p_source_table text DEFAULT NULL + , p_ignored_columns text[] DEFAULT NULL + , p_override_system_value boolean DEFAULT false + , p_ignore_infinity boolean DEFAULT false +) + RETURNS bigint + LANGUAGE plpgsql + SET search_path = @extschema@, pg_catalog, pg_temp + AS $$ +DECLARE + +v_analyze boolean := FALSE; +v_async_rowcount int; +v_column_list_filtered text; +v_column_list_full text; +v_control text; +v_control_type text; +v_datetime_string text; +v_current_partition_name text; +v_decoded_col text; +v_default_exists boolean; +v_default_schemaname text; +v_default_tablename text; +v_epoch text; +v_infinity_sql text; +v_last_partition text; +v_lock_iter int := 1; +v_lock_obtained boolean := FALSE; +v_max_partition_timestamp timestamptz; +v_min_partition_timestamp timestamptz; +v_override_statement text; +v_parent_schemaname text; +v_parent_tablename text; +v_partition_expression text; +v_partition_interval interval; +v_partition_suffix text; +v_partition_timestamp timestamptz[]; +v_source_schemaname text; +v_source_tablename text; +v_rowcount bigint; +v_sql text; +v_start_control timestamptz; +v_temp_storage_table text; +v_time_decoder text; +v_time_decoder_safe text; +v_time_encoder text; +v_total_rows bigint := 0; + +BEGIN +/* + * Populate the child table(s) of a time-based partition set with data from the default or a source table + */ + +SELECT partition_interval::interval + , control + , time_encoder + , time_decoder + , datetime_string + , epoch +INTO v_partition_interval + , v_control + , v_time_encoder + , v_time_decoder + , v_datetime_string + , v_epoch +FROM @extschema@.part_config +WHERE parent_table = p_parent_table; +IF NOT FOUND THEN + RAISE EXCEPTION 'ERROR: No entry in part_config found for given table: %', p_parent_table; +END IF; + +IF p_ignore_infinity THEN + IF p_source_table IS NOT NULL THEN + RAISE EXCEPTION 'The ignore infinity parameter is only for usage when moving data out of the default. Found that p_source_table was set: %', p_source_table; + END IF; + IF v_time_encoder IS NOT NULL OR v_time_decoder IS NOT NULL THEN + RAISE EXCEPTION 'p_ignore_infinity cannot be set when using encoded time values. part_config.time_encoder has value: %', v_time_encoder; + END IF; +END IF; + +SELECT schemaname, tablename INTO v_parent_schemaname, v_parent_tablename +FROM pg_catalog.pg_tables +WHERE schemaname = split_part(p_parent_table, '.', 1)::name +AND tablename = split_part(p_parent_table, '.', 2)::name; + +SELECT general_type INTO v_control_type FROM @extschema@.check_control_type(v_parent_schemaname, v_parent_tablename, v_control); +IF v_control_type <> 'time' THEN + IF (v_control_type = 'id' AND v_epoch = 'none') OR v_control_type NOT IN ('text', 'id', 'uuid') OR (v_control_type IN ('text', 'uuid') AND v_time_encoder IS NULL) THEN + RAISE EXCEPTION 'Cannot run on partition set without time based control column, an epoch flag set with an id column or time_encoder set with text column. Found control: %, epoch: %, time_encoder: %s', v_control_type, v_epoch, v_time_encoder; + END IF; +END IF; + +SELECT n.nspname::text, c.relname::text +INTO v_default_schemaname, v_default_tablename +FROM pg_catalog.pg_inherits h +JOIN pg_catalog.pg_class c ON c.oid = h.inhrelid +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE h.inhparent = format('%I.%I', v_parent_schemaname, v_parent_tablename)::regclass +AND pg_get_expr(relpartbound, c.oid) = 'DEFAULT'; + +IF p_source_table IS NOT NULL THEN + -- Set source table to user given source table instead of default table + v_source_schemaname := NULL; + v_source_tablename := NULL; + + SELECT schemaname, tablename INTO v_source_schemaname, v_source_tablename + FROM pg_catalog.pg_tables + WHERE schemaname = split_part(p_source_table, '.', 1)::name + AND tablename = split_part(p_source_table, '.', 2)::name; + + IF v_default_tablename IS NOT NULL THEN + -- Cannot set source parameter to default. Otherwise things get put into a weird loop since data is getting put back into where it was just pulled out + IF v_default_schemaname = v_source_schemaname AND v_default_tablename = v_source_tablename THEN + RAISE EXCEPTION 'Cannot set p_source_table to the same value as the default table for this partition set. If you are moving data out of the default, please leave p_source_table unset and data will be moved out of the default table automatically.'; + END IF; + END IF; + + IF v_source_tablename IS NULL THEN + RAISE EXCEPTION 'Given source table does not exist in system catalogs: %', p_source_table; + END IF; + + +ELSE + + IF p_batch_interval IS NOT NULL AND p_batch_interval != v_partition_interval THEN + -- This is true because all data for a given child table must be moved out of the default partition before the child table can be created. + -- So cannot create the child table when only some of the data has been moved out of the default partition. + RAISE EXCEPTION 'If any interval smaller than the partition interval must be used for moving data out of the default, please use the partition_data_async() procedure.'; + END IF; + + -- Set source table to default table if p_source_table is not set, and it exists + -- Otherwise just return with a NOTICE that no data source exists + + IF v_default_tablename IS NOT NULL THEN + v_source_schemaname := v_default_schemaname; + v_source_tablename := v_default_tablename; + + v_default_exists := true; + + v_temp_storage_table := format('%I', 'partman_temp_data_storage'); + EXECUTE format ('CREATE TEMP TABLE IF NOT EXISTS %s (LIKE %I.%I INCLUDING INDEXES) ON COMMIT DROP', v_temp_storage_table, v_source_schemaname, v_source_tablename); + + ELSE + RAISE NOTICE 'No default table found when partition_data_time() was called'; + RETURN v_total_rows; + END IF; +END IF; + +IF p_batch_interval IS NULL OR p_batch_interval > v_partition_interval THEN + p_batch_interval := v_partition_interval; +END IF; + +SELECT partition_tablename INTO v_last_partition FROM @extschema@.show_partitions(p_parent_table, 'DESC') LIMIT 1; + +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 = 'microseconds' THEN format('to_timestamp((%I/1000000)::float)', v_control) + WHEN v_epoch = 'nanoseconds' THEN format('to_timestamp((%I/1000000000)::float)', v_control) + ELSE format('%I', v_control) +END; + +-- Generate filtered column list to use in SELECT/INSERT statements below. Allows for exclusion of GENERATED (or any other desired) columns. +SELECT string_agg(quote_ident(attname), ',') +INTO v_column_list_filtered +FROM pg_catalog.pg_attribute a +JOIN pg_catalog.pg_class c ON a.attrelid = c.oid +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = v_source_schemaname +AND c.relname = v_source_tablename +AND a.attnum > 0 +AND a.attisdropped = false +AND attname <> ALL(COALESCE(p_ignored_columns, ARRAY[]::text[])); + +-- Generate full column list to use in SELECT/INSERT statements below when temp table is in use +SELECT string_agg(quote_ident(attname), ',') +INTO v_column_list_full +FROM pg_catalog.pg_attribute a +JOIN pg_catalog.pg_class c ON a.attrelid = c.oid +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = v_source_schemaname +AND c.relname = v_source_tablename +AND a.attnum > 0 +AND a.attisdropped = false; + +IF p_ignore_infinity AND v_time_decoder IS NULL THEN + v_infinity_sql := format(' WHERE %I NOT IN (''-infinity'', ''infinity'')', v_control); +ELSIF p_ignore_infinity AND v_time_decoder IS NOT NULL THEN + RAISE EXCEPTION 'When using a decoder for partition values, infinity cannot be ignored and is generally not supported as a value.'; +ELSE + v_infinity_sql := ''; +END IF; + +IF v_time_decoder IS NOT NULL THEN + v_time_decoder_safe := partman_safe_obj_name(v_time_decoder); +END IF; + +FOR i IN 1..p_batch_count LOOP + + IF v_time_decoder IS NULL THEN + IF p_order = 'ASC' THEN + EXECUTE format('SELECT min(%s) FROM ONLY %I.%I %s', v_partition_expression, v_source_schemaname, v_source_tablename, v_infinity_sql) INTO v_start_control; + ELSIF p_order = 'DESC' THEN + EXECUTE format('SELECT max(%s) FROM ONLY %I.%I %s', v_partition_expression, v_source_schemaname, v_source_tablename, v_infinity_sql) INTO v_start_control; + ELSE + RAISE EXCEPTION 'Invalid value for p_order. Must be ASC or DESC'; + END IF; + + ELSE + -- Currently time decoder function must take a text parameter. See if this can be more flexible in the future + -- infinity value not supported in uuid columns, so shouldn't have to worry about it. Exception catches it above if user tries to ignore infinity. + + + IF p_order = 'ASC' THEN + EXECUTE format('SELECT min(%s(%s::text)) FROM ONLY %I.%I', v_time_decoder_safe, v_partition_expression, v_source_schemaname, v_source_tablename) INTO v_start_control; + ELSIF p_order = 'DESC' THEN + EXECUTE format('SELECT max(%s(%s::text)) FROM ONLY %I.%I', v_time_decoder_safe, v_partition_expression, v_source_schemaname, v_source_tablename) INTO v_start_control; + ELSE + RAISE EXCEPTION 'Invalid value for p_order. Must be ASC or DESC'; + END IF; + END IF; + + IF v_start_control IS NULL THEN + EXIT; + END IF; + + SELECT child_start_time INTO v_min_partition_timestamp FROM @extschema@.show_partition_info(v_parent_schemaname||'.'||v_last_partition + , v_partition_interval::text + , p_parent_table); + v_max_partition_timestamp := v_min_partition_timestamp + v_partition_interval; + LOOP + IF v_start_control >= v_min_partition_timestamp AND v_start_control < v_max_partition_timestamp THEN + EXIT; + ELSE + BEGIN + IF v_start_control >= v_max_partition_timestamp THEN + -- Keep going forward in time, checking if child partition time interval encompasses the current v_start_control value + v_min_partition_timestamp := v_max_partition_timestamp; + v_max_partition_timestamp := v_max_partition_timestamp + v_partition_interval; + + ELSE + -- Keep going backwards in time, checking if child partition time interval encompasses the current v_start_control value + v_max_partition_timestamp := v_min_partition_timestamp; + v_min_partition_timestamp := v_min_partition_timestamp - v_partition_interval; + END IF; + EXCEPTION WHEN datetime_field_overflow THEN + RAISE EXCEPTION 'Attempted partition time interval is outside PostgreSQL''s supported time range. + Unable to create partition with interval before timestamp % ', v_min_partition_timestamp; + END; + END IF; + END LOOP; + + v_partition_timestamp := ARRAY[v_min_partition_timestamp]; + IF p_order = 'ASC' THEN + -- Ensure batch interval given as parameter doesn't cause maximum to overflow the current partition maximum + IF (v_start_control + p_batch_interval) >= (v_min_partition_timestamp + v_partition_interval) THEN + v_max_partition_timestamp := v_min_partition_timestamp + v_partition_interval; + ELSE + v_max_partition_timestamp := v_start_control + p_batch_interval; + END IF; + ELSIF p_order = 'DESC' THEN + -- Must be greater than max value still in parent table since query below grabs < max + v_max_partition_timestamp := v_min_partition_timestamp + v_partition_interval; + -- Ensure batch interval given as parameter doesn't cause minimum to underflow current partition minimum + IF (v_start_control - p_batch_interval) >= v_min_partition_timestamp THEN + v_min_partition_timestamp := v_start_control - p_batch_interval; + END IF; + ELSE + RAISE EXCEPTION 'Invalid value for p_order. Must be ASC or DESC'; + END IF; + +-- do some locking with timeout, if required + IF p_lock_wait > 0 THEN + v_lock_iter := 0; + WHILE v_lock_iter <= 5 LOOP + v_lock_iter := v_lock_iter + 1; + BEGIN + v_sql := format('SELECT * FROM ONLY %I.%I WHERE %s >= %L AND %3$s < %5$L FOR UPDATE NOWAIT' + , v_source_schemaname + , v_source_tablename + , v_partition_expression + , v_min_partition_timestamp + , v_max_partition_timestamp); + EXECUTE v_sql; + v_lock_obtained := TRUE; + EXCEPTION + WHEN lock_not_available THEN + PERFORM pg_sleep( p_lock_wait / 5.0 ); + CONTINUE; + END; + EXIT WHEN v_lock_obtained; + END LOOP; + IF NOT v_lock_obtained THEN + RETURN -1; + END IF; + END IF; + + -- This suffix generation code is in create_partition_time() as well + v_partition_suffix := to_char(v_min_partition_timestamp, v_datetime_string); + v_current_partition_name := @extschema@.check_name_length(v_parent_tablename, v_partition_suffix, TRUE); + + IF p_override_system_value THEN + v_override_statement = ' OVERRIDING SYSTEM VALUE '; + ELSE + v_override_statement = ' '; + END IF; + + -- Create a variable to use in all scenarios below for handling encoded columns + + IF v_time_decoder IS NULL THEN + -- Compare in the control column's own type, which is how the child table's bounds are read. + -- Compared as text, a date sorts before the timestamptz bound it is equal to ('2026-10-01' < '2026-10-01 00:00:00+00'), + -- so the first day of a child table's range was left behind and the first day of the next one taken (Github Issues #838, #856) + v_decoded_col := v_partition_expression; + ELSE + v_decoded_col := format('%s(%s::text)', v_time_decoder_safe, v_partition_expression); + END IF; + + IF v_default_exists THEN + -- Child tables cannot be created if data that belongs to it exists in the default + -- Have to move data out to temporary location, create child table, then move it back + + -- Temp table created above to avoid excessive temp creation in loop + -- Must use full column list here since the temp table cannot have generated/identity values for defaults. + -- This allows for all scenarios where some people may want newly generated values and others may not. + -- Those that want them are handled by the filtered column list when moving to the real table + + -- Infinity only needs to be of concern when being removed from the default + + v_sql := format('DELETE FROM %1$I.%2$I WHERE %3$s >= %4$L AND %3$s < %5$L RETURNING *' + , v_source_schemaname + , v_source_tablename + , v_decoded_col + , v_min_partition_timestamp + , v_max_partition_timestamp); + v_sql := format('WITH partition_data AS (%s) INSERT INTO %2$s (%3$s) SELECT %3$s FROM partition_data' + , v_sql + , v_temp_storage_table + , v_column_list_full); + + EXECUTE v_sql; + + -- Set analyze to true if a table is created + v_analyze := @extschema@.create_partition_time(p_parent_table, v_partition_timestamp); + + EXECUTE format('WITH partition_data AS ( + DELETE FROM %s RETURNING *) + INSERT INTO %I.%I (%4$s) %5$s SELECT %4$s FROM partition_data' + , v_temp_storage_table + , v_parent_schemaname + , v_current_partition_name + , v_column_list_filtered + , v_override_statement); + + ELSE + + -- Set analyze to true if a table is created + v_analyze := @extschema@.create_partition_time(p_parent_table, v_partition_timestamp); + + v_sql := format('WITH partition_data AS ( + DELETE FROM ONLY %I.%I WHERE %3$s >= %L AND %3$s < %5$L RETURNING *) + INSERT INTO %6$I.%7$I (%8$s) %9$s SELECT %8$s FROM partition_data' + , v_source_schemaname + , v_source_tablename + , v_decoded_col + , v_min_partition_timestamp + , v_max_partition_timestamp + , v_parent_schemaname + , v_current_partition_name + , v_column_list_filtered + , v_override_statement); + EXECUTE v_sql; + + END IF; + + GET DIAGNOSTICS v_rowcount = ROW_COUNT; + v_total_rows := v_total_rows + v_rowcount; + IF v_rowcount = 0 THEN + EXIT; + END IF; + +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_partition() to avoid long exclusive lock or run_maintenance() to avoid long creation runs. +IF v_analyze AND p_analyze THEN + RAISE DEBUG 'partiton_data_time: Begin analyze of %.%', v_parent_schemaname, v_parent_tablename; + EXECUTE format('ANALYZE %I.%I', v_parent_schemaname, v_parent_tablename); + RAISE DEBUG 'partiton_data_time: End analyze of %.%', v_parent_schemaname, v_parent_tablename; +END IF; + +RETURN v_total_rows; + +END +$$; + + +CREATE OR REPLACE FUNCTION @extschema@.create_partition( + p_parent_table text + , p_control text + , p_interval text + , p_type text DEFAULT 'range' + , p_epoch text DEFAULT 'none' + , p_premake int DEFAULT 4 + , p_start_partition text DEFAULT NULL + , p_default_table boolean DEFAULT true + , p_automatic_maintenance text DEFAULT 'on' + , p_constraint_cols text[] DEFAULT NULL + , p_template_table text DEFAULT NULL + , p_jobmon boolean DEFAULT true + , p_date_trunc_interval text DEFAULT NULL + , p_control_not_null boolean DEFAULT true + , p_time_encoder text DEFAULT NULL + , p_time_decoder text DEFAULT NULL + , p_offset_id bigint DEFAULT 0 +) + RETURNS boolean + LANGUAGE plpgsql + SET search_path = @extschema@, pg_catalog, pg_temp + AS $$ +DECLARE + +ex_context text; +ex_detail text; +ex_hint text; +ex_message text; +v_base_timestamp timestamptz; +v_count int := 1; +v_control_type text; +v_control_exact_type text; +v_datetime_string text; +v_default_partition text; +v_higher_control_type text; +v_higher_parent_control text; +v_higher_parent_epoch text; +v_higher_parent_schema text := split_part(p_parent_table, '.', 1); +v_higher_parent_table text := split_part(p_parent_table, '.', 2); +v_id_interval bigint; +v_inherit_privileges boolean := false; -- This is false by default so initial partition set creation doesn't require superuser. +v_job_id bigint; +v_jobmon_schema text; +v_last_partition_created boolean; +v_max bigint; +v_notnull boolean; +v_new_search_path text; +v_old_search_path text; +v_parent_owner text; +v_parent_partition_id bigint; +v_parent_partition_timestamp timestamptz; +v_parent_schemaname text; +v_parent_tablename text; +v_parent_tablespace name; +v_part_col text; +v_part_type text; +v_partattrs smallint[]; +v_partition_time timestamptz; +v_partition_time_array timestamptz[]; +v_partition_id_array bigint[]; +v_partstrat char; +v_row record; +v_sql text; +v_start_time timestamptz; +v_starting_partition_id bigint; +v_step_id bigint; +v_step_overflow_id bigint; +v_success boolean := false; +v_template_schema text; +v_template_tablename text; +v_time_interval interval; +v_top_parent_schema text := split_part(p_parent_table, '.', 1); +v_top_parent_table text := split_part(p_parent_table, '.', 2); +v_unlogged char; + +BEGIN +/* + * Function to turn a table into the parent of a partition set + * IMPORTANT: Do not forget to update parameters for create_parent() if they change in this function + */ + +IF array_length(string_to_array(p_parent_table, '.'), 1) < 2 THEN + RAISE EXCEPTION 'Parent table must be schema qualified'; +ELSIF array_length(string_to_array(p_parent_table, '.'), 1) > 2 THEN + RAISE EXCEPTION 'pg_partman does not support objects with periods in their names'; +END IF; + +IF p_interval = 'yearly' + OR p_interval = 'quarterly' + OR p_interval = 'monthly' + OR p_interval = 'weekly' + OR p_interval = 'daily' + OR p_interval = 'hourly' + OR p_interval = 'half-hour' + OR p_interval = 'quarter-hour' +THEN + RAISE EXCEPTION 'Special partition interval values from old pg_partman versions (%) are no longer supported. Please use a supported interval time value from core PostgreSQL (https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT)';, p_interval; +END IF; + +SELECT n.nspname + , c.relname + , t.spcname +INTO v_parent_schemaname + , 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; + IF v_parent_tablename IS NULL THEN + RAISE EXCEPTION 'Unable to find given parent table in system catalogs. Please create parent table first: %', p_parent_table; + END IF; + +SELECT attnotnull INTO v_notnull +FROM pg_catalog.pg_attribute a +JOIN pg_catalog.pg_class c ON a.attrelid = c.oid +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE c.relname = v_parent_tablename::name +AND n.nspname = v_parent_schemaname::name +AND a.attname = p_control::name; + IF (v_notnull IS NULL) THEN + RAISE EXCEPTION 'Control column given (%) for parent table (%) does not exist', p_control, p_parent_table; + ELSIF (v_notnull = false and p_control_not_null = true) THEN + RAISE EXCEPTION 'Control column given (%) for parent table (%) must be set to NOT NULL', p_control, p_parent_table; + END IF; + +SELECT general_type, exact_type INTO v_control_type, v_control_exact_type +FROM @extschema@.check_control_type(v_parent_schemaname, v_parent_tablename, p_control); + +IF v_control_type IS NULL THEN + RAISE EXCEPTION 'pg_partman only supports partitioning of data types that are integer, numeric, date/timestamp or specially encoded text. Supplied column is of type %', v_control_exact_type; +END IF; + +IF (p_epoch <> 'none' AND v_control_type <> 'id') THEN + RAISE EXCEPTION 'p_epoch can only be used with an integer based control column'; +END IF; + + +IF NOT @extschema@.check_partition_type(p_type) THEN + RAISE EXCEPTION '% is not a valid partitioning type for pg_partman', p_type; +END IF; + +IF current_setting('server_version_num')::int < 140000 THEN + RAISE EXCEPTION 'pg_partman requires PostgreSQL 14 or greater'; +END IF; +-- Check if given parent table has been already set up as a partitioned table +SELECT p.partstrat + , p.partattrs +INTO v_partstrat + , v_partattrs +FROM pg_catalog.pg_partitioned_table p +JOIN pg_catalog.pg_class c ON p.partrelid = c.oid +JOIN pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = v_parent_schemaname::name +AND c.relname = v_parent_tablename::name; + +IF v_partstrat NOT IN ('r', 'l') OR v_partstrat IS NULL THEN + RAISE EXCEPTION 'You must have created the given parent table as ranged or list partitioned already. Ex: CREATE TABLE ... PARTITION BY [RANGE|LIST] ...)'; +END IF; + +IF array_length(v_partattrs, 1) > 1 THEN + RAISE NOTICE 'pg_partman only supports single column partitioning at this time. Found % columns in given parent definition.', array_length(v_partattrs, 1); +END IF; + +SELECT a.attname, t.typname +INTO v_part_col, v_part_type +FROM pg_attribute a +JOIN pg_class c ON a.attrelid = c.oid +JOIN pg_namespace n ON c.relnamespace = n.oid +JOIN pg_type t ON a.atttypid = t.oid +WHERE n.nspname = v_parent_schemaname::name +AND c.relname = v_parent_tablename::name +AND attnum IN (SELECT unnest(partattrs) FROM pg_partitioned_table p WHERE a.attrelid = p.partrelid); + +IF p_control <> v_part_col OR v_control_exact_type <> v_part_type THEN + RAISE EXCEPTION 'Control column and type given in arguments (%, %) does not match the control column and type of the given partition set (%, %)', p_control, v_control_exact_type, v_part_col, v_part_type; +END IF; + +-- Check that control column is a usable type for pg_partman. +IF v_control_type NOT IN ('time', 'id', 'text', 'uuid') THEN + RAISE EXCEPTION 'Only date/time, text/uuid or integer types are allowed for the control column.'; +ELSIF v_control_type IN ('text', 'uuid') AND (p_time_encoder IS NULL OR p_time_decoder IS NULL) THEN + RAISE EXCEPTION 'p_time_encoder and p_time_decoder needs to be set for text/uuid type control column.'; +ELSIF v_control_type NOT IN ('text', 'uuid') AND (p_time_encoder IS NOT NULL OR p_time_decoder IS NOT NULL) THEN + RAISE EXCEPTION 'p_time_encoder and p_time_decoder can only be used with text/uuid type control column.'; +END IF; + +-- Table to handle properties not managed by core PostgreSQL yet +IF p_template_table IS NULL THEN + v_template_schema := '@extschema@'; + v_template_tablename := @extschema@.check_name_length('template_'||v_parent_schemaname||'_'||v_parent_tablename); + EXECUTE format('CREATE TABLE IF NOT EXISTS %I.%I (LIKE %I.%I)', v_template_schema, v_template_tablename, v_parent_schemaname, v_parent_tablename); + + SELECT pg_get_userbyid(c.relowner) INTO v_parent_owner + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE n.nspname = v_parent_schemaname::name + AND c.relname = v_parent_tablename::name; + + EXECUTE format('ALTER TABLE %s.%I OWNER TO %I' + , '@extschema@' + , v_template_tablename + , v_parent_owner); +ELSIF lower(p_template_table) IN ('false', 'f') THEN + v_template_schema := NULL; + v_template_tablename := NULL; + RAISE DEBUG 'create_partition(): parent_table: %, skipped template table creation', p_parent_table; +ELSE + SELECT n.nspname, c.relname INTO v_template_schema, v_template_tablename + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE n.nspname = split_part(p_template_table, '.', 1)::name + AND c.relname = split_part(p_template_table, '.', 2)::name; + IF v_template_tablename IS NULL THEN + RAISE EXCEPTION 'Unable to find given template table in system catalogs (%). Please create template table first or leave parameter NULL to have a default one created for you.', p_parent_table; + END IF; +END IF; + +IF p_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 + SELECT current_setting('search_path') INTO v_old_search_path; + IF v_jobmon_schema IS NOT NULL THEN + v_new_search_path := format('%s,%s',v_jobmon_schema, v_old_search_path); + EXECUTE format('SET LOCAL search_path TO %s', v_new_search_path); + END IF; + END IF; +END IF; + +EXECUTE format('LOCK TABLE %I.%I IN ACCESS EXCLUSIVE MODE', v_parent_schemaname, v_parent_tablename); + +IF v_jobmon_schema IS NOT NULL THEN + v_job_id := add_job(format('PARTMAN SETUP PARENT: %s', p_parent_table)); + v_step_id := add_step(v_job_id, format('Creating initial partitions on new parent table: %s', p_parent_table)); +END IF; + +-- If this parent table has siblings that are also partitioned (subpartitions), ensure this parent gets added to part_config_sub table so future maintenance will subpartition it +-- Just doing in a loop to avoid having to assign a bunch of variables (should only run once, if at all; constraint should enforce only one value.) +FOR v_row IN + WITH parent_table AS ( + SELECT h.inhparent AS parent_oid + FROM pg_catalog.pg_inherits h + JOIN pg_catalog.pg_class c ON h.inhrelid = c.oid + JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE c.relname = v_parent_tablename::name + AND n.nspname = v_parent_schemaname::name + ), sibling_children AS ( + SELECT i.inhrelid::regclass::text AS tablename + FROM pg_inherits i + JOIN parent_table p ON i.inhparent = p.parent_oid + ) + -- These column lists must be kept consistent between: + -- create_partition, check_subpart_sameconfig, create_partition_id, create_partition_time, dump_partitioned_table_definition and table definition + SELECT DISTINCT + a.sub_control + , a.sub_partition_interval + , a.sub_partition_type + , a.sub_premake + , a.sub_automatic_maintenance + , a.sub_template_table + , a.sub_retention + , a.sub_retention_schema + , a.sub_retention_keep_table + , a.sub_retention_keep_index + , a.sub_epoch + , a.sub_constraint_cols + , a.sub_optimize_constraint + , a.sub_infinite_time_partitions + , a.sub_jobmon + , a.sub_inherit_privileges + , a.sub_constraint_valid + , a.sub_date_trunc_interval + , a.sub_ignore_default_data + , a.sub_default_table + , a.sub_retention_keep_publication + , a.sub_detach_before_drop + , a.sub_maintenance_role + FROM @extschema@.part_config_sub a + JOIN sibling_children b on a.sub_parent = b.tablename LIMIT 1 +LOOP + INSERT INTO @extschema@.part_config_sub ( + sub_parent + , sub_partition_type + , sub_control + , sub_partition_interval + , sub_constraint_cols + , sub_premake + , sub_retention + , sub_retention_schema + , sub_retention_keep_table + , sub_retention_keep_index + , sub_automatic_maintenance + , sub_epoch + , sub_optimize_constraint + , sub_infinite_time_partitions + , sub_jobmon + , sub_template_table + , sub_inherit_privileges + , sub_constraint_valid + , sub_date_trunc_interval + , sub_ignore_default_data + , sub_retention_keep_publication + , sub_detach_before_drop + , sub_maintenance_role) + VALUES ( + p_parent_table + , v_row.sub_partition_type + , v_row.sub_control + , v_row.sub_partition_interval + , v_row.sub_constraint_cols + , v_row.sub_premake + , v_row.sub_retention + , v_row.sub_retention_schema + , v_row.sub_retention_keep_table + , v_row.sub_retention_keep_index + , v_row.sub_automatic_maintenance + , v_row.sub_epoch + , v_row.sub_optimize_constraint + , v_row.sub_infinite_time_partitions + , v_row.sub_jobmon + , v_row.sub_template_table + , v_row.sub_inherit_privileges + , v_row.sub_constraint_valid + , v_row.sub_date_trunc_interval + , v_row.sub_ignore_default_data + , v_row.sub_retention_keep_publication + , v_row.sub_detach_before_drop + , v_row.sub_maintenance_role); + + -- Set this equal to sibling configs so that newly created child table + -- privileges are set properly below during initial setup. + -- This setting is special because it applies immediately to the new child + -- tables of a given parent, not just during maintenance like most other settings. + v_inherit_privileges = v_row.sub_inherit_privileges; +END LOOP; + +IF v_control_type IN ('time', 'text', 'uuid') OR (v_control_type = 'id' AND p_epoch <> 'none') THEN + + v_time_interval := p_interval::interval; + IF v_time_interval < '1 second'::interval THEN + RAISE EXCEPTION 'Partitioning interval must be 1 second or greater'; + END IF; + + -- First partition is either the min premake or p_start_partition + v_start_time := COALESCE(p_start_partition::timestamptz, CURRENT_TIMESTAMP - (v_time_interval * p_premake)); + + SELECT base_timestamp, datetime_string + INTO v_base_timestamp, v_datetime_string + FROM @extschema@.calculate_time_partition_info(v_time_interval, v_start_time, p_date_trunc_interval); + + RAISE DEBUG '(): parent_table: %, v_base_timestamp: %', p_parent_table, v_base_timestamp; + + v_partition_time_array := array_append(v_partition_time_array, v_base_timestamp); + + LOOP + -- If current loop value is less than or equal to the value of the max premake, add time to array. + IF (v_base_timestamp + (v_time_interval * v_count)) < (CURRENT_TIMESTAMP + (v_time_interval * p_premake)) THEN + BEGIN + v_partition_time := (v_base_timestamp + (v_time_interval * v_count))::timestamptz; + v_partition_time_array := array_append(v_partition_time_array, v_partition_time); + 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_partition_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_partition_time||' skipped'); + CONTINUE; + END; + ELSE + EXIT; -- all needed partitions added to array. Exit the loop. + END IF; + v_count := v_count + 1; + END LOOP; + + INSERT INTO @extschema@.part_config ( + parent_table + , partition_type + , partition_interval + , epoch + , control + , premake + , time_encoder + , time_decoder + , constraint_cols + , datetime_string + , automatic_maintenance + , jobmon + , template_table + , inherit_privileges + , date_trunc_interval) + VALUES ( + p_parent_table + , p_type + , v_time_interval + , p_epoch + , p_control + , p_premake + , p_time_encoder + , p_time_decoder + , p_constraint_cols + , v_datetime_string + , p_automatic_maintenance + , p_jobmon + , v_template_schema||'.'||v_template_tablename + , v_inherit_privileges + , p_date_trunc_interval); + + RAISE DEBUG ': v_partition_time_array: %', v_partition_time_array; + + v_last_partition_created := @extschema@.create_partition_time(p_parent_table, v_partition_time_array); + + IF v_last_partition_created = false THEN + -- This can happen with subpartitioning when future or past partitions prevent child creation because they're out of range of the parent + -- First see if this parent is a subpartition managed by pg_partman + WITH top_oid AS ( + SELECT i.inhparent AS top_parent_oid + FROM pg_catalog.pg_inherits i + JOIN pg_catalog.pg_class c ON c.oid = i.inhrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE c.relname = v_parent_tablename::name + AND n.nspname = v_parent_schemaname::name + ) SELECT n.nspname, c.relname + INTO v_top_parent_schema, v_top_parent_table + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN top_oid t ON c.oid = t.top_parent_oid + JOIN @extschema@.part_config p ON p.parent_table = n.nspname||'.'||c.relname; + + IF v_top_parent_table IS NOT NULL THEN + -- If so create the lowest possible partition that is within the boundary of the parent + SELECT child_start_time INTO v_parent_partition_timestamp FROM @extschema@.show_partition_info(p_parent_table, p_parent_table := v_top_parent_schema||'.'||v_top_parent_table); + IF v_base_timestamp >= v_parent_partition_timestamp THEN + WHILE v_base_timestamp >= v_parent_partition_timestamp LOOP + v_base_timestamp := v_base_timestamp - v_time_interval; + END LOOP; + v_base_timestamp := v_base_timestamp + v_time_interval; -- add one back since while loop set it one lower than is needed + ELSIF v_base_timestamp < v_parent_partition_timestamp THEN + WHILE v_base_timestamp < v_parent_partition_timestamp LOOP + v_base_timestamp := v_base_timestamp + v_time_interval; + END LOOP; + -- Don't need to remove one since new starting time will fit in top parent interval + END IF; + v_partition_time_array := NULL; + v_partition_time_array := array_append(v_partition_time_array, v_base_timestamp); + v_last_partition_created := @extschema@.create_partition_time(p_parent_table, v_partition_time_array); + ELSE + RAISE WARNING 'No child tables created. Check that all child tables did not already exist and may not have been part of partition set. Given parent has still been configured with pg_partman, but may not have expected children. Please review schema and config to confirm things are ok.'; + + IF v_jobmon_schema IS NOT NULL THEN + PERFORM update_step(v_step_id, 'OK', 'Done'); + 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; + + RETURN v_success; + END IF; + END IF; -- End v_last_partition IF + + IF v_jobmon_schema IS NOT NULL THEN + PERFORM update_step(v_step_id, 'OK', format('Time partitions premade: %s', p_premake)); + END IF; + +END IF; + +IF v_control_type = 'id' AND p_epoch = 'none' THEN + v_id_interval := p_interval::bigint; + IF v_id_interval < 2 AND p_type != 'list' THEN + RAISE EXCEPTION 'Interval for range partitioning must be greater than or equal to 2. Use LIST partitioning for single value partitions. (Values given: p_interval: %, p_type: %)', p_interval, p_type; + END IF; + + -- Check if parent table is a subpartition of an already existing id partition set managed by pg_partman. + WHILE v_higher_parent_table IS NOT NULL LOOP -- initially set in DECLARE + WITH top_oid AS ( + SELECT i.inhparent AS top_parent_oid + FROM pg_catalog.pg_inherits i + JOIN pg_catalog.pg_class c ON c.oid = i.inhrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = v_higher_parent_schema::name + AND c.relname = v_higher_parent_table::name + ) SELECT n.nspname, c.relname, p.control, p.epoch + INTO v_higher_parent_schema, v_higher_parent_table, v_higher_parent_control, v_higher_parent_epoch + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN top_oid t ON c.oid = t.top_parent_oid + JOIN @extschema@.part_config p ON p.parent_table = n.nspname||'.'||c.relname; + + IF v_higher_parent_table IS NOT NULL THEN + SELECT general_type INTO v_higher_control_type + FROM @extschema@.check_control_type(v_higher_parent_schema, v_higher_parent_table, v_higher_parent_control); + IF v_higher_control_type <> 'id' or (v_higher_control_type = 'id' AND v_higher_parent_epoch <> 'none') THEN + -- The parent above the p_parent_table parameter is not partitioned by ID + -- so don't check for max values in parents that aren't partitioned by ID. + -- This avoids missing child tables in subpartition sets that have differing ID data + EXIT; + END IF; + -- v_top_parent initially set in DECLARE + v_top_parent_schema := v_higher_parent_schema; + v_top_parent_table := v_higher_parent_table; + END IF; + END LOOP; + + -- If custom start partition is set, use that. + -- If custom start is not set and there is already data, start partitioning with the highest current value and ensure it's grabbed from highest top parent table + IF p_start_partition IS NOT NULL THEN + v_max := p_start_partition::bigint; + ELSE + v_sql := format('SELECT COALESCE(trunc(max(%I))::bigint, 0) FROM %I.%I LIMIT 1' + , p_control + , v_top_parent_schema + , v_top_parent_table); + EXECUTE v_sql INTO v_max; + END IF; + + v_starting_partition_id := ((v_max - (v_max % v_id_interval)) + p_offset_id); + FOR i IN 0..p_premake LOOP + -- Only make previous partitions if ID value is less than the starting value and positive (and custom start partition wasn't set) + IF p_start_partition IS NULL AND + (v_starting_partition_id - (v_id_interval*i)) > 0 AND + (v_starting_partition_id - (v_id_interval*i)) < v_starting_partition_id + THEN + v_partition_id_array = array_append(v_partition_id_array, (v_starting_partition_id - v_id_interval*i)); + END IF; + v_partition_id_array = array_append(v_partition_id_array, (v_id_interval*i) + v_starting_partition_id); + END LOOP; + + INSERT INTO @extschema@.part_config ( + parent_table + , partition_type + , partition_interval + , control + , premake + , constraint_cols + , automatic_maintenance + , jobmon + , template_table + , inherit_privileges + , date_trunc_interval) + VALUES ( + p_parent_table + , p_type + , v_id_interval + , p_control + , p_premake + , p_constraint_cols + , p_automatic_maintenance + , p_jobmon + , v_template_schema||'.'||v_template_tablename + , v_inherit_privileges + , p_date_trunc_interval); + + v_last_partition_created := @extschema@.create_partition_id(p_parent_table, v_partition_id_array); + + IF v_last_partition_created = false THEN + -- This can happen with subpartitioning when future or past partitions prevent child creation because they're out of range of the parent + -- See if it's actually a subpartition of a parent id partition + WITH top_oid AS ( + SELECT i.inhparent AS top_parent_oid + FROM pg_catalog.pg_inherits i + JOIN pg_catalog.pg_class c ON c.oid = i.inhrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE c.relname = v_parent_tablename::name + AND n.nspname = v_parent_schemaname::name + ) SELECT n.nspname||'.'||c.relname + INTO v_top_parent_table + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN top_oid t ON c.oid = t.top_parent_oid + JOIN @extschema@.part_config p ON p.parent_table = n.nspname||'.'||c.relname; + + IF v_top_parent_table IS NOT NULL THEN + -- Create the lowest possible partition that is within the boundary of the parent + SELECT child_start_id INTO v_parent_partition_id FROM @extschema@.show_partition_info(p_parent_table, p_parent_table := v_top_parent_table); + IF v_starting_partition_id >= v_parent_partition_id THEN + WHILE v_starting_partition_id >= v_parent_partition_id LOOP + v_starting_partition_id := v_starting_partition_id - v_id_interval; + END LOOP; + v_starting_partition_id := v_starting_partition_id + v_id_interval; -- add one back since while loop set it one lower than is needed + ELSIF v_starting_partition_id < v_parent_partition_id THEN + WHILE v_starting_partition_id < v_parent_partition_id LOOP + v_starting_partition_id := v_starting_partition_id + v_id_interval; + END LOOP; + -- Don't need to remove one since new starting id will fit in top parent interval + END IF; + v_partition_id_array = NULL; + v_partition_id_array = array_append(v_partition_id_array, v_starting_partition_id); + v_last_partition_created := @extschema@.create_partition_id(p_parent_table, v_partition_id_array); + ELSE + -- Currently unknown edge case if code gets here + RAISE WARNING 'No child tables created. Check that all child tables did not already exist and may not have been part of partition set. Given parent has still been configured with pg_partman, but may not have expected children. Please review schema and config to confirm things are ok.'; + IF v_jobmon_schema IS NOT NULL THEN + PERFORM update_step(v_step_id, 'OK', 'Done'); + 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; + + RETURN v_success; + END IF; + END IF; -- End v_last_partition_created IF + +END IF; -- End IF id + +IF p_default_table THEN + -- Add default partition + + v_default_partition := @extschema@.check_name_length(v_parent_tablename, '_default', FALSE); + v_sql := 'CREATE'; + + -- Same INCLUDING list is used in create_partition_*(). INDEXES is handled when partition is attached if it's supported. + v_sql := v_sql || format(' TABLE IF NOT EXISTS %I.%I (LIKE %I.%I INCLUDING COMMENTS INCLUDING COMPRESSION INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING GENERATED INCLUDING STATISTICS INCLUDING STORAGE)' + , v_parent_schemaname, v_default_partition, v_parent_schemaname, v_parent_tablename); + IF v_parent_tablespace IS NOT NULL THEN + v_sql := format('%s TABLESPACE %I ', v_sql, v_parent_tablespace); + END IF; + EXECUTE v_sql; + + v_sql := format('ALTER TABLE %I.%I ATTACH PARTITION %I.%I DEFAULT' + , v_parent_schemaname, v_parent_tablename, v_parent_schemaname, v_default_partition); + EXECUTE v_sql; + + PERFORM @extschema@.inherit_replica_identity(v_parent_schemaname, v_parent_tablename, v_default_partition); + + PERFORM @extschema@.inherit_parent_properties(v_parent_schemaname, v_parent_tablename, v_default_partition); + + -- Manage template inherited properties + IF v_template_tablename IS NOT NULL THEN + PERFORM @extschema@.inherit_template_properties(p_parent_table, v_parent_schemaname, v_default_partition); + END IF; + +END IF; + + +IF v_jobmon_schema IS NOT NULL THEN + PERFORM update_step(v_step_id, 'OK', 'Done'); + 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; + +v_success := true; + +RETURN v_success; + +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(%L)', v_jobmon_schema, format('PARTMAN CREATE PARENT: %s', p_parent_table)) INTO v_job_id; + EXECUTE format('SELECT %I.add_step(%s, ''Partition creation for table '||p_parent_table||' failed'')', 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 +$$; + + +CREATE OR REPLACE FUNCTION @extschema@.create_partition_time( + p_parent_table text + , p_partition_times timestamptz[] + , p_start_partition text DEFAULT NULL +) + RETURNS boolean + LANGUAGE plpgsql + SET search_path = @extschema@, pg_catalog, pg_temp + AS $$ +DECLARE + +ex_context text; +ex_detail text; +ex_hint text; +ex_message text; +v_control text; +v_control_type text; +v_datetime_string text; +v_epoch text; +v_exists smallint; +v_inherit_privileges boolean; +v_job_id bigint; +v_jobmon boolean; +v_jobmon_schema text; +v_new_search_path text; +v_old_search_path text; +v_parent_oid oid; +v_parent_schema text; +v_parent_tablename text; +v_parent_tablespace name; +v_partition_created boolean := false; +v_partition_name text; +v_partition_suffix text; +v_partition_expression text; +v_partition_interval interval; +v_partition_timestamp_end timestamptz; +v_partition_timestamp_start timestamptz; +v_row record; +v_sql text; +v_step_id bigint; +v_step_overflow_id bigint; +v_sub_control text; +v_sub_partition_type text; +v_sub_timestamp_max timestamptz; +v_sub_timestamp_min timestamptz; +v_template_table text; +v_time timestamptz; +v_time_encoder text; +v_time_encoder_safe text; +v_partition_text_start text; +v_partition_text_end text; + +BEGIN +/* + * Function to create a child table in a time-based partition set + */ + +SELECT control + , time_encoder + , partition_interval::interval -- this shared field also used in partition_id as bigint + , epoch + , jobmon + , datetime_string + , template_table + , inherit_privileges +INTO v_control + , v_time_encoder + , v_partition_interval + , v_epoch + , v_jobmon + , v_datetime_string + , v_template_table + , v_inherit_privileges +FROM @extschema@.part_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 + , c.oid + , t.spcname +INTO v_parent_schema + , v_parent_tablename + , v_parent_oid + , 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 @extschema@.check_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 NOT IN ('text', 'id', 'uuid') OR (v_control_type IN ('text', 'uuid') AND v_time_encoder IS NULL) THEN + RAISE EXCEPTION 'Cannot run on partition set without time based control column, an epoch flag set with an id column or time_encoder set with text column. Found control: %, epoch: %, time_encoder: %s', v_control_type, v_epoch, v_time_encoder; + END IF; +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 + SELECT current_setting('search_path') INTO v_old_search_path; + IF v_jobmon_schema IS NOT NULL THEN + v_new_search_path := format('%s,%s',v_jobmon_schema, v_old_search_path); + EXECUTE format('SET LOCAL search_path TO %s', v_new_search_path); + END IF; + END IF; +END IF; + +-- 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 @extschema@.check_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 = 'microseconds' THEN format('to_timestamp((%I/1000000)::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 := @extschema@.check_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; + + 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'; + + + -- Same INCLUDING list is used in create_partition() + v_sql := v_sql || format(' TABLE %I.%I (LIKE %I.%I INCLUDING COMMENTS INCLUDING COMPRESSION INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING GENERATED INCLUDING STATISTICS INCLUDING STORAGE) ' + , v_parent_schema + , v_partition_name + , v_parent_schema + , v_parent_tablename); + + IF v_parent_tablespace IS NOT NULL THEN + v_sql := format('%s TABLESPACE %I ', v_sql, v_parent_tablespace); + END IF; + + SELECT sub_partition_type, sub_control INTO v_sub_partition_type, v_sub_control + FROM @extschema@.part_config_sub + WHERE sub_parent = p_parent_table; + IF v_sub_partition_type = 'range' THEN + v_sql := format('%s PARTITION BY RANGE (%I) ', v_sql, v_sub_control); + ELSIF v_sub_partition_type = 'list' THEN + v_sql := format('%s PARTITION BY LIST (%I) ', v_sql, v_sub_control); + END IF; + + RAISE DEBUG 'create_partition_time v_sql: %', v_sql; + EXECUTE v_sql; + + PERFORM @extschema@.inherit_parent_properties(v_parent_schema, v_parent_tablename, v_partition_name); + + IF v_template_table IS NOT NULL THEN + PERFORM @extschema@.inherit_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 built-in constraint + IF v_time_encoder IS NULL 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 + , v_partition_timestamp_start + , v_partition_timestamp_end); + ELSE + v_time_encoder_safe := partman_safe_obj_name(v_time_encoder); + + EXECUTE format('SELECT %s(%L)', v_time_encoder_safe, v_partition_timestamp_start) INTO v_partition_text_start; + EXECUTE format('SELECT %s(%L)', v_time_encoder_safe, v_partition_timestamp_end) INTO v_partition_text_end; + + 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_text_start + , v_partition_text_end); + END IF; + + ELSE + -- Must attach with integer based values for built-in 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 = 'microseconds' 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 * 1000000 + , EXTRACT('epoch' FROM v_partition_timestamp_end)::bigint * 1000000); + 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 built-in'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; + + -- NOTE: Privileges not automatically inherited. Only do so if config flag is set + IF v_inherit_privileges = TRUE THEN + PERFORM @extschema@.apply_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 and doing an IF condition + -- This column list and the update statement below must be kept consistent between: + -- create_partition, check_subpart_sameconfig, create_partition_id, create_partition_time, dump_partitioned_table_definition, and table definition + FOR v_row IN + SELECT + sub_parent + , sub_control + , sub_time_encoder + , sub_time_decoder + , sub_partition_interval + , sub_partition_type + , sub_premake + , sub_automatic_maintenance + , sub_template_table + , sub_retention + , sub_retention_schema + , sub_retention_keep_index + , sub_retention_keep_table + , sub_epoch + , sub_constraint_cols + , sub_optimize_constraint + , sub_infinite_time_partitions + , sub_jobmon + , sub_inherit_privileges + , sub_constraint_valid + , sub_date_trunc_interval + , sub_ignore_default_data + , sub_default_table + , sub_maintenance_order + , sub_retention_keep_publication + , sub_control_not_null + , sub_detach_before_drop + , sub_maintenance_role + FROM @extschema@.part_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 @extschema@.create_partition( + p_parent_table := %L + , p_control := %L + , p_time_encoder := %L + , p_time_decoder := %L + , p_interval := %L + , p_type := %L + , p_default_table := %L + , p_constraint_cols := %L + , p_premake := %L + , p_automatic_maintenance := %L + , p_epoch := %L + , p_template_table := %L + , p_jobmon := %L + , p_start_partition := %L + , p_date_trunc_interval := %L + , p_control_not_null := %L )' + , v_parent_schema||'.'||v_partition_name + , v_row.sub_control + , v_row.sub_time_encoder + , v_row.sub_time_decoder + , v_row.sub_partition_interval + , v_row.sub_partition_type + , v_row.sub_default_table + , v_row.sub_constraint_cols + , v_row.sub_premake + , v_row.sub_automatic_maintenance + , v_row.sub_epoch + , v_row.sub_template_table + , v_row.sub_jobmon + , p_start_partition + , v_row.sub_date_trunc_interval + , v_row.sub_control_not_null); + + RAISE DEBUG 'create_partition_time (create_partition loop): %', v_sql; + EXECUTE v_sql; + + UPDATE @extschema@.part_config SET + retention_schema = v_row.sub_retention_schema + , retention_keep_table = v_row.sub_retention_keep_table + , optimize_constraint = v_row.sub_optimize_constraint + , infinite_time_partitions = v_row.sub_infinite_time_partitions + , inherit_privileges = v_row.sub_inherit_privileges + , constraint_valid = v_row.sub_constraint_valid + , ignore_default_data = v_row.sub_ignore_default_data + , maintenance_order = v_row.sub_maintenance_order + , retention_keep_publication = v_row.sub_retention_keep_publication + , detach_before_drop = v_row.sub_detach_before_drop + , maintenance_role = v_row.sub_maintenance_role + WHERE parent_table = v_parent_schema||'.'||v_partition_name; + + END LOOP; -- end sub partitioning LOOP + + -- NOTE: Replication identity not automatically inherited as of PG16 (revisit in future versions) + PERFORM @extschema@.inherit_replica_identity(v_parent_schema, v_parent_tablename, v_partition_name); + + -- Manage additional constraints if set + PERFORM @extschema@.apply_constraints(p_parent_table, p_job_id := v_job_id); + + v_partition_created := true; + +END LOOP; + +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; + +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(%L)', v_jobmon_schema, format('PARTMAN CREATE TABLE: %s', 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 +$$; + + +CREATE OR REPLACE FUNCTION @extschema@.inherit_template_properties( + p_parent_table text + , p_child_schema text + , p_child_tablename text +) + RETURNS boolean + LANGUAGE plpgsql + SET search_path = @extschema@, pg_catalog, pg_temp + AS $$ +DECLARE + +v_child_relkind char; +v_child_schema text; +v_child_tablename text; +v_child_unlogged char; +v_dupe_found boolean := false; +v_index_list record; +v_parent_index_list record; +v_parent_oid oid; +v_parent_table text; +v_relopt record; +v_sql text; +v_template_oid oid; +v_template_schemaname text; +v_template_table text; +v_template_tablename name; +v_template_unlogged char; +v_toast_table_oid oid; + +BEGIN +/* + * Function to inherit the properties of the template table to newly created child tables. + * For PG14+, used to inherit non-partition-key unique indexes & primary keys, unlogged status, and relation options + */ + +SELECT parent_table, template_table +INTO v_parent_table, v_template_table +FROM @extschema@.part_config +WHERE parent_table = p_parent_table; +IF v_parent_table IS NULL THEN + RAISE EXCEPTION 'Given parent table has no configuration in pg_partman: %', p_parent_table; +ELSIF v_template_table IS NULL THEN + RAISE EXCEPTION 'No template table set in configuration for given parent table: %', p_parent_table; +END IF; + +SELECT c.oid INTO v_parent_oid +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = split_part(p_parent_table, '.', 1)::name +AND c.relname = split_part(p_parent_table, '.', 2)::name; + IF v_parent_oid IS NULL THEN + RAISE EXCEPTION 'Unable to find given parent table in system catalogs: %', p_parent_table; + END IF; + +SELECT n.nspname, c.relname, c.relkind INTO v_child_schema, v_child_tablename, v_child_relkind +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = p_child_schema::name +AND c.relname = p_child_tablename::name; + IF v_child_tablename IS NULL THEN + RAISE EXCEPTION 'Unable to find given child table in system catalogs: %.%', v_child_schema, v_child_tablename; + END IF; + +IF v_child_relkind = 'p' THEN + -- Subpartitioned parent, do not apply properties + RAISE DEBUG 'inherit_template_properties: found given child is subpartition parent, so properties not inherited'; + RETURN false; +END IF; + +v_template_schemaname := split_part(v_template_table, '.', 1)::name; +v_template_tablename := split_part(v_template_table, '.', 2)::name; + +SELECT c.oid INTO v_template_oid +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +LEFT OUTER JOIN pg_catalog.pg_tablespace ts ON c.reltablespace = ts.oid +WHERE n.nspname = v_template_schemaname +AND c.relname = v_template_tablename; + IF v_template_oid IS NULL THEN + RAISE EXCEPTION 'Unable to find configured template table in system catalogs: %', v_template_table; + END IF; + + IF NOT pg_has_role(current_user, (SELECT relowner FROM pg_class WHERE oid = v_template_oid), 'USAGE') THEN + RAISE EXCEPTION 'inherit_template_properties: caller % does not own template table %', current_user, v_template_table; + END IF; + + +-- Index creation (Only for unique, non-partition key indexes) +FOR v_index_list IN + SELECT + array_to_string(regexp_matches(pg_get_indexdef(indexrelid), ' USING .*'),',') AS statement + , i.indisprimary + , i.indisunique + , ( SELECT array_agg( a.attname ORDER by x.r ) + FROM pg_catalog.pg_attribute a + JOIN ( SELECT k, row_number() over () as r + FROM unnest(i.indkey) k ) as x + ON a.attnum = x.k AND a.attrelid = i.indrelid + WHERE x.r <= i.indnkeyatts + ) AS indkey_names + , c.relname AS index_name + , ts.spcname AS tablespace_name + FROM pg_catalog.pg_index i + JOIN pg_catalog.pg_class c ON i.indexrelid = c.oid + LEFT OUTER JOIN pg_catalog.pg_tablespace ts ON c.reltablespace = ts.oid + WHERE i.indrelid = v_template_oid + AND i.indisvalid + AND (i.indisprimary OR i.indisunique) + ORDER BY 1 +LOOP + v_dupe_found := false; + + FOR v_parent_index_list IN + SELECT + array_to_string(regexp_matches(pg_get_indexdef(indexrelid), ' USING .*'),',') AS statement + , i.indisprimary + , ( SELECT array_agg( a.attname ORDER by x.r ) + FROM pg_catalog.pg_attribute a + JOIN ( SELECT k, row_number() over () as r + FROM unnest(i.indkey) k ) as x + ON a.attnum = x.k AND a.attrelid = i.indrelid + WHERE x.r <= i.indnkeyatts + ) AS indkey_names + FROM pg_catalog.pg_index i + WHERE i.indrelid = v_parent_oid + AND i.indisvalid + ORDER BY 1 + LOOP + + IF v_parent_index_list.indisprimary AND v_index_list.indisprimary THEN + IF v_parent_index_list.indkey_names = v_index_list.indkey_names THEN + RAISE DEBUG 'inherit_template_properties: Ignoring duplicate primary key on template table: % ', v_index_list.indkey_names; + v_dupe_found := true; + CONTINUE; -- only continue within this nested loop + END IF; + END IF; + + IF v_parent_index_list.statement = v_index_list.statement THEN + RAISE DEBUG 'inherit_template_properties: Ignoring duplicate unique index on template table: %', v_index_list.statement; + v_dupe_found := true; + CONTINUE; -- only continue within this nested loop + END IF; + + END LOOP; -- end parent index loop + + IF v_dupe_found = true THEN + CONTINUE; + END IF; + + IF v_index_list.indisprimary THEN + v_sql := format('ALTER TABLE %I.%I ADD PRIMARY KEY (%s)' + , v_child_schema + , v_child_tablename + , (SELECT string_agg(quote_ident(c), ', ') + FROM unnest(v_index_list.indkey_names) AS c)); + IF v_index_list.tablespace_name IS NOT NULL THEN + v_sql := v_sql || format(' USING INDEX TABLESPACE %I', v_index_list.tablespace_name); + END IF; + RAISE DEBUG 'inherit_template_properties: Create pk: %', v_sql; + EXECUTE v_sql; + ELSIF v_index_list.indisunique THEN + -- statement column should be just the portion of the index definition that defines what it actually is + v_sql := format('CREATE UNIQUE INDEX ON %I.%I %s', v_child_schema, v_child_tablename, v_index_list.statement); + IF v_index_list.tablespace_name IS NOT NULL THEN + IF (ARRAY_length(regexp_matches(v_sql, ' WHERE ', 'i'), 1) > 0) THEN + v_sql := regexp_replace(v_sql, ' WHERE ', format(' TABLESPACE %I WHERE ', v_index_list.tablespace_name), 'i'); + ELSE + v_sql := v_sql || format(' TABLESPACE %I', v_index_list.tablespace_name); + END IF; + END IF; + + RAISE DEBUG 'inherit_template_properties: Create index: %', v_sql; + EXECUTE v_sql; + ELSE + RAISE EXCEPTION 'inherit_template_properties: Unexpected code path in unique index creation. Please report the steps that lead to this error to extension maintainers.'; + END IF; + +END LOOP; +-- End index creation + +/* +UNLOGGED status. + As of PG12, the unlogged/logged status of a parent table cannot be changed via an ALTER TABLE in order to affect its children. + As of partman v4.2x, the unlogged state will be managed via the template table. See 4.2.0 release notes. + As of PG18, the unlogged flag cannot be set on the parent table. But it can be set on the child tables. So it can continue to be supported + in pg_partman via the template table for now. +*/ + +SELECT relpersistence INTO v_template_unlogged +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = v_template_schemaname +AND c.relname = v_template_tablename; + +SELECT relpersistence INTO v_child_unlogged +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE n.nspname = v_child_schema::name +AND c.relname = v_child_tablename::name; + +IF v_template_unlogged = 'u' AND v_child_unlogged = 'p' THEN + v_sql := format ('ALTER TABLE %I.%I SET UNLOGGED', v_child_schema, v_child_tablename); + RAISE DEBUG 'inherit_template_properties: Alter UNLOGGED: %', v_sql; + EXECUTE v_sql; +ELSIF v_template_unlogged = 'p' AND v_child_unlogged = 'u' THEN + v_sql := format ('ALTER TABLE %I.%I SET LOGGED', v_child_schema, v_child_tablename); + RAISE DEBUG 'inherit_template_properties: Alter UNLOGGED: %', v_sql; + EXECUTE v_sql; +END IF; + +-- Relation options are not either not being inherited or not supported (autovac tuning) on <= PG15 +FOR v_relopt IN + SELECT unnest(reloptions) as value + FROM pg_catalog.pg_class + WHERE oid = v_template_oid +LOOP + v_sql := format('ALTER TABLE %I.%I SET (%s)' + , v_child_schema + , v_child_tablename + , v_relopt.value); + RAISE DEBUG 'inherit_template_properties: Set relopts: %', v_sql; + EXECUTE v_sql; +END LOOP; + +-- Get toast table options +SELECT reltoastrelid INTO v_toast_table_oid +FROM pg_catalog.pg_class c +JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid +WHERE c.oid = v_template_oid; + +FOR v_relopt IN + SELECT unnest(reloptions) as value + FROM pg_catalog.pg_class + WHERE oid = v_toast_table_oid +LOOP + v_sql := format('ALTER TABLE %I.%I SET (toast.%s)' + , v_child_schema + , v_child_tablename + , v_relopt.value); + RAISE DEBUG 'inherit_template_properties: Set toast relopts: %', v_sql; + EXECUTE v_sql; +END LOOP; + +RETURN true; + +END +$$; + + +-- uuid7 helpers were VOLATILE by default, which blocked partition pruning (Github Issue #873) +ALTER FUNCTION @extschema@.uuid7_time_encoder(timestamptz) IMMUTABLE PARALLEL SAFE; +ALTER FUNCTION @extschema@.uuid7_time_decoder(text) IMMUTABLE PARALLEL SAFE;
^ permalink raw [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-03 17:04  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-03 17:04 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 assigned @keithf4.

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-03 17:04  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-03 17:04 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 set milestone to "Next Patch".

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-03 17:04  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-03 17:04 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 applied label "in development".

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-04 13:39  "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: hunleyd (@hunleyd) @ 2026-11-04 13:39 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

(on CLAUDE.md:7)

this seems specific to your setup?

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883 (changes requested): Version 5.5.1
@ 2026-11-04 13:41  "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: hunleyd (@hunleyd) @ 2026-11-04 13:41 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>



^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-04 14:32  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-04 14:32 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

(on CLAUDE.md:7)

Yeah I still like to manually commit/push. :)

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-04 14:32  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-04 14:32 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 requested a review.

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883 (approved): Version 5.5.1
@ 2026-11-05 14:07  "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: hunleyd (@hunleyd) @ 2026-11-05 14:07 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>



^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-05 14:08  "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: hunleyd (@hunleyd) @ 2026-11-05 14:08 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

(on CLAUDE.md)

My point was that I don't think it belongs in the repo's CLAUDE.md but in `~/.claude/CLAUDE.md` instead so it affects you, not other contributors :)

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883 (approved): Version 5.5.1
@ 2026-11-06 15:41  "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: hunleyd (@hunleyd) @ 2026-11-06 15:41 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>



^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-17 22:02  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-17 22:02 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 closed this.

^ permalink  raw  [nested|flat] 13+ messages in thread

* Re: [pgpartman/pg_partman] PR #883: Version 5.5.1
@ 2026-11-17 22:02  "keithf4 (@keithf4)" <noreply+keithf4@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: keithf4 (@keithf4) @ 2026-11-17 22:02 UTC (permalink / raw)
  To: pgpartman/pg_partman <noreply+pgpartman-pg_partman@github.com>

@keithf4 merged this (commit e0a6c384a6c7).

^ permalink  raw  [nested|flat] 13+ messages in thread


end of thread, other threads:[~2026-11-17 22:02 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 17:04 [pgpartman/pg_partman] PR #883: Version 5.5.1 "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-03 17:04 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-03 17:04 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-03 17:04 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-04 13:39 ` "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-04 13:41 ` Re: [pgpartman/pg_partman] PR #883 (changes requested): Version 5.5.1 "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-04 14:32 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-04 14:32 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-05 14:07 ` Re: [pgpartman/pg_partman] PR #883 (approved): Version 5.5.1 "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-05 14:08 ` "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-06 15:41 ` Re: [pgpartman/pg_partman] PR #883 (approved): Version 5.5.1 "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-17 22:02 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>
2026-11-17 22:02 ` "keithf4 (@keithf4)" <noreply+keithf4@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox