2

Since Postgres also supports partitioned tables, what is the use of child table.

Suppose there is a table of users which has a column created_date. We can store data in 2 ways:

  1. We create many child tables of this user table and distribute the data of users on the basis of created_date (say, one table for every date, like user_jan01_21).
  2. We can create a partitioned table with the partitioning key created_date

Then what is the difference between these solution? Basically, I want to know what problem table inheritance can solve that partitioning cannot.

Another doubt I have: if I follow solution 1, and I query the user table without the ONLY keyword, will it scan all the child tables?

For example:

SELECT * FROM WHERE where created_date = current_date - 10;
2
  • 2
    The old inheritance based partitioning is deprecated. If you really need partitioning, use the new declarative partitioning. Commented Feb 9, 2021 at 6:48
  • Use EXPLAIN(ANALYZE, BUFFERS) to see how the query planner works. No reason to keep your doubt, when you can actually see how it works. Commented Feb 9, 2021 at 12:32

1 Answer 1

4

If the objective is partitioning, as in your example, then there is no advantage in using table inheritance. Declarative partitioning is far superior in ease of use, performance and available features.

Table inheritance has uses that are unrelated to partitioning. Features that partitioning doesn't offer are:

  • the child table can have additional columns

  • a table can inherit from more than one table

With table inheritance, if you select from the parent table, you will also get all results from the child tables, just as if you had used UNION ALL to combine the results.

Sign up to request clarification or add additional context in comments.

1 Comment

…and with table inheritance, foreign key references to the parent table don't work as one might expect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.