The annotation of Psalm 34 (ESV) reads:
Of David, when he changed his behavior before Abimelech, so that he drove him out, and he went away.
Clearly this is a reference to 1 Samuel 21:10-15 (ESV):
And David rose and fled that day from Saul and went to Achish the king of Gath. And the servants of Achish said to him, “Is not this David the king of the land? Did they not sing to one another of him in dances,
‘Saul has struck down his thousands, and David his ten thousands’?”And David took these words to heart and was much afraid of Achish the king of Gath. So he changed his behavior before them and pretended to be insane in their hands and made marks on the doors of the gate and let his spittle run down his beard. Then Achish said to his servants, “Behold, you see the man is mad. Why then have you brought him to me? Do I lack madmen, that you have brought this fellow to behave as a madman in my presence? Shall this fellow come into my house?”
But the Psalm itself doesn't seem to be referring to that event. Why are they linked?
Clarification: I know that they are linked by the annotation, which is part of the received text of the Psalm, but I don't know what the connection is. Is it a Psalm written by David at the time or is it a later Psalm by David looking back to the event or is it a Psalm by another author reflecting back on the story about David?