[PATCHES] SIGPIPE handling, take two.

2003-11-02 Thread Manfred Spraul
pqsecure_write tries to catch SIGPIPE signals generated by network 
disconnects by setting the signal handler to SIG_IGN. The current 
approach causes several problems:
- it always sets SA_RESTART when it restores the old handler.
- it's not reliable for multi threaded apps, because another thread 
could change the signal handler inbetween.
- it's slow, because after setting a signal handler to SIG_IGN the 
kernel must enumerate all threads and clear all pending signals (at 
least FreeBSD-5.1 and linux-2.6 do that. Earlier linux kernels don't - 
their signal handling is known to be broken for multithreaded apps).

Initially I proposed a new option for PQconnectdb, but Tom didn't like 
that. The attached patch autodetects if it should set the signal 
handler, Tom proposed that. The code doesn't try to check if the signal 
is handled by blocking it, because I haven't figured out how to check 
that: sigprocmask is undefined for multithreaded apps and calling 
pthread_sigmask would force every libpq user to link against libpthread.

--
   Manfred
? src/interfaces/libpq/libpq.so.3.1
Index: src/interfaces/libpq/fe-connect.c
===
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.263
diff -c -r1.263 fe-connect.c
*** src/interfaces/libpq/fe-connect.c   18 Oct 2003 05:02:06 -  1.263
--- src/interfaces/libpq/fe-connect.c   2 Nov 2003 18:29:40 -
***
*** 41,46 
--- 41,47 
  #include netinet/tcp.h
  #endif
  #include arpa/inet.h
+ #include signal.h
  #endif
  
  #include libpq/ip.h
***
*** 951,956 
--- 952,983 
else if (conn-sslmode[0] == 'a')   /* allow */
conn-wait_ssl_try = true;
  #endif
+   /* 
+* Autodetect SIGPIPE signal handling:
+* The default action per Unix spec is kill current process and
+* that's not acceptable. If the current setting is not the default,
+* then assume that the caller knows what he's doing and leave the
+* signal handler unchanged. Otherwise set the signal handler to
+* SIG_IGN around each send() syscall. Unfortunately this is both
+* unreliable and slow for multithreaded apps.
+*/
+   conn-do_sigaction = true;
+ #if !defined(HAVE_POSIX_SIGNALS)
+   {
+   pqsigfunc old;
+   old = signal(SIGPIPE, SIG_IGN);
+   if (old != SIG_DFL)
+   conn-do_sigaction = false;
+   signal(SIGPIPE, old);
+   }
+ #else
+   {
+   struct sigaction oact;
+ 
+   if (sigaction(SIGPIPE, NULL, oact) == 0  oact.sa_handler != SIG_DFL)
+   conn-do_sigaction = false;
+   }
+ #endif   /* !HAVE_POSIX_SIGNALS */
  
/*
 * Set up to try to connect, with protocol 3.0 as the first attempt.
Index: src/interfaces/libpq/fe-secure.c
===
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-secure.c,v
retrieving revision 1.32
diff -c -r1.32 fe-secure.c
*** src/interfaces/libpq/fe-secure.c29 Sep 2003 16:38:04 -  1.32
--- src/interfaces/libpq/fe-secure.c2 Nov 2003 18:29:41 -
***
*** 348,354 
ssize_t n;
  
  #ifndef WIN32
!   pqsigfunc   oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
  #endif
  
  #ifdef USE_SSL
--- 348,357 
ssize_t n;
  
  #ifndef WIN32
!   pqsigfunc   oldsighandler = NULL;
! 
!   if (conn-do_sigaction)
!   oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
  #endif
  
  #ifdef USE_SSL
***
*** 408,414 
n = send(conn-sock, ptr, len, 0);
  
  #ifndef WIN32
!   pqsignal(SIGPIPE, oldsighandler);
  #endif
  
return n;
--- 411,418 
n = send(conn-sock, ptr, len, 0);
  
  #ifndef WIN32
!   if (conn-do_sigaction)
!   pqsignal(SIGPIPE, oldsighandler);
  #endif
  
return n;
Index: src/interfaces/libpq/libpq-int.h
===
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/libpq-int.h,v
retrieving revision 1.82
diff -c -r1.82 libpq-int.h
*** src/interfaces/libpq/libpq-int.h5 Sep 2003 02:08:36 -   1.82
--- src/interfaces/libpq/libpq-int.h2 Nov 2003 18:29:42 -
***
*** 329,334 
--- 329,335 
charpeer_dn[256 + 1];   /* peer distinguished name */
charpeer_cn[SM_USER + 1];   /* peer common name */
  #endif
+   booldo_sigaction;   /* set SIGPIPE to SIG_IGN around every send() 
call */
  
/* Buffer for current error message */
PQExpBufferData errorMessage;   /* expansible string */

---(end of broadcast)---
TIP 4: Don't 'kill -9' the 

[PATCHES] psql: \dg and groups for \du

2003-11-02 Thread Markus Bertheau
Hi,

attached is a patch that adds display of the groups a user belongs to to
\du and a \dg command to psql. It's against 7.4beta5.

-- 
Markus Bertheau [EMAIL PROTECTED]
diff -ru postgresql-7.4beta5.orig/src/bin/psql/command.c postgresql-7.4beta5/src/bin/psql/command.c
--- postgresql-7.4beta5.orig/src/bin/psql/command.c	2003-10-11 20:04:26.0 +0200
+++ postgresql-7.4beta5/src/bin/psql/command.c	2003-11-02 19:31:27.0 +0100
@@ -363,6 +363,9 @@
 			case 'f':
 success = describeFunctions(pattern, show_verbose);
 break;
+			case 'g':
+success = describeGroups(pattern);
+break;
 			case 'l':
 success = do_lo_list();
 break;
diff -ru postgresql-7.4beta5.orig/src/bin/psql/describe.c postgresql-7.4beta5/src/bin/psql/describe.c
--- postgresql-7.4beta5.orig/src/bin/psql/describe.c	2003-10-17 02:57:04.0 +0200
+++ postgresql-7.4beta5/src/bin/psql/describe.c	2003-11-02 19:38:31.0 +0100
@@ -1272,12 +1272,13 @@
 			   WHEN u.usesuper THEN CAST('%s' AS pg_catalog.text)\n
 		WHEN u.usecreatedb THEN CAST('%s' AS pg_catalog.text)\n
 	 ELSE CAST('' AS pg_catalog.text)\n
-	END AS \%s\\n
+	END AS \%s\,\n
+	ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as \%s\\n
 	  FROM pg_catalog.pg_user u\n,
 	  _(User name), _(User ID),
 	  _(superuser, create database),
 	  _(superuser), _(create database),
-	  _(Attributes));
+	  _(Attributes), _(Groups));
 
 	processNamePattern(buf, pattern, false, false,
 	   NULL, u.usename, NULL, NULL);
@@ -1300,6 +1301,46 @@
 
 
 /*
+ * \dg
+ *
+ * Describes groups.
+ */
+bool
+describeGroups(const char *pattern)
+{
+	PQExpBufferData buf;
+	PGresult   *res;
+	printQueryOpt myopt = pset.popt;
+
+	initPQExpBuffer(buf);
+
+	printfPQExpBuffer(buf,
+	  SELECT g.groname AS \%s\,\n
+	g.grosysid AS \%s\\n
+	  FROM pg_catalog.pg_group g\n,
+	  _(Group name), _(Group ID));
+
+	processNamePattern(buf, pattern, false, false,
+	   NULL, g.groname, NULL, NULL);
+
+	appendPQExpBuffer(buf, ORDER BY 1;);
+
+	res = PSQLexec(buf.data, false);
+	termPQExpBuffer(buf);
+	if (!res)
+		return false;
+
+	myopt.nullPrint = NULL;
+	myopt.title = _(List of database groups);
+
+	printQuery(res, myopt, pset.queryFout);
+
+	PQclear(res);
+	return true;
+}
+
+
+/*
  * listTables()
  *
  * handler for \d, \dt, etc.
diff -ru postgresql-7.4beta5.orig/src/bin/psql/describe.h postgresql-7.4beta5/src/bin/psql/describe.h
--- postgresql-7.4beta5.orig/src/bin/psql/describe.h	2003-08-05 01:59:40.0 +0200
+++ postgresql-7.4beta5/src/bin/psql/describe.h	2003-11-02 19:31:27.0 +0100
@@ -25,6 +25,9 @@
 /* \du */
 bool		describeUsers(const char *pattern);
 
+/* \dg */
+bool		describeGroups(const char *pattern);
+
 /* \z (or \dp) */
 bool		permissionsList(const char *pattern);
 
diff -ru postgresql-7.4beta5.orig/src/bin/psql/help.c postgresql-7.4beta5/src/bin/psql/help.c
--- postgresql-7.4beta5.orig/src/bin/psql/help.c	2003-10-02 08:39:31.0 +0200
+++ postgresql-7.4beta5/src/bin/psql/help.c	2003-11-02 19:31:27.0 +0100
@@ -216,6 +216,7 @@
 	fprintf(output, _(  \\dd [PATTERN]  show comment for object\n));
 	fprintf(output, _(  \\dD [PATTERN]  list domains\n));
 	fprintf(output, _(  \\df [PATTERN]  list functions (add \+\ for more detail)\n));
+	fprintf(output, _(  \\dg [PATTERN]  list groups\n));
 	fprintf(output, _(  \\dn [PATTERN]  list schemas\n));
 	fprintf(output, _(  \\do [NAME] list operators\n));
 	fprintf(output, _(  \\dllist large objects, same as \\lo_list\n));
diff -ru postgresql-7.4beta5.orig/src/bin/psql/tab-complete.c postgresql-7.4beta5/src/bin/psql/tab-complete.c
--- postgresql-7.4beta5.orig/src/bin/psql/tab-complete.c	2003-10-17 13:52:06.0 +0200
+++ postgresql-7.4beta5/src/bin/psql/tab-complete.c	2003-11-02 19:32:40.0 +0100
@@ -614,7 +614,7 @@
 
 	static char *backslash_commands[] = {
 		\\a, \\connect, \\C, \\cd, \\copy, \\copyright,
-		\\d, \\da, \\dc, \\dC, \\dd, \\dD, \\df, \\di,
+		\\d, \\da, \\dc, \\dC, \\dd, \\dD, \\df, \\dg, \\di,
 		\\dl, \\dn, \\do, \\dp, \\ds, \\dS, \\dt, \\dT,
 		\\dv, \\du,
 		\\e, \\echo, \\encoding,

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

   http://archives.postgresql.org


Re: [PATCHES] UW 713UP3 patch

2003-11-02 Thread Peter Eisentraut
Larry Rosenman writes:

 The problem is MOST people will **NOT** be able to get the fixed compiler
 as it's on the Upgrade Pack path (PAY FOR), and **NOT** the Maintenance
 Pack path (Free).

Why did they upgrade to the broken compiler in the first place, and why
doesn't SCO provide free fixes for broken products?

-- 
Peter Eisentraut   [EMAIL PROTECTED]


---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


Re: [PATCHES] UW 713UP3 patch

2003-11-02 Thread Larry Rosenman


--On Sunday, November 02, 2003 23:05:21 +0100 Peter Eisentraut 
[EMAIL PROTECTED] wrote:

Larry Rosenman writes:

The problem is MOST people will **NOT** be able to get the fixed compiler
as it's on the Upgrade Pack path (PAY FOR), and **NOT** the Maintenance
Pack path (Free).
Why did they upgrade to the broken compiler in the first place, and why
doesn't SCO provide free fixes for broken products?
The Broken Compiler is in EVERY version prior to the UP3 compiler.  We 
just started tripping it with the changes in 7.4.

I don't know why they didn't/haven't put this fix in the MP path, and I 
can't change that decision, therefore, we need to work around it.

It's not that big of a deal.  See the patch I posted that SHOULD meet your
requirements.

--
Peter Eisentraut   [EMAIL PROTECTED]


--
Larry Rosenman http://www.lerctr.org/~ler
Phone: +1 972-414-9812 E-Mail: [EMAIL PROTECTED]
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749


pgp0.pgp
Description: PGP signature


Re: [PATCHES] UW 713UP3 patch

2003-11-02 Thread Tom Lane
Larry Rosenman [EMAIL PROTECTED] writes:
 +# version check for the 7.1.3UP3 compiler (version 401200310):
 +cat conftest.c __EOF__
 +int main(int argc, char **argv)
 +#if __SCO_VERSION__ =3D 401200310
 +#error good compiler
 +#else
 +#error bad compiler
 +#endif
 +__EOF__
 +  $CC conftest.c 2conftest.err 12
 +  grep -q good conftest.err
 +  if test $? =3D 0; then
 +CFLAGS=3D-O -Kinline
 +  else
 +CFLAGS=3D-O -Kinline,no_host
 +  fi

Couldn't this be simplified to

+cat conftest.c __EOF__
+int main(int argc, char **argv)
+{
+#if __SCO_VERSION__  401200310
+#error bad compiler
+#endif
+}
+__EOF__
+  $CC conftest.c /dev/null 21
+  if test $? = 0; then
+CFLAGS=-O -Kinline
+  else
+CFLAGS=-O -Kinline,no_host
+  fi

regards, tom lane

---(end of broadcast)---
TIP 8: explain analyze is your friend


[PATCHES] Two very minor win32 patches

2003-11-02 Thread Claudio Natoli
For src/backend/postmaster/postmaster.c and src/include/c.h
(Note: should ioctlsocket_ret be initialized to 1 for BEOS too, and can it
take an unsigned parameter? If so, could simplify.)





--- 
WE HAVE MOVED - PLEASE NOTE OUR NEW CONTACT DETAILS: 
THE BASEMENT, 33 EWELL STREET, BALMAIN NSW 2041 
TEL: +61 2 9555 1544 FAX: +61 2 9555 6911 
Certain disclaimers and policies apply to all email sent from Memetrics.
For the full text of these disclaimers and policies see 
http://www.memetrics.com/emailpolicy.html


Index: src/backend/postmaster/postmaster.c
===
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.341.2.1
diff -c -2 -r1.341.2.1 postmaster.c
*** src/backend/postmaster/postmaster.c 7 Sep 2003 04:36:53 -   1.341.2.1
--- src/backend/postmaster/postmaster.c 3 Nov 2003 05:09:26 -
***
*** 215,221 
  
  /* For FNCTL_NONBLOCK */
! #if defined(WIN32) || defined(__BEOS__)
  long  ioctlsocket_ret;
  #endif
  
  /* list of library:init-function to be preloaded */
--- 215,225 
  
  /* For FNCTL_NONBLOCK */
! #if defined(WIN32)
! unsigned long ioctlsocket_ret = 1;
! #endif
! #if defined(__BEOS__)
  long  ioctlsocket_ret;
  #endif
+ 
  
  /* list of library:init-function to be preloaded */
Index: src/include/c.h
===
RCS file: /projects/cvsroot/pgsql-server/src/include/c.h,v
retrieving revision 1.152
diff -c -2 -r1.152 c.h
*** src/include/c.h 4 Aug 2003 02:40:10 -   1.152
--- src/include/c.h 3 Nov 2003 05:12:10 -
***
*** 707,717 
  #define FCNTL_NONBLOCK(sock)  fcntl(sock, F_SETFL, O_NONBLOCK)
  #else
- extern long ioctlsocket_ret;
  
  /* Returns non-0 on failure, while fcntl() returns -1 on failure */
  #ifdef WIN32
  #define FCNTL_NONBLOCK(sock)  ((ioctlsocket(sock, FIONBIO, ioctlsocket_ret) == 0) ? 
0 : -1)
  #endif
  #ifdef __BEOS__
  #define FCNTL_NONBLOCK(sock)  ((ioctl(sock, FIONBIO, ioctlsocket_ret) == 0) ? 0 : 
-1)
  #endif
--- 707,718 
  #define FCNTL_NONBLOCK(sock)  fcntl(sock, F_SETFL, O_NONBLOCK)
  #else
  
  /* Returns non-0 on failure, while fcntl() returns -1 on failure */
  #ifdef WIN32
+  extern unsigned long ioctlsocket_ret;
  #define FCNTL_NONBLOCK(sock)  ((ioctlsocket(sock, FIONBIO, ioctlsocket_ret) == 0) ? 
0 : -1)
  #endif
  #ifdef __BEOS__
+  extern long ioctlsocket_ret;
  #define FCNTL_NONBLOCK(sock)  ((ioctl(sock, FIONBIO, ioctlsocket_ret) == 0) ? 0 : 
-1)
  #endif

---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


[PATCHES] bufmgr code cleanup

2003-11-02 Thread Neil Conway
This patch cleans up some of the bufmgr code:

- replace uses of

LockBuffer(buf, BUFFER_LOCK_UNLOCK);
ReleaseBuffer(buf);

  with the equivalent, but more concise:

UnlockAndReleaseBuffer(buf);

- analogous changes were made by replacing LockBuffer() + WriteBuffer()
with UnlockAndWriteBuffer()

- remove a bunch of #ifdef BMTRACE code, since it was ugly and broken
anyway

- remove an unused buffer descriptor bit flag (BM_PRIVATE)

- move the definition of INVALID_DESCRIPTOR to out of bufmgr.h and into
freelist.c, since it is the only file that uses it

- remove another unused function, and fix a few comments

Please apply to the CVS HEAD.

-Neil

Index: src/backend/access/hash/hashpage.c
===
RCS file: /var/lib/cvs/pgsql-server/src/backend/access/hash/hashpage.c,v
retrieving revision 1.42
diff -c -r1.42 hashpage.c
*** src/backend/access/hash/hashpage.c	4 Sep 2003 22:06:27 -	1.42
--- src/backend/access/hash/hashpage.c	31 Oct 2003 22:55:59 -
***
*** 135,142 
  void
  _hash_relbuf(Relation rel, Buffer buf)
  {
! 	LockBuffer(buf, BUFFER_LOCK_UNLOCK);
! 	ReleaseBuffer(buf);
  }
  
  /*
--- 135,141 
  void
  _hash_relbuf(Relation rel, Buffer buf)
  {
! 	UnlockAndReleaseBuffer(buf);
  }
  
  /*
***
*** 166,173 
  void
  _hash_wrtbuf(Relation rel, Buffer buf)
  {
! 	LockBuffer(buf, BUFFER_LOCK_UNLOCK);
! 	WriteBuffer(buf);
  }
  
  /*
--- 165,171 
  void
  _hash_wrtbuf(Relation rel, Buffer buf)
  {
! 	UnlockAndWriteBuffer(buf);
  }
  
  /*
Index: src/backend/access/heap/heapam.c
===
RCS file: /var/lib/cvs/pgsql-server/src/backend/access/heap/heapam.c,v
retrieving revision 1.157
diff -c -r1.157 heapam.c
*** src/backend/access/heap/heapam.c	1 Oct 2003 21:30:52 -	1.157
--- src/backend/access/heap/heapam.c	31 Oct 2003 22:59:14 -
***
*** 899,906 
  	 */
  	if (!ItemIdIsUsed(lp))
  	{
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		*userbuf = InvalidBuffer;
  		tuple-t_datamcxt = NULL;
  		tuple-t_data = NULL;
--- 899,905 
  	 */
  	if (!ItemIdIsUsed(lp))
  	{
! 		UnlockAndReleaseBuffer(buffer);
  		*userbuf = InvalidBuffer;
  		tuple-t_datamcxt = NULL;
  		tuple-t_data = NULL;
***
*** 1006,1013 
  	}
  	if (invalidBlock)
  	{
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		return NULL;
  	}
  
--- 1005,1011 
  	}
  	if (invalidBlock)
  	{
! 		UnlockAndReleaseBuffer(buffer);
  		return NULL;
  	}
  
***
*** 1033,1040 
  		!ItemPointerEquals(tid, ctid))
  		linkend = false;
  
! 	LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 	ReleaseBuffer(buffer);
  
  	if (!valid)
  	{
--- 1031,1037 
  		!ItemPointerEquals(tid, ctid))
  		linkend = false;
  
! 	UnlockAndReleaseBuffer(buffer);
  
  	if (!valid)
  	{
***
*** 1174,1181 
  
  	END_CRIT_SECTION();
  
! 	LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 	WriteBuffer(buffer);
  
  	/*
  	 * If tuple is cachable, mark it for invalidation from the caches in
--- 1171,1177 
  
  	END_CRIT_SECTION();
  
! 	UnlockAndWriteBuffer(buffer);
  
  	/*
  	 * If tuple is cachable, mark it for invalidation from the caches in
***
*** 1253,1260 
  
  	if (result == HeapTupleInvisible)
  	{
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		elog(ERROR, attempted to delete invisible tuple);
  	}
  	else if (result == HeapTupleBeingUpdated  wait)
--- 1249,1255 
  
  	if (result == HeapTupleInvisible)
  	{
! 		UnlockAndReleaseBuffer(buffer);
  		elog(ERROR, attempted to delete invisible tuple);
  	}
  	else if (result == HeapTupleBeingUpdated  wait)
***
*** 1301,1308 
  			   result == HeapTupleUpdated ||
  			   result == HeapTupleBeingUpdated);
  		*ctid = tp.t_data-t_ctid;
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		return result;
  	}
  
--- 1296,1302 
  			   result == HeapTupleUpdated ||
  			   result == HeapTupleBeingUpdated);
  		*ctid = tp.t_data-t_ctid;
! 		UnlockAndReleaseBuffer(buffer);
  		return result;
  	}
  
***
*** 1483,1490 
  
  	if (result == HeapTupleInvisible)
  	{
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		elog(ERROR, attempted to update invisible tuple);
  	}
  	else if (result == HeapTupleBeingUpdated  wait)
--- 1477,1483 
  
  	if (result == HeapTupleInvisible)
  	{
! 		UnlockAndReleaseBuffer(buffer);
  		elog(ERROR, attempted to update invisible tuple);
  	}
  	else if (result == HeapTupleBeingUpdated  wait)
***
*** 1531,1538 
  			   result == HeapTupleUpdated ||
  			   result == HeapTupleBeingUpdated);
  		*ctid = oldtup.t_data-t_ctid;
! 		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
! 		ReleaseBuffer(buffer);
  		return result;
  	}
  
--- 1524,1530 
  			   result ==