0

I have a small python script that pushes data to my django postgres db. It imports the relevant model from a django project and uses the .save function to save the data to the db without issue.

Yesterday the system was running fine. I started and stopped both my django project and the python script many times over the course of the day, but never rebooted or powered off my computer, until the end of the day.

Today I have discovered that the data is no longer in the db!

This seems silly, as I probably forgotten to do something obvious, but I thought that when the save function is called from a model, the data is committed to the db.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
michael
  • 2,577
  • 5
  • 39
  • 62
  • perhaps I need to explicitly call a commit function when using postgres as opposed to mysql of sqlite? – michael Dec 19 '11 at 19:16
  • rebooted my computer again to see if the same thing happened, but the data was there this time. Maybe the computer didn't shut off cleanly? – michael Dec 19 '11 at 20:20

1 Answers1

1

So this answer is "where to start troubleshooting problems like this" since the question is quite vague and we don't have enough info to troubleshoot effectively.

If this ever happens again, the first thing to do is to turn on statement logging for PostgreSQL and look at the statements as they come in. This should show you begin and commit statements as well as the queries. It's virtually impossible to troubleshoot this sort of problem without access to the queries. Things to look for include missing COMMITs, and missing statements.

After that, the next thing to do is to look at the circumstances under which your computer rebooted. Is it possible it did so before an expected commit? Or did it lose power and not have the transaction log flushed to disk in time?

Those two should rule out just about all possible causes on the db side in a development environment. In a production environment for old versions of PostgreSQL you do want to verify that the system has autovacuum running properly and that you aren't getting warnings about xid wraparound. In newer versions this is not a problem because PostgreSQL will refuse to accept queries when approaching xid wraparound.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182