Handling concurrent database modifications in GNUmed

A design principle in GNUmed is to block concurrent use of patient data as little as possible. Most of the time this is no problem because different clients work on different patients and the design of PostgreSQL easily allows for that.

However, every so often, the same patient will be open and edited on several workplaces throughout a practice. There are two problems that need to be taken care of in this scenario:

1) Propagation of changes to a patient record displayed on several machines

Assume this scenario as an example:

The problem arises because the exam room machine read the patient record before the allergy was entered such that no allergy is being displayed to the doctor. While the doctor is, of course, medico-legally required to ask for allergies before starting a treatment, it is desirable that the exam room frontend be notified about the new allergy having been entered into the system without dependence on the downstream user to check whether the information had undergone any change.

To enable this notification GNUmed uses the NOTIFY/LISTEN facility of PostgreSQL.

Backend

During database creation the allergy table is configured to deliver signals to interested frontends:
   select gm.add_table_for_notifies('clin', 'allergy', 'allg');
There are several flavours of add_table_for_notifies() available and documented in the gm schema. Metadata for tables sending out notifications is stored in gm.notifying_tables. The last (boolean) parameter defines whether the notification system will .

Later in the bootstrapping process the Python script gmNotificationSchemaGenerator.py creates appropriate AFTER UPDATE/INSERT/DELETE triggers based on the information in gm.notifying_tables. If the respective table contains a column known to directly or indirectly link to a patient identity the trigger will add the respective patient primary key to the signal it sends out - as opposed to, say, lookup tables.

During normal database operation those triggers send out a NOTIFY whenever an UPDATE/INSERT/DELETE commits from any client. In the above example the signal name eventually sent out would be allg_mod_db:123 assuming a row related to patient 123 was changed.

Frontend

During client startup a thread is created which listens for NOTIFY signals from the database. It autoconfigures itself to listen for all signals recorded in gm.notifying_tables. It also listens to the patient change signals sent from within its own client. Whenever the active patient changes the thread reconfigures itself to only listen for those patient-related backend signals which are specific to that patient -- to that end the backend signals are tagged with patient primary keys where appropriate (see above). The signal is stripped of the patient ID and reinjected into the client for further processing via gmDispatcher.py. That way the frontend code only needs to listen to a generic signal (such as allg_mod_db) and can still be sure to receive signals relevant to the currently active patient only.

Going along with the above example: the top panel (the two-line pane at the top of the client) listens for the signal allg_mod_db. Note that this signal can arrive asynchronously at any time but in the context of the listener thread ! Since wxPython is not written to reliably do GUI operations outside the GUI thread (often the main thread but in any case the thread that initialized wxPython) care needs to be taken when acting upon signals. If any GUI work needs to get done (such as updating fields onscreen) the recommended procedure is to use wx.CallAfter() to execute the GUI code in the context of the GUI thread like this:
gmDispatcher.connect(signal = 'allg_mod_db', receiver = _on_allg_mod_db_in_listener_thread)

def _on_allg_mod_db_in_listener_thread(*args, *kwargs):
   wx.CallAfter(self._on_allg_mod_db_in_gui_thread, *args, *kwargs)

def _on_allg_mod_db(*args, *kwargs):
   update_allergies_display()

This code makes allergies entered by any client automagically appear in all other clients displaying the same patient.

2) Conflicting patient record updates

GNUmed only ever "takes a write lock" on a row when the row is actually about to be written to. It does not:

but rather

Assume this scenario as an example:

There are two potential problems in this situation:

1) Two overlapping transactions try to update the same row

This occurs very rarely and happens in the following sequence of events (note how it only concerns itself with two concurrent transactions):

transaction A transaction B
BEGIN  
  BEGIN
... UI work ... ... UI work ...
COMMIT  
  COMMIT

GNUmed uses the transaction level serializable which means transaction B will fail at the COMMIT step. This is standard transactional behaviour. Using the serializable transaction level is what amounts to the "write lock" alluded to above. GNUmed doesn't ever really take an actual lock since MVCC and transaction semantics afford the needed behaviour.

Note that since GNUmed keeps the duration of updating transactions to a minimum (by only starting them just-in-time) the probability of such serialization conflicts are very rare.

In the above scenario it would require that doctor and nurse start saving their respective allergy update virtually at the same time.

2) Two user actions framing each other try to update the same row

This is way more likely to occur than with overlapping transactions as described above. The sequence of events is as follows (note how this concerns itself with user actions reaching beyond the duration of a single transaction):

nurse doctor
reads row (begin; ...; commit;)  
  reads row (begin; ...; commit;)
changes row in UI  
  changes row in UI
  updates row (begin; ...; commit;)
updates row (begin; ...; commit;)  

There is no open transaction from when a read finishes until an update starts. Therefore the doctor's change to the row commits successfully. However, the nurse's change would commit successfully, too without her being aware of the intermittent change by the doctor ! The reason being that the database transactions do not actually conflict even in serializable mode.

One of the solutions to this concurrency problem is Optimistic Locking. GNUmed employs the change indicator column strategy ("mark the source with a unique identifier") to detect intermittent updates (google for "handy proxy for row has not changed is to see if its XMIN system column is still the same"). The update indicator comes cheap in the form of the XMIN column of PostgreSQL. XMIN is the ID of the database transaction which last modified the row. If a row changes XMIN changes, too. This is a core feature of PostgreSQL's MVCC architecture and is thus very unlikely to disappear (google for " XMIN semantic at peril").

There is a base class cBusinessDBObject in client/pycommon/gmBusinessDBObject.py which implements the optimistic locking for all it subclasses. Each instance represents a row in the database. When the table data is read from the backend the corresponding XMIN and primary key value are remembered. Later on when the data has been updated and is to be written back to the backend the class employs the following construct in the WHERE clause of its update query:
update <table> set
   <column> = <value>
   ...
where
   <pk column> = <pk value of this row> and
   XMIN = <original XMIN value>

Assuming the query is syntactically correct three situations can arise:

GNUmed needs to improve the handling of detected deletions and concurrent updates. Currently it at least detects them and refuses to silently overwrite existing medical data.