fix: chunk FIRMS bulk inserts under asyncpg 32767 bind cap
A ~90k-row INSERT ... ON CONFLICT dies with InterfaceError, so fire_loop fails every poll and /api/health stays degraded (fires age > 30 min). Chunk 2000 rows per statement, still one commit per poll.
This commit is contained in:
parent
19fa0db32c
commit
5302e261d7
2 changed files with 67 additions and 7 deletions
|
|
@ -20,6 +20,11 @@ from sources import event_dedup_key
|
|||
|
||||
logger = logging.getLogger("osint.ingestor")
|
||||
|
||||
# asyncpg rejects statements with >32767 bind params. A FIRMS poll is ~90k
|
||||
# rows × 14 columns. Chunk inserts; still one transaction / one commit.
|
||||
FIRE_ROW_BIND_PARAMS = 14
|
||||
FIRE_INSERT_CHUNK = 2000
|
||||
|
||||
# NATS connection settings
|
||||
NATS_URLS = NATS_URL
|
||||
NATS_STREAM = "events"
|
||||
|
|
@ -126,15 +131,18 @@ async def ingest_fire_rows(msgs: list[dict]) -> int:
|
|||
rows.append(row)
|
||||
if not rows:
|
||||
return 0
|
||||
inserted = 0
|
||||
async with async_session() as session:
|
||||
stmt = (
|
||||
pg_insert(fires_table)
|
||||
.values(rows)
|
||||
.on_conflict_do_nothing(constraint="pk_fires_natural_key")
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
for i in range(0, len(rows), FIRE_INSERT_CHUNK):
|
||||
chunk = rows[i:i + FIRE_INSERT_CHUNK]
|
||||
stmt = (
|
||||
pg_insert(fires_table)
|
||||
.values(chunk)
|
||||
.on_conflict_do_nothing(constraint="pk_fires_natural_key")
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
inserted += int(result.rowcount or 0)
|
||||
await session.commit()
|
||||
inserted = int(result.rowcount or 0)
|
||||
if inserted:
|
||||
logger.info("bulk ingested %d/%d FIRMS hotspots", inserted, len(rows))
|
||||
from live_layers import aircraft_last_known
|
||||
|
|
|
|||
Loading…
Reference in a new issue