whitepaper: re-add 10 missing entries + 11 new defects this session, count 578→590; rebuild PDF

This commit is contained in:
russell@unturf.com 2026-03-27 22:18:31 -04:00
parent e4ee168b1e
commit 2e4f7807d5
401 changed files with 3914 additions and 114 deletions

View file

@ -1,3 +1,4 @@
# UNDF: UNDF-2026-000000218
--- a/src/backend/optimizer/util/tlist.c
+++ b/src/backend/optimizer/util/tlist.c
@@ -132,6 +132,9 @@

View file

@ -1,3 +1,4 @@
# UNDF: UNDF-2026-000000219
--- a/src/backend/optimizer/util/tlist.c
+++ b/src/backend/optimizer/util/tlist.c
@@ -739,21 +739,45 @@

View file

@ -1,3 +1,4 @@
# UNDF: UNDF-2026-000000220
diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c
index abcdef..123456 100644
--- a/src/backend/optimizer/path/joinpath.c

View file

@ -0,0 +1,119 @@
# postgresql-0009 — `typeInheritsFrom`: O(V²) BFS with visited List in type coercibility check
## Status
PATCHED
## Severity
MEDIUM (>100× speedup at V=200 inheritance depth; called at query parse time for every type cast)
## Location
`src/backend/catalog/pg_inherits.c`, function `typeInheritsFrom()`
## Description
`typeInheritsFrom()` performs a breadth-first traversal of the PostgreSQL
inheritance graph (via `pg_inherits`) to determine whether one composite type
inherits from another. The BFS uses `List *visited` to avoid revisiting nodes,
but uses `list_member_oid(visited, this_relid)` — an O(|visited|) linear scan —
for every node popped from the queue.
The pattern:
```c
visited = NIL;
queue = list_make1_oid(subclassRelid);
foreach(queue_item, queue)
{
Oid this_relid = lfirst_oid(queue_item);
if (list_member_oid(visited, this_relid)) /* O(|visited|) scan */
continue;
visited = lappend_oid(visited, this_relid); /* visited grows by 1 */
/* scan pg_inherits for parents of this_relid, lappend to queue */
}
```
With V distinct ancestor nodes visited, the total membership-check cost is:
0 + 1 + 2 + ... + (V-1) = **O(V²)**.
The fix replaces `List *visited` with a hash set (modelled as `HTAB` with OID
keys in C). Each `list_member_oid` becomes an O(1) hash lookup, reducing total
cost to **O(V)**.
### Hot path
`typeInheritsFrom()` is called from `coerce_to_target_type()` in
`src/backend/parser/parse_coerce.c` at query parse time:
- Line 510: whenever an expression needs to be coerced to a composite type target.
- Line 639: when checking function argument compatibility.
These are called for every query involving type casts, polymorphic functions,
and typed tables — potentially dozens of times per complex query.
### Practical scale
- Table inheritance chains: typical 5–20 levels, 10–100 ancestors.
At V=100: O(V²)=5,000 ops → O(V)=100 ops → **50× speedup**.
- PostgreSQL 12+ supports partition hierarchies with hundreds of levels.
At V=200: O(V²)=20,000 ops → O(V)=200 ops → **100× speedup**.
- Multiple inheritance (multiple parents per table): V can grow faster.
## Patch
```c
--- a/src/backend/catalog/pg_inherits.c
+++ b/src/backend/catalog/pg_inherits.c
@@ -407,13 +407,23 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
Relation inhrel;
- List *visited,
- *queue;
+ List *queue;
+ /* CWE-407 fix (postgresql-0009): replace List-based visited with a hash
+ * table so each membership check is O(1) instead of O(|visited|).
+ * Eliminates O(V²) → O(V) for BFS over V ancestor nodes. */
+ HTAB *visited_htab;
+ HASHCTL ctl;
ListCell *queue_item;
+ memset(&ctl, 0, sizeof(ctl));
+ ctl.keysize = sizeof(Oid);
+ ctl.entrysize = sizeof(Oid);
+ ctl.hcxt = CurrentMemoryContext;
+ visited_htab = hash_create("typeInheritsFrom visited",
+ 64, &ctl,
+ HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
+
queue = list_make1_oid(subclassRelid);
- visited = NIL;
@@ -437,7 +447,9 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
- if (list_member_oid(visited, this_relid))
+ bool found;
+ hash_search(visited_htab, &this_relid, HASH_FIND, &found);
+ if (found)
continue;
- visited = lappend_oid(visited, this_relid);
+ hash_search(visited_htab, &this_relid, HASH_ENTER, NULL);
@@ -490,7 +502,7 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
table_close(inhrel, AccessShareLock);
- list_free(visited);
+ hash_destroy(visited_htab);
list_free(queue);
```
## Speedup
At V=200 distinct ancestors (deep inheritance hierarchy):
- Defective: 0+1+...+199 = 19,900 ops total
- Fixed: 200 hash lookups = 200 ops
- **Ratio: 99.5× speedup**
At V=50 (typical moderate hierarchy):
- Defective: 1,225 ops
- Fixed: 50 ops
- **Ratio: 24.5× speedup**
## Test
`defects/postgresql/unit/PostgresqlTest.java` — `postgresql-0009` section.

View file

@ -0,0 +1,68 @@
# UNDF: UNDF-2026-000000221
--- a/src/backend/catalog/pg_inherits.c
+++ b/src/backend/catalog/pg_inherits.c
@@ -407,13 +407,27 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
bool result = false;
Oid subclassRelid;
Oid superclassRelid;
Relation inhrel;
- List *visited,
- *queue;
+ List *queue;
+ /*
+ * CWE-407 fix (postgresql-0009): replace List *visited with a hash table
+ * so each membership check is O(1) instead of O(|visited|).
+ * The old code performed list_member_oid(visited, this_relid) on every
+ * BFS node, accumulating O(V²) total comparisons for V ancestors.
+ * With a HTAB keyed on OID we get O(V) total.
+ */
+ HTAB *visited_htab;
+ HASHCTL ctl;
ListCell *queue_item;
+ memset(&ctl, 0, sizeof(ctl));
+ ctl.keysize = sizeof(Oid);
+ ctl.entrysize = sizeof(Oid);
+ ctl.hcxt = CurrentMemoryContext;
+ visited_htab = hash_create("typeInheritsFrom visited",
+ 64, &ctl,
+ HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
+
/* We need to work with the associated relation OIDs */
subclassRelid = typeOrDomainTypeRelid(subclassTypeId);
if (subclassRelid == InvalidOid)
@@ -430,7 +444,7 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
queue = list_make1_oid(subclassRelid);
- visited = NIL;
inhrel = table_open(InheritsRelationId, AccessShareLock);
@@ -447,12 +461,16 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
Oid this_relid = lfirst_oid(queue_item);
ScanKeyData skey;
SysScanDesc inhscan;
HeapTuple inhtup;
+ bool found;
- if (list_member_oid(visited, this_relid))
+ /* O(1) hash lookup instead of O(|visited|) list scan */
+ hash_search(visited_htab, &this_relid, HASH_FIND, &found);
+ if (found)
continue;
- visited = lappend_oid(visited, this_relid);
+ /* Mark as visited in O(1) */
+ hash_search(visited_htab, &this_relid, HASH_ENTER, NULL);
ScanKeyInit(&skey,
Anum_pg_inherits_inhrelid,
@@ -497,7 +515,7 @@ typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
/* clean up ... */
table_close(inhrel, AccessShareLock);
- list_free(visited);
+ hash_destroy(visited_htab);
list_free(queue);
return result;