# Detecting Postgresql Table Change for LiveView

**URL:** <https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114>\
**Category:** Frontend Chat/Discussions\
**Tags:** postgres, liveview\
**Created:** [18 July 2020 15:01 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114 "2020-07-18T15:01:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://forum.devtalk.com/user_avatar/forum.devtalk.com/michael/32/590_2.png) [@michael](https://forum.devtalk.com/u/michael)\
**Post date:** [18 July 2020 15:01 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/1 "2020-07-18T15:01:00Z")

</div>

So I have a table of MediaItems and when that list changes, say new entries, I’d like LiveView to know about that so that the Timeline updates automatically. How to know when the table updates? I’ve seen supabase but that appears to be for JS.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [18 July 2020 16:33 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/2 "2020-07-18T16:33:17Z")

</div>

You are looking for [Change Data Capture](https://medium.com/@ramesh.esl/change-data-capture-cdc-in-postgresql-7dee2d467d1b) which goes well beyond the scope of PostgreSQL. If you can achieve CDC with your DB then you should be able to pretty easily propagate events to any other frontend/backend system, LiveView included.

---

<div class="post-metadata">

**Author:** ![michael](https://forum.devtalk.com/user_avatar/forum.devtalk.com/michael/32/590_2.png) [@michael](https://forum.devtalk.com/u/michael)\
**Post date:** [18 July 2020 21:54 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/3 "2020-07-18T21:54:25Z")

</div>

Hi thanks for the reply because I never heard of CDC before(probably because when it comes to backend development I’m just a novice). I do think though that CDC goes way beyond the scope of what I’m looking for. I’m just trying to learn LiveView and everything I’ve found about is so far in terms of examples is user-initiated actions changing state rather than server events. I think if I followed this CDC path I would get bogged-down in the details of CDC rather than develop the frontend.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.devtalk.com/user_avatar/forum.devtalk.com/kokolegorille/32/575_2.png) [@kokolegorille](https://forum.devtalk.com/u/kokolegorille)\
**Post date:** [18 July 2020 22:31 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/4 "2020-07-18T22:31:14Z")

</div>

You might use postgresql triggers to be notified when tables update.

I remember following this post when doing so…

> **[Callbacks in the database (Postgres)](https://medium.com/@kaisersly/callbacks-in-the-database-postgres-5bb4ae3f4fd9)**
>
> WARNING : This is more about Postgres than Ecto.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [18 July 2020 22:56 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/5 "2020-07-18T22:56:31Z")

</div>

Triggers definitely don’t scale well though. They slow down the database exponentially with every new one you add. Of course for many apps it won’t ever matter and they are indeed the easiest way out.

* * *

@michael it really depends on your goal. If you just want to capture a monotonically increasing log of user actions and act on them then what you said is absolutely going to work. But don’t forget that sometimes net hiccups happen, people get pissed and start clicking an element 20 times until the UI unfreezes etc. 😃 That’s why I offer CDC, as complex as it is, because all of your code is going to “drink from the same well”.

But I get that you don’t want complexity so you could try with DB triggers.

---

<div class="post-metadata">

**Author:** ![Exadra37](https://forum.devtalk.com/user_avatar/forum.devtalk.com/exadra37/32/683_2.png) [@Exadra37](https://forum.devtalk.com/u/Exadra37)\
**Post date:** [19 July 2020 09:23 UTC](https://forum.devtalk.com/t/detecting-postgresql-table-change-for-liveview/1114/6 "2020-07-19T09:23:10Z")

</div>

> [@michael](#):
>
> So I have a table of MediaItems and when that list changes, say new entries, I’d like LiveView to know about that so that the Timeline updates automatically. How to know when the table updates?

You use Phoenix PubSub to broadcast them each time you create, update or delete them.

Then in the `handle_info` on your live view you can use [send\_update/2](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#send_update/2) to update the client.

Some useful links:

- [Phoenix LiveView Tutorial: PubSub and Pow Authentication | Curiosum](https://curiosum.dev/blog/elixir-phoenix-liveview-messenger-part-3)
- [Building Real-Time Features with Phoenix Live View and PubSub | Blog · Elixir School](https://elixirschool.com/blog/live-view-with-pub-sub/)
