Sunday, May 26, 2019

Table Inheritance: What's it Good For?

Table inheritance is one of the most misunderstood -- and powerful -- features of PostgreSQL.  With it, certain kinds of hard problems become easy.  While many folks who have been bitten by table inheritance tend to avoid the feature, this blog post is intended to provide a framework for reasoning about when table inheritance is actually the right tool for the job.

Table inheritance is, to be sure, a power tool and thus something to use only when it brings an overall reduction in complexity to the design.  Moreover the current documentation doesn't provide a lot of guidance regarding what the tool actually helps with and where are the performance costs and because inheritance sits orthogonal to relational design, working this out individually is very difficult.

This blog post covers uses of table inheritance which simplify overall database design and are not addressed by declarative partitioning, because they are used in areas other than table partitioning.

Table Inheritance Explained

PostgreSQL provides the ability for tables to exist in an inheritance directed acyclic graph.  Columns provided by parent tables are merged in name and type into the child table.  Altering a parent table and adding a column thus cascades this operation to all child tables, though if any child table has a column with the same name and different type, the operation will fail.

Inheritance, Tables, and Types

Every table in PostgreSQL has a corresponding campsite type, and any table can be implicitly cast to any parent table.  This is transitive.  Combined with tuple processing functions, this gives you a number of very powerful ways of working with data at various different levels of scale.

Indexes and foreign keys are not inherited.  Check constraints are inherited unless set to NO INHERIT.

Inheritance and Querying

When a table is queried, by default all child tables are also queried and their results appended to the result.  Because of exclusion constraint processing, this takes out an ACCESS SHARE lock on all child tables at planning time.  All rows are cast back to the type of the table target (in other words you get the columns of the table you queried).

Comparison to Java Interfaces

Despite the name, the closest equivalent to table inheritance in other programming languages are Java Interfaces.  Here too you get implicit casts, a subset of fields, and a promise of compatible interfaces. And as a Java class can implement multiple interfaces, multiple inheritance in PostgreSQL is supported.  Java programmers are encouraged to think of inheriting tables in interface rather than inheritance terms.

Use in Database Management Design

When we design a database there are often two overlapping concerns.  The first is in relational algebra operations on the data, and the second is in managing the data.  In a purely relational model this breaks down.

Notes Tables

One of the first really productive uses of table inheritance I had was in the notes tables in LedgerSMB.  There are several hundred table in the database, and we want to attach notes to some subset of these tables.  A naive approach might be a single global notes table with a bunch of foreign keys, or an ambiguous foreign key, or we just have a bunch of completely independent notes tables.  All of these have serious obvious problems however.  Large numbers of sparse foreign keys provide tons of NULL-handling problems, and provide a wide table that is harder to reason about.  Ambiguous foreign keys are a terrible anti pattern which should never be used due to data consistency problems, and large numbers of independent tables provide an opportunity for subtle errors due to knowledge management problems.

A slightly better solution might be to define a notes composite type, and use CREATE TABLE OF TYPE instead.  However typed tables of this sort have completely immutable schemas which makes them harder to manage over time.

We can then define a table structure something like as follows:

create table notes (
    id serial primary key,
    created_at timestamp not null default now(),
    created_by text not null,
    subject text not null,
    body text not null,
    fkey int not null,
    check (false) NO INHERIT
);

This table will never have any rows, but child tables can have rows.  For child tables, creating them is now easy:

create table invoice_notes (
    LIKE notes INCLUDING INDEXES,
    foreign key fkey REFERENCES invoice(id),
) INHERITS (notes);

The LIKE ... INCLUDING ALL indicates that we will copy in defaults, primary keys, and index definitions.  This now provides a forward-looking way of managing all notes tables going forward.  Uniqueness criteria remains enforced on a per-table basis.

If I later want to add a materialization of a column using a function I can do that in a reasonably straight-forward manner, at least compared to alternative approaches.

However, that's not all I can do.  I can then provide a search_terms function on the parent table which can be used to query child tables.

create or replace function search_terms(notes)
returns tsvector language sql immutable as
$$
select to_tsvector($1.subject) || to_tsvector($1.body);
$$;

I could then index, using GIN, the output of this function.  I still have to create the index on all current tables but if I index it now on the notes table, all tables I create with LIKE notes INCLUDING ALL will now have that index too. 

The function itself can be queried in a number of ways:

select * from invoice_notes n 
 where plainto_tsvector('something') @@ search_terms(n);

-- or

select * from invoice_notes n 
 where plainto_tsvector('something') @@ n.search_terms;

Once the function is created, that query works out of the box even though I never created a corresponding function for the invoice_notes table type.  Thus providing a consistent interface to a group of tables is an area where table inheritance can help clear out a lot of complexity very fast. Benefits include a more robust database design, more easily re-used human knowledge in how pieces fit together, and easier management of database schemas.

Note on Use in Set/Superset Modeling

There are a number of cases where the query implications of inheritance are more important.  This area is typically tricky because it often involves multiple inheritance and therefore there are a number of additional concerns that quickly crop up, though these have well-defined solutions discussed below.

Imagine we have an analytics database with numbers of pre-aggregated over possibly overlapping sets.  We want to sum up numbers quickly and easily without complicating the query language.  One option would be to create multiple views over a base table which includes the superset, but if your bulk operations primarily work over discrete subsets, you might get more out of breaking these out into subset tables which inherit the larger sets into which they are members.  This is, in effect, a reverse partitioning scheme where a single physical table shows up in multiple query tables.

In certain cases this can be easier to manage than a single large table with multiple views selecting portions of that view.  Use of this technique requires weighing different kinds of complexity and is best left for other posts.

Managing Schema Changes with Multiple Inheritance

In cases where multiple inheritance is used, adding and removing columns is relatively straight-forward, but altering existing tables can result in cases where an alteration interferes with checks on the other parent.  Renaming columns or changing types of columns is particularly tricky.  In most cases where this happens, a type change will not be done because rewriting tables is prohibitive, but renaming columns becomes the substitute and that is no less of a headache.

The key problem to note here is that the problem is that you have to make sure that both parents are changed at the same time, in the same statement.    So the solution here is to create a super parent table with the subset of columns to be acted on, and then drop it when done.   So here we:

begin;
create table to_modify (id int, new_id bigint);
alter table first_parent inherit to_modify;
alter table second_parent inherit to_modify;
alter table to_modify rename id to old_id;
alter table to_modify rename new_id to id;
commit;

The changes will then cascade down the inheritance graph properly.

Conclusions


Table inheritance is a surprisingly awesome feature in PostgreSQL, but misuse has given it a bad reputation.  There are many cases where it simplifies operation and long-term management of the database in cases where partitioning actually doesn't work that well.  This is a feature I expect to try to improve over time and hope others find it useful too, but to start we need to start using it for what it is good for, not the areas it falls short.

23 comments:

  1. Sobha Neopolis is a luxurious project launched by Sobha Properties. It is located on the way to Panathur Road, East Bangalore.

    ReplyDelete
  2. Godrej Ananda is indeed a new residential apartment project located in Gummanahalli, Bagalur, Aero Space Park, Bangalore.

    ReplyDelete
  3. At Helperji - washroom cleaning services, our experience in the cleaning industry, coupled with our dedicated team of experts in Noida, ensures that your expectations are not just met but surpassed.

    ReplyDelete
  4. The History of Poki Games is a vibrant, diverse, and exciting gaming universe that continues to captivate players of all ages. With a rich history and an ever-expanding library of games, it’s no wonder that Games remains a top choice for online gaming enthusiasts.

    ReplyDelete
  5. Ivacy Vpn Version 10 6.2.0 Free The 32-bit version of Ivacy VPN 10 6.2.0 is specifically designed for Windows 10 users. It offers a range of features that make it a go-to choice for those who value their privacy and need a reliable VPN for their 32-bit systems.

    ReplyDelete
  6. Final Draft 12.0.9 Crack With Keygen Key Updated Version software is an intelligent and astute tool for writing. The platform offers a user-friendly and streamlined interface conducive to efficient writing. The software is compatible with both Windows and Mac operating systems.

    ReplyDelete
  7. Emphasize the importance of educating and training stakeholders about data governance principles. Discuss how organizations can foster a culture of data literacy, ensuring that employees understand and adhere to governance policies. KNOW MORE: Data Consolidation services
    Data Consolidation Tools

    ReplyDelete
  8. Emphasize the symbiotic relationship between patience and consistency. Discuss how consistent, steady efforts compound over time, yielding more significant results than sporadic, intense bursts of activity. KNOW MORE: online supplement store
    buy supplements online

    ReplyDelete
  9. Microsoft Office 365 is a collection of productivity software programs that the company created. It includes a variety of programs designed to help users create, edit, and manage documents, spreadsheets, presentations, and more.

    ReplyDelete
  10. Headache: Another frequent side effect is headaches. These can range from mild discomfort to more severe migraines. Staying hydrated, getting enough rest, and practicing relaxation techniques may help reduce the frequency and intensity of headaches.

    lexapro-uses-dosage-side-effects-warnings

    ReplyDelete
  11. Prestige Park Ridge is one of the flagship projects of Prestige Group near Bannerghatta Road, Bengaluru. Offering different housing options such as 1, 2 and 3 BHK flats, the project strives to offer best-in-class facilities to its residents.

    ReplyDelete
  12. ¡Gracias por compartir tus conocimientos de una manera tan accesible! Este artículo cambia las reglas del juego Contador De Clics. Ya sea por negocios o por placer, este contador de clics cambia las reglas del juego.

    ReplyDelete
  13. Hero Homes Sector 104 Gurgaon epitomizes the fusion of modern living with a legacy of excellence.
    Read Also - Hero Homes Sector 104 Gurgaon

    ReplyDelete
  14. Sumadhura Panorama, where modern luxury meets panoramic views. Situated in the heart of Bangalore, Sumadhura Panorama offers a blend of sophistication and serenity, providing residents with an unparalleled living experience.
    Read Also - Sumadhura Panorama

    ReplyDelete
  15. Thanks for sharing your expertise—this will definitely help many of us make more informed decisions about our database structures!

    ReplyDelete
  16. Flykez is a European Award-winning creative branding & marketing agency in Georgia that aims to drive impactful change

    ReplyDelete
  17. This was such a thoughtful post. Thank you for sharing. For more information, visit this article Mental Age Test. Curious about your mental age? Take a fun test to see how young or mature your brain really is.

    ReplyDelete
  18. It is obvious that untangling the specifics of database design can lead to more productive and impactful systems. Cisco distributors in dubai

    ReplyDelete
  19. L&T Realty Panvel is a prominent real estate development division of Larsen & Toubro (L&T), a major Indian multinational conglomerate.
    L&T Realty Panvel is a prominent real estate development division of Larsen & Toubro (L&T), a major Indian multinational conglomerate.

    ReplyDelete
  20. Hello

    Wonderful information about table inheritance! It's true that, despite its potential for power, PostgreSQL's feature is frequently misinterpreted. Thank you for emphasizing that it should be used to simplify design rather than complicate it. I'm eager to investigate the examples you offer."

    Regards
    Nisha Marshall
    Prestige Evergreen

    ReplyDelete
  21. Birla Estates Dwarka Expressway Gurgaon is an exemplary residential development by Birla Estates, renowned for its commitment to quality and excellence in real estate.
    Birla Estates Dwarka Expressway Gurgaon

    ReplyDelete
  22. http://4blabla.ru/read-blog/929_garlic-oil-bulk-manufacturer-choosing-quality-for-your-products.html
    http://otherarticles.com/business/sales/346684-elevate-your-brand-with-quality-from-hetaksh-essential-oil-wholesale-supplier.html
    http://articlescad.com/leading-supplier-for-essential-oil-bulk-and-wholesale-needs-570987.html
    http://www.123articleonline.com/articles/1435156/your-top-choice-for-essential-oil-bulk-manufacturer-and-wholesale-distributor
    http://www.zupyak.com/p/4343466/t/leading-carrier-oil-bulk-and-wholesale-supplier-in-india
    http://www.diigo.com/item/note/b63tj/x1iu?k=afe3f4813845925b7e47a06f31987745
    http://www.tumblr.com/ekajaahuja/765575319972364288/saw-palmetto-oil-in-bulk-why-choose-hetaksh-as?source=share

    ReplyDelete
  23. godrej Plots Panvel offers an exciting opportunity for homebuyers looking to invest in premium residential plots in one of Mumbai's most desirable locations. Situated in Panvel, this project combines modern amenities with the beauty of nature, providing a balanced lifestyle for its residents.
    For More - Godrej Plots Panvel

    ReplyDelete