Trying to measure street lengths, help required

I want to create a script that will output the longest streets and do other kinds of statistics on street lengths (for example: how many km of roads are there in Jerusalem). Importing OSM data to PostGIS makes that easy - but I encountered a problem: because streets with physical separation are mapped as separate ways, this causes them to be counted twice.

Does anyone have any ideas on ways I could (in my local copy of the data, of course) merge these into one “way” automatically so the counting will be more accurate?

I take into account only half of the length for oneway ways.

That won’t work - some streets are oneway without being dual carriageways…