Re: Questions about PostgreSQL implementation details

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Julien Delplanque <julien(dot)delplanque(at)inria(dot)fr>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Questions about PostgreSQL implementation details
Date: 2019-12-09 16:12:57
Message-ID: 16180.1575907977@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Julien Delplanque <julien(dot)delplanque(at)inria(dot)fr> writes:
> I have a few questions about the internals of PostgreSQL and I think they require experts knowledge.

> Q1. Are PostgreSQL's meta-description tables (such as pg_class) the "reality" concerning the state of the DB or are they just a virtual representation ?

The system catalogs are reality as far as the effects of DDL go. In the
particular case of pg_class, there is additional reality, which is that
(most) pg_class rows represent one or more data files on-disk. You could
in fact simulate many DDL operations by manual inserts/deletes/updates on
system catalogs; but that would not result in any actions on the data
files, so it falls down for the specific cases of CREATE/DROP TABLE,
CREATE/DROP INDEX, etc.

> What I would like to know with this question is: would it be possible to implement DDL queries (e.g. CREATE TABLE, DROP TABLE, CREATE VIEW, ALTER TABLE, etc.) as DML queries that modify the meta-data stored in meta-description tables?

Underneath, many of those operations are just catalog manipulations,
so yes up to the point where you need to do something that impacts
user data storage.

(In practice, getting all the details right from a SQL client
would be a pretty painful thing, so I'm not sure I see the point.
Usually, modifying the PG C code or writing an extension would be
a saner approach to modifying the system's behavior.)

> Q2. Are PostgreSQL's "meta-constraints" (i.e. constraints related to database structure such as "a table can only have a single primary key") implemented in C code or via data constraints on PostgreSQL's meta-description tables?

Mostly code. The only real constraints on the system catalogs, in the
sense of something that would reject an ill-advised low-level update,
are the unique indexes. There are also NOT NULL markers on many of
the catalogs' columns, but those are only enforced against manual
SQL updates not updates made by C code. This is all somewhat historical,
I suppose, but it's worked well enough for us.

> Thanks in advance for answering my questions, any help or pointers to existing documentation will be appreciated.

There's no substitute for reading the source code. Unlike some other
systems, PG was developed as open source from the beginning, so there
is not much in the way of a "theory of operations manual" or other
separate internals documentation --- people with these sorts of
questions are expected to go read the code. You could perhaps start
by skimming

https://www.postgresql.org/docs/devel/internals.html

and then go look for README files in the portions of the source
tree that interest you.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2019-12-09 16:13:13 Re: Questions about PostgreSQL implementation details
Previous Message Robert Haas 2019-12-09 15:56:40 Re: [HACKERS] WAL logging problem in 9.4.3?