Discussion
Search code, repositories, users, issues, pull requests...
sourcegrift: We have everything optimized, and yet somehow DB queries need to be "interpreted" at runtime. There's no reason for DB queries to not be precompiled.
jpfr: The "byte-code" coming from the query planner typically only has a handful of steps in a linear sequence. Joins, filters, and such. But the individual steps can be very costly.So there is not much to gain from JITing the query plan execution only.JITing begins to make more sense, when the individual query plan steps (join, filter, ...) themselves be specialized/recompiled/improved/merged by knowing the context of the query plan.
SigmundA: Postgresql uses a process per connection model and it has no way to serialize a query plan to some form that can be shared between processes, so the time it takes to make the plan including JIT is very important.Most other DB's cache query plans including jitted code so they are basically precompiled from one request to the next with the same statement.
hans_castorp: > and it has no way to serialize a query plan to some form that can be shared between processeshttps://www.postgresql.org/docs/current/parallel-query.html"PostgreSQL can devise query plans that can leverage multiple CPUs in order to answer queries faster."
SigmundA: Nothing to do with plan caching, thats just talking about plan execution of parallel operations which is that thread or process based in PG?If process based then they can send small parts of plan across processes.
hans_castorp: Ah, didn't see the caching part.Plans for prepared statements are cached though.
SigmundA: Yes if the client manually prepares the statement it will be cached for just that connection because in PG a connection is a process, but it won't survive from one connection to the next even in same process.Other databases like MSSQL have prepared statements but they are rarely used now days since plan caching based on query text was introduced decades ago.
the_biot: What sort of things are people doing in their SQL queries that make them CPU bound? Admittedly I'm a meat-and-potatoes guy, but I like mine I/O bound.Really amazed to see not one but several generic JIT frameworks though, no idea that was a thing.
martinald: Anything jsonb in my experience is quickly CPU bound...
jjice: Definitely. If you're doing regular queries with filters on jsonb columns, having the index directly on the JSON paths is really powerful. If I have a jsonb filter in the codebase at all, it probably needs an index, unless I know the result set is already very small.
martinald: Yeah, the other problem is I've really struggled to have postgres use multiple threads/cores on one query. Often maxes out one CPU thread while dozens go unused. I constantly have to fight loads of defaults to get this to change and even then I never feel like I can get it working quite right (probably operator error to some extent).This compares to clickhouse where it constantly uses the whole hardware. Obviously it's easier to do that on a columnar database but it seems that postgres is actively designed to _not_ saturate multiple cores, which may be a good assumption in the past but definitely isn't a good one now IMO.
asah: awesome! I wonder if it's possible to point AI at this problem and synthesize a bespoke compiler (per-architecture?) for postgresql expressions?
kvdveer: Two things are holding back current LLM-style AI of being of value here:* Latency. LLM responses are measured in order of 1000s of milliseconds, where this project targets 10s of milliseconds, that's off by almost two orders of magnitute.* Determinism. LLMs are inherently non-deterministic. Even with temperature=0, slight variations of the input lead to major changes in output. You really don't want your DB to be non-deterministic, ever.
qeternity: > LLMs are inherently non-deterministic.This isn't true, and certainly not inherently so.Changes to input leading to changes in output does not violate determinism.
yomismoaqui: Quoting:"But why aren’t LLM inference engines deterministic? One common hypothesis is that some combination of floating-point non-associativity and concurrent execution leads to nondeterminism based on which concurrent core finishes first."From https://thinkingmachines.ai/blog/defeating-nondeterminism-in...
qeternity: Yes, lots of things can create indeterminism. But nothing is inherent.
magicalhippo: > This isn't trueFrom what I understand, in practice it often is true[1]:Matrix multiplication should be “independent” along every element in the batch — neither the other elements in the batch nor how large the batch is should affect the computation results of a specific element in the batch. However, as we can observe empirically, this isn’t true.In other words, the primary reason nearly all LLM inference endpoints are nondeterministic is that the load (and thus batch-size) nondeterministically varies! This nondeterminism is not unique to GPUs — LLM inference endpoints served from CPUs or TPUs will also have this source of nondeterminism.[1]: https://thinkingmachines.ai/blog/defeating-nondeterminism-in...
qeternity: Yes, lots of things can create indeterminism. But nothing is inherent.
fabian2k: The last time I looked into it my impression was that disabling the JIT in PostgreSQL was the better default choice. I had a massive slowdown in some queries, and that doesn't seem to be an entirely unusual experience. It does not seem worth it to me to add such a large variability to query performance by default. The JIT seemed like something that could be useful if you benchmark the effect on your actual queries, but not as a default for everyone.
pjmlp: That is quite strange, given that big boys RDMS (Oracle, SQL Server, DB2, Informix,...) all have JIT capabilities for several decades now.
SigmundA: The big boys all cache query plans so the amount it time it take to compile is not really a concern.
vladich: Postgres caches query plans too, the problem is you can only cache what you can share, and if your planner works well, you can share very little, there can be a lot of unique plans even for the same query
SigmundA: No it cannot cache query plans between processes (connections) and the only way it can cache in the same process in the same connection is by the client manually preparing it, this was how the big boys did it 30 years ago, not anymore.Was common guidance back in the day to use stored procedures for all application access code because they where cached in MSSQL (which PG doesn't even do). Then around 2000 it started caching based on statement text and that became much less important.You would only used prepared statements if doing a bunch of inserts in a loop or something and it has a very small benefit now days only because its not sending the same text over the network over and over and hashing to lookup plan.
vladich: I didn't say it can cache between processes. The problem is not caching between processes, it's that caching itself is not very useful, because the planner creates different plans for different input parameters of the same query in the general case. So you can reliably cache plans only for the same sets of parameters. Or you can cache generic plans, which Postgres already does as well (and sharing that cache won't solve much of the problem too).
SigmundA: Other databases cache plans and have for years because it's very useful, many (most?) apps run many of the same statement with differing parameters, its a big win.They also do things like auto parameterization if the statement doesn't have them and parameter sniffing to make multiple different plans based on different values where it makes sense.https://learn.microsoft.com/en-us/sql/relational-databases/q...You can also get this, add HINTs to control this behavior if you don't like it or its causing a problem in production, crazy I know.https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-tr...PG is extremely primitive compared to these other systems in this area, and it has to be since it doesn't cache anything unless specifically instructed to for a single connection.
vladich: You make some unsubstantiated claims here. I assure you that it isn't as simple as you claim. And what Postgres does here is (mostly) the right thing, you can't do much better. You simply can't decide what plan you need to use based on the query and its parameters alone, unless you already cached that plan for those parameters (and even in that case you need to watch out for possible dramatic changes in statistics). Prepared statements != cached execution plans.
Asm2D: Many SQL engines have JIT compilers.The problems related to PostgreSQL are pretty much all described here. It's very difficult to do low-latency queries if you cannot cache the compiled code and do it over and over again. And once your JIT is slow you need a logic to decide whether to interpret or compile.I think it would be the best to start interpreting the query and start compilation in another thread, and once the compilation is finished and interpreter still running, stop the interpreter and run the JIT compiled code. This would give you the best latency, because there would be no waiting for JIT compiler.
chrisaycock: > I think it would be the best to start interpreting the query and start compilation in another threadThis technique is known as a "tiered JIT". It's how production virtual machines operate for high-level languages like JavaScript.There can be many tiers, like an interpreter, baseline compiler, optimizing compiler, etc. The runtime switches into the faster tier once it becomes ready.More info for the interested:https://ieeexplore.ieee.org/document/10444855
hinkley: It’s also common for JITs to sprout a tier and shed a tier over time, as the last and first tiers shift in cost/benefit. If the first tier works better you delay the other tiers. If the last tier gets faster (in run time or code optimization) you engage it sooner, or strip the middle tier entirely and hand half that budget to the last tier.
zaphirplane: What do you mean ? Cause the obvious thing is a shared cache and if there is one thing the writers of a db know it is locking
SigmundA: Sharing executable code between processes it not as easy as sharing data. AFAIK unless somethings changed recently PG shares nothing about plans between process and can't even share a cached plan between session/connections.
_flux: Write the binary to a file, call it `libquery-id1234.so`, and link that to whichever processes that need it?
vladich: Won't work well if it executes 20k+ queries per second. Filesystem will be a bottleneck among other things.
_flux: You can put more than one function in one file.