Monthly Archives: May 2014

Updating Royal Mail PAF data and ignorance about PostgreSQL deferred foreign keys

A few months ago I bashed together a pl/pgsql function for importing Royal Mail PAF
data into PostgreSQL
and at the same time I also put together another function to perform an update from their “changes” files.

Today I came to actually try and use that function, and it’s not much of a surprise that it needed a little more work; at the time I was working off a fresh import and a small set of changes, whilst looking (with squinty eyes) at the fairly long winded docs. The changes files from RM are supplied as a journal of changes, which I can only assume were intended to be read and applied sequentially; this means we have to rely on some of the smarts SQL provides us with to apply them in bulk. The docs even try to dissuade the use of the changes files by saying “Applying Changes files regularly is ‘high maintenance’, because applying thousands of changes is a very long & involved process compared to buying full refreshes”, however I don’t want to do full refreshes as that’s likely to cause more interruption, and besides it just seems a bit of a cop out to me.

So after another quick skim over the docs, I made a couple of corrections to the update function where I’d complicated things by replacing blank key columns with nulls, and added in an update procedure for the Alias and Postzon files (which are only supplied as a full refresh), this time using postgis to generate the latitude and longitude and save running my horrible perl script.

Then I had a go at running the update:

TEST=# SELECT update_pc_paf('Y14M04', '/tmp');
NOTICE:  2014-05-22 19:40:00.161582+01: Import starting for edition Y14M04 with data root /tmp/Y14M04_CHANGES/
NOTICE:  2014-05-22 19:40:00.165233+01: Begin staging Changes1 file
NOTICE:  2014-05-22 19:40:00.439189+01: Done staging Changes1 file
NOTICE:  2014-05-22 19:40:00.439379+01: Preparing to update localities
NOTICE:  2014-05-22 19:40:00.487656+01: Prepared 15 records for update on localities
NOTICE:  2014-05-22 19:40:00.489481+01: Removed 0 records from localities
NOTICE:  2014-05-22 19:40:00.504522+01: Created 15 records in localities
NOTICE:  2014-05-22 19:40:00.504651+01: Preparing to update thoroughfares
NOTICE:  2014-05-22 19:40:00.54741+01: Prepared 377 records for update on thoroughfares
NOTICE:  2014-05-22 19:40:00.548924+01: Removed 0 records from thoroughfares
NOTICE:  2014-05-22 19:40:00.589353+01: Created 377 records in thoroughfares
NOTICE:  2014-05-22 19:40:00.589477+01: Preparing to update thoroughfare_descriptor
NOTICE:  2014-05-22 19:40:00.631469+01: Prepared 0 records for update on thoroughfare_descriptor
NOTICE:  2014-05-22 19:40:00.632967+01: Removed 0 records from thoroughfare_descriptor
NOTICE:  2014-05-22 19:40:00.633275+01: Created 0 records in thoroughfare_descriptor
NOTICE:  2014-05-22 19:40:00.633444+01: Preparing to update building_names
NOTICE:  2014-05-22 19:40:00.700856+01: Prepared 7947 records for update on building_names
NOTICE:  2014-05-22 19:40:00.704261+01: Removed 0 records from building_names
NOTICE:  2014-05-22 19:40:01.155068+01: Created 7947 records in building_names
NOTICE:  2014-05-22 19:40:01.155178+01: Preparing to update sub_building_names
NOTICE:  2014-05-22 19:40:01.223108+01: Prepared 7798 records for update on sub_building_names
NOTICE:  2014-05-22 19:40:01.226335+01: Removed 0 records from sub_building_names
NOTICE:  2014-05-22 19:40:01.669071+01: Created 7798 records in sub_building_names
NOTICE:  2014-05-22 19:40:01.675826+01: Begin staging Changes2 file
NOTICE:  2014-05-22 19:40:02.59217+01: Done staging Changes2 file
NOTICE:  2014-05-22 19:40:02.592303+01: Preparing to update mainfile
NOTICE:  2014-05-22 19:40:05.07166+01: Prepared 216438 records for update on mainfile
NOTICE:  2014-05-22 19:41:41.439761+01: Removed 87291 records from mainfile
NOTICE:  2014-05-22 19:42:54.976222+01: Created 129147 records in mainfile
NOTICE:  2014-05-22 19:42:54.97639+01: Preparing to update organisations
NOTICE:  2014-05-22 19:42:55.609607+01: Prepared 65158 records for update on organisations
NOTICE:  2014-05-22 19:43:07.778516+01: Removed 32840 records from organisations
NOTICE:  2014-05-22 19:43:10.141383+01: Created 32318 records in organisations
NOTICE:  2014-05-22 19:43:10.18821+01: Begin staging WChanges file
NOTICE:  2014-05-22 19:43:10.218245+01: Done staging WChanges file
NOTICE:  2014-05-22 19:43:10.218346+01: Preparing to update welsh mainfile
NOTICE:  2014-05-22 19:43:10.311915+01: Prepared 8395 records for update on welsh mainfile
NOTICE:  2014-05-22 19:43:20.631738+01: Removed 3098 records from welsh mainfile
NOTICE:  2014-05-22 19:43:23.616801+01: Created 5149 records in welsh mainfile
NOTICE:  2014-05-22 19:43:23.61696+01: Preparing to update welsh organisations
NOTICE:  2014-05-22 19:43:23.624131+01: Prepared 0 records for update on welsh organisations
NOTICE:  2014-05-22 19:43:23.624836+01: Removed 0 records from welsh organisations
NOTICE:  2014-05-22 19:43:23.625512+01: Created 0 records in welsh organisations
NOTICE:  2014-05-22 19:43:23.634001+01: Begin staging postzon
NOTICE:  2014-05-22 19:43:29.097317+01: Done staging postzon file, now staging with lat long data
NOTICE:  2014-05-22 19:44:41.996743+01: Done staging postzon with lat long data, updating
NOTICE:  2014-05-22 19:44:57.195802+01: Inserted 3594 new records for update on postzon_100m
NOTICE:  2014-05-22 19:46:13.242174+01: Updated 212398 records for update on postzon_100m
NOTICE:  2014-05-22 19:46:24.038271+01: Deleted 3991 records for update on postzon_100m
NOTICE:  2014-05-22 19:46:24.808325+01: Begin staging alias file
NOTICE:  2014-05-22 19:46:32.810242+01: Done staging alias file
NOTICE:  2014-05-22 19:46:34.021948+01: Done staging counties , updating
NOTICE:  2014-05-22 19:46:34.023405+01: Inserted 0 new records for update on counties
NOTICE:  2014-05-22 19:46:34.024632+01: Updated 0 records for update on counties
NOTICE:  2014-05-22 19:46:34.025273+01: Deleted 0 records for update on counties
NOTICE:  2014-05-22 19:46:38.25488+01: Done staging county_alias , updating
NOTICE:  2014-05-22 19:46:41.711214+01: Inserted 3594 new records for update on county_alias
NOTICE:  2014-05-22 19:46:46.227681+01: Updated 12 records for update on county_alias
NOTICE:  2014-05-22 19:46:53.812323+01: Deleted 3991 records for update on county_alias
NOTICE:  2014-05-22 19:46:53.812483+01: Completed
(1 row)

My schema for the PAF data has deferrable foreign keys, and in order to apply the updates in chunks I’ve set them as deferred in the above function; I only find out if I’ve violated them when they’re checked on commit:


…. And it just sits there forever! I go and do something else, but when I come back it’s still sitting there; what did I forget?

Well we can make a pretty good guess that we’re waiting whilst the deferred constraints are checked, but the constraint checks don’t take that long when they’re not deferred so why is it taking so long?

Well it turns out I’ve got indexes on the satellite tables that are referenced by the foreign keys on my main address table, but no indexes for those keys on the actual main address table. It’s apparent that when the deferred constraint is checked it needs to look up the row first; something that wouldn’t have been required if the constraint were immediate. I add some indexes, and try again:

Time: 281979.437 ms

It worked, and we didn’t violate any constraints!

The new script (named paf_postgresql_import_postgis.plpgsql) is in the git repository at postgresql/geographic_data (mirrored also on github glynastill/geographic_data).

Experimenting with Slony 2.2

I’ve been spending my spare bits of time over the last couple of weeks looking at the latest release of Slony-I. At a quick glance the main change between 2.1 and 2.2 appears to be to the sl_log table format, but although seemingly minor, the changes to the way clusters are failed over and reshaped actually go much deeper too.

For example in previous versions it was possible for a subscriber to pull multiple sets from different providers and later change the provider for any set at will using the “SCUBSCRIBE SET” command. However as of 2.2, although it’s still possible to initially subscribe a node with different providers for each set, any changes must use the “RESUBSCRIBE NODE” command, which only allows resubscribing all sets from a particular origin to a single provider.

There’s also changes to the “FAILOVER” command to improve reliability in a situation where multiple nodes have failed; you can now pass in multiple failed nodes and Slony should do the right thing. So far my tests with 2.2.2 show there may be some issues when passing in multiple failed nodes where one is a downstream provider to a cascaded subscriber, however that’s a corner case and hopefully we’ll see a fix soonish. (Edit 16/05/2014: There’s now a patch against 2.2.2 for this)

The changes to the sl_log table mean that replicated data is now replicated in a slightly more logical way; data is logged as arrays of values rather than chunks of sql to execute on the subscriber, and the data is sent over a pipe using copy rather than fetched in chunks via a cursor. Also DDL had been moved out of sl_event and into a new sl_log_script table. Upgrade will most likely require some brief downtime, as running update functions requires a lock of all sets and waiting out the cleanup interval for a cleanupevent/logswitch to happen to clear out the tables.

On a separate note, this evening (not the best use of a bank holiday weekend) whilst looking at how these changes would affect my experimental failover script I had a quick bash at adding in an “autofailover” functionality; the idea being that the script keeps polling all the nodes, and upon detecting any unavailable nodes runs the failover command. It’s a functionality I’ve never personally wanted as it’s possible to get into all sorts of trouble blindly failing over onto an asynchronous replica, in fact in a busy environment it’s pretty much guaranteed (E.g missing a single update to a product price and then taking millions of sales on the wrong price!). However, perhaps it could be quite useful in a mostly read only environment where updates are low volume such as a wiki; more thought needed I think.

(The script by the way is here slony-i/slony_failover and also mirrored on github glynastill/slony_failover)