CYBERTEC PostgreSQL Logo

Debugging PostgreSQL more easily

03.2025 / Category: / Tags:

PostgreSQL is the foundation for thousands and thousands of applications. The system has long proven its worth and is operating reliably. However, the question people often ask is: What is the best way to actually debug a database application? 

For a couple of years now, I have been using a simple method that greatly speeds up the debugging process of typical applications.

Using inheritance to store data

One of the core features of PostgreSQL, which has been around for decades already, is the idea of "inheritance". So what does that mean, exactly? Simply speaking, tables can inherit columns from each other. A child table will simply have all the columns of the parent table, plus its own additional ones.

But what does this mean in real life, and what does it have to do with debugging? Let us take a closer look:

The t_global table contains a sequence including a timestamp. Those two columns are passed on to t_product. What this means is that the sequence now also applies to the child table.

Here is a second table:

We are now using the same parent table for the country table. What does it matter? What we have just produced is two things: First of all, there is ONE global sequence for all tables, which means that all IDs across the entirety of the systems are unique (we will need this later). Also: All tables have a timestamp that has an identical default value. 

Using the parent table for debugging applications

Let us now imagine for a second that we want to debug an application, and we want to know two things:

  • In which table is the ID we are looking for located?
  • Which operations have happened inside the same transaction?

The structure we have just created makes answering these two questions really easy:

Remember, there is just one single sequence that feeds all the ID columns, so we can rely on the fact that the ID is unique all across the database. But there is more: In PostgreSQL, every table supports a virtual column called "tableoid". It shows the object ID of a table. The trick now is as follows: If we cast this object ID to a special data type (= regclass), it will give us a string representation of the table name. In other words, we can run a single SQL statement and it will give us all the IDs in the entire system, including the tablename containing this ID. Supported by the fact that everything in your entire application is unique, we can easily figure out where to find anything we might be looking for.

However, we might want to take a closer look at the second column: The timestamp. Note that all timestamps are the same. This is the case because all data has been written by the same transaction. At one glance, we can see in which order data has been inserted, and, at the same time, we can see if this has happened inside the same transaction. All the while, we still have full visibility of the order in which data has changed across ALL tables. 

The method outlined in this post has served me well for many years, and has made my work of debugging the database side of things a lot easier. Therefore, I hope that this can help other people out there as well.

Do you need support for your PostgreSQL request? We will be happy to help you. Contact us directly.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2025
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram