Index: Pg.pm
===================================================================
RCS file: /usr/local/cvsroot/dbdpg/dbdpg/Pg.pm,v
retrieving revision 1.18
diff -c -r1.18 Pg.pm
*** Pg.pm	26 Feb 2003 19:01:36 -0000	1.18
--- Pg.pm	20 Mar 2003 22:26:05 -0000
***************
*** 57,62 ****
--- 57,78 ----
  	$drh;
      }
  
+     ## Used by both the dr and db packages
+     sub pg_server_version {
+ 		my $dbh = shift;
+ 		return $dbh->{pg_server_version} if defined $dbh->{pg_server_version};
+         my ($version) = $dbh->selectrow_array("SELECT version();");
+         return 0 unless $version =~ /^PostgreSQL ([\d\.]+)/;
+         $dbh{pg_server_version} = $1;
+         return $dbh{pg_server_version};
+ 	}
+ 
+     sub pg_use_catalog {
+       my $dbh = shift;
+       my $version = DBD::Pg::pg_server_version($dbh);
+       return $version < 7.3 ? "" : "pg_catalog.";
+     }
+ 
      1;
  }
  
***************
*** 68,74 ****
          my $drh = shift;
          my $dbh = DBD::Pg::dr::connect($drh, 'dbname=template1') or return undef;
          $dbh->{AutoCommit} = 1;
!         my $sth = $dbh->prepare("SELECT datname FROM pg_database ORDER BY datname") or return undef;
          $sth->execute or return undef;
          my (@sources, @datname);
          while (@datname = $sth->fetchrow_array) {
--- 84,91 ----
          my $drh = shift;
          my $dbh = DBD::Pg::dr::connect($drh, 'dbname=template1') or return undef;
          $dbh->{AutoCommit} = 1;
!         my $CATALOG = DBD::Pg::pg_use_catalog($dbh);
!         my $sth = $dbh->prepare("SELECT datname FROM ${CATALOG}pg_database ORDER BY datname");
          $sth->execute or return undef;
          my (@sources, @datname);
          while (@datname = $sth->fetchrow_array) {
***************
*** 153,166 ****
  		my ($dbh) = shift;
  		my @attrs = @_;
  		# my ($dbh, $catalog, $schema, $table, $column) = @_;
  
  		my @wh = ();
! 		my @flds = qw/catname u.usename c.relname a.attname/;
  
  		for my $idx (0 .. $#attrs) {
  			next if ($flds[$idx] eq 'catname'); # Skip catalog
! 			if(defined $attrs[$idx]) {
! # Insure that the value is enclosed in single quotes.
  				$attrs[$idx] =~ s/^'?(\w+)'?$/'$1'/;
  				if ($attrs[$idx] =~ m/[,%]/) {
  					# contains a meta character.
--- 170,184 ----
  		my ($dbh) = shift;
  		my @attrs = @_;
  		# my ($dbh, $catalog, $schema, $table, $column) = @_;
+ 		my $CATALOG = DBD::Pg::pg_use_catalog($dbh);
  
  		my @wh = ();
! 		my @flds = qw/catname n.nspname c.relname a.attname/;
  
  		for my $idx (0 .. $#attrs) {
  			next if ($flds[$idx] eq 'catname'); # Skip catalog
! 			if(defined $attrs[$idx] and length $attrs[$idx]) {
! 				# Insure that the value is enclosed in single quotes.
  				$attrs[$idx] =~ s/^'?(\w+)'?$/'$1'/;
  				if ($attrs[$idx] =~ m/[,%]/) {
  					# contains a meta character.
***************
*** 179,189 ****
  		}
  
  		my $wh = ""; # ();
! 		$wh = join( " and ", '', @wh ) if (@wh);
  		my $col_info_sql = qq{
! 			select
! 				  NULL::text    AS "TABLE_CAT"
! 				, u.usename     AS "TABLE_SCHEM"
  				, c.relname		AS "TABLE_NAME"
  				, a.attname		AS "COLUMN_NAME"
  				, t.typname		AS "DATA_TYPE"
--- 197,210 ----
  		}
  
  		my $wh = ""; # ();
! 		$wh = join( " AND ", '', @wh ) if (@wh);
! 		my $version = DBD::Pg::pg_server_version($dbh);
! 		my $showschema = $version < 7.3 ? "NULL::text" : "n.nspname";
! 		my $schemajoin = $version < 7.3 ? "" : "LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)";
  		my $col_info_sql = qq{
! 			SELECT
! 				  NULL::text	AS "TABLE_CAT"
! 				, $showschema	AS "TABLE_SCHEM"
  				, c.relname		AS "TABLE_NAME"
  				, a.attname		AS "COLUMN_NAME"
  				, t.typname		AS "DATA_TYPE"
***************
*** 199,237 ****
  				, NULL::text	AS "SQL_DATETIME_SUB"
  				, NULL::text	AS "CHAR_OCTET_LENGTH"
  				, a.attnum		AS "ORDINAL_POSITION"
! 				, a.attnotnull  AS "IS_NULLABLE"
  				, a.atttypmod	as atttypmod
  				, a.attnotnull	as attnotnull
  				, a.atthasdef	as atthasdef
  				, a.attnum		as attnum
! 			from 
! 				  pg_attribute	a
! 				, pg_class		c
! 				, pg_type		t
! 				, pg_user		u
! 			where
  					a.attrelid = c.oid
! 				and a.attnum  >= 0
! 				and t.oid      = a.atttypid
! 				and c.relkind  in ('r','v')
! 				and c.relowner = u.usesysid 
  				$wh
! 			order by u.usename, c.relname, a.attnum
  		};
  
! 				# and c.relname !~ '^pg_' # Removing the restriction on the
! 				# system tables.
! 		my $sth = $dbh->prepare( $col_info_sql ) or
! 			return undef;
!         $sth->execute();
  
!         return $sth;
  	}
  
  	sub primary_key_info {
          my $dbh = shift;
  		my ($catalog, $schema, $table) = @_;
  		my @attrs = @_;
  
  		# TABLE_CAT:, TABLE_SCHEM:, TABLE_NAME:, COLUMN_NAME:, KEY_SEQ:
  		# , PK_NAME:
--- 220,255 ----
  				, NULL::text	AS "SQL_DATETIME_SUB"
  				, NULL::text	AS "CHAR_OCTET_LENGTH"
  				, a.attnum		AS "ORDINAL_POSITION"
! 				, a.attnotnull	AS "IS_NULLABLE"
  				, a.atttypmod	as atttypmod
  				, a.attnotnull	as attnotnull
  				, a.atthasdef	as atthasdef
  				, a.attnum		as attnum
! 			FROM 
! 				  ${CATALOG}pg_attribute	a
! 				, ${CATALOG}pg_type		t
! 				, ${CATALOG}pg_class		c
! 				$schemajoin
! 			WHERE
  					a.attrelid = c.oid
! 				AND a.attnum  >= 0
! 				AND t.oid      = a.atttypid
! 				AND c.relkind  in ('r','v')
  				$wh
! 			ORDER BY 2, 3, 4
  		};
  
! 		my $sth = $dbh->prepare( $col_info_sql ) or return undef;
! 		$sth->execute();
  
! 		return $sth;
  	}
  
  	sub primary_key_info {
          my $dbh = shift;
  		my ($catalog, $schema, $table) = @_;
  		my @attrs = @_;
+         my $CATALOG = DBD::Pg::pg_use_catalog($dbh);
  
  		# TABLE_CAT:, TABLE_SCHEM:, TABLE_NAME:, COLUMN_NAME:, KEY_SEQ:
  		# , PK_NAME:
***************
*** 241,247 ****
  
  		for my $idx (0 .. $#attrs) {
  			next if ($flds[$idx] eq 'catname'); # Skip catalog
! 			if(defined $attrs[$idx]) {
  				if ($attrs[$idx] =~ m/[,%_?]/) {
  					# contains a meta character.
  					push( @wh, q{( } . join ( " OR "
--- 259,265 ----
  
  		for my $idx (0 .. $#attrs) {
  			next if ($flds[$idx] eq 'catname'); # Skip catalog
! 			if(defined $attrs[$idx] and length $attrs[$idx]) {
  				if ($attrs[$idx] =~ m/[,%_?]/) {
  					# contains a meta character.
  					push( @wh, q{( } . join ( " OR "
***************
*** 261,325 ****
  		}
  
  		my $wh = '';
! 		$wh = join( " and ", '', @wh ) if (@wh);
  
  		# Base primary key selection query borrowed from phpPgAdmin.
  		my $pri_key_sql = qq{
  			SELECT
  				NULL::text		AS "TABLE_CAT"
! 				, u.usename		AS "TABLE_SCHEM"
  				, bc.relname	AS "TABLE_NAME"
  				, a.attname		AS "COLUMN_NAME"
  				, a.attnum		AS "KEY_SEQ"
! 				, ic.relname	AS "PK_NAME"
  			FROM
! 				  pg_class bc
! 				, pg_class ic
! 				, pg_index i
! 				, pg_attribute a
! 				, pg_user	u
  			WHERE
  				i.indrelid = bc.oid
! 			and i.indexrelid = ic.oid
! 			and
  			(
  				i.indkey[0] = a.attnum
! 				or
  				i.indkey[1] = a.attnum
! 				or
  				i.indkey[2] = a.attnum
! 				or
  				i.indkey[3] = a.attnum
! 				or
  				i.indkey[4] = a.attnum
! 				or
  				i.indkey[5] = a.attnum
! 				or
  				i.indkey[6] = a.attnum
! 				or
  				i.indkey[7] = a.attnum
! 				or
  				i.indkey[8] = a.attnum
! 				or
  				i.indkey[9] = a.attnum
! 				or
  				i.indkey[10] = a.attnum
! 				or
  				i.indkey[11] = a.attnum
! 				or
  				i.indkey[12] = a.attnum
  			)
! 			and a.attrelid = bc.oid
! 			and i.indproc = '0'::oid
! 			and i.indisprimary = 't' 
! 			and bc.relowner    =  u.usesysid
  			$wh
! 			order by
! 				u.usename, bc.relname, a.attnum
  		};
  
!         my $sth = $dbh->prepare( $pri_key_sql ) or
! 			return undef;
          $sth->execute(@dat);
  
          return $sth;
--- 279,343 ----
  		}
  
  		my $wh = '';
! 		$wh = join( " AND ", '', @wh ) if (@wh);
  
  		# Base primary key selection query borrowed from phpPgAdmin.
+ 		my $version = DBD::Pg::pg_server_version($dbh);
+ 		my $showschema = $version < 7.3 ? "NULL::text" : "n.nspname";
+         my $schemajoin = $version < 7.3 ? "" : "LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = bc.relnamespace)";
  		my $pri_key_sql = qq{
  			SELECT
  				NULL::text		AS "TABLE_CAT"
! 				, $showschema	AS "TABLE_SCHEM"
  				, bc.relname	AS "TABLE_NAME"
  				, a.attname		AS "COLUMN_NAME"
  				, a.attnum		AS "KEY_SEQ"
! 				, ic.relname    AS "PK_NAME"
  			FROM
! 				${CATALOG}pg_index i
! 				, ${CATALOG}pg_attribute a
! 				, ${CATALOG}pg_class ic
! 				, ${CATALOG}pg_class bc
! 				$schemajoin
  			WHERE
  				i.indrelid = bc.oid
! 			AND i.indexrelid = ic.oid
! 			AND
  			(
  				i.indkey[0] = a.attnum
! 				OR
  				i.indkey[1] = a.attnum
! 				OR
  				i.indkey[2] = a.attnum
! 				OR
  				i.indkey[3] = a.attnum
! 				OR
  				i.indkey[4] = a.attnum
! 				OR
  				i.indkey[5] = a.attnum
! 				OR
  				i.indkey[6] = a.attnum
! 				OR
  				i.indkey[7] = a.attnum
! 				OR
  				i.indkey[8] = a.attnum
! 				OR
  				i.indkey[9] = a.attnum
! 				OR
  				i.indkey[10] = a.attnum
! 				OR
  				i.indkey[11] = a.attnum
! 				OR
  				i.indkey[12] = a.attnum
  			)
! 			AND a.attrelid = bc.oid
! 			AND i.indproc = '0'::oid
! 			AND i.indisprimary = 't' 
  			$wh
! 			ORDER BY 2, 3, 5
  		};
  
!         my $sth = $dbh->prepare( $pri_key_sql ) or return undef;
          $sth->execute(@dat);
  
          return $sth;
***************
*** 333,338 ****
--- 351,359 ----
  
  		my $tbl_sql = ();
  
+         my $version = DBD::Pg::pg_server_version($dbh);
+         my $CATALOG = DBD::Pg::pg_use_catalog($dbh);
+ 
  		if ( # Rules 19a
  			    (defined $catalog and $catalog eq '%')
  			and (defined $schema  and $schema  eq  '')
***************
*** 352,367 ****
  			and (defined $schema  and $schema  eq '%')
  			and (defined $table   and $table   eq  '')
  			) {
! 				$tbl_sql = q{
  					SELECT 
  					   NULL::text    AS "TABLE_CAT"
! 					 , u.usename     AS "TABLE_SCHEM"
  					 , NULL::text    AS "TABLE_NAME"
  					 , NULL::text    AS "TABLE_TYPE"
  					 , NULL::text    AS "REMARKS"
! 					from pg_user u
! 					order by u.usename
! 				};
  		}
  		elsif (# Rules 19c
  			    (defined $catalog and $catalog eq  '')
--- 373,395 ----
  			and (defined $schema  and $schema  eq '%')
  			and (defined $table   and $table   eq  '')
  			) {
! 				$tbl_sql = ($version < 7.3) ? q{
! 					SELECT 
! 					   NULL::text    AS "TABLE_CAT"
! 					 , NULL::text    AS "TABLE_SCHEM"
! 					 , NULL::text    AS "TABLE_NAME"
! 					 , NULL::text    AS "TABLE_TYPE"
! 					 , NULL::text    AS "REMARKS"
!                     } : q{
  					SELECT 
  					   NULL::text    AS "TABLE_CAT"
! 					 , n.nspname     AS "TABLE_SCHEM"
  					 , NULL::text    AS "TABLE_NAME"
  					 , NULL::text    AS "TABLE_TYPE"
  					 , NULL::text    AS "REMARKS"
! 					FROM pg_catalog.pg_namespace n
! 					ORDER BY 1
! 					};
  		}
  		elsif (# Rules 19c
  			    (defined $catalog and $catalog eq  '')
***************
*** 395,401 ****
  					   NULL::text    AS "TABLE_CAT"
  					 , NULL::text    AS "TABLE_SCHEM"
  					 , NULL::text    AS "TABLE_NAME"
! 					 , 'sequence'       AS "TABLE_TYPE"
  					 , 'sequence - S'    AS "REMARKS"
  					union
  					SELECT 
--- 423,429 ----
  					   NULL::text    AS "TABLE_CAT"
  					 , NULL::text    AS "TABLE_SCHEM"
  					 , NULL::text    AS "TABLE_NAME"
! 					 , 'sequence     AS "TABLE_TYPE"
  					 , 'sequence - S'    AS "REMARKS"
  					union
  					SELECT 
***************
*** 422,440 ****
  		}
  		else {
  				# Default SQL
  				$tbl_sql = qq{
  				SELECT NULL::text    AS "TABLE_CAT"
! 					 , u.usename     AS "TABLE_SCHEM"
  					 , c.relname     AS "TABLE_NAME"
  					 , CASE
  					 	 WHEN c.relkind = 'v' THEN 'VIEW'
! 					  	 ELSE 'TABLE'
  						END			 AS "TABLE_TYPE"
  					 , d.description AS "REMARKS"
! 				FROM pg_user		AS u
! 				   , pg_class		AS c LEFT OUTER JOIN 
! 					 pg_description	AS d 
! 						ON c.relfilenode = d.objoid and d.objsubid = 0
  				WHERE 
  					  ((c.relkind     =  'r'
  				  AND c.relhasrules =  FALSE) OR
--- 450,474 ----
  		}
  		else {
  				# Default SQL
+ 				my $showschema = $version < 7.3 ? "NULL::text" : "n.nspname";
+                 my $schemajoin = $version < 7.3 ? "" : "LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)";
+ 				my $schemacase = $version < 7.3 ? "CASE WHEN c.relname ~ '^pg_' THEN 'SYSTEM TABLE' ELSE 'TABLE' END" : 
+ 					"CASE WHEN n.nspname ~ '^pg_' THEN 'SYSTEM TABLE' ELSE 'TABLE' END";
  				$tbl_sql = qq{
  				SELECT NULL::text    AS "TABLE_CAT"
! 					 , $showschema   AS "TABLE_SCHEM"
  					 , c.relname     AS "TABLE_NAME"
  					 , CASE
  					 	 WHEN c.relkind = 'v' THEN 'VIEW'
! 					  	 ELSE $schemacase
  						END			 AS "TABLE_TYPE"
  					 , d.description AS "REMARKS"
! 				FROM ${CATALOG}pg_user		AS u
! 				   , ${CATALOG}pg_class		AS c
! 					 LEFT JOIN 
! 					 ${CATALOG}pg_description	AS d 
! 						ON (c.relfilenode = d.objoid AND d.objsubid = 0)
!                      $schemajoin
  				WHERE 
  					  ((c.relkind     =  'r'
  				  AND c.relhasrules =  FALSE) OR
***************
*** 442,461 ****
  				  AND c.relhasrules =  TRUE))
  				  AND c.relname     !~ '^xin[vx][0-9]+'
  				  AND c.relowner    =  u.usesysid
! 				ORDER BY 2, 3
  				};
  
- 				#   AND c.relname     !~ '^pg_' Removed the restrcition from
- 				#   viewing the system tables.
- 
  			# Did we receive any arguments?
  			if (@attrs) {
  				my @wh = ();
! 				my @flds = qw/catname u.usename c.relname c.relkind/;
  
  				for my $idx (0 .. $#attrs) {
  					next if ($flds[$idx] eq 'catname'); # Skip catalog
! 					if(defined $attrs[$idx]) {
  						# Change the "name" of the types to the real value.
  						if ($flds[$idx]  =~ m/relkind/) {
  							$attrs[$idx] =~ s/^\'?table\'?/'r'/i;
--- 476,492 ----
  				  AND c.relhasrules =  TRUE))
  				  AND c.relname     !~ '^xin[vx][0-9]+'
  				  AND c.relowner    =  u.usesysid
! 				ORDER BY 1, 2, 3
  				};
  
  			# Did we receive any arguments?
  			if (@attrs) {
  				my @wh = ();
! 				my @flds = qw/catname n.nspname c.relname c.relkind/;
  
  				for my $idx (0 .. $#attrs) {
  					next if ($flds[$idx] eq 'catname'); # Skip catalog
! 					if(defined $attrs[$idx] and length $attrs[$idx]) {
  						# Change the "name" of the types to the real value.
  						if ($flds[$idx]  =~ m/relkind/) {
  							$attrs[$idx] =~ s/^\'?table\'?/'r'/i;
***************
*** 465,471 ****
  							$attrs[$idx] =~ s/^\'?special\'?/'s'/i;
  							$attrs[$idx] =~ s/^\'?secondary\'?/'t'/i;
  						}
! # Insure that the value is enclosed in single quotes.
  						$attrs[$idx] =~ s/^'?(\w+)'?$/'$1'/;
  						if ($attrs[$idx] =~ m/[,%]/) {
  							# contains a meta character.
--- 496,502 ----
  							$attrs[$idx] =~ s/^\'?special\'?/'s'/i;
  							$attrs[$idx] =~ s/^\'?secondary\'?/'t'/i;
  						}
! 						# Insure that the value is enclosed in single quotes.
  						$attrs[$idx] =~ s/^'?(\w+)'?$/'$1'/;
  						if ($attrs[$idx] =~ m/[,%]/) {
  							# contains a meta character.
***************
*** 485,498 ****
  
  				my $wh = ();
  				if (@wh) {
! 					$wh = join( " and ",'', @wh );
! 
  					$tbl_sql = qq{
  					SELECT NULL::text    AS "TABLE_CAT"
! 						 , u.usename     AS "TABLE_SCHEM"
  						 , c.relname     AS "TABLE_NAME"
  						 , CASE
! 							 WHEN c.relkind = 'r' THEN 'TABLE'
  							 WHEN c.relkind = 'v' THEN 'VIEW'
  							 WHEN c.relkind = 'i' THEN 'INDEX'
  							 WHEN c.relkind = 'S' THEN 'SEQUENCE'
--- 516,529 ----
  
  				my $wh = ();
  				if (@wh) {
! 					$wh = join( " AND ",'', @wh );
  					$tbl_sql = qq{
  					SELECT NULL::text    AS "TABLE_CAT"
! 						 , $showschema   AS "TABLE_SCHEM"
  						 , c.relname     AS "TABLE_NAME"
  						 , CASE
! 							 WHEN c.relkind = 'r' THEN 
! 								CASE WHEN n.nspname ~ '^pg_' THEN 'SYSTEM TABLE' ELSE 'TABLE' END
  							 WHEN c.relkind = 'v' THEN 'VIEW'
  							 WHEN c.relkind = 'i' THEN 'INDEX'
  							 WHEN c.relkind = 'S' THEN 'SEQUENCE'
***************
*** 501,523 ****
  							 ELSE 'UNKNOWN'
  							END			 AS "TABLE_TYPE"
  						 , d.description AS "REMARKS"
! 					FROM pg_user		AS u
! 					   , pg_class		AS c LEFT OUTER JOIN 
! 						 pg_description	AS d 
! 							ON c.relfilenode = d.objoid and d.objsubid = 0
  					WHERE 
  					  	  c.relname     !~ '^xin[vx][0-9]+'
- 					  AND c.relowner    =  u.usesysid
  					  $wh
  					ORDER BY 2, 3
  					};
- # c.relname     !~ '^pg_'
  				}
  			}
  		}
  
!         my $sth = $dbh->prepare( $tbl_sql ) or
! 			return undef;
          $sth->execute();
  
          return $sth;
--- 532,552 ----
  							 ELSE 'UNKNOWN'
  							END			 AS "TABLE_TYPE"
  						 , d.description AS "REMARKS"
! 					FROM ${CATALOG}pg_class		AS c
! 						LEFT JOIN 
! 						 ${CATALOG}pg_description	AS d 
! 							ON (c.relfilenode = d.objoid AND d.objsubid = 0)
! 						$schemajoin
  					WHERE 
  					  	  c.relname     !~ '^xin[vx][0-9]+'
  					  $wh
  					ORDER BY 2, 3
  					};
  				}
  			}
  		}
  
!         my $sth = $dbh->prepare( $tbl_sql ) or return undef;
          $sth->execute();
  
          return $sth;
***************
*** 526,545 ****
  
      sub tables {
          my($dbh) = @_;
  
!         my $sth = $dbh->prepare("
!             select relname  AS \"TABLE_NAME\"
!             from   pg_class 
!             where  relkind = 'r'
!             and    relname !~ '^pg_'
!             and    relname !~ '^xin[vx][0-9]+'
!             order by 1 
!         ") or return undef;
!             # and    relname !~ '^pg_' 
          $sth->execute or return undef;
          my (@tables, @relname);
          while (@relname = $sth->fetchrow_array) {
!             push @tables, $relname[0];
          }
          $sth->finish;
  
--- 555,581 ----
  
      sub tables {
          my($dbh) = @_;
+         my $version = DBD::Pg::pg_server_version($dbh);
  
! 		my $SQL = ($version < 7.3) ? 
!             "SELECT relname  AS \"TABLE_NAME\"
!             FROM   pg_class 
!             WHERE  relkind = 'r'
!             AND    relname !~ '^pg_'
!             AND    relname !~ '^xin[vx][0-9]+'
!             ORDER BY 1" : 
!             "SELECT n.nspname AS \"SCHEMA_NAME\", c.relname  AS \"TABLE_NAME\"
!             FROM   pg_catalog.pg_class c
!             LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
!             WHERE  c.relkind = 'r'
!             AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
!             AND pg_catalog.pg_table_is_visible(c.oid)
!             ORDER BY 1,2";
!         my $sth = $dbh->prepare($SQL) or return undef;
          $sth->execute or return undef;
          my (@tables, @relname);
          while (@relname = $sth->fetchrow_array) {
!             push @tables, $version < 7.3 ? $relname[0] : "$relname[0].$relname[1]";
          }
          $sth->finish;
  
***************
*** 549,560 ****
  
      sub table_attributes {
          my ($dbh, $table) = @_;
          my $result = [];    
          my $attrs  = $dbh->selectall_arrayref(
               "select a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
!               from pg_attribute a,
!                    pg_class     c,
!                    pg_type      t
                where c.relname  = ?
                  and a.attrelid = c.oid
                  and a.attnum  >= 0
--- 585,597 ----
  
      sub table_attributes {
          my ($dbh, $table) = @_;
+         my $CATALOG = DBD::Pg::pg_use_catalog($dbh);
          my $result = [];    
          my $attrs  = $dbh->selectall_arrayref(
               "select a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
!               from ${CATALOG}pg_attribute a,
!                    ${CATALOG}pg_class     c,
!                    ${CATALOG}pg_type      t
                where c.relname  = ?
                  and a.attrelid = c.oid
                  and a.attnum  >= 0
***************
*** 566,572 ****
  
  	# Select the array value for tables primary key.
  	my $pk_key_sql = qq{SELECT pg_index.indkey
!                             FROM   pg_class, pg_index
                              WHERE
                                     pg_class.oid          = pg_index.indrelid
                              AND    pg_class.relname      = '$table'
--- 603,609 ----
  
  	# Select the array value for tables primary key.
  	my $pk_key_sql = qq{SELECT pg_index.indkey
!                             FROM   ${CATALOG}pg_class, ${CATALOG}pg_index
                              WHERE
                                     pg_class.oid          = pg_index.indrelid
                              AND    pg_class.relname      = '$table'
***************
*** 585,597 ****
  		
          # Get the primary key
          my $pri_key = $dbh->selectcol_arrayref("SELECT pg_attribute.attname
!                                                FROM   pg_class, pg_attribute, pg_index
                                                 WHERE  pg_class.oid          = pg_attribute.attrelid 
                                                 AND    pg_class.oid          = pg_index.indrelid 
  					       $pk_bt
                                                 AND    pg_index.indisprimary = 't'
                                                 AND    pg_class.relname      = ?
! 					       order by pg_attribute.attnum
  					       ", undef, $table );
          $pri_key = [] unless $pri_key;
  
--- 622,634 ----
  		
          # Get the primary key
          my $pri_key = $dbh->selectcol_arrayref("SELECT pg_attribute.attname
!                                                FROM   ${CATALOG}pg_class, ${CATALOG}pg_attribute, ${CATALOG}pg_index
                                                 WHERE  pg_class.oid          = pg_attribute.attrelid 
                                                 AND    pg_class.oid          = pg_index.indrelid 
  					       $pk_bt
                                                 AND    pg_index.indisprimary = 't'
                                                 AND    pg_class.relname      = ?
! 					       ORDER BY pg_attribute.attnum
  					       ", undef, $table );
          $pri_key = [] unless $pri_key;
  
***************
*** 613,619 ****
              };
  
              # Get the default value, if any
!             my ($default) = $dbh->selectrow_array("SELECT adsrc FROM pg_attrdef WHERE  adnum = $attnum") if -1 == $attnum;
              $default = '' unless $default;
  
              # Test for any constraints
--- 650,656 ----
              };
  
              # Get the default value, if any
!             my ($default) = $dbh->selectrow_array("SELECT adsrc FROM ${CATALOG}pg_attrdef WHERE  adnum = $attnum") if -1 == $attnum;
              $default = '' unless $default;
  
              # Test for any constraints
***************
*** 621,630 ****
              # by pg_constraint. To maintain compatibility, check 
              # version number and execute appropriate query.
  	
!             my ($version) = $dbh->selectrow_array("SELECT version()");
!             $version =~ /^PostgreSQL (\d)\.(\d)/;
              
!             my $con_query = $1.$2 < 73
               ? "SELECT rcsrc FROM pg_relcheck WHERE rcname = '${table}_$col_name'"
               : "SELECT consrc FROM pg_catalog.pg_constraint WHERE contype = 'c' AND conname = '${table}_$col_name'";
              my ($constraint) = $dbh->selectrow_array($con_query);
--- 658,666 ----
              # by pg_constraint. To maintain compatibility, check 
              # version number and execute appropriate query.
  	
!             my $version = pg_server_version( $dbh );
              
!             my $con_query = $version < 7.3
               ? "SELECT rcsrc FROM pg_relcheck WHERE rcname = '${table}_$col_name'"
               : "SELECT consrc FROM pg_catalog.pg_constraint WHERE contype = 'c' AND conname = '${table}_$col_name'";
              my ($constraint) = $dbh->selectrow_array($con_query);
***************
*** 771,785 ****
      sub quote {
          my ($dbh, $str, $data_type) = @_;
          return "NULL" unless defined $str;
! 	return $str if $data_type && $no_escape{$data_type};
  
          $dbh->DBI::set_err(1, "Use of SQL_BINARY invalid in quote()")
            if $data_type && $data_type == DBI::SQL_BINARY;
  
! 	$str =~ s/(['\\\0])/$esc{$1}/g;
! 	return "'$str'";
      }
! }
  
  {   package DBD::Pg::st; # ====== STATEMENT ======
  
--- 807,822 ----
      sub quote {
          my ($dbh, $str, $data_type) = @_;
          return "NULL" unless defined $str;
! 		return $str if $data_type && $no_escape{$data_type};
  
          $dbh->DBI::set_err(1, "Use of SQL_BINARY invalid in quote()")
            if $data_type && $data_type == DBI::SQL_BINARY;
  
! 		$str =~ s/(['\\\0])/$esc{$1}/g;
! 		return "'$str'";
      }
! 
! }    # end of package DBD::Pg::db
  
  {   package DBD::Pg::st; # ====== STATEMENT ======
  
