Let $X$, $Y$ be integral separated schemes of finite type over $\mathbb{C}$, $Y$ be normal, $f:X\rightarrow Y$ be a surjective morphism of schemes. Can the non-flat locus of $f$ be non-empty and have codimension $\geq 2$ in $X$? 



Codimension of non-flat locus
Let $Y$ be a cone over a smooth projective variety which is a product and projectively normal (so $Y$ is normal). For example let $Y$ be the cone over $\mathbb P^1\times \mathbb P^1\subseteq \mathbb P^3$. In general, say $Y$ is a cone over $V\times W$.


Next let $H\subseteq W$ be an effective Cartier divisor (In the $\mathbb P^1\times \mathbb P^1$ example, $H$ is simply a point) and let $H'=V\times H$. Finally, let
$f:X\to Y$ be the blow up of $Y$ along its subscheme $Z$ which is the cone over $H'$. Then $f$ is an isomorphism (and hence flat) outside $Z$. Since $Z$ is a Weil divisor, which is Cartier except at the vertex, $f$ is a small morphism, so it is an isom outside a codimension $2$ subset. 


**Addendum:**
Here is an example with $Y$ smooth. Start with the above example with $V=\mathbb P^1$ and $W=\mathbb P^n$ with $n>1$ and $V\times W$ embedded with the Segre embedding. Construct the same (and for the sake of avoiding confusion, let's denote it differently) $f\_0:X\_0\to Y\_0$ and let $v\in Y\_0$ denote the vertex of the cone. Then a relatively simple computation shows that then $f^{-1}(v)\simeq V=\mathbb P^1$ (for the actual computation see Prop 3.3 of [this paper](https://doi.org/10.1017/CBO9781107416000.014). Note that this means that the exceptional set of $f$ is $1$-dimensional. Another simple calculation shows that $X\_0$ is smooth (this is where the choice of $V$, $W$ and the embedding matters). 


Now let $f:X\colon =X\_0\times \_{Y\_0}X\_0\to Y\colon=X\_0$. Then $f:X\to Y$ is birational (because of the dimensions there is only one component) and it's exceptional set (on $X$) is $2$-dimensional. From the construction and by the assumption that $n>1$ we obtain that $\dim X=\dim X\_0= n+2>3$. Hence $f$ is an isomorphism (in particular, flat) outside a codimension $2$ subset of $X$ (but it is obviously not flat along the exceptional set).


