Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq1DK-00FhsY-C1 for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 02:43:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tq1DJ-00CiQ6-1u for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 02:43:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq1DI-00CiL1-IL for pgsql-general@lists.postgresql.org; Thu, 06 Mar 2025 02:43:56 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tq1DF-001DVb-0d for pgsql-general@lists.postgresql.org; Thu, 06 Mar 2025 02:43:56 +0000 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2feb91a2492so333521a91.2 for ; Wed, 05 Mar 2025 18:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741229032; x=1741833832; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Vi17HZln7yDMdetELlzhzXcpm7YIoOek2RGKYSgFIcU=; b=WciM9XKUzil1th93Qx22i0DiVtPWyDNJRr3RCwPW1SqC6w8AYaJiGDcmssX9OxGQkg 34a/dMD0q3FnxSe5h2EH2wqovGeBNbLLzaL8Hj8xEUY8qfOf7oki8PtGX9gXAVYKOs54 AgR/d6wqIp56cTqerEzb/QnQtami2RLz79DI6FlaLuFQqZaQEHnSudt/b45dbA4HIFQH nCqx1yPwv138wN2qgkkCeMxkSrtjopuOkdY0EspFmNlwUIF6PeU+/g1ZXNL7VZEYXVgz g50ReK5VvhE6/GqM/wzgY4zbPVB44yH2MkbLRdZPo6063trePN8hnJzWixZTTfnXEiDF G5fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741229032; x=1741833832; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Vi17HZln7yDMdetELlzhzXcpm7YIoOek2RGKYSgFIcU=; b=F1YObjZAcbPyIr78BVYholE+nbLf6SlVKkcB5bIFoisPcH2JxHJDBLcp1NP4TzSBv8 psYZseTLIFos2++53jakwZc014jDnM79kRNyK493LkcMyMOvuudxCHLhSu5MwBX9kp07 XtcsDkXdfY3/a2j1oPzJTFSbsGMH5Htb4PNBGvE4W/V+1CNeDy1qi3Ib6Led9OlGe+n5 UW/UNMWhv+1+/QzvNAgio4UymDzQ7qrt2rGXIJdH7AjC5Zvz+X5egu7mN5owdEeEAzn7 ik5aT2zDDu14Fa/C3TWcPsPW/Dw32Y22G/jvZn7b6+DBX+yX2tXH3Sflr7pvgAtrtudB WJkQ== X-Forwarded-Encrypted: i=1; AJvYcCVrbqeYdXS978JqqCfQKk4Wv0abxPLHcHbTBRNYw27qaUOipbc3BFaGJD0UMfPsPd/ECZWJbP1QccV6NL7+@lists.postgresql.org X-Gm-Message-State: AOJu0Ywlooe9KkxR5i76S3Ug0WB8uZHYOhkXiejJGNzuQHJnZecp3Qaj DLwE68sfaa4l8FmComVXATcRXWLcYB4cOpHyUytPbHWuBZguKKdy4UpKucp96/2tgD67uHNBZ8z W5PF/r022RuvwdPOLrRNyQZVeR3852tmcO3J47Q== X-Gm-Gg: ASbGncuF1br+dHwzcAMY5B0ncXCXKtBRDA6Pu7FFmdgWBy7axJONB1XQgcgPRxwilYW fgx4Ox7rwGkgapPdZUx07Tc3zOqrvmhcoE9bZbzZR+6tbsVDsIrk6A6AsSMcjKoboHtYBBiQ+sK SL3IWAk3wtLbZxRjGNGMJzd7AcvA== X-Google-Smtp-Source: AGHT+IGomkGGZ8AOz43bEGerjfgsoZxiAxwshOMclR6wGVy/Z+iDYBW/1U1eoC8g6uBoH5R2fw7DHSQlK3PpPQeZyKA= X-Received: by 2002:a17:90b:134f:b0:2fe:a742:51b0 with SMTP id 98e67ed59e1d1-2ff497c6827mr7768228a91.31.1741229031935; Wed, 05 Mar 2025 18:43:51 -0800 (PST) MIME-Version: 1.0 References: <099b49ebae94e23f19afdad3f8c9c6e702a3a2d5.camel@cybertec.at> <6d7e1022-6404-4dab-8467-8d1f6e8b63cb@aklaver.com> In-Reply-To: <6d7e1022-6404-4dab-8467-8d1f6e8b63cb@aklaver.com> From: me nefcanto Date: Thu, 6 Mar 2025 06:13:39 +0330 X-Gm-Features: AQ5f1JpdlXOCAY2Sv5aU4XH6G7v5WGy40GTCWQGY8625COmCngRPJ31c5EJZwCI Message-ID: Subject: Re: Quesion about querying distributed databases To: Adrian Klaver Cc: Laurenz Albe , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000ebda95062fa37903" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ebda95062fa37903 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I once worked with a monolithic SQL Server database with more than 10 billion records and about 8 Terabytes of data. A single backup took us more than 21 days. It was a nightmare. Almost everybody knows that scaling up has a ceiling, but scaling out has no boundaries. Therefore I will never choose a monolithic database design unless it's a small project. But my examples are just examples. We predict 100 million records per year. So we have to design accordingly. And it's not just sales records. Many applications have requirements that are cheap data but vast in multitude. Consider a language-learning app that wants to store the known words of any learner. 10 thousand learners each knowing 2 thousand words means 20 million records. Convert that to 100 thousand learners each knowing 7 thousand words and now you almost have a billion records. Cheap, but necessary. Let's not dive into telemetry or time-series data. We initially chose to break the database into smaller databases, because it seemed natural for our modularized monolith architecture. And it worked great for SQL Server. If you're small, we host them all on one server. If you get bigger, we can put heavy databases on separate machines. However, I don't have experience working with other types of database scaling. I have used table partitioning, but I have never used sharding. Anyway, that's why I asked you guys. However, encouraging me to go back to monolith without giving solutions on how to scale, is not helping. To be honest, I'm somehow disappointed by how the most advanced open source database does not support cross-database querying just like how SQL Server does. But if it doesn't, it doesn't. Our team should either drop it as a choice or find a way (by asking the experts who built it or use it) how to design based on its features. That's why I'm asking. One thing that comes to my mind, is to use custom types. Instead of storing data in ItemCategories and ItemAttributes, store them as arrays in the relevant tables in the same database. But then it seems to me that in this case, Mongo would become a better choice because I lose the relational nature and normalization somehow. What drawbacks have you experienced in that sense? Regards Saeed On Wed, Mar 5, 2025 at 7:38=E2=80=AFPM Adrian Klaver wrote: > On 3/5/25 04:15, me nefcanto wrote: > > Dear Laurenz, the point is that I think if we put all databases into on= e > > database, then we have blocked our growth in the future. > > How? > > > A monolith database can be scaled only vertically. We have had huge > > headaches in the past with SQL Server on Windows and a single database. > > But when you divide bounded contexts into different databases, then you > > have the chance to deploy each database on a separate physical machine. > > That means a lot in terms of performance. Please correct me if I am > wrong. > > And you add the complexity of talking across machines, as well as > maintaining separate machines. > > > > > Let's put this physical restriction on ourselves that we have different > > databases. What options do we have? One option that comes to my mind, i= s > > to store the ID of the categories in the Products table. This means tha= t > > I don't need FDW anymore. And databases can be on separate machines. I > > first query the categories database first, get the category IDs, and > > then add a where clause to limit the product search. That could be an > > option. Array data type in Postgres is something that I think other > > RDBMSs do not have. Will that work? And how about attributes? Because > > attributes are more than a single ID. I should store the attribute key, > > alongside its value. It's a key-value pair. What can I do for that? > > You seem to be going out of the way to make your life more complicated. > > The only way you are going to find an answer is set up test cases and > experiment. My bet is a single server with a single database and > multiple schemas is where you end up, after all that is where you are > starting from. > > > > > > Thank you for sharing your time. I really appreciate it. > > Saeed > > > > > > > > > > > > On Wed, Mar 5, 2025 at 3:18=E2=80=AFPM Laurenz Albe > > wrote: > > > > On Wed, 2025-03-05 at 14:18 +0330, me nefcanto wrote: > > > That means a solid monolith database. We lose many goodies with > that. > > > As a real-world example, right now we can import a single databa= se > > > from the production to the development to test and troubleshoot > data. > > > > Well, can't you import a single schema then? > > > > > What if we host all databases on the same server and use FDW. Wh= at > > > happens in that case? Does it return 100 thousand records and jo= in > > > in the memory? > > > > It will do just the same thing. The performance could be better > > because of the reduced latency. > > > > > Because in SQL Server, when you perform a cross-database query > > > (not cross-server) the performance is extremely good, proving th= at > > > it does not return 100 thousand ItemId from > Taxonomy.ItemCategories > > > to join with ProductId. > > > > > > Is that the same case with Postgres too, If databases are locate= d > > > on one server? > > > > No, you cannot perform cross-database queries without a foreign > > data wrapper. I don't see a reason why the statement shouldn't > > perform as well as in SQL Server if you use schemas instead of > > databases. > > > > Yours, > > Laurenz Albe > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > --000000000000ebda95062fa37903 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I once worked with a monolithic SQL Server database with more th= an 10 billion records and about 8 Terabytes=C2=A0of data. A single backup t= ook us more than 21 days. It was a nightmare. Almost everybody knows that s= caling up has a ceiling, but scaling out has no boundaries.

Therefore I = will never choose a monolithic database design unless it's a small proj= ect. But my examples are just examples. We predict 100 million records per = year. So we have to design accordingly. And it's not just sales records= . Many applications have requirements that are cheap data but vast in multi= tude. Consider a language-learning app that wants to store the known words = of any learner. 10 thousand learners each knowing 2 thousand words means 20= million records. Convert that to 100 thousand learners each knowing 7 thou= sand words and now you almost have a billion records. Cheap, but necessary.= Let's not dive into telemetry or time-series data.

We initially cho= se to break the database into smaller databases, because it seemed natural = for our modularized monolith architecture. And it worked great for SQL Serv= er. If you're small, we host them all on one server. If you get bigger,= we can put heavy databases on separate machines.

However, I don't h= ave experience working with other types of database scaling. I have used ta= ble partitioning, but I have never used sharding.

Anyway, that's why= I asked you guys. However, encouraging me to go back to monolith without g= iving solutions on how to scale, is not helping. To be honest, I'm some= how disappointed by how the most advanced open source database does not sup= port cross-database querying just like how SQL Server does. But if it doesn= 't, it doesn't. Our team should either drop it as a choice or find = a way (by asking the experts who built it or use it) how to design based on= its features. That's why I'm asking.

One thing that comes to my= mind, is to use custom types. Instead of storing data in ItemCategories an= d ItemAttributes, store them as arrays in the relevant tables in the same d= atabase. But then it seems to me that in this case, Mongo would become a be= tter choice because I lose the relational nature and normalization somehow.= What drawbacks have you experienced in that sense?

Regards
Saeed

On Wed, Mar 5, 2025 at 7:38=E2=80=AFPM Adrian Klaver &l= t;adrian.klaver@aklaver.com> wrote:
On = 3/5/25 04:15, me nefcanto wrote:
> Dear Laurenz, the point is that I think if we put all databases into o= ne
> database, then we have blocked our growth in the future.

How?

> A monolith database can be scaled only vertically. We have had huge > headaches in the past with SQL Server on Windows and a single database= .
> But when you divide bounded contexts into different databases, then yo= u
> have the chance to deploy each database on a separate physical machine= .
> That means a lot in terms of performance. Please correct me if I am wr= ong.

And you add the complexity of talking across machines, as well as
maintaining separate machines.

>
> Let's put this physical restriction on ourselves that we have diff= erent
> databases. What options do we have? One option that comes to my mind, = is
> to store the ID of the categories in the Products table. This means th= at
> I don't need FDW anymore. And databases can be on separate machine= s. I
> first query the categories database first, get the category IDs, and <= br> > then add a where clause to limit the product search. That could be an =
> option. Array data type in Postgres is something that I think other > RDBMSs do not have. Will that work? And how about attributes? Because =
> attributes are more than a single ID. I should store the attribute key= ,
> alongside its value. It's a key-value pair. What can I do for that= ?

You seem to be going out of the way to make your life more complicated.

The only way you are going to find an answer is set up test cases and
experiment. My bet is a single server with a single database and
multiple schemas is where you end up, after all that is where you are
starting from.


>
> Thank you for sharing your time. I really appreciate it.
> Saeed
>
>
>
>
>
> On Wed, Mar 5, 2025 at 3:18=E2=80=AFPM Laurenz Albe <
laurenz.albe@cybertec.at
> <mailto:
laurenz.albe@cybertec.at>> wrote:
>
>=C2=A0 =C2=A0 =C2=A0On Wed, 2025-03-05 at 14:18 +0330, me nefcanto wrot= e:
>=C2=A0 =C2=A0 =C2=A0 > That means a solid monolith database. We lose= many goodies with that.
>=C2=A0 =C2=A0 =C2=A0 > As a real-world example, right now we can imp= ort a single database
>=C2=A0 =C2=A0 =C2=A0 > from the production to the development to tes= t and troubleshoot data.
>
>=C2=A0 =C2=A0 =C2=A0Well, can't you import a single schema then? >
>=C2=A0 =C2=A0 =C2=A0 > What if we host all databases on the same ser= ver and use FDW. What
>=C2=A0 =C2=A0 =C2=A0 > happens in that case? Does it return 100 thou= sand records and join
>=C2=A0 =C2=A0 =C2=A0 > in the memory?
>
>=C2=A0 =C2=A0 =C2=A0It will do just the same thing.=C2=A0 The performan= ce could be better
>=C2=A0 =C2=A0 =C2=A0because of the reduced latency.
>
>=C2=A0 =C2=A0 =C2=A0 > Because in SQL Server, when you perform a cro= ss-database query
>=C2=A0 =C2=A0 =C2=A0 > (not cross-server) the performance is extreme= ly good, proving that
>=C2=A0 =C2=A0 =C2=A0 > it does not return 100 thousand ItemId from T= axonomy.ItemCategories
>=C2=A0 =C2=A0 =C2=A0 > to join with ProductId.
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 > Is that the same case with Postgres too, If d= atabases are located
>=C2=A0 =C2=A0 =C2=A0 > on one server?
>
>=C2=A0 =C2=A0 =C2=A0No, you cannot perform cross-database queries witho= ut a foreign
>=C2=A0 =C2=A0 =C2=A0data wrapper.=C2=A0 I don't see a reason why th= e statement shouldn't
>=C2=A0 =C2=A0 =C2=A0perform as well as in SQL Server if you use schemas= instead of
>=C2=A0 =C2=A0 =C2=A0databases.
>
>=C2=A0 =C2=A0 =C2=A0Yours,
>=C2=A0 =C2=A0 =C2=A0Laurenz Albe
>

--
Adrian Klaver
adrian.klave= r@aklaver.com

--000000000000ebda95062fa37903--