1. 개요

<aside> 💡

모든 회원에게 알림을 보내야하는 상황입니다(브로드캐스트).

회원 50,000명이 DB에 저장되어 있고, 해당 모든 알림 정보를 DB에 저장해야 합니다.

</aside>

2. 문제상황

2-1. PSQLException: ERROR: too many parameters in SQL statement

PSQLException

PSQLException

Cause: org.postgresql.util.PSQLException: PreparedStatement can have at most 65,535 parameters. Please consider using arrays, or splitting the query in several ones, or using COPY. Given query has 300,060 parameters<EOL>; PreparedStatement can have at most 65,535 parameters. Please consider using arrays, or splitting the query in several ones, or using COPY. Given query has 300,060 parameters]

2-1-1. 원인

2-1-2. 해결방법 후보

  1. DB 변경 (PostgreSQL → MySql(MariaDB))
  2. DB 테이블 추가 (공지사항 테이블)
  3. 배치 삽입:
  4. COPY 삽입

3. 해결

3-1. 배치 삽입

@Transactional
    public void saveAll(
            List<MemberResponseDto> memberList,
            Long templateId,
            String subject,
            String content,
            Instant createdAt
    ) {
        if (memberList == null || memberList.isEmpty()) return;

        final int CHUNK = 5_000;
        for (int i = 0; i < memberList.size(); i += CHUNK) {
            int end = Math.min(i + CHUNK, memberList.size());
            List<MemberResponseDto> sub = memberList.subList(i, end);
            notificationMapper.saveAll(sub, templateId, subject, content, createdAt, StatusIds.Notification.UNREAD);
        }
    }

3-1-1. 결과