Skip to content

Commit 7b3c289

Browse files
committed
400: updated pg compaction bounds
1 parent 90aac49 commit 7b3c289

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

modules/module-postgres-storage/src/storage/PostgresCompactor.ts

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,33 +110,63 @@ export class PostgresCompactor {
110110
let upperOpIdLimit = BIGINT_MAX;
111111

112112
while (true) {
113-
const batch = await this.db.sql`
114-
SELECT
115-
op,
116-
op_id,
117-
source_table,
118-
table_name,
119-
row_id,
120-
source_key,
121-
bucket_name
122-
FROM
123-
bucket_data
124-
WHERE
125-
group_id = ${{ type: 'int4', value: this.group_id }}
126-
AND bucket_name >= ${{ type: 'varchar', value: bucketLower }}
127-
AND (
128-
(
129-
bucket_name = ${{ type: 'varchar', value: bucketUpper }}
130-
AND op_id < ${{ type: 'int8', value: upperOpIdLimit }}
131-
)
132-
OR bucket_name < ${{ type: 'varchar', value: bucketUpper }} COLLATE "C" -- Use binary comparison
133-
)
134-
ORDER BY
135-
bucket_name DESC,
136-
op_id DESC
137-
LIMIT
138-
${{ type: 'int4', value: this.moveBatchQueryLimit }}
139-
`
113+
const bucketLowerValue = bucketLower ?? '';
114+
const bucketUpperValue = bucketUpper ?? MAX_CHAR;
115+
const bucketLowerParam = { type: 'varchar' as const, value: bucketLowerValue };
116+
const bucketUpperParam = { type: 'varchar' as const, value: bucketUpperValue };
117+
const limitParam = { type: 'int4' as const, value: this.moveBatchQueryLimit };
118+
const groupParam = { type: 'int4' as const, value: this.group_id };
119+
const upperOpParam = { type: 'int8' as const, value: upperOpIdLimit };
120+
121+
const batchStatement =
122+
upperOpIdLimit === BIGINT_MAX
123+
? this.db.sql`
124+
SELECT
125+
op,
126+
op_id,
127+
source_table,
128+
table_name,
129+
row_id,
130+
source_key,
131+
bucket_name
132+
FROM
133+
bucket_data
134+
WHERE
135+
group_id = ${groupParam}
136+
AND bucket_name >= ${bucketLowerParam}
137+
AND bucket_name < ${bucketUpperParam} COLLATE "C" -- Use binary comparison
138+
ORDER BY
139+
bucket_name DESC,
140+
op_id DESC
141+
LIMIT
142+
${limitParam}
143+
`
144+
: this.db.sql`
145+
SELECT
146+
op,
147+
op_id,
148+
source_table,
149+
table_name,
150+
row_id,
151+
source_key,
152+
bucket_name
153+
FROM
154+
bucket_data
155+
WHERE
156+
group_id = ${groupParam}
157+
AND bucket_name >= ${bucketLowerParam}
158+
AND ROW (bucket_name COLLATE "C", op_id) < ROW (
159+
${bucketUpperParam},
160+
${upperOpParam}
161+
)
162+
ORDER BY
163+
bucket_name DESC,
164+
op_id DESC
165+
LIMIT
166+
${limitParam}
167+
`;
168+
169+
const batch = await batchStatement
140170
.decoded(
141171
pick(models.BucketData, ['op', 'source_table', 'table_name', 'source_key', 'row_id', 'op_id', 'bucket_name'])
142172
)

0 commit comments

Comments
 (0)