Is there any reason why argument 'nth' is not checked earlier? IMO, it is more natural "if (nth <= 0)..." is placed right after "nth = DatumGetInt32...".
Attached is the patch which does this.
Hmm, shouldn't we check if the argument of nth_value is null before we check if it is greater than zero? So maybe we need to do this.