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:
The Missing square puzzle problem is a good illustration. It illustrates a simple and universal problem/question/solution process, where "some illusion" causes complications.
There are a self-evident problem — the 1×1 hole in that arises on the second figure —, but the solution is evident only after we know (like an Egg of Columbus)... All people, experts and non-experts, agree that there is a problem.
The clever geometry-expert thinks in terms of "similar geometries that aren't perfect equivalents".
The normal user thinks, mistakenly, in terms of exact congruence. So, the use of wrong hypothesis, produce ill
The "XY problem" as a specialization of the "wrong hypothesis behaviour"
You want to solve the real question-
So, "XY Problem" is only another (more specialized) term to say "Use of wrong working hypothesis".
(separated from my other answer because this one has more opinion than explanation)
If you agree that the "XY Problem" is only another (more specialized) term for "Use of wrong working hypothesis", as illustrated and explained here, and similar to the "Einstellung effect" explained here by @Jonathan Benn…
We can think in terms of some main situations:
So let's put focus on the normal situation.
The "XY problem" is a valid problem!
The Stack Exchange question is to show a problem. If my question helps to show that the real problem is my wrong hypothesis, it is OK! It is a first step, and perhaps I will not need any other questions after obtaining the correct hypothesis (and trying to solve by myself).
Example. See my rushed question about "self axis that fails"; the real problem is that
Negotiating a union contract...
The XY problem is relevant to the difference between "interest" based bargaining (X) and "position" based bargaining (Y).
X = the employee's overall take-home pay has been stagnant for the last several years, while their cost of living continually increases; the employee needs more take-home pay.
Y = picking "you will not raise parking fees" as a hill to die on / the only "solution" to X that the employee is willing to accept.
Aiming for Y (taking a position) is aiming for a very specific and limited solution to the problem (X). It cuts the employee off from from the universe of solutions to the problem (X) by insisting that it only be solved in one very specific and limited way. If that way (Y) is, for whatever reason, objectionable to the employer, then there will be a stalemate -- a period of time where there is no solution to X, where a solution has to be acceptable to both sides.
If the employee can aim for X instead of Y, then the universe of solutions remains open / unrestricted, and the employer can be recruited to help find possible solutions to X. (Maybe it will turn out to be OK with the employee to raise parking rates, in exchange for a cost of living raise...)
This interest/position paradigm seems relevant to asking questions on SO, when OP asks for help making their position work. Sometimes you can cut to X by simply asking: "Why are you trying to do this?"; But I wonder if sometimes, aiming for Y isn't just as valuable. How many of us have ever tried to do something, just to see if we could? Anyone here running their own DNS server with some specific type of software for that very reason? :-) Might not be your best solution to X, but it's still interesting...