
We will keep the table definitions to a minimum to keep the focus on foreign keys: Now let’s start with a real-world example-depicted in 4 separate steps-to show what you can do with Citus 10 and what a hybrid data model can look like in Citus 10. Real-world example of using foreign keys between local & reference tables
POSTGRESQL FOREIGN KEY HOW TO
Update in August 2021: You can learn more about how to access the Citus 10 features in Hyperscale (Citus) across the different regions in Nik’s GA post.


Note that if you are using the Hyperscale (Citus) option in the Azure Database for PostgreSQL managed service, the good news is that Citus 10 is already available there. In this post, you will learn how Citus 10 extends its foreign key support and why extending the foreign key support for local tables is so useful for hybrid local+distributed data models. In those 2 cases, the network cost that we mentioned is already amortized by the benefits. you want to have foreign keys from distributed tables into those small tables.you want to make efficient joins with distributed tables, or.Of course, it still makes a lot of sense to convert some of your small tables to reference tables, if: So now you won’t need to incur any of the costs of network overhead that reference tables have, just because you want a local table to have a foreign key with a reference table. keep your regular Postgres tables local to coordinator, while still enjoying foreign keys between your regular PostgreSQL tables and reference tables.While reference tables are extremely useful, writes to reference tables are slower than writes to regular (local) tables since they are replicated to all nodes over the network. Instead, if you wanted to use foreign keys between local Postgres tables and reference tables, you had to convert your local Postgres table to a reference table too. However, prior to Citus 10, defining foreign keys between your local Postgres tables and Citus reference tables wasn’t supported. foreign keys between two colocated distributed tables when the key includes the distribution column.foreign keys from distributed tables to reference tables.Foreign key support in Citus: before & after Citus 10īefore Citus 10, you could already use foreign keys with Citus in the following ways: Fortunately, Citus allows you to mix regular (local) tables and distributed tables in the same database. However, distributing your tables does add some latency to your SQL queries, and might be unnecessary for some of your small tables. Reference tables are replicated to enable fast joins with distributed tables.Distributed tables are sharded across many PostgreSQL nodes to combine their compute capacity, and.If you’re not yet familiar with how Citus works, Citus adds 2 new table types to Postgres to scale out your workload: distributed tables and reference tables. This post will walk you through one of the new features in Citus 10: support for foreign keys between local Postgres tables and Citus reference tables. If you have already heard about Citus 10, you know that Citus 10 gives you more support for hybrid data models, which means that you can easily combine regular Postgres tables with distributed Citus tables to get the best of the single node and distributed Postgres worlds. For example, a foreign key can be used to ensure that a table of “orders” can only reference customer IDs that exist in the “customers” table. And one of the ways you can relate your tables is of course to use foreign keys.Ī foreign key ensures referential integrity, which can help you to avoid bugs in applications. Whether you’re already a Postgres expert or are new to Postgres, you probably know one of the benefits of using a relational database is to have relations between your tables. One of the main reasons people use Citus to transform Postgres into a distributed database is that with Citus, you can scale out horizontally while still enjoying PostgreSQL’s great RDBMS features.
