


Hard to say the best strategy for you use case on the available data. The example has a :pool_size setting of 1, which I’m guessing indicates a ‘pool’ with a single connection.īut a transaction should be fine too for my case. Replicas and dynamic repositories - Ecto v3.5.6.One potential workaround, to avoid using a transaction, seems to be to use a dynamic repo: Thanks – I did know the first part, but I hadn’t seen any explicit mention of connections being (equivalent to) sessions. Since a connection (maintained by Postgrex) is a session and connections are reused, you would be best served by ensuring you wrap the temporary table lifecycle in a transaction so the table is deleted at the end of the transaction. The above result was expected, since as per GTT definition rows will be deleted after commit.As you know, temporary tables are deleted at the end of session or transaction. NOTICE: merging column "state" with inherited definitionĮdb=# INSERT INTO global_temp VALUES (current_timestamp, 'node-2-request sended.', 'OK') Įdb=# INSERT INTO global_temp VALUES (current_timestamp, 'node-1-answer received.', 'Failed') Įdb=# INSERT INTO global_temp VALUES (current_timestamp, 'node-2-answer received.', 'OK') Įdb=# SELECT * FROM global_temp WHERE state = 'OK' NOTICE: merging column "action" with inherited definition NOTICE: merging column "ts" with inherited definition Let's insert some records into the Global Temporary Table and verify how it works:Įdb=# INSERT INTO global_temp VALUES (current_timestamp, 'node-1-request sended.', 'OK') (LIKE global_temp_backend INCLUDING ALL )ĬREATE TRIGGER insert_trigger_gt INSTEAD OF INSERT ON global_tempįOR EACH ROW EXECUTE PROCEDURE global_temp_insert() Įdb=# CREATE UNLOGGED TABLE global_temp_backend (Įdb=# CREATE VIEW global_temp AS SELECT * FROM global_temp_backend Įdb=# CREATE OR REPLACE FUNCTION global_temp_insert()Įdb$# INSERT INTO global_local_temp_backend VALUES(NEW.*) Įdb$# EXCEPTION WHEN undefined_table THENĮdb$# CREATE TEMP TABLE global_local_temp_backend () INHERITS (global_temp_backend)Įdb=# CREATE TRIGGER insert_trigger_gt INSTEAD OF INSERT ON global_tempĮdb$# FOR EACH ROW EXECUTE PROCEDURE global_temp_insert() * create local temporary table if not exists */ĬREATE TEMP TABLE global_local_temp_backend INSERT INTO global_local_temp_backend VALUES(NEW.*) Below is an example of such a trigger:ĬREATE OR REPLACE FUNCTION global_temp_insert() Re-route the insert to a Local Temporary Table.Ģ. Now, we can create an INSTEAD OF trigger on the view, which will do the following:ĬREATE a Local Temporary Table using the global_temp_backend definition if it does not exist in session.ġ. To create the above Global Temporary Table, we will first create a backend UNLOGGED table, global_temp_backend, as given below:ĬREATE UNLOGGED TABLE global_temp_backend (Īfter creating the above UNLOGGED table, we can create a view, which users will use as a Global Temporary Table:ĬREATE VIEW global_temp AS SELECT * FROM global_temp_backend A TRIGGER on view that will help in redirecting the INSERT on the view to the backend Local temporary table (LTT).īased on the above, let's look at an example of how DBAs and Developers can create a Global Temporary Table in EDB Postgres.īelow is a definition of a Global Temporary Table:ĬREATE GLOBAL TEMPORARY TABLE global_temp ( An automatic updatable VIEW with the name Global temporary table that will be used for the frontend SELECT/INSERT/DELETE/UPDATE andģ. An UNLOGGED table structure that can help in creating a backend LTT Ģ.

To implement a Global Temporary Table in EDB Postgres, a user must have following objects in EDB Postgres:ġ. ( ) supports both options and can be leveraged for a GTT.

Users also have the option of implementing a Global Temporary Table using a Local Temporary Table (LTT). However option two is not as easy to implement. Option one can be implemented as mentioned in the Tip:: PPAS 9.4 and Global Temporary Table. With above characteristics, there are two options available for GTT. The data in a Global Temporary Table are private, such that session can only access data inserted by a session. It’s an unlogged table which means any activity on this table will not be logged.ģ. The Global Temporary Table gives predefined structure for storing data.Ģ. In that blog I had shared the following characteristics a Global Temporary Table:ġ. In the past, I posted a blog on the concept of creating a Global Temporary Table (GTT) for migration from Oracle to EDB Postgres.
