Skip to content

Commit e2f360a

Browse files
iSignalddhodge
andauthored
[DOC-860] [DOC-832] Add details of YSQL table inheritance support (yugabyte#27830)
* [DOC-860] Add details of YSQL table inheritance support * Also update cat cache docs for incremental cache invalidation. * rename to right extension * Minor edits * add incremental inval gflag * Minor fixup * edits * Copy changes * format --------- Co-authored-by: Dwight Hodge <ghodge@yugabyte.com>
1 parent 83f9637 commit e2f360a

File tree

10 files changed

+268
-14
lines changed

10 files changed

+268
-14
lines changed

docs/content/preview/best-practices-operations/ysql-catalog-cache-tuning-guide.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ You can customize this tradeoff to control preloading of entries into PostgreSQL
439439

440440
### Default behavior
441441

442-
By default, YSQL backends do not preload any catalog entries, except right after a schema change (DDL).
442+
By default, YSQL backends do not preload any catalog entries.
443443

444-
On most schema changes, running backends discard their catalog cache, and are then refreshed from either the YB-Master leader or an intermediate response cache on the local YB-TServer. This refresh causes a latency hit on running queries while they wait for this process to complete. There is also a memory increase because the cache is now preloaded with all rows of these catalog tables (as opposed to just the actively used entries that it had before).
444+
On most schema changes, running backends invalidate their caches incrementally and refresh such invalidated entries directly from the leader yb-master. This can result in a slight effect on the latency and memory usage of running queries but it should not be significant in most cases.
445+
446+
In rare cases, a full catalog refresh may be needed (this typically happens through an intermediate response cache on the local YB-TServer to minimize load on the yb-master). Such a rare full refresh may cause a more pronounced latency effect on running queries as well as a noticeable spike in memory consumption. If you encounter such cases commonly, report these at {{<issue 23785>}}.
445447

446448
## Problem scenarios and recommendations
447449

@@ -472,11 +474,9 @@ To confirm that catalog caching is the cause of this, see [Confirm that catalog
472474
- [Use connection pooling](#connection-pooling) to reuse existing connections.
473475
- [Preload additional system tables](#preload-additional-system-tables).
474476

475-
### Memory spikes on PostgreSQL backends or out of memory (OOM) events
476-
477-
On the flip side, automatic preloading of caches after a DDL change may cause memory spikes on PostgreSQL backends or out of memory (OOM) events.
477+
### High memory usage on PostgreSQL backends
478478

479-
To confirm that catalog caching is the cause of this, correlate the time when DDLs were run (Write RPCs on YB-Master) to the time of the OOM event or a spike in PostgreSQL RSS metrics.
479+
On the flip side, automatic preloading of caches may result in higher memory usage of PostgreSQL backends than desired.
480480

481481
**Possible solution**
482482

@@ -521,7 +521,7 @@ For example:
521521

522522
### Minimal catalog cache preloading {#minimal-catalog-cache-preloading}
523523

524-
When enabled, only a small subset of the catalog cache entries is preloaded. This reduces the memory spike that results, but can increase the warm up time for queries after a DDL change, as well as the initial query latency when [additional tables are preloaded](#preload-additional-system-tables).
524+
When enabled, only a small subset of the catalog cache entries is preloaded. This reduces the memory spike that results, but can increase the initial query latency when [additional tables are preloaded](#preload-additional-system-tables).
525525

526526
To enable minimal catalog cache preloading, set the [YB-TServer flag](../../reference/configuration/yb-tserver/#catalog-flags) `--ysql_minimal_catalog_caches_preload=true`.
527527

docs/content/preview/explore/ysql-language-features/advanced-features/_index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,11 @@ Snapshot synchronization ensures that two or more independent, concurrently runn
8282
{{<lead link="snapshot-synchronization/">}}
8383
To learn how different transactions can maintain a consistent view of the data, see [Synchronize snapshots](snapshot-synchronization/)
8484
{{</lead>}}
85+
86+
## Inheritance
87+
88+
Table inheritance lets you create child tables that automatically share columns and constraints from a parent table.
89+
90+
{{<lead link="inheritance/">}}
91+
To see how to create tables that inherit from a parent table, see [Inheritance](inheritance/)
92+
{{</lead>}}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Table inheritance
3+
linkTitle: Table inheritance
4+
description: Table inheritance in YSQL
5+
menu:
6+
preview:
7+
identifier: advanced-features-inheritance
8+
parent: advanced-features
9+
weight: 900
10+
tags:
11+
feature: tech-preview
12+
type: docs
13+
---
14+
15+
YSQL supports table inheritance using the INHERITS keyword, which is a [PostgreSQL feature](https://www.postgresql.org/docs/current/ddl-inherit.html) that allows you to create child tables that inherit columns and certain constraints from one or more parent tables.
16+
17+
## Example
18+
19+
To illustrate with a basic example, create the following tables:
20+
21+
```sql
22+
-- Columns common to all account types
23+
CREATE TABLE accounts (
24+
account_id INTEGER PRIMARY KEY,
25+
balance NUMERIC NOT NULL CHECK (balance >= 0),
26+
profit NUMERIC DEFAULT 0
27+
);
28+
29+
-- Child table for investment accounts
30+
CREATE TABLE investment_accounts (
31+
investment_type TEXT NOT NULL CHECK (investment_type IN ('stocks', 'bonds', 'funds')),
32+
CHECK (balance >= 5000),
33+
PRIMARY KEY (account_id, investment_type)
34+
) INHERITS (accounts);
35+
36+
-- Child table for savings accounts
37+
CREATE TABLE savings_accounts (
38+
interest_rate NUMERIC NOT NULL CHECK (interest_rate >= 0 AND interest_rate <= 0.1),
39+
CHECK (balance >= 100),
40+
PRIMARY KEY (account_id)
41+
) INHERITS (accounts);
42+
```
43+
44+
```sql
45+
testdb=# \d investment_accounts
46+
```
47+
48+
```output
49+
Table "public.investment_accounts"
50+
Column | Type | Collation | Nullable | Default
51+
-----------------+---------+-----------+----------+---------
52+
account_id | integer | | not null |
53+
balance | numeric | | not null |
54+
profit | numeric | | | 0
55+
investment_type | text | | not null |
56+
Indexes:
57+
"investment_accounts_pkey" PRIMARY KEY, lsm (account_id HASH, investment_type ASC)
58+
Check constraints:
59+
"accounts_balance_check" CHECK (balance >= 0::numeric)
60+
"investment_accounts_balance_check" CHECK (balance >= 5000::numeric)
61+
"investment_accounts_investment_type_check" CHECK (investment_type = ANY (ARRAY['stocks'::text, 'bonds'::text, 'funds'::text]))
62+
Inherits: accounts
63+
```
64+
65+
This schema allows for certain queries to be performed over all accounts while still preserving features unique to each account type. For example:
66+
67+
```sql
68+
SELECT SUM(balance) FROM accounts WHERE account_id = 10;
69+
```
70+
71+
Any columns added to or dropped from the parent `accounts` table are propagated to child tables so that such queries on the parent accounts table are always well formed.
72+
73+
However, there are certain caveats to keep in mind:
74+
75+
1. The parent table `accounts` may have its own rows that are not part of any child tables.
76+
1. The primary key for `account_id` on the parent `accounts` table does not propagate to children and has to be redefined for each child table. This is also the behavior for foreign key constraints and non-primary key unique constaints. You need to take special care to maintain such constraints across parent-child hierarchies.
77+
78+
Table inheritance can lead to complex hierarchies similar to class inheritance in object-oriented programming because a specific table can inherit from multiple parent tables and can itself be a parent table for other child tables.
79+
80+
### Queries and updates on data
81+
82+
SELECT and UPDATE queries on the parent table operate on a union of the parent and all child tables in the hierarchy. To restrict queries to just the specific table, use the ONLY keyword:
83+
84+
```sql
85+
SELECT SUM(balance) FROM ONLY accounts WHERE account_id = 10;
86+
87+
UPDATE ONLY accounts SET balance = balance + 100 WHERE account_id = 1;
88+
```
89+
90+
## Schema changes
91+
92+
1. Adding columns to or dropping columns from or altering columns on the parent table propagates to all children in the hierarchy. The behavior of such operations can be more complex when a child table has a column of the same name and type before establishing inheritance or when a child table inherits from multiple parent tables with slightly differing definitions of a column (for example, NULL vs NOT NULL constraints on the column). For more details on the allowed differences in column definitions and the resolution of such differences in inheritance, [consult the PostgreSQL documentation](https://www.postgresql.org/docs/current/ddl-inherit.html).
93+
2. Adding or dropping certain kinds of constraints propagates to all children in the hierarchy. The exceptions are primary key constrains, unique constraints and foreign key constraints. For more details, consult the [PostgreSQL documentation](https://www.postgresql.org/docs/current/ddl-inherit.html).
94+
3. For certain schema changes, the `ONLY` keyword can be used to restrict the schema change to just the parent table. For example, `ALTER TABLE ONLY accounts DROP COLUMN profit` drops the column from the parent table alone while leaving it on the child tables.
95+
96+
## Limitations
97+
98+
Table inheritance is {{<tags/feature/tp idea="2158">}} - report any problems at using issue {{<issue 5956>}}.
99+
100+
- Dropping or adding a column to a parent fails when "local" column on child table exists. (Issue {{<issue 26094>}})
101+
- Crash while obtaining a row lock on a parent inheritance table with a child file_fdw table. (Issue {{<issue 27105>}})
102+
103+
For an up-to-date list, see issue {{<issue 5956>}}.

docs/content/preview/reference/configuration/yb-master.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,12 @@ expensive when the number of YB-TServers, or the number of databases goes up.
10321032

10331033
{{< /note >}}
10341034

1035+
##### --ysql_yb_enable_invalidation_messages
1036+
1037+
Enables YSQL backends to generate and consume invalidation messages incrementally for schema changes. When enabled (true), invalidation messages are propagated via the `pg_yb_invalidation_messages` per-database catalog table. Details of the invalidation messages generated by a DDL are also logged when [ysql_log_min_messages](../yb-tserver/#ysql-log-min-messages) is set to `DEBUG1` or when `yb_debug_log_catcache_events` is set to true. When disabled, schema changes cause a full catalog cache refresh on existing backends, which can result in a latency and memory spike on existing YSQL backends.
1038+
1039+
Default: `true`
1040+
10351041
## Advisory lock flags
10361042

10371043
Support for advisory locks is {{<tags/feature/tp idea="812">}}.

docs/content/preview/reference/configuration/yb-tserver.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,16 @@ Specifies the threshold (in bytes) beyond which catalog tuples will get compress
14851485

14861486
To minimize performance impact when enabling this flag, set it to 2KB or higher.
14871487

1488+
##### --ysql_yb_enable_invalidation_messages
1489+
1490+
{{% tags/wrap %}}
1491+
1492+
1493+
Default: `true`
1494+
{{% /tags/wrap %}}
1495+
1496+
Enables YSQL backends to generate and consume invalidation messages incrementally for schema changes. When enabled (true), invalidation messages are propagated via the `pg_yb_invalidation_messages` per-database catalog table. Details of the invalidation messages generated by a DDL are also logged when [ysql_log_min_messages](#ysql-log-min-messages) is set to `DEBUG1` or when `yb_debug_log_catcache_events` is set to true. When disabled, schema changes cause a full catalog cache refresh on existing backends, which can result in a latency and memory spike on existing YSQL backends.
1497+
14881498
## Performance Tuning
14891499

14901500
### Memory division flags

docs/content/v2025.1/best-practices-operations/ysql-catalog-cache-tuning-guide.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ You can customize this tradeoff to control preloading of entries into PostgreSQL
439439

440440
### Default behavior
441441

442-
By default, YSQL backends do not preload any catalog entries, except right after a schema change (DDL).
442+
By default, YSQL backends do not preload any catalog entries.
443443

444-
On most schema changes, running backends discard their catalog cache, and are then refreshed from either the YB-Master leader or an intermediate response cache on the local YB-TServer. This refresh causes a latency hit on running queries while they wait for this process to complete. There is also a memory increase because the cache is now preloaded with all rows of these catalog tables (as opposed to just the actively used entries that it had before).
444+
On most schema changes, running backends invalidate their caches incrementally and refresh such invalidated entries directly from the leader yb-master. This can result in a slight effect on the latency and memory usage of running queries but it should not be significant in most cases.
445+
446+
In rare cases, a full catalog refresh may be needed (this typically happens through an intermediate response cache on the local YB-TServer to minimize load on the yb-master). Such a rare full refresh may cause a more pronounced latency effect on running queries as well as a noticeable spike in memory consumption. If you encounter such cases commonly, report these at {{<issue 23785>}}.
445447

446448
## Problem scenarios and recommendations
447449

@@ -472,11 +474,9 @@ To confirm that catalog caching is the cause of this, see [Confirm that catalog
472474
- [Use connection pooling](#connection-pooling) to reuse existing connections.
473475
- [Preload additional system tables](#preload-additional-system-tables).
474476

475-
### Memory spikes on PostgreSQL backends or out of memory (OOM) events
476-
477-
On the flip side, automatic preloading of caches after a DDL change may cause memory spikes on PostgreSQL backends or out of memory (OOM) events.
477+
### High memory usage on PostgreSQL backends
478478

479-
To confirm that catalog caching is the cause of this, correlate the time when DDLs were run (Write RPCs on YB-Master) to the time of the OOM event or a spike in PostgreSQL RSS metrics.
479+
On the flip side, automatic preloading of caches may result in higher memory usage of PostgreSQL backends than desired.
480480

481481
**Possible solution**
482482

@@ -521,7 +521,7 @@ For example:
521521

522522
### Minimal catalog cache preloading {#minimal-catalog-cache-preloading}
523523

524-
When enabled, only a small subset of the catalog cache entries is preloaded. This reduces the memory spike that results, but can increase the warm up time for queries after a DDL change, as well as the initial query latency when [additional tables are preloaded](#preload-additional-system-tables).
524+
When enabled, only a small subset of the catalog cache entries is preloaded. This reduces the memory spike that results, but can increase the initial query latency when [additional tables are preloaded](#preload-additional-system-tables).
525525

526526
To enable minimal catalog cache preloading, set the [YB-TServer flag](../../reference/configuration/yb-tserver/#catalog-flags) `--ysql_minimal_catalog_caches_preload=true`.
527527

docs/content/v2025.1/explore/ysql-language-features/advanced-features/_index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,11 @@ Snapshot synchronization ensures that two or more independent, concurrently runn
8282
{{<lead link="snapshot-synchronization/">}}
8383
To learn how different transactions can maintain a consistent view of the data, see [Synchronize snapshots](snapshot-synchronization/)
8484
{{</lead>}}
85+
86+
## Inheritance
87+
88+
Table inheritance lets you create child tables that automatically share columns and constraints from a parent table.
89+
90+
{{<lead link="inheritance/">}}
91+
To see how to create tables that inherit from a parent table, see [Inheritance](inheritance/)
92+
{{</lead>}}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Table inheritance
3+
linkTitle: Table inheritance
4+
description: Table inheritance in YSQL
5+
menu:
6+
v2025.1:
7+
identifier: advanced-features-inheritance
8+
parent: advanced-features
9+
weight: 900
10+
tags:
11+
feature: tech-preview
12+
type: docs
13+
---
14+
15+
YSQL supports table inheritance using the INHERITS keyword, which is a [PostgreSQL feature](https://www.postgresql.org/docs/current/ddl-inherit.html) that allows you to create child tables that inherit columns and certain constraints from one or more parent tables.
16+
17+
## Example
18+
19+
To illustrate with a basic example, create the following tables:
20+
21+
```sql
22+
-- Columns common to all account types
23+
CREATE TABLE accounts (
24+
account_id INTEGER PRIMARY KEY,
25+
balance NUMERIC NOT NULL CHECK (balance >= 0),
26+
profit NUMERIC DEFAULT 0
27+
);
28+
29+
-- Child table for investment accounts
30+
CREATE TABLE investment_accounts (
31+
investment_type TEXT NOT NULL CHECK (investment_type IN ('stocks', 'bonds', 'funds')),
32+
CHECK (balance >= 5000),
33+
PRIMARY KEY (account_id, investment_type)
34+
) INHERITS (accounts);
35+
36+
-- Child table for savings accounts
37+
CREATE TABLE savings_accounts (
38+
interest_rate NUMERIC NOT NULL CHECK (interest_rate >= 0 AND interest_rate <= 0.1),
39+
CHECK (balance >= 100),
40+
PRIMARY KEY (account_id)
41+
) INHERITS (accounts);
42+
```
43+
44+
```sql
45+
testdb=# \d investment_accounts
46+
```
47+
48+
```output
49+
Table "public.investment_accounts"
50+
Column | Type | Collation | Nullable | Default
51+
-----------------+---------+-----------+----------+---------
52+
account_id | integer | | not null |
53+
balance | numeric | | not null |
54+
profit | numeric | | | 0
55+
investment_type | text | | not null |
56+
Indexes:
57+
"investment_accounts_pkey" PRIMARY KEY, lsm (account_id HASH, investment_type ASC)
58+
Check constraints:
59+
"accounts_balance_check" CHECK (balance >= 0::numeric)
60+
"investment_accounts_balance_check" CHECK (balance >= 5000::numeric)
61+
"investment_accounts_investment_type_check" CHECK (investment_type = ANY (ARRAY['stocks'::text, 'bonds'::text, 'funds'::text]))
62+
Inherits: accounts
63+
```
64+
65+
This schema allows for certain queries to be performed over all accounts while still preserving features unique to each account type. For example:
66+
67+
```sql
68+
SELECT SUM(balance) FROM accounts WHERE account_id = 10;
69+
```
70+
71+
Any columns added to or dropped from the parent `accounts` table are propagated to child tables so that such queries on the parent accounts table are always well formed.
72+
73+
However, there are certain caveats to keep in mind:
74+
75+
1. The parent table `accounts` may have its own rows that are not part of any child tables.
76+
1. The primary key for `account_id` on the parent `accounts` table does not propagate to children and has to be redefined for each child table. This is also the behavior for foreign key constraints and non-primary key unique constaints. You need to take special care to maintain such constraints across parent-child hierarchies.
77+
78+
Table inheritance can lead to complex hierarchies similar to class inheritance in object-oriented programming because a specific table can inherit from multiple parent tables and can itself be a parent table for other child tables.
79+
80+
### Queries and updates on data
81+
82+
SELECT and UPDATE queries on the parent table operate on a union of the parent and all child tables in the hierarchy. To restrict queries to just the specific table, use the ONLY keyword:
83+
84+
```sql
85+
SELECT SUM(balance) FROM ONLY accounts WHERE account_id = 10;
86+
87+
UPDATE ONLY accounts SET balance = balance + 100 WHERE account_id = 1;
88+
```
89+
90+
## Schema changes
91+
92+
1. Adding columns to or dropping columns from or altering columns on the parent table propagates to all children in the hierarchy. The behavior of such operations can be more complex when a child table has a column of the same name and type before establishing inheritance or when a child table inherits from multiple parent tables with slightly differing definitions of a column (for example, NULL vs NOT NULL constraints on the column). For more details on the allowed differences in column definitions and the resolution of such differences in inheritance, [consult the PostgreSQL documentation](https://www.postgresql.org/docs/current/ddl-inherit.html).
93+
2. Adding or dropping certain kinds of constraints propagates to all children in the hierarchy. The exceptions are primary key constrains, unique constraints and foreign key constraints. For more details, consult the [PostgreSQL documentation](https://www.postgresql.org/docs/current/ddl-inherit.html).
94+
3. For certain schema changes, the `ONLY` keyword can be used to restrict the schema change to just the parent table. For example, `ALTER TABLE ONLY accounts DROP COLUMN profit` drops the column from the parent table alone while leaving it on the child tables.
95+
96+
## Limitations
97+
98+
Table inheritance is {{<tags/feature/tp idea="2158">}} - report any problems at using issue {{<issue 5956>}}.
99+
100+
- Dropping or adding a column to a parent fails when "local" column on child table exists. (Issue {{<issue 26094>}})
101+
- Crash while obtaining a row lock on a parent inheritance table with a child file_fdw table. (Issue {{<issue 27105>}})
102+
103+
For an up-to-date list, see issue {{<issue 5956>}}.

0 commit comments

Comments
 (0)