Found solution for PostgreSQL. There is no real "temporary" rows but it allows to filter out the rows created in the sessions that already closed.
It based on the fact that the temporary tables names from all sessions could be listed by querying the pg_class
table.
Preparation:
create sequence seq_session_mark; -- Will be unique session ID
-- Test table
create table test_temp_rows(
id serial,
sid bigint default current_setting('public.sid')::bigint,
x text);
-- This view returns only rows created in active sessions
create view v_temp_rows as
select * from test_temp_rows
where
sid = any(
select substring(relname, length('session_mark_#'))::bigint
from pg_class
where relname like 'session_mark_%');
Usage:
Just after the start of each session execute:
-- Generate new session ID and store it
select set_config('public.sid', nextval('seq_session_mark')::text, false);
-- Create temporary table with name containing session ID
do $$
begin
execute 'create temp table session_mark_' || current_setting('public.sid') || '()';
end $$;
Test:
Session 1:
postgres=# insert into test_temp_rows(x) values ('a');
Session 2:
postgres=# table v_temp_rows;
┌────┬─────┬───┐
│ id │ sid │ x │
╞════╪═════╪═══╡
│ 1 │ 1 │ a │
└────┴─────┴───┘
postgres=# insert into test_temp_rows(x) values ('b');
Session 1:
postgres=# table v_temp_rows;
┌────┬─────┬───┐
│ id │ sid │ x │
╞════╪═════╪═══╡
│ 1 │ 1 │ a │
│ 2 │ 2 │ b │
└────┴─────┴───┘
Close session 1, then in session 2:
postgres=# table v_temp_rows;
┌────┬─────┬───┐
│ id │ sid │ x │
╞════╪═════╪═══╡
│ 2 │ 2 │ b │
└────┴─────┴───┘
You could periodically clean up your table:
delete from test_temp_rows
where sid != all(
select substring(relname, length('session_mark_#'))::bigint
from pg_class
where relname like 'session_mark_%');
PS: Probably something similar also could by done in MS SQL Server.