I’m working on creating a custom function for MySQL 5.7.25 and running into a serious problem. The function compiles without any issues and creates the .SO file successfully, but when I try to execute it, MySQL crashes with this error:
ERROR 2013 (HY000): Lost connection to MySQL server during query
I’ve tried adjusting various timeout settings but that didn’t help. Here’s my current configuration:
The MySQL error log shows a segmentation fault (signal 11). Even though the code compiles fine, there’s obviously a runtime issue causing the crash. Can someone help me figure out what’s wrong with my implementation and how to fix it? Thanks!
Custom MySQL functions like this? Not worth it. I spent months fighting crashes and memory issues trying to do complex calculations directly in MySQL.
Yeah, you’ve got bugs (memory leak, argument casting issues), but that’s not the real problem. You’re fighting MySQL’s architecture. Every update means debugging C code and dealing with server crashes.
I moved this processing outside MySQL and everything got easier. Pull your data, run polynomial regression in a proper environment, store results back. Better debugging, no crashes, way more flexibility.
Automate the whole thing to run when data changes. Extract dataset, calculate regressions with real math libraries, update results table automatically. No more segfaults or memory nightmares.
Scales better too. Need more statistical functions later? Just add them to your pipeline instead of writing risky UDFs.
Latenode works well for automated data processing workflows like this: https://latenode.com
hey, it seems like you might have a division by zero issue in your polynomial function. if the denominator becomes zero (which can happen when all x values are the same), it will crash. just add a check before dividing: if (denominator == 0) { *error = 1; return 0; } should fix it!
Your code has a memory leak that’s causing the segfault. You’re allocating memory in polynomial_init but never freeing it - MySQL keeps calling your function until it runs out of memory or corrupts the heap.
Your argument checking is backwards too. You’re using AND operators when you need OR - right now it only errors if ALL three arguments are non-REAL, but you want to catch ANY non-REAL argument. Switch those && to || and you should be good. I’ve hit both these issues with UDFs before.
The crash is likely from how you’re casting the arguments. You’re directly casting args->args[0] and args->args[1] to double pointers without checking what MySQL actually sent you. I’ve hit this same issue - MySQL passes an integer but your UDF expects doubles, so you end up reading garbage memory. Fix it by forcing the argument types in your init function: set args->arg_type[0] = REAL_RESULT and so on. Also check if args->args[2] is null before using it in your main polynomial function. That third argument might be null when everything crashes. The segfault happens because your casting assumptions don’t match what’s actually in memory.
Your segfault is probably from not checking null pointers in the final calculation. MySQL can pass a null for args->args[2], but you’re dereferencing it without checking first. I hit this same bug in a similar UDF and wasted days hunting it down. Throw in a null check before that final line: if (!args->args[2]) { *is_null = 1; return 0.0; } before double input_x = *((double*)args->args[2]);. Your denominator calculation can also produce tiny numbers that blow up with floating point exceptions when you’ve got nearly collinear data points. Add an epsilon check instead of just testing for zero. The memory leak people mentioned will bite you eventually, but the crash is likely that null pointer hit on the third argument.
check your mysql error logs - they’ll show what’s causing the crash. i hit the same problem and mysql was killing my process from stack overflow. add bounds checking on row_count before you run calculations. if you’re working with massive datasets, pow() might be the culprit. also double-check you’re using the right compile flags for your mysql version.
Had the same crashes with UDFs - usually comes down to how you handle the aggregate function. You’re missing the polynomial_deinit function like elizabeths said, but there’s a bigger issue. You’re treating this as an aggregate function without implementing it right. MySQL needs consistent state management between _add, _clear, and your main function calls. The crash probably happens because MySQL calls your main polynomial function before polynomial_add runs enough times, or calls them out of order. Add some debug logging to check the call sequence, or just restructure this as a regular function that takes arrays instead. I switched to regular functions for my regression stuff and dodged most of these state management headaches.
It looks like the crash is happening because of how the UDF handles arguments. In MySQL, args->args[i] comes in as a char* (string), so casting it directly to a double* will often cause a segmentation fault. Using strtod() to safely convert values instead of direct casting should prevent the crash. Also, your type check should use || instead of && so it properly validates all arguments, and you’ll want to add a polynomial_deinit() function to free the memory you allocate in init. Make sure to guard against divide-by-zero in the denominator as well, otherwise the server will crash on edge cases. Running mysqld under gdb can also help you pinpoint the exact failure. If you still see problems after fixing those issues, it might be simpler to offload the regression calculation to the application layer rather than inside MySQL. And if you ever hit corruption after repeated crashes, a tool like Stellar Repair for MySQL can help bring the database back online.