Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pq: unexpected Parse response 'D' #635

Open
rothskeller opened this issue Jul 8, 2017 · 15 comments
Open

pq: unexpected Parse response 'D' #635

rothskeller opened this issue Jul 8, 2017 · 15 comments

Comments

@rothskeller
Copy link

You can probably close this issue immediately; it was user error on my part. But I searched your issues list looking for help debugging it and couldn't find any, so I'm adding this issue for future people searching for it.

In my case, this wonderfully cryptic message resulted from trying to start a new statement before reading all of the rows of the preceding statement. Should the future searcher get this error, that would be the first thing to look for.

@maddyblue
Copy link
Collaborator

This is a bug. Which commit were you running? We just added something similar to this in the most recent commit (dd1fe20).

@rothskeller
Copy link
Author

I'm running 8837942.

@rothskeller
Copy link
Author

What I was trying to do, by the way, was read the rows of a Query and, while processing each one, insert a corresponding row to a different table; all of this within the same transaction. Of course I should have realized that's not legal. It's too bad, though; I really don't want to cache all of the data in memory, because there's a lot of it. But I can't think of a better answer.

@tamird
Copy link
Collaborator

tamird commented Jul 9, 2017

@rothskeller you can probably do what you're after using cursors: https://www.postgresql.org/docs/current/static/plpgsql-cursors.html

@rothskeller
Copy link
Author

Bless you, @tamird, that's exactly what I need. Learn something new every day....

@hypnoglow
Copy link

hypnoglow commented Sep 7, 2017

Example on using cursors for your case (with some random data):

if _, err := tx.Exec(`
	DECLARE orders_cursor CURSOR FOR
	SELECT id
	FROM order
	WHERE client_id = $1
	ORDER BY createdAt
	FOR UPDATE
	`,
	client.ID,
); err != nil {
	return err
}
defer tx.Exec("CLOSE orders_cursor")

var id int64

for {
	if err := tx.QueryRow(
		`FETCH NEXT FROM orders_cursor`,
	).Scan(&id); err != nil {
		if err == sql.ErrNoRows {
			// End of rows.
			break
		}

		return err
	}

	if _, err := tx.Exec(`
		UPDATE order
		SET amount = amount - 1
		WHERE CURRENT OF orders_cursor
		`,
	); err != nil {
		return err
	}
}

return nil

@timbunce
Copy link

timbunce commented Oct 9, 2017

Thanks for the example code @hypnoglow.

What I was trying to do, by the way, was read the rows of a Query and, while processing each one, insert a corresponding row to a different table; all of this within the same transaction. Of course I should have realized that's not legal.

It works in the official libpq (and so all drivers that build on it or emulate it closely) because libpq defaults to fetching and buffering all the rows in memory. It's arguably a cheat but a very effectively one that's very widely used.

See also #81.

carmenlau added a commit to carmenlau/skygear-server that referenced this issue Nov 1, 2018
…ous query cursor is not yet closed.

Problem only happen when the queries are run in the same transaction, we don’t have db transaction in v1
schema create handler. So only apply this fix to next.
Related issue: lib/pq#635
carmenlau added a commit to carmenlau/skygear-server that referenced this issue Nov 1, 2018
…ous query cursor is not yet closed.

Problem only happen when the queries are run in the same transaction, we don’t have db transaction in v1
schema create handler. So only apply this fix to next.
Related issue: lib/pq#635
carmenlau added a commit to carmenlau/skygear-server that referenced this issue Nov 5, 2018
…ous query cursor is not yet closed.

Problem only happen when the queries are run in the same transaction, we don’t have db transaction in v1
schema create handler. So only apply this fix to next.
Related issue: lib/pq#635
bartekn added a commit to stellar/go that referenced this issue Dec 9, 2019
This commit adds `Synchronized` flag to `support/db.Session`. When set
to `true` and `Session` runs a transaction all `Exec*` and `Select*`
methods are protected by mutex what allows running them in multiple
goroutines.

This is an experimental feature (see below) and not a breaking change
(default is `false`).

Postgres protocol does not allow sending Exec query results if
previously sent Query haven't been fully read. This issue manifested
itself when a PR that's sending read and write queries in multiple
goroutines was merged.

More info: lib/pq#81 lib/pq#635

Known limitations:

* It's possible that it will not be needed if we decide to remove
concurrency from ingestion pipeline (see #1983). We are adding this to
unblock development of new ingestion processors with a more readable
code (currently all database processors are merged into one big
processor that is hard to read and test). Splitting `DatabaseProcessor`
will be done in a separate PR/PRs.
* During Horizon Team meeting we agreed to create a new
`SynchronizedSession` struct embedding `db.Session` inside that would
not be placed in a shared `support` package. The problem is that
`history.Q` embeds `db.Session` inside. Changing this to
`db.SessionInterface` requires updating a lot of files that create
`history.Q` objects, tests and mocks. Given that we may want to revert
this change in the future, the change in this commit seems to be
simpler.
@hsblhsn
Copy link

hsblhsn commented Aug 10, 2020

Is there anyone alive to fix it? @bartekn can you please have a deeper look into this?

@johto
Copy link
Contributor

johto commented Aug 10, 2020

Is there anyone alive to fix it? @bartekn can you please have a deeper look into this?

What's there to fix?

@hsblhsn
Copy link

hsblhsn commented Aug 10, 2020

What's there to fix?

I want to run 5 concurrent select query using a single transaction using gorm. But these errors like driver: bad connection, pq: unexpected Parse response 'C', no rows returned keeps happening.

libraries like github.com/jackc/pgx is working without any issue. But lib/pq keeps failing.

Can we please safely handle this without confusing with memory?

@maddyblue
Copy link
Collaborator

lib/pq isn't well maintained and I'm not sure anyone here will fix this problem. You'll probably need to either write that PR yourself, or use pgx (I recommend using pgx).

@andreypanin
Copy link

> I'm adding this issue for future people searching for it.

Future people thank you!

@woswos
Copy link

woswos commented Dec 10, 2021

still not fixed, can confirm

@dovydas55
Copy link

still not fixed, can confirm

1 similar comment
@kmorake
Copy link

kmorake commented Aug 23, 2024

still not fixed, can confirm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests