Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:45:16+00:00 2026-05-26T22:45:16+00:00

As per Postgres Documentation – Once prepared, a transaction can later be committed or

  • 0

As per Postgres Documentation – Once prepared, a transaction can later be committed or rolled back with COMMIT PREPARED or ROLLBACK PREPARED, respectively. Those commands can be issued from any session, not only the one that executed the original transaction.

I am trying to import data from csv into database tables and for this, I am using the

COPY tablename [ ( column [, ...] ) ]
FROM { 'filename' }

all this is done in a shell script.
Now the issue is that I am executing psql command and passing this command as parameter via the -c option ( I start transaction via the command

prepare transaction 'some-id' in that command).

I want to create a Savepoint and rollback to it incase of any errors.

After a few other tasks in the shell script, I check for errors that the previous psql statement have produced and when I then try to rollback using the command

Prepared Rollback 'transaction-id' ( in separate psql command with sql statements )

It reports “No "transaction-id" found“

Am I getting the concept wrong or missing something in the process?

Is this happening because I am issuing psql command multiple time and each is resulting in new transaction ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:45:16+00:00Added an answer on May 26, 2026 at 10:45 pm

    For your prepare to work, the COPY and PREPARE must be in the same session. Since your question lacks concrete commands, I’m assuming that when you write:

    Prepared Rollback ‘transaction-id’ ( in separate psql command with sql statements )

    You’re using different psql commands to COPY and PREPARE. This is wrong. Combine the COPY and PREPARE to the same session.

    E.g.

    $ psql -c "BEGIN; COPY tablename FROM '/tmp/sql'; PREPARE TRANSACTION 'foobar';" db
    $ while /bin/not-ready-to-commit ; do sleep 1 ; done
    $ psql -c "COMMIT PREPARED 'foobar';" db
    

    The PREPARE TRANSACTION works by writing the current transaction to the disc and exiting the transaction process in the current session. This is why you need a BEGIN: it starts the transaction you want to prepare. All commands you want to be affected by the prepeare must come after the transaction has been started (in your case the COPY command). When the PREPARE TRANSACTION is issued, the transaction you are currently in is written to disk with the identifier you give. Any statements issued after the transaction is prepared are no longer part of the transaction. So doing BEGIN; PREPARE... ; COPY runs the COPY operation without a transaction.

    Here’s an example in psql shell:

    demo=# DELETE FROM foo;
    DELETE 4
    demo=# BEGIN; -- start a transaction
    BEGIN
    DEMO=# COPY foo FROM '/tmp/sql'; -- do what you want to commit later
    COPY 4
    demo=# PREPARE TRANSACTION 'demo'; -- prepare the transaction
    PREPARE TRANSACTION
    demo=# ROLLBACK; -- this is just to show that there is no longer a transaction
    NOTICE:  there is no transaction in progress
    ROLLBACK
    demo=# SELECT * FROM foo; -- the table is empty, copy waiting for commit
     a | b 
    ---+---
    (0 rows)
    demo=# COMMIT PREPARED 'demo'; -- do the commit
    COMMIT PREPARED
    demo=# SELECT * FROM foo; -- data is visible
     a | b 
    ---+---
     1 | 2
     3 | 4
     5 | 6
     7 | 8
    (4 rows)
    

    Edit: You must enable prepared transactions in postgresql.conf:

    max_prepared_transactions = 1 # or more, zero (default) disables this feature.
    

    If max_prepared_transactions is zero, psql reports that the transaction id is not found, but does not warn you about this feature being disabled. Psql gives a warning for PREPARE TRANSACTION but it’s easy to miss if your shell scripts print stuff after the prepare statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Per the Postgres manual on date/time , the timestamp type can go from 4713
Per the Java documentation, the hash code for a String object is computed as:
Per the documentation , String.Format will throw a FormatException if either (A) the format
Per documentation, Doctrine_Record after saving should set id of newly created record as object
Per http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html The init method of NSDateFormatter is Available in iPhone OS 2.0 through
Per the SWIG Documentation (21.9.1 Default primitive type mappings), the C uint8_t is mapped
per the discussion of R programming styles, I saw someone once said he puts
in postgres as an SQL command execute runs a prepared statement from within a
Per man pages, snprintf is returning number of bytes written from glibc version 2.2
Per this helpful article I have confirmed I have a connection pool leak in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.