Researcher says PostgreSQL core hid a 22-year-old type-confusion primitive

The post is the third part of a series called "Systemic Risks in the Managed PostgreSQL Industry". The author, who gives his first name as Mehmet, says a single question, whether a project could use managed PostgreSQL, sent him down a months-long rabbit hole. At the bottom he found what he calls a 22-year-old zero-day in PostgreSQL core and a new class of type-confusion vulnerabilities. His one-line summary of the technique: he gave PostgreSQL a value inside a composite type, changed the type of one of its fields, and asked it to read the value back. He says this led to one of the most reliable memory-corruption exploits he has seen.
He did not set out to attack the core. During June and July 2026 he had been exploiting memory-corruption bugs in several PostgreSQL extensions, among them pg_clickhouse, which translates ClickHouse values into native PostgreSQL values. A ClickHouse Array is not a PostgreSQL array, so the extension has to rebuild its elements as Datum values in the format PostgreSQL expects. A mistake in that conversion gave him code execution. He reports that he disclosed the pg_clickhouse out-of-bounds write plus read chain to the vendor, who fixed it on June 23. The exploit was his first encounter with Datums and OIDs, and he learned only enough to finish it. Around late July he turned to the question that was left over and followed it into PostgreSQL core.
He started with SELECT 1337 and the debug_print_parse setting. The typed query tree showed a CONST node with consttype 23 (int4), constlen 4, constbyval true, and a constvalue of [ 57 5 0 0 0 0 0 0 ]. Working with an AI agent he named pg_expert (he had asked Opus 5 to prepare a deep-dive course on PostgreSQL types), he learned how to read it. 57 is 0x39 and 5 is 0x05, so on a little-endian machine the bytes 39 05 00 00 give 0x00000539, which is 1337. The value is eight bytes, not four, because constvalue is declared as a Datum, a generic 8-byte carrier, and the int4 sits in its lower four bytes.
The key lesson: a Datum does not know what it represents. It carries no label saying "I am an integer". PostgreSQL knows the type only from the surrounding type OID, here consttype 23, which points to a row in the pg_type catalog. That row gives the length, whether the value is passed by value, and the input and output functions (int4in and int4out for int4). For text, typlen is -1, meaning variable length, and typbyval is false, so the Datum holds a pointer to the text representation instead of the value itself. He also found that a quoted literal such as '1337' has the type unknown, a real type with OID 705 that PostgreSQL gives to string literals whose final type is not yet decided.
The turning point came when he noticed that an output function does not inspect a Datum to discover its type. It trusts the values and flags around it, so int4out() reads an integer only because PostgreSQL chose it for int4. He recalled the TOCTOU class of web-app bugs he had been exploiting and fixing for years, and reasoned that a type confusion could let him push a Datum made for one type through the functions of another. LANGUAGE internal, which lets a superuser create an SQL function pointing at native code already compiled into PostgreSQL, gave him a way to do that on purpose. He declared pg_temp.confused_textout(integer), backed by textout, called it with 1337, and his local backend crashed. He built a working exploit from this against security extensions deployed by managed PostgreSQL providers; that work became part of Part 2 of the series. Then he set the question aside.
About two weeks later an unrelated conversation brought it back. On a run or walk, he and his partner Bilge, whom he describes as a Staff Machine Learning Engineer at EDB, were discussing second-order SQL injection in PostgreSQL AI extensions and the chance of tricking their AI features into producing malicious SQL. That led to prepared statements. He has taught SQL injection to thousands of trainees with the image of the query as a recipe and the values as ingredients: the database understands the recipe first and receives the ingredients separately. He had always seen that separation as a security feature. This time he noticed that the database finishes parsing and then waits to be told to execute, and in between he can run other queries. His conclusion: "I can alter types before executing the query!"
The visible text ends at the next step. He cannot redefine built-in types such as int4, text or UUID, and at that point he barely knew what composite types were. The composite-type exploit itself and the path to remote code execution are in the part of the article beyond this retelling.
Key facts
- The author calls the bug a 22-year-old zero-day in PostgreSQL core and a new class of type-confusion vulnerabilities; his technique is to put a value in a composite type, change a field's type, and read the value back.
- He reached it by accident: exploiting the pg_clickhouse extension (bad ClickHouse-to-Datum conversion gave him code execution) made him study Datums, OIDs and pg_type, with an AI agent he named pg_expert as tutor.
- Core point: a Datum is a generic 8-byte carrier with no type label, and output functions trust the surrounding type OID, so mismatched types make functions misread data. In his example, textout called on an int4 crashed his local backend.
- The enabling idea came from a talk about prepared statements: parsing finishes before execution, so types can be altered in between.
- He says the pg_clickhouse vendor fixed the out-of-bounds write and read chain on June 23; the available text stops before the composite-type exploit and the RCE.
Why it matters
If the author is right, the flaw sits in how PostgreSQL core handles types, not in a single extension, and he frames it as a whole new class of type-confusion bugs. The post is also a worked example of how the idea arose: from reading parse trees and the pg_type catalog, from a pre-existing crash, and from a rethink of prepared statements that are normally treated purely as a defence against SQL injection. It is the third part of a series on systemic risks in the managed PostgreSQL industry.
Who it affects
The immediate audience is PostgreSQL users and operators, and especially people who rely on managed PostgreSQL services, since the series is about those providers. The author says his LANGUAGE internal exploit hit security extensions deployed by managed providers, which is covered in Part 2. The post does not say which providers were affected by the core bug, and extension authors are also in view: pg_clickhouse had a conversion bug that gave code execution and was fixed by its vendor.
How to use it
This is a research narrative, not a tool or a patch. For a reader the practical value is the mental model it teaches. A Datum carries no type information; the type OID in the surrounding structure decides which functions interpret it. A short list of things to try in a safe local setup: set debug_print_parse = on and run SELECT 1337 to see the CONST node; query pg_type for typlen, typbyval, typinput and typoutput; check pg_typeof('1337') to see the unknown type, OID 705. The LANGUAGE internal crash example needs a superuser and should only be run on a throwaway local instance.
How solid is it
Everything here is the author's own first-person account, including the claims of a 22-year-old zero-day, a new vulnerability class, and a very reliable exploit. The post shows the crash from calling textout on an int4 value and the parse-tree and catalog output. The part available to us ends before the composite-type exploit and the RCE, so those claims are not backed here by shown code or output. The age of the bug is the author's description; the visible text does not say how it was determined.
Risks and caveats
The text does not say whether the core issue needs superuser rights or ordinary privileges; superuser is stated only for LANGUAGE internal. It gives no affected versions, severity score, patch or PostgreSQL project response for the core bug. The only fix date mentioned (June 23) is for the pg_clickhouse chain. The author notes that PostgreSQL experts would probably not be surprised by the textout crash, which he found surprising himself. Treat the headline claims as unconfirmed until the full article and any project advisory are read.
“A Datum does NOT know what it represents.”
— Mehmet, author of the blog post, Part 3 of "Systemic Risks in the Managed PostgreSQL Industry"