views:

153

answers:

1

I'm using the libpq library in C for accessing my Postgresql database. The application inserts a piece of data fed from a queue. When there is a lot of data and it's inserting very quickly it randomly inserts and empty row into the table. Before I even perform the insert I check to make sure that the length of the text being inserted is greater then one. Is there a reason why this is randomly happening? It dosen't happen when there is less data.

*I'd like to note, that this does not happen on Mysql, only Postgresql

+1  A: 

See Milen A. Radev's comment. It should have been an answer. You should not allow empty rows.

Surely at least one column can have a constraint that would cause the insert to fail. Then your app can print/log the error with enough diagnostics for you to figure out what's going on, and under what conditions.

Without retrofitting the above, determine if all data from your queue was inserted properly, or if some rows are missing. I.e., see if some rows are being translated into empty inserts. If you see that some data is causing this you can find what the problem data has in common.

Are you using prepared, parameterized inserts, or are you building a SQL insert statement string each time and executing that? If you're building SQL strings to execute then you must make sure you are quoting character/binary string columns properly with the routines provided by libpq. Or switch to the other method of preparing the insert and passing the data as parameters where it can be properly quoted by libpq itself. This may also improve performance.

dwc
I'm building the SQL strings each time and executing, I'm also using the string escaping function provided by libpq. Do you know if it's possible to programatically add a constraint via libpq?
whatWhat
It's possible, but you may want to put on your DBA hat and do it outside of your code. Either way, see http://www.postgresql.org/docs/current/static/sql-altertable.html for ALTER TABLE syntax, which often refers to http://www.postgresql.org/docs/current/static/sql-createtable.html. Search for "constraint" on either/both pages.
dwc