anonymous blocks inside a transaction
There is a semicolon missing after the DO statement:

...
END     -- semicolon is not necessary here
$do$;   -- but here it is
COMMIT;


The first example works, because the semicolon is optional at the end of a script. That's also why the semicolon after END inside the block is optional.