All I know of "Fair use" is from reading Wikipedia: see Fair use.
My assumption is that: 1) posting 1 or two bars could be ok and within fair use; 2) posting the whole piece is not ok. Is that correct?
That's my assumption too. Similarly (on other SE sites) I'll upload a paragraph of an article, a screenshot of a page of a book of text, if I want to ask about it or reference it in an answer.
And if so, is there a strict borderline (e.g. a number of bars or a fraction(%) of the piece)?
I don't think so.
Per Wikipedia, "Amount and substantiality" is important (less is better), and "Effect upon work's value" is also important.
IMO my uploading just a couple of staves (for critical or academic reasons) won't reduce the market's demand for the complete work from the copyright owner, so my doing that should be considered fair, not "infringing".
In the case in question it's an extract from a book -- i.e. it's not the whole book.
Also the music is from the 18th or 19th century (therefore out of copyright, I assume), although I don't know whether the book can copyright its representation of the music.
Also the book and its author are attributed (referenced, mentioned in the question). People on Stack Exchange like that: they'll ask you to reference (with a citation or hyperlink) what you quote. Doing that is mentioned in Wikipedia as one of the Additional factors:
As explained by Judge Leval, courts are permitted to include additional factors in their analysis.
One such factor is acknowledgement of the copyrighted source. Giving the name of the photographer or author may help, but it does not automatically make a use fair.
I'd guess that, if anything, that would tend to help rather than hurt the copyright holder commercially (and would therefore please them, and make them disinclined to ask you to remove your posted extract) -- because a referenced quote of a snippet acts as kind of advertisement for the whole work.
Note there's a specific help centre topic about to reference work written by others: How to reference material written by others -- it says things including ...
- Provide a link to the original page or answer
- Quote only the relevant portion
- Provide the name of the original author
Do not copy the complete text of external sources; instead, use their words and ideas to support your own. And always give proper credit to the author and site where you found the text, including a direct link to it.
... which are compatible with what I wrote in the answer above.