Look at it in terms of productivity, not maintenance. Having a research building you don't need sit there and generate social points through focus can get you the new structures faster. Generally, faster buildings will outweigh spending an extra 10-20bc a turn on maintenance. Depending on your slider settings, 50+ bc mainenance may be worthwhile, and upgrading will cut the cost of those stock markets in half, at least.
Unfortunately, to truly maximize return it doesn't need to be done on a planet-by-planet basis, but on a building-by-building basis. You will usually get some credit for upgrading instead of building, but that credit is highly variable and not easily determined. Two identical reseach structures may give 0% credit and 100+% credit, depending on *how* they were built. You get credit for how many MP went into the structure when it was built, so if it was rush bought at 0%, no MP were used and no credit is given. If it was built entirely and more MP were used to build it than were strictly needed, you will get credit for the entire MP usage, not just the cost of the building.
Example: Research Centers cost 100MP to build. If the planet was spending 16 MP on social production for all seven turns it took to build at that rate, you would get 112 points of credit when upgrading the building. A stock market costs 120, so you could easily build it in one turn
If the building has more credit than the new structure needs, you don't get it free. You will spend 40-50 MP (or however the math works out, I know it will cost you 436 to rush buy) to get your building, whatever it is. Note this may be more than the building is worth. If you are upgrading, plan to build expensive things like high end factories and CE buildings on other high-cost structures, as this will save you the most time and money. If you see any building take only one turn to upgrade, at least look into rush buying it. You may get a factory or CE for as little as 6bc (meaning you got credit for all but one MP needed), and that is hardly worth wasting a turn
In general, upgrade to factories first, then other high cost buildings, then stock markets, morale and farms when they become necessary (farms will likely be cheaper to destroy and rebuild). This lets you use the productivity of the existing buildings to speed up your construction, you get to use the credit for upgrading, and the maintenance bill will drop as stuff gets done.