If both A and B are on top of the same linestring you can follow these steps.
Use http://postgis.net/docs/ST_LineLocatePoint.html and locate points A and B on the linestring. The result is a fraction of the length of the linestring
By using the two fractional lengths cut the linestring between A and B with https://postgis.net/docs/ST_LineSubstring.html
Finally get the length of the substring with https://postgis.net/docs/ST_Length.html
PostGIS example:
Source geometries:
LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )
POINT ( 206 234 )
POINT ( 420 268 )

Locate A
SELECT ST_LineLocatePoint(
ST_GeomFromText(
'LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )'),
ST_GeomFromText(
'POINT ( 206 234 )'));
Result: 0.2589818067666891
Locate B
SELECT ST_LineLocatePoint(
ST_GeomFromText(
'LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )'),
ST_GeomFromText(
'POINT ( 420 268 )'));
Result: 0.8290726913727895
Query the lenght of the substring with the fractions
SELECT ST_Length(
ST_LineSubstring(
ST_GeomFromText(
'LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )'),
0.2589818067666891,0.8290726913727895));
Result: 530.4905141151486
Check if the result feels reasonable by making a comparison with the total length of the linestring
SELECT ST_Length(
ST_GeomFromText(
'LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )'),
0.2589818067666891,0.8290726913727895);
SELECT ST_Length(
ST_GeomFromText(
'LINESTRING ( 174 425, 230 402, 246 369, 242 309, 218 270, 198 192, 216 156, 257 88, 336 63, 395 66, 429 96, 444 170, 431 216, 412 249, 415 293, 448 329, 478 356, 510 388 )')
);
Result: 930.536741491116
Makes sense.