News for hackers. Stuff that matters.
Re:Features I'd Like in PostgreSQL by riku_iki on Saturday January 28, 2023 @04:03PM (#34561833)
You usually can manage this by dividing your query into subqueries each creating some temp table, so you have control over how joining actually happens.
Re:Features I'd Like in PostgreSQL by derefr on Saturday January 28, 2023 @04:20PM (#34562025)
It's a shame, then, that there's no way to define at-first-purely-in-memory tables, which only "spill" to disk if they cause your query to exceed work_mem.

Within PL/pgSQL, CREATE TEMPORARY TABLE is still (sometimes a lot!) slower than just SELECTing an array_agg(...) INTO a variable, and then `SELECT ... FROM unnest(that_variable) AS t` to scan over it. (And CTEs with MATERIALIZED are really just CREATE TEMPORARY TABLE in disguise, so that's no help.)

Re:Features I'd Like in PostgreSQL by taffer on Saturday January 28, 2023 @04:35PM (#34562191)
> It's a shame, then, that there's no way to define at-first-purely-in-memory tables, which only "spill" to disk if they cause your query to exceed work_mem.

But that isn't this exactly how temporary tables work? A temp tabe lives in memory and only spills to disk if it exceeds temp_buffers[1].

[1] https://www.postgresql.org/docs/current/runtime-config-resou...

Re:Features I'd Like in PostgreSQL by riku_iki on Saturday January 28, 2023 @04:51PM (#34562343)
that one is for some access buffers and not actual temp tables?
Re:Features I'd Like in PostgreSQL by taffer on Saturday January 28, 2023 @05:05PM (#34562469)
No temp_buffers is for temp tables and materialized CTEs. Maybe you are confusing it with shared_buffers?
Re:Features I'd Like in PostgreSQL by riku_iki on Saturday January 28, 2023 @05:16PM (#34562572)
I am trying to say that your link says it is for access buffer for accessing temp table, and it doesn't say actual temp table is stored in that buffer and not flushed on the disk.
Re:Features I'd Like in PostgreSQL by derefr on Saturday January 28, 2023 @04:53PM (#34562361)
Huh, I think you're right... but it's still slower! I've definitely measured this effect in practice.

Just spitballing here — I think the difference might come from where the metadata required to treat the table "as a table" in queries has to be entered into, and the overhead (esp. in terms of locking) required to do so.

Or, perhaps, it might come from the serialization overhead of converting "view" row-tuples (whose contents might be merged together from several actual material tables / function results) into flattened fully-materialized row-tuples... which, presumably, emitting data into an array-typed PL/pgSQL variable might get to skip, since the handles to the constituent data can be held inside the array and thunked later on when needed.

Re:Features I'd Like in PostgreSQL by taffer on Saturday January 28, 2023 @05:07PM (#34562496)
Temp tables are per session, there shouldn't be any locking involved.
Re:Features I'd Like in PostgreSQL by zeroimpl on Saturday January 28, 2023 @06:47PM (#34563238)
I believe the metadata about them is still written to various system catalog tables. Creating lots of temp tables will cause autovacuum activity on tables like pg_attr, for example.
Re:Features I'd Like in PostgreSQL by SigmundA on Saturday January 28, 2023 @04:55PM (#34562380)
Or there could just be a hint that says do it in source code order:

https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-tr...

There could also be hints that tell what kind of join to use and which indexes to use!

Re:Features I'd Like in PostgreSQL by aeyes on Saturday January 28, 2023 @05:49PM (#34562838)
If you do this be careful what your temp_buffers is set to so that your temp tables don't spill to disk. If you are on a network file system, for example on AWS RDS, writing big (a few hundred MB) temp tables to disk will stall all transactions.

The same is of course true when you have big joins that don't fit in work_mem but default size of this will be much larger.

I can usually fix bad plans with CTEs, no need to get much fancier. And the problem is often caused by schema design where you have a mapping table of two tables in the middle and your join is N-M-M where the planner has no information about the relationship between the two outer tables.

Re:Features I'd Like in PostgreSQL by riku_iki on Saturday January 28, 2023 @06:10PM (#34562976)
> temp tables don't spill to disk

my setup is local nvme ssd raid, so I hope this part won't be bottle neck. Also, if you are doing heavy join, where join order and method is need to be controlled, you temp table likely will be large, so you will need to be ready to have disk io.