You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this article you learn how to optimize Azure Database for PostgreSQL Flexible Server by using pg_partman. When tables in the database get large, it's hard to manage how often they're vacuumed, how much space they take up, and how to keep their indexes efficient. This can make queries slower and affect performance. Partitioning of large tables is a solution for these situations. In this article, you find out how to use pg_partman extension to create range-based partitions of tables in your Azure Database for PostgreSQL Flexible Server.
18
+
In this article, you learn how to optimize the Azure Database for PostgreSQL Flexible Server using pg_partman. When tables in the database get large, it's hard to manage how often they're vacuumed, how much space they take up, and how to keep their indexes efficient. This can make queries slower and affect performance. Partitioning of large tables is a solution for these situations. In this article, you find out how to use pg_partman extension to create range-based partitions of tables in your Azure Database for PostgreSQL Flexible Server.
19
19
20
20
## Prerequisites
21
21
22
-
To enable pg_partman extension, follow these steps.
22
+
To enable the pg_partman extension, follow these steps.
23
23
24
-
- Add pg_partman extension under Azure extensions as shown from server parameters on the portal.
24
+
- Add the pg_partman extension under Azure extensions as shown by the server parameters on the portal.
25
25
26
-
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-prerequisites.png" alt-text="Screenshot of prerequisites.":::
26
+
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-prerequisites.png" alt-text="Screenshot of prerequisites to get started.":::
27
27
28
28
```sql
29
29
CREATE EXTENSION pg_partman;
30
30
```
31
31
32
-
- There's another extension related to `pg_partman` called `pg_partman_bgw`, which must be included in Shared_Preload_Libraries. It offers a scheduled function run_maintenance(). It takes care of the partition sets that have automatic_maintenance turned ON in `part_config`.
32
+
- There's another extension related to `pg_partman` called `pg_partman_bgw`, which must be included in Shared_Preload_Libraries. It offers a scheduled function run_maintenance(). It takes care of the partition sets that have `automatic_maintenance` turned ON in `part_config`.
33
33
34
34
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-prerequisites-outlined.png" alt-text="Screenshot of prerequisites highlighted.":::
35
35
@@ -46,12 +46,13 @@ To enable pg_partman extension, follow these steps.
46
46
`pg_partman_bgw.jobmon` - Same purpose as the p_jobmon argument to run_maintenance(). By default, it's set to ON.
47
47
48
48
> [!NOTE]
49
-
>1. When an identity feature uses sequences, the data that comes from the parent table gets new sequence value. It doesn't generate new sequence values when the data is directly added to the child table.
50
-
> 2. `pg_partman` uses a template to control whether the table is UNLOGGED or not. This means that the Alter table command can't change this status for a partition set. By changing the status on the template, you can apply it to all future partitions. But for existing child tables, you must use the Alter command manually. [Here](https://www.postgresql.org/message-id/flat/15954-b61523bed4b110c4%40postgresql.org) is a bug that shows why.
49
+
>1. When an identity feature uses sequences, the data from the parent table gets new sequence value. It doesn't generate new sequence values when the data is directly added to the child table.
50
+
>
51
+
> 1. `pg_partman` uses a template to control whether the table is UNLOGGED. This means the Alter table command can't change this status for a partition set. By changing the status on the template, you can apply it to all future partitions. But for existing child tables, you must use the Alter command manually. [Here](https://www.postgresql.org/message-id/flat/15954-b61523bed4b110c4%40postgresql.org) is a bug that shows why.
51
52
52
53
## Permissions
53
54
54
-
Super user role is not required with `pg_partman`. The only requirement is that the role that runs `pg_partman` functions has ownership over all the partition sets/schema where new objects will be created. It's recommended to create a separate role for `pg_partman` and give it ownership over the schema/all the objects that `pg_partman` will operate on.
55
+
Super user role isn't required with `pg_partman`. The only requirement is that the role that runs `pg_partman` functions has ownership over all the partition sets/schema where new objects will be created. It's recommended to create a separate role for `pg_partman`and give it ownership over the schema/all the objects that `pg_partman` will operate on.
55
56
56
57
```sql
57
58
CREATE ROLE partman_role WITH LOGIN;
@@ -61,11 +62,11 @@ GRANT ALL ON ALL TABLES IN SCHEMA partman TO partman_role;
61
62
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA partman TO partman_role;
62
63
GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA partman TO partman_role;
63
64
GRANT ALL ON SCHEMA <partition_schema> TO partman_role;
64
-
GRANT TEMPORARY ON DATABASE <databasename> to partman_role; -- this allows creation of temporary table to move data.
65
+
GRANT TEMPORARY ON DATABASE <databasename> to partman_role; -- this allows temporary table creation to move data.
65
66
```
66
67
## Creating partitions
67
68
68
-
Range type partitions are supported in `pg_partman`, not trigger-based partitions. The below code shows how `pg_partman` assists with the partitioning of a table.
69
+
`pg_partman` supports range-type partitions only, not trigger-based partitions. The code below shows how `pg_partman` assists with partitioning a table.
69
70
70
71
```sql
71
72
CREATE SCHEMA partman;
@@ -100,7 +101,7 @@ This command divides the `p_parent_table` into smaller parts based on the `p_con
100
101
101
102
The `create_parent()` function populates two tables `part_config` and `part_config_sub`. There's a maintenance function `run_maintenance()`. You can schedule a cron job for this procedure to run on a periodic basis. This function checks all parent tables in`part_config` table and creates new partitions for them or runs the tables set retention policy. To know more about the functions and tables in`pg_partman` go through the [PostgreSQL Partition Manager Extension](https://github.com/pgpartman/pg_partman/blob/master/doc/pg_partman.md) article.
102
103
103
-
To create new partitions every time the `run_maintenance()` is run in the background using `pg_partman_bgw` extension, run the below `update` statement.
104
+
To create new partitions every time the `run_maintenance()` is run in the background using the `pg_partman_bgw` extension, run the `update` statement below.
104
105
105
106
```sql
106
107
UPDATE partman.part_config SET premake = premake+1 WHERE parent_table = 'partman.partition_test';
> If you insert data before creating partitions, the data goes to the default partition. If the default partition has data that belongs to a new partition that you want to be created later, then you get a default partition violation error and the procedure won't work. Therefore, change the premake value as recommended above and then run the procedure.
157
+
> If you insert data before creating partitions, the data goes to the default partition. If the default partition has data belonging to a new partition that you want to be created later, you get a default partition violation error and the procedure won't work. Therefore, change the premake value recommended above and then run the procedure.
157
158
158
159
## How to schedule maintenance procedure using pg_cron
159
160
160
161
Run the maintenance procedure using `pg_cron`. To enable `pg_cron`on your server follow the below steps.
161
-
1. Add pg_cron to `azure.extensions`, `Shared_preload_libraries` and `cron.database_name` server parameter from Azure portal.
162
+
1. Add pg_cron to `azure.extensions`, `shared_preload_libraries`,and`cron.database_name` server parametersfrom the Azure portal.
162
163
163
164
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-pgcron-prerequisites.png" alt-text="Screenshot of pgcron extension prerequisites.":::
164
165
165
166
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-pgcron-prerequisites-2.png" alt-text="Screenshot of pgcron extension prerequisites2.":::
166
167
167
168
:::image type="content" source="media/how-to-use-pg-partman/pg-partman-pgcron-database-name.png" alt-text="Screenshot of pgcron extension databasename.":::
168
169
169
-
2. Hit Save button and let the deployment complete.
170
+
2. Select the **Save** button and let the deployment complete.
170
171
171
-
3. Once done the pg_cron is automatically created. If you still, try to install then you get the below message.
172
+
3. Once done, the pg_cron is automatically created. If you still try to install it, you'll get the message below.
172
173
173
174
```sql
174
175
CREATE EXTENSION pg_cron;
@@ -204,13 +205,13 @@ Run the maintenance procedure using `pg_cron`. To enable `pg_cron` on your serve
204
205
jobname | sample_job
205
206
```
206
207
207
-
6. Run history of the job can be checked using the command below.
208
+
6. The job's run history can be checked using the command below.
208
209
209
210
```sql
210
211
SELECT * FROM cron.job_run_details;
211
212
```
212
213
213
-
Currently the results show 0 records as the job has not run yet.
214
+
The results show 0 records as the job hasn't yet been run.
214
215
215
216
7. To unschedule the cron job, use the command below.
216
217
@@ -220,17 +221,17 @@ Run the maintenance procedure using `pg_cron`. To enable `pg_cron` on your serve
220
221
221
222
## Frequently Asked Questions
222
223
223
-
- Why is my `pg_partman_bgw` not running the maintenance proc based on the interval provided.
224
+
- Why is my `pg_partman_bgw` not running the maintenance proc based on the interval provided?
224
225
225
-
Check the server parameter `pg_partman_bgw.dbname` and update it with the proper databasename. Also, check the server parameter `pg_partman_bgw.role` and provide the appropriate role with the role. You should also make sure you connecting to server using the same user to create the extension instead of postgres.
226
+
Check the server parameter `pg_partman_bgw.dbname` and update it with the proper databasename. Also, check the server parameter `pg_partman_bgw.role` and provide the appropriate role with the role. You should also make sure you connect to the server using the same user to create the extension instead of Postgres.
226
227
227
228
- I'm encountering an error when my `pg_partman_bgw` is running the maintenance proc. What could be the reasons?
228
229
229
230
Same as above.
230
231
231
232
- How to set the partitions to start from the previous day.
232
233
233
-
`p_start_partition`in which we mention the previous datefrom which the partition needs to be created.
234
+
`p_start_partition`refers to the datefrom which the partition must be created.
0 commit comments