I'm adding records to a postgres database using a powershell script.

How can I tell how many records are in a transaction - or to put it another way how can I tell how many INSERT statements were executed between issuing a BEGIN, and closing it with a COMMIT?

I'm adding batches of records using code something like this....

$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "INSERT xyz INTO blah"
$insertedRec = $insertedRec + $n

Where $n is the number of records inserted. I start each batch with a BEGIN and periodically issue a COMMIT like this....

 $DBCmd = $DBConn.CreateCommand();
 $DBCmd.CommandText = "COMMIT; ";

I'd like to do some reconciliation and confirm that the number of records I think I've INSERTed match the number of records COMMITed in the transaction. I had hoped that $z might tell me this but it doesn't.

Is there a simple way to achieve this?



While I do not work directly with PostgreSQL, your SQL code would have to handle outputting the records changed. PowerShell or the ExecuteNonQuery() method does not usually contain that value on most DBMS drivers.

You can check this answer on SO to see how it is collected via SQL code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.