2

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?

3
  • 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.. Commented Jul 14, 2013 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. Commented Jul 14, 2013 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. Commented Jul 14, 2013 at 11:15

1 Answer 1

1

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

4
  • 1
    But why, oh why? (see Craig's comments) Commented Jul 14, 2013 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? Commented Jul 14, 2013 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)? Commented Jul 15, 2013 at 3:16
  • I want to check some parameters before ending the transaction in order to stop it if the check fails. Commented Jul 18, 2013 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.