 
 foreach my $sub (keys %$shared) {
     my $ref = ref($_SHARED{$sub});
-    elog NOTICE, $ref if $_SHARED{debug};
+    # elog NOTICE, $ref if $_SHARED{debug};
     if ($ref eq 'CODE') {
-        elog NOTICE, "$sub already set." if $_SHARED{debug};
+        # Do nothing.
+        # elog NOTICE, "$sub already set." if $_SHARED{debug};
     }
     else {
         elog NOTICE, "Setting $sub in \%_SHARED hash." if $_SHARED{debug};

my $data_source_id = shift;
elog ERROR, "In shadow_trigger_function, data_source_id must be an integer"
    unless ($data_source_id =~ /^\d+$/);
my $query = "SELECT cache_connection( $data_source_id )";
elog NOTICE, "In shadow_trigger_function, calling\n    $query" if $_SHARED{debug};
elog NOTICE, "In shadow_trigger_function, the trigger payload is\n". Dump(\$_TD) if $_SHARED{debug};
my $rv = spi_exec_query($query);

my $table = $_TD->{relname};

########################################################################
#                                                                      #
# We are only INSERTing into the shadow table, so to distinguish OLD.* #
# from NEW.*, we do the following based on the prefix of the column    #
# name.                                                                #
#                                                                      #
########################################################################
my ($old, $new, $key, $col);
foreach $key (grep {/^.?old_/} keys %{ $_TD->{'new'} }) {
    ($col = $key) =~ s/old_//;
    if (defined $_TD->{new}{$key}) {
        $old->{$col} = $_SHARED{dbh}{ $data_source_id }->quote($_TD->{'new'}{$key});
    }
    else {
        $old->{$col} = 'NULL';
    }
}

foreach my $key (grep {/^.?new_/} keys %{ $_TD->{'new'} }) {
    ($col = $key) =~ s/new_//;
    if (defined $_TD->{new}{$key}) {
        $new->{$col} = $_SHARED{dbh}{ $data_source_id }->quote($_TD->{'new'}{$key});
    }
    else {
        $new->{$col} = 'NULL';
    }
}

elog NOTICE, "old is:\n". Dump(\$old)."\n\nnew is:\n". Dump(\$new);

my $iud = {
    I => \&insert,
    U => \&update,
    D => \&delete,
};

elog NOTICE, "In trigger on $table, action is $_TD->{'new'}{iud_action}" if $_SHARED{debug};
if ($iud->{ $_TD->{'new'}{iud_action} }) {
    $table =~ s/.*\.//;
    $table =~ s/_shadow$//;
    $iud->{ $_TD->{'new'}{iud_action} }->();
}
else {
    elog ERROR, "Trigger event was $_TD->{'new'}{iud_action}<, but should have been one of I, U or D!"
}

return 'SKIP';

sub insert {
    my $sql = <<SQL;
INSERT INTO $table (
    @{[join(",\n    ", sort keys %$new) ]}
)
VALUES (
    @{[join(
        ",\n    ",
        map {
            $new->{$_}
        }
        sort keys %$new
    ) ]}
)
SQL
    elog NOTICE, "SQL is\n$sql" if $_SHARED{debug};
    my $sth = $_SHARED{dbh}{ $data_source_id }->prepare($sql);
    $sth->execute();
    $sth->finish();
}

sub update {
    my $sql = <<SQL;
UPDATE $table
SET
    @{[ join(",\n    ", map { "$_ = $new->{$_}" } sort keys %$new) ]}
WHERE
    @{[
        join(
            "\nAND ",
            map {
                ($old->{$_} eq 'NULL') ?
                "$_ IS NULL"           :
                "$_ = " . $old->{$_}
            } sort keys %$old
        )
    ]}
SQL
    elog NOTICE, "SQL is\n$sql" if $_SHARED{debug};
    my $sth = $_SHARED{dbh}{ $data_source_id }->prepare($sql);
    $sth->execute();
    $sth->finish();
}

sub delete {
    my $sql = <<SQL;
DELETE FROM $table
WHERE
    @{[
        join(
            "\nAND ",
            map {
                ($old->{$_} eq 'NULL') ?
                "$_ IS NULL"           :
                "$_ = $old->{$_}"
            } sort keys %$old
        )
    ]}
SQL
    elog NOTICE, "SQL is\n$sql" if $_SHARED{debug};
    my $sth = $_SHARED{dbh}{ $data_source_id }->prepare($sql);
    $sth->execute();
    $sth->finish();
}

$$;

    elog NOTICE, "Entering get_connection_info" if $_SHARED{debug};
    elog NOTICE, 'ref($args) is '.ref($args)."\n".Dump($args) if $_SHARED{debug};
    unless (defined $args->{data_source_id}) {
        elog ERROR, "In get_connection_info, must provide a data_source_id";
    }
    unless ($args->{data_source_id} =~ /^\d+$/) {
        elog ERROR, "In get_connection_info, must provide an integer data_source_id";
