Re: Trigger (Transaction related)

Lists: pgsql-general
From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Trigger (Transaction related)
Date: 2006-09-01 06:51:59
Message-ID: d86a77ef0608312351h175bede8v2d537380f096c10d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,
I've written a trigger after insert on a table (parser) and the trigger
calls a function that dynamically loads a shared object written in C.

This shared object intends to use the newly inserted row in the table on
which trigger is written and insert values in another table, something like
this,

sprintf(buffer, "INSERT INTO headers (id, header_content) VALUES ( %d,
SELECT contents FROM parser WHERE id = %d ), id, id);
SPI_exec(buffer,0).

Above line of code is not able to read the values of newly inserted code i.e
SELECT contents FROM parser WHERE id = %d
and throws an error.

Is it because its still the same transaction going on and the insert fired
on parser talbe won't be reflected in the select query in the shared object
which is a part of the same transaction???

Regards,

~Harpreet


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Harpreet Dhaliwal <harpreet(dot)dhaliwal01(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 07:00:17
Message-ID: 20060901070017.GD12644@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Sep 01, 2006 at 02:51:59AM -0400, Harpreet Dhaliwal wrote:
> Hi,
> I've written a trigger after insert on a table (parser) and the trigger
> calls a function that dynamically loads a shared object written in C.
>
> This shared object intends to use the newly inserted row in the table on
> which trigger is written and insert values in another table, something like
> this,

<snip>

> Above line of code is not able to read the values of newly inserted code i.e
> SELECT contents FROM parser WHERE id = %d
> and throws an error.

The new values are in the "NEW" pseudo-tuple. See the trigger
documentation on how to access it. In the pl/sql and pl/pgsql languages
you just say "NEW.<fieldname>". Not sure about C though.

> Is it because its still the same transaction going on and the insert fired
> on parser talbe won't be reflected in the select query in the shared object
> which is a part of the same transaction???

I don't think its visible in the table yet for a BEFORE INSERT trigger.
AFTER INSERT maybe, but in either case, the NEW tuple has the actual
values.

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.


From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 07:19:03
Message-ID: d86a77ef0609010019o193bfd95p795e9ba04c9bc3d7@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

If that is the case then why does it throw error in one on the insert
queries in the shared object written in SPI without inserting the row on the
table on which record is inserted.

Follwing query in the shared object throws an error.

INSERT INTO headers (id, header_content) VALUES (1, SELECT raw_email FROM
parser WHERE id = 1)

Error is
ERROR: syntax error at or near "SELECT" at character 53

Regardless the error in the shared object, why doesn't it insert the row in
the table on which AFTER INSERT trigger is written?

Thanks,

~Harpreet
On 9/1/06, Martijn van Oosterhout <kleptog(at)svana(dot)org> wrote:
>
> On Fri, Sep 01, 2006 at 02:51:59AM -0400, Harpreet Dhaliwal wrote:
> > Hi,
> > I've written a trigger after insert on a table (parser) and the trigger
> > calls a function that dynamically loads a shared object written in C.
> >
> > This shared object intends to use the newly inserted row in the table on
> > which trigger is written and insert values in another table, something
> like
> > this,
>
> <snip>
>
> > Above line of code is not able to read the values of newly inserted code
> i.e
> > SELECT contents FROM parser WHERE id = %d
> > and throws an error.
>
> The new values are in the "NEW" pseudo-tuple. See the trigger
> documentation on how to access it. In the pl/sql and pl/pgsql languages
> you just say "NEW.<fieldname>". Not sure about C though.
>
> > Is it because its still the same transaction going on and the insert
> fired
> > on parser talbe won't be reflected in the select query in the shared
> object
> > which is a part of the same transaction???
>
> I don't think its visible in the table yet for a BEFORE INSERT trigger.
> AFTER INSERT maybe, but in either case, the NEW tuple has the actual
> values.
>
> Hope this helps,
> --
> Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> > From each according to his ability. To each according to his ability to
> litigate.
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQFE99qBIB7bNG8LQkwRAq7KAJ4p+rTE1TYgKjpydTlb30YY0yDdFgCghDiP
> Yj3c+7ayJihJ9LKYwrFYoX0=
> =4W51
> -----END PGP SIGNATURE-----
>
>
>


From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 07:33:55
Message-ID: d86a77ef0609010033v505fee96l75d3a6718e9cd846@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thats OK, but my question is why did the original insert not take place.
Syntax error was in the select query that is fired as a result of the
trigger

Thanks,
~Harpreet

On 9/1/06, Roman Neuhauser <neuhauser(at)sigpipe(dot)cz> wrote:
>
> # harpreet(dot)dhaliwal01(at)gmail(dot)com / 2006-09-01 03:19:03 -0400:
> > If that is the case then why does it throw error in one on the insert
> > queries in the shared object written in SPI without inserting the row on
> the
> > table on which record is inserted.
> >
> > Follwing query in the shared object throws an error.
> >
> >
> > INSERT INTO headers (id, header_content) VALUES (1, SELECT raw_email
> FROM
> > parser WHERE id = 1)
> >
> > Error is
> > ERROR: syntax error at or near "SELECT" at character 53
>
> Because there's a syntax error.
>
> test=# INSERT INTO headers (id, header_content) VALUES (1, SELECT
> raw_email FROM parser WHERE id = 1);
> ERROR: syntax error at or near "SELECT" at character 53
> LINE 1: ...SERT INTO headers (id, header_content) VALUES (1, SELECT
> raw...
> ^
> test=# INSERT INTO headers (id, header_content) VALUES (1, (SELECT
> raw_email FROM parser WHERE id = 1));
> ERROR: relation "headers" does not exist
> test=#
>
> > Regardless the error in the shared object, why doesn't it insert the
> > row in the table on which AFTER INSERT trigger is written?
>
> Perhaps you should verify the syntax of your queries in a less
> demanding environment, like psql, first.
>
> --
> How many Vietnam vets does it take to screw in a light bulb?
> You don't know, man. You don't KNOW.
> Cause you weren't THERE. http://bash.org/?255991
>


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Harpreet Dhaliwal <harpreet(dot)dhaliwal01(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 07:55:07
Message-ID: 20060901075507.GH12644@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Sep 01, 2006 at 03:33:55AM -0400, Harpreet Dhaliwal wrote:
> Thats OK, but my question is why did the original insert not take place.
> Syntax error was in the select query that is fired as a result of the
> trigger

The trigger is part of the transaction. If the trigger fails, so does
the whole transaction.

It would be kinda silly if a transaction was allowed to succeed even
though not all the triggers were run.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.


From: Roman Neuhauser <neuhauser(at)sigpipe(dot)cz>
To: Harpreet Dhaliwal <harpreet(dot)dhaliwal01(at)gmail(dot)com>
Cc: Martijn van Oosterhout <kleptog(at)svana(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 09:28:56
Message-ID: 20060901092856.GA99468@dagan.sigpipe.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

# harpreet(dot)dhaliwal01(at)gmail(dot)com / 2006-09-01 03:19:03 -0400:
> If that is the case then why does it throw error in one on the insert
> queries in the shared object written in SPI without inserting the row on the
> table on which record is inserted.
>
> Follwing query in the shared object throws an error.
>
>
> INSERT INTO headers (id, header_content) VALUES (1, SELECT raw_email FROM
> parser WHERE id = 1)
>
> Error is
> ERROR: syntax error at or near "SELECT" at character 53

Because there's a syntax error.

test=# INSERT INTO headers (id, header_content) VALUES (1, SELECT raw_email FROM parser WHERE id = 1);
ERROR: syntax error at or near "SELECT" at character 53
LINE 1: ...SERT INTO headers (id, header_content) VALUES (1, SELECT raw...
^
test=# INSERT INTO headers (id, header_content) VALUES (1, (SELECT raw_email FROM parser WHERE id = 1));
ERROR: relation "headers" does not exist
test=#

> Regardless the error in the shared object, why doesn't it insert the
> row in the table on which AFTER INSERT trigger is written?

Perhaps you should verify the syntax of your queries in a less
demanding environment, like psql, first.

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 14:19:46
Message-ID: 3835.1157120386@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

"Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com> writes:
> sprintf(buffer, "INSERT INTO headers (id, header_content) VALUES ( %d,
> SELECT contents FROM parser WHERE id = %d ), id, id);
> SPI_exec(buffer,0).

You're short some parentheses --- try executing a comparable query
manually.

regards, tom lane


From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger (Transaction related)
Date: 2006-09-01 15:59:57
Message-ID: d86a77ef0609010859m3c214b00xe2ecffb61038a5aa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Exactly Tom,
I was missing a few paranthesis like a nut.
Thanks for pointing out.

Thanks and regards

~Harpreet

On 9/1/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com> writes:
> > sprintf(buffer, "INSERT INTO headers (id, header_content) VALUES ( %d,
> > SELECT contents FROM parser WHERE id = %d ), id, id);
> > SPI_exec(buffer,0).
>
> You're short some parentheses --- try executing a comparable query
> manually.
>
> regards, tom lane
>