def explode_hyperedge(hyperedge, hyperedge_id):
    """
    Takes a hyperedge set, and generates all the dyadic connections for a similar regular edge.
    :param hyperedge: (list) List of participating terms.
    :param hyperedge_id: (int) Identifier for the hyperedge, used for postgres.
    :return: (list) List of connection tuples
    """

    # We'll sort, so we have a clear idea of how many of the same edge we have, since it will always be
    # of the form (smaller_id, larger_id).
    hyperedge = sorted(hyperedge)
    edge_set = []
    postgres_set = []
    # essentially build connections to all "vertices after". This guarantees we don't do edges twice.
    for i, _ in enumerate(hyperedge):
        for j in range(i+1, len(hyperedge)):
            edge_set.append((hyperedge[i], hyperedge[j]))
            # postgres performs better with duplicate edges, so we manually correct this here
            postgres_set.append((hyperedge_id, hyperedge[i], hyperedge[j]))
            postgres_set.append((hyperedge_id, hyperedge[j], hyperedge[i]))

    return edge_set, postgres_set