I am having N number of tables(around 100) as shown below. These are base tables
CREATE TABLE table1 (
SignalID NCHAR(36) NOT NULL,
Timestamp VARCHAR(24) NOT NULL,
Value DOUBLE NOT NULL
)
CREATE TABLE table2 (
SignalID NCHAR(36) NOT NULL,
Timestamp VARCHAR(24) NOT NULL,
Value DOUBLE NOT NULL
)
table3(), table4(), so on.............
Now I have a final table as shown below
CREATE TABLE final_table (
Timestamp VARCHAR(24) NOT NULL,
table1_Value DOUBLE NOT NULL,
table2_Value DOUBLE NOT NULL,
table3_Value DOUBLE NOT NULL,
table4_Value DOUBLE NOT NULL,
so on,,,,,,,,,,,,,,,
)
Base tables (like table1, table2, table3 and so on) rows will be updated in realtime like 50 rows will be inseted per second. Now what i want is to join all the base tables and make them copied to final_table when timestamp of all the base tables are equal. This should happen in realtime. Whenever a new row is inserted in base tables, final_table should also update with new row.
Is it possible to implemet this?
I want a final table(with all value columns from base tables joined and copied to this) beacuse, I want to access that table using python code for processing the values. As there are 50 rows insterted every second, i am planning to use NOTIFY/LISTEN and Trigger method. It will be difficult to handle N number of triggers if I can create a final table.
I have tried using VIEW(With all the value columns from base table joined and copied to view). But there is issue with view where I cann’t use AFTER INSERT ON trigger on VIEWS. This only works on tables.
Or is there any kind of trigger on VIEW where a pg_notify will be sent to client when new row inserted to VIEW?
2
Answers
For storage and access, it’s easier, more convenient and efficient if you merge these tables and just save source info in an additional field for each entry. Whenever you want to structure the information the way you showed in your example, you can use
crosstab()
from tablefunc extension. Demo at db<>fiddle:date_bin()
(shown in the fiddle demo).avg()
might be a good idea in case somewhere, sometime you get two signals from the same source, with the same timestamp. You can swap this for any other aggregate function to process the peer group.union
, etc.Here’s how you can query one crosstab row.
Another take: you can keep feeding all signals into one, common table and construct your "incremental materialized view" on the fly, in the same singular trigger which broadcasts
notify
to your python code.Whenever you get a signal from any source, the trigger checks if it’s already collecting signals from that source and if not, adds the new source dynamically. The same trigger relays the incoming information to your python code via
pg_notify()
.Generating some sample signals:
You can see they automatically populate your other table on the fly, adjusting its structure accordingly.
fiddle
notify
for your python code tolisten
to.alter table
gets, so if you don’t ever need the full table, you can just keep a view for the purpose of looking up signals that match the new one’s timestamp, and feed that intonotify
.notify
and assemble/update your output structure entirely in python.