Database connection lost whenever SQL query string is too long
Woo! I finally figured it out!
The problem was solved by upgrading libmariadb-dev on the Rails application server and rebuilding the mysql2 gem.
Previously, the system was running on a single server with a mariadb-10.1 database. After the switch to a separate database server, the database was on mariadb-10.3 but it was still connecting from the same Rails environment as before, with a mysql2 gem built against 10.1.