Precondition in the iterative-stokes demo code

I am testing the demos-iterative-stokes.py code provided on the fenicsproject website. The preconditioned linear system given in the code converges well with minres + amg. However, indeed, after removing the preconditioning matrix P, the same solver gives a wrong numerical solution (due to that the actual residual fails to converge).
I am wondering given a situation like the above, how do we come up with a suitable preconditioner, like the one used in the demo code? Are there some guidelines, or how do we analyze the linear system resulted from a weak formulation?

Any explanations or references are appreciated!

Thank you.

Here is a good reference for you: Preconditioning PDEs

Thank you. This is very helpful.