user44739: I have a wallet that has several hundred small amounts residing at multiple addresses.  I noticed yesterday that, when sending a relatively small amount to a third party that the bitcoind "swept" about 47 of those small amounts together as vin amounts and generated two vout destinations; 1 is the third party, the second is a new address in the wallet (a change address).
What surprised me is that, rather than assembling only the number of fragments necessary for the transfer (plus fee), it swept many of the fragments together to generate a much larger than necessary change transfer.
To give some specifics but to keep it simple:

I initiated a sendtoaddress via the cli.  The amount transferred was 0.004 BTC
Bitcoind used some 47 existing fragments as vin, generating a total value of 0.08+ BTC

Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force sendtoaddress to assemble only the minimal pieces needed?

user5406: Bitcoin Core's coin selection algorithm has had multiple improvements in the past years. More recent versions of Bitcoin Core (0.17.0+) will attempt to select an input set that does not require a change output. If you are on a sufficiently recent version of Bitcoin Core, it should find and prefer such a solution if one is possible.
This means that either the wallet's UTXO pool did not yield such a solution or your version of Bitcoind is older than 0.17.0. In the former case, you may want to use coin control or createrawtransaction  to fall back to manual selection to enforce your selection requirements, in the latter case, you may want to update and try again.
Note that the current difficulty period has had exceptionally low blockspace demand, and it would be an excellent time to consolidate some of your "several hundred small" UTXOs to bigger UTXOs at the minimum feerate of 1 sat/vB. If you were attempting to send at a low feerate, a transaction with 47 inputs might actually save you money in the long run.

user-1: Coin selection algorithm in Bitcoin Core is weird and lot of other users had similar issues in past. Example: https://github.com/bitcoin/bitcoin/issues/20598
There are few open PRs and few PRs that recently got merged which might change few things although I am not sure as I did not review them.
Apart from fees related issues, there can be some privacy related issues as well. Example: https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2021-May/019027.html

Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force sendtoaddress to assemble only the minimal pieces needed?


You should use latest Bitcoin Core
Avoid using coin selection algorithm if possible.
Manually select inputs if using GUI for each transaction
If using CLI and don't want to manually add inputs, at least check transaction details before broadcasting :
bitcoin-cli createrawtransaction "[]" "{\"myaddress\":0.01}"
bitcoin-cli fundrawtransaction "rawtransactionhex"
bitcoin-cli signrawtransactionwithwallet "fundedtransactionhex"

bitcoin-cli decoderawtransaction "hexstring"

If inputs look okay:
bitcoin-cli sendrawtransaction "signedtransactionhex"




44739: <p>I have a wallet that has several hundred small amounts residing at multiple addresses.  I noticed yesterday that, when sending a relatively small amount to a third party that the bitcoind &quot;swept&quot; about 47 of those small amounts together as <code>vin</code> amounts and generated two <code>vout</code> destinations; 1 is the third party, the second is a new address in the wallet (a change address).</p>
<p>What surprised me is that, rather than assembling only the number of fragments necessary for the transfer (plus fee), it swept many of the fragments together to generate a much larger than necessary change transfer.</p>
<p>To give some specifics but to keep it simple:</p>
<ul>
<li>I initiated a <code>sendtoaddress</code> via the cli.  The amount transferred was 0.004 BTC</li>
<li>Bitcoind used some 47 existing fragments as vin, generating a total value of 0.08+ BTC</li>
</ul>
<p>Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force <code>sendtoaddress</code> to assemble only the minimal pieces needed?</p>
 5406: <p>Bitcoin Core's coin selection algorithm has had multiple improvements in the past years. More recent versions of Bitcoin Core (<a href="https://github.com/bitcoin/bitcoin/pull/10637" rel="nofollow noreferrer">0.17.0+</a>) will attempt to select an input set that does not require a change output. If you are on a sufficiently recent version of Bitcoin Core, it should find and prefer such a solution if one is possible.</p>
<p>This means that either the wallet's UTXO pool did not yield such a solution or your version of Bitcoind is older than 0.17.0. In the former case, you may want to use coin control or <code>createrawtransaction</code>  to fall back to manual selection to enforce your selection requirements, in the latter case, you may want to update and try again.</p>
<p>Note that the current difficulty period has had exceptionally low blockspace demand, and it would be an excellent time to consolidate some of your &quot;several hundred small&quot; UTXOs to bigger UTXOs at the minimum feerate of 1 sat/vB. If you were attempting to send at a low feerate, a transaction with 47 inputs might actually save you money in the long run.</p>
 -1: <p>Coin selection algorithm in Bitcoin Core is weird and lot of other users had similar issues in past. Example: <a href="https://github.com/bitcoin/bitcoin/issues/20598" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/issues/20598</a></p>
<p>There are few open PRs and few PRs that recently got merged which might change few things although I am not sure as I did not review them.</p>
<p>Apart from fees related issues, there can be some privacy related issues as well. Example: <a href="https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2021-May/019027.html" rel="nofollow noreferrer">https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2021-May/019027.html</a></p>
<blockquote>
<p>Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force sendtoaddress to assemble only the minimal pieces needed?</p>
</blockquote>
<ol>
<li>You should use latest Bitcoin Core</li>
<li>Avoid using coin selection algorithm if possible.</li>
<li>Manually select inputs if using GUI for each transaction</li>
<li>If using CLI and don't want to manually add inputs, at least check transaction details before broadcasting :
<pre><code>bitcoin-cli createrawtransaction &quot;[]&quot; &quot;{\&quot;myaddress\&quot;:0.01}&quot;
bitcoin-cli fundrawtransaction &quot;rawtransactionhex&quot;
bitcoin-cli signrawtransactionwithwallet &quot;fundedtransactionhex&quot;

bitcoin-cli decoderawtransaction &quot;hexstring&quot;
</code></pre>
If inputs look okay:
<pre><code>bitcoin-cli sendrawtransaction &quot;signedtransactionhex&quot;
</code></pre>
</li>
</ol>
