Is the embedding dimension minus the dimension upper semicontinuous? For a Noetherian local ring $R$ with maximal ideal $\mathfrak{m}$ and residue class field $K$, consider the invariant $$\operatorname{def}(R) := \operatorname{dim}_K(\mathfrak{m}/\mathfrak{m}^2) - \operatorname{dim}(R).$$ Here are some questions:


*

*If $P \subseteq R$ is prime ideal, is it always true that $\operatorname{def}(R_P) \le \operatorname{def}(R)$?

*For which Noetherian rings is the function $\operatorname{Spec(}R) \to \mathbb{N}$, $P \mapsto \operatorname{def}(R_P)$ upper semicontinuous?

*Is there a name (and/or notation) that is commonly used for this invariant?
By a variant of the Jacobian criterion one can see that question 2 has an affirmaitve answer if $R$ is a finitely generated algebra over a perfect field. But are there more general results?
 A: Edit. Finally the proof was not so long, so I include it complete:
Question 3. Embedding codimension (sometimes simply codimension).
Question 1. I don't have access here to "Lech, Inequalities related to certain couples of local rings, Acta math. 112 (1964), 69-89", but maybe that paper will answer it better, I can't remember. However, here is a proof when $A$ is quasi-excellent:
Notation: dim = Krull dimension, edim = embedding dimension, codim = edim - dim = embedding codimension.
Lemma 1. Let $(A,m) \to (B,n)$ be a flat homomorphism of noetherian local rings. Then dim $A$ + dim $B/mB$ = dim $B$.
Lemma 2. Let $(A,m) \to (B,n)$ be a flat homomorphism of noetherian local rings with regular closed fiber $B/mB$. Then edim $A$ + edim $B/mB$ = edim $B$.
Deduction of Lemma 2 from Andre-Quillen homology (references (x.y) are to Result y from Chapter x in Andre, Homologie des algebres commutatives, Springer, 1974): let $f:(A,m,k) \to (B,n,l)$ be a flat homomorphism of noetherian local rings with regular closed fiber. Let $p=n/mB$ be the maximal ideal of $B/mB$. It is sufficient to prove that we have an exact sequence of $l$-vector spaces
$$0 \to m/m^2 \otimes_k l \to n/n^2 \to p/p^2 \to 0.$$
But this exact sequence is a part of the Jacobi -Zariski exact sequence in Andre-Quillen homology (5.1) associated to $B \to B/mB \to l$:
$$H_2(B/mB,l,l) \to H_1(B,B/mB,l) \to H_1(B,l,l) \to H_1(B/mB,l,l) \to H_0(B,B/mB,l).$$
$H_2(B/mB,l,l)=0$ since $B/mB$ is regular by (6.26).
$H_1(B,B/mB,l)=H_1(A,k,l)= m/m^2 \otimes_k l$ by (4.54) (since $f$ is flat) and (6.1) respectively.
$H_1(B,l,l)= n/n^2$, and $H_1(B/mB,l,l)= p/p^2$ by (6.1).
$H_0(B,B/mB,l)=0$ by (4.60).
Another proof without Andre-Quillen homology can be found in arXiv:1205.2119v3, Lemma 3.1.
Corollary 3. Let $(A,m) \to (B,n)$ be a flat homomorphism of noetherian local rings with regular closed fiber. Then codim $A$ + codim $B/mB$ = codim $B$. In particular, codim $A \leq $ codim $B$. Another (trivial) particular case is codim $A$ = codim $\hat{A}$.
Definition. We say that a noetherian local ring is a G-ring if the completion homomorphism $A \to \hat{A}$ is regular. By Matsumura, Commutative Algebra, (33.C) Theorem 75 page 251 this is equivalent to the usual definition of G-ring, and by Theorems, 73, 76 and 77 this is also equivalent to be quasi-excellent (since it is local).
Corollary 4. Let $A$ be a local G-ring. Then for any prime ideal $p$ of $A$ we have codim $A_p \leq $ codim $A$.
Proof. The result is valid for a complete ring $A$ (choose a regular local ring $S$ with the same embedding dimension of $A$ such that $A=S/I$ and localize), so codim $\hat{A}_q \leq $ codim $\hat{A}$ = codim $A$. Since $A \to \hat{A}$ is faithfully flat, there exists a prime ideal $q$ of $\hat{A}$ contracting to $p$. The local homomorphism $A_p \to \hat{A}_q$ is regular and so by Corollary 3 codim $A_p \leq $ codim $\hat{A}_q$.
Question 2.
For excellent rings. An indirect proof, but short enough to be written here, is as follows. Embedding dimension is the diference between the invariant $\delta_2$ in "Ragusa, On Opennes ..." which is upper semicontinuous by Proposition 3.6 in that paper, and the complete intersection defect which is upper semicontinuous by the main theorem in "Alonso-Rodicio, On the upper ...". So for each $n$ the set of primes $p$ such that codim $A_p > n$ is constructibe. By Corollary 4 it is also stable under specialization. So by Matsumura, Commutative Algebra, (6.G) Lemma, page 46, it is closed. 
