If I leave out the "analyze", here's what I get (note that the categories_staging_N table's name changes every time; it'screated on demand as "create table categories_staging_n(id integer)").
How/when are they created? In the same statement? After create, are you analyzing these tables? If not, the optimizer is blind and may be choosing a bad plan by chance.