1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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 442 443 444 445 446 447 448 449 450 451 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
|
/*-------------------------------------------------------------------------
*
* pglogical_node.c
* pglogical node and subscription catalog manipulation functions
*
* TODO: caching
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_node.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/genam.h"
#include "access/hash.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "pglogical_node.h"
#include "pglogical_repset.h"
#include "pglogical_worker.h"
#include "pglogical.h"
#define CATALOG_NODE "node"
#define CATALOG_LOCAL_NODE "local_node"
#define CATALOG_NODE_INTERFACE "node_interface"
#define CATALOG_SUBSCRIPTION "subscription"
typedef struct NodeTuple
{
Oid node_id;
NameData node_name;
} NodeTuple;
#define Natts_node 4
#define Anum_node_id 1
#define Anum_node_name 2
#define Natts_local_node 2
#define Anum_node_local_id 1
#define Anum_node_local_node_if 2
typedef struct NodeInterfaceTuple
{
Oid if_id;
NameData if_name;
Oid if_nodeid;
text if_dsn;
} NodeInterfaceTuple;
#define Natts_node_inteface 4
#define Anum_if_id 1
#define Anum_if_name 2
#define Anum_if_nodeid 3
#define Anum_if_dsn 4
typedef struct SubscriptionTuple
{
Oid sub_id;
NameData sub_name;
Oid sub_origin;
Oid sub_target;
Oid sub_origin_if;
Oid sub_target_if;
bool sub_enabled;
NameData sub_slot_name;
} SubscriptionTuple;
#define Natts_subscription 12
#define Anum_sub_id 1
#define Anum_sub_name 2
#define Anum_sub_origin 3
#define Anum_sub_target 4
#define Anum_sub_origin_if 5
#define Anum_sub_target_if 6
#define Anum_sub_enabled 7
#define Anum_sub_slot_name 8
#define Anum_sub_replication_sets 9
#define Anum_sub_forward_origins 10
#define Anum_sub_apply_delay 11
#define Anum_sub_force_text_transfer 12
/*
* We impose same validation rules as replication slot name validation does.
*/
static void
validate_subscription_name(const char *name)
{
const char *cp;
if (strlen(name) == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("subscription name \"%s\" is too short", name)));
if (strlen(name) >= NAMEDATALEN)
ereport(ERROR,
(errcode(ERRCODE_NAME_TOO_LONG),
errmsg("subscription name \"%s\" is too long", name)));
for (cp = name; *cp; cp++)
{
if (!((*cp >= 'a' && *cp <= 'z')
|| (*cp >= '0' && *cp <= '9')
|| (*cp == '_')))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("subscription name \"%s\" contains invalid character",
name),
errhint("Subscription names may only contain lower case "
"letters, numbers, and the underscore character.")));
}
}
}
/*
* Add new node to catalog.
*/
void
create_node(PGLogicalNode *node)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_node];
bool nulls[Natts_node];
NameData node_name;
if (get_node_by_name(node->name, true) != NULL)
elog(ERROR, "node %s already exists", node->name);
/* Generate new id unless one was already specified. */
if (node->id == InvalidOid)
node->id =
DatumGetUInt32(hash_any((const unsigned char *) node->name,
strlen(node->name)));
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_node_id - 1] = ObjectIdGetDatum(node->id);
namestrcpy(&node_name, node->name);
values[Anum_node_name - 1] = NameGetDatum(&node_name);
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, NoLock);
CommandCounterIncrement();
pglogical_subscription_changed(InvalidOid, false);
}
/*
* Delete node from the catalog.
*/
void
drop_node(Oid nodeid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_node_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "node %u not found", nodeid);
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
pglogical_subscription_changed(InvalidOid, false);
}
static PGLogicalNode *
node_fromtuple(HeapTuple tuple)
{
NodeTuple *nodetup = (NodeTuple *) GETSTRUCT(tuple);
PGLogicalNode *node
= (PGLogicalNode *) palloc(sizeof(PGLogicalNode));
node->id = nodetup->node_id;
node->name = pstrdup(NameStr(nodetup->node_name));
return node;
}
/*
* Load the info for specific node.
*/
PGLogicalNode *
get_node(Oid nodeid)
{
PGLogicalNode *node;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_node_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "node %u not found", nodeid);
node = node_fromtuple(tuple);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return node;
}
/*
* Load the info for specific node.
*/
PGLogicalNode *
get_node_by_name(const char *name, bool missing_ok)
{
PGLogicalNode *node;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_node_name,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(name));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return NULL;
}
elog(ERROR, "node %s not found", name);
}
node = node_fromtuple(tuple);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return node;
}
/*
* Add local node record to catalog.
*/
void
create_local_node(Oid nodeid, Oid ifid)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_local_node];
bool nulls[Natts_local_node];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_LOCAL_NODE, -1);
rel = table_openrv(rv, AccessExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* TODO: better error message */
if (get_local_node(false, true))
elog(ERROR, "current database is already configured as pglogical node");
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_node_local_id - 1] = ObjectIdGetDatum(nodeid);
values[Anum_node_local_node_if - 1] = ObjectIdGetDatum(ifid);
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, AccessExclusiveLock);
CommandCounterIncrement();
}
/*
* Drop local node record from catalog.
*/
void
drop_local_node(void)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_LOCAL_NODE, -1);
rel = table_openrv(rv, AccessExclusiveLock);
/* Find the local node tuple. */
scan = systable_beginscan(rel, 0, true, NULL, 0, NULL);
tuple = systable_getnext(scan);
/* No local node record found. */
if (!HeapTupleIsValid(tuple))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("local node not found")));
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
}
/*
* Return local node.
*/
PGLogicalLocalNode *
get_local_node(bool for_update, bool missing_ok)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
TupleDesc desc;
Oid nodeid;
Oid nodeifid;
bool isnull;
PGLogicalLocalNode *res;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_LOCAL_NODE, -1);
rel = table_openrv_extended(rv, for_update ?
ShareUpdateExclusiveLock : RowExclusiveLock,
true);
if (!rel)
{
if (missing_ok)
return NULL;
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("local pglogical node not found")));
}
/* Find the local node tuple. */
scan = systable_beginscan(rel, 0, true, NULL, 0, NULL);
tuple = systable_getnext(scan);
/* No local node record found. */
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
table_close(rel, for_update ?
NoLock : RowExclusiveLock);
return NULL;
}
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("local pglogical node not found")));
}
desc = RelationGetDescr(rel);
nodeid = DatumGetObjectId(fastgetattr(tuple, Anum_node_local_id, desc,
&isnull));
nodeifid = DatumGetObjectId(fastgetattr(tuple, Anum_node_local_node_if,
desc, &isnull));
systable_endscan(scan);
table_close(rel, for_update ? NoLock : RowExclusiveLock);
res = (PGLogicalLocalNode *) palloc(sizeof(PGLogicalLocalNode));
res->node = get_node(nodeid);
res->node_if = get_node_interface(nodeifid);
return res;
}
/*
* Add new node interface to catalog.
*/
void
create_node_interface(PGlogicalInterface *nodeif)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_node_inteface];
bool nulls[Natts_node_inteface];
NameData nodeif_name;
/* Generate new id unless one was already specified. */
if (nodeif->id == InvalidOid)
{
uint32 hashinput[2];
hashinput[0] = nodeif->nodeid;
hashinput[1] = DatumGetUInt32(hash_any((const unsigned char *) nodeif->name,
strlen(nodeif->name)));
nodeif->id = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
(int) sizeof(hashinput)));
}
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE_INTERFACE, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_if_id - 1] = ObjectIdGetDatum(nodeif->id);
namestrcpy(&nodeif_name, nodeif->name);
values[Anum_if_name - 1] = NameGetDatum(&nodeif_name);
values[Anum_if_nodeid - 1] = ObjectIdGetDatum(nodeif->nodeid);
values[Anum_if_dsn - 1] = CStringGetTextDatum(nodeif->dsn);
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, RowExclusiveLock);
CommandCounterIncrement();
}
/*
* Delete node interface from the catalog.
*/
void
drop_node_interface(Oid ifid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE_INTERFACE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_if_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ifid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "node interface %u not found", ifid);
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
}
/*
* Delete all node interfaces from the catalog.
*/
void
drop_node_interfaces(Oid nodeid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE_INTERFACE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_if_nodeid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
/* Remove tuples. */
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
}
/*
* Get the node interface from the catalog.
*/
PGlogicalInterface *
get_node_interface(Oid ifid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
NodeInterfaceTuple *iftup;
PGlogicalInterface *nodeif;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE_INTERFACE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_if_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ifid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "node interface %u not found", ifid);
iftup = (NodeInterfaceTuple *) GETSTRUCT(tuple);
nodeif = (PGlogicalInterface *) palloc(sizeof(PGlogicalInterface));
nodeif->id = iftup->if_id;
nodeif->name = pstrdup(NameStr(iftup->if_name));
nodeif->nodeid = iftup->if_nodeid;
nodeif->dsn = pstrdup(text_to_cstring(&iftup->if_dsn));
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return nodeif;
}
/*
* Get the node interface by name.
*/
PGlogicalInterface *
get_node_interface_by_name(Oid nodeid, const char *name, bool missing_ok)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[2];
NodeInterfaceTuple *iftup;
PGlogicalInterface *nodeif;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_NODE_INTERFACE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for interface record. */
ScanKeyInit(&key[0],
Anum_if_nodeid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
ScanKeyInit(&key[1],
Anum_if_name,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(name));
scan = systable_beginscan(rel, 0, true, NULL, 2, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return NULL;
}
else
elog(ERROR, "node interface \"%s\" not found for nod %u",
name, nodeid);
}
iftup = (NodeInterfaceTuple *) GETSTRUCT(tuple);
nodeif = (PGlogicalInterface *) palloc(sizeof(PGlogicalInterface));
nodeif->id = iftup->if_id;
nodeif->name = pstrdup(NameStr(iftup->if_name));
nodeif->nodeid = iftup->if_nodeid;
nodeif->dsn = pstrdup(text_to_cstring(&iftup->if_dsn));
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return nodeif;
}
/*
* Add new subscription to catalog.
*/
void
create_subscription(PGLogicalSubscription *sub)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_subscription];
bool nulls[Natts_subscription];
NameData sub_name;
NameData sub_slot_name;
/* Validate the new subscription name. */
validate_subscription_name(sub->name);
if (get_subscription_by_name(sub->name, true) != NULL)
elog(ERROR, "subscription %s already exists", sub->name);
/* Generate new id unless one was already specified. */
if (sub->id == InvalidOid)
sub->id =
DatumGetObjectId(hash_any((const unsigned char *) sub->name,
strlen(sub->name)));
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_sub_id - 1] = ObjectIdGetDatum(sub->id);
namestrcpy(&sub_name, sub->name);
values[Anum_sub_name - 1] = NameGetDatum(&sub_name);
values[Anum_sub_origin - 1] = ObjectIdGetDatum(sub->origin_if->nodeid);
values[Anum_sub_target - 1] = ObjectIdGetDatum(sub->target_if->nodeid);
values[Anum_sub_origin_if - 1] = ObjectIdGetDatum(sub->origin_if->id);
values[Anum_sub_target_if - 1] = ObjectIdGetDatum(sub->target_if->id);
values[Anum_sub_enabled - 1] = BoolGetDatum(sub->enabled);
namestrcpy(&sub_slot_name, sub->slot_name);
values[Anum_sub_slot_name - 1] = NameGetDatum(&sub_slot_name);
if (list_length(sub->replication_sets) > 0)
values[Anum_sub_replication_sets - 1] =
PointerGetDatum(strlist_to_textarray(sub->replication_sets));
else
nulls[Anum_sub_replication_sets - 1] = true;
if (list_length(sub->forward_origins) > 0)
values[Anum_sub_forward_origins - 1] =
PointerGetDatum(strlist_to_textarray(sub->forward_origins));
else
nulls[Anum_sub_forward_origins - 1] = true;
if (sub->apply_delay)
values[Anum_sub_apply_delay - 1] = IntervalPGetDatum(sub->apply_delay);
else
nulls[Anum_sub_apply_delay - 1] = true;
values[Anum_sub_force_text_transfer - 1] = BoolGetDatum(sub->force_text_transfer);
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, RowExclusiveLock);
CommandCounterIncrement();
pglogical_subscription_changed(sub->id, true);
}
/*
* Change the subscription tuple.
*/
void
alter_subscription(PGLogicalSubscription *sub)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
SysScanDesc scan;
SubscriptionTuple *oldsub;
HeapTuple oldtup,
newtup;
ScanKeyData key[1];
Datum values[Natts_subscription];
bool nulls[Natts_subscription];
bool replaces[Natts_subscription];
NameData sub_slot_name;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_sub_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(sub->id));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
oldtup = systable_getnext(scan);
if (!HeapTupleIsValid(oldtup))
elog(ERROR, "subscription %u not found", sub->id);
oldsub = (SubscriptionTuple *) GETSTRUCT(oldtup);
if (strcmp(NameStr(oldsub->sub_name), sub->name) != 0)
ereport(LOG,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("subscription name change is not supported")));
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
memset(replaces, true, sizeof(replaces));
replaces[Anum_sub_id - 1] = false;
replaces[Anum_sub_name - 1] = false;
values[Anum_sub_origin - 1] = ObjectIdGetDatum(sub->origin_if->nodeid);
values[Anum_sub_target - 1] = ObjectIdGetDatum(sub->target_if->nodeid);
values[Anum_sub_origin_if - 1] = ObjectIdGetDatum(sub->origin_if->id);
values[Anum_sub_target_if - 1] = ObjectIdGetDatum(sub->target_if->id);
values[Anum_sub_enabled - 1] = BoolGetDatum(sub->enabled);
namestrcpy(&sub_slot_name, sub->slot_name);
values[Anum_sub_slot_name - 1] = NameGetDatum(&sub_slot_name);
if (list_length(sub->replication_sets) > 0)
values[Anum_sub_replication_sets - 1] =
PointerGetDatum(strlist_to_textarray(sub->replication_sets));
else
nulls[Anum_sub_replication_sets - 1] = true;
if (list_length(sub->forward_origins) > 0)
values[Anum_sub_forward_origins - 1] =
PointerGetDatum(strlist_to_textarray(sub->forward_origins));
else
nulls[Anum_sub_forward_origins - 1] = true;
values[Anum_sub_apply_delay - 1] = IntervalPGetDatum(sub->apply_delay);
values[Anum_sub_force_text_transfer - 1] = BoolGetDatum(sub->force_text_transfer);
newtup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
/* Update the tuple in catalog. */
CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
/* Cleanup. */
heap_freetuple(newtup);
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
pglogical_subscription_changed(sub->id, true);
}
/*
* Delete the tuple from subsription catalog.
*/
void
drop_subscription(Oid subid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_sub_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "subscription %u not found", subid);
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, NoLock);
CommandCounterIncrement();
pglogical_subscription_changed(subid, true);
}
static PGLogicalSubscription*
subscription_fromtuple(HeapTuple tuple, TupleDesc desc)
{
SubscriptionTuple *subtup = (SubscriptionTuple *) GETSTRUCT(tuple);
Datum d;
bool isnull;
PGLogicalSubscription *sub =
(PGLogicalSubscription *) palloc(sizeof(PGLogicalSubscription));
sub->id = subtup->sub_id;
sub->name = pstrdup(NameStr(subtup->sub_name));
sub->enabled = subtup->sub_enabled;
sub->slot_name = pstrdup(NameStr(subtup->sub_slot_name));
sub->origin = get_node(subtup->sub_origin);
sub->target = get_node(subtup->sub_target);
sub->origin_if = get_node_interface(subtup->sub_origin_if);
sub->target_if = get_node_interface(subtup->sub_target_if);
/* Get replication sets. */
d = heap_getattr(tuple, Anum_sub_replication_sets, desc, &isnull);
if (isnull)
sub->replication_sets = NIL;
else
{
List *repset_names;
repset_names = textarray_to_list(DatumGetArrayTypeP(d));
sub->replication_sets = repset_names;
}
/* Get origin forwarding. */
d = heap_getattr(tuple, Anum_sub_forward_origins, desc, &isnull);
if (isnull)
sub->forward_origins = NIL;
else
{
List *forward_origin_names;
forward_origin_names = textarray_to_list(DatumGetArrayTypeP(d));
sub->forward_origins = forward_origin_names;
}
/* Get apply_delay. */
d = heap_getattr(tuple, Anum_sub_apply_delay, desc, &isnull);
if (isnull)
sub->apply_delay = NULL;
else
sub->apply_delay = DatumGetIntervalP(d);
/* Get force_text_transfer. */
d = heap_getattr(tuple, Anum_sub_force_text_transfer, desc, &isnull);
if (isnull)
sub->force_text_transfer = false;
else
sub->force_text_transfer = DatumGetBool(d);
return sub;
}
/*
* Load the info for specific subscriber.
*/
PGLogicalSubscription *
get_subscription(Oid subid)
{
PGLogicalSubscription *sub;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
TupleDesc desc;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_sub_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "subscription %u not found", subid);
desc = RelationGetDescr(rel);
sub = subscription_fromtuple(tuple, desc);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return sub;
}
/*
* Load the info for specific subscriber.
*/
PGLogicalSubscription *
get_subscription_by_name(const char *name, bool missing_ok)
{
PGLogicalSubscription *sub;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
TupleDesc desc;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for node record. */
ScanKeyInit(&key[0],
Anum_sub_name,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(name));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return NULL;
}
elog(ERROR, "subscriber %s not found", name);
}
desc = RelationGetDescr(rel);
sub = subscription_fromtuple(tuple, desc);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return sub;
}
/*
* Return all target node subscriptions.
*/
List *
get_node_subscriptions(Oid nodeid, bool origin)
{
PGLogicalSubscription *sub;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
TupleDesc desc;
ScanKeyData key[1];
List *res = NIL;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SUBSCRIPTION, -1);
rel = table_openrv(rv, RowExclusiveLock);
desc = RelationGetDescr(rel);
ScanKeyInit(&key[0],
origin ? Anum_sub_origin : Anum_sub_target,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
sub = subscription_fromtuple(tuple, desc);
res = lappend(res, sub);
}
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return res;
}
|