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

PostgreSQL Batch Example in Go #201

Closed
wants to merge 13 commits into from

Conversation

giuliocn
Copy link

@giuliocn giuliocn commented May 2, 2024

Description:

This Go program demonstrates interacting with a PostgreSQL database using batch operations for improved efficiency. It leverages the pgx/v5 library for database connectivity.

Features:

  • Creates a table named ledger to store data (description and amount).
  • Executes multiple SQL statements within a single database transaction using pgx.Batch. This includes inserts (INSERT), selects (SELECT), and an aggregate function (SUM).
  • Gracefully handles potential errors during database operations.
  • Ensures data consistency by using transactions with Begin, Commit, and Rollback.
  • Deletes all rows from the ledger table after processing.

examples/batch/batch.go Outdated Show resolved Hide resolved
examples/batch/batch.go Outdated Show resolved Hide resolved
@giuliocn giuliocn requested a review from pashagolub May 5, 2024 13:12
examples/batch/batch.go Outdated Show resolved Hide resolved
examples/batch/batch.go Outdated Show resolved Hide resolved
fmt.Println(query.SQL)

// BatchResult.Query reads results from a queued query
// BatchResult.Query reads results from the current query
rows, err := br.Query()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first query is INSERT. It returns no rows. It's wrong to use br.Query for it

@pashagolub pashagolub self-assigned this May 13, 2024
@pashagolub pashagolub added the documentation Improvements or additions to documentation label May 13, 2024
@pashagolub pashagolub force-pushed the pull-request-batch branch from 2957dd3 to b0d1e2a Compare May 31, 2024 09:47
@pashagolub
Copy link
Owner

You may finish your work now. Please, make sure folder structure looks the same as for other examples, e.g. no .md files, please.

@giuliocn
Copy link
Author

Thanks @pashagolub I would like to reopen this PR to further improve this example.

@pashagolub
Copy link
Owner

OK, the pull-request branch was deleted. We need to proceed in the new PR

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

Successfully merging this pull request may close these issues.

2 participants