block transactions in stored procedures

From: Ghiz x <mrghiz(at)yahoo(dot)it>
To: pgsql-novice(at)postgresql(dot)org
Subject: block transactions in stored procedures
Date: 2006-01-16 13:04:44
Message-ID: 20060116130444.48434.qmail@web26403.mail.ukl.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I'd like to write a plperl stored procedure that inserts data collected from a web form into multiple tables related by means of foreign keys for integral reference's sake.

Each table uses an independent sequence as primary key.

For instance the db consists of tableA and tableB; tableA's primary key is referenced as a foreign key
by one field of tableB.
In order to insert the tableA_id of the latest inserted row into the foreign key field referencing to it in tableB I'd retrieve tableA_id from tableA_id_seq before inserting the new row in both tables as follows (ala plperl):

$sql='select nextval(\'indirizzo_indirizzo_id_seq\');';

$rv = spi_exec_query($sql);

$row = $rv->{rows}[0];

$tableA_id = $row->{nextval};

$sql=insert into tableA($tableA_id,...);

$rv = spi_exec_query($sql);

$sql=insert into tableB(DEFAULT,..,$tableA_id,...);

$rv = spi_exec_query($sql);

Reading through SPI (Server Programming Api) documentation, it seems SPI_execute() runs a single sql statement per call an I myself verified that
spi_exec_query('BEGIN WORK;SELECT nextval(\'indirizzo_indirizzo_id_seq\');COMMIT WORK;') doesn't work.

Apart from my specific problem, my questioning is on how does one enforce a BEGIN WORK;
......

COMMIT WORK;

block transaction within a plperl stored procedure


Any hint or reference to tutorials about this issue would be really appreciated.

Ghiz


---------------------------------
Yahoo! Mail: gratis 1GB per i messaggi, antispam, antivirus, POP3

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Andreas Kretschmer 2006-01-16 16:46:08 Re: Help with query
Previous Message Christian Hofmann 2006-01-16 12:56:03 Re: Help with query