Click here to Skip to main content
15,892,059 members
Articles / Database Development / PostgreSQL
Tip/Trick

A PostgreSQL Notification Example

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
15 Oct 2020CPOL1 min read 41.2K   2.8K   22   16
PostgreSQL notifications Windows Forms application
In this tip, you will see a fully fledged PostgreSQL notifications WinForms application created with VS 2013.

Introduction

This is a fully fledged PostgreSQL notifications Windows Forms application created with Visual Studio 2013.

There is also an updated version for VS2019 which uses Npgsql 4.1.5.

When searching the internet, I only found scraps of information on how to do notification events in PostgreSQL, but not a complete working example.

Image 1

Using the Code

So here it is, the only thing that needs to be present is a database, the table and triggers will be created by the application.

I tested the old version with PostgreSQL 9.5 and the NpgSql 2.1.3 driver.

The updated version was tested with PostgreSQL 10.14 and NpgSql 4.1.5.

After pressing the <Start> button, listening starts with the PostgreSQL "listen mynotification" command.
Notification events can be triggered from the application using the <Trigger> and <Insert row> buttons, and also by directly editing the database.

Image 2

Points of Interest

More information can be found from the links below:

There is also a portable PostgreSQL version, I don't like the newest versions as they tend to be bigger and don't have PgAdminIII included with them, so my favourite remains version 9.4.5 which is only a 10 MB download.

You can download it from the link provided on top, or from:

History

  • V 1.0 Old version
  • V 2.0 New version for NpgSql 4.1.5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
Software developer in the Netherlands, currently working on Video Surveillance applications.
Experience: C#, C++, VB, ASP, SQL Server, PostgreSQL, Gitea, TeamCity.
It all started with Black&White, no not the whiskey but the Sinclair ZX81 followed by several Atari's and PC's. The journey continues ...

Comments and Discussions

 
GeneralMy vote of 5 Pin
rtty26-Oct-21 9:21
rtty26-Oct-21 9:21 
GeneralMy vote of 5 Pin
Jan Heckman15-Oct-20 23:28
professionalJan Heckman15-Oct-20 23:28 
GeneralRe: My vote of 5 Pin
RickZeeland15-Oct-20 23:48
mveRickZeeland15-Oct-20 23:48 
Questionthanks for your post it's working. However I try to add/delete multiple records not working Pin
Eranga Dayarathne11-Apr-19 17:34
Eranga Dayarathne11-Apr-19 17:34 
GeneralMy vote of 5 Pin
Maciej Los6-Nov-18 9:06
mveMaciej Los6-Nov-18 9:06 
QuestionNeed help getting this to work with Npgsql 4.x.x Pin
9lbhammer12-Jun-18 7:47
9lbhammer12-Jun-18 7:47 
AnswerRe: Need help getting this to work with Npgsql 4.x.x Pin
RickZeeland12-Jun-18 10:00
mveRickZeeland12-Jun-18 10:00 
GeneralRe: Need help getting this to work with Npgsql 4.x.x Pin
9lbhammer12-Jun-18 10:15
9lbhammer12-Jun-18 10:15 
GeneralRe: Need help getting this to work with Npgsql 4.x.x Pin
RickZeeland12-Jun-18 10:21
mveRickZeeland12-Jun-18 10:21 
NewsRe: Need help getting this to work with Npgsql 4.x.x Pin
RickZeeland14-Oct-20 23:49
mveRickZeeland14-Oct-20 23:49 
QuestionA Bit More Info... Pin
C Grant Anderson12-Mar-18 8:24
professionalC Grant Anderson12-Mar-18 8:24 
AnswerRe: A Bit More Info... Pin
RickZeeland12-Mar-18 8:42
mveRickZeeland12-Mar-18 8:42 
GeneralRe: A Bit More Info... Pin
Samuel Teixeira15-Mar-18 4:30
Samuel Teixeira15-Mar-18 4:30 
GeneralRe: A Bit More Info... Pin
RickZeeland15-Mar-18 4:37
mveRickZeeland15-Mar-18 4:37 
GeneralRe: A Bit More Info... Pin
Samuel Teixeira15-Mar-18 4:41
Samuel Teixeira15-Mar-18 4:41 
GeneralRe: A Bit More Info... Pin
Kirk 1038982116-Oct-20 11:56
Kirk 1038982116-Oct-20 11:56 
Think of it as an EventSink for a Database Table!

We used DBMS_SEND() in Oracle with a listener queued up waiting for a message (actually one per server, listening).

When internet bids came in, they were processed against the database, the result was An "Event" that told all the servers, hey "This is updated data" (no polling, the process waits for an interrupt, it could be for a timeout period, or infinite).

Then the servers pushed that data (bid updates) back out to the clients that were connected to it.

Our implementation pre-dated the more elaborate queuing that is available now. Oracles implementation was quite fast, 16ms or faster, and it carries ZERO overhead when there are no bids being updated.
In fact, we have another client using it to offline "long lookups" (like reverse DNS) for improved reporting that happens later. NO reason to tie up the DB with that work. If the lookup needs to happen a message is sent, and nobody cares how long it takes, because it isn't needed until hours later.

==

One other thing you COULD do with it. Configure your website to log 404 errors, and then after so many from the same IP address, trigger an external process to BAN the IP Address with a firewall command.
If it's done well, you can push the specific program to have it's own security profile outside of the database!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.