From cb6438158b3d184249d2e6a353ed07438b87ab48 Mon Sep 17 00:00:00 2001 From: Ritwik Puri Date: Wed, 27 Jul 2022 22:46:56 +0530 Subject: [PATCH] fix: don't use cache for sequence in mariadb (#17640) * fix: don't use cache for sequence in mariadb * chore: update sequence related comments --- frappe/database/database.py | 17 +++++++++++++++++ frappe/database/mariadb/database.py | 9 --------- frappe/database/mariadb/schema.py | 2 +- frappe/database/postgres/database.py | 6 ------ 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index 68286507ba..c55704eb64 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -54,6 +54,23 @@ class Database: CHILD_TABLE_COLUMNS = ("parent", "parenttype", "parentfield") MAX_WRITES_PER_TRANSACTION = 200_000 + # NOTE: + # FOR MARIADB - using no cache - as during backup, if the sequence was used in anyform, + # it drops the cache and uses the next non cached value in setval query and + # puts that in the backup file, which will start the counter + # from that value when inserting any new record in the doctype. + # By default the cache is 1000 which will mess up the sequence when + # using the system after a restore. + # + # Another case could be if the cached values expire then also there is a chance of + # the cache being skipped. + # + # FOR POSTGRES - The sequence cache for postgres is per connection. + # Since we're opening and closing connections for every request this results in skipping the cache + # to the next non-cached value hence not using cache in postgres. + # ref: https://stackoverflow.com/questions/21356375/postgres-9-0-4-sequence-skipping-numbers + SEQUENCE_CACHE = 0 + class InvalidColumnName(frappe.ValidationError): pass diff --git a/frappe/database/mariadb/database.py b/frappe/database/mariadb/database.py index 5e6d62f842..303098049a 100644 --- a/frappe/database/mariadb/database.py +++ b/frappe/database/mariadb/database.py @@ -129,15 +129,6 @@ class MariaDBConnectionUtil: class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database): REGEX_CHARACTER = "regexp" - - # NOTE: using a very small cache - as during backup, if the sequence was used in anyform, - # it drops the cache and uses the next non cached value in setval query and - # puts that in the backup file, which will start the counter - # from that value when inserting any new record in the doctype. - # By default the cache is 1000 which will mess up the sequence when - # using the system after a restore. - # issue link: https://jira.mariadb.org/browse/MDEV-21786 - SEQUENCE_CACHE = 50 CONVERSION_MAP = conversions | { FIELD_TYPE.NEWDECIMAL: float, FIELD_TYPE.DATETIME: get_datetime, diff --git a/frappe/database/mariadb/schema.py b/frappe/database/mariadb/schema.py index 24a78012e1..99297fbab2 100644 --- a/frappe/database/mariadb/schema.py +++ b/frappe/database/mariadb/schema.py @@ -44,7 +44,7 @@ class MariaDBTable(DBTable): # NOTE: not used nextval func as default as the ability to restore # database with sequences has bugs in mariadb and gives a scary error. - # issue link: https://jira.mariadb.org/browse/MDEV-21786 + # issue link: https://jira.mariadb.org/browse/MDEV-20070 name_column = "name bigint primary key" # create table diff --git a/frappe/database/postgres/database.py b/frappe/database/postgres/database.py index 58b63e6547..cb566736ad 100644 --- a/frappe/database/postgres/database.py +++ b/frappe/database/postgres/database.py @@ -102,12 +102,6 @@ class PostgresExceptionUtil: class PostgresDatabase(PostgresExceptionUtil, Database): REGEX_CHARACTER = "~" - - # NOTE; The sequence cache for postgres is per connection. - # Since we're opening and closing connections for every transaction this results in skipping the cache - # to the next non-cached value hence not using cache in postgres. - # ref: https://stackoverflow.com/questions/21356375/postgres-9-0-4-sequence-skipping-numbers - SEQUENCE_CACHE = 0 default_port = "5432" def setup_type_map(self):