popoto.backends.postgres.memory¶
popoto.backends.postgres.memory
¶
Ranking and memory state on Postgres (#759 M2a; plan §2 groups D and E).
This module is the Postgres half of Valor's ranking slice: the decay clock
and touch, rank_decayed with base score and confidence modulation,
ConfidenceField's capped-evidence update, AccessTrackerMixin's staged
and confirmed reads, rank_composite for the decay / confidence / access /
sorted arms, and the storage ObservationProtocol and RecallProposal
need. :class:PostgresMemoryOps is mixed into
:class:~popoto.backends.postgres.PostgresBackend.
Storage (plan §3, "Field → column type mapping")¶
DecayingSortedField: the field's owndouble precisioncolumn is the decay clock -- the epoch seconds Redis keeps as the member's sorted-set score.touchupdates it (on Redis the hash keeps the save-time value while the sorted set moves; here a reload sees the touched time, a documented divergence). B-tree(partition cols…, f, _pk COLLATE "C").ConfidenceField: the field's column keeps the model attribute, exactly as the Redis hash does, and four state columns replace the companion hash entry:<f>__conf,<f>__n,<f>__corr,<f>__contra.NULLstate is the seedon_savewrites withHSETNXon Redis (initial_confidenceand three zeros), so a save never has to write it and a re-save can never reset it.AccessTrackerMixin:_access_count,_last_accessed,_staged_readsand_staged_at. Staged reads expire by comparison -- they count only while_staged_atis within_staged_ttl_seconds, which is what the Redis list's refreshedEXPIREmeans. The confirmed access log (a capped Redis list) is not kept; count and last read are.RecallProposal: one engine table per schema,popoto_recall_proposal (model, part, member, surfaced_at), created on first use likepopoto_schema.
No state column is indexed: each index would turn every
update_confidence / confirm_access into a non-HOT update, and no query
filters or orders by one alone (the composite arms scan the model's rows).
The decay expression (the DECAY_SCORE_LUA oracle)¶
Operation for operation, in double precision, so the platform pow
behind power() gives the Lua's bits::
e = greatest((now - f) / 86400, 0.01)
s = sign(b) * |b| * e ^ -rate
s *= greatest(e, 1) ^ -(rate * 2 ^ (s2 * (c0 - c)) - rate) -- modulation
where b is the base-score column (1.0 when unset, missing, NULL
or not numeric, as the script's HGET + cmsgpack rule gives) and
c the clamped stored confidence (c0 when there is none).
Postgres raises value out of range where C's pow and * overflow to
inf or underflow to 0 (power(0.01, -155), 1e-300 * 1e-300).
Every row whose inputs sit in a box where no operation can leave the
double range is scored by the plain expression above. Any other row --
and every row when the call's constants are outside the box -- takes a
correlated scalar subquery that computes the same steps with each power,
* and - clamped: the exponent |y · ln x| is checked against the
double range first, so the result is inf / 0 (with its sign) where the
Lua's is, instead of an error. Inside a band of 0.003 in log space at each
end of the double range (results within 0.25% of DBL_MAX, or between
2.47e-324 and 2.48e-324) the clamp saturates where the exact value
would still be finite; that is the only place the two legs can differ.
Scores come back rounded through %.14g -- what the Lua's tostring
replies with -- so rank_decayed is score-identical to the Redis field
method. Ties break on _pk COLLATE "C" ascending, the script's comparator.
PostgresMemoryOps
¶
Bases: PostgresValidityOps
Protocol groups D/E (memory state, ranking) for
:class:~popoto.backends.postgres.PostgresBackend. Relies on the
backend's _table, _run, transaction and schema.
supersede, chain and the validity adapters come from
:class:~.validity.PostgresValidityOps (M3).
Source code in src/popoto/backends/postgres/memory.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 | |
touch(spec, id, field, *, at, uow=None)
¶
UPDATE … SET f = $at: the decay clock moves, as ZADD moves
the member's score. A record that no longer exists is left alone
(Redis would re-add an orphan member, which ranking then drops).
Source code in src/popoto/backends/postgres/memory.py
update_confidence(spec, id, field, signal, *, uow=None)
¶
CAPPED_BAYESIAN_UPDATE_LUA as one UPDATE … RETURNING.
n_eff = min(n + 1, cap); c' = clamp01(c + (signal - c) /
(n_eff + 1)); one more corroboration when signal >= 0.5, else a
contradiction. Returns the new state (confidence rounded through
%.14g, as the script's reply is), or None when the record does
not exist -- the script's EXISTS guard.
Source code in src/popoto/backends/postgres/memory.py
rank_decayed(spec, field, *, now, n, where=None, as_of=None, decay_rate=None, base_score_field=None, confidence_field=None, validity_field=None, confidence_partition=None)
¶
SELECT _pk, <decay> s … ORDER BY s DESC, _pk COLLATE "C" LIMIT n.
where scopes the scan (the partition top_by_decay resolves);
n=None ranks every record; confidence_field turns modulation
on with Defaults.DECAY_CONFIDENCE_MODULATION_STRENGTH read now,
read from confidence_partition when that field is partitioned.
validity_field gates the scan at as_of (None = now) with
the exclusion rule DECAY_SCORE_LUA applies (M3): a record closed
at or before as_of, or starting after it, is not ranked; a record
with no interval is.
Source code in src/popoto/backends/postgres/memory.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
rank_composite(spec, terms, *, limit, aggregate, min_score, where, as_of, temperature, validity_field=None)
¶
composite_score as one SELECT: each arm is a score over its
own domain (the set its ZUNIONSTORE input would hold), a record is
ranked when any arm holds it, and the aggregate runs over the arms
that do -- SUM in the arms' order, MAX/MIN ignoring the
rest. Ordered as ZREVRANGE orders: score, then _pk descending
(COLLATE "C"). min_score is applied before temperature
divides the scores, as on Redis. validity_field is the validity
mask (M3): a record the exclusion rule drops at as_of (None =
now) is not ranked, whichever arms hold it -- the ZDIFFSTORE the
Redis path applies after the union.
Source code in src/popoto/backends/postgres/memory.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 | |
lua_tostring(value)
¶
value as Lua 5.1's tostring renders it (%.14g), read back:
what every number in a Redis Lua reply has been through.
safe_pow(x, y)
¶
power(x, y) for x > 0 that returns C's inf / 0 where
Postgres would raise (x and y are cheap SQL terms: column
references or literals, each referenced several times).
Source code in src/popoto/backends/postgres/memory.py
safe_mul(a, b)
¶
a * b that saturates to a signed inf / 0 where Postgres
would raise on overflow or underflow.
Source code in src/popoto/backends/postgres/memory.py
safe_sub(a, b)
¶
a - b that saturates to ±inf where Postgres would raise.
Halving is exact for operands of magnitude >= 1e290, and their halved
difference cannot overflow, so the test is exact.
Source code in src/popoto/backends/postgres/memory.py
memory_columns(spec)
¶
State columns for the memory fields and mixins (role "state").
Source code in src/popoto/backends/postgres/memory.py
memory_indexes(spec, index_name)
¶
A (partition cols…, f, _pk COLLATE "C") B-tree per decay clock,
the shape a SortedField gets: it serves the partition filter of a
ranking scan and a range query on the clock.
Source code in src/popoto/backends/postgres/memory.py
partition_match_sql(ts, partition, alias='t')
¶
TRUE for a row in the partition a query names, as literals (the
decay expression carries no parameters). A partitioned
ConfidenceField keeps one companion hash per partition on Redis, and
a query reads the one its filters name (#759 M3): a row outside it reads
as having no confidence data. Empty when there is no partition.
Source code in src/popoto/backends/postgres/memory.py
decay_score_sql(ts, spec, field, *, now, rate, base_field, confidence_field, strength, clamped=False, confidence_partition=None)
¶
The DECAY_SCORE_LUA score of one row of ts (aliased t), as
one SQL expression with no parameters (every constant is an exact
float8 literal). clamped=True returns the clamped expression alone
(the tests compare it with the plain one). confidence_partition is
the partition a partitioned confidence field is read from: a row outside
it modulates as if it had no confidence data (M3).
Source code in src/popoto/backends/postgres/memory.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
partition_where(partition)
¶
AND of field = value for a partition mapping (None when
empty).