Sidebar

Main Menu Mobile

  • Home
  • Blog(s)
    • Marco's Blog
  • Technical Tips
    • MySQL
      • Store Procedure
      • Performance and tuning
      • Architecture and design
      • NDB Cluster
      • NDB Connectors
      • Perl Scripts
      • MySQL not on feed
    • Applications ...
    • Windows System
    • DRBD
    • How To ...
  • Never Forget
    • Environment
  • Search
TusaCentral
  • Home
  • Blog(s)
    • Marco's Blog
  • Technical Tips
    • MySQL
      • Store Procedure
      • Performance and tuning
      • Architecture and design
      • NDB Cluster
      • NDB Connectors
      • Perl Scripts
      • MySQL not on feed
    • Applications ...
    • Windows System
    • DRBD
    • How To ...
  • Never Forget
    • Environment
  • Search

MySQL Blogs

My MySQL tips valid-rss-rogers

 

What the Duck? Speeding Up MySQL Analytics Without a Whole Second System.

Details
Marco Tusa
MySQL
30 November 2026

In my career I have seen the same pattern many times. 
A database grows without control, but not evenly. The live dataset, the rows the application really works on, stays relatively small.
Next to it, a few huge tables keep growing, kept only because someone needs them for reporting or just because they initially do not care to clean it up. Sometimes a single table holds 80% of the data or, trust me, even worse.

The worst case I met more than once is reporting run directly on the OLTP system.
A report that scans months of orders competes with the application for CPU, memory and I/O, and the buffer pool fills with old pages nobody else needs.
The application slows down, and the report is slow anyway, because InnoDB was never built to scan and aggregate hundreds of millions of rows.

There are several ways to act on this scenario, the most common answer to this impractical situation is my database is huge I must use sharding to be able to continue to grow.
What normally happens is that once there (sharding) the user discovers that, aside the obvious effort to maintain the sharded environment, one or two of his shards are still growing like crazy in some parts and he needs to shard again, and again … And they discover that being lazy, without checking whether they really need sharding or just need to clean up the data, is not a comfortable thing to do long term.

The most diligent accept the challenge and go for another answer which is separate analytical system: a data warehouse, an ETL pipeline, another product to run and pay for.
Very often this is more than people need. They do not need a new platform. They need to access the same data in a different way and get answers from large datasets in seconds instead of minutes or hours.

There is a second point I care about: how we get there.
I wanted a solution with the smallest possible impact on the MySQL core. This is not just a stylish decision but a very practical one. Changing the core brings a lot of hard work: maintaining the code, merging upstream changes and, not least, bug and CVE fixes. 
This is why I rejected two common approaches from the start: writing a new storage engine and changing core server code.
Both are possible, others have done it, but both create a fork that must be carried release after release. If we can avoid touching the core, we should.

When and how it started

During Percona Live I had the opportunity to learn more about DuckDB.
DuckDB is an embedded analytical database: no server, just a library that runs inside the program using it.
It stores data by column, compresses it, and processes it in batches using all the CPU cores. In short, it is very good exactly where InnoDB struggles: large scans, aggregations and joins over many rows.

The question came naturally: what if DuckDB could live inside MySQL, next to InnoDB, and answer the heavy queries while InnoDB keeps doing what it does best?

At the time I was just wondering, will it work, can I do it? In situations like this for me the best way to answer is stop wondering and do it or at least try to do it.
When I say do it, I do not mean, talk about, or just write about it, but do it, put hands at work and build something I can use in one of the many cases I had experienced in the past. Not some abstract thing that could be usable or not, but something that is there to be used tomorrow on a customer site.

That is the moment when I started to talk with Clio Annali (the co-author), and we started to put down the idea and the plan.
Just to be clear, what we are presenting, is not a production ready version, it is still a proof of concept but built with the intent to become a real solution: tested, documented, and honest about its limits.

What: DUCKDB_SE

This is why DUCKDB_SE was born: DuckDB as a secondary engine for Percona Server for MySQL. It is a plugin, built on the secondary engine framework MySQL already has, with no change to the server code (only build integration).

The idea is simple. InnoDB stays the primary engine and receives every write, exactly as today. For the tables we choose, the plugin keeps a second, columnar copy in DuckDB. When a query is expensive enough, MySQL sends it to DuckDB instead of InnoDB. The application does not change: same SQL, same connection, same users and privileges.

The only visible difference is the time the query takes.

How it works, in four steps:

  1. One ALTER TABLE ... SECONDARY_LOAD copies the table into DuckDB. The copy is stored on disk next to the MySQL data, in a native DuckDB database file.
  2. After the load, every committed change on InnoDB reaches the copy. The plugin reads MySQL's own binary log, groups the changes in batches and applies them to DuckDB. Each batch also records how far it got, in the same commit, so after a crash nothing is lost and nothing is applied twice.
  3. When a query is expensive, or scans a large table, MySQL asks the plugin to run it. The plugin translates the query into DuckDB SQL, DuckDB runs it, and the rows come back through MySQL to the client.
  4. Refuse when unsure. A query that cannot be translated exactly is refused, and InnoDB answers it as usual. The rule is strict: DuckDB answers only when its result is exactly what InnoDB would return.

That last rule matters more than it seems. For example, MySQL compares text according to collations: under utf8mb4_0900_ai_ci, "José" and "jose" are equal. DuckDB has no such rules. DUCKDB_SE stores, next to each text column whose collation DuckDB cannot reproduce, MySQL's own sort key for every value, so grouping, sorting and joining in DuckDB give the same result as InnoDB, accents and case included.

Live data and full history

There is one more feature, and it goes back to the pattern we described at the start: small live data, huge history.

A table can be declared with history retention. Then a purge job can delete old rows from InnoDB, keeping InnoDB small and fast, while DuckDB keeps those rows as archived.
A normal delete still deletes everywhere. Only deletes done in a purge transaction, marked with a special GTID tag, are kept as history. History tables need sync_binlog = 1, and the purge job needs the TRANSACTION_GTID_TAG privilege.

Each session then chooses what a table name means, with duckdb_history_scope:

  • LIVE (the default): only live rows, the same answer InnoDB gives.
  • FULL: live and archived rows. Such queries run only in DuckDB and never fall back to InnoDB, because InnoDB does not have the archived rows. Reading the full history needs a dedicated privilege, DUCKDB_HISTORY_READER.

One MySQL table, two views of it: the live one for the application, and the full one for the reports.

History also needs care: obsolete records must go after some years, a legal erasure request must be honored, a historical flag must be corrected.
InnoDB no longer has those rows, so no DELETE or UPDATE can reach them.

The plugin adds functions for that: duckdb_history_delete and duckdb_history_update take the statement as text, evaluate it’s WHERE exactly as MySQL would, with the privileges of whoever calls them, and change only the archived rows; you can also use duckdb_history_count  to identify first how many rows a statement would touch.
The change is made on the server where it runs, one server at a time, so a mistake can be caught after the first. A table can also ask for a safety net: with "history_undo_days": N, a request can be undone for N day(s).

What it looks like

The plugin is loaded from the configuration file. Two settings are mandatory, because DuckDB runs inside mysqld and we want its resources bounded:

[mysqld]
plugin-load-add           = ha_duckdb.so
loose-duckdb_memory_limit = 8G
loose-duckdb_threads      = 8

The copies follow InnoDB through the binary log, so it must be on, in ROW format with full row images and metadata, and with GTIDs.

Then we choose a table and load it:

-- We give the table a DuckDB copy that also keeps the history
ALTER TABLE orders
  SECONDARY_ENGINE = duckdb,
  SECONDARY_ENGINE_ATTRIBUTE = '{"retain_history": true}';

-- We load the current data into the copy
ALTER TABLE orders SECONDARY_LOAD;

-- We let MySQL send expensive queries to DuckDB
SET SESSION use_secondary_engine = ON;

SELECT YEAR(created_at), SUM(amount)
FROM orders
GROUP BY 1;

Nothing else changes for the application. EXPLAIN shows in secondary engine DUCKDB when a query runs there, and SHOW WARNINGS shows the SQL that DuckDB received.

The purge job and the history report look like this:

-- We purge old rows from InnoDB; DuckDB keeps them as archived
SET gtid_next = 'AUTOMATIC:duckdb_avoid_purge';
DELETE FROM orders WHERE created_at < NOW() - INTERVAL 90 DAY;
SET gtid_next = 'AUTOMATIC';

-- We report on five years of orders, not only the last 90 days
SELECT /*+ SET_VAR(duckdb_history_scope = 'FULL') */
       YEAR(created_at), SUM(amount)
FROM orders
GROUP BY 1;

And the maintenance of the history itself:

-- How many archived orders from before 2020 would go
SELECT duckdb_history_count(“DELETE FROM orders WHERE created_at < '2020-01-01'”, FALSE);

-- Remove them from this server's copy (InnoDB no longer has them)
SELECT duckdb_history_delete(“DELETE FROM orders WHERE created_at < '2020-01-01'”, FALSE);

-- Correct a flag on the archived rows
SELECT duckdb_history_update(“UPDATE orders SET obsolete = 1 WHERE created_at < '2021-01-01'”, FALSE);

Numbers

On a production-like workload, offloaded queries ran 54 to 574 times faster than on InnoDB, with identical results.

The workload simulates wind farm monitoring: 200 turbines sending a reading every 10 minutes for one year, 10.5 million readings, plus sites, regions, alarms and a maintenance history table. Every query runs on InnoDB and on DuckDB, and the results are compared.
These are the median times of the queries DuckDB answered, from one run on the release build on my test machine, on 14 November 2026, when the copies were still stored with DuckLake so not optimal given that the native storage adopted since then scans 2-3 times faster:

Query

InnoDB (s)

DuckDB (s)

Speedup

Two periods side by side (UNION ALL)

35.73

0.062

574x

Top 10 turbines by average power, one quarter

17.88

0.041

439x

Readings above each turbine's yearly average

36.81

0.147

250x

Quarter over quarter energy per region (CTE)

21.76

0.108

201x

Daily energy per site, one month

5.25

0.049

107x

Hourly profile over the year

7.27

0.087

84x

Whole table aggregate

4.22

0.056

75x

Turbines reporting per day with faults

2.47

0.046

54x

Just as important is what did not move. Point lookups by primary key and short index ranges stay on InnoDB, because that is where InnoDB is fast. 
Queries using something not translated yet, such as window functions, also stay on InnoDB and return the same result as before, at the same speed.

To test coverage beyond my own queries, I used TPC-H in the form HammerDB sends it to MySQL.
All 22 TPC-H queries are offloaded and return exactly InnoDB's results. HammerDB's TPROC-H also runs offloaded at scale factor 100; there, query 21, one of the hardest, completes in about 2.2 seconds.

Other ways to bring DuckDB to MySQL

DUCKDB_SE is not the only attempt. Alibaba's AliSQL and MariaDB both added DuckDB as a storage engine, where the table lives only in DuckDB. AliSQL reached that by changing the MySQL SQL layer and maintaining its own DuckDB fork.
MariaDB maps its collations to DuckDB's own case rules, and its documentation warns that results can differ from InnoDB.

DUCKDB_SE takes the opposite path: InnoDB keeps the data, the plugin changes nothing in the server, DuckDB is used as released upstream, and a query either returns exactly InnoDB's result or goes back to InnoDB.
Being this strict could have cost speed.

On HammerDB’s TPROC-H at scale factor 100 it costs little: with one user DUCKDB_SE is within 6% of AliSQL’s total and 15% faster than MariaDB, and the fastest of the three on 12 of 21 queries; with 16 users it is 13% behind AliSQL and level with MariaDB.

One Vuser running:

stats1

Two Vuser running:

stats2

Four Vuser running:

stats4

Eight Vuser running:

stats8

Sixteen Vuser running:

stats16

As you can see we are not doing so bad are we?

What it does not do yet, and what is next

DUCKDB_SE is a proof of concept, and we prefer to be clear about where it stops today:

•         Not every query is translated. Window functions, ROLLUP, GROUP_CONCAT and some string and date functions still run on InnoDB. They return correct results, only without the speedup.

•         MySQL never uses a secondary engine inside a multi-statement transaction (BEGIN, or autocommit = 0). Reports must run with autocommit on.

•         Tables need a primary key. DECIMAL columns with more than 38 digits, VECTOR columns and zero dates are not supported yet.

•         The DuckDB file never shrinks (DuckDB reuses freed space), and its size under continuous updates is still to be measured. Encryption is designed, not built; backup works today only with the plugin stopped, and integration with XtraBackup is still to do.

•         A DDL on a loaded table reloads the whole copy, which can take minutes on large tables, and the other tables wait meanwhile.

•         Archived rows are deleted and corrected one server at a time (the functions above); doing it on every server at once, through the binary log, comes next.

The next steps are the ones that turn a POC into something customers can run: crash and consistency tests, storage compaction, backup, resource governance, and more translated SQL driven by real workloads.
The plugin targets Percona Server 9.7 today; building it on upstream MySQL 9.7 is a goal, and a port to 8.4 LTS is still to be decided.

The code and its documentation are public, in the 9.7-duckdb branch of my Percona Server fork: the plugin in storage/duckdb, and the design, how-to and tutorial in project_duckdb. A second, more technical post will follow, about how it is built without touching the core.

Where to start: the documentation, in reading order

Everything is in the repository, from the overview to the code. To understand the product, read it in this order, from the simplest to the most technical:

1.       README (.github/README.md): what the plugin is, how it works, its status, a quick start, the performance numbers and what is not supported yet. Start here.

2.       Tutorial (project_duckdb/TUTORIAL.md): a first session, step by step: build, configure, load a table, offload queries, keep the history.

3.       HOWTO (project_duckdb/HOWTO.md): the reference: build options, configuration, every variable and status counter, what is offloaded, the limitations.

4.       Design document (project_duckdb/duckdb-secondary-engine-design.md): goals, architecture, the decisions and why they were taken, the paths of a write and of a query through the code (with diagrams), risks, production readiness and the plan.

5.       Worklogs (project_duckdb/worklog-index.md): the work item by item, in the style of MySQL worklogs: DWL-01 to DWL-08 are the proof of concept (requirements, design and tests of each function), DWL-09 to DWL-23 the plan towards beta and general availability.

6.       TPC-H check (project_duckdb/tpch/README.md): the TPC-H queries one by one, the HammerDB setup and the memory and spill check script.

7.       Monitoring (project_duckdb/monitoring/README.md): the PMM / Grafana dashboard, its custom queries and alert ideas.

8.       Container image (project_duckdb/docker/README.md): build and run the server with the plugin in Docker, sized from the container’s memory and CPU limits.

9.       Tests: the MTR suite test by test (storage/duckdb/mysql-test/duckdb/README.md) and the scenarios run by hand, with their results (project_duckdb/manual-test-plan.md).

10.   Raw results (project_duckdb/perf): the performance runs behind the numbers in this post.

The code itself, in storage/duckdb, comes last: the “Paths through the code” section of the design document is its map.

If you recognize the pattern we described at the start, small live data and huge reporting tables, we would like to hear from you, and even more to see DUCKDB_SE on your queries.

In the meanwhile, please try it and let us know what you think participate to the discussion (https://github.com/Tusamarco/percona-server-duckdb-se/discussions/1) and if you find issues open one.


More to come...


Marco & Clio

 marco small  clio3 small reduced

Great MySQL to all!

No comments on “What the Duck? Speeding Up MySQL Analytics Without a Whole Second System.”

Just Because You Can, Doesn't Mean You Should: Re-evaluating Galera Node Weights

Details
Marco Tusa
MySQL
13 November 2026

We all know that when using a cluster based on Galera (Codership) such as Percona XtraDB Cluster (PXC), it is recommended to deploy an odd number of nodes.

The generic answer to this recommendation is that otherwise the risk of having an inconsistent cluster (like split-brain) is higher.  (see Get started with Percona XtraDB Cluster - Percona XtraDB Cluster ) 

As a reminder, when split-brain occurs, a cluster will stop accepting queries, which is what a safe solution should do while waiting for the DBA to review and decide which of the two nodes is the leader. I do not even want to describe the infamous wsrep_provider_options="pc.ignore_sb = yes", which has been the cause of many, many nights spent trying to reconcile the corrupted datasets. If you have it, remove it.

However, in Galera, we also have the setting wsrep_provider_options="pc.weight=N". With it, we can define a specific weight in the quorum calculation for each node. The question naturally arises: if we can assign a different weight to each node, why can't we have an even number of nodes and assign them different weights?

The answer to the question is simple, yes, we can. 

What is not simple is to identify who should have a different weight and how to distribute it.

To understand the problem, we need to review how the quorum is calculated (I have touched on the topic long ago here TusaCentral - Geographic replication and quorum Calculation in MySQL/Galera) and what a View is.

In Percona XtraDB Cluster (PXC) and Galera, a View (or cluster view) is the agreed-upon membership of the cluster at any specific moment in time. It represents the exact list of nodes that are currently connected, synchronized, and actively communicating with each other.

Whenever a node joins the cluster, leaves gracefully, or drops out due to a network failure, the remaining connected nodes negotiate a new View and assign it a unique View ID.

Its relationship to the Primary Component (PC) is based on quorum:

  • The Quorum Check: Every time a new View is formed, the nodes inside it calculate whether they hold the majority of the cluster's total votes.
  • The Primary Component: If the new View holds the majority, it is designated as the Primary Component. Only the Primary Component is allowed to accept and process transactions.
  • Non-Primary Views: If a network split occurs, isolated nodes will form their own separate View. Because this smaller View lacks a majority, it becomes a non-primary component and immediately halts database operations to prevent split-brain data corruption.

Essentially, a View defines who is currently connected to each other, while the Primary Component is the specific View that has enough members to safely keep the database running.

2026-11-10T13:45:01.498927Z 0 [Note] [MY-000000] [Galera] Node 49d8df08-bad6 state primary
2026-11-10T13:45:01.500967Z 0 [Note] [MY-000000] [Galera] Current view of cluster as seen by this node
view (view_id(PRIM,49d8df08-bad6,93)
memb {
	49d8df08-bad6,2
	5ce1a9aa-8e06,2
	8458c8f0-9b8f,2
	}
joined {}
left {}
partitioned {	85a6e937-b8c4,2}
)

The formula for calculating the quorum is:

  Σ p·w  <  2·(Σ m·w + Σ l·w)

Where: p = members of the last PRIMARY view l = members that left gracefully (LEFT) m = members of the component being evaluated w = pc.weight (0–255).

In short, the total weight of the previous Primary view must be less than twice the weight of the members of the new component plus the weight of the members that left gracefully.  To be clear, a crashed / unreachable node (DOWN or in another partition) still counts in Σ p·w, so it pushes the threshold up. A graceful leave (LEFT), instead, is credited to the new component. Also, any node that was not in the last PC is dropped from calculation, so a node that has just joined or rejoined contributes nothing to this check.

Look for the function:

bool gcomm::pc::Proto::have_quorum(const View& view, const View& pc_view) const
{
    // Compare only against members and left which were part of the pc_view.
    gcomm::NodeList memb_intersection(
        node_list_intersection(view.members(), pc_view.members()));
    gcomm::NodeList left_intersection(
        node_list_intersection(view.left(), pc_view.members()));
    if (have_weights(...))
        return (weighted_sum(memb_intersection, instances_) * 2
                + weighted_sum(left_intersection, instances_) >
                weighted_sum(pc_view.members(), instances_));
    else
        return (memb_intersection.size()*2 + left_intersection.size() >
                pc_view.members().size());
}

(repo https://github.com/percona/galera/tree/a6b54d887d58bae43ae3f46f5696195329405b7e) 

Now let's assume that we have a cluster of 4 nodes, there are many possible combinations where things will work and where instead they will hit us badly.

legend

The most common is the split-brain scenario:

1 split brain same weigh

 

 

 

 

 

 

 

 

 

 

 

 

 

To prevent this, we may decide to use one node with different weight:

1 split brain diff weigh

 

 

 

 

 

 

 

 

 

 

 

 

 

Here we go, problem solved… really?

No, because changing weights may cause other issues, for example if we calculate the weights wrongly with respect to the number of nodes:

3 unbalnced weight

 

 

 

 

 

 

 

 

 

 

 

 

 

In this case one single node could bring down the whole cluster. 

Of course, if we perform standard operations, or crashes happen in cascade and the cluster has time to rebuild the Primary Component, all will work well:

2 crash on cascade

 

 

 

 

 

 

 

 

 

 

 

 

 

2 crash on cascade b

 

 

 

 

 

 

 

 

 

 

 

 

 

The problem is that:

  • Cluster dimensions are dynamic and change in relation to the active Primary Component
  • In environments managed by operators (e.g., Kubernetes), scaling the number of nodes up or down is even more dynamic and difficult to predict.
  • In real life we may have an incredible variety of scenarios, I have built just a few (see PDF)  

The real question is not if we can do it but whether it is worth it: does using an odd number of nodes help or not?

The answer is partly. 

Odd nodes get rid of the exact-tie case in the simple situation. They don't completely stop the cluster losing PRIMARY state.

What matters is the parity of the total weight, not the node count.

A tie happens when present = partitioned, which means the counted weight (Σw(PC) − Left) splits exactly in half. 

That is only possible when the counted weight is even. So:

  • 5 nodes of w=1, any two-way split: 3|2 or 4|1. One side always strictly wins.
  • 6 nodes with one node at w=2 (total 7) behave exactly like an odd cluster. Adding the Galera Arbitrator (garbd) as a 7th voter does the same thing.
  • 3 nodes weighted 2,1,1 (total 4) is an odd node count with an even total, so it can tie again.

Where odd nodes still lose PRIMARY:

  1. The PC shrinks after each failure, and its parity changes. 5 nodes lose 1 and stay PRIMARY, but the new PC has 4 nodes. A 2|2 split after that ties. A rolling failure still ends at 2 nodes and losing one of those ties.
  2. Graceful leaves change the parity. If 1 of 5 shuts down cleanly, the counted weight is 4, so a 2|2 split ties.
  3. Splits into three or more groups. A 2|2|1 split of 5 has no tie, but no group is bigger than the rest combined (2 < 3), so every group goes NON-PRIMARY.
  4. Losing a majority is still losing. 5 nodes survive 2 failures but not 3, so odd doesn't add fault tolerance. 5 and 6 nodes both tolerate only 2 failures. The 6th node adds cost and a tie risk, and no extra resilience.
  5. Two data centers. With 3+2 nodes, the 3-node site survives a link cut, but if that site goes down, the 2-node site stops. To survive the loss of either site we need a third location, even if it only runs garbd.

In practice: aim for an odd total weight in the PC and use a third site or an arbitrator for multi-DC setups. Even then, a cluster that has already shrunk can become even again, so the last-two-nodes tie can't be designed away. 

Only pc.ignore_sb or a manual bootstrap gets past it. But you do not want to go there.

Given this, and given that what counts is NOT the number of nodes but the balance in the weight calculation, is using weights the right way to compensate?

Well, this may be my personal opinion, but in my experience, weights make the cluster harder to manage, and the stability they add is narrow.

A weight-2 node acts like two nodes that always fail together. Four nodes weighted 2,1,1,1 behave like a 5-node cluster where two of the nodes share one box. We trade a split-brain tie for a single point of fate:

  • Gain: a clean 2|2 split now has a fixed winner, the side holding the heavy node.
  • Loss: losing the heavy node plus one other is fatal. Over-weighting makes it worse: with w=3, losing that node alone causes a tie.

The set of survivable failures shifts rather than grows. We still tolerate only one arbitrary failure plus some specific splits.

What weights add to day-to-day operations:

  1. Nodes are no longer equal. Everyone must know which node is "special". Runbooks, monitoring and on-call decisions depend on it.
  2. Maintenance order matters. Shut the heavy node down cleanly so its weight counts for neither side. If it crashes during maintenance on another node, the cluster can stop. Patching or rebooting it may first need its weight moved to another node, a step that's easy to forget.
  3. Weight is a configuration that can drift. It has to be consistent across config files, rebuilds and node replacements. A rebuilt node that comes back with the default w=1 silently changes the quorum maths.
  4. The tie-breaker site is fixed. With two data centers, the weighted side always wins. That's no different from simply putting 3 nodes in one DC and 2 in the other, and in both cases losing that DC stops the cluster.
  5. Shrinking still breaks it. After failures the PC changes, so a setup tuned for 4 nodes may tie at 3 or 2.

Simple to most complex for the same goal:

  1. An odd number of equal nodes: symmetric, nothing special to manage.
  2. An even number of nodes plus the Galera Arbitrator (garbd): the data nodes stay equal, and the arbitrator holds the tie-breaking vote. If it runs at a third site, the cluster survives losing either main site, which weights alone can't give you.
  3. An even number of nodes with weights: use this only when you can't add a node or an arbitrator and you deliberately want one side to always win a split.

Weights don't make the cluster generally more stable, and they add operational complexity. They make sense as a deliberate tiebreaker, not as a fix for even node counts.

Conclusions

I stand by the evergreen KISS motto: Keep It Simple and Straightforward. Having a PXC cluster with more than 3 nodes makes sense in very few cases, and only to scale reads or if you plan to kill 2 nodes at the same time on a cluster of 5. Given the significant complications the increase of nodes or the use of weight brings, I do not really see why someone in a healthy state of mind should look for trouble.

Just because it can be done, it doesn’t mean “do it”, especially if the gain is minimal and the complexity increases. 

Do yourself a favor: keep your cluster to the minimum, to what is really needed and do not try to stretch it. You will save yourself from spending miserable nights doing cluster recovery or worse data reconciliation.

Great MySQL to everyone.

No comments on “Just Because You Can, Doesn't Mean You Should: Re-evaluating Galera Node Weights”

The Galera Crossroads, Part 2: Swap the Binaries, Keep the Cluster

Details
Marco Tusa
MySQL
02 November 2026

Zero downtime, zero new hardware: converting MySQL Galera Cluster to Percona XtraDB Cluster node by node goat1

In my previous blog I have highlighted how the ones that were using the Codership version of MySQL + Galera are at a crossroad and must take a decision about their freedom and future. 

Percona with the new PXC 8.4.11 had made things very simple for users to keep shifting from the dismissed Codership version to the fully maintained Percona Xtradb Cluster (PXC).

While you can get more information about how to cover different cases reading the Percona PXC Manual I am going to focus on a specific case, the one that for me is going to be the most useful when in production, the change in flight.

First of all try to upgrade to one of the latest version of Codership/Galera, for this test I was using:

Codership MySQL (wsrep) mysql-wsrep-8.4.6-26.24-linux-x86_64.tar.gz

Galera galera-4-26.4.24-Linux-x86_64.tar.gz

As said for PXC I am on the 8.4.11 release of PXC, specifically Percona-XtraDB-Cluster_8.4.11-11.1_Linux.x86_64.glibc2.28-minimal.tar.gz. 

The process

Preliminary steps

This is an inflight process, a bit as if while you are at 33000 feets flying at 600mph someone starts to replace the engines one by one. It can be done but you need to be mindful. In our case this means, no DDL, low write rate, no long transaction, no data load. 

Just to be sure we do not keep it unsaid, this is a migration so you must start and finish it and do not keep things mixed.

Take a full backup before start, and validate it! A backup that is not validated is just a waste of disk space.  

PXC come with two settings that we must take into account:

--pxc-strict-mode

--pxcencrypt-cluster-traffic

About the first we need to disable it for the time of the migration so in our my.cnf we need to add:

--pxc-strict-mode=DISABLED

For pxcencrypt-cluster-traffic we have two options:

  • No encryption currently in place: loose_pxcencrypt-cluster-traffic=OFF  we disable the encryption in PXC
  • With encryption currently in place then read Encrypt PXC traffic to correctly set your PXC instance to use the certificates from the current environment. 

Keep in mind that by default pxcencrypt-cluster-traffic in PXC is on so to disable you must explicitly set it.

Last thing to be sure about is the SST method, let us use Xtrabackup to be sure as such be sure to have: wsrep-sst-method = xtrabackup-v2 and to have the Xtrabackup binaries in place. To be clear, we should NOT go for an SST, but just in case. 

Swapping the binaries

To make my life simpler when I need to update MySQL or like in this case do tests I use symbolic links to binaries:

lrwxrwxrwx.  1 mysql mysql         36 Jul  7 14:24 COD84P -> mysql-wsrep-8.4.6-26.24-linux-x86_64
lrwxrwxrwx.  1 mysql mysql         65 Oct 14 18:15 PXC8P -> Percona-XtraDB-Cluster_8.4.11-11.1_Linux.x86_64.glibc2.35-minimal

So what I will do to swap the binaries on the node will be to have the link COD84P pointing to PXC8P and restart the instance, easy peasy. 

Initial state

Ok this is normally what you have:

galera to pxc step1 codership cluster

                     

I have put ProxySQL here but it could have been HAProxy or any other proxy/tool that allows you to define a writer and N reader, because you are doing that right? You are NOT writing over all the nodes, correct? Because if you do so, well stop here and read these instructions. Do you remember … flying at 33K feet 600mph? Something simply cannot be done. 

sysbench oltp_write.lua  --mysql-host=<> --mysql-port=6033 --mysql-user=app_test --mysql-password=<> --mysql-db=migrate --db-driver=mysql --skip_trx=on --report-interval=1 --mysql-ignore-errors=none --histogram --table_name=mill  --stats_format=csv --db-ps-mode=disable --mysql-ssl=preferred --threads=256 --tables=5 --table_size=10000 run

Step 1

Now while we have the app running and modifying data, we will stop one of the non primary nodes. Remember the concept of “Primary” is artificial when using Galera, and is achieved by electing one of the nodes as writer in ProxySQL:

+--------------+---------------+------+--------+--------------------------+
| hostgroup_id | hostname      | port | status | comment                  |
+--------------+---------------+------+--------+--------------------------+
| 100          | 192.168.4.205 | 3308 | ONLINE | Node1 main primary       |
| 101          | 192.168.4.205 | 3308 | ONLINE | Node1                    |
| 101          | 192.168.4.21  | 3308 | ONLINE | Node2 first failover     |
| 101          | 192.168.4.231 | 3308 | ONLINE | Node3 second failover    |
+--------------+---------------+------+--------+--------------------------+

galera to pxc step2 node3 converted

                       

Once we stop the node n3:

View:
  id: 036d186c-77cf-11f1-8338-fb6a1aa4f205:470059
  status: primary
  protocol_version: 7
  capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO
  final: no
  own_index: 0
  members(2):
	0: 88c98ce9-c97e-11f1-ba76-9a0a085461a2, cnode1
	1: f0a3125c-cca4-11f1-8e8a-b611d8c7e61a, cnode2

We have the new node view in the cluster that will continue to serve traffic, while we can modify the binaries in the stopped node.

lrwxrwxrwx. 1 mysql mysql 36 Jul  7 14:24 COD84P -> mysql-wsrep-8.4.6-26.24-linux-x86_64
root@pxc3:/opt/mysql_templates# rm -f COD84P; ln -s PXC8P COD84P;chown -R mysql:mysql COD84P;
root@pxc3:/opt/mysql_templates# ll COD84P
lrwxrwxrwx. 1 mysql mysql 5 Oct 21 10:58 COD84P -> PXC8P

Done, let us restart the instance:

2026-10-21T09:03:17.806872Z 0 [System] [MY-000000] [WSREP] P: Member 0.2 (cnode3) requested state transfer from '*any*'. Selected 2.2 (cnode2)(SYNCED) as donor.
2026-10-21T09:03:19.122175Z 0 [System] [MY-000000] [WSREP] P: 2.2 (cnode2): State transfer to 0.2 (cnode3) complete.
2026-10-21T09:03:19.125662Z 0 [System] [MY-000000] [WSREP] P: Member 2.2 (cnode2) synced with group.
2026-10-21T09:04:00.976655Z 0 [System] [MY-000000] [WSREP] P: 0.2 (cnode3): State transfer from 2.2 (cnode2) complete.
2026-10-21T09:04:07.909508Z 0 [System] [MY-000000] [WSREP] P: Member 0.2 (cnode3) synced with group.

After a few seconds of synchronization, we can see that our node 3 is back online and working using Percona PXC binaries:

Server version:8.4.11-11.1 Percona XtraDB Cluster binary (GPL) 8.4.11, Revision 33f7510, WSREP version 26.1.4.3

Step 2

Let us do the same for node2 and stop it:

View:
  id: 036d186c-77cf-11f1-8338-fb6a1aa4f205:758293
  status: primary
  protocol_version: 7
  capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO
  final: no
  own_index: 1
  members(2):
	0: 3f0059e6-cd2e-11f1-9129-d24205451337, cnode3
	1: 88c98ce9-c97e-11f1-ba76-9a0a085461a2, cnode1

And cluster on proxysql is:

+--------------+---------------+------+--------+--------------------------------------------------------------------------+
| hostgroup_id | hostname      | port | status | comment                                                                  |
+--------------+---------------+------+--------+--------------------------------------------------------------------------+
| 100          | 192.168.4.205 | 3308 | ONLINE | Node1 main primary                                                       |
| 101          | 192.168.4.205 | 3308 | ONLINE | Node1                                                                    |
| 101          | 192.168.4.231 | 3308 | ONLINE | Node3 second failover   100_W_101_R_retry_up=0;100_W_101_R_retry_down=0; |
+--------------+---------------+------+--------+--------------------------------------------------------------------------+

galera to pxc step3 node2 converted

                       

We change the link:

root@pxc2:/opt/mysql_templates# ll COD84P
lrwxrwxrwx. 1 mysql mysql 5 Oct 21 11:31 COD84P -> PXC8P

And again we can see the new node joining and cluster synchronizing:

View:
  id: 036d186c-77cf-11f1-8338-fb6a1aa4f205:825441
  status: primary
  protocol_version: 7
  capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO
  final: no
  own_index: 2
  members(3):
	0: 3f0059e6-cd2e-11f1-9129-d24205451337, cnode3
	1: 5153a9e8-cd32-11f1-9034-17a540af26a2, cnode2
	2: 88c98ce9-c97e-11f1-ba76-9a0a085461a2, cnode1
=================================================
2026-10-21T09:32:23.708142Z 12 [System] [MY-000000] [WSREP] wsrep_notify_cmd is not defined, skipping notification.
2026-10-21T09:32:23.712580Z 12 [System] [MY-000000] [WSREP] P: Lowest cert index boundary for CC from group: 825392
2026-10-21T09:32:23.712662Z 12 [System] [MY-000000] [WSREP] P: Min available from gcache for CC from group: 1
2026-10-21T09:32:25.281302Z 0 [System] [MY-000000] [WSREP] P: (88c98ce9-ba76, 'tcp://10.0.0.205:4569') turning message relay requesting off
2026-10-21T09:32:26.880929Z 0 [System] [MY-000000] [WSREP] P: Member 1.2 (cnode2) requested state transfer from '*any*'. Selected 0.2 (cnode3)(SYNCED) as donor.
2026-10-21T09:32:30.851066Z 0 [System] [MY-000000] [WSREP] P: 0.2 (cnode3): State transfer to 1.2 (cnode2) complete.
2026-10-21T09:32:30.853938Z 0 [System] [MY-000000] [WSREP] P: Member 0.2 (cnode3) synced with group.
2026-10-21T09:33:54.295264Z 0 [System] [MY-000000] [WSREP] P: 1.2 (cnode2): State transfer from 0.2 (cnode3) complete.
2026-10-21T09:34:04.065755Z 0 [System] [MY-000000] [WSREP] P: Member 1.2 (cnode2) synced with group.

Node using Percona PXC: 

Server version:		8.4.11-11.1 Percona XtraDB Cluster binary (GPL) 8.4.11, Revision 33f7510, WSREP version 26.1.4.3

Step 3

Now is the moment to shift the writes from current Primary to new primary.

To do so we will shift the current primary to OFFLINE_SOFT to give the connections time to complete the operations, and at the same time we will move to writer group node2:

UPDATE mysql_servers set status='OFFLINE_SOFT' where hostgroup_id=100 and hostname='192.168.4.205';
INSERT INTO mysql_servers (hostname,hostgroup_id,port,weight,max_connections,comment) VALUES ('192.168.4.21',100,3308,998,2000,'Node2 Writer');
LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK; 

+--------------+---------------+------+--------------+----------------------------------------------------------------------------+
| hostgroup_id | hostname      | port | status       | comment                                                                    |
+--------------+---------------+------+--------------+----------------------------------------------------------------------------+
| 100          | 192.168.4.205 | 3308 | OFFLINE_SOFT | Node1 main primary                                                         |
| 100          | 192.168.4.21  | 3308 | ONLINE       | Node2 Writer                                                               |
| 101          | 192.168.4.205 | 3308 | ONLINE       | Node1                                                                      |
| 101          | 192.168.4.21  | 3308 | ONLINE       | Node2 first failover  100_W_101_R_retry_up=0;100_W_101_R_retry_down=0;     |
| 101          | 192.168.4.231 | 3308 | ONLINE       | Node3 second failover     100_W_101_R_retry_up=0;100_W_101_R_retry_down=0; |
+--------------+---------------+------+--------------+----------------------------------------------------------------------------+

Once the connections on Node1 are gone to 0:

select  c.hostgroup,c.srv_host,c.srv_port,c.status,c.ConnUsed from stats_mysql_connection_pool c left JOIN runtime_mysql_servers b ON  c.hostgroup=b.hostgroup_id and c.srv_host=b.hostname and c.srv
_port = b.port   order by hostgroup,srv_host desc;
+-----------+---------------+----------+--------------+----------+
| hostgroup | srv_host      | srv_port | status       | ConnUsed |
+-----------+---------------+----------+--------------+----------+
| 100       | 192.168.4.21  | 3308     | ONLINE       | 257      |
| 100       | 192.168.4.205 | 3308     | OFFLINE_SOFT | 0        |<----
| 101       | 192.168.4.231 | 3308     | ONLINE       | 0        |
| 101       | 192.168.4.21  | 3308     | ONLINE       | 0        |
| 101       | 192.168.4.205 | 3308     | ONLINE       | 0        |
+-----------+---------------+----------+--------------+----------+

We are ready to start and do the final swap.

galera to pxc step4a writer moved to pxc

                     

 

Step 4

I stop the node1 and cluster creates a new view and continue to serve the traffic as expected:

View:
  id: 036d186c-77cf-11f1-8338-fb6a1aa4f205:1079040
  status: primary
  protocol_version: 7
  capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO
  final: no
  own_index: 1
  members(2):
	0: 3f0059e6-cd2e-11f1-9129-d24205451337, cnode3
	1: 5153a9e8-cd32-11f1-9034-17a540af26a2, cnode2

Now let me do the binary swap:

root@pxc1:/opt/mysql_templates# ll COD84P
lrwxrwxrwx. 1 mysql mysql 5 Oct 21 11:56 COD84P -> PXC8P

And after the usual synchronization we have the cluster fully using Percona PXC:

View:
  id: 036d186c-77cf-11f1-8338-fb6a1aa4f205:1109495
  status: primary
  protocol_version: 7
  capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO
  final: no
  own_index: 1
  members(3):
	0: 3f0059e6-cd2e-11f1-9129-d24205451337, cnode3
	1: 5153a9e8-cd32-11f1-9034-17a540af26a2, cnode2
	2: b7669a8f-cd35-11f1-8ea4-527375537a0b, cnode1
=================================================
2026-10-21T09:56:43.482088Z 1 [Note] [MY-000000] [WSREP] wsrep_notify_cmd is not defined, skipping notification.
2026-10-21T09:56:43.508133Z 1 [Note] [MY-000000] [Galera] Recording CC from group: 1109495
2026-10-21T09:56:43.508222Z 1 [Note] [MY-000000] [Galera] Lowest cert index boundary for CC from group: 1109364
2026-10-21T09:56:43.508260Z 1 [Note] [MY-000000] [Galera] Min available from gcache for CC from group: 4
2026-10-21T09:56:45.000170Z 0 [Note] [MY-000000] [Galera] (5153a9e8-9034, 'tcp://10.0.0.21:4569') turning message relay requesting off
2026-10-21T09:56:47.022659Z 0 [Note] [MY-000000] [Galera] Member 2.2 (cnode1) requested state transfer from '*any*'. Selected 0.2 (cnode3)(SYNCED) as donor.
2026-10-21T09:56:49.599440Z 0 [Note] [MY-000000] [Galera] 0.2 (cnode3): State transfer to 2.2 (cnode1) complete.
2026-10-21T09:56:49.601423Z 0 [Note] [MY-000000] [Galera] Member 0.2 (cnode3) synced with group.
2026-10-21T09:57:49.384391Z 0 [Note] [MY-000000] [Galera] 2.2 (cnode1): State transfer from 0.2 (cnode3) complete.
2026-10-21T09:57:56.826355Z 0 [Note] [MY-000000] [Galera] Member 2.2 (cnode1) synced with group.

galera to pxc step4b all nodes pxc

                       

Final refining

Now all the cluster is using PXC so we can configure the variables we have left out like

--pxc-strict-mode read instructions

--pxcencrypt-cluster-traffic read instructions

Also if not really relevant we may also wish to switch back the write to Node1, which can be done as easily as we have done the previous shift:

UPDATE mysql_servers set status='OFFLINE_SOFT' where hostgroup_id=100 and hostname='192.168.4.21';
INSERT INTO mysql_servers (hostname,hostgroup_id,port,weight,max_connections,comment) VALUES ('192.168.4.205',100,3308,1000,2000,'Node1 Primary Writer');
LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK; 

+--------------+---------------+------+--------------+------------------------------------------------------------------------------+
| hostgroup_id | hostname      | port | status       | comment                                                                      |
+--------------+---------------+------+--------------+------------------------------------------------------------------------------+
| 100          | 192.168.4.205 | 3308 | ONLINE       | Node1 Primary Writer                                                         |
| 100          | 192.168.4.21  | 3308 | OFFLINE_SOFT | Node2 Writer                                                                 |
| 101          | 192.168.4.205 | 3308 | ONLINE       | Node1    100_W_101_R_retry_up=0;100_W_101_R_retry_down=0;                    |
| 101          | 192.168.4.21  | 3308 | ONLINE       | Node2 first failover  100_W_101_R_retry_up=0;100_W_101_R_retry_down=0;       |
| 101          | 192.168.4.231 | 3308 | ONLINE       | Node3 second failover       100_W_101_R_retry_up=0;100_W_101_R_retry_down=0; |
+--------------+---------------+------+--------------+------------------------------------------------------------------------------+

Done!

Rollback

There is no compatibility if you cannot rollback what you have done if you do not like it. 

So it is important to have the process clear. Yes we can rollback the installation, however given a current limitation in galera itself, once the cluster you have upgraded has all nodes using a specific Galera Protocol version you cannot inject a node with a lower protocol version number. This is not dependent on using Codership version or PXC but by the galera library used. 

In those cases the only option we have is to put the cluster down and restart it with the different libraries/binaries. 

A reliable compatibility plan requires a clear rollback process. While rolling back an installation is possible, a limitation within the core Galera library regardless of whether you use Codership or Percona XtraDB Cluster (PXC) complicates the procedure. Once an upgraded cluster has all nodes running on a newer Galera Protocol version, it is impossible to introduce a node running a lower protocol version. To perform a rollback in this scenario, the entire cluster must be taken offline and completely restarted using the older binaries and libraries.

Full Cluster Downgrade Procedure

Because a rolling downgrade is not possible when the protocol versions differ, you must perform a full cluster restart.

  1. Halt Application Traffic: Stop all application writes to the cluster to prevent data loss or inconsistencies during the shutdown process.
  2. Shut Down the Cluster: Gracefully stop the database service on every node in the cluster one at a time.
  3. Verify grastate.dat: Inspect the grastate.dat file (typically located in /var/lib/mysql/) on all nodes. Ensure the seqno is not -1 (which indicates an unclean shutdown) and identify the node with the highest seqno. This node holds the most up-to-date data and must be used to bootstrap the downgraded cluster.
  4. Downgrade the Binaries/Library
  5. Bootstrap the First Node: On the node you identified with the highest seqno, bootstrap the new cluster.
  6. Verify Bootstrap Success and data correctness. 
  7. Start Remaining Nodes: Start the database service on the other nodes one by one. Monitor the MySQL error logs and wait for each node to fully join and synchronize (via SST or IST) before starting the next one.
  8. Restore Traffic: Once wsrep_cluster_size matches your total node count and all nodes report wsrep_ready = ON, allow application traffic back into the cluster.

Conclusion

What I have just done is, honestly, not much more than a rolling restart. 

One node at a time we stopped the service, pointed a symbolic link to the new binaries, started it again, and watched it rejoin the cluster through IST. 

No new hardware, no logical dump and reload, no new cluster to build alongside the old one, and above all no downtime for the application: sysbench kept writing for the whole migration, and the only “visible” event was a controlled writer switch in ProxySQL. 

If you have ever performed a minor version upgrade on a Galera cluster, you already have all the skills needed to move to PXC.

This is possible because PXC is not “another product” for a Galera user. It is Galera, kept alive, maintained, and open. 

Percona has made sure that a MySQL Galera node and a PXC 8.4.11 node can sit in the same cluster, talk to each other, and exchange state transfers as if nothing had changed. 

While the Codership MySQL distribution is being dismissed, PXC gives us a fully open source, actively developed path forward, with the extra value Percona adds on top: Percona Server underneath, PXC strict mode, built-in traffic encryption, integration with XtraBackup and PMM, and a community that is not going away. 

Choosing PXC is not abandoning Galera, it is the way we keep Galera going.

That said, let us not forget the plane at 33,000 feet. 

Replacing the engines in flight works, but only if we fly carefully while doing it. During the migration we must accept a few limitations: no DDL, no long running transactions, no bulk data loads, and a reduced write rate to keep contention and certification conflicts low. Writes must go to a single node, and a proper validated backup must be in place before we start. Features like pxc_strict_mode and cluster traffic encryption must be relaxed for the duration of the swap and re-enabled only once the whole cluster is on PXC. 

Finally, a migration is a migration: once started, we finish it. A mixed cluster is a transition state, not a place to live.

Respect these rules, and the move from MySQL Galera Cluster to Percona XtraDB Cluster becomes what it should be: a quiet, almost boring operation. And in production, boring is exactly what we want.

Great MySQL to everyone!

No comments on “The Galera Crossroads, Part 2: Swap the Binaries, Keep the Cluster”

JavaScript in MySQL: Extending SQL, Not Moving the Application

Details
Marco Tusa
MySQL
15 October 2026

scaled goatAt Percona Live Europe 2026, Dmitry Lenev and I presented JavaScript in My Database? Over My Dead Query, the story of component_js_lang, the V8-based JavaScript engine for Percona Server for MySQL 8.4. That experience told us few things:

  • The presentation was seen as “for developers”
  • What most DBA or DevOps heard was "JavaScript in the database" = "application logic in the database"

This let most of the people pictured developers shipping their business logic into stored procedures. Order workflows, pricing rules, REST calls, all running inside mysqld. 

That picture is wrong, and in this post I want to replace it with the right one, using the same four demos we showed on stage.

The fear is reasonable, the target is wrong

DBAs have earned their skepticism. Many of us have inherited a schema where thousands of lines of stored procedures were the application: impossible to test, impossible to version, impossible to scale out, and owned by nobody who still works there.

So when a new language arrives inside the server, the reflex is to protect the database from becoming an application server again. 

 

We share that reflex.

The point of this post is that JavaScript routines, used as designed, do not move us in that direction. They move the opposite way: they make SQL stronger so that less logic needs to be pulled out of the database and re-implemented somewhere else.

The engine itself also enforces the boundary. Inside component_js_lang a routine has:

  • No network I/O. No fetch(), no sockets. A routine cannot call your payment provider or another microservice.
  • No file system access. It cannot read configs, keys or files.
  • No Node.js or DOM APIs. No require(), no process, no npm ecosystem.
  • No useful async. Everything runs synchronously inside the statement.

You cannot build an application with that toolbox. However you can build very good functions!

What JavaScript routines are actually for

A JavaScript routine extends the SQL vocabulary. It is a new function you call from SELECT, JOIN, WHERE, GROUP BY or a trigger, exactly like LOWER() or JSON_EXTRACT(). The only new syntax is LANGUAGE JS:

CREATE FUNCTION norm_keys(d JSON) RETURNS JSON
DETERMINISTIC NO SQL LANGUAGE JS AS $$
  const walk = (j) => {
    if (j === null || typeof j !== 'object') return j
    if (Array.isArray(j)) return j.map(walk)
    const out = {}
    for (const [k, v] of Object.entries(j))
      out[k.toLowerCase()] = walk(v)
    return out
  }
  return walk(typeof d === 'string' ? JSON.parse(d) : d)
$$;

 

The rule we kept repeating on stage is simple: SQL owns the set, JavaScript owns the value. SQL is unbeatable at filtering, joining, ordering, windowing and aggregating. Where SQL and its procedural language stop is a short, very specific list:

  • Recursion over a shape not known in advance. JSON_TABLE needs every path as a literal.
  • Arrays. SQL has no array type, so any algorithm that fills a grid has nowhere to live.
  • Big integers. BIGINT stops at 19 digits; an IBAN checksum needs 29.
  • Real iteration with aggregates. A recursive CTE cannot contain an aggregate or window function (ERROR 3575), and a WHILE loop in PL pays an executor round trip on every statement.
  • Rich string handling. UPPER() and LOWER() do not know that McDonald has a capital D.

None of these is business logic. They are missing building blocks. JavaScript supplies them and hands the result straight back to SQL.

schema

The JavaScript routine lives inside the SQL layer, not beside the application: a query calls it, and it returns a value to that query.

Four demos, one pattern

slide1

Each demo in the pleu_2026 repository follows the same shape: SQL does the set work, a small JavaScript function does the one step SQL cannot phrase, and the decision that matters to the business stays in the application.

Demo What SQL could not do The JavaScript piece Result What stays in the application
Normalize JSON keys Walk a JSON tree of unknown shape; four partners spell customer.id four ways norm_keys(), 9 lines One report went from 51 lines with four UNION ALL branches to 19 lines with no partner names Which partners to onboard and what the report means
Validate input at the door Proper-case names, normalise phones, run an IBAN mod-97 on a 29-digit number proper_name(), clean_email(), clean_phone(), valid_iban(), clean_contact() 460 of 2,000 rows caught, each with a list of what was wrong What to do with a rejected contact: fix, call the customer, quarantine
Deduplicate customers Levenshtein distance needs a grid, and SQL has no arrays lev_distance() inside a JOIN predicate Name equality found 21.7% of duplicates; blocking in SQL plus two distances found about 80% at 99.7% precision Whether to merge two records, and how
Where should Federico live? Iterate to a geometric median; recursive CTEs refuse aggregates Weiszfeld procedure 11.00 s in procedural SQL vs 0.52 s in JavaScript; 327,033 statements vs 6 Whether Federico actually moves

A few details are worth calling out, because they are exactly where DBAs and developers should meet.

Dedupe: SQL narrows, JavaScript decides the distance. Comparing 3,000 customers with 2,000 signups is six million pairs. We never let JavaScript see them all. Ordinary join predicates on city and initials cut the work to 21,352 pairs, and only those reach lev_distance(). The function returns a number. Merging two customers is a business decision, and it stays in the application.

Validation: the function reports, the policy decides. clean_contact() returns the cleaned values and an array of errors. A trigger uses it to guarantee that nothing dirty lands in contacts, whoever does the insert: web app, mobile app, nightly import, or someone at a mysql prompt at 2am. What happens next to a rejected row is still the application's job.

Federico: three steps, one of them JavaScript. Turning 44,347 raw stops into 16,232 weighted endpoints is two window functions, and nothing beats SQL there. Turning the resulting coordinate into a district is plain joins. JavaScript only runs the inner loop in the middle. Note that this demo uses session.runSql(), so today it runs on MySQL Enterprise; SQL from JavaScript is the top roadmap item for Percona Server.

Where we draw the line

A JavaScript routine belongs in the database when it makes a data operation possible or cheaper. It belongs in the application when it decides what the business does. In practice we use five questions before accepting a routine:

  1. Does it work on values the database already holds? If it needs an external service, a file or a user session, it is application code.
  2. Is it deterministic, with no side effects outside the transaction? DETERMINISTIC NO SQL is not only a binlog requirement; it is a good design filter.
  3. Is it called from SQL? A good routine sits inside a SELECT, a JOIN, a JSON_TABLE or a trigger. If nothing in SQL calls it, ask why it is in the database.
  4. Is the result smaller than the input, or does it save a round trip? Two numbers out instead of 5,000 names, emails and cities is the kind of win we want.
  5. Would every client need the same rule? Validation that the web app, the mobile app and the importer all must apply is a data rule. Pricing for a campaign that changes next week is business policy.
Belongs in a JS routine Stays in the application
Normalising and validating values (keys, names, phones, IBAN) Workflows, approvals, notifications
Distance, similarity, checksum and parsing functions Decisions based on those scores: merge, reject, refund
The inner loop of a numeric algorithm over rows SQL selected Orchestration across services and external APIs
Integrity rules every writer must obey UI, sessions, authentication flows
Reshaping JSON so SQL can query it Business rules owned by product and changed often

If a routine starts to need configuration tables, feature flags or calls to another system, that is our signal that it has crossed the line.

Why developers should ask for it

For developers this is not about giving up control of the application. It is about no longer having to pull data out of MySQL just because SQL could not express one step.

  • It is the language you already write. ECMAScript on V8, the same runtime as Chrome and Node.js. JSON columns arrive as native JS objects, no JSON.parse() needed.
  • Fewer round trips. In the commute demo one CALL read 16,232 rows and wrote 2,000 answers with zero rows crossing the network. The speed-up from 11.00 s to 0.52 s came from not crossing the executor boundary 327,033 times, not from JavaScript being a faster language.
  • Less personal data in motion. The dedupe query returns two numbers instead of shipping 5,000 names, emails and cities to an app server, and into its logs. Your GDPR scope stays in the database.
  • One rule, written once. Validation that lives in five clients becomes four out-of-date copies. A function and a trigger are one copy nobody can bypass.
  • Simpler queries in your code. norm_keys() turned a 51-line, partner-specific report into 19 lines. Partner five becomes a data change, not a code change.
  • Pure functions are easy to test. Deterministic, no I/O, no hidden state: the same unit-testing habits you already have.

How we suggest developers and DBAs work together:

  1. The developer proposes the function, with the SQL that will call it.
  2. The DBA reviews it against the five questions above, and checks if the call site uses blocking or filtering so the function runs on as few rows as possible.
  3. Grant CREATE_JS_ROUTINE deliberately; it is required on top of CREATE ROUTINE, so who can write JavaScript is an explicit choice.
  4. Keep the routine in version control next to the schema migrations.

SQL did not get worse, it got a companion

JavaScript in MySQL is not an invitation to rebuild the application inside the server. 

It is a set of sharp tools for the few places where SQL and PL run out: recursion, arrays, big numbers and tight loops. The set work stays in SQL, the business logic stays in the application, and JavaScript fills the gap between them.

If you want to try it, component_js_lang is available as a tech preview in Percona Server for MySQL 8.4.5-5 and later, free and open source, while the equivalent feature from Oracle requires MySQL Enterprise. 

You can easily use the code available here Demo code from Percona Live Europe 2026 to perform some tests, just doing cut and paste. 

Tell us what you think on the Percona Community Forum or in Jira with the label PS-JS-LANG. Your feedback decides what goes into GA, starting with SQL execution from JavaScript routines.

Resources

  • Percona Server js_lang documentation
  • Demo code from Percona Live Europe 2026
  • Source code on GitHub
No comments on “JavaScript in MySQL: Extending SQL, Not Moving the Application”

PGO or not PGO this is the dilemma. Step 3

Details
Marco Tusa
MySQL
09 October 2026

Step 3: Why my sysbench-trained build loses and how to do it right.

Given the topic complexity and the length of this article I have split it in 3 three different blog-post:

  1. What is PGO
  2. How PGO it works
  3. Why my sysbench-trained build loses and how to do it right.

Three compounding reasons:

  1. Uncovered code gets pessimized.
    Sysbench-tpcc touches a narrow slice of mysqld.
    Every function with zero counts is treated as cold: GCC optimizes it for size, skips inlining, and shoves it into cold sections.
    But at runtime I still execute plenty of code my training never touched, such as purge, flushing, stats recalculation, error paths, different optimizer plans, connection churn.
    All of that is now running de-optimized code, and I pay icache penalties every time hot code calls into "cold" regions.
    This is why GCC added -fprofile-partial-training, without it, a narrow profile actively hurts everything outside it.
  2. High-concurrency instrumented runs produce corrupted or skewed profiles.
    GCC's profile counters are non-atomic by default.
    With 128–1024 threads hammering the same counters I get lost updates and internally inconsistent counts (I actually had "profile count data file corrupted/inconsistent" warnings at the -fprofile-use compile).
    I need to use -fprofile-update=atomic, which almost nobody sets and was at the beginning not aware of.
    In short my profile was garbage and a garbage profile is worse than no profile, consistent with my PGO build being slower than plain -O3 at 128 threads.
  3. Instrumentation distorts what "hot" means under contention.
    The instrumented binary is 2–10x slower, which shifts where threads pile up. Spin loops in mutexes and rw-locks record enormous counts, so the compiler lavishes optimization on waiting code instead of useful work. While at high thread counts my real bottleneck is lock contention and memory latency things branch layout can't fix.

Do I have a way to merge the different profiles like MTR + sysbench?
The answer is yes. For GCC it's simple to do so, because the runtime automatically merges profile data across multiple training runs against the same instrumented binary. I don't need a separate merge step like Clang does.

What it does is that each time an instrumented binary exits, it writes its counters into files. If those files already exist (from a previous run), GCC's runtime adds the new counts to the existing ones rather than overwriting them.
So if I run MTR first, then run sysbench against the same instrumented build with the same FPROFILE_DIR, the second run's counts accumulate on top of the first. The final profile reflects both workloads combined. 

Combining MTR with a moderate-thread sysbench run for PGO training is helpful because the two workloads cover different, complementary dimensions of mysqld's behavior: 

  • MTR sweeps broad functional breadth parser, optimizer, DDL, replication, error paths. However it runs almost entirely single-connection, so it never exercises the branches that only exist under real concurrency, like the contended slow-path of a latch, MVCC visibility checks against in-flight writers, lock-wait queuing, or redo-log group-commit batching;
  • A moderate-concurrency sysbench run (something like 4–64 threads, enough to create actual simultaneous access without descending into the timing-distortion and counter-corruption problems), fills exactly that gap by giving those concurrency-only branches nonzero execution counts, which keeps the compiler from treating them as cold, since cold code paths get optimized for size instead of speed.

So the merged profile ends up with both the wide code coverage MTR provides and the concurrent-path coverage MTR structurally can't, at the cost of only a modest, second-order improvement over MTR alone since PGO's overall gains are already small and this specific slice of the binary is a narrow fraction of total execution.

 

However even where it does help, I am stacking a small effect on top of a small effect. I have already found PGO vs non-PGO gives me under 5%.
The incremental gain from better-covering a narrow slice of concurrency-only code within that is a second-order refinement.
It is plausibly in the sub-1% range, quite  smaller than the run-to-run noise I had seen just from benchmark variance.

Or at least that is what I now think, let me validate it.

Commands to build the code:

cmake ../mysql-9.7.2 \
-DCMAKE_INSTALL_PREFIX=/opt/mysql_templates/mysql-9.7.2-PGO-instrument \
-DCMAKE_BUILD_TYPE=Release \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_PACKAGE_FLAGS=OFF \
-DCOMPILATION_COMMENT_SERVER="Marco compile 9.7.2-PGO instrument" \
-DCOMPILATION_COMMENT="Marco compile 9.7.2 PGO instrument" \
-DCMAKE_C_COMPILER=clang-20 \
-DCMAKE_CXX_COMPILER=clang++-20 \
-DCMAKE_C_FLAGS="-fuse-ld=lld" \
-DCMAKE_CXX_FLAGS="-fuse-ld=lld" \
-DFPROFILE_GENERATE=ON \
-DWITH_LTO=OFF \
-DFPROFILE_DIR=/opt/mysql_source/profile

Run the mtr:

perl mysql-test-run.pl --force --max-test-fail=0 --parallel=8 --suite=main,innodb,innodb_undo,binlog,rpl,perfschema,sys_vars

 

Then run the sysbench-tpcc test with 64 threads

sysbench /opt/tools/sysbench-tpcc/tpcc.lua --mysql-host=127.0.0.1 --mysql-port=3307  --mysql-user=app_test --mysql-password=test --mysql-db=tpcc --db-driver=mysql --tables=10 --scale=100 --rand-type=uniform --report-interval=1  --histogram --report_csv=yes  --stats_format=csv --db-ps-mode=disable --trx_level=RR --enable_purge=yes --time=600 --threads=64 --mysql-ssl=PREFERRED --mysql-ignore-errors=none  --reconnect=0  run

And got the profile as

[root@sm-blade03 profile]# llvm-profdata-20 show -detailed-summary /opt/mysql_source/profile/mysql.profdata
/opt/mysql_source/profile/mysql.profdata
Instrumentation level: IR entry_first = 0 instrument_loop_entries = 0
Total functions: 52403
Maximum function count: 289910820864
Maximum internal block count: 16881288068
Total number of blocks: 619050
Total count: 2473216025729
Detailed summary:
2 blocks (0.00%) with count >= 289910820864 account for 1% of the total counts.
2 blocks (0.00%) with count >= 289910820864 account for 10% of the total counts.
2 blocks (0.00%) with count >= 289910820864 account for 20% of the total counts.
11 blocks (0.00%) with count >= 16777125216 account for 30% of the total counts.
32 blocks (0.01%) with count >= 7806039615 account for 40% of the total counts.
77 blocks (0.01%) with count >= 3839409465 account for 50% of the total counts.
160 blocks (0.03%) with count >= 2273821592 account for 60% of the total counts.
312 blocks (0.05%) with count >= 1111890303 account for 70% of the total counts.
719 blocks (0.12%) with count >= 365989756 account for 80% of the total counts.
2165 blocks (0.35%) with count >= 85858392 account for 90% of the total counts.
4893 blocks (0.79%) with count >= 24359463 account for 95% of the total counts.
16760 blocks (2.71%) with count >= 2841200 account for 99% of the total counts.
45006 blocks (7.27%) with count >= 148881 account for 99.9% of the total counts.
96619 blocks (15.61%) with count >= 8640 account for 99.99% of the total counts.
157023 blocks (25.37%) with count >= 960 account for 99.999% of the total counts.
220001 blocks (35.54%) with count >= 91 account for 99.9999% of the total counts 

Checking on how counts concentrate: just 2 blocks account for 20% of all executed instructions across the entire training run (almost certainly a tight InnoDB buffer-pool/redo-log/lock_manager loop  counts in the hundreds of billions), while 90% of total execution volume is concentrated in only 2,165 blocks (0.35% of all covered blocks). 

That's the "hot core" PGO is designed to find and optimize aggressively. Meanwhile the long tail,  the other 96%+ of blocks, still has nonzero counts (this is a sparse profile, so anything appearing here was actually executed at least once).

Meaning a huge amount of MTR's functional-path breadth got captured even though it's numerically dwarfed by sysbench's tight hot loops.

This is the ideal shape for a merged profile: a small, extremely hot core (from sustained sysbench load) sitting on top of broad, low-frequency-but-nonzero coverage (from MTR's functional sweep). 

If MTR had contributed nothing, I would have a much flatter, narrower distribution with far fewer total functions covered.
If sysbench had swamped everything with no MTR contribution, I would have a similar shape but with a much smaller "Total functions" number, since sysbench-tpcc only touches a fraction of mysqld's total surface.

Command to build the final optimized binaries:

llvm-profdata-20 merge -sparse /opt/mysql_source/profile/*.profraw -o /opt/mysql_source/profile/mysql.profdata

cmake ../mysql-9.7.2 \
 -DCMAKE_INSTALL_PREFIX=/opt/mysql_templates/mysql-9.7.2-PGO-optimized-MTR-sysbench \
  -DCMAKE_BUILD_TYPE=Release \
  -DENABLED_LOCAL_INFILE=1 \
  -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  -DWITH_PACKAGE_FLAGS=OFF \
  -DCOMPILATION_COMMENT_SERVER="Marco compile 9.7.2-PGO optimized MTR+sysbench" \
  -DCOMPILATION_COMMENT="Marco compile 9.7.2 PGO optimized MTR+sysbench" \
  -DCMAKE_C_COMPILER=clang-20 \
  -DCMAKE_CXX_COMPILER=clang++-20 \
  -DFPROFILE_USE=ON \
  -DFPROFILE_DIR=/opt/mysql_source/profile/mysql.profdata \
  -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LZ4=system -DWITH_ICU=system \
  -DWITH_NUMA=ON -DWITH_LTO=ON -DWITH_LD=lld -DWITH_SYSTEMD=1 \
  -DWITH_UNIT_TESTS=OFF -DWITH_ROUTER=OFF -DMYSQL_MAINTAINER_MODE=OFF

Re-running the test I got this:

 

  

 

 

 

 

 

 

 

 

 

 

 

As expected the benefit I got was minimal, something was there but that disappeared while the concurrency increased.

 

Conclusions

  • Non-PGO vs PGO comparison: we had a 12% increase with low concurrency. But when simulating a more realistic load with higher contention the win becomes smaller and smaller, under 2%.
  • Training-workload choice mattered a lot: a sysbench-tpcc-trained PGO build ended up slower than a non-PGO.
  • After switching to a merged MTR + moderate-concurrency-sysbench training profile and re-running, the gain was still minimal, and it shrank further as concurrency increased.

 

Why the sysbench-only build lost

Sysbench-tpcc only exercises a narrow slice of mysqld, so everything outside that slice (purge, flushing, stats, error paths, alternate optimizer plans) gets pessimized as "cold" code.

At 128–1024 threads, GCC's non-atomic profile counters get corrupted under contention unless I explicitly set -fprofile-update=atomic,a garbage profile is worse than no profile at all.

Instrumentation overhead (2–10x slowdown) distorts what looks "hot" under load mutex/rwlock spin loops dominate the counts, so the compiler optimizes waiting code instead of real work, while the actual bottleneck (lock contention, memory latency) is something branch layout can't fix anyway.

Bottom line: PGO for MySQL/Percona Server binaries does work in the sense that the mechanism is sound whole-binary function reordering for something the size of mysqld is a legitimate, often the single biggest, win in PGO generally. 

But empirically here the payoff was consistently small (sub-5%, trending toward sub-1% for the concurrency-specific refinement), fragile to training-workload choice, fragile to build-flag correctness (atomic counters, -fprofile-partial-training), and it erodes further as thread count rises,  which is exactly what happens in production and what we care about most.

So my practical answer: it's not a clear "yes, always compile with PGO." It's more a “maybe, and only if you get every detail right". Correct broad-coverage training data (MTR, ideally merged with moderate-concurrency sysbench), atomic profile counters, and realistic expectations that the win is marginal and shrinks under heavy concurrency. 

Get any of those wrong and you can end up worse than a plain build.
Given the size of the benefit versus the number of ways to mess up the training methodology, PGO reads more like a niche optimization for a well-controlled build pipeline than a default you'd flip on broadly.

I would be more than happy to prove wrong and I am eager to get other people's feedback, so please test, test, test and let me know. 

 

Happy MySQL to everyone

Go to:

What is PGO

How PGO it works

No comments on “PGO or not PGO this is the dilemma. Step 3”

More Articles …

  1. PGO or not PGO this is the dilemma. Step 2
  2. PGO or not PGO this is the dilemma. Step 1
  3. The Galera Crossroads: Why PXC is the Lifeline for MariaDB Community Users
  4. Stop Guessing Your Kubernetes MySQL Configs: Meet the MySQL Operator Calculator
  5. The Failover Brownout: Rethinking High Availability in MySQL Group Replication
  6. Group Replication VS Percona XtraDB Cluster: The True Cost of Consistency
  7. MySQL Belgian Days and FOSDEM 2026: My Impressions
  8. joins... joins... everywhere
  9. The 10 TB Scale Survival Guide for Percona Operator PXC on Kubernetes
  10. MySQL January 2026 Performance review
Page 1 of 27
  • Start
  • Prev
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • Next
  • End

Related Articles

  • The Jerry Maguire effect combines with John Lennon “Imagine”…
  • The horizon line
  • La storia dei figli del mare
  • A dream on MySQL parallel replication
  • Binary log and Transaction cache in MySQL 5.1 & 5.5
  • How to recover for deleted binlogs
  • How to Reset root password in MySQL
  • How and why tmp_table_size and max_heap_table_size are bounded.
  • How to insert information on Access denied on the MySQL error log
  • How to set up the MySQL Replication

Path

  1. Home
  2. MySQL Blogs
  3. Using SKIP LOCK in MySQL For Queue Processing

Unable to display online user data at this time.

login

Remember Me
  • Forgot your username?
  • Forgot your password?
Bootstrap is a front-end framework of Twitter, Inc. Code licensed under MIT License. Font Awesome font licensed under SIL OFL 1.1.