Postgres: why did this trigram index slow down regexp queries?
I expected the trigram index to figure out immediately there's no second 0 string anywhere


'second' and '0' are separate words, so it cannot detect their joint absence as such.  It seems like it could detect the absence of '  0', but this comment from "contrib/pg_trgm/trgm_regexp.c" seems pertinent:

 * Note: Using again the example "foo bar", we will not consider the
 * trigram "  b", though this trigram would be found by the trigram
 * extraction code.  Since we will find " ba", it doesn't seem worth
 * trying to hack the algorithm to generate the additional trigram.


Since 0 is the last character in the pattern string, there will be no trigram of the form " 0a", either, so it just misses that opportunity.

Even if it were not for this limitation, your approach seems extremely fragile.