What is the XY problem?
When asking questions, how do I recognize when I'm falling into it? How do I avoid it?
What is it?
That is, you are trying to solve problem
This can lead to frustration by people who are trying to help you solve the problem because by the time you ask about it, the solution that you need help with might not have any obvious connections to the problem that you are trying to solve.
How to Avoid
To avoid falling into this trap, always include information about a broader picture along with any attempted solution. If someone asks for more information, or especially a more specific question, do provide details. If there are other solutions which you believe will be suggested and which you've already ruled out, then don't try to avoid going over them again – instead state why you've ruled them out, as this gives more information about your requirements and helps others provide better answers.
A recent IRC conversation for illustration:
This is easier to do in a fully interactive chat (regardless of what mode), but on a SE site, where you polish a post a bit, post it, and then have 5-30 mins, or longer, before feedback, it really helps to head in the right direction from the start.
The X-Y Problem, as it is sometimes called, is a mental block which leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. It often goes something like this
The problem occurs when people get their train of thought stuck on one approach and become unable to take a step back. Remaining open to having a new look at the bigger picture, these people might find their way back to X and continue searching for alternative solutions.
I'm adding an extra answer as a complement to the excellent answers already present.
An XY problem seems to be a subset of the Einstellung effect, where a problem-solver gets stuck on a particular solution and is unable to backtrack mentally to see potentially superior solutions. This psychological phenomenon affects everyone, novices and experts alike.
An example from Chess would be a person winning with a 5-move smothered mate in one game. The next game, the same player fails to see a faster 3-move mate because they are stuck on the idea of a 5-move mate.
Specific to Q&A, the perniciousness of an XY problem comes from the fact that it is frustrating for everyone involved: