Skip to content

Commit 27d3824

Browse files
committed
[18.0][ADD] database_autovacuum_tuning
1 parent 909ef5d commit 27d3824

18 files changed

Lines changed: 908 additions & 0 deletions
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
==========================
2+
Database Autovacuum Tuning
3+
==========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:162a7723c383e0b6239b46e50d6884767430de1a22f1c42a6c90d80e25b1b4fd
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
20+
:target: https://github.com/OCA/server-tools/tree/18.0/database_autovacuum_tuning
21+
:alt: OCA/server-tools
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-database_autovacuum_tuning
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=18.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
Database Autovacuum Tuning helps administrators keep PostgreSQL healthy
32+
by exposing recommended autovacuum settings in Odoo. It provides
33+
guidance and documentation for sizing thresholds and scale factors so
34+
large, busy databases avoid table bloat and excessive vacuum lag. Use it
35+
to standardize autovacuum configuration across environments and speed up
36+
maintenance operations without manual tuning.
37+
38+
This module is mostly useful for PostgreSQL <= 17. PostgreSQL 18.0
39+
introduces the ``autovacuum_vacuum_max_threshold`` parameter, which
40+
already provides the capability this module targets.
41+
42+
The ``pgstattuple`` extension must be installed on the database.
43+
44+
**Table of contents**
45+
46+
.. contents::
47+
:local:
48+
49+
Usage
50+
=====
51+
52+
1. Install the module on the database you want to tune.
53+
54+
2. Ensure the ``pgstattuple`` extension is installed in PostgreSQL.
55+
56+
3. Go to Settings > Technical > Database Structure > Database Autovacuum
57+
Tuning and review the recommended thresholds and scale factors.
58+
59+
4. If needed, override the defaults using the following system
60+
parameters:
61+
62+
- ``database_autovacuum_tuning.autovacuum_vacuum_max_threshold``
63+
- ``database_autovacuum_tuning.autovacuum_vacuum_analyze_max_threshold``
64+
65+
5. The configuration parameters are applied to tables by the daily cron
66+
job. When the number of dead tuples in a table exceeds the vacuum
67+
threshold, it applies the following configuration:
68+
69+
.. code:: sql
70+
71+
ALTER TABLE {schemaname}.{tablename} SET (
72+
autovacuum_vacuum_scale_factor = 0,
73+
autovacuum_vacuum_threshold = %s,
74+
autovacuum_analyze_scale_factor = 0,
75+
autovacuum_analyze_threshold = %s
76+
)
77+
78+
6. Monitor vacuum activity and table bloat, then adjust the settings if
79+
your workload changes.
80+
81+
Bug Tracker
82+
===========
83+
84+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
85+
In case of trouble, please check there if your issue has already been reported.
86+
If you spotted it first, help us to smash it by providing a detailed and welcomed
87+
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20database_autovacuum_tuning%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
88+
89+
Do not contact contributors directly about support or help with technical issues.
90+
91+
Credits
92+
=======
93+
94+
Authors
95+
-------
96+
97+
* Camptocamp
98+
99+
Contributors
100+
------------
101+
102+
- Telmo Santos <telmo.santos@camptocamp.com>
103+
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
104+
105+
Maintainers
106+
-----------
107+
108+
This module is maintained by the OCA.
109+
110+
.. image:: https://odoo-community.org/logo.png
111+
:alt: Odoo Community Association
112+
:target: https://odoo-community.org
113+
114+
OCA, or the Odoo Community Association, is a nonprofit organization whose
115+
mission is to support the collaborative development of Odoo features and
116+
promote its widespread use.
117+
118+
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/18.0/database_autovacuum_tuning>`_ project on GitHub.
119+
120+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2026 Camptocamp (https://www.camptocamp.com).
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
3+
4+
{
5+
"name": "Database Autovacuum Tuning",
6+
"summary": "Scheduled checks for Odoo autovacuum thresholds and scale factors",
7+
"version": "18.0.1.0.1",
8+
"author": "Camptocamp, Odoo Community Association (OCA)",
9+
"website": "https://github.com/OCA/server-tools",
10+
"category": "Tools",
11+
"depends": [
12+
"base_setup",
13+
],
14+
"data": [
15+
"data/config_parameter.xml",
16+
"data/ir_cron.xml",
17+
"security/ir.model.access.csv",
18+
"views/res_config_settings_views.xml",
19+
"views/database_autovacuum_tuning_views.xml",
20+
],
21+
"license": "LGPL-3",
22+
"installable": True,
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record
4+
id="config_autovacuum_vacuum_max_threshold"
5+
model="ir.config_parameter"
6+
>
7+
<field
8+
name="key"
9+
>database_autovacuum_tuning.autovacuum_vacuum_max_threshold</field>
10+
<field name="value">100000</field>
11+
</record>
12+
<record
13+
id="config_autovacuum_vacuum_analyze_max_threshold"
14+
model="ir.config_parameter"
15+
>
16+
<field
17+
name="key"
18+
>database_autovacuum_tuning.autovacuum_vacuum_analyze_max_threshold</field>
19+
<field name="value">50000</field>
20+
</record>
21+
</odoo>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="cron_database_autovacuum_tuning" model="ir.cron">
4+
<field name="name">Database Autovacuum Tuning</field>
5+
<field name="model_id" ref="model_database_autovacuum_tuning" />
6+
<field name="state">code</field>
7+
<field name="code">model._tune()</field>
8+
<field name="interval_number">1</field>
9+
<field name="interval_type">days</field>
10+
<field name="active">True</field>
11+
<field name="user_id" ref="base.user_root" />
12+
</record>
13+
</odoo>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import database_autovacuum_tuning
2+
from . import res_config_settings
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2026 Camptocamp (https://www.camptocamp.com).
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
3+
4+
5+
from odoo import api, fields, models
6+
7+
8+
class DatabaseAutovacuumTuning(models.Model):
9+
_name = "database.autovacuum.tuning"
10+
_description = "Database Autovacuum Tuning"
11+
12+
name = fields.Char(required=True, help="Table name")
13+
vacuum_threshold = fields.Integer()
14+
analyze_threshold = fields.Integer()
15+
16+
@api.model
17+
def _tune(self):
18+
vacuum_threshold = int(
19+
self.env["ir.config_parameter"]
20+
.sudo()
21+
.get_param(
22+
"database_autovacuum_tuning.autovacuum_vacuum_max_threshold",
23+
default="100000",
24+
)
25+
or 100000
26+
)
27+
if vacuum_threshold <= 0:
28+
return
29+
analyze_threshold = int(
30+
self.env["ir.config_parameter"]
31+
.sudo()
32+
.get_param(
33+
"database_autovacuum_tuning.autovacuum_vacuum_analyze_max_threshold",
34+
default="50000",
35+
)
36+
or 50000
37+
)
38+
query = """
39+
SELECT
40+
t.schemaname,
41+
t.tablename,
42+
st.dead_tuple_count
43+
FROM pg_tables AS t
44+
JOIN LATERAL pgstattuple(
45+
format('%%I.%%I', t.schemaname, t.tablename)::regclass
46+
) AS st ON true
47+
WHERE t.tableowner = current_user
48+
AND t.schemaname = 'public'
49+
AND st.dead_tuple_count > %s
50+
ORDER BY t.schemaname, t.tablename
51+
"""
52+
self.env.cr.execute(query, (vacuum_threshold,))
53+
results = self.env.cr.fetchall()
54+
for schemaname, tablename, _ in results:
55+
self.env.cr.execute(
56+
f"""
57+
ALTER TABLE {schemaname}.{tablename} SET (
58+
autovacuum_vacuum_scale_factor = 0,
59+
autovacuum_vacuum_threshold = %s,
60+
autovacuum_analyze_scale_factor = 0,
61+
autovacuum_analyze_threshold = %s
62+
)
63+
""",
64+
(vacuum_threshold, analyze_threshold),
65+
)
66+
self.sudo().create(
67+
{
68+
"name": f"{schemaname}.{tablename}",
69+
"vacuum_threshold": vacuum_threshold,
70+
"analyze_threshold": analyze_threshold,
71+
}
72+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2026 Camptocamp (https://www.camptocamp.com).
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
3+
from odoo import fields, models
4+
5+
6+
class ResConfigSettings(models.TransientModel):
7+
_inherit = "res.config.settings"
8+
9+
autovacuum_vacuum_max_threshold = fields.Integer(
10+
config_parameter="database_autovacuum_tuning.autovacuum_vacuum_max_threshold",
11+
)
12+
autovacuum_vacuum_analyze_max_threshold = fields.Integer(
13+
config_parameter="database_autovacuum_tuning.autovacuum_vacuum_analyze_max_threshold",
14+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Telmo Santos \<<telmo.santos@camptocamp.com>\>
2+
- Alexandre Fayolle \<<alexandre.fayolle@camptocamp.com>\>

0 commit comments

Comments
 (0)