TITLE: Can flatness be specified by a natural coherent sheaf?
QUESTION [11 upvotes]: More precisely:

Given a finite-type morphism $f \colon X \to Y$ of nice schemes (say, both of finite type over a field), is there a "natural" coherent sheaf $\mathcal F_f$ on $X$ such that the support of $\mathcal F_f$ is precisely $\{x \in X : f \text{ is not flat at }x\}$?

I'd rather not say what exactly "natural" should mean here, but if you held a gun to my head, I would guess it means that $\mathcal F_f$ is preserved under flat pullbacks; in other words, that if $g\colon Z \to Y$ is a flat finite-type morphism of nice schemes, then $\mathcal F_{f \circ g} = g^* \mathcal F_{f}$.
Example: If $X = \operatorname{Spec} B$, $Y = \operatorname{Spec} A$, and $A \to B$ is a local morphism of local noetherian rings, then $$\ker(B \otimes_A \mathfrak m \to B)$$ is 
a finitely generated $B$-module that is zero iff $B$ is flat over $A$ (by the local criterion for flatness).  [Notation: $\mathfrak m$ is the maximal ideal of $A$.] This construction is respected under pullback by flat local morphisms $B \to C$ of local noetherian rings. Unfortunately, it runs into trouble if you look at flatness anywhere except the closed point of $\operatorname{Spec} B$.
Motivation: There are standard theorems that certain sets (including the one above) are "open," without really specifying a closed subscheme structure on the complement. In some cases, the complement can be described as the support of a coherent sheaf, which gives it a natural scheme structure. For instance, the "indeterminacy locus" of a rational function may be described by the cokernel of the ideal of denominators. I personally find such constructions more satisfying and memorable than more direct proofs that a particular subset is open.

REPLY [4 votes]: Assume the morphism $f$ is projective. Let $L$ be a relatively ample line bundle on $X$. Then there is such $N$ that the nonflatness locus of $f$ coincides with the locus where $F := f_*(L^N)$ is not locally free. If additionally $Y$ is smooth the latter is the union of supports of the sheaves ${\mathcal Ext}^i(F,O_Y)$, $i>0$. 
Of course this is not quite what you want, but still sometimes is useful.