Shmuel HaKatan: Pirkei Avot 4:24 cites a pasuk of "B'nephol Oyveycha Al Tismach," at the fall of your enemy, do not rejoice (Mishle 24:17-18). Brisker Rav based on an insight of Nefesh h'Chaim, Rav Chaim of Volozhin, that only a prophet could formulate the Amidah. Rav Schwab explains the Rambam in Hilchos Tefillah, which states that although prayer according to the dictates of the Torah had no set formulation, the Rabbis later formulated a set order of prayer. The Anshei Knesset H'Gidolah had the last of the prophets. In preparing the nation for an era without prophecy (Yoma 49a), they formulated the text of the Amidah. Until that time, sparks of prophetic vision were common. Therefore, people were able to formulate their prayers.
Rav Yitzchak Sender in The Commentators Siddur suggests that Shmuel h'Kattan was chosen since he was worthy of receiving prophecy, but his generation was not worthy (Sanhedrin 11a). Therefore, he could write a new blessing to include in the Amidah.
Despite Shmuel haKattan's being a highly respected, holy person worthy of receiving prophecy, the Gemara questions whether he had turned to be a heretic. The Gemara cites the example of Yochanan KoHen Gadol as a prominent individual who became a heretic. This religious environment offers a background for the tough standards of who Rabban Gamiel allowed to study in the basis medrash, only someone whose "tocho kiboro," whose insides matched their outsides.
Gemara Megillah discusses the halacha that Tefillah (Amidah/Shemoneh Esrei) needs to be said in order. After the siege of Jerusalem and the subsequent establishment of the Sanhedrin in Yavne by Rav Yochanan, Rabban Gamliel eventually became the Nasi. Gemara Meglliah explains that the seder (order) of the Amidah was forgotten, and Shimon HaPakuli arranged the order in front of Rabban Gamliel. Gemara Brachot 28b gives hints for the source of 18 for Tefillah, while Gemara Megillah shows sources for the sequence of Tefillah. Rabbi Levi explains that the 19th bracha was added in Yavne for the heretics. The Gemara then cites the Baratia with the account of Rabban Gamliel asking for someone to Tiken a bracha against the heretics. Shmuel HaKattan tiken the baracha. Interestingly, the Gemara records another year, Shmuel Katan forgot the tefillah he instituted. This hints that the underlying motivation for the insertion of the blessing was to select out heretics from being the prayer leader. The explicit inclusion of noserim in the geniza text thus suggested that the addition of this blessing was a deliberate move to exclude Christians from the synagogue, an understanding reinforced by a late midrashic tradition that requires one leading the prayers who errs in the blessing to repeat it correctly, thus cursing himself or he would be removed from leading the congregation.
הלכה: וְלָמָּה שְׁמוֹנֶה עֶשְׂרֵה. רִבִּי יְהוֹשֻׁעַ בֶּן לֵוִי אוֹמֵר כְּנֶגֶד שְׁמוֹנָה עָשָׂר הַמִּזְמוֹרוֹת שֶׁכָּתוּב מֵרֹאשׁוֹ שֶׁל תִּילִים עַד יַעַנְךָ ײ֨ בְּיוֹם צָרָה. אִם יֹאמֵר לָךְ תְּשַׁע עֶשְׂרֵה הֵן אֱמוֹר לוֹ לָמָּה רָֽגְשׁוּ גּוֹיִם לֵית הוּא מִינּוֹן. מִכַּן אָֽמְרוּ הַמִּתְפַּלֵּל וְלֹא נֶעֱנֶה צָרִיךְ תַּעֲנִית. אָמַר רִבִּי מָנָא רֶמֶז לְתַלְמִיד חָכָם שֶׁאָדָם צָרִיךְ לוֹמַר לְרַבּוֹ תִּשָּׁמַע תְּפִילָּתָךְ. אָמַר רִבִּי סִימוֹן כְּנֶגֶד שְׁמוֹנֶה עֶשְׂרֵה חוּלְיוֹת שֶׁבְּשִׁדְרָה שֶׁבְּשָׁעָה שֶׁאָדָם עוֹמֵד וּמִתְפַּלֵּל צָרִיךְ לָשׁוּחַ בְּכוּלָּן. מַה טַעַם כָּֽל עַצְמוֹתַי תֹאמַרְנָה ײ֨ מִי כָמוֹךָ. אָמַר רִבִּי לֵוִי כְּנֶגֶד שְׁמוֹנֶה עֶשְׂרֵה הַזְכָּרוֹת שֶׁכָּתוּב בְּהָבוּ לַײ֨ בְּנֵי אֵלִים. אָמַר רִבִּי חוּנָה אִם יֹאמַר לָךְ אָדָם שְׁבַע עֶשְׂרֵה אִינּוּן. אֱמוֹר לוֹ שֶׁל מִינִין כְּבַר קָֽבְעוּ חֲכָמִים בְּיַבְנֶה. הָתִיב רִבִּי אֶלְעָזָר בֵּי רִבִּי יוֹסֵי קוֹמֵי רִבִּי יוֹסֵי וְהָֽכְתִיב אֵל הַכָּבוֹד הִרְעִים. אָמַר לֵיהּ וְהָתַנִּי כּוֹלֵל שֶׁל מִינִין וְשֶׁל פּוֹשְׁעִים בְּמַכְנִיעַ זֵדִים. וְשֶׁל זְקֵנִים וְשֶׁל גֵּרִים בְּמִבְטָח לַצַּדִּיקִים וְשֶׁל דָּוִד בְּבוֹנֶה יְרוּשָׁלַיִם. אִית לָךְ מַסְפְּקָא לְכָל־חָדָא וְחָדָא מִינְהוֹן אַדְכָּרָה. רִבִּי חֲנִינָה בְשֵׁם רִבִּי פִינְחָס כְּנֶגֶד שְׁמוֹנֶה עֶשְׂרֵה פְעָמִים שֶׁאָבוֹת כְּתוּבִין בַּתּוֹרָה. אַבְרָהָם יִצְחָק יַעֲקֹב. אִם יֹאמַר לָךְ אָדָם תְּשַׁע עֶשְׂרֵה הֵן אֱמוֹר לוֹ וְהִנֵּה ײ֨ נִצָּב עָלָיו לֵית הוּא מִינְהוֹן. אִם יֹאמַר לְָךְ אָדָם שְׁבַע עֶשְׂרֵה הֵן אֱמוֹר לוֹ וְיִקָּרֵא בָהֶם שְׁמִי וְשֵׁם אֲבוֹתַי אַבְרָהָם וְיִצְחָק מִינְהוֹן.
HALAKHAH: And why 18? Rebbi Joshua ben Levi says, corresponding to the 18 psalms that are written from the start of Psalms to (Ps. 20:2): “May the Lord listen to you on the day of need.” If somebody would say to you they are 19, tell him (Ps. 2:1) “Why are the Gentiles in upheaval” is not in the count. From here they said that one who prayed and was not answered needs to fast. Rebbi Mana said, a hint that a Torah student must say to his teacher: May your prayers be answered. Rebbi Simon said, corresponding to the 18 bones in the spine since when a person prays he has to bend all of them bowing down. What is the reason? (Ps. 35:10) “All my bones shall say, o Eternal, who is like You?” Rebbi Levi said, corresponding to the 18 Divine Names written in (Ps. 29) “Give to the Eternal, o sons of the mighty.” Rebbi Ḥuna said: If it were said that they are 17, tell him the one against sectarians the Sages fixed already at Jabneh. Rebbi Eleazar ben Rebbi Yose objected before Rebbi Yose, but is it not written (Ps. 29:3) “the God of Glory thundered”? He said to him: Has it not been stated: He takes together the one against sectarians and sinners in “He Who subdues offenders”, the one for elders and proselytes in “Assurance to the righteous”, and the one for David in “He Who builds Jerusalem.” Do you have enough mentions of the Divine name for each one of them?
Rebbi Ḥanina in the name of Rebbi Phineas: Corresponding to the eighteen times the Patriarchs are mentioned together in the Torah, Abraham, Isaac, Jacob. If somebody will tell you that there are nineteen, tell him that (Gen. 23:13) “Behold, the Eternal was standing over him” is not counted. If somebody will tell you that there are seventeen, tell him that (Gen. 48:16) “My name should be called over them, as well the names of my fathers Abraham and Isaac” is counted.
Chareidim and Pnei Moshe, Yerushalmi Berachos 48a, and Shenos Eliyahu explain the Gemara's question as follows: This objection is not directed toward Rabbi Levi's explanation in particular but is a general objection, which calls into question any scriptural parallel that implies 18 blessings. For in fact, the number of blessings instituted in the times of Ezra and the great assembly was not 18, but 17. Although another blessing was added in later years – namely, the blessing of the sectarians, which was added under Rabban Gamliel in Yavne – this was not instituted as a full-fledged blessing, but only as a lesser addendum to the regular blessings, so that so does not require a scriptural source. Since the original blessings number only 17, the Gamarra should not be advancing scriptural sources for 18.
The Chareidim writes that since the Sanhedrin of Rabban Gamiel was the end of the era of the Sanhedrin, the addition of the beracha for the minim is not considered as an addendum and is of equal status to the other 18 berachos. The Gemara explains that it too requires a scriptural remez.
Mahara Fulda Berachos 48b writes the beracha for the Minim was established in Yavneh after the establishment of 18 berachos by the Anshei Knesset H'Gidolah and only need "remez" only for their institution of 18 berachos and not the addendum of Rabban Gamielel.
שמונה עשרה שאמרו חכמים כנגד שמונה עשרה אזכרות שבהבו לה' בני אלים. וכולל של מינים בשל פרושין ושל גרים בשל זקנים ושל דוד בירושלים ואם אמר אלו לעצמן ואלו לעצמן יצא.
Talmudic Israel (c.190 - c.230 CE)
Eighteen Berachot (blessings) that the Sages have established [for the prayer of Shemoneh Esreh have been established] corresponding to eighteen mentionings [of God’s name] that are in [the chapter of Tehillim that begins with] “Ascribe to God, children of princes…” (Tehillim 29) And [a person] should include [the Beracha against] the heretics into [the Beracha] for the Separatist Jews, and [the Beracha] for the converts into [the Beracha] for the elders, and [the Beracha] for [King] David into [the Beracha] for [the rebuilding of] Jerusalem. But if he said each one of them separately, he has fulfilled his obligation [of praying Shemoneh Esreh].
The Tosefta begins with various insertions that were absorbed into preexisting blessings in the Amidah that fit the blessing's theme, maintaining the number of 18 blessings. During this time, there was a need to make three separate insertions into the Amidah: for the Minim (Heretics), Geirim (converts), and David (Mashiach). They share the theme of reaction to the confusion early Christianity caused, namely, how to distinguish between those people who are truly joining the rank of Judaism vs. those who are the followers of Jesus. The confusion was so widespread across several communities that the Tsoefta notes that changes were added to the daily Amidah.
Each need was placed in a preexisting blessing, minim into the separatists, the converts were given the treatment for the righteous, and the desire for Mashiach was placed within the blessing for Jerusalem.
L'Chatchilah, ideally, the Amidah should maintain 18 blessings. B'dieved, if it happened that blessings were added, the obligation has still been fulfilled.
The Tosefta predates the adoption of Christianity in Rome in 380 CE by emperor Theodosius issuing the Edict of Thessalonica, which made Christianity, specifically Nicene Christianity, the Roman Empire's official religion. The earlier versions were likely written by the Pharisaic in their struggle against internal and external religious threats, including early Christians.
Later during the time of early Christianity, Rabban Gamliel and his court asked Shmuel ha'Katan to write a new blessing instead of adding Minim, who Rambam writes were confusing the masses and leading away from G-d.
בִימֵי רַבָּן גַּמְלִיאֵל רַבּוּ הָאֶפִּיקוֹרוֹסִין בְּיִשְׂרָאֵל וְהָיוּ מְצֵרִים לְיִשְׂרָאֵל וּמְסִיתִין אוֹתָן לָשׁוּב מֵאַחֲרֵי הַשֵּׁם. וְכֵיוָן שֶׁרָאָה שֶׁזּוֹ גְּדוֹלָה מִכָּל צָרְכֵי בְּנֵי אָדָם עָמַד הוּא וּבֵית דִּינוֹ וְהִתְקִין בְּרָכָה אַחַת שֶׁתִּהְיֶה בָּהּ שְׁאֵלָה מִלִּפְנֵי הַשֵּׁם לְאַבֵּד הָאֶפִּיקוֹרוֹסִין וְקָבַע אוֹתָהּ בַּתְּפִלָּה כְּדֵי שֶׁתִּהְיֶה עֲרוּכָה בְּפִי הַכּל. נִמְצְאוּ כָּל הַבְּרָכוֹת שֶׁבַּתְּפִלָּה תְּשַׁע עֶשְׂרֵה בְּרָכוֹת:
Middle-Age Egypt (c.1176 - c.1178 CE)
In the days of Rabban Gamliel, the number of heretics among the Jews increased. They would oppress the Jews and entice them to turn away from God.
Since he saw this as the people's greatest need, he stood, and his court established one blessing that contained a request to God to destroy the heretics. He inserted it into the Shemoneh Esreh to arrange it in all mouths. Consequently, there are nineteen blessings in the Tefillah (Shemoneh Esreh).
Rambam explains the impact of the political environment that necessitated Rabban Gamiel to strengthen the Amidah by formalizing the order of the blessings and inserting a new blessing. The urgency and confusion of Rabban Gamiel's era with internal heretics and Jewdo Christians led Rabban Gamiel to add the precedent set by the Anshei Knesset HaGedolah, credited with introducing the Amidah and establishing its sequence. The Rambam added that the danger was in the success of the heretics in enticing Jews to turn away from G-d. וּמְסִיתִין אוֹתָן לָשׁוּב מֵאַחֲרֵי הַשֵּׁם. Rambam writes וְכֵיוָן שֶׁרָאָה שֶׁזּוֹ גְּדוֹלָה מִכָּל צָרְכֵי בְּנֵי אָדָם, this was the greatest need of people. Rambam explains that Rabban Gamliel placed it into the Amidah to ensure it would fluid in the mouths of all people. Given the import of the Amidah as replacing the sacrificial service, placing a prayer against the heretics in the Amidah ensured that all people would know of its importance since it warranted such placement.
Like the Anshei Knesset HaGedolah, the rabbis in Yavneh, under the leadership of Rav Yochanan and later Rabban Gamliel, faced a critical juncture in Jewish history. They recognized that the nation's needs were transitioning. Each recognized the impact of giving prayer more structure. Their innovations in prayer did not take hold, leading Rabban Gamiel to give Shimon HaPikuli the responsibility for ordering the Amidah.
כָּל תִּשָּׂא עָוֹן וְקַח טוֹב (הושע יד, ג). אָמְרוּ יִשְׂרָאֵל, רִבּוֹנוֹ שֶׁל עוֹלָם, בִּזְמַן שֶׁבֵּית הַמִּקְדָּשׁ קַיָּם, הָיִינוּ מַקְרִיבִין קָרְבָּן וּמִתְכַּפֵּר. וְעַכְשָׁו, אֵין בְּיָדֵינוּ אֶלָּא תְּפִלָּה. טוֹב, בַּגִּימַטְרִיָּא שְׁבַע עֶשְׂרֵה. תְּפִלָּה, תְּשַׁע עֶשְׂרֵה. הוֹצֵא מֵהֶן בִּרְכַּת הַזֵּדִים שֶׁתִּקְּנוּ בְּיַבְנֶה, וְאֶת צֶמַח דָּוִד שֶׁתִּקְּנוּ עַל שׁוּם בְּחָנֵנִי ה' וְנַסֵּנִי (תהלים כו, ב).
"Forgive all guilt and take the good (tov)" (Hosea 14:3). Israel said, "Master of the world, at the time that the Temple existed, we would offer a sacrifice and be cleansed. But now all we have in our hand is prayer." The numerical value of tov is seventeen. Prayer [consists of] nineteen [blessings]. Take away from them the blessing for the malfeasers that was composed at Yavneh, and "Let the sprout of David blossom," which they ordained for the sake of "Probe me, Lord, and try me" (Psalms 26:2).
The Midrash states that there was a core of 17 blessings, and 2 were added (for the Zeidim and Es Tzemach David). The Midrash cites Tehillim 26:2 as the source of Es Tzemach David.
אלא שזה מתפלל ח"י וזה מתפלל י"ט. פי' המורה סתם תפלה קרוי ח"י דברכת הבייתוסים ביבנה תקנוה כדאמרי' בברכות (דכ"ח ע"ב). ואינו נ"ל דכיון דשמואל הקטן תיקנה היאך שוגה התנא ח"י ונ"ל דלהכי תני ח"י דתניא בתוספתא במס' ברכות בשלהי פ"ג: ח"י ברכות שאמרו חכמים כנגד חי הזכרות שבהבו לה' בני אילים. וכולל של בייתוסים ושל פושעים במכניע זדים, ושל זן ושל גרים במבטח לצדיקים, ושל דוד בבונה ירושלי'. ואם אמר אלו לעצמן ואלו לעצמן יצא ומייתי' לה בירו' בפ' היה קורא ובפ' תפלת השחר מוכח מכאן דלאחר שתיקנו ברכות הבייתוסים מונה אותם התנא ח"י. וטעמא דמלתא שהיו כוללים של דוד בבונה ירושלים. וכך אתה מוצא בקריבות שנתקנו על ח"י ברכות בפורים וביום הושענא ובי"ז בתמוז ובי' בטבת הוא כולל של דוד בבונה ירושלים (ת"י ועיין במדרש רבה ובתנחומא סוף פ' קרח על פי' ונשלמה פרים שפתינו אמרו ישראל רבש"ע בזמן שבה"ק קיים היינו מקריבים קרבן ומתכפר ועכשיו אין בידינו אלא תפלה טוב (בגי' י"ז), תפלה י"ט ברכות הוצא משם ברכות המינין שתיקנוה ביבנה וכן צמח דוד שתקנן אחריו עכ"ל) אבל התלמוד שלנו מוכיח שאנו מברכים של דוד בפני עצמו ושל בונה ירושלים בפני עצמו דהכי אמרי' בפ' הקורא את המגילה למפרע שמסדר שם כל ח"י ברכות ומונה ברכת הצדיקים לאחריה בונה ירושלים לאחריה ברכת דוד ואע"פ שבתחילה אמר ח"י דאמרי' התם תפלה מנלן פירוש שאם אמרם למפרע לא יצא דתניא שמעון הפקולי הסדיר ח"י ברכות לפני ר"ג על הסדר ביבנה וא"ר ירמיה ואיתימא ר' חייא בר אבא ואמרו לה במתניתא תנא ק"ך זקנים ומהם כמו נביאים תקנו ח"י ברכות על הסדר ואח"כ הולך ומונה י"ט א' אבות ב' גבורות ג' קדושת השם ד' חונן הדעת ה' השיבנו ו' סלח לנו ז' גאולה ח' רפואה ט' ברכות השנים י' קבוץ גליות י"א השיבה שופטינו י"ב ברכת הבייתוסים י"ג על הצדיקים י"ד ברכת ירושלים ט"ו ברכת דוד ט"ז שומע תפלה וג' אחרונות הרי י"ט לאחר שהובעה ברכת הבייתוסים בימי ר"ג כדאמרי' בפ' תפלת השחר (דכ"א ע"ב) ר"ג אומר בכל יום מתפלל אדם ח"י ומקשה תלמוד הני ח"י י"ט הויין א"כ ברכת הבייתוסים ביבנה תקנוה. ת"ש שמעון הפקולי הסדיר ח"י ברכות על הסדר לפני ר"ג א"ל ר"ג לחכמים כלום יש כאן אדם שיודע לתקן ברכה לבייתויסים ירד שמואל הקטן ותקנה ואע"ג שתיקן שמואל הקטן ברכות הבייתוסים לא היו אומרים אלא ח"י ברכות שהיו כוללים ברכת דוד בבונה ירושלים כדתניא בתוספתא דברכות. אבל התלמוד שלנו סובר כדתנ'י בההיא ברייתא ואם אמר אלו לעצמן ואלו לעצמו יצא וכך נהגנו אנו (בתלמוד) [כתלמוד] שלנו שאנו מברכים י"ט ברכות של דוד בפני עצמה ושל בונה ירושלים בפני עצמה. והכי אמרי' בפ' ע"פ אמר רבא בר שילא בצלותא מצמיח קרן ישועה בהפטרתא מגן דוד ועשיתי לך שם גדול כשם הגדולים אשר בארץ תני רב יוסף זה מגן דוד אלמא מברכים מצמיח קרן ישועה במלכות בית דוד ואינו כוללה עם בונה ירושלים. ופליגי בה אתלמוד שלנו עם תלמוד ירושלמי דבפ' בתרא דר"ה גרסי' בירו' ר' אבא בשם אבא בר רב הונא בתפלה הוא אומר אלקי דוד ובונה ירושלים בנביא אומרים אלקי דוד ומצמיח ישועה. והכי גרסי' נמי בפ' תפלת השחר בירו' בנוי לתלפיות תל שהכל פונים לו תל שכל הפיות מתפללים עליו בברכה וק"ש ובתפלה בברכה בונה ירושלים בק"ש פורס סוכת שלום ומנחם ציון ובונה ירושלים בתפלה אלקי דוד ובונה ירושלים. אלמא תלמוד שלנו סובר שאינו כוללן ותלמוד ירו' סובר שהוא כוללן. ועוד פליגי בהא דתלמוד יר' סובר שמברכים אלקי דוד ובפ' חלק (דק"ז) בהלכה וד' הדיוטות גרסי' אר"י אמר רב לעולם לא יבאי אדם עצמו לידי נסיון שהרי דוד הביא עצמו לידי נסיון ונכשל מיד אמר דוד נפני הקב"ה רבש"ע מפני מה אומים אלקי אברהם אלקי יצחק ואלקי יעקב ואין אומרים אלקי דוד א"ל לדידהו נסינא יתהון וכו' אלמא שאין אומרים אלקי דוד ולפי דברי הירו' הרי אומרים אותו בבונה ירושלים. ועוד מאי דתני רב יוסף ועשיתי לך שם גדול כשם הגדולים אשר בארץ זה מגן דוד למה לא אמר זה אלקי דוד ובונה ירושלים. אל אש"מ התלמוד שלנו סובר שאין מזכירים אלקי דוד כלל הלכך טעות הוא לאומרו [אפילו] לדברי היר' שאומר אותו ה"מ בכולל ברכת דוד עם בונה ירושלים. אבל אנו שאנו עושים כפי התלמוד שלנו ואין אנו כוללים אלא מברכים בברכת דוד מצמיח קרן ישועה הדיוטות גדולה היא לומר אלקי דוד בבונה ירושלים שבברכתו אין אומרים אלקי דוד והיאך נאמר אותו בברכת ירושלים אם יש לאומרו נאמר אותו בברכתו ולא נחתום מצמיח קרן ישועה אלא אלקי דוד. אלא ודאי בורות וטעות הוא לאומרו כלל כי כל היכא דפליגי גתלמוד בבלי עם תלמוד ירו' הלכה כתלמוד בבלי כ"ש שאניו עושים כתלמוד בבלישלא לכלול ואנו חותמים בברכת דוד מצמיח קרן ישועה מה טיבו של אלקי דוד בבנין ירושלים (ת"י דברי הרב ז"ל הביאם מר אבי זלה"ה בס' בית יצחק כ"י על ש"ט א"ח (ס"ס קי"ח) ופלפל עליהם באורך וברוחב:
Trani, Italy (c.1200 - c.1250 CE)
“This one prays 18 and this one prays 19.”
The explanation is that regular Tefillah is called 18, since the blessing for the Biytusim was tiken/established in Yanveh as stated in Brachot 28a. This is not clear to me. Since Shmuel Hakattan tiken, how does the Tanna error [in Taanis 13a] 18? It appears to me; this is the reason teaching of 18. (As is explained in the Tosefta Brachot Chapter 3: The 18 blessings that the chachamim said are parallel to the 18 mentions of G-d name in Havu L’Shem Bnei Eilim (Tehillim 29). They included the blessing regarding Baytusim and Poshim in the blessing Machnea Zedim, the bracha regarding Zan and converts in Mivtach l’Tzadikim, and the blessing regarding David in Boneh Yerushalayim.) And if these were said privately, they were fulfilled.)
The explanation of the Mishna Barchot, Chapter 3, Haya’ah Koreh, is that the morning tefillah (Amidah), it is evident from here [Taanis 13a], after the institution of the blessing regarding the Bytusim, the Tanna [Taanis 13a] accounted the blessing numbering 18. The reason is that they included the blessing regarding David in Boneh Yerushalayim.
So, one finds in the collections they established 18 blessings on Purim, Hoshana Rabba, 17th Tammuz, and the 10th of Tevet. They would incorporate the blessing for David into Boneh Yerushalayim. [Gary Lelonek's understanding: If an additional blessing was inserted into the Amidah, the blessings of David and Boneh Yerushalayim were combined]
(Look in the Midrash Rabba and Tanchuma at the end of Parshat Korah on the heading of Neshlama Parim Sefateynu: Israel said: Master of the World, At the era of the Bais HaMikdash, we would bring sacrifices, and achieve atonement. Now we only have Tefillah TOV in our hands. The numerical value of TOV is 17. The Tefillah of 19 blessings subtracts the blessing for the Minim that was established in Yavneh and the blessing Tzemach David that was established afterward. Ad Kaan L’Shono)
But our Talmud shows that we bless David in its own blessing and regarding Boneh Yerushalayim in its own blessing. This is evident from Megillah 18a, Chapter Hakoreh Megillah L’Maphrea: They put in order all 18 blessings and included the blessing for the tzadikim after Boneh Yerushalayim in the count. After Boneh Yerushalayim, the blessing for David. And even though the opening statement said 18,[2] as it says there:
Tefillah from where? It is explained if they are read out of order, one does not fulfill their obligation since it was taught in a Baraita that Shimon HaPikulei ordered the 18 blessing in front of Rabban Gamliel in their order in Yavneh. And Rabbi Yirmeya and some say Rabbi Chiya bar Abba that it says in a Mishna it was taught that 120 elders, among whom were prophets, established 18 blessings in order.
Afterward [the Gemara Megillah 18a] goes and lists 19 blessings. 1. Avot. 2. Gevurot. 3. Kedushat HaShem. 4. Chonen Hadaat. 5. HaSheveynu. 6. Selach Lanu. 7. Geulah. 8. Refuah. 9. Brachot HaShanim. 10. Kebutz Goliot. 11. HaSheva Shofteinu. 12. Birchat HaBytusim. 13. Al HaTzadikim. 14. Birchat Yerushalayim. 15. Briochat David. 16. Shomeh Tefillah and the last 3, which sum to 19 blessings, after the inclusion of the blessing for Bytusim in the days of Rabban Gamliel. As stated in Brachot 21b:
Every day a person should pray 18 blessings and the Gemara asks, these 18, there are 19?! If so, the blessing for the Bytusim was established in Yavneh. Come and hear, Shimon HaPakuli arranged the eighteen blessings, already extant during the period of the Great Assembly, before Rabban Gamliel, the Nasi of the Sanhedrin, in order in Yavne. Rabban Gamliel said to the Sages: Is there any person who knows to tiken/institute the blessing of the Bytusim? Shmuel HaKatan, one of that generation's most pious men, stood and instituted it.
Even though Shmuel HaKattan established the blessing regarding the Bytusim, they still only read 18 blessings since they would include the blessing for David in Boneh Yerushalayim as is taught in the Tosefta in Brachot.
But our Talmud holds like the teaching of that Baratia, “and if one reads this privately and this one reads privately [18 or 19 blessings], they fulfill their requirement. And this is the way we practice like our Talmud, we recite 19 blessings with the blessing of David by itself and the blessing of Boneh Yerushalayim by itself.
למשומדים אל תהי תקוה, ומלכות זדון מהרה תעקר, תשבר ותכניע בימינו והנוצרים והמינים והמלשינים והמוסרים כולם כרגע יאבדו וכל אויבי עמך ושונאיהם וצורריהם ומבקשי רעתם מהרה יכנעו ויישמדו ושבור עול הגוים מעל צוארינו ואל תתן לכולם ולכל אויבי נפשינו ומבקשי רעתינו ברוך אתה ה’ שובר אויבים ומכניע זדים
The Genizah reflects the diversity of nusaḥ ha-tefillah (the liturgy text) in various communities, a diversity lost as tefillah became standardized and homogenized, a process abetted by the advent of printing.
The Rishonim had a text that started with L'Shumadim and Malchut Zadon (Chasedei Ashkanaz, Abudharam, Rokeach, Rabbi Yehuda ben Yakar in Tefillah L'Moshe. The Rabbi Yehuda ben Yakar text cited in the Mossad HaRav Kook, Siddur Torat Chaim using a text with L'Malshinim instead of L'Shumadim).
In our version of the Amidah, the blessings V'Lamalshinim and V'LeYerushalyim begin with the letter vav (and). Earlier texts of these blessings had clauses that preceded them. The preceding clauses were removed when the text was changed over time, but the vav was retained, perhaps to remind us that these were later changes to the Amidah. Leaving the vav reminds us of the missing clauses, like a redacted government document removing sensitive information.
The insertion of these two blessings into the Amidah rooted out any early followers of Jesus who were trying to pray with Jews. The recitation of the blessing LaMalshinim would mean that they were cursing themselves. The blessing of Matzmiach Keren Yeshua would be heretical, acknowledging that the Mashiach had not yet come and therefore was not Jesus.

Interestingly, the Tosfos R"id (Ta'anis 13b), based in the Talmud Yerushalmi, suggest an alternative reason for this phenomenon that relates to the berachos of V’LiYerushalayim Ircha and Es Tzemach Dovid Avdecha.
...
p189 Rav Schachter states, “Thus, explains the Tsofos R"id, before Shmuel Hakatan composed the berachah of V’Lamalshinim, the Amidah consisted of seventeen berachos. After establishing the Birkas HaMinim as an additional berachah, it became “Shemoneh Esrei,” since the common practice was to recite V’LiYerushalayim Ircha and Es Tzemach Dovid Avdecha in combination. We continue to refer to it this way, even though our custom is to divide these two berachos and thus recite a total of nineteen berachos. [See B’Ikveoi HaTzon, p.221-222; Rav Schachter on the Moadim, Yom Yerushalayim, section II-IV.]
The words the "throne of David" etc, have been inserted in order to comply with the precept of the Jerusalem Talmud (Berachos 2:4): To include David into the blessing of Boneh Yerushalayim, the pleas for the rebuilding of Jerusalem must be combined with "David," i.e., the prayer for the establishment of the Davidic Messianic kingdom. This arrangement was to show clearly the indivisibility of these two issues. Later, a special berachah for the coming of the Messiah was introduced- Es Tzemach David. Hence the reference to it in this blessing is omitted in many old Siddur texts. However, the vav (and) at the beginning of our berachah points to a time when both formed one berachah. It began with Rachem (similar to the corresponding berachah in Grace after Meals) and ended Elhoei David v'Boneh Yerushalayim, the G-d of David and the Builder of Jerusalem.
In the commentary for Es Tzemach David, p196, Rabbi Elie Munk continues:
There are many widely divergent opinions regarding the date of the composition of this berachah and the reason for its inclusion. The Babylonian Talmud holds that it should be a separate berachah (Megillah 17b), whereas according to the Jerusalem Talmud, it is to be united with the preceding one so that the original number of eighteen berachos would be maintained after the addition of V'lamalshinim. However, later on, Es Tzemach David was generally recognized as a separate berachah. A midrash leads us to conjecture that the motive for the introduction of both V'lamalshinim and Es Tzemach David might have been the same. It is not at all unlikely that the danger to Judaism from the new teachings of the rising wave of Christianity prompted the addition of Es Tzemach David, just as it had caused the introduction of V'lamalshinim. By teaching the belief that the Messiah, in the person of the founder of the Christian religion, had already arrived, Christianity immediately set itself in direct opposition to a fundamental teaching of Judaism. The entire question of the right of Judaism to exist depended upon recognizing the founder as the Messiah. It was, therefore, decided to accentuate the petition for the Messiah, until then included within Boneh Yerushalayim, by creating a special berachah for it. The hope for the coming of the Messiah would thus be kept awake in the heart of the Jew at all times.
The true Messiah will not be of unknown origin but "a branch of the house of David (Yeshayahu 11:1), and his descent is considered so significant that tzemach, "branch," has even become one of the names of the Messiah (Jerusalem Talmud, Berachos 2:4). Like a tzemach (a plant), which has a small and insignificant beginning and then grows imperceptibly toward maturity, redemption too will come slowly and gradually, but abruptly and suddenly, leading Israel out of repair and distress towards freedom.
תְּפִלָּה מְנָא לַן? דְּתַנְיָא: שִׁמְעוֹן הַפָּקוֹלִי הִסְדִּיר שְׁמוֹנֶה עֶשְׂרֵה בְּרָכוֹת לִפְנֵי רַבָּן גַּמְלִיאֵל עַל הַסֵּדֶר בְּיַבְנֶה. אָמַר רַבִּי יוֹחָנָן, וְאָמְרִי לַהּ בְּמַתְנִיתָא תָּנָא: מֵאָה וְעֶשְׂרִים זְקֵנִים, וּבָהֶם כַּמָּה נְבִיאִים, תִּיקְּנוּ שְׁמוֹנֶה עֶשְׂרֵה בְּרָכוֹת עַל הַסֵּדֶר.
Talmudic Babylon (c.450 - c.550 CE)
§ The baraita cited previously taught that the halakha against reciting a text out of order also applies to the Amida prayer. The Gemara asks: From where do we derive this? As it is taught in a baraita: Shimon HaPakuli arranged the eighteen blessings of the Amida prayer before Rabban Gamliel in their fixed order in Yavne, indicating a specific order to these blessings that must not be changed. Rabbi Yoḥanan said, and some say that it was taught in a baraita: A hundred and twenty Elders, i.e., the Men of the Great Assembly, and among them several prophets, established the eighteen blessings of the Amida in their fixed order, which also shows that the order of these blessings may not be changed.
Talmudic Babylon (c.450 - c.550 CE)
The Gemara returns to the baraita cited at the beginning of the discussion: Now, since the baraita teaches that a hundred and twenty Elders, including many prophets, established the Amida prayer in its fixed order, what is it that Shimon HaPakuli arranged in a much later period of time, as related by Rabbi Yoḥanan? The Gemara answers: Indeed, the blessings of the Amida prayer were originally arranged by the hundred and twenty members of the Great Assembly, but over the course of time the people forgot them, and Shimon HaPakuli then arranged them again.
וּמָה רָאוּ לוֹמַר בִּרְכַּת הַשָּׁנִים בִּתְשִׁיעִית? אָמַר רַבִּי אֲלֶכְּסַנְדְּרִי: כְּנֶגֶד מַפְקִיעֵי שְׁעָרִים, דִּכְתִיב: ״שְׁבוֹר זְרוֹעַ רָשָׁע״, וְדָוִד כִּי אַמְרַהּ — בִּתְשִׁיעִית אַמְרַהּ. וּמָה רָאוּ לוֹמַר קִיבּוּץ גָּלִיּוֹת לְאַחַר בִּרְכַּת הַשָּׁנִים? דִּכְתִיב: ״וְאַתֶּם הָרֵי יִשְׂרָאֵל עַנְפְּכֶם תִּתֵּנוּ וּפֶרְיְכֶם תִּשְׂאוּ לְעַמִּי יִשְׂרָאֵל כִּי קֵרְבוּ לָבוֹא״. וְכֵיוָן שֶׁנִּתְקַבְּצוּ גָּלִיּוֹת — נַעֲשֶׂה דִּין בָּרְשָׁעִים, שֶׁנֶּאֱמַר: ״וְאָשִׁיבָה יָדִי עָלַיִךְ וְאֶצְרוֹף כַּבּוֹר סִיגָיִךְ״, וּכְתִיב: ״וְאָשִׁיבָה שׁוֹפְטַיִךְ כְּבָרִאשׁוֹנָה״. וְכֵיוָן שֶׁנַּעֲשָׂה דִּין מִן הָרְשָׁעִים — כָּלוּ הַפּוֹשְׁעִים, וְכוֹלֵל זֵדִים עִמָּהֶם, שֶׁנֶּאֱמַר: ״וְשֶׁבֶר פּוֹשְׁעִים וְחַטָּאִים יַחְדָּיו (יִכְלוּ)״. וְכֵיוָן שֶׁכָּלוּ הַפּוֹשְׁעִים — מִתְרוֹמֶמֶת קֶרֶן צַדִּיקִים, דִּכְתִיב: ״וְכׇל קַרְנֵי רְשָׁעִים אֲגַדֵּעַ תְּרוֹמַמְנָה קַרְנוֹת צַדִּיק״. וְכוֹלֵל גֵּירֵי הַצֶּדֶק עִם הַצַּדִּיקִים, שֶׁנֶּאֱמַר: ״מִפְּנֵי שֵׂיבָה תָּקוּם וְהָדַרְתָּ פְּנֵי זָקֵן״ — וּסְמִיךְ לֵיהּ: ״וְכִי יָגוּר אִתְּכֶם גֵּר״. וְהֵיכָן מִתְרוֹמֶמֶת קַרְנָם — בִּירוּשָׁלַיִם, שֶׁנֶּאֱמַר: ״שַׁאֲלוּ שְׁלוֹם יְרוּשָׁלִָם יִשְׁלָיוּ אוֹהֲבָיִךְ״. וְכֵיוָן שֶׁנִּבְנֵית יְרוּשָׁלַיִם — בָּא דָּוִד, שֶׁנֶּאֱמַר: ״אַחַר יָשׁוּבוּ בְּנֵי יִשְׂרָאֵל וּבִקְשׁוּ אֶת ה׳ אֱלֹהֵיהֶם וְאֵת דָּוִד מַלְכָּם״. וְכֵיוָן שֶׁבָּא דָּוִד — בָּאתָה תְּפִלָּה, שֶׁנֶּאֱמַר: ״וַהֲבִיאוֹתִים אֶל הַר קׇדְשִׁי וְשִׂמַּחְתִּים בְּבֵית תְּפִלָּתִי״. וְכֵיוָן שֶׁבָּאת תְּפִלָּה — בָּאת עֲבוֹדָה, שֶׁנֶּאֱמַר: ״עוֹלוֹתֵיהֶם וְזִבְחֵיהֶם לְרָצוֹן עַל מִזְבְּחִי״. וְכֵיוָן שֶׁבָּאת עֲבוֹדָה — בָּאתָה תּוֹדָה, שֶׁנֶּאֱמַר: ״זוֹבֵחַ תּוֹדָה יְכַבְּדָנְנִי״.
Talmudic Babylon (c.450 - c.550 CE)
And why did they see fit to institute that one says the blessing of bountiful years as the ninth blessing? Rabbi Alexandri said: This blessing was instituted in reference to those who raise the prices of food. We pray for rain so that the price of produce will not rise as a result of shortages, as it is written: “Break the arm of the wicked” (Psalms 10:15), referring to the wicked, who practice deception and extort the poor. And when David expressed this request, he expressed it in the ninth psalm. Although today it is considered the tenth psalm, the first and second psalms are actually counted as one, and therefore this is the ninth psalm. Therefore, the blessing of the years was fixed as the ninth blessing. The Gemara asks: And why did they see fit to institute that one says the blessing of the ingathering of the exiles after the blessing of the years? As it is written: “And you, O mountains of Israel, you shall shoot forth your branches, and yield your fruit to My people Israel; for they will soon be coming” (Ezekiel 36:8), which indicates that the ingathering of the exiles will follow after Eretz Yisrael is blessed with bountiful produce. And once the exiles have been gathered, judgment will be meted out to the wicked (Rashaim), as it is stated: “And I will turn my hand against you and purge away your dross as with lye” (Isaiah 1:25), and immediately after it is written: “And I will restore your judges as at first” (Isaiah 1:26). For this reason the blessing of the restoration of judges comes after the blessing of the ingathering of the exiles. And once judgment is meted out to the wicked, the transgressors (Poshim-rebellious sinners) will cease to be. Consequently, the next blessing is that of the transgressors (Poshim), and one includes heretics (Zeidim-intentional sinners) with them, as it is stated: “And the destruction of the transgressors (Poshim) and of the sinners (Chataim) shall be together, and they that forsake the Lord shall cease to be” (Isaiah 1:28). The “transgressors and sinners” are the evildoers, and “they that forsake the Lord” are the heretics. And once the heretics cease to be, the horn, i.e., the glory, of the righteous will be exalted, as it is written: “All the horns of the wicked will I cut off; but the horns of the righteous shall be exalted” (Psalms 75:11). Therefore, after the blessing of the heretics, one says the blessing about the righteous. And he includes the righteous converts along with the righteous, as it is stated: “You shall rise up before the hoary head, and honor the face of the elder” (Leviticus 19:32), and adjacent to this it is stated: “And if a stranger sojourns with you” (Leviticus 19:33). An “elder” is one with Torah wisdom and a “stranger” is one who has converted to Judaism. And where will the horns of the righteous be exalted? In Jerusalem, as it is stated: “Pray for the peace of Jerusalem; they who love you shall prosper” (Psalms 122:6). “They who love you” are the righteous. Therefore, the blessing of the rebuilding of Jerusalem is placed after the blessing of the righteous. And once Jerusalem is rebuilt, the Messiah, scion of the house of David, will come, as it is stated: “Afterward the children of Israel shall return, and seek the Lord their God and David their king” (Hosea 3:5), and consequently, the blessing of the kingdom of David follows the blessing of the building of Jerusalem. And once the scion of David comes, the time for prayer will come, as it is stated: “I will bring them to My sacred mountain and make them joyful in My house of prayer” (Isaiah 56:7). Therefore, the blessing of hearing prayer is recited after the blessing of the kingdom of David. And after prayer comes, the Temple service will arrive, as it is stated in the continuation of that verse: “Their burnt-offerings and their sacrifices shall be accepted on My altar” (Isaiah 56:7). The blessing of restoration of the Temple service follows the blessing of hearing prayer. And when the Temple service comes, with it will also come thanksgiving, as it is stated: “Whoever sacrifices a thanks-offering honors Me” (Psalms 50:23), which teaches that thanksgiving follows sacrifice. Therefore, the blessing of thanksgiving follows the blessing of restoration of the Temple service.
The Gemara starts by assigning a number to the various blessings and then switches to the sequence.
The Gemara explains blessing for the Mishpat includes judgment for the wicked. They followed that Bracha with a blessing against the Poshim (rebellious transgressors). The Gemara states the blessing against Zedeim, and evildoers should be included in that bracha.
So too, the blessing for righteous converts is included in the blessing for the righteous.
The text of the brachot seems to be fluid, while the order is static.