tags:

views:

50

answers:

2

I'm taking over development of a set of User Defined Functions and User Defined Aggregates written for PostgreSQL, and I'm having difficulty diagnosing what's causing the code to constantly seg fault. Does any one know if/how it's possible to use GDB with a UDF written in C++? A couple of google searches didn't turn up anything. I've used ELOG before to debug UDFs, but this project is complex enough that I need something a little more powerful.

Thanks, Kevin

+1  A: 

You should be able to attach gdb to a running postgresql backend- although you'll probably want to ensure your postgresql build has debugging symbols left in it to make that comprehensible. If you do select pg_backend_pid() you'll get the backend's process ID that you're dealing with, and can then attach gdb to it (using the --pid switch or attach command). This approach is only useful if you can reproduce the problem using psql, for example, though: having new backend processes automatically attached to be a gdb is... harder. For example, you can set options such as post_auth_delay to make postgresql wait after authentication has finished, giving you a chance to attach the debugger before it continues processing.

araqnid
A: 

With most UDF, you can have problem in two places. The body or the interface of the function. In my experience the problems are usually in the body, not really in the part that interfaces with the database. That means you could de-couple the function from the interface for testing. Write some test scripts in C++ that will call the body of those functions and exercize the code. That way if the tests are failing, your debugging problem is much easier. And if the problem is in the interface, usually adding few assert statements that check the quality of the input to the segfaulting method can be enough to find the culprit.

Jiri Klouda