porsager / porsager/postgres

Incorrect multi-query results

Open
#1,031 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

  1. Try executing DELETE... ; UPDATE ...; SELECT ...;.
  2. Get result as an array of 2, where first element is an empty array.
  3. Expectation is to get array of 3 results, like [[], [], [...]].
  4. Somehow all empty arrays from DELETE, UPDATE, INSERT get merged into one.
const postgres = require('postgres');
	it('Multi-query', (done) => {
		const db = postgres.default(CONNECTION_STRING);
		const upd = /*sql*/`
			UPDATE "settings"
			SET "_json" = '[]'
			WHERE "setting" = 'test';
			--RETURNING "setting";
		`;
		const del = /*sql*/`
			DELETE FROM "settings"
			WHERE "setting" = 'test';
			--RETURNING "setting";
			`;
		const ins = /*sql*/`
			INSERT INTO "settings" ("package", "setting", "_json")
			VALUES ('test', 'test', '{}')
			ON CONFLICT ("package", "setting")
			DO NOTHING;
			--RETURNING "settings";
		`;
		const sel = /*sql*/`
			SELECT "_json"
			FROM "settings"
			WHERE "setting" = 'test';
		`;
		Promise.allSettled([
			db.unsafe(upd),
			db.unsafe(del),
			db.unsafe(ins),
			db.unsafe(sel),
			db.unsafe(del + del + ins + sel),
		]).then(([upd, del, ins, sel, all]) => {
			let success = upd.value.length == 0
				&& del.value.length == 0
				&& ins.value.length == 0
				&& sel.value.length == 1
				&& all.value.length == 4; // 4 sub-results: 0th, 1st, 2nd = [], 3rd = [{}]
			done(success ? undefined : all.value.length); // actually, all.value = [[], [{}]]
		});
	});

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the db.unsafe entry point and reproduce the multi-statement case from the issue using DELETE, UPDATE, INSERT, and SELECT queries. Done means the combined query preserves one result array per statement, including separate empty arrays for the write statements.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.