Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/install-js-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ For more details, check out [INSTALL COMPONENT](install-component.md).
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang component system variables](js-lang-variables.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)
12 changes: 12 additions & 0 deletions docs/js-lang-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,22 @@ The system always maps JS null and undefined values to SQL NULL, regardless of t
| `GEOMETRY` | - Valid `ArrayBuffer`/`View`: stored as binary<br>- Others: cause an error | Enforces format rules to maintain spatial integrity | valid buffer → `GEOMETRY` |
| `JSON` | Converted using `JSON.stringify()` | Converts objects or arrays to serialized strings | `{key: "value"}` → `"{"key":"value"}"` |

## System variables

The js_lang component provides the following system variables for configuring JS routine execution:

| Variable name | Description | Default |
|---|---|---|
| [`js_lang.max_mem_size`](js-lang-variables.md#js_langmax_mem_size) | Maximum memory size (soft limit) for JS routines | 8 MB |
| [`js_lang.max_mem_size_hard_limit_factor`](js-lang-variables.md#js_langmax_mem_size_hard_limit_factor) | Hard limit factor for memory allocation | 0 (disabled) |

These variables help prevent runaway scripts from consuming excessive memory or CPU time. For detailed information about each variable, including configuration options and examples, see [js_lang component system variables](js-lang-variables.md).

## Further reading

- [Install js_lang component](install-js-lang.md)
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang component system variables](js-lang-variables.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)
1 change: 1 addition & 0 deletions docs/js-lang-privileges.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ However, it is important to note that at this time, the creation of JS triggers
- [Install js_lang component](install-js-lang.md)
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang component system variables](js-lang-variables.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)
1 change: 1 addition & 0 deletions docs/js-lang-procedures.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ You can modify or delete stored programs in JS by using the standard `ALTER PROC
- [Install js_lang component](install-js-lang.md)
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang component system variables](js-lang-variables.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)

3 changes: 2 additions & 1 deletion docs/js-lang-troubleshoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ If the `MAX_EXECUTION_TIME` timeout is exceeded for a statement running a JS rou
- [Install js_lang component](install-js-lang.md)
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang component system variables](js-lang-variables.md)
102 changes: 102 additions & 0 deletions docs/js-lang-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# js_lang component system variables

--8<--- "tech.preview.md:5:5"

The following sections describe the system variables available in the js_lang component.

## js_lang system variables

The following js_lang system variables are available:

| Variable name | Description |
|---|---|
| [js_lang.max_mem_size](#js_langmax_mem_size) | Maximum memory size for JS routines |
| [js_lang.max_mem_size_hard_limit_factor](#js_langmax_mem_size_hard_limit_factor) | Maximum memory size hard limit factor |

The following variables are described in detail:

### `js_lang.max_mem_size`

| Options | Description |
| --- | --- |
| Command-line | --js-lang.max-mem-size=value |
| Dynamic | Yes |
| Scope | Global |
| Data type | Numeric |
| Default | 8388608 (8 MB) |
| Minimum value | 3145728 (3 MB) |
| Maximum value | 1073741824 (1 GB) |
| Block size | 1024 bytes |

This variable sets the soft memory allocation limit for JS routines. The component configures V8 with this limit. Since every JS execution (or session, depending on the isolation mode) creates a V8 environment, this limit prevents a single poorly written script or a loop from consuming all the server's physical memory.

Values are rounded down to the nearest multiple of 1024 bytes (block size). V8 requires a minimum heap size to start, which is typically around 10 MB.

V8 behavior with the soft limit:

The soft limit is a threshold that triggers V8's garbage collection (GC) process. When memory usage approaches the soft limit configured by this variable, V8 starts incremental marking in the background to identify objects that can be freed. This background GC runs without pausing JS execution. If the soft limit is reached or exceeded and GC cannot free enough memory, V8 Isolate Termination is triggered, stopping JS execution and returning a "Memory limit exceeded" error to the MySQL client.

Relationship to hard limit:

The hard limit is calculated as `js_lang.max_mem_size * js_lang.max_mem_size_hard_limit_factor` and is configured separately using the [`js_lang.max_mem_size_hard_limit_factor`](#js_langmax_mem_size_hard_limit_factor) variable. When the hard limit is reached, V8 performs a "last resort" garbage collection, stopping all execution to attempt to free every possible byte of memory. If this final GC cannot free enough memory, V8 triggers out-of-memory (OOM) handling, which terminates the mysqld process and causes a server exit. The hard limit is disabled by default (factor = 0) because it causes a server exit.

This variable is dynamic and can be changed at runtime using the `SET` statement. The updated value is applied to new V8 isolate contexts created after the change. Sessions that are already running may not be affected until the next time a context is initialized.

An example of setting the variable:

```sql
SET GLOBAL js_lang.max_mem_size = 16777216;
```

This sets the limit to 16 MB. To make the change persistent across server restarts, add this setting to your configuration file:

```ini
[mysqld]
js_lang.max_mem_size = 16777216
```

### `js_lang.max_mem_size_hard_limit_factor`

| Options | Description |
| --- | --- |
| Command-line | --js-lang.max-mem-size-hard-limit-factor=value |
| Dynamic | No |
| Scope | Global |
| Data type | Numeric |
| Default | 0 |
| Minimum value | 0 |
| Maximum value | 1024 |

This variable controls the hard limit for JS routine memory allocation. When set to a non-zero value, the component calculates the hard limit as `js_lang.max_mem_size * js_lang.max_mem_size_hard_limit_factor` in bytes.

When a JS session's memory usage reaches the hard limit threshold, V8 triggers out-of-memory (OOM) handling, which terminates and crashes the entire mysqld process. This is different from the soft limit behavior, where V8 Isolate Termination stops only the JS execution and returns an error to the client.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps also mention here the last chance GC before crashing process which you describe above?


When set to `0` (the default), the component does not set an explicit V8 memory limit. Instead, V8 uses the default limit, which is typically greater than 1 GB). The default V8 limt avoids a server exit from an abrupt out-of-memory (OOM) scenario.

!!! warning

Allocating more than 1 GB of memory is not safe, as this allocation can exceed V8's default hard limit and cause the server to exit.

A non-zero value enforces the V8 hard limit. This setting ensures that the hard memory limit will never be exceeded, at the price of process abort. Note that large single allocations that exceed this limit will cause server exit.

You should be aware that:

* V8 may abort the process if a single allocation attempt exceeds the limit and garbage collection cannot free enough memory.

* Changing this variable requires a server restart

In earlier versions, the component set the V8 memory limit to `js_lang.max_mem_size * 4` bytes by default. This setting worked well for detecting memory limit violations with small allocations, but failed when a single huge allocation exceeded the limit. Attempting to allocate more than `js_lang.max_mem_size * 4` bytes in one operation could cause the server to crash because V8 aborts the process when an allocation exceeds its memory limit and garbage collection cannot help.

!!! note

This variable relates to the internal heap limit for JS routines and works in conjunction with `js_lang.max_mem_size`. For more information about memory limits and troubleshooting, see [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md).

## Further reading

- [js_lang stored procedure and function overview](js-lang-overview.md)
- [Install js_lang component](install-js-lang.md)
- [Uninstall the js_lang component](uninstall-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang privileges](js-lang-privileges.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)

1 change: 1 addition & 0 deletions docs/uninstall-js-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ mysql> UNINSTALL COMPONENT 'file://component_js_lang';
- [Install js_lang component](install-js-lang.md)
- [js_lang stored function or procedure](js-lang-procedures.md)
- [js_lang privileges](js-lang-privileges.md)
- [js_lang component system variables](js-lang-variables.md)
- [Troubleshoot js_lang procedures and functions](js-lang-troubleshoot.md)
1 change: 1 addition & 0 deletions mkdocs-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ nav:
- JS language support:
- js-lang-overview.md
- install-js-lang.md
- js-lang-variables.md
- js-lang-privileges.md
- js-lang-procedures.md
- js-lang-troubleshoot.md
Expand Down