1
$ sudo netstat -ap | grep postg
tcp        0      0 localhost:postgresql    0.0.0.0:*               LISTEN      1567/postgres       
udp        0      0 localhost:57504         localhost:57504         ESTABLISHED 1567/postgres       
unix  2      [ ACC ]     STREAM     LISTENING     27116    1567/postgres        /var/run/postgresql/.s.PGSQL.5432

Why does postgresql server have a localhost:57504 to localhost:57504 UDP "connection"? What does that mean?

Thanks.

  • Fun fact, udp sockets have completely different semantics and behavior than TCP sockets, because UDP is very different than TCP. see man7.org/linux/man-pages/man7/udp.7.html – 炸鱼薯条德里克 Mar 20 at 23:42
  • Don't trust the remote IP / Port of UDP. As 炸鱼薯条德里克 says, UDP is different. And port 57504 sounds like an Ephemeral Port en.wikipedia.org/wiki/Ephemeral_port You might want to read this : postgresql.org/message-id/21283.1008989228%40sss.pgh.pa.us – Philip Couling Mar 20 at 23:43
  • @PhilipCouling Is there some client "connecting" to postgresql server via UDP? I didn't create such client explicitly. Did you mean "that would be the connection to the statistics collector subprocess" in the second link? – Tim Mar 20 at 23:50
  • Basically UDP is stateless, any UDP endpoint has its own local address, but no foreign address, unlike connection based TCP, which has an important state called foreign address. Linux support foreign address settings for a UDP socket as a default packet sending target, but you don't have to follow the default. – 炸鱼薯条德里克 Mar 21 at 2:05
  • TCP connection sockets doesn't support this, it's the abstraction of a TCP connection provided by linux. Once the TCP connection is established, all data has to be send to the exact other endpoint of the connection, it doesn't support real sendto() semantics. – 炸鱼薯条德里克 Mar 21 at 2:13
3

A little bit of digging this is the stats collector. Apparently stats are posted on local loopback (aka localhost) UDP. See here: http://www.neilconway.org/talks/hacking/ottawa/ottawa_slides.pdf . Essentially the different (backend) subprocesses of PostgreSQL are using this UDP port to send statistics to the stats collector. What you are seeing is the "listening" socket waiting to receive messages.

Port 57504 is an Ephemeral port. Meaning nobody picked it, it's randomly assigned at runtime. This is noted here: https://www.postgresql.org/message-id/1273002081.2590.7.camel@w-ehs-psa.telbiomed.at

Note that UDP is very different to TCP because it doesn't care about connections. UDP sockets behave much more like a mailbox; accepting messages from whatever and sending wherever each one is addressed. So the remote host and remote port can often be ignored.

UDP also doesn't care about whether packets are received or not. I suspect that UDP was chosen because the backend will never wait for the stats collector even if the stats collector gets flooded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.