473,800 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

COMMIT within function?

Suppose I have vacuum_values() function, which removes all
"no longer referenced" by parent column. Kind of function
to be run from time to time to clean table from crud.
It looks like this:
CREATE FUNCTION vacuum_values() RETURNS void AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT value_id FROM values NATURAL LEFT JOIN
other_tab WHERE other_tab.value _id IS NULL FOR UPDATE OF values LOOP
DELETE FROM values WHERE value_id = r.value_id;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

Here, as the query runs against two table values (2 mln. rows) and
ther_tab (20 mln. rows) it is relatively slow... However there is
a chance that while this query goes, and goes, some rows will become
referenced once more... and the DELETE will fail because of FOREIGN
KEY, and the whole function will ROLLBACK... Is there a way to force
"ignore errors" or something? As far as I checked, I can catch errors,
but I don't really can stop the ROLLBACK. There are SAVEPOINTs but
I guess they are useful for explicit ROLLBACK TO SAVEPOINT...

Of course I can move all this logic outside of backend, and make
the backend just 'do' the DELETEs, ignoring errors... But still,
it should be doable in the procedural languages aswell.....

Regards,
Dawid

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
4 10255
Suppose I have vacuum_values() function, which removes all
"no longer referenced" by parent column. Kind of function
to be run from time to time to clean table from crud.
It looks like this:
I suppose you have a good reason to not use a foreign key with "ON DELETE
CASCADE" ?
FOR r IN SELECT value_id FROM values NATURAL LEFT JOIN
other_tab WHERE other_tab.value _id IS NULL FOR UPDATE OF values LOOP
DELETE FROM values WHERE value_id = r.value_id;
END LOOP;
RETURN;


I don't remember the exact syntax (look in the DELETE docs) but you can
certainly put a left join inside a delete and do it all at once with only
one query, and it'll be faster to boot.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
On Mon, 22 Nov 2004 00:16:07 +0100, Pierre-Frédéric Caillaud
<li***@boutique numerique.com> wrote:
Suppose I have vacuum_values() function, which removes all
"no longer referenced" by parent column. Kind of function

I suppose you have a good reason to not use a foreign key with "ON DELETE
CASCADE" ?


Well, the issue here is saving space and speed with
lots of repeatable data. Like e-mail addresses, most
of them are frequently reused, so instead of a table

CREATE TABLE messages (author text, ...);

I create two:
CREATE TABLE authors (author_id serial PRIMARY KEY, author text UNIQUE
NOT NULL);
CREATE TABLE messages (author_id integer REFERENCES authors, ...);

....and a matching view, and a function/rule which "invisibly"
changes author to author_id whenever data is added (with
authors table being updated when necessary).

Now, after some time I remove old messages, and some of authors become
"unreferenc ed" (think: From-s of spam messages). It would be nice to vacuum
them out. The problem is when one of those authors "shows up" after
long absence between our SELECT and actual DELETE. For a busy table
(this happen to be one) it is quite possible. :)

Ah, and ON DELETE CASCADE would mean I would loose perfectly
good messages. Having LOCK on the table is also not-so-good
an idea (think: authors with 2mln rows, messags with 20mln rows).
FOR r IN SELECT value_id FROM values NATURAL LEFT JOIN
other_tab WHERE other_tab.value _id IS NULL FOR UPDATE OF values LOOP
DELETE FROM values WHERE value_id = r.value_id;
END LOOP;
RETURN;


I don't remember the exact syntax (look in the DELETE docs) but you can
certainly put a left join inside a delete and do it all at once with only
one query, and it'll be faster to boot.


Well, DELETE FROM ... WHERE ... is nice, but it will be explicitly
"all-or-nothing",
whereas with FUNCTION I have a ghost of hope that it may not be atomic. :)
....and I don't think you can do OUTER JOIN without subselect using DELETE FROM
WHERE.

Regards,
Dawid

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3
On Sun, Nov 21, 2004 at 07:29:26PM +0100, Dawid Kuroczko wrote:
Of course I can move all this logic outside of backend, and make
the backend just 'do' the DELETEs, ignoring errors... But still,
it should be doable in the procedural languages aswell.....


In PostgreSQL 8.0, PL/pgSQL functions can trap errors without rolling
back the entire transaction:

http://developer.postgresql.org/docs...ERROR-TRAPPING

BEGIN
DELETE FROM values WHERE value_id = r.value_id;
EXCEPTION
WHEN foreign_key_vio lation THEN
NULL;
END;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
On Sun, 21 Nov 2004 20:10:03 -0700, Michael Fuhr <mi**@fuhr.or g> wrote:
http://developer.postgresql.org/docs...ERROR-TRAPPING

BEGIN
DELETE FROM values WHERE value_id = r.value_id;
EXCEPTION
WHEN foreign_key_vio lation THEN
NULL;
END;


Ahh, exactly what I was looking for. :) The thing I didn't notice
was that, while exception causes rollback to "BEGIN", it does
not mean to the beginning of the function. In other words
I didn't nest BEGIN...END blocks and all I got from using
exceptions was that they did not show any errors. :)

Thank you! I am now enlightened. This works perfect, exactly
as I hoped it would. :)

Regards,
dawid

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10059
by: anders_tung | last post by:
Hi, I have a procedure which will call 3 functions. First function will update a record. Second function will delete a record. Third function will insert a record. Each function will return a zero if successful, otherwise return 1. My questions are
11
12723
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
3
6644
by: Alberto | last post by:
I've a complex stored procedure, that makes a lot of insert, update, delete and so on. I would like to make some commits durint this sp, but of course they are not "real" commit because who call the sp could decide for a rollback. But I know that this commit has to be real. In fact, the transaction log grows really too much during the execution.
1
4169
by: ezra epstein | last post by:
I found a post about something similar in an older release: http://archives.postgresql.org/pgsql-bugs/2002-08/msg00151.php Here's the issue. Inside a function I'm calling CREATE LOCAL TEMPORARY TABLE ds_copy_item_meta ( LIKE merchandise.item_meta EXCLUDING DEFAULTS ) on COMMIT DROP; Of course it might be that the ON COMMIT DROP is redundant, I don't know enough about temp tables. Here's the problem, however, the first call of the...
3
8729
by: Mark | last post by:
If a java applicaiton using the type 4 driver calls a DB2 stored procedure, does the stored procedure need to do its own commit when updates are completed? If the stored procedure does a commit or rollback, does that affect the UOW for any SQL that was directly issued by the java program before calling the stored procedure?
3
5902
by: Giganews | last post by:
I presently use an SQL SP with cursors to cycle through records to update, insert etc. Within this I maintain a variable for a commit count, as in before I FETCH the next record I increment v_commit_cnt = v_commit_cnt + 1. I use an IF v_commit_cnt > say 1000 then COMMIT and set v_commit_cnt = 0 and so on. If we can now use MERGE to do similar processing as above , how can you now force frequent commits when dealing with a very large...
1
2049
by: Mark Day | last post by:
Hi all, I am running some sql from Access 2000 to an existing Oracle 8i database using the "INSERT INTO 'connection string for Oracle' etc etc. then the docmd.runSQL which works fine. However I then have to open the Oracle SQL Plus program and type 'commit' to finally commit the changes. I would like to automate this last step from within Access. ie commit the Oracle changes from within Access VB. Can this be achieved? If so can anyone be...
1
4412
by: Horace | last post by:
Hello I am writing an ASP.NET / SQL-Server application and wanting to know when to connect/begin-transaction and commit/disconnect. I have a common module which I call at the beginning of Page_Load which, when instantiated performs the connect/begin-transaction. The page then does what it has to do (eg when a Save button is clicked it will do a read/write to the database within the btnSave_Clicked event). At the end of the 'event'...
1
1501
by: Scott Cupstid | last post by:
We are working on a VB.NET application using SQLClient command objects to post data to the underlying SQL Server 2000 database. The application is deployed in a multi-user environment with no more than 10 user accessing the database at any given time. The host server is supporting approximately 4 other active databases. In general, there are approximately 60 active connections on the server. The incident we are trying to resolve has...
0
3033
by: evil tabby cat | last post by:
This is a python script which is fired off from a batchfile pre- commit.bat on Windows2000 server. Everything I've read says that it should work & each part does work when tested individually. But when it runs within subversion this statement always ends with this log_msg being an empty string. log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.