getaddrinfo() for threading instead of gethostbyname()

Started by Bruce Momjianover 22 years ago4 messages
#1Bruce Momjian
pgman@candle.pha.pa.us

When FreeBSD didn't have gethostbyname_r(), and gethostbyname() wasn't
thread-safe, Marc asked around and found out that the threaded solution
for this is to use getaddrinfo(). This makes sense because
getaddrinfo() is described as:

getaddrinfo () function is defined for protocol-independent nodename-
to-address translation. It performs functionality of gethostbyname(3)
and getservbyname(3), in more sophisticated manner.

A number of platforms have getpwuid_r(), but not gethostbyname_r(). I
now realize it is because they are assuming you arew using getaddrinfo(),
which has freeaddrinfo() to free the allocated memory in a thread-safe
manner.

Right now, we call gethostname() from two places:

port/getaddrinfo() (if backend)
port/thread.c::pqGethostbyname()

and pqGethostbyname() (thread-safe) is called only by:

port/getaddrinfo() (if frontend)
libpq/fe-secure.c

If we convert fe-secure.c to use getaddrinfo(), then all host address
lookups go through getaddrinfo().

Then, if we don't need our port/getaddrinfo(), we don't care about a
non-thread-safe gethostbyname() on that platform. This improves our
thread-safe support. Specifically, it prevents host name lookups from
be serialized by our pthread locks.

Once everything goes through getaddrinfo(), I will modify my thread test
program to test gethostbyname() threading _only_ if getaddrinfo()
doesn't exist.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#2Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Bruce Momjian (#1)
Re: getaddrinfo() for threading instead of gethostbyname()

OK, the thread test program is read for platform testing,
src/tools/thread_test. You will find the README, Makefile tests, and
program output to be very clear and almost error-proof.

Please run it on platforms we support and report back. Thanks.

---------------------------------------------------------------------------

Bruce Momjian wrote:

When FreeBSD didn't have gethostbyname_r(), and gethostbyname() wasn't
thread-safe, Marc asked around and found out that the threaded solution
for this is to use getaddrinfo(). This makes sense because
getaddrinfo() is described as:

getaddrinfo () function is defined for protocol-independent nodename-
to-address translation. It performs functionality of gethostbyname(3)
and getservbyname(3), in more sophisticated manner.

A number of platforms have getpwuid_r(), but not gethostbyname_r(). I
now realize it is because they are assuming you arew using getaddrinfo(),
which has freeaddrinfo() to free the allocated memory in a thread-safe
manner.

Right now, we call gethostname() from two places:

port/getaddrinfo() (if backend)
port/thread.c::pqGethostbyname()

and pqGethostbyname() (thread-safe) is called only by:

port/getaddrinfo() (if frontend)
libpq/fe-secure.c

If we convert fe-secure.c to use getaddrinfo(), then all host address
lookups go through getaddrinfo().

Then, if we don't need our port/getaddrinfo(), we don't care about a
non-thread-safe gethostbyname() on that platform. This improves our
thread-safe support. Specifically, it prevents host name lookups from
be serialized by our pthread locks.

Once everything goes through getaddrinfo(), I will modify my thread test
program to test gethostbyname() threading _only_ if getaddrinfo()
doesn't exist.

-- 
Bruce Momjian                        |  http://candle.pha.pa.us
pgman@candle.pha.pa.us               |  (610) 359-1001
+  If your life is a hard drive,     |  13 Roberts Road
+  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#3Nigel J. Andrews
nandrews@investsystems.co.uk
In reply to: Bruce Momjian (#2)
Re: getaddrinfo() for threading instead of gethostbyname()

On Sat, 27 Sep 2003, Bruce Momjian wrote:

OK, the thread test program is read for platform testing,
src/tools/thread_test. You will find the README, Makefile tests, and
program output to be very clear and almost error-proof.

Please run it on platforms we support and report back. Thanks.

Debian linux
kernel 2.2.x (- which was 2.0 or 2.2 of debian I think, can't remember)
glibc 2.1.3
gcc 2.95.2
it looks like the thread stuff is enabled in ports/linux in cvs tip

hmmmm...

software@ponder:~/database/postgres/pgsql/src/tools/thread$ make
gcc -O2 -g -Wall -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -I../../../src/include -D_GNU_SOURCE -c -o thread_test.o thread_test.c
gcc -O2 -g -Wall -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -L../../../src/port -Wl,-rpath,/usr/local/stow/postgresql-7.3.4/lib -lpthread thread_test.o -lpam -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm -lpgport -o thread_test
software@ponder:~/database/postgres/pgsql/src/tools/thread$ ./thread_test
Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'
defines to your template/$port file before compiling this program.

Your gethostbyname() is _not_ thread-safe
Your getpwuid() is _not_ thread-safe
Not all non-*_r functions are thread-safe.
Add this to your template/$port file:

NEED_REENTRANT_FUNCS=yes
software@ponder:~/database/postgres/pgsql/src/tools/thread$

Nigel

#4Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Nigel J. Andrews (#3)
Re: getaddrinfo() for threading instead of gethostbyname()

Thanks. That's what I needed. Yes, I left the stuff enabled becuase
the new test is pretty much the same as the old one, but some OS's might
pass the tests if they only failed on gethostbyname().

---------------------------------------------------------------------------

Nigel J. Andrews wrote:

On Sat, 27 Sep 2003, Bruce Momjian wrote:

OK, the thread test program is read for platform testing,
src/tools/thread_test. You will find the README, Makefile tests, and
program output to be very clear and almost error-proof.

Please run it on platforms we support and report back. Thanks.

Debian linux
kernel 2.2.x (- which was 2.0 or 2.2 of debian I think, can't remember)
glibc 2.1.3
gcc 2.95.2
it looks like the thread stuff is enabled in ports/linux in cvs tip

hmmmm...

software@ponder:~/database/postgres/pgsql/src/tools/thread$ make
gcc -O2 -g -Wall -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -I../../../src/include -D_GNU_SOURCE -c -o thread_test.o thread_test.c
gcc -O2 -g -Wall -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -L../../../src/port -Wl,-rpath,/usr/local/stow/postgresql-7.3.4/lib -lpthread thread_test.o -lpam -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm -lpgport -o thread_test
software@ponder:~/database/postgres/pgsql/src/tools/thread$ ./thread_test
Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'
defines to your template/$port file before compiling this program.

Your gethostbyname() is _not_ thread-safe
Your getpwuid() is _not_ thread-safe
Not all non-*_r functions are thread-safe.
Add this to your template/$port file:

NEED_REENTRANT_FUNCS=yes
software@ponder:~/database/postgres/pgsql/src/tools/thread$

Nigel

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073