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

use COPY statement for flush #1508

Merged
merged 23 commits into from
Feb 14, 2025
Merged

use COPY statement for flush #1508

merged 23 commits into from
Feb 14, 2025

Conversation

kyscott18
Copy link
Collaborator

@kyscott18 kyscott18 commented Feb 12, 2025

Closes #1469

@kyscott18 kyscott18 linked an issue Feb 12, 2025 that may be closed by this pull request
Base automatically changed from kjs/indexing-transactions to main February 12, 2025 22:00
An error occurred while trying to automatically change base from kjs/indexing-transactions to main February 12, 2025 22:00
@kyscott18 kyscott18 requested a review from typedarray February 13, 2025 00:35
Comment on lines +118 to +119
await database.retry(async () => {
await database.transaction(async (client, tx) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was this an oversight from the tx PR (run setup functions in tx) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not really an oversight, it just wasn't necessary

schema: schemaBuild.schema,
});
const result = await fn(client, tx);
await client.query("COMMIT");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a note that if we'd rather not use the pool-level SET trick, you can set the current transaction to be asynchronous.

This parameter can be changed at any time; the behavior for any one transaction is determined by the setting in effect when it commits. It is therefore possible, and useful, to have some transactions commit synchronously and others asynchronously. For example, to make a single multistatement transaction commit asynchronously when the default is the opposite, issue SET LOCAL synchronous_commit TO OFF within the transaction.

https://arc.net/l/quote/fgpdpxhz

await client.query("COMMIT");
return result;
} catch (error) {
await client?.query("ROLLBACK");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is client nullable?

Comment on lines 419 to 431

common.logger.debug({
service: "database",
msg: `Inserted ${insertValues.length} '${getTableName(table)}' rows`,
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this unintentionally reverts the logs I changed


const text = getCopyText(
table,
insertValues.map(({ row }) => row),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't we have to be careful last time about memory here, to make sure the insertValues are garbage-collectable as we build the text?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previously, yes. That was when we were handling a cache that was too large and flushing buffer entries to the database at the same step. Now because the buffer entries get flushed in smaller and more frequent chunks it isn't necessary.

Comment on lines +244 to +270
export const getCopyText = (
table: Table,
rows: { [key: string]: unknown }[],
) => {
const columns = Object.entries(getTableColumns(table));
const results: string[] = [];
for (const row of rows) {
const values: string[] = [];
for (const [columnName, column] of columns) {
let value = row[columnName];
if (value === null || value === undefined) {
values.push("\\N");
} else {
if (column.mapToDriverValue !== undefined) {
value = column.mapToDriverValue(value);
}
if (value === null || value === undefined) {
values.push("\\N");
} else {
values.push(String(value).replace(/\\/g, "\\\\"));
}
}
}
results.push(values.join("\t"));
}
return results.join("\n");
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@typedarray Is this what you had in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup LGTM

@kyscott18 kyscott18 changed the base branch from main to kjs/indexing-speed February 14, 2025 00:27
@kyscott18 kyscott18 merged commit 4331e71 into kjs/indexing-speed Feb 14, 2025
6 checks passed
@kyscott18 kyscott18 deleted the kjs/copy branch February 14, 2025 00:27
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

Successfully merging this pull request may close these issues.

Error Flushing Cash
2 participants