Index: src/bin/psql/describe.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/describe.c,v
retrieving revision 1.147
diff -c -r1.147 describe.c
*** src/bin/psql/describe.c	9 Oct 2006 23:30:33 -0000	1.147
--- src/bin/psql/describe.c	7 Nov 2006 05:19:22 -0000
***************
*** 1054,1065 ****
  				   *result3 = NULL,
  				   *result4 = NULL,
  				   *result5 = NULL,
! 				   *result6 = NULL;
  		int			check_count = 0,
  					index_count = 0,
  					foreignkey_count = 0,
  					rule_count = 0,
  					trigger_count = 0,
  					inherits_count = 0;
  		int			count_footers = 0;
  
--- 1054,1067 ----
  				   *result3 = NULL,
  				   *result4 = NULL,
  				   *result5 = NULL,
! 				   *result6 = NULL,
! 				   *result7 = NULL;
  		int			check_count = 0,
  					index_count = 0,
  					foreignkey_count = 0,
  					rule_count = 0,
  					trigger_count = 0,
+ 					disabled_trigger_count = 0,
  					inherits_count = 0;
  		int			count_footers = 0;
  
***************
*** 1125,1131 ****
  					 "SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid)\n"
  							  "FROM pg_catalog.pg_trigger t\n"
  							  "WHERE t.tgrelid = '%s' "
! 							  "AND (not tgisconstraint "
  							  " OR NOT EXISTS"
  							  "  (SELECT 1 FROM pg_catalog.pg_depend d "
  							  "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
--- 1127,1134 ----
  					 "SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid)\n"
  							  "FROM pg_catalog.pg_trigger t\n"
  							  "WHERE t.tgrelid = '%s' "
! 							  "AND t.tgenabled "
! 							  "AND (NOT t.tgisconstraint "
  							  " OR NOT EXISTS"
  							  "  (SELECT 1 FROM pg_catalog.pg_depend d "
  							  "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
***************
*** 1142,1147 ****
--- 1145,1175 ----
  			}
  			else
  				trigger_count = PQntuples(result4);
+ 
+ 			/* acquire disabled triggers as a separate list */
+ 			printfPQExpBuffer(&buf,
+ 					 "SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid)\n"
+ 							  "FROM pg_catalog.pg_trigger t\n"
+ 							  "WHERE t.tgrelid = '%s' "
+ 							  "AND NOT t.tgenabled "
+ 							  "AND (NOT t.tgisconstraint "
+ 							  " OR NOT EXISTS"
+ 							  "  (SELECT 1 FROM pg_catalog.pg_depend d "
+ 							  "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
+ 							  "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))"
+ 							  "   ORDER BY 1",
+ 							  oid);
+ 			result7 = PSQLexec(buf.data, false);
+ 			if (!result7)
+ 			{
+ 				PQclear(result1);
+ 				PQclear(result2);
+ 				PQclear(result3);
+ 				PQclear(result4);
+ 				goto error_return;
+ 			}
+ 			else
+ 				disabled_trigger_count = PQntuples(result7);
  		}
  
  		/* count foreign-key constraints (there are none if no triggers) */
***************
*** 1160,1165 ****
--- 1188,1194 ----
  				PQclear(result2);
  				PQclear(result3);
  				PQclear(result4);
+ 				PQclear(result7);
  				goto error_return;
  			}
  			else
***************
*** 1305,1310 ****
--- 1334,1361 ----
  			}
  		}
  
+ 		/* print disabled triggers */
+ 		if (disabled_trigger_count > 0)
+ 		{
+ 			printfPQExpBuffer(&buf, _("Disabled triggers:"));
+ 			footers[count_footers++] = pg_strdup(buf.data);
+ 			for (i = 0; i < disabled_trigger_count; i++)
+ 			{
+ 				const char *tgdef;
+ 				const char *usingpos;
+ 
+ 				/* Everything after "TRIGGER" is echoed verbatim */
+ 				tgdef = PQgetvalue(result7, i, 1);
+ 				usingpos = strstr(tgdef, " TRIGGER ");
+ 				if (usingpos)
+ 					tgdef = usingpos + 9;
+ 
+ 				printfPQExpBuffer(&buf, "    %s", tgdef);
+ 
+ 				footers[count_footers++] = pg_strdup(buf.data);
+ 			}
+ 		}
+ 
  		/* print inherits */
  		for (i = 0; i < inherits_count; i++)
  		{
***************
*** 1340,1345 ****
--- 1391,1397 ----
  		PQclear(result4);
  		PQclear(result5);
  		PQclear(result6);
+ 		PQclear(result7);
  	}
  
  	printTable(title.data, headers,
