IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Postgres / serial datatype / perl question
When using Perl DBD::Pg to insert a record that contains a "serial" field
for the id, how to I determine the value it just assigned.

I've got these paired transactions (insert on begin, update on end)
that I need to update the original generated record.

How do I determine what it was?
New Getting closer
\n       "last_insert_id"\n             $rv = $dbh->last_insert_id($catalog, $schema, $table, $field);\n


Except I'm not sure what to put for catalog or schema.
I'm used to oracle where I would consider the schmea to user/owner.
I assume catalog would be the particular database (oracle instance).
But this does not work, returning null.
Blech.
New Did you google?
I looked for "DBD::Pg serial perl" and skimmed to something that looked relevant. That took me to [link|http://opensource.atlassian.com/projects/hibernate/browse/HB-875|http://opensource.at...ate/browse/HB-875] which pointed at [link|http://www.postgresql.org/docs/faqs/FAQ.html#4.15.2|http://www.postgresq...s/FAQ.html#4.15.2] that seems to answer this question pretty well.

Cheers,
Ben
About the use of language: it is impossible to sharpen a pencil with a blunt axe. It is equally vain to try to do it with ten blunt axes instead. -- Edsger W. Dijkstra
New Yup. Didn't find that though.
Saw lots of people whining about coming from mysql where is has
something built in for this.

Hacked around it already.

lock table
do insert
select max(id)
commit - which I BELIEVE unlocks table
disconnect - which I'm sure unlocks table.

I'm using Sun Grid Engine to control my print spooling. I've got dozens of
jobs associated with the spooling task, all of which have lots of logging
to individual log files / emails to user.

So I'm telling SGE to execute my logging scripts before and after each job,
which should allow reporting to come off the centralized database rather than
track stuff through the many log files.

So the before task creates the log entry, while the after task has to update it.
New my, that's a big hammer you have there...
Yes, the 'commit' does unlock the table. It tells the database server to 'make it so'. 'rollback' undoes whatever you did, provided you haven't already done a commit.

The lock ... select max... is really a brute force and ignorance (in the non-pejorative sense) approach. It will kill you on a multi-user transaction system. A data warehousing system can probably get away with it, but it is still poor form. Take a few minutes to implement the method Ben pointed to. You'll save yourself mysterious headaches later.
Have fun,
Carl Forde
New Oh, I'll modify it
But the current way would NOT be a killer.
The id field is indexed. So select max is not doing a table scan.
Log entries instaneous from my timing point of view.
The acual insert/updates might be 20-40 an hour.
Each less than a second.

So my hammer is not dangerous, it is just not the best way.
New SELECT currval
That's the one I've used in the past. Works like a charm.
--
Chris Altmann
New OK, now I want logging suggestions
The following is the help message from my program.

#1 - Is it understandable to you?
#2 - Are there any other standard fields I should be logging, ie: What did I miss?

\n/lf/bin/sql_log.pl: Insert / Update an entry in the log table\n\nProgram will display to STDOUT the id of the record generated.\n\nMust have at LEAST one field assigned via:\n        --val field=data\n\nSuch as:\n        --val control_file="test2"\n\nAdd multiple --val flags for each field.\nPossible fields are:\n        control_file\n        run_user\n        sge_id\n        sge_job\n        sge_stdout_file\n        sge_stderr_file\n        parent_sge_id\n        parent_sge_job\n        project\n        job\n        subsystem\n        program\n        log_text\n        result_message\n        exit_value\n        program_start_time\n        program_end_time\n\n\nOptional:\n        --dbname=s        Currently: mash01\n        --user=s          Currently: Log\n        --passwd=s\n        --log_table=s     Currently: prod_log\n        --id_field=s      Currently: id\n        --update\n        --last_id=s\n        --key_override\n\nIf using --update, you must supply the id generated by the insert statement, ie:\n        --last_id=1234\n\nIf you want to use fields this program is unaware of, use:\n        --key_override\nThis flag will accept all keys as valid columns. It will NOT attempt to validate\nthem and the program will error out if they do not exist in the target log table.\n\n\nExample usage\n-------------\nInsert step:\n\nsql_log.pl \\\n  --val control_file=p9250t12.ctl.20040914_162603 \\\n  --val run_user=broom@cc3.com \\\n  --val sge_job=Qp9250t12-12-gen_afp \\\n  --val sge_id=11202 \\\n  --val sge_stdout_file=Qp9250t12-12-gen_afp.o11202 \\\n  --val sge_stderr_file=Qp9250t12-12-gen_afp.e11202 \\\n  --val parent_sge_job=p9250t12 \\\n  --val parent_sge_id=11178 \\\n  --val project=p9250 \\\n  --val job=p9250t12 \\\n  --val subsystem=PNT_SPOOL \\\n  --val program=gen_afp.pl \\\n  --val program_start_time="`date`"\n\n\nThis produces:\n\nAssigned ID: 7\n\nYou must take that ID and then:\n\nUpdate step:\n\nsql_log.pl \\\n        --update \\\n        --last_id=7 \\\n        --val log_text="This program ran OK - random text" \\\n        --val result_message="24 pages produced" \\\n        --val exit_value=0 \\\n        --val program_end_time="`date`"\n\n\nYou may feed the full text "Assigned ID: 7" as the arg for the\n--last_id and the program will parse it out, allowing seamless\ntemp var/file usage for passing the id around.\n\n\nThe above example will produce this log record:\n-[ RECORD 7 ]-----+----------------------------------\nid                 | 7\nctime              | 2004-09-19 10:12:00.633873\ncontrol_file       | p9250t12.ctl.20040914_162603\nrun_user           | broom@cc3.com\nsge_job            | Qp9250t12-12-gen_afp\nsge_id             | 11202\nsge_stdout_file    | Qp9250t12-12-gen_afp.o11202\nsge_stderr_file    | Qp9250t12-12-gen_afp.e11202\nparent_sge_job     | p9250t12\nparent_sge_id      | 11178\nproject            | p9250\njob                | p9250t12\nsubsystem          | PNT_SPOOL\nprogram            | gen_afp.pl\nlog_text           | This program ran OK - random text\nresult_message     | 24 pages produced\nexit_value         | 0\nprogram_start_time | 2004-09-19 10:12:00\nprogram_end_time   | 2004-09-19 10:12:05\n\n\n\n
New A thought on Q#1 only, because...
...answering Q2 would require me to think about the actual job you're trying to do, and I'm not sure I remember the details too well, and I'm not about to look them up in a bunch of old threads -- Anyway, about #1, "Is [the help message] understandable to you?", one comment:

I assume the single 's' in "--dbname=s", "--user=s", etc, stands for "string". If you're mostly going to be running this yourself, that's fine; but, if you have a bunch of less-sophisticated (l)users who'll be running this, then it might be good to spell that out.

Just a thought.


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
New (l)users don't run command line loggers
This is strictly for programmers automating production flows.
     Postgres / serial datatype / perl question - (broomberg) - (9)
         Getting closer - (broomberg)
         Did you google? - (ben_tilly) - (4)
             Yup. Didn't find that though. - (broomberg) - (2)
                 my, that's a big hammer you have there... - (cforde) - (1)
                     Oh, I'll modify it - (broomberg)
             SELECT currval - (altmann)
         OK, now I want logging suggestions - (broomberg) - (2)
             A thought on Q#1 only, because... - (CRConrad) - (1)
                 (l)users don't run command line loggers - (broomberg)

It didn’t ruin my childhood, but it did aggressively strip-mine several shallow deposits of nostalgia.
50 ms