Re: [SQL] Undo an update

2006-08-12 Thread Markus Schaber
Hi, Judith,

Judith wrote:

>Is there a way to undo an update???

I'm sorry, but his is one of those short questions that only have long,
complex answers.

If this answer is to long / complicated for you, begin from the top, and
whenever it get's worse, ask yourself how urgent your problem is, how
much you're willing to invest to recover your data, and that the simple
alternative answer is "No.".

Depending on what happened since you fired the update, and how you fired
it, you may have several possibilities to recover, some of which might
be more expensive than the worth of your data.

1) If you created an savepoint / checkpoint / subtransaction just prior
to the update, and the transaction is still open, roll back to the
savepoint.

2) If you did not create a savepoint, but the transaction is still open,
you'll have to rollback the whole transaction.

3) If you did start your update in a transaction, and then closed your
frontend without committing (IOW, having the offending transaction still
open), the transaction will usually be rolled back by the server when it
notices that the connection vanished.

4) If you did not start your update in a transaction (e. G. by starting
it in auto-commit mode), but it is still running in the backend (a very
long running update), you can abort the backend process, which will roll
back the transaction, but is very likely to disturb or abort other
connections running concurrently.

  (Explanation: If you don't know what transactions are[1], and you just
   fired your query via psql or another frontend (most which use the
   auto-commit mode per default), or your frontend program is already
   closed, it's very likely that the transaction is not open any
   more.)

5) If you used two-phase-commit, and did prepare, but not commit the
transaction, you can still reconnect to the database and roll back the
frozen transaction.

6) If your transaction is not open any more, but you saved the PG XLog
files for PITR (Point In Time Recovery), or maybe just have enough of
the most recent xlog files lying around, you should be able to roll back
your database to the point just before the transaction containing your
update committed, but all changes by other transactions after your
update will be lost, too. But PITR is only available with recent
versions of PostgreSQL.

If neither of the above points is true for you, I see the following
possibilities, which are not exactly "UNDO", but will restore your data.

7a) Try to formulate an UPDATE (or series of SQL commands) that exactly
reverts the effect of your offending update.

7b) Restore your database from a recent backup[2] / your original data
sources[3], and recreate all actions that happened between that and your
offending update.

7c) If neither you nor the autovacuum daemon did not run any VACUUM
commands yet, you might use forensics or manipulation with the
PostgreSQL transaction ID counters to recover the old row versions from
the PostgreSQL tables. You should be (or hire) an absolute expert for
PostgreSQL internas to do this, and only try this on a copy of your
PostgreSQL cluster, as the risk is high that your whole cluster gets
inconsistent. If you want to go this way, you should shut down your
database server _now_, and don't restart it unless that absolute expert
does so.


Maybe I've missed some additional option or academical corner cases, but
I'm pretty shure the most important ones are mentioned.

HTH,
Markus

Footnotes:
[1] In this case, it is advisable that your read appropriate beginners
documentation about RDBMS (Relational DataBase Management Systems) in
general, and the appropriate parts of the PostgreSQL documentation, to
avoid getting in the same trouble again in the future. (And, in my
opinion, MySQL based literature does not qualify as "appropriate
beginners documentation about RDBMS" as they view some things somwehat
differently than all other RDBMSes I know of.)

[2] If you don't have a recent backup at hands, some experts may regard
this as a proof that your data was not important.

[3] For read-only / look-up data that you get from an upstream source,
like phonebooks, street network data, or ISBN/ISSN/EAN databases, etc.

-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf. | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org

---(end of broadcast)---
TIP 2: Don't 'kill -9' the postmaster


Re: [SQL] to_dec()

2006-08-12 Thread Aaron Bono
On 8/4/06, Melvin Davidson <[EMAIL PROTECTED]> wrote:










I have created a to_dec() function, which is in essence, the opposite of the to_hex() function.


Does anybody know how I can submit this function to Postgresql ?




See 1.5 on the FAQ: http://www.postgresql.org/docs/faqs.FAQ_DEV.html

You will need to submit the patch to pgsql-patches@postgresql.org. It
will be reviewed by other contributors to the project and will be
either accepted or sent back for further work.

Read the FAQ as they specify the format of your submission.==   Aaron Bono   Aranya Software Technologies, Inc.   
http://www.aranya.com   http://codeelixir.com==


Re: [SQL] Breaking up a query

2006-08-12 Thread Aaron Bono
On 8/10/06, Saad Anis <[EMAIL PROTECTED]> wrote:
Hi Guys,A fellow at work has written the SQL below to retrieve some data frommultiple tables. Obviously it is inefficient and unnecessarily complex, andI am trying to break it into 2 or more queries so as to enhance performance.
Can you please help me do so? I would appreciate any help you can provide.I have also attached the output of the "explain analyze" of this query.Thank you.SaadSELECTv.xcvr_id
 as xcvr_id, v.bumper_number as bumper_number, v.vehicle_type as vehicle_type, p.epoch as epoch, p.latitude as latitude, p.longitude as longitude, p.fom as fom, i.version
 as version, i.rfid_status as rfid_status, t.tag_id as tag_id, t.tag_status as tag_statusFROMpositions pLEFT OUTER JOIN data_transfers dtON p.id
 = dt.position_idINNER JOIN vehicles vON p.vehicle_id = v.idLEFT OUTER JOIN interrogations iON p.id = i.position_idAND 
v.id = i.vehicle_idLEFT OUTER JOIN tags tON i.id = t.interrogation_idWHEREp.id NOT IN (SELECT dt.position_id
FROM data_transfersWHERE dt.target_id = ?)ORDER BY v.xcvr_id, v.bumper_number, v.vehicle_type, i.version, i.rfid_status, p.epoch;
 On an surface scan of what you have I don't see anything obviously wrong.  Do you have your foreign keys defined along all the joins?  What kind of indexes do you have defined on the tables?My guess is your problem is occurring here:
->  Merge Left Join  (cost=0.00..11334.00 rows=1000 width=28) (actual time=705.104..706.789 rows=55 loops=1)  Merge Cond: ("outer".id = "inner".position_id)   Filter: (NOT (subplan))
   ->  Index Scan using positions_pkey on positions p  (cost=0.00..32.00 rows=1000 width=28) (actual time=0.019..90.920 rows=13958 loops=1)   ->  Index Scan using data_transfers_position_id_idx on data_transfers dt  (cost=
0.00..32.00 rows=1000 width=8) (actual time=0.015..91.859 rows=13903 loops=1)You see that the cost jumps significantly.==   Aaron Bono
   Aranya Software Technologies, Inc.   http://www.aranya.com   http://codeelixir.com==