code: func GetDevicesWithClassBOrClassCDeviceQueueItems(db sqlx.Ext, count int) ([]Device, error) {
  	gpsEpochScheduleTime := gps.Time(time.Now().Add(schedulerInterval * 2)).TimeSinceGPSEpoch()
  
  	var devices []Device
  	err := sqlx.Select(db, &devices, `
          select
              d.*
          from
              device d
          where
  			d.mode in ('B', 'C')
              -- we want devices with queue items
              and exists (
                  select
                      1
                  from
                      device_queue dq
                  where
                      dq.dev_eui = d.dev_eui
                      and (
  						d.mode = 'C'
                      	or (
  							d.mode = 'B'
                      		and dq.emit_at_time_since_gps_epoch <= $2
                      	)
                      )
              )
              -- we don't want device with pending queue items that did not yet
              -- timeout
              and not exists (
                  select
                      1
                  from
                      device_queue dq
                  where
                      dq.dev_eui = d.dev_eui
                      and is_pending = true
                      and dq.timeout_after > $3 
              )
          order by
              d.dev_eui
          limit $1
          for update of d skip locked`,
  		count,
  		gpsEpochScheduleTime,
  		time.Now(),
  	)
  	if err != nil {
  		return nil, handlePSQLError(err, "select error")
  	}
  
  	return devices, nil
  }
docs: // GetDevicesWithClassBOrClassCDeviceQueueItems returns a slice of devices that qualify
  // for downlink Class-C transmission.
  // The device records will be locked for update so that multiple instances can
  // run this query in parallel without the risk of duplicate scheduling.