The Gemara discusses Dovid’s suitability as king in connection with the pedigree and status issues surrounding the Davidic line. It is relevant to how חז״ל understood the legitimacy of his monarchy.
הָכִי קָאָמַר שָׁאוּל: אִי מִפֶּרֶץ אָתֵי, אִי מִזֶּרַח אָתֵי? אִי מִפֶּרֶץ אָתֵי — מַלְכָּא הָוֵי, שֶׁהַמֶּלֶךְ פּוֹרֵץ לַעֲשׂוֹת דֶּרֶךְ וְאֵין מְמַחִין בְּיָדוֹ. אִי מִזֶּרַח אָתֵי — חֲשִׁיבָא בְּעָלְמָא הָוֵי. מַאי טַעְמָא אֲמַר לֵיהּ שַׁאֵל עֲלֵיהּ, דִּכְתִיב: ״וַיַּלְבֵּשׁ שָׁאוּל אֶת דָּוִד מַדָּיו״, כְּמִדָּתוֹ. וּכְתִיב בֵּיהּ בְּשָׁאוּל: ״מִשִּׁכְמוֹ וָמַעְלָה גָּבוֹהַּ מִכׇּל הָעָם״. אֲמַר לֵיהּ דּוֹאֵג הָאֲדוֹמִי: עַד שֶׁאַתָּה מְשַׁאֵיל עָלָיו אִם הָגוּן הוּא לַמַּלְכוּת אִם לָאו, שְׁאַל עָלָיו אִם רָאוּי לָבֹא בַּקָּהָל אִם לָאו. מַאי טַעְמָא? דְּקָאָתֵי מֵרוּת הַמּוֹאֲבִיָּה. אֲמַר לֵיהּ אַבְנֵר, תְּנֵינָא: ״עַמּוֹנִי״, וְלֹא עַמּוֹנִית. ״מוֹאָבִי״, וְלֹא מוֹאָבִית. אֶלָּא מֵעַתָּה: ״מַמְזֵר״, וְלֹא מַמְזֶרֶת? ״מַמְזֵר״ כְּתִיב — מוּם זָר.
Rather, this is what Saul was saying, in his attempt to clarify David’s lineage: Does he come from the descendants of Perez, or does he come from the descendants of Zerah? What is the significance of this question? If he comes from Perez he will be king, as a king may breach [poretz] a way for himself and no one can stop him. And if he comes from Zerah he will be merely a man of importance, but not a king. The Gemara continues with its explanation: For what reason did Saul say to Abner that he should inquire about David? As it is written: “And Saul clad David with his apparel [maddav]” (I Samuel 17:38), which indicates that the clothes were of David’s size [kemiddato]. And it is written with regard to Saul: “From his shoulders and upward he was higher than any of the people” (I Samuel 9:2). Upon seeing that his clothes fit David, Saul began to fear that it might be David who was destined for the throne, and he therefore inquired into his background. At that point, Doeg the Edomite said to Saul: Before you inquire as to whether or not he is fit for kingship, inquire as to whether or not he is even fit to enter into the congregation. What is the reason for such doubts? It is that he descends from Ruth the Moabite, and Moabites are permanently barred from entering the congregation. Abner said to him: We already learned that there is no room for such concern. As the verse states: “An Ammonite or a Moabite shall not enter into the congregation of the Lord” (Deuteronomy 23:4), teaching that an Ammonite man is barred from entering into the congregation, but not an Ammonite woman; and similarly, a Moabite man is barred from entering into the congregation, but not a Moabite woman. Doeg said to him: However, if that is so, say that the verse that renders it prohibited for a mamzer to enter the congregation renders prohibited only a male mamzer, but not a female mamzer. Abner replied: It is written: “A mamzer,” which should be understood not as a noun but as an adjective, denoting a strange blemish [mum zar], one who is defective due to a forbidden relationship, and this applies to males and females alike.