  We study ($p$-harmonic) singular functions, defined by means of upper
gradients, in bounded domains in metric measure spaces. It is shown that
singular functions exist if and only if the complement of the domain has
positive capacity, and that they satisfy very precise capacitary identities for
superlevel sets. Suitably normalized singular functions are called Green
functions. Uniqueness of Green functions is largely an open problem beyond
unweighted $\mathbf{R}^n$, but we show that all Green functions (in a given
domain and with the same singularity) are comparable. As a consequence, for
$p$-harmonic functions with a given pole we obtain a similar comparison result
near the pole. Various characterizations of singular functions are also given.
Our results hold in complete metric spaces with a doubling measure supporting a
$p$-Poincar\'e inequality, or under similar local assumptions.
