Does a flat compactification always exist? Let $\pi:X\to S$ be a separated flat morphism of finite type of Noetherian schemes. Does $\pi$ necessarily factor as an open immersion followed by a proper flat morphism? The analogue of this question with the word "flat" replaced by "smooth" has a negative answer (consider an elliptic curve over $\mathbb{Q}_p$ that has bad reduction).
 A: This already fails if $S$ is regular of dimension $3$ and $\pi$ is quasi-finite. Indeed, let $X$ be a normal affine variety over $\mathbf C$ of dimension $3$ with an isolated non-Cohen–Macaulay singularity (e.g. an affine cone over a smooth projective surface $Y$ with $H^1(Y,\mathcal O_Y) \neq 0$). By Noether normalisation, there exists a finite surjection
$$\pi \colon X \to S = \mathbf A^3,$$
without loss of generality taking the isolated singularity $x_0 \in X$ to the origin $0 \in \mathbf A^3$. Let $U = X \setminus x_0$, which is smooth by assumption, so $\pi|_U$ is flat by miracle flatness. Now I claim that $\pi|_U \colon U \to \mathbf A^3$ does not have a flat compactification.
Indeed, suppose $U \hookrightarrow X' \stackrel{\pi'}\to S$ is a factorisation into an open immersion and a proper flat morphism. Because $\pi'$ is flat and generically finite, it is quasi-finite, hence finite since it is proper. Since $\pi'$ is finite flat and $S$ is regular, we conclude that $X'$ is Cohen–Macaulay.
Let $\bar U \subseteq X'$ be the scheme-theoretic closure of $U$, and let $V = S \setminus 0$. Since $\pi^{-1}(V) \subseteq U$, we conclude that $\bar U \setminus U$ is supported on $\pi'^{-1}(0)$, in particular has dimension $0$. By Hartshorne's connectedness theorem, this implies that there are no other components in $X'$ (otherwise two components would meet only in a $0$-dimensional set), i.e. $\bar U = X'$ set-theoretically. Since $X'$ is generically reduced and Cohen–Macaulay, it is reduced, so $\bar U = X'$ scheme-theoretically.
In particular, $X' \setminus U$ is $0$-dimensional, so $X'$ is regular in codimension $1$ since the same holds for $U$. Since $X'$ is Cohen–Macaulay, this forces $X'$ normal, so it equals the normalisation of $S$ in $K(U)$, which is $X$. But $X$ is not Cohen–Macaulay, contradicting flatness of $\pi'$. $\square$
