  We investigate which relations for families of commuting matrices are stable
under small perturbations, or in other words, which commutative $C^*$-algebras
$C(X)$ are matricially semiprojective. Extending the works of Davidson,
Eilers-Loring-Pedersen, Lin and Voiculescu on almost commuting matrices, we
identify the precise dimensional and cohomological restrictions for
finite-dimensional spaces $X$ and thus obtain a complete characterization:
$C(X)$ is matricially semiprojective if and only if $\dim(X)\leq 2$ and
$H^2(X;\mathbb{Q})=0$. We give several applications to lifting problems for
commutative $C^*$-algebras, in particular to liftings from the Calkin algebra
and to $l$-closed $C^*$-algebras in the sense of Blackadar.
