Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm working inside postgres.c (inside exexc_simple_query) of postgresql 9.1 (line 1094 of http://doxygen.postgresql.org/postgres_8c_source.html#l00829 ). I need to insert some query information in a table that I will check in a second moment. I'm trying to do it with SPI using these three lines:

SPI_connect();
SPI_exec("INSERT INTO testvalues (11,6)", 5);
SPI_finish();

but when I start the server and I send a query I get segmentation fault:

LOG:  server process (PID 13856) was terminated by signal 11: Segmentation fault
LOG:  terminating any other active server processes
WARNING:  terminating connection because of crash of another server process
DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
HINT:  In a moment you should be able to reconnect to the database and repeat your command.
FATAL:  the database system is in recovery mode
LOG:  all server processes terminated; reinitializing
LOG:  database system was interrupted; last known up at 2013-07-14 00:40:22 CEST
LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  record with zero length at 0/17D7368
LOG:  redo is not required
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections

Do you have any idea how I can fix it? Is there a better way to insert data from the postgres.c?

share|improve this question
3  
"inside postgres.c". Er. where inside postgres.c? Function name and line number? PostgreSQL version? In general, that's not the file I'd choose to modify to do database access.. –  Craig Ringer Jul 14 '13 at 7:04
    
Thank you for helping me. doxygen.postgresql.org/postgres_8c_source.html#l00829 line 1094 of postgresql 9.1. I also modified the question with this detail. –  DarkCoffee Jul 14 '13 at 9:51
3  
Seems like a downright bizarre thing to do. You're trying to fire a query via the SPI just before returning success to the client? Why? What query? Please show the exact code you've added and explain what you're trying to achieve with it. You'll also want to get a backtrace - see wiki.postgresql.org/wiki/… . Again, comment here when edits made, as notifications aren't sent when you do an edit, only when you comment. –  Craig Ringer Jul 14 '13 at 11:15

1 Answer 1

up vote 0 down vote accepted

I found the error. SPI functions must be inside the transaction and in my case between start_xact_command and finish_xact_command.

share|improve this answer
1  
But why, oh why? (see Craig's comments) –  dezso Jul 14 '13 at 15:53
    
What why? Do you mean why I'm trying to do a query via the SPI before returning success? Or you mean why between those command? –  DarkCoffee Jul 14 '13 at 22:55
2  
@DarkCoffee "but why" are you trying to do anything with the SPI inside the basic protocol routines! What are you trying to achieve with this? How will your change interact with the user's own transaction handling (if they do an explicit BEGIN, say)? –  Craig Ringer Jul 15 '13 at 3:16
    
I want to check some parameters before ending the transaction in order to stop it if the check fails. –  DarkCoffee Jul 18 '13 at 16:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.