Is open() on something other than /dev/stderr returning 2 a thing? Or is there something screwy that was causing it to get associated with the open database file?
You’re right, but it’s more than that: stderr is defined to be fd 2. Whatever is assigned to that fd, is the stderr. If you close it and open something else, that something becomes your stderr.
That is a guarantee by an actual standard. More specifically, it's one of the things that POSIX.1-2017 guarantees but the ISO C standard does not. In practical terms this means that all Unix-like systems use the traditional fd numbers for stdin, stdout and stderr but it's possible that non-Unix systems might not.
> The following symbolic values in <unistd.h> define the file descriptors that shall be associated with the C-language stdin, stdout, and stderr when the application is started:
> STDIN_FILENO Standard input value, stdin. Its value is 0.
> STDOUT_FILENO Standard output value, stdout. Its value is 1.
> STDERR_FILENO Standard error value, stderr. Its value is 2.
It is not only guaranteed by the POSIX standard, as pointed out by the siblings, it is necessary for standard shell scripting idioms to work, e.g. "command1 2>&1 | command2".