Answers.Body: <p>I've been reading the code, and I <em>think</em> this is pretty safe, at least on server with MySQL 5.7.5+ or MariaDB 10.0.2+ (if your database is older than this, it's pretty old).</p>
  <p>The code that updates smart groups has various checks in place to make sure it only does each group once in a request.</p>
  <p>All the contacts in a smart group are stored in a &quot;cache&quot; table. (A cache is a temporary store of data that can be recreated as needs be, e.g. when updating the smart group). The cache is entirely separate from the manually added/removed contacts; the cache contains and only contains all the contacts in the group, whether they were included manually or because they match the criteria.</p>
  <p>The process is:</p>
  <ol>
  <li><p>Delete all the cache records for this group. This is important in the example of this question, since it means the search for &quot;contacts whose related contact is in the group&quot; will only be looking at the related contacts that are <em>manually</em> included in the group </p>
  </li>
  <li><p>Creates a temporary table to store new cache entries.</p>
  </li>
  <li><p>Add to the temporary store by running the search, excluding any that are <em>manually removed</em>. In this question: this means we'll be adding the individuals that are related to the local campaign groups, unless they have been manually removed from the group for whatever reason.</p>
  </li>
  <li><p>Add to the temporary store a copy of all those <em>manually added</em>. Of course these contacts may already be in there (from step 2), but that's fine, it ensures every contact is only included once. By now, the temporary store has a copy of everyone that should be in the group. In our example, it means the temporary store now includes:</p>
  <ul>
  <li>All the local campaign groups (and possibly other contacts) that were manually added to the smart group.</li>
  <li>All the activists related to the local campaign groups, unless they had been manually removed.</li>
  </ul>
  </li>
  </ol>
  <hr />
  <p>But it goes on if this group has any child (sub) groups. This is not relevant to this question but I thought I'd include it for completeness' sake...</p>
  <ol start="4">
  <li><p>For each child group, it looks up the contacts in that, exclude any manually excluded from our group, and adds these to our temporary store (again, ignoring any duplicates).</p>
  <p>It will also refresh the cache of any child smart groups, so if there was a smart group within the child groups that referenced our main smart group (!) it would not result in an infinite loop because it knows we've just updated [are updating] that, but I'm unclear what contacts it would see because:</p>
  <ul>
  <li>if it goes on the <em>cache</em> table, that's empty at this point.</li>
  <li>if it goes on (manually added + cache table) then it will see the manually added contacts only.</li>
  </ul>
  </li>
  <li><p>Finally, it empties our group's cache (again? I wonder why...) and copies all the entries from our temporary store into the cache table, and we're done!</p>
  </li>
  </ol>
  <p>So: I think, miraculously, this should actually work and be safe for the purpose stated in my original question!</p>
  <p>Please comment, or post other answers though if you have any experience to share.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-10T09:05:07.043Z
Answers.Text: Link to related dev [chat thread](https://chat.civicrm.org/civicrm/pl/qxwi64zfd7yy3m6t63qmjdfszw)
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-10T11:04:42.300Z
Answers.Text: It feels relatively safe in that the groups are added statically and those pulled in via relationship are related to those groups. But I'd rely much more on your code analysis!
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-10T07:56:47.033Z
Answers.LastActivityDate: 2020-09-25T08:41:17.740Z
Answers.LastEditDate: 2020-09-25T08:41:17.740Z
Answers.UserDisplayName: artfulrobot
Body: <p>An organisation has local campaign groups. Some of these campaign groups belong to a set we call the &quot;core activist network&quot;.</p>
  <p>I want the core activist network group to contain:</p>
  <ul>
  <li>all these groups that are manually added</li>
  <li>all individuals that are related to a local campaign group which is part of the group.</li>
  </ul>
  <p>I've set this up as follows:</p>
  <ol>
  <li>Search for &quot;name contains 'a'&quot; - anything that will return at least one result.</li>
  <li>Save the search as a Smart Group.</li>
  <li>Add at least one local campaign group to this group manually (Add to Group).</li>
  <li>Edit the smart group criteria, remove the silly 'a' criteria, then go to Relationships, choose &quot;Target Contact(s) in group&quot; and enter the name of <strong>this smart group</strong>!</li>
  <li>Run the search and from there click Update Smart Group.</li>
  </ol>
  <p>Amazingly, this appears to have worked! But is there a reason why this is likely to end up in knots, e.g. some infinite loop? Since calculating the smart group requires...calculating the smart group.</p>
  <p>I realise that this is likely to fall foul of SE's &quot;answers likely to be opinion based&quot; but I think this is a valid case. I encourage anyone to upvote any answers that point out potential problems with this situation. Of course, if someone says &quot;I've done this and it works beautifully&quot; then I'll accept that answer as an expression of joy :-). I'm also interested in any answers that are along the lines of &quot;that's daft, but you can do it like this instead:...&quot;</p>
  <p>Thanks in advance wonderful Civi community!</p>
  
Tags: <smartgroups>
Title: Self referential Smart Group - good idea?
ThreadText: # Self referential Smart Group - good idea?
  
  (Asked by: username_0 on 2020-07-09)
  
  An organisation has local campaign groups. Some of these campaign groups belong to a set we call the "core activist network".
  
  I want the core activist network group to contain:
  
  * all these groups that are manually added
  * all individuals that are related to a local campaign group which is part of the group.
  
  I've set this up as follows:
  
  1. Search for "name contains 'a'" - anything that will return at least one result.
  2. Save the search as a Smart Group.
  3. Add at least one local campaign group to this group manually (Add to Group).
  4. Edit the smart group criteria, remove the silly 'a' criteria, then go to Relationships, choose "Target Contact(s) in group" and enter the name of **this smart group**!
  5. Run the search and from there click Update Smart Group.
  
  Amazingly, this appears to have worked! But is there a reason why this is likely to end up in knots, e.g. some infinite loop? Since calculating the smart group requires...calculating the smart group.
  
  I realise that this is likely to fall foul of SE's "answers likely to be opinion based" but I think this is a valid case. I encourage anyone to upvote any answers that point out potential problems with this situation. Of course, if someone says "I've done this and it works beautifully" then I'll accept that answer as an expression of joy :-). I'm also interested in any answers that are along the lines of "that's daft, but you can do it like this instead:..."
  
  Thanks in advance wonderful Civi community!
  
  **Tags:** smartgroups
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-07-10. Score: 3 | Accepted answer
  
  I've been reading the code, and I *think* this is pretty safe, at least on server with MySQL 5.7.5+ or MariaDB 10.0.2+ (if your database is older than this, it's pretty old).
  
  The code that updates smart groups has various checks in place to make sure it only does each group once in a request.
  
  All the contacts in a smart group are stored in a "cache" table. (A cache is a temporary store of data that can be recreated as needs be, e.g. when updating the smart group). The cache is entirely separate from the manually added/removed contacts; the cache contains and only contains all the contacts in the group, whether they were included manually or because they match the criteria.
  
  The process is:
  
  1. Delete all the cache records for this group. This is important in the example of this question, since it means the search for "contacts whose related contact is in the group" will only be looking at the related contacts that are *manually* included in the group
  2. Creates a temporary table to store new cache entries.
  3. Add to the temporary store by running the search, excluding any that are *manually removed*. In this question: this means we'll be adding the individuals that are related to the local campaign groups, unless they have been manually removed from the group for whatever reason.
  4. Add to the temporary store a copy of all those *manually added*. Of course these contacts may already be in there (from step 2), but that's fine, it ensures every contact is only included once. By now, the temporary store has a copy of everyone that should be in the group. In our example, it means the temporary store now includes:
  
     * All the local campaign groups (and possibly other contacts) that were manually added to the smart group.
     * All the activists related to the local campaign groups, unless they had been manually removed.
  
  ---
  
  But it goes on if this group has any child (sub) groups. This is not relevant to this question but I thought I'd include it for completeness' sake...
  
  4. For each child group, it looks up the contacts in that, exclude any manually excluded from our group, and adds these to our temporary store (again, ignoring any duplicates).
  
     It will also refresh the cache of any child smart groups, so if there was a smart group within the child groups that referenced our main smart group (!) it would not result in an infinite loop because it knows we've just updated [are updating] that, but I'm unclear what contacts it would see because:
  
     * if it goes on the *cache* table, that's empty at this point.
     * if it goes on (manually added + cache table) then it will see the manually added contacts only.
  5. Finally, it empties our group's cache (again? I wonder why...) and copies all the entries from our temporary store into the cache table, and we're done!
  
  So: I think, miraculously, this should actually work and be safe for the purpose stated in my original question!
  
  Please comment, or post other answers though if you have any experience to share.
  
  > **Comments:**
  > **username_0**: Link to related dev [chat thread](https://chat.civicrm.org/civicrm/pl/qxwi64zfd7yy3m6t63qmjdfszw)  
  > **username_1**: It feels relatively safe in that the groups are added statically and those pulled in via relationship are related to those groups. But I'd rely much more on your code analysis!  
  
  ---
  
  