diff --git a/docs/en_US/images/preferences_sql_auto_completion.png b/docs/en_US/images/preferences_sql_auto_completion.png
old mode 100755
new mode 100644
index 19239f954d3f53198846b6b0b41684363153d10f..484c9096b6d8d75e82e9e538b8d271fd84396c0a
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_csv_output.png b/docs/en_US/images/preferences_sql_csv_output.png
old mode 100755
new mode 100644
index 10fd1454b325149a7450fae0c7b1456dd3a7dc3a..78247c14c39545cb8a3def9d93a63e93b39d191b
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_display.png b/docs/en_US/images/preferences_sql_display.png
old mode 100755
new mode 100644
index 4d48840f828d4c5964a3b2f02b423c99dd17f311..0d6d497697c4518b841842e9dd80e3ee0fa8d088
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_editor.png b/docs/en_US/images/preferences_sql_editor.png
new file mode 100644
index 0000000000000000000000000000000000000000..b7f43b5f869851995a88177b529bb68423cb9bf2
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_explain.png b/docs/en_US/images/preferences_sql_explain.png
old mode 100755
new mode 100644
index c1b667a894aa73308c922379f09cee45856f0b5e..5ee134cc9d29c168470bb026e53e2776a40b20a9
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_keyboard_shortcuts.png b/docs/en_US/images/preferences_sql_keyboard_shortcuts.png
old mode 100755
new mode 100644
index e66806bd4e993cecd2c87e312ac0c9b9cc7c9f4b..c8a2c0beffe5ed4ffc423a4f9dac407b69b8d2e0
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_options.png b/docs/en_US/images/preferences_sql_options.png
old mode 100755
new mode 100644
index ebc072c0dfa490497752ac008b6037cc29f793de..c1fce1ccaef9e59dd0871afe979fb8e0543bd95b
GIT binary patch
diff --git a/docs/en_US/images/preferences_sql_results_grid.png b/docs/en_US/images/preferences_sql_results_grid.png
old mode 100755
new mode 100644
index cf0abbe14f49b88172de78400c432510b9a0e6cd..35a37b31aa061acd890cc1f87351b43e02318157
GIT binary patch
diff --git a/docs/en_US/preferences.rst b/docs/en_US/preferences.rst
index ab00a45d..6ffef2bb 100644
--- a/docs/en_US/preferences.rst
+++ b/docs/en_US/preferences.rst
@@ -268,6 +268,37 @@ Tool display.
   a positive value above zero is specified, the notifier will be displayed for
   the specified number of seconds. The default is *5*.
 
+.. image:: images/preferences_sql_editor.png
+    :alt: Preferences dialog sqleditor editor settings
+    :align: center
+
+Use the fields on the *Editor* panel to change settings of the query editor
+
+* When the *Brace matching?* switch is set to *True*, the editor will highlight
+  pairs of matched braces.
+
+* When the *Code folding?* switch is set to *False*, the editor will disable
+  code folding. Disabling will improve editor performance with large files.
+
+* Use the *Font size* field to specify the font size that will be used in text
+  boxes and editors.
+
+* When the *Insert bracket pairs?* switch is set to *True*, the editor will
+  automatically insert paired brackets.
+
+* When the *Line wrapping* switch is set to *True*, the editor will implement
+  line-wrapping behavior.
+
+* When the *Plain text mode?* switch is set to *True*, the editor mode will be
+  changed to text/plain. Keyword highlighting and code folding will be disabled.
+  This will improve editor performance with large files.
+
+* Use the *Tab size* field to specify the number of spaces per tab character in
+  the editor.
+
+* When the *Use spaces* switch is set to *True*, the editor will insert spaces
+  (instead of tab characters) when the tab key or auto-indent are used.
+
 .. image:: images/preferences_sql_explain.png
     :alt: Preferences dialog sqleditor explain options
     :align: center
@@ -300,18 +331,6 @@ Use the fields on the *Options* panel to manage editor preferences.
 * When the *Auto-Rollback?* switch is set to *True*, failed queries are rolled
   back.
 
-* When the *Brace matching?* switch is set to *True*, the editor will highlight
-  pairs of matched braces.
-
-* Use the *Font size* field to specify the font size that will be used in text
-  boxes and editors.
-
-* When the *Insert bracket pairs?* switch is set to *True*, the editor will
-  automatically insert paired brackets.
-
-* When the *Line wrapping* switch is set to *True*, the editor will implement
-  line-wrapping behavior.
-
 * When the *Prompt to save unsaved data changes?* switch is set to *True*, the
   editor will prompt the user to saved unsaved data when exiting the data
   editor.
@@ -324,12 +343,6 @@ Use the fields on the *Options* panel to manage editor preferences.
   *True*, the editor will prompt the user to commit or rollback changes when
   exiting the Query Tool while the current transaction is not committed.
 
-* Use the *Tab size* field to specify the number of spaces per tab character in
-  the editor.
-
-* When the *Use spaces* switch is set to *True*, the editor will insert spaces
-  (instead of tab characters) when the tab key or auto-indent are used.
-
 .. image:: images/preferences_sql_results_grid.png
     :alt: Preferences dialog sql results grid section
     :align: center
diff --git a/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js b/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js
index 595c7dc7..b5a3520c 100644
--- a/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js
+++ b/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js
@@ -17,21 +17,30 @@
 })(function(CodeMirror) {
   'use strict';
 
-  CodeMirror.pgadminKeywordRangeFinder = function(cm, start, startTkn, endTkn) {
+  var pgadminKeywordRangeFinder = function(cm, start, tokenSet) {
     var line = start.line,
       lineText = cm.getLine(line);
     var at = lineText.length,
       startChar, tokenType;
+
+    let tokenSetNo = 0;
+    let startTkn = tokenSet[tokenSetNo].start,
+      endTkn = tokenSet[tokenSetNo].end;
     for (; at > 0;) {
       var found = lineText.lastIndexOf(startTkn, at);
       var startToken = startTkn;
       var endToken = endTkn;
 
       if (found < start.ch) {
-        found = lineText.lastIndexOf('[', at);
-        if (found < start.ch) break;
-        startToken = '[';
-        endToken = ']';
+        /* If the start token is not found then search for the next set of token */
+        tokenSetNo++;
+        if(tokenSetNo >= tokenSet.length) {
+          return undefined;
+        }
+        startTkn = tokenSet[tokenSetNo].start;
+        endTkn = tokenSet[tokenSetNo].end;
+        at = lineText.length;
+        continue;
       }
 
       tokenType = cm.getTokenAt(CodeMirror.Pos(line, found + 1)).type;
@@ -73,32 +82,13 @@
     };
   };
 
-  CodeMirror.pgadminBeginRangeFinder = function(cm, start) {
-    var startToken = 'BEGIN';
-    var endToken = 'END;';
-    var fromToPos = CodeMirror.pgadminKeywordRangeFinder(cm, start, startToken, endToken);
+  CodeMirror.registerHelper('fold', 'sql', function(cm, start) {
+    var fromToPos = pgadminKeywordRangeFinder(cm, start, [
+      {start: 'BEGIN', end:'END;'},
+      {start: 'IF', end:'END IF'},
+      {start: 'LOOP', end:'END LOOP'},
+      {start: 'CASE', end:'END CASE'},
+    ]);
     return fromToPos;
-  };
-
-  CodeMirror.pgadminIfRangeFinder = function(cm, start) {
-    var startToken = 'IF';
-    var endToken = 'END IF';
-    var fromToPos = CodeMirror.pgadminKeywordRangeFinder(cm, start, startToken, endToken);
-    return fromToPos;
-  };
-
-  CodeMirror.pgadminLoopRangeFinder = function(cm, start) {
-    var startToken = 'LOOP';
-    var endToken = 'END LOOP';
-    var fromToPos = CodeMirror.pgadminKeywordRangeFinder(cm, start, startToken, endToken);
-    return fromToPos;
-  };
-
-  CodeMirror.pgadminCaseRangeFinder = function(cm, start) {
-    var startToken = 'CASE';
-    var endToken = 'END CASE';
-    var fromToPos = CodeMirror.pgadminKeywordRangeFinder(cm, start, startToken, endToken);
-    return fromToPos;
-  };
-
+  });
 });
diff --git a/web/pgadmin/static/js/sqleditor/query_tool_preferences.js b/web/pgadmin/static/js/sqleditor/query_tool_preferences.js
index 491f0ca9..8e74e65d 100644
--- a/web/pgadmin/static/js/sqleditor/query_tool_preferences.js
+++ b/web/pgadmin/static/js/sqleditor/query_tool_preferences.js
@@ -182,6 +182,15 @@ function updateUIPreferences(sqlEditor) {
   let sql_font_size = SqlEditorUtils.calcFontSize(preferences.sql_font_size);
   $(sqlEditor.query_tool_obj.getWrapperElement()).css('font-size', sql_font_size);
 
+  if(preferences.plain_editor_mode) {
+    sqlEditor.query_tool_obj.setOption('mode', 'text/plain');
+    /* Although not required, setting explicitly as codemirror will remove code folding only on next edit */
+    sqlEditor.query_tool_obj.setOption('foldGutter', false);
+  } else {
+    sqlEditor.query_tool_obj.setOption('mode', sqlEditor.handler.server_type === 'gpdb' ? 'text/x-gpsql' : 'text/x-pgsql');
+    sqlEditor.query_tool_obj.setOption('foldGutter', preferences.code_folding);
+  }
+  sqlEditor.query_tool_obj.setOption('foldGutter', preferences.code_folding);
   sqlEditor.query_tool_obj.setOption('indentWithTabs', !preferences.use_spaces);
   sqlEditor.query_tool_obj.setOption('indentUnit', preferences.tab_size);
   sqlEditor.query_tool_obj.setOption('tabSize', preferences.tab_size);
diff --git a/web/pgadmin/tools/debugger/static/js/direct.js b/web/pgadmin/tools/debugger/static/js/direct.js
index 123b9cf1..b7ee19c8 100644
--- a/web/pgadmin/tools/debugger/static/js/direct.js
+++ b/web/pgadmin/tools/debugger/static/js/direct.js
@@ -1792,14 +1792,7 @@ define([
           foldOptions: {
             widget: '\u2026',
           },
-          foldGutter: {
-            rangeFinder: CodeMirror.fold.combine(
-              CodeMirror.pgadminBeginRangeFinder,
-              CodeMirror.pgadminIfRangeFinder,
-              CodeMirror.pgadminLoopRangeFinder,
-              CodeMirror.pgadminCaseRangeFinder
-            ),
-          },
+          foldGutter: true,
           gutters: [
             'CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'breakpoints',
           ],
diff --git a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
index 905f8422..f6fbadf3 100644
--- a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
@@ -356,14 +356,7 @@ define('tools.querytool', [
         foldOptions: {
           widget: '\u2026',
         },
-        foldGutter: {
-          rangeFinder: CodeMirror.fold.combine(
-            CodeMirror.pgadminBeginRangeFinder,
-            CodeMirror.pgadminIfRangeFinder,
-            CodeMirror.pgadminLoopRangeFinder,
-            CodeMirror.pgadminCaseRangeFinder
-          ),
-        },
+        foldGutter: true,
         gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
         extraKeys: pgBrowser.editor_shortcut_keys,
         scrollbarStyle: 'simple',
diff --git a/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py b/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py
index 12e5f363..079f9e72 100644
--- a/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py
+++ b/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py
@@ -90,8 +90,61 @@ def RegisterQueryToolPreferences(self):
                          'Tool tabs.')
     )
 
+    self.show_prompt_save_query_changes = self.preference.register(
+        'Options', 'prompt_save_query_changes',
+        gettext("Prompt to save unsaved query changes?"), 'boolean', True,
+        category_label=gettext('Options'),
+        help_str=gettext(
+            'Specifies whether or not to prompt user to save unsaved '
+            'query on query tool exit.'
+        )
+    )
+
+    self.show_prompt_save_data_changes = self.preference.register(
+        'Options', 'prompt_save_data_changes',
+        gettext("Prompt to save unsaved data changes?"), 'boolean', True,
+        category_label=gettext('Options'),
+        help_str=gettext(
+            'Specifies whether or not to prompt user to save unsaved '
+            'data on data grid exit.'
+        )
+    )
+
+    self.show_prompt_commit_transaction = self.preference.register(
+        'Options', 'prompt_commit_transaction',
+        gettext("Prompt to commit/rollback active transactions?"), 'boolean',
+        True,
+        category_label=gettext('Options'),
+        help_str=gettext(
+            'Specifies whether or not to prompt user to commit or rollback '
+            'an active transaction on Query Tool exit.'
+        )
+    )
+
+    self.sql_font_size = self.preference.register(
+        'Editor', 'plain_editor_mode',
+        gettext("Plain text mode?"), 'boolean', False,
+        category_label=gettext('Editor'),
+        help_str=gettext(
+            'When set to True, keywords won''t be highlighted and code '
+            'folding will be disabled. Plain text mode will improve editor '
+            'performance with large files.'
+        )
+    )
+
     self.sql_font_size = self.preference.register(
-        'Options', 'sql_font_size',
+        'Editor', 'code_folding',
+        gettext("Code folding?"), 'boolean', True,
+        category_label=gettext('Editor'),
+        help_str=gettext(
+            'Enable or disable code folding. In plain text mode, this will '
+            'have no effect as code folding is always disabled in that mode. '
+            'Disabling will improve editor performance with large files.'
+        )
+    )
+
+    self.sql_font_size = self.preference.register(
+        'Editor', 'sql_font_size',
         gettext("Font size"), 'numeric', '1',
         min_val=0.1,
         max_val=10,
@@ -106,7 +159,7 @@ def RegisterQueryToolPreferences(self):
     )
 
     self.tab_size = self.preference.register(
-        'Options', 'tab_size',
+        'Editor', 'tab_size',
         gettext("Tab size"), 'integer', 4,
         min_val=2,
         max_val=8,
@@ -117,7 +170,7 @@ def RegisterQueryToolPreferences(self):
     )
 
     self.use_spaces = self.preference.register(
-        'Options', 'use_spaces',
+        'Editor', 'use_spaces',
         gettext("Use spaces?"), 'boolean', False,
         category_label=gettext('Options'),
         help_str=gettext(
@@ -127,7 +180,7 @@ def RegisterQueryToolPreferences(self):
     )
 
     self.wrap_code = self.preference.register(
-        'Options', 'wrap_code',
+        'Editor', 'wrap_code',
         gettext("Line wrapping?"), 'boolean', False,
         category_label=gettext('Options'),
         help_str=gettext(
@@ -136,7 +189,7 @@ def RegisterQueryToolPreferences(self):
     )
 
     self.insert_pair_brackets = self.preference.register(
-        'Options', 'insert_pair_brackets',
+        'Editor', 'insert_pair_brackets',
         gettext("Insert bracket pairs?"), 'boolean', True,
         category_label=gettext('Options'),
         help_str=gettext(
@@ -146,7 +199,7 @@ def RegisterQueryToolPreferences(self):
     )
 
     self.brace_matching = self.preference.register(
-        'Options', 'brace_matching',
+        'Editor', 'brace_matching',
         gettext("Brace matching?"), 'boolean', True,
         category_label=gettext('Options'),
         help_str=gettext(
@@ -155,37 +208,6 @@ def RegisterQueryToolPreferences(self):
         )
     )
 
-    self.show_prompt_save_query_changes = self.preference.register(
-        'Options', 'prompt_save_query_changes',
-        gettext("Prompt to save unsaved query changes?"), 'boolean', True,
-        category_label=gettext('Options'),
-        help_str=gettext(
-            'Specifies whether or not to prompt user to save unsaved '
-            'query on query tool exit.'
-        )
-    )
-
-    self.show_prompt_save_data_changes = self.preference.register(
-        'Options', 'prompt_save_data_changes',
-        gettext("Prompt to save unsaved data changes?"), 'boolean', True,
-        category_label=gettext('Options'),
-        help_str=gettext(
-            'Specifies whether or not to prompt user to save unsaved '
-            'data on data grid exit.'
-        )
-    )
-
-    self.show_prompt_commit_transaction = self.preference.register(
-        'Options', 'prompt_commit_transaction',
-        gettext("Prompt to commit/rollback active transactions?"), 'boolean',
-        True,
-        category_label=gettext('Options'),
-        help_str=gettext(
-            'Specifies whether or not to prompt user to commit or rollback '
-            'an active transaction on Query Tool exit.'
-        )
-    )
-
     self.csv_quoting = self.preference.register(
         'CSV_output', 'csv_quoting',
         gettext("CSV quoting"), 'options', 'strings',
