Let $\pi:X\rightarrow Y$ be a smooth proper (assume projective if needed) morphism of schemes with $Y$ locally noetherian, and let $Z\subset X$ be an irreducible integral closed subscheme containing no fiber of $\pi$.

- Is the locus $Pic_\pi(Z)=\{y\in Y:Z_y \text{ is Cartier in }X_y\}$ closed in $Y$?
- If not, what extra hypotheses would make it closed?

As $\pi$ is smooth, Cartier and Weil divisors on fibers are the same, and as it is proper, the dimension of fibers is semicontinuous, so the issue is actually about components of smaller dimension in the fibers. Thus I'd drop the hypothesis on not containing fibers and replace it by the hypotheses that $Z$ dominates $Y$; then the question would be:

- Is the locus of $y$ such that $Z_y$ has a (possibly embedded) component of codimension $\ge 2$ in $X_y$ open in $Y$?

I have the feeling that this is related to Zariski's main theorem, although in the first formulation it seems closer to asking whether the subscheme of relative effective divisors is closed in the Hilbert scheme when $\pi$ is smooth. But I can't pin it down.

(In my situation, $Z$ is actually of codimension 2, and everything is over the complex field, but I don't think this is necessary).