Notification table to every user

Im working on a social network and now I’d really like to implement a notifications feature. This feature works like this: every time a user likes a photo, the uploader should get notified in real time. So I thought I should create a notifications table for every user and then use pg_notify() to notify the user in real time using Javascript (front end). So every time a user likes a photo, a new row should be listed in the uploader notifications table so only the uploader will get notified with pg_notify(). Because i want to use pg_notify() and every user should be listening to his own table, it would not make sense to pack all notifications of all users in only one table. Does that make sense? or should I think of another solution? Please help me if you know a better way of doing this. thank you :slight_smile:

maybe you could consider partitioning, but it highly depends on the amount of data, how you want to retrieve those data and so on. Can we switch to german language, it’s a german speaking forum.

Danke für deine Antwort. Partitioning hört sich gut an. Aber kann man pg_notify() auf eine konkrete Partition der Tabelle anwenden?

Ich bin mir nicht sicher, Dich richtig zu verstehen, aber du kannst individuelle TRIGGER erstellen, oder eben die Triggerfunktion für alle Partitionen erstellen.

Also ich habe die Benachrichtigungen so implementiert:

Benachrichtigungstabelle:

DROP TABLE IF EXISTS notifications;

CREATE TABLE notifications (
id SERIAL NOT NULL,
username_from VARCHAR(30) NOT NULL,
username_to VARCHAR(30) PRIMARY KEY,
date TIMESTAMP NOT NULL DEFAULT NOW(),
content text
)PARTITION BY LIST(username_to);

Die Tabelle wird nach Benutzernamen partitioniert. Das würde so ja gut funktionieren, wenn ein neuer Benutzer sich zum ersten mal einlogget. Beim ersten einloggen wird seine Partition erstellt, wie folgt:

CREATE TABLE jo_notifications PARTITION OF notifications FOR VALUES IN (‘jo’);

dann wird für diesen Benutzer ein Trigger angelegt, der auf seine Partition ( in diesem Fall ‘user1_notifications’ ) hört. So wird pg_notify() nur auf die Benachrichtigungen von diesem einen Benutzer angewendet. Ein Problem bleibt noch: Ich muss noch für schon bestehende Benutzer eine Partition erstellen und ich möchte das nicht manuell machen also ich muss eine Funktion schreiben die für jeden bestehenden Benutzer eine Partition anlegt. Das kriege ich grade nicht in psql hin aber es ist ja nicht schlimm ich könnte das über python machen also ganz naiv, :smiley:

Dieses Thema wurde automatisch 60 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.